From 6fb96e18a91cb940e4729b46f943746d45dc9fb5 Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Tue, 14 Nov 2017 15:44:53 +0100 Subject: [PATCH] Add the option to append entries during restore --- .../andotp/Activities/BackupActivity.java | 32 +++++++++++++++++-- .../andotp/Utilities/DatabaseHelper.java | 15 +++------ app/src/main/res/layout/content_backup.xml | 26 +++++++++++++++ app/src/main/res/values/strings_backup.xml | 2 ++ 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java index e596f64a..627127c0 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java @@ -40,6 +40,7 @@ import android.util.Log; import android.view.View; import android.view.ViewStub; import android.widget.LinearLayout; +import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; @@ -92,6 +93,8 @@ public class BackupActivity extends BaseActivity { private Uri encryptTargetFile; private Uri decryptSourceFile; + private Switch replace; + private boolean reload = false; @Override @@ -193,6 +196,8 @@ public class BackupActivity extends BaseActivity { }); } + replace = v.findViewById(R.id.backup_replace); + } // End with a result @@ -361,10 +366,18 @@ public class BackupActivity extends BaseActivity { private void doRestorePlain(Uri uri) { if (Tools.isExternalStorageReadable()) { - boolean success = DatabaseHelper.importFromJSON(this, uri); + ArrayList entries = DatabaseHelper.importFromJSON(this, uri); - if (success) { + if (entries != null) { + if (! replace.isChecked()) { + ArrayList currentEntries = DatabaseHelper.loadDatabase(this); + entries.removeAll(currentEntries); + entries.addAll(currentEntries); + } + + DatabaseHelper.saveDatabase(this, entries); reload = true; + Toast.makeText(this, R.string.backup_toast_import_success, Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, R.string.backup_toast_import_failed, Toast.LENGTH_LONG).show(); @@ -427,6 +440,13 @@ public class BackupActivity extends BaseActivity { byte[] decrypted = EncryptionHelper.decrypt(key, encrypted); ArrayList entries = DatabaseHelper.stringToEntries(new String(decrypted, StandardCharsets.UTF_8)); + + if (! replace.isChecked()) { + ArrayList currentEntries = DatabaseHelper.loadDatabase(this); + entries.removeAll(currentEntries); + entries.addAll(currentEntries); + } + DatabaseHelper.saveDatabase(this, entries); } catch (Exception e) { e.printStackTrace(); @@ -491,9 +511,15 @@ public class BackupActivity extends BaseActivity { ArrayList entries = DatabaseHelper.stringToEntries(content); if (entries.size() > 0) { - DatabaseHelper.saveDatabase(this, entries); + if (! replace.isChecked()) { + ArrayList currentEntries = DatabaseHelper.loadDatabase(this); + entries.removeAll(currentEntries); + entries.addAll(currentEntries); + } + DatabaseHelper.saveDatabase(this, entries); reload = true; + Toast.makeText(this, R.string.backup_toast_import_success, Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, R.string.backup_toast_import_failed, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java index 68c3c3e1..477e1c93 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java @@ -113,19 +113,14 @@ public class DatabaseHelper { return FileHelper.writeStringToFile(context, file, entriesToString(entries)); } - public static boolean importFromJSON(Context context, Uri file) { - boolean success = false; - + public static ArrayList importFromJSON(Context context, Uri file) { String content = FileHelper.readFileToString(context, file); - if (! content.isEmpty()) { - ArrayList entries = stringToEntries(content); + ArrayList entries = null; - saveDatabase(context, entries); + if (! content.isEmpty()) + entries = stringToEntries(content); - success = true; - } - - return success; + return entries; } } \ No newline at end of file diff --git a/app/src/main/res/layout/content_backup.xml b/app/src/main/res/layout/content_backup.xml index cce0cd2e..4cc475bb 100644 --- a/app/src/main/res/layout/content_backup.xml +++ b/app/src/main/res/layout/content_backup.xml @@ -191,5 +191,31 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings_backup.xml b/app/src/main/res/values/strings_backup.xml index 0d8d24c4..c15347d6 100644 --- a/app/src/main/res/values/strings_backup.xml +++ b/app/src/main/res/values/strings_backup.xml @@ -31,6 +31,8 @@ before you can create encrypted backups. + Replace already existing entries + Security warning