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

Method getInstance

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

Source from the content-addressed store, hash-verified

435 }
436
437 public static synchronized DB getInstance(Context context) {
438 int apid = android.os.Process.myPid();
439 Context acontext = context.getApplicationContext();
440 if (sInstance != null &&
441 (sPid != apid || !Objects.equals(sContext, acontext)))
442 try {
443 Log.e("Orphan database instance pid=" + apid + "/" + sPid);
444 sInstance = null;
445 } catch (Throwable ex) {
446 Log.e(ex);
447 }
448 sPid = apid;
449 sContext = acontext;
450
451 if (sInstance == null) {
452 Log.i("Creating database instance pid=" + sPid);
453
454 sInstance = migrate(sContext, getBuilder(sContext)).build();
455
456 Helper.getSerialExecutor().execute(new Runnable() {
457 @Override
458 public void run() {
459 checkEmergencyBackup(sContext);
460 }
461 });
462
463 try {
464 Log.i("Disabling view invalidation");
465
466 Field fmViewTables = InvalidationTracker.class.getDeclaredField("mViewTables");
467 fmViewTables.setAccessible(true);
468
469 Map<String, Set<String>> mViewTables = (Map) fmViewTables.get(sInstance.getInvalidationTracker());
470 mViewTables.get("account_view").clear();
471 mViewTables.get("identity_view").clear();
472 mViewTables.get("folder_view").clear();
473
474 Log.i("Disabled view invalidation");
475 } catch (ReflectiveOperationException ex) {
476 // Should never happen
477 Log.forceCrashReport(context, ex);
478 }
479
480 sInstance.getInvalidationTracker().addObserver(new InvalidationTracker.Observer(DB_TABLES) {
481 @Override
482 public void onInvalidated(@NonNull Set<String> tables) {
483 Log.d("ROOM invalidated=" + TextUtils.join(",", tables));
484 }
485 });
486
487 // Ref: https://android-review.googlesource.com/c/platform/frameworks/support/+/1797472
488 Log.i("DB critical section start");
489 File dbDir = context.getDatabasePath(DB_NAME).getParentFile();
490 dbDir.mkdirs();
491 File lockFile = new File(dbDir, DB_NAME + ".lock");
492 try (FileOutputStream fos = new FileOutputStream(lockFile)) {
493 ObjectHolder<FileLock> lock = new ObjectHolder<>(null);
494 try {

Callers 15

onCreateMethod · 0.95
delegateMethod · 0.95
monitorAccountMethod · 0.95
fetchMethod · 0.95
updateQuotaMethod · 0.95
optimizeAccountMethod · 0.95
logMethod · 0.95
cleanupMethod · 0.95
onExecuteMethod · 0.95
onExecuteMethod · 0.95
cleanupMethod · 0.95
onCreateMethod · 0.95

Calls 15

eMethod · 0.95
iMethod · 0.95
migrateMethod · 0.95
getBuilderMethod · 0.95
getSerialExecutorMethod · 0.95
forceCrashReportMethod · 0.95
_hasJsonMethod · 0.95
joinMethod · 0.80
getOpenHelperMethod · 0.80
equalsMethod · 0.45
buildMethod · 0.45

Tested by

no test coverage detected