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

Commit 899849fc authored by Evan Laird's avatar Evan Laird Committed by Android (Google) Code Review
Browse files

Merge "[Networking] Use SharingStarted.Eagerly for broadcast" into 24D1-dev

parents 2483fb08 1c7a0d34
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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)
+8 −2
Original line number Diff line number Diff line
@@ -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<NetworkNameModel> =
        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
+18 −0
Original line number Diff line number Diff line
@@ -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<BroadcastReceiver>()
            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 {