1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.statusbar.pipeline.wifi.data.repository
18 
19 import com.android.systemui.CoreStartable
20 import com.android.systemui.statusbar.pipeline.shared.data.model.DataActivityModel
21 import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel
22 import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiScanEntry
23 import kotlinx.coroutines.flow.StateFlow
24 
25 /** Provides data related to the wifi state. */
26 interface WifiRepository {
27     /** Observable for the current wifi enabled status. */
28     val isWifiEnabled: StateFlow<Boolean>
29 
30     /** Observable for the current wifi default status. */
31     val isWifiDefault: StateFlow<Boolean>
32 
33     /** Observable for the current primary wifi network. */
34     val wifiNetwork: StateFlow<WifiNetworkModel>
35 
36     /**
37      * Observable for secondary wifi networks (if any). Should specifically exclude the primary
38      * network emitted by [wifiNetwork].
39      *
40      * This isn't used by phones/tablets, which only display the primary network, but may be used by
41      * other variants like Car.
42      */
43     val secondaryNetworks: StateFlow<List<WifiNetworkModel>>
44 
45     /** Observable for the current wifi network activity. */
46     val wifiActivity: StateFlow<DataActivityModel>
47 
48     /**
49      * The list of known wifi networks, per [WifiManager.scanResults]. This list is passively
50      * updated and does not trigger a scan.
51      */
52     val wifiScanResults: StateFlow<List<WifiScanEntry>>
53 
54     /**
55      * Returns true if the device is currently connected to a wifi network with a valid SSID and
56      * false otherwise.
57      */
58     fun isWifiConnectedWithValidSsid(): Boolean {
59         val currentNetwork = wifiNetwork.value
60         return currentNetwork is WifiNetworkModel.Active && currentNetwork.hasValidSsid()
61     }
62 
63     companion object {
64         /** Column name to use for [isWifiEnabled] for table logging. */
65         const val COL_NAME_IS_ENABLED = "isEnabled"
66         /** Column name to use for [isWifiDefault] for table logging. */
67         const val COL_NAME_IS_DEFAULT = "isDefault"
68 
69         const val CARRIER_MERGED_INVALID_SUB_ID_REASON =
70             "Wifi network was carrier merged but had invalid sub ID"
71     }
72 }
73 
74 /**
75  * A no-op interface used for Dagger bindings.
76  *
77  * [WifiRepositorySwitcher] needs to inject the "real" wifi repository, which could either be the
78  * full [WifiRepositoryImpl] or just [DisabledWifiRepository]. Having this interface lets us bind
79  * [RealWifiRepository], and then [WifiRepositorySwitcher] will automatically get the correct real
80  * repository.
81  */
82 interface RealWifiRepository : WifiRepository
83 
84 /** Used only by Dagger to bind [WifiRepositoryImpl]. */
85 interface WifiRepositoryDagger : RealWifiRepository, CoreStartable
86 /** Used only by Dagger to bind [WifiRepositoryViaTrackerLib]. */
87 interface WifiRepositoryViaTrackerLibDagger : RealWifiRepository
88