Add the option to append entries during restore

This commit is contained in:
Jakob Nixdorf 2017-11-14 15:44:53 +01:00
parent 9e6a235e62
commit 6fb96e18a9
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
4 changed files with 62 additions and 13 deletions

View file

@ -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<Entry> entries = DatabaseHelper.importFromJSON(this, uri);
if (success) {
if (entries != null) {
if (! replace.isChecked()) {
ArrayList<Entry> 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<Entry> entries = DatabaseHelper.stringToEntries(new String(decrypted, StandardCharsets.UTF_8));
if (! replace.isChecked()) {
ArrayList<Entry> 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<Entry> entries = DatabaseHelper.stringToEntries(content);
if (entries.size() > 0) {
DatabaseHelper.saveDatabase(this, entries);
if (! replace.isChecked()) {
ArrayList<Entry> 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();

View file

@ -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<Entry> importFromJSON(Context context, Uri file) {
String content = FileHelper.readFileToString(context, file);
if (! content.isEmpty()) {
ArrayList<Entry> entries = stringToEntries(content);
ArrayList<Entry> entries = null;
saveDatabase(context, entries);
if (! content.isEmpty())
entries = stringToEntries(content);
success = true;
}
return success;
return entries;
}
}

View file

@ -191,5 +191,31 @@
</LinearLayout>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:layout_gravity="center"
android:padding="@dimen/activity_margin"
android:background="?android:attr/selectableItemBackground" >
<CheckedTextView
android:id="@+id/backup_replace_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toStartOf="@id/backup_replace"
android:text="@string/backup_desc_replace"
android:textAppearance="?android:attr/textAppearanceListItem" />
<Switch
android:id="@+id/backup_replace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:checked="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

View file

@ -31,6 +31,8 @@
before you can create encrypted backups.
</string>
<string name="backup_desc_replace">Replace already existing entries</string>
<!-- Dialogs -->
<string name="backup_dialog_title_security_warning">Security warning</string>