From 88daf1eb577f22f8f936d0a6816ce3a226e95cbd Mon Sep 17 00:00:00 2001 From: Forrest Guice Date: Fri, 22 Apr 2022 13:09:27 -0700 Subject: [PATCH] ExportAlarmsTask export alarm list to json array (#588) --- .../alarmclock/ExportAlarmsTask.java | 88 +++++++++++++++++++ .../alarmclock/ui/AlarmListDialog.java | 84 +++++++++++++++++- 2 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ExportAlarmsTask.java diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ExportAlarmsTask.java b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ExportAlarmsTask.java new file mode 100644 index 000000000..f91d30457 --- /dev/null +++ b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ExportAlarmsTask.java @@ -0,0 +1,88 @@ +/** + Copyright (C) 2022 Forrest Guice + This file is part of SuntimesWidget. + + SuntimesWidget is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SuntimesWidget is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with SuntimesWidget. If not, see . +*/ + +package com.forrestguice.suntimeswidget.alarmclock; + +import android.content.ContentValues; +import android.content.Context; + +import com.forrestguice.suntimeswidget.ExportTask; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Set; + +public class ExportAlarmsTask extends ExportTask +{ + public ExportAlarmsTask(Context context, String exportTarget) + { + super(context, exportTarget); + initTask(); + } + public ExportAlarmsTask(Context context, String exportTarget, boolean useExternalStorage, boolean saveToCache) + { + super(context, exportTarget, useExternalStorage, saveToCache); + initTask(); + } + + private void initTask() + { + ext = ".json"; + mimeType = "text/plain"; + } + + private AlarmClockItem[] items = null; + public void setItems( AlarmClockItem[] values) { + items = values; + } + public AlarmClockItem[] getItems() { + return items; + } + + @Override + protected boolean export(Context context, BufferedOutputStream out) throws IOException + { + if (items != null) + { + numEntries = items.length; + out.write("[".getBytes()); + for (int i=0; i map = new HashMap<>(); + for (String key : values.keySet()) { + map.put(key, values.getAsString(key)); + } + + JSONObject json = new JSONObject(map); + out.write(json.toString().getBytes()); + if (i != items.length-1) { + out.write(", ".getBytes()); + } + } + out.write("]".getBytes()); + return true; + } + return false; + } + +} diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ui/AlarmListDialog.java b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ui/AlarmListDialog.java index 64d03f91c..847dbc78e 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ui/AlarmListDialog.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/ui/AlarmListDialog.java @@ -21,6 +21,7 @@ import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.content.res.TypedArray; import android.database.Cursor; import android.database.DatabaseUtils; @@ -39,6 +40,7 @@ import android.support.design.widget.Snackbar; import android.support.v4.app.DialogFragment; import android.support.v4.content.ContextCompat; +import android.support.v4.content.FileProvider; import android.support.v4.graphics.ColorUtils; import android.support.v4.view.ViewCompat; import android.support.v4.widget.ImageViewCompat; @@ -63,6 +65,7 @@ import android.widget.TextView; import android.widget.Toast; +import com.forrestguice.suntimeswidget.ExportTask; import com.forrestguice.suntimeswidget.R; import com.forrestguice.suntimeswidget.SuntimesUtils; import com.forrestguice.suntimeswidget.alarmclock.AlarmAddon; @@ -72,12 +75,14 @@ import com.forrestguice.suntimeswidget.alarmclock.AlarmNotifications; import com.forrestguice.suntimeswidget.alarmclock.AlarmSettings; import com.forrestguice.suntimeswidget.alarmclock.AlarmState; +import com.forrestguice.suntimeswidget.alarmclock.ExportAlarmsTask; import com.forrestguice.suntimeswidget.calculator.core.Location; import com.forrestguice.suntimeswidget.settings.AppSettings; import com.forrestguice.suntimeswidget.settings.SolarEventIcons; import com.forrestguice.suntimeswidget.settings.SolarEvents; import com.forrestguice.suntimeswidget.settings.WidgetSettings; +import java.io.File; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Calendar; @@ -434,11 +439,86 @@ public void onFinished(Boolean result, AlarmClockItem item) return alarm; } - public void exportAlarms(Context context) + //////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////// + + public boolean exportAlarms(Context context) { - Toast.makeText(context, "TODO: export", Toast.LENGTH_SHORT).show(); // TODO + if (exportTask != null) { + Log.e("ExportAlarms", "Already busy importing/exporting! ignoring request"); + return false; + } + + AlarmListDialogAdapter adapter = getAdapter(); + if (context != null && adapter != null) + { + AlarmClockItem[] items = adapter.getItems().toArray(new AlarmClockItem[0]); + if (items.length > 0) + { + exportTask = new ExportAlarmsTask(context, "SuntimesAlarms", true, true); // export to external cache + exportTask.setItems(items); + exportTask.setTaskListener(exportListener); + exportTask.execute(); + return true; + + } else return false; + } + return false; } + protected ExportAlarmsTask exportTask = null; + private ExportTask.TaskListener exportListener = new ExportTask.TaskListener() + { + public void onStarted() + { + setRetainInstance(true); + if (progress != null) { + progress.setVisibility(View.VISIBLE); + } + } + + @Override + public void onFinished(ExportAlarmsTask.ExportResult results) + { + setRetainInstance(false); + exportTask = null; + if (progress != null) { + progress.setVisibility(View.GONE); + } + + if (results.getResult()) + { + Intent shareIntent = new Intent(); + shareIntent.setAction(Intent.ACTION_SEND); + shareIntent.setType(results.getMimeType()); + shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + + try { + Uri shareURI = FileProvider.getUriForFile(getActivity(), ExportTask.FILE_PROVIDER_AUTHORITY, results.getExportFile()); + shareIntent.putExtra(Intent.EXTRA_STREAM, shareURI); + startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.msg_export_to))); + + if (isAdded()) { + String successMessage = getString(R.string.msg_export_success, results.getExportFile().getAbsolutePath()); + Toast.makeText(getActivity(), successMessage, Toast.LENGTH_LONG).show(); + } + return; // successful export ends here... + + } catch (Exception e) { + Log.e("ExportAlarms", "Failed to share file URI! " + e); + } + } + + if (isAdded()) + { + File file = results.getExportFile(); // export failed + String path = ((file != null) ? file.getAbsolutePath() : ""); + String failureMessage = getString(R.string.msg_export_failure, path); + Toast.makeText(getActivity(), failureMessage, Toast.LENGTH_LONG).show(); + } + } + }; + public void importAlarms(Context context) { Toast.makeText(context, "TODO: import", Toast.LENGTH_SHORT).show(); // TODO