Files
Despensapp/app/src/main/java/com/example/despensapp/util/BiometricHelper.kt
T

55 lines
2.2 KiB
Kotlin

package com.example.despensapp.util
import android.content.Context
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.example.despensapp.R
object BiometricHelper {
fun isBiometricAvailable(context: Context): Boolean {
val biometricManager = BiometricManager.from(context)
return when (biometricManager.canAuthenticate(BIOMETRIC_STRONG or BIOMETRIC_WEAK)) {
BiometricManager.BIOMETRIC_SUCCESS -> true
else -> false
}
}
fun showBiometricPrompt(
activity: FragmentActivity,
onResult: (Boolean, String?) -> Unit
) {
val executor = ContextCompat.getMainExecutor(activity)
val biometricPrompt = BiometricPrompt(activity, executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
onResult(false, errString.toString())
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
onResult(true, null)
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
onResult(false, activity.getString(R.string.biometric_error_failed))
}
})
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(R.string.biometric_title))
.setSubtitle(activity.getString(R.string.biometric_subtitle))
.setNegativeButtonText(activity.getString(R.string.biometric_negative_button))
.setAllowedAuthenticators(BIOMETRIC_STRONG or BIOMETRIC_WEAK)
.build()
biometricPrompt.authenticate(promptInfo)
}
}