private void ShowBiometricPrompt() { var executor = ContextCompat.GetMainExecutor(Context); var passwordStorage = new PasswordStorageManager(Context); var callback = new AuthenticationCallback(); callback.Succeeded += (_, result) => { string password; try { password = passwordStorage.Fetch(result.CryptoObject.Cipher); } catch (Exception e) { Logger.Error(e); Toast.MakeText(Context, Resource.String.genericError, ToastLength.Short); return; } UnlockAttempted?.Invoke(this, password); }; callback.Failed += delegate { FocusPasswordText(); }; callback.Errored += (_, result) => { Toast.MakeText(Context, result.Message, ToastLength.Short).Show(); FocusPasswordText(); }; _prompt = new BiometricPrompt(this, executor, callback); var promptInfo = new BiometricPrompt.PromptInfo.Builder() .SetTitle(GetString(Resource.String.unlock)) .SetSubtitle(GetString(Resource.String.unlockBiometricsMessage)) .SetNegativeButtonText(GetString(Resource.String.cancel)) .SetConfirmationRequired(false) .SetAllowedAuthenticators(BiometricManager.Authenticators.BiometricStrong) .Build(); try { var cipher = passwordStorage.GetDecryptionCipher(); _prompt.Authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher)); } catch (Exception e) { Logger.Error(e); _canUseBiometrics = false; _useBiometricsButton.Enabled = false; FocusPasswordText(); } }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { var view = base.OnCreateView(inflater, container, savedInstanceState); SetupToolbar(view, Resource.String.unlock); _preferences = new PreferenceWrapper(Context); _progressBar = view.FindViewById <ProgressBar>(Resource.Id.appBarProgressBar); _passwordLayout = view.FindViewById <TextInputLayout>(Resource.Id.editPasswordLayout); _passwordText = view.FindViewById <TextInputEditText>(Resource.Id.editPassword); TextInputUtil.EnableAutoErrorClear(_passwordLayout); _passwordText.EditorAction += (_, e) => { if (e.ActionId == ImeAction.Done) { UnlockAttempted?.Invoke(this, _passwordText.Text); } }; _unlockButton = view.FindViewById <MaterialButton>(Resource.Id.buttonUnlock); _unlockButton.Click += delegate { UnlockAttempted?.Invoke(this, _passwordText.Text); }; if (_preferences.AllowBiometrics) { var biometricManager = BiometricManager.From(Context); _canUseBiometrics = biometricManager.CanAuthenticate(BiometricManager.Authenticators.BiometricStrong) == BiometricManager.BiometricSuccess; } _useBiometricsButton = view.FindViewById <MaterialButton>(Resource.Id.buttonUseBiometrics); _useBiometricsButton.Enabled = _canUseBiometrics; _useBiometricsButton.Click += delegate { ShowBiometricPrompt(); }; if (_canUseBiometrics) { ShowBiometricPrompt(); } else { FocusPasswordText(); } return(view); }