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

Commit bbdbfaa7 authored by Daniel Akinola's avatar Daniel Akinola
Browse files

[3/n] Update DisplayManager to persist display connection preference

The new dialog has a checkbox to allow users to save their preferred
connection type for each individual display. To perist this data so it
can be written & read, we are using PersistentDataStore and storing it
as a key value pair in DisplayState, with keys being a displays unique
id.

Bug: 413620089
Test: ConnectedDisplayInteractorTest
Test: DisplayRepositoryTest
Flag: com.android.window.flags.enable_updated_display_connection_dialog
Change-Id: I4fde2c1b23e9aa98de5bb65c8db1ebca913dc566
parent 1367a127
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
@@ -21,9 +21,15 @@ import android.hardware.display.DisplayManager.DisplayListener
import android.hardware.display.DisplayManager.EVENT_TYPE_DISPLAY_ADDED
import android.hardware.display.DisplayManager.EVENT_TYPE_DISPLAY_CHANGED
import android.hardware.display.DisplayManager.EVENT_TYPE_DISPLAY_REMOVED
import android.hardware.display.DisplayManager.EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_ASK
import android.hardware.display.DisplayManager.EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_DESKTOP
import android.hardware.display.DisplayManager.EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_MIRROR
import android.os.Handler
import android.util.Log
import android.view.Display
import com.android.app.displaylib.ExternalDisplayConnectionType.DESKTOP
import com.android.app.displaylib.ExternalDisplayConnectionType.MIRROR
import com.android.app.displaylib.ExternalDisplayConnectionType.NOT_SPECIFIED
import com.android.app.tracing.FlowTracing.traceEach
import com.android.app.tracing.traceSection
import javax.inject.Inject
@@ -113,6 +119,20 @@ interface DisplayRepository {
        /** Id of the pending display. */
        val id: Int

        /**
         * The saved connection preference for the display, either desktop, mirroring or show the
         * dialog. Defaults to [ExternalDisplayConnectionType.NOT_SPECIFIED], if no value saved.
         */
        val connectionType: ExternalDisplayConnectionType

        /**
         * Updates the saved connection preference for the display, triggered by the connection
         * dialog's "remember my choice" checkbox
         *
         * @see com.android.systemui.display.ui.viewmodel.ConnectingDisplayViewModel
         */
        suspend fun updateConnectionPreference(connectionType: ExternalDisplayConnectionType)

        /** Enables the display, making it available to the system. */
        suspend fun enable()

@@ -353,8 +373,28 @@ constructor(
        pendingDisplayId
            .map { displayId ->
                val id = displayId ?: return@map null
                val pendingDisplay = getDisplay(id) ?: displayManager.getDisplay(id)
                val uniqueId = pendingDisplay?.uniqueId ?: return@map null
                val connectionPreference =
                    displayManager.getExternalDisplayConnectionPreference(uniqueId)

                object : DisplayRepository.PendingDisplay {
                    override val id = id
                    override val connectionType: ExternalDisplayConnectionType =
                        when (connectionPreference) {
                            EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_DESKTOP -> DESKTOP
                            EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_MIRROR -> MIRROR
                            else -> NOT_SPECIFIED
                        }

                    override suspend fun updateConnectionPreference(
                        connectionType: ExternalDisplayConnectionType
                    ) {
                        displayManager.setExternalDisplayConnectionPreference(
                            uniqueId,
                            connectionType.preference,
                        )
                    }

                    override suspend fun enable() {
                        traceSection("DisplayRepository#enable($id)") {
@@ -459,6 +499,17 @@ constructor(
    }
}

/**
 * Possible connection types for an external display.
 *
 * @property preference The integer value that represents the connection type in the system.
 */
enum class ExternalDisplayConnectionType(val preference: Int) {
    NOT_SPECIFIED(EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_ASK),
    DESKTOP(EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_DESKTOP),
    MIRROR(EXTERNAL_DISPLAY_CONNECTION_PREFERENCE_MIRROR),
}

/** Used to provide default implementations for all methods. */
private interface DisplayConnectionListener : DisplayListener {