Refactoring

This commit is contained in:
M66B 2021-02-27 15:31:58 +01:00
parent 52d97d9da3
commit 45bb31e975
2 changed files with 82 additions and 74 deletions

View File

@ -220,30 +220,55 @@ public abstract class DB extends RoomDatabase {
}); });
} }
public static synchronized DB getInstance(Context context) { static void createEmergencyBackup(Context context) {
if (sInstance == null) { Log.i("Creating emergency backup");
Context acontext = context.getApplicationContext();
sInstance = migrate(acontext, getBuilder(acontext)).build();
sInstance.getQueryExecutor().execute(new Runnable() {
@Override
public void run() {
try { try {
File dbfile = acontext.getDatabasePath(DB_NAME); DB db = DB.getInstance(context);
JSONArray jaccounts = new JSONArray();
List<EntityAccount> accounts = db.account().getAccounts();
for (EntityAccount account : accounts) {
JSONObject jaccount = account.toJSON();
JSONArray jfolders = new JSONArray();
List<EntityFolder> 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<EntityIdentity> 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()) { if (dbfile.exists()) {
Log.i("No emergency restore /dbfile"); Log.i("Emergency restore /dbfile");
return; return;
} }
File emergency = new File(acontext.getFilesDir(), "emergency.json"); File emergency = new File(context.getFilesDir(), "emergency.json");
if (!emergency.exists()) { if (!emergency.exists()) {
Log.i("No emergency restore /backup"); Log.i("Emergency restore /json");
return; return;
} }
if (sInstance.account().getAccounts().size() > 0) { DB db = DB.getInstance(context);
Log.e("No emergency restore /accounts"); if (db.account().getAccounts().size() > 0) {
Log.e("Emergency restore /accounts");
return; return;
} }
@ -254,26 +279,38 @@ public abstract class DB extends RoomDatabase {
for (int a = 0; a < jaccounts.length(); a++) { for (int a = 0; a < jaccounts.length(); a++) {
JSONObject jaccount = jaccounts.getJSONObject(a); JSONObject jaccount = jaccounts.getJSONObject(a);
EntityAccount account = EntityAccount.fromJSON(jaccount); EntityAccount account = EntityAccount.fromJSON(jaccount);
account.id = sInstance.account().insertAccount(account); account.id = db.account().insertAccount(account);
JSONArray jfolders = jaccount.getJSONArray("folders"); JSONArray jfolders = jaccount.getJSONArray("folders");
for (int f = 0; f < jfolders.length(); f++) { for (int f = 0; f < jfolders.length(); f++) {
EntityFolder folder = EntityFolder.fromJSON(jfolders.getJSONObject(f)); EntityFolder folder = EntityFolder.fromJSON(jfolders.getJSONObject(f));
folder.account = account.id; folder.account = account.id;
sInstance.folder().insertFolder(folder); db.folder().insertFolder(folder);
} }
JSONArray jidentities = jaccount.getJSONArray("identities"); JSONArray jidentities = jaccount.getJSONArray("identities");
for (int i = 0; i < jidentities.length(); i++) { for (int i = 0; i < jidentities.length(); i++) {
EntityIdentity identity = EntityIdentity.fromJSON(jidentities.getJSONObject(i)); EntityIdentity identity = EntityIdentity.fromJSON(jidentities.getJSONObject(i));
identity.account = account.id; identity.account = account.id;
sInstance.identity().insertIdentity(identity); db.identity().insertIdentity(identity);
} }
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }
} }
public static synchronized DB getInstance(Context context) {
if (sInstance == null) {
Context acontext = context.getApplicationContext();
sInstance = migrate(acontext, getBuilder(acontext)).build();
sInstance.getQueryExecutor().execute(new Runnable() {
@Override
public void run() {
checkEmergencybackup(acontext);
}
}); });
try { try {

View File

@ -35,9 +35,6 @@ import androidx.work.WorkManager;
import androidx.work.Worker; import androidx.work.Worker;
import androidx.work.WorkerParameters; import androidx.work.WorkerParameters;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -326,33 +323,7 @@ public class WorkerCleanup extends Worker {
EntityLog.log(context, "Analyze=" + (new Date().getTime() - analyze) + " ms"); EntityLog.log(context, "Analyze=" + (new Date().getTime() - analyze) + " ms");
} }
Log.i("Creating emergency backup"); DB.createEmergencyBackup(context);
try {
JSONArray jaccounts = new JSONArray();
List<EntityAccount> accounts = db.account().getAccounts();
for (EntityAccount account : accounts) {
JSONObject jaccount = account.toJSON();
JSONArray jfolders = new JSONArray();
List<EntityFolder> 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<EntityIdentity> 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) { if (manual) {
// https://www.sqlite.org/lang_vacuum.html // https://www.sqlite.org/lang_vacuum.html