fix: Exception as Snackbar

This commit is contained in:
J-Jamet committed 2025-08-10 21:32:12 +02:00
1 parent 7be554a378
commit 3a8245ee74
43 files changed
+58 -126

No files matched your search

@@ -57,13 +57,18 @@ import com.kunzisoft.keepass.autofill.AutofillComponent
import com.kunzisoft.keepass.autofill.AutofillHelper
import com.kunzisoft.keepass.biometric.DeviceUnlockFragment
import com.kunzisoft.keepass.biometric.DeviceUnlockManager
import com.kunzisoft.keepass.biometric.deviceUnlockError
import com.kunzisoft.keepass.database.ContextualDatabase
import com.kunzisoft.keepass.database.MainCredential
import com.kunzisoft.keepass.database.exception.DuplicateUuidDatabaseException
import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException
import com.kunzisoft.keepass.education.PasswordActivityEducation
import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.model.*
import com.kunzisoft.keepass.model.CipherDecryptDatabase
import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.model.CredentialStorage
import com.kunzisoft.keepass.model.RegisterInfo
import com.kunzisoft.keepass.model.SearchInfo
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_LOAD_TASK
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.CIPHER_DATABASE_KEY
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.DATABASE_URI_KEY
@@ -81,8 +86,8 @@ import com.kunzisoft.keepass.utils.getParcelableExtraCompat
import com.kunzisoft.keepass.view.MainCredentialView
import com.kunzisoft.keepass.view.asError
import com.kunzisoft.keepass.view.showActionErrorIfNeeded
import com.kunzisoft.keepass.viewmodels.DeviceUnlockViewModel
import com.kunzisoft.keepass.viewmodels.DatabaseFileViewModel
import com.kunzisoft.keepass.viewmodels.DeviceUnlockViewModel
import kotlinx.coroutines.launch
import java.io.FileNotFoundException
@@ -244,6 +249,14 @@ class MainCredentialActivity : DatabaseModeActivity() {
onCredentialDecrypted(cipherDecryptDatabase)
mDeviceUnlockViewModel.consumeCredentialDecrypted()
}
uiState.exception?.let { error ->
Snackbar.make(
coordinatorLayout,
deviceUnlockError(error, this@MainCredentialActivity),
Snackbar.LENGTH_LONG
).asError().show()
}
mDeviceUnlockViewModel.exceptionShown()
}
}
}
@@ -25,7 +25,6 @@ import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.security.keystore.KeyPermanentlyInvalidatedException
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
@@ -52,7 +51,6 @@ import com.kunzisoft.keepass.view.showByFading
import com.kunzisoft.keepass.viewmodels.DeviceUnlockViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.security.UnrecoverableKeyException
import java.util.concurrent.Executors
@RequiresApi(Build.VERSION_CODES.M)
@@ -164,8 +162,6 @@ class DeviceUnlockFragment: Fragment() {
closeBiometricPrompt()
mDeviceUnlockViewModel.biometricPromptClosed()
}
// Errors
setAdvancedUnlockedError(uiState.error)
// Advanced menu
mAllowAdvancedUnlockMenu = uiState.allowAdvancedUnlockMenu
activity?.invalidateOptionsMenu()
@@ -249,7 +245,7 @@ class DeviceUnlockFragment: Fragment() {
DeviceUnlockMode.EXTRACT_CREDENTIAL -> setExtractCredentialMode()
}
} catch (e: Exception) {
showGenericException(e)
mDeviceUnlockViewModel.setException(e)
}
}
@@ -297,14 +293,6 @@ class DeviceUnlockFragment: Fragment() {
}
}
fun showGenericException(e: Exception) {
lifecycleScope.launch(Dispatchers.Main) {
val errorMessage = e.cause?.localizedMessage ?: e.localizedMessage ?: ""
setAdvancedUnlockedMessageView(errorMessage)
}
}
private fun setNotAvailableMode() {
lifecycleScope.launch(Dispatchers.Main) {
showViews(false)
@@ -346,7 +334,6 @@ class DeviceUnlockFragment: Fragment() {
lifecycleScope.launch(Dispatchers.Main) {
showViews(true)
setAdvancedUnlockedTitleView(R.string.configure_biometric)
setAdvancedUnlockedMessageView("")
openBiometricSetting()
}
}
@@ -363,37 +350,26 @@ class DeviceUnlockFragment: Fragment() {
lifecycleScope.launch(Dispatchers.Main) {
showViews(true)
setAdvancedUnlockedTitleView(R.string.unavailable)
setAdvancedUnlockedMessageView("")
context?.let { context ->
mDeviceUnlockView?.setDeviceUnlockButtonViewClickListener {
showError(
BiometricPrompt.ERROR_UNABLE_TO_PROCESS,
mDeviceUnlockViewModel.setException(SecurityException(
context.getString(R.string.credential_before_click_advanced_unlock_button)
)
))
}
}
}
}
private fun showError(errorCode: Int, errString: CharSequence) {
lifecycleScope.launch(Dispatchers.Main) {
Log.e(TAG, "Biometric authentication error. Code : $errorCode Error : $errString")
setAdvancedUnlockedMessageView(errString.toString())
}
}
private fun setStoreCredentialMode() {
lifecycleScope.launch(Dispatchers.Main) {
showViews(true)
setAdvancedUnlockedTitleView(R.string.unlock_and_link_biometric)
setAdvancedUnlockedMessageView("")
context?.let { context ->
mDeviceUnlockView?.setDeviceUnlockButtonViewClickListener { view ->
storeCredentialButtonClickListener?.onClick(view) ?: run {
showError(
BiometricPrompt.ERROR_HW_UNAVAILABLE,
mDeviceUnlockViewModel.setException(SecurityException(
context.getString(R.string.keystore_not_accessible)
)
))
}
storeCredentialButtonClickListener = null
}
@@ -405,14 +381,12 @@ class DeviceUnlockFragment: Fragment() {
lifecycleScope.launch(Dispatchers.Main) {
showViews(true)
setAdvancedUnlockedTitleView(R.string.unlock)
setAdvancedUnlockedMessageView("")
context?.let { context ->
mDeviceUnlockView?.setDeviceUnlockButtonViewClickListener { view ->
extractCredentialButtonClickListener?.onClick(view) ?: run {
showError(
BiometricPrompt.ERROR_HW_UNAVAILABLE,
mDeviceUnlockViewModel.setException(SecurityException(
context.getString(R.string.keystore_not_accessible)
)
))
}
extractCredentialButtonClickListener = null
}
@@ -443,47 +417,25 @@ class DeviceUnlockFragment: Fragment() {
}
}
private fun setAdvancedUnlockedMessageView(textId: Int) {
lifecycleScope.launch(Dispatchers.Main) {
mDeviceUnlockView?.setMessage(textId)
}
}
private fun setAdvancedUnlockedMessageView(text: CharSequence?) {
lifecycleScope.launch(Dispatchers.Main) {
mDeviceUnlockView?.setMessage(text)
}
}
private fun setAdvancedUnlockedError(exception: Exception?) {
when (exception) {
is UnrecoverableKeyException -> {
setAdvancedUnlockedMessageView(R.string.advanced_unlock_invalid_key)
}
is KeyPermanentlyInvalidatedException -> {
setAdvancedUnlockedMessageView(R.string.advanced_unlock_invalid_key)
}
else -> {
setAdvancedUnlockedMessageView(
exception?.cause?.localizedMessage
?: exception?.localizedMessage
?: "")
}
}
}
private fun setAuthenticationError(errorCode: Int, errString: CharSequence) {
lifecycleScope.launch(Dispatchers.Main) {
Log.e(TAG, "Biometric authentication error. Code : $errorCode Error : $errString")
setAdvancedUnlockedMessageView(errString.toString())
Log.e(TAG, "Biometric authentication error. Code : $errorCode Error : $errString")
when (errorCode) {
BiometricPrompt.ERROR_NEGATIVE_BUTTON ->
mDeviceUnlockViewModel.setException(
SecurityException(getString(R.string.error_cancel_by_user))
)
else ->
mDeviceUnlockViewModel.setException(
SecurityException(errString.toString())
)
}
}
private fun setAuthenticationFailed() {
lifecycleScope.launch(Dispatchers.Main) {
Log.e(TAG, "Biometric authentication failed, biometric not recognized")
setAdvancedUnlockedMessageView(R.string.advanced_unlock_not_recognized)
}
Log.e(TAG, "Biometric authentication failed, biometric not recognized")
mDeviceUnlockViewModel.setException(SecurityException(
getString(R.string.advanced_unlock_not_recognized))
)
}
override fun onPause() {
@@ -369,10 +369,7 @@ class DeviceUnlockManager(private var appContext: Context) {
deleteEntryKeyInKeystoreForBiometric(appContext)
} catch (e: Exception) {
Toast.makeText(appContext,
appContext.getString(
R.string.advanced_unlock_scanning_error,
e.localizedMessage
),
deviceUnlockError(e, appContext),
Toast.LENGTH_SHORT).show()
} finally {
CipherDatabaseAction.getInstance(appContext).deleteAll()
@@ -381,6 +378,17 @@ class DeviceUnlockManager(private var appContext: Context) {
}
}
fun deviceUnlockError(error: Exception, context: Context): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& (error is UnrecoverableKeyException
|| error is KeyPermanentlyInvalidatedException)) {
context.getString(R.string.advanced_unlock_invalid_key)
} else
error.cause?.localizedMessage
?: error.localizedMessage
?: error.toString()
}
fun isBiometricUnlockEnable(appContext: Context) =
PreferencesUtil.isBiometricUnlockEnable(appContext)
@@ -25,7 +25,6 @@ import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import com.kunzisoft.keepass.R
@@ -60,14 +59,4 @@ class DeviceUnlockView @JvmOverloads constructor(context: Context,
fun setTitle(@StringRes textId: Int) {
title = context.getString(textId)
}
fun setMessage(text: CharSequence?) {
if (!text.isNullOrEmpty())
Toast.makeText(context, text, Toast.LENGTH_LONG).show()
}
fun setMessage(@StringRes textId: Int) {
Toast.makeText(context, textId, Toast.LENGTH_LONG).show()
}
}
@@ -283,7 +283,15 @@ class DeviceUnlockViewModel(application: Application): AndroidViewModel(applicat
fun setException(value: Exception?) {
_uiState.update { currentState ->
currentState.copy(
error = value
exception = value
)
}
}
fun exceptionShown() {
_uiState.update { currentState ->
currentState.copy(
exception = null
)
}
}
@@ -384,5 +392,5 @@ data class DeviceUnlockState(
val cipherDecryptDatabase: CipherDecryptDatabase? = null,
val cryptoPrompt: DeviceUnlockCryptoPrompt? = null,
val closePromptRequested: Boolean = false,
val error: Exception? = null
val exception: Exception? = null
)