1 /* 2 * Copyright (C) 2021 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.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.hardware.biometrics.common.OperationContext; 23 import android.os.IBinder; 24 25 import com.android.server.biometrics.log.BiometricContext; 26 import com.android.server.biometrics.log.BiometricLogger; 27 28 import java.util.function.Supplier; 29 30 /** 31 * Abstract {@link BaseClientMonitor} implementation that supports HAL operations. 32 * @param <T> HAL template 33 */ 34 public abstract class HalClientMonitor<T> extends BaseClientMonitor { 35 36 @NonNull 37 protected final Supplier<T> mLazyDaemon; 38 39 @NonNull 40 private final OperationContext mOperationContext = new OperationContext(); 41 42 /** 43 * @param context system_server context 44 * @param lazyDaemon pointer for lazy retrieval of the HAL 45 * @param token a unique token for the client 46 * @param listener recipient of related events (e.g. authentication) 47 * @param userId target user id for operation 48 * @param owner name of the client that owns this 49 * @param cookie BiometricPrompt authentication cookie (to be moved into a subclass soon) 50 * @param sensorId ID of the sensor that the operation should be requested of 51 * @param biometricLogger framework stats logger 52 * @param biometricContext system context metadata 53 */ HalClientMonitor(@onNull Context context, @NonNull Supplier<T> lazyDaemon, @Nullable IBinder token, @Nullable ClientMonitorCallbackConverter listener, int userId, @NonNull String owner, int cookie, int sensorId, @NonNull BiometricLogger biometricLogger, @NonNull BiometricContext biometricContext)54 public HalClientMonitor(@NonNull Context context, @NonNull Supplier<T> lazyDaemon, 55 @Nullable IBinder token, @Nullable ClientMonitorCallbackConverter listener, int userId, 56 @NonNull String owner, int cookie, int sensorId, 57 @NonNull BiometricLogger biometricLogger, @NonNull BiometricContext biometricContext) { 58 super(context, token, listener, userId, owner, cookie, sensorId, 59 biometricLogger, biometricContext); 60 mLazyDaemon = lazyDaemon; 61 } 62 63 @Nullable getFreshDaemon()64 public T getFreshDaemon() { 65 return mLazyDaemon.get(); 66 } 67 68 /** 69 * Starts the HAL operation specific to the ClientMonitor subclass. 70 */ startHalOperation()71 protected abstract void startHalOperation(); 72 73 /** 74 * Invoked if the scheduler is unable to start the ClientMonitor (for example the HAL is null). 75 * If such a problem is detected, the scheduler will not invoke 76 * {@link #start(ClientMonitorCallback)}. 77 */ unableToStart()78 public abstract void unableToStart(); 79 80 @Override destroy()81 public void destroy() { 82 super.destroy(); 83 84 // subclasses should do this earlier in most cases, but ensure it happens now 85 unsubscribeBiometricContext(); 86 } 87 getOperationContext()88 protected OperationContext getOperationContext() { 89 return getBiometricContext().updateContext(mOperationContext, isCryptoOperation()); 90 } 91 getBiometricContextUnsubscriber()92 protected ClientMonitorCallback getBiometricContextUnsubscriber() { 93 return new ClientMonitorCallback() { 94 @Override 95 public void onClientFinished(@NonNull BaseClientMonitor monitor, boolean success) { 96 unsubscribeBiometricContext(); 97 } 98 }; 99 } 100 101 protected void unsubscribeBiometricContext() { 102 getBiometricContext().unsubscribe(mOperationContext); 103 } 104 } 105