Skip to content

Commit

Permalink
ExportAlarmsTask
Browse files Browse the repository at this point in the history
export alarm list to json array (#588)
  • Loading branch information
forrestguice committed Apr 22, 2022
1 parent e113e74 commit 88daf1e
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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<items.length; i++)
{
ContentValues values = items[i].asContentValues(true);
HashMap<String,String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() : "<path>");
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
Expand Down

0 comments on commit 88daf1e

Please sign in to comment.