Extract showing biometric prompt as an extension function

This commit is contained in:
Andrii Chubko 2021-08-24 17:20:31 +03:00
parent 7212ed2a54
commit 16731c9ba5
2 changed files with 32 additions and 33 deletions

View file

@ -8,7 +8,6 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.TransactionTooLargeException
import android.provider.ContactsContract
import android.provider.DocumentsContract
@ -23,9 +22,12 @@ import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricPrompt
import androidx.biometric.auth.AuthPromptCallback
import androidx.biometric.auth.AuthPromptHost
import androidx.biometric.auth.Class2BiometricAuthPrompt
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.FragmentActivity
import com.simplemobiletools.commons.R
@ -861,6 +863,30 @@ fun BaseSimpleActivity.getFileOutputStreamSync(path: String, mimeType: String, p
}
}
fun FragmentActivity.showBiometricPrompt(successCallback: (BiometricPrompt.AuthenticationResult) -> Unit) {
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)
}
override fun onAuthenticationError(activity: FragmentActivity?, errorCode: Int, errString: CharSequence) {
val isCanceledByUser = errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON || errorCode == BiometricPrompt.ERROR_USER_CANCELED
if (!isCanceledByUser) {
toast(errString.toString())
}
}
override fun onAuthenticationFailed(activity: FragmentActivity?) {
toast(R.string.authentication_failed)
}
}
)
}
fun FragmentActivity.handleHiddenFolderPasswordProtection(callback: () -> Unit) {
if (baseConfig.isHiddenPasswordProtectionOn) {
SecurityDialog(this, baseConfig.hiddenPasswordHash, baseConfig.hiddenProtectionType) { _, _, success ->

View file

@ -2,14 +2,9 @@ package com.simplemobiletools.commons.views
import android.content.Context
import android.util.AttributeSet
import androidx.biometric.BiometricPrompt
import androidx.biometric.auth.AuthPromptCallback
import androidx.biometric.auth.AuthPromptHost
import androidx.biometric.auth.Class2BiometricAuthPrompt
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.FragmentActivity
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.showBiometricPrompt
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.PROTECTION_FINGERPRINT
import com.simplemobiletools.commons.interfaces.HashListener
@ -25,34 +20,12 @@ class BiometricIdTab(context: Context, attrs: AttributeSet) : ConstraintLayout(c
context.updateTextColors(biometric_lock_holder)
open_biometric_dialog.setOnClickListener {
showBiometricPrompt()
biometricPromptHost.activity?.showBiometricPrompt {
hashListener.receivedHash("", PROTECTION_FINGERPRINT)
}
}
}
private fun showBiometricPrompt() {
Class2BiometricAuthPrompt.Builder(context.getText(R.string.authenticate), context.getText(R.string.cancel))
.build()
.startAuthentication(
biometricPromptHost,
object : AuthPromptCallback() {
override fun onAuthenticationSucceeded(activity: FragmentActivity?, result: BiometricPrompt.AuthenticationResult) {
hashListener.receivedHash("", PROTECTION_FINGERPRINT)
}
override fun onAuthenticationError(activity: FragmentActivity?, errorCode: Int, errString: CharSequence) {
val isCanceledByUser = errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON || errorCode == BiometricPrompt.ERROR_USER_CANCELED
if (!isCanceledByUser) {
context.toast(errString.toString())
}
}
override fun onAuthenticationFailed(activity: FragmentActivity?) {
context.toast(R.string.authentication_failed)
}
}
)
}
override fun initTab(requiredHash: String, listener: HashListener, scrollView: MyScrollView, biometricPromptHost: AuthPromptHost) {
this.biometricPromptHost = biometricPromptHost
hashListener = listener