(Context context)
| 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 { |
no test coverage detected