refactor: extract prompt authentication callback creation
This commit is contained in:
parent
fe7aee24d4
commit
7435842bd9
1 changed files with 64 additions and 58 deletions
|
@ -56,8 +56,30 @@ object BiometricAuthenticator {
|
|||
@StringRes dialogTitleRes: Int = R.string.biometric_prompt_title,
|
||||
callback: (Result) -> Unit
|
||||
) {
|
||||
val authCallback =
|
||||
object : BiometricPrompt.AuthenticationCallback() {
|
||||
val authCallback = createPromptAuthenticationCallback(activity, callback)
|
||||
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) {
|
||||
super.onAuthenticationError(errorCode, errString)
|
||||
logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" }
|
||||
|
@ -113,21 +135,5 @@ object BiometricAuthenticator {
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue