Skip to content

Commit

Permalink
Save backup import/export location for feature import/exports
Browse files Browse the repository at this point in the history
  • Loading branch information
ATofighi committed May 17, 2021
1 parent 2d4a3c2 commit 82f43ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment {

private ContentSettingsManager manager;

private String importExportDataPathKey;

private String thumbnailLoadToggleKey;
private String youtubeRestrictedModeEnabledKey;

Expand All @@ -56,13 +58,18 @@ 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)
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
.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;
});
Expand All @@ -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;
});
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
</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>

Expand Down

0 comments on commit 82f43ac

Please sign in to comment.