From f2bf3fc591fd7bd400ec7a5a2165b6f9e30355fb Mon Sep 17 00:00:00 2001 From: Forrest Guice Date: Wed, 11 May 2022 09:19:21 -0700 Subject: [PATCH] export places use storage access framework to choose save destination (api19+) --- .../getfix/ExportPlacesTask.java | 16 +++- .../getfix/PlacesListFragment.java | 73 +++++++++++++------ 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/getfix/ExportPlacesTask.java b/app/src/main/java/com/forrestguice/suntimeswidget/getfix/ExportPlacesTask.java index a0e814660..d47b7e0e2 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/getfix/ExportPlacesTask.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/getfix/ExportPlacesTask.java @@ -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 @@ -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; @@ -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; @@ -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 @@ -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); } diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/getfix/PlacesListFragment.java b/app/src/main/java/com/forrestguice/suntimeswidget/getfix/PlacesListFragment.java index c5a104758..8698cda9a 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/getfix/PlacesListFragment.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/getfix/PlacesListFragment.java @@ -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 @@ -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; @@ -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; @@ -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) { @@ -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 @@ -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() : ""); - 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(); + } } } };