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

Commit c72852fa authored by Haijie Hong's avatar Haijie Hong
Browse files

Move all service connection operation to background thread

BUG: 343317785
Test: atest DeviceSettingRepositoryTest
Flag: com.android.settings.flags.enable_bluetooth_device_details_polish
Change-Id: I3cfbbee25345d7f45882754a8f2b0c133a81e4f6
parent 48cdc816
Loading
Loading
Loading
Loading
+24 −14
Original line number Diff line number Diff line
@@ -56,11 +56,13 @@ import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext

@OptIn(ExperimentalCoroutinesApi::class)
@@ -101,8 +103,7 @@ class DeviceSettingServiceConnection(
                    } else if (allStatus.all { it is ServiceConnectionStatus.Connected }) {
                        allStatus
                            .filterIsInstance<
                                ServiceConnectionStatus.Connected<
                                        IDeviceSettingsProviderService>
                                ServiceConnectionStatus.Connected<IDeviceSettingsProviderService>
                            >()
                            .all { it.service.serviceStatus?.enabled == true }
                    } else {
@@ -232,7 +233,7 @@ class DeviceSettingServiceConnection(
                                IDeviceSettingsProviderService.Stub::asInterface,
                            )
                            .stateIn(
                                coroutineScope,
                                coroutineScope.plus(backgroundCoroutineContext),
                                SharingStarted.WhileSubscribed(),
                                ServiceConnectionStatus.Connecting,
                            )
@@ -273,11 +274,20 @@ class DeviceSettingServiceConnection(
                            launch { send(ServiceConnectionStatus.Connecting) }
                        }
                    }
            if (!context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)) {
                if (
                    !context.bindService(
                        intent,
                        Context.BIND_AUTO_CREATE,
                        { launch { it.run() } },
                        serviceConnection,
                    )
                ) {
                    Log.w(TAG, "Fail to bind service $intent")
                    launch { send(ServiceConnectionStatus.Failed) }
                }
                awaitClose { context.unbindService(serviceConnection) }
            }
            .flowOn(backgroundCoroutineContext)
    }

    private suspend fun tryGetEndpointFromMetadata(cachedDevice: CachedBluetoothDevice): EndPoint? =
+3 −3
Original line number Diff line number Diff line
@@ -102,9 +102,9 @@ class DeviceSettingRepositoryTest {
        `when`(settingProviderService2.queryLocalInterface(anyString()))
            .thenReturn(settingProviderService2)

        `when`(context.bindService(any(), any(), anyInt())).then { input ->
        `when`(context.bindService(any(), anyInt(), any(), any())).then { input ->
            val intent = input.getArgument<Intent?>(0)
            val connection = input.getArgument<ServiceConnection>(1)
            val connection = input.getArgument<ServiceConnection>(3)

            when (intent?.action) {
                CONFIG_SERVICE_INTENT_ACTION ->
@@ -210,7 +210,7 @@ class DeviceSettingRepositoryTest {
    fun getDeviceSettingsConfig_bindingServiceFail_returnNull() {
        testScope.runTest {
            `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG)
            doReturn(false).`when`(context).bindService(any(), any(), anyInt())
            doReturn(false).`when`(context).bindService(any(), anyInt(), any(), any())

            val config = underTest.getDeviceSettingsConfig(cachedDevice)