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

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

Desks: Deactivate active desk on Home task launch

Launching Home should close the active desktop session. When multiple
desks are enabled, this means the desk needs to be deactivated.

This fixes an issue where in CTS (where each test might launch Home
using adb) a prior test's desktop session might stay active causing
other tests to fail.

Flag: com.android.window.flags.enable_multiple_desktops_backend
Bug: 407602007
Test: atest ActivityLifecycleFreeformTests
Change-Id: I4d9c151b1f86c2907c78f32a26f3cd7dd4927e30
parent 64d0e9e2
Loading
Loading
Loading
Loading
+39 −7
Original line number Diff line number Diff line
@@ -477,7 +477,8 @@ class DesktopTasksController(
                displayId = DEFAULT_DISPLAY,
                willExitDesktop = true,
                shouldEndUpAtHome = true,
                fromRecentsTransition = true,
                // No need to clean up the wallpaper / home when coming from a recents transition.
                skipWallpaperAndHomeOrdering = true,
            )
        runOnTransitStart?.invoke(transition)
    }
@@ -2084,16 +2085,15 @@ class DesktopTasksController(
        displayId: Int,
        willExitDesktop: Boolean,
        shouldEndUpAtHome: Boolean = true,
        fromRecentsTransition: Boolean = false,
        skipWallpaperAndHomeOrdering: Boolean = false,
    ): RunOnTransitStart? {
        if (!willExitDesktop) return null
        desktopModeEnterExitTransitionListener?.onExitDesktopModeTransitionStarted(
            FULLSCREEN_ANIMATION_DURATION,
            shouldEndUpAtHome,
        )
        // No need to clean up the wallpaper / reorder home when coming from a recents transition.
        if (
            !fromRecentsTransition ||
            !skipWallpaperAndHomeOrdering ||
                !DesktopExperienceFlags.ENABLE_MULTIPLE_DESKTOPS_BACKEND.isTrue
        ) {
            removeWallpaperActivity(wct, displayId)
@@ -2173,8 +2173,15 @@ class DesktopTasksController(
                    reason = "transition type not handled (${request.type})"
                    false
                }
                // Only handle standard type tasks
                triggerTask.activityType != ACTIVITY_TYPE_STANDARD -> {
                // Home launches are only handled with multiple desktops enabled.
                triggerTask.activityType == ACTIVITY_TYPE_HOME &&
                    !DesktopExperienceFlags.ENABLE_MULTIPLE_DESKTOPS_BACKEND.isTrue -> {
                    reason = "ACTIVITY_TYPE_HOME not handled"
                    false
                }
                // Only handle standard and home tasks types.
                triggerTask.activityType != ACTIVITY_TYPE_STANDARD &&
                    triggerTask.activityType != ACTIVITY_TYPE_HOME -> {
                    reason = "activityType not handled (${triggerTask.activityType})"
                    false
                }
@@ -2194,6 +2201,8 @@ class DesktopTasksController(

        val result =
            when {
                triggerTask.activityType == ACTIVITY_TYPE_HOME ->
                    handleHomeTaskLaunch(triggerTask, transition)
                // Check if freeform task launch during recents should be handled
                shouldHandleMidRecentsFreeformLaunch ->
                    handleMidRecentsFreeformTaskLaunch(triggerTask, transition)
@@ -2309,7 +2318,8 @@ class DesktopTasksController(
    private fun shouldHandleTaskClosing(request: TransitionRequestInfo): Boolean =
        ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY.isTrue() &&
            TransitionUtil.isClosingType(request.type) &&
            request.triggerTask != null
            request.triggerTask != null &&
            request.triggerTask?.activityType != ACTIVITY_TYPE_HOME

    /** Open an existing instance of an app. */
    fun openInstance(callingTask: RunningTaskInfo, requestedTaskId: Int) {
@@ -2440,6 +2450,28 @@ class DesktopTasksController(
        }
    }

    private fun handleHomeTaskLaunch(
        task: RunningTaskInfo,
        transition: IBinder,
    ): WindowContainerTransaction? {
        logV("DesktopTasksController: handleHomeTaskLaunch")
        val activeDeskId = taskRepository.getActiveDeskId(task.displayId) ?: return null
        val wct = WindowContainerTransaction()
        // TODO: b/393978539 - desktop-first displays may need to keep the desk active.
        val runOnTransitStart =
            performDesktopExitCleanUp(
                wct = wct,
                deskId = activeDeskId,
                displayId = task.displayId,
                willExitDesktop = true,
                shouldEndUpAtHome = true,
                // No need to clean up the wallpaper / home order if Home is launching directly.
                skipWallpaperAndHomeOrdering = true,
            )
        runOnTransitStart?.invoke(transition)
        return wct
    }

    /**
     * Handles the case where a freeform task is launched from recents.
     *
+38 −0
Original line number Diff line number Diff line
@@ -7556,6 +7556,44 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
            .exitImmersiveIfApplicable(eq(binder), any(), eq(task.displayId), any())
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun handleRequest_homeTask_notHandled() {
        val home = createHomeTask(DEFAULT_DISPLAY)

        val transition = Binder()
        val result = controller.handleRequest(transition, createTransition(home))

        assertNull(result)
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun handleRequest_homeTask_activeDesk_deactivates() {
        taskRepository.setActiveDesk(DEFAULT_DISPLAY, deskId = 0)
        val home = createHomeTask(DEFAULT_DISPLAY)

        val transition = Binder()
        val result = controller.handleRequest(transition, createTransition(home))

        assertNotNull(result)
        verify(desksOrganizer).deactivateDesk(result, deskId = 0)
        verify(desksTransitionsObserver)
            .addPendingTransition(DeskTransition.DeactivateDesk(token = transition, deskId = 0))
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun handleRequest_homeTask_closing_notHandled() {
        taskRepository.setActiveDesk(DEFAULT_DISPLAY, deskId = 0)
        val home = createHomeTask(DEFAULT_DISPLAY)

        val transition = Binder()
        val result = controller.handleRequest(transition, createTransition(home, TRANSIT_CLOSE))

        assertNull(result)
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_FULLY_IMMERSIVE_IN_DESKTOP)
    fun shouldPlayDesktopAnimation_notShowingDesktop_doesNotPlay() {