Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Add support for merging application data to prevent crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Oct 14, 2023
1 parent 735ac1f commit af846ac
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class MainActivity : FragmentActivity() {
finish()
}

// merge application data when application updated
UpdateMerge.update(this)

// init datastore
userSecrets = SecretStore.initialize(this)

Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/dev/medzik/librepass/android/UpdateMerge.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dev.medzik.librepass.android

import android.content.Context
import dev.medzik.librepass.android.data.getRepository
import dev.medzik.librepass.android.utils.SecretStore
import dev.medzik.librepass.android.utils.SecretStore.readKey
import dev.medzik.librepass.android.utils.SecretStore.writeKey
import dev.medzik.librepass.android.utils.StoreKey
import kotlinx.coroutines.runBlocking

object UpdateMerge {
fun update(context: Context) {
if (context.getRepository().credentials.get() == null)
context.writeKey(StoreKey.AppVersionCode, BuildConfig.VERSION_CODE)

val lastVersionCode = context.readKey(StoreKey.AppVersionCode)
var versionCode = lastVersionCode

while (versionCode < BuildConfig.VERSION_CODE) {
when (versionCode) {
-1 -> merge4To5(context)
}

versionCode++
}
}

private fun merge4To5(context: Context) {
val repositoryCredentials = context.getRepository().credentials
val credentials = repositoryCredentials.get()
?: return

SecretStore.delete(context)

runBlocking {
repositoryCredentials.update(
credentials.copy(
biometricProtectedPrivateKey = null,
biometricProtectedPrivateKeyIV = null,
biometricEnabled = false
)
)
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/java/dev/medzik/librepass/android/utils/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ sealed class StoreKey<T>(
stringSetPreferencesKey("custom_servers"),
emptySet()
)

data object AppVersionCode : StoreKey<Int>(
intPreferencesKey("app_version_code"),
-1
)
}

enum class ThemeValues {
Expand Down

0 comments on commit af846ac

Please sign in to comment.