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);
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            _activityDesign.ApplyTheme();
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.fingerprint_setup);

            Enum.TryParse(
                PreferenceManager.GetDefaultSharedPreferences(this).GetString(App.Kp2a.CurrentDb.CurrentFingerprintModePrefKey, ""),
                out _unlockMode);

            _fpIcon     = FindViewById <ImageView>(Resource.Id.fingerprint_icon);
            _fpTextView = FindViewById <TextView>(Resource.Id.fingerprint_status);


            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetHomeButtonEnabled(true);

            int[] radioButtonIds =
            {
                Resource.Id.radio_fingerprint_quickunlock, Resource.Id.radio_fingerprint_unlock,
                Resource.Id.radio_fingerprint_disabled
            };
            _radioButtons        = radioButtonIds.Select(FindViewById <RadioButton>).ToArray();
            _radioButtons[0].Tag = FingerprintUnlockMode.QuickUnlock.ToString();
            _radioButtons[1].Tag = FingerprintUnlockMode.FullUnlock.ToString();
            _radioButtons[2].Tag = FingerprintUnlockMode.Disabled.ToString();
            foreach (RadioButton r in _radioButtons)
            {
                r.CheckedChange += (sender, args) =>
                {
                    var rbSender = ((RadioButton)sender);
                    if (!rbSender.Checked)
                    {
                        return;
                    }
                    foreach (RadioButton rOther in _radioButtons)
                    {
                        if (rOther == sender)
                        {
                            continue;
                        }
                        rOther.Checked = false;
                    }
                    FingerprintUnlockMode newMode;
                    Enum.TryParse(rbSender.Tag.ToString(), out newMode);
                    ChangeUnlockMode(_unlockMode, newMode);
                };
            }

            CheckCurrentRadioButton();

            int errorId = Resource.String.fingerprint_os_error;

            SetError(errorId);

            FindViewById(Resource.Id.cancel_button).Click += (sender, args) =>
            {
                _enc.StopListening();
                _unlockMode = FingerprintUnlockMode.Disabled;                 //cancelling a FingerprintEncryption means a new key has been created but not been authenticated to encrypt something. We can't keep the previous state.
                StoreUnlockMode();
                FindViewById(Resource.Id.radio_buttons).Visibility = ViewStates.Visible;
                FindViewById(Resource.Id.fingerprint_auth_container).Visibility = ViewStates.Gone;
                _enc = null;
                CheckCurrentRadioButton();
            };

            FindViewById(Resource.Id.radio_buttons).Visibility = ViewStates.Gone;
            FindViewById(Resource.Id.fingerprint_auth_container).Visibility = ViewStates.Gone;

            FindViewById <CheckBox>(Resource.Id.close_database_after_failed).Checked =
                Util.GetCloseDatabaseAfterFailedBiometricQuickUnlock(this);

            FindViewById <CheckBox>(Resource.Id.close_database_after_failed).CheckedChange += (sender, args) =>
            {
                PreferenceManager.GetDefaultSharedPreferences(this)
                .Edit()
                .PutBoolean(GetString(Resource.String.CloseDatabaseAfterFailedBiometricQuickUnlock_key), args.IsChecked)
                .Commit();
            };


            UpdateCloseDatabaseAfterFailedBiometricQuickUnlockVisibility();
        }