diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index 3a487e6871..bdac882596 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -220,6 +220,86 @@ public abstract class DB extends RoomDatabase { }); } + static void createEmergencyBackup(Context context) { + Log.i("Creating emergency backup"); + try { + DB db = DB.getInstance(context); + + JSONArray jaccounts = new JSONArray(); + List accounts = db.account().getAccounts(); + for (EntityAccount account : accounts) { + JSONObject jaccount = account.toJSON(); + + JSONArray jfolders = new JSONArray(); + List folders = db.folder().getFolders(account.id, false, true); + for (EntityFolder folder : folders) + jfolders.put(folder.toJSON()); + jaccount.put("folders", jfolders); + + JSONArray jidentities = new JSONArray(); + List identities = db.identity().getIdentities(account.id); + for (EntityIdentity identity : identities) + jidentities.put(identity.toJSON()); + jaccount.put("identities", jidentities); + + jaccounts.put(jaccount); + } + + File emergency = new File(context.getFilesDir(), "emergency.json"); + Helper.writeText(emergency, jaccounts.toString(2)); + } catch (Throwable ex) { + Log.e(ex); + } + } + + static void checkEmergencybackup(Context context) { + try { + File dbfile = context.getDatabasePath(DB_NAME); + if (dbfile.exists()) { + Log.i("Emergency restore /dbfile"); + return; + } + + File emergency = new File(context.getFilesDir(), "emergency.json"); + if (!emergency.exists()) { + Log.i("Emergency restore /json"); + return; + } + + DB db = DB.getInstance(context); + if (db.account().getAccounts().size() > 0) { + Log.e("Emergency restore /accounts"); + return; + } + + Log.e("Emergency restore"); + + String json = Helper.readText(emergency); + JSONArray jaccounts = new JSONArray(json); + for (int a = 0; a < jaccounts.length(); a++) { + JSONObject jaccount = jaccounts.getJSONObject(a); + EntityAccount account = EntityAccount.fromJSON(jaccount); + account.id = db.account().insertAccount(account); + + JSONArray jfolders = jaccount.getJSONArray("folders"); + for (int f = 0; f < jfolders.length(); f++) { + EntityFolder folder = EntityFolder.fromJSON(jfolders.getJSONObject(f)); + folder.account = account.id; + db.folder().insertFolder(folder); + } + + JSONArray jidentities = jaccount.getJSONArray("identities"); + for (int i = 0; i < jidentities.length(); i++) { + EntityIdentity identity = EntityIdentity.fromJSON(jidentities.getJSONObject(i)); + identity.account = account.id; + db.identity().insertIdentity(identity); + } + } + } catch (Throwable ex) { + Log.e(ex); + } + } + public static synchronized DB getInstance(Context context) { if (sInstance == null) { Context acontext = context.getApplicationContext(); @@ -229,50 +309,7 @@ public abstract class DB extends RoomDatabase { sInstance.getQueryExecutor().execute(new Runnable() { @Override public void run() { - try { - File dbfile = acontext.getDatabasePath(DB_NAME); - if (dbfile.exists()) { - Log.i("No emergency restore /dbfile"); - return; - } - - File emergency = new File(acontext.getFilesDir(), "emergency.json"); - if (!emergency.exists()) { - Log.i("No emergency restore /backup"); - return; - } - - if (sInstance.account().getAccounts().size() > 0) { - Log.e("No emergency restore /accounts"); - return; - } - - Log.e("Emergency restore"); - - String json = Helper.readText(emergency); - JSONArray jaccounts = new JSONArray(json); - for (int a = 0; a < jaccounts.length(); a++) { - JSONObject jaccount = jaccounts.getJSONObject(a); - EntityAccount account = EntityAccount.fromJSON(jaccount); - account.id = sInstance.account().insertAccount(account); - - JSONArray jfolders = jaccount.getJSONArray("folders"); - for (int f = 0; f < jfolders.length(); f++) { - EntityFolder folder = EntityFolder.fromJSON(jfolders.getJSONObject(f)); - folder.account = account.id; - sInstance.folder().insertFolder(folder); - } - - JSONArray jidentities = jaccount.getJSONArray("identities"); - for (int i = 0; i < jidentities.length(); i++) { - EntityIdentity identity = EntityIdentity.fromJSON(jidentities.getJSONObject(i)); - identity.account = account.id; - sInstance.identity().insertIdentity(identity); - } - } - } catch (Throwable ex) { - Log.e(ex); - } + checkEmergencybackup(acontext); } }); diff --git a/app/src/main/java/eu/faircode/email/WorkerCleanup.java b/app/src/main/java/eu/faircode/email/WorkerCleanup.java index ca42b2ac2c..f88777687d 100644 --- a/app/src/main/java/eu/faircode/email/WorkerCleanup.java +++ b/app/src/main/java/eu/faircode/email/WorkerCleanup.java @@ -35,9 +35,6 @@ import androidx.work.WorkManager; import androidx.work.Worker; import androidx.work.WorkerParameters; -import org.json.JSONArray; -import org.json.JSONObject; - import java.io.File; import java.util.ArrayList; import java.util.Arrays; @@ -326,33 +323,7 @@ public class WorkerCleanup extends Worker { EntityLog.log(context, "Analyze=" + (new Date().getTime() - analyze) + " ms"); } - Log.i("Creating emergency backup"); - try { - JSONArray jaccounts = new JSONArray(); - List accounts = db.account().getAccounts(); - for (EntityAccount account : accounts) { - JSONObject jaccount = account.toJSON(); - - JSONArray jfolders = new JSONArray(); - List folders = db.folder().getFolders(account.id, false, true); - for (EntityFolder folder : folders) - jfolders.put(folder.toJSON()); - jaccount.put("folders", jfolders); - - JSONArray jidentities = new JSONArray(); - List identities = db.identity().getIdentities(account.id); - for (EntityIdentity identity : identities) - jidentities.put(identity.toJSON()); - jaccount.put("identities", jidentities); - - jaccounts.put(jaccount); - } - - File emergency = new File(context.getFilesDir(), "emergency.json"); - Helper.writeText(emergency, jaccounts.toString(2)); - } catch (Throwable ex) { - Log.e(ex); - } + DB.createEmergencyBackup(context); if (manual) { // https://www.sqlite.org/lang_vacuum.html