• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.demo
18 
19 import com.android.systemui.dagger.qualifiers.Application
20 import com.android.systemui.statusbar.pipeline.shared.data.model.DataActivityModel
21 import com.android.systemui.statusbar.pipeline.shared.data.model.toWifiDataActivityModel
22 import com.android.systemui.statusbar.pipeline.wifi.data.repository.WifiRepository
23 import com.android.systemui.statusbar.pipeline.wifi.data.repository.demo.model.FakeWifiEventModel
24 import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel
25 import javax.inject.Inject
26 import kotlinx.coroutines.CoroutineScope
27 import kotlinx.coroutines.Job
28 import kotlinx.coroutines.flow.MutableStateFlow
29 import kotlinx.coroutines.flow.StateFlow
30 import kotlinx.coroutines.flow.filterNotNull
31 import kotlinx.coroutines.launch
32 
33 /** Demo-able wifi repository to support SystemUI demo mode commands. */
34 class DemoWifiRepository
35 @Inject
36 constructor(
37     private val dataSource: DemoModeWifiDataSource,
38     @Application private val scope: CoroutineScope,
39 ) : WifiRepository {
40     private var demoCommandJob: Job? = null
41 
42     private val _isWifiEnabled = MutableStateFlow(false)
43     override val isWifiEnabled: StateFlow<Boolean> = _isWifiEnabled
44 
45     private val _isWifiDefault = MutableStateFlow(false)
46     override val isWifiDefault: StateFlow<Boolean> = _isWifiDefault
47 
48     private val _wifiNetwork = MutableStateFlow<WifiNetworkModel>(WifiNetworkModel.Inactive)
49     override val wifiNetwork: StateFlow<WifiNetworkModel> = _wifiNetwork
50 
51     private val _wifiActivity =
52         MutableStateFlow(DataActivityModel(hasActivityIn = false, hasActivityOut = false))
53     override val wifiActivity: StateFlow<DataActivityModel> = _wifiActivity
54 
55     fun startProcessingCommands() {
56         demoCommandJob =
57             scope.launch {
58                 dataSource.wifiEvents.filterNotNull().collect { event -> processEvent(event) }
59             }
60     }
61 
62     fun stopProcessingCommands() {
63         demoCommandJob?.cancel()
64     }
65 
66     private fun processEvent(event: FakeWifiEventModel) =
67         when (event) {
68             is FakeWifiEventModel.Wifi -> processEnabledWifiState(event)
69             is FakeWifiEventModel.CarrierMerged -> processCarrierMergedWifiState(event)
70             is FakeWifiEventModel.WifiDisabled -> processDisabledWifiState()
71         }
72 
73     private fun processDisabledWifiState() {
74         _isWifiEnabled.value = false
75         _isWifiDefault.value = false
76         _wifiActivity.value = DataActivityModel(hasActivityIn = false, hasActivityOut = false)
77         _wifiNetwork.value = WifiNetworkModel.Inactive
78     }
79 
80     private fun processEnabledWifiState(event: FakeWifiEventModel.Wifi) {
81         _isWifiEnabled.value = true
82         _isWifiDefault.value = true
83         _wifiActivity.value = event.activity.toWifiDataActivityModel()
84         _wifiNetwork.value = event.toWifiNetworkModel()
85     }
86 
87     private fun processCarrierMergedWifiState(event: FakeWifiEventModel.CarrierMerged) {
88         _isWifiEnabled.value = true
89         _isWifiDefault.value = true
90         _wifiActivity.value = event.activity.toWifiDataActivityModel()
91         _wifiNetwork.value = event.toCarrierMergedModel()
92     }
93 
94     private fun FakeWifiEventModel.Wifi.toWifiNetworkModel(): WifiNetworkModel =
95         WifiNetworkModel.Active(
96             networkId = DEMO_NET_ID,
97             isValidated = validated ?: true,
98             level = level ?: 0,
99             ssid = ssid,
100 
101             // These fields below aren't supported in demo mode, since they aren't needed to satisfy
102             // the interface.
103             isPasspointAccessPoint = false,
104             isOnlineSignUpForPasspointAccessPoint = false,
105             passpointProviderFriendlyName = null,
106         )
107 
108     private fun FakeWifiEventModel.CarrierMerged.toCarrierMergedModel(): WifiNetworkModel =
109         WifiNetworkModel.CarrierMerged(
110             networkId = DEMO_NET_ID,
111             subscriptionId = subscriptionId,
112             level = level,
113             numberOfLevels = numberOfLevels,
114         )
115 
116     companion object {
117         private const val DEMO_NET_ID = 1234
118     }
119 }
120