61 lines
2.3 KiB
Kotlin
61 lines
2.3 KiB
Kotlin
package com.expapp.trigger.security
|
|||
|
|
|
||
|
|
import android.content.Context
|
||
|
|
import androidx.biometric.BiometricManager
|
||
|
|
import androidx.biometric.BiometricPrompt
|
||
|
|
import androidx.core.content.ContextCompat
|
||
|
|
import androidx.fragment.app.FragmentActivity
|
||
|
|
|
||
|
|
object BiometricHelper {
|
||
|
|
|
||
|
|
fun isBiometricAvailable(context: Context): Boolean {
|
||
|
|
val biometricManager = BiometricManager.from(context)
|
||
|
|
val authenticators = BiometricManager.Authenticators.BIOMETRIC_STRONG or
|
||
|
|
BiometricManager.Authenticators.BIOMETRIC_WEAK or
|
||
|
|
BiometricManager.Authenticators.DEVICE_CREDENTIAL
|
||
|
|
return biometricManager.canAuthenticate(authenticators) == BiometricManager.BIOMETRIC_SUCCESS
|
||
|
|
}
|
||
|
|
|
||
|
|
fun authenticate(
|
||
|
|
activity: FragmentActivity,
|
||
|
|
title: String,
|
||
|
|
subtitle: String = "Verify your identity to proceed",
|
||
|
|
onResult: (success: Boolean, errorMessage: String?) -> Unit
|
||
|
|
) {
|
||
|
|
val executor = ContextCompat.getMainExecutor(activity)
|
||
|
|
|
||
|
|
val prompt = BiometricPrompt(
|
||
|
|
activity,
|
||
|
|
executor,
|
||
|
|
object : BiometricPrompt.AuthenticationCallback() {
|
||
|
|
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
||
|
|
super.onAuthenticationSucceeded(result)
|
||
|
|
onResult(true, null)
|
||
|
|
}
|
||
|
|
|
||
|
|
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
|
||
|
|
super.onAuthenticationError(errorCode, errString)
|
||
|
|
// If user clicked cancel or back, return false with message
|
||
|
|
onResult(false, errString.toString())
|
||
|
|
}
|
||
|
|
|
||
|
|
override fun onAuthenticationFailed() {
|
||
|
|
super.onAuthenticationFailed()
|
||
|
|
// Intermediate failure (e.g. unrecognised finger), BiometricPrompt handles retry UI
|
||
|
|
}
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
val promptInfo = BiometricPrompt.PromptInfo.Builder()
|
||
|
|
.setTitle(title)
|
||
|
|
.setSubtitle(subtitle)
|
||
|
|
.setAllowedAuthenticators(
|
||
|
|
BiometricManager.Authenticators.BIOMETRIC_STRONG or
|
||
|
|
BiometricManager.Authenticators.DEVICE_CREDENTIAL
|
||
|
|
)
|
||
|
|
.build()
|
||
|
|
|
||
|
|
prompt.authenticate(promptInfo)
|
||
|
|
}
|
||
|
|
}
|