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