DatabaseModule.kt
TLDR
This file is a Kotlin class named DatabaseModule
that provides dependencies related to the database in the LeakCanary application.
Methods
provideWriteAheadLoggingEnabled
This method takes an Application
object as a parameter and returns a Boolean
indicating if write-ahead logging is enabled based on the device's RAM. It uses the ActivityManager
to check if the device is a low RAM device.
provideSqliteDriver
This method provides an SqlDriver
object for the SQLite database used in the application. It takes an Application
object and a Boolean
indicating if write-ahead logging is enabled as parameters. It creates an AndroidSqliteDriver
using the provided Schema
from the Database
class. The FrameworkSQLiteOpenHelperFactory
is used to create the SQLiteOpenHelper. The writeAheadLoggingEnabled
flag is set based on the provided parameter.
provideDatabase
This method provides a Database
object that represents the SQLite database. It takes an SqlDriver
object as a parameter and creates a new instance of the Database
class using the provided driver.
provideDatabaseDispatchers
This method provides a DatabaseDispatchers
object that encapsulates the dispatchers used for database operations. It takes a Boolean
indicating if write-ahead logging is enabled, a provider for WriteAheadLoggingEnabledDatabaseDispatchers
, and a provider for SingleConnectionDatabaseDispatchers
as parameters. It returns the appropriate dispatchers based on the value of the wolEnabled
parameter.
Classes
None
package org.leakcanary.data
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import app.cash.sqldelight.db.SqlDriver
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Provider
import javax.inject.Qualifier
import javax.inject.Singleton
import org.leakcanary.Database
@Module
@InstallIn(SingletonComponent::class)
class DatabaseModule {
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class WriteAheadLoggingEnabled
@Provides @WriteAheadLoggingEnabled
fun provideWriteAheadLoggingEnabled(app: Application): Boolean {
val activityManager = app.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
return !activityManager.isLowRamDevice
}
@Provides @Singleton fun provideSqliteDriver(
app: Application, @WriteAheadLoggingEnabled wolEnabled: Boolean
): SqlDriver {
val realFactory = FrameworkSQLiteOpenHelperFactory()
return AndroidSqliteDriver(
schema = Database.Schema, factory = { configuration ->
realFactory.create(configuration).apply { setWriteAheadLoggingEnabled(wolEnabled) }
}, context = app, name = "leakcanary.db"
)
}
@Provides @Singleton fun provideDatabase(driver: SqlDriver): Database = Database(driver)
@Provides fun provideDatabaseDispatchers(
@WriteAheadLoggingEnabled wolEnabled: Boolean,
wolDispatchers: Provider<WriteAheadLoggingEnabledDatabaseDispatchers>,
singleDispatchers: Provider<SingleConnectionDatabaseDispatchers>
): DatabaseDispatchers {
return if (wolEnabled) wolDispatchers.get() else singleDispatchers.get()
}
}