Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 7435842

Browse files
committed
refactor: extract prompt authentication callback creation
1 parent fe7aee2 commit 7435842

File tree

1 file changed

+64
-58
lines changed

1 file changed

+64
-58
lines changed

app/src/main/java/app/passwordstore/util/auth/BiometricAuthenticator.kt

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -56,63 +56,7 @@ object BiometricAuthenticator {
5656
@StringRes dialogTitleRes: Int = R.string.biometric_prompt_title,
5757
callback: (Result) -> Unit
5858
) {
59-
val authCallback =
60-
object : BiometricPrompt.AuthenticationCallback() {
61-
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
62-
super.onAuthenticationError(errorCode, errString)
63-
logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" }
64-
when (errorCode) {
65-
BiometricPrompt.ERROR_CANCELED,
66-
BiometricPrompt.ERROR_USER_CANCELED,
67-
BiometricPrompt.ERROR_NEGATIVE_BUTTON -> {
68-
callback(Result.Cancelled)
69-
}
70-
BiometricPrompt.ERROR_HW_NOT_PRESENT,
71-
BiometricPrompt.ERROR_HW_UNAVAILABLE,
72-
BiometricPrompt.ERROR_NO_BIOMETRICS,
73-
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> {
74-
callback(Result.HardwareUnavailableOrDisabled)
75-
}
76-
BiometricPrompt.ERROR_LOCKOUT,
77-
BiometricPrompt.ERROR_LOCKOUT_PERMANENT,
78-
BiometricPrompt.ERROR_NO_SPACE,
79-
BiometricPrompt.ERROR_TIMEOUT,
80-
BiometricPrompt.ERROR_VENDOR -> {
81-
callback(
82-
Result.Failure(
83-
errorCode,
84-
activity.getString(R.string.biometric_auth_error_reason, errString)
85-
)
86-
)
87-
}
88-
BiometricPrompt.ERROR_UNABLE_TO_PROCESS -> {
89-
callback(Result.Retry)
90-
}
91-
// We cover all guaranteed values above, but [errorCode] is still an Int
92-
// at the end of the day so a catch-all else will always be required.
93-
else -> {
94-
callback(
95-
Result.Failure(
96-
errorCode,
97-
activity.getString(R.string.biometric_auth_error_reason, errString)
98-
)
99-
)
100-
}
101-
}
102-
}
103-
104-
override fun onAuthenticationFailed() {
105-
super.onAuthenticationFailed()
106-
logcat(TAG) { "onAuthenticationFailed()" }
107-
callback(Result.Retry)
108-
}
109-
110-
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
111-
super.onAuthenticationSucceeded(result)
112-
logcat(TAG) { "onAuthenticationSucceeded()" }
113-
callback(Result.Success(result.cryptoObject))
114-
}
115-
}
59+
val authCallback = createPromptAuthenticationCallback(activity, callback)
11660
val deviceHasKeyguard = activity.getSystemService<KeyguardManager>()?.isDeviceSecure == true
11761
if (canAuthenticate(activity) || deviceHasKeyguard) {
11862
val promptInfo =
@@ -123,11 +67,73 @@ object BiometricAuthenticator {
12367
BiometricPrompt(
12468
activity,
12569
ContextCompat.getMainExecutor(activity.applicationContext),
126-
authCallback
70+
authCallback,
12771
)
12872
.authenticate(promptInfo)
12973
} else {
13074
callback(Result.HardwareUnavailableOrDisabled)
13175
}
13276
}
77+
78+
private fun createPromptAuthenticationCallback(
79+
activity: FragmentActivity,
80+
callback: (Result) -> Unit,
81+
): BiometricPrompt.AuthenticationCallback {
82+
return object : BiometricPrompt.AuthenticationCallback() {
83+
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
84+
super.onAuthenticationError(errorCode, errString)
85+
logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" }
86+
when (errorCode) {
87+
BiometricPrompt.ERROR_CANCELED,
88+
BiometricPrompt.ERROR_USER_CANCELED,
89+
BiometricPrompt.ERROR_NEGATIVE_BUTTON -> {
90+
callback(Result.Cancelled)
91+
}
92+
BiometricPrompt.ERROR_HW_NOT_PRESENT,
93+
BiometricPrompt.ERROR_HW_UNAVAILABLE,
94+
BiometricPrompt.ERROR_NO_BIOMETRICS,
95+
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> {
96+
callback(Result.HardwareUnavailableOrDisabled)
97+
}
98+
BiometricPrompt.ERROR_LOCKOUT,
99+
BiometricPrompt.ERROR_LOCKOUT_PERMANENT,
100+
BiometricPrompt.ERROR_NO_SPACE,
101+
BiometricPrompt.ERROR_TIMEOUT,
102+
BiometricPrompt.ERROR_VENDOR -> {
103+
callback(
104+
Result.Failure(
105+
errorCode,
106+
activity.getString(R.string.biometric_auth_error_reason, errString)
107+
)
108+
)
109+
}
110+
BiometricPrompt.ERROR_UNABLE_TO_PROCESS -> {
111+
callback(Result.Retry)
112+
}
113+
// We cover all guaranteed values above, but [errorCode] is still an Int
114+
// at the end of the day so a catch-all else will always be required.
115+
else -> {
116+
callback(
117+
Result.Failure(
118+
errorCode,
119+
activity.getString(R.string.biometric_auth_error_reason, errString)
120+
)
121+
)
122+
}
123+
}
124+
}
125+
126+
override fun onAuthenticationFailed() {
127+
super.onAuthenticationFailed()
128+
logcat(TAG) { "onAuthenticationFailed()" }
129+
callback(Result.Retry)
130+
}
131+
132+
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
133+
super.onAuthenticationSucceeded(result)
134+
logcat(TAG) { "onAuthenticationSucceeded()" }
135+
callback(Result.Success(result.cryptoObject))
136+
}
137+
}
138+
}
133139
}

0 commit comments

Comments
 (0)