Skip to content

Commit

Permalink
export places
Browse files Browse the repository at this point in the history
use storage access framework to choose save destination (api19+)
  • Loading branch information
forrestguice committed May 11, 2022
1 parent 3ba4fb3 commit f2bf3fc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Copyright (C) 2017-2019 Forrest Guice
Copyright (C) 2017-2022 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
Expand All @@ -22,6 +22,7 @@
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.util.Log;

import com.forrestguice.suntimeswidget.ExportTask;
Expand All @@ -32,6 +33,9 @@

public class ExportPlacesTask extends ExportTask
{
public static final String FILEEXT = ".csv";
public static final String MIMETYPE = "text/csv";

private Cursor cursor;
private GetFixDatabaseAdapter db;

Expand All @@ -45,11 +49,16 @@ public ExportPlacesTask(Context context, String exportTarget, boolean useExterna
super(context, exportTarget, useExternalStorage, saveToCache);
initTask();
}
public ExportPlacesTask(Context context, Uri uri)
{
super(context, uri);
initTask();
}

private void initTask()
{
ext = ".csv";
mimeType = "text/csv";
ext = FILEEXT;
mimeType = MIMETYPE;
}

@Override
Expand All @@ -59,7 +68,6 @@ public boolean export( Context context, BufferedOutputStream out ) throws IOExce
db = new GetFixDatabaseAdapter(context.getApplicationContext());
db.open();
numEntries = db.getPlaceCount();
out = new BufferedOutputStream(new FileOutputStream(exportFile));
cursor = db.getAllPlaces(-1, true);
return exportDatabase(db, cursor, out);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Copyright (C) 2014-2020 Forrest Guice
Copyright (C) 2014-2022 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -35,7 +35,6 @@
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.FileProvider;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
Expand Down Expand Up @@ -82,6 +81,7 @@ public class PlacesListFragment extends Fragment
public static final String DIALOG_EDITPLACE = "placedialog";

public static final int IMPORT_REQUEST = 100;
public static final int EXPORT_REQUEST = 200;

protected FragmentListener listener;
protected PlacesListAdapter adapter;
Expand Down Expand Up @@ -160,6 +160,16 @@ public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case EXPORT_REQUEST:
if (resultCode == Activity.RESULT_OK)
{
Uri uri = (data != null ? data.getData() : null);
if (uri != null) {
exportPlaces(getActivity(), uri);
}
}
break;

case IMPORT_REQUEST:
if (resultCode == Activity.RESULT_OK)
{
Expand Down Expand Up @@ -938,10 +948,28 @@ public boolean importPlaces(Context context, @NonNull Uri uri)

public void exportPlaces(Context context)
{
ExportPlacesTask task = new ExportPlacesTask(context, "SuntimesPlaces", true, true); // export to external cache
String exportTarget = "SuntimesPlaces";
if (Build.VERSION.SDK_INT >= 19)
{
String filename = exportTarget + ExportPlacesTask.FILEEXT;
Intent intent = ExportTask.getCreateFileIntent(filename, ExportPlacesTask.MIMETYPE);
startActivityForResult(intent, EXPORT_REQUEST);

} else {
ExportPlacesTask task = new ExportPlacesTask(context, exportTarget, true, true); // export to external cache
task.setTaskListener(exportPlacesListener);
task.execute();
}
}

public void exportPlaces(Context context, @NonNull Uri uri)
{
Log.i("exportPlaces", "Starting export task: " + uri);
ExportPlacesTask task = new ExportPlacesTask(context, uri);
task.setTaskListener(exportPlacesListener);
task.execute();
}

private ExportPlacesTask.TaskListener exportPlacesListener = new ExportPlacesTask.TaskListener()
{
@Override
Expand All @@ -963,34 +991,31 @@ public void onFinished(ExportPlacesTask.ExportResult results)
Context context = getActivity();
if (context != null)
{
File file = results.getExportFile();
String path = ((file != null) ? file.getAbsolutePath()
: ExportTask.getFileName(context.getContentResolver(), results.getExportUri()));

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 = Uri.fromFile(results.getExportFile()); // this URI works until api26 (throws FileUriExposedException)
Uri shareURI = FileProvider.getUriForFile(context, ExportTask.FILE_PROVIDER_AUTHORITY, results.getExportFile());
shareIntent.putExtra(Intent.EXTRA_STREAM, shareURI);

String successMessage = context.getString(R.string.msg_export_success, results.getExportFile().getAbsolutePath());
if (isAdded()) {
String successMessage = context.getString(R.string.msg_export_success, path);
Toast.makeText(context, successMessage, Toast.LENGTH_LONG).show();

context.startActivity(Intent.createChooser(shareIntent, context.getResources().getText(R.string.msg_export_to)));
return; // successful export ends here...

} catch (Exception e) {
Log.e("ExportPlaces", "Failed to share file URI! " + e);
}

if (Build.VERSION.SDK_INT >= 19) {
if (results.getExportUri() == null) {
ExportTask.shareResult(context, file, results.getMimeType());
}
} else {
ExportTask.shareResult(context, file, results.getMimeType());
}
return;
}

File file = results.getExportFile(); // export failed
String path = ((file != null) ? file.getAbsolutePath() : "<path>");
String failureMessage = context.getString(R.string.msg_export_failure, path);
Toast.makeText(context, failureMessage, Toast.LENGTH_LONG).show();
if (isAdded()) {
String failureMessage = context.getString(R.string.msg_export_failure, path);
Toast.makeText(context, failureMessage, Toast.LENGTH_LONG).show();
}
}
}
};
Expand Down

0 comments on commit f2bf3fc

Please sign in to comment.