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
def biometric_version = '1.0.0' | |
implementation "androidx.biometric:biometric:$biometric_version" |
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() | |
} |
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 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() |
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
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 | |
} | |
} |
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
// 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() | |
} | |
} |
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
override fun onClick(view: View) { | |
val promptInfo = createPromptInfo() | |
if (BiometricManager.from(context) | |
.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { | |
biometricPrompt.authenticate(promptInfo) | |
} else { | |
loginWithPassword() | |
} | |
} |
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
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { | |
super.onAuthenticationSucceeded(result) | |
Log.d(TAG, "Authentication was successful") | |
showMessage() | |
} |
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
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 |
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
def biometric_version = '1.0.1' | |
implementation "androidx.biometric:biometric:$biometric_version" |
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
class MainActivity : AppCompatActivity() { | |
... | |
private lateinit var biometricPrompt: BiometricPrompt | |
... | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
... | |
biometricPrompt = createBiometricPrompt() |