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

Commit 5dc10c1f authored by Jorge Gil's avatar Jorge Gil
Browse files

[14/N] Desks: Deactivate desk when last window is closed

When the last task closes, the desk is now deactivated to prevent future
app launches from launching in freeform.

Also fixes:
-One log that had desk/task ids inverted
-A missing early return when moving a running task to front, which was
 also producing an incorrect log

Flag: com.android.window.flags.enable_multiple_desktops_backend
Bug: 394268248
Test: open fullscren app, enter desktop, close the window using X button
  1) Verify window closes
  2) Home launches
  3) DesktopRepository dump shows
    A) activeDeskId=null
    B) closed window no longer inside the desk
  4) Launching a new window from taskbar launches fullscreen, not
     freeform in a tablet
Change-Id: I2b51e3abbea8650397000657c734049418607b97
parent 88568848
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -821,7 +821,7 @@ class DesktopRepository(

    /** Removes the given task from the given desk. */
    fun removeTaskFromDesk(deskId: Int, taskId: Int) {
        logD("removeTaskFromDesk: deskId=%d, taskId=%d", taskId, deskId)
        logD("removeTaskFromDesk: deskId=%d, taskId=%d", deskId, taskId)
        // TODO: b/362720497 - consider not clearing bounds on any removal, such as when moving
        //  it between desks. It might be better to allow restoring to the previous bounds as long
        //  as they're valid (probably valid if in the same display).
+49 −15
Original line number Diff line number Diff line
@@ -481,7 +481,7 @@ class DesktopTasksController(
    ): Boolean {
        val runningTask = shellTaskOrganizer.getRunningTaskInfo(taskId)
        if (runningTask != null) {
            moveRunningTaskToDesk(
            return moveRunningTaskToDesk(
                task = runningTask,
                deskId = deskId,
                wct = wct,
@@ -563,10 +563,10 @@ class DesktopTasksController(
        transitionSource: DesktopModeTransitionSource,
        remoteTransition: RemoteTransition? = null,
        callback: IMoveToDesktopCallback? = null,
    ) {
    ): Boolean {
        if (desktopModeCompatPolicy.isTopActivityExemptFromDesktopWindowing(task)) {
            logW("Cannot enter desktop for taskId %d, ineligible top activity found", task.taskId)
            return
            return false
        }
        val displayId = taskRepository.getDisplayForDesk(deskId)
        logV(
@@ -621,6 +621,7 @@ class DesktopTasksController(
        } else {
            taskRepository.setActiveDesk(displayId = displayId, deskId = deskId)
        }
        return true
    }

    /**
@@ -789,17 +790,46 @@ class DesktopTasksController(
        wct: WindowContainerTransaction,
        displayId: Int,
        taskInfo: RunningTaskInfo,
    ): ((IBinder) -> Unit)? {
    ): ((IBinder) -> Unit) {
        val taskId = taskInfo.taskId
        snapEventHandler.removeTaskIfTiled(displayId, taskId)
        // TODO: b/394268248 - desk needs to be deactivated when closing the last task and going
        //  home.
        performDesktopExitCleanupIfNeeded(taskId, displayId, wct, forceToFullscreen = false)
        val shouldExitDesktop =
            willExitDesktop(
                triggerTaskId = taskInfo.taskId,
                displayId = displayId,
                forceToFullscreen = false,
            )
        taskRepository.setPipShouldKeepDesktopActive(displayId, keepActive = true)
        // TODO: b/393978539 - Deactivation should not happen in desktop-first devices.
        val deactivatingDeskId =
            if (shouldExitDesktop) {
                performDesktopExitCleanUp(wct, displayId, shouldEndUpAtHome = true)
                val deskId = taskRepository.getDeskIdForTask(taskInfo.taskId)
                if (
                    DesktopExperienceFlags.ENABLE_MULTIPLE_DESKTOPS_BACKEND.isTrue && deskId != null
                ) {
                    desksOrganizer.deactivateDesk(wct, deskId)
                }
                deskId
            } else {
                null
            }
        val deskDeactivationRunnable =
            deactivatingDeskId?.let { deskId ->
                { transition: IBinder ->
                    desksTransitionObserver.addPendingTransition(
                        DeskTransition.DeactivateDesk(token = transition, deskId = deskId)
                    )
                }
            }

        taskRepository.addClosingTask(displayId, taskId)
        taskbarDesktopTaskListener?.onTaskbarCornerRoundingUpdate(
            doesAnyTaskRequireTaskbarRounding(displayId, taskId)
        )
        return desktopImmersiveController

        val immersiveRunnable =
            desktopImmersiveController
                .exitImmersiveIfApplicable(
                    wct = wct,
                    taskInfo = taskInfo,
@@ -807,6 +837,10 @@ class DesktopTasksController(
                )
                .asExit()
                ?.runOnTransitionStart
        return { transitionToken ->
            immersiveRunnable?.invoke(transitionToken)
            deskDeactivationRunnable?.invoke(transitionToken)
        }
    }

    fun minimizeTask(taskInfo: RunningTaskInfo, minimizeReason: MinimizeReason) {
+1 −1
Original line number Diff line number Diff line
@@ -984,7 +984,7 @@ public class DesktopModeWindowDecorViewModel implements WindowDecorViewModel,
                            mDesktopTasksController.onDesktopWindowClose(
                                    wct, mDisplayId, decoration.mTaskInfo);
                    final IBinder transition = mTaskOperations.closeTask(mTaskToken, wct);
                    if (transition != null && runOnTransitionStart != null) {
                    if (transition != null) {
                        runOnTransitionStart.invoke(transition);
                    }
                }
+26 −0
Original line number Diff line number Diff line
@@ -2921,6 +2921,32 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        }
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun onDesktopWindowClose_lastWindow_deactivatesDesk() {
        val task = setUpFreeformTask()
        val wct = WindowContainerTransaction()

        controller.onDesktopWindowClose(wct, displayId = DEFAULT_DISPLAY, task)

        verify(desksOrganizer).deactivateDesk(wct, deskId = 0)
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun onDesktopWindowClose_lastWindow_addsPendingDeactivateTransition() {
        val task = setUpFreeformTask()
        val wct = WindowContainerTransaction()

        val transition = Binder()
        val runOnTransitStart =
            controller.onDesktopWindowClose(wct, displayId = DEFAULT_DISPLAY, task)
        runOnTransitStart(transition)

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

    @Test
    fun onDesktopWindowMinimize_noActiveTask_doesntRemoveWallpaper() {
        val task = setUpFreeformTask(active = false)