Skip to content

Commit 24e65ef

Browse files
committed
Merge branch 'dev'
2 parents a557ac3 + a69bbab commit 24e65ef

27 files changed

Lines changed: 665 additions & 244 deletions

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: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
import androidx.preference.Preference;
2222
import androidx.preference.PreferenceManager;
2323

24+
import com.grack.nanojson.JsonParserException;
25+
2426
import org.schabi.newpipe.NewPipeDatabase;
2527
import org.schabi.newpipe.R;
28+
import org.schabi.newpipe.error.ErrorInfo;
2629
import org.schabi.newpipe.error.ErrorUtil;
30+
import org.schabi.newpipe.error.UserAction;
31+
import org.schabi.newpipe.settings.export.BackupFileLocator;
32+
import org.schabi.newpipe.settings.export.ImportExportManager;
2733
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
2834
import org.schabi.newpipe.streams.io.StoredFileHelper;
2935
import org.schabi.newpipe.util.NavigationHelper;
@@ -42,7 +48,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {
4248

4349
private final SimpleDateFormat exportDateFormat =
4450
new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
45-
private ContentSettingsManager manager;
51+
private ImportExportManager manager;
4652
private String importExportDataPathKey;
4753
private final ActivityResultLauncher<Intent> requestImportPathLauncher =
4854
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
@@ -57,8 +63,7 @@ public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
5763
@Nullable final String rootKey) {
5864
final File homeDir = ContextCompat.getDataDir(requireContext());
5965
Objects.requireNonNull(homeDir);
60-
manager = new ContentSettingsManager(new NewPipeFileLocator(homeDir));
61-
manager.deleteSettingsFile();
66+
manager = new ImportExportManager(new BackupFileLocator(homeDir));
6267

6368
importExportDataPathKey = getString(R.string.import_export_data_path);
6469

@@ -165,7 +170,7 @@ private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri
165170
Toast.makeText(requireContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT)
166171
.show();
167172
} catch (final Exception e) {
168-
ErrorUtil.showUiErrorSnackbar(this, "Exporting database", e);
173+
showErrorSnackbar(e, "Exporting database and settings");
169174
}
170175
}
171176

@@ -182,16 +187,21 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
182187
throw new IOException("Could not create databases dir");
183188
}
184189

190+
// replace the current database
185191
if (!manager.extractDb(file)) {
186192
Toast.makeText(requireContext(), R.string.could_not_import_all_files,
187193
Toast.LENGTH_LONG)
188194
.show();
189195
}
190196

191197
// if settings file exist, ask if it should be imported.
192-
if (manager.extractSettings(file)) {
198+
final boolean hasJsonPrefs = manager.exportHasJsonPrefs(file);
199+
if (hasJsonPrefs || manager.exportHasSerializedPrefs(file)) {
193200
new androidx.appcompat.app.AlertDialog.Builder(requireContext())
194201
.setTitle(R.string.import_settings)
202+
.setMessage(hasJsonPrefs ? null : requireContext()
203+
.getString(R.string.import_settings_vulnerable_format))
204+
.setOnDismissListener(dialog -> finishImport(importDataUri))
195205
.setNegativeButton(R.string.cancel, (dialog, which) -> {
196206
dialog.dismiss();
197207
finishImport(importDataUri);
@@ -201,7 +211,16 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
201211
final Context context = requireContext();
202212
final SharedPreferences prefs = PreferenceManager
203213
.getDefaultSharedPreferences(context);
204-
manager.loadSharedPreferences(prefs);
214+
try {
215+
if (hasJsonPrefs) {
216+
manager.loadJsonPrefs(file, prefs);
217+
} else {
218+
manager.loadSerializedPrefs(file, prefs);
219+
}
220+
} catch (IOException | ClassNotFoundException | JsonParserException e) {
221+
createErrorNotification(e, "Importing preferences");
222+
return;
223+
}
205224
cleanImport(context, prefs);
206225
finishImport(importDataUri);
207226
})
@@ -210,7 +229,7 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
210229
finishImport(importDataUri);
211230
}
212231
} catch (final Exception e) {
213-
ErrorUtil.showUiErrorSnackbar(this, "Importing database", e);
232+
showErrorSnackbar(e, "Importing database and settings");
214233
}
215234
}
216235

@@ -247,7 +266,7 @@ private void cleanImport(@NonNull final Context context,
247266
}
248267

249268
/**
250-
* Save import path and restart system.
269+
* Save import path and restart app.
251270
*
252271
* @param importDataUri The import path to save
253272
*/
@@ -268,4 +287,15 @@ private void saveLastImportExportDataUri(final Uri importExportDataUri) {
268287
.putString(importExportDataPathKey, importExportDataUri.toString());
269288
editor.apply();
270289
}
290+
291+
private void showErrorSnackbar(final Throwable e, final String request) {
292+
ErrorUtil.showSnackbar(this, new ErrorInfo(e, UserAction.DATABASE_IMPORT_EXPORT, request));
293+
}
294+
295+
private void createErrorNotification(final Throwable e, final String request) {
296+
ErrorUtil.createNotification(
297+
requireContext(),
298+
new ErrorInfo(e, UserAction.DATABASE_IMPORT_EXPORT, request)
299+
);
300+
}
271301
}

app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt

Lines changed: 0 additions & 120 deletions
This file was deleted.

app/src/main/java/org/schabi/newpipe/settings/NewPipeFileLocator.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.schabi.newpipe.settings.export
2+
3+
import java.io.File
4+
5+
/**
6+
* Locates specific files of NewPipe based on the home directory of the app.
7+
*/
8+
class BackupFileLocator(private val homeDir: File) {
9+
companion object {
10+
const val FILE_NAME_DB = "newpipe.db"
11+
@Deprecated(
12+
"Serializing preferences with Java's ObjectOutputStream is vulnerable to injections",
13+
replaceWith = ReplaceWith("FILE_NAME_JSON_PREFS")
14+
)
15+
const val FILE_NAME_SERIALIZED_PREFS = "newpipe.settings"
16+
const val FILE_NAME_JSON_PREFS = "preferences.json"
17+
}
18+
19+
val dbDir by lazy { File(homeDir, "/databases") }
20+
21+
val db by lazy { File(dbDir, FILE_NAME_DB) }
22+
23+
val dbJournal by lazy { File(dbDir, "$FILE_NAME_DB-journal") }
24+
25+
val dbShm by lazy { File(dbDir, "$FILE_NAME_DB-shm") }
26+
27+
val dbWal by lazy { File(dbDir, "$FILE_NAME_DB-wal") }
28+
}

0 commit comments

Comments
 (0)