1 /*
2  * Copyright (C) 2023 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.dream.lowlight.dagger
17 
18 import android.app.DreamManager
19 import android.content.ComponentName
20 import android.content.Context
21 import com.android.dream.lowlight.R
22 import com.android.dream.lowlight.dagger.qualifiers.Application
23 import com.android.dream.lowlight.dagger.qualifiers.Main
24 import dagger.Module
25 import dagger.Provides
26 import kotlinx.coroutines.CoroutineDispatcher
27 import kotlinx.coroutines.CoroutineScope
28 import kotlinx.coroutines.Dispatchers
29 import javax.inject.Named
30 
31 /**
32  * Dagger module for low light dream.
33  *
34  * @hide
35  */
36 @Module
37 object LowLightDreamModule {
38     /**
39      * Provides dream manager.
40      */
41     @Provides
providesDreamManagernull42     fun providesDreamManager(context: Context): DreamManager {
43         return requireNotNull(context.getSystemService(DreamManager::class.java))
44     }
45 
46     /**
47      * Provides the component name of the low light dream, or null if not configured.
48      */
49     @Provides
50     @Named(LOW_LIGHT_DREAM_COMPONENT)
providesLowLightDreamComponentnull51     fun providesLowLightDreamComponent(context: Context): ComponentName? {
52         val lowLightDreamComponent = context.resources.getString(
53             R.string.config_lowLightDreamComponent
54         )
55         return if (lowLightDreamComponent.isEmpty()) {
56             null
57         } else {
58             ComponentName.unflattenFromString(lowLightDreamComponent)
59         }
60     }
61 
62     @Provides
63     @Named(LOW_LIGHT_TRANSITION_TIMEOUT_MS)
providesLowLightTransitionTimeoutnull64     fun providesLowLightTransitionTimeout(context: Context): Long {
65         return context.resources.getInteger(R.integer.config_lowLightTransitionTimeoutMs).toLong()
66     }
67 
68     @Provides
69     @Main
providesMainDispatchernull70     fun providesMainDispatcher(): CoroutineDispatcher {
71         return Dispatchers.Main.immediate
72     }
73 
74     @Provides
75     @Application
providesApplicationScopenull76     fun providesApplicationScope(@Main dispatcher: CoroutineDispatcher): CoroutineScope {
77         return CoroutineScope(dispatcher)
78     }
79 
80     const val LOW_LIGHT_DREAM_COMPONENT = "low_light_dream_component"
81     const val LOW_LIGHT_TRANSITION_TIMEOUT_MS = "low_light_transition_timeout"
82 }
83