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) if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH)
&& resultCode == Activity.RESULT_OK && data.getData() != null) { && resultCode == Activity.RESULT_OK && data.getData() != null) {
final File file = Utils.getFileForUri(data.getData()); final File file = Utils.getFileForUri(data.getData());
final String path = file.getAbsolutePath();
if (requestCode == REQUEST_EXPORT_PATH) { if (requestCode == REQUEST_EXPORT_PATH) {
exportDatabase(file); exportDatabase(file);
@ -185,7 +184,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setMessage(R.string.override_current_data) builder.setMessage(R.string.override_current_data)
.setPositiveButton(getString(R.string.finish), .setPositiveButton(getString(R.string.finish),
(d, id) -> importDatabase(path)) (d, id) -> importDatabase(file))
.setNegativeButton(android.R.string.cancel, .setNegativeButton(android.R.string.cancel,
(d, id) -> d.cancel()); (d, id) -> d.cancel());
builder.create().show(); 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 // check if file is supported
if (!ZipHelper.isValidZipFile(filePath)) { if (!ZipHelper.isValidZipFile(filePath)) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT) Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
.show(); .show();
return; return;
} }
@ -239,26 +240,37 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
alert.setNegativeButton(android.R.string.no, (dialog, which) -> { alert.setNegativeButton(android.R.string.no, (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
// restart app to properly load db finishImport(file);
System.exit(0);
}); });
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> { alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
manager.loadSharedPreferences(PreferenceManager manager.loadSharedPreferences(PreferenceManager
.getDefaultSharedPreferences(requireContext())); .getDefaultSharedPreferences(requireContext()));
// restart app to properly load db finishImport(file);
System.exit(0);
}); });
alert.show(); alert.show();
} else { } else {
// restart app to properly load db finishImport(file);
System.exit(0);
} }
} catch (final Exception e) { } catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", 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) { private void setImportExportDataPath(final File file) {
final String directoryPath; final String directoryPath;
if (file.isDirectory()) { if (file.isDirectory()) {