From 82f43ac6a64c1754e023aed6c5cd7a117f1a99dc Mon Sep 17 00:00:00 2001 From: Alireza Tofighi Date: Mon, 17 May 2021 21:57:27 +0430 Subject: [PATCH] Save backup import/export location for feature import/exports --- .../settings/ContentSettingsFragment.java | 39 ++++++++++++++++++- app/src/main/res/values/settings_keys.xml | 1 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 3fd44c4d521..e4850d7fa8e 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -41,6 +41,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment { private ContentSettingsManager manager; + private String importExportDataPathKey; + private String thumbnailLoadToggleKey; private String youtubeRestrictedModeEnabledKey; @@ -56,6 +58,7 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro addPreferencesFromResource(R.xml.content_settings); + importExportDataPathKey = getString(R.string.import_export_data_path); final Preference importDataPreference = findPreference(getString(R.string.import_data)); importDataPreference.setOnPreferenceClickListener(p -> { final Intent i = new Intent(getActivity(), FilePickerActivityHelper.class) @@ -63,6 +66,10 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false) .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE); + final String path = defaultPreferences.getString(importExportDataPathKey, ""); + if (isValidPath(path)) { + i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path); + } startActivityForResult(i, REQUEST_IMPORT_PATH); return true; }); @@ -74,6 +81,10 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true) .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_DIR); + final String path = defaultPreferences.getString(importExportDataPathKey, ""); + if (isValidPath(path)) { + i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path); + } startActivityForResult(i, REQUEST_EXPORT_PATH); return true; }); @@ -164,7 +175,10 @@ public void onActivityResult(final int requestCode, final int resultCode, if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) && resultCode == Activity.RESULT_OK && data.getData() != null) { - final String path = Utils.getFileForUri(data.getData()).getAbsolutePath(); + final File file = Utils.getFileForUri(data.getData()); + final String path = file.getAbsolutePath(); + setImportExportDataPath(file); + if (requestCode == REQUEST_EXPORT_PATH) { final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip"); @@ -239,4 +253,27 @@ private void importDatabase(final String filePath) { ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); } } + + private boolean isValidPath(final String path) { + if (path == null || path.isEmpty()) { + return false; + } + final File file = new File(path); + return file.exists() && file.isDirectory(); + } + + private void setImportExportDataPath(final File file) { + final String directoryPath; + if (!file.isDirectory()) { + final File parentFile = file.getParentFile(); + if (parentFile != null) { + directoryPath = parentFile.getAbsolutePath(); + } else { + directoryPath = ""; + } + } else { + directoryPath = file.getAbsolutePath(); + } + defaultPreferences.edit().putString(importExportDataPathKey, directoryPath).apply(); + } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index fd6cc725140..c23e81fbe22 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -265,6 +265,7 @@ feed_use_dedicated_fetch_method + import_export_data_path import_data export_data