Merge pull request #6531 from XiangRongLin/immediat_pref_commit

Remove option to immediately commit pref changes on import
This commit is contained in:
Tobi 2021-07-14 09:48:58 +02:00 committed by GitHub
commit 1584028995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 32 deletions

View File

@ -12,7 +12,6 @@ import android.widget.Toast;
import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.preference.Preference; import androidx.preference.Preference;
@ -33,6 +32,7 @@ import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.ZipHelper; import org.schabi.newpipe.util.ZipHelper;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
@ -43,7 +43,8 @@ import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
public class ContentSettingsFragment extends BasePreferenceFragment { public class ContentSettingsFragment extends BasePreferenceFragment {
private static final String ZIP_MIME_TYPE = "application/zip"; private static final String ZIP_MIME_TYPE = "application/zip";
private static final SimpleDateFormat EXPORT_DATE_FORMAT
private final SimpleDateFormat exportDateFormat
= new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
private ContentSettingsManager manager; private ContentSettingsManager manager;
@ -52,7 +53,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
private String thumbnailLoadToggleKey; private String thumbnailLoadToggleKey;
private String youtubeRestrictedModeEnabledKey; private String youtubeRestrictedModeEnabledKey;
@Nullable private Uri lastImportExportDataUri = null;
private Localization initialSelectedLocalization; private Localization initialSelectedLocalization;
private ContentCountry initialSelectedContentCountry; private ContentCountry initialSelectedContentCountry;
private String initialLanguage; private String initialLanguage;
@ -86,7 +86,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
requestExportPathLauncher.launch( requestExportPathLauncher.launch(
StoredFileHelper.getNewPicker(requireContext(), StoredFileHelper.getNewPicker(requireContext(),
"NewPipeData-" + EXPORT_DATE_FORMAT.format(new Date()) + ".zip", "NewPipeData-" + exportDateFormat.format(new Date()) + ".zip",
ZIP_MIME_TYPE, getImportExportDataUri())); ZIP_MIME_TYPE, getImportExportDataUri()));
return true; return true;
}); });
@ -162,19 +162,21 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
private void requestExportPathResult(final ActivityResult result) { private void requestExportPathResult(final ActivityResult result) {
assureCorrectAppLanguage(getContext()); assureCorrectAppLanguage(getContext());
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
lastImportExportDataUri = result.getData().getData(); // will be saved only on success // will be saved only on success
final Uri lastExportDataUri = result.getData().getData();
final StoredFileHelper file final StoredFileHelper file
= new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);
exportDatabase(file); exportDatabase(file, lastExportDataUri);
} }
} }
private void requestImportPathResult(final ActivityResult result) { private void requestImportPathResult(final ActivityResult result) {
assureCorrectAppLanguage(getContext()); assureCorrectAppLanguage(getContext());
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
lastImportExportDataUri = result.getData().getData(); // will be saved only on success // will be saved only on success
final Uri lastImportDataUri = result.getData().getData();
final StoredFileHelper file final StoredFileHelper file
= new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);
@ -182,7 +184,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
new AlertDialog.Builder(requireActivity()) new AlertDialog.Builder(requireActivity())
.setMessage(R.string.override_current_data) .setMessage(R.string.override_current_data)
.setPositiveButton(R.string.finish, (d, id) -> .setPositiveButton(R.string.finish, (d, id) ->
importDatabase(file)) importDatabase(file, lastImportDataUri))
.setNegativeButton(R.string.cancel, (d, id) -> .setNegativeButton(R.string.cancel, (d, id) ->
d.cancel()) d.cancel())
.create() .create()
@ -190,33 +192,33 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
} }
} }
private void exportDatabase(final StoredFileHelper file) { private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri) {
try { try {
//checkpoint before export //checkpoint before export
NewPipeDatabase.checkpoint(); NewPipeDatabase.checkpoint();
final SharedPreferences preferences = PreferenceManager final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(requireContext()); .getDefaultSharedPreferences(requireContext());
manager.exportDatabase(preferences, file); manager.exportDatabase(preferences, file);
saveLastImportExportDataUri(false); // save export path only on success saveLastImportExportDataUri(exportDataUri); // save export path only on success
Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show();
} catch (final Exception e) { } catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e); ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e);
} }
} }
private void importDatabase(final StoredFileHelper file) { private void importDatabase(final StoredFileHelper file, final Uri importDataUri) {
// check if file is supported // check if file is supported
if (!ZipHelper.isValidZipFile(file)) { if (!ZipHelper.isValidZipFile(file)) {
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;
} }
try { try {
if (!manager.ensureDbDirectoryExists()) { if (!manager.ensureDbDirectoryExists()) {
throw new Exception("Could not create databases dir"); throw new IOException("Could not create databases dir");
} }
if (!manager.extractDb(file)) { if (!manager.extractDb(file)) {
@ -231,17 +233,17 @@ 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();
finishImport(); finishImport(importDataUri);
}); });
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()));
finishImport(); finishImport(importDataUri);
}); });
alert.show(); alert.show();
} else { } else {
finishImport(); finishImport(importDataUri);
} }
} catch (final Exception e) { } catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
@ -250,10 +252,12 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
/** /**
* Save import path and restart system. * Save import path and restart system.
*
* @param importDataUri The import path to save
*/ */
private void finishImport() { private void finishImport(final Uri importDataUri) {
// save import path only on success; save immediately because app is about to exit // save import path only on success
saveLastImportExportDataUri(true); saveLastImportExportDataUri(importDataUri);
// restart app to properly load db // restart app to properly load db
NavigationHelper.restartApp(requireActivity()); NavigationHelper.restartApp(requireActivity());
} }
@ -263,16 +267,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
return isBlank(path) ? null : Uri.parse(path); return isBlank(path) ? null : Uri.parse(path);
} }
private void saveLastImportExportDataUri(final boolean immediately) { private void saveLastImportExportDataUri(final Uri importExportDataUri) {
if (lastImportExportDataUri != null) { final SharedPreferences.Editor editor = defaultPreferences.edit()
final SharedPreferences.Editor editor = defaultPreferences.edit() .putString(importExportDataPathKey, importExportDataUri.toString());
.putString(importExportDataPathKey, lastImportExportDataUri.toString()); editor.apply();
if (immediately) {
// noinspection ApplySharedPref
editor.commit(); // app about to be restarted, commit immediately
} else {
editor.apply();
}
}
} }
} }