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