From f13a1b04e64d01d2337662b724ddff4b9ede54fc Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Mon, 31 May 2021 12:38:21 +0200 Subject: [PATCH] Set ImportExportDataPath only on successful import Also set the folder instead of the file itself as path --- .../settings/ContentSettingsFragment.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index ef1558186..c77ca034f 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -177,7 +177,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment { if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) && resultCode == Activity.RESULT_OK && data.getData() != null) { final File file = Utils.getFileForUri(data.getData()); - final String path = file.getAbsolutePath(); if (requestCode == REQUEST_EXPORT_PATH) { exportDatabase(file); @@ -185,7 +184,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); builder.setMessage(R.string.override_current_data) .setPositiveButton(getString(R.string.finish), - (d, id) -> importDatabase(path)) + (d, id) -> importDatabase(file)) .setNegativeButton(android.R.string.cancel, (d, id) -> d.cancel()); builder.create().show(); @@ -214,11 +213,13 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - private void importDatabase(final String filePath) { + private void importDatabase(final File file) { + final String filePath = file.getAbsolutePath(); + // check if file is supported if (!ZipHelper.isValidZipFile(filePath)) { Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT) - .show(); + .show(); return; } @@ -239,26 +240,37 @@ public class ContentSettingsFragment extends BasePreferenceFragment { alert.setNegativeButton(android.R.string.no, (dialog, which) -> { dialog.dismiss(); - // restart app to properly load db - System.exit(0); + finishImport(file); }); alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> { dialog.dismiss(); manager.loadSharedPreferences(PreferenceManager - .getDefaultSharedPreferences(requireContext())); - // restart app to properly load db - System.exit(0); + .getDefaultSharedPreferences(requireContext())); + finishImport(file); }); alert.show(); } else { - // restart app to properly load db - System.exit(0); + finishImport(file); } } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); } } + /** + * Save import path and restart system. + * + * @param file The file of the created backup + */ + private void finishImport(final File file) { + if (file.getParentFile() != null) { + setImportExportDataPath(file.getParentFile()); + } + + // restart app to properly load db + System.exit(0); + } + private void setImportExportDataPath(final File file) { final String directoryPath; if (file.isDirectory()) {