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

Commit 38ae6341 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add transition handler for desktop disconnect." into main

parents 377469f0 a948ea22
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -110,6 +110,7 @@ import com.android.wm.shell.desktopmode.DesktopTasksController;
import com.android.wm.shell.desktopmode.DesktopTasksLimiter;
import com.android.wm.shell.desktopmode.DesktopTasksTransitionObserver;
import com.android.wm.shell.desktopmode.DesktopUserRepositories;
import com.android.wm.shell.desktopmode.DisplayDisconnectTransitionHandler;
import com.android.wm.shell.desktopmode.DragToDesktopTransitionHandler;
import com.android.wm.shell.desktopmode.DragToDisplayTransitionHandler;
import com.android.wm.shell.desktopmode.EnterDesktopTaskTransitionHandler;
@@ -835,6 +836,7 @@ public abstract class WMShellModule {
            DesktopModeDragAndDropTransitionHandler desktopModeDragAndDropTransitionHandler,
            ToggleResizeDesktopTaskTransitionHandler toggleResizeDesktopTaskTransitionHandler,
            DragToDesktopTransitionHandler dragToDesktopTransitionHandler,
            DisplayDisconnectTransitionHandler displayDisconnectTransitionHandler,
            @DynamicOverride DesktopUserRepositories desktopUserRepositories,
            DesktopRepositoryInitializer desktopRepositoryInitializer,
            Optional<DesktopImmersiveController> desktopImmersiveController,
@@ -885,6 +887,7 @@ public abstract class WMShellModule {
                desktopModeDragAndDropTransitionHandler,
                toggleResizeDesktopTaskTransitionHandler,
                dragToDesktopTransitionHandler,
                displayDisconnectTransitionHandler,
                desktopImmersiveController.get(),
                desktopUserRepositories,
                desktopRepositoryInitializer,
@@ -1044,6 +1047,14 @@ public abstract class WMShellModule {
                        desktopState);
    }

    @WMSingleton
    @Provides
    static DisplayDisconnectTransitionHandler provideDesktopDisconnectTransitionHandler(
            Transitions transitions,
            ShellInit shellInit) {
        return new DisplayDisconnectTransitionHandler(transitions, shellInit);
    }

    @WMSingleton
    @Provides
    static DesktopWallpaperActivityTokenProvider provideDesktopWallpaperActivityTokenProvider() {
+33 −6
Original line number Diff line number Diff line
@@ -205,6 +205,7 @@ class DesktopTasksController(
    private val desktopModeDragAndDropTransitionHandler: DesktopModeDragAndDropTransitionHandler,
    private val toggleResizeDesktopTaskTransitionHandler: ToggleResizeDesktopTaskTransitionHandler,
    private val dragToDesktopTransitionHandler: DragToDesktopTransitionHandler,
    private val displayDisconnectTransitionHandler: DisplayDisconnectTransitionHandler,
    private val desktopImmersiveController: DesktopImmersiveController,
    private val userRepositories: DesktopUserRepositories,
    desktopRepositoryInitializer: DesktopRepositoryInitializer,
@@ -668,13 +669,37 @@ class DesktopTasksController(
                        desktopRepository
                            .getActiveTasks(disconnectedDisplayId)
                            .contains(focusTransitionObserver.globallyFocusedTaskId)
                    // Remove desk if it's empty.
                    if (desktopRepository.getActiveTasks(disconnectedDisplayId).isEmpty()) {
                        desksOrganizer.removeDesk(wct, deskId, desktopRepository.userId)
                        desksTransitionObserver.addPendingTransition(
                            DeskTransition.RemoveDesk(
                                token = transition,
                                displayId = disconnectedDisplayId,
                                deskId = deskId,
                                tasks = emptySet(),
                                onDeskRemovedListener = onDeskRemovedListener,
                            )
                        )
                    } else {
                        // Otherwise, reparent it to the destination display.
                        desksOrganizer.moveDeskToDisplay(wct, deskId, destinationDisplayId, toTop)
                        desksTransitionObserver.addPendingTransition(
                        DeskTransition.ChangeDeskDisplay(transition, deskId, destinationDisplayId)
                            DeskTransition.ChangeDeskDisplay(
                                transition,
                                deskId,
                                destinationDisplayId,
                            )
                        )
                        updateDesksActivationOnDisconnection(
                                deskId,
                                destinationDisplayId,
                                wct,
                                toTop,
                            )
                    updateDesksActivationOnDisconnection(deskId, destinationDisplayId, wct, toTop)
                            ?.invoke(transition)
                    }
                }
            } else {
                // Desktop not supported on display; reparent tasks to display area, remove desk.
                val tdaInfo =
@@ -709,6 +734,8 @@ class DesktopTasksController(
                }
            }
        }
        // Inform the transition handler here since this class will handle the request.
        displayDisconnectTransitionHandler.addPendingTransition(transition)
        return wct
    }

+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.desktopmode

import android.os.IBinder
import android.view.Display.INVALID_DISPLAY
import android.view.SurfaceControl
import android.window.DesktopExperienceFlags
import android.window.TransitionInfo
import android.window.TransitionRequestInfo
import android.window.WindowContainerTransaction
import com.android.wm.shell.sysui.ShellInit
import com.android.wm.shell.transition.Transitions

/** Handler to animate the transition from disconnecting a display. */
class DisplayDisconnectTransitionHandler(val transitions: Transitions, shellInit: ShellInit) :
    Transitions.TransitionHandler {

    private val pendingTransitions = mutableSetOf<IBinder>()

    init {
        shellInit.addInitCallback({ onInit() }, this)
    }

    private fun onInit() {
        transitions.addHandler(this)
    }

    fun addPendingTransition(transition: IBinder) = pendingTransitions.add(transition)

    override fun startAnimation(
        transition: IBinder,
        info: TransitionInfo,
        startTransaction: SurfaceControl.Transaction,
        finishTransaction: SurfaceControl.Transaction,
        finishCallback: Transitions.TransitionFinishCallback,
    ): Boolean {
        if (!pendingTransitions.contains(transition)) return false
        // TODO: b/391652399 - Animate transitions
        startTransaction.apply()
        finishCallback.onTransitionFinished(null)
        pendingTransitions.remove(transition)
        return true
    }

    override fun handleRequest(
        transition: IBinder,
        request: TransitionRequestInfo,
    ): WindowContainerTransaction? {
        // Fallback method; if no other handler takes the transition, we still need to tell
        // this one to handle the animation later. Currently this is possible on a device
        // that supports multi-display but does not support desktop mode, as
        // DesktopTasksController will not handle the disconnect request.
        val displayChange = request.displayChange ?: return null
        if (
            DesktopExperienceFlags.ENABLE_DISPLAY_DISCONNECT_INTERACTION.isTrue &&
                displayChange.disconnectReparentDisplay != INVALID_DISPLAY
        ) {
            addPendingTransition(transition)
        }
        // Return null since another handler may want to make specific task changes.
        return null
    }
}
+39 −21
Original line number Diff line number Diff line
@@ -237,6 +237,7 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
    @Mock
    lateinit var toggleResizeDesktopTaskTransitionHandler: ToggleResizeDesktopTaskTransitionHandler
    @Mock lateinit var dragToDesktopTransitionHandler: DragToDesktopTransitionHandler
    @Mock lateinit var mDisplayDisconnectTransitionHandler: DisplayDisconnectTransitionHandler
    @Mock lateinit var mMockDesktopImmersiveController: DesktopImmersiveController
    @Mock lateinit var splitScreenController: SplitScreenController
    @Mock lateinit var recentsTransitionHandler: RecentsTransitionHandler
@@ -460,6 +461,7 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
            dragAndDropTransitionHandler,
            toggleResizeDesktopTaskTransitionHandler,
            dragToDesktopTransitionHandler,
            mDisplayDisconnectTransitionHandler,
            mMockDesktopImmersiveController,
            userRepositories,
            repositoryInitializer,
@@ -9081,7 +9083,8 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        val defaultDisplayTask = setUpFreeformTask()
        val transition = Binder()
        taskRepository.addDesk(SECOND_DISPLAY, DISCONNECTED_DESK_ID)
        val secondDisplayTask = setUpFreeformTask(SECOND_DISPLAY)
        taskRepository.setActiveDesk(displayId = SECOND_DISPLAY, deskId = DISCONNECTED_DESK_ID)
        val secondDisplayTask = setUpFreeformTask(SECOND_DISPLAY, deskId = DISCONNECTED_DESK_ID)

        performDisplayDisconnectTransition(
            transition = transition,
@@ -9122,7 +9125,8 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        val defaultDisplayTask = setUpFreeformTask()
        val transition = Binder()
        taskRepository.addDesk(SECOND_DISPLAY, DISCONNECTED_DESK_ID)
        val secondDisplayTask = setUpFreeformTask(SECOND_DISPLAY)
        taskRepository.setActiveDesk(displayId = SECOND_DISPLAY, deskId = DISCONNECTED_DESK_ID)
        val secondDisplayTask = setUpFreeformTask(SECOND_DISPLAY, deskId = DISCONNECTED_DESK_ID)

        performDisplayDisconnectTransition(
            transition = transition,
@@ -9155,12 +9159,43 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
            )
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_DISPLAY_DISCONNECT_INTERACTION,
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
    )
    fun onDisplayDisconnect_desktopModeSupported_emptyDeskRemoved() {
        val defaultDisplayTask = setUpFreeformTask()
        val transition = Binder()
        taskRepository.addDesk(SECOND_DISPLAY, DISCONNECTED_DESK_ID)
        taskRepository.setActiveDesk(SECOND_DISPLAY, DISCONNECTED_DESK_ID)

        performDisplayDisconnectTransition(
            transition = transition,
            desktopSupportedOnDefaultDisplay = true,
            taskOnSecondDisplayHasFocus = false,
            defaultDisplayTask = defaultDisplayTask,
            secondDisplayTask = null,
        )

        verify(desksOrganizer).removeDesk(any(), eq(DISCONNECTED_DESK_ID), anyInt())
        verify(desksTransitionsObserver)
            .addPendingTransition(
                argThat {
                    this is DeskTransition.RemoveDesk &&
                        this.token == transition &&
                        this.displayId == SECOND_DISPLAY &&
                        this.deskId == DISCONNECTED_DESK_ID
                }
            )
    }

    private fun performDisplayDisconnectTransition(
        transition: IBinder,
        desktopSupportedOnDefaultDisplay: Boolean,
        taskOnSecondDisplayHasFocus: Boolean,
        defaultDisplayTask: RunningTaskInfo,
        secondDisplayTask: RunningTaskInfo,
        secondDisplayTask: RunningTaskInfo?,
    ): WindowContainerTransaction {
        desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] =
            desktopSupportedOnDefaultDisplay
@@ -9173,26 +9208,9 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
                    /* remoteTransition= */ null,
                )
                .apply { setDisplayChange(displayChange) }
        if (desktopSupportedOnDefaultDisplay) {
            taskRepository.addDesk(displayId = DEFAULT_DISPLAY, deskId = DEFAULT_DESK_ID)
            taskRepository.setActiveDesk(displayId = DEFAULT_DISPLAY, deskId = DEFAULT_DESK_ID)
            taskRepository.addTaskToDesk(
                displayId = DEFAULT_DISPLAY,
                deskId = DEFAULT_DESK_ID,
                taskId = defaultDisplayTask.taskId,
                isVisible = true,
            )
        }
        taskRepository.setActiveDesk(displayId = SECOND_DISPLAY, deskId = DISCONNECTED_DESK_ID)
        taskRepository.addTaskToDesk(
            displayId = SECOND_DISPLAY,
            deskId = DISCONNECTED_DESK_ID,
            taskId = secondDisplayTask.taskId,
            isVisible = true,
        )
        val focusedTaskId =
            if (taskOnSecondDisplayHasFocus) {
                secondDisplayTask.taskId
                secondDisplayTask?.taskId ?: error("Cannot have a focused null task")
            } else {
                defaultDisplayTask.taskId
            }