Merge "Move changeScene calls to the application scope" into main
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/data/repository/CommunalRepositoryImplTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/data/repository/CommunalRepositoryImplTest.kt
index 45e7d8a..fd0bf4d 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/data/repository/CommunalRepositoryImplTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/data/repository/CommunalRepositoryImplTest.kt
@@ -41,6 +41,7 @@
     private val underTest by lazy {
         CommunalSceneRepositoryImpl(
             kosmos.applicationCoroutineScope,
+            kosmos.applicationCoroutineScope,
             kosmos.sceneDataSource,
         )
     }
diff --git a/packages/SystemUI/src/com/android/systemui/communal/data/repository/CommunalSceneRepository.kt b/packages/SystemUI/src/com/android/systemui/communal/data/repository/CommunalSceneRepository.kt
index d6d08b4..260dcba 100644
--- a/packages/SystemUI/src/com/android/systemui/communal/data/repository/CommunalSceneRepository.kt
+++ b/packages/SystemUI/src/com/android/systemui/communal/data/repository/CommunalSceneRepository.kt
@@ -22,6 +22,7 @@
 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 javax.inject.Inject
@@ -34,6 +35,7 @@
 import kotlinx.coroutines.flow.flatMapLatest
 import kotlinx.coroutines.flow.flowOf
 import kotlinx.coroutines.flow.stateIn
+import kotlinx.coroutines.launch
 
 /** Encapsulates the state of communal mode. */
 interface CommunalSceneRepository {
@@ -64,6 +66,7 @@
 class CommunalSceneRepositoryImpl
 @Inject
 constructor(
+    @Application private val applicationScope: CoroutineScope,
     @Background backgroundScope: CoroutineScope,
     @Communal private val sceneDataSource: SceneDataSource,
 ) : CommunalSceneRepository {
@@ -82,11 +85,19 @@
             )
 
     override fun changeScene(toScene: SceneKey, transitionKey: TransitionKey?) {
-        sceneDataSource.changeScene(toScene, transitionKey)
+        applicationScope.launch {
+            // SceneTransitionLayout state updates must be triggered on the thread the STL was
+            // created on.
+            sceneDataSource.changeScene(toScene, transitionKey)
+        }
     }
 
     override fun snapToScene(toScene: SceneKey) {
-        sceneDataSource.snapToScene(toScene)
+        applicationScope.launch {
+            // SceneTransitionLayout state updates must be triggered on the thread the STL was
+            // created on.
+            sceneDataSource.snapToScene(toScene)
+        }
     }
 
     /**