Last active
November 12, 2021 12:04
-
-
Save sergeykomlach/f2933c353def4ccf40539901e6ac3530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun startBioAuth() { | |
val iris = BiometricAuthRequest( | |
BiometricApi.AUTO, | |
BiometricType.BIOMETRIC_IRIS, | |
BiometricConfirmation.ANY | |
) | |
val faceId = BiometricAuthRequest( | |
BiometricApi.AUTO, | |
BiometricType.BIOMETRIC_FACE, | |
BiometricConfirmation.ANY | |
) | |
val fingerprint = BiometricAuthRequest( | |
BiometricApi.AUTO, | |
BiometricType.BIOMETRIC_FINGERPRINT, | |
BiometricConfirmation.ANY | |
) | |
var title = "" | |
val currentBiometric = | |
if (BiometricManagerCompat.isHardwareDetected(iris) | |
&& BiometricManagerCompat.hasEnrolled(iris) | |
) { | |
title = | |
"Your eyes are beautiful, but you can use them to unlock our app" | |
iris | |
} else | |
if (BiometricManagerCompat.isHardwareDetected(faceId) | |
&& BiometricManagerCompat.hasEnrolled(faceId) | |
) { | |
title = "Use your smiling face to enter the app" | |
faceId | |
} else if (BiometricManagerCompat.isHardwareDetected(fingerprint) | |
&& BiometricManagerCompat.hasEnrolled(fingerprint) | |
) { | |
title = "Your unique fingerprints can unlock this app" | |
fingerprint | |
} else { | |
null | |
} | |
currentBiometric?.let { biometricAuthRequest -> | |
if (BiometricManagerCompat.isBiometricSensorPermanentlyLocked(biometricAuthRequest) | |
|| BiometricManagerCompat.isLockOut(biometricAuthRequest) | |
) { | |
showToast("Biometric not available right now. Try again later") | |
return | |
} | |
val prompt = BiometricPromptCompat.Builder(this).apply { | |
this.setTitle(title) | |
this.setNegativeButton("Cancel", null) | |
}.build() | |
prompt.authenticate(object: BiometricPromptCompat.AuthenticationCallback { | |
override fun onSucceeded(confirmed: Set<BiometricType>) { | |
showToast("User authorized :)") | |
} | |
override fun onCanceled() { | |
showToast("Auth canceled :|") | |
} | |
override fun onFailed(reason: AuthenticationFailureReason?){ | |
showToast("Fatal error happens :( Reason $reason") | |
} | |
override fun onUIOpened() {} | |
override fun onUIClosed() {} | |
}) | |
} ?: run { | |
showToast("No available biometric on this device") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment