Set ImportExportDataPath only on successful import

Also set the folder instead of the file itself as path
This commit is contained in:
XiangRongLin 2021-05-31 12:38:21 +02:00
parent fd4408e572
commit f13a1b04e6

View file

@ -177,7 +177,6 @@ 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 = file.getAbsolutePath();
if (requestCode == REQUEST_EXPORT_PATH) {
exportDatabase(file);
@ -185,7 +184,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
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();
@ -214,11 +213,13 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
}
}
private void importDatabase(final String filePath) {
private void importDatabase(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;
}
@ -239,26 +240,37 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
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);
}
}
/**
* Save import path and restart system.
*
* @param file The file of the created backup
*/
private void finishImport(final File file) {
if (file.getParentFile() != null) {
setImportExportDataPath(file.getParentFile());
}
// restart app to properly load db
System.exit(0);
}
private void setImportExportDataPath(final File file) {
final String directoryPath;
if (file.isDirectory()) {