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         if (userId != UserHandle.USER_NULL) {
83             mFingerprintManager.setActiveUser(userId);
84         }
85         mFingerprintRemoving = fingerprint;
86         mFingerprintManager.remove(fingerprint, userId, mRemoveCallback);;
87     }
88 
FingerprintRemoveSidecar()89     public FingerprintRemoveSidecar() {
90         mFingerprintsRemoved = new LinkedList<>();
91     }
92 
setFingerprintManager(FingerprintManager fingerprintManager)93     public void setFingerprintManager(FingerprintManager fingerprintManager) {
94         mFingerprintManager = fingerprintManager;
95     }
96 
97     @Override
onCreate(@ullable Bundle savedInstanceState)98     public void onCreate(@Nullable Bundle savedInstanceState) {
99         super.onCreate(savedInstanceState);
100         setRetainInstance(true);
101     }
102 
setListener(Listener listener)103     public void setListener(Listener listener) {
104         if (mListener == null && listener != null) {
105             while (!mFingerprintsRemoved.isEmpty()) {
106                 Object o = mFingerprintsRemoved.poll();
107                 if (o instanceof Fingerprint) {
108                     listener.onRemovalSucceeded((Fingerprint)o);
109                 } else if (o instanceof RemovalError) {
110                     RemovalError e = (RemovalError) o;
111                     listener.onRemovalError(e.fingerprint, e.errMsgId, e.errString);
112                 }
113             }
114         }
115         mListener = listener;
116     }
117 
118     public interface Listener {
onRemovalSucceeded(Fingerprint fingerprint)119         void onRemovalSucceeded(Fingerprint fingerprint);
onRemovalError(Fingerprint fp, int errMsgId, CharSequence errString)120         void onRemovalError(Fingerprint fp, int errMsgId, CharSequence errString);
121     }
122 
isRemovingFingerprint(int fid)123     final boolean isRemovingFingerprint(int fid) {
124         return inProgress() && mFingerprintRemoving.getBiometricId() == fid;
125     }
126 
inProgress()127     final boolean inProgress() {
128         return mFingerprintRemoving != null;
129     }
130 
131     @Override
getMetricsCategory()132     public int getMetricsCategory() {
133         return SettingsEnums.FINGERPRINT_REMOVE_SIDECAR;
134     }
135 
136 }
137