From 126e1e2d9d66a7a88672c2836ae6d53105142603 Mon Sep 17 00:00:00 2001 From: arkon Date: Tue, 13 Apr 2021 15:02:57 -0400 Subject: [PATCH] Allow weaker unlock methods in Android 6 - 10 (fixes #4833) --- .../ui/security/BiometricUnlockActivity.kt | 2 ++ .../tachiyomi/util/system/BiometricUtil.kt | 21 +++++++++++++++++-- .../util/system/ContextExtensions.kt | 13 ++++-------- 3 files changed, 25 insertions(+), 11 deletions(-) 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 e346c3d67..bb7ed1338 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 @@ -6,6 +6,7 @@ import androidx.biometric.BiometricPrompt import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.system.BiometricUtil +import timber.log.Timber import uy.kohesive.injekt.injectLazy import java.util.Date import java.util.concurrent.Executors @@ -27,6 +28,7 @@ class BiometricUnlockActivity : AppCompatActivity() { object : BiometricPrompt.AuthenticationCallback() { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { super.onAuthenticationError(errorCode, errString) + Timber.e(errString.toString()) finishAffinity() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/BiometricUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/BiometricUtil.kt index 1f42f3d7d..c7e397bef 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/BiometricUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/BiometricUtil.kt @@ -1,12 +1,17 @@ package eu.kanade.tachiyomi.util.system import android.content.Context +import android.os.Build import androidx.biometric.BiometricManager import androidx.biometric.BiometricManager.Authenticators object BiometricUtil { fun getSupportedAuthenticators(context: Context): Int { + if (isLegacySecured(context)) { + return Authenticators.BIOMETRIC_WEAK or Authenticators.DEVICE_CREDENTIAL + } + return listOf( Authenticators.BIOMETRIC_STRONG, Authenticators.BIOMETRIC_WEAK, @@ -17,10 +22,22 @@ object BiometricUtil { } fun isSupported(context: Context): Boolean { - return getSupportedAuthenticators(context) != 0 + return isLegacySecured(context) || getSupportedAuthenticators(context) != 0 } fun isDeviceCredentialAllowed(context: Context): Boolean { - return getSupportedAuthenticators(context) and Authenticators.DEVICE_CREDENTIAL != 0 + return isLegacySecured(context) || (getSupportedAuthenticators(context) and Authenticators.DEVICE_CREDENTIAL != 0) + } + + /** + * Returns whether the device is secured with a PIN, pattern or password. + */ + private fun isLegacySecured(context: Context): Boolean { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { + if (context.keyguardManager.isDeviceSecure) { + return true + } + } + return false } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt index ed83778f8..936ec57ec 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.util.system import android.app.ActivityManager +import android.app.KeyguardManager import android.app.Notification import android.app.NotificationManager import android.content.BroadcastReceiver @@ -153,24 +154,18 @@ val Float.dpToPxEnd: Float val Resources.isLTR get() = configuration.layoutDirection == View.LAYOUT_DIRECTION_LTR -/** - * Property to get the notification manager from the context. - */ val Context.notificationManager: NotificationManager get() = getSystemService()!! -/** - * Property to get the connectivity manager from the context. - */ val Context.connectivityManager: ConnectivityManager get() = getSystemService()!! -/** - * Property to get the power manager from the context. - */ val Context.powerManager: PowerManager get() = getSystemService()!! +val Context.keyguardManager: KeyguardManager + get() = getSystemService()!! + /** * Convenience method to acquire a partial wake lock. */