Skip to content

Commit

Permalink
Fix biometric lock issue on devices running Android 10 or 9 (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
PranshulGG authored Dec 23, 2024
1 parent a8d79e1 commit 4529e3f
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions app/src/main/java/com/philkes/notallyx/utils/security/LockUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,64 @@ private fun showBiometricOrPinPrompt(
onFailure: () -> Unit,
) {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
// Android 11+ with BiometricPrompt and Authenticators
val prompt = BiometricPrompt.Builder(context)
.apply {
setTitle(context.getString(titleResId))
descriptionResId?.let {
setDescription(context.getString(descriptionResId))
}
setAllowedAuthenticators(
BiometricManager.Authenticators.BIOMETRIC_STRONG or
BiometricManager.Authenticators.DEVICE_CREDENTIAL
)
}
.build()
val cipher =
if (isForDecrypt) {
getInitializedCipherForDecryption(iv = cipherIv!!)
} else {
getInitializedCipherForEncryption()
}
prompt.authenticate(
BiometricPrompt.CryptoObject(cipher),
getCancellationSignal(context),
context.mainExecutor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(
result: BiometricPrompt.AuthenticationResult?
) {
super.onAuthenticationSucceeded(result)
onSuccess.invoke(result!!.cryptoObject!!.cipher)
}

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
onFailure.invoke()
}

override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
super.onAuthenticationError(errorCode, errString)
onFailure.invoke()
}
},
)
}
Build.VERSION.SDK_INT == Build.VERSION_CODES.Q -> {
// Android 10: Use BiometricPrompt without Authenticators
val prompt =
BiometricPrompt.Builder(context)
.apply {
setTitle(context.getString(titleResId))
descriptionResId?.let {
setDescription(context.getString(descriptionResId))
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
setAllowedAuthenticators(
BiometricManager.Authenticators.BIOMETRIC_STRONG or
BiometricManager.Authenticators.DEVICE_CREDENTIAL
)
setNegativeButton(
context.getString(R.string.cancel),
context.mainExecutor
) { _, _ ->
onFailure.invoke()
}
}
.build()
Expand Down Expand Up @@ -115,12 +160,13 @@ private fun showBiometricOrPinPrompt(
},
)
}

Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> {
val fingerprintManager =
ContextCompat.getSystemService(context, FingerprintManager::class.java)
if (
fingerprintManager?.isHardwareDetected == true &&
fingerprintManager.hasEnrolledFingerprints()
fingerprintManager.hasEnrolledFingerprints()
) {
val cipher =
if (isForDecrypt) {
Expand Down

0 comments on commit 4529e3f

Please sign in to comment.