From d8668ed226dccf36666fc325204acdb29a078f6d Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 27 Mar 2024 15:02:27 +0100 Subject: [PATCH] Show snackbar error when settings import fails --- .../org/schabi/newpipe/error/UserAction.java | 1 + .../BackupRestoreSettingsFragment.java | 17 ++++- .../settings/export/ImportExportManager.kt | 69 ++++++++----------- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/error/UserAction.java b/app/src/main/java/org/schabi/newpipe/error/UserAction.java index c8701cd77..6ca66e0d2 100644 --- a/app/src/main/java/org/schabi/newpipe/error/UserAction.java +++ b/app/src/main/java/org/schabi/newpipe/error/UserAction.java @@ -6,6 +6,7 @@ package org.schabi.newpipe.error; public enum UserAction { USER_REPORT("user report"), UI_ERROR("ui error"), + DATABASE_IMPORT_EXPORT("database import or export"), SUBSCRIPTION_CHANGE("subscription change"), SUBSCRIPTION_UPDATE("subscription update"), SUBSCRIPTION_GET("get subscription"), diff --git a/app/src/main/java/org/schabi/newpipe/settings/BackupRestoreSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/BackupRestoreSettingsFragment.java index 842023e50..1d00ef287 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/BackupRestoreSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/BackupRestoreSettingsFragment.java @@ -23,7 +23,9 @@ import androidx.preference.PreferenceManager; import org.schabi.newpipe.NewPipeDatabase; import org.schabi.newpipe.R; +import org.schabi.newpipe.error.ErrorInfo; import org.schabi.newpipe.error.ErrorUtil; +import org.schabi.newpipe.error.UserAction; import org.schabi.newpipe.settings.export.ImportExportManager; import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard; import org.schabi.newpipe.streams.io.StoredFileHelper; @@ -166,7 +168,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment { Toast.makeText(requireContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT) .show(); } catch (final Exception e) { - ErrorUtil.showUiErrorSnackbar(this, "Exporting database", e); + showErrorSnackbar(e, "Exporting database and settings"); } } @@ -202,7 +204,12 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment { final Context context = requireContext(); final SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(context); - manager.loadSharedPreferences(prefs); + try { + manager.loadSharedPreferences(prefs); + } catch (IOException | ClassNotFoundException e) { + showErrorSnackbar(e, "Importing preferences"); + return; + } cleanImport(context, prefs); finishImport(importDataUri); }) @@ -211,7 +218,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment { finishImport(importDataUri); } } catch (final Exception e) { - ErrorUtil.showUiErrorSnackbar(this, "Importing database", e); + showErrorSnackbar(e, "Importing database and settings"); } } @@ -269,4 +276,8 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment { .putString(importExportDataPathKey, importExportDataUri.toString()); editor.apply(); } + + private void showErrorSnackbar(final Throwable e, final String request) { + ErrorUtil.showSnackbar(this, new ErrorInfo(e, UserAction.DATABASE_IMPORT_EXPORT, request)); + } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt b/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt index 298841c5f..b4503bdd6 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt @@ -73,50 +73,41 @@ class ImportExportManager(private val fileLocator: NewPipeFileLocator) { /** * Remove all shared preferences from the app and load the preferences supplied to the manager. */ + @Throws(IOException::class, ClassNotFoundException::class) fun loadSharedPreferences(preferences: SharedPreferences) { - try { - val preferenceEditor = preferences.edit() + val preferenceEditor = preferences.edit() - PreferencesObjectInputStream( - fileLocator.settings.inputStream() - ).use { input -> - preferenceEditor.clear() - @Suppress("UNCHECKED_CAST") - val entries = input.readObject() as Map - for ((key, value) in entries) { - when (value) { - is Boolean -> { - preferenceEditor.putBoolean(key, value) - } - is Float -> { - preferenceEditor.putFloat(key, value) - } - is Int -> { - preferenceEditor.putInt(key, value) - } - is Long -> { - preferenceEditor.putLong(key, value) - } - is String -> { - preferenceEditor.putString(key, value) - } - is Set<*> -> { - // There are currently only Sets with type String possible - @Suppress("UNCHECKED_CAST") - preferenceEditor.putStringSet(key, value as Set?) - } + PreferencesObjectInputStream( + fileLocator.settings.inputStream() + ).use { input -> + preferenceEditor.clear() + @Suppress("UNCHECKED_CAST") + val entries = input.readObject() as Map + for ((key, value) in entries) { + when (value) { + is Boolean -> { + preferenceEditor.putBoolean(key, value) + } + is Float -> { + preferenceEditor.putFloat(key, value) + } + is Int -> { + preferenceEditor.putInt(key, value) + } + is Long -> { + preferenceEditor.putLong(key, value) + } + is String -> { + preferenceEditor.putString(key, value) + } + is Set<*> -> { + // There are currently only Sets with type String possible + @Suppress("UNCHECKED_CAST") + preferenceEditor.putStringSet(key, value as Set?) } } - preferenceEditor.commit() - } - } catch (e: IOException) { - if (DEBUG) { - Log.e(TAG, "Unable to loadSharedPreferences", e) - } - } catch (e: ClassNotFoundException) { - if (DEBUG) { - Log.e(TAG, "Unable to loadSharedPreferences", e) } + preferenceEditor.commit() } } }