1 /*
2  * Copyright (C) 2024 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;
18 
19 import static android.os.Process.THREAD_PRIORITY_DEFAULT;
20 import static android.os.Process.THREAD_PRIORITY_DISPLAY;
21 
22 import android.os.Handler;
23 import android.os.HandlerThread;
24 
25 /**
26  * This class provides the handler to process biometric operations.
27  */
28 public class BiometricHandlerProvider {
29     private static final BiometricHandlerProvider sBiometricHandlerProvider =
30             new BiometricHandlerProvider();
31 
32     private Handler mBiometricsCallbackHandler;
33     private Handler mFingerprintHandler;
34     private Handler mFaceHandler;
35 
36     /**
37      * @return an instance of {@link BiometricHandlerProvider} which contains the three
38      *         threads needed for running biometric operations
39      */
getInstance()40     public static BiometricHandlerProvider getInstance() {
41         return sBiometricHandlerProvider;
42     }
43 
BiometricHandlerProvider()44     private BiometricHandlerProvider() {}
45 
46     /**
47     * @return the handler to process all biometric callback operations
48     */
getBiometricCallbackHandler()49     public synchronized Handler getBiometricCallbackHandler() {
50         if (mBiometricsCallbackHandler == null) {
51             mBiometricsCallbackHandler = getNewHandler("BiometricsCallbackHandler",
52                     THREAD_PRIORITY_DISPLAY);
53         }
54         return mBiometricsCallbackHandler;
55     }
56 
57     /**
58      * @return the handler to process all face related biometric operations
59      */
getFaceHandler()60     public synchronized Handler getFaceHandler() {
61         if (mFaceHandler == null) {
62             mFaceHandler = getNewHandler("FaceHandler", THREAD_PRIORITY_DEFAULT);
63         }
64         return mFaceHandler;
65     }
66 
67     /**
68      * @return the handler to process all fingerprint related biometric operations
69      */
getFingerprintHandler()70     public synchronized Handler getFingerprintHandler() {
71         if (mFingerprintHandler == null) {
72             mFingerprintHandler = getNewHandler("FingerprintHandler", THREAD_PRIORITY_DEFAULT);
73         }
74         return mFingerprintHandler;
75     }
76 
getNewHandler(String tag, int priority)77     private Handler getNewHandler(String tag, int priority) {
78         HandlerThread handlerThread = new HandlerThread(tag, priority);
79         handlerThread.start();
80         return new Handler(handlerThread.getLooper());
81     }
82 }
83