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 squishedQqsHeight: Int = 0,
31     override val qsHeight: Int = 0,
32     override val squishedQsHeight: Int = 0,
33 ) : QSSceneAdapter {
34     private val _customizerState = MutableStateFlow<CustomizerState>(CustomizerState.Hidden)
35 
36     private val _customizerShowing = MutableStateFlow(false)
37     override val isCustomizerShowing = _customizerShowing.asStateFlow()
38 
39     private val _customizing = MutableStateFlow(false)
40     override val isCustomizing = _customizing.asStateFlow()
41 
42     private val _animationDuration = MutableStateFlow(0)
43     override val customizerAnimationDuration = _animationDuration.asStateFlow()
44 
45     private val _view = MutableStateFlow<View?>(null)
46     override val qsView: StateFlow<View?> = _view.asStateFlow()
47 
48     private val _state = MutableStateFlow<QSSceneAdapter.State?>(null)
49     val state = _state.filterNotNull()
50 
51     private val _navBarPadding = MutableStateFlow<Int>(0)
52     val navBarPadding = _navBarPadding.asStateFlow()
53 
54     var brightnessMirrorController: MirrorController? = null
55         private set
56 
57     override var isQsFullyCollapsed: Boolean = true
58 
inflatenull59     override suspend fun inflate(context: Context) {
60         _view.value = inflateDelegate(context)
61     }
62 
setStatenull63     override fun setState(state: QSSceneAdapter.State) {
64         if (_view.value != null) {
65             _state.value = state
66         }
67     }
68 
applyLatestExpansionAndSquishinessnull69     override fun applyLatestExpansionAndSquishiness() {}
70 
setCustomizingnull71     fun setCustomizing(value: Boolean) {
72         updateCustomizerFlows(if (value) CustomizerState.Showing else CustomizerState.Hidden)
73     }
74 
applyBottomNavBarPaddingnull75     override suspend fun applyBottomNavBarPadding(padding: Int) {
76         _navBarPadding.value = padding
77     }
78 
requestCloseCustomizernull79     override fun requestCloseCustomizer() {
80         updateCustomizerFlows(CustomizerState.Hidden)
81     }
82 
setBrightnessMirrorControllernull83     override fun setBrightnessMirrorController(mirrorController: MirrorController?) {
84         brightnessMirrorController = mirrorController
85     }
86 
updateCustomizerFlowsnull87     private fun updateCustomizerFlows(customizerState: CustomizerState) {
88         _customizerState.value = customizerState
89         _customizing.value = customizerState.isCustomizing
90         _customizerShowing.value = customizerState.isShowing
91         _animationDuration.value =
92             (customizerState as? CustomizerState.Animating)?.animationDuration?.toInt() ?: 0
93     }
94 }
95