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.domain.interactor 18 19 import com.android.systemui.dagger.SysUISingleton 20 import com.android.systemui.dagger.qualifiers.Application 21 import com.android.systemui.statusbar.pipeline.shared.data.model.ConnectivitySlot 22 import com.android.systemui.statusbar.pipeline.shared.data.model.DataActivityModel 23 import com.android.systemui.statusbar.pipeline.shared.data.repository.ConnectivityRepository 24 import com.android.systemui.statusbar.pipeline.wifi.data.repository.WifiRepository 25 import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel 26 import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiScanEntry 27 import javax.inject.Inject 28 import kotlinx.coroutines.CoroutineScope 29 import kotlinx.coroutines.flow.Flow 30 import kotlinx.coroutines.flow.SharingStarted 31 import kotlinx.coroutines.flow.StateFlow 32 import kotlinx.coroutines.flow.combine 33 import kotlinx.coroutines.flow.map 34 import kotlinx.coroutines.flow.stateIn 35 36 /** 37 * The business logic layer for the wifi icon. 38 * 39 * This interactor processes information from our data layer into information that the UI layer can 40 * use. 41 */ 42 interface WifiInteractor { 43 /** 44 * The SSID (service set identifier) of the wifi network. Null if we don't have a network, or 45 * have a network but no valid SSID. 46 */ 47 val ssid: Flow<String?> 48 49 /** Our current enabled status. */ 50 val isEnabled: Flow<Boolean> 51 52 /** Our current default status. */ 53 val isDefault: Flow<Boolean> 54 55 /** Our current wifi network. See [WifiNetworkModel]. */ 56 val wifiNetwork: Flow<WifiNetworkModel> 57 58 /** Our current wifi activity. See [DataActivityModel]. */ 59 val activity: StateFlow<DataActivityModel> 60 61 /** True if we're configured to force-hide the wifi icon and false otherwise. */ 62 val isForceHidden: Flow<Boolean> 63 64 /** True if there are networks available other than the currently-connected one */ 65 val areNetworksAvailable: StateFlow<Boolean> 66 } 67 68 @SysUISingleton 69 class WifiInteractorImpl 70 @Inject 71 constructor( 72 connectivityRepository: ConnectivityRepository, 73 wifiRepository: WifiRepository, 74 @Application scope: CoroutineScope, 75 ) : WifiInteractor { 76 77 override val ssid: Flow<String?> = infonull78 wifiRepository.wifiNetwork.map { info -> 79 when (info) { 80 is WifiNetworkModel.Unavailable -> null 81 is WifiNetworkModel.Invalid -> null 82 is WifiNetworkModel.Inactive -> null 83 is WifiNetworkModel.CarrierMerged -> null 84 is WifiNetworkModel.Active -> 85 when { 86 info.hasValidSsid() -> info.ssid 87 else -> null 88 } 89 } 90 } 91 92 override val isEnabled: Flow<Boolean> = wifiRepository.isWifiEnabled 93 94 override val isDefault: Flow<Boolean> = wifiRepository.isWifiDefault 95 96 override val wifiNetwork: Flow<WifiNetworkModel> = wifiRepository.wifiNetwork 97 98 override val activity: StateFlow<DataActivityModel> = wifiRepository.wifiActivity 99 100 override val isForceHidden: Flow<Boolean> = <lambda>null101 connectivityRepository.forceHiddenSlots.map { it.contains(ConnectivitySlot.WIFI) } 102 103 override val areNetworksAvailable: StateFlow<Boolean> = 104 combine( 105 wifiNetwork, 106 wifiRepository.wifiScanResults, currentNetworknull107 ) { currentNetwork, scanResults -> 108 // We consider networks to be available if the scan results list contains networks 109 // other than the one that is currently connected 110 if (scanResults.isEmpty()) { 111 false 112 } else if (currentNetwork !is WifiNetworkModel.Active) { 113 true 114 } else { 115 anyNonMatchingNetworkExists(currentNetwork, scanResults) 116 } 117 } 118 .stateIn(scope, SharingStarted.WhileSubscribed(), false) 119 anyNonMatchingNetworkExistsnull120 private fun anyNonMatchingNetworkExists( 121 currentNetwork: WifiNetworkModel.Active, 122 availableNetworks: List<WifiScanEntry> 123 ): Boolean = availableNetworks.firstOrNull { it.ssid != currentNetwork.ssid } != null 124 } 125