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

Commit 35e0e1c6 authored by Nicolo' Mazzucato's avatar Nicolo' Mazzucato
Browse files

Fix DisplayRepository defaultDisplayOff

The defaultDisplayOff flow was assuming the entire displays were updated when any changed. This caused the event to not be propagated anymore with the enableEfficientDisplayRepository flag.

Bug: 345472038
Test: DisplayRepositoryTest
Flag: com.android.systemui.enable_efficient_display_repository
Change-Id: Ie663f71e37b88b3172e7e47a1d79274886f27434
parent 54054783
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
@@ -137,6 +138,7 @@ constructor(
    override val displayAdditionEvent: Flow<Display?> =
        allDisplayEvents.filterIsInstance<DisplayEvent.Added>().map { getDisplay(it.displayId) }

    // TODO: b/345472038 - Delete after the flag is ramped up.
    private val oldEnabledDisplays: Flow<Set<Display>> =
        allDisplayEvents
            .map { getDisplays() }
@@ -167,6 +169,9 @@ constructor(
            }
            .debugLog("enabledDisplayIds")

    private val defaultDisplay by lazy {
        getDisplay(Display.DEFAULT_DISPLAY) ?: error("Unable to get default display.")
    }
    /**
     * Represents displays that went though the [DisplayListener.onDisplayAdded] callback.
     *
@@ -181,11 +186,7 @@ constructor(
                .stateIn(
                    bgApplicationScope,
                    started = SharingStarted.WhileSubscribed(),
                    initialValue =
                        setOf(
                            getDisplay(Display.DEFAULT_DISPLAY)
                                ?: error("Unable to get default display.")
                        )
                    initialValue = setOf(defaultDisplay)
                )
        } else {
            oldEnabledDisplays
@@ -344,9 +345,10 @@ constructor(
            .debugLog("pendingDisplay")

    override val defaultDisplayOff: Flow<Boolean> =
        displays
            .map { displays -> displays.firstOrNull { it.displayId == Display.DEFAULT_DISPLAY } }
            .map { it?.state == Display.STATE_OFF }
        displayChangeEvent
            .filter { it == Display.DEFAULT_DISPLAY }
            .map { defaultDisplay.state == Display.STATE_OFF }
            .distinctUntilChanged()

    private fun <T> Flow<T>.debugLog(flowName: String): Flow<T> {
        return if (DEBUG) {
+10 −12
Original line number Diff line number Diff line
@@ -59,12 +59,14 @@ class DisplayRepositoryTest : SysuiTestCase() {

    private val testHandler = FakeHandler(Looper.getMainLooper())
    private val testScope = TestScope(UnconfinedTestDispatcher())
    private val defaultDisplay =
        display(type = TYPE_INTERNAL, id = DEFAULT_DISPLAY, state = Display.STATE_ON)

    private lateinit var displayRepository: DisplayRepositoryImpl

    @Before
    fun setup() {
        setDisplays(DEFAULT_DISPLAY)
        setDisplays(listOf(defaultDisplay))
        setAllDisplaysIncludingDisabled(DEFAULT_DISPLAY)
        displayRepository =
            DisplayRepositoryImpl(
@@ -434,19 +436,12 @@ class DisplayRepositoryTest : SysuiTestCase() {
    fun defaultDisplayOff_changes() =
        testScope.runTest {
            val defaultDisplayOff by latestDefaultDisplayOffFlowValue()
            setDisplays(
                listOf(
                    display(type = TYPE_INTERNAL, id = DEFAULT_DISPLAY, state = Display.STATE_OFF)
                )
            )

            whenever(defaultDisplay.state).thenReturn(Display.STATE_OFF)
            displayListener.value.onDisplayChanged(DEFAULT_DISPLAY)
            assertThat(defaultDisplayOff).isTrue()

            setDisplays(
                listOf(
                    display(type = TYPE_INTERNAL, id = DEFAULT_DISPLAY, state = Display.STATE_ON)
                )
            )
            whenever(defaultDisplay.state).thenReturn(Display.STATE_ON)
            displayListener.value.onDisplayChanged(DEFAULT_DISPLAY)
            assertThat(defaultDisplayOff).isFalse()
        }
@@ -545,7 +540,10 @@ class DisplayRepositoryTest : SysuiTestCase() {
    }

    private fun setAllDisplaysIncludingDisabled(vararg ids: Int) {
        val displays = ids.map { display(type = TYPE_EXTERNAL, id = it) }.toTypedArray()
        val displays =
            (ids.toSet() - DEFAULT_DISPLAY) // Default display always added.
                .map { display(type = TYPE_EXTERNAL, id = it) }
                .toTypedArray() + defaultDisplay
        whenever(
                displayManager.getDisplays(
                    eq(DisplayManager.DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED)