Last active
October 25, 2021 09:52
-
-
Save CreepyMob/962489974dcc56a8d267690725d082f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.database.sqlite.SQLiteTransactionListener | |
import androidx.sqlite.db.SupportSQLiteDatabase | |
import androidx.sqlite.db.SupportSQLiteOpenHelper | |
import com.commonsware.cwac.saferoom.SafeHelperFactory | |
import java.util.concurrent.locks.ReentrantLock | |
class SupportSQLiteOpenHelperWrapped(val source: SupportSQLiteOpenHelper) : SupportSQLiteOpenHelper by source { | |
private val writableDatabaseLazy: SupportSQLiteDatabaseWrapped by lazy { | |
SupportSQLiteDatabaseWrapped(source.writableDatabase) | |
} | |
override fun getWritableDatabase(): SupportSQLiteDatabase = writableDatabaseLazy | |
override fun close() { | |
try { | |
writableDatabaseLazy.lock.lock() | |
source.close() | |
} finally { | |
writableDatabaseLazy.lock.unlock() | |
} | |
} | |
} | |
class SupportSQLiteDatabaseWrapped(val source: SupportSQLiteDatabase) : SupportSQLiteDatabase by source { | |
val lock: ReentrantLock = ReentrantLock(true) | |
override fun beginTransaction() { | |
lock.lock() | |
source.beginTransaction() | |
} | |
override fun beginTransactionNonExclusive() { | |
lock.lock() | |
source.beginTransactionNonExclusive() | |
} | |
override fun beginTransactionWithListener(transactionListener: SQLiteTransactionListener?) { | |
lock.lock() | |
source.beginTransactionWithListener(transactionListener) | |
} | |
override fun beginTransactionWithListenerNonExclusive(transactionListener: SQLiteTransactionListener?) { | |
lock.lock() | |
source.beginTransactionWithListenerNonExclusive(transactionListener) | |
} | |
override fun endTransaction() { | |
try { | |
source.endTransaction() | |
} finally { | |
lock.unlock() | |
} | |
} | |
} | |
class SafeHelperFactoryWrapped(val source: SafeHelperFactory) : SupportSQLiteOpenHelper.Factory { | |
override fun create(configuration: SupportSQLiteOpenHelper.Configuration?): SupportSQLiteOpenHelper { | |
return SupportSQLiteOpenHelperWrapped(source.create(configuration)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment