1 /* 2 * Copyright (C) 2021 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.car.settings.qc; 18 19 import android.content.Context; 20 import android.net.wifi.WifiInfo; 21 import android.net.wifi.WifiManager; 22 import android.text.TextUtils; 23 24 import androidx.annotation.DrawableRes; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.wifi.WifiUtil; 28 29 /** 30 * Helper methods for Wifi-related quick controls. 31 */ 32 public class WifiQCUtils { WifiQCUtils()33 private WifiQCUtils() { 34 } 35 36 /** 37 * Returns the subtitle string based on the current wifi state. 38 */ getSubtitle(Context context, WifiManager wifiManager)39 public static String getSubtitle(Context context, WifiManager wifiManager) { 40 int wifiState = wifiManager.getWifiState(); 41 int stringId = WifiUtil.getStateDesc(wifiState); 42 if (stringId != 0) { 43 return context.getString(stringId); 44 } 45 if (wifiState == WifiManager.WIFI_STATE_ENABLED) { 46 String wifiName; 47 WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 48 if (wifiInfo.isPasspointAp() || wifiInfo.isOsuAp()) { 49 wifiName = wifiInfo.getPasspointProviderFriendlyName(); 50 if (TextUtils.isEmpty(wifiName)) { 51 wifiName = wifiInfo.getSSID(); 52 } 53 } else { 54 wifiName = wifiInfo.getSSID(); 55 } 56 57 if (wifiName == null || wifiName.equals(WifiManager.UNKNOWN_SSID)) { 58 return context.getString(R.string.wifi_disconnected); 59 } 60 return WifiInfo.sanitizeSsid(wifiName); 61 } 62 return context.getString(R.string.wifi_disabled); 63 } 64 65 /** 66 * Returns the icon resource for the current wifi state. 67 */ 68 @DrawableRes getIcon(WifiManager wifiManager)69 public static int getIcon(WifiManager wifiManager) { 70 if (!wifiManager.isWifiEnabled()) { 71 return R.drawable.ic_qc_wifi_disabled; 72 } 73 WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 74 if (wifiInfo.getNetworkId() == -1) { 75 return R.drawable.ic_qc_wifi_disconnected; 76 } 77 int rssi = wifiInfo.getRssi(); 78 if (rssi == WifiInfo.INVALID_RSSI) { 79 return R.drawable.ic_qc_wifi_disconnected; 80 } 81 int level = wifiManager.calculateSignalLevel(rssi); 82 switch (level) { 83 case 0: 84 return R.drawable.ic_qc_wifi_level_0; 85 case 1: 86 return R.drawable.ic_qc_wifi_level_1; 87 case 2: 88 return R.drawable.ic_qc_wifi_level_2; 89 case 3: 90 return R.drawable.ic_qc_wifi_level_3; 91 case 4: 92 default: 93 return R.drawable.ic_qc_wifi_level_4; 94 } 95 } 96 97 /** 98 * Returns if the wifi is changing state between enabled and disabled. 99 */ isWifiBusy(WifiManager wifiManager)100 public static boolean isWifiBusy(WifiManager wifiManager) { 101 int state = wifiManager.getWifiState(); 102 return state == WifiManager.WIFI_STATE_ENABLING 103 || state == WifiManager.WIFI_STATE_DISABLING; 104 } 105 106 } 107