From e702a34a01d82d9e8183d9b7308406575aa434d4 Mon Sep 17 00:00:00 2001 From: Evan Laird Date: Mon, 25 Sep 2023 17:03:55 -0400 Subject: [Sb] flowOn(bgDispatcher) to IPC calls Any place in the mobile repositories that used a `conflatedCallbackFlow` or `callbackFlow` which registered an IPC is now defined to `flowOn(bgDispatcher)`. This ensures that the initial IPC that registers the callbacks with the system runs on a background thread and doesn't block. Any processing after the `flowOn` still runs in the calling context. Fixes: 290336252 Test: tests in systemui/statusbar/pipeline/ Change-Id: I9748040006701cbcbcafcb84b7b7ddce8f17a8ac --- .../prod/MobileConnectionRepositoryImpl.kt | 3 +++ .../prod/MobileConnectionsRepositoryImpl.kt | 31 ++++++++++++---------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryImpl.kt b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryImpl.kt index cd6862113ee9..dc50990d002a 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryImpl.kt @@ -71,6 +71,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapNotNull @@ -181,6 +182,7 @@ class MobileConnectionRepositoryImpl( telephonyManager.registerTelephonyCallback(bgDispatcher.asExecutor(), callback) awaitClose { telephonyManager.unregisterTelephonyCallback(callback) } } + .flowOn(bgDispatcher) .scan(initial = initial) { state, event -> state.applyEvent(event) } .stateIn(scope = scope, started = SharingStarted.WhileSubscribed(), initial) } @@ -358,6 +360,7 @@ class MobileConnectionRepositoryImpl( awaitClose { context.unregisterReceiver(receiver) } } + .flowOn(bgDispatcher) .stateIn(scope, SharingStarted.WhileSubscribed(), defaultNetworkName) override val dataEnabled = run { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt index 67b04db64463..9800b0c7329a 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt @@ -134,22 +134,24 @@ constructor( ) .stateIn(scope, started = SharingStarted.WhileSubscribed(), null) - private val mobileSubscriptionsChangeEvent: Flow = conflatedCallbackFlow { - val callback = - object : SubscriptionManager.OnSubscriptionsChangedListener() { - override fun onSubscriptionsChanged() { - logger.logOnSubscriptionsChanged() - trySend(Unit) - } - } + private val mobileSubscriptionsChangeEvent: Flow = + conflatedCallbackFlow { + val callback = + object : SubscriptionManager.OnSubscriptionsChangedListener() { + override fun onSubscriptionsChanged() { + logger.logOnSubscriptionsChanged() + trySend(Unit) + } + } - subscriptionManager.addOnSubscriptionsChangedListener( - bgDispatcher.asExecutor(), - callback, - ) + subscriptionManager.addOnSubscriptionsChangedListener( + bgDispatcher.asExecutor(), + callback, + ) - awaitClose { subscriptionManager.removeOnSubscriptionsChangedListener(callback) } - } + awaitClose { subscriptionManager.removeOnSubscriptionsChangedListener(callback) } + } + .flowOn(bgDispatcher) /** * State flow that emits the set of mobile data subscriptions, each represented by its own @@ -184,6 +186,7 @@ constructor( telephonyManager.registerTelephonyCallback(bgDispatcher.asExecutor(), callback) awaitClose { telephonyManager.unregisterTelephonyCallback(callback) } } + .flowOn(bgDispatcher) .distinctUntilChanged() .logDiffsForTable( tableLogger, -- cgit v1.2.3-59-g8ed1b