Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 07f9abb3 authored by Fabian Kozynski's avatar Fabian Kozynski Committed by Android (Google) Code Review
Browse files

Merge "[Flexiglass] Implement brightness mirror" into main

parents 150b21eb 339845d7
Loading
Loading
Loading
Loading
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.qs.ui.composable

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.viewinterop.AndroidView
import com.android.compose.modifiers.height
import com.android.compose.modifiers.width
import com.android.systemui.qs.ui.adapter.QSSceneAdapter
import com.android.systemui.settings.brightness.ui.binder.BrightnessMirrorInflater
import com.android.systemui.settings.brightness.ui.viewModel.BrightnessMirrorViewModel

@Composable
fun BrightnessMirror(
    viewModel: BrightnessMirrorViewModel,
    qsSceneAdapter: QSSceneAdapter,
    modifier: Modifier = Modifier,
) {
    val isShowing by viewModel.isShowing.collectAsState()
    val mirrorAlpha by
        animateFloatAsState(
            targetValue = if (isShowing) 1f else 0f,
            label = "alphaAnimationBrightnessMirrorShowing",
        )
    val mirrorOffsetAndSize by viewModel.locationAndSize.collectAsState()
    val offset = IntOffset(0, mirrorOffsetAndSize.yOffset)

    Box(modifier = modifier.fillMaxSize().graphicsLayer { alpha = mirrorAlpha }) {
        QuickSettingsTheme {
            // The assumption for using this AndroidView is that there will be only one in view at
            // a given time (which is a reasonable assumption). Because `QSSceneAdapter` (actually
            // `BrightnessSliderController` only supports a single mirror).
            // The benefit of doing it like this is that if the configuration changes or QSImpl is
            // re-inflated, it's not relevant to the composable, as we'll always get a new one.
            AndroidView(
                modifier =
                    Modifier.align(Alignment.TopCenter)
                        .offset { offset }
                        .width { mirrorOffsetAndSize.width }
                        .height { mirrorOffsetAndSize.height },
                factory = { context ->
                    val (view, controller) =
                        BrightnessMirrorInflater.inflate(context, viewModel.sliderControllerFactory)
                    viewModel.setToggleSlider(controller)
                    view
                },
                update = { qsSceneAdapter.setBrightnessMirrorController(viewModel) },
                onRelease = { qsSceneAdapter.setBrightnessMirrorController(null) }
            )
        }
    }
}
+15 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.qs.ui.composable
import android.view.ViewGroup
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
@@ -49,6 +50,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.colorResource
@@ -120,8 +122,20 @@ private fun SceneScope.QuickSettingsScene(
    statusBarIconController: StatusBarIconController,
    modifier: Modifier = Modifier,
) {
    val brightnessMirrorShowing by viewModel.brightnessMirrorViewModel.isShowing.collectAsState()
    val contentAlpha by
        animateFloatAsState(
            targetValue = if (brightnessMirrorShowing) 0f else 1f,
            label = "alphaAnimationBrightnessMirrorContentHiding",
        )

    BrightnessMirror(
        viewModel = viewModel.brightnessMirrorViewModel,
        qsSceneAdapter = viewModel.qsSceneAdapter
    )

    // TODO(b/280887232): implement the real UI.
    Box(modifier = modifier.fillMaxSize()) {
    Box(modifier = modifier.fillMaxSize().graphicsLayer { alpha = contentAlpha }) {
        val isCustomizing by viewModel.qsSceneAdapter.isCustomizing.collectAsState()

        BackHandler(
+73 −37
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.shade.ui.composable

import android.view.ViewGroup
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.clipScrollableContainer
@@ -33,6 +34,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
@@ -45,6 +47,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layout
import androidx.compose.ui.platform.LocalDensity
@@ -70,6 +73,7 @@ import com.android.systemui.media.controls.ui.view.MediaHostState
import com.android.systemui.media.dagger.MediaModule.QUICK_QS_PANEL
import com.android.systemui.notifications.ui.composable.NotificationScrollingStack
import com.android.systemui.qs.footer.ui.compose.FooterActionsWithAnimatedVisibility
import com.android.systemui.qs.ui.composable.BrightnessMirror
import com.android.systemui.qs.ui.composable.QuickSettings
import com.android.systemui.res.R
import com.android.systemui.scene.shared.model.Scenes
@@ -302,12 +306,25 @@ private fun SceneScope.SplitShade(
        }
    }

    val brightnessMirrorShowing by viewModel.brightnessMirrorViewModel.isShowing.collectAsState()
    val contentAlpha by
        animateFloatAsState(
            targetValue = if (brightnessMirrorShowing) 0f else 1f,
            label = "alphaAnimationBrightnessMirrorContentHiding",
        )

    val brightnessMirrorShowingModifier = Modifier.graphicsLayer { alpha = contentAlpha }

    Box(
        modifier =
            modifier
                .fillMaxSize()
                .element(Shade.Elements.BackgroundScrim)
                .background(colorResource(R.color.shade_scrim_background_dark))
                // Cannot set the alpha of the whole element to 0, because the mirror should be
                // in the QS column.
                .background(
                    colorResource(R.color.shade_scrim_background_dark).copy(alpha = contentAlpha)
                )
    ) {
        Column(
            modifier = Modifier.fillMaxSize(),
@@ -317,20 +334,32 @@ private fun SceneScope.SplitShade(
                createTintedIconManager = createTintedIconManager,
                createBatteryMeterViewController = createBatteryMeterViewController,
                statusBarIconController = statusBarIconController,
                modifier = Modifier.padding(horizontal = Shade.Dimensions.HorizontalPadding)
                modifier =
                    Modifier.padding(horizontal = Shade.Dimensions.HorizontalPadding)
                        .then(brightnessMirrorShowingModifier)
            )

            Row(modifier = Modifier.fillMaxWidth().weight(1f)) {
                Box(modifier = Modifier.weight(1f)) {
                    BrightnessMirror(
                        viewModel = viewModel.brightnessMirrorViewModel,
                        qsSceneAdapter = viewModel.qsSceneAdapter,
                        // Need to remove the offset of the header height, as the mirror uses
                        // the position of the Brightness slider in the window
                        modifier = Modifier.offset(y = -ShadeHeader.Dimensions.CollapsedHeight)
                    )
                    Column(
                        verticalArrangement = Arrangement.Top,
                        modifier =
                        Modifier.weight(1f).fillMaxSize().thenIf(!isCustomizing) {
                            Modifier.fillMaxSize().thenIf(!isCustomizing) {
                                Modifier.padding(bottom = navBarBottomHeight)
                            },
                    ) {
                        Column(
                            modifier =
                            Modifier.fillMaxSize().weight(1f).thenIf(!isCustomizing) {
                                Modifier.fillMaxSize()
                                    .weight(1f)
                                    .thenIf(!isCustomizing) {
                                        Modifier.verticalNestedScrollToScene()
                                            .verticalScroll(
                                                quickSettingsScrollState,
@@ -338,6 +367,7 @@ private fun SceneScope.SplitShade(
                                            )
                                            .clipScrollableContainer(Orientation.Horizontal)
                                    }
                                    .then(brightnessMirrorShowingModifier)
                        ) {
                            Box(
                                modifier =
@@ -363,15 +393,21 @@ private fun SceneScope.SplitShade(
                            viewModel = footerActionsViewModel,
                            isCustomizing = isCustomizing,
                            lifecycleOwner = lifecycleOwner,
                        modifier = Modifier.align(Alignment.CenterHorizontally),
                            modifier =
                                Modifier.align(Alignment.CenterHorizontally)
                                    .then(brightnessMirrorShowingModifier),
                        )
                    }
                }

                NotificationScrollingStack(
                    viewModel = viewModel.notifications,
                    maxScrimTop = { 0f },
                    modifier =
                        Modifier.weight(1f).fillMaxHeight().padding(bottom = navBarBottomHeight),
                        Modifier.weight(1f)
                            .fillMaxHeight()
                            .padding(bottom = navBarBottomHeight)
                            .then(brightnessMirrorShowingModifier),
                )
            }
        }
+18 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import com.android.systemui.kosmos.testScope
import com.android.systemui.qs.QSImpl
import com.android.systemui.qs.dagger.QSComponent
import com.android.systemui.qs.dagger.QSSceneComponent
import com.android.systemui.settings.brightness.MirrorController
import com.android.systemui.shade.data.repository.fakeShadeRepository
import com.android.systemui.shade.domain.interactor.shadeInteractor
import com.android.systemui.shade.shared.model.ShadeMode
@@ -508,4 +509,21 @@ class QSSceneAdapterImplTest : SysuiTestCase() {
            underTest.requestCloseCustomizer()
            verify(qsImpl!!).closeCustomizer()
        }

    @Test
    fun setBrightnessMirrorController() =
        testScope.runTest {
            val qsImpl by collectLastValue(underTest.qsImpl)

            underTest.inflate(context)
            runCurrent()

            val mirrorController = mock<MirrorController>()
            underTest.setBrightnessMirrorController(mirrorController)

            verify(qsImpl!!).setBrightnessMirrorController(mirrorController)

            underTest.setBrightnessMirrorController(null)
            verify(qsImpl!!).setBrightnessMirrorController(null)
        }
}
+2 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.systemui.qs.footer.ui.viewmodel.FooterActionsViewModel
import com.android.systemui.qs.ui.adapter.FakeQSSceneAdapter
import com.android.systemui.res.R
import com.android.systemui.scene.shared.model.Scenes
import com.android.systemui.settings.brightness.ui.viewmodel.brightnessMirrorViewModel
import com.android.systemui.shade.domain.interactor.privacyChipInteractor
import com.android.systemui.shade.domain.interactor.shadeHeaderClockInteractor
import com.android.systemui.shade.domain.interactor.shadeInteractor
@@ -108,6 +109,7 @@ class QuickSettingsSceneViewModelTest : SysuiTestCase() {

        underTest =
            QuickSettingsSceneViewModel(
                brightnessMirrorViewModel = kosmos.brightnessMirrorViewModel,
                shadeHeaderViewModel = shadeHeaderViewModel,
                qsSceneAdapter = qsFlexiglassAdapter,
                notifications = kosmos.notificationsPlaceholderViewModel,
Loading