Use same checks as BiometricAuthenticator in UserPreference (#1088)

This commit is contained in:
Aditya Wasan 2020-09-08 14:24:19 +05:30 committed by GitHub
parent c65f3c7099
commit ff780b02de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 8 deletions

View file

@ -12,7 +12,7 @@ insert_final_newline = true
[*.{java,kt,kts,xml}] [*.{java,kt,kts,xml}]
indent_size = 4 indent_size = 4
ij_continuation_indent_size = 2 ij_continuation_indent_size = 4
[*.{kt,kts}] [*.{kt,kts}]
kotlin_imports_layout=ascii kotlin_imports_layout=ascii

View file

@ -24,7 +24,6 @@ import androidx.activity.result.contract.ActivityResultContracts.OpenDocument
import androidx.activity.result.contract.ActivityResultContracts.OpenDocumentTree import androidx.activity.result.contract.ActivityResultContracts.OpenDocumentTree
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView import androidx.appcompat.widget.AppCompatTextView
import androidx.biometric.BiometricManager
import androidx.core.content.edit import androidx.core.content.edit
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
@ -281,9 +280,9 @@ class UserPreference : AppCompatActivity() {
findPreference<CheckBoxPreference>(PreferenceKeys.ENABLE_DEBUG_LOGGING)?.isVisible = !BuildConfig.ENABLE_DEBUG_FEATURES findPreference<CheckBoxPreference>(PreferenceKeys.ENABLE_DEBUG_LOGGING)?.isVisible = !BuildConfig.ENABLE_DEBUG_FEATURES
findPreference<CheckBoxPreference>(PreferenceKeys.BIOMETRIC_AUTH)?.apply { findPreference<CheckBoxPreference>(PreferenceKeys.BIOMETRIC_AUTH)?.apply {
val isFingerprintSupported = BiometricManager.from(requireContext()) val canAuthenticate = BiometricAuthenticator.canAuthenticate(prefsActivity)
.canAuthenticate(BiometricManager.Authenticators.DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS
if (!isFingerprintSupported) { if (!canAuthenticate) {
isEnabled = false isEnabled = false
isChecked = false isChecked = false
summary = getString(R.string.biometric_auth_summary_error) summary = getString(R.string.biometric_auth_summary_error)

View file

@ -20,6 +20,7 @@ import com.zeapo.pwdstore.R
object BiometricAuthenticator { object BiometricAuthenticator {
private const val TAG = "BiometricAuthenticator" private const val TAG = "BiometricAuthenticator"
private const val validAuthenticators = Authenticators.DEVICE_CREDENTIAL or Authenticators.BIOMETRIC_WEAK
sealed class Result { sealed class Result {
data class Success(val cryptoObject: BiometricPrompt.CryptoObject?) : Result() data class Success(val cryptoObject: BiometricPrompt.CryptoObject?) : Result()
@ -28,6 +29,10 @@ object BiometricAuthenticator {
object Cancelled : Result() object Cancelled : Result()
} }
fun canAuthenticate(activity: FragmentActivity): Boolean {
return BiometricManager.from(activity).canAuthenticate(validAuthenticators) == BiometricManager.BIOMETRIC_SUCCESS
}
fun authenticate( fun authenticate(
activity: FragmentActivity, activity: FragmentActivity,
@StringRes dialogTitleRes: Int = R.string.biometric_prompt_title, @StringRes dialogTitleRes: Int = R.string.biometric_prompt_title,
@ -60,10 +65,8 @@ object BiometricAuthenticator {
callback(Result.Success(result.cryptoObject)) callback(Result.Success(result.cryptoObject))
} }
} }
val validAuthenticators = Authenticators.DEVICE_CREDENTIAL or Authenticators.BIOMETRIC_WEAK
val canAuth = BiometricManager.from(activity).canAuthenticate(validAuthenticators) == BiometricManager.BIOMETRIC_SUCCESS
val deviceHasKeyguard = activity.getSystemService<KeyguardManager>()?.isDeviceSecure == true val deviceHasKeyguard = activity.getSystemService<KeyguardManager>()?.isDeviceSecure == true
if (canAuth || deviceHasKeyguard) { if (canAuthenticate(activity) || deviceHasKeyguard) {
val promptInfo = BiometricPrompt.PromptInfo.Builder() val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(dialogTitleRes)) .setTitle(activity.getString(dialogTitleRes))
.setAllowedAuthenticators(validAuthenticators) .setAllowedAuthenticators(validAuthenticators)