Show snackbar error when settings import fails

This commit is contained in:
Stypox 2024-03-27 15:02:27 +01:00
parent d75a6eaa41
commit d8668ed226
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
3 changed files with 45 additions and 42 deletions

View file

@ -6,6 +6,7 @@ package org.schabi.newpipe.error;
public enum UserAction { public enum UserAction {
USER_REPORT("user report"), USER_REPORT("user report"),
UI_ERROR("ui error"), UI_ERROR("ui error"),
DATABASE_IMPORT_EXPORT("database import or export"),
SUBSCRIPTION_CHANGE("subscription change"), SUBSCRIPTION_CHANGE("subscription change"),
SUBSCRIPTION_UPDATE("subscription update"), SUBSCRIPTION_UPDATE("subscription update"),
SUBSCRIPTION_GET("get subscription"), SUBSCRIPTION_GET("get subscription"),

View file

@ -23,7 +23,9 @@ import androidx.preference.PreferenceManager;
import org.schabi.newpipe.NewPipeDatabase; import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.error.ErrorInfo;
import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.error.ErrorUtil;
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.settings.export.ImportExportManager; import org.schabi.newpipe.settings.export.ImportExportManager;
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard; import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
import org.schabi.newpipe.streams.io.StoredFileHelper; 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) Toast.makeText(requireContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT)
.show(); .show();
} catch (final Exception e) { } 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 Context context = requireContext();
final SharedPreferences prefs = PreferenceManager final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context); .getDefaultSharedPreferences(context);
manager.loadSharedPreferences(prefs); try {
manager.loadSharedPreferences(prefs);
} catch (IOException | ClassNotFoundException e) {
showErrorSnackbar(e, "Importing preferences");
return;
}
cleanImport(context, prefs); cleanImport(context, prefs);
finishImport(importDataUri); finishImport(importDataUri);
}) })
@ -211,7 +218,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {
finishImport(importDataUri); finishImport(importDataUri);
} }
} catch (final Exception e) { } 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()); .putString(importExportDataPathKey, importExportDataUri.toString());
editor.apply(); editor.apply();
} }
private void showErrorSnackbar(final Throwable e, final String request) {
ErrorUtil.showSnackbar(this, new ErrorInfo(e, UserAction.DATABASE_IMPORT_EXPORT, request));
}
} }

View file

@ -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. * Remove all shared preferences from the app and load the preferences supplied to the manager.
*/ */
@Throws(IOException::class, ClassNotFoundException::class)
fun loadSharedPreferences(preferences: SharedPreferences) { fun loadSharedPreferences(preferences: SharedPreferences) {
try { val preferenceEditor = preferences.edit()
val preferenceEditor = preferences.edit()
PreferencesObjectInputStream( PreferencesObjectInputStream(
fileLocator.settings.inputStream() fileLocator.settings.inputStream()
).use { input -> ).use { input ->
preferenceEditor.clear() preferenceEditor.clear()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val entries = input.readObject() as Map<String, *> val entries = input.readObject() as Map<String, *>
for ((key, value) in entries) { for ((key, value) in entries) {
when (value) { when (value) {
is Boolean -> { is Boolean -> {
preferenceEditor.putBoolean(key, value) preferenceEditor.putBoolean(key, value)
} }
is Float -> { is Float -> {
preferenceEditor.putFloat(key, value) preferenceEditor.putFloat(key, value)
} }
is Int -> { is Int -> {
preferenceEditor.putInt(key, value) preferenceEditor.putInt(key, value)
} }
is Long -> { is Long -> {
preferenceEditor.putLong(key, value) preferenceEditor.putLong(key, value)
} }
is String -> { is String -> {
preferenceEditor.putString(key, value) preferenceEditor.putString(key, value)
} }
is Set<*> -> { is Set<*> -> {
// There are currently only Sets with type String possible // There are currently only Sets with type String possible
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
preferenceEditor.putStringSet(key, value as Set<String>?) preferenceEditor.putStringSet(key, value as Set<String>?)
}
} }
} }
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()
} }
} }
} }