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

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

[33/N] Desks: Replace #bringDesktopAppsToFront usages

Replaces existing #bringDesktopAppsToFront callers with the multi-desk
compatible #addDeskActivationChanges.

This is mostly a refactor, except in:
1) #handleFreeformTaskLaunch, where prior to this change the return
   value (the minimized task) was not added as a pending minimized
   change even though the minimization WCT changes were applied.
   See Ibc697a4b0dff6a0b1583018d802b5caa66814220 where it was first
   added.
2) #handleFullscreenTaskLaunch, where prior to this change the
   task-limit logic could run twice: once in bringDesktopAppsToFront
   (without saving the pending minimized change) and another time
   through addAndGetMinimizedChanges (with saving of the pending
   minimized change).
   See Ia3419f2924e05f8f4cb607678f74c048bb888377 where it was first
   added.

Flag: com.android.window.flags.enable_multiple_desktops_backend
Bug: 362720497
Test: atest WMShellUnitTests
Change-Id: I49f46058c121d25f345860412336652939d5e3b1
parent 0d3ebe80
Loading
Loading
Loading
Loading
+61 −27
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.app.ActivityManager.RunningTaskInfo
import android.app.ActivityOptions
import android.app.KeyguardManager
import android.app.PendingIntent
import android.app.TaskInfo
import android.app.WindowConfiguration.ACTIVITY_TYPE_HOME
import android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD
import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM
@@ -533,7 +534,8 @@ class DesktopTasksController(
            return false
        }
        logV("moveBackgroundTaskToDesktop with taskId=%d", taskId)
        val taskIdToMinimize = bringDesktopAppsToFront(task.displayId, wct, taskId)
        val deskId = getDefaultDeskId(task.displayId)
        val runOnTransitStart = addDeskActivationChanges(deskId, wct, task)
        val exitResult =
            desktopImmersiveController.exitImmersiveIfApplicable(
                wct = wct,
@@ -562,9 +564,7 @@ class DesktopTasksController(
        desktopModeEnterExitTransitionListener?.onEnterDesktopModeTransitionStarted(
            FREEFORM_ANIMATION_DURATION
        )
        taskIdToMinimize?.let {
            addPendingMinimizeTransition(transition, it, MinimizeReason.TASK_LIMIT)
        }
        runOnTransitStart?.invoke(transition)
        exitResult.asExit()?.runOnTransitionStart?.invoke(transition)
        return true
    }
@@ -1638,7 +1638,10 @@ class DesktopTasksController(
        }
    }

    @Deprecated("Use activeDesk() instead.", ReplaceWith("activateDesk()"))
    @Deprecated(
        "Use addDeskActivationChanges() instead.",
        ReplaceWith("addDeskActivationChanges()"),
    )
    private fun bringDesktopAppsToFront(
        displayId: Int,
        wct: WindowContainerTransaction,
@@ -2262,7 +2265,9 @@ class DesktopTasksController(
                runOnTransitStart?.invoke(transition)
                return wct
            }
            bringDesktopAppsToFront(task.displayId, wct, task.taskId)
            val deskId = getDefaultDeskId(task.displayId)
            val runOnTransitStart = addDeskActivationChanges(deskId, wct, task)
            runOnTransitStart?.invoke(transition)
            wct.reorder(task.token, true)
            return wct
        }
@@ -2325,25 +2330,42 @@ class DesktopTasksController(
            return WindowContainerTransaction().also { wct ->
                val deskId = getDefaultDeskId(task.displayId)
                addMoveToDeskTaskChanges(wct = wct, task = task, deskId = deskId)
                // In some launches home task is moved behind new task being launched. Make sure
                // that's not the case for launches in desktop. Also, if this launch is the first
                // one to trigger the desktop mode (e.g., when [forceEnterDesktop()]), activate the
                // desktop mode here.
                val runOnTransitStart: RunOnTransitStart? =
                    if (
                        task.baseIntent.flags.and(Intent.FLAG_ACTIVITY_TASK_ON_HOME) != 0 ||
                            !isDesktopModeShowing(task.displayId)
                    ) {
                    bringDesktopAppsToFront(task.displayId, wct, task.taskId)
                        // In some launches home task is moved behind new task being launched. Make
                        // sure that's not the case for launches in desktop. Also, if this launch is
                        // the first one to trigger the desktop mode (e.g., when
                        // [forceEnterDesktop()]), activate the desk here.
                        val activationRunnable =
                            addDeskActivationChanges(
                                deskId = deskId,
                                wct = wct,
                                newTask = task,
                                addPendingLaunchTransition = true,
                            )
                        wct.reorder(task.token, true)
                        activationRunnable
                    } else {
                        { transition: IBinder ->
                            // The desk was already showing and we're launching a new Task - we
                            // might need to minimize another Task.
                            val taskIdToMinimize =
                                addAndGetMinimizeChanges(task.displayId, wct, task.taskId)
                            taskIdToMinimize?.let { minimizingTaskId ->
                                addPendingMinimizeTransition(
                                    transition,
                                    minimizingTaskId,
                                    MinimizeReason.TASK_LIMIT,
                                )
                            }

                // Desktop Mode is already showing and we're launching a new Task - we might need to
                // minimize another Task.
                val taskIdToMinimize = addAndGetMinimizeChanges(task.displayId, wct, task.taskId)
                taskIdToMinimize?.let {
                    addPendingMinimizeTransition(transition, it, MinimizeReason.TASK_LIMIT)
                }
                            // Also track the pending launching task.
                            addPendingAppLaunchTransition(transition, task.taskId, taskIdToMinimize)
                        }
                    }
                runOnTransitStart?.invoke(transition)
                desktopImmersiveController.exitImmersiveIfApplicable(
                    transition,
                    wct,
@@ -2739,11 +2761,20 @@ class DesktopTasksController(
        activateDesk(deskId, remoteTransition)
    }

    /** Activates the given desk but without starting a transition. */
    /**
     * Applies the necessary [wct] changes to activate the given desk.
     *
     * When a task is being brought into a desk together with the activation, then [newTask] is not
     * null and may be used to run other desktop policies, such as minimizing another task if the
     * task limit has been exceeded.
     */
    fun addDeskActivationChanges(
        deskId: Int,
        wct: WindowContainerTransaction,
        newTask: RunningTaskInfo? = null,
        newTask: TaskInfo? = null,
        // TODO: b/362720497 - should this be true in other places? Can it be calculated locally
        //  without having to specify the value?
        addPendingLaunchTransition: Boolean = false,
    ): RunOnTransitStart? {
        logV("addDeskActivationChanges newTaskId=%d deskId=%d", newTask?.taskId, deskId)
        val newTaskIdInFront = newTask?.taskId
@@ -2758,6 +2789,9 @@ class DesktopTasksController(
                        minimizeReason = MinimizeReason.TASK_LIMIT,
                    )
                }
                if (newTask != null && addPendingLaunchTransition) {
                    addPendingAppLaunchTransition(transition, newTask.taskId, taskIdToMinimize)
                }
            }
        }
        prepareForDeskActivation(displayId, wct)
+16 −4
Original line number Diff line number Diff line
@@ -3608,9 +3608,15 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        val wct = controller.handleRequest(Binder(), createTransition(fullscreenTask))

        // Make sure we reorder the new task to top, and the back task to the bottom
        assertThat(wct!!.hierarchyOps.size).isEqualTo(9)
        assertThat(wct!!.hierarchyOps.size).isEqualTo(8)
        wct.assertReorderAt(0, fullscreenTask, toTop = true)
        wct.assertReorderAt(8, freeformTasks[0], toTop = false)
        // Oldest task that needs to minimized is never reordered to top over Home.
        val taskToMinimize = freeformTasks[0]
        wct.assertWithoutHop { hop ->
            hop.container == taskToMinimize.token &&
                hop.type == HIERARCHY_OP_TYPE_REORDER &&
                hop.toTop == true
        }
    }

    @Test
@@ -3625,12 +3631,18 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()

        val wct = controller.handleRequest(Binder(), createTransition(fullscreenTask))

        assertThat(wct!!.hierarchyOps.size).isEqualTo(10)
        assertThat(wct!!.hierarchyOps.size).isEqualTo(9)
        wct.assertReorderAt(0, fullscreenTask, toTop = true)
        // Make sure we reorder the home task to the top, desktop tasks to top of them and minimized
        // task is under the home task.
        wct.assertReorderAt(1, homeTask, toTop = true)
        wct.assertReorderAt(9, freeformTasks[0], toTop = false)
        // Oldest task that needs to minimized is never reordered to top over Home.
        val taskToMinimize = freeformTasks[0]
        wct.assertWithoutHop { hop ->
            hop.container == taskToMinimize.token &&
                hop.type == HIERARCHY_OP_TYPE_REORDER &&
                hop.toTop == true
        }
    }

    @Test