Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improve setting import and export paths #6402

Merged
merged 4 commits into from
Jun 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.schabi.newpipe.settings;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand All @@ -25,8 +26,8 @@
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.FilePickerActivityHelper;
import org.schabi.newpipe.util.FilePathUtils;
import org.schabi.newpipe.util.FilePickerActivityHelper;
import org.schabi.newpipe.util.ZipHelper;

import java.io.File;
Expand Down Expand Up @@ -177,44 +178,49 @@ 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 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");
exportDatabase(file);
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setMessage(R.string.override_current_data)
.setPositiveButton(getString(R.string.finish),
(d, id) -> importDatabase(path))
(d, id) -> importDatabase(file))
.setNegativeButton(android.R.string.cancel,
(d, id) -> d.cancel());
builder.create().show();
}
}
}

private void exportDatabase(final String path) {
private void exportDatabase(@NonNull final File folder) {
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(final String filePath) {
private void importDatabase(@NonNull final File file) {
final String filePath = file.getAbsolutePath();

// check if file is supported
if (!ZipHelper.isValidZipFile(filePath)) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
.show();
.show();
return;
}

Expand All @@ -225,7 +231,7 @@ private void importDatabase(final String filePath) {

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.
Expand All @@ -235,27 +241,40 @@ private void importDatabase(final String filePath) {

alert.setNegativeButton(android.R.string.no, (dialog, which) -> {
dialog.dismiss();
// restart app to properly load db
System.exit(0);
finishImport(file);
});
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
dialog.dismiss();
manager.loadSharedPreferences(PreferenceManager
.getDefaultSharedPreferences(requireContext()));
// restart app to properly load db
System.exit(0);
.getDefaultSharedPreferences(requireContext()));
finishImport(file);
});
alert.show();
} else {
// restart app to properly load db
System.exit(0);
finishImport(file);
}
} catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
}
}

private void setImportExportDataPath(final File file) {
/**
* 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();
Expand All @@ -267,6 +286,13 @@ private void setImportExportDataPath(final File file) {
directoryPath = "";
}
}
defaultPreferences.edit().putString(importExportDataPathKey, directoryPath).apply();
final SharedPreferences.Editor editor = defaultPreferences
.edit()
.putString(importExportDataPathKey, directoryPath);
if (immediately) {
editor.commit();
} else {
editor.apply();
}
}
}