From 1c7a0d348459ca437c72e928c5ac5540a7d70ada Mon Sep 17 00:00:00 2001 From: Evan Laird Date: Wed, 20 Mar 2024 18:00:29 -0400 Subject: [Networking] Use SharingStarted.Eagerly for broadcast The network name that comes from the ACTION_SERVICE_PROVIDERS_UPDATED intent needs to be always listening because in some cases the sticky broadcast is not representative of the information that we need. Bug: 322432056 Test: MobileConnectionRepositoryTest Test: Reboot device and insert SIM. Check QS internet tile Flag: NONE (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:736a954b04286afed7b5f03621dcd66b4d14da6b) Merged-In: Iaf53aae3d29148765232f05ba15d0a3439d3d9ee Change-Id: Iaf53aae3d29148765232f05ba15d0a3439d3d9ee 24D1-dev is based on 24Q2-release. Therefore, we merged this CL to 24D1-dev. --- .../repository/prod/FullMobileConnectionRepository.kt | 4 ++-- .../repository/prod/MobileConnectionRepositoryImpl.kt | 10 ++++++++-- .../repository/prod/MobileConnectionRepositoryTest.kt | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/FullMobileConnectionRepository.kt b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/FullMobileConnectionRepository.kt index a1241965de7a..cc72a1e3f842 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/FullMobileConnectionRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/FullMobileConnectionRepository.kt @@ -301,7 +301,7 @@ class FullMobileConnectionRepository( .flatMapLatest { it.networkName } .logDiffsForTable( tableLogBuffer, - columnPrefix = "", + columnPrefix = "intent", initialValue = activeRepo.value.networkName.value, ) .stateIn(scope, SharingStarted.WhileSubscribed(), activeRepo.value.networkName.value) @@ -311,7 +311,7 @@ class FullMobileConnectionRepository( .flatMapLatest { it.carrierName } .logDiffsForTable( tableLogBuffer, - columnPrefix = "", + columnPrefix = "sub", initialValue = activeRepo.value.carrierName.value, ) .stateIn(scope, SharingStarted.WhileSubscribed(), activeRepo.value.carrierName.value) 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 77fd6bef8a33..e4a2a6820d5b 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 @@ -358,7 +358,13 @@ class MobileConnectionRepositoryImpl( } .stateIn(scope, SharingStarted.WhileSubscribed(), telephonyManager.simCarrierId) - /** BroadcastDispatcher does not handle sticky broadcasts, so we can't use it here */ + /** + * BroadcastDispatcher does not handle sticky broadcasts, so we can't use it here. Note that we + * now use the [SharingStarted.Eagerly] strategy, because there have been cases where the sticky + * broadcast does not represent the correct state. + * + * See b/322432056 for context. + */ @SuppressLint("RegisterReceiverViaContext") override val networkName: StateFlow = conflatedCallbackFlow { @@ -388,7 +394,7 @@ class MobileConnectionRepositoryImpl( awaitClose { context.unregisterReceiver(receiver) } } .flowOn(bgDispatcher) - .stateIn(scope, SharingStarted.WhileSubscribed(), defaultNetworkName) + .stateIn(scope, SharingStarted.Eagerly, defaultNetworkName) override val dataEnabled = run { val initial = telephonyManager.isDataConnectionAllowed diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt index 98556514f8ec..f761bcfe63d6 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt @@ -867,6 +867,24 @@ class MobileConnectionRepositoryTest : SysuiTestCase() { job.cancel() } + @Test + fun networkName_usingEagerStrategy_retainsNameBetweenSubscribers() = + testScope.runTest { + // Use the [StateFlow.value] getter so we can prove that the collection happens + // even when there is no [Job] + + // Starts out default + assertThat(underTest.networkName.value).isEqualTo(DEFAULT_NAME_MODEL) + + val intent = spnIntent() + val captor = argumentCaptor() + verify(context).registerReceiver(captor.capture(), any()) + captor.value!!.onReceive(context, intent) + + // The value is still there despite no active subscribers + assertThat(underTest.networkName.value).isEqualTo(intent.toNetworkNameModel(SEP)) + } + @Test fun operatorAlphaShort_tracked() = testScope.runTest { -- cgit v1.2.3-59-g8ed1b