From 4dc97b090f1a6835705924bb44b2d76e30d75a1b Mon Sep 17 00:00:00 2001 From: Anton Tkachev Date: Wed, 3 Apr 2024 15:36:10 +0400 Subject: [PATCH 1/2] 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, From cd282c3d4ce6f6a61475396506c8de82ea413e87 Mon Sep 17 00:00:00 2001 From: Vlad Kozarez Date: Mon, 8 Apr 2024 17:35:21 +0100 Subject: [PATCH 2/2] AND-5647 fixed crash on NFC dialog show --- .../java/com/tangem/sdk/DefaultSessionViewDelegate.kt | 3 ++- .../src/main/java/com/tangem/sdk/ui/NfcEnableDialog.kt | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tangem-sdk-android/src/main/java/com/tangem/sdk/DefaultSessionViewDelegate.kt b/tangem-sdk-android/src/main/java/com/tangem/sdk/DefaultSessionViewDelegate.kt index 1f3d164e..55a7e6ff 100644 --- a/tangem-sdk-android/src/main/java/com/tangem/sdk/DefaultSessionViewDelegate.kt +++ b/tangem-sdk-android/src/main/java/com/tangem/sdk/DefaultSessionViewDelegate.kt @@ -2,6 +2,7 @@ package com.tangem.sdk import android.app.Activity import android.os.Build +import androidx.core.app.ComponentActivity import com.tangem.Log import com.tangem.Message import com.tangem.SessionViewDelegate @@ -27,7 +28,7 @@ import com.tangem.sdk.ui.NfcSessionDialog */ class DefaultSessionViewDelegate( private val nfcManager: NfcManager, - private val activity: Activity, + private val activity: ComponentActivity, ) : SessionViewDelegate { var sdkConfig: Config = Config() diff --git a/tangem-sdk-android/src/main/java/com/tangem/sdk/ui/NfcEnableDialog.kt b/tangem-sdk-android/src/main/java/com/tangem/sdk/ui/NfcEnableDialog.kt index 1934e5e2..030d62fa 100644 --- a/tangem-sdk-android/src/main/java/com/tangem/sdk/ui/NfcEnableDialog.kt +++ b/tangem-sdk-android/src/main/java/com/tangem/sdk/ui/NfcEnableDialog.kt @@ -1,10 +1,12 @@ package com.tangem.sdk.ui -import android.app.Activity import android.content.ActivityNotFoundException import android.content.Intent import android.provider.Settings import androidx.appcompat.app.AlertDialog +import androidx.core.app.ComponentActivity +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleOwner import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.tangem.sdk.R import com.tangem.sdk.extensions.sdkThemeContext @@ -13,7 +15,7 @@ class NfcEnableDialog { private var dialog: AlertDialog? = null - fun show(activity: Activity) { + fun show(activity: ComponentActivity) { val builder = MaterialAlertDialogBuilder(activity.sdkThemeContext()) builder.setCancelable(false) .setIcon(R.drawable.ic_action_nfc_gray) @@ -21,7 +23,9 @@ class NfcEnableDialog { .setMessage(R.string.dialog_nfc_enable_text) .setPositiveButton(R.string.common_ok) { _, _ -> try { - activity.startActivity(Intent(Settings.ACTION_NFC_SETTINGS)) + if ((activity as? LifecycleOwner)?.lifecycle?.currentState == Lifecycle.State.STARTED) { + activity.startActivity(Intent(Settings.ACTION_NFC_SETTINGS)) + } } catch (ex: ActivityNotFoundException) { print(ex.toString()) }