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.systemui.scene.domain.resolver
18 
19 import com.android.compose.animation.scene.SceneKey
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.dagger.qualifiers.Application
22 import com.android.systemui.scene.shared.model.SceneFamilies
23 import com.android.systemui.scene.shared.model.Scenes
24 import com.android.systemui.shade.domain.interactor.ShadeInteractor
25 import com.android.systemui.shade.shared.model.ShadeMode
26 import dagger.Binds
27 import dagger.Module
28 import dagger.multibindings.IntoSet
29 import javax.inject.Inject
30 import kotlinx.coroutines.CoroutineScope
31 import kotlinx.coroutines.flow.SharingStarted
32 import kotlinx.coroutines.flow.StateFlow
33 import kotlinx.coroutines.flow.map
34 import kotlinx.coroutines.flow.stateIn
35 
36 @SysUISingleton
37 class QuickSettingsSceneFamilyResolver
38 @Inject
39 constructor(
40     @Application applicationScope: CoroutineScope,
41     shadeInteractor: ShadeInteractor,
42 ) : SceneResolver {
43     override val targetFamily: SceneKey = SceneFamilies.QuickSettings
44 
45     override val resolvedScene: StateFlow<SceneKey> =
46         shadeInteractor.shadeMode
47             .map(::quickSettingsScene)
48             .stateIn(
49                 applicationScope,
50                 started = SharingStarted.Eagerly,
51                 initialValue = quickSettingsScene(shadeInteractor.shadeMode.value),
52             )
53 
includesScenenull54     override fun includesScene(scene: SceneKey): Boolean = scene in quickSettingsScenes
55 
56     private fun quickSettingsScene(shadeMode: ShadeMode) =
57         when (shadeMode) {
58             is ShadeMode.Single -> Scenes.QuickSettings
59             is ShadeMode.Dual -> Scenes.QuickSettingsShade
60             is ShadeMode.Split -> Scenes.Shade
61         }
62 
63     companion object {
64         val quickSettingsScenes =
65             setOf(
66                 Scenes.QuickSettings,
67                 Scenes.QuickSettingsShade,
68                 Scenes.Shade,
69             )
70     }
71 }
72 
73 @Module
74 interface QuickSettingsSceneFamilyResolverModule {
bindResolvernull75     @Binds @IntoSet fun bindResolver(interactor: QuickSettingsSceneFamilyResolver): SceneResolver
76 }
77