|
|
@ -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();
|
|
|
|
try {
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
|
|
|
|
sInstance = migrate(acontext, getBuilder(acontext)).build();
|
|
|
|
JSONArray jaccounts = new JSONArray();
|
|
|
|
|
|
|
|
List<EntityAccount> accounts = db.account().getAccounts();
|
|
|
|
|
|
|
|
for (EntityAccount account : accounts) {
|
|
|
|
|
|
|
|
JSONObject jaccount = account.toJSON();
|
|
|
|
|
|
|
|
|
|
|
|
sInstance.getQueryExecutor().execute(new Runnable() {
|
|
|
|
JSONArray jfolders = new JSONArray();
|
|
|
|
@Override
|
|
|
|
List<EntityFolder> folders = db.folder().getFolders(account.id, false, true);
|
|
|
|
public void run() {
|
|
|
|
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 {
|
|
|
|
try {
|
|
|
|
File dbfile = acontext.getDatabasePath(DB_NAME);
|
|
|
|
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 {
|
|
|
|