diff options
| author | 2024-09-19 06:47:39 +0000 | |
|---|---|---|
| committer | 2024-09-19 06:47:39 +0000 | |
| commit | 392246dc1b783a40acd5cf64aa8867e83745c0ce (patch) | |
| tree | 74f4fc2ae679910d1fbc68baadcb376bc0ef30cc | |
| parent | 3f5f810433792a85d8ab7d844978d4d254006004 (diff) | |
| parent | c72852fa46cb63b77632c979dec188120d7d975b (diff) | |
Merge "Move all service connection operation to background thread" into main
2 files changed, 27 insertions, 17 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt index 7eae5b2a1f5f..3d8ff86c9377 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt @@ -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, ) @@ -263,21 +264,30 @@ class DeviceSettingServiceConnection( transform: ((IBinder) -> T), ): Flow<ServiceConnectionStatus<T>> { return callbackFlow { - val serviceConnection = - object : ServiceConnection { - override fun onServiceConnected(name: ComponentName, service: IBinder) { - launch { send(ServiceConnectionStatus.Connected(transform(service))) } - } + val serviceConnection = + object : ServiceConnection { + override fun onServiceConnected(name: ComponentName, service: IBinder) { + launch { send(ServiceConnectionStatus.Connected(transform(service))) } + } - override fun onServiceDisconnected(name: ComponentName?) { - launch { send(ServiceConnectionStatus.Connecting) } + override fun onServiceDisconnected(name: ComponentName?) { + launch { send(ServiceConnectionStatus.Connecting) } + } } + if ( + !context.bindService( + intent, + Context.BIND_AUTO_CREATE, + { launch { it.run() } }, + serviceConnection, + ) + ) { + Log.w(TAG, "Fail to bind service $intent") + launch { send(ServiceConnectionStatus.Failed) } } - if (!context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)) { - launch { send(ServiceConnectionStatus.Failed) } + awaitClose { context.unbindService(serviceConnection) } } - awaitClose { context.unbindService(serviceConnection) } - } + .flowOn(backgroundCoroutineContext) } private suspend fun tryGetEndpointFromMetadata(cachedDevice: CachedBluetoothDevice): EndPoint? = diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt index 81b56343ceed..0cb6bc1b1261 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt @@ -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) |