Skip to content

Instantly share code, notes, and snippets.

@isaidamier
Last active December 12, 2019 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaidamier/26e3c1aafd6f9d0775127183e7faead3 to your computer and use it in GitHub Desktop.
Save isaidamier/26e3c1aafd6f9d0775127183e7faead3 to your computer and use it in GitHub Desktop.
create a BiometricPrompt
private lateinit var biometricPrompt: BiometricPrompt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
// ...
biometricPrompt = createBiometricPrompt()
}
private fun createBiometricPrompt(): BiometricPrompt {
val executor = ContextCompat.getMainExecutor(this)
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d(TAG, "$errorCode :: $errString")
if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
loginWithPassword() // Because in this app, the negative button allows the user to enter an account password. This is completely optional and your app doesn’t have to do it.
}
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Log.d(TAG, "Authentication failed for an unknown reason")
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Log.d(TAG, "Authentication was successful")
// Proceed with viewing the private encrypted message.
showEncryptedMessage(result.cryptoObject)
}
}
val biometricPrompt = BiometricPrompt(this, executor, callback)
return biometricPrompt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment