mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-10-31 15:23:00 +00:00 
			
		
		
		
	Merge pull request #6402 from XiangRongLin/save_backup_bug
Fix and improve setting import and export paths
This commit is contained in:
		| @@ -1,5 +1,6 @@ | |||||||
| package org.schabi.newpipe.settings; | package org.schabi.newpipe.settings; | ||||||
|  |  | ||||||
|  | import android.annotation.SuppressLint; | ||||||
| import android.app.Activity; | import android.app.Activity; | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
| import android.content.Intent; | import android.content.Intent; | ||||||
| @@ -25,8 +26,8 @@ import org.schabi.newpipe.error.ReCaptchaActivity; | |||||||
| import org.schabi.newpipe.extractor.NewPipe; | import org.schabi.newpipe.extractor.NewPipe; | ||||||
| import org.schabi.newpipe.extractor.localization.ContentCountry; | import org.schabi.newpipe.extractor.localization.ContentCountry; | ||||||
| import org.schabi.newpipe.extractor.localization.Localization; | import org.schabi.newpipe.extractor.localization.Localization; | ||||||
| import org.schabi.newpipe.util.FilePickerActivityHelper; |  | ||||||
| import org.schabi.newpipe.util.FilePathUtils; | import org.schabi.newpipe.util.FilePathUtils; | ||||||
|  | import org.schabi.newpipe.util.FilePickerActivityHelper; | ||||||
| import org.schabi.newpipe.util.ZipHelper; | import org.schabi.newpipe.util.ZipHelper; | ||||||
|  |  | ||||||
| import java.io.File; | import java.io.File; | ||||||
| @@ -177,17 +178,14 @@ public class ContentSettingsFragment extends BasePreferenceFragment { | |||||||
|         if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) |         if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) | ||||||
|                 && resultCode == Activity.RESULT_OK && data.getData() != null) { |                 && resultCode == Activity.RESULT_OK && data.getData() != null) { | ||||||
|             final File file = Utils.getFileForUri(data.getData()); |             final File file = Utils.getFileForUri(data.getData()); | ||||||
|             final String path = file.getAbsolutePath(); |  | ||||||
|             setImportExportDataPath(file); |  | ||||||
|  |  | ||||||
|             if (requestCode == REQUEST_EXPORT_PATH) { |             if (requestCode == REQUEST_EXPORT_PATH) { | ||||||
|                 final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); |                 exportDatabase(file); | ||||||
|                 exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip"); |  | ||||||
|             } else { |             } else { | ||||||
|                 final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); |                 final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); | ||||||
|                 builder.setMessage(R.string.override_current_data) |                 builder.setMessage(R.string.override_current_data) | ||||||
|                         .setPositiveButton(getString(R.string.finish), |                         .setPositiveButton(getString(R.string.finish), | ||||||
|                                 (d, id) -> importDatabase(path)) |                                 (d, id) -> importDatabase(file)) | ||||||
|                         .setNegativeButton(android.R.string.cancel, |                         .setNegativeButton(android.R.string.cancel, | ||||||
|                                 (d, id) -> d.cancel()); |                                 (d, id) -> d.cancel()); | ||||||
|                 builder.create().show(); |                 builder.create().show(); | ||||||
| @@ -195,26 +193,34 @@ public class ContentSettingsFragment extends BasePreferenceFragment { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void exportDatabase(final String path) { |     private void exportDatabase(@NonNull final File folder) { | ||||||
|         try { |         try { | ||||||
|  |             final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); | ||||||
|  |             final String path = folder.getAbsolutePath() + "/NewPipeData-" | ||||||
|  |                     + sdf.format(new Date()) + ".zip"; | ||||||
|  |  | ||||||
|             //checkpoint before export |             //checkpoint before export | ||||||
|             NewPipeDatabase.checkpoint(); |             NewPipeDatabase.checkpoint(); | ||||||
|  |  | ||||||
|             final SharedPreferences preferences = PreferenceManager |             final SharedPreferences preferences = PreferenceManager | ||||||
|                 .getDefaultSharedPreferences(requireContext()); |                     .getDefaultSharedPreferences(requireContext()); | ||||||
|             manager.exportDatabase(preferences, path); |             manager.exportDatabase(preferences, path); | ||||||
|  |  | ||||||
|  |             setImportExportDataPath(folder, false); | ||||||
|  |  | ||||||
|             Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); |             Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); | ||||||
|         } catch (final Exception e) { |         } catch (final Exception e) { | ||||||
|             ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e); |             ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void importDatabase(final String filePath) { |     private void importDatabase(@NonNull final File file) { | ||||||
|  |         final String filePath = file.getAbsolutePath(); | ||||||
|  |  | ||||||
|         // check if file is supported |         // check if file is supported | ||||||
|         if (!ZipHelper.isValidZipFile(filePath)) { |         if (!ZipHelper.isValidZipFile(filePath)) { | ||||||
|             Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT) |             Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT) | ||||||
|                 .show(); |                     .show(); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -225,7 +231,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { | |||||||
|  |  | ||||||
|             if (!manager.extractDb(filePath)) { |             if (!manager.extractDb(filePath)) { | ||||||
|                 Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG) |                 Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG) | ||||||
|                     .show(); |                         .show(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             //If settings file exist, ask if it should be imported. |             //If settings file exist, ask if it should be imported. | ||||||
| @@ -235,27 +241,40 @@ public class ContentSettingsFragment extends BasePreferenceFragment { | |||||||
|  |  | ||||||
|                 alert.setNegativeButton(android.R.string.no, (dialog, which) -> { |                 alert.setNegativeButton(android.R.string.no, (dialog, which) -> { | ||||||
|                     dialog.dismiss(); |                     dialog.dismiss(); | ||||||
|                     // restart app to properly load db |                     finishImport(file); | ||||||
|                     System.exit(0); |  | ||||||
|                 }); |                 }); | ||||||
|                 alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> { |                 alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> { | ||||||
|                     dialog.dismiss(); |                     dialog.dismiss(); | ||||||
|                     manager.loadSharedPreferences(PreferenceManager |                     manager.loadSharedPreferences(PreferenceManager | ||||||
|                         .getDefaultSharedPreferences(requireContext())); |                             .getDefaultSharedPreferences(requireContext())); | ||||||
|                     // restart app to properly load db |                     finishImport(file); | ||||||
|                     System.exit(0); |  | ||||||
|                 }); |                 }); | ||||||
|                 alert.show(); |                 alert.show(); | ||||||
|             } else { |             } else { | ||||||
|                 // restart app to properly load db |                 finishImport(file); | ||||||
|                 System.exit(0); |  | ||||||
|             } |             } | ||||||
|         } catch (final Exception e) { |         } catch (final Exception e) { | ||||||
|             ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); |             ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void setImportExportDataPath(final File file) { |     /** | ||||||
|  |      * Save import path and restart system. | ||||||
|  |      * | ||||||
|  |      * @param file The file of the created backup | ||||||
|  |      */ | ||||||
|  |     private void finishImport(@NonNull final File file) { | ||||||
|  |         if (file.getParentFile() != null) { | ||||||
|  |             //immediately because app is about to exit | ||||||
|  |             setImportExportDataPath(file.getParentFile(), true); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // restart app to properly load db | ||||||
|  |         System.exit(0); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @SuppressLint("ApplySharedPref") | ||||||
|  |     private void setImportExportDataPath(@NonNull final File file, final boolean immediately) { | ||||||
|         final String directoryPath; |         final String directoryPath; | ||||||
|         if (file.isDirectory()) { |         if (file.isDirectory()) { | ||||||
|             directoryPath = file.getAbsolutePath(); |             directoryPath = file.getAbsolutePath(); | ||||||
| @@ -267,6 +286,13 @@ public class ContentSettingsFragment extends BasePreferenceFragment { | |||||||
|                 directoryPath = ""; |                 directoryPath = ""; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         defaultPreferences.edit().putString(importExportDataPathKey, directoryPath).apply(); |         final SharedPreferences.Editor editor = defaultPreferences | ||||||
|  |                 .edit() | ||||||
|  |                 .putString(importExportDataPathKey, directoryPath); | ||||||
|  |         if (immediately) { | ||||||
|  |             editor.commit(); | ||||||
|  |         } else { | ||||||
|  |             editor.apply(); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Tobi
					Tobi