more tweaking

This commit is contained in:
Milos Kozak 2019-10-26 23:21:51 +02:00
parent 7af7220301
commit 466084db71
2 changed files with 24 additions and 11 deletions

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.utils.protection package info.nightscout.androidaps.utils.protection
import androidx.biometric.BiometricConstants
import androidx.biometric.BiometricPrompt import androidx.biometric.BiometricPrompt
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.MainApp
@ -8,17 +9,30 @@ import info.nightscout.androidaps.utils.ToastUtils
import java.util.concurrent.Executors import java.util.concurrent.Executors
object BiometricCheck { 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 executor = Executors.newSingleThreadExecutor()
val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() { val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString) super.onAuthenticationError(errorCode, errString)
if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) cancel?.run() when (errorCode) {
else { BiometricConstants.ERROR_UNABLE_TO_PROCESS,
// Called when an unrecoverable error has been encountered and the operation is complete. 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()) 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 // call ok, because it's not possible to bypass it when biometrics fail
ok?.run() ok?.run()
} }
@ -38,8 +52,7 @@ object BiometricCheck {
}) })
val promptInfo = BiometricPrompt.PromptInfo.Builder() val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(MainApp.gs(R.string.biometric_title)) .setTitle(MainApp.gs(title))
.setSubtitle("Set the subtitle to display.")
.setDescription(MainApp.gs(R.string.biometric_title)) .setDescription(MainApp.gs(R.string.biometric_title))
.setNegativeButtonText(MainApp.gs(R.string.cancel)) .setNegativeButtonText(MainApp.gs(R.string.cancel))
.build() .build()

View file

@ -47,7 +47,7 @@ object ProtectionCheck {
ProtectionType.NONE -> ProtectionType.NONE ->
ok?.run() ok?.run()
ProtectionType.BIOMETRIC -> ProtectionType.BIOMETRIC ->
BiometricCheck.biometricPrompt(activity, ok, cancel, fail) BiometricCheck.biometricPrompt(activity, titleResourceIDs[protection.ordinal], ok, cancel, fail)
ProtectionType.PASSWORD -> ProtectionType.PASSWORD ->
PasswordCheck.queryPassword(activity, titleResourceIDs[protection.ordinal], passwordsResourceIDs[protection.ordinal], ok, cancel, fail) PasswordCheck.queryPassword(activity, titleResourceIDs[protection.ordinal], passwordsResourceIDs[protection.ordinal], ok, cancel, fail)
} }