Use platform SQLite on Android 11+

pull/3321/head
Alex Baker 10 months ago
parent a35176d093
commit 7ede174adf

@ -2,7 +2,6 @@ package org.tasks.injection
import android.content.Context import android.content.Context
import androidx.room.Room import androidx.room.Room
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
@ -24,12 +23,12 @@ import javax.inject.Singleton
class TestModule { class TestModule {
@Provides @Provides
@Singleton @Singleton
fun getDatabase(@ApplicationContext context: Context): Database { fun getDatabase(@ApplicationContext context: Context): Database =
return Room.inMemoryDatabaseBuilder(context, Database::class.java) Room
.setDriver(BundledSQLiteDriver()) .inMemoryDatabaseBuilder(context, Database::class.java)
.fallbackToDestructiveMigration(dropAllTables = true) .fallbackToDestructiveMigration(dropAllTables = true)
.build() .setDriver()
} .build()
@Provides @Provides
fun getPermissionChecker(@ApplicationContext context: Context): PermissionChecker { fun getPermissionChecker(@ApplicationContext context: Context): PermissionChecker {

@ -6,6 +6,7 @@ import androidx.room.RoomDatabase
import androidx.sqlite.SQLiteConnection import androidx.sqlite.SQLiteConnection
import androidx.sqlite.driver.bundled.BundledSQLiteDriver import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import androidx.sqlite.execSQL import androidx.sqlite.execSQL
import com.todoroo.andlib.utility.AndroidUtilities.atLeastR
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
@ -37,20 +38,13 @@ internal class ProductionModule {
fileStorage: FileStorage, fileStorage: FileStorage,
): Database { ): Database {
val databaseFile = context.getDatabasePath(Database.NAME) val databaseFile = context.getDatabasePath(Database.NAME)
val builder = Room.databaseBuilder<Database>( val builder = Room
context = context, .databaseBuilder<Database>(
name = databaseFile.absolutePath context = context,
) name = databaseFile.absolutePath
.setDriver(BundledSQLiteDriver()) )
.setQueryCoroutineContext(Dispatchers.IO)
.addCallback(object : RoomDatabase.Callback() {
override fun onOpen(connection: SQLiteConnection) {
super.onOpen(connection)
connection.execSQL("PRAGMA busy_timeout = 30000")
}
})
.addMigrations(*Migrations.migrations(context, fileStorage)) .addMigrations(*Migrations.migrations(context, fileStorage))
.setDriver()
if (!BuildConfig.DEBUG || !preferences.getBoolean(R.string.p_crash_main_queries, false)) { if (!BuildConfig.DEBUG || !preferences.getBoolean(R.string.p_crash_main_queries, false)) {
builder.allowMainThreadQueries() builder.allowMainThreadQueries()
} }
@ -71,4 +65,21 @@ internal class ProductionModule {
caldavDao: CaldavDao, caldavDao: CaldavDao,
openTaskDao: OpenTaskDao, openTaskDao: OpenTaskDao,
): WorkManager = WorkManagerImpl(context, preferences, caldavDao, openTaskDao) ): WorkManager = WorkManagerImpl(context, preferences, caldavDao, openTaskDao)
} }
fun <T : RoomDatabase> RoomDatabase.Builder<T>.setDriver() =
if (atLeastR()) {
this
} else {
// need bundled sqlite for window functions
this
.setDriver(BundledSQLiteDriver())
.setQueryCoroutineContext(Dispatchers.IO)
.addCallback(object : RoomDatabase.Callback() {
override fun onOpen(connection: SQLiteConnection) {
super.onOpen(connection)
connection.execSQL("PRAGMA busy_timeout = 60000")
}
})
}

Loading…
Cancel
Save