Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.settingslib.wifi; |
| 18 | |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 20 | import android.annotation.MainThread; |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 23 | import android.content.Context; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 24 | import android.content.pm.ApplicationInfo; |
| 25 | import android.content.pm.IPackageManager; |
| 26 | import android.content.pm.PackageManager; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 27 | import android.net.ConnectivityManager; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 28 | import android.net.NetworkCapabilities; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 29 | import android.net.NetworkInfo; |
| 30 | import android.net.NetworkInfo.DetailedState; |
| 31 | import android.net.NetworkInfo.State; |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 32 | import android.net.NetworkKey; |
Stephen Chen | 36dd5cf1 | 2017-03-20 13:27:51 -0700 | [diff] [blame] | 33 | import android.net.NetworkScoreManager; |
| 34 | import android.net.NetworkScorerAppData; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 35 | import android.net.ScoredNetwork; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 36 | import android.net.wifi.IWifiManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 37 | import android.net.wifi.ScanResult; |
| 38 | import android.net.wifi.WifiConfiguration; |
| 39 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 40 | import android.net.wifi.WifiEnterpriseConfig; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 41 | import android.net.wifi.WifiInfo; |
| 42 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 43 | import android.net.wifi.WifiNetworkScoreCache; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 44 | import android.net.wifi.hotspot2.PasspointConfiguration; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 45 | import android.os.Bundle; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 46 | import android.os.Parcelable; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 47 | import android.os.RemoteException; |
| 48 | import android.os.ServiceManager; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 49 | import android.os.SystemClock; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 50 | import android.os.UserHandle; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 51 | import android.text.TextUtils; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 52 | import android.util.ArraySet; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 53 | import android.util.Log; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 54 | |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 55 | import androidx.annotation.NonNull; |
| 56 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 57 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 58 | import com.android.settingslib.R; |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 59 | import com.android.settingslib.utils.ThreadUtils; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 60 | |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 61 | import java.lang.annotation.Retention; |
| 62 | import java.lang.annotation.RetentionPolicy; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 63 | import java.util.ArrayList; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 64 | import java.util.Collection; |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 65 | import java.util.HashMap; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 66 | import java.util.Iterator; |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 67 | import java.util.Map; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 68 | import java.util.Set; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 69 | import java.util.concurrent.atomic.AtomicInteger; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 70 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 71 | /** |
| 72 | * Represents a selectable Wifi Network for use in various wifi selection menus backed by |
| 73 | * {@link WifiTracker}. |
| 74 | * |
| 75 | * <p>An AccessPoint, which would be more fittingly named "WifiNetwork", is an aggregation of |
| 76 | * {@link ScanResult ScanResults} along with pertinent metadata (e.g. current connection info, |
| 77 | * network scores) required to successfully render the network to the user. |
| 78 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 79 | public class AccessPoint implements Comparable<AccessPoint> { |
| 80 | static final String TAG = "SettingsLib.AccessPoint"; |
| 81 | |
| 82 | /** |
| 83 | * Lower bound on the 2.4 GHz (802.11b/g/n) WLAN channels |
| 84 | */ |
| 85 | public static final int LOWER_FREQ_24GHZ = 2400; |
| 86 | |
| 87 | /** |
| 88 | * Upper bound on the 2.4 GHz (802.11b/g/n) WLAN channels |
| 89 | */ |
| 90 | public static final int HIGHER_FREQ_24GHZ = 2500; |
| 91 | |
| 92 | /** |
| 93 | * Lower bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels |
| 94 | */ |
| 95 | public static final int LOWER_FREQ_5GHZ = 4900; |
| 96 | |
| 97 | /** |
| 98 | * Upper bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels |
| 99 | */ |
| 100 | public static final int HIGHER_FREQ_5GHZ = 5900; |
| 101 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 102 | /** The key which identifies this AccessPoint grouping. */ |
| 103 | private String mKey; |
| 104 | |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 105 | @IntDef({Speed.NONE, Speed.SLOW, Speed.MODERATE, Speed.FAST, Speed.VERY_FAST}) |
| 106 | @Retention(RetentionPolicy.SOURCE) |
| 107 | public @interface Speed { |
| 108 | /** |
| 109 | * Constant value representing an unlabeled / unscored network. |
| 110 | */ |
| 111 | int NONE = 0; |
| 112 | /** |
| 113 | * Constant value representing a slow speed network connection. |
| 114 | */ |
| 115 | int SLOW = 5; |
| 116 | /** |
| 117 | * Constant value representing a medium speed network connection. |
| 118 | */ |
| 119 | int MODERATE = 10; |
| 120 | /** |
| 121 | * Constant value representing a fast speed network connection. |
| 122 | */ |
| 123 | int FAST = 20; |
| 124 | /** |
| 125 | * Constant value representing a very fast speed network connection. |
| 126 | */ |
| 127 | int VERY_FAST = 30; |
| 128 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 129 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 130 | /** The underlying set of scan results comprising this AccessPoint. */ |
| 131 | private final ArraySet<ScanResult> mScanResults = new ArraySet<>(); |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 132 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Map of BSSIDs to scored networks for individual bssids. |
| 135 | * |
| 136 | * <p>This cache should not be evicted with scan results, as the values here are used to |
| 137 | * generate a fallback in the absence of scores for the visible APs. |
| 138 | */ |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 139 | private final Map<String, TimestampedScoredNetwork> mScoredNetworkCache = new HashMap<>(); |
| 140 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 141 | static final String KEY_NETWORKINFO = "key_networkinfo"; |
| 142 | static final String KEY_WIFIINFO = "key_wifiinfo"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 143 | static final String KEY_SSID = "key_ssid"; |
| 144 | static final String KEY_SECURITY = "key_security"; |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 145 | static final String KEY_SPEED = "key_speed"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 146 | static final String KEY_PSKTYPE = "key_psktype"; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 147 | static final String KEY_SCANRESULTS = "key_scanresults"; |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 148 | static final String KEY_SCOREDNETWORKCACHE = "key_scorednetworkcache"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 149 | static final String KEY_CONFIG = "key_config"; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 150 | static final String KEY_FQDN = "key_fqdn"; |
| 151 | static final String KEY_PROVIDER_FRIENDLY_NAME = "key_provider_friendly_name"; |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 152 | static final String KEY_IS_CARRIER_AP = "key_is_carrier_ap"; |
| 153 | static final String KEY_CARRIER_AP_EAP_TYPE = "key_carrier_ap_eap_type"; |
| 154 | static final String KEY_CARRIER_NAME = "key_carrier_name"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 155 | static final AtomicInteger sLastId = new AtomicInteger(0); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 156 | |
Maurice Lam | 7cdbe19 | 2018-11-13 13:52:50 -0800 | [diff] [blame] | 157 | /* |
| 158 | * NOTE: These constants for security and PSK types are saved to the bundle in saveWifiState, |
| 159 | * and sent across IPC. The numeric values should remain stable, otherwise the changes will need |
| 160 | * to be synced with other unbundled users of this library. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 161 | */ |
| 162 | public static final int SECURITY_NONE = 0; |
Hai Shalom | 9effa64 | 2018-11-12 11:43:52 -0800 | [diff] [blame] | 163 | public static final int SECURITY_WEP = 1; |
| 164 | public static final int SECURITY_PSK = 2; |
| 165 | public static final int SECURITY_EAP = 3; |
| 166 | public static final int SECURITY_OWE = 4; |
| 167 | public static final int SECURITY_SAE = 5; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 168 | public static final int SECURITY_EAP_SUITE_B = 6; |
Hai Shalom | 828b13c | 2018-11-13 16:43:14 -0800 | [diff] [blame] | 169 | public static final int SECURITY_MAX_VAL = 7; // Has to be the last |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 170 | |
| 171 | private static final int PSK_UNKNOWN = 0; |
| 172 | private static final int PSK_WPA = 1; |
| 173 | private static final int PSK_WPA2 = 2; |
| 174 | private static final int PSK_WPA_WPA2 = 3; |
| 175 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 176 | /** |
| 177 | * The number of distinct wifi levels. |
| 178 | * |
| 179 | * <p>Must keep in sync with {@link R.array.wifi_signal} and {@link WifiManager#RSSI_LEVELS}. |
| 180 | */ |
| 181 | public static final int SIGNAL_LEVELS = 5; |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 182 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 183 | public static final int UNREACHABLE_RSSI = Integer.MIN_VALUE; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 184 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 185 | private final Context mContext; |
| 186 | |
| 187 | private String ssid; |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 188 | private String bssid; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 189 | private int security; |
| 190 | private int networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 191 | |
| 192 | private int pskType = PSK_UNKNOWN; |
| 193 | |
| 194 | private WifiConfiguration mConfig; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 195 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 196 | private int mRssi = UNREACHABLE_RSSI; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 197 | |
| 198 | private WifiInfo mInfo; |
| 199 | private NetworkInfo mNetworkInfo; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 200 | AccessPointListener mAccessPointListener; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 201 | |
| 202 | private Object mTag; |
| 203 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 204 | @Speed private int mSpeed = Speed.NONE; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 205 | private boolean mIsScoredNetworkMetered = false; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 206 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 207 | // used to co-relate internal vs returned accesspoint. |
| 208 | int mId; |
| 209 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Information associated with the {@link PasspointConfiguration}. Only maintaining |
| 212 | * the relevant info to preserve spaces. |
| 213 | */ |
| 214 | private String mFqdn; |
| 215 | private String mProviderFriendlyName; |
| 216 | |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 217 | private boolean mIsCarrierAp = false; |
| 218 | /** |
| 219 | * The EAP type {@link WifiEnterpriseConfig.Eap} associated with this AP if it is a carrier AP. |
| 220 | */ |
| 221 | private int mCarrierApEapType = WifiEnterpriseConfig.Eap.NONE; |
| 222 | private String mCarrierName = null; |
| 223 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 224 | public AccessPoint(Context context, Bundle savedState) { |
| 225 | mContext = context; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 226 | |
| 227 | if (savedState.containsKey(KEY_CONFIG)) { |
| 228 | mConfig = savedState.getParcelable(KEY_CONFIG); |
| 229 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 230 | if (mConfig != null) { |
| 231 | loadConfig(mConfig); |
| 232 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 233 | if (savedState.containsKey(KEY_SSID)) { |
| 234 | ssid = savedState.getString(KEY_SSID); |
| 235 | } |
| 236 | if (savedState.containsKey(KEY_SECURITY)) { |
| 237 | security = savedState.getInt(KEY_SECURITY); |
| 238 | } |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 239 | if (savedState.containsKey(KEY_SPEED)) { |
| 240 | mSpeed = savedState.getInt(KEY_SPEED); |
| 241 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 242 | if (savedState.containsKey(KEY_PSKTYPE)) { |
| 243 | pskType = savedState.getInt(KEY_PSKTYPE); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 244 | } |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 245 | mInfo = savedState.getParcelable(KEY_WIFIINFO); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 246 | if (savedState.containsKey(KEY_NETWORKINFO)) { |
| 247 | mNetworkInfo = savedState.getParcelable(KEY_NETWORKINFO); |
| 248 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 249 | if (savedState.containsKey(KEY_SCANRESULTS)) { |
| 250 | Parcelable[] scanResults = savedState.getParcelableArray(KEY_SCANRESULTS); |
| 251 | mScanResults.clear(); |
| 252 | for (Parcelable result : scanResults) { |
| 253 | mScanResults.add((ScanResult) result); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 256 | if (savedState.containsKey(KEY_SCOREDNETWORKCACHE)) { |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 257 | ArrayList<TimestampedScoredNetwork> scoredNetworkArrayList = |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 258 | savedState.getParcelableArrayList(KEY_SCOREDNETWORKCACHE); |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 259 | for (TimestampedScoredNetwork timedScore : scoredNetworkArrayList) { |
| 260 | mScoredNetworkCache.put(timedScore.getScore().networkKey.wifiKey.bssid, timedScore); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 263 | if (savedState.containsKey(KEY_FQDN)) { |
| 264 | mFqdn = savedState.getString(KEY_FQDN); |
| 265 | } |
| 266 | if (savedState.containsKey(KEY_PROVIDER_FRIENDLY_NAME)) { |
| 267 | mProviderFriendlyName = savedState.getString(KEY_PROVIDER_FRIENDLY_NAME); |
| 268 | } |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 269 | if (savedState.containsKey(KEY_IS_CARRIER_AP)) { |
| 270 | mIsCarrierAp = savedState.getBoolean(KEY_IS_CARRIER_AP); |
| 271 | } |
| 272 | if (savedState.containsKey(KEY_CARRIER_AP_EAP_TYPE)) { |
| 273 | mCarrierApEapType = savedState.getInt(KEY_CARRIER_AP_EAP_TYPE); |
| 274 | } |
| 275 | if (savedState.containsKey(KEY_CARRIER_NAME)) { |
| 276 | mCarrierName = savedState.getString(KEY_CARRIER_NAME); |
| 277 | } |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 278 | update(mConfig, mInfo, mNetworkInfo); |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 279 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 280 | // Calculate required fields |
| 281 | updateKey(); |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 282 | updateRssi(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 283 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 284 | mId = sLastId.incrementAndGet(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 287 | public AccessPoint(Context context, WifiConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 288 | mContext = context; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 289 | loadConfig(config); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 290 | mId = sLastId.incrementAndGet(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 293 | /** |
| 294 | * Initialize an AccessPoint object for a {@link PasspointConfiguration}. This is mainly |
| 295 | * used by "Saved Networks" page for managing the saved {@link PasspointConfiguration}. |
| 296 | */ |
| 297 | public AccessPoint(Context context, PasspointConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 298 | mContext = context; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 299 | mFqdn = config.getHomeSp().getFqdn(); |
| 300 | mProviderFriendlyName = config.getHomeSp().getFriendlyName(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 301 | mId = sLastId.incrementAndGet(); |
| 302 | } |
| 303 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 304 | AccessPoint(Context context, Collection<ScanResult> results) { |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 305 | mContext = context; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 306 | |
| 307 | if (results.isEmpty()) { |
| 308 | throw new IllegalArgumentException("Cannot construct with an empty ScanResult list"); |
| 309 | } |
| 310 | mScanResults.addAll(results); |
| 311 | |
| 312 | // Information derived from scan results |
| 313 | ScanResult firstResult = results.iterator().next(); |
| 314 | ssid = firstResult.SSID; |
| 315 | bssid = firstResult.BSSID; |
| 316 | security = getSecurity(firstResult); |
| 317 | if (security == SECURITY_PSK) { |
| 318 | pskType = getPskType(firstResult); |
| 319 | } |
| 320 | updateKey(); |
| 321 | updateRssi(); |
| 322 | |
| 323 | // Passpoint Info |
| 324 | mIsCarrierAp = firstResult.isCarrierAp; |
| 325 | mCarrierApEapType = firstResult.carrierApEapType; |
| 326 | mCarrierName = firstResult.carrierName; |
| 327 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 328 | mId = sLastId.incrementAndGet(); |
| 329 | } |
| 330 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 331 | @VisibleForTesting void loadConfig(WifiConfiguration config) { |
| 332 | ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); |
| 333 | bssid = config.BSSID; |
| 334 | security = getSecurity(config); |
| 335 | updateKey(); |
| 336 | networkId = config.networkId; |
| 337 | mConfig = config; |
| 338 | } |
| 339 | |
| 340 | /** Updates {@link #mKey} and should only called upon object creation/initialization. */ |
| 341 | private void updateKey() { |
| 342 | // TODO(sghuman): Consolidate Key logic on ScanResultMatchInfo |
| 343 | |
| 344 | StringBuilder builder = new StringBuilder(); |
| 345 | |
| 346 | if (TextUtils.isEmpty(getSsidStr())) { |
| 347 | builder.append(getBssid()); |
| 348 | } else { |
| 349 | builder.append(getSsidStr()); |
| 350 | } |
| 351 | |
| 352 | builder.append(',').append(getSecurity()); |
| 353 | mKey = builder.toString(); |
| 354 | } |
| 355 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 356 | /** |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 357 | * Returns a negative integer, zero, or a positive integer if this AccessPoint is less than, |
| 358 | * equal to, or greater than the other AccessPoint. |
| 359 | * |
| 360 | * Sort order rules for AccessPoints: |
| 361 | * 1. Active before inactive |
| 362 | * 2. Reachable before unreachable |
| 363 | * 3. Saved before unsaved |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 364 | * 4. Network speed value |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 365 | * 5. Stronger signal before weaker signal |
| 366 | * 6. SSID alphabetically |
| 367 | * |
| 368 | * Note that AccessPoints with a signal are usually also Reachable, |
| 369 | * and will thus appear before unreachable saved AccessPoints. |
| 370 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 371 | @Override |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 372 | public int compareTo(@NonNull AccessPoint other) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 373 | // Active one goes first. |
| 374 | if (isActive() && !other.isActive()) return -1; |
| 375 | if (!isActive() && other.isActive()) return 1; |
| 376 | |
| 377 | // Reachable one goes before unreachable one. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 378 | if (isReachable() && !other.isReachable()) return -1; |
| 379 | if (!isReachable() && other.isReachable()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 380 | |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 381 | // Configured (saved) one goes before unconfigured one. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 382 | if (isSaved() && !other.isSaved()) return -1; |
| 383 | if (!isSaved() && other.isSaved()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 384 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 385 | // Faster speeds go before slower speeds - but only if visible change in speed label |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 386 | if (getSpeed() != other.getSpeed()) { |
| 387 | return other.getSpeed() - getSpeed(); |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 390 | // Sort by signal strength, bucketed by level |
| 391 | int difference = WifiManager.calculateSignalLevel(other.mRssi, SIGNAL_LEVELS) |
| 392 | - WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 393 | if (difference != 0) { |
| 394 | return difference; |
| 395 | } |
Sundeep Ghuman | d44b8e4 | 2017-06-21 21:40:06 -0700 | [diff] [blame] | 396 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 397 | // Sort by ssid. |
Sundeep Ghuman | d44b8e4 | 2017-06-21 21:40:06 -0700 | [diff] [blame] | 398 | difference = getSsidStr().compareToIgnoreCase(other.getSsidStr()); |
| 399 | if (difference != 0) { |
| 400 | return difference; |
| 401 | } |
| 402 | |
| 403 | // Do a case sensitive comparison to distinguish SSIDs that differ in case only |
| 404 | return getSsidStr().compareTo(other.getSsidStr()); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public boolean equals(Object other) { |
| 409 | if (!(other instanceof AccessPoint)) return false; |
| 410 | return (this.compareTo((AccessPoint) other) == 0); |
| 411 | } |
| 412 | |
| 413 | @Override |
| 414 | public int hashCode() { |
| 415 | int result = 0; |
| 416 | if (mInfo != null) result += 13 * mInfo.hashCode(); |
| 417 | result += 19 * mRssi; |
| 418 | result += 23 * networkId; |
| 419 | result += 29 * ssid.hashCode(); |
| 420 | return result; |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public String toString() { |
| 425 | StringBuilder builder = new StringBuilder().append("AccessPoint(") |
| 426 | .append(ssid); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 427 | if (bssid != null) { |
| 428 | builder.append(":").append(bssid); |
| 429 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 430 | if (isSaved()) { |
| 431 | builder.append(',').append("saved"); |
| 432 | } |
| 433 | if (isActive()) { |
| 434 | builder.append(',').append("active"); |
| 435 | } |
| 436 | if (isEphemeral()) { |
| 437 | builder.append(',').append("ephemeral"); |
| 438 | } |
| 439 | if (isConnectable()) { |
| 440 | builder.append(',').append("connectable"); |
| 441 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 442 | if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 443 | builder.append(',').append(securityToString(security, pskType)); |
| 444 | } |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 445 | builder.append(",level=").append(getLevel()); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 446 | if (mSpeed != Speed.NONE) { |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 447 | builder.append(",speed=").append(mSpeed); |
Sundeep Ghuman | 8920e9c | 2017-04-27 16:16:41 -0700 | [diff] [blame] | 448 | } |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 449 | builder.append(",metered=").append(isMetered()); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 450 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 451 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 452 | builder.append(",rssi=").append(mRssi); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 453 | builder.append(",scan cache size=").append(mScanResults.size()); |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 456 | return builder.append(')').toString(); |
| 457 | } |
| 458 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 459 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 460 | * Updates the AccessPoint rankingScore, metering, and speed, returning true if the data has |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 461 | * changed. |
| 462 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 463 | * @param scoreCache The score cache to use to retrieve scores |
| 464 | * @param scoringUiEnabled Whether to show scoring and badging UI |
| 465 | * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when |
| 466 | * generating speed labels |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 467 | */ |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 468 | boolean update( |
| 469 | WifiNetworkScoreCache scoreCache, |
| 470 | boolean scoringUiEnabled, |
| 471 | long maxScoreCacheAgeMillis) { |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 472 | boolean scoreChanged = false; |
| 473 | if (scoringUiEnabled) { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 474 | scoreChanged = updateScores(scoreCache, maxScoreCacheAgeMillis); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 475 | } |
| 476 | return updateMetered(scoreCache) || scoreChanged; |
| 477 | } |
| 478 | |
| 479 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 480 | * Updates the AccessPoint rankingScore and speed, returning true if the data has changed. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 481 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 482 | * <p>Any cached {@link TimestampedScoredNetwork} objects older than the given max age in millis |
| 483 | * will be removed when this method is invoked. |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 484 | * |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 485 | * <p>Precondition: {@link #mRssi} is up to date before invoking this method. |
| 486 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 487 | * @param scoreCache The score cache to use to retrieve scores |
| 488 | * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when |
| 489 | * generating speed labels |
| 490 | * |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 491 | * @return true if the set speed has changed |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 492 | */ |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 493 | private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) { |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 494 | long nowMillis = SystemClock.elapsedRealtime(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 495 | for (ScanResult result : mScanResults) { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 496 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 497 | if (score == null) { |
| 498 | continue; |
| 499 | } |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 500 | TimestampedScoredNetwork timedScore = mScoredNetworkCache.get(result.BSSID); |
| 501 | if (timedScore == null) { |
| 502 | mScoredNetworkCache.put( |
| 503 | result.BSSID, new TimestampedScoredNetwork(score, nowMillis)); |
| 504 | } else { |
| 505 | // Update data since the has been seen in the score cache |
| 506 | timedScore.update(score, nowMillis); |
| 507 | } |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 510 | // Remove old cached networks |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 511 | long evictionCutoff = nowMillis - maxScoreCacheAgeMillis; |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 512 | Iterator<TimestampedScoredNetwork> iterator = mScoredNetworkCache.values().iterator(); |
| 513 | iterator.forEachRemaining(timestampedScoredNetwork -> { |
| 514 | if (timestampedScoredNetwork.getUpdatedTimestampMillis() < evictionCutoff) { |
| 515 | iterator.remove(); |
| 516 | } |
| 517 | }); |
| 518 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 519 | return updateSpeed(); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Updates the internal speed, returning true if the update resulted in a speed label change. |
| 524 | */ |
| 525 | private boolean updateSpeed() { |
| 526 | int oldSpeed = mSpeed; |
| 527 | mSpeed = generateAverageSpeedForSsid(); |
| 528 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 529 | boolean changed = oldSpeed != mSpeed; |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 530 | if(isVerboseLoggingEnabled() && changed) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 531 | Log.i(TAG, String.format("%s: Set speed to %d", ssid, mSpeed)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 532 | } |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 533 | return changed; |
| 534 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 535 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 536 | /** Creates a speed value for the current {@link #mRssi} by averaging all non zero badges. */ |
| 537 | @Speed private int generateAverageSpeedForSsid() { |
| 538 | if (mScoredNetworkCache.isEmpty()) { |
| 539 | return Speed.NONE; |
| 540 | } |
| 541 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 542 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 543 | Log.d(TAG, String.format("Generating fallbackspeed for %s using cache: %s", |
| 544 | getSsidStr(), mScoredNetworkCache)); |
| 545 | } |
| 546 | |
Sundeep Ghuman | a782569 | 2017-08-23 15:53:00 -0700 | [diff] [blame] | 547 | // TODO(b/63073866): If flickering issues persist, consider mapping using getLevel rather |
| 548 | // than specific rssi value so score doesn't change without a visible wifi bar change. This |
| 549 | // issue is likely to be more evident for the active AP whose RSSI value is not half-lifed. |
| 550 | |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 551 | int count = 0; |
| 552 | int totalSpeed = 0; |
| 553 | for (TimestampedScoredNetwork timedScore : mScoredNetworkCache.values()) { |
| 554 | int speed = timedScore.getScore().calculateBadge(mRssi); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 555 | if (speed != Speed.NONE) { |
| 556 | count++; |
| 557 | totalSpeed += speed; |
| 558 | } |
| 559 | } |
| 560 | int speed = count == 0 ? Speed.NONE : totalSpeed / count; |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 561 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 562 | Log.i(TAG, String.format("%s generated fallback speed is: %d", getSsidStr(), speed)); |
| 563 | } |
| 564 | return roundToClosestSpeedEnum(speed); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 567 | /** |
| 568 | * Updates the AccessPoint's metering based on {@link ScoredNetwork#meteredHint}, returning |
| 569 | * true if the metering changed. |
| 570 | */ |
| 571 | private boolean updateMetered(WifiNetworkScoreCache scoreCache) { |
| 572 | boolean oldMetering = mIsScoredNetworkMetered; |
| 573 | mIsScoredNetworkMetered = false; |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 574 | |
| 575 | if (isActive() && mInfo != null) { |
Eric Schwarzenbach | 733ea26 | 2017-08-08 10:28:21 -0700 | [diff] [blame] | 576 | NetworkKey key = NetworkKey.createFromWifiInfo(mInfo); |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 577 | ScoredNetwork score = scoreCache.getScoredNetwork(key); |
| 578 | if (score != null) { |
| 579 | mIsScoredNetworkMetered |= score.meteredHint; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 580 | } |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 581 | } else { |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 582 | for (ScanResult result : mScanResults) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 583 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 584 | if (score == null) { |
| 585 | continue; |
| 586 | } |
| 587 | mIsScoredNetworkMetered |= score.meteredHint; |
| 588 | } |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 589 | } |
| 590 | return oldMetering == mIsScoredNetworkMetered; |
| 591 | } |
| 592 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 593 | public static String getKey(ScanResult result) { |
| 594 | StringBuilder builder = new StringBuilder(); |
| 595 | |
| 596 | if (TextUtils.isEmpty(result.SSID)) { |
| 597 | builder.append(result.BSSID); |
| 598 | } else { |
| 599 | builder.append(result.SSID); |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 600 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 601 | |
| 602 | builder.append(',').append(getSecurity(result)); |
| 603 | return builder.toString(); |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 606 | public static String getKey(WifiConfiguration config) { |
| 607 | StringBuilder builder = new StringBuilder(); |
| 608 | |
| 609 | if (TextUtils.isEmpty(config.SSID)) { |
| 610 | builder.append(config.BSSID); |
| 611 | } else { |
| 612 | builder.append(removeDoubleQuotes(config.SSID)); |
| 613 | } |
| 614 | |
| 615 | builder.append(',').append(getSecurity(config)); |
| 616 | return builder.toString(); |
| 617 | } |
| 618 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 619 | public String getKey() { |
| 620 | return mKey; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | public boolean matches(WifiConfiguration config) { |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 624 | if (config.isPasspoint() && mConfig != null && mConfig.isPasspoint()) { |
Shinji Sogo | f29e122 | 2015-12-09 17:21:38 +0900 | [diff] [blame] | 625 | return ssid.equals(removeDoubleQuotes(config.SSID)) && config.FQDN.equals(mConfig.FQDN); |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 626 | } else { |
| 627 | return ssid.equals(removeDoubleQuotes(config.SSID)) |
| 628 | && security == getSecurity(config) |
| 629 | && (mConfig == null || mConfig.shared == config.shared); |
| 630 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | public WifiConfiguration getConfig() { |
| 634 | return mConfig; |
| 635 | } |
| 636 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 637 | public String getPasspointFqdn() { |
| 638 | return mFqdn; |
| 639 | } |
| 640 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 641 | public void clearConfig() { |
| 642 | mConfig = null; |
| 643 | networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 644 | } |
| 645 | |
| 646 | public WifiInfo getInfo() { |
| 647 | return mInfo; |
| 648 | } |
| 649 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 650 | /** |
| 651 | * Returns the number of levels to show for a Wifi icon, from 0 to {@link #SIGNAL_LEVELS}-1. |
| 652 | * |
| 653 | * <p>Use {@#isReachable()} to determine if an AccessPoint is in range, as this method will |
| 654 | * always return at least 0. |
| 655 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 656 | public int getLevel() { |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 657 | return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 658 | } |
| 659 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 660 | public int getRssi() { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 661 | return mRssi; |
| 662 | } |
| 663 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 664 | /** |
| 665 | * Returns the underlying scan result set. |
| 666 | * |
| 667 | * <p>Callers should not modify this set. |
| 668 | */ |
| 669 | public Set<ScanResult> getScanResults() { return mScanResults; } |
jackqdyulei | 9ee9b39 | 2017-12-22 11:31:49 -0800 | [diff] [blame] | 670 | |
| 671 | public Map<String, TimestampedScoredNetwork> getScoredNetworkCache() { |
| 672 | return mScoredNetworkCache; |
| 673 | } |
| 674 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 675 | /** |
| 676 | * Updates {@link #mRssi}. |
| 677 | * |
| 678 | * <p>If the given connection is active, the existing value of {@link #mRssi} will be returned. |
| 679 | * If the given AccessPoint is not active, a value will be calculated from previous scan |
Sundeep Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 680 | * results, returning the best RSSI for all matching AccessPoints averaged with the previous |
| 681 | * value. If the access point is not connected and there are no scan results, the rssi will be |
| 682 | * set to {@link #UNREACHABLE_RSSI}. |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 683 | */ |
| 684 | private void updateRssi() { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 685 | if (this.isActive()) { |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | int rssi = UNREACHABLE_RSSI; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 690 | for (ScanResult result : mScanResults) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 691 | if (result.level > rssi) { |
| 692 | rssi = result.level; |
| 693 | } |
| 694 | } |
| 695 | |
Sundeep Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 696 | if (rssi != UNREACHABLE_RSSI && mRssi != UNREACHABLE_RSSI) { |
| 697 | mRssi = (mRssi + rssi) / 2; // half-life previous value |
| 698 | } else { |
| 699 | mRssi = rssi; |
| 700 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 703 | /** |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 704 | * Returns if the network should be considered metered. |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 705 | */ |
| 706 | public boolean isMetered() { |
| 707 | return mIsScoredNetworkMetered |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 708 | || WifiConfiguration.isMetered(mConfig, mInfo); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 711 | public NetworkInfo getNetworkInfo() { |
| 712 | return mNetworkInfo; |
| 713 | } |
| 714 | |
| 715 | public int getSecurity() { |
| 716 | return security; |
| 717 | } |
| 718 | |
| 719 | public String getSecurityString(boolean concise) { |
| 720 | Context context = mContext; |
Tomoharu Hatano | 2bde6e7 | 2017-05-30 18:38:56 +0900 | [diff] [blame] | 721 | if (isPasspoint() || isPasspointConfig()) { |
Sanket Padawe | 194cce6 | 2015-07-10 10:53:31 -0700 | [diff] [blame] | 722 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 723 | context.getString(R.string.wifi_security_eap); |
Sanket Padawe | d187880 | 2015-05-12 10:27:19 -0700 | [diff] [blame] | 724 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 725 | switch(security) { |
| 726 | case SECURITY_EAP: |
| 727 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 728 | context.getString(R.string.wifi_security_eap); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 729 | case SECURITY_EAP_SUITE_B: |
| 730 | return concise ? context.getString(R.string.wifi_security_short_eap_suiteb) : |
| 731 | context.getString(R.string.wifi_security_eap_suiteb); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 732 | case SECURITY_PSK: |
| 733 | switch (pskType) { |
| 734 | case PSK_WPA: |
| 735 | return concise ? context.getString(R.string.wifi_security_short_wpa) : |
| 736 | context.getString(R.string.wifi_security_wpa); |
| 737 | case PSK_WPA2: |
| 738 | return concise ? context.getString(R.string.wifi_security_short_wpa2) : |
| 739 | context.getString(R.string.wifi_security_wpa2); |
| 740 | case PSK_WPA_WPA2: |
| 741 | return concise ? context.getString(R.string.wifi_security_short_wpa_wpa2) : |
| 742 | context.getString(R.string.wifi_security_wpa_wpa2); |
| 743 | case PSK_UNKNOWN: |
| 744 | default: |
| 745 | return concise ? context.getString(R.string.wifi_security_short_psk_generic) |
| 746 | : context.getString(R.string.wifi_security_psk_generic); |
| 747 | } |
| 748 | case SECURITY_WEP: |
| 749 | return concise ? context.getString(R.string.wifi_security_short_wep) : |
| 750 | context.getString(R.string.wifi_security_wep); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 751 | case SECURITY_SAE: |
| 752 | return concise ? context.getString(R.string.wifi_security_short_sae) : |
| 753 | context.getString(R.string.wifi_security_sae); |
| 754 | case SECURITY_OWE: |
| 755 | return concise ? context.getString(R.string.wifi_security_short_owe) : |
| 756 | context.getString(R.string.wifi_security_owe); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 757 | case SECURITY_NONE: |
| 758 | default: |
| 759 | return concise ? "" : context.getString(R.string.wifi_security_none); |
| 760 | } |
| 761 | } |
| 762 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 763 | public String getSsidStr() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 764 | return ssid; |
| 765 | } |
| 766 | |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 767 | public String getBssid() { |
| 768 | return bssid; |
| 769 | } |
| 770 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 771 | public CharSequence getSsid() { |
pastychang | a2998fc | 2018-06-26 13:37:34 +0800 | [diff] [blame] | 772 | return ssid; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 773 | } |
| 774 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 775 | public String getConfigName() { |
| 776 | if (mConfig != null && mConfig.isPasspoint()) { |
| 777 | return mConfig.providerFriendlyName; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 778 | } else if (mFqdn != null) { |
| 779 | return mProviderFriendlyName; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 780 | } else { |
| 781 | return ssid; |
| 782 | } |
| 783 | } |
| 784 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 785 | public DetailedState getDetailedState() { |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 786 | if (mNetworkInfo != null) { |
| 787 | return mNetworkInfo.getDetailedState(); |
| 788 | } |
| 789 | Log.w(TAG, "NetworkInfo is null, cannot return detailed state"); |
| 790 | return null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 791 | } |
| 792 | |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 793 | public boolean isCarrierAp() { |
| 794 | return mIsCarrierAp; |
| 795 | } |
| 796 | |
| 797 | public int getCarrierApEapType() { |
| 798 | return mCarrierApEapType; |
| 799 | } |
| 800 | |
| 801 | public String getCarrierName() { |
| 802 | return mCarrierName; |
| 803 | } |
| 804 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 805 | public String getSavedNetworkSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 806 | WifiConfiguration config = mConfig; |
| 807 | if (config != null) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 808 | PackageManager pm = mContext.getPackageManager(); |
| 809 | String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 810 | int userId = UserHandle.getUserId(config.creatorUid); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 811 | ApplicationInfo appInfo = null; |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 812 | if (config.creatorName != null && config.creatorName.equals(systemName)) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 813 | appInfo = mContext.getApplicationInfo(); |
| 814 | } else { |
| 815 | try { |
| 816 | IPackageManager ipm = AppGlobals.getPackageManager(); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 817 | appInfo = ipm.getApplicationInfo(config.creatorName, 0 /* flags */, userId); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 818 | } catch (RemoteException rex) { |
| 819 | } |
| 820 | } |
| 821 | if (appInfo != null && |
| 822 | !appInfo.packageName.equals(mContext.getString(R.string.settings_package)) && |
| 823 | !appInfo.packageName.equals( |
| 824 | mContext.getString(R.string.certinstaller_package))) { |
| 825 | return mContext.getString(R.string.saved_network, appInfo.loadLabel(pm)); |
| 826 | } |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 827 | } |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 828 | return ""; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 831 | public String getSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 832 | return getSettingsSummary(mConfig); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | public String getSettingsSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 836 | return getSettingsSummary(mConfig); |
| 837 | } |
| 838 | |
| 839 | private String getSettingsSummary(WifiConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 840 | // Update to new summary |
| 841 | StringBuilder summary = new StringBuilder(); |
| 842 | |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 843 | if (isActive() && config != null && config.isPasspoint()) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 844 | // This is the active connection on passpoint |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 845 | summary.append(getSummary(mContext, getDetailedState(), |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 846 | false, config.providerFriendlyName)); |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 847 | } else if (isActive() && config != null && getDetailedState() == DetailedState.CONNECTED |
| 848 | && mIsCarrierAp) { |
| 849 | summary.append(String.format(mContext.getString(R.string.connected_via_carrier), mCarrierName)); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 850 | } else if (isActive()) { |
| 851 | // This is the active connection on non-passpoint network |
| 852 | summary.append(getSummary(mContext, getDetailedState(), |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 853 | mInfo != null && mInfo.isEphemeral())); |
Yuxin Chang | 6b75086 | 2017-01-11 14:59:17 +0900 | [diff] [blame] | 854 | } else if (config != null && config.isPasspoint() |
| 855 | && config.getNetworkSelectionStatus().isNetworkEnabled()) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 856 | String format = mContext.getString(R.string.available_via_passpoint); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 857 | summary.append(String.format(format, config.providerFriendlyName)); |
| 858 | } else if (config != null && config.hasNoInternetAccess()) { |
Salvador Martinez | 5d937fc | 2016-09-23 08:54:20 -0700 | [diff] [blame] | 859 | int messageID = config.getNetworkSelectionStatus().isNetworkPermanentlyDisabled() |
| 860 | ? R.string.wifi_no_internet_no_reconnect |
| 861 | : R.string.wifi_no_internet; |
| 862 | summary.append(mContext.getString(messageID)); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 863 | } else if (config != null && !config.getNetworkSelectionStatus().isNetworkEnabled()) { |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 864 | WifiConfiguration.NetworkSelectionStatus networkStatus = |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 865 | config.getNetworkSelectionStatus(); |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 866 | switch (networkStatus.getNetworkSelectionDisableReason()) { |
| 867 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE: |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 868 | summary.append(mContext.getString(R.string.wifi_disabled_password_failure)); |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 869 | break; |
Peter Qiu | 9c4c6ad | 2017-06-20 13:17:36 -0700 | [diff] [blame] | 870 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD: |
| 871 | summary.append(mContext.getString(R.string.wifi_check_password_try_again)); |
| 872 | break; |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 873 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE: |
| 874 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_DNS_FAILURE: |
| 875 | summary.append(mContext.getString(R.string.wifi_disabled_network_failure)); |
| 876 | break; |
| 877 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION: |
| 878 | summary.append(mContext.getString(R.string.wifi_disabled_generic)); |
| 879 | break; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 880 | } |
Amin Shaikh | 98773d4 | 2017-02-02 17:50:12 -0800 | [diff] [blame] | 881 | } else if (config != null && config.getNetworkSelectionStatus().isNotRecommended()) { |
| 882 | summary.append(mContext.getString(R.string.wifi_disabled_by_recommendation_provider)); |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 883 | } else if (mIsCarrierAp) { |
| 884 | summary.append(String.format(mContext.getString(R.string.available_via_carrier), mCarrierName)); |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 885 | } else if (!isReachable()) { // Wifi out of range |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 886 | summary.append(mContext.getString(R.string.wifi_not_in_range)); |
| 887 | } else { // In range, not disabled. |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 888 | if (config != null) { // Is saved network |
Glen Kuhne | 446afac | 2017-06-22 16:02:33 -0700 | [diff] [blame] | 889 | // Last attempt to connect to this failed. Show reason why |
| 890 | switch (config.recentFailure.getAssociationStatus()) { |
| 891 | case WifiConfiguration.RecentFailure.STATUS_AP_UNABLE_TO_HANDLE_NEW_STA: |
| 892 | summary.append(mContext.getString( |
| 893 | R.string.wifi_ap_unable_to_handle_new_sta)); |
| 894 | break; |
| 895 | default: |
| 896 | // "Saved" |
| 897 | summary.append(mContext.getString(R.string.wifi_remembered)); |
| 898 | break; |
| 899 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 903 | if (isVerboseLoggingEnabled()) { |
jackqdyulei | 9ee9b39 | 2017-12-22 11:31:49 -0800 | [diff] [blame] | 904 | summary.append(WifiUtils.buildLoggingSummary(this, config)); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 905 | } |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 906 | |
Salvador Martinez | 6f6fe75 | 2018-03-21 16:33:53 -0700 | [diff] [blame] | 907 | if (config != null && (WifiUtils.isMeteredOverridden(config) || config.meteredHint)) { |
| 908 | return mContext.getResources().getString( |
| 909 | R.string.preference_summary_default_combination, |
| 910 | WifiUtils.getMeteredLabel(mContext, config), |
| 911 | summary.toString()); |
| 912 | } |
| 913 | |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 914 | // If Speed label and summary are both present, use the preference combination to combine |
| 915 | // the two, else return the non-null one. |
| 916 | if (getSpeedLabel() != null && summary.length() != 0) { |
Sundeep Ghuman | d3171ca | 2017-07-12 23:02:24 -0700 | [diff] [blame] | 917 | return mContext.getResources().getString( |
| 918 | R.string.preference_summary_default_combination, |
| 919 | getSpeedLabel(), |
| 920 | summary.toString()); |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 921 | } else if (getSpeedLabel() != null) { |
| 922 | return getSpeedLabel(); |
Sundeep Ghuman | d3171ca | 2017-07-12 23:02:24 -0700 | [diff] [blame] | 923 | } else { |
| 924 | return summary.toString(); |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 925 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /** |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 929 | * Return whether this is the active connection. |
| 930 | * For ephemeral connections (networkId is invalid), this returns false if the network is |
| 931 | * disconnected. |
| 932 | */ |
| 933 | public boolean isActive() { |
| 934 | return mNetworkInfo != null && |
| 935 | (networkId != WifiConfiguration.INVALID_NETWORK_ID || |
| 936 | mNetworkInfo.getState() != State.DISCONNECTED); |
| 937 | } |
| 938 | |
| 939 | public boolean isConnectable() { |
| 940 | return getLevel() != -1 && getDetailedState() == null; |
| 941 | } |
| 942 | |
| 943 | public boolean isEphemeral() { |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 944 | return mInfo != null && mInfo.isEphemeral() && |
| 945 | mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 946 | } |
| 947 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 948 | /** |
| 949 | * Return true if this AccessPoint represents a Passpoint AP. |
| 950 | */ |
Vinit Deshpande | 5b7352c | 2015-07-09 16:53:12 -0700 | [diff] [blame] | 951 | public boolean isPasspoint() { |
| 952 | return mConfig != null && mConfig.isPasspoint(); |
| 953 | } |
| 954 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 955 | /** |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 956 | * Return true if this AccessPoint represents a Passpoint provider configuration. |
| 957 | */ |
| 958 | public boolean isPasspointConfig() { |
| 959 | return mFqdn != null; |
| 960 | } |
| 961 | |
| 962 | /** |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 963 | * Return whether the given {@link WifiInfo} is for this access point. |
| 964 | * If the current AP does not have a network Id then the config is used to |
| 965 | * match based on SSID and security. |
| 966 | */ |
| 967 | private boolean isInfoForThisAccessPoint(WifiConfiguration config, WifiInfo info) { |
Vinit Deshpande | 5b7352c | 2015-07-09 16:53:12 -0700 | [diff] [blame] | 968 | if (isPasspoint() == false && networkId != WifiConfiguration.INVALID_NETWORK_ID) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 969 | return networkId == info.getNetworkId(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 970 | } else if (config != null) { |
| 971 | return matches(config); |
| 972 | } |
| 973 | else { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 974 | // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID. |
| 975 | // (Note that we only do this if the WifiConfiguration explicitly equals INVALID). |
| 976 | // TODO: Handle hex string SSIDs. |
| 977 | return ssid.equals(removeDoubleQuotes(info.getSSID())); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | public boolean isSaved() { |
| 982 | return networkId != WifiConfiguration.INVALID_NETWORK_ID; |
| 983 | } |
| 984 | |
| 985 | public Object getTag() { |
| 986 | return mTag; |
| 987 | } |
| 988 | |
| 989 | public void setTag(Object tag) { |
| 990 | mTag = tag; |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * Generate and save a default wifiConfiguration with common values. |
| 995 | * Can only be called for unsecured networks. |
| 996 | */ |
| 997 | public void generateOpenNetworkConfig() { |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 998 | if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 999 | throw new IllegalStateException(); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1000 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1001 | if (mConfig != null) |
| 1002 | return; |
| 1003 | mConfig = new WifiConfiguration(); |
| 1004 | mConfig.SSID = AccessPoint.convertToQuotedString(ssid); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1005 | |
| 1006 | if (security == SECURITY_NONE) { |
| 1007 | mConfig.allowedKeyManagement.set(KeyMgmt.NONE); |
| 1008 | } else { |
| 1009 | mConfig.allowedKeyManagement.set(KeyMgmt.OWE); |
| 1010 | mConfig.requirePMF = true; |
| 1011 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1012 | } |
| 1013 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1014 | public void saveWifiState(Bundle savedState) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1015 | if (ssid != null) savedState.putString(KEY_SSID, getSsidStr()); |
| 1016 | savedState.putInt(KEY_SECURITY, security); |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 1017 | savedState.putInt(KEY_SPEED, mSpeed); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1018 | savedState.putInt(KEY_PSKTYPE, pskType); |
| 1019 | if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1020 | savedState.putParcelable(KEY_WIFIINFO, mInfo); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1021 | savedState.putParcelableArray(KEY_SCANRESULTS, |
| 1022 | mScanResults.toArray(new Parcelable[mScanResults.size()])); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1023 | savedState.putParcelableArrayList(KEY_SCOREDNETWORKCACHE, |
| 1024 | new ArrayList<>(mScoredNetworkCache.values())); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1025 | if (mNetworkInfo != null) { |
| 1026 | savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo); |
| 1027 | } |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1028 | if (mFqdn != null) { |
| 1029 | savedState.putString(KEY_FQDN, mFqdn); |
| 1030 | } |
| 1031 | if (mProviderFriendlyName != null) { |
| 1032 | savedState.putString(KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName); |
| 1033 | } |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 1034 | savedState.putBoolean(KEY_IS_CARRIER_AP, mIsCarrierAp); |
| 1035 | savedState.putInt(KEY_CARRIER_AP_EAP_TYPE, mCarrierApEapType); |
| 1036 | savedState.putString(KEY_CARRIER_NAME, mCarrierName); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | public void setListener(AccessPointListener listener) { |
| 1040 | mAccessPointListener = listener; |
| 1041 | } |
| 1042 | |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1043 | /** |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1044 | * Sets {@link #mScanResults} to the given collection. |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1045 | * |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1046 | * @param scanResults a collection of scan results to add to the internal set |
| 1047 | * @throws IllegalArgumentException if any of the given ScanResults did not belong to this AP |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1048 | */ |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1049 | void setScanResults(Collection<ScanResult> scanResults) { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1050 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1051 | // Validate scan results are for current AP only |
| 1052 | String key = getKey(); |
| 1053 | for (ScanResult result : scanResults) { |
| 1054 | String scanResultKey = AccessPoint.getKey(result); |
| 1055 | if (!mKey.equals(scanResultKey)) { |
| 1056 | throw new IllegalArgumentException( |
| 1057 | String.format("ScanResult %s\nkey of %s did not match current AP key %s", |
| 1058 | result, scanResultKey, key)); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1059 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | int oldLevel = getLevel(); |
| 1064 | mScanResults.clear(); |
| 1065 | mScanResults.addAll(scanResults); |
| 1066 | updateRssi(); |
| 1067 | int newLevel = getLevel(); |
| 1068 | |
| 1069 | // If newLevel is 0, there will be no displayed Preference since the AP is unreachable |
| 1070 | if (newLevel > 0 && newLevel != oldLevel) { |
| 1071 | // Only update labels on visible rssi changes |
| 1072 | updateSpeed(); |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1073 | ThreadUtils.postOnMainThread(() -> { |
| 1074 | if (mAccessPointListener != null) { |
| 1075 | mAccessPointListener.onLevelChanged(this); |
| 1076 | } |
| 1077 | }); |
| 1078 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1081 | ThreadUtils.postOnMainThread(() -> { |
| 1082 | if (mAccessPointListener != null) { |
| 1083 | mAccessPointListener.onAccessPointChanged(this); |
| 1084 | } |
| 1085 | }); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1086 | |
| 1087 | if (!scanResults.isEmpty()) { |
| 1088 | ScanResult result = scanResults.iterator().next(); |
| 1089 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1090 | // This flag only comes from scans, is not easily saved in config |
| 1091 | if (security == SECURITY_PSK) { |
| 1092 | pskType = getPskType(result); |
| 1093 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1094 | |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 1095 | // The carrier info in the ScanResult is set by the platform based on the SSID and will |
| 1096 | // always be the same for all matching scan results. |
| 1097 | mIsCarrierAp = result.isCarrierAp; |
| 1098 | mCarrierApEapType = result.carrierApEapType; |
| 1099 | mCarrierName = result.carrierName; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1100 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1101 | } |
| 1102 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1103 | /** Attempt to update the AccessPoint and return true if an update occurred. */ |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1104 | public boolean update( |
| 1105 | @Nullable WifiConfiguration config, WifiInfo info, NetworkInfo networkInfo) { |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 1106 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1107 | boolean updated = false; |
| 1108 | final int oldLevel = getLevel(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1109 | if (info != null && isInfoForThisAccessPoint(config, info)) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1110 | updated = (mInfo == null); |
Sundeep Ghuman | f4d3302 | 2017-06-05 19:47:36 -0700 | [diff] [blame] | 1111 | if (mConfig != config) { |
| 1112 | // We do not set updated = true as we do not want to increase the amount of sorting |
| 1113 | // and copying performed in WifiTracker at this time. If issues involving refresh |
| 1114 | // are still seen, we will investigate further. |
| 1115 | update(config); // Notifies the AccessPointListener of the change |
| 1116 | } |
Glen Kuhne | d38708c | 2017-07-19 14:53:34 -0700 | [diff] [blame] | 1117 | if (mRssi != info.getRssi() && info.getRssi() != WifiInfo.INVALID_RSSI) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1118 | mRssi = info.getRssi(); |
| 1119 | updated = true; |
Sundeep Ghuman | 5c5cd7a | 2017-05-03 12:45:44 -0700 | [diff] [blame] | 1120 | } else if (mNetworkInfo != null && networkInfo != null |
| 1121 | && mNetworkInfo.getDetailedState() != networkInfo.getDetailedState()) { |
Sundeep Ghuman | 96a5357 | 2017-04-20 21:25:41 -0700 | [diff] [blame] | 1122 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1123 | } |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1124 | mInfo = info; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1125 | mNetworkInfo = networkInfo; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1126 | } else if (mInfo != null) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1127 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1128 | mInfo = null; |
| 1129 | mNetworkInfo = null; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1130 | } |
| 1131 | if (updated && mAccessPointListener != null) { |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1132 | ThreadUtils.postOnMainThread(() -> { |
| 1133 | if (mAccessPointListener != null) { |
| 1134 | mAccessPointListener.onAccessPointChanged(this); |
| 1135 | } |
| 1136 | }); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (oldLevel != getLevel() /* current level */) { |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1139 | ThreadUtils.postOnMainThread(() -> { |
| 1140 | if (mAccessPointListener != null) { |
| 1141 | mAccessPointListener.onLevelChanged(this); |
| 1142 | } |
| 1143 | }); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1144 | } |
| 1145 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 1146 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1147 | return updated; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1150 | void update(@Nullable WifiConfiguration config) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1151 | mConfig = config; |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1152 | networkId = config != null ? config.networkId : WifiConfiguration.INVALID_NETWORK_ID; |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1153 | ThreadUtils.postOnMainThread(() -> { |
| 1154 | if (mAccessPointListener != null) { |
| 1155 | mAccessPointListener.onAccessPointChanged(this); |
| 1156 | } |
| 1157 | }); |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1158 | } |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 1159 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1160 | @VisibleForTesting |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 1161 | void setRssi(int rssi) { |
| 1162 | mRssi = rssi; |
| 1163 | } |
| 1164 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1165 | /** Sets the rssi to {@link #UNREACHABLE_RSSI}. */ |
| 1166 | void setUnreachable() { |
| 1167 | setRssi(AccessPoint.UNREACHABLE_RSSI); |
| 1168 | } |
| 1169 | |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1170 | int getSpeed() { return mSpeed;} |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1171 | |
| 1172 | @Nullable |
| 1173 | String getSpeedLabel() { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 1174 | return getSpeedLabel(mSpeed); |
| 1175 | } |
| 1176 | |
| 1177 | @Nullable |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1178 | @Speed |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1179 | private static int roundToClosestSpeedEnum(int speed) { |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1180 | if (speed < Speed.SLOW) { |
| 1181 | return Speed.NONE; |
| 1182 | } else if (speed < (Speed.SLOW + Speed.MODERATE) / 2) { |
| 1183 | return Speed.SLOW; |
| 1184 | } else if (speed < (Speed.MODERATE + Speed.FAST) / 2) { |
| 1185 | return Speed.MODERATE; |
| 1186 | } else if (speed < (Speed.FAST + Speed.VERY_FAST) / 2) { |
| 1187 | return Speed.FAST; |
| 1188 | } else { |
| 1189 | return Speed.VERY_FAST; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | @Nullable |
jackqdyulei | 9ee9b39 | 2017-12-22 11:31:49 -0800 | [diff] [blame] | 1194 | String getSpeedLabel(@Speed int speed) { |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1195 | return getSpeedLabel(mContext, speed); |
| 1196 | } |
| 1197 | |
| 1198 | private static String getSpeedLabel(Context context, int speed) { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 1199 | switch (speed) { |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1200 | case Speed.VERY_FAST: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1201 | return context.getString(R.string.speed_label_very_fast); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1202 | case Speed.FAST: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1203 | return context.getString(R.string.speed_label_fast); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1204 | case Speed.MODERATE: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1205 | return context.getString(R.string.speed_label_okay); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1206 | case Speed.SLOW: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1207 | return context.getString(R.string.speed_label_slow); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1208 | case Speed.NONE: |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1209 | default: |
| 1210 | return null; |
| 1211 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1214 | /** Return the speed label for a {@link ScoredNetwork} at the specified {@code rssi} level. */ |
| 1215 | @Nullable |
| 1216 | public static String getSpeedLabel(Context context, ScoredNetwork scoredNetwork, int rssi) { |
| 1217 | return getSpeedLabel(context, roundToClosestSpeedEnum(scoredNetwork.calculateBadge(rssi))); |
| 1218 | } |
| 1219 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1220 | /** Return true if the current RSSI is reachable, and false otherwise. */ |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 1221 | public boolean isReachable() { |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1222 | return mRssi != UNREACHABLE_RSSI; |
| 1223 | } |
| 1224 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1225 | public static String getSummary(Context context, String ssid, DetailedState state, |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1226 | boolean isEphemeral, String passpointProvider) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1227 | if (state == DetailedState.CONNECTED && ssid == null) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1228 | if (TextUtils.isEmpty(passpointProvider) == false) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1229 | // Special case for connected + passpoint networks. |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1230 | String format = context.getString(R.string.connected_via_passpoint); |
| 1231 | return String.format(format, passpointProvider); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1232 | } else if (isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1233 | // Special case for connected + ephemeral networks. |
Stephen Chen | 36dd5cf1 | 2017-03-20 13:27:51 -0700 | [diff] [blame] | 1234 | final NetworkScoreManager networkScoreManager = context.getSystemService( |
| 1235 | NetworkScoreManager.class); |
| 1236 | NetworkScorerAppData scorer = networkScoreManager.getActiveScorer(); |
| 1237 | if (scorer != null && scorer.getRecommendationServiceLabel() != null) { |
| 1238 | String format = context.getString(R.string.connected_via_network_scorer); |
| 1239 | return String.format(format, scorer.getRecommendationServiceLabel()); |
| 1240 | } else { |
| 1241 | return context.getString(R.string.connected_via_network_scorer_default); |
| 1242 | } |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1243 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1244 | } |
| 1245 | |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1246 | // Case when there is wifi connected without internet connectivity. |
| 1247 | final ConnectivityManager cm = (ConnectivityManager) |
| 1248 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 1249 | if (state == DetailedState.CONNECTED) { |
| 1250 | IWifiManager wifiManager = IWifiManager.Stub.asInterface( |
| 1251 | ServiceManager.getService(Context.WIFI_SERVICE)); |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1252 | NetworkCapabilities nc = null; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1253 | |
| 1254 | try { |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1255 | nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork()); |
| 1256 | } catch (RemoteException e) {} |
| 1257 | |
| 1258 | if (nc != null) { |
| 1259 | if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) { |
Maurice Lam | 7f6fc86 | 2017-07-12 16:43:49 -0700 | [diff] [blame] | 1260 | int id = context.getResources() |
| 1261 | .getIdentifier("network_available_sign_in", "string", "android"); |
| 1262 | return context.getString(id); |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1263 | } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) { |
| 1264 | return context.getString(R.string.wifi_connected_no_internet); |
| 1265 | } |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1266 | } |
| 1267 | } |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 1268 | if (state == null) { |
| 1269 | Log.w(TAG, "state is null, returning empty summary"); |
| 1270 | return ""; |
| 1271 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1272 | String[] formats = context.getResources().getStringArray((ssid == null) |
| 1273 | ? R.array.wifi_status : R.array.wifi_status_with_ssid); |
| 1274 | int index = state.ordinal(); |
| 1275 | |
| 1276 | if (index >= formats.length || formats[index].length() == 0) { |
Sanket Padawe | 3e9e5fa | 2015-05-28 10:41:14 -0700 | [diff] [blame] | 1277 | return ""; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1278 | } |
| 1279 | return String.format(formats[index], ssid); |
| 1280 | } |
| 1281 | |
| 1282 | public static String getSummary(Context context, DetailedState state, boolean isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1283 | return getSummary(context, null, state, isEphemeral, null); |
| 1284 | } |
| 1285 | |
| 1286 | public static String getSummary(Context context, DetailedState state, boolean isEphemeral, |
| 1287 | String passpointProvider) { |
| 1288 | return getSummary(context, null, state, isEphemeral, passpointProvider); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | public static String convertToQuotedString(String string) { |
| 1292 | return "\"" + string + "\""; |
| 1293 | } |
| 1294 | |
| 1295 | private static int getPskType(ScanResult result) { |
| 1296 | boolean wpa = result.capabilities.contains("WPA-PSK"); |
| 1297 | boolean wpa2 = result.capabilities.contains("WPA2-PSK"); |
| 1298 | if (wpa2 && wpa) { |
| 1299 | return PSK_WPA_WPA2; |
| 1300 | } else if (wpa2) { |
| 1301 | return PSK_WPA2; |
| 1302 | } else if (wpa) { |
| 1303 | return PSK_WPA; |
| 1304 | } else { |
| 1305 | Log.w(TAG, "Received abnormal flag string: " + result.capabilities); |
| 1306 | return PSK_UNKNOWN; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | private static int getSecurity(ScanResult result) { |
| 1311 | if (result.capabilities.contains("WEP")) { |
| 1312 | return SECURITY_WEP; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1313 | } else if (result.capabilities.contains("SAE")) { |
| 1314 | return SECURITY_SAE; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1315 | } else if (result.capabilities.contains("PSK")) { |
| 1316 | return SECURITY_PSK; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1317 | } else if (result.capabilities.contains("EAP_SUITE_B_192")) { |
| 1318 | return SECURITY_EAP_SUITE_B; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1319 | } else if (result.capabilities.contains("EAP")) { |
| 1320 | return SECURITY_EAP; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1321 | } else if (result.capabilities.contains("OWE")) { |
| 1322 | return SECURITY_OWE; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1323 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1324 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1325 | return SECURITY_NONE; |
| 1326 | } |
| 1327 | |
| 1328 | static int getSecurity(WifiConfiguration config) { |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1329 | if (config.allowedKeyManagement.get(KeyMgmt.SAE)) { |
| 1330 | return SECURITY_SAE; |
| 1331 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1332 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) { |
| 1333 | return SECURITY_PSK; |
| 1334 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1335 | if (config.allowedKeyManagement.get(KeyMgmt.SUITE_B_192)) { |
| 1336 | return SECURITY_EAP_SUITE_B; |
| 1337 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1338 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || |
| 1339 | config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) { |
| 1340 | return SECURITY_EAP; |
| 1341 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1342 | if (config.allowedKeyManagement.get(KeyMgmt.OWE)) { |
| 1343 | return SECURITY_OWE; |
| 1344 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1345 | return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE; |
| 1346 | } |
| 1347 | |
| 1348 | public static String securityToString(int security, int pskType) { |
| 1349 | if (security == SECURITY_WEP) { |
| 1350 | return "WEP"; |
| 1351 | } else if (security == SECURITY_PSK) { |
| 1352 | if (pskType == PSK_WPA) { |
| 1353 | return "WPA"; |
| 1354 | } else if (pskType == PSK_WPA2) { |
| 1355 | return "WPA2"; |
| 1356 | } else if (pskType == PSK_WPA_WPA2) { |
| 1357 | return "WPA_WPA2"; |
| 1358 | } |
| 1359 | return "PSK"; |
| 1360 | } else if (security == SECURITY_EAP) { |
| 1361 | return "EAP"; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1362 | } else if (security == SECURITY_SAE) { |
| 1363 | return "SAE"; |
| 1364 | } else if (security == SECURITY_EAP_SUITE_B) { |
| 1365 | return "SUITE_B"; |
| 1366 | } else if (security == SECURITY_OWE) { |
| 1367 | return "OWE"; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1368 | } |
| 1369 | return "NONE"; |
| 1370 | } |
| 1371 | |
| 1372 | static String removeDoubleQuotes(String string) { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 1373 | if (TextUtils.isEmpty(string)) { |
| 1374 | return ""; |
| 1375 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1376 | int length = string.length(); |
| 1377 | if ((length > 1) && (string.charAt(0) == '"') |
| 1378 | && (string.charAt(length - 1) == '"')) { |
| 1379 | return string.substring(1, length - 1); |
| 1380 | } |
| 1381 | return string; |
| 1382 | } |
| 1383 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1384 | /** |
| 1385 | * Callbacks relaying changes to the AccessPoint representation. |
| 1386 | * |
| 1387 | * <p>All methods are invoked on the Main Thread. |
| 1388 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1389 | public interface AccessPointListener { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1390 | /** |
| 1391 | * Indicates a change to the externally visible state of the AccessPoint trigger by an |
| 1392 | * update of ScanResults, saved configuration state, connection state, or score |
| 1393 | * (labels/metered) state. |
| 1394 | * |
| 1395 | * <p>Clients should refresh their view of the AccessPoint to match the updated state when |
| 1396 | * this is invoked. Overall this method is extraneous if clients are listening to |
| 1397 | * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks. |
| 1398 | * |
| 1399 | * <p>Examples of changes include signal strength, connection state, speed label, and |
| 1400 | * generally anything that would impact the summary string. |
| 1401 | * |
| 1402 | * @param accessPoint The accessPoint object the listener was registered on which has |
| 1403 | * changed |
| 1404 | */ |
| 1405 | @MainThread void onAccessPointChanged(AccessPoint accessPoint); |
| 1406 | |
| 1407 | /** |
| 1408 | * Indicates the "wifi pie signal level" has changed, retrieved via calls to |
| 1409 | * {@link AccessPoint#getLevel()}. |
| 1410 | * |
| 1411 | * <p>This call is a subset of {@link #onAccessPointChanged(AccessPoint)} , hence is also |
| 1412 | * extraneous if the client is already reacting to that or the |
| 1413 | * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks. |
| 1414 | * |
| 1415 | * @param accessPoint The accessPoint object the listener was registered on whose level has |
| 1416 | * changed |
| 1417 | */ |
| 1418 | @MainThread void onLevelChanged(AccessPoint accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1419 | } |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 1420 | |
| 1421 | private static boolean isVerboseLoggingEnabled() { |
| 1422 | return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE); |
| 1423 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1424 | } |