1 /*
2 * Copyright (C) 2025 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 package com.android.app.displaylib
17
18 import android.hardware.display.DisplayManager
19 import android.os.Handler
20 import dagger.Binds
21 import dagger.BindsInstance
22 import dagger.Component
23 import dagger.Module
24 import javax.inject.Singleton
25 import kotlinx.coroutines.CoroutineDispatcher
26 import kotlinx.coroutines.CoroutineScope
27
28 /**
29 * Component that creates all classes in displaylib.
30 *
31 * Each user of this library will bind the required element in the factory constructor. It's advised
32 * to use this component through [createDisplayLibComponent], which wraps the dagger generated
33 * method.
34 */
35 @Component(modules = [DisplayLibModule::class])
36 @Singleton
37 interface DisplayLibComponent {
38
39 @Component.Factory
40 interface Factory {
createnull41 fun create(
42 @BindsInstance displayManager: DisplayManager,
43 @BindsInstance bgHandler: Handler,
44 @BindsInstance bgApplicationScope: CoroutineScope,
45 @BindsInstance backgroundCoroutineDispatcher: CoroutineDispatcher,
46 ): DisplayLibComponent
47 }
48
49 val displayRepository: DisplayRepository
50 }
51
52 @Module
53 interface DisplayLibModule {
54 @Binds fun bindDisplayManagerImpl(impl: DisplayRepositoryImpl): DisplayRepository
55 }
56
57 /**
58 * Just a wrapper to make the generated code to create the component more explicit.
59 *
60 * This should be called only once per process. Note that [bgHandler], [bgApplicationScope] and
61 * [backgroundCoroutineDispatcher] are expected to be backed by background threads. In the future
62 * this might throw an exception if they are tied to the main thread!
63 */
createDisplayLibComponentnull64 fun createDisplayLibComponent(
65 displayManager: DisplayManager,
66 bgHandler: Handler,
67 bgApplicationScope: CoroutineScope,
68 backgroundCoroutineDispatcher: CoroutineDispatcher,
69 ): DisplayLibComponent {
70 return DaggerDisplayLibComponent.factory()
71 .create(displayManager, bgHandler, bgApplicationScope, backgroundCoroutineDispatcher)
72 }
73