11package org .schabi .newpipe .settings ;
22
3- import android .annotation .SuppressLint ;
43import android .app .Activity ;
54import android .content .Context ;
65import android .content .Intent ;
2625import org .schabi .newpipe .extractor .NewPipe ;
2726import org .schabi .newpipe .extractor .localization .ContentCountry ;
2827import org .schabi .newpipe .extractor .localization .Localization ;
29- import org .schabi .newpipe .util .FilePathUtils ;
3028import org .schabi .newpipe .util .FilePickerActivityHelper ;
3129import org .schabi .newpipe .util .ZipHelper ;
3230
@@ -43,8 +41,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
4341
4442 private ContentSettingsManager manager ;
4543
46- private String importExportDataPathKey ;
47-
4844 private String thumbnailLoadToggleKey ;
4945 private String youtubeRestrictedModeEnabledKey ;
5046
@@ -60,18 +56,13 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
6056
6157 addPreferencesFromResource (R .xml .content_settings );
6258
63- importExportDataPathKey = getString (R .string .import_export_data_path );
6459 final Preference importDataPreference = findPreference (getString (R .string .import_data ));
6560 importDataPreference .setOnPreferenceClickListener (p -> {
6661 final Intent i = new Intent (getActivity (), FilePickerActivityHelper .class )
6762 .putExtra (FilePickerActivityHelper .EXTRA_ALLOW_MULTIPLE , false )
6863 .putExtra (FilePickerActivityHelper .EXTRA_ALLOW_CREATE_DIR , false )
6964 .putExtra (FilePickerActivityHelper .EXTRA_MODE ,
7065 FilePickerActivityHelper .MODE_FILE );
71- final String path = defaultPreferences .getString (importExportDataPathKey , "" );
72- if (FilePathUtils .isValidDirectoryPath (path )) {
73- i .putExtra (FilePickerActivityHelper .EXTRA_START_PATH , path );
74- }
7566 startActivityForResult (i , REQUEST_IMPORT_PATH );
7667 return true ;
7768 });
@@ -83,10 +74,6 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
8374 .putExtra (FilePickerActivityHelper .EXTRA_ALLOW_CREATE_DIR , true )
8475 .putExtra (FilePickerActivityHelper .EXTRA_MODE ,
8576 FilePickerActivityHelper .MODE_DIR );
86- final String path = defaultPreferences .getString (importExportDataPathKey , "" );
87- if (FilePathUtils .isValidDirectoryPath (path )) {
88- i .putExtra (FilePickerActivityHelper .EXTRA_START_PATH , path );
89- }
9077 startActivityForResult (i , REQUEST_EXPORT_PATH );
9178 return true ;
9279 });
@@ -177,50 +164,42 @@ public void onActivityResult(final int requestCode, final int resultCode,
177164
178165 if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH )
179166 && resultCode == Activity .RESULT_OK && data .getData () != null ) {
180- final File file = Utils .getFileForUri (data .getData ());
181-
167+ final String path = Utils .getFileForUri (data .getData ()).getAbsolutePath ();
182168 if (requestCode == REQUEST_EXPORT_PATH ) {
183- exportDatabase (file );
169+ final SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd_HHmmss" , Locale .US );
170+ exportDatabase (path + "/NewPipeData-" + sdf .format (new Date ()) + ".zip" );
184171 } else {
185172 final AlertDialog .Builder builder = new AlertDialog .Builder (requireActivity ());
186173 builder .setMessage (R .string .override_current_data )
187174 .setPositiveButton (getString (R .string .finish ),
188- (d , id ) -> importDatabase (file ))
175+ (d , id ) -> importDatabase (path ))
189176 .setNegativeButton (android .R .string .cancel ,
190177 (d , id ) -> d .cancel ());
191178 builder .create ().show ();
192179 }
193180 }
194181 }
195182
196- private void exportDatabase (@ NonNull final File folder ) {
183+ private void exportDatabase (final String path ) {
197184 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-
202185 //checkpoint before export
203186 NewPipeDatabase .checkpoint ();
204187
205188 final SharedPreferences preferences = PreferenceManager
206- .getDefaultSharedPreferences (requireContext ());
189+ .getDefaultSharedPreferences (requireContext ());
207190 manager .exportDatabase (preferences , path );
208191
209- setImportExportDataPath (folder , false );
210-
211192 Toast .makeText (getContext (), R .string .export_complete_toast , Toast .LENGTH_SHORT ).show ();
212193 } catch (final Exception e ) {
213194 ErrorActivity .reportUiErrorInSnackbar (this , "Exporting database" , e );
214195 }
215196 }
216197
217- private void importDatabase (@ NonNull final File file ) {
218- final String filePath = file .getAbsolutePath ();
219-
198+ private void importDatabase (final String filePath ) {
220199 // check if file is supported
221200 if (!ZipHelper .isValidZipFile (filePath )) {
222201 Toast .makeText (getContext (), R .string .no_valid_zip_file , Toast .LENGTH_SHORT )
223- .show ();
202+ .show ();
224203 return ;
225204 }
226205
@@ -231,7 +210,7 @@ private void importDatabase(@NonNull final File file) {
231210
232211 if (!manager .extractDb (filePath )) {
233212 Toast .makeText (getContext (), R .string .could_not_import_all_files , Toast .LENGTH_LONG )
234- .show ();
213+ .show ();
235214 }
236215
237216 //If settings file exist, ask if it should be imported.
@@ -241,58 +220,23 @@ private void importDatabase(@NonNull final File file) {
241220
242221 alert .setNegativeButton (android .R .string .no , (dialog , which ) -> {
243222 dialog .dismiss ();
244- finishImport (file );
223+ // restart app to properly load db
224+ System .exit (0 );
245225 });
246226 alert .setPositiveButton (getString (R .string .finish ), (dialog , which ) -> {
247227 dialog .dismiss ();
248228 manager .loadSharedPreferences (PreferenceManager
249- .getDefaultSharedPreferences (requireContext ()));
250- finishImport (file );
229+ .getDefaultSharedPreferences (requireContext ()));
230+ // restart app to properly load db
231+ System .exit (0 );
251232 });
252233 alert .show ();
253234 } else {
254- finishImport (file );
235+ // restart app to properly load db
236+ System .exit (0 );
255237 }
256238 } catch (final Exception e ) {
257239 ErrorActivity .reportUiErrorInSnackbar (this , "Importing database" , e );
258240 }
259241 }
260-
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 ) {
278- final String directoryPath ;
279- if (file .isDirectory ()) {
280- directoryPath = file .getAbsolutePath ();
281- } else {
282- final File parentFile = file .getParentFile ();
283- if (parentFile != null ) {
284- directoryPath = parentFile .getAbsolutePath ();
285- } else {
286- directoryPath = "" ;
287- }
288- }
289- final SharedPreferences .Editor editor = defaultPreferences
290- .edit ()
291- .putString (importExportDataPathKey , directoryPath );
292- if (immediately ) {
293- editor .commit ();
294- } else {
295- editor .apply ();
296- }
297- }
298242}
0 commit comments