|
@@ -21,6 +21,7 @@
|
|
|
package com.nextcloud.talk.data.source.local
|
|
|
|
|
|
import android.content.Context
|
|
|
+import android.util.Log
|
|
|
import androidx.room.Database
|
|
|
import androidx.room.Room
|
|
|
import androidx.room.RoomDatabase
|
|
@@ -36,7 +37,9 @@ import com.nextcloud.talk.data.storage.ArbitraryStoragesDao
|
|
|
import com.nextcloud.talk.data.storage.model.ArbitraryStorageEntity
|
|
|
import com.nextcloud.talk.data.user.UsersDao
|
|
|
import com.nextcloud.talk.data.user.model.UserEntity
|
|
|
+import com.nextcloud.talk.utils.preferences.AppPreferences
|
|
|
import net.sqlcipher.database.SQLiteDatabase
|
|
|
+import net.sqlcipher.database.SQLiteDatabaseHook
|
|
|
import net.sqlcipher.database.SupportFactory
|
|
|
import java.util.Locale
|
|
|
|
|
@@ -58,20 +61,28 @@ abstract class TalkDatabase : RoomDatabase() {
|
|
|
abstract fun arbitraryStoragesDao(): ArbitraryStoragesDao
|
|
|
|
|
|
companion object {
|
|
|
+ const val TAG = "TalkDatabase"
|
|
|
|
|
|
@Volatile
|
|
|
private var INSTANCE: TalkDatabase? = null
|
|
|
|
|
|
@JvmStatic
|
|
|
- fun getInstance(context: Context): TalkDatabase =
|
|
|
+ fun getInstance(context: Context, appPreferences: AppPreferences): TalkDatabase =
|
|
|
INSTANCE ?: synchronized(this) {
|
|
|
- INSTANCE ?: build(context).also { INSTANCE = it }
|
|
|
+ INSTANCE ?: build(context, appPreferences).also { INSTANCE = it }
|
|
|
}
|
|
|
|
|
|
- private fun build(context: Context): TalkDatabase {
|
|
|
+ private fun build(context: Context, appPreferences: AppPreferences): TalkDatabase {
|
|
|
val passCharArray = context.getString(R.string.nc_talk_database_encryption_key).toCharArray()
|
|
|
val passphrase: ByteArray = SQLiteDatabase.getBytes(passCharArray)
|
|
|
- val factory = SupportFactory(passphrase)
|
|
|
+
|
|
|
+ val factory = if (appPreferences.isDbRoomMigrated) {
|
|
|
+ Log.i(TAG, "No cipher migration needed")
|
|
|
+ SupportFactory(passphrase)
|
|
|
+ } else {
|
|
|
+ Log.i(TAG, "Add cipher migration hook")
|
|
|
+ SupportFactory(passphrase, getCipherMigrationHook())
|
|
|
+ }
|
|
|
|
|
|
val dbName = context
|
|
|
.resources
|
|
@@ -95,5 +106,19 @@ abstract class TalkDatabase : RoomDatabase() {
|
|
|
})
|
|
|
.build()
|
|
|
}
|
|
|
+
|
|
|
+ private fun getCipherMigrationHook(): SQLiteDatabaseHook {
|
|
|
+ return object : SQLiteDatabaseHook {
|
|
|
+ override fun preKey(database: SQLiteDatabase) {
|
|
|
+ // unused atm
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun postKey(database: SQLiteDatabase) {
|
|
|
+ Log.i(TAG, "DB cipher_migrate START")
|
|
|
+ database.rawExecSQL("PRAGMA cipher_migrate;")
|
|
|
+ Log.i(TAG, "DB cipher_migrate END")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|