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

Commit 03c0e9e1 authored by Matt Sziklay's avatar Matt Sziklay
Browse files

No-op desk creation if display doesn't support desks.

Persistent repository defaults to restoring desks on default display. However, because createDesk does not include a check that the provided display supports desktop mode, this can result in non-desktop devices launching desks from overview.

This CL no-ops the creation in DesktopTasksController and DesktopRepositoryInitializerImpl in this particular case as a quick fix, but long term the plan is for the persistent repository to support restoring to external displays.

Bug: 417907552
Test: Manual; open desk on external display on phone, reboot device, confirm no desk appears in overview.
Flag: com.android.window.flags.enable_multiple_desktops_backend
Change-Id: I7c7acda85f130a1fc5176906efd145854d45abe3
parent 11538b19
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -1633,9 +1633,10 @@ public abstract class WMShellModule {
            Context context,
            DesktopPersistentRepository desktopPersistentRepository,
            @ShellMainThread CoroutineScope mainScope,
            DesktopConfig desktopConfig) {
            DesktopConfig desktopConfig,
            DesktopState desktopState) {
        return new DesktopRepositoryInitializerImpl(context, desktopPersistentRepository,
                mainScope, desktopConfig);
                mainScope, desktopConfig, desktopState);
    }

    @WMSingleton
+5 −0
Original line number Diff line number Diff line
@@ -557,6 +557,11 @@ class DesktopTasksController(
            userId,
            enforceDeskLimit,
        )
        if (!desktopState.isDesktopModeSupportedOnDisplay(displayId)) {
            // Display does not support desktops, no-op.
            logW("createDesk displayId $displayId does not support desktops, ignoring request")
            return
        }
        val repository = userRepositories.getProfile(userId)
        if (enforceDeskLimit && !canCreateDesks(repository)) {
            // At the limit, no-op.
+7 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import com.android.wm.shell.desktopmode.persistence.DesktopRepositoryInitializer
import com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE
import com.android.wm.shell.shared.annotations.ShellMainThread
import com.android.wm.shell.shared.desktopmode.DesktopConfig
import com.android.wm.shell.shared.desktopmode.DesktopState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -43,6 +44,7 @@ class DesktopRepositoryInitializerImpl(
    private val persistentRepository: DesktopPersistentRepository,
    @ShellMainThread private val mainCoroutineScope: CoroutineScope,
    private val desktopConfig: DesktopConfig,
    private val desktopState: DesktopState,
) : DesktopRepositoryInitializer {

    override var deskRecreationFactory: DeskRecreationFactory = DefaultDeskRecreationFactory()
@@ -51,7 +53,11 @@ class DesktopRepositoryInitializerImpl(
    override val isInitialized: StateFlow<Boolean> = _isInitialized

    override fun initialize(userRepositories: DesktopUserRepositories) {
        if (!DesktopModeFlags.ENABLE_DESKTOP_WINDOWING_PERSISTENCE.isTrue) {
        // TODO: b/401107440 - Remove second check once restoring to external displays is supported
        if (
            !DesktopModeFlags.ENABLE_DESKTOP_WINDOWING_PERSISTENCE.isTrue ||
                !desktopState.isDesktopModeSupportedOnDisplay(Display.DEFAULT_DISPLAY)
        ) {
            _isInitialized.value = true
            return
        }
+14 −0
Original line number Diff line number Diff line
@@ -9593,6 +9593,7 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun testCreateDesk() {
        desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = true
        val currentDeskCount = taskRepository.getNumberOfDesks(DEFAULT_DISPLAY)
        whenever(desksOrganizer.createDesk(eq(DEFAULT_DISPLAY), any(), any())).thenAnswer {
            invocation ->
@@ -9607,6 +9608,7 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun testCreateDesk_invalidDisplay_dropsRequest() {
        desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = true
        controller.createDesk(INVALID_DISPLAY)

        verify(desksOrganizer, never()).createDesk(any(), any(), any())
@@ -9615,6 +9617,7 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun testCreateDesk_systemUser_dropsRequest() {
        desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = true
        assumeTrue(UserManager.isHeadlessSystemUserMode())

        controller.createDesk(DEFAULT_DISPLAY, UserHandle.USER_SYSTEM)
@@ -9625,6 +9628,7 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun testCreateDesk_enforceLimitAndOverLimit_dropsRequest() {
        desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = true
        desktopConfig.maxDeskLimit = 2
        // Add a second desk to bring the number up to the limit.
        taskRepository.addDesk(displayId = DEFAULT_DISPLAY, deskId = 2)
@@ -9634,6 +9638,16 @@ class DesktopTasksControllerTest(flags: FlagsParameterization) : ShellTestCase()
        verify(desksOrganizer, never()).createDesk(any(), any(), any())
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun testCreateDesk_displayDoesNotSupportDesks_dropsRequest() {
        desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = false

        controller.createDesk(DEFAULT_DISPLAY)

        verify(desksOrganizer, never()).createDesk(any(), any(), any())
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_DISPLAY_DISCONNECT_INTERACTION,
+25 −0
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ class DesktopRepositoryInitializerTest : ShellTestCase() {
                persistentRepository,
                datastoreScope,
                desktopConfig,
                desktopState,
            )
        desktopUserRepositories =
            DesktopUserRepositories(
@@ -116,6 +117,7 @@ class DesktopRepositoryInitializerTest : ShellTestCase() {
    )
    fun initWithPersistence_multipleUsers_addedCorrectly() =
        runTest(StandardTestDispatcher()) {
            desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = true
            whenever(persistentRepository.getUserDesktopRepositoryMap())
                .thenReturn(
                    mapOf(
@@ -202,6 +204,7 @@ class DesktopRepositoryInitializerTest : ShellTestCase() {
    @EnableFlags(FLAG_ENABLE_DESKTOP_WINDOWING_PERSISTENCE, FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun initWithPersistence_singleUser_addedCorrectly() =
        runTest(StandardTestDispatcher()) {
            desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = true
            whenever(persistentRepository.getUserDesktopRepositoryMap())
                .thenReturn(mapOf(USER_ID_1 to desktopRepositoryState1))
            whenever(persistentRepository.getDesktopRepositoryState(USER_ID_1))
@@ -269,6 +272,7 @@ class DesktopRepositoryInitializerTest : ShellTestCase() {
    )
    fun initWithPersistence_deskRecreationFailed_deskNotAdded() =
        runTest(StandardTestDispatcher()) {
            desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = true
            whenever(persistentRepository.getUserDesktopRepositoryMap())
                .thenReturn(mapOf(USER_ID_1 to desktopRepositoryState1))
            whenever(persistentRepository.getDesktopRepositoryState(USER_ID_1))
@@ -291,6 +295,27 @@ class DesktopRepositoryInitializerTest : ShellTestCase() {
                .containsExactly(DESKTOP_ID_1)
        }

    @Test
    @EnableFlags(
        FLAG_ENABLE_DESKTOP_WINDOWING_PERSISTENCE,
        FLAG_ENABLE_DESKTOP_WINDOWING_HSUM,
        FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
    )
    fun initWithPersistence_defaultDisplayDoesNotSupportDesks_deskNotAdded() =
        runTest(StandardTestDispatcher()) {
            desktopState.overrideDesktopModeSupportPerDisplay[DEFAULT_DISPLAY] = false
            whenever(persistentRepository.getUserDesktopRepositoryMap())
                .thenReturn(mapOf(USER_ID_1 to desktopRepositoryState1))
            whenever(persistentRepository.getDesktopRepositoryState(USER_ID_1))
                .thenReturn(desktopRepositoryState1)
            whenever(persistentRepository.readDesktop(USER_ID_1, DESKTOP_ID_1)).thenReturn(desktop1)

            repositoryInitializer.initialize(desktopUserRepositories)

            assertThat(desktopUserRepositories.getProfile(USER_ID_1).getDeskIds(DEFAULT_DISPLAY))
                .isEmpty()
        }

    @After
    fun tearDown() {
        datastoreScope.cancel()