Exemple #1
0
        protected override void OnResume()
        {
            base.OnResume();
            _design.ReapplyTheme();

            CheckIfUnloaded();


            bool showKeyboard = ((!InitFingerprintUnlock()) || (Util.GetShowKeyboardDuringFingerprintUnlock(this)));

            EditText pwd = (EditText)FindViewById(Resource.Id.QuickUnlock_password);

            pwd.PostDelayed(() =>
            {
                InputMethodManager keyboard = (InputMethodManager)GetSystemService(Context.InputMethodService);
                if (showKeyboard)
                {
                    keyboard.ShowSoftInput(pwd, 0);
                }
                else
                {
                    keyboard.HideSoftInputFromWindow(pwd.WindowToken, HideSoftInputFlags.ImplicitOnly);
                }
            }, 50);


            var btn = FindViewById <ImageButton>(Resource.Id.fingerprintbtn);

            btn.Click += (sender, args) =>
            {
                if (_biometryIdentifier.HasUserInterface || string.IsNullOrEmpty((string)btn.Tag))
                {
                    _biometryIdentifier.StartListening(this);
                }
                else
                {
                    AlertDialog.Builder b = new AlertDialog.Builder(this);
                    b.SetTitle(Resource.String.fingerprint_prefs);
                    b.SetMessage(btn.Tag.ToString());
                    b.SetPositiveButton(Android.Resource.String.Ok, (o, eventArgs) => ((Dialog)o).Dismiss());
                    if (_biometryIdentifier != null)
                    {
                        b.SetNegativeButton(Resource.String.disable_sensor, (senderAlert, alertArgs) =>
                        {
                            btn.SetImageResource(Resource.Drawable.ic_fingerprint_error);
                            _biometryIdentifier?.StopListening();
                            _biometryIdentifier = null;
                        });
                    }
                    else
                    {
                        b.SetNegativeButton(Resource.String.enable_sensor, (senderAlert, alertArgs) =>
                        {
                            InitFingerprintUnlock();
                        });
                    }
                    b.Show();
                }
            };
        }
        private void HandleFingerprintKeyInvalidated()
        {
            var btn = FindViewById <ImageButton>(Resource.Id.fingerprintbtn);

//key invalidated permanently
            btn.SetImageResource(Resource.Drawable.ic_fingerprint_error);
            btn.Tag             = GetString(Resource.String.fingerprint_unlock_failed) + " " + GetString(Resource.String.fingerprint_reenable2);
            _biometryIdentifier = null;
        }
        private bool InitFingerprintUnlock()
        {
            Kp2aLog.Log("InitFingerprintUnlock");

            if (_biometryIdentifier != null)
            {
                Kp2aLog.Log("Already listening for fingerprint!");
                return(true);
            }


            var btn = FindViewById <ImageButton>(Resource.Id.fingerprintbtn);

            try
            {
                FingerprintUnlockMode um;
                Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(this).GetString(App.Kp2a.GetDbForQuickUnlock().CurrentFingerprintModePrefKey, ""), out um);
                btn.Visibility = (um != FingerprintUnlockMode.Disabled) ? ViewStates.Visible : ViewStates.Gone;

                if (um == FingerprintUnlockMode.Disabled)
                {
                    _biometryIdentifier = null;
                    return(false);
                }



                if (um == FingerprintUnlockMode.QuickUnlock && Util.GetCloseDatabaseAfterFailedBiometricQuickUnlock(this))
                {
                    maxNumFailedAttempts = 3;
                }

                BiometricModule fpModule = new BiometricModule(this);
                Kp2aLog.Log("fpModule.IsHardwareAvailable=" + fpModule.IsHardwareAvailable);
                if (fpModule.IsHardwareAvailable)                 //see FingerprintSetupActivity
                {
                    _biometryIdentifier = new BiometricDecryption(fpModule, App.Kp2a.GetDbForQuickUnlock().CurrentFingerprintPrefKey, this,
                                                                  App.Kp2a.GetDbForQuickUnlock().CurrentFingerprintPrefKey);
                }

                if ((_biometryIdentifier == null) && (!BiometricDecryption.IsSetUp(this, App.Kp2a.GetDbForQuickUnlock().CurrentFingerprintPrefKey)))
                {
                    try
                    {
                        Kp2aLog.Log("trying Samsung Fingerprint API...");
                        _biometryIdentifier = new BiometrySamsungIdentifier(this);
                        btn.Click          += (sender, args) =>
                        {
                            if (_biometryIdentifier.Init())
                            {
                                if (numFailedAttempts < maxNumFailedAttempts)
                                {
                                    _biometryIdentifier.StartListening(this);
                                }
                            }
                        };
                        Kp2aLog.Log("trying Samsung Fingerprint API...Seems to work!");
                    }
                    catch (Exception)
                    {
                        Kp2aLog.Log("trying Samsung Fingerprint API...failed.");
                        _biometryIdentifier = null;
                    }
                }
                if (_biometryIdentifier == null)
                {
                    FindViewById <ImageButton>(Resource.Id.fingerprintbtn).Visibility = ViewStates.Gone;
                    return(false);
                }


                if (_biometryIdentifier.Init())
                {
                    Kp2aLog.Log("successfully initialized fingerprint.");
                    btn.SetImageResource(Resource.Drawable.ic_fp_40px);
                    _biometryIdentifier.StartListening(this);
                    return(true);
                }
                else
                {
                    Kp2aLog.Log("failed to initialize fingerprint.");
                    HandleFingerprintKeyInvalidated();
                }
            }
            catch (Exception e)
            {
                Kp2aLog.Log("Error initializing Fingerprint Unlock: " + e);
                btn.SetImageResource(Resource.Drawable.ic_fingerprint_error);
                btn.Tag = "Error initializing Fingerprint Unlock: " + e;

                _biometryIdentifier = null;
            }
            return(false);
        }