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

Commit f875abfb authored by Jorge Gil's avatar Jorge Gil
Browse files

[18/N] Desks: Deactive desk on core-started task close

Deactivates the desk when handling a transition to close a task and
we're about to exit the deskt (such as when it is the last task in the
desk).

Flag: com.android.window.flags.enable_multiple_desktops_backend
Bug: 394268248
Test: open 1 app in the desk, then close with "adb shell am force-stop
<pkg-name>" to simulate core-started close. Verify we exit the desktop
properly: goes to Home, new launches launch in fullscreen and
DesksOrganizer dump shows deactivated desk.

Change-Id: I0d636306a4421c399051313660d6fb1c909100ce
parent 3e523f55
Loading
Loading
Loading
Loading
+10 −17
Original line number Diff line number Diff line
@@ -314,29 +314,22 @@ class DesktopRepository(
        return false
    }

    /**
     * Adds given task to the closing task list for [displayId]'s active desk.
     *
     * TODO: b/389960283 - add explicit [deskId] argument.
     */
    fun addClosingTask(displayId: Int, taskId: Int) {
        val activeDesk =
            desktopData.getActiveDesk(displayId)
                ?: error("Expected active desk in display: $displayId")
        if (activeDesk.closingTasks.add(taskId)) {
            logD(
                "Added closing task=%d displayId=%d deskId=%d",
                taskId,
                displayId,
                activeDesk.deskId,
            )
    /** Adds given task to the closing task list of its desk. */
    fun addClosingTask(displayId: Int, deskId: Int?, taskId: Int) {
        val desk =
            deskId?.let { desktopData.getDesk(it) }
                ?: checkNotNull(desktopData.getActiveDesk(displayId)) {
                    "Expected active desk in display: $displayId"
                }
        if (desk.closingTasks.add(taskId)) {
            logD("Added closing task=%d displayId=%d deskId=%d", taskId, displayId, desk.deskId)
        } else {
            // If the task hasn't been removed from closing list after it disappeared.
            logW(
                "Task with taskId=%d displayId=%d deskId=%d is already closing",
                taskId,
                displayId,
                activeDesk.deskId,
                desk.deskId,
            )
        }
    }
+31 −15
Original line number Diff line number Diff line
@@ -801,6 +801,9 @@ class DesktopTasksController(
    ): ((IBinder) -> Unit) {
        val taskId = taskInfo.taskId
        val deskId = taskRepository.getDeskIdForTask(taskInfo.taskId)
        if (deskId == null && DesktopExperienceFlags.ENABLE_MULTIPLE_DESKTOPS_BACKEND.isTrue) {
            error("Did not find desk for task: $taskId")
        }
        snapEventHandler.removeTaskIfTiled(displayId, taskId)
        val shouldExitDesktop =
            willExitDesktop(
@@ -818,7 +821,7 @@ class DesktopTasksController(
                shouldEndUpAtHome = true,
            )

        taskRepository.addClosingTask(displayId, taskId)
        taskRepository.addClosingTask(displayId = displayId, deskId = deskId, taskId = taskId)
        taskbarDesktopTaskListener?.onTaskbarCornerRoundingUpdate(
            doesAnyTaskRequireTaskbarRounding(displayId, taskId)
        )
@@ -1251,9 +1254,9 @@ class DesktopTasksController(
        //  home.
        if (Flags.enablePerDisplayDesktopWallpaperActivity()) {
            performDesktopExitCleanupIfNeeded(
                task.taskId,
                task.displayId,
                wct,
                taskId = task.taskId,
                displayId = task.displayId,
                wct = wct,
                forceToFullscreen = false,
                // TODO: b/371096166 - Temporary turing home relaunch off to prevent home stealing
                // display focus. Remove shouldEndUpAtHome = false when home focus handling
@@ -1820,6 +1823,7 @@ class DesktopTasksController(

    private fun performDesktopExitCleanupIfNeeded(
        taskId: Int,
        deskId: Int? = null,
        displayId: Int,
        wct: WindowContainerTransaction,
        forceToFullscreen: Boolean,
@@ -1833,13 +1837,14 @@ class DesktopTasksController(
        //  |RunOnTransitStart| when the transition is started.
        return performDesktopExitCleanUp(
            wct = wct,
            deskId = null,
            deskId = deskId,
            displayId = displayId,
            willExitDesktop = true,
            shouldEndUpAtHome = shouldEndUpAtHome,
        )
    }

    /** TODO: b/394268248 - update [deskId] to be non-null. */
    private fun performDesktopExitCleanUp(
        wct: WindowContainerTransaction,
        deskId: Int?,
@@ -2390,17 +2395,28 @@ class DesktopTasksController(
    ): WindowContainerTransaction? {
        logV("handleTaskClosing")
        if (!isDesktopModeShowing(task.displayId)) return null
        val deskId = taskRepository.getDeskIdForTask(task.taskId)
        if (deskId == null && DesktopExperienceFlags.ENABLE_MULTIPLE_DESKTOPS_BACKEND.isTrue) {
            return null
        }

        val wct = WindowContainerTransaction()
        val deactivationRunnable =
            performDesktopExitCleanupIfNeeded(
            task.taskId,
            task.displayId,
            wct,
                taskId = task.taskId,
                deskId = deskId,
                displayId = task.displayId,
                wct = wct,
                forceToFullscreen = false,
            )
        deactivationRunnable?.invoke(transition)

        if (!DesktopModeFlags.ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION.isTrue()) {
            taskRepository.addClosingTask(task.displayId, task.taskId)
            taskRepository.addClosingTask(
                displayId = task.displayId,
                deskId = deskId,
                taskId = task.taskId,
            )
            snapEventHandler.removeTaskIfTiled(task.displayId, task.taskId)
        }

@@ -2604,9 +2620,9 @@ class DesktopTasksController(
        wct.setDensityDpi(taskInfo.token, getDefaultDensityDpi())

        performDesktopExitCleanupIfNeeded(
            taskInfo.taskId,
            taskInfo.displayId,
            wct,
            taskId = taskInfo.taskId,
            displayId = taskInfo.displayId,
            wct = wct,
            forceToFullscreen = false,
            shouldEndUpAtHome = false,
        )
+1 −1
Original line number Diff line number Diff line
@@ -333,7 +333,7 @@ class DesktopRepositoryTest(flags: FlagsParameterization) : ShellTestCase() {
    @Test
    fun isOnlyVisibleNonClosingTask_singleVisibleClosingTask() {
        repo.updateTask(DEFAULT_DISPLAY, taskId = 1, isVisible = true)
        repo.addClosingTask(DEFAULT_DISPLAY, 1)
        repo.addClosingTask(displayId = DEFAULT_DISPLAY, deskId = 0, taskId = 1)

        // A visible task that's closing
        assertThat(repo.isVisibleTask(1)).isTrue()
+46 −4
Original line number Diff line number Diff line
@@ -2827,7 +2827,7 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
    fun onDesktopWindowClose_singleActiveTask_isClosing() {
        val task = setUpFreeformTask()

        taskRepository.addClosingTask(DEFAULT_DISPLAY, task.taskId)
        taskRepository.addClosingTask(displayId = DEFAULT_DISPLAY, deskId = 0, taskId = task.taskId)

        val wct = WindowContainerTransaction()
        controller.onDesktopWindowClose(wct, displayId = DEFAULT_DISPLAY, task)
@@ -2864,7 +2864,11 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        val task1 = setUpFreeformTask()
        val task2 = setUpFreeformTask()

        taskRepository.addClosingTask(DEFAULT_DISPLAY, task2.taskId)
        taskRepository.addClosingTask(
            displayId = DEFAULT_DISPLAY,
            deskId = 0,
            taskId = task2.taskId,
        )

        val wct = WindowContainerTransaction()
        controller.onDesktopWindowClose(wct, displayId = DEFAULT_DISPLAY, task1)
@@ -3996,7 +4000,11 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        val task1 = setUpFreeformTask(displayId = DEFAULT_DISPLAY)
        val task2 = setUpFreeformTask(displayId = DEFAULT_DISPLAY)

        taskRepository.addClosingTask(displayId = DEFAULT_DISPLAY, taskId = task2.taskId)
        taskRepository.addClosingTask(
            displayId = DEFAULT_DISPLAY,
            deskId = 0,
            taskId = task2.taskId,
        )
        val result =
            controller.handleRequest(Binder(), createTransition(task1, type = TRANSIT_TO_BACK))

@@ -4106,6 +4114,36 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
            .assertReorderAt(index = 0, wallpaperToken, toTop = false)
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY,
        Flags.FLAG_ENABLE_DESKTOP_WALLPAPER_ACTIVITY_FOR_SYSTEM_USER,
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
    )
    fun handleRequest_closeTransition_onlyDesktopTask_deactivatesDesk() {
        val task = setUpFreeformTask()

        controller.handleRequest(Binder(), createTransition(task, type = TRANSIT_CLOSE))

        verify(desksOrganizer).deactivateDesk(any(), /* deskId= */ eq(0))
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY,
        Flags.FLAG_ENABLE_DESKTOP_WALLPAPER_ACTIVITY_FOR_SYSTEM_USER,
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
    )
    fun handleRequest_closeTransition_onlyDesktopTask_addsDeactivatesDeskTransition() {
        val transition = Binder()
        val task = setUpFreeformTask()

        controller.handleRequest(transition, createTransition(task, type = TRANSIT_CLOSE))

        verify(desksTransitionsObserver)
            .addPendingTransition(DeskTransition.DeactivateDesk(token = transition, deskId = 0))
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY)
    fun handleRequest_closeTransition_multipleTasks_noWallpaper_doesNotHandle() {
@@ -4139,7 +4177,11 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        val task1 = setUpFreeformTask(displayId = DEFAULT_DISPLAY)
        val task2 = setUpFreeformTask(displayId = DEFAULT_DISPLAY)

        taskRepository.addClosingTask(displayId = DEFAULT_DISPLAY, taskId = task2.taskId)
        taskRepository.addClosingTask(
            displayId = DEFAULT_DISPLAY,
            deskId = 0,
            taskId = task2.taskId,
        )
        val result =
            controller.handleRequest(Binder(), createTransition(task1, type = TRANSIT_CLOSE))