From 9a1a54a6fcfa6fd6cf142f2af9804375255d14cf Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 2 Oct 2019 11:00:45 +0530 Subject: Initial biometric authentication support (#541) * [WIP] Initial biometric authentication support * Redirect decryption app shortcut to go through LaunchActivity * UserPreference: Clear existing shortcuts when toggling password auth Clears out any auth-bypassed entries that exist * Fix hilarious copypasta derp Signed-off-by: Harsh Shandilya --- .../main/java/com/zeapo/pwdstore/UserPreference.kt | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'app/src/main/java/com/zeapo/pwdstore/UserPreference.kt') diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index 3384356f..48edf141 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -4,6 +4,7 @@ import android.accessibilityservice.AccessibilityServiceInfo import android.app.Activity import android.content.Context import android.content.Intent +import android.content.pm.ShortcutManager import android.net.Uri import android.os.Build import android.os.Bundle @@ -15,16 +16,21 @@ import android.view.MenuItem import android.view.accessibility.AccessibilityManager import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.biometric.BiometricManager +import androidx.core.content.getSystemService import androidx.documentfile.provider.DocumentFile import androidx.preference.CheckBoxPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager +import androidx.preference.SwitchPreference import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity import com.zeapo.pwdstore.crypto.PgpActivity import com.zeapo.pwdstore.git.GitActivity import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.auth.AuthenticationResult +import com.zeapo.pwdstore.utils.auth.Authenticator import org.apache.commons.io.FileUtils import org.openintents.openpgp.util.OpenPgpUtils import java.io.File @@ -249,6 +255,41 @@ class UserPreference : AppCompatActivity() { false } } + + findPreference("biometric_auth")?.apply { + val isFingerprintSupported = BiometricManager.from(requireContext()).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS + if (!isFingerprintSupported) { + isEnabled = false + isChecked = false + summary = getString(R.string.biometric_auth_summary_error) + } else { + setOnPreferenceClickListener { + val editor = sharedPreferences.edit() + val checked = isChecked + Authenticator(requireActivity()) { result -> + when (result) { + is AuthenticationResult.Success -> { + // Apply the changes + editor.putBoolean("biometric_auth", checked) + } + else -> { + // If any error occurs, revert back to the previous state. This + // catch-all clause includes the cancellation case. + editor.putBoolean("biometric_auth", !checked) + isChecked = !checked + } + } + }.authenticate() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { + requireContext().getSystemService()?.apply { + removeDynamicShortcuts(dynamicShortcuts.map { it.id }.toMutableList()) + } + } + editor.apply() + true + } + } + } } override fun onResume() { -- cgit v1.2.3