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 
17 package com.android.systemui.qs.ui.adapter
18 
19 import android.content.Context
20 import android.view.View
21 import com.android.systemui.settings.brightness.MirrorController
22 import kotlinx.coroutines.flow.MutableStateFlow
23 import kotlinx.coroutines.flow.StateFlow
24 import kotlinx.coroutines.flow.asStateFlow
25 import kotlinx.coroutines.flow.filterNotNull
26 
27 class FakeQSSceneAdapter(
28     private val inflateDelegate: suspend (Context) -> View,
29     override val qqsHeight: Int = 0,
30     override val qsHeight: Int = 0,
31 ) : QSSceneAdapter {
32     private val _customizerState = MutableStateFlow<CustomizerState>(CustomizerState.Hidden)
33 
34     private val _customizerShowing = MutableStateFlow(false)
35     override val isCustomizerShowing = _customizerShowing.asStateFlow()
36 
37     private val _customizing = MutableStateFlow(false)
38     override val isCustomizing = _customizing.asStateFlow()
39 
40     private val _animationDuration = MutableStateFlow(0)
41     override val customizerAnimationDuration = _animationDuration.asStateFlow()
42 
43     private val _view = MutableStateFlow<View?>(null)
44     override val qsView: StateFlow<View?> = _view.asStateFlow()
45 
46     private val _state = MutableStateFlow<QSSceneAdapter.State?>(null)
47     val state = _state.filterNotNull()
48 
49     private val _navBarPadding = MutableStateFlow<Int>(0)
50     val navBarPadding = _navBarPadding.asStateFlow()
51 
52     var brightnessMirrorController: MirrorController? = null
53         private set
54 
55     override var isQsFullyCollapsed: Boolean = true
56 
inflatenull57     override suspend fun inflate(context: Context) {
58         _view.value = inflateDelegate(context)
59     }
60 
setStatenull61     override fun setState(state: QSSceneAdapter.State) {
62         if (_view.value != null) {
63             _state.value = state
64         }
65     }
66 
applyLatestExpansionAndSquishinessnull67     override fun applyLatestExpansionAndSquishiness() {}
68 
setCustomizingnull69     fun setCustomizing(value: Boolean) {
70         updateCustomizerFlows(if (value) CustomizerState.Showing else CustomizerState.Hidden)
71     }
72 
applyBottomNavBarPaddingnull73     override suspend fun applyBottomNavBarPadding(padding: Int) {
74         _navBarPadding.value = padding
75     }
76 
requestCloseCustomizernull77     override fun requestCloseCustomizer() {
78         updateCustomizerFlows(CustomizerState.Hidden)
79     }
80 
setBrightnessMirrorControllernull81     override fun setBrightnessMirrorController(mirrorController: MirrorController?) {
82         brightnessMirrorController = mirrorController
83     }
84 
updateCustomizerFlowsnull85     private fun updateCustomizerFlows(customizerState: CustomizerState) {
86         _customizerState.value = customizerState
87         _customizing.value = customizerState.isCustomizing
88         _customizerShowing.value = customizerState.isShowing
89         _animationDuration.value =
90             (customizerState as? CustomizerState.Animating)?.animationDuration?.toInt() ?: 0
91     }
92 }
93