Create a unified API that hides SecurityDialog and BiometricPrompt

This commit is contained in:
Andrii Chubko 2021-08-24 19:21:14 +03:00
parent 16731c9ba5
commit 615349768d
2 changed files with 32 additions and 5 deletions

View file

@ -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)) Class2BiometricAuthPrompt.Builder(getText(R.string.authenticate), getText(R.string.cancel))
.build() .build()
.startAuthentication( .startAuthentication(
AuthPromptHost(this), AuthPromptHost(this),
object : AuthPromptCallback() { object : AuthPromptCallback() {
override fun onAuthenticationSucceeded(activity: FragmentActivity?, result: BiometricPrompt.AuthenticationResult) { override fun onAuthenticationSucceeded(activity: FragmentActivity?, result: BiometricPrompt.AuthenticationResult) {
successCallback(result) successCallback?.invoke("", PROTECTION_FINGERPRINT)
} }
override fun onAuthenticationError(activity: FragmentActivity?, errorCode: Int, errString: CharSequence) { override fun onAuthenticationError(activity: FragmentActivity?, errorCode: Int, errString: CharSequence) {
@ -878,10 +905,12 @@ fun FragmentActivity.showBiometricPrompt(successCallback: (BiometricPrompt.Authe
if (!isCanceledByUser) { if (!isCanceledByUser) {
toast(errString.toString()) toast(errString.toString())
} }
failureCallback?.invoke()
} }
override fun onAuthenticationFailed(activity: FragmentActivity?) { override fun onAuthenticationFailed(activity: FragmentActivity?) {
toast(R.string.authentication_failed) toast(R.string.authentication_failed)
failureCallback?.invoke()
} }
} }
) )

View file

@ -20,9 +20,7 @@ class BiometricIdTab(context: Context, attrs: AttributeSet) : ConstraintLayout(c
context.updateTextColors(biometric_lock_holder) context.updateTextColors(biometric_lock_holder)
open_biometric_dialog.setOnClickListener { open_biometric_dialog.setOnClickListener {
biometricPromptHost.activity?.showBiometricPrompt { biometricPromptHost.activity?.showBiometricPrompt(successCallback = hashListener::receivedHash)
hashListener.receivedHash("", PROTECTION_FINGERPRINT)
}
} }
} }