Skip to content

Commit d8668ed

Browse files
committed
Show snackbar error when settings import fails
1 parent d75a6ea commit d8668ed

3 files changed

Lines changed: 45 additions & 42 deletions

File tree

app/src/main/java/org/schabi/newpipe/error/UserAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public enum UserAction {
77
USER_REPORT("user report"),
88
UI_ERROR("ui error"),
9+
DATABASE_IMPORT_EXPORT("database import or export"),
910
SUBSCRIPTION_CHANGE("subscription change"),
1011
SUBSCRIPTION_UPDATE("subscription update"),
1112
SUBSCRIPTION_GET("get subscription"),

app/src/main/java/org/schabi/newpipe/settings/BackupRestoreSettingsFragment.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import org.schabi.newpipe.NewPipeDatabase;
2525
import org.schabi.newpipe.R;
26+
import org.schabi.newpipe.error.ErrorInfo;
2627
import org.schabi.newpipe.error.ErrorUtil;
28+
import org.schabi.newpipe.error.UserAction;
2729
import org.schabi.newpipe.settings.export.ImportExportManager;
2830
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
2931
import org.schabi.newpipe.streams.io.StoredFileHelper;
@@ -166,7 +168,7 @@ private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri
166168
Toast.makeText(requireContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT)
167169
.show();
168170
} catch (final Exception e) {
169-
ErrorUtil.showUiErrorSnackbar(this, "Exporting database", e);
171+
showErrorSnackbar(e, "Exporting database and settings");
170172
}
171173
}
172174

@@ -202,7 +204,12 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
202204
final Context context = requireContext();
203205
final SharedPreferences prefs = PreferenceManager
204206
.getDefaultSharedPreferences(context);
205-
manager.loadSharedPreferences(prefs);
207+
try {
208+
manager.loadSharedPreferences(prefs);
209+
} catch (IOException | ClassNotFoundException e) {
210+
showErrorSnackbar(e, "Importing preferences");
211+
return;
212+
}
206213
cleanImport(context, prefs);
207214
finishImport(importDataUri);
208215
})
@@ -211,7 +218,7 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
211218
finishImport(importDataUri);
212219
}
213220
} catch (final Exception e) {
214-
ErrorUtil.showUiErrorSnackbar(this, "Importing database", e);
221+
showErrorSnackbar(e, "Importing database and settings");
215222
}
216223
}
217224

@@ -269,4 +276,8 @@ private void saveLastImportExportDataUri(final Uri importExportDataUri) {
269276
.putString(importExportDataPathKey, importExportDataUri.toString());
270277
editor.apply();
271278
}
279+
280+
private void showErrorSnackbar(final Throwable e, final String request) {
281+
ErrorUtil.showSnackbar(this, new ErrorInfo(e, UserAction.DATABASE_IMPORT_EXPORT, request));
282+
}
272283
}

app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -73,50 +73,41 @@ class ImportExportManager(private val fileLocator: NewPipeFileLocator) {
7373
/**
7474
* Remove all shared preferences from the app and load the preferences supplied to the manager.
7575
*/
76+
@Throws(IOException::class, ClassNotFoundException::class)
7677
fun loadSharedPreferences(preferences: SharedPreferences) {
77-
try {
78-
val preferenceEditor = preferences.edit()
78+
val preferenceEditor = preferences.edit()
7979

80-
PreferencesObjectInputStream(
81-
fileLocator.settings.inputStream()
82-
).use { input ->
83-
preferenceEditor.clear()
84-
@Suppress("UNCHECKED_CAST")
85-
val entries = input.readObject() as Map<String, *>
86-
for ((key, value) in entries) {
87-
when (value) {
88-
is Boolean -> {
89-
preferenceEditor.putBoolean(key, value)
90-
}
91-
is Float -> {
92-
preferenceEditor.putFloat(key, value)
93-
}
94-
is Int -> {
95-
preferenceEditor.putInt(key, value)
96-
}
97-
is Long -> {
98-
preferenceEditor.putLong(key, value)
99-
}
100-
is String -> {
101-
preferenceEditor.putString(key, value)
102-
}
103-
is Set<*> -> {
104-
// There are currently only Sets with type String possible
105-
@Suppress("UNCHECKED_CAST")
106-
preferenceEditor.putStringSet(key, value as Set<String>?)
107-
}
80+
PreferencesObjectInputStream(
81+
fileLocator.settings.inputStream()
82+
).use { input ->
83+
preferenceEditor.clear()
84+
@Suppress("UNCHECKED_CAST")
85+
val entries = input.readObject() as Map<String, *>
86+
for ((key, value) in entries) {
87+
when (value) {
88+
is Boolean -> {
89+
preferenceEditor.putBoolean(key, value)
90+
}
91+
is Float -> {
92+
preferenceEditor.putFloat(key, value)
93+
}
94+
is Int -> {
95+
preferenceEditor.putInt(key, value)
96+
}
97+
is Long -> {
98+
preferenceEditor.putLong(key, value)
99+
}
100+
is String -> {
101+
preferenceEditor.putString(key, value)
102+
}
103+
is Set<*> -> {
104+
// There are currently only Sets with type String possible
105+
@Suppress("UNCHECKED_CAST")
106+
preferenceEditor.putStringSet(key, value as Set<String>?)
108107
}
109108
}
110-
preferenceEditor.commit()
111-
}
112-
} catch (e: IOException) {
113-
if (DEBUG) {
114-
Log.e(TAG, "Unable to loadSharedPreferences", e)
115-
}
116-
} catch (e: ClassNotFoundException) {
117-
if (DEBUG) {
118-
Log.e(TAG, "Unable to loadSharedPreferences", e)
119109
}
110+
preferenceEditor.commit()
120111
}
121112
}
122113
}

0 commit comments

Comments
 (0)