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 
23 import androidx.annotation.DrawableRes;
24 
25 import com.android.car.settings.R;
26 import com.android.car.settings.wifi.WifiUtil;
27 
28 /**
29  * Helper methods for Wifi-related quick controls.
30  */
31 public class WifiQCUtils {
WifiQCUtils()32     private WifiQCUtils() {
33     }
34 
35     /**
36      * Returns the subtitle string based on the current wifi state.
37      */
getSubtitle(Context context, WifiManager wifiManager)38     public static String getSubtitle(Context context, WifiManager wifiManager) {
39         int wifiState = wifiManager.getWifiState();
40         int stringId = WifiUtil.getStateDesc(wifiState);
41         if (stringId != 0) {
42             return context.getString(stringId);
43         }
44         if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
45             String wifiName = wifiManager.getConnectionInfo().getSSID();
46             if (wifiName.equals(WifiManager.UNKNOWN_SSID)) {
47                 return context.getString(R.string.wifi_disconnected);
48             }
49             return WifiInfo.sanitizeSsid(wifiName);
50         }
51         return context.getString(R.string.wifi_disabled);
52     }
53 
54     /**
55      * Returns the icon resource for the current wifi state.
56      */
57     @DrawableRes
getIcon(WifiManager wifiManager)58     public static int getIcon(WifiManager wifiManager) {
59         if (!wifiManager.isWifiEnabled()) {
60             return R.drawable.ic_qc_wifi_disabled;
61         }
62         WifiInfo wifiInfo = wifiManager.getConnectionInfo();
63         if (wifiInfo.getNetworkId() == -1) {
64             return R.drawable.ic_qc_wifi_disconnected;
65         }
66         int rssi = wifiInfo.getRssi();
67         if (rssi == WifiInfo.INVALID_RSSI) {
68             return R.drawable.ic_qc_wifi_disconnected;
69         }
70         int level = wifiManager.calculateSignalLevel(rssi);
71         switch (level) {
72             case 0:
73                 return R.drawable.ic_qc_wifi_level_0;
74             case 1:
75                 return R.drawable.ic_qc_wifi_level_1;
76             case 2:
77                 return R.drawable.ic_qc_wifi_level_2;
78             case 3:
79                 return R.drawable.ic_qc_wifi_level_3;
80             case 4:
81             default:
82                 return R.drawable.ic_qc_wifi_level_4;
83         }
84     }
85 }
86