From 9767e98e50e14bfa824572a2be5f7967737f2e2a Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Sun, 20 Jun 2021 09:17:55 +0200 Subject: [PATCH 1/3] Remove option to immediately commit pref changes on import System is now not restarted with `System.exit(0)`. Instead it is done properly by finishing the activity and restarting the activity. This allows preference changes which are queued up asynchronously through `apply` to be applied. --- .../newpipe/settings/ContentSettingsFragment.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 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 b66300759..0d67c9a48 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -199,7 +199,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { .getDefaultSharedPreferences(requireContext()); manager.exportDatabase(preferences, file); - saveLastImportExportDataUri(false); // save export path only on success + saveLastImportExportDataUri(); // save export path only on success Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e); @@ -252,8 +252,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment { * Save import path and restart system. */ private void finishImport() { - // save import path only on success; save immediately because app is about to exit - saveLastImportExportDataUri(true); + // save import path only on success + saveLastImportExportDataUri(); // restart app to properly load db NavigationHelper.restartApp(requireActivity()); } @@ -263,16 +263,11 @@ public class ContentSettingsFragment extends BasePreferenceFragment { return isBlank(path) ? null : Uri.parse(path); } - private void saveLastImportExportDataUri(final boolean immediately) { + private void saveLastImportExportDataUri() { if (lastImportExportDataUri != null) { final SharedPreferences.Editor editor = defaultPreferences.edit() .putString(importExportDataPathKey, lastImportExportDataUri.toString()); - if (immediately) { - // noinspection ApplySharedPref - editor.commit(); // app about to be restarted, commit immediately - } else { - editor.apply(); - } + editor.apply(); } } } From 0bdf8de38e38924d7b9f0d862821306a829e7c74 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Sun, 20 Jun 2021 09:22:43 +0200 Subject: [PATCH 2/3] Resolve sonar issues in ContentSettingsFragment https://sonarcloud.io/organizations/teamnewpipe/rules?open=java%3AS2885&rule_key=java%3AS2885 https://sonarcloud.io/organizations/teamnewpipe/rules?open=java%3AS112&rule_key=java%3AS112 --- .../newpipe/settings/ContentSettingsFragment.java | 11 +++++++---- 1 file changed, 7 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 0d67c9a48..ce462982a 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -33,6 +33,7 @@ import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.ZipHelper; import java.io.File; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; @@ -43,7 +44,8 @@ import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public class ContentSettingsFragment extends BasePreferenceFragment { private static final String ZIP_MIME_TYPE = "application/zip"; - private static final SimpleDateFormat EXPORT_DATE_FORMAT + + private final SimpleDateFormat exportDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); private ContentSettingsManager manager; @@ -52,7 +54,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment { private String thumbnailLoadToggleKey; private String youtubeRestrictedModeEnabledKey; - @Nullable private Uri lastImportExportDataUri = null; + @Nullable + private Uri lastImportExportDataUri = null; private Localization initialSelectedLocalization; private ContentCountry initialSelectedContentCountry; private String initialLanguage; @@ -86,7 +89,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { requestExportPathLauncher.launch( StoredFileHelper.getNewPicker(requireContext(), - "NewPipeData-" + EXPORT_DATE_FORMAT.format(new Date()) + ".zip", + "NewPipeData-" + exportDateFormat.format(new Date()) + ".zip", ZIP_MIME_TYPE, getImportExportDataUri())); return true; }); @@ -216,7 +219,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { try { if (!manager.ensureDbDirectoryExists()) { - throw new Exception("Could not create databases dir"); + throw new IOException("Could not create databases dir"); } if (!manager.extractDb(file)) { From 785c0376f8a9fa4b2170ff582d4b8cbe20c5771f Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Sun, 20 Jun 2021 09:28:39 +0200 Subject: [PATCH 3/3] Remove variable ContentSettingsFragment.lastImportExportDataUri Instead pass the value through the methods as parameter --- .../settings/ContentSettingsFragment.java | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 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 ce462982a..f1e19af94 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -12,7 +12,6 @@ import android.widget.Toast; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; -import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import androidx.preference.Preference; @@ -54,8 +53,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment { private String thumbnailLoadToggleKey; private String youtubeRestrictedModeEnabledKey; - @Nullable - private Uri lastImportExportDataUri = null; private Localization initialSelectedLocalization; private ContentCountry initialSelectedContentCountry; private String initialLanguage; @@ -165,19 +162,21 @@ public class ContentSettingsFragment extends BasePreferenceFragment { private void requestExportPathResult(final ActivityResult result) { assureCorrectAppLanguage(getContext()); if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { - lastImportExportDataUri = result.getData().getData(); // will be saved only on success + // will be saved only on success + final Uri lastExportDataUri = result.getData().getData(); final StoredFileHelper file = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); - exportDatabase(file); + exportDatabase(file, lastExportDataUri); } } private void requestImportPathResult(final ActivityResult result) { assureCorrectAppLanguage(getContext()); if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { - lastImportExportDataUri = result.getData().getData(); // will be saved only on success + // will be saved only on success + final Uri lastImportDataUri = result.getData().getData(); final StoredFileHelper file = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); @@ -185,7 +184,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { new AlertDialog.Builder(requireActivity()) .setMessage(R.string.override_current_data) .setPositiveButton(R.string.finish, (d, id) -> - importDatabase(file)) + importDatabase(file, lastImportDataUri)) .setNegativeButton(R.string.cancel, (d, id) -> d.cancel()) .create() @@ -193,27 +192,27 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - private void exportDatabase(final StoredFileHelper file) { + private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri) { try { //checkpoint before export NewPipeDatabase.checkpoint(); final SharedPreferences preferences = PreferenceManager - .getDefaultSharedPreferences(requireContext()); + .getDefaultSharedPreferences(requireContext()); manager.exportDatabase(preferences, file); - saveLastImportExportDataUri(); // save export path only on success + saveLastImportExportDataUri(exportDataUri); // save export path only on success Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e); } } - private void importDatabase(final StoredFileHelper file) { + private void importDatabase(final StoredFileHelper file, final Uri importDataUri) { // check if file is supported if (!ZipHelper.isValidZipFile(file)) { Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT) - .show(); + .show(); return; } @@ -234,17 +233,17 @@ public class ContentSettingsFragment extends BasePreferenceFragment { alert.setNegativeButton(android.R.string.no, (dialog, which) -> { dialog.dismiss(); - finishImport(); + finishImport(importDataUri); }); alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> { dialog.dismiss(); manager.loadSharedPreferences(PreferenceManager - .getDefaultSharedPreferences(requireContext())); - finishImport(); + .getDefaultSharedPreferences(requireContext())); + finishImport(importDataUri); }); alert.show(); } else { - finishImport(); + finishImport(importDataUri); } } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); @@ -253,10 +252,12 @@ public class ContentSettingsFragment extends BasePreferenceFragment { /** * Save import path and restart system. + * + * @param importDataUri The import path to save */ - private void finishImport() { + private void finishImport(final Uri importDataUri) { // save import path only on success - saveLastImportExportDataUri(); + saveLastImportExportDataUri(importDataUri); // restart app to properly load db NavigationHelper.restartApp(requireActivity()); } @@ -266,11 +267,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment { return isBlank(path) ? null : Uri.parse(path); } - private void saveLastImportExportDataUri() { - if (lastImportExportDataUri != null) { - final SharedPreferences.Editor editor = defaultPreferences.edit() - .putString(importExportDataPathKey, lastImportExportDataUri.toString()); - editor.apply(); - } + private void saveLastImportExportDataUri(final Uri importExportDataUri) { + final SharedPreferences.Editor editor = defaultPreferences.edit() + .putString(importExportDataPathKey, importExportDataUri.toString()); + editor.apply(); } }