From 466084db71dd66748dda438e5b5d87b6e9d355bc Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 26 Oct 2019 23:21:51 +0200 Subject: [PATCH] more tweaking --- .../utils/protection/BiometricCheck.kt | 33 +++++++++++++------ .../utils/protection/ProtectionCheck.kt | 2 +- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/protection/BiometricCheck.kt b/app/src/main/java/info/nightscout/androidaps/utils/protection/BiometricCheck.kt index decbf92056..5aba1cf12f 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/protection/BiometricCheck.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/protection/BiometricCheck.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.utils.protection +import androidx.biometric.BiometricConstants import androidx.biometric.BiometricPrompt import androidx.fragment.app.FragmentActivity import info.nightscout.androidaps.MainApp @@ -8,19 +9,32 @@ import info.nightscout.androidaps.utils.ToastUtils import java.util.concurrent.Executors object BiometricCheck { - fun biometricPrompt(activity: FragmentActivity, ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) { + fun biometricPrompt(activity: FragmentActivity, title: Int, ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) { val executor = Executors.newSingleThreadExecutor() val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() { - override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { super.onAuthenticationError(errorCode, errString) - if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) cancel?.run() - else { - // Called when an unrecoverable error has been encountered and the operation is complete. - ToastUtils.showToastInUiThread(activity.baseContext, errString.toString()) - // call ok, because it's not possible to bypass it when biometrics fail - ok?.run() + when (errorCode) { + BiometricConstants.ERROR_UNABLE_TO_PROCESS, + BiometricConstants.ERROR_TIMEOUT, + BiometricConstants.ERROR_CANCELED, + BiometricConstants.ERROR_LOCKOUT, + BiometricConstants.ERROR_VENDOR, + BiometricConstants.ERROR_LOCKOUT_PERMANENT, + BiometricConstants.ERROR_USER_CANCELED -> { + ToastUtils.showToastInUiThread(activity.baseContext, errString.toString()) + fail?.run() + } + BiometricConstants.ERROR_NEGATIVE_BUTTON -> + cancel?.run() + BiometricConstants.ERROR_NO_SPACE, + BiometricConstants.ERROR_HW_UNAVAILABLE, + BiometricConstants.ERROR_HW_NOT_PRESENT, + BiometricConstants.ERROR_NO_DEVICE_CREDENTIAL, + BiometricConstants.ERROR_NO_BIOMETRICS -> + // call ok, because it's not possible to bypass it when biometrics fail + ok?.run() } } @@ -38,8 +52,7 @@ object BiometricCheck { }) val promptInfo = BiometricPrompt.PromptInfo.Builder() - .setTitle(MainApp.gs(R.string.biometric_title)) - .setSubtitle("Set the subtitle to display.") + .setTitle(MainApp.gs(title)) .setDescription(MainApp.gs(R.string.biometric_title)) .setNegativeButtonText(MainApp.gs(R.string.cancel)) .build() diff --git a/app/src/main/java/info/nightscout/androidaps/utils/protection/ProtectionCheck.kt b/app/src/main/java/info/nightscout/androidaps/utils/protection/ProtectionCheck.kt index 86e29a7ed7..c7880aee08 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/protection/ProtectionCheck.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/protection/ProtectionCheck.kt @@ -47,7 +47,7 @@ object ProtectionCheck { ProtectionType.NONE -> ok?.run() ProtectionType.BIOMETRIC -> - BiometricCheck.biometricPrompt(activity, ok, cancel, fail) + BiometricCheck.biometricPrompt(activity, titleResourceIDs[protection.ordinal], ok, cancel, fail) ProtectionType.PASSWORD -> PasswordCheck.queryPassword(activity, titleResourceIDs[protection.ordinal], passwordsResourceIDs[protection.ordinal], ok, cancel, fail) }