From 1e09a1768e0263b5f0b5d774d4d8173c22b0e398 Mon Sep 17 00:00:00 2001 From: Stypox Date: Sat, 5 Jun 2021 23:52:39 +0200 Subject: [PATCH] Revert all commits related to ContentSettingsFragment Revert "Annotate methode parameters as NonNull" This reverts commit 004907d306f84bbe800b624e9af650f73989652b. Revert "Commit path immediately when import backup" This reverts commit 05eb0d0fbe7ffc13d6b9a431d0825c6ce6c8dc79. Revert "Set ImportExportDataPath only on successful import" This reverts commit f13a1b04e64d01d2337662b724ddff4b9ede54fc. Revert "Set ImportExportDataPath only on successful export" This reverts commit fd4408e572f5c5aacf5c74ace91ef7c1ed46c474. Revert "Invert if condition in ContentSettingsFragment.setImportExportDataPath for better readability" This reverts commit 92ab9cae27620337d21e4f634b918a919aa5d830. Revert "Move ContentSettingsFragment.isValidPath to helpers and add unit test for it." This reverts commit fa2b11b7685d989d1e28e62f65ff1f80d890a9d1. Revert "Save backup import/export location for feature import/exports" This reverts commit 82f43ac6a64c1754e023aed6c5cd7a117f1a99dc. Remove FilePathHelperTest file --- .../settings/ContentSettingsFragment.java | 88 ++++--------------- .../schabi/newpipe/util/FilePathUtils.java | 22 ----- app/src/main/res/values/settings_keys.xml | 1 - .../newpipe/util/FilePathHelperTest.java | 56 ------------ 4 files changed, 16 insertions(+), 151 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java delete mode 100644 app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java 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 9af3666a6..3fd44c4d5 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.settings; -import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -26,7 +25,6 @@ import org.schabi.newpipe.error.ReCaptchaActivity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.localization.Localization; -import org.schabi.newpipe.util.FilePathUtils; import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.ZipHelper; @@ -43,8 +41,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment { private ContentSettingsManager manager; - private String importExportDataPathKey; - private String thumbnailLoadToggleKey; private String youtubeRestrictedModeEnabledKey; @@ -60,7 +56,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment { 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) @@ -68,10 +63,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment { .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false) .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE); - final String path = defaultPreferences.getString(importExportDataPathKey, ""); - if (FilePathUtils.isValidDirectoryPath(path)) { - i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path); - } startActivityForResult(i, REQUEST_IMPORT_PATH); return true; }); @@ -83,10 +74,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment { .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true) .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_DIR); - final String path = defaultPreferences.getString(importExportDataPathKey, ""); - if (FilePathUtils.isValidDirectoryPath(path)) { - i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path); - } startActivityForResult(i, REQUEST_EXPORT_PATH); return true; }); @@ -177,15 +164,15 @@ public class ContentSettingsFragment extends BasePreferenceFragment { if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) && resultCode == Activity.RESULT_OK && data.getData() != null) { - final File file = Utils.getFileForUri(data.getData()); - + final String path = Utils.getFileForUri(data.getData()).getAbsolutePath(); if (requestCode == REQUEST_EXPORT_PATH) { - exportDatabase(file); + final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); + exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip"); } else { final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); builder.setMessage(R.string.override_current_data) .setPositiveButton(getString(R.string.finish), - (d, id) -> importDatabase(file)) + (d, id) -> importDatabase(path)) .setNegativeButton(android.R.string.cancel, (d, id) -> d.cancel()); builder.create().show(); @@ -193,34 +180,26 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - private void exportDatabase(@NonNull final File folder) { + private void exportDatabase(final String path) { try { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); - final String path = folder.getAbsolutePath() + "/NewPipeData-" - + sdf.format(new Date()) + ".zip"; - //checkpoint before export NewPipeDatabase.checkpoint(); final SharedPreferences preferences = PreferenceManager - .getDefaultSharedPreferences(requireContext()); + .getDefaultSharedPreferences(requireContext()); manager.exportDatabase(preferences, path); - setImportExportDataPath(folder, false); - Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e); } } - private void importDatabase(@NonNull final File file) { - final String filePath = file.getAbsolutePath(); - + private void importDatabase(final String filePath) { // check if file is supported if (!ZipHelper.isValidZipFile(filePath)) { Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT) - .show(); + .show(); return; } @@ -231,7 +210,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { if (!manager.extractDb(filePath)) { 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. @@ -241,58 +220,23 @@ public class ContentSettingsFragment extends BasePreferenceFragment { alert.setNegativeButton(android.R.string.no, (dialog, which) -> { dialog.dismiss(); - finishImport(file); + // restart app to properly load db + System.exit(0); }); alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> { dialog.dismiss(); manager.loadSharedPreferences(PreferenceManager - .getDefaultSharedPreferences(requireContext())); - finishImport(file); + .getDefaultSharedPreferences(requireContext())); + // restart app to properly load db + System.exit(0); }); alert.show(); } else { - finishImport(file); + // restart app to properly load db + System.exit(0); } } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); } } - - /** - * 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; - if (file.isDirectory()) { - directoryPath = file.getAbsolutePath(); - } else { - final File parentFile = file.getParentFile(); - if (parentFile != null) { - directoryPath = parentFile.getAbsolutePath(); - } else { - directoryPath = ""; - } - } - final SharedPreferences.Editor editor = defaultPreferences - .edit() - .putString(importExportDataPathKey, directoryPath); - if (immediately) { - editor.commit(); - } else { - editor.apply(); - } - } } diff --git a/app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java b/app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java deleted file mode 100644 index 4162e563a..000000000 --- a/app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.schabi.newpipe.util; - -import java.io.File; - -public final class FilePathUtils { - private FilePathUtils() { } - - - /** - * Check that the path is a valid directory path and it exists. - * - * @param path full path of directory, - * @return is path valid or not - */ - public static boolean isValidDirectoryPath(final String path) { - if (path == null || path.isEmpty()) { - return false; - } - final File file = new File(path); - return file.exists() && file.isDirectory(); - } -} diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index c23e81fbe..fd6cc7251 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -265,7 +265,6 @@ feed_use_dedicated_fetch_method - import_export_data_path import_data export_data diff --git a/app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java b/app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java deleted file mode 100644 index 3c9f12720..000000000 --- a/app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package org.schabi.newpipe.util; - -import org.junit.Before; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class FilePathHelperTest { - - private Path dir; - - @Before - public void setUp() throws IOException { - dir = Files.createTempDirectory("dir1"); - } - - @Test - public void testIsValidDirectoryPathWithEmptyString() { - assertFalse(FilePathUtils.isValidDirectoryPath("")); - } - - @Test - public void testIsValidDirectoryPathWithNullString() { - assertFalse(FilePathUtils.isValidDirectoryPath(null)); - } - - @Test - public void testIsValidDirectoryPathWithValidPath() { - assertTrue(FilePathUtils.isValidDirectoryPath(dir.toAbsolutePath().toString())); - } - - @Test - public void testIsValidDirectoryPathWithDeepValidDirectory() throws IOException { - final File subDir = Files.createDirectory(dir.resolve("subdir")).toFile(); - assertTrue(FilePathUtils.isValidDirectoryPath(subDir.getAbsolutePath())); - } - - @Test - public void testIsValidDirectoryPathWithNotExistDirectory() { - assertFalse(FilePathUtils.isValidDirectoryPath(dir.resolve("not-exists-subdir"). - toFile().getAbsolutePath())); - } - - @Test - public void testIsValidDirectoryPathWithFile() throws IOException { - final File tempFile = Files.createFile(dir.resolve("simple_file")).toFile(); - assertFalse(FilePathUtils.isValidDirectoryPath(tempFile.getAbsolutePath())); - } - -}