2121import androidx .preference .Preference ;
2222import androidx .preference .PreferenceManager ;
2323
24+ import com .grack .nanojson .JsonParserException ;
25+
2426import org .schabi .newpipe .NewPipeDatabase ;
2527import org .schabi .newpipe .R ;
28+ import org .schabi .newpipe .error .ErrorInfo ;
2629import 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 ;
2733import org .schabi .newpipe .streams .io .NoFileManagerSafeGuard ;
2834import org .schabi .newpipe .streams .io .StoredFileHelper ;
2935import 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}
0 commit comments