From ff780b02de77ae0adde23e57d9cc3245552c0f1c Mon Sep 17 00:00:00 2001 From: Aditya Wasan Date: Tue, 8 Sep 2020 14:24:19 +0530 Subject: Use same checks as BiometricAuthenticator in UserPreference (#1088) --- app/src/main/java/com/zeapo/pwdstore/UserPreference.kt | 7 +++---- .../main/java/com/zeapo/pwdstore/utils/BiometricAuthenticator.kt | 9 ++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'app') diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index 35c9e248..fa325f9c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -24,7 +24,6 @@ import androidx.activity.result.contract.ActivityResultContracts.OpenDocument import androidx.activity.result.contract.ActivityResultContracts.OpenDocumentTree import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.AppCompatTextView -import androidx.biometric.BiometricManager import androidx.core.content.edit import androidx.core.content.getSystemService import androidx.documentfile.provider.DocumentFile @@ -281,9 +280,9 @@ class UserPreference : AppCompatActivity() { findPreference(PreferenceKeys.ENABLE_DEBUG_LOGGING)?.isVisible = !BuildConfig.ENABLE_DEBUG_FEATURES findPreference(PreferenceKeys.BIOMETRIC_AUTH)?.apply { - val isFingerprintSupported = BiometricManager.from(requireContext()) - .canAuthenticate(BiometricManager.Authenticators.DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS - if (!isFingerprintSupported) { + val canAuthenticate = BiometricAuthenticator.canAuthenticate(prefsActivity) + + if (!canAuthenticate) { isEnabled = false isChecked = false summary = getString(R.string.biometric_auth_summary_error) diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/BiometricAuthenticator.kt b/app/src/main/java/com/zeapo/pwdstore/utils/BiometricAuthenticator.kt index d7ecb4cd..a1b4a279 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/BiometricAuthenticator.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/BiometricAuthenticator.kt @@ -20,6 +20,7 @@ import com.zeapo.pwdstore.R object BiometricAuthenticator { private const val TAG = "BiometricAuthenticator" + private const val validAuthenticators = Authenticators.DEVICE_CREDENTIAL or Authenticators.BIOMETRIC_WEAK sealed class Result { data class Success(val cryptoObject: BiometricPrompt.CryptoObject?) : Result() @@ -28,6 +29,10 @@ object BiometricAuthenticator { object Cancelled : Result() } + fun canAuthenticate(activity: FragmentActivity): Boolean { + return BiometricManager.from(activity).canAuthenticate(validAuthenticators) == BiometricManager.BIOMETRIC_SUCCESS + } + fun authenticate( activity: FragmentActivity, @StringRes dialogTitleRes: Int = R.string.biometric_prompt_title, @@ -60,10 +65,8 @@ object BiometricAuthenticator { callback(Result.Success(result.cryptoObject)) } } - val validAuthenticators = Authenticators.DEVICE_CREDENTIAL or Authenticators.BIOMETRIC_WEAK - val canAuth = BiometricManager.from(activity).canAuthenticate(validAuthenticators) == BiometricManager.BIOMETRIC_SUCCESS val deviceHasKeyguard = activity.getSystemService()?.isDeviceSecure == true - if (canAuth || deviceHasKeyguard) { + if (canAuthenticate(activity) || deviceHasKeyguard) { val promptInfo = BiometricPrompt.PromptInfo.Builder() .setTitle(activity.getString(dialogTitleRes)) .setAllowedAuthenticators(validAuthenticators) -- cgit v1.2.3