Example #1
0
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
        {
            if (requestCode == FINGERPRINT_PERMISSION_REQUEST_CODE && grantResults[0] == Android.Content.PM.Permission.Granted)
            {
                SetContentView(Resource.Layout.activity_main);
                var purchaseButton = FindViewById <Button> (Resource.Id.purchase_button);

                if (!mKeyguardManager.IsKeyguardSecure)
                {
                    purchaseButton.Enabled = false;
                    // Show a message that the user hasn't set up a fingerprint or lock screen.
                    Toast.MakeText(this, "Secure lock screen hasn't set up.\n"
                                   + "Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint", ToastLength.Long).Show();
                    return;
                }

                mFingerprintManager = (FingerprintManager)GetSystemService(Context.FingerprintService);
                if (!mFingerprintManager.HasEnrolledFingerprints)
                {
                    purchaseButton.Enabled = false;
                    // This happens when no fingerprints are registered.
                    Toast.MakeText(this, "Go to 'Settings -> Security -> Fingerprint' " +
                                   "and register at least one fingerprint", ToastLength.Long).Show();
                    return;
                }

                CreateKey();
                purchaseButton.Enabled = true;
                purchaseButton.Click  += (sender, e) => {
                    // Show the fingerprint dialog. The user has the option to use the fingerprint with
                    // crypto, or you can fall back to using a server-side verified password.
                    FindViewById(Resource.Id.confirmation_message).Visibility = ViewStates.Gone;
                    FindViewById(Resource.Id.encrypted_message).Visibility    = ViewStates.Gone;

                    mFragment          = new FingerprintAuthenticationDialogFragment();
                    mSharedPreferences = this.GetPreferences(FileCreationMode.Private);

                    if (InitCipher())
                    {
                        mFragment.SetCryptoObject(new FingerprintManager.CryptoObject(mCipher));
                        var useFingerprintPreference = mSharedPreferences.GetBoolean(GetString(Resource.String.use_fingerprint_to_authenticate_key), true);
                        if (useFingerprintPreference)
                        {
                            mFragment.SetStage(FingerprintAuthenticationDialogFragment.Stage.Fingerprint);
                        }
                        else
                        {
                            mFragment.SetStage(FingerprintAuthenticationDialogFragment.Stage.Password);
                        }
                        mFragment.Show(FragmentManager, DIALOG_FRAGMENT_TAG);
                    }
                    else
                    {
                        mFragment.SetCryptoObject(new FingerprintManager.CryptoObject(mCipher));
                        mFragment.SetStage(FingerprintAuthenticationDialogFragment.Stage.NewFingerprintEnrolled);
                        mFragment.Show(FragmentManager, DIALOG_FRAGMENT_TAG);
                    }
                };
            }
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
        {
            if (requestCode == FINGERPRINT_PERMISSION_REQUEST_CODE && grantResults[0] == Android.Content.PM.Permission.Granted) {
                SetContentView (Resource.Layout.activity_main);
                var purchaseButton = FindViewById<Button> (Resource.Id.purchase_button);

                if (!mKeyguardManager.IsKeyguardSecure) {
                    purchaseButton.Enabled = false;
                    // Show a message that the user hasn't set up a fingerprint or lock screen.
                    Toast.MakeText (this, "Secure lock screen hasn't set up.\n"
                        + "Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint", ToastLength.Long).Show ();
                    return;
                }

                mFingerprintManager = (FingerprintManager)GetSystemService (Context.FingerprintService);
                if (!mFingerprintManager.HasEnrolledFingerprints) {
                    purchaseButton.Enabled = false;
                    // This happens when no fingerprints are registered.
                    Toast.MakeText (this, "Go to 'Settings -> Security -> Fingerprint' " +
                        "and register at least one fingerprint", ToastLength.Long).Show ();
                    return;
                }

                CreateKey ();
                purchaseButton.Enabled = true;
                purchaseButton.Click += (sender, e) => {
                    // Show the fingerprint dialog. The user has the option to use the fingerprint with
                    // crypto, or you can fall back to using a server-side verified password.
                    FindViewById (Resource.Id.confirmation_message).Visibility = ViewStates.Gone;
                    FindViewById (Resource.Id.encrypted_message).Visibility = ViewStates.Gone;

                    mFragment = new FingerprintAuthenticationDialogFragment ();
                    mSharedPreferences = this.GetPreferences (FileCreationMode.Private);

                    if (InitCipher ()) {
                        mFragment.SetCryptoObject (new FingerprintManager.CryptoObject (mCipher));
                        var useFingerprintPreference = mSharedPreferences.GetBoolean (GetString (Resource.String.use_fingerprint_to_authenticate_key), true);
                        if (useFingerprintPreference) {
                            mFragment.SetStage (FingerprintAuthenticationDialogFragment.Stage.Fingerprint);
                        } else {
                            mFragment.SetStage (FingerprintAuthenticationDialogFragment.Stage.Password);
                        }
                        mFragment.Show (FragmentManager, DIALOG_FRAGMENT_TAG);
                    } else {
                        mFragment.SetCryptoObject (new FingerprintManager.CryptoObject (mCipher));
                        mFragment.SetStage (FingerprintAuthenticationDialogFragment.Stage.NewFingerprintEnrolled);
                        mFragment.Show (FragmentManager, DIALOG_FRAGMENT_TAG);
                    }
                };
            }
        }