From b42afcd32cc082d45daf64e37d89ba88f0c3eb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 4 Aug 2014 11:19:46 +0200 Subject: [PATCH] Fix debug backup/restore methods --- OpenKeychain/src/main/AndroidManifest.xml | 2 +- .../keychain/provider/KeychainDatabase.java | 41 ++++++++----------- .../keychain/ui/KeyListActivity.java | 12 +++--- .../res/layout/view_key_share_fragment.xml | 1 - 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/OpenKeychain/src/main/AndroidManifest.xml b/OpenKeychain/src/main/AndroidManifest.xml index df5eaa69e..c10629c6d 100644 --- a/OpenKeychain/src/main/AndroidManifest.xml +++ b/OpenKeychain/src/main/AndroidManifest.xml @@ -177,7 +177,7 @@ android:windowSoftInputMode="stateHidden"> - + diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java index 7a63ec3d7..ebc53ddf6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java @@ -316,42 +316,37 @@ public class KeychainDatabase extends SQLiteOpenHelper { } private static void copy(File in, File out) throws IOException { - FileInputStream ss = new FileInputStream(in); - FileOutputStream ds = new FileOutputStream(out); + FileInputStream is = new FileInputStream(in); + FileOutputStream os = new FileOutputStream(out); byte[] buf = new byte[512]; - while (ss.available() > 0) { - int count = ss.read(buf, 0, 512); - ds.write(buf, 0, count); + while (is.available() > 0) { + int count = is.read(buf, 0, 512); + os.write(buf, 0, count); } } - public static void debugRead(Context context) throws IOException { + public static void debugBackup(Context context, boolean restore) throws IOException { if (!Constants.DEBUG) { return; } - File in = context.getDatabasePath("debug.db"); - File out = context.getDatabasePath("openkeychain.db"); + + File in; + File out; + if (restore) { + in = context.getDatabasePath("debug_backup.db"); + out = context.getDatabasePath("openkeychain.db"); + } else { + in = context.getDatabasePath("openkeychain.db"); + out = context.getDatabasePath("debug_backup.db"); + out.createNewFile(); + } if (!in.canRead()) { throw new IOException("Cannot read " + in.getName()); } - if (!out.canRead()) { + if (!out.canWrite()) { throw new IOException("Cannot write " + out.getName()); } copy(in, out); } - public static void debugWrite(Context context) throws IOException { - if (!Constants.DEBUG) { - return; - } - File in = context.getDatabasePath("openkeychain.db"); - File out = context.getDatabasePath("debug.db"); - if (!in.canRead()) { - throw new IOException("Cannot read " + in.getName()); - } - if (!out.canRead()) { - throw new IOException("Cannot write " + out.getName()); - } - copy(in, out); - } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index 50ff5c753..7a6e78a7d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -94,22 +94,22 @@ public class KeyListActivity extends DrawerActivity { case R.id.menu_key_list_debug_read: try { - KeychainDatabase.debugRead(this); - Notify.showNotify(this, "Restored Notify.Style backup", Notify.Style.INFO); + KeychainDatabase.debugBackup(this, true); + Notify.showNotify(this, "Restored debug_backup.db", Notify.Style.INFO); getContentResolver().notifyChange(KeychainContract.KeyRings.CONTENT_URI, null); } catch (IOException e) { Log.e(Constants.TAG, "IO Error", e); - Notify.showNotify(this, "IO Notify.Style: " + e.getMessage(), Notify.Style.ERROR); + Notify.showNotify(this, "IO Error " + e.getMessage(), Notify.Style.ERROR); } return true; case R.id.menu_key_list_debug_write: try { - KeychainDatabase.debugWrite(this); - Notify.showNotify(this, "Backup Notify.Style", Notify.Style.INFO); + KeychainDatabase.debugBackup(this, false); + Notify.showNotify(this, "Backup to debug_backup.db completed", Notify.Style.INFO); } catch(IOException e) { Log.e(Constants.TAG, "IO Error", e); - Notify.showNotify(this, "IO Notify.Style: " + e.getMessage(), Notify.Style.ERROR); + Notify.showNotify(this, "IO Error: " + e.getMessage(), Notify.Style.ERROR); } return true; diff --git a/OpenKeychain/src/main/res/layout/view_key_share_fragment.xml b/OpenKeychain/src/main/res/layout/view_key_share_fragment.xml index a8786c461..7905473c0 100644 --- a/OpenKeychain/src/main/res/layout/view_key_share_fragment.xml +++ b/OpenKeychain/src/main/res/layout/view_key_share_fragment.xml @@ -133,7 +133,6 @@ android:id="@+id/view_key_action_upload" android:paddingLeft="8dp" android:paddingRight="8dp" - android:layout_marginBottom="8dp" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="match_parent" android:layout_height="wrap_content"