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

Commit 4edfa6ed authored by Quang Luong's avatar Quang Luong
Browse files

Add wifi tile toggle changes to WifiRepository

Add methods in WifiRepository for the planned wifi toggle to
1) Enable wifi
2) Pause wifi
3) Scan for wifi

We will also include a flow for the current expected toggle state, which
will be used in a future CL for optimistic UI updates.

Flag: com.android.systemui.qs_split_internet_tile

Bug: 201143076
Test: WIP
Change-Id: I1add85a5b00194ac9d7efb6a64d7740768be006b
parent 66d250d3
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -154,6 +154,7 @@ class MobileConnectionsRepositoryKairosTest : SysuiTestCase() {
                WifiRepositoryImpl(
                    applicationContext,
                    userRepository,
                    connectivityRepository,
                    applicationCoroutineScope,
                    mainExecutor,
                    testDispatcher,
+6 −0
Original line number Diff line number Diff line
@@ -25,10 +25,12 @@ import com.android.systemui.demomode.DemoModeController
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.table.TableLogBuffer
import com.android.systemui.statusbar.connectivity.WifiPickerTrackerFactory
import com.android.systemui.statusbar.pipeline.shared.data.repository.connectivityRepository
import com.android.systemui.statusbar.pipeline.wifi.data.repository.demo.DemoModeWifiDataSource
import com.android.systemui.statusbar.pipeline.wifi.data.repository.demo.DemoWifiRepository
import com.android.systemui.statusbar.pipeline.wifi.data.repository.demo.model.FakeWifiEventModel
import com.android.systemui.statusbar.pipeline.wifi.data.repository.prod.WifiRepositoryImpl
import com.android.systemui.testKosmos
import com.android.systemui.user.data.repository.FakeUserRepository
import com.android.systemui.util.concurrency.FakeExecutor
import com.android.systemui.util.mockito.any
@@ -55,6 +57,8 @@ import org.mockito.MockitoAnnotations
@SmallTest
@RunWith(AndroidJUnit4::class)
class WifiRepositorySwitcherTest : SysuiTestCase() {
    private val kosmos = testKosmos()

    private lateinit var underTest: WifiRepositorySwitcher
    private lateinit var realImpl: WifiRepositoryImpl
    private lateinit var demoImpl: DemoWifiRepository
@@ -71,6 +75,7 @@ class WifiRepositorySwitcherTest : SysuiTestCase() {

    private val mainExecutor = FakeExecutor(FakeSystemClock())
    private val userRepository = FakeUserRepository()
    private val connectivityRepository = kosmos.connectivityRepository

    private val testDispatcher = UnconfinedTestDispatcher()
    private val testScope = TestScope(testDispatcher)
@@ -89,6 +94,7 @@ class WifiRepositorySwitcherTest : SysuiTestCase() {
            WifiRepositoryImpl(
                mContext,
                userRepository,
                connectivityRepository,
                testScope.backgroundScope,
                mainExecutor,
                testDispatcher,
+113 −0
Original line number Diff line number Diff line
@@ -39,7 +39,12 @@ import com.android.systemui.log.LogBuffer
import com.android.systemui.log.table.logcatTableLogBuffer
import com.android.systemui.statusbar.connectivity.WifiPickerTrackerFactory
import com.android.systemui.statusbar.pipeline.shared.data.model.DataActivityModel
import com.android.systemui.statusbar.pipeline.shared.data.repository.connectivityRepository
import com.android.systemui.statusbar.pipeline.shared.data.repository.fake
import com.android.systemui.statusbar.pipeline.shared.ui.model.WifiToggleState
import com.android.systemui.statusbar.pipeline.wifi.data.repository.prod.WifiRepositoryImpl.Companion.WIFI_NETWORK_DEFAULT
import com.android.systemui.statusbar.pipeline.wifi.data.repository.prod.WifiRepositoryImpl.Companion.WIFI_TOGGLE_OPTIMISTIC_PAUSE_TIMEOUT_MS
import com.android.systemui.statusbar.pipeline.wifi.data.repository.prod.WifiRepositoryImpl.Companion.WIFI_TOGGLE_OPTIMISTIC_SCANNING_TIMEOUT_MS
import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel
import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiScanEntry
import com.android.systemui.testKosmos
@@ -73,6 +78,7 @@ import org.mockito.kotlin.whenever
class WifiRepositoryImplTest : SysuiTestCase() {
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val userRepository = kosmos.fakeUserRepository
    private val connectivityRepository = kosmos.connectivityRepository

    // Using lazy means that the class will only be constructed once it's fetched. Because the
    // repository internally sets some values on construction, we need to set up some test
@@ -82,6 +88,7 @@ class WifiRepositoryImplTest : SysuiTestCase() {
        WifiRepositoryImpl(
            mContext,
            userRepository,
            connectivityRepository,
            testScope.backgroundScope,
            executor,
            dispatcher,
@@ -1293,6 +1300,112 @@ class WifiRepositoryImplTest : SysuiTestCase() {
            verify(wifiPickerTracker, never()).onStop()
        }

    @Test
    fun wifiToggleState_initialValueIsNormal() =
        testScope.runTest {
            assertThat(underTest.wifiToggleState.value).isEqualTo(WifiToggleState.Normal)
        }

    @Test
    fun pauseWifi_callsManager() =
        testScope.runTest {
            underTest.pauseWifi()

            verify(wifiManager).startRestrictingAutoJoinToSubscriptionId(any())
        }

    @Test
    fun pauseWifi_afterScanning_resetsStateToNormalOnSuccess() =
        testScope.runTest {
            val toggleState by collectLastValue(underTest.wifiToggleState)

            underTest.scanForWifi()
            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)

            underTest.pauseWifi()
            connectivityRepository.fake.setWifiConnected(default = false, validated = false)

            assertThat(toggleState).isEqualTo(WifiToggleState.Normal)
        }

    @Test
    fun pauseWifi_afterScanning_resetsStateToNormalOnTimeout() =
        testScope.runTest {
            val toggleState by collectLastValue(underTest.wifiToggleState)

            underTest.scanForWifi()
            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)

            underTest.pauseWifi()
            testScope.testScheduler.advanceTimeBy(WIFI_TOGGLE_OPTIMISTIC_PAUSE_TIMEOUT_MS)

            assertThat(toggleState).isEqualTo(WifiToggleState.Normal)
        }

    @Test
    fun scanForWifi_setsStateToScanningAndCallsManager() =
        testScope.runTest {
            val toggleState by collectLastValue(underTest.wifiToggleState)

            underTest.scanForWifi()

            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)
            verify(wifiManager).stopRestrictingAutoJoinToSubscriptionId()
            verify(wifiManager).startScan()
        }

    @Test
    fun scanForWifi_doesNotResetToNormalOnCarrierMerged() =
        testScope.runTest {
            val toggleState by collectLastValue(underTest.wifiToggleState)

            underTest.scanForWifi()
            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)

            connectivityRepository.fake.setCarrierMergedConnected()

            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)
        }

    @Test
    fun scanForWifi_resetsStateToNormalOnSuccess() =
        testScope.runTest {
            val toggleState by collectLastValue(underTest.wifiToggleState)

            underTest.scanForWifi()
            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)

            connectivityRepository.fake.setWifiConnected()

            assertThat(toggleState).isEqualTo(WifiToggleState.Normal)
        }

    @Test
    fun scanForWifi_resetsStateToNormalOnTimeout() =
        testScope.runTest {
            val toggleState by collectLastValue(underTest.wifiToggleState)

            underTest.scanForWifi()
            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)

            testScope.testScheduler.advanceTimeBy(WIFI_TOGGLE_OPTIMISTIC_SCANNING_TIMEOUT_MS)

            assertThat(toggleState).isEqualTo(WifiToggleState.Normal)
        }

    @Test
    fun enableWifi_enablesWifiAndScans() =
        testScope.runTest {
            val toggleState by collectLastValue(underTest.wifiToggleState)

            underTest.enableWifi()

            verify(wifiManager).setWifiEnabled(true)
            verify(wifiManager).stopRestrictingAutoJoinToSubscriptionId()
            verify(wifiManager).startScan()
            assertThat(toggleState).isEqualTo(WifiToggleState.Scanning)
        }

    private fun getCallback(): WifiPickerTracker.WifiPickerTrackerCallback {
        return callbackCaptor.firstValue
    }
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.pipeline.shared.ui.model

/** Represents the user's latest toggle action for Wi-Fi, for optimistic UI updates. */
enum class WifiToggleState {
    /** The default state, no toggle action is in progress. */
    Normal,

    /** The user has just requested to pause Wi-Fi */
    Pausing,

    /** The user has just requested to scan for Wi-Fi */
    Scanning,
}
+20 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.statusbar.pipeline.wifi.data.repository

import com.android.systemui.statusbar.pipeline.shared.data.model.DataActivityModel
import com.android.systemui.statusbar.pipeline.shared.ui.model.WifiToggleState
import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel
import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiScanEntry
import kotlinx.coroutines.flow.StateFlow
@@ -50,6 +51,25 @@ interface WifiRepository {
     */
    val wifiScanResults: StateFlow<List<WifiScanEntry>>

    /** Current optimistic state of the toggle. */
    val wifiToggleState: StateFlow<WifiToggleState>

    /** Enables Wi-Fi. See [WifiManager.setWifiEnabled] */
    fun enableWifi()

    /**
     * Pauses Wi-Fi by immediately disconnecting and restricting autojoin to all networks except for
     * carrier Wi-Fi networks of the current subscription ID. See
     * [WifiManager.startRestrictingAutoJoinToSubscriptionId]
     */
    fun pauseWifi()

    /**
     * Unpauses Wi-fi and immediately initiates a scan for available Wi-Fi. See
     * [WifiManager.stopRestrictingAutoJoinToSubscriptionId] See [WifiManager.startScan]
     */
    fun scanForWifi()

    /**
     * Returns true if the device is currently connected to a wifi network with a valid SSID and
     * false otherwise.
Loading