mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-01-22 07:40:03 +01:00
Merge pull request #6319 from ATofighi/feat-6039-store-backup-location
Save backup import/export location for future import/exports
This commit is contained in:
commit
fc7944d287
@ -26,6 +26,7 @@ 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.FilePickerActivityHelper;
|
||||||
|
import org.schabi.newpipe.util.FilePathUtils;
|
||||||
import org.schabi.newpipe.util.ZipHelper;
|
import org.schabi.newpipe.util.ZipHelper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -41,6 +42,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||||||
|
|
||||||
private ContentSettingsManager manager;
|
private ContentSettingsManager manager;
|
||||||
|
|
||||||
|
private String importExportDataPathKey;
|
||||||
|
|
||||||
private String thumbnailLoadToggleKey;
|
private String thumbnailLoadToggleKey;
|
||||||
private String youtubeRestrictedModeEnabledKey;
|
private String youtubeRestrictedModeEnabledKey;
|
||||||
|
|
||||||
@ -56,6 +59,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||||||
|
|
||||||
addPreferencesFromResource(R.xml.content_settings);
|
addPreferencesFromResource(R.xml.content_settings);
|
||||||
|
|
||||||
|
importExportDataPathKey = getString(R.string.import_export_data_path);
|
||||||
final Preference importDataPreference = findPreference(getString(R.string.import_data));
|
final Preference importDataPreference = findPreference(getString(R.string.import_data));
|
||||||
importDataPreference.setOnPreferenceClickListener(p -> {
|
importDataPreference.setOnPreferenceClickListener(p -> {
|
||||||
final Intent i = new Intent(getActivity(), FilePickerActivityHelper.class)
|
final Intent i = new Intent(getActivity(), FilePickerActivityHelper.class)
|
||||||
@ -63,6 +67,10 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||||||
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false)
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false)
|
||||||
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
|
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
|
||||||
FilePickerActivityHelper.MODE_FILE);
|
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);
|
startActivityForResult(i, REQUEST_IMPORT_PATH);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@ -74,6 +82,10 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||||||
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true)
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true)
|
||||||
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
|
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
|
||||||
FilePickerActivityHelper.MODE_DIR);
|
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);
|
startActivityForResult(i, REQUEST_EXPORT_PATH);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@ -164,7 +176,10 @@ 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 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) {
|
if (requestCode == REQUEST_EXPORT_PATH) {
|
||||||
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
|
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
|
||||||
exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip");
|
exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip");
|
||||||
@ -239,4 +254,19 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||||||
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
|
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setImportExportDataPath(final File file) {
|
||||||
|
final String directoryPath;
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
directoryPath = file.getAbsolutePath();
|
||||||
|
} else {
|
||||||
|
final File parentFile = file.getParentFile();
|
||||||
|
if (parentFile != null) {
|
||||||
|
directoryPath = parentFile.getAbsolutePath();
|
||||||
|
} else {
|
||||||
|
directoryPath = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defaultPreferences.edit().putString(importExportDataPathKey, directoryPath).apply();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
22
app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java
Normal file
22
app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@ -265,6 +265,7 @@
|
|||||||
</string-array>
|
</string-array>
|
||||||
<string name="feed_use_dedicated_fetch_method_key" translatable="false">feed_use_dedicated_fetch_method</string>
|
<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="import_data" translatable="false">import_data</string>
|
||||||
<string name="export_data" translatable="false">export_data</string>
|
<string name="export_data" translatable="false">export_data</string>
|
||||||
|
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user