refactor: extract prompt authentication callback creation

This commit is contained in:
Harsh Shandilya 2023-07-09 18:03:41 +05:30
parent fe7aee24d4
commit 7435842bd9
No known key found for this signature in database

View file

@ -56,8 +56,30 @@ object BiometricAuthenticator {
@StringRes dialogTitleRes: Int = R.string.biometric_prompt_title, @StringRes dialogTitleRes: Int = R.string.biometric_prompt_title,
callback: (Result) -> Unit callback: (Result) -> Unit
) { ) {
val authCallback = val authCallback = createPromptAuthenticationCallback(activity, callback)
object : BiometricPrompt.AuthenticationCallback() { val deviceHasKeyguard = activity.getSystemService<KeyguardManager>()?.isDeviceSecure == true
if (canAuthenticate(activity) || deviceHasKeyguard) {
val promptInfo =
BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(dialogTitleRes))
.setAllowedAuthenticators(VALID_AUTHENTICATORS)
.build()
BiometricPrompt(
activity,
ContextCompat.getMainExecutor(activity.applicationContext),
authCallback,
)
.authenticate(promptInfo)
} else {
callback(Result.HardwareUnavailableOrDisabled)
}
}
private fun createPromptAuthenticationCallback(
activity: FragmentActivity,
callback: (Result) -> Unit,
): BiometricPrompt.AuthenticationCallback {
return object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString) super.onAuthenticationError(errorCode, errString)
logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" } logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" }
@ -113,21 +135,5 @@ object BiometricAuthenticator {
callback(Result.Success(result.cryptoObject)) callback(Result.Success(result.cryptoObject))
} }
} }
val deviceHasKeyguard = activity.getSystemService<KeyguardManager>()?.isDeviceSecure == true
if (canAuthenticate(activity) || deviceHasKeyguard) {
val promptInfo =
BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(dialogTitleRes))
.setAllowedAuthenticators(VALID_AUTHENTICATORS)
.build()
BiometricPrompt(
activity,
ContextCompat.getMainExecutor(activity.applicationContext),
authCallback
)
.authenticate(promptInfo)
} else {
callback(Result.HardwareUnavailableOrDisabled)
}
} }
} }