Refactor logic to allow touches to use SceneTransitionLayout APIs

MutableSceneTransitionLayoutState already offers a way to pass in a
function which gets called when a gesture is detected, to determine
whether the gesture is allowed to change scenes or not. This change
migrates the existing mechanism which disallows touches to this built-in
API instead.

This has the benefit of not needing to recompose the container when the
shade is expanded/collapsed.

In the future, we can also check falsing signal inside this same
callback.

Bug: 336330508
Test: atest CommunalViewModelTest
Flag: ACONFIG com.android.systemui.communal_hub TEAMFOOD
Change-Id: I558f03b5e26d5d520bfd07f690278a049e56fd2c
diff --git a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt
index 356bfe2..e07cd05 100644
--- a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt
+++ b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt
@@ -79,10 +79,10 @@
 ) {
     val coroutineScope = rememberCoroutineScope()
     val currentSceneKey: SceneKey by viewModel.currentScene.collectAsState(CommunalScenes.Blank)
-    val touchesAllowed by viewModel.touchesAllowed.collectAsState(initial = false)
     val state: MutableSceneTransitionLayoutState = remember {
         MutableSceneTransitionLayoutState(
             initialScene = currentSceneKey,
+            canChangeScene = { _ -> viewModel.canChangeScene() },
             transitions = sceneTransitions,
             enableInterruptions = false,
         )
@@ -112,14 +112,9 @@
         scene(
             CommunalScenes.Blank,
             userActions =
-                if (touchesAllowed) {
-                    mapOf(
-                        Swipe(SwipeDirection.Left, fromSource = Edge.Right) to
-                            CommunalScenes.Communal
-                    )
-                } else {
-                    emptyMap()
-                }
+                mapOf(
+                    Swipe(SwipeDirection.Left, fromSource = Edge.Right) to CommunalScenes.Communal
+                )
         ) {
             // This scene shows nothing only allowing for transitions to the communal scene.
             Box(modifier = Modifier.fillMaxSize())
@@ -128,13 +123,7 @@
         scene(
             CommunalScenes.Communal,
             userActions =
-                if (touchesAllowed) {
-                    mapOf(
-                        Swipe(SwipeDirection.Right, fromSource = Edge.Left) to CommunalScenes.Blank
-                    )
-                } else {
-                    emptyMap()
-                },
+                mapOf(Swipe(SwipeDirection.Right, fromSource = Edge.Left) to CommunalScenes.Blank)
         ) {
             CommunalScene(viewModel, colors, dialogFactory, modifier = modifier)
         }
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/view/viewmodel/CommunalViewModelTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/view/viewmodel/CommunalViewModelTest.kt
index 5be765d..779e79ed 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/view/viewmodel/CommunalViewModelTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/communal/view/viewmodel/CommunalViewModelTest.kt
@@ -44,11 +44,13 @@
 import com.android.systemui.flags.fakeFeatureFlagsClassic
 import com.android.systemui.keyguard.data.repository.FakeKeyguardRepository
 import com.android.systemui.keyguard.data.repository.fakeKeyguardRepository
+import com.android.systemui.keyguard.shared.model.StatusBarState
 import com.android.systemui.kosmos.testScope
 import com.android.systemui.log.logcatLogBuffer
 import com.android.systemui.media.controls.ui.controller.MediaHierarchyManager
 import com.android.systemui.media.controls.ui.view.MediaHost
 import com.android.systemui.settings.fakeUserTracker
+import com.android.systemui.shade.data.repository.fakeShadeRepository
 import com.android.systemui.shade.domain.interactor.shadeInteractor
 import com.android.systemui.smartspace.data.repository.FakeSmartspaceRepository
 import com.android.systemui.smartspace.data.repository.fakeSmartspaceRepository
@@ -59,6 +61,7 @@
 import com.google.common.truth.Truth.assertThat
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.test.advanceTimeBy
+import kotlinx.coroutines.test.runCurrent
 import kotlinx.coroutines.test.runTest
 import org.junit.Before
 import org.junit.Test
@@ -266,6 +269,26 @@
             assertThat(isPopupOnDismissCtaShowing).isEqualTo(false)
         }
 
+    @Test
+    fun canChangeScene_shadeNotExpanded() =
+        testScope.runTest {
+            // On keyguard without any shade expansion.
+            kosmos.fakeKeyguardRepository.setStatusBarState(StatusBarState.KEYGUARD)
+            kosmos.fakeShadeRepository.setLockscreenShadeExpansion(0f)
+            runCurrent()
+            assertThat(underTest.canChangeScene()).isTrue()
+        }
+
+    @Test
+    fun canChangeScene_shadeExpanded() =
+        testScope.runTest {
+            // On keyguard with shade fully expanded.
+            kosmos.fakeKeyguardRepository.setStatusBarState(StatusBarState.KEYGUARD)
+            kosmos.fakeShadeRepository.setLockscreenShadeExpansion(1f)
+            runCurrent()
+            assertThat(underTest.canChangeScene()).isFalse()
+        }
+
     private suspend fun setIsMainUser(isMainUser: Boolean) {
         whenever(user.isMain).thenReturn(isMainUser)
         userRepository.setUserInfos(listOf(user))
diff --git a/packages/SystemUI/src/com/android/systemui/communal/ui/viewmodel/CommunalViewModel.kt b/packages/SystemUI/src/com/android/systemui/communal/ui/viewmodel/CommunalViewModel.kt
index c73d738..f13b5759 100644
--- a/packages/SystemUI/src/com/android/systemui/communal/ui/viewmodel/CommunalViewModel.kt
+++ b/packages/SystemUI/src/com/android/systemui/communal/ui/viewmodel/CommunalViewModel.kt
@@ -30,7 +30,6 @@
 import com.android.systemui.media.controls.ui.view.MediaHostState
 import com.android.systemui.media.dagger.MediaModule
 import com.android.systemui.shade.domain.interactor.ShadeInteractor
-import com.android.systemui.util.kotlin.BooleanFlowOperators.not
 import javax.inject.Inject
 import javax.inject.Named
 import kotlinx.coroutines.CoroutineScope
@@ -57,7 +56,7 @@
     @Application private val scope: CoroutineScope,
     private val communalInteractor: CommunalInteractor,
     tutorialInteractor: CommunalTutorialInteractor,
-    shadeInteractor: ShadeInteractor,
+    private val shadeInteractor: ShadeInteractor,
     deviceEntryInteractor: DeviceEntryInteractor,
     @Named(MediaModule.COMMUNAL_HUB) mediaHost: MediaHost,
     @CommunalLog logBuffer: LogBuffer,
@@ -103,9 +102,6 @@
     val isEnableWorkProfileDialogShowing: Flow<Boolean> =
         _isEnableWorkProfileDialogShowing.asStateFlow()
 
-    /** Whether touches should be disabled in communal */
-    val touchesAllowed: Flow<Boolean> = not(shadeInteractor.isAnyFullyExpanded)
-
     val deviceUnlocked: Flow<Boolean> = deviceEntryInteractor.isUnlocked
 
     init {
@@ -192,6 +188,11 @@
         delayedHidePopupJob = null
     }
 
+    /** Whether we can transition to a new scene based on a user gesture. */
+    fun canChangeScene(): Boolean {
+        return !shadeInteractor.isAnyFullyExpanded.value
+    }
+
     companion object {
         const val POPUP_AUTO_HIDE_TIMEOUT_MS = 12000L
     }