diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2015-01-25 15:08:57 +0100 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2015-01-25 15:08:57 +0100 |
commit | 11dbaa746303b312dae53b8e83a21034e89f3fa6 (patch) | |
tree | 3653fed26bc81dd352cfe8bac1d60126ad9b3a8b /main | |
parent | 82266c9f64604d142804501ee08b4df041b6212d (diff) | |
download | cgeo-11dbaa746303b312dae53b8e83a21034e89f3fa6.zip cgeo-11dbaa746303b312dae53b8e83a21034e89f3fa6.tar.gz cgeo-11dbaa746303b312dae53b8e83a21034e89f3fa6.tar.bz2 |
fix #3082: confirmation on backup/restore
Diffstat (limited to 'main')
-rw-r--r-- | main/res/values/strings.xml | 2 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/settings/SettingsActivity.java | 3 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java | 51 |
3 files changed, 49 insertions, 7 deletions
diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml index 2d8bdf7..2b23b08 100644 --- a/main/res/values/strings.xml +++ b/main/res/values/strings.xml @@ -497,6 +497,8 @@ <string name="init_backup_success">c:geo\'s database was successfully copied to:</string> <string name="init_backup_failed">Backup of c:geo\'s database failed.</string> <string name="init_backup_unnecessary">Database is empty, no backup necessary.</string> + <string name="backup_confirm_overwrite">Do you want to overwrite the existing backup from %s?</string> + <string name="restore_confirm_overwrite">Do you want to overwrite %s on your device with the backup?</string> <string name="init_restore_success">Restoration completed.</string> <string name="init_restore_failed">Restoration failed.</string> <string name="init_restore_running">Restoring cache database…</string> diff --git a/main/src/cgeo/geocaching/settings/SettingsActivity.java b/main/src/cgeo/geocaching/settings/SettingsActivity.java index 99de30f..776b621 100644 --- a/main/src/cgeo/geocaching/settings/SettingsActivity.java +++ b/main/src/cgeo/geocaching/settings/SettingsActivity.java @@ -327,13 +327,14 @@ public class SettingsActivity extends PreferenceActivity { backup.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(final Preference preference) { - return DatabaseBackupUtils.createBackup(SettingsActivity.this, new Runnable() { + DatabaseBackupUtils.createBackup(SettingsActivity.this, new Runnable() { @Override public void run() { VALUE_CHANGE_LISTENER.onPreferenceChange(SettingsActivity.this.getPreference(R.string.pref_fakekey_preference_backup_info), ""); } }); + return true; } }); diff --git a/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java b/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java index e7e793f..edfa4f0 100644 --- a/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java +++ b/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java @@ -14,6 +14,8 @@ import rx.schedulers.Schedulers; import android.app.Activity; import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; import android.content.res.Resources; import java.io.File; @@ -26,12 +28,30 @@ public class DatabaseBackupUtils { } /** - * restore the database in a new thread, showing a progress window + * After confirming to overwrite the existing caches on the devices, restore the database in a new thread, showing a + * progress window * * @param activity * calling activity */ public static void restoreDatabase(final Activity activity) { + final int caches = DataStore.getAllCachesCount(); + if (caches == 0) { + restoreDatabaseInternal(activity); + } + else { + Dialogs.confirm(activity, R.string.init_backup_restore, activity.getString(R.string.restore_confirm_overwrite, activity.getResources().getQuantityString(R.plurals.cache_counts, caches)), new OnClickListener() { + + @Override + public void onClick(final DialogInterface dialog, final int which) { + restoreDatabaseInternal(activity); + } + }); + + } + } + + private static void restoreDatabaseInternal(final Activity activity) { final Resources res = activity.getResources(); final ProgressDialog dialog = ProgressDialog.show(activity, res.getString(R.string.init_backup_restore), res.getString(R.string.init_restore_running), true, false); final AtomicBoolean restoreSuccessful = new AtomicBoolean(false); @@ -44,8 +64,8 @@ public class DatabaseBackupUtils { @Override public void call() { dialog.dismiss(); - boolean restored = restoreSuccessful.get(); - String message = restored ? res.getString(R.string.init_restore_success) : res.getString(R.string.init_restore_failed); + final boolean restored = restoreSuccessful.get(); + final String message = restored ? res.getString(R.string.init_restore_success) : res.getString(R.string.init_restore_failed); Dialogs.message(activity, R.string.init_backup_restore, message); if (activity instanceof MainActivity) { ((MainActivity) activity).updateCacheCounter(); @@ -54,14 +74,34 @@ public class DatabaseBackupUtils { }); } - public static boolean createBackup(final Activity activity, final Runnable runAfterwards) { + /** + * Create a backup after confirming to overwrite the existing backup. + * + * @param activity + * @param runAfterwards + */ + public static void createBackup(final Activity activity, final Runnable runAfterwards) { // avoid overwriting an existing backup with an empty database // (can happen directly after reinstalling the app) if (DataStore.getAllCachesCount() == 0) { Dialogs.message(activity, R.string.init_backup, R.string.init_backup_unnecessary); - return false; + return; + } + if (hasBackup()) { + Dialogs.confirm(activity, R.string.init_backup, activity.getString(R.string.backup_confirm_overwrite, getBackupDateTime()), new OnClickListener() { + + @Override + public void onClick(final DialogInterface dialog, final int which) { + createBackupInternal(activity, runAfterwards); + } + }); } + else { + createBackupInternal(activity, runAfterwards); + } + } + private static void createBackupInternal(final Activity activity, final Runnable runAfterwards) { final ProgressDialog dialog = ProgressDialog.show(activity, activity.getString(R.string.init_backup), activity.getString(R.string.init_backup_running), true, false); @@ -85,7 +125,6 @@ public class DatabaseBackupUtils { } } }); - return true; } public static File getRestoreFile() { |