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

Commit 3e663a92 authored by Pragya Bajoria's avatar Pragya Bajoria Committed by Android (Google) Code Review
Browse files

Merge "[WM Shell] Move `isDesktopModeShowing` to DesktopTasksController since...

Merge "[WM Shell] Move `isDesktopModeShowing` to DesktopTasksController since it is only used there." into main
parents 58eb0b24 ea1181ef
Loading
Loading
Loading
Loading
+14 −37
Original line number Diff line number Diff line
@@ -88,14 +88,17 @@ class DesktopModeTaskRepository {
    /** Add a [VisibleTasksListener] to be notified when freeform tasks are visible or not. */
    fun addVisibleTasksListener(visibleTasksListener: VisibleTasksListener, executor: Executor) {
        visibleTasksListeners[visibleTasksListener] = executor
        displayData.keyIterator().forEach { displayId ->
            val visibleTasksCount = getVisibleTaskCount(displayId)
        displayData.keyIterator().forEach {
            executor.execute {
                visibleTasksListener.onTasksVisibilityChanged(displayId, visibleTasksCount)
                visibleTasksListener.onTasksVisibilityChanged(it, visibleTaskCount(it))
            }
        }
    }

    /** Returns a list of all [DisplayData]. */
    private fun displayDataList(): Sequence<DisplayData> =
        displayData.valueIterator().asSequence()

    /**
     * Add a Consumer which will inform other classes of changes to exclusion regions for all
     * Desktop tasks.
@@ -208,37 +211,17 @@ class DesktopModeTaskRepository {
        return removed
    }

    /** Check if a task with the given [taskId] was marked as an active task */
    fun isActiveTask(taskId: Int): Boolean {
        return displayData.valueIterator().asSequence().any { data ->
            data.activeTasks.contains(taskId)
        }
    }

    /** Check if a task with the given [taskId] was marked as a closing task */
    fun isClosingTask(taskId: Int): Boolean =
        displayData.valueIterator().asSequence().any { data -> taskId in data.closingTasks }

    /** Whether a task is visible. */
    fun isVisibleTask(taskId: Int): Boolean {
        return displayData.valueIterator().asSequence().any { data ->
            data.visibleTasks.contains(taskId)
        }
    }

    /** Return whether the given Task is minimized. */
    fun isMinimizedTask(taskId: Int): Boolean {
        return displayData.valueIterator().asSequence().any { data ->
            data.minimizedTasks.contains(taskId)
        }
    }
    fun isActiveTask(taskId: Int) = displayDataList().any { taskId in it.activeTasks }
    fun isClosingTask(taskId: Int) = displayDataList().any { taskId in it.closingTasks }
    fun isVisibleTask(taskId: Int) = displayDataList().any { taskId in it.visibleTasks }
    fun isMinimizedTask(taskId: Int) = displayDataList().any { taskId in it.minimizedTasks }

    /**
     * Check if a task with the given [taskId] is the only visible, non-closing, not-minimized task
     * on its display
     */
    fun isOnlyVisibleNonClosingTask(taskId: Int): Boolean =
        displayData.valueIterator().asSequence().any { data ->
        displayDataList().any { data ->
            data.visibleTasks
                .subtract(data.closingTasks)
                .subtract(data.minimizedTasks)
@@ -254,12 +237,6 @@ class DesktopModeTaskRepository {
    fun getMinimizedTasks(displayId: Int): ArraySet<Int> =
        ArraySet(displayData[displayId]?.minimizedTasks)

    /**
     * Returns whether Desktop Mode is currently showing any tasks, i.e. whether any Desktop Tasks
     * are visible.
     */
    fun isDesktopModeShowing(displayId: Int): Boolean = getVisibleTaskCount(displayId) > 0

    /**
     * Returns a list of Tasks IDs representing all active non-minimized Tasks on the given display,
     * ordered from front to back.
@@ -305,14 +282,14 @@ class DesktopModeTaskRepository {
            return
        }

        val prevCount = getVisibleTaskCount(displayId)
        val prevCount = visibleTaskCount(displayId)
        if (visible) {
            displayData.getOrCreate(displayId).visibleTasks.add(taskId)
            unminimizeTask(displayId, taskId)
        } else {
            displayData[displayId]?.visibleTasks?.remove(taskId)
        }
        val newCount = getVisibleTaskCount(displayId)
        val newCount = visibleTaskCount(displayId)

        // Check if count changed
        if (prevCount != newCount) {
@@ -340,7 +317,7 @@ class DesktopModeTaskRepository {
    }

    /** Get number of tasks that are marked as visible on given [displayId] */
    fun getVisibleTaskCount(displayId: Int): Int {
    fun visibleTaskCount(displayId: Int): Int {
        ProtoLog.d(
            WM_SHELL_DESKTOP_MODE,
            "DesktopTaskRepo: visibleTaskCount= %d",
+11 −10
Original line number Diff line number Diff line
@@ -246,10 +246,12 @@ class DesktopTasksController(
        }
    }

    /** Get number of tasks that are marked as visible */
    fun getVisibleTaskCount(displayId: Int): Int {
        return desktopModeTaskRepository.getVisibleTaskCount(displayId)
    }
    /** Gets number of visible tasks in [displayId]. */
    fun visibleTaskCount(displayId: Int): Int =
        desktopModeTaskRepository.visibleTaskCount(displayId)

    /** Returns true if any tasks are visible in Desktop Mode. */
    fun isDesktopModeShowing(displayId: Int): Boolean = visibleTaskCount(displayId) > 0

    /** Enter desktop by using the focused task in given `displayId` */
    fun moveFocusedTaskToDesktop(displayId: Int, transitionSource: DesktopModeTransitionSource) {
@@ -981,7 +983,7 @@ class DesktopTasksController(
            ProtoLog.v(WM_SHELL_DESKTOP_MODE, "DesktopTasksController: skip keyguard is locked")
            return null
        }
        if (!desktopModeTaskRepository.isDesktopModeShowing(task.displayId)) {
        if (!isDesktopModeShowing(task.displayId)) {
            ProtoLog.d(
                WM_SHELL_DESKTOP_MODE,
                "DesktopTasksController: bring desktop tasks to front on transition" +
@@ -1012,7 +1014,7 @@ class DesktopTasksController(
        transition: IBinder
    ): WindowContainerTransaction? {
        ProtoLog.v(WM_SHELL_DESKTOP_MODE, "DesktopTasksController: handleFullscreenTaskLaunch")
        if (desktopModeTaskRepository.isDesktopModeShowing(task.displayId)) {
        if (isDesktopModeShowing(task.displayId)) {
            ProtoLog.d(
                WM_SHELL_DESKTOP_MODE,
                "DesktopTasksController: switch fullscreen task to freeform on transition" +
@@ -1396,8 +1398,7 @@ class DesktopTasksController(
        onFinishCallback: Consumer<Boolean>
    ): Boolean {
        // TODO(b/320797628): Pass through which display we are dropping onto
        val activeTasks = desktopModeTaskRepository.getActiveTasks(DEFAULT_DISPLAY)
        if (!activeTasks.any { desktopModeTaskRepository.isVisibleTask(it) }) {
        if (!isDesktopModeShowing(DEFAULT_DISPLAY)) {
            // Not currently in desktop mode, ignore the drop
            return false
        }
@@ -1556,8 +1557,8 @@ class DesktopTasksController(
            val result = IntArray(1)
            executeRemoteCallWithTaskPermission(
                controller,
                "getVisibleTaskCount",
                { controller -> result[0] = controller.getVisibleTaskCount(displayId) },
                "visibleTaskCount",
                { controller -> result[0] = controller.visibleTaskCount(displayId) },
                true /* blocking */
            )
            return result[0]
+1 −1
Original line number Diff line number Diff line
@@ -353,7 +353,7 @@ public class DefaultMixedHandler implements MixedTransitionHandler,
                return this::setRecentsTransitionDuringKeyguard;
            } else if (mDesktopTasksController != null
                    // Check on the default display. Recents/gesture nav is only available there
                    && mDesktopTasksController.getVisibleTaskCount(DEFAULT_DISPLAY) > 0) {
                    && mDesktopTasksController.visibleTaskCount(DEFAULT_DISPLAY) > 0) {
                return this::setRecentsTransitionDuringDesktop;
            }
        }
+21 −43
Original line number Diff line number Diff line
@@ -337,65 +337,65 @@ class DesktopModeTaskRepositoryTest : ShellTestCase() {
    }

    @Test
    fun getVisibleTaskCount() {
    fun visibleTaskCount_defaultDisplay_returnsCorrectCount() {
        // No tasks, count is 0
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)

        // New task increments count to 1
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 1, visible = true)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)

        // Visibility update to same task does not increase count
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 1, visible = true)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)

        // Second task visible increments count
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 2, visible = true)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(2)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(2)

        // Hiding a task decrements count
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 1, visible = false)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)

        // Hiding all tasks leaves count at 0
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 2, visible = false)
        assertThat(repo.getVisibleTaskCount(displayId = 9)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(displayId = 9)).isEqualTo(0)

        // Hiding a not existing task, count remains at 0
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 999, visible = false)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
    }

    @Test
    fun getVisibleTaskCount_multipleDisplays() {
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.getVisibleTaskCount(SECOND_DISPLAY)).isEqualTo(0)
    fun visibleTaskCount_multipleDisplays_returnsCorrectCount() {
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(SECOND_DISPLAY)).isEqualTo(0)

        // New task on default display increments count for that display only
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 1, visible = true)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
        assertThat(repo.getVisibleTaskCount(SECOND_DISPLAY)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
        assertThat(repo.visibleTaskCount(SECOND_DISPLAY)).isEqualTo(0)

        // New task on secondary display, increments count for that display only
        repo.updateVisibleFreeformTasks(SECOND_DISPLAY, taskId = 2, visible = true)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
        assertThat(repo.getVisibleTaskCount(SECOND_DISPLAY)).isEqualTo(1)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
        assertThat(repo.visibleTaskCount(SECOND_DISPLAY)).isEqualTo(1)

        // Marking task visible on another display, updates counts for both displays
        repo.updateVisibleFreeformTasks(SECOND_DISPLAY, taskId = 1, visible = true)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.getVisibleTaskCount(SECOND_DISPLAY)).isEqualTo(2)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(SECOND_DISPLAY)).isEqualTo(2)

        // Marking task that is on secondary display, hidden on default display, does not affect
        // secondary display
        repo.updateVisibleFreeformTasks(DEFAULT_DISPLAY, taskId = 1, visible = false)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.getVisibleTaskCount(SECOND_DISPLAY)).isEqualTo(2)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(SECOND_DISPLAY)).isEqualTo(2)

        // Hiding a task on that display, decrements count
        repo.updateVisibleFreeformTasks(SECOND_DISPLAY, taskId = 1, visible = false)
        assertThat(repo.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.getVisibleTaskCount(SECOND_DISPLAY)).isEqualTo(1)
        assertThat(repo.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
        assertThat(repo.visibleTaskCount(SECOND_DISPLAY)).isEqualTo(1)
    }

    @Test
@@ -493,28 +493,6 @@ class DesktopModeTaskRepositoryTest : ShellTestCase() {
        assertThat(repo.isMinimizedTask(taskId = 2)).isFalse()
    }

    @Test
    fun isDesktopModeShowing_noActiveTasks_returnsFalse() {
        assertThat(repo.isDesktopModeShowing(displayId = 0)).isFalse()
    }

    @Test
    fun isDesktopModeShowing_noTasksVisible_returnsFalse() {
        repo.addActiveTask(displayId = 0, taskId = 1)
        repo.addActiveTask(displayId = 0, taskId = 2)

        assertThat(repo.isDesktopModeShowing(displayId = 0)).isFalse()
    }

    @Test
    fun isDesktopModeShowing_tasksActiveAndVisible_returnsTrue() {
        repo.addActiveTask(displayId = 0, taskId = 1)
        repo.addActiveTask(displayId = 0, taskId = 2)
        repo.updateVisibleFreeformTasks(displayId = 0, taskId = 1, visible = true)

        assertThat(repo.isDesktopModeShowing(displayId = 0)).isTrue()
    }

    @Test
    fun getActiveNonMinimizedTasksOrderedFrontToBack_returnsFreeformTasksInCorrectOrder() {
        repo.addActiveTask(displayId = DEFAULT_DISPLAY, taskId = 1)
+33 −8
Original line number Diff line number Diff line
@@ -310,6 +310,31 @@ class DesktopTasksControllerTest : ShellTestCase() {
    wct.assertReorderAt(index = 2, task2)
  }

  @Test
  fun isDesktopModeShowing_noTasks_returnsFalse() {
    assertThat(controller.isDesktopModeShowing(displayId = 0)).isFalse()
  }

  @Test
  fun isDesktopModeShowing_noTasksVisible_returnsFalse() {
    val task1 = setUpFreeformTask()
    val task2 = setUpFreeformTask()
    markTaskHidden(task1)
    markTaskHidden(task2)

    assertThat(controller.isDesktopModeShowing(displayId = 0)).isFalse()
  }

  @Test
  fun isDesktopModeShowing_tasksActiveAndVisible_returnsTrue() {
    val task1 = setUpFreeformTask()
    val task2 = setUpFreeformTask()
    markTaskVisible(task1)
    markTaskHidden(task2)

    assertThat(controller.isDesktopModeShowing(displayId = 0)).isTrue()
  }

  @Test
  @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY)
  fun showDesktopApps_onSecondaryDisplay_desktopWallpaperEnabled_shouldNotShowWallpaper() {
@@ -526,32 +551,32 @@ class DesktopTasksControllerTest : ShellTestCase() {
  }

  @Test
  fun getVisibleTaskCount_noTasks_returnsZero() {
    assertThat(controller.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
  fun visibleTaskCount_noTasks_returnsZero() {
    assertThat(controller.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(0)
  }

  @Test
  fun getVisibleTaskCount_twoTasks_bothVisible_returnsTwo() {
  fun visibleTaskCount_twoTasks_bothVisible_returnsTwo() {
    setUpHomeTask()
    setUpFreeformTask().also(::markTaskVisible)
    setUpFreeformTask().also(::markTaskVisible)
    assertThat(controller.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(2)
    assertThat(controller.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(2)
  }

  @Test
  fun getVisibleTaskCount_twoTasks_oneVisible_returnsOne() {
  fun visibleTaskCount_twoTasks_oneVisible_returnsOne() {
    setUpHomeTask()
    setUpFreeformTask().also(::markTaskVisible)
    setUpFreeformTask().also(::markTaskHidden)
    assertThat(controller.getVisibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
    assertThat(controller.visibleTaskCount(DEFAULT_DISPLAY)).isEqualTo(1)
  }

  @Test
  fun getVisibleTaskCount_twoTasksVisibleOnDifferentDisplays_returnsOne() {
  fun visibleTaskCount_twoTasksVisibleOnDifferentDisplays_returnsOne() {
    setUpHomeTask()
    setUpFreeformTask(DEFAULT_DISPLAY).also(::markTaskVisible)
    setUpFreeformTask(SECOND_DISPLAY).also(::markTaskVisible)
    assertThat(controller.getVisibleTaskCount(SECOND_DISPLAY)).isEqualTo(1)
    assertThat(controller.visibleTaskCount(SECOND_DISPLAY)).isEqualTo(1)
  }

  @Test