Java tutorial
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.rnd.snapsplit.view; import android.support.v4.app.DialogFragment; import android.content.Context; import android.content.SharedPreferences; import android.hardware.fingerprint.FingerprintManager; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import com.rnd.snapsplit.DialogClickListener; import com.rnd.snapsplit.FingerprintUiHelper; import com.rnd.snapsplit.PaymentRequest; import com.rnd.snapsplit.R; /** * A dialog which uses fingerprint APIs to authenticate the user, and falls back to password * authentication if fingerprint is not available. */ public class DialogFragmentFingerprintAuthentication extends DialogFragment implements TextView.OnEditorActionListener, FingerprintUiHelper.Callback { private DialogClickListener callback; private Button mCancelButton; private Button mSecondDialogButton; private View mFingerprintContent; private View mBackupContent; private EditText mPassword; private CheckBox mUseFingerprintFutureCheckBox; private TextView mPasswordDescriptionTextView; private TextView mNewFingerprintEnrolledTextView; private PaymentRequest pr; private Stage mStage = Stage.FINGERPRINT; private FingerprintManager.CryptoObject mCryptoObject; private FingerprintUiHelper mFingerprintUiHelper; private Context mContext; private InputMethodManager mInputMethodManager; private SharedPreferences mSharedPreferences; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); callback = (DialogClickListener) getTargetFragment(); // Do not create a new Fragment when the Activity is re-created such as orientation changes. setRetainInstance(true); setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().setTitle(getString(R.string.sign_in)); View v = inflater.inflate(R.layout.fingerprint_dialog_container, container, false); Bundle bundle = this.getArguments(); pr = (PaymentRequest) bundle.getSerializable("pr"); final TextView amount = (TextView) v.findViewById(R.id.amount_value); String amountValue = "HKD" + String.format("%.2f", pr.getShareAmount()); amount.setText(amountValue); final TextView receipient = (TextView) v.findViewById(R.id.receipient_value); receipient.setText((String) pr.getRequestorName() + " - " + pr.getRequestorPhoneNumber()); mCancelButton = (Button) v.findViewById(R.id.cancel_button); mCancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dismiss(); } }); mSecondDialogButton = (Button) v.findViewById(R.id.second_dialog_button); mSecondDialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mStage == Stage.FINGERPRINT) { goToBackup(); } else { verifyPassword(); } } }); mFingerprintContent = v.findViewById(R.id.fingerprint_container); mBackupContent = v.findViewById(R.id.backup_container); mPassword = (EditText) v.findViewById(R.id.password); mPassword.setOnEditorActionListener(this); mPasswordDescriptionTextView = (TextView) v.findViewById(R.id.password_description); //mFingerprintContent mUseFingerprintFutureCheckBox = (CheckBox) v.findViewById(R.id.use_fingerprint_in_future_check); mNewFingerprintEnrolledTextView = (TextView) v.findViewById(R.id.new_fingerprint_enrolled_description); mFingerprintUiHelper = new FingerprintUiHelper(mContext.getSystemService(FingerprintManager.class), (ImageView) v.findViewById(R.id.fingerprint_icon), (TextView) v.findViewById(R.id.fingerprint_status), this); updateStage(); // If fingerprint authentication is not available, switch immediately to the backup // (password) screen. if (!mFingerprintUiHelper.isFingerprintAuthAvailable()) { goToBackup(); } return v; } @Override public void onResume() { super.onResume(); if (mStage == Stage.FINGERPRINT) { mFingerprintUiHelper.startListening(mCryptoObject); } } public void setStage(Stage stage) { mStage = stage; } @Override public void onPause() { super.onPause(); mFingerprintUiHelper.stopListening(); } @Override public void onAttach(Context context) { super.onAttach(context); mContext = context; mInputMethodManager = context.getSystemService(InputMethodManager.class); mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); } /** * Sets the crypto object to be passed in when authenticating with fingerprint. */ public void setCryptoObject(FingerprintManager.CryptoObject cryptoObject) { mCryptoObject = cryptoObject; } /** * Switches to backup (password) screen. This either can happen when fingerprint is not * available or the user chooses to use the password authentication method by pressing the * button. This can also happen when the user had too many fingerprint attempts. */ private void goToBackup() { mStage = Stage.PASSWORD; updateStage(); mPassword.requestFocus(); // Show the keyboard. mPassword.postDelayed(mShowKeyboardRunnable, 500); // Fingerprint is not used anymore. Stop listening for it. mFingerprintUiHelper.stopListening(); } /** * Checks whether the current entered password is correct, and dismisses the the dialog and * let's the activity know about the result. */ private void verifyPassword() { if (!checkPassword(mPassword.getText().toString())) { return; } if (mStage == Stage.NEW_FINGERPRINT_ENROLLED) { SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putBoolean(getString(R.string.use_fingerprint_to_authenticate_key), mUseFingerprintFutureCheckBox.isChecked()); editor.apply(); if (mUseFingerprintFutureCheckBox.isChecked()) { // Re-create the key so that fingerprints including new ones are validated. //mActivity.createKey(mActivity.DEFAULT_KEY_NAME, true); mStage = Stage.FINGERPRINT; } } mPassword.setText(""); callback.onPurchased(false /* without Fingerprint */, null, pr); dismiss(); } /** * @return true if {@code password} is correct, false otherwise */ private boolean checkPassword(String password) { // Assume the password is always correct. // In the real world situation, the password needs to be verified in the server side. return password.length() > 0; } private final Runnable mShowKeyboardRunnable = new Runnable() { @Override public void run() { mInputMethodManager.showSoftInput(mPassword, 0); } }; private void updateStage() { switch (mStage) { case FINGERPRINT: mCancelButton.setText(R.string.cancel); mSecondDialogButton.setText(R.string.use_password); mFingerprintContent.setVisibility(View.VISIBLE); mBackupContent.setVisibility(View.GONE); break; case NEW_FINGERPRINT_ENROLLED: // Intentional fall through case PASSWORD: mCancelButton.setText(R.string.cancel); mSecondDialogButton.setText(R.string.ok); mFingerprintContent.setVisibility(View.GONE); mBackupContent.setVisibility(View.VISIBLE); if (mStage == Stage.NEW_FINGERPRINT_ENROLLED) { mPasswordDescriptionTextView.setVisibility(View.GONE); mNewFingerprintEnrolledTextView.setVisibility(View.VISIBLE); mUseFingerprintFutureCheckBox.setVisibility(View.VISIBLE); } break; } } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_GO) { verifyPassword(); return true; } return false; } @Override public void onAuthenticated() { // Callback from FingerprintUiHelper. Let the activity know that authentication was // successful. callback.onPurchased(true /* withFingerprint */, mCryptoObject, pr); dismiss(); } @Override public void onError() { goToBackup(); } /** * Enumeration to indicate which authentication method the user is trying to authenticate with. */ public enum Stage { FINGERPRINT, NEW_FINGERPRINT_ENROLLED, PASSWORD } }