1 /*
2  * Copyright (C) 2020 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.biometrics.sensors;
18 
19 import android.util.SparseArray;
20 
21 /**
22  * Tracks biometric performance across sensors and users.
23  */
24 public class PerformanceTracker {
25 
26     private static final String TAG = "PerformanceTracker";
27     // Keyed by SensorId
28     private static SparseArray<PerformanceTracker> sTrackers;
29 
getInstanceForSensorId(int sensorId)30     public static PerformanceTracker getInstanceForSensorId(int sensorId) {
31         if (sTrackers == null) {
32             sTrackers = new SparseArray<>();
33         }
34 
35         if (!sTrackers.contains(sensorId)) {
36             sTrackers.put(sensorId, new PerformanceTracker());
37         }
38         return sTrackers.get(sensorId);
39     }
40 
41     private static class Info {
42         int mAccept; // number of accepted biometrics
43         int mReject; // number of rejected biometrics
44         int mAcquire; // total number of acquisitions.
45 
46         int mAcceptCrypto;
47         int mRejectCrypto;
48         int mAcquireCrypto;
49 
50         int mTimedLockout; // total number of lockouts
51         int mPermanentLockout; // total number of permanent lockouts
52     }
53 
54     // Keyed by UserId
55     private final SparseArray<Info> mAllUsersInfo;
56     private int mHALDeathCount;
57 
PerformanceTracker()58     private PerformanceTracker() {
59         mAllUsersInfo = new SparseArray<>();
60     }
61 
createUserEntryIfNecessary(int userId)62     private void createUserEntryIfNecessary(int userId) {
63         if (!mAllUsersInfo.contains(userId)) {
64             mAllUsersInfo.put(userId, new Info());
65         }
66     }
67 
incrementAuthForUser(int userId, boolean accepted)68     public void incrementAuthForUser(int userId, boolean accepted) {
69         createUserEntryIfNecessary(userId);
70 
71         if (accepted) {
72             mAllUsersInfo.get(userId).mAccept++;
73         } else {
74             mAllUsersInfo.get(userId).mReject++;
75         }
76     }
77 
incrementCryptoAuthForUser(int userId, boolean accepted)78     void incrementCryptoAuthForUser(int userId, boolean accepted) {
79         createUserEntryIfNecessary(userId);
80 
81         if (accepted) {
82             mAllUsersInfo.get(userId).mAcceptCrypto++;
83         } else {
84             mAllUsersInfo.get(userId).mRejectCrypto++;
85         }
86     }
87 
incrementAcquireForUser(int userId, boolean isCrypto)88     void incrementAcquireForUser(int userId, boolean isCrypto) {
89         createUserEntryIfNecessary(userId);
90 
91         if (isCrypto) {
92             mAllUsersInfo.get(userId).mAcquireCrypto++;
93         } else {
94             mAllUsersInfo.get(userId).mAcquire++;
95         }
96     }
97 
incrementTimedLockoutForUser(int userId)98     void incrementTimedLockoutForUser(int userId) {
99         createUserEntryIfNecessary(userId);
100 
101         mAllUsersInfo.get(userId).mTimedLockout++;
102     }
103 
incrementPermanentLockoutForUser(int userId)104     void incrementPermanentLockoutForUser(int userId) {
105         createUserEntryIfNecessary(userId);
106 
107         mAllUsersInfo.get(userId).mPermanentLockout++;
108     }
109 
incrementHALDeathCount()110     public void incrementHALDeathCount() {
111         mHALDeathCount++;
112     }
113 
clear()114     public void clear() {
115         mAllUsersInfo.clear();
116         mHALDeathCount = 0;
117     }
118 
getAcceptForUser(int userId)119     public int getAcceptForUser(int userId) {
120         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mAccept : 0;
121     }
122 
getRejectForUser(int userId)123     public int getRejectForUser(int userId) {
124         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mReject : 0;
125     }
126 
getAcquireForUser(int userId)127     public int getAcquireForUser(int userId) {
128         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mAcquire : 0;
129     }
130 
getAcceptCryptoForUser(int userId)131     public int getAcceptCryptoForUser(int userId) {
132         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mAcceptCrypto : 0;
133     }
134 
getRejectCryptoForUser(int userId)135     public int getRejectCryptoForUser(int userId) {
136         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mRejectCrypto : 0;
137     }
138 
getAcquireCryptoForUser(int userId)139     public int getAcquireCryptoForUser(int userId) {
140         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mAcquireCrypto : 0;
141     }
142 
getTimedLockoutForUser(int userId)143     public int getTimedLockoutForUser(int userId) {
144         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mTimedLockout : 0;
145     }
146 
getPermanentLockoutForUser(int userId)147     public int getPermanentLockoutForUser(int userId) {
148         return mAllUsersInfo.contains(userId) ? mAllUsersInfo.get(userId).mPermanentLockout : 0;
149     }
150 
getHALDeathCount()151     public int getHALDeathCount() {
152         return mHALDeathCount;
153     }
154 }
155