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 | |
| 19 | import android.content.Context; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame^] | 20 | import android.net.ConnectivityManager; |
| 21 | import android.net.Network; |
| 22 | import android.net.NetworkCapabilities; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 23 | import android.net.NetworkInfo; |
| 24 | import android.net.NetworkInfo.DetailedState; |
| 25 | import android.net.NetworkInfo.State; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame^] | 26 | import android.net.wifi.IWifiManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 27 | import android.net.wifi.ScanResult; |
| 28 | import android.net.wifi.WifiConfiguration; |
| 29 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
| 30 | import android.net.wifi.WifiInfo; |
| 31 | import android.net.wifi.WifiManager; |
| 32 | import android.os.Bundle; |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 33 | import android.text.TextUtils; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame^] | 34 | import android.os.RemoteException; |
| 35 | import android.os.ServiceManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.util.LruCache; |
| 38 | |
| 39 | import com.android.settingslib.R; |
| 40 | |
| 41 | import java.util.Map; |
| 42 | |
| 43 | |
| 44 | public class AccessPoint implements Comparable<AccessPoint> { |
| 45 | static final String TAG = "SettingsLib.AccessPoint"; |
| 46 | |
| 47 | /** |
| 48 | * Lower bound on the 2.4 GHz (802.11b/g/n) WLAN channels |
| 49 | */ |
| 50 | public static final int LOWER_FREQ_24GHZ = 2400; |
| 51 | |
| 52 | /** |
| 53 | * Upper bound on the 2.4 GHz (802.11b/g/n) WLAN channels |
| 54 | */ |
| 55 | public static final int HIGHER_FREQ_24GHZ = 2500; |
| 56 | |
| 57 | /** |
| 58 | * Lower bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels |
| 59 | */ |
| 60 | public static final int LOWER_FREQ_5GHZ = 4900; |
| 61 | |
| 62 | /** |
| 63 | * Upper bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels |
| 64 | */ |
| 65 | public static final int HIGHER_FREQ_5GHZ = 5900; |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Experimental: we should be able to show the user the list of BSSIDs and bands |
| 70 | * for that SSID. |
| 71 | * For now this data is used only with Verbose Logging so as to show the band and number |
| 72 | * of BSSIDs on which that network is seen. |
| 73 | */ |
| 74 | public LruCache<String, ScanResult> mScanResultCache; |
| 75 | private static final String KEY_NETWORKINFO = "key_networkinfo"; |
| 76 | private static final String KEY_WIFIINFO = "key_wifiinfo"; |
| 77 | private static final String KEY_SCANRESULT = "key_scanresult"; |
| 78 | private static final String KEY_CONFIG = "key_config"; |
| 79 | |
| 80 | /** |
| 81 | * These values are matched in string arrays -- changes must be kept in sync |
| 82 | */ |
| 83 | public static final int SECURITY_NONE = 0; |
| 84 | public static final int SECURITY_WEP = 1; |
| 85 | public static final int SECURITY_PSK = 2; |
| 86 | public static final int SECURITY_EAP = 3; |
| 87 | |
| 88 | private static final int PSK_UNKNOWN = 0; |
| 89 | private static final int PSK_WPA = 1; |
| 90 | private static final int PSK_WPA2 = 2; |
| 91 | private static final int PSK_WPA_WPA2 = 3; |
| 92 | |
| 93 | private static final int VISIBILITY_OUTDATED_AGE_IN_MILLI = 20000; |
| 94 | private final Context mContext; |
| 95 | |
| 96 | private String ssid; |
| 97 | private int security; |
| 98 | private int networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 99 | |
| 100 | private int pskType = PSK_UNKNOWN; |
| 101 | |
| 102 | private WifiConfiguration mConfig; |
| 103 | private ScanResult mScanResult; |
| 104 | |
| 105 | private int mRssi = Integer.MAX_VALUE; |
| 106 | private long mSeen = 0; |
| 107 | |
| 108 | private WifiInfo mInfo; |
| 109 | private NetworkInfo mNetworkInfo; |
| 110 | private AccessPointListener mAccessPointListener; |
| 111 | |
| 112 | private Object mTag; |
| 113 | |
| 114 | public AccessPoint(Context context, Bundle savedState) { |
| 115 | mContext = context; |
| 116 | mConfig = savedState.getParcelable(KEY_CONFIG); |
| 117 | if (mConfig != null) { |
| 118 | loadConfig(mConfig); |
| 119 | } |
| 120 | mScanResult = (ScanResult) savedState.getParcelable(KEY_SCANRESULT); |
| 121 | if (mScanResult != null) { |
| 122 | loadResult(mScanResult); |
| 123 | } |
| 124 | mInfo = (WifiInfo) savedState.getParcelable(KEY_WIFIINFO); |
| 125 | if (savedState.containsKey(KEY_NETWORKINFO)) { |
| 126 | mNetworkInfo = savedState.getParcelable(KEY_NETWORKINFO); |
| 127 | } |
| 128 | update(mInfo, mNetworkInfo); |
| 129 | } |
| 130 | |
| 131 | AccessPoint(Context context, ScanResult result) { |
| 132 | mContext = context; |
| 133 | loadResult(result); |
| 134 | } |
| 135 | |
| 136 | AccessPoint(Context context, WifiConfiguration config) { |
| 137 | mContext = context; |
| 138 | loadConfig(config); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int compareTo(AccessPoint other) { |
| 143 | // Active one goes first. |
| 144 | if (isActive() && !other.isActive()) return -1; |
| 145 | if (!isActive() && other.isActive()) return 1; |
| 146 | |
| 147 | // Reachable one goes before unreachable one. |
| 148 | if (mRssi != Integer.MAX_VALUE && other.mRssi == Integer.MAX_VALUE) return -1; |
| 149 | if (mRssi == Integer.MAX_VALUE && other.mRssi != Integer.MAX_VALUE) return 1; |
| 150 | |
| 151 | // Configured one goes before unconfigured one. |
| 152 | if (networkId != WifiConfiguration.INVALID_NETWORK_ID |
| 153 | && other.networkId == WifiConfiguration.INVALID_NETWORK_ID) return -1; |
| 154 | if (networkId == WifiConfiguration.INVALID_NETWORK_ID |
| 155 | && other.networkId != WifiConfiguration.INVALID_NETWORK_ID) return 1; |
| 156 | |
| 157 | // Sort by signal strength. |
| 158 | int difference = WifiManager.compareSignalLevel(other.mRssi, mRssi); |
| 159 | if (difference != 0) { |
| 160 | return difference; |
| 161 | } |
| 162 | // Sort by ssid. |
| 163 | return ssid.compareToIgnoreCase(other.ssid); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public boolean equals(Object other) { |
| 168 | if (!(other instanceof AccessPoint)) return false; |
| 169 | return (this.compareTo((AccessPoint) other) == 0); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public int hashCode() { |
| 174 | int result = 0; |
| 175 | if (mInfo != null) result += 13 * mInfo.hashCode(); |
| 176 | result += 19 * mRssi; |
| 177 | result += 23 * networkId; |
| 178 | result += 29 * ssid.hashCode(); |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public String toString() { |
| 184 | StringBuilder builder = new StringBuilder().append("AccessPoint(") |
| 185 | .append(ssid); |
| 186 | if (isSaved()) { |
| 187 | builder.append(',').append("saved"); |
| 188 | } |
| 189 | if (isActive()) { |
| 190 | builder.append(',').append("active"); |
| 191 | } |
| 192 | if (isEphemeral()) { |
| 193 | builder.append(',').append("ephemeral"); |
| 194 | } |
| 195 | if (isConnectable()) { |
| 196 | builder.append(',').append("connectable"); |
| 197 | } |
| 198 | if (security != SECURITY_NONE) { |
| 199 | builder.append(',').append(securityToString(security, pskType)); |
| 200 | } |
| 201 | return builder.append(')').toString(); |
| 202 | } |
| 203 | |
| 204 | public boolean matches(ScanResult result) { |
| 205 | return ssid.equals(result.SSID) && security == getSecurity(result); |
| 206 | } |
| 207 | |
| 208 | public boolean matches(WifiConfiguration config) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 209 | if (config.isPasspoint() && mConfig != null && mConfig.isPasspoint()) |
| 210 | return config.FQDN.equals(mConfig.providerFriendlyName); |
| 211 | else |
| 212 | return ssid.equals(removeDoubleQuotes(config.SSID)) && security == getSecurity(config); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | public WifiConfiguration getConfig() { |
| 216 | return mConfig; |
| 217 | } |
| 218 | |
| 219 | public void clearConfig() { |
| 220 | mConfig = null; |
| 221 | networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 222 | } |
| 223 | |
| 224 | public WifiInfo getInfo() { |
| 225 | return mInfo; |
| 226 | } |
| 227 | |
| 228 | public int getLevel() { |
| 229 | if (mRssi == Integer.MAX_VALUE) { |
| 230 | return -1; |
| 231 | } |
| 232 | return WifiManager.calculateSignalLevel(mRssi, 4); |
| 233 | } |
| 234 | |
| 235 | public NetworkInfo getNetworkInfo() { |
| 236 | return mNetworkInfo; |
| 237 | } |
| 238 | |
| 239 | public int getSecurity() { |
| 240 | return security; |
| 241 | } |
| 242 | |
| 243 | public String getSecurityString(boolean concise) { |
| 244 | Context context = mContext; |
| 245 | switch(security) { |
| 246 | case SECURITY_EAP: |
| 247 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 248 | context.getString(R.string.wifi_security_eap); |
| 249 | case SECURITY_PSK: |
| 250 | switch (pskType) { |
| 251 | case PSK_WPA: |
| 252 | return concise ? context.getString(R.string.wifi_security_short_wpa) : |
| 253 | context.getString(R.string.wifi_security_wpa); |
| 254 | case PSK_WPA2: |
| 255 | return concise ? context.getString(R.string.wifi_security_short_wpa2) : |
| 256 | context.getString(R.string.wifi_security_wpa2); |
| 257 | case PSK_WPA_WPA2: |
| 258 | return concise ? context.getString(R.string.wifi_security_short_wpa_wpa2) : |
| 259 | context.getString(R.string.wifi_security_wpa_wpa2); |
| 260 | case PSK_UNKNOWN: |
| 261 | default: |
| 262 | return concise ? context.getString(R.string.wifi_security_short_psk_generic) |
| 263 | : context.getString(R.string.wifi_security_psk_generic); |
| 264 | } |
| 265 | case SECURITY_WEP: |
| 266 | return concise ? context.getString(R.string.wifi_security_short_wep) : |
| 267 | context.getString(R.string.wifi_security_wep); |
| 268 | case SECURITY_NONE: |
| 269 | default: |
| 270 | return concise ? "" : context.getString(R.string.wifi_security_none); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | public String getSsid() { |
| 275 | return ssid; |
| 276 | } |
| 277 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 278 | public String getConfigName() { |
| 279 | if (mConfig != null && mConfig.isPasspoint()) { |
| 280 | return mConfig.providerFriendlyName; |
| 281 | } else { |
| 282 | return ssid; |
| 283 | } |
| 284 | } |
| 285 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 286 | public DetailedState getDetailedState() { |
| 287 | return mNetworkInfo != null ? mNetworkInfo.getDetailedState() : null; |
| 288 | } |
| 289 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 290 | public String getSavedNetworkSummary() { |
| 291 | // Update to new summary |
| 292 | if (mConfig != null && mConfig.isPasspoint()) { |
| 293 | return ""; |
| 294 | } else { |
| 295 | return getSettingsSummary(); |
| 296 | } |
| 297 | } |
| 298 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 299 | public String getSummary() { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 300 | return getSettingsSummary(); |
| 301 | } |
| 302 | |
| 303 | public String getSettingsSummary() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 304 | // Update to new summary |
| 305 | StringBuilder summary = new StringBuilder(); |
| 306 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 307 | if (isActive() && mConfig != null && mConfig.isPasspoint()) { |
| 308 | // This is the active connection on passpoint |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 309 | summary.append(getSummary(mContext, getDetailedState(), |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 310 | false, mConfig.providerFriendlyName)); |
| 311 | } else if (isActive()) { |
| 312 | // This is the active connection on non-passpoint network |
| 313 | summary.append(getSummary(mContext, getDetailedState(), |
| 314 | networkId == WifiConfiguration.INVALID_NETWORK_ID)); |
| 315 | } else if (mConfig != null && mConfig.isPasspoint()) { |
| 316 | String format = mContext.getString(R.string.available_via_passpoint); |
| 317 | summary.append(String.format(format, mConfig.providerFriendlyName)); |
| 318 | } else if (mConfig != null && mConfig.hasNoInternetAccess()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 319 | summary.append(mContext.getString(R.string.wifi_no_internet)); |
| 320 | } else if (mConfig != null && ((mConfig.status == WifiConfiguration.Status.DISABLED && |
| 321 | mConfig.disableReason != WifiConfiguration.DISABLED_UNKNOWN_REASON) |
| 322 | || mConfig.autoJoinStatus |
| 323 | >= WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE)) { |
| 324 | if (mConfig.autoJoinStatus |
| 325 | >= WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) { |
| 326 | if (mConfig.disableReason == WifiConfiguration.DISABLED_DHCP_FAILURE) { |
| 327 | summary.append(mContext.getString(R.string.wifi_disabled_network_failure)); |
| 328 | } else if (mConfig.disableReason == WifiConfiguration.DISABLED_AUTH_FAILURE) { |
| 329 | summary.append(mContext.getString(R.string.wifi_disabled_password_failure)); |
| 330 | } else { |
| 331 | summary.append(mContext.getString(R.string.wifi_disabled_wifi_failure)); |
| 332 | } |
| 333 | } else { |
| 334 | switch (mConfig.disableReason) { |
| 335 | case WifiConfiguration.DISABLED_AUTH_FAILURE: |
| 336 | summary.append(mContext.getString(R.string.wifi_disabled_password_failure)); |
| 337 | break; |
| 338 | case WifiConfiguration.DISABLED_DHCP_FAILURE: |
| 339 | case WifiConfiguration.DISABLED_DNS_FAILURE: |
| 340 | summary.append(mContext.getString(R.string.wifi_disabled_network_failure)); |
| 341 | break; |
| 342 | case WifiConfiguration.DISABLED_UNKNOWN_REASON: |
| 343 | case WifiConfiguration.DISABLED_ASSOCIATION_REJECT: |
| 344 | summary.append(mContext.getString(R.string.wifi_disabled_generic)); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | } else if (mRssi == Integer.MAX_VALUE) { // Wifi out of range |
| 349 | summary.append(mContext.getString(R.string.wifi_not_in_range)); |
| 350 | } else { // In range, not disabled. |
| 351 | if (mConfig != null) { // Is saved network |
| 352 | summary.append(mContext.getString(R.string.wifi_remembered)); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (WifiTracker.sVerboseLogging > 0) { |
| 357 | // Add RSSI/band information for this config, what was seen up to 6 seconds ago |
| 358 | // verbose WiFi Logging is only turned on thru developers settings |
| 359 | if (mInfo != null && mNetworkInfo != null) { // This is the active connection |
| 360 | summary.append(" f=" + Integer.toString(mInfo.getFrequency())); |
| 361 | } |
| 362 | summary.append(" " + getVisibilityStatus()); |
| 363 | if (mConfig != null && mConfig.autoJoinStatus > 0) { |
| 364 | summary.append(" (" + mConfig.autoJoinStatus); |
| 365 | if (mConfig.blackListTimestamp > 0) { |
| 366 | long now = System.currentTimeMillis(); |
| 367 | long diff = (now - mConfig.blackListTimestamp)/1000; |
| 368 | long sec = diff%60; //seconds |
| 369 | long min = (diff/60)%60; //minutes |
| 370 | long hour = (min/60)%60; //hours |
| 371 | summary.append(", "); |
| 372 | if (hour > 0) summary.append(Long.toString(hour) + "h "); |
| 373 | summary.append( Long.toString(min) + "m "); |
| 374 | summary.append( Long.toString(sec) + "s "); |
| 375 | } |
| 376 | summary.append(")"); |
| 377 | } |
| 378 | if (mConfig != null && mConfig.numIpConfigFailures > 0) { |
| 379 | summary.append(" ipf=").append(mConfig.numIpConfigFailures); |
| 380 | } |
| 381 | if (mConfig != null && mConfig.numConnectionFailures > 0) { |
| 382 | summary.append(" cf=").append(mConfig.numConnectionFailures); |
| 383 | } |
| 384 | if (mConfig != null && mConfig.numAuthFailures > 0) { |
| 385 | summary.append(" authf=").append(mConfig.numAuthFailures); |
| 386 | } |
| 387 | if (mConfig != null && mConfig.numNoInternetAccessReports > 0) { |
| 388 | summary.append(" noInt=").append(mConfig.numNoInternetAccessReports); |
| 389 | } |
| 390 | } |
| 391 | return summary.toString(); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Returns the visibility status of the WifiConfiguration. |
| 396 | * |
| 397 | * @return autojoin debugging information |
| 398 | * TODO: use a string formatter |
| 399 | * ["rssi 5Ghz", "num results on 5GHz" / "rssi 5Ghz", "num results on 5GHz"] |
| 400 | * For instance [-40,5/-30,2] |
| 401 | */ |
| 402 | private String getVisibilityStatus() { |
| 403 | StringBuilder visibility = new StringBuilder(); |
| 404 | StringBuilder scans24GHz = null; |
| 405 | StringBuilder scans5GHz = null; |
| 406 | String bssid = null; |
| 407 | |
| 408 | long now = System.currentTimeMillis(); |
| 409 | |
| 410 | if (mInfo != null) { |
| 411 | bssid = mInfo.getBSSID(); |
| 412 | if (bssid != null) { |
| 413 | visibility.append(" ").append(bssid); |
| 414 | } |
| 415 | visibility.append(" rssi=").append(mInfo.getRssi()); |
| 416 | visibility.append(" "); |
| 417 | visibility.append(" score=").append(mInfo.score); |
| 418 | visibility.append(String.format(" tx=%.1f,", mInfo.txSuccessRate)); |
| 419 | visibility.append(String.format("%.1f,", mInfo.txRetriesRate)); |
| 420 | visibility.append(String.format("%.1f ", mInfo.txBadRate)); |
| 421 | visibility.append(String.format("rx=%.1f", mInfo.rxSuccessRate)); |
| 422 | } |
| 423 | |
| 424 | if (mScanResultCache != null) { |
| 425 | int rssi5 = WifiConfiguration.INVALID_RSSI; |
| 426 | int rssi24 = WifiConfiguration.INVALID_RSSI; |
| 427 | int num5 = 0; |
| 428 | int num24 = 0; |
| 429 | int numBlackListed = 0; |
| 430 | int n24 = 0; // Number scan results we included in the string |
| 431 | int n5 = 0; // Number scan results we included in the string |
| 432 | Map<String, ScanResult> list = mScanResultCache.snapshot(); |
| 433 | // TODO: sort list by RSSI or age |
| 434 | for (ScanResult result : list.values()) { |
| 435 | if (result.seen == 0) |
| 436 | continue; |
| 437 | |
| 438 | if (result.autoJoinStatus != ScanResult.ENABLED) numBlackListed++; |
| 439 | |
| 440 | if (result.frequency >= LOWER_FREQ_5GHZ |
| 441 | && result.frequency <= HIGHER_FREQ_5GHZ) { |
| 442 | // Strictly speaking: [4915, 5825] |
| 443 | // number of known BSSID on 5GHz band |
| 444 | num5 = num5 + 1; |
| 445 | } else if (result.frequency >= LOWER_FREQ_24GHZ |
| 446 | && result.frequency <= HIGHER_FREQ_24GHZ) { |
| 447 | // Strictly speaking: [2412, 2482] |
| 448 | // number of known BSSID on 2.4Ghz band |
| 449 | num24 = num24 + 1; |
| 450 | } |
| 451 | |
| 452 | // Ignore results seen, older than 20 seconds |
| 453 | if (now - result.seen > VISIBILITY_OUTDATED_AGE_IN_MILLI) continue; |
| 454 | |
| 455 | if (result.frequency >= LOWER_FREQ_5GHZ |
| 456 | && result.frequency <= HIGHER_FREQ_5GHZ) { |
| 457 | if (result.level > rssi5) { |
| 458 | rssi5 = result.level; |
| 459 | } |
| 460 | if (n5 < 4) { |
| 461 | if (scans5GHz == null) scans5GHz = new StringBuilder(); |
| 462 | scans5GHz.append(" \n{").append(result.BSSID); |
| 463 | if (bssid != null && result.BSSID.equals(bssid)) scans5GHz.append("*"); |
| 464 | scans5GHz.append("=").append(result.frequency); |
| 465 | scans5GHz.append(",").append(result.level); |
| 466 | if (result.autoJoinStatus != 0) { |
| 467 | scans5GHz.append(",st=").append(result.autoJoinStatus); |
| 468 | } |
| 469 | if (result.numIpConfigFailures != 0) { |
| 470 | scans5GHz.append(",ipf=").append(result.numIpConfigFailures); |
| 471 | } |
| 472 | scans5GHz.append("}"); |
| 473 | n5++; |
| 474 | } |
| 475 | } else if (result.frequency >= LOWER_FREQ_24GHZ |
| 476 | && result.frequency <= HIGHER_FREQ_24GHZ) { |
| 477 | if (result.level > rssi24) { |
| 478 | rssi24 = result.level; |
| 479 | } |
| 480 | if (n24 < 4) { |
| 481 | if (scans24GHz == null) scans24GHz = new StringBuilder(); |
| 482 | scans24GHz.append(" \n{").append(result.BSSID); |
| 483 | if (bssid != null && result.BSSID.equals(bssid)) scans24GHz.append("*"); |
| 484 | scans24GHz.append("=").append(result.frequency); |
| 485 | scans24GHz.append(",").append(result.level); |
| 486 | if (result.autoJoinStatus != 0) { |
| 487 | scans24GHz.append(",st=").append(result.autoJoinStatus); |
| 488 | } |
| 489 | if (result.numIpConfigFailures != 0) { |
| 490 | scans24GHz.append(",ipf=").append(result.numIpConfigFailures); |
| 491 | } |
| 492 | scans24GHz.append("}"); |
| 493 | n24++; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | visibility.append(" ["); |
| 498 | if (num24 > 0) { |
| 499 | visibility.append("(").append(num24).append(")"); |
| 500 | if (n24 <= 4) { |
| 501 | if (scans24GHz != null) { |
| 502 | visibility.append(scans24GHz.toString()); |
| 503 | } |
| 504 | } else { |
| 505 | visibility.append("max=").append(rssi24); |
| 506 | if (scans24GHz != null) { |
| 507 | visibility.append(",").append(scans24GHz.toString()); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | visibility.append(";"); |
| 512 | if (num5 > 0) { |
| 513 | visibility.append("(").append(num5).append(")"); |
| 514 | if (n5 <= 4) { |
| 515 | if (scans5GHz != null) { |
| 516 | visibility.append(scans5GHz.toString()); |
| 517 | } |
| 518 | } else { |
| 519 | visibility.append("max=").append(rssi5); |
| 520 | if (scans5GHz != null) { |
| 521 | visibility.append(",").append(scans5GHz.toString()); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | if (numBlackListed > 0) |
| 526 | visibility.append("!").append(numBlackListed); |
| 527 | visibility.append("]"); |
| 528 | } else { |
| 529 | if (mRssi != Integer.MAX_VALUE) { |
| 530 | visibility.append(" rssi="); |
| 531 | visibility.append(mRssi); |
| 532 | if (mScanResult != null) { |
| 533 | visibility.append(", f="); |
| 534 | visibility.append(mScanResult.frequency); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return visibility.toString(); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Return whether this is the active connection. |
| 544 | * For ephemeral connections (networkId is invalid), this returns false if the network is |
| 545 | * disconnected. |
| 546 | */ |
| 547 | public boolean isActive() { |
| 548 | return mNetworkInfo != null && |
| 549 | (networkId != WifiConfiguration.INVALID_NETWORK_ID || |
| 550 | mNetworkInfo.getState() != State.DISCONNECTED); |
| 551 | } |
| 552 | |
| 553 | public boolean isConnectable() { |
| 554 | return getLevel() != -1 && getDetailedState() == null; |
| 555 | } |
| 556 | |
| 557 | public boolean isEphemeral() { |
| 558 | return !isSaved() && mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED; |
| 559 | } |
| 560 | |
| 561 | /** Return whether the given {@link WifiInfo} is for this access point. */ |
| 562 | private boolean isInfoForThisAccessPoint(WifiInfo info) { |
| 563 | if (networkId != WifiConfiguration.INVALID_NETWORK_ID) { |
| 564 | return networkId == info.getNetworkId(); |
| 565 | } else { |
| 566 | // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID. |
| 567 | // (Note that we only do this if the WifiConfiguration explicitly equals INVALID). |
| 568 | // TODO: Handle hex string SSIDs. |
| 569 | return ssid.equals(removeDoubleQuotes(info.getSSID())); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | public boolean isSaved() { |
| 574 | return networkId != WifiConfiguration.INVALID_NETWORK_ID; |
| 575 | } |
| 576 | |
| 577 | public Object getTag() { |
| 578 | return mTag; |
| 579 | } |
| 580 | |
| 581 | public void setTag(Object tag) { |
| 582 | mTag = tag; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Generate and save a default wifiConfiguration with common values. |
| 587 | * Can only be called for unsecured networks. |
| 588 | */ |
| 589 | public void generateOpenNetworkConfig() { |
| 590 | if (security != SECURITY_NONE) |
| 591 | throw new IllegalStateException(); |
| 592 | if (mConfig != null) |
| 593 | return; |
| 594 | mConfig = new WifiConfiguration(); |
| 595 | mConfig.SSID = AccessPoint.convertToQuotedString(ssid); |
| 596 | mConfig.allowedKeyManagement.set(KeyMgmt.NONE); |
| 597 | } |
| 598 | |
| 599 | void loadConfig(WifiConfiguration config) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 600 | if (config.isPasspoint()) |
| 601 | ssid = config.providerFriendlyName; |
| 602 | else |
| 603 | ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); |
| 604 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 605 | security = getSecurity(config); |
| 606 | networkId = config.networkId; |
| 607 | mConfig = config; |
| 608 | } |
| 609 | |
| 610 | private void loadResult(ScanResult result) { |
| 611 | ssid = result.SSID; |
| 612 | security = getSecurity(result); |
| 613 | if (security == SECURITY_PSK) |
| 614 | pskType = getPskType(result); |
| 615 | mRssi = result.level; |
| 616 | mScanResult = result; |
| 617 | if (result.seen > mSeen) { |
| 618 | mSeen = result.seen; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | public void saveWifiState(Bundle savedState) { |
| 623 | savedState.putParcelable(KEY_CONFIG, mConfig); |
| 624 | savedState.putParcelable(KEY_SCANRESULT, mScanResult); |
| 625 | savedState.putParcelable(KEY_WIFIINFO, mInfo); |
| 626 | if (mNetworkInfo != null) { |
| 627 | savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | public void setListener(AccessPointListener listener) { |
| 632 | mAccessPointListener = listener; |
| 633 | } |
| 634 | |
| 635 | boolean update(ScanResult result) { |
| 636 | if (result.seen > mSeen) { |
| 637 | mSeen = result.seen; |
| 638 | } |
| 639 | if (WifiTracker.sVerboseLogging > 0) { |
| 640 | if (mScanResultCache == null) { |
| 641 | mScanResultCache = new LruCache<String, ScanResult>(32); |
| 642 | } |
| 643 | mScanResultCache.put(result.BSSID, result); |
| 644 | } |
| 645 | |
| 646 | if (ssid.equals(result.SSID) && security == getSecurity(result)) { |
| 647 | if (WifiManager.compareSignalLevel(result.level, mRssi) > 0) { |
| 648 | int oldLevel = getLevel(); |
| 649 | mRssi = result.level; |
| 650 | if (getLevel() != oldLevel && mAccessPointListener != null) { |
| 651 | mAccessPointListener.onLevelChanged(this); |
| 652 | } |
| 653 | } |
| 654 | // This flag only comes from scans, is not easily saved in config |
| 655 | if (security == SECURITY_PSK) { |
| 656 | pskType = getPskType(result); |
| 657 | } |
| 658 | mScanResult = result; |
| 659 | if (mAccessPointListener != null) { |
| 660 | mAccessPointListener.onAccessPointChanged(this); |
| 661 | } |
| 662 | return true; |
| 663 | } |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | boolean update(WifiInfo info, NetworkInfo networkInfo) { |
| 668 | boolean reorder = false; |
| 669 | if (info != null && isInfoForThisAccessPoint(info)) { |
| 670 | reorder = (mInfo == null); |
| 671 | mRssi = info.getRssi(); |
| 672 | mInfo = info; |
| 673 | mNetworkInfo = networkInfo; |
| 674 | if (mAccessPointListener != null) { |
| 675 | mAccessPointListener.onAccessPointChanged(this); |
| 676 | } |
| 677 | } else if (mInfo != null) { |
| 678 | reorder = true; |
| 679 | mInfo = null; |
| 680 | mNetworkInfo = null; |
| 681 | if (mAccessPointListener != null) { |
| 682 | mAccessPointListener.onAccessPointChanged(this); |
| 683 | } |
| 684 | } |
| 685 | return reorder; |
| 686 | } |
| 687 | |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 688 | void update(WifiConfiguration config) { |
| 689 | mConfig = config; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 690 | networkId = config.networkId; |
| 691 | if (mAccessPointListener != null) { |
| 692 | mAccessPointListener.onAccessPointChanged(this); |
| 693 | } |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 696 | public static String getSummary(Context context, String ssid, DetailedState state, |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 697 | boolean isEphemeral, String passpointProvider) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 698 | if (state == DetailedState.CONNECTED && ssid == null) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 699 | if (TextUtils.isEmpty(passpointProvider) == false) { |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 700 | // Special case for connected + passpoint networks. |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 701 | String format = context.getString(R.string.connected_via_passpoint); |
| 702 | return String.format(format, passpointProvider); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 703 | } else if (isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 704 | // Special case for connected + ephemeral networks. |
| 705 | return context.getString(R.string.connected_via_wfa); |
| 706 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 707 | } |
| 708 | |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame^] | 709 | // Case when there is wifi connected without internet connectivity. |
| 710 | final ConnectivityManager cm = (ConnectivityManager) |
| 711 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 712 | if (state == DetailedState.CONNECTED) { |
| 713 | IWifiManager wifiManager = IWifiManager.Stub.asInterface( |
| 714 | ServiceManager.getService(Context.WIFI_SERVICE)); |
| 715 | Network nw; |
| 716 | |
| 717 | try { |
| 718 | nw = wifiManager.getCurrentNetwork(); |
| 719 | } catch (RemoteException e) { |
| 720 | nw = null; |
| 721 | } |
| 722 | NetworkCapabilities nc = cm.getNetworkCapabilities(nw); |
| 723 | if (nc != null && !nc.hasCapability(nc.NET_CAPABILITY_VALIDATED)) { |
| 724 | return context.getString(R.string.wifi_connected_no_internet); |
| 725 | } |
| 726 | } |
| 727 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 728 | String[] formats = context.getResources().getStringArray((ssid == null) |
| 729 | ? R.array.wifi_status : R.array.wifi_status_with_ssid); |
| 730 | int index = state.ordinal(); |
| 731 | |
| 732 | if (index >= formats.length || formats[index].length() == 0) { |
| 733 | return null; |
| 734 | } |
| 735 | return String.format(formats[index], ssid); |
| 736 | } |
| 737 | |
| 738 | public static String getSummary(Context context, DetailedState state, boolean isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 739 | return getSummary(context, null, state, isEphemeral, null); |
| 740 | } |
| 741 | |
| 742 | public static String getSummary(Context context, DetailedState state, boolean isEphemeral, |
| 743 | String passpointProvider) { |
| 744 | return getSummary(context, null, state, isEphemeral, passpointProvider); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | public static String convertToQuotedString(String string) { |
| 748 | return "\"" + string + "\""; |
| 749 | } |
| 750 | |
| 751 | private static int getPskType(ScanResult result) { |
| 752 | boolean wpa = result.capabilities.contains("WPA-PSK"); |
| 753 | boolean wpa2 = result.capabilities.contains("WPA2-PSK"); |
| 754 | if (wpa2 && wpa) { |
| 755 | return PSK_WPA_WPA2; |
| 756 | } else if (wpa2) { |
| 757 | return PSK_WPA2; |
| 758 | } else if (wpa) { |
| 759 | return PSK_WPA; |
| 760 | } else { |
| 761 | Log.w(TAG, "Received abnormal flag string: " + result.capabilities); |
| 762 | return PSK_UNKNOWN; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | private static int getSecurity(ScanResult result) { |
| 767 | if (result.capabilities.contains("WEP")) { |
| 768 | return SECURITY_WEP; |
| 769 | } else if (result.capabilities.contains("PSK")) { |
| 770 | return SECURITY_PSK; |
| 771 | } else if (result.capabilities.contains("EAP")) { |
| 772 | return SECURITY_EAP; |
| 773 | } |
| 774 | return SECURITY_NONE; |
| 775 | } |
| 776 | |
| 777 | static int getSecurity(WifiConfiguration config) { |
| 778 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) { |
| 779 | return SECURITY_PSK; |
| 780 | } |
| 781 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || |
| 782 | config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) { |
| 783 | return SECURITY_EAP; |
| 784 | } |
| 785 | return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE; |
| 786 | } |
| 787 | |
| 788 | public static String securityToString(int security, int pskType) { |
| 789 | if (security == SECURITY_WEP) { |
| 790 | return "WEP"; |
| 791 | } else if (security == SECURITY_PSK) { |
| 792 | if (pskType == PSK_WPA) { |
| 793 | return "WPA"; |
| 794 | } else if (pskType == PSK_WPA2) { |
| 795 | return "WPA2"; |
| 796 | } else if (pskType == PSK_WPA_WPA2) { |
| 797 | return "WPA_WPA2"; |
| 798 | } |
| 799 | return "PSK"; |
| 800 | } else if (security == SECURITY_EAP) { |
| 801 | return "EAP"; |
| 802 | } |
| 803 | return "NONE"; |
| 804 | } |
| 805 | |
| 806 | static String removeDoubleQuotes(String string) { |
| 807 | int length = string.length(); |
| 808 | if ((length > 1) && (string.charAt(0) == '"') |
| 809 | && (string.charAt(length - 1) == '"')) { |
| 810 | return string.substring(1, length - 1); |
| 811 | } |
| 812 | return string; |
| 813 | } |
| 814 | |
| 815 | public interface AccessPointListener { |
| 816 | void onAccessPointChanged(AccessPoint accessPoint); |
| 817 | void onLevelChanged(AccessPoint accessPoint); |
| 818 | } |
| 819 | } |