Merge "[SB Refactor] Update the wifi network flow to use stateIn so that new subscribers will receive the current value." into tm-qpr-dev am: 716ba8ea29
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/19755228 Change-Id: I20c711f860eceae9cad25a02bc234d08245387f7 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -46,7 +46,7 @@ import kotlinx.coroutines.channels.awaitClose
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
import kotlinx.coroutines.flow.shareIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides data related to the wifi state.
|
* Provides data related to the wifi state.
|
||||||
@@ -118,12 +118,19 @@ class WifiRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trySend(WIFI_NETWORK_DEFAULT)
|
|
||||||
connectivityManager.registerNetworkCallback(WIFI_NETWORK_CALLBACK_REQUEST, callback)
|
connectivityManager.registerNetworkCallback(WIFI_NETWORK_CALLBACK_REQUEST, callback)
|
||||||
|
|
||||||
awaitClose { connectivityManager.unregisterNetworkCallback(callback) }
|
awaitClose { connectivityManager.unregisterNetworkCallback(callback) }
|
||||||
}
|
}
|
||||||
.shareIn(scope, started = SharingStarted.WhileSubscribed())
|
// There will be multiple wifi icons in different places that will frequently
|
||||||
|
// subscribe/unsubscribe to flows as the views attach/detach. Using [stateIn] ensures that
|
||||||
|
// new subscribes will get the latest value immediately upon subscription. Otherwise, the
|
||||||
|
// views could show stale data. See b/244173280.
|
||||||
|
.stateIn(
|
||||||
|
scope,
|
||||||
|
started = SharingStarted.WhileSubscribed(),
|
||||||
|
initialValue = WIFI_NETWORK_DEFAULT
|
||||||
|
)
|
||||||
|
|
||||||
override val wifiActivity: Flow<WifiActivityModel> =
|
override val wifiActivity: Flow<WifiActivityModel> =
|
||||||
if (wifiManager == null) {
|
if (wifiManager == null) {
|
||||||
|
|||||||
@@ -473,6 +473,40 @@ class WifiRepositoryImplTest : SysuiTestCase() {
|
|||||||
job.cancel()
|
job.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Regression test for b/244173280. */
|
||||||
|
@Test
|
||||||
|
fun wifiNetwork_multipleSubscribers_newSubscribersGetCurrentValue() = runBlocking(IMMEDIATE) {
|
||||||
|
var latest1: WifiNetworkModel? = null
|
||||||
|
val job1 = underTest
|
||||||
|
.wifiNetwork
|
||||||
|
.onEach { latest1 = it }
|
||||||
|
.launchIn(this)
|
||||||
|
|
||||||
|
getNetworkCallback()
|
||||||
|
.onCapabilitiesChanged(NETWORK, createWifiNetworkCapabilities(PRIMARY_WIFI_INFO))
|
||||||
|
|
||||||
|
assertThat(latest1 is WifiNetworkModel.Active).isTrue()
|
||||||
|
val latest1Active = latest1 as WifiNetworkModel.Active
|
||||||
|
assertThat(latest1Active.networkId).isEqualTo(NETWORK_ID)
|
||||||
|
assertThat(latest1Active.ssid).isEqualTo(SSID)
|
||||||
|
|
||||||
|
// WHEN we add a second subscriber after having already emitted a value
|
||||||
|
var latest2: WifiNetworkModel? = null
|
||||||
|
val job2 = underTest
|
||||||
|
.wifiNetwork
|
||||||
|
.onEach { latest2 = it }
|
||||||
|
.launchIn(this)
|
||||||
|
|
||||||
|
// THEN the second subscribe receives the already-emitted value
|
||||||
|
assertThat(latest2 is WifiNetworkModel.Active).isTrue()
|
||||||
|
val latest2Active = latest2 as WifiNetworkModel.Active
|
||||||
|
assertThat(latest2Active.networkId).isEqualTo(NETWORK_ID)
|
||||||
|
assertThat(latest2Active.ssid).isEqualTo(SSID)
|
||||||
|
|
||||||
|
job1.cancel()
|
||||||
|
job2.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun wifiActivity_nullWifiManager_receivesDefault() = runBlocking(IMMEDIATE) {
|
fun wifiActivity_nullWifiManager_receivesDefault() = runBlocking(IMMEDIATE) {
|
||||||
underTest = WifiRepositoryImpl(
|
underTest = WifiRepositoryImpl(
|
||||||
|
|||||||
Reference in New Issue
Block a user