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

Commit c6f01172 authored by Suhua Lei's avatar Suhua Lei
Browse files

Fix crash of moving app to desk

Bug:407576369

Flag: com.android.window.flags.enable_multiple_desktops_frontend
Flag: com.android.window.flags.enable_multiple_desktops_backend
Test: move app to desk via its icon menu
Change-Id: I2419bc63e199f0c8aaaa907697b035f5d4b6eb61
parent 8da82965
Loading
Loading
Loading
Loading
+15 −4
Original line number Diff line number Diff line
@@ -639,6 +639,15 @@ class DesktopTasksController(
        return runOnTransitStart
    }

    private fun getDisplayIdForTaskOrDefault(task: TaskInfo): Int {
        return when {
            task.displayId != INVALID_DISPLAY -> task.displayId
            focusTransitionObserver.globallyFocusedDisplayId != INVALID_DISPLAY ->
                focusTransitionObserver.globallyFocusedDisplayId
            else -> DEFAULT_DISPLAY
        }
    }

    /** Moves task to desktop mode if task is running, else launches it in desktop mode. */
    @JvmOverloads
    fun moveTaskToDefaultDeskAndActivate(
@@ -655,7 +664,8 @@ class DesktopTasksController(
            logW("moveTaskToDefaultDeskAndActivate taskId=%d not found", taskId)
            return false
        }
        val deskId = getOrCreateDefaultDeskId(task.displayId) ?: return false
        val displayId = getDisplayIdForTaskOrDefault(task)
        val deskId = getOrCreateDefaultDeskId(displayId) ?: return false
        return moveTaskToDesk(
            taskId = taskId,
            deskId = deskId,
@@ -688,9 +698,9 @@ class DesktopTasksController(
        }
        val backgroundTask = recentTasksController?.findTaskInBackground(taskId)
        if (backgroundTask != null) {
            // TODO: b/391484662 - add support for |deskId|.
            return moveBackgroundTaskToDesktop(
                taskId,
                deskId,
                wct,
                transitionSource,
                remoteTransition,
@@ -703,6 +713,7 @@ class DesktopTasksController(

    private fun moveBackgroundTaskToDesktop(
        taskId: Int,
        deskId: Int,
        wct: WindowContainerTransaction,
        transitionSource: DesktopModeTransitionSource,
        remoteTransition: RemoteTransition? = null,
@@ -713,8 +724,8 @@ class DesktopTasksController(
            logW("moveBackgroundTaskToDesktop taskId=%d not found", taskId)
            return false
        }
        logV("moveBackgroundTaskToDesktop with taskId=%d", taskId)
        val deskId = getOrCreateDefaultDeskId(task.displayId) ?: return false
        logV("moveBackgroundTaskToDesktop with taskId=%d to deskId=%d", taskId, deskId)

        val runOnTransitStart = addDeskActivationChanges(deskId, wct, task)
        val exitResult =
            desktopImmersiveController.exitImmersiveIfApplicable(
+88 −0
Original line number Diff line number Diff line
@@ -1803,6 +1803,94 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        assertThat(wallpaperReorderIndex).isGreaterThan(homeReorderIndex)
    }

    @Test
    fun moveBackgroundTaskToDesktop_invalidDisplay_invalidFocusedDisplay_reordersHomeAndWallpaperInDefaultDisplay() {
        val task = createRecentTaskInfo(1, INVALID_DISPLAY)
        val homeTask = setUpHomeTask(displayId = DEFAULT_DISPLAY)
        val wallpaperToken = MockToken().token()
        whenever(focusTransitionObserver.globallyFocusedDisplayId).thenReturn(INVALID_DISPLAY)
        whenever(shellTaskOrganizer.getRunningTaskInfo(anyInt())).thenReturn(null)
        whenever(recentTasksController.findTaskInBackground(anyInt())).thenReturn(task)
        whenever(desktopWallpaperActivityTokenProvider.getToken(DEFAULT_DISPLAY))
            .thenReturn(wallpaperToken)

        controller.moveTaskToDefaultDeskAndActivate(
            taskId = task.taskId,
            transitionSource = UNKNOWN,
            remoteTransition = RemoteTransition(TestRemoteTransition()),
        )

        val wct = getLatestTransition()
        wct.assertReorder(homeTask)
        wct.assertReorder(wallpaperToken)
    }

    @Test
    fun moveBackgroundTaskToDesktop_invalidDisplay_validFocusedDisplay_reordersHomeAndWallpaperInFocusedDisplay() {
        val task = createRecentTaskInfo(1, INVALID_DISPLAY)
        val focusedDisplayId = 5
        val homeTask = setUpHomeTask(displayId = focusedDisplayId)
        val wallpaperToken = MockToken().token()
        taskRepository.addDesk(displayId = focusedDisplayId, deskId = 5)
        whenever(focusTransitionObserver.globallyFocusedDisplayId).thenReturn(focusedDisplayId)
        whenever(shellTaskOrganizer.getRunningTaskInfo(anyInt())).thenReturn(null)
        whenever(recentTasksController.findTaskInBackground(anyInt())).thenReturn(task)
        whenever(desktopWallpaperActivityTokenProvider.getToken(focusedDisplayId))
            .thenReturn(wallpaperToken)

        controller.moveTaskToDefaultDeskAndActivate(
            taskId = task.taskId,
            transitionSource = UNKNOWN,
            remoteTransition = RemoteTransition(TestRemoteTransition()),
        )

        val wct = getLatestTransition()
        wct.assertReorder(homeTask)
        wct.assertReorder(wallpaperToken)
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun moveBackgroundTaskToDesktop_invalidDisplay_invalidFocusedDisplay_activatesDeskInDefaultDisplay() {
        val task = createRecentTaskInfo(1, INVALID_DISPLAY)
        val deskId = 2
        whenever(focusTransitionObserver.globallyFocusedDisplayId).thenReturn(INVALID_DISPLAY)
        whenever(shellTaskOrganizer.getRunningTaskInfo(anyInt())).thenReturn(null)
        whenever(recentTasksController.findTaskInBackground(anyInt())).thenReturn(task)
        taskRepository.addDesk(displayId = DEFAULT_DISPLAY, deskId = deskId)
        taskRepository.setActiveDesk(displayId = DEFAULT_DISPLAY, deskId = deskId)

        controller.moveTaskToDefaultDeskAndActivate(
            taskId = task.taskId,
            transitionSource = UNKNOWN,
            remoteTransition = RemoteTransition(TestRemoteTransition()),
        )

        val wct = getLatestTransition()
        verify(desksOrganizer).activateDesk(wct, deskId = deskId)
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun moveBackgroundTaskToDesktop_invalidDisplay_validFocusedDisplay_activatesDeskInFocusedDisplay() {
        val task = createRecentTaskInfo(1, INVALID_DISPLAY)
        val focusedDisplayId = 5
        val deskId = 2
        whenever(focusTransitionObserver.globallyFocusedDisplayId).thenReturn(focusedDisplayId)
        whenever(shellTaskOrganizer.getRunningTaskInfo(anyInt())).thenReturn(null)
        whenever(recentTasksController.findTaskInBackground(anyInt())).thenReturn(task)
        taskRepository.addDesk(displayId = focusedDisplayId, deskId = deskId)

        controller.moveTaskToDefaultDeskAndActivate(
            taskId = task.taskId,
            transitionSource = UNKNOWN,
            remoteTransition = RemoteTransition(TestRemoteTransition()),
        )

        val wct = getLatestTransition()
        verify(desksOrganizer).activateDesk(wct, deskId = deskId)
    }

    @Test
    fun moveRunningTaskToDesktop_remoteTransition_usesOneShotHandler() {
        val transitionHandlerArgCaptor = argumentCaptor<TransitionHandler>()