From fd4408e572f5c5aacf5c74ace91ef7c1ed46c474 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Mon, 31 May 2021 12:36:21 +0200 Subject: [PATCH 1/4] Set ImportExportDataPath only on successful export --- .../settings/ContentSettingsFragment.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 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 ab6ff7414..ef1558186 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -25,8 +25,8 @@ import org.schabi.newpipe.error.ReCaptchaActivity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.localization.Localization; -import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.FilePathUtils; +import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.ZipHelper; import java.io.File; @@ -178,11 +178,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment { && resultCode == Activity.RESULT_OK && data.getData() != null) { final File file = Utils.getFileForUri(data.getData()); final String path = file.getAbsolutePath(); - setImportExportDataPath(file); if (requestCode == REQUEST_EXPORT_PATH) { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); - exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip"); + exportDatabase(file); } else { final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); builder.setMessage(R.string.override_current_data) @@ -195,15 +193,21 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - private void exportDatabase(final String path) { + private void exportDatabase(final File folder) { try { + final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); + final String path = folder.getAbsolutePath() + "/NewPipeData-" + + sdf.format(new Date()) + ".zip"; + //checkpoint before export NewPipeDatabase.checkpoint(); final SharedPreferences preferences = PreferenceManager - .getDefaultSharedPreferences(requireContext()); + .getDefaultSharedPreferences(requireContext()); manager.exportDatabase(preferences, path); + setImportExportDataPath(folder); + Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e); 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 2/4] 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()) { From 05eb0d0fbe7ffc13d6b9a431d0825c6ce6c8dc79 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Mon, 31 May 2021 12:40:14 +0200 Subject: [PATCH 3/4] Commit path immediately when import backup --- .../settings/ContentSettingsFragment.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 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 c77ca034f..7b2b1aeb5 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.settings; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -205,7 +206,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { .getDefaultSharedPreferences(requireContext()); manager.exportDatabase(preferences, path); - setImportExportDataPath(folder); + setImportExportDataPath(folder, false); Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); } catch (final Exception e) { @@ -230,7 +231,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { if (!manager.extractDb(filePath)) { Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG) - .show(); + .show(); } //If settings file exist, ask if it should be imported. @@ -264,14 +265,16 @@ public class ContentSettingsFragment extends BasePreferenceFragment { */ private void finishImport(final File file) { if (file.getParentFile() != null) { - setImportExportDataPath(file.getParentFile()); + //immediately because app is about to exit + setImportExportDataPath(file.getParentFile(), true); } // restart app to properly load db System.exit(0); } - private void setImportExportDataPath(final File file) { + @SuppressLint("ApplySharedPref") + private void setImportExportDataPath(final File file, final boolean immediately) { final String directoryPath; if (file.isDirectory()) { directoryPath = file.getAbsolutePath(); @@ -283,6 +286,13 @@ public class ContentSettingsFragment extends BasePreferenceFragment { directoryPath = ""; } } - defaultPreferences.edit().putString(importExportDataPathKey, directoryPath).apply(); + final SharedPreferences.Editor editor = defaultPreferences + .edit() + .putString(importExportDataPathKey, directoryPath); + if (immediately) { + editor.commit(); + } else { + editor.apply(); + } } } From 004907d306f84bbe800b624e9af650f73989652b Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Mon, 31 May 2021 15:09:57 +0200 Subject: [PATCH 4/4] Annotate methode parameters as NonNull --- .../schabi/newpipe/settings/ContentSettingsFragment.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 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 7b2b1aeb5..9af3666a6 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -193,7 +193,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - private void exportDatabase(final File folder) { + private void exportDatabase(@NonNull final File folder) { try { final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); final String path = folder.getAbsolutePath() + "/NewPipeData-" @@ -214,7 +214,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - private void importDatabase(final File file) { + private void importDatabase(@NonNull final File file) { final String filePath = file.getAbsolutePath(); // check if file is supported @@ -263,7 +263,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { * * @param file The file of the created backup */ - private void finishImport(final File file) { + private void finishImport(@NonNull final File file) { if (file.getParentFile() != null) { //immediately because app is about to exit setImportExportDataPath(file.getParentFile(), true); @@ -274,7 +274,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } @SuppressLint("ApplySharedPref") - private void setImportExportDataPath(final File file, final boolean immediately) { + private void setImportExportDataPath(@NonNull final File file, final boolean immediately) { final String directoryPath; if (file.isDirectory()) { directoryPath = file.getAbsolutePath();