@@ -56,63 +56,7 @@ object BiometricAuthenticator {
56
56
@StringRes dialogTitleRes : Int = R .string.biometric_prompt_title,
57
57
callback : (Result ) -> Unit
58
58
) {
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)
116
60
val deviceHasKeyguard = activity.getSystemService<KeyguardManager >()?.isDeviceSecure == true
117
61
if (canAuthenticate(activity) || deviceHasKeyguard) {
118
62
val promptInfo =
@@ -123,11 +67,73 @@ object BiometricAuthenticator {
123
67
BiometricPrompt (
124
68
activity,
125
69
ContextCompat .getMainExecutor(activity.applicationContext),
126
- authCallback
70
+ authCallback,
127
71
)
128
72
.authenticate(promptInfo)
129
73
} else {
130
74
callback(Result .HardwareUnavailableOrDisabled )
131
75
}
132
76
}
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
+ }
133
139
}
0 commit comments