From 9a9c198cfe4e7eaceb4718c38dbdd60ff2a02dfd Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 27 Feb 2021 11:22:17 +0100 Subject: [PATCH] Added emergency backup --- app/src/main/java/eu/faircode/email/DB.java | 50 +++++++++++++++++++ .../java/eu/faircode/email/WorkerCleanup.java | 31 ++++++++++++ 2 files changed, 81 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index 78bec2baac..c93c05735b 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -226,6 +226,56 @@ public abstract class DB extends RoomDatabase { sInstance = migrate(acontext, getBuilder(acontext)).build(); + 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.i("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); + } + } + }); + try { Log.i("Disabling view invalidation"); diff --git a/app/src/main/java/eu/faircode/email/WorkerCleanup.java b/app/src/main/java/eu/faircode/email/WorkerCleanup.java index 43f2eaee72..ca42b2ac2c 100644 --- a/app/src/main/java/eu/faircode/email/WorkerCleanup.java +++ b/app/src/main/java/eu/faircode/email/WorkerCleanup.java @@ -35,6 +35,9 @@ 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; @@ -323,6 +326,34 @@ 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); + } + if (manual) { // https://www.sqlite.org/lang_vacuum.html long size = context.getDatabasePath(db.getOpenHelper().getDatabaseName()).length();