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.BiometricsProtoEnums;
23 import android.os.IBinder;
24 
25 /**
26  * Abstract {@link BaseClientMonitor} implementation that supports HAL operations.
27  * @param <T> HAL template
28  */
29 public abstract class HalClientMonitor<T> extends BaseClientMonitor {
30     /**
31      * Interface that allows ClientMonitor subclasses to retrieve a fresh instance to the HAL.
32      */
33     public interface LazyDaemon<T> {
34         /**
35          * @return A fresh instance to the biometric HAL
36          */
getDaemon()37         T getDaemon();
38     }
39 
40     /**
41      * Starts the HAL operation specific to the ClientMonitor subclass.
42      */
startHalOperation()43     protected abstract void startHalOperation();
44 
45     /**
46      * Invoked if the scheduler is unable to start the ClientMonitor (for example the HAL is null).
47      * If such a problem is detected, the scheduler will not invoke
48      * {@link #start(Callback)}.
49      */
unableToStart()50     public abstract void unableToStart();
51 
52     @NonNull
53     protected final LazyDaemon<T> mLazyDaemon;
54 
55     /**
56      * @param context    system_server context
57      * @param lazyDaemon pointer for lazy retrieval of the HAL
58      * @param token      a unique token for the client
59      * @param listener   recipient of related events (e.g. authentication)
60      * @param userId     target user id for operation
61      * @param owner      name of the client that owns this
62      * @param cookie     BiometricPrompt authentication cookie (to be moved into a subclass soon)
63      * @param sensorId   ID of the sensor that the operation should be requested of
64      * @param statsModality One of {@link BiometricsProtoEnums} MODALITY_* constants
65      * @param statsAction   One of {@link BiometricsProtoEnums} ACTION_* constants
66      * @param statsClient   One of {@link BiometricsProtoEnums} CLIENT_* constants
67      */
HalClientMonitor(@onNull Context context, @NonNull LazyDaemon<T> lazyDaemon, @Nullable IBinder token, @Nullable ClientMonitorCallbackConverter listener, int userId, @NonNull String owner, int cookie, int sensorId, int statsModality, int statsAction, int statsClient)68     public HalClientMonitor(@NonNull Context context, @NonNull LazyDaemon<T> lazyDaemon,
69             @Nullable IBinder token, @Nullable ClientMonitorCallbackConverter listener, int userId,
70             @NonNull String owner, int cookie, int sensorId, int statsModality, int statsAction,
71             int statsClient) {
72         super(context, token, listener, userId, owner, cookie, sensorId, statsModality,
73                 statsAction, statsClient);
74         mLazyDaemon = lazyDaemon;
75     }
76 
77     @Nullable
getFreshDaemon()78     public T getFreshDaemon() {
79         return mLazyDaemon.getDaemon();
80     }
81 }
82