• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.brightness.ui.viewModel
18 
19 import android.content.res.Resources
20 import android.util.Log
21 import android.view.View
22 import com.android.systemui.dagger.qualifiers.Main
23 import com.android.systemui.res.R
24 import com.android.systemui.settings.brightness.BrightnessSliderController
25 import com.android.systemui.settings.brightness.MirrorController
26 import com.android.systemui.settings.brightness.ToggleSlider
27 import com.android.systemui.settings.brightness.domain.interactor.BrightnessMirrorShowingInteractor
28 import dagger.assisted.AssistedFactory
29 import dagger.assisted.AssistedInject
30 import kotlinx.coroutines.flow.MutableStateFlow
31 import kotlinx.coroutines.flow.asStateFlow
32 
33 class BrightnessMirrorViewModel
34 @AssistedInject
35 constructor(
36     private val brightnessMirrorShowingInteractor: BrightnessMirrorShowingInteractor,
37     @Main private val resources: Resources,
38     val sliderControllerFactory: BrightnessSliderController.Factory,
39 ) : MirrorController {
40 
41     private val tempPosition = IntArray(2)
42 
43     private var _toggleSlider: BrightnessSliderController? = null
44 
45     val isShowing = brightnessMirrorShowingInteractor.isShowing
46 
47     private val _locationAndSize: MutableStateFlow<LocationAndSize> =
48         MutableStateFlow(LocationAndSize())
49     val locationAndSize = _locationAndSize.asStateFlow()
50 
getToggleSlidernull51     override fun getToggleSlider(): ToggleSlider? {
52         return _toggleSlider
53     }
54 
setToggleSlidernull55     fun setToggleSlider(toggleSlider: BrightnessSliderController) {
56         _toggleSlider = toggleSlider
57     }
58 
showMirrornull59     override fun showMirror() {
60         brightnessMirrorShowingInteractor.setMirrorShowing(true)
61     }
62 
hideMirrornull63     override fun hideMirror() {
64         brightnessMirrorShowingInteractor.setMirrorShowing(false)
65     }
66 
setLocationAndSizenull67     override fun setLocationAndSize(view: View) {
68         view.getLocationInWindow(tempPosition)
69         val padding = resources.getDimensionPixelSize(R.dimen.rounded_slider_background_padding)
70         // Account for desired padding
71         _locationAndSize.value =
72             LocationAndSize(
73                 yOffsetFromContainer = view.findTopFromContainer() - padding,
74                 yOffsetFromWindow = tempPosition[1] - padding,
75                 width = view.measuredWidth + 2 * padding,
76                 height = view.measuredHeight + 2 * padding,
77             )
78     }
79 
findTopFromContainernull80     private fun View.findTopFromContainer(): Int {
81         var out = 0
82         var view = this
83         while (view.id != R.id.quick_settings_container) {
84             out += view.top
85             val parent = view.parent as? View
86             if (parent == null) {
87                 Log.wtf(TAG, "Couldn't find container in parents of $this")
88                 break
89             }
90             view = parent
91         }
92         return out
93     }
94 
95     // Callbacks are used for indicating reinflation when the config changes in some ways (like
96     // density). However, we don't need that as we recompose the view anyway
addCallbacknull97     override fun addCallback(listener: MirrorController.BrightnessMirrorListener) {}
98 
removeCallbacknull99     override fun removeCallback(listener: MirrorController.BrightnessMirrorListener) {}
100 
101     @AssistedFactory
102     interface Factory {
createnull103         fun create(): BrightnessMirrorViewModel
104     }
105 
106     companion object {
107         private const val TAG = "BrightnessMirrorViewModel"
108     }
109 }
110 
111 data class LocationAndSize(
112     val yOffsetFromContainer: Int = 0,
113     val yOffsetFromWindow: Int = 0,
114     val width: Int = 0,
115     val height: Int = 0,
116 )
117