From 68600b337e7d96350c994c87fb052c1a70640990 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 3 Apr 2021 11:35:33 -0400 Subject: [PATCH] Allow weaker unlock methods (closes #4265) --- .../ui/security/BiometricUnlockActivity.kt | 12 ++++++--- .../ui/security/SecureActivityDelegate.kt | 4 +-- .../ui/setting/SettingsSecurityController.kt | 4 +-- .../kanade/tachiyomi/widget/BiometricUtil.kt | 26 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 5 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/BiometricUtil.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt index 92a7d778a0..c3e0c51fc2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt @@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.biometric.BiometricPrompt import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.widget.BiometricUtil import uy.kohesive.injekt.injectLazy import java.util.Date import java.util.concurrent.Executors @@ -38,12 +39,15 @@ class BiometricUnlockActivity : AppCompatActivity() { } ) - val promptInfo = BiometricPrompt.PromptInfo.Builder() + var promptInfo = BiometricPrompt.PromptInfo.Builder() .setTitle(getString(R.string.unlock_app)) - .setDeviceCredentialAllowed(true) + .setAllowedAuthenticators(BiometricUtil.getSupportedAuthenticators(this)) .setConfirmationRequired(false) - .build() - biometricPrompt.authenticate(promptInfo) + if (!BiometricUtil.isDeviceCredentialAllowed(this)) { + promptInfo = promptInfo.setNegativeButtonText(getString(R.string.action_cancel)) + } + + biometricPrompt.authenticate(promptInfo.build()) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt index aebbc61ea3..f45d702ae6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt @@ -2,10 +2,10 @@ package eu.kanade.tachiyomi.ui.security import android.content.Intent import android.view.WindowManager -import androidx.biometric.BiometricManager import androidx.fragment.app.FragmentActivity import androidx.lifecycle.lifecycleScope import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.widget.BiometricUtil import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import uy.kohesive.injekt.injectLazy @@ -29,7 +29,7 @@ class SecureActivityDelegate(private val activity: FragmentActivity) { fun onResume() { val lockApp = preferences.useBiometricLock().get() - if (lockApp && BiometricManager.from(activity).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { + if (lockApp && BiometricUtil.isSupported(activity)) { if (isAppLocked()) { val intent = Intent(activity, BiometricUnlockActivity::class.java) activity.startActivity(intent) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt index 87798287e4..27ad71866f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.ui.setting -import androidx.biometric.BiometricManager import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.asImmediateFlow @@ -9,6 +8,7 @@ import eu.kanade.tachiyomi.util.preference.intListPreference import eu.kanade.tachiyomi.util.preference.summaryRes import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.titleRes +import eu.kanade.tachiyomi.widget.BiometricUtil import kotlinx.coroutines.flow.launchIn import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys @@ -17,7 +17,7 @@ class SettingsSecurityController : SettingsController() { override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply { titleRes = R.string.pref_category_security - if (BiometricManager.from(context).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { + if (BiometricUtil.isSupported(context)) { switchPreference { key = Keys.useBiometricLock titleRes = R.string.lock_with_biometrics diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/BiometricUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/BiometricUtil.kt new file mode 100644 index 0000000000..ebc091200a --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/BiometricUtil.kt @@ -0,0 +1,26 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import androidx.biometric.BiometricManager +import androidx.biometric.BiometricManager.Authenticators + +object BiometricUtil { + + fun getSupportedAuthenticators(context: Context): Int { + return listOf( + Authenticators.BIOMETRIC_STRONG, + Authenticators.BIOMETRIC_WEAK, + Authenticators.DEVICE_CREDENTIAL, + ) + .filter { BiometricManager.from(context).canAuthenticate(it) == BiometricManager.BIOMETRIC_SUCCESS } + .fold(0) { acc, auth -> acc or auth } + } + + fun isSupported(context: Context): Boolean { + return getSupportedAuthenticators(context) != 0 + } + + fun isDeviceCredentialAllowed(context: Context): Boolean { + return getSupportedAuthenticators(context) and Authenticators.DEVICE_CREDENTIAL != 0 + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a6a73aa0cf..2cb9e5d2a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -167,7 +167,7 @@ Manage notifications Security - Lock with biometrics + Require unlock Lock when idle Always Never