From 9a60573c2448e5525d78d7eaaccdf345b9707cb6 Mon Sep 17 00:00:00 2001 From: Pranshul Date: Mon, 23 Dec 2024 13:18:48 +0530 Subject: [PATCH 1/3] fixed BiometricLock on older versions --- .../notallyx/utils/security/LockUtils.kt | 68 +++++++++++++++++-- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt b/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt index 97a117e2..a3976481 100644 --- a/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt +++ b/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt @@ -69,7 +69,62 @@ private fun showBiometricOrPinPrompt( onFailure: () -> Unit, ) { when { - Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> { + // Android 11+ with BiometricPrompt and Authenticators + val prompt = BiometricPrompt.Builder(context) + .apply { + setTitle(context.getString(titleResId)) + descriptionResId?.let { + setDescription(context.getString(descriptionResId)) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + setAllowedAuthenticators( + BiometricManager.Authenticators.BIOMETRIC_STRONG or + BiometricManager.Authenticators.DEVICE_CREDENTIAL + ) + } else { + // Add negative button text for Android 9 and 10 + setNegativeButton( + context.getString(R.string.cancel), + context.mainExecutor + ) { _, _ -> + onFailure.invoke() + } + } + } + .build() + val cipher = + if (isForDecrypt) { + getInitializedCipherForDecryption(iv = cipherIv!!) + } else { + getInitializedCipherForEncryption() + } + prompt.authenticate( + BiometricPrompt.CryptoObject(cipher), + getCancellationSignal(context), + context.mainExecutor, + object : BiometricPrompt.AuthenticationCallback() { + override fun onAuthenticationSucceeded( + result: BiometricPrompt.AuthenticationResult? + ) { + super.onAuthenticationSucceeded(result) + onSuccess.invoke(result!!.cryptoObject!!.cipher) + } + + override fun onAuthenticationFailed() { + super.onAuthenticationFailed() + onFailure.invoke() + } + + override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) { + super.onAuthenticationError(errorCode, errString) + onFailure.invoke() + } + }, + ) + } + Build.VERSION.SDK_INT == Build.VERSION_CODES.Q -> { + // Android 10: Use BiometricPrompt without Authenticators val prompt = BiometricPrompt.Builder(context) .apply { @@ -77,11 +132,11 @@ private fun showBiometricOrPinPrompt( descriptionResId?.let { setDescription(context.getString(descriptionResId)) } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - setAllowedAuthenticators( - BiometricManager.Authenticators.BIOMETRIC_STRONG or - BiometricManager.Authenticators.DEVICE_CREDENTIAL - ) + setNegativeButton( + context.getString(R.string.cancel), + context.mainExecutor + ) { _, _ -> + onFailure.invoke() } } .build() @@ -115,6 +170,7 @@ private fun showBiometricOrPinPrompt( }, ) } + Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> { val fingerprintManager = ContextCompat.getSystemService(context, FingerprintManager::class.java) From 3eb53c6ecb1c9be258c2508445b73d80d31eff99 Mon Sep 17 00:00:00 2001 From: Pranshul Date: Mon, 23 Dec 2024 15:04:05 +0530 Subject: [PATCH 2/3] simplify biometricprompt version check --- .../notallyx/utils/security/LockUtils.kt | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt b/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt index a3976481..61bbe673 100644 --- a/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt +++ b/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt @@ -77,22 +77,19 @@ private fun showBiometricOrPinPrompt( descriptionResId?.let { setDescription(context.getString(descriptionResId)) } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - setAllowedAuthenticators( - BiometricManager.Authenticators.BIOMETRIC_STRONG or - BiometricManager.Authenticators.DEVICE_CREDENTIAL - ) - } else { - // Add negative button text for Android 9 and 10 - setNegativeButton( - context.getString(R.string.cancel), - context.mainExecutor - ) { _, _ -> - onFailure.invoke() - } + setAllowedAuthenticators( + BiometricManager.Authenticators.BIOMETRIC_STRONG or + BiometricManager.Authenticators.DEVICE_CREDENTIAL + ) + setNegativeButton( + context.getString(R.string.cancel), + context.mainExecutor + ) { _, _ -> + onFailure.invoke() } } .build() + val cipher = if (isForDecrypt) { getInitializedCipherForDecryption(iv = cipherIv!!) @@ -176,7 +173,7 @@ private fun showBiometricOrPinPrompt( ContextCompat.getSystemService(context, FingerprintManager::class.java) if ( fingerprintManager?.isHardwareDetected == true && - fingerprintManager.hasEnrolledFingerprints() + fingerprintManager.hasEnrolledFingerprints() ) { val cipher = if (isForDecrypt) { From 4561bd6f0fe352dd1e3d42b8ac87889714bc6632 Mon Sep 17 00:00:00 2001 From: Pranshul Date: Mon, 23 Dec 2024 15:54:17 +0530 Subject: [PATCH 3/3] removed setNegativeButton --- .../java/com/philkes/notallyx/utils/security/LockUtils.kt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt b/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt index 61bbe673..a9e0fa0b 100644 --- a/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt +++ b/app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt @@ -81,15 +81,8 @@ private fun showBiometricOrPinPrompt( BiometricManager.Authenticators.BIOMETRIC_STRONG or BiometricManager.Authenticators.DEVICE_CREDENTIAL ) - setNegativeButton( - context.getString(R.string.cancel), - context.mainExecutor - ) { _, _ -> - onFailure.invoke() - } } .build() - val cipher = if (isForDecrypt) { getInitializedCipherForDecryption(iv = cipherIv!!)