Skip to content

Instantly share code, notes, and snippets.

View isaidamier's full-sized avatar

Isai Damier isaidamier

View GitHub Profile
@isaidamier
isaidamier / biometric_version.gradle
Last active November 21, 2019 22:39
biometric_version 1
def biometric_version = '1.0.0'
implementation "androidx.biometric:biometric:$biometric_version"
@isaidamier
isaidamier / createBiometricPrompt.kt
Last active December 12, 2019 17:26
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()
}
@isaidamier
isaidamier / createPromptInfo.kt
Created November 26, 2019 02:41
createPromptInfo
private fun createPromptInfo(): BiometricPrompt.PromptInfo {
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.prompt_info_title))
.setSubtitle(getString(R.string.prompt_info_subtitle))
.setDescription(getString(R.string.prompt_info_description))
// Authenticate without requiring the user to press a "confirm"
// button after satisfying the biometric check
.setConfirmationRequired(false)
.setNegativeButtonText(getString(R.string.prompt_info_use_app_password))
.build()
@isaidamier
isaidamier / onAuthenticationError.kt
Created November 26, 2019 02:44
onAuthenticationError
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d(TAG, "$errorCode :: $errString")
if(errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
loginWithPassword() // Because negative button says use application password
}
}
@isaidamier
isaidamier / biometricOnClick.kt
Created November 26, 2019 02:48
biometricOnClick
// Callback for the "authenticate" button in your app's UI.
override fun onClick(view: View) {
val promptInfo = createPromptInfo()
if (BiometricManager.from(context)
.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
biometricPrompt.authenticate(promptInfo, cryptoObject)
} else {
loginWithPassword()
}
}
@isaidamier
isaidamier / onClickNoCrypto.kt
Created November 26, 2019 02:51
onClickNoCrypto
override fun onClick(view: View) {
val promptInfo = createPromptInfo()
if (BiometricManager.from(context)
.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
biometricPrompt.authenticate(promptInfo)
} else {
loginWithPassword()
}
}
@isaidamier
isaidamier / onAuthenticationSucceededNoCrypto.kt
Created November 26, 2019 02:54
onAuthenticationSucceededNoCrypto
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Log.d(TAG, "Authentication was successful")
showMessage()
}
interface CryptographyManager {
/**
* This method first gets or generates an instance of SecretKey and then initializes the Cipher
* with the key. The secret key uses [ENCRYPT_MODE][Cipher.ENCRYPT_MODE] is used.
*/
fun getInitializedCipherForEncryption(keyName: String): Cipher
/**
* This method first gets or generates an instance of SecretKey and then initializes the Cipher
def biometric_version = '1.0.1'
implementation "androidx.biometric:biometric:$biometric_version"
class MainActivity : AppCompatActivity() {
...
private lateinit var biometricPrompt: BiometricPrompt
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
biometricPrompt = createBiometricPrompt()