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

Commit a3c24fe0 authored by Evan Laird's avatar Evan Laird
Browse files

[Sb refactor] switch broadcast off of BroadcastDispatcher

`ACTION_SERVICE_PROVIDERS_UPDATED` is a sticky broadcast, so we avoid
using the BroadcastDispatcher so that we never miss the message due to
sticky behavior.

Also fix the incorrect intent extra, as this broadcast uses
`EXTRA_SUBSCRIPTION_INDEX` rather than the subscription_id field common
in some other telephony broadcasts.

Test: MobileConnectionRepositoryTest
Bug: 291321279
Change-Id: I513abe7904a48cd5022ffeed15ebb7933ad5953e
parent 018e1a1f
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.systemui.statusbar.pipeline.mobile.data

import android.content.Intent
import android.telephony.ServiceState
import android.telephony.SignalStrength
import android.telephony.TelephonyDisplayInfo
import android.telephony.TelephonyManager
import com.android.settingslib.SignalIcon
import com.android.settingslib.mobile.MobileMappings
import com.android.systemui.dagger.SysUISingleton
@@ -162,6 +164,28 @@ constructor(
    fun logOnSubscriptionsChanged() {
        buffer.log(TAG, LogLevel.INFO, {}, { "onSubscriptionsChanged" })
    }

    fun logServiceProvidersUpdatedBroadcast(intent: Intent) {
        val showSpn = intent.getBooleanExtra(TelephonyManager.EXTRA_SHOW_SPN, false)
        val spn = intent.getStringExtra(TelephonyManager.EXTRA_DATA_SPN)
        val showPlmn = intent.getBooleanExtra(TelephonyManager.EXTRA_SHOW_PLMN, false)
        val plmn = intent.getStringExtra(TelephonyManager.EXTRA_PLMN)

        buffer.log(
            TAG,
            LogLevel.INFO,
            {
                bool1 = showSpn
                str1 = spn
                bool2 = showPlmn
                str2 = plmn
            },
            {
                "Intent: ACTION_SERVICE_PROVIDERS_UPDATED." +
                    " showSpn=$bool1 spn=$str1 showPlmn=$bool2 plmn=$str2"
            }
        )
    }
}

private const val TAG = "MobileInputLog"
+35 −8
Original line number Diff line number Diff line
@@ -16,12 +16,16 @@

package com.android.systemui.statusbar.pipeline.mobile.data.repository.prod

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.telephony.CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN
import android.telephony.CellSignalStrengthCdma
import android.telephony.ServiceState
import android.telephony.SignalStrength
import android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX
import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID
import android.telephony.TelephonyCallback
import android.telephony.TelephonyDisplayInfo
@@ -34,6 +38,7 @@ import android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN
import android.telephony.TelephonyManager.UNKNOWN_CARRIER_ID
import com.android.settingslib.Utils
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.log.table.TableLogBuffer
@@ -81,6 +86,7 @@ import kotlinx.coroutines.flow.stateIn
@OptIn(ExperimentalCoroutinesApi::class)
class MobileConnectionRepositoryImpl(
    override val subId: Int,
    private val context: Context,
    subscriptionModel: StateFlow<SubscriptionModel?>,
    defaultNetworkName: NetworkNameModel,
    networkNameSeparator: String,
@@ -323,16 +329,35 @@ class MobileConnectionRepositoryImpl(
            }
            .stateIn(scope, SharingStarted.WhileSubscribed(), telephonyManager.simCarrierId)

    /** BroadcastDispatcher does not handle sticky broadcasts, so we can't use it here */
    @SuppressLint("RegisterReceiverViaContext")
    override val networkName: StateFlow<NetworkNameModel> =
        broadcastDispatcher
            .broadcastFlow(
                filter = IntentFilter(TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED),
                map = { intent, _ -> intent },
        conflatedCallbackFlow {
                val receiver =
                    object : BroadcastReceiver() {
                        override fun onReceive(context: Context, intent: Intent) {
                            if (
                                intent.getIntExtra(
                                    EXTRA_SUBSCRIPTION_INDEX,
                                    INVALID_SUBSCRIPTION_ID
                                ) == subId
                            ) {
                                logger.logServiceProvidersUpdatedBroadcast(intent)
                                trySend(
                                    intent.toNetworkNameModel(networkNameSeparator)
                                        ?: defaultNetworkName
                                )
            .filter { intent ->
                intent.getIntExtra(EXTRA_SUBSCRIPTION_ID, INVALID_SUBSCRIPTION_ID) == subId
                            }
            .map { intent -> intent.toNetworkNameModel(networkNameSeparator) ?: defaultNetworkName }
                        }
                    }

                context.registerReceiver(
                    receiver,
                    IntentFilter(TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED)
                )

                awaitClose { context.unregisterReceiver(receiver) }
            }
            .stateIn(scope, SharingStarted.WhileSubscribed(), defaultNetworkName)

    override val dataEnabled = run {
@@ -349,6 +374,7 @@ class MobileConnectionRepositoryImpl(
    class Factory
    @Inject
    constructor(
        private val context: Context,
        private val broadcastDispatcher: BroadcastDispatcher,
        private val telephonyManager: TelephonyManager,
        private val logger: MobileInputLogger,
@@ -366,6 +392,7 @@ class MobileConnectionRepositoryImpl(
        ): MobileConnectionRepository {
            return MobileConnectionRepositoryImpl(
                subId,
                context,
                subscriptionModel,
                defaultNetworkName,
                networkNameSeparator,
+1 −0
Original line number Diff line number Diff line
@@ -674,6 +674,7 @@ class FullMobileConnectionRepositoryTest : SysuiTestCase() {
        val realRepo =
            MobileConnectionRepositoryImpl(
                SUB_ID,
                context,
                subscriptionModel,
                DEFAULT_NAME_MODEL,
                SEP,
+22 −6
Original line number Diff line number Diff line
@@ -16,18 +16,22 @@

package com.android.systemui.statusbar.pipeline.mobile.data.repository.prod

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.CarrierConfigManager.KEY_INFLATE_SIGNAL_STRENGTH_BOOL
import android.telephony.NetworkRegistrationInfo
import android.telephony.ServiceState
import android.telephony.ServiceState.STATE_IN_SERVICE
import android.telephony.ServiceState.STATE_OUT_OF_SERVICE
import android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX
import android.telephony.TelephonyCallback
import android.telephony.TelephonyCallback.DataActivityListener
import android.telephony.TelephonyCallback.ServiceStateListener
import android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA
import android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE
import android.telephony.TelephonyManager
import android.telephony.TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED
import android.telephony.TelephonyManager.DATA_ACTIVITY_DORMANT
import android.telephony.TelephonyManager.DATA_ACTIVITY_IN
import android.telephony.TelephonyManager.DATA_ACTIVITY_INOUT
@@ -75,6 +79,7 @@ import com.android.systemui.statusbar.pipeline.mobile.util.FakeMobileMappingsPro
import com.android.systemui.statusbar.pipeline.shared.data.model.DataActivityModel
import com.android.systemui.statusbar.pipeline.shared.data.model.toMobileDataActivityModel
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
@@ -88,6 +93,7 @@ import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
@@ -100,6 +106,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
    @Mock private lateinit var telephonyManager: TelephonyManager
    @Mock private lateinit var logger: MobileInputLogger
    @Mock private lateinit var tableLogger: TableLogBuffer
    @Mock private lateinit var context: Context

    private val mobileMappings = FakeMobileMappingsProxy()
    private val systemUiCarrierConfig =
@@ -129,6 +136,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
        underTest =
            MobileConnectionRepositoryImpl(
                SUB_1_ID,
                context,
                subscriptionModel,
                DEFAULT_NAME_MODEL,
                SEP,
@@ -706,7 +714,9 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
            val job = underTest.networkName.onEach { latest = it }.launchIn(this)

            val intent = spnIntent()
            fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent)
            val captor = argumentCaptor<BroadcastReceiver>()
            verify(context).registerReceiver(captor.capture(), any())
            captor.value!!.onReceive(context, intent)

            assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP))

@@ -720,13 +730,16 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
            val job = underTest.networkName.onEach { latest = it }.launchIn(this)

            val intent = spnIntent()
            fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent)
            val captor = argumentCaptor<BroadcastReceiver>()
            verify(context).registerReceiver(captor.capture(), any())
            captor.value!!.onReceive(context, intent)

            assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP))

            // WHEN an intent with a different subId is sent
            val wrongSubIntent = spnIntent(subId = 101)

            fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, wrongSubIntent)
            captor.value!!.onReceive(context, wrongSubIntent)

            // THEN the previous intent's name is still used
            assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP))
@@ -741,7 +754,10 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
            val job = underTest.networkName.onEach { latest = it }.launchIn(this)

            val intent = spnIntent()
            fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent)
            val captor = argumentCaptor<BroadcastReceiver>()
            verify(context).registerReceiver(captor.capture(), any())
            captor.value!!.onReceive(context, intent)

            assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP))

            val intentWithoutInfo =
@@ -750,7 +766,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
                    showPlmn = false,
                )

            fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intentWithoutInfo)
            captor.value!!.onReceive(context, intentWithoutInfo)

            assertThat(latest).isEqualTo(DEFAULT_NAME_MODEL)

@@ -893,7 +909,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
        plmn: String = PLMN,
    ): Intent =
        Intent(TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED).apply {
            putExtra(EXTRA_SUBSCRIPTION_ID, subId)
            putExtra(EXTRA_SUBSCRIPTION_INDEX, subId)
            putExtra(EXTRA_SHOW_SPN, showSpn)
            putExtra(EXTRA_SPN, spn)
            putExtra(EXTRA_SHOW_PLMN, showPlmn)
+1 −0
Original line number Diff line number Diff line
@@ -125,6 +125,7 @@ class MobileConnectionTelephonySmokeTests : SysuiTestCase() {
        underTest =
            MobileConnectionRepositoryImpl(
                SUB_1_ID,
                context,
                subscriptionModel,
                DEFAULT_NAME,
                SEP,
Loading