From 4dc97b090f1a6835705924bb44b2d76e30d75a1b Mon Sep 17 00:00:00 2001 From: Anton Tkachev Date: Wed, 3 Apr 2024 15:36:10 +0400 Subject: [PATCH] AND-6643 [Biometrics] Fixed a crash that occurred when checking biometrics availability --- .../authentication/AndroidAuthenticationManager.kt | 14 +++++++------- .../src/main/java/com/tangem/TangemSdk.kt | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tangem-sdk-android/src/main/java/com/tangem/sdk/authentication/AndroidAuthenticationManager.kt b/tangem-sdk-android/src/main/java/com/tangem/sdk/authentication/AndroidAuthenticationManager.kt index 823fd940..79236bda 100644 --- a/tangem-sdk-android/src/main/java/com/tangem/sdk/authentication/AndroidAuthenticationManager.kt +++ b/tangem-sdk-android/src/main/java/com/tangem/sdk/authentication/AndroidAuthenticationManager.kt @@ -43,11 +43,11 @@ internal class AndroidAuthenticationManager( .build() } - private val biometricsStatus = MutableStateFlow(BiometricsStatus.UNINITIALIZED) + private val biometricsStatus = MutableStateFlow(BiometricsStatus.NOT_INITIALIZED) override val canAuthenticate: Boolean get() { - if (biometricsStatus.value == BiometricsStatus.UNAVAILABLE) { + if (biometricsStatus.value == BiometricsStatus.NOT_INITIALIZED) { error("Biometrics status must be initialized before checking if biometrics can authenticate") } @@ -56,7 +56,7 @@ internal class AndroidAuthenticationManager( override val needEnrollBiometrics: Boolean get() { - if (biometricsStatus.value == BiometricsStatus.UNAVAILABLE) { + if (biometricsStatus.value == BiometricsStatus.NOT_INITIALIZED) { error("Biometrics status must be initialized before checking if biometrics need to be enrolled") } @@ -74,7 +74,7 @@ internal class AndroidAuthenticationManager( override fun onPause(owner: LifecycleOwner) { Log.biometric { "Owner has been paused, biometrics was uninitialized" } - biometricsStatus.value = BiometricsStatus.UNINITIALIZED + biometricsStatus.value = BiometricsStatus.NOT_INITIALIZED } override suspend fun authenticate( @@ -106,7 +106,7 @@ internal class AndroidAuthenticationManager( throw TangemSdkError.AuthenticationUnavailable() } - BiometricsStatus.UNINITIALIZED -> { + BiometricsStatus.NOT_INITIALIZED -> { Log.biometric { "Awaiting for the biometrics status to be initialized" } awaitBiometricsInititialization() @@ -164,7 +164,7 @@ internal class AndroidAuthenticationManager( ) private suspend fun awaitBiometricsInititialization() { - biometricsStatus.first { it != BiometricsStatus.UNINITIALIZED } + biometricsStatus.first { it != BiometricsStatus.NOT_INITIALIZED } } @Suppress("LongMethod") @@ -258,7 +258,7 @@ internal class AndroidAuthenticationManager( AUTHENTICATING, UNAVAILABLE, NEED_ENROLL, - UNINITIALIZED, + NOT_INITIALIZED, } private sealed interface BiometricAuthenticationResult { diff --git a/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt b/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt index 35b10cd7..0f5f41e5 100644 --- a/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt +++ b/tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt @@ -1157,8 +1157,8 @@ class TangemSdk( keystoreManager: KeystoreManager, secureStorage: SecureStorage, config: Config, - ): UserCodeRepository? { - return if (authenticationManager.canAuthenticate && + ): UserCodeRepository? = runCatching { + if (authenticationManager.canAuthenticate && config.userCodeRequestPolicy is UserCodeRequestPolicy.AlwaysWithBiometrics ) { UserCodeRepository( @@ -1168,7 +1168,7 @@ class TangemSdk( } else { null } - } + }.getOrNull() private fun makeSession( cardId: String? = null,