diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt index 750b895b7..081719cf8 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -863,14 +863,41 @@ fun BaseSimpleActivity.getFileOutputStreamSync(path: String, mimeType: String, p } } -fun FragmentActivity.showBiometricPrompt(successCallback: (BiometricPrompt.AuthenticationResult) -> Unit) { +fun FragmentActivity.performSecurityCheck( + protectionType: Int, + requiredHash: String, + successCallback: ((String, Int) -> Unit)? = null, + failureCallback: (() -> Unit)? = null +) { + if (protectionType == PROTECTION_FINGERPRINT && isTargetSdkVersion30Plus()) { + showBiometricPrompt(successCallback, failureCallback) + } else { + SecurityDialog( + activity = this, + requiredHash = requiredHash, + showTabIndex = protectionType, + callback = { hash, type, success -> + if (success) { + successCallback?.invoke(hash, type) + } else { + failureCallback?.invoke() + } + } + ) + } +} + +fun FragmentActivity.showBiometricPrompt( + successCallback: ((String, Int) -> Unit)? = null, + failureCallback: (() -> Unit)? = null +) { Class2BiometricAuthPrompt.Builder(getText(R.string.authenticate), getText(R.string.cancel)) .build() .startAuthentication( AuthPromptHost(this), object : AuthPromptCallback() { override fun onAuthenticationSucceeded(activity: FragmentActivity?, result: BiometricPrompt.AuthenticationResult) { - successCallback(result) + successCallback?.invoke("", PROTECTION_FINGERPRINT) } override fun onAuthenticationError(activity: FragmentActivity?, errorCode: Int, errString: CharSequence) { @@ -878,10 +905,12 @@ fun FragmentActivity.showBiometricPrompt(successCallback: (BiometricPrompt.Authe if (!isCanceledByUser) { toast(errString.toString()) } + failureCallback?.invoke() } override fun onAuthenticationFailed(activity: FragmentActivity?) { toast(R.string.authentication_failed) + failureCallback?.invoke() } } ) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/BiometricIdTab.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/BiometricIdTab.kt index dcbcba549..6b1179a72 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/BiometricIdTab.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/BiometricIdTab.kt @@ -20,9 +20,7 @@ class BiometricIdTab(context: Context, attrs: AttributeSet) : ConstraintLayout(c context.updateTextColors(biometric_lock_holder) open_biometric_dialog.setOnClickListener { - biometricPromptHost.activity?.showBiometricPrompt { - hashListener.receivedHash("", PROTECTION_FINGERPRINT) - } + biometricPromptHost.activity?.showBiometricPrompt(successCallback = hashListener::receivedHash) } }