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

Commit 36ed3cc3 authored by Nergi Rahardi's avatar Nergi Rahardi
Browse files

Update getConnectedDisplays to generic getDisplays with extra field

Filtering out non-connected displays should be business logic that is
done on caller side. This unifies existing getConnectedDisplays() and
getAllDisplayIds() into a single getDisplays() and processed
accordingly.

Bug: 419742776
Test: atest DisplayTopologyPreferenceTest
Test: atest ExternalDisplayPreferenceFragmentTest
Test: atest ExternalDisplayUpdaterTest
Test: atest ResolutionPreferenceFragmentTest
Flag: EXEMPT refactoring util
Change-Id: If2e9f410469b09f6558b4d1a4d2c5f126ac4fbdc
parent 1de76a2a
Loading
Loading
Loading
Loading
+23 −31
Original line number Diff line number Diff line
@@ -102,11 +102,20 @@ open class ConnectedDisplayInjector(open val context: Context?) {
        return RevealedWallpaper(display.displayId, view, windowManager)
    }

    private fun wrapDmDisplay(display: Display, isEnabled: DisplayIsEnabled): DisplayDevice =
        DisplayDevice(display.displayId, display.name, display.mode,
                display.getSupportedModes().asList(), isEnabled)

    private fun isDisplayAllowed(display: Display): Boolean =
    private fun wrapDmDisplay(
        display: Display,
        isEnabled: DisplayIsEnabled,
        isConnectedDisplay: Boolean
    ): DisplayDevice = DisplayDevice(
        display.displayId,
        display.name,
        display.mode,
        display.supportedModes.asList(),
        isEnabled,
        isConnectedDisplay
    )

    private fun isConnectedDisplay(display: Display): Boolean =
        display.type == Display.TYPE_EXTERNAL || display.type == Display.TYPE_OVERLAY
                || isVirtualDisplayAllowed(display);

@@ -133,35 +142,18 @@ open class ConnectedDisplayInjector(open val context: Context?) {
    }

    /**
     * TODO(b/419742776): Unify this with #getAllDisplayIds
     * @return all displays including disabled.
     */
    open fun getConnectedDisplays(): List<DisplayDevice> {
    open fun getDisplays(): List<DisplayDevice> {
        val dm = displayManager ?: return emptyList()

        val enabledIds = dm.getDisplays().map { it.getDisplayId() }.toSet()

        return dm.getDisplays(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED)
            .filter { isDisplayAllowed(it) }
            .map {
                val isEnabled = if (enabledIds.contains(it.displayId))
                    DisplayIsEnabled.YES
                else
                    DisplayIsEnabled.NO
                wrapDmDisplay(it, isEnabled)
            }
            .toList()
    }
        val enabledIds = dm.displays.map { it.displayId }.toSet()

    /**
     * This method return all enabled display ids without further filtering
     * TODO(b/419742776): Unify this with #getConnectedDisplays
     *
     * @see getConnectedDisplays to specifically fetch all connected displays
     */
    open fun getAllDisplayIds(): List<Int> {
        val dm = displayManager ?: return emptyList()
        return dm.getDisplays().map { it.displayId }.toList()
        return dm.getDisplays(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED).map {
            val isEnabled = if (enabledIds.contains(it.displayId)) DisplayIsEnabled.YES
            else DisplayIsEnabled.NO
            wrapDmDisplay(it, isEnabled, isConnectedDisplay(it))
        }.toList()
    }

    /**
@@ -174,8 +166,8 @@ open class ConnectedDisplayInjector(open val context: Context?) {
            return null
        }
        val display = displayManager?.getDisplay(displayId) ?: return null
        return if (isDisplayAllowed(display)) {
            wrapDmDisplay(display, DisplayIsEnabled.UNKNOWN)
        return if (isConnectedDisplay(display)) {
            wrapDmDisplay(display, DisplayIsEnabled.UNKNOWN, true)
        } else {
            null
        }
+17 −4
Original line number Diff line number Diff line
@@ -17,15 +17,28 @@
package com.android.settings.connecteddevice.display

import android.view.Display.Mode

import androidx.annotation.Keep

enum class DisplayIsEnabled { YES, NO, UNKNOWN }
/**
 * Unknown is a convenience enum to denote the query for isEnabled was skipped, since it took more
 * time to query this info.
 */
enum class DisplayIsEnabled {
    YES,
    NO,
    UNKNOWN,
}

/**
 * Contains essential information from {@link android.view.Display} needed by the user to configure
 * a display.
 */
@Keep
data class DisplayDevice(val id: Int, val name: String, val mode: Mode?,
        val supportedModes: List<Mode>, val isEnabled: DisplayIsEnabled) {}
data class DisplayDevice(
    val id: Int,
    val name: String,
    val mode: Mode?,
    val supportedModes: List<Mode>,
    val isEnabled: DisplayIsEnabled,
    val isConnectedDisplay: Boolean,
)
+6 −1
Original line number Diff line number Diff line
@@ -324,7 +324,12 @@ class DisplayTopologyPreference(val injector: ConnectedDisplayInjector) :
    private fun processDisplayBoundsMirroringMode(
        logicalDisplaySizeFetcher: LogicalDisplaySizeFetcher
    ): List<Pair<Int, RectF>> {
        val displayIds = injector.getAllDisplayIds().sortedBy { it }
        val displayIds =
            injector
                .getDisplays()
                .filter { it.isEnabled == DisplayIsEnabled.YES }
                .map { it.id }
                .sortedBy { it }

        val bounds = mutableListOf<Pair<Int, RectF>>()
        val mirroringDiagonalStackOffsetPx =
+2 −1
Original line number Diff line number Diff line
@@ -360,7 +360,8 @@ public class ExternalDisplayPreferenceFragment extends SettingsPreferenceFragmen
    }

    private void updateScreen(final PrefRefresh screen) {
        final var displaysToShow = mInjector.getConnectedDisplays();
        final var displaysToShow = mInjector.getDisplays().stream().filter(
                DisplayDevice::isConnectedDisplay).toList();

        if (displaysToShow.isEmpty()) {
            showTextWhenNoDisplaysToShow(screen, /* position= */ 0);
+2 −1
Original line number Diff line number Diff line
@@ -131,7 +131,8 @@ public class ExternalDisplayUpdater {
            return null;
        }

        var allDisplays = mInjector.getConnectedDisplays();
        var allDisplays = mInjector.getDisplays().stream().filter(
                DisplayDevice::isConnectedDisplay).toList();
        for (var display : allDisplays) {
            if (display.isEnabled() == DisplayIsEnabled.YES) {
                return context.getString(R.string.external_display_on);
Loading