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

Commit aa09e1ba authored by Lucas Silva's avatar Lucas Silva
Browse files

Dispatch scene changes on immediate dispatcher

This ensures the scene change happens sooner and reduces the risk of
race conditions.

Bug: 392969914
Test: atest CommunalInteractorTest
Flag: EXEMPT bugfix
Change-Id: I1e598720931013ff0df4c3a237dd3f62e8c00caa
parent 3cc93840
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import com.android.systemui.SysuiTestCase
import com.android.systemui.communal.shared.model.CommunalScenes
import com.android.systemui.keyguard.data.repository.fakeKeyguardRepository
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.applicationCoroutineScope
import com.android.systemui.kosmos.backgroundScope
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
@@ -49,7 +48,6 @@ class CommunalSceneRepositoryImplTest : SysuiTestCase() {
    private val Kosmos.underTest by
        Kosmos.Fixture {
            CommunalSceneRepositoryImpl(
                applicationScope = applicationCoroutineScope,
                backgroundScope = backgroundScope,
                sceneDataSource = delegator,
                delegator = delegator,
+10 −17
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
package com.android.systemui.communal.data.repository

import android.content.res.Configuration
import com.android.app.tracing.coroutines.launchTraced as launch
import androidx.annotation.MainThread
import com.android.compose.animation.scene.ObservableTransitionState
import com.android.compose.animation.scene.OverlayKey
import com.android.compose.animation.scene.SceneKey
@@ -25,7 +25,6 @@ import com.android.compose.animation.scene.TransitionKey
import com.android.systemui.communal.dagger.Communal
import com.android.systemui.communal.shared.model.CommunalScenes
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.scene.shared.model.SceneDataSource
import com.android.systemui.scene.shared.model.SceneDataSourceDelegator
@@ -54,13 +53,13 @@ interface CommunalSceneRepository {
    val communalContainerOrientation: StateFlow<Int>

    /** Updates the requested scene. */
    fun changeScene(toScene: SceneKey, transitionKey: TransitionKey? = null)
    @MainThread fun changeScene(toScene: SceneKey, transitionKey: TransitionKey? = null)

    /** Immediately snaps to the desired scene. */
    fun snapToScene(toScene: SceneKey)
    @MainThread fun snapToScene(toScene: SceneKey)

    /** Shows the hub from a power button press. */
    suspend fun showHubFromPowerButton()
    @MainThread fun showHubFromPowerButton()

    /**
     * Updates the transition state of the hub [SceneTransitionLayout].
@@ -77,7 +76,6 @@ interface CommunalSceneRepository {
class CommunalSceneRepositoryImpl
@Inject
constructor(
    @Application private val applicationScope: CoroutineScope,
    @Background backgroundScope: CoroutineScope,
    @Communal private val sceneDataSource: SceneDataSource,
    @Communal private val delegator: SceneDataSourceDelegator,
@@ -101,27 +99,22 @@ constructor(
    override val communalContainerOrientation: StateFlow<Int> =
        _communalContainerOrientation.asStateFlow()

    @MainThread
    override fun changeScene(toScene: SceneKey, transitionKey: TransitionKey?) {
        applicationScope.launch {
            // SceneTransitionLayout state updates must be triggered on the thread the STL was
            // created on.
        sceneDataSource.changeScene(toScene, transitionKey)
    }
    }

    @MainThread
    override fun snapToScene(toScene: SceneKey) {
        applicationScope.launch {
            // SceneTransitionLayout state updates must be triggered on the thread the STL was
            // created on.
        sceneDataSource.snapToScene(toScene)
    }
    }

    override fun setCommunalContainerOrientation(orientation: Int) {
        _communalContainerOrientation.value = orientation
    }

    override suspend fun showHubFromPowerButton() {
    @MainThread
    override fun showHubFromPowerButton() {
        // If keyguard is not showing yet, the hub view is not ready and the
        // [SceneDataSourceDelegator] will still be using the default [NoOpSceneDataSource]
        // and initial key, which is Blank. This means that when the hub container loads, it
+6 −3
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.systemui.communal.shared.model.CommunalScenes.toSceneContaine
import com.android.systemui.communal.shared.model.EditModeState
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.keyguard.shared.model.KeyguardState
import com.android.systemui.scene.domain.interactor.SceneInteractor
import com.android.systemui.scene.shared.flag.SceneContainerFlag
@@ -37,6 +38,7 @@ import com.android.systemui.statusbar.policy.KeyguardStateController
import com.android.systemui.util.kotlin.BooleanFlowOperators.allOf
import com.android.systemui.util.kotlin.pairwiseBy
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
@@ -57,6 +59,7 @@ class CommunalSceneInteractor
@Inject
constructor(
    @Application private val applicationScope: CoroutineScope,
    @Main private val mainImmediateDispatcher: CoroutineDispatcher,
    private val repository: CommunalSceneRepository,
    private val logger: CommunalSceneLogger,
    private val sceneInteractor: SceneInteractor,
@@ -123,7 +126,7 @@ constructor(
        transitionKey: TransitionKey? = null,
        keyguardState: KeyguardState? = null,
    ) {
        applicationScope.launch("$TAG#changeScene") {
        applicationScope.launch("$TAG#changeScene", mainImmediateDispatcher) {
            if (SceneContainerFlag.isEnabled) {
                sceneInteractor.changeScene(
                    toScene = newScene.toSceneContainerSceneKey(),
@@ -153,7 +156,7 @@ constructor(
        delayMillis: Long = 0,
        keyguardState: KeyguardState? = null,
    ) {
        applicationScope.launch("$TAG#snapToScene") {
        applicationScope.launch("$TAG#snapToScene", mainImmediateDispatcher) {
            if (SceneContainerFlag.isEnabled) {
                sceneInteractor.snapToScene(
                    toScene = newScene.toSceneContainerSceneKey(),
@@ -177,7 +180,7 @@ constructor(

    fun showHubFromPowerButton() {
        val loggingReason = "showing hub from power button"
        applicationScope.launch("$TAG#showHubFromPowerButton") {
        applicationScope.launch("$TAG#showHubFromPowerButton", mainImmediateDispatcher) {
            if (SceneContainerFlag.isEnabled) {
                sceneInteractor.changeScene(
                    toScene = CommunalScenes.Communal.toSceneContainerSceneKey(),
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ class FakeCommunalSceneRepository(
        }
    }

    override suspend fun showHubFromPowerButton() {
    override fun showHubFromPowerButton() {
        snapToScene(CommunalScenes.Communal)
    }

+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import com.android.systemui.communal.data.repository.communalSceneRepository
import com.android.systemui.communal.shared.log.communalSceneLogger
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.applicationCoroutineScope
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.scene.domain.interactor.sceneInteractor
import com.android.systemui.statusbar.policy.keyguardStateController

@@ -27,6 +28,7 @@ val Kosmos.communalSceneInteractor: CommunalSceneInteractor by
    Kosmos.Fixture {
        CommunalSceneInteractor(
            applicationScope = applicationCoroutineScope,
            mainImmediateDispatcher = testDispatcher,
            repository = communalSceneRepository,
            logger = communalSceneLogger,
            sceneInteractor = sceneInteractor,