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.systemui.biometrics.dagger
18 
19 import android.content.Context
20 import android.content.res.Resources
21 import com.android.internal.R
22 import com.android.launcher3.icons.IconProvider
23 import com.android.systemui.CoreStartable
24 import com.android.systemui.biometrics.AuthController
25 import com.android.systemui.biometrics.EllipseOverlapDetectorParams
26 import com.android.systemui.biometrics.UdfpsUtils
27 import com.android.systemui.biometrics.data.repository.BiometricStatusRepository
28 import com.android.systemui.biometrics.data.repository.BiometricStatusRepositoryImpl
29 import com.android.systemui.biometrics.data.repository.DisplayStateRepository
30 import com.android.systemui.biometrics.data.repository.DisplayStateRepositoryImpl
31 import com.android.systemui.biometrics.data.repository.FacePropertyRepository
32 import com.android.systemui.biometrics.data.repository.FacePropertyRepositoryImpl
33 import com.android.systemui.biometrics.data.repository.FaceSettingsRepository
34 import com.android.systemui.biometrics.data.repository.FaceSettingsRepositoryImpl
35 import com.android.systemui.biometrics.data.repository.FingerprintPropertyRepository
36 import com.android.systemui.biometrics.data.repository.FingerprintPropertyRepositoryImpl
37 import com.android.systemui.biometrics.data.repository.PromptRepository
38 import com.android.systemui.biometrics.data.repository.PromptRepositoryImpl
39 import com.android.systemui.biometrics.udfps.BoundingBoxOverlapDetector
40 import com.android.systemui.biometrics.udfps.EllipseOverlapDetector
41 import com.android.systemui.biometrics.udfps.OverlapDetector
42 import com.android.systemui.biometrics.ui.binder.DeviceEntryUnlockTrackerViewBinder
43 import com.android.systemui.biometrics.ui.binder.SideFpsOverlayViewBinder
44 import com.android.systemui.dagger.SysUISingleton
45 import com.android.systemui.keyguard.ui.binder.AlternateBouncerViewBinder
46 import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener
47 import com.android.systemui.util.concurrency.ThreadFactory
48 import dagger.Binds
49 import dagger.BindsOptionalOf
50 import dagger.Module
51 import dagger.Provides
52 import dagger.multibindings.ClassKey
53 import dagger.multibindings.IntoMap
54 import dagger.multibindings.IntoSet
55 import java.util.concurrent.Executor
56 import javax.inject.Qualifier
57 
58 /** Dagger module for all things biometric. */
59 @Module
60 interface BiometricsModule {
61     /** Starts AuthController.  */
62     @Binds
63     @IntoMap
64     @ClassKey(AuthController::class)
bindAuthControllerStartablenull65     fun bindAuthControllerStartable(service: AuthController): CoreStartable
66 
67     /** Listen to config changes for AuthController. */
68     @Binds
69     @IntoSet
70     fun bindAuthControllerConfigChanges(service: AuthController): ConfigurationListener
71 
72     @Binds
73     @IntoMap
74     @ClassKey(SideFpsOverlayViewBinder::class)
75     fun bindsSideFpsOverlayViewBinder(viewBinder: SideFpsOverlayViewBinder): CoreStartable
76 
77     @Binds
78     @IntoMap
79     @ClassKey(AlternateBouncerViewBinder::class)
80     fun bindAlternateBouncerViewBinder(viewBinder: AlternateBouncerViewBinder): CoreStartable
81 
82     @Binds
83     @SysUISingleton
84     fun faceSettings(impl: FaceSettingsRepositoryImpl): FaceSettingsRepository
85 
86     @Binds @SysUISingleton fun faceSensors(impl: FacePropertyRepositoryImpl): FacePropertyRepository
87 
88     @Binds
89     @SysUISingleton
90     fun biometricPromptRepository(impl: PromptRepositoryImpl): PromptRepository
91 
92     @Binds
93     @SysUISingleton
94     fun biometricStatusRepository(impl: BiometricStatusRepositoryImpl): BiometricStatusRepository
95 
96     @Binds
97     @SysUISingleton
98     fun fingerprintRepository(
99         impl: FingerprintPropertyRepositoryImpl
100     ): FingerprintPropertyRepository
101 
102     @Binds
103     @SysUISingleton
104     fun displayStateRepository(impl: DisplayStateRepositoryImpl): DisplayStateRepository
105 
106     @BindsOptionalOf
107     fun deviceEntryUnlockTrackerViewBinder(): DeviceEntryUnlockTrackerViewBinder
108 
109     companion object {
110         /** Background [Executor] for HAL related operations. */
111         @Provides
112         @SysUISingleton
113         @JvmStatic
114         @BiometricsBackground
115         fun providesPluginExecutor(threadFactory: ThreadFactory): Executor =
116             threadFactory.buildExecutorOnNewThread("biometrics")
117 
118         @Provides fun providesUdfpsUtils(): UdfpsUtils = UdfpsUtils()
119 
120         @Provides
121         fun provideIconProvider(context: Context): IconProvider = IconProvider(context)
122 
123         @Provides
124         @SysUISingleton
125         fun providesOverlapDetector(): OverlapDetector {
126             val selectedOption =
127                 Resources.getSystem().getInteger(R.integer.config_selected_udfps_touch_detection)
128             val values =
129                 Resources.getSystem()
130                     .getStringArray(R.array.config_udfps_touch_detection_options)[selectedOption]
131                     .split(",")
132                     .map { it.toFloat() }
133 
134             return if (values[0] == 1f) {
135                 EllipseOverlapDetector(
136                     EllipseOverlapDetectorParams(
137                         minOverlap = values[3],
138                         targetSize = values[2],
139                         stepSize = values[4].toInt()
140                     )
141                 )
142             } else {
143                 BoundingBoxOverlapDetector(values[2])
144             }
145         }
146     }
147 }
148 
149 /**
150  * Background executor for HAL operations that are latency sensitive but too slow to run on the main
151  * thread. Prefer the shared executors, such as [com.android.systemui.dagger.qualifiers.Background]
152  * when a HAL is not directly involved.
153  */
154 @Qualifier
155 @MustBeDocumented
156 @Retention(AnnotationRetention.RUNTIME)
157 annotation class BiometricsBackground
158