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