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 | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 20 | import android.app.AppGlobals; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 21 | import android.content.Context; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 22 | import android.content.pm.ApplicationInfo; |
| 23 | import android.content.pm.IPackageManager; |
| 24 | import android.content.pm.PackageManager; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 25 | import android.net.ConnectivityManager; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 26 | import android.net.NetworkCapabilities; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 27 | import android.net.NetworkInfo; |
| 28 | import android.net.NetworkInfo.DetailedState; |
| 29 | import android.net.NetworkInfo.State; |
Stephen Chen | 36dd5cf1 | 2017-03-20 13:27:51 -0700 | [diff] [blame] | 30 | import android.net.NetworkScoreManager; |
| 31 | import android.net.NetworkScorerAppData; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 32 | import android.net.ScoredNetwork; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 33 | import android.net.wifi.IWifiManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 34 | import android.net.wifi.ScanResult; |
| 35 | import android.net.wifi.WifiConfiguration; |
| 36 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
| 37 | import android.net.wifi.WifiInfo; |
| 38 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 39 | import android.net.wifi.WifiNetworkScoreCache; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 40 | import android.net.wifi.hotspot2.PasspointConfiguration; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 41 | import android.os.Bundle; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 42 | import android.os.RemoteException; |
| 43 | import android.os.ServiceManager; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 44 | import android.os.SystemClock; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 45 | import android.os.UserHandle; |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 46 | import android.support.annotation.NonNull; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 47 | import android.text.Spannable; |
| 48 | import android.text.SpannableString; |
| 49 | import android.text.TextUtils; |
| 50 | import android.text.style.TtsSpan; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 51 | import android.util.Log; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 52 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 53 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 54 | import com.android.settingslib.R; |
| 55 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 56 | import java.util.ArrayList; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 57 | import java.util.Iterator; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 58 | import java.util.concurrent.ConcurrentHashMap; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 59 | import java.util.concurrent.atomic.AtomicInteger; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 60 | |
| 61 | |
| 62 | public class AccessPoint implements Comparable<AccessPoint> { |
| 63 | static final String TAG = "SettingsLib.AccessPoint"; |
| 64 | |
| 65 | /** |
| 66 | * Lower bound on the 2.4 GHz (802.11b/g/n) WLAN channels |
| 67 | */ |
| 68 | public static final int LOWER_FREQ_24GHZ = 2400; |
| 69 | |
| 70 | /** |
| 71 | * Upper bound on the 2.4 GHz (802.11b/g/n) WLAN channels |
| 72 | */ |
| 73 | public static final int HIGHER_FREQ_24GHZ = 2500; |
| 74 | |
| 75 | /** |
| 76 | * Lower bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels |
| 77 | */ |
| 78 | public static final int LOWER_FREQ_5GHZ = 4900; |
| 79 | |
| 80 | /** |
| 81 | * Upper bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels |
| 82 | */ |
| 83 | public static final int HIGHER_FREQ_5GHZ = 5900; |
| 84 | |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Constant value representing an unlabeled / unscored network. |
| 87 | */ |
| 88 | @VisibleForTesting |
| 89 | static final int SPEED_NONE = 0; |
| 90 | |
| 91 | /** |
| 92 | * Constant value representing a slow speed network connection. |
| 93 | */ |
| 94 | @VisibleForTesting |
| 95 | static final int SPEED_SLOW = 5; |
| 96 | |
| 97 | /** |
| 98 | * Constant value representing a medium speed network connection. |
| 99 | */ |
| 100 | @VisibleForTesting |
| 101 | static final int SPEED_MEDIUM = 10; |
| 102 | |
| 103 | /** |
| 104 | * Constant value representing a fast speed network connection. |
| 105 | */ |
| 106 | @VisibleForTesting |
| 107 | static final int SPEED_FAST = 20; |
| 108 | |
| 109 | /** |
| 110 | * Constant value representing a very fast speed network connection. |
| 111 | */ |
| 112 | @VisibleForTesting |
| 113 | static final int SPEED_VERY_FAST = 30; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Experimental: we should be able to show the user the list of BSSIDs and bands |
| 117 | * for that SSID. |
| 118 | * For now this data is used only with Verbose Logging so as to show the band and number |
| 119 | * of BSSIDs on which that network is seen. |
| 120 | */ |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 121 | private final ConcurrentHashMap<String, ScanResult> mScanResultCache = |
| 122 | new ConcurrentHashMap<String, ScanResult>(32); |
| 123 | private static final long MAX_SCAN_RESULT_AGE_MS = 15000; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 124 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 125 | static final String KEY_NETWORKINFO = "key_networkinfo"; |
| 126 | static final String KEY_WIFIINFO = "key_wifiinfo"; |
| 127 | static final String KEY_SCANRESULT = "key_scanresult"; |
| 128 | static final String KEY_SSID = "key_ssid"; |
| 129 | static final String KEY_SECURITY = "key_security"; |
| 130 | static final String KEY_PSKTYPE = "key_psktype"; |
| 131 | static final String KEY_SCANRESULTCACHE = "key_scanresultcache"; |
| 132 | static final String KEY_CONFIG = "key_config"; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 133 | static final String KEY_FQDN = "key_fqdn"; |
| 134 | static final String KEY_PROVIDER_FRIENDLY_NAME = "key_provider_friendly_name"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 135 | static final AtomicInteger sLastId = new AtomicInteger(0); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * These values are matched in string arrays -- changes must be kept in sync |
| 139 | */ |
| 140 | public static final int SECURITY_NONE = 0; |
| 141 | public static final int SECURITY_WEP = 1; |
| 142 | public static final int SECURITY_PSK = 2; |
| 143 | public static final int SECURITY_EAP = 3; |
| 144 | |
| 145 | private static final int PSK_UNKNOWN = 0; |
| 146 | private static final int PSK_WPA = 1; |
| 147 | private static final int PSK_WPA2 = 2; |
| 148 | private static final int PSK_WPA_WPA2 = 3; |
| 149 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 150 | /** |
| 151 | * The number of distinct wifi levels. |
| 152 | * |
| 153 | * <p>Must keep in sync with {@link R.array.wifi_signal} and {@link WifiManager#RSSI_LEVELS}. |
| 154 | */ |
| 155 | public static final int SIGNAL_LEVELS = 5; |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 156 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 157 | public static final int UNREACHABLE_RSSI = Integer.MIN_VALUE; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 158 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 159 | private final Context mContext; |
| 160 | |
| 161 | private String ssid; |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 162 | private String bssid; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 163 | private int security; |
| 164 | private int networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 165 | |
| 166 | private int pskType = PSK_UNKNOWN; |
| 167 | |
| 168 | private WifiConfiguration mConfig; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 169 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 170 | private int mRssi = UNREACHABLE_RSSI; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 171 | private long mSeen = 0; |
| 172 | |
| 173 | private WifiInfo mInfo; |
| 174 | private NetworkInfo mNetworkInfo; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 175 | AccessPointListener mAccessPointListener; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 176 | |
| 177 | private Object mTag; |
| 178 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 179 | private int mRankingScore = Integer.MIN_VALUE; |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 180 | private int mSpeed = AccessPoint.SPEED_NONE; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 181 | private boolean mIsScoredNetworkMetered = false; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 182 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 183 | // used to co-relate internal vs returned accesspoint. |
| 184 | int mId; |
| 185 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Information associated with the {@link PasspointConfiguration}. Only maintaining |
| 188 | * the relevant info to preserve spaces. |
| 189 | */ |
| 190 | private String mFqdn; |
| 191 | private String mProviderFriendlyName; |
| 192 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 193 | public AccessPoint(Context context, Bundle savedState) { |
| 194 | mContext = context; |
| 195 | mConfig = savedState.getParcelable(KEY_CONFIG); |
| 196 | if (mConfig != null) { |
| 197 | loadConfig(mConfig); |
| 198 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 199 | if (savedState.containsKey(KEY_SSID)) { |
| 200 | ssid = savedState.getString(KEY_SSID); |
| 201 | } |
| 202 | if (savedState.containsKey(KEY_SECURITY)) { |
| 203 | security = savedState.getInt(KEY_SECURITY); |
| 204 | } |
| 205 | if (savedState.containsKey(KEY_PSKTYPE)) { |
| 206 | pskType = savedState.getInt(KEY_PSKTYPE); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 207 | } |
| 208 | mInfo = (WifiInfo) savedState.getParcelable(KEY_WIFIINFO); |
| 209 | if (savedState.containsKey(KEY_NETWORKINFO)) { |
| 210 | mNetworkInfo = savedState.getParcelable(KEY_NETWORKINFO); |
| 211 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 212 | if (savedState.containsKey(KEY_SCANRESULTCACHE)) { |
| 213 | ArrayList<ScanResult> scanResultArrayList = |
| 214 | savedState.getParcelableArrayList(KEY_SCANRESULTCACHE); |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 215 | mScanResultCache.clear(); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 216 | for (ScanResult result : scanResultArrayList) { |
| 217 | mScanResultCache.put(result.BSSID, result); |
| 218 | } |
| 219 | } |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 220 | if (savedState.containsKey(KEY_FQDN)) { |
| 221 | mFqdn = savedState.getString(KEY_FQDN); |
| 222 | } |
| 223 | if (savedState.containsKey(KEY_PROVIDER_FRIENDLY_NAME)) { |
| 224 | mProviderFriendlyName = savedState.getString(KEY_PROVIDER_FRIENDLY_NAME); |
| 225 | } |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 226 | update(mConfig, mInfo, mNetworkInfo); |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 227 | updateRssi(); |
| 228 | updateSeen(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 229 | mId = sLastId.incrementAndGet(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 232 | public AccessPoint(Context context, WifiConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 233 | mContext = context; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 234 | loadConfig(config); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 235 | mId = sLastId.incrementAndGet(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 236 | } |
| 237 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 238 | /** |
| 239 | * Initialize an AccessPoint object for a {@link PasspointConfiguration}. This is mainly |
| 240 | * used by "Saved Networks" page for managing the saved {@link PasspointConfiguration}. |
| 241 | */ |
| 242 | public AccessPoint(Context context, PasspointConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 243 | mContext = context; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 244 | mFqdn = config.getHomeSp().getFqdn(); |
| 245 | mProviderFriendlyName = config.getHomeSp().getFriendlyName(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 246 | mId = sLastId.incrementAndGet(); |
| 247 | } |
| 248 | |
| 249 | AccessPoint(Context context, AccessPoint other) { |
| 250 | mContext = context; |
| 251 | copyFrom(other); |
| 252 | } |
| 253 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 254 | AccessPoint(Context context, ScanResult result) { |
| 255 | mContext = context; |
| 256 | initWithScanResult(result); |
| 257 | mId = sLastId.incrementAndGet(); |
| 258 | } |
| 259 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Copy accesspoint information. NOTE: We do not copy tag information because that is never |
| 262 | * set on the internal copy. |
| 263 | * @param that |
| 264 | */ |
| 265 | void copyFrom(AccessPoint that) { |
| 266 | that.evictOldScanResults(); |
| 267 | this.ssid = that.ssid; |
| 268 | this.bssid = that.bssid; |
| 269 | this.security = that.security; |
| 270 | this.networkId = that.networkId; |
| 271 | this.pskType = that.pskType; |
| 272 | this.mConfig = that.mConfig; //TODO: Watch out, this object is mutated. |
| 273 | this.mRssi = that.mRssi; |
| 274 | this.mSeen = that.mSeen; |
| 275 | this.mInfo = that.mInfo; |
| 276 | this.mNetworkInfo = that.mNetworkInfo; |
| 277 | this.mScanResultCache.clear(); |
| 278 | this.mScanResultCache.putAll(that.mScanResultCache); |
| 279 | this.mId = that.mId; |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 280 | this.mSpeed = that.mSpeed; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 281 | this.mIsScoredNetworkMetered = that.mIsScoredNetworkMetered; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 282 | this.mRankingScore = that.mRankingScore; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 285 | /** |
| 286 | * Returns a negative integer, zero, or a positive integer if this AccessPoint is less than, |
| 287 | * equal to, or greater than the other AccessPoint. |
| 288 | * |
| 289 | * Sort order rules for AccessPoints: |
| 290 | * 1. Active before inactive |
| 291 | * 2. Reachable before unreachable |
| 292 | * 3. Saved before unsaved |
| 293 | * 4. (Internal only) Network ranking score |
| 294 | * 5. Stronger signal before weaker signal |
| 295 | * 6. SSID alphabetically |
| 296 | * |
| 297 | * Note that AccessPoints with a signal are usually also Reachable, |
| 298 | * and will thus appear before unreachable saved AccessPoints. |
| 299 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 300 | @Override |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 301 | public int compareTo(@NonNull AccessPoint other) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 302 | // Active one goes first. |
| 303 | if (isActive() && !other.isActive()) return -1; |
| 304 | if (!isActive() && other.isActive()) return 1; |
| 305 | |
| 306 | // Reachable one goes before unreachable one. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 307 | if (isReachable() && !other.isReachable()) return -1; |
| 308 | if (!isReachable() && other.isReachable()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 309 | |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 310 | // Configured (saved) one goes before unconfigured one. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 311 | if (isSaved() && !other.isSaved()) return -1; |
| 312 | if (!isSaved() && other.isSaved()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 313 | |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 314 | // Higher scores go before lower scores |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 315 | if (getRankingScore() != other.getRankingScore()) { |
| 316 | return (getRankingScore() > other.getRankingScore()) ? -1 : 1; |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 319 | // Sort by signal strength, bucketed by level |
| 320 | int difference = WifiManager.calculateSignalLevel(other.mRssi, SIGNAL_LEVELS) |
| 321 | - WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 322 | if (difference != 0) { |
| 323 | return difference; |
| 324 | } |
| 325 | // Sort by ssid. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 326 | return getSsidStr().compareToIgnoreCase(other.getSsidStr()); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public boolean equals(Object other) { |
| 331 | if (!(other instanceof AccessPoint)) return false; |
| 332 | return (this.compareTo((AccessPoint) other) == 0); |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public int hashCode() { |
| 337 | int result = 0; |
| 338 | if (mInfo != null) result += 13 * mInfo.hashCode(); |
| 339 | result += 19 * mRssi; |
| 340 | result += 23 * networkId; |
| 341 | result += 29 * ssid.hashCode(); |
| 342 | return result; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public String toString() { |
| 347 | StringBuilder builder = new StringBuilder().append("AccessPoint(") |
| 348 | .append(ssid); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 349 | if (bssid != null) { |
| 350 | builder.append(":").append(bssid); |
| 351 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 352 | if (isSaved()) { |
| 353 | builder.append(',').append("saved"); |
| 354 | } |
| 355 | if (isActive()) { |
| 356 | builder.append(',').append("active"); |
| 357 | } |
| 358 | if (isEphemeral()) { |
| 359 | builder.append(',').append("ephemeral"); |
| 360 | } |
| 361 | if (isConnectable()) { |
| 362 | builder.append(',').append("connectable"); |
| 363 | } |
| 364 | if (security != SECURITY_NONE) { |
| 365 | builder.append(',').append(securityToString(security, pskType)); |
| 366 | } |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 367 | builder.append(",level=").append(getLevel()); |
Sundeep Ghuman | 8920e9c | 2017-04-27 16:16:41 -0700 | [diff] [blame] | 368 | if (mRankingScore != Integer.MIN_VALUE) { |
| 369 | builder.append(",rankingScore=").append(mRankingScore); |
| 370 | } |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 371 | if (mSpeed != SPEED_NONE) { |
| 372 | builder.append(",speed=").append(mSpeed); |
Sundeep Ghuman | 8920e9c | 2017-04-27 16:16:41 -0700 | [diff] [blame] | 373 | } |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 374 | builder.append(",metered=").append(isMetered()); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 375 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 376 | return builder.append(')').toString(); |
| 377 | } |
| 378 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 379 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 380 | * 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] | 381 | * changed. |
| 382 | * |
| 383 | * @param scoreCache The score cache to use to retrieve scores. |
| 384 | * @param scoringUiEnabled Whether to show scoring and badging UI. |
| 385 | */ |
| 386 | boolean update(WifiNetworkScoreCache scoreCache, boolean scoringUiEnabled) { |
| 387 | boolean scoreChanged = false; |
| 388 | if (scoringUiEnabled) { |
| 389 | scoreChanged = updateScores(scoreCache); |
| 390 | } |
| 391 | return updateMetered(scoreCache) || scoreChanged; |
| 392 | } |
| 393 | |
| 394 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 395 | * 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] | 396 | * |
| 397 | * @param scoreCache The score cache to use to retrieve scores. |
| 398 | */ |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 399 | private boolean updateScores(WifiNetworkScoreCache scoreCache) { |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 400 | int oldSpeed = mSpeed; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 401 | int oldRankingScore = mRankingScore; |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 402 | mSpeed = SPEED_NONE; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 403 | mRankingScore = Integer.MIN_VALUE; |
| 404 | |
| 405 | for (ScanResult result : mScanResultCache.values()) { |
| 406 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 407 | if (score == null) { |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | if (score.hasRankingScore()) { |
| 412 | mRankingScore = Math.max(mRankingScore, score.calculateRankingScore(result.level)); |
| 413 | } |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 414 | // TODO(sghuman): Rename calculateBadge API |
| 415 | mSpeed = Math.max(mSpeed, score.calculateBadge(result.level)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 418 | return (oldSpeed != mSpeed || oldRankingScore != mRankingScore); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Updates the AccessPoint's metering based on {@link ScoredNetwork#meteredHint}, returning |
| 423 | * true if the metering changed. |
| 424 | */ |
| 425 | private boolean updateMetered(WifiNetworkScoreCache scoreCache) { |
| 426 | boolean oldMetering = mIsScoredNetworkMetered; |
| 427 | mIsScoredNetworkMetered = false; |
| 428 | for (ScanResult result : mScanResultCache.values()) { |
| 429 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 430 | if (score == null) { |
| 431 | continue; |
| 432 | } |
| 433 | mIsScoredNetworkMetered |= score.meteredHint; |
| 434 | } |
| 435 | return oldMetering == mIsScoredNetworkMetered; |
| 436 | } |
| 437 | |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 438 | private void evictOldScanResults() { |
| 439 | long nowMs = SystemClock.elapsedRealtime(); |
| 440 | for (Iterator<ScanResult> iter = mScanResultCache.values().iterator(); iter.hasNext(); ) { |
| 441 | ScanResult result = iter.next(); |
| 442 | // result timestamp is in microseconds |
| 443 | if (nowMs - result.timestamp / 1000 > MAX_SCAN_RESULT_AGE_MS) { |
| 444 | iter.remove(); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 449 | public boolean matches(ScanResult result) { |
| 450 | return ssid.equals(result.SSID) && security == getSecurity(result); |
| 451 | } |
| 452 | |
| 453 | public boolean matches(WifiConfiguration config) { |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 454 | if (config.isPasspoint() && mConfig != null && mConfig.isPasspoint()) { |
Shinji Sogo | f29e122 | 2015-12-09 17:21:38 +0900 | [diff] [blame] | 455 | return ssid.equals(removeDoubleQuotes(config.SSID)) && config.FQDN.equals(mConfig.FQDN); |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 456 | } else { |
| 457 | return ssid.equals(removeDoubleQuotes(config.SSID)) |
| 458 | && security == getSecurity(config) |
| 459 | && (mConfig == null || mConfig.shared == config.shared); |
| 460 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | public WifiConfiguration getConfig() { |
| 464 | return mConfig; |
| 465 | } |
| 466 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 467 | public String getPasspointFqdn() { |
| 468 | return mFqdn; |
| 469 | } |
| 470 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 471 | public void clearConfig() { |
| 472 | mConfig = null; |
| 473 | networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 474 | } |
| 475 | |
| 476 | public WifiInfo getInfo() { |
| 477 | return mInfo; |
| 478 | } |
| 479 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 480 | /** |
| 481 | * Returns the number of levels to show for a Wifi icon, from 0 to {@link #SIGNAL_LEVELS}-1. |
| 482 | * |
| 483 | * <p>Use {@#isReachable()} to determine if an AccessPoint is in range, as this method will |
| 484 | * always return at least 0. |
| 485 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 486 | public int getLevel() { |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 487 | return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 490 | public int getRssi() { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 491 | return mRssi; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Updates {@link #mRssi}. |
| 496 | * |
| 497 | * <p>If the given connection is active, the existing value of {@link #mRssi} will be returned. |
| 498 | * 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] | 499 | * results, returning the best RSSI for all matching AccessPoints averaged with the previous |
| 500 | * value. If the access point is not connected and there are no scan results, the rssi will be |
| 501 | * set to {@link #UNREACHABLE_RSSI}. |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 502 | * |
| 503 | * <p>Old scan results will be evicted from the cache when this method is invoked. |
| 504 | */ |
| 505 | private void updateRssi() { |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 506 | evictOldScanResults(); |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 507 | |
| 508 | if (this.isActive()) { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | int rssi = UNREACHABLE_RSSI; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 513 | for (ScanResult result : mScanResultCache.values()) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 514 | if (result.level > rssi) { |
| 515 | rssi = result.level; |
| 516 | } |
| 517 | } |
| 518 | |
Sundeep Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 519 | if (rssi != UNREACHABLE_RSSI && mRssi != UNREACHABLE_RSSI) { |
| 520 | mRssi = (mRssi + rssi) / 2; // half-life previous value |
| 521 | } else { |
| 522 | mRssi = rssi; |
| 523 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 526 | /** |
| 527 | * Updates {@link #mSeen} based on the scan result cache. |
| 528 | * |
| 529 | * <p>Old scan results will be evicted from the cache when this method is invoked. |
| 530 | */ |
| 531 | private void updateSeen() { |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 532 | evictOldScanResults(); |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 533 | |
| 534 | // TODO(sghuman): Set to now if connected |
| 535 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 536 | long seen = 0; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 537 | for (ScanResult result : mScanResultCache.values()) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 538 | if (result.timestamp > seen) { |
| 539 | seen = result.timestamp; |
| 540 | } |
| 541 | } |
| 542 | |
Sundeep Ghuman | 2412817 | 2017-06-15 17:55:21 -0700 | [diff] [blame] | 543 | // Only replace the previous value if we have a recent scan result to use |
| 544 | if (seen != 0) { |
| 545 | mSeen = seen; |
| 546 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 549 | /** |
| 550 | * Returns if the network is marked metered. Metering can be marked through its config in |
| 551 | * {@link WifiConfiguration}, after connection in {@link WifiInfo}, or from a score config in |
| 552 | * {@link ScoredNetwork}. |
| 553 | */ |
| 554 | public boolean isMetered() { |
| 555 | return mIsScoredNetworkMetered |
| 556 | || (mConfig != null && mConfig.meteredHint) |
Stephen Chen | b5bcb8d | 2017-05-19 15:55:02 -0700 | [diff] [blame] | 557 | || (mInfo != null && mInfo.getMeteredHint() |
| 558 | || (mNetworkInfo != null && mNetworkInfo.isMetered())); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 561 | public NetworkInfo getNetworkInfo() { |
| 562 | return mNetworkInfo; |
| 563 | } |
| 564 | |
| 565 | public int getSecurity() { |
| 566 | return security; |
| 567 | } |
| 568 | |
| 569 | public String getSecurityString(boolean concise) { |
| 570 | Context context = mContext; |
Sanket Padawe | d187880 | 2015-05-12 10:27:19 -0700 | [diff] [blame] | 571 | if (mConfig != null && mConfig.isPasspoint()) { |
Sanket Padawe | 194cce6 | 2015-07-10 10:53:31 -0700 | [diff] [blame] | 572 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 573 | context.getString(R.string.wifi_security_eap); |
Sanket Padawe | d187880 | 2015-05-12 10:27:19 -0700 | [diff] [blame] | 574 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 575 | switch(security) { |
| 576 | case SECURITY_EAP: |
| 577 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 578 | context.getString(R.string.wifi_security_eap); |
| 579 | case SECURITY_PSK: |
| 580 | switch (pskType) { |
| 581 | case PSK_WPA: |
| 582 | return concise ? context.getString(R.string.wifi_security_short_wpa) : |
| 583 | context.getString(R.string.wifi_security_wpa); |
| 584 | case PSK_WPA2: |
| 585 | return concise ? context.getString(R.string.wifi_security_short_wpa2) : |
| 586 | context.getString(R.string.wifi_security_wpa2); |
| 587 | case PSK_WPA_WPA2: |
| 588 | return concise ? context.getString(R.string.wifi_security_short_wpa_wpa2) : |
| 589 | context.getString(R.string.wifi_security_wpa_wpa2); |
| 590 | case PSK_UNKNOWN: |
| 591 | default: |
| 592 | return concise ? context.getString(R.string.wifi_security_short_psk_generic) |
| 593 | : context.getString(R.string.wifi_security_psk_generic); |
| 594 | } |
| 595 | case SECURITY_WEP: |
| 596 | return concise ? context.getString(R.string.wifi_security_short_wep) : |
| 597 | context.getString(R.string.wifi_security_wep); |
| 598 | case SECURITY_NONE: |
| 599 | default: |
| 600 | return concise ? "" : context.getString(R.string.wifi_security_none); |
| 601 | } |
| 602 | } |
| 603 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 604 | public String getSsidStr() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 605 | return ssid; |
| 606 | } |
| 607 | |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 608 | public String getBssid() { |
| 609 | return bssid; |
| 610 | } |
| 611 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 612 | public CharSequence getSsid() { |
Fan Zhang | eb83a0d | 2016-09-21 11:34:08 -0700 | [diff] [blame] | 613 | final SpannableString str = new SpannableString(ssid); |
| 614 | str.setSpan(new TtsSpan.TelephoneBuilder(ssid).build(), 0, ssid.length(), |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 615 | Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 616 | return str; |
| 617 | } |
| 618 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 619 | public String getConfigName() { |
| 620 | if (mConfig != null && mConfig.isPasspoint()) { |
| 621 | return mConfig.providerFriendlyName; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 622 | } else if (mFqdn != null) { |
| 623 | return mProviderFriendlyName; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 624 | } else { |
| 625 | return ssid; |
| 626 | } |
| 627 | } |
| 628 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 629 | public DetailedState getDetailedState() { |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 630 | if (mNetworkInfo != null) { |
| 631 | return mNetworkInfo.getDetailedState(); |
| 632 | } |
| 633 | Log.w(TAG, "NetworkInfo is null, cannot return detailed state"); |
| 634 | return null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 635 | } |
| 636 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 637 | public String getSavedNetworkSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 638 | WifiConfiguration config = mConfig; |
| 639 | if (config != null) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 640 | PackageManager pm = mContext.getPackageManager(); |
| 641 | String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 642 | int userId = UserHandle.getUserId(config.creatorUid); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 643 | ApplicationInfo appInfo = null; |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 644 | if (config.creatorName != null && config.creatorName.equals(systemName)) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 645 | appInfo = mContext.getApplicationInfo(); |
| 646 | } else { |
| 647 | try { |
| 648 | IPackageManager ipm = AppGlobals.getPackageManager(); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 649 | appInfo = ipm.getApplicationInfo(config.creatorName, 0 /* flags */, userId); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 650 | } catch (RemoteException rex) { |
| 651 | } |
| 652 | } |
| 653 | if (appInfo != null && |
| 654 | !appInfo.packageName.equals(mContext.getString(R.string.settings_package)) && |
| 655 | !appInfo.packageName.equals( |
| 656 | mContext.getString(R.string.certinstaller_package))) { |
| 657 | return mContext.getString(R.string.saved_network, appInfo.loadLabel(pm)); |
| 658 | } |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 659 | } |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 660 | return ""; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 663 | public String getSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 664 | return getSettingsSummary(mConfig); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | public String getSettingsSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 668 | return getSettingsSummary(mConfig); |
| 669 | } |
| 670 | |
| 671 | private String getSettingsSummary(WifiConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 672 | // Update to new summary |
| 673 | StringBuilder summary = new StringBuilder(); |
| 674 | |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 675 | // TODO(b/62354743): Standardize and international delimiter usage |
| 676 | final String concatenator = " / "; |
| 677 | |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 678 | if (mSpeed != SPEED_NONE) { |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 679 | summary.append(getSpeedLabel() + concatenator); |
| 680 | } |
| 681 | |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 682 | if (isActive() && config != null && config.isPasspoint()) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 683 | // This is the active connection on passpoint |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 684 | summary.append(getSummary(mContext, getDetailedState(), |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 685 | false, config.providerFriendlyName)); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 686 | } else if (isActive()) { |
| 687 | // This is the active connection on non-passpoint network |
| 688 | summary.append(getSummary(mContext, getDetailedState(), |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 689 | mInfo != null && mInfo.isEphemeral())); |
Yuxin Chang | 6b75086 | 2017-01-11 14:59:17 +0900 | [diff] [blame] | 690 | } else if (config != null && config.isPasspoint() |
| 691 | && config.getNetworkSelectionStatus().isNetworkEnabled()) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 692 | String format = mContext.getString(R.string.available_via_passpoint); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 693 | summary.append(String.format(format, config.providerFriendlyName)); |
| 694 | } else if (config != null && config.hasNoInternetAccess()) { |
Salvador Martinez | 5d937fc | 2016-09-23 08:54:20 -0700 | [diff] [blame] | 695 | int messageID = config.getNetworkSelectionStatus().isNetworkPermanentlyDisabled() |
| 696 | ? R.string.wifi_no_internet_no_reconnect |
| 697 | : R.string.wifi_no_internet; |
| 698 | summary.append(mContext.getString(messageID)); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 699 | } else if (config != null && !config.getNetworkSelectionStatus().isNetworkEnabled()) { |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 700 | WifiConfiguration.NetworkSelectionStatus networkStatus = |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 701 | config.getNetworkSelectionStatus(); |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 702 | switch (networkStatus.getNetworkSelectionDisableReason()) { |
| 703 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE: |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 704 | summary.append(mContext.getString(R.string.wifi_disabled_password_failure)); |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 705 | break; |
Peter Qiu | 9c4c6ad | 2017-06-20 13:17:36 -0700 | [diff] [blame^] | 706 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD: |
| 707 | summary.append(mContext.getString(R.string.wifi_check_password_try_again)); |
| 708 | break; |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 709 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE: |
| 710 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_DNS_FAILURE: |
| 711 | summary.append(mContext.getString(R.string.wifi_disabled_network_failure)); |
| 712 | break; |
| 713 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION: |
| 714 | summary.append(mContext.getString(R.string.wifi_disabled_generic)); |
| 715 | break; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 716 | } |
Amin Shaikh | 98773d4 | 2017-02-02 17:50:12 -0800 | [diff] [blame] | 717 | } else if (config != null && config.getNetworkSelectionStatus().isNotRecommended()) { |
| 718 | summary.append(mContext.getString(R.string.wifi_disabled_by_recommendation_provider)); |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 719 | } else if (!isReachable()) { // Wifi out of range |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 720 | summary.append(mContext.getString(R.string.wifi_not_in_range)); |
| 721 | } else { // In range, not disabled. |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 722 | if (config != null) { // Is saved network |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 723 | summary.append(mContext.getString(R.string.wifi_remembered)); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if (WifiTracker.sVerboseLogging > 0) { |
| 728 | // Add RSSI/band information for this config, what was seen up to 6 seconds ago |
| 729 | // verbose WiFi Logging is only turned on thru developers settings |
| 730 | if (mInfo != null && mNetworkInfo != null) { // This is the active connection |
| 731 | summary.append(" f=" + Integer.toString(mInfo.getFrequency())); |
| 732 | } |
| 733 | summary.append(" " + getVisibilityStatus()); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 734 | if (config != null && !config.getNetworkSelectionStatus().isNetworkEnabled()) { |
| 735 | summary.append(" (" + config.getNetworkSelectionStatus().getNetworkStatusString()); |
| 736 | if (config.getNetworkSelectionStatus().getDisableTime() > 0) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 737 | long now = System.currentTimeMillis(); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 738 | long diff = (now - config.getNetworkSelectionStatus().getDisableTime()) / 1000; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 739 | long sec = diff%60; //seconds |
| 740 | long min = (diff/60)%60; //minutes |
| 741 | long hour = (min/60)%60; //hours |
| 742 | summary.append(", "); |
| 743 | if (hour > 0) summary.append(Long.toString(hour) + "h "); |
| 744 | summary.append( Long.toString(min) + "m "); |
| 745 | summary.append( Long.toString(sec) + "s "); |
| 746 | } |
| 747 | summary.append(")"); |
| 748 | } |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 749 | |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 750 | if (config != null) { |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 751 | WifiConfiguration.NetworkSelectionStatus networkStatus = |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 752 | config.getNetworkSelectionStatus(); |
xinhe | f7705c3 | 2015-12-01 14:44:37 -0800 | [diff] [blame] | 753 | for (int index = WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLE; |
| 754 | index < WifiConfiguration.NetworkSelectionStatus |
| 755 | .NETWORK_SELECTION_DISABLED_MAX; index++) { |
| 756 | if (networkStatus.getDisableReasonCounter(index) != 0) { |
| 757 | summary.append(" " + WifiConfiguration.NetworkSelectionStatus |
| 758 | .getNetworkDisableReasonString(index) + "=" |
| 759 | + networkStatus.getDisableReasonCounter(index)); |
| 760 | } |
| 761 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 762 | } |
| 763 | } |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 764 | |
| 765 | // Strip trailing delimiter if applicable |
| 766 | int concatLength = concatenator.length(); |
| 767 | if (summary.length() >= concatLength && summary.substring( |
| 768 | summary.length() - concatLength, summary.length()).equals(concatenator)) { |
| 769 | summary.delete(summary.length() - concatLength, summary.length()); |
| 770 | } |
| 771 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 772 | return summary.toString(); |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * Returns the visibility status of the WifiConfiguration. |
| 777 | * |
| 778 | * @return autojoin debugging information |
| 779 | * TODO: use a string formatter |
| 780 | * ["rssi 5Ghz", "num results on 5GHz" / "rssi 5Ghz", "num results on 5GHz"] |
| 781 | * For instance [-40,5/-30,2] |
| 782 | */ |
| 783 | private String getVisibilityStatus() { |
| 784 | StringBuilder visibility = new StringBuilder(); |
| 785 | StringBuilder scans24GHz = null; |
| 786 | StringBuilder scans5GHz = null; |
| 787 | String bssid = null; |
| 788 | |
| 789 | long now = System.currentTimeMillis(); |
| 790 | |
| 791 | if (mInfo != null) { |
| 792 | bssid = mInfo.getBSSID(); |
| 793 | if (bssid != null) { |
| 794 | visibility.append(" ").append(bssid); |
| 795 | } |
| 796 | visibility.append(" rssi=").append(mInfo.getRssi()); |
| 797 | visibility.append(" "); |
| 798 | visibility.append(" score=").append(mInfo.score); |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 799 | if (mRankingScore != Integer.MIN_VALUE) { |
| 800 | visibility.append(" rankingScore=").append(getRankingScore()); |
| 801 | } |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 802 | if (mSpeed != SPEED_NONE) { |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 803 | visibility.append(" speed=").append(getSpeedLabel()); |
| 804 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 805 | visibility.append(String.format(" tx=%.1f,", mInfo.txSuccessRate)); |
| 806 | visibility.append(String.format("%.1f,", mInfo.txRetriesRate)); |
| 807 | visibility.append(String.format("%.1f ", mInfo.txBadRate)); |
| 808 | visibility.append(String.format("rx=%.1f", mInfo.rxSuccessRate)); |
| 809 | } |
| 810 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 811 | int rssi5 = WifiConfiguration.INVALID_RSSI; |
| 812 | int rssi24 = WifiConfiguration.INVALID_RSSI; |
| 813 | int num5 = 0; |
| 814 | int num24 = 0; |
| 815 | int numBlackListed = 0; |
| 816 | int n24 = 0; // Number scan results we included in the string |
| 817 | int n5 = 0; // Number scan results we included in the string |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 818 | evictOldScanResults(); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 819 | // TODO: sort list by RSSI or age |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 820 | for (ScanResult result : mScanResultCache.values()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 821 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 822 | if (result.frequency >= LOWER_FREQ_5GHZ |
| 823 | && result.frequency <= HIGHER_FREQ_5GHZ) { |
| 824 | // Strictly speaking: [4915, 5825] |
| 825 | // number of known BSSID on 5GHz band |
| 826 | num5 = num5 + 1; |
| 827 | } else if (result.frequency >= LOWER_FREQ_24GHZ |
| 828 | && result.frequency <= HIGHER_FREQ_24GHZ) { |
| 829 | // Strictly speaking: [2412, 2482] |
| 830 | // number of known BSSID on 2.4Ghz band |
| 831 | num24 = num24 + 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 832 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 833 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 834 | |
| 835 | if (result.frequency >= LOWER_FREQ_5GHZ |
| 836 | && result.frequency <= HIGHER_FREQ_5GHZ) { |
| 837 | if (result.level > rssi5) { |
| 838 | rssi5 = result.level; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 839 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 840 | if (n5 < 4) { |
| 841 | if (scans5GHz == null) scans5GHz = new StringBuilder(); |
| 842 | scans5GHz.append(" \n{").append(result.BSSID); |
| 843 | if (bssid != null && result.BSSID.equals(bssid)) scans5GHz.append("*"); |
| 844 | scans5GHz.append("=").append(result.frequency); |
| 845 | scans5GHz.append(",").append(result.level); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 846 | scans5GHz.append("}"); |
| 847 | n5++; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 848 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 849 | } else if (result.frequency >= LOWER_FREQ_24GHZ |
| 850 | && result.frequency <= HIGHER_FREQ_24GHZ) { |
| 851 | if (result.level > rssi24) { |
| 852 | rssi24 = result.level; |
| 853 | } |
| 854 | if (n24 < 4) { |
| 855 | if (scans24GHz == null) scans24GHz = new StringBuilder(); |
| 856 | scans24GHz.append(" \n{").append(result.BSSID); |
| 857 | if (bssid != null && result.BSSID.equals(bssid)) scans24GHz.append("*"); |
| 858 | scans24GHz.append("=").append(result.frequency); |
| 859 | scans24GHz.append(",").append(result.level); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 860 | scans24GHz.append("}"); |
| 861 | n24++; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 865 | visibility.append(" ["); |
| 866 | if (num24 > 0) { |
| 867 | visibility.append("(").append(num24).append(")"); |
| 868 | if (n24 <= 4) { |
| 869 | if (scans24GHz != null) { |
| 870 | visibility.append(scans24GHz.toString()); |
| 871 | } |
| 872 | } else { |
| 873 | visibility.append("max=").append(rssi24); |
| 874 | if (scans24GHz != null) { |
| 875 | visibility.append(",").append(scans24GHz.toString()); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | visibility.append(";"); |
| 880 | if (num5 > 0) { |
| 881 | visibility.append("(").append(num5).append(")"); |
| 882 | if (n5 <= 4) { |
| 883 | if (scans5GHz != null) { |
| 884 | visibility.append(scans5GHz.toString()); |
| 885 | } |
| 886 | } else { |
| 887 | visibility.append("max=").append(rssi5); |
| 888 | if (scans5GHz != null) { |
| 889 | visibility.append(",").append(scans5GHz.toString()); |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | if (numBlackListed > 0) |
| 894 | visibility.append("!").append(numBlackListed); |
| 895 | visibility.append("]"); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 896 | |
| 897 | return visibility.toString(); |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * Return whether this is the active connection. |
| 902 | * For ephemeral connections (networkId is invalid), this returns false if the network is |
| 903 | * disconnected. |
| 904 | */ |
| 905 | public boolean isActive() { |
| 906 | return mNetworkInfo != null && |
| 907 | (networkId != WifiConfiguration.INVALID_NETWORK_ID || |
| 908 | mNetworkInfo.getState() != State.DISCONNECTED); |
| 909 | } |
| 910 | |
| 911 | public boolean isConnectable() { |
| 912 | return getLevel() != -1 && getDetailedState() == null; |
| 913 | } |
| 914 | |
| 915 | public boolean isEphemeral() { |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 916 | return mInfo != null && mInfo.isEphemeral() && |
| 917 | mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 918 | } |
| 919 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 920 | /** |
| 921 | * Return true if this AccessPoint represents a Passpoint AP. |
| 922 | */ |
Vinit Deshpande | 5b7352c | 2015-07-09 16:53:12 -0700 | [diff] [blame] | 923 | public boolean isPasspoint() { |
| 924 | return mConfig != null && mConfig.isPasspoint(); |
| 925 | } |
| 926 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 927 | /** |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 928 | * Return true if this AccessPoint represents a Passpoint provider configuration. |
| 929 | */ |
| 930 | public boolean isPasspointConfig() { |
| 931 | return mFqdn != null; |
| 932 | } |
| 933 | |
| 934 | /** |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 935 | * Return whether the given {@link WifiInfo} is for this access point. |
| 936 | * If the current AP does not have a network Id then the config is used to |
| 937 | * match based on SSID and security. |
| 938 | */ |
| 939 | private boolean isInfoForThisAccessPoint(WifiConfiguration config, WifiInfo info) { |
Vinit Deshpande | 5b7352c | 2015-07-09 16:53:12 -0700 | [diff] [blame] | 940 | if (isPasspoint() == false && networkId != WifiConfiguration.INVALID_NETWORK_ID) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 941 | return networkId == info.getNetworkId(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 942 | } else if (config != null) { |
| 943 | return matches(config); |
| 944 | } |
| 945 | else { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 946 | // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID. |
| 947 | // (Note that we only do this if the WifiConfiguration explicitly equals INVALID). |
| 948 | // TODO: Handle hex string SSIDs. |
| 949 | return ssid.equals(removeDoubleQuotes(info.getSSID())); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | public boolean isSaved() { |
| 954 | return networkId != WifiConfiguration.INVALID_NETWORK_ID; |
| 955 | } |
| 956 | |
| 957 | public Object getTag() { |
| 958 | return mTag; |
| 959 | } |
| 960 | |
| 961 | public void setTag(Object tag) { |
| 962 | mTag = tag; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Generate and save a default wifiConfiguration with common values. |
| 967 | * Can only be called for unsecured networks. |
| 968 | */ |
| 969 | public void generateOpenNetworkConfig() { |
| 970 | if (security != SECURITY_NONE) |
| 971 | throw new IllegalStateException(); |
| 972 | if (mConfig != null) |
| 973 | return; |
| 974 | mConfig = new WifiConfiguration(); |
| 975 | mConfig.SSID = AccessPoint.convertToQuotedString(ssid); |
| 976 | mConfig.allowedKeyManagement.set(KeyMgmt.NONE); |
| 977 | } |
| 978 | |
| 979 | void loadConfig(WifiConfiguration config) { |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 980 | ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 981 | bssid = config.BSSID; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 982 | security = getSecurity(config); |
| 983 | networkId = config.networkId; |
| 984 | mConfig = config; |
| 985 | } |
| 986 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 987 | private void initWithScanResult(ScanResult result) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 988 | ssid = result.SSID; |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 989 | bssid = result.BSSID; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 990 | security = getSecurity(result); |
| 991 | if (security == SECURITY_PSK) |
| 992 | pskType = getPskType(result); |
Sundeep Ghuman | 2412817 | 2017-06-15 17:55:21 -0700 | [diff] [blame] | 993 | |
| 994 | mScanResultCache.put(result.BSSID, result); |
| 995 | updateRssi(); |
| 996 | mSeen = result.timestamp; // even if the timestamp is old it is still valid |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | public void saveWifiState(Bundle savedState) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1000 | if (ssid != null) savedState.putString(KEY_SSID, getSsidStr()); |
| 1001 | savedState.putInt(KEY_SECURITY, security); |
| 1002 | savedState.putInt(KEY_PSKTYPE, pskType); |
| 1003 | if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1004 | savedState.putParcelable(KEY_WIFIINFO, mInfo); |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 1005 | evictOldScanResults(); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1006 | savedState.putParcelableArrayList(KEY_SCANRESULTCACHE, |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 1007 | new ArrayList<ScanResult>(mScanResultCache.values())); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1008 | if (mNetworkInfo != null) { |
| 1009 | savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo); |
| 1010 | } |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1011 | if (mFqdn != null) { |
| 1012 | savedState.putString(KEY_FQDN, mFqdn); |
| 1013 | } |
| 1014 | if (mProviderFriendlyName != null) { |
| 1015 | savedState.putString(KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName); |
| 1016 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | public void setListener(AccessPointListener listener) { |
| 1020 | mAccessPointListener = listener; |
| 1021 | } |
| 1022 | |
| 1023 | boolean update(ScanResult result) { |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1024 | if (matches(result)) { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1025 | int oldLevel = getLevel(); |
| 1026 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1027 | /* Add or update the scan result for the BSSID */ |
| 1028 | mScanResultCache.put(result.BSSID, result); |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1029 | updateSeen(); |
| 1030 | updateRssi(); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1031 | int newLevel = getLevel(); |
| 1032 | |
| 1033 | if (newLevel > 0 && newLevel != oldLevel && mAccessPointListener != null) { |
| 1034 | mAccessPointListener.onLevelChanged(this); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1035 | } |
| 1036 | // This flag only comes from scans, is not easily saved in config |
| 1037 | if (security == SECURITY_PSK) { |
| 1038 | pskType = getPskType(result); |
| 1039 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1040 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1041 | if (mAccessPointListener != null) { |
| 1042 | mAccessPointListener.onAccessPointChanged(this); |
| 1043 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1044 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1045 | return true; |
| 1046 | } |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1050 | /** Attempt to update the AccessPoint and return true if an update occurred. */ |
Sundeep Ghuman | dc6cf4b | 2017-03-08 16:18:29 -0800 | [diff] [blame] | 1051 | public boolean update(WifiConfiguration config, WifiInfo info, NetworkInfo networkInfo) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1052 | boolean updated = false; |
| 1053 | final int oldLevel = getLevel(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1054 | if (info != null && isInfoForThisAccessPoint(config, info)) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1055 | updated = (mInfo == null); |
| 1056 | if (mRssi != info.getRssi()) { |
| 1057 | mRssi = info.getRssi(); |
| 1058 | updated = true; |
Sundeep Ghuman | 5c5cd7a | 2017-05-03 12:45:44 -0700 | [diff] [blame] | 1059 | } else if (mNetworkInfo != null && networkInfo != null |
| 1060 | && mNetworkInfo.getDetailedState() != networkInfo.getDetailedState()) { |
Sundeep Ghuman | 96a5357 | 2017-04-20 21:25:41 -0700 | [diff] [blame] | 1061 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1062 | } |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1063 | mInfo = info; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1064 | mNetworkInfo = networkInfo; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1065 | } else if (mInfo != null) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1066 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1067 | mInfo = null; |
| 1068 | mNetworkInfo = null; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1069 | } |
| 1070 | if (updated && mAccessPointListener != null) { |
| 1071 | mAccessPointListener.onAccessPointChanged(this); |
| 1072 | |
| 1073 | if (oldLevel != getLevel() /* current level */) { |
| 1074 | mAccessPointListener.onLevelChanged(this); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1075 | } |
| 1076 | } |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1077 | return updated; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1078 | } |
| 1079 | |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1080 | void update(WifiConfiguration config) { |
| 1081 | mConfig = config; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1082 | networkId = config.networkId; |
| 1083 | if (mAccessPointListener != null) { |
| 1084 | mAccessPointListener.onAccessPointChanged(this); |
| 1085 | } |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1086 | } |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 1087 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1088 | @VisibleForTesting |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 1089 | void setRssi(int rssi) { |
| 1090 | mRssi = rssi; |
| 1091 | } |
| 1092 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1093 | /** Sets the rssi to {@link #UNREACHABLE_RSSI}. */ |
| 1094 | void setUnreachable() { |
| 1095 | setRssi(AccessPoint.UNREACHABLE_RSSI); |
| 1096 | } |
| 1097 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 1098 | int getRankingScore() { |
| 1099 | return mRankingScore; |
| 1100 | } |
| 1101 | |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1102 | int getSpeed() { return mSpeed;} |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1103 | |
| 1104 | @Nullable |
| 1105 | String getSpeedLabel() { |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1106 | switch (mSpeed) { |
| 1107 | case SPEED_VERY_FAST: |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1108 | return mContext.getString(R.string.speed_label_very_fast); |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1109 | case SPEED_FAST: |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1110 | return mContext.getString(R.string.speed_label_fast); |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1111 | case SPEED_MEDIUM: |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1112 | return mContext.getString(R.string.speed_label_okay); |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1113 | case SPEED_SLOW: |
| 1114 | return mContext.getString(R.string.speed_label_slow); |
| 1115 | case SPEED_NONE: |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1116 | default: |
| 1117 | return null; |
| 1118 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 1119 | } |
| 1120 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1121 | /** Return true if the current RSSI is reachable, and false otherwise. */ |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 1122 | public boolean isReachable() { |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1123 | return mRssi != UNREACHABLE_RSSI; |
| 1124 | } |
| 1125 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1126 | public static String getSummary(Context context, String ssid, DetailedState state, |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1127 | boolean isEphemeral, String passpointProvider) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1128 | if (state == DetailedState.CONNECTED && ssid == null) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1129 | if (TextUtils.isEmpty(passpointProvider) == false) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1130 | // Special case for connected + passpoint networks. |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1131 | String format = context.getString(R.string.connected_via_passpoint); |
| 1132 | return String.format(format, passpointProvider); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1133 | } else if (isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1134 | // Special case for connected + ephemeral networks. |
Stephen Chen | 36dd5cf1 | 2017-03-20 13:27:51 -0700 | [diff] [blame] | 1135 | final NetworkScoreManager networkScoreManager = context.getSystemService( |
| 1136 | NetworkScoreManager.class); |
| 1137 | NetworkScorerAppData scorer = networkScoreManager.getActiveScorer(); |
| 1138 | if (scorer != null && scorer.getRecommendationServiceLabel() != null) { |
| 1139 | String format = context.getString(R.string.connected_via_network_scorer); |
| 1140 | return String.format(format, scorer.getRecommendationServiceLabel()); |
| 1141 | } else { |
| 1142 | return context.getString(R.string.connected_via_network_scorer_default); |
| 1143 | } |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1144 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1145 | } |
| 1146 | |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1147 | // Case when there is wifi connected without internet connectivity. |
| 1148 | final ConnectivityManager cm = (ConnectivityManager) |
| 1149 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 1150 | if (state == DetailedState.CONNECTED) { |
| 1151 | IWifiManager wifiManager = IWifiManager.Stub.asInterface( |
| 1152 | ServiceManager.getService(Context.WIFI_SERVICE)); |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1153 | NetworkCapabilities nc = null; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1154 | |
| 1155 | try { |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1156 | nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork()); |
| 1157 | } catch (RemoteException e) {} |
| 1158 | |
| 1159 | if (nc != null) { |
| 1160 | if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) { |
| 1161 | return context.getString( |
| 1162 | com.android.internal.R.string.network_available_sign_in); |
| 1163 | } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) { |
| 1164 | return context.getString(R.string.wifi_connected_no_internet); |
| 1165 | } |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1166 | } |
| 1167 | } |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 1168 | if (state == null) { |
| 1169 | Log.w(TAG, "state is null, returning empty summary"); |
| 1170 | return ""; |
| 1171 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1172 | String[] formats = context.getResources().getStringArray((ssid == null) |
| 1173 | ? R.array.wifi_status : R.array.wifi_status_with_ssid); |
| 1174 | int index = state.ordinal(); |
| 1175 | |
| 1176 | if (index >= formats.length || formats[index].length() == 0) { |
Sanket Padawe | 3e9e5fa | 2015-05-28 10:41:14 -0700 | [diff] [blame] | 1177 | return ""; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1178 | } |
| 1179 | return String.format(formats[index], ssid); |
| 1180 | } |
| 1181 | |
| 1182 | public static String getSummary(Context context, DetailedState state, boolean isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1183 | return getSummary(context, null, state, isEphemeral, null); |
| 1184 | } |
| 1185 | |
| 1186 | public static String getSummary(Context context, DetailedState state, boolean isEphemeral, |
| 1187 | String passpointProvider) { |
| 1188 | return getSummary(context, null, state, isEphemeral, passpointProvider); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | public static String convertToQuotedString(String string) { |
| 1192 | return "\"" + string + "\""; |
| 1193 | } |
| 1194 | |
| 1195 | private static int getPskType(ScanResult result) { |
| 1196 | boolean wpa = result.capabilities.contains("WPA-PSK"); |
| 1197 | boolean wpa2 = result.capabilities.contains("WPA2-PSK"); |
| 1198 | if (wpa2 && wpa) { |
| 1199 | return PSK_WPA_WPA2; |
| 1200 | } else if (wpa2) { |
| 1201 | return PSK_WPA2; |
| 1202 | } else if (wpa) { |
| 1203 | return PSK_WPA; |
| 1204 | } else { |
| 1205 | Log.w(TAG, "Received abnormal flag string: " + result.capabilities); |
| 1206 | return PSK_UNKNOWN; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | private static int getSecurity(ScanResult result) { |
| 1211 | if (result.capabilities.contains("WEP")) { |
| 1212 | return SECURITY_WEP; |
| 1213 | } else if (result.capabilities.contains("PSK")) { |
| 1214 | return SECURITY_PSK; |
| 1215 | } else if (result.capabilities.contains("EAP")) { |
| 1216 | return SECURITY_EAP; |
| 1217 | } |
| 1218 | return SECURITY_NONE; |
| 1219 | } |
| 1220 | |
| 1221 | static int getSecurity(WifiConfiguration config) { |
| 1222 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) { |
| 1223 | return SECURITY_PSK; |
| 1224 | } |
| 1225 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || |
| 1226 | config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) { |
| 1227 | return SECURITY_EAP; |
| 1228 | } |
| 1229 | return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE; |
| 1230 | } |
| 1231 | |
| 1232 | public static String securityToString(int security, int pskType) { |
| 1233 | if (security == SECURITY_WEP) { |
| 1234 | return "WEP"; |
| 1235 | } else if (security == SECURITY_PSK) { |
| 1236 | if (pskType == PSK_WPA) { |
| 1237 | return "WPA"; |
| 1238 | } else if (pskType == PSK_WPA2) { |
| 1239 | return "WPA2"; |
| 1240 | } else if (pskType == PSK_WPA_WPA2) { |
| 1241 | return "WPA_WPA2"; |
| 1242 | } |
| 1243 | return "PSK"; |
| 1244 | } else if (security == SECURITY_EAP) { |
| 1245 | return "EAP"; |
| 1246 | } |
| 1247 | return "NONE"; |
| 1248 | } |
| 1249 | |
| 1250 | static String removeDoubleQuotes(String string) { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 1251 | if (TextUtils.isEmpty(string)) { |
| 1252 | return ""; |
| 1253 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1254 | int length = string.length(); |
| 1255 | if ((length > 1) && (string.charAt(0) == '"') |
| 1256 | && (string.charAt(length - 1) == '"')) { |
| 1257 | return string.substring(1, length - 1); |
| 1258 | } |
| 1259 | return string; |
| 1260 | } |
| 1261 | |
| 1262 | public interface AccessPointListener { |
| 1263 | void onAccessPointChanged(AccessPoint accessPoint); |
| 1264 | void onLevelChanged(AccessPoint accessPoint); |
| 1265 | } |
| 1266 | } |