main

square/leakcanary

Last updated at: 29/12/2023 09:38

Db.kt

TLDR

The Db.kt file in the leakcanary.internal.activity.db package contains a set of functions and classes related to database operations in the LeakCanary library for Android.

Methods

execute

This method executes a database operation on a given View, using the provided OnDb block. It creates a database helper if it doesn't exist, opens a writable database, and executes the provided block. It also handles UI updates if necessary.

closeDatabase

This method closes the database by calling the close method on the database helper in the serial IO thread.

Classes

No classes are defined in this file.

package leakcanary.internal.activity.db

import android.database.sqlite.SQLiteDatabase
import android.view.View
import leakcanary.internal.activity.db.Db.OnDb
import leakcanary.internal.activity.db.Io.OnIo
import leakcanary.internal.activity.db.ScopedLeaksDb.DbOpener

internal object Db {

  // Accessed on the IO thread only.
  private var dbHelper: DbOpener? = null

  interface OnDb : OnIo {
    val db: SQLiteDatabase
  }

  private class DbContext(override val db: SQLiteDatabase) : OnDb {
    var updateUi: (View.() -> Unit)? = null

    override fun updateUi(updateUi: View.() -> Unit) {
      this.updateUi = updateUi
    }
  }

  fun execute(
    view: View,
    block: OnDb.() -> Unit
  ) {
    val appContext = view.context.applicationContext
    Io.execute(view) {
      if (dbHelper == null) {
        dbHelper = ScopedLeaksDb.open(appContext)
      }
      val dbBlock = DbContext(dbHelper!!.writableDatabase)
      block(dbBlock)
      val updateUi = dbBlock.updateUi
      if (updateUi != null) {
        updateUi(updateUi)
      }
    }
  }

  fun closeDatabase() {
    // Closing on the serial IO thread to ensure we don't close while using the db.
    Io.execute {
      dbHelper?.close()
      dbHelper = null
    }
  }
}

internal fun View.executeOnDb(block: OnDb.() -> Unit) {
  Db.execute(this, block)
}