1 /*
<lambda>null2  * 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.qs.ui.composable
18 
19 import androidx.compose.animation.core.animateFloatAsState
20 import androidx.compose.foundation.layout.Box
21 import androidx.compose.foundation.layout.fillMaxSize
22 import androidx.compose.foundation.layout.offset
23 import androidx.compose.runtime.Composable
24 import androidx.compose.runtime.getValue
25 import androidx.compose.ui.Alignment
26 import androidx.compose.ui.Modifier
27 import androidx.compose.ui.graphics.graphicsLayer
28 import androidx.compose.ui.unit.IntOffset
29 import androidx.compose.ui.viewinterop.AndroidView
30 import androidx.lifecycle.compose.collectAsStateWithLifecycle
31 import com.android.compose.modifiers.height
32 import com.android.compose.modifiers.width
33 import com.android.systemui.qs.ui.adapter.QSSceneAdapter
34 import com.android.systemui.settings.brightness.ui.binder.BrightnessMirrorInflater
35 import com.android.systemui.settings.brightness.ui.viewModel.BrightnessMirrorViewModel
36 
37 @Composable
38 fun BrightnessMirror(
39     viewModel: BrightnessMirrorViewModel,
40     qsSceneAdapter: QSSceneAdapter,
41     modifier: Modifier = Modifier,
42 ) {
43     val isShowing by viewModel.isShowing.collectAsStateWithLifecycle()
44     val mirrorAlpha by
45         animateFloatAsState(
46             targetValue = if (isShowing) 1f else 0f,
47             label = "alphaAnimationBrightnessMirrorShowing",
48         )
49     val mirrorOffsetAndSize by viewModel.locationAndSize.collectAsStateWithLifecycle()
50     val offset = IntOffset(0, mirrorOffsetAndSize.yOffset)
51 
52     Box(modifier = modifier.fillMaxSize().graphicsLayer { alpha = mirrorAlpha }) {
53         QuickSettingsTheme {
54             // The assumption for using this AndroidView is that there will be only one in view at
55             // a given time (which is a reasonable assumption). Because `QSSceneAdapter` (actually
56             // `BrightnessSliderController` only supports a single mirror).
57             // The benefit of doing it like this is that if the configuration changes or QSImpl is
58             // re-inflated, it's not relevant to the composable, as we'll always get a new one.
59             AndroidView(
60                 modifier =
61                     Modifier.align(Alignment.TopCenter)
62                         .offset { offset }
63                         .width { mirrorOffsetAndSize.width }
64                         .height { mirrorOffsetAndSize.height },
65                 factory = { context ->
66                     val (view, controller) =
67                         BrightnessMirrorInflater.inflate(context, viewModel.sliderControllerFactory)
68                     viewModel.setToggleSlider(controller)
69                     view
70                 },
71                 update = { qsSceneAdapter.setBrightnessMirrorController(viewModel) },
72                 onRelease = { qsSceneAdapter.setBrightnessMirrorController(null) }
73             )
74         }
75     }
76 }
77