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

Commit e11279cd authored by Ben Lin's avatar Ben Lin
Browse files

DesktopMode: Make DesktopWallpaperActivity per-display.

This uses a combination of Intent flags - ACTIVITY_NEW_TASK and
FLAG_ACTIVITY_MULTIPLE_TASK and checking TokenProvider's current
existing token to ensure we aren't spawning new task containing
WallpaperActivity if they already exist.

Bug: 381935663
Test: atest and Manual checking on Connected Displays
Flag:com.android.window.flags.enable_per_display_desktop_wallpaper_activity

Change-Id: I95d4106a5e9194ece2931309c9908626c2060b22
parent 49f0b46a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@
        <activity
            android:name=".desktopmode.DesktopWallpaperActivity"
            android:excludeFromRecents="true"
            android:launchMode="singleInstance"
            android:launchMode="singleInstancePerTask"
            android:showForAllUsers="true"
            android:theme="@style/DesktopWallpaperTheme" />

+36 −13
Original line number Diff line number Diff line
@@ -552,7 +552,11 @@ class DesktopTasksController(
        )
        val wct = WindowContainerTransaction()
        exitSplitIfApplicable(wct, taskInfo)
        if (Flags.enablePerDisplayDesktopWallpaperActivity()) {
            moveHomeTask(wct, toTop = true, taskInfo.displayId)
        } else {
            moveHomeTask(wct, toTop = true)
        }
        val taskIdToMinimize =
            bringDesktopAppsToFrontBeforeShowingNewTask(taskInfo.displayId, wct, taskInfo.taskId)
        addMoveToDesktopChanges(wct, taskInfo)
@@ -1309,11 +1313,15 @@ class DesktopTasksController(
    ): Int? {
        logV("bringDesktopAppsToFront, newTaskId=%d", newTaskIdInFront)
        // Move home to front, ensures that we go back home when all desktop windows are closed
        if (Flags.enablePerDisplayDesktopWallpaperActivity()) {
            moveHomeTask(wct, toTop = true, displayId)
        } else {
            moveHomeTask(wct, toTop = true)
        }

        // Currently, we only handle the desktop on the default display really.
        if (
            (displayId == DEFAULT_DISPLAY || Flags.enableBugFixesForSecondaryDisplay()) &&
            (displayId == DEFAULT_DISPLAY || Flags.enablePerDisplayDesktopWallpaperActivity()) &&
                ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY.isTrue()
        ) {
            // Add translucent wallpaper activity to show the wallpaper underneath
@@ -1359,9 +1367,13 @@ class DesktopTasksController(
        return taskIdToMinimize
    }

    private fun moveHomeTask(wct: WindowContainerTransaction, toTop: Boolean) {
    private fun moveHomeTask(
        wct: WindowContainerTransaction,
        toTop: Boolean,
        displayId: Int = DEFAULT_DISPLAY,
    ) {
        shellTaskOrganizer
            .getRunningTasks(context.displayId)
            .getRunningTasks(displayId)
            .firstOrNull { task -> task.activityType == ACTIVITY_TYPE_HOME }
            ?.let { homeTask -> wct.reorder(homeTask.getToken(), /* onTop= */ toTop) }
    }
@@ -1370,12 +1382,19 @@ class DesktopTasksController(
        logV("addWallpaperActivity")
        if (Flags.enableDesktopWallpaperActivityForSystemUser()) {
            val intent = Intent(context, DesktopWallpaperActivity::class.java)
            if (
                desktopWallpaperActivityTokenProvider.getToken(displayId) == null &&
                    Flags.enablePerDisplayDesktopWallpaperActivity()
            ) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
            }
            val options =
                ActivityOptions.makeBasic().apply {
                    launchWindowingMode = WINDOWING_MODE_FULLSCREEN
                    pendingIntentBackgroundActivityStartMode =
                        ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS
                    if (Flags.enableBugFixesForSecondaryDisplay()) {
                    if (Flags.enablePerDisplayDesktopWallpaperActivity()) {
                        launchDisplayId = displayId
                    }
                }
@@ -1391,13 +1410,20 @@ class DesktopTasksController(
            val userHandle = UserHandle.of(userId)
            val userContext = context.createContextAsUser(userHandle, /* flags= */ 0)
            val intent = Intent(userContext, DesktopWallpaperActivity::class.java)
            if (
                desktopWallpaperActivityTokenProvider.getToken(displayId) == null &&
                    Flags.enablePerDisplayDesktopWallpaperActivity()
            ) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
            }
            intent.putExtra(Intent.EXTRA_USER_HANDLE, userId)
            val options =
                ActivityOptions.makeBasic().apply {
                    launchWindowingMode = WINDOWING_MODE_FULLSCREEN
                    pendingIntentBackgroundActivityStartMode =
                        ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS
                    if (Flags.enableBugFixesForSecondaryDisplay()) {
                    if (Flags.enablePerDisplayDesktopWallpaperActivity()) {
                        launchDisplayId = displayId
                    }
                }
@@ -1414,8 +1440,8 @@ class DesktopTasksController(
        }
    }

    private fun removeWallpaperActivity(wct: WindowContainerTransaction) {
        desktopWallpaperActivityTokenProvider.getToken()?.let { token ->
    private fun removeWallpaperActivity(wct: WindowContainerTransaction, displayId: Int) {
        desktopWallpaperActivityTokenProvider.getToken(displayId)?.let { token ->
            logV("removeWallpaperActivity")
            if (Flags.enableDesktopWallpaperActivityForSystemUser()) {
                wct.reorder(token, /* onTop= */ false)
@@ -1440,9 +1466,6 @@ class DesktopTasksController(
            if (!taskRepository.isOnlyVisibleNonClosingTask(taskId, displayId)) {
                return
            }
            if (displayId != DEFAULT_DISPLAY) {
                return
            }
        } else if (
            Flags.enableDesktopWindowingPip() &&
                taskRepository.isMinimizedPipPresentInDisplay(displayId) &&
@@ -1457,7 +1480,7 @@ class DesktopTasksController(
        desktopModeEnterExitTransitionListener?.onExitDesktopModeTransitionStarted(
            FULLSCREEN_ANIMATION_DURATION
        )
        removeWallpaperActivity(wct)
        removeWallpaperActivity(wct, displayId)
    }

    fun releaseVisualIndicator() {
+25 −19
Original line number Diff line number Diff line
@@ -60,7 +60,9 @@ class DesktopTasksTransitionObserver(
    shellInit: ShellInit,
) : Transitions.TransitionObserver {

    private var transitionToCloseWallpaper: IBinder? = null
    data class CloseWallpaperTransition(val transition: IBinder, val displayId: Int)

    private var transitionToCloseWallpaper: CloseWallpaperTransition? = null
    /* Pending PiP transition and its associated display id and task id. */
    private var pendingPipTransitionAndPipTask: Triple<IBinder, Int, Int>? = null
    private var currentProfileId: Int
@@ -248,9 +250,10 @@ class DesktopTasksTransitionObserver(
                desktopRepository.getVisibleTaskCount(taskInfo.displayId) == 0 &&
                    change.mode == TRANSIT_CLOSE &&
                    taskInfo.windowingMode == WINDOWING_MODE_FREEFORM &&
                    desktopWallpaperActivityTokenProvider.getToken() != null
                    desktopWallpaperActivityTokenProvider.getToken(taskInfo.displayId) != null
            ) {
                transitionToCloseWallpaper = transition
                transitionToCloseWallpaper =
                    CloseWallpaperTransition(transition, taskInfo.displayId)
                currentProfileId = taskInfo.userId
            }
        }
@@ -265,10 +268,13 @@ class DesktopTasksTransitionObserver(
    }

    override fun onTransitionFinished(transition: IBinder, aborted: Boolean) {
        val lastSeenTransitionToCloseWallpaper = transitionToCloseWallpaper
        // TODO: b/332682201 Update repository state
        if (transitionToCloseWallpaper == transition) {
        if (lastSeenTransitionToCloseWallpaper?.transition == transition) {
            // TODO: b/362469671 - Handle merging the animation when desktop is also closing.
            desktopWallpaperActivityTokenProvider.getToken()?.let { wallpaperActivityToken ->
            desktopWallpaperActivityTokenProvider
                .getToken(lastSeenTransitionToCloseWallpaper.displayId)
                ?.let { wallpaperActivityToken ->
                    if (Flags.enableDesktopWallpaperActivityForSystemUser()) {
                        transitions.startTransition(
                            TRANSIT_TO_BACK,
+25 −0
Original line number Diff line number Diff line
@@ -601,8 +601,33 @@ class DesktopTasksControllerTest : ShellTestCase() {
        assertThat(controller.isDesktopModeShowing(displayId = DEFAULT_DISPLAY)).isFalse()
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY,
        Flags.FLAG_ENABLE_PER_DISPLAY_DESKTOP_WALLPAPER_ACTIVITY,
    )
    fun showDesktopApps_onSecondaryDisplay_desktopWallpaperEnabled_perDisplayWallpaperEnabled_shouldShowWallpaper() {
        val homeTask = setUpHomeTask(SECOND_DISPLAY)
        val task1 = setUpFreeformTask(SECOND_DISPLAY)
        val task2 = setUpFreeformTask(SECOND_DISPLAY)
        markTaskHidden(task1)
        markTaskHidden(task2)

        controller.showDesktopApps(SECOND_DISPLAY, RemoteTransition(TestRemoteTransition()))

        val wct =
            getLatestWct(type = TRANSIT_TO_FRONT, handlerClass = OneShotRemoteHandler::class.java)
        assertThat(wct.hierarchyOps).hasSize(4)
        // Expect order to be from bottom: home, wallpaperIntent, task1, task2
        wct.assertReorderAt(index = 0, homeTask)
        wct.assertPendingIntentAt(index = 1, desktopWallpaperIntent)
        wct.assertReorderAt(index = 2, task1)
        wct.assertReorderAt(index = 3, task2)
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY)
    @DisableFlags(Flags.FLAG_ENABLE_PER_DISPLAY_DESKTOP_WALLPAPER_ACTIVITY)
    fun showDesktopApps_onSecondaryDisplay_desktopWallpaperEnabled_shouldNotShowWallpaper() {
        val homeTask = setUpHomeTask(SECOND_DISPLAY)
        val task1 = setUpFreeformTask(SECOND_DISPLAY)