mirror of https://github.com/M66B/FairEmail.git
Refactoring
This commit is contained in:
parent
52d97d9da3
commit
45bb31e975
|
@ -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<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()) {
|
||||||
|
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) {
|
public static synchronized DB getInstance(Context context) {
|
||||||
if (sInstance == null) {
|
if (sInstance == null) {
|
||||||
Context acontext = context.getApplicationContext();
|
Context acontext = context.getApplicationContext();
|
||||||
|
@ -229,50 +309,7 @@ public abstract class DB extends RoomDatabase {
|
||||||
sInstance.getQueryExecutor().execute(new Runnable() {
|
sInstance.getQueryExecutor().execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
checkEmergencybackup(acontext);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue