MCPcopy Create free account
hub / github.com/M66B/FairEmail / checkEmergencyBackup

Method checkEmergencyBackup

app/src/main/java/eu/faircode/email/DB.java:388–435  ·  view source on GitHub ↗
(Context context)

Source from the content-addressed store, hash-verified

386 }
387
388 private static void checkEmergencyBackup(Context context) {
389 try {
390 File dbfile = context.getDatabasePath(DB_NAME);
391 if (dbfile.exists()) {
392 Log.i("Emergency restore /dbfile");
393 return;
394 }
395
396 File emergency = new File(context.getFilesDir(), "emergency.json");
397 if (!emergency.exists()) {
398 Log.i("Emergency restore /json");
399 return;
400 }
401
402 DB db = DB.getInstance(context);
403 if (db.account().getAccounts().size() > 0) {
404 Log.e("Emergency restore /accounts");
405 return;
406 }
407
408 Log.e("Emergency restore");
409
410 String json = Helper.readText(emergency);
411 JSONArray jaccounts = new JSONArray(json);
412 for (int a = 0; a < jaccounts.length(); a++) {
413 JSONObject jaccount = jaccounts.getJSONObject(a);
414 EntityAccount account = EntityAccount.fromJSON(jaccount);
415 account.created = new Date().getTime();
416 account.id = db.account().insertAccount(account);
417
418 JSONArray jfolders = jaccount.getJSONArray("folders");
419 for (int f = 0; f < jfolders.length(); f++) {
420 EntityFolder folder = EntityFolder.fromJSON(jfolders.getJSONObject(f));
421 folder.account = account.id;
422 db.folder().insertFolder(folder);
423 }
424
425 JSONArray jidentities = jaccount.getJSONArray("identities");
426 for (int i = 0; i < jidentities.length(); i++) {
427 EntityIdentity identity = EntityIdentity.fromJSON(jidentities.getJSONObject(i));
428 identity.account = account.id;
429 db.identity().insertIdentity(identity);
430 }
431 }
432 } catch (Throwable ex) {
433 Log.e(ex);
434 }
435 }
436
437 public static synchronized DB getInstance(Context context) {
438 int apid = android.os.Process.myPid();

Callers 1

runMethod · 0.95

Calls 15

iMethod · 0.95
getInstanceMethod · 0.95
accountMethod · 0.95
eMethod · 0.95
readTextMethod · 0.95
fromJSONMethod · 0.95
fromJSONMethod · 0.95
folderMethod · 0.95
fromJSONMethod · 0.95
identityMethod · 0.95
getAccountsMethod · 0.80
insertAccountMethod · 0.80

Tested by

no test coverage detected