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

Commit 7817a211 authored by Fabián Kozynski's avatar Fabián Kozynski
Browse files

Animate QS between Shade and QuickSettings

Use the transitionState to drive the expanding animation in the adapter.
Now that we are using movable content, we don't need to make sure to
attach and detach manually.

Quick fix is needed in QSAnimator so that after the animators are
updated (and all the views are reset), we set the progress to the last
progress. This is an issue in compose because the view is dettached and
reattached at the end of the animation.

Fixes: 312503265
Test: atest QSSceneAdapterImplTest QSSceneAdapterTest
Test: manual: enable flags and expand/collapse
Flag: ACONFIG com.android.systemui.scene_container DEVELOPMENT

Change-Id: I2e1c86eea01f9926797209224c5d10e2f6bee06d
parent 3c941e1a
Loading
Loading
Loading
Loading
+42 −34
Original line number Diff line number Diff line
@@ -17,11 +17,7 @@
package com.android.systemui.qs.ui.composable

import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
@@ -30,7 +26,6 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
@@ -38,9 +33,15 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.android.compose.animation.scene.ElementKey
import com.android.compose.animation.scene.SceneScope
import com.android.compose.animation.scene.TransitionState
import com.android.compose.theme.colorAttr
import com.android.systemui.qs.ui.adapter.QSSceneAdapter
import com.android.systemui.qs.ui.adapter.QSSceneAdapter.State.Companion.Collapsing
import com.android.systemui.qs.ui.adapter.QSSceneAdapter.State.Expanding
import com.android.systemui.res.R
import com.android.systemui.scene.ui.composable.Gone
import com.android.systemui.scene.ui.composable.QuickSettings as QuickSettingsSceneKey
import com.android.systemui.scene.ui.composable.Shade

object QuickSettings {
    object Elements {
@@ -59,21 +60,45 @@ private fun QuickSettingsTheme(content: @Composable () -> Unit) {
    CompositionLocalProvider(LocalContext provides themedContext) { content() }
}

private fun SceneScope.stateForQuickSettingsContent(): QSSceneAdapter.State {
    return when (val transitionState = layoutState.transitionState) {
        is TransitionState.Idle -> {
            when (transitionState.currentScene) {
                Shade -> QSSceneAdapter.State.QQS
                QuickSettingsSceneKey -> QSSceneAdapter.State.QS
                else -> QSSceneAdapter.State.CLOSED
            }
        }
        is TransitionState.Transition ->
            with(transitionState) {
                when {
                    fromScene == Shade && toScene == QuickSettingsSceneKey -> Expanding(progress)
                    fromScene == QuickSettingsSceneKey && toScene == Shade -> Collapsing(progress)
                    toScene == Shade -> QSSceneAdapter.State.QQS
                    toScene == QuickSettingsSceneKey -> QSSceneAdapter.State.QS
                    toScene == Gone -> QSSceneAdapter.State.CLOSED
                    else -> error("Bad transition for QuickSettings: $transitionState")
                }
            }
    }
}

/**
 * This composable will show QuickSettingsContent in the correct state (as determined by its
 * [SceneScope]).
 */
@Composable
fun SceneScope.QuickSettings(
    modifier: Modifier = Modifier,
    qsSceneAdapter: QSSceneAdapter,
    state: QSSceneAdapter.State
) {
    // TODO(b/272780058): implement.
    Column(
        modifier =
            modifier
                .element(QuickSettings.Elements.Content)
                .fillMaxWidth()
                .defaultMinSize(minHeight = 300.dp)
    val contentState = stateForQuickSettingsContent()

    MovableElement(
        key = QuickSettings.Elements.Content,
        modifier = modifier.fillMaxWidth().defaultMinSize(minHeight = 300.dp)
    ) {
        QuickSettingsContent(qsSceneAdapter = qsSceneAdapter, state)
        QuickSettingsContent(qsSceneAdapter = qsSceneAdapter, contentState)
    }
}

@@ -87,37 +112,20 @@ private fun QuickSettingsContent(
    QuickSettingsTheme {
        val context = LocalContext.current

        val frame by remember(context) { mutableStateOf(FrameLayout(context)) }

        LaunchedEffect(key1 = context) {
            if (qsView == null) {
                qsSceneAdapter.inflate(context, frame)
                qsSceneAdapter.inflate(context)
            }
        }
        qsView?.let {
            it.attachToParent(frame)
        qsView?.let { view ->
            AndroidView(
                modifier = modifier.fillMaxSize().background(colorAttr(R.attr.underSurface)),
                factory = { _ ->
                    qsSceneAdapter.setState(state)
                    frame
                    view
                },
                onRelease = { frame.removeAllViews() },
                update = { qsSceneAdapter.setState(state) }
            )
        }
    }
}

private fun View.attachToParent(parent: ViewGroup) {
    if (this.parent != null && this.parent != parent) {
        (this.parent as ViewGroup).removeView(this)
    }
    if (this.parent != parent) {
        parent.addView(
            this,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
        )
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ import com.android.systemui.battery.BatteryMeterViewController
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.notifications.ui.composable.HeadsUpNotificationSpace
import com.android.systemui.qs.ui.adapter.QSSceneAdapter
import com.android.systemui.qs.ui.viewmodel.QuickSettingsSceneViewModel
import com.android.systemui.scene.shared.model.SceneKey
import com.android.systemui.scene.ui.composable.ComposableScene
@@ -156,7 +155,6 @@ private fun SceneScope.QuickSettingsScene(
                QuickSettings(
                    modifier = Modifier.fillMaxHeight(),
                    viewModel.qsSceneAdapter,
                    QSSceneAdapter.State.QS
                )
            }
        }
+0 −2
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import com.android.systemui.battery.BatteryMeterViewController
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.notifications.ui.composable.NotificationStack
import com.android.systemui.qs.ui.adapter.QSSceneAdapter
import com.android.systemui.qs.ui.composable.QuickSettings
import com.android.systemui.scene.shared.model.Direction
import com.android.systemui.scene.shared.model.SceneKey
@@ -157,7 +156,6 @@ private fun SceneScope.ShadeScene(
            QuickSettings(
                modifier = Modifier.wrapContentHeight(),
                viewModel.qsSceneAdapter,
                QSSceneAdapter.State.QQS
            )
            Spacer(modifier = Modifier.height(16.dp))
            NotificationStack(
+31 −0
Original line number Diff line number Diff line
@@ -215,6 +215,37 @@ class QSSceneAdapterImplTest : SysuiTestCase() {
            }
        }

    @Test
    fun state_expanding() =
        testScope.runTest {
            val qsImpl by collectLastValue(underTest.qsImpl)
            val progress = 0.34f

            underTest.inflate(context)
            runCurrent()
            clearInvocations(qsImpl!!)

            underTest.setState(QSSceneAdapter.State.Expanding(progress))
            with(qsImpl!!) {
                verify(this).setQsVisible(true)
                verify(this)
                    .setQsExpansion(
                        /* expansion= */ progress,
                        /* panelExpansionFraction= */ 1f,
                        /* proposedTranslation= */ 0f,
                        /* squishinessFraction= */ 1f,
                    )
                verify(this).setListening(true)
                verify(this).setExpanded(true)
                verify(this)
                    .setTransitionToFullShadeProgress(
                        /* isTransitioningToFullShade= */ false,
                        /* qsTransitionFraction= */ 1f,
                        /* qsSquishinessFraction = */ 1f,
                    )
            }
        }

    @Test
    fun customizing_QS() =
        testScope.runTest {
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.adapter

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.qs.ui.adapter.QSSceneAdapter.State.Companion.Collapsing
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class QSSceneAdapterTest : SysuiTestCase() {
    @Test
    fun expandingSpecialValues() {
        assertThat(QSSceneAdapter.State.QQS).isEqualTo(QSSceneAdapter.State.Expanding(0f))
        assertThat(QSSceneAdapter.State.QS).isEqualTo(QSSceneAdapter.State.Expanding(1f))
    }

    @Test
    fun collapsing() {
        val collapsingProgress = 0.3f
        assertThat(Collapsing(collapsingProgress))
            .isEqualTo(QSSceneAdapter.State.Expanding(1 - collapsingProgress))
    }
}
Loading