From 678c07fff5dee306b3cdc4f7f7b1e86f701ac5f2 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sat, 29 Jul 2023 00:50:24 +0800 Subject: [PATCH] suBiometric: remove biometric use device credential to support more devices and second user --- app/build.gradle.kts | 1 - .../magisk/core/utils/BiometricHelper.kt | 80 ++++++++++--------- stub/src/main/AndroidManifest.xml | 6 +- 3 files changed, 46 insertions(+), 41 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 2fa3bb8dc..c8292f0f3 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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") diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/BiometricHelper.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/BiometricHelper.kt index 6cdfa6cf5..9c73840f2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/utils/BiometricHelper.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/BiometricHelper.kt @@ -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) + } + } } diff --git a/stub/src/main/AndroidManifest.xml b/stub/src/main/AndroidManifest.xml index 1b51fa507..8dae7245f 100644 --- a/stub/src/main/AndroidManifest.xml +++ b/stub/src/main/AndroidManifest.xml @@ -3,9 +3,9 @@ - - - +