diff --git a/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java b/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java index 6133403b..9dc3ee4a 100644 --- a/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java +++ b/app/src/androidTest/java/org/shadowice/flocke/andotp/ApplicationTest.java @@ -131,7 +131,7 @@ public class ApplicationTest extends ApplicationTestCase { new File(context.getFilesDir() + "/" + DatabaseHelper.SETTINGS_FILE).delete(); new File(context.getFilesDir() + "/" + DatabaseHelper.KEY_FILE).delete(); - ArrayList b = DatabaseHelper.load(context); + ArrayList b = DatabaseHelper.loadDatabase(context); assertEquals(0, b.size()); ArrayList a = new ArrayList<>(); @@ -145,8 +145,8 @@ public class ApplicationTest extends ApplicationTestCase { e.setSecret("secret2".getBytes()); a.add(e); - DatabaseHelper.store(context, a); - b = DatabaseHelper.load(context); + DatabaseHelper.saveDatabase(context, a); + b = DatabaseHelper.loadDatabase(context); assertEquals(a, b); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java index 87a5178e..3e52a0ff 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java @@ -331,10 +331,10 @@ public class BackupActivity extends AppCompatActivity { private void doRestoreEncrypted(String content) { if (StorageHelper.isExternalStorageReadable()) { - ArrayList entries = DatabaseHelper.stringToEntries(this, content); + ArrayList entries = DatabaseHelper.stringToEntries(content); if (entries.size() > 0) { - DatabaseHelper.store(this, entries); + DatabaseHelper.saveDatabase(this, entries); reload = true; Toast.makeText(this, R.string.backup_toast_import_success, Toast.LENGTH_LONG).show(); @@ -383,7 +383,8 @@ public class BackupActivity extends AppCompatActivity { } private void backupEncryptedWithPGP(Uri uri) { - String plainJSON = DatabaseHelper.entriesToString(this); + ArrayList entries = DatabaseHelper.loadDatabase(this); + String plainJSON = DatabaseHelper.entriesToString(entries); Intent encryptIntent = new Intent(); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/DatabaseHelper.java b/app/src/main/java/org/shadowice/flocke/andotp/DatabaseHelper.java index e9edb02f..c4a41d22 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/DatabaseHelper.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/DatabaseHelper.java @@ -28,62 +28,43 @@ import android.net.Uri; import org.json.JSONArray; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.util.ArrayList; import javax.crypto.SecretKey; -import static org.shadowice.flocke.andotp.Utils.readFully; -import static org.shadowice.flocke.andotp.Utils.writeFully; - public class DatabaseHelper { public static final String KEY_FILE = "otp.key"; public static final String SETTINGS_FILE = "secrets.dat"; - public static void store(Context context, ArrayList entries) { - JSONArray a = new JSONArray(); + /* Database functions */ - for(Entry e: entries){ - try { - a.put(e.toJSON()); - } catch (Exception error) { - error.printStackTrace(); - } - } + public static void saveDatabase(Context context, ArrayList entries) { + String jsonString = entriesToString(entries); try { - byte[] data = a.toString().getBytes(); + byte[] data = jsonString.getBytes(); SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE)); data = EncryptionHelper.encrypt(key,data); - writeFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE), data); + Utils.writeFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE), data); } catch (Exception error) { error.printStackTrace(); } } - public static ArrayList load(Context context){ + public static ArrayList loadDatabase(Context context){ ArrayList entries = new ArrayList<>(); try { - byte[] data = readFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE)); + byte[] data = Utils.readFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE)); SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE)); data = EncryptionHelper.decrypt(key, data); - JSONArray json = new JSONArray(new String(data)); - - for (int i = 0; i < json.length(); i++) { - entries.add(new Entry(json.getJSONObject(i))); - } + entries = stringToEntries(new String(data)); } catch (Exception error) { error.printStackTrace(); } @@ -91,9 +72,9 @@ public class DatabaseHelper { return entries; } - public static String entriesToString(Context context) { - ArrayList entries = load(context); + /* Conversion functions */ + public static String entriesToString(ArrayList entries) { JSONArray json = new JSONArray(); for(Entry e: entries){ @@ -107,11 +88,7 @@ public class DatabaseHelper { return json.toString(); } - public static boolean exportAsJSON(Context context, Uri file) { - return FileHelper.writeStringToFile(context, file, entriesToString(context)); - } - - public static ArrayList stringToEntries(Context context, String data) { + public static ArrayList stringToEntries(String data) { ArrayList entries = new ArrayList<>(); try { @@ -127,13 +104,26 @@ public class DatabaseHelper { return entries; } + /* Export functions */ + + public static boolean exportAsJSON(Context context, Uri file) { + ArrayList entries = loadDatabase(context); + + return FileHelper.writeStringToFile(context, file, entriesToString(entries)); + } + public static boolean importFromJSON(Context context, Uri file) { - boolean success = true; + boolean success = false; String content = FileHelper.readFileToString(context, file); - ArrayList entries = stringToEntries(context, content); - store(context, entries); + if (! content.isEmpty()) { + ArrayList entries = stringToEntries(content); + + saveDatabase(context, entries); + + success = true; + } return success; } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java index 906f238a..c0f52154 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java @@ -93,11 +93,11 @@ public class EntriesCardAdapter extends RecyclerView.Adapter