| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.biometrics; |
| 18 |
|
| 19 |
import static android.Manifest.permission.TEST_BIOMETRIC; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.RequiresPermission; |
| 24 |
import android.annotation.TestApi; |
| 25 |
import android.content.Context; |
| 26 |
import android.os.RemoteException; |
| 27 |
import android.util.ArraySet; |
| 28 |
import android.util.Log; |
| 29 |
|
| 30 |
import java.util.concurrent.CountDownLatch; |
| 31 |
import java.util.concurrent.TimeUnit; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@TestApi |
| 39 |
public class BiometricTestSession implements AutoCloseable { |
| 40 |
private static final String BASE_TAG = "BiometricTestSession"; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public interface TestSessionProvider { |
| 46 |
@NonNull |
| 47 |
ITestSession createTestSession(@NonNull Context context, int sensorId, |
| 48 |
@NonNull ITestSessionCallback callback) throws RemoteException; |
| 49 |
} |
| 50 |
|
| 51 |
private final Context mContext; |
| 52 |
private final int mSensorId; |
| 53 |
private final ITestSession mTestSession; |
| 54 |
|
| 55 |
|
| 56 |
@NonNull private final ArraySet<Integer> mTestedUsers; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
@Nullable private CountDownLatch mCloseLatch; |
| 63 |
@NonNull private final ArraySet<Integer> mUsersCleaningUp; |
| 64 |
|
| 65 |
private final ITestSessionCallback mCallback = new ITestSessionCallback.Stub() { |
| 66 |
@Override |
| 67 |
public void onCleanupStarted(int userId) { |
| 68 |
Log.d(getTag(), "onCleanupStarted, sensor: " + mSensorId + ", userId: " + userId); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onCleanupFinished(int userId) { |
| 73 |
Log.d(getTag(), "onCleanupFinished, sensor: " + mSensorId |
| 74 |
+ ", userId: " + userId |
| 75 |
+ ", remaining users: " + mUsersCleaningUp.size()); |
| 76 |
mUsersCleaningUp.remove(userId); |
| 77 |
|
| 78 |
if (mUsersCleaningUp.isEmpty() && mCloseLatch != null) { |
| 79 |
mCloseLatch.countDown(); |
| 80 |
} |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public BiometricTestSession(@NonNull Context context, int sensorId, |
| 88 |
@NonNull TestSessionProvider testSessionProvider) throws RemoteException { |
| 89 |
mContext = context; |
| 90 |
mSensorId = sensorId; |
| 91 |
mTestSession = testSessionProvider.createTestSession(context, sensorId, mCallback); |
| 92 |
mTestedUsers = new ArraySet<>(); |
| 93 |
mUsersCleaningUp = new ArraySet<>(); |
| 94 |
setTestHalEnabled(true); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@RequiresPermission(TEST_BIOMETRIC) |
| 107 |
private void setTestHalEnabled(boolean enabled) { |
| 108 |
try { |
| 109 |
Log.w(getTag(), "setTestHalEnabled, sensor: " + mSensorId + " enabled: " + enabled); |
| 110 |
mTestSession.setTestHalEnabled(enabled); |
| 111 |
} catch (RemoteException e) { |
| 112 |
throw e.rethrowFromSystemServer(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@RequiresPermission(TEST_BIOMETRIC) |
| 122 |
public void startEnroll(int userId) { |
| 123 |
try { |
| 124 |
mTestedUsers.add(userId); |
| 125 |
mTestSession.startEnroll(userId); |
| 126 |
} catch (RemoteException e) { |
| 127 |
throw e.rethrowFromSystemServer(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
@RequiresPermission(TEST_BIOMETRIC) |
| 137 |
public void finishEnroll(int userId) { |
| 138 |
try { |
| 139 |
mTestedUsers.add(userId); |
| 140 |
mTestSession.finishEnroll(userId); |
| 141 |
} catch (RemoteException e) { |
| 142 |
throw e.rethrowFromSystemServer(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
@RequiresPermission(TEST_BIOMETRIC) |
| 152 |
public void acceptAuthentication(int userId) { |
| 153 |
try { |
| 154 |
mTestSession.acceptAuthentication(userId); |
| 155 |
} catch (RemoteException e) { |
| 156 |
throw e.rethrowFromSystemServer(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
@RequiresPermission(TEST_BIOMETRIC) |
| 166 |
public void rejectAuthentication(int userId) { |
| 167 |
try { |
| 168 |
mTestSession.rejectAuthentication(userId); |
| 169 |
} catch (RemoteException e) { |
| 170 |
throw e.rethrowFromSystemServer(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
@RequiresPermission(TEST_BIOMETRIC) |
| 183 |
public void notifyAcquired(int userId, int acquireInfo) { |
| 184 |
try { |
| 185 |
mTestSession.notifyAcquired(userId, acquireInfo); |
| 186 |
} catch (RemoteException e) { |
| 187 |
throw e.rethrowFromSystemServer(); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
@RequiresPermission(TEST_BIOMETRIC) |
| 200 |
public void notifyError(int userId, int errorCode) { |
| 201 |
try { |
| 202 |
mTestSession.notifyError(userId, errorCode); |
| 203 |
} catch (RemoteException e) { |
| 204 |
throw e.rethrowFromSystemServer(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
@RequiresPermission(TEST_BIOMETRIC) |
| 216 |
public void cleanupInternalState(int userId) { |
| 217 |
try { |
| 218 |
if (mUsersCleaningUp.contains(userId)) { |
| 219 |
Log.w(getTag(), "Cleanup already in progress for user: " + userId); |
| 220 |
} |
| 221 |
|
| 222 |
mUsersCleaningUp.add(userId); |
| 223 |
mTestSession.cleanupInternalState(userId); |
| 224 |
} catch (RemoteException e) { |
| 225 |
throw e.rethrowFromSystemServer(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
@RequiresPermission(TEST_BIOMETRIC) |
| 231 |
public void close() { |
| 232 |
Log.d(getTag(), "Close, mTestedUsers size; " + mTestedUsers.size()); |
| 233 |
|
| 234 |
|
| 235 |
if (!mTestedUsers.isEmpty()) { |
| 236 |
mCloseLatch = new CountDownLatch(1); |
| 237 |
for (int user : mTestedUsers) { |
| 238 |
cleanupInternalState(user); |
| 239 |
} |
| 240 |
|
| 241 |
try { |
| 242 |
Log.d(getTag(), "Awaiting latch..."); |
| 243 |
mCloseLatch.await(3, TimeUnit.SECONDS); |
| 244 |
Log.d(getTag(), "Finished awaiting"); |
| 245 |
} catch (InterruptedException e) { |
| 246 |
Log.e(getTag(), "Latch interrupted", e); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
if (!mUsersCleaningUp.isEmpty()) { |
| 251 |
|
| 252 |
Log.e(getTag(), "Cleanup not finished before shutdown - pending: " |
| 253 |
+ mUsersCleaningUp.size()); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
setTestHalEnabled(false); |
| 258 |
} |
| 259 |
|
| 260 |
private String getTag() { |
| 261 |
return BASE_TAG + "_" + mSensorId; |
| 262 |
} |
| 263 |
} |