1 /* 2 * Copyright (C) 2017 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 17 package com.android.settings.biometrics.fingerprint; 18 19 import android.annotation.Nullable; 20 import android.app.settings.SettingsEnums; 21 import android.hardware.fingerprint.Fingerprint; 22 import android.hardware.fingerprint.FingerprintManager; 23 import android.os.Bundle; 24 import android.os.UserHandle; 25 import android.util.Log; 26 27 import com.android.settings.core.InstrumentedFragment; 28 29 import java.util.LinkedList; 30 import java.util.Queue; 31 32 /** 33 * Sidecar fragment to handle the state around fingerprint removal. 34 */ 35 public class FingerprintRemoveSidecar extends InstrumentedFragment { 36 37 private static final String TAG = "FingerprintRemoveSidecar"; 38 private Listener mListener; 39 private Fingerprint mFingerprintRemoving; 40 private Queue<Object> mFingerprintsRemoved; 41 FingerprintManager mFingerprintManager; 42 43 private class RemovalError { 44 Fingerprint fingerprint; 45 int errMsgId; 46 CharSequence errString; RemovalError(Fingerprint fingerprint, int errMsgId, CharSequence errString)47 public RemovalError(Fingerprint fingerprint, int errMsgId, CharSequence errString) { 48 this.fingerprint = fingerprint; 49 this.errMsgId = errMsgId; 50 this.errString = errString; 51 } 52 } 53 54 private FingerprintManager.RemovalCallback 55 mRemoveCallback = new FingerprintManager.RemovalCallback() { 56 @Override 57 public void onRemovalSucceeded(Fingerprint fingerprint, int remaining) { 58 if (mListener != null) { 59 mListener.onRemovalSucceeded(fingerprint); 60 } else { 61 mFingerprintsRemoved.add(fingerprint); 62 }; 63 mFingerprintRemoving = null; 64 } 65 66 @Override 67 public void onRemovalError(Fingerprint fp, int errMsgId, CharSequence errString) { 68 if (mListener != null) { 69 mListener.onRemovalError(fp, errMsgId, errString); 70 } else { 71 mFingerprintsRemoved.add(new RemovalError(fp, errMsgId, errString)); 72 } 73 mFingerprintRemoving = null; 74 } 75 }; 76 startRemove(Fingerprint fingerprint, int userId)77 public void startRemove(Fingerprint fingerprint, int userId) { 78 if (mFingerprintRemoving != null) { 79 Log.e(TAG, "Remove already in progress"); 80 return; 81 } 82 mFingerprintRemoving = fingerprint; 83 mFingerprintManager.remove(fingerprint, userId, mRemoveCallback);; 84 } 85 FingerprintRemoveSidecar()86 public FingerprintRemoveSidecar() { 87 mFingerprintsRemoved = new LinkedList<>(); 88 } 89 setFingerprintManager(FingerprintManager fingerprintManager)90 public void setFingerprintManager(FingerprintManager fingerprintManager) { 91 mFingerprintManager = fingerprintManager; 92 } 93 94 @Override onCreate(@ullable Bundle savedInstanceState)95 public void onCreate(@Nullable Bundle savedInstanceState) { 96 super.onCreate(savedInstanceState); 97 setRetainInstance(true); 98 } 99 setListener(Listener listener)100 public void setListener(Listener listener) { 101 if (mListener == null && listener != null) { 102 while (!mFingerprintsRemoved.isEmpty()) { 103 Object o = mFingerprintsRemoved.poll(); 104 if (o instanceof Fingerprint) { 105 listener.onRemovalSucceeded((Fingerprint)o); 106 } else if (o instanceof RemovalError) { 107 RemovalError e = (RemovalError) o; 108 listener.onRemovalError(e.fingerprint, e.errMsgId, e.errString); 109 } 110 } 111 } 112 mListener = listener; 113 } 114 115 public interface Listener { onRemovalSucceeded(Fingerprint fingerprint)116 void onRemovalSucceeded(Fingerprint fingerprint); onRemovalError(Fingerprint fp, int errMsgId, CharSequence errString)117 void onRemovalError(Fingerprint fp, int errMsgId, CharSequence errString); 118 } 119 isRemovingFingerprint(int fid)120 final boolean isRemovingFingerprint(int fid) { 121 return inProgress() && mFingerprintRemoving.getBiometricId() == fid; 122 } 123 inProgress()124 final boolean inProgress() { 125 return mFingerprintRemoving != null; 126 } 127 128 @Override getMetricsCategory()129 public int getMetricsCategory() { 130 return SettingsEnums.FINGERPRINT_REMOVE_SIDECAR; 131 } 132 133 } 134