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

Commit 603363a5 authored by Toshiki Kikuchi's avatar Toshiki Kikuchi
Browse files

Extract desktop-first decision factors into methods

This CL extracts the desktop-first decision factors into dedicated
methods.
This is a refactor and no functional change is expected.

Flag: com.android.window.flags.enable_display_windowing_mode_switching
Bug: 397557203
Test: DesktopDisplayModeControllerTest

Change-Id: Id13447c55cc11e80cc13b6c64423115c01d58829
parent a54f82cd
Loading
Loading
Loading
Loading
+20 −23
Original line number Original line Diff line number Diff line
@@ -48,30 +48,8 @@ class DesktopDisplayModeController(


    fun refreshDisplayWindowingMode() {
    fun refreshDisplayWindowingMode() {
        if (!DesktopExperienceFlags.ENABLE_DISPLAY_WINDOWING_MODE_SWITCHING.isTrue) return
        if (!DesktopExperienceFlags.ENABLE_DISPLAY_WINDOWING_MODE_SWITCHING.isTrue) return
        // TODO: b/375319538 - Replace the check with a DisplayManager API once it's available.
        val isExtendedDisplayEnabled =
            0 !=
                Settings.Global.getInt(
                    context.contentResolver,
                    DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS,
                    0,
                )
        if (!isExtendedDisplayEnabled) {
            // No action needed in mirror or projected mode.
            return
        }


        val hasNonDefaultDisplay =
        val targetDisplayWindowingMode = getTargetWindowingModeForDefaultDisplay()
            rootTaskDisplayAreaOrganizer.getDisplayIds().any { displayId ->
                displayId != DEFAULT_DISPLAY
            }
        val targetDisplayWindowingMode =
            if (hasNonDefaultDisplay) {
                WINDOWING_MODE_FREEFORM
            } else {
                // Use the default display windowing mode when no non-default display.
                windowManager.getWindowingMode(DEFAULT_DISPLAY)
            }
        val tdaInfo = rootTaskDisplayAreaOrganizer.getDisplayAreaInfo(DEFAULT_DISPLAY)
        val tdaInfo = rootTaskDisplayAreaOrganizer.getDisplayAreaInfo(DEFAULT_DISPLAY)
        requireNotNull(tdaInfo) { "DisplayAreaInfo of DEFAULT_DISPLAY must be non-null." }
        requireNotNull(tdaInfo) { "DisplayAreaInfo of DEFAULT_DISPLAY must be non-null." }
        val currentDisplayWindowingMode = tdaInfo.configuration.windowConfiguration.windowingMode
        val currentDisplayWindowingMode = tdaInfo.configuration.windowConfiguration.windowingMode
@@ -111,6 +89,25 @@ class DesktopDisplayModeController(
        transitions.startTransition(TRANSIT_CHANGE, wct, /* handler= */ null)
        transitions.startTransition(TRANSIT_CHANGE, wct, /* handler= */ null)
    }
    }


    private fun getTargetWindowingModeForDefaultDisplay(): Int {
        if (isExtendedDisplayEnabled() && hasExternalDisplay()) {
            return WINDOWING_MODE_FREEFORM
        }
        return windowManager.getWindowingMode(DEFAULT_DISPLAY)
    }

    // TODO: b/375319538 - Replace the check with a DisplayManager API once it's available.
    private fun isExtendedDisplayEnabled() =
        0 !=
            Settings.Global.getInt(
                context.contentResolver,
                DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS,
                0,
            )

    private fun hasExternalDisplay() =
        rootTaskDisplayAreaOrganizer.getDisplayIds().any { it != DEFAULT_DISPLAY }

    private fun logV(msg: String, vararg arguments: Any?) {
    private fun logV(msg: String, vararg arguments: Any?) {
        ProtoLog.v(WM_SHELL_DESKTOP_MODE, "%s: $msg", TAG, *arguments)
        ProtoLog.v(WM_SHELL_DESKTOP_MODE, "%s: $msg", TAG, *arguments)
    }
    }
+10 −6
Original line number Original line Diff line number Diff line
@@ -101,7 +101,7 @@ class DesktopDisplayModeControllerTest : ShellTestCase() {
    private fun testDisplayWindowingModeSwitch(
    private fun testDisplayWindowingModeSwitch(
        defaultWindowingMode: Int,
        defaultWindowingMode: Int,
        extendedDisplayEnabled: Boolean,
        extendedDisplayEnabled: Boolean,
        expectTransition: Boolean,
        expectToSwitch: Boolean,
    ) {
    ) {
        defaultTDA.configuration.windowConfiguration.windowingMode = defaultWindowingMode
        defaultTDA.configuration.windowConfiguration.windowingMode = defaultWindowingMode
        whenever(mockWindowManager.getWindowingMode(anyInt())).thenReturn(defaultWindowingMode)
        whenever(mockWindowManager.getWindowingMode(anyInt())).thenReturn(defaultWindowingMode)
@@ -113,10 +113,14 @@ class DesktopDisplayModeControllerTest : ShellTestCase() {


        settingsSession.use {
        settingsSession.use {
            connectExternalDisplay()
            connectExternalDisplay()
            if (expectToSwitch) {
                // Assumes [connectExternalDisplay] properly triggered the switching transition.
                // Will verify the transition later along with [disconnectExternalDisplay].
                defaultTDA.configuration.windowConfiguration.windowingMode = WINDOWING_MODE_FREEFORM
                defaultTDA.configuration.windowConfiguration.windowingMode = WINDOWING_MODE_FREEFORM
            }
            disconnectExternalDisplay()
            disconnectExternalDisplay()


            if (expectTransition) {
            if (expectToSwitch) {
                val arg = argumentCaptor<WindowContainerTransaction>()
                val arg = argumentCaptor<WindowContainerTransaction>()
                verify(transitions, times(2))
                verify(transitions, times(2))
                    .startTransition(eq(TRANSIT_CHANGE), arg.capture(), isNull())
                    .startTransition(eq(TRANSIT_CHANGE), arg.capture(), isNull())
@@ -139,7 +143,7 @@ class DesktopDisplayModeControllerTest : ShellTestCase() {
        testDisplayWindowingModeSwitch(
        testDisplayWindowingModeSwitch(
            defaultWindowingMode = WINDOWING_MODE_FULLSCREEN,
            defaultWindowingMode = WINDOWING_MODE_FULLSCREEN,
            extendedDisplayEnabled = false,
            extendedDisplayEnabled = false,
            expectTransition = false,
            expectToSwitch = false,
        )
        )
    }
    }


@@ -148,7 +152,7 @@ class DesktopDisplayModeControllerTest : ShellTestCase() {
        testDisplayWindowingModeSwitch(
        testDisplayWindowingModeSwitch(
            defaultWindowingMode = WINDOWING_MODE_FULLSCREEN,
            defaultWindowingMode = WINDOWING_MODE_FULLSCREEN,
            extendedDisplayEnabled = true,
            extendedDisplayEnabled = true,
            expectTransition = true,
            expectToSwitch = true,
        )
        )
    }
    }


@@ -157,7 +161,7 @@ class DesktopDisplayModeControllerTest : ShellTestCase() {
        testDisplayWindowingModeSwitch(
        testDisplayWindowingModeSwitch(
            defaultWindowingMode = WINDOWING_MODE_FREEFORM,
            defaultWindowingMode = WINDOWING_MODE_FREEFORM,
            extendedDisplayEnabled = true,
            extendedDisplayEnabled = true,
            expectTransition = false,
            expectToSwitch = false,
        )
        )
    }
    }