11package org .schabi .newpipe .settings ;
22
3+ import android .annotation .SuppressLint ;
34import android .app .Activity ;
45import android .content .Context ;
56import android .content .Intent ;
2526import org .schabi .newpipe .extractor .NewPipe ;
2627import org .schabi .newpipe .extractor .localization .ContentCountry ;
2728import org .schabi .newpipe .extractor .localization .Localization ;
28- import org .schabi .newpipe .util .FilePickerActivityHelper ;
2929import org .schabi .newpipe .util .FilePathUtils ;
30+ import org .schabi .newpipe .util .FilePickerActivityHelper ;
3031import org .schabi .newpipe .util .ZipHelper ;
3132
3233import java .io .File ;
@@ -177,44 +178,49 @@ public void onActivityResult(final int requestCode, final int resultCode,
177178 if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH )
178179 && resultCode == Activity .RESULT_OK && data .getData () != null ) {
179180 final File file = Utils .getFileForUri (data .getData ());
180- final String path = file .getAbsolutePath ();
181- setImportExportDataPath (file );
182181
183182 if (requestCode == REQUEST_EXPORT_PATH ) {
184- final SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd_HHmmss" , Locale .US );
185- exportDatabase (path + "/NewPipeData-" + sdf .format (new Date ()) + ".zip" );
183+ exportDatabase (file );
186184 } else {
187185 final AlertDialog .Builder builder = new AlertDialog .Builder (requireActivity ());
188186 builder .setMessage (R .string .override_current_data )
189187 .setPositiveButton (getString (R .string .finish ),
190- (d , id ) -> importDatabase (path ))
188+ (d , id ) -> importDatabase (file ))
191189 .setNegativeButton (android .R .string .cancel ,
192190 (d , id ) -> d .cancel ());
193191 builder .create ().show ();
194192 }
195193 }
196194 }
197195
198- private void exportDatabase (final String path ) {
196+ private void exportDatabase (@ NonNull final File folder ) {
199197 try {
198+ final SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd_HHmmss" , Locale .US );
199+ final String path = folder .getAbsolutePath () + "/NewPipeData-"
200+ + sdf .format (new Date ()) + ".zip" ;
201+
200202 //checkpoint before export
201203 NewPipeDatabase .checkpoint ();
202204
203205 final SharedPreferences preferences = PreferenceManager
204- .getDefaultSharedPreferences (requireContext ());
206+ .getDefaultSharedPreferences (requireContext ());
205207 manager .exportDatabase (preferences , path );
206208
209+ setImportExportDataPath (folder , false );
210+
207211 Toast .makeText (getContext (), R .string .export_complete_toast , Toast .LENGTH_SHORT ).show ();
208212 } catch (final Exception e ) {
209213 ErrorActivity .reportUiErrorInSnackbar (this , "Exporting database" , e );
210214 }
211215 }
212216
213- private void importDatabase (final String filePath ) {
217+ private void importDatabase (@ NonNull final File file ) {
218+ final String filePath = file .getAbsolutePath ();
219+
214220 // check if file is supported
215221 if (!ZipHelper .isValidZipFile (filePath )) {
216222 Toast .makeText (getContext (), R .string .no_valid_zip_file , Toast .LENGTH_SHORT )
217- .show ();
223+ .show ();
218224 return ;
219225 }
220226
@@ -225,7 +231,7 @@ private void importDatabase(final String filePath) {
225231
226232 if (!manager .extractDb (filePath )) {
227233 Toast .makeText (getContext (), R .string .could_not_import_all_files , Toast .LENGTH_LONG )
228- .show ();
234+ .show ();
229235 }
230236
231237 //If settings file exist, ask if it should be imported.
@@ -235,27 +241,40 @@ private void importDatabase(final String filePath) {
235241
236242 alert .setNegativeButton (android .R .string .no , (dialog , which ) -> {
237243 dialog .dismiss ();
238- // restart app to properly load db
239- System .exit (0 );
244+ finishImport (file );
240245 });
241246 alert .setPositiveButton (getString (R .string .finish ), (dialog , which ) -> {
242247 dialog .dismiss ();
243248 manager .loadSharedPreferences (PreferenceManager
244- .getDefaultSharedPreferences (requireContext ()));
245- // restart app to properly load db
246- System .exit (0 );
249+ .getDefaultSharedPreferences (requireContext ()));
250+ finishImport (file );
247251 });
248252 alert .show ();
249253 } else {
250- // restart app to properly load db
251- System .exit (0 );
254+ finishImport (file );
252255 }
253256 } catch (final Exception e ) {
254257 ErrorActivity .reportUiErrorInSnackbar (this , "Importing database" , e );
255258 }
256259 }
257260
258- private void setImportExportDataPath (final File file ) {
261+ /**
262+ * Save import path and restart system.
263+ *
264+ * @param file The file of the created backup
265+ */
266+ private void finishImport (@ NonNull final File file ) {
267+ if (file .getParentFile () != null ) {
268+ //immediately because app is about to exit
269+ setImportExportDataPath (file .getParentFile (), true );
270+ }
271+
272+ // restart app to properly load db
273+ System .exit (0 );
274+ }
275+
276+ @ SuppressLint ("ApplySharedPref" )
277+ private void setImportExportDataPath (@ NonNull final File file , final boolean immediately ) {
259278 final String directoryPath ;
260279 if (file .isDirectory ()) {
261280 directoryPath = file .getAbsolutePath ();
@@ -267,6 +286,13 @@ private void setImportExportDataPath(final File file) {
267286 directoryPath = "" ;
268287 }
269288 }
270- defaultPreferences .edit ().putString (importExportDataPathKey , directoryPath ).apply ();
289+ final SharedPreferences .Editor editor = defaultPreferences
290+ .edit ()
291+ .putString (importExportDataPathKey , directoryPath );
292+ if (immediately ) {
293+ editor .commit ();
294+ } else {
295+ editor .apply ();
296+ }
271297 }
272298}
0 commit comments