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

Commit 5e6ef9ac authored by Alex Shabalin's avatar Alex Shabalin Committed by Android Build Coastguard Worker
Browse files

Start device scan even if device is already discovered.

The SuggestedDeviceConnectionManager should ensure that scan is always
active during the connection attempt, which means that `startScan`
should be called before `connect` and `stopScan` should be called only
after the connection attempt is finished.

Fix: 445443975
Test: atest SuggestedDeviceConnectionManagerTest
Flag: com.android.media.flags.use_suggested_device_connection_manager
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:76d45a5a5a48166db1874d04711ee0422057fe09
Merged-In: Id5248da30fc035f5946f18db5b3405a131a9f4cf
Change-Id: Id5248da30fc035f5946f18db5b3405a131a9f4cf
parent 98b75e3e
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
    }
    }