mirror of
https://github.com/topjohnwu/Magisk
synced 2024-11-14 22:28:37 +01:00
suBiometric: remove biometric
use device credential to support more devices and second user
This commit is contained in:
parent
91c92051f1
commit
678c07fff5
@ -108,7 +108,6 @@ dependencies {
|
||||
implementation("androidx.navigation:navigation-fragment-ktx:${vNav}")
|
||||
implementation("androidx.navigation:navigation-ui-ktx:${vNav}")
|
||||
|
||||
implementation("androidx.biometric:biometric:1.1.0")
|
||||
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
|
||||
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
|
||||
implementation("androidx.appcompat:appcompat:1.6.1")
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,9 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
|
||||
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
|
||||
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
|
||||
<uses-permission
|
||||
android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
|
||||
android:maxSdkVersion="25" />
|
||||
|
||||
<application tools:ignore="MissingApplicationIcon">
|
||||
</application>
|
||||
|
Loading…
Reference in New Issue
Block a user