1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package com.android.settings.fingerprint; 18 19import android.hardware.fingerprint.FingerprintManager; 20import android.os.CancellationSignal; 21import android.view.View; 22import android.widget.ImageView; 23import android.widget.TextView; 24 25import com.android.settings.R; 26 27/** 28 * Small helper class to manage text/icon around fingerprint authentication UI. 29 */ 30public class FingerprintUiHelper extends FingerprintManager.AuthenticationCallback { 31 32 private static final long ERROR_TIMEOUT = 1300; 33 34 private ImageView mIcon; 35 private TextView mErrorTextView; 36 private CancellationSignal mCancellationSignal; 37 private int mUserId; 38 39 private Callback mCallback; 40 private FingerprintManager mFingerprintManager; 41 42 public FingerprintUiHelper(ImageView icon, TextView errorTextView, Callback callback, 43 int userId) { 44 mFingerprintManager = icon.getContext().getSystemService(FingerprintManager.class); 45 mIcon = icon; 46 mErrorTextView = errorTextView; 47 mCallback = callback; 48 mUserId = userId; 49 } 50 51 public void startListening() { 52 if (mFingerprintManager != null && mFingerprintManager.isHardwareDetected() 53 && mFingerprintManager.getEnrolledFingerprints(mUserId).size() > 0) { 54 mCancellationSignal = new CancellationSignal(); 55 mFingerprintManager.setActiveUser(mUserId); 56 mFingerprintManager.authenticate( 57 null, mCancellationSignal, 0 /* flags */, this, null, mUserId); 58 setFingerprintIconVisibility(true); 59 mIcon.setImageResource(R.drawable.ic_fingerprint); 60 } 61 } 62 63 public void stopListening() { 64 if (mCancellationSignal != null) { 65 mCancellationSignal.cancel(); 66 mCancellationSignal = null; 67 } 68 } 69 70 public boolean isListening() { 71 return mCancellationSignal != null && !mCancellationSignal.isCanceled(); 72 } 73 74 private void setFingerprintIconVisibility(boolean visible) { 75 mIcon.setVisibility(visible ? View.VISIBLE : View.GONE); 76 mCallback.onFingerprintIconVisibilityChanged(visible); 77 } 78 79 @Override 80 public void onAuthenticationError(int errMsgId, CharSequence errString) { 81 showError(errString); 82 setFingerprintIconVisibility(false); 83 } 84 85 @Override 86 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { 87 showError(helpString); 88 } 89 90 @Override 91 public void onAuthenticationFailed() { 92 showError(mIcon.getResources().getString( 93 R.string.fingerprint_not_recognized)); 94 } 95 96 @Override 97 public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { 98 mIcon.setImageResource(R.drawable.ic_fingerprint_success); 99 mCallback.onAuthenticated(); 100 } 101 102 private void showError(CharSequence error) { 103 if (!isListening()) { 104 return; 105 } 106 107 mIcon.setImageResource(R.drawable.ic_fingerprint_error); 108 mErrorTextView.setText(error); 109 mErrorTextView.removeCallbacks(mResetErrorTextRunnable); 110 mErrorTextView.postDelayed(mResetErrorTextRunnable, ERROR_TIMEOUT); 111 } 112 113 private Runnable mResetErrorTextRunnable = new Runnable() { 114 @Override 115 public void run() { 116 mErrorTextView.setText(""); 117 mIcon.setImageResource(R.drawable.ic_fingerprint); 118 } 119 }; 120 121 public interface Callback { 122 void onAuthenticated(); 123 void onFingerprintIconVisibilityChanged(boolean visible); 124 } 125} 126