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

Commit b7ca87d5 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Merge cherrypicks of ['googleplex-android-review.googlesource.com/35809832'] into 25Q4-release.

Change-Id: Ie78d99ac6e7e692e78a90d456df5c5b4677919a0
parents 98b75e3e 5e6ef9ac
Loading
Loading
Loading
Loading
+15 −13
Original line number Original line Diff line number Diff line
@@ -87,18 +87,12 @@ open class SuggestedDeviceConnectionManager(
        var scanStarted = false
        var scanStarted = false
        try {
        try {
            val suggestedRouteId = suggestedDeviceState.suggestedDeviceInfo.routeId
            val suggestedRouteId = suggestedDeviceState.suggestedDeviceInfo.routeId
            val deviceFromCache =
                getDeviceByRouteId(localMediaManager.mediaDevices, suggestedRouteId)
            val deviceToConnect =
                deviceFromCache?.also { Log.i(TAG, "Device from cache found.") }
                    ?: run {
                        Log.i(TAG, "Scanning for device.")
            // Start listening before starting scan to avoid missing events.
            // Start listening before starting scan to avoid missing events.
                        val scanResultDeferred = async { awaitScanForDevice(suggestedRouteId) }
            val deviceDiscoveryResult = async { awaitForDevice(suggestedRouteId) }
            localMediaManager.startScan()
            localMediaManager.startScan()
            scanStarted = true
            scanStarted = true
                        scanResultDeferred.await()
            val deviceToConnect = deviceDiscoveryResult.await()
                    }

            if (deviceToConnect == null) {
            if (deviceToConnect == null) {
                Log.w(TAG, "Failed to find a device to connect to. routeId = $suggestedRouteId")
                Log.w(TAG, "Failed to find a device to connect to. routeId = $suggestedRouteId")
                return@coroutineScope false
                return@coroutineScope false
@@ -110,7 +104,15 @@ open class SuggestedDeviceConnectionManager(
        }
        }
    }
    }


    private suspend fun awaitScanForDevice(suggestedRouteId: String): MediaDevice? {
    private suspend fun awaitForDevice(suggestedRouteId: String): MediaDevice? {
        val deviceFromCache =
            getDeviceByRouteId(localMediaManager.mediaDevices, suggestedRouteId)
        deviceFromCache?.let {
            Log.i(TAG, "Device from cache found.")
            return it
        }

        Log.i(TAG, "Scanning for device.")
        var callback: LocalMediaManager.DeviceCallback? = null
        var callback: LocalMediaManager.DeviceCallback? = null
        try {
        try {
            return withTimeoutOrNull(SCAN_TIMEOUT) {
            return withTimeoutOrNull(SCAN_TIMEOUT) {
+2 −1
Original line number Original line Diff line number Diff line
@@ -120,8 +120,9 @@ class SuggestedDeviceConnectionManagerTest {
        )
        )
        runCurrent()
        runCurrent()


        // Scan should be started anyway to ensure the device is discoverable during connection.
        verify(localMediaManager).startScan()
        verify(localMediaManager).connectDevice(mediaDevice1, ROUTING_CHANGE_INFO)
        verify(localMediaManager).connectDevice(mediaDevice1, ROUTING_CHANGE_INFO)
        verify(localMediaManager, never()).startScan()
        assertThat(deviceCallbacks).hasSize(1) // Callback for connection state change
        assertThat(deviceCallbacks).hasSize(1) // Callback for connection state change
    }
    }