-
Notifications
You must be signed in to change notification settings - Fork 20
/
BiometricPromptUtils.kt
95 lines (84 loc) 路 3.48 KB
/
BiometricPromptUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.biomitric
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.biometric.BiometricPrompt.ERROR_NEGATIVE_BUTTON
import androidx.core.content.ContextCompat
import com.wire.android.R
import com.wire.android.appLogger
private const val TAG = "BiometricPromptUtils"
object BiometricPromptUtils {
fun createBiometricPrompt(
activity: AppCompatActivity,
onSuccess: () -> Unit,
onCancel: () -> Unit,
onRequestPasscode: () -> Unit
): BiometricPrompt {
val executor = ContextCompat.getMainExecutor(activity)
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errorString: CharSequence) {
super.onAuthenticationError(errorCode, errorString)
appLogger.i("$TAG errorCode is $errorCode and errorString is: $errorString")
if (errorCode == ERROR_NEGATIVE_BUTTON) {
onRequestPasscode()
} else {
onCancel()
}
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
appLogger.i("$TAG User biometric rejected")
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
appLogger.i("$TAG User biometric accepted")
onSuccess()
}
}
return BiometricPrompt(activity, executor, callback)
}
fun createPromptInfo(context: Context): BiometricPrompt.PromptInfo =
BiometricPrompt.PromptInfo.Builder().apply {
setTitle(context.getString(R.string.biometrics_prompt_dialog_title))
setSubtitle(context.getString(R.string.biometrics_prompt_dialog_subtitle))
setConfirmationRequired(false)
setNegativeButtonText(context.getString(R.string.biometrics_use_passcode_button))
}.build()
}
fun AppCompatActivity.showBiometricPrompt(
onSuccess: () -> Unit,
onCancel: () -> Unit,
onRequestPasscode: () -> Unit
) {
appLogger.i("$TAG showing biometrics dialog...")
val canAuthenticate = BiometricManager.from(this)
.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)
if (canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS) {
val biometricPrompt = BiometricPromptUtils.createBiometricPrompt(
this,
onSuccess,
onCancel,
onRequestPasscode,
)
val promptInfo = BiometricPromptUtils.createPromptInfo(this)
biometricPrompt.authenticate(promptInfo)
}
}