Last active
December 12, 2019 17:26
-
-
Save isaidamier/26e3c1aafd6f9d0775127183e7faead3 to your computer and use it in GitHub Desktop.
create a BiometricPrompt
This file contains hidden or 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 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