Merge "[Status Bar] Fetch WifiManager.isWifiEnabled in the background." into udc-qpr-dev am: 5fd0554514

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/24010823

Change-Id: I4454c01b2472b0ca1f8411d6c0aada825e43b55b
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Caitlin Shkuratov
2023-07-11 16:24:20 +00:00
committed by Automerger Merge Worker
4 changed files with 22 additions and 6 deletions

View File

@@ -34,6 +34,7 @@ import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.dagger.qualifiers.Main import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.log.table.TableLogBuffer import com.android.systemui.log.table.TableLogBuffer
import com.android.systemui.log.table.logDiffsForTable import com.android.systemui.log.table.logDiffsForTable
@@ -48,6 +49,7 @@ import com.android.systemui.statusbar.pipeline.wifi.shared.WifiInputLogger
import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel
import java.util.concurrent.Executor import java.util.concurrent.Executor
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
@@ -60,7 +62,9 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
/** Real implementation of [WifiRepository]. */ /** Real implementation of [WifiRepository]. */
@Suppress("EXPERIMENTAL_IS_NOT_ENABLED") @Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
@@ -76,8 +80,9 @@ constructor(
logger: WifiInputLogger, logger: WifiInputLogger,
@WifiTableLog wifiTableLogBuffer: TableLogBuffer, @WifiTableLog wifiTableLogBuffer: TableLogBuffer,
@Main mainExecutor: Executor, @Main mainExecutor: Executor,
@Background private val bgDispatcher: CoroutineDispatcher,
@Application scope: CoroutineScope, @Application scope: CoroutineScope,
wifiManager: WifiManager, private val wifiManager: WifiManager,
) : RealWifiRepository { ) : RealWifiRepository {
private val wifiStateChangeEvents: Flow<Unit> = private val wifiStateChangeEvents: Flow<Unit> =
@@ -93,20 +98,25 @@ constructor(
// have changed. // have changed.
override val isWifiEnabled: StateFlow<Boolean> = override val isWifiEnabled: StateFlow<Boolean> =
merge(wifiNetworkChangeEvents, wifiStateChangeEvents) merge(wifiNetworkChangeEvents, wifiStateChangeEvents)
.mapLatest { wifiManager.isWifiEnabled } .onStart { emit(Unit) }
.mapLatest { isWifiEnabled() }
.distinctUntilChanged() .distinctUntilChanged()
.logDiffsForTable( .logDiffsForTable(
wifiTableLogBuffer, wifiTableLogBuffer,
columnPrefix = "", columnPrefix = "",
columnName = "isEnabled", columnName = "isEnabled",
initialValue = wifiManager.isWifiEnabled, initialValue = false,
) )
.stateIn( .stateIn(
scope = scope, scope = scope,
started = SharingStarted.WhileSubscribed(), started = SharingStarted.Eagerly,
initialValue = wifiManager.isWifiEnabled, initialValue = false,
) )
// [WifiManager.isWifiEnabled] is a blocking IPC call, so fetch it in the background.
private suspend fun isWifiEnabled(): Boolean =
withContext(bgDispatcher) { wifiManager.isWifiEnabled }
override val isWifiDefault: StateFlow<Boolean> = override val isWifiDefault: StateFlow<Boolean> =
connectivityRepository.defaultConnections connectivityRepository.defaultConnections
// TODO(b/274493701): Should wifi be considered default if it's carrier merged? // TODO(b/274493701): Should wifi be considered default if it's carrier merged?
@@ -289,6 +299,7 @@ constructor(
private val logger: WifiInputLogger, private val logger: WifiInputLogger,
@WifiTableLog private val wifiTableLogBuffer: TableLogBuffer, @WifiTableLog private val wifiTableLogBuffer: TableLogBuffer,
@Main private val mainExecutor: Executor, @Main private val mainExecutor: Executor,
@Background private val bgDispatcher: CoroutineDispatcher,
@Application private val scope: CoroutineScope, @Application private val scope: CoroutineScope,
) { ) {
fun create(wifiManager: WifiManager): WifiRepositoryImpl { fun create(wifiManager: WifiManager): WifiRepositoryImpl {
@@ -299,6 +310,7 @@ constructor(
logger, logger,
wifiTableLogBuffer, wifiTableLogBuffer,
mainExecutor, mainExecutor,
bgDispatcher,
scope, scope,
wifiManager, wifiManager,
) )

View File

@@ -159,6 +159,7 @@ class MobileConnectionsRepositoryTest : SysuiTestCase() {
mock(), mock(),
mock(), mock(),
FakeExecutor(FakeSystemClock()), FakeExecutor(FakeSystemClock()),
dispatcher,
testScope.backgroundScope, testScope.backgroundScope,
mock(), mock(),
) )

View File

@@ -83,6 +83,7 @@ class WifiRepositorySwitcherTest : SysuiTestCase() {
logger, logger,
tableLogger, tableLogger,
mainExecutor, mainExecutor,
testDispatcher,
testScope.backgroundScope, testScope.backgroundScope,
wifiManager, wifiManager,
) )

View File

@@ -76,7 +76,8 @@ class WifiRepositoryImplTest : SysuiTestCase() {
private lateinit var executor: Executor private lateinit var executor: Executor
private lateinit var connectivityRepository: ConnectivityRepository private lateinit var connectivityRepository: ConnectivityRepository
private val testScope = TestScope(UnconfinedTestDispatcher()) private val dispatcher = UnconfinedTestDispatcher()
private val testScope = TestScope(dispatcher)
@Before @Before
fun setUp() { fun setUp() {
@@ -1301,6 +1302,7 @@ class WifiRepositoryImplTest : SysuiTestCase() {
logger, logger,
tableLogger, tableLogger,
executor, executor,
dispatcher,
testScope.backgroundScope, testScope.backgroundScope,
wifiManager, wifiManager,
) )