mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-11 04:34:40 -08:00
suBiometric: remove biometric
use device credential to support more devices and second user
This commit is contained in:
@@ -1,53 +1,59 @@
|
||||
package com.topjohnwu.magisk.core.utils
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.KeyguardManager
|
||||
import android.content.Context
|
||||
import androidx.biometric.BiometricManager
|
||||
import androidx.biometric.BiometricManager.Authenticators
|
||||
import androidx.biometric.BiometricPrompt
|
||||
import androidx.core.content.ContextCompat
|
||||
import android.content.Intent
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.core.Config
|
||||
|
||||
class BiometricHelper(context: Context) {
|
||||
|
||||
private val mgr = BiometricManager.from(context)
|
||||
private val mgr = context.getSystemService(KeyguardManager::class.java)
|
||||
|
||||
val isSupported get() = when (mgr.canAuthenticate(Authenticators.BIOMETRIC_WEAK)) {
|
||||
BiometricManager.BIOMETRIC_SUCCESS -> true
|
||||
else -> false
|
||||
}
|
||||
val isSupported get() = mgr.isDeviceSecure
|
||||
|
||||
val isEnabled get() = isSupported && Config.suBiometric
|
||||
|
||||
fun authenticate(
|
||||
activity: FragmentActivity,
|
||||
onError: () -> Unit = {},
|
||||
onSuccess: () -> Unit): BiometricPrompt {
|
||||
val prompt = BiometricPrompt(activity,
|
||||
ContextCompat.getMainExecutor(activity),
|
||||
object : BiometricPrompt.AuthenticationCallback() {
|
||||
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
|
||||
onError()
|
||||
}
|
||||
|
||||
override fun onAuthenticationFailed() {
|
||||
onError()
|
||||
}
|
||||
|
||||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
||||
onSuccess()
|
||||
}
|
||||
}
|
||||
)
|
||||
val info = BiometricPrompt.PromptInfo.Builder()
|
||||
.setConfirmationRequired(true)
|
||||
.setAllowedAuthenticators(Authenticators.BIOMETRIC_WEAK)
|
||||
.setTitle(activity.getString(R.string.authenticate))
|
||||
.setNegativeButtonText(activity.getString(android.R.string.cancel))
|
||||
.build()
|
||||
prompt.authenticate(info)
|
||||
return prompt
|
||||
activity: FragmentActivity,
|
||||
onError: () -> Unit = {},
|
||||
onSuccess: () -> Unit) {
|
||||
val tag = BiometricFragment::class.java.name
|
||||
val intent = mgr.createConfirmDeviceCredentialIntent(null, null)
|
||||
val fragmentManager = activity.supportFragmentManager
|
||||
var fragment = fragmentManager.findFragmentByTag(tag) as BiometricFragment?
|
||||
if (fragment == null) {
|
||||
fragment = BiometricFragment()
|
||||
fragmentManager.beginTransaction()
|
||||
.add(0, fragment, tag)
|
||||
.commitNow()
|
||||
}
|
||||
fragment.start(intent, onError, onSuccess)
|
||||
}
|
||||
|
||||
class BiometricFragment : Fragment() {
|
||||
private val code = 1
|
||||
private var onError: () -> Unit = {}
|
||||
private var onSuccess: () -> Unit = {}
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (requestCode == code) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
onSuccess()
|
||||
} else {
|
||||
onError()
|
||||
}
|
||||
onError = {}
|
||||
onSuccess = {}
|
||||
}
|
||||
}
|
||||
|
||||
fun start(intent: Intent, onError: () -> Unit, onSuccess: () -> Unit) {
|
||||
this.onError = onError
|
||||
this.onSuccess = onSuccess
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
|
||||
startActivityForResult(intent, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user