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