mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-25 10:55:55 +03:00
Merge pull request #603 from vector-im/feature/clear_corrupted_realm
Feature/clear corrupted realm
This commit is contained in:
commit
adf0382d28
2 changed files with 95 additions and 14 deletions
|
@ -0,0 +1,91 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.matrix.android.internal.database
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import im.vector.matrix.android.internal.database.model.SessionRealmModule
|
||||||
|
import im.vector.matrix.android.internal.di.UserCacheDirectory
|
||||||
|
import im.vector.matrix.android.internal.di.UserMd5
|
||||||
|
import im.vector.matrix.android.internal.session.SessionModule
|
||||||
|
import io.realm.Realm
|
||||||
|
import io.realm.RealmConfiguration
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.io.File
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
private const val REALM_SHOULD_CLEAR_FLAG_ = "REALM_SHOULD_CLEAR_FLAG_"
|
||||||
|
private const val REALM_NAME = "disk_store.realm"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is handling creation of RealmConfiguration for a session.
|
||||||
|
* It will handle corrupted realm by clearing the db file. It allows to just clear cache without losing your crypto keys.
|
||||||
|
* It's clearly not perfect but there is no way to catch the native crash.
|
||||||
|
*/
|
||||||
|
internal class SessionRealmConfigurationFactory @Inject constructor(private val realmKeysUtils: RealmKeysUtils,
|
||||||
|
@UserCacheDirectory val directory: File,
|
||||||
|
@UserMd5 val userMd5: String,
|
||||||
|
context: Context) {
|
||||||
|
|
||||||
|
private val sharedPreferences = context.getSharedPreferences("im.vector.matrix.android.realm", Context.MODE_PRIVATE)
|
||||||
|
|
||||||
|
|
||||||
|
fun create(): RealmConfiguration {
|
||||||
|
val shouldClearRealm = sharedPreferences.getBoolean("$REALM_SHOULD_CLEAR_FLAG_$userMd5", false)
|
||||||
|
if (shouldClearRealm) {
|
||||||
|
Timber.v("************************************************************")
|
||||||
|
Timber.v("The realm file session was corrupted and couldn't be loaded.")
|
||||||
|
Timber.v("The file has been deleted to recover.")
|
||||||
|
Timber.v("************************************************************")
|
||||||
|
deleteRealmFiles()
|
||||||
|
}
|
||||||
|
sharedPreferences
|
||||||
|
.edit()
|
||||||
|
.putBoolean("$REALM_SHOULD_CLEAR_FLAG_$userMd5", true)
|
||||||
|
.apply()
|
||||||
|
|
||||||
|
val realmConfiguration = RealmConfiguration.Builder()
|
||||||
|
.directory(directory)
|
||||||
|
.name(REALM_NAME)
|
||||||
|
.apply {
|
||||||
|
realmKeysUtils.configureEncryption(this, "${SessionModule.DB_ALIAS_PREFIX}$userMd5")
|
||||||
|
}
|
||||||
|
.modules(SessionRealmModule())
|
||||||
|
.deleteRealmIfMigrationNeeded()
|
||||||
|
.build()
|
||||||
|
|
||||||
|
// Try creating a realm instance and if it succeeds we can clear the flag
|
||||||
|
Realm.getInstance(realmConfiguration).use {
|
||||||
|
Timber.v("Successfully create realm instance")
|
||||||
|
sharedPreferences
|
||||||
|
.edit()
|
||||||
|
.putBoolean("$REALM_SHOULD_CLEAR_FLAG_$userMd5", false)
|
||||||
|
.apply()
|
||||||
|
}
|
||||||
|
return realmConfiguration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete all the realm files of the session
|
||||||
|
private fun deleteRealmFiles() {
|
||||||
|
listOf(REALM_NAME, "$REALM_NAME.lock", "$REALM_NAME.note", "$REALM_NAME.management").forEach { file ->
|
||||||
|
try {
|
||||||
|
File(directory, file).deleteRecursively()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e, "Unable to move files")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,8 +30,7 @@ import im.vector.matrix.android.api.session.Session
|
||||||
import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilitiesService
|
import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilitiesService
|
||||||
import im.vector.matrix.android.api.session.securestorage.SecureStorageService
|
import im.vector.matrix.android.api.session.securestorage.SecureStorageService
|
||||||
import im.vector.matrix.android.internal.database.LiveEntityObserver
|
import im.vector.matrix.android.internal.database.LiveEntityObserver
|
||||||
import im.vector.matrix.android.internal.database.RealmKeysUtils
|
import im.vector.matrix.android.internal.database.SessionRealmConfigurationFactory
|
||||||
import im.vector.matrix.android.internal.database.model.SessionRealmModule
|
|
||||||
import im.vector.matrix.android.internal.di.*
|
import im.vector.matrix.android.internal.di.*
|
||||||
import im.vector.matrix.android.internal.network.AccessTokenInterceptor
|
import im.vector.matrix.android.internal.network.AccessTokenInterceptor
|
||||||
import im.vector.matrix.android.internal.network.RetrofitFactory
|
import im.vector.matrix.android.internal.network.RetrofitFactory
|
||||||
|
@ -54,6 +53,7 @@ internal abstract class SessionModule {
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
internal const val DB_ALIAS_PREFIX = "session_db_"
|
internal const val DB_ALIAS_PREFIX = "session_db_"
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
@ -94,18 +94,8 @@ internal abstract class SessionModule {
|
||||||
@Provides
|
@Provides
|
||||||
@SessionDatabase
|
@SessionDatabase
|
||||||
@SessionScope
|
@SessionScope
|
||||||
fun providesRealmConfiguration(realmKeysUtils: RealmKeysUtils,
|
fun providesRealmConfiguration(realmConfigurationFactory: SessionRealmConfigurationFactory): RealmConfiguration {
|
||||||
@UserCacheDirectory directory: File,
|
return realmConfigurationFactory.create()
|
||||||
@UserMd5 userMd5: String): RealmConfiguration {
|
|
||||||
return RealmConfiguration.Builder()
|
|
||||||
.directory(directory)
|
|
||||||
.name("disk_store.realm")
|
|
||||||
.apply {
|
|
||||||
realmKeysUtils.configureEncryption(this, "$DB_ALIAS_PREFIX$userMd5")
|
|
||||||
}
|
|
||||||
.modules(SessionRealmModule())
|
|
||||||
.deleteRealmIfMigrationNeeded()
|
|
||||||
.build()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
|
Loading…
Reference in a new issue