From b3c744b872eb3f233112f5b1fa4df48c62608921 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 4 Jun 2019 15:19:19 -0300 Subject: [PATCH] Store backup folder Uri in default shared preferences and keep using it as long as it continues to be available. --- .../backup/activity/MainActivity.java | 2 +- .../activity/MainActivityController.java | 39 ++++++++++++++++--- .../backup/settings/SettingsManager.java | 27 +++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.java diff --git a/app/src/main/java/com/stevesoltys/backup/activity/MainActivity.java b/app/src/main/java/com/stevesoltys/backup/activity/MainActivity.java index 43dfba94..a14f8830 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/MainActivity.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/MainActivity.java @@ -34,7 +34,7 @@ public class MainActivity extends Activity implements View.OnClickListener { switch (viewId) { case R.id.create_backup_button: - controller.showChooseFolderActivity(this); + controller.onBackupButtonClicked(this); break; case R.id.restore_backup_button: diff --git a/app/src/main/java/com/stevesoltys/backup/activity/MainActivityController.java b/app/src/main/java/com/stevesoltys/backup/activity/MainActivityController.java index c4155c77..7eb084aa 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/MainActivityController.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/MainActivityController.java @@ -25,6 +25,8 @@ import static android.provider.DocumentsContract.buildDocumentUriUsingTree; import static android.provider.DocumentsContract.createDocument; import static android.provider.DocumentsContract.getTreeDocumentId; import static com.stevesoltys.backup.activity.MainActivity.OPEN_DOCUMENT_TREE_REQUEST_CODE; +import static com.stevesoltys.backup.settings.SettingsManager.getBackupFolderUri; +import static com.stevesoltys.backup.settings.SettingsManager.setBackupFolderUri; /** * @author Steve Soltys @@ -35,7 +37,22 @@ class MainActivityController { private static final String DOCUMENT_MIME_TYPE = "application/octet-stream"; private static final String DOCUMENT_SUFFIX = "yyyy-MM-dd_HH_mm_ss"; - void showChooseFolderActivity(Activity parent) { + void onBackupButtonClicked(Activity parent) { + Uri folderUri = getBackupFolderUri(parent); + if (folderUri == null) { + showChooseFolderActivity(parent); + } else { + try { + Uri fileUri = createBackupFile(parent.getContentResolver(), folderUri); + showCreateBackupActivity(parent, fileUri); + } catch (IOException e) { + e.printStackTrace(); + showChooseFolderActivity(parent); + } + } + } + + private void showChooseFolderActivity(Activity parent) { Intent createDocumentIntent = new Intent(ACTION_OPEN_DOCUMENT_TREE); createDocumentIntent.addFlags(FLAG_GRANT_PERSISTABLE_URI_PERMISSION | FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); @@ -77,7 +94,10 @@ class MainActivityController { (FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); contentResolver.takePersistableUriPermission(folderUri, takeFlags); - // create backup file in folder + // store backup folder location in settings + setBackupFolderUri(parent, folderUri); + + // create a new backup file in folder Uri fileUri; try { fileUri = createBackupFile(contentResolver, folderUri); @@ -88,6 +108,10 @@ class MainActivityController { return; } + showCreateBackupActivity(parent, fileUri); + } + + private void showCreateBackupActivity(Activity parent, Uri fileUri) { Intent intent = new Intent(parent, CreateBackupActivity.class); intent.setData(fileUri); parent.startActivity(intent); @@ -106,9 +130,14 @@ class MainActivityController { private Uri createBackupFile(ContentResolver contentResolver, Uri folderUri) throws IOException { Uri documentUri = buildDocumentUriUsingTree(folderUri, getTreeDocumentId(folderUri)); - Uri fileUri = createDocument(contentResolver, documentUri, DOCUMENT_MIME_TYPE, getBackupFileName()); - if (fileUri == null) throw new IOException(); - return fileUri; + try { + Uri fileUri = createDocument(contentResolver, documentUri, DOCUMENT_MIME_TYPE, getBackupFileName()); + if (fileUri == null) throw new IOException(); + return fileUri; + } catch (SecurityException e) { + // happens when folder was deleted and thus Uri permission don't exist anymore + throw new IOException(e); + } } private String getBackupFileName() { diff --git a/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.java b/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.java new file mode 100644 index 00000000..11624464 --- /dev/null +++ b/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.java @@ -0,0 +1,27 @@ +package com.stevesoltys.backup.settings; + +import android.annotation.Nullable; +import android.content.Context; +import android.net.Uri; + +import static android.preference.PreferenceManager.getDefaultSharedPreferences; + +public class SettingsManager { + + private static final String PREF_KEY_BACKUP_URI = "backupUri"; + + public static void setBackupFolderUri(Context context, Uri uri) { + getDefaultSharedPreferences(context) + .edit() + .putString(PREF_KEY_BACKUP_URI, uri.toString()) + .apply(); + } + + @Nullable + public static Uri getBackupFolderUri(Context context) { + String uriStr = getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_URI, null); + if (uriStr == null) return null; + return Uri.parse(uriStr); + } + +}