Skip to content

Instantly share code, notes, and snippets.

@aneury1
Created August 11, 2024 07:52
Show Gist options
  • Save aneury1/4c3d37d7cb21c239c19fcc0b2e6cf59b to your computer and use it in GitHub Desktop.
Save aneury1/4c3d37d7cb21c239c19fcc0b2e6cf59b to your computer and use it in GitHub Desktop.
import android.content.Context
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.biometric.BiometricPrompt.PromptInfo
import androidx.fragment.app.FragmentActivity
import com.aneury1.jholisnutris.config.BiometricStatus
class BiometricAuthentication(
private val context: Context
) {
private lateinit var promptInfo: PromptInfo
private val biometricManager = BiometricManager.from(context)
private lateinit var biometricPrompt: BiometricPrompt
fun isBioemtricAuthAvailable() : BiometricStatus{
return when(biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)){
BiometricManager.BIOMETRIC_SUCCESS-> BiometricStatus.Ready
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE-> BiometricStatus.NotAvailable
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE-> BiometricStatus.TemporaryNotAvailable
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED-> BiometricStatus.AvailableButNorEnrolled
else -> {
return BiometricStatus.NotAvailable
}
}
}
fun promptBiometricAuthentication(
title: String,
subtitle: String,
negativeButtonText: String,
fragment: FragmentActivity,
onSuccess: (result: BiometricPrompt.AuthenticationResult) -> Unit,
onFailed: () -> Unit,
onError: (errorCode: Int, errorMessage: String) -> Unit
){
when(isBioemtricAuthAvailable()){
BiometricStatus.NotAvailable->{
onError(BiometricStatus.NotAvailable.id, "Biometric Not Available")
return
}
BiometricStatus.Ready ->{
}
BiometricStatus.TemporaryNotAvailable->{
onError(BiometricStatus.TemporaryNotAvailable.id, "Biometric Temp. Not Available")
return ;
}
BiometricStatus.AvailableButNorEnrolled-> {
onError(BiometricStatus.TemporaryNotAvailable.id, "Biometric Not Enrolled yet")
return;
}
}
biometricPrompt = BiometricPrompt(
fragment,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
onSuccess(result)
}
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
onError(errorCode, errString.toString());
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
onFailed()
}
}
)
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(title)
.setSubtitle(subtitle)
.setNegativeButtonText(negativeButtonText)
.build()
biometricPrompt.authenticate(promptInfo)
}
}
///// Jetpack Activity bellow
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val activity = LocalContext.current as FragmentActivity
var message by remember { mutableStateOf("") }
TextButton(onClick = {
biometricAuthentication.promptBiometricAuthentication(
title = "Biometric Authentication",
subtitle = "Log in using your biometric credential",
negativeButtonText = "Cancel",
fragment = activity,
onSuccess = {
message = "Authenticated..."
},
onFailed = {
message = "Failed Authentication..."
},
onError = { _, errormessage ->
message = errormessage
})
}) {
Text(text = "User Entry")
}
Spacer(modifier = Modifier.height(16.dp))
Text(text = message)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment