[SB Refactor] Don't update the network name if the subId doesn't match.

Fixes: 268340701
Bug: 238425913
Test: atest MobileConnectionRepositoryTest
Change-Id: Id2e73b834a1dc1bcffc7c62596b1f41c9612e649
This commit is contained in:
Caitlin Shkuratov
2023-02-08 17:09:29 +00:00
parent d0d9fbe3cb
commit f57212f00b
2 changed files with 47 additions and 15 deletions

View File

@@ -61,6 +61,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.mapNotNull
@@ -267,15 +268,14 @@ class MobileConnectionRepositoryImpl(
override val networkName: StateFlow<NetworkNameModel> =
broadcastDispatcher
.broadcastFlow(IntentFilter(TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED)) {
intent,
_ ->
if (intent.getIntExtra(EXTRA_SUBSCRIPTION_ID, INVALID_SUBSCRIPTION_ID) != subId) {
defaultNetworkName
} else {
intent.toNetworkNameModel(networkNameSeparator) ?: defaultNetworkName
}
.broadcastFlow(
filter = IntentFilter(TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED),
map = { intent, _ -> intent },
)
.filter { intent ->
intent.getIntExtra(EXTRA_SUBSCRIPTION_ID, INVALID_SUBSCRIPTION_ID) == subId
}
.map { intent -> intent.toNetworkNameModel(networkNameSeparator) ?: defaultNetworkName }
.stateIn(scope, SharingStarted.WhileSubscribed(), defaultNetworkName)
override val dataEnabled = run {

View File

@@ -24,7 +24,6 @@ import android.telephony.ServiceState
import android.telephony.ServiceState.STATE_IN_SERVICE
import android.telephony.ServiceState.STATE_OUT_OF_SERVICE
import android.telephony.SignalStrength
import android.telephony.SubscriptionInfo
import android.telephony.TelephonyCallback
import android.telephony.TelephonyCallback.DataActivityListener
import android.telephony.TelephonyCallback.ServiceStateListener
@@ -556,16 +555,51 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
}
@Test
fun `network name - broadcast not for this sub id - returns default`() =
fun `network name - broadcast not for this sub id - keeps old value`() =
runBlocking(IMMEDIATE) {
var latest: NetworkNameModel? = null
val job = underTest.networkName.onEach { latest = it }.launchIn(this)
val intent = spnIntent(subId = 101)
val intent = spnIntent()
fakeBroadcastDispatcher.registeredReceivers.forEach { receiver ->
receiver.onReceive(context, intent)
}
assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP))
// WHEN an intent with a different subId is sent
val wrongSubIntent = spnIntent(subId = 101)
fakeBroadcastDispatcher.registeredReceivers.forEach { receiver ->
receiver.onReceive(context, wrongSubIntent)
}
// THEN the previous intent's name is still used
assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP))
job.cancel()
}
@Test
fun `network name - broadcast has no data - updates to default`() =
runBlocking(IMMEDIATE) {
var latest: NetworkNameModel? = null
val job = underTest.networkName.onEach { latest = it }.launchIn(this)
val intent = spnIntent()
fakeBroadcastDispatcher.registeredReceivers.forEach { receiver ->
receiver.onReceive(context, intent)
}
assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP))
val intentWithoutInfo =
spnIntent(
showSpn = false,
showPlmn = false,
)
fakeBroadcastDispatcher.registeredReceivers.forEach { receiver ->
receiver.onReceive(context, intentWithoutInfo)
}
assertThat(latest).isEqualTo(DEFAULT_NAME)
@@ -573,7 +607,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
}
@Test
fun `network name - operatorAlphaShort - tracked`() =
fun `operatorAlphaShort - tracked`() =
runBlocking(IMMEDIATE) {
var latest: String? = null
@@ -703,8 +737,6 @@ class MobileConnectionRepositoryTest : SysuiTestCase() {
companion object {
private val IMMEDIATE = Dispatchers.Main.immediate
private const val SUB_1_ID = 1
private val SUB_1 =
mock<SubscriptionInfo>().also { whenever(it.subscriptionId).thenReturn(SUB_1_ID) }
private val DEFAULT_NAME = NetworkNameModel.Default("default name")
private const val SEP = "-"