suBiometric: remove biometric

use device credential to support more devices and second user
This commit is contained in:
vvb2060 2023-07-29 00:50:24 +08:00 committed by John Wu
parent 91c92051f1
commit 678c07fff5
3 changed files with 46 additions and 41 deletions

View File

@ -108,7 +108,6 @@ dependencies {
implementation("androidx.navigation:navigation-fragment-ktx:${vNav}") implementation("androidx.navigation:navigation-fragment-ktx:${vNav}")
implementation("androidx.navigation:navigation-ui-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.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0") implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
implementation("androidx.appcompat:appcompat:1.6.1") implementation("androidx.appcompat:appcompat:1.6.1")

View File

@ -1,53 +1,59 @@
package com.topjohnwu.magisk.core.utils package com.topjohnwu.magisk.core.utils
import android.app.Activity
import android.app.KeyguardManager
import android.content.Context import android.content.Context
import androidx.biometric.BiometricManager import android.content.Intent
import androidx.biometric.BiometricManager.Authenticators import androidx.fragment.app.Fragment
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Config
class BiometricHelper(context: Context) { 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)) { val isSupported get() = mgr.isDeviceSecure
BiometricManager.BIOMETRIC_SUCCESS -> true
else -> false
}
val isEnabled get() = isSupported && Config.suBiometric val isEnabled get() = isSupported && Config.suBiometric
fun authenticate( fun authenticate(
activity: FragmentActivity, activity: FragmentActivity,
onError: () -> Unit = {}, onError: () -> Unit = {},
onSuccess: () -> Unit): BiometricPrompt { onSuccess: () -> Unit) {
val prompt = BiometricPrompt(activity, val tag = BiometricFragment::class.java.name
ContextCompat.getMainExecutor(activity), val intent = mgr.createConfirmDeviceCredentialIntent(null, null)
object : BiometricPrompt.AuthenticationCallback() { val fragmentManager = activity.supportFragmentManager
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { var fragment = fragmentManager.findFragmentByTag(tag) as BiometricFragment?
onError() if (fragment == null) {
} fragment = BiometricFragment()
fragmentManager.beginTransaction()
override fun onAuthenticationFailed() { .add(0, fragment, tag)
onError() .commitNow()
} }
fragment.start(intent, onError, onSuccess)
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
} }
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)
}
}
} }

View File

@ -3,9 +3,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.USE_BIOMETRIC" /> <uses-permission
<uses-permission android:name="android.permission.USE_FINGERPRINT" /> 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 tools:ignore="MissingApplicationIcon">
</application> </application>