Revert all commits related to ContentSettingsFragment

Revert "Annotate methode parameters as NonNull"
This reverts commit 004907d306.

Revert "Commit path immediately when import backup"
This reverts commit 05eb0d0fbe.

Revert "Set ImportExportDataPath only on successful import"
This reverts commit f13a1b04e6.

Revert "Set ImportExportDataPath only on successful export"
This reverts commit fd4408e572.

Revert "Invert if condition in ContentSettingsFragment.setImportExportDataPath for better readability"
This reverts commit 92ab9cae27.

Revert "Move ContentSettingsFragment.isValidPath to helpers and add unit test for it."
This reverts commit fa2b11b768.

Revert "Save backup import/export location for feature import/exports"
This reverts commit 82f43ac6a6.

Remove FilePathHelperTest file
This commit is contained in:
Stypox 2021-06-05 23:52:39 +02:00
parent 7c78d963d9
commit 1e09a1768e
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
4 changed files with 16 additions and 151 deletions

View File

@ -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();
}
}
}

View File

@ -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();
}
}

View File

@ -265,7 +265,6 @@
</string-array>
<string name="feed_use_dedicated_fetch_method_key" translatable="false">feed_use_dedicated_fetch_method</string>
<string name="import_export_data_path" translatable="false">import_export_data_path</string>
<string name="import_data" translatable="false">import_data</string>
<string name="export_data" translatable="false">export_data</string>

View File

@ -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()));
}
}