Repository: square/leakcanary
File: leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt (Kotlin)

Code before:
      Configuration.UI_MODE_TYPE_WATCH -> WATCH
      else -> MOBILE
    }
  }

  val isInstantApp by lazy {
    VERSION.SDK_INT >= VERSION_CODES.O && application.packageManager.isInstantApp
  }

  val onRetainInstanceListener by lazy {
    when (formFactor) {
      TV -> TvOnRetainInstanceListener(application)
      else -> DefaultOnRetainInstanceListener()
    }
  }

  var resumedActivity: Activity? = null

  private lateinit var heapDumpPrefs: SharedPreferences

  internal var dumpEnabledInAboutScreen: Boolean
    get() {
      return if (::heapDumpPrefs.isInitialized) {
        heapDumpPrefs
                .getBoolean("AboutScreenDumpEnabled", true)
      } else true
    }
    set(value) {
      if (::heapDumpPrefs.isInitialized) {
        heapDumpPrefs
                .edit()
                .putBoolean("AboutScreenDumpEnabled", value)
                .apply()
      }
    }

  override fun invoke(application: Application) {
    _application = application

    checkRunningInDebuggableBuild()

    AppWatcher.objectWatcher.addOnObjectRetainedListener(this)

    val heapDumper = AndroidHeapDumper(application, createLeakDirectoryProvider(application))

    val gcTrigger = GcTrigger.Default

    val configProvider = { LeakCanary.config }

    val handlerThread = HandlerThread(LEAK_CANARY_THREAD_NAME)
    handlerThread.start()

Review comment (suggestion):
I'm not sure we should return true as a default value here, instead we should probably block until the shared preferences are loaded. Which, granted, will mean IO / blocking on API 27 but heh. A simple way to do that is create a CountDownLatch(1),  call await here and countDown where we initialize shared prefs. Also, I'm not sure lateinit is volatile, if not we need to make this volatile.

Code after:
      Configuration.UI_MODE_TYPE_WATCH -> WATCH
      else -> MOBILE
    }
  }

  val isInstantApp by lazy {
    VERSION.SDK_INT >= VERSION_CODES.O && application.packageManager.isInstantApp
  }

  val onRetainInstanceListener by lazy {
    when (formFactor) {
      TV -> TvOnRetainInstanceListener(application)
      else -> DefaultOnRetainInstanceListener()
    }
  }

  var resumedActivity: Activity? = null

  private val heapDumpPrefs by lazy {
    val oldPolicy = StrictMode.allowThreadDiskReads()
    application.getSharedPreferences("LeakCanaryHeapDumpPrefs", Context.MODE_PRIVATE)
            .also {
              StrictMode.setThreadPolicy(oldPolicy)
            }
  }

  internal var dumpEnabledInAboutScreen: Boolean
    get() {
      return heapDumpPrefs
                .getBoolean("AboutScreenDumpEnabled", true)
    }
    set(value) {
      heapDumpPrefs
                .edit()
                .putBoolean("AboutScreenDumpEnabled", value)
                .apply()
    }

  override fun invoke(application: Application) {
    _application = application

    checkRunningInDebuggableBuild()

    AppWatcher.objectWatcher.addOnObjectRetainedListener(this)

    val heapDumper = AndroidHeapDumper(application, createLeakDirectoryProvider(application))

    val gcTrigger = GcTrigger.Default

    val configProvider = { LeakCanary.config }