Home | History | Annotate | Download | only in fingerprint
      1 /**
      2  * Copyright (C) 2016 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.server.fingerprint;
     18 
     19 import android.content.Context;
     20 import android.hardware.fingerprint.FingerprintManager;
     21 import android.hardware.fingerprint.IFingerprintDaemon;
     22 import android.hardware.fingerprint.IFingerprintServiceReceiver;
     23 import android.os.IBinder;
     24 import android.os.RemoteException;
     25 import android.util.Slog;
     26 
     27 import com.android.internal.logging.MetricsLogger;
     28 import com.android.internal.logging.MetricsProto.MetricsEvent;
     29 
     30 import java.util.Arrays;
     31 
     32 /**
     33  * A class to keep track of the enrollment state for a given client.
     34  */
     35 public abstract class EnrollClient extends ClientMonitor {
     36     private static final long MS_PER_SEC = 1000;
     37     private static final int ENROLLMENT_TIMEOUT_MS = 60 * 1000; // 1 minute
     38     private byte[] mCryptoToken;
     39 
     40     public EnrollClient(Context context, long halDeviceId, IBinder token,
     41             IFingerprintServiceReceiver receiver, int userId, int groupId, byte [] cryptoToken,
     42             boolean restricted, String owner) {
     43         super(context, halDeviceId, token, receiver, userId, groupId, restricted, owner);
     44         mCryptoToken = Arrays.copyOf(cryptoToken, cryptoToken.length);
     45     }
     46 
     47     @Override
     48     public boolean onEnrollResult(int fingerId, int groupId, int remaining) {
     49         if (groupId != getGroupId()) {
     50             Slog.w(TAG, "groupId != getGroupId(), groupId: " + groupId +
     51                     " getGroupId():" + getGroupId());
     52         }
     53         if (remaining == 0) {
     54             FingerprintUtils.getInstance().addFingerprintForUser(getContext(), fingerId,
     55                     getTargetUserId());
     56         }
     57         return sendEnrollResult(fingerId, groupId, remaining);
     58     }
     59 
     60     /*
     61      * @return true if we're done.
     62      */
     63     private boolean sendEnrollResult(int fpId, int groupId, int remaining) {
     64         IFingerprintServiceReceiver receiver = getReceiver();
     65         if (receiver == null)
     66             return true; // client not listening
     67 
     68         FingerprintUtils.vibrateFingerprintSuccess(getContext());
     69         MetricsLogger.action(getContext(), MetricsEvent.ACTION_FINGERPRINT_ENROLL);
     70         try {
     71             receiver.onEnrollResult(getHalDeviceId(), fpId, groupId, remaining);
     72             return remaining == 0;
     73         } catch (RemoteException e) {
     74             Slog.w(TAG, "Failed to notify EnrollResult:", e);
     75             return true;
     76         }
     77     }
     78 
     79     @Override
     80     public int start() {
     81         IFingerprintDaemon daemon = getFingerprintDaemon();
     82         if (daemon == null) {
     83             Slog.w(TAG, "enroll: no fingeprintd!");
     84             return ERROR_ESRCH;
     85         }
     86         final int timeout = (int) (ENROLLMENT_TIMEOUT_MS / MS_PER_SEC);
     87         try {
     88             final int result = daemon.enroll(mCryptoToken, getGroupId(), timeout);
     89             if (result != 0) {
     90                 Slog.w(TAG, "startEnroll failed, result=" + result);
     91                 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE);
     92                 return result;
     93             }
     94         } catch (RemoteException e) {
     95             Slog.e(TAG, "startEnroll failed", e);
     96         }
     97         return 0; // success
     98     }
     99 
    100     @Override
    101     public int stop(boolean initiatedByClient) {
    102         IFingerprintDaemon daemon = getFingerprintDaemon();
    103         if (daemon == null) {
    104             Slog.w(TAG, "stopEnrollment: no fingeprintd!");
    105             return ERROR_ESRCH;
    106         }
    107         try {
    108             final int result = daemon.cancelEnrollment();
    109             if (result != 0) {
    110                 Slog.w(TAG, "startEnrollCancel failed, result = " + result);
    111                 return result;
    112             }
    113         } catch (RemoteException e) {
    114             Slog.e(TAG, "stopEnrollment failed", e);
    115         }
    116         if (initiatedByClient) {
    117             onError(FingerprintManager.FINGERPRINT_ERROR_CANCELED);
    118         }
    119         return 0;
    120     }
    121 
    122     @Override
    123     public boolean onRemoved(int fingerId, int groupId) {
    124         if (DEBUG) Slog.w(TAG, "onRemoved() called for enroll!");
    125         return true; // Invalid for EnrollClient
    126     }
    127 
    128     @Override
    129     public boolean onEnumerationResult(int fingerId, int groupId) {
    130         if (DEBUG) Slog.w(TAG, "onEnumerationResult() called for enroll!");
    131         return true; // Invalid for EnrollClient
    132     }
    133 
    134     @Override
    135     public boolean onAuthenticated(int fingerId, int groupId) {
    136         if (DEBUG) Slog.w(TAG, "onAuthenticated() called for enroll!");
    137         return true; // Invalid for EnrollClient
    138     }
    139 
    140 }
    141