AccessPoint.java revision 56cfbfb0319dfed65b6307b1baf73cb9b92db9b9
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; 32import android.content.pm.ApplicationInfo; 33import android.content.pm.IPackageManager; 34import android.content.pm.PackageManager; 35import android.os.UserHandle; 36import android.os.RemoteException; 37import android.app.AppGlobals; 38 39import com.android.settingslib.R; 40 41import java.util.Map; 42 43 44public 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) { 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); 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 278 public String getConfigName() { 279 if (mConfig != null && mConfig.isPasspoint()) { 280 return mConfig.providerFriendlyName; 281 } else { 282 return ssid; 283 } 284 } 285 286 public DetailedState getDetailedState() { 287 return mNetworkInfo != null ? mNetworkInfo.getDetailedState() : null; 288 } 289 290 public String getSavedNetworkSummary() { 291 if (mConfig != null) { 292 PackageManager pm = mContext.getPackageManager(); 293 String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID); 294 int userId = UserHandle.getUserId(mConfig.creatorUid); 295 ApplicationInfo appInfo = null; 296 if (mConfig.creatorName != null && mConfig.creatorName.equals(systemName)) { 297 appInfo = mContext.getApplicationInfo(); 298 } else { 299 try { 300 IPackageManager ipm = AppGlobals.getPackageManager(); 301 appInfo = ipm.getApplicationInfo(mConfig.creatorName, 0 /* flags */, userId); 302 } catch (RemoteException rex) { 303 } 304 } 305 if (appInfo != null && 306 !appInfo.packageName.equals(mContext.getString(R.string.settings_package)) && 307 !appInfo.packageName.equals( 308 mContext.getString(R.string.certinstaller_package))) { 309 return mContext.getString(R.string.saved_network, appInfo.loadLabel(pm)); 310 } 311 } 312 return ""; 313 } 314 315 public String getSummary() { 316 return getSettingsSummary(); 317 } 318 319 public String getSettingsSummary() { 320 // Update to new summary 321 StringBuilder summary = new StringBuilder(); 322 323 if (isActive() && mConfig != null && mConfig.isPasspoint()) { 324 // This is the active connection on passpoint 325 summary.append(getSummary(mContext, getDetailedState(), 326 false, mConfig.providerFriendlyName)); 327 } else if (isActive()) { 328 // This is the active connection on non-passpoint network 329 summary.append(getSummary(mContext, getDetailedState(), 330 networkId == WifiConfiguration.INVALID_NETWORK_ID)); 331 } else if (mConfig != null && mConfig.isPasspoint()) { 332 String format = mContext.getString(R.string.available_via_passpoint); 333 summary.append(String.format(format, mConfig.providerFriendlyName)); 334 } else if (mConfig != null && mConfig.hasNoInternetAccess()) { 335 summary.append(mContext.getString(R.string.wifi_no_internet)); 336 } else if (mConfig != null && ((mConfig.status == WifiConfiguration.Status.DISABLED && 337 mConfig.disableReason != WifiConfiguration.DISABLED_UNKNOWN_REASON) 338 || mConfig.autoJoinStatus 339 >= WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE)) { 340 if (mConfig.autoJoinStatus 341 >= WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) { 342 if (mConfig.disableReason == WifiConfiguration.DISABLED_DHCP_FAILURE) { 343 summary.append(mContext.getString(R.string.wifi_disabled_network_failure)); 344 } else if (mConfig.disableReason == WifiConfiguration.DISABLED_AUTH_FAILURE) { 345 summary.append(mContext.getString(R.string.wifi_disabled_password_failure)); 346 } else { 347 summary.append(mContext.getString(R.string.wifi_disabled_wifi_failure)); 348 } 349 } else { 350 switch (mConfig.disableReason) { 351 case WifiConfiguration.DISABLED_AUTH_FAILURE: 352 summary.append(mContext.getString(R.string.wifi_disabled_password_failure)); 353 break; 354 case WifiConfiguration.DISABLED_DHCP_FAILURE: 355 case WifiConfiguration.DISABLED_DNS_FAILURE: 356 summary.append(mContext.getString(R.string.wifi_disabled_network_failure)); 357 break; 358 case WifiConfiguration.DISABLED_UNKNOWN_REASON: 359 case WifiConfiguration.DISABLED_ASSOCIATION_REJECT: 360 summary.append(mContext.getString(R.string.wifi_disabled_generic)); 361 break; 362 } 363 } 364 } else if (mRssi == Integer.MAX_VALUE) { // Wifi out of range 365 summary.append(mContext.getString(R.string.wifi_not_in_range)); 366 } else { // In range, not disabled. 367 if (mConfig != null) { // Is saved network 368 summary.append(mContext.getString(R.string.wifi_remembered)); 369 } 370 } 371 372 if (WifiTracker.sVerboseLogging > 0) { 373 // Add RSSI/band information for this config, what was seen up to 6 seconds ago 374 // verbose WiFi Logging is only turned on thru developers settings 375 if (mInfo != null && mNetworkInfo != null) { // This is the active connection 376 summary.append(" f=" + Integer.toString(mInfo.getFrequency())); 377 } 378 summary.append(" " + getVisibilityStatus()); 379 if (mConfig != null && mConfig.autoJoinStatus > 0) { 380 summary.append(" (" + mConfig.autoJoinStatus); 381 if (mConfig.blackListTimestamp > 0) { 382 long now = System.currentTimeMillis(); 383 long diff = (now - mConfig.blackListTimestamp)/1000; 384 long sec = diff%60; //seconds 385 long min = (diff/60)%60; //minutes 386 long hour = (min/60)%60; //hours 387 summary.append(", "); 388 if (hour > 0) summary.append(Long.toString(hour) + "h "); 389 summary.append( Long.toString(min) + "m "); 390 summary.append( Long.toString(sec) + "s "); 391 } 392 summary.append(")"); 393 } 394 if (mConfig != null && mConfig.numIpConfigFailures > 0) { 395 summary.append(" ipf=").append(mConfig.numIpConfigFailures); 396 } 397 if (mConfig != null && mConfig.numConnectionFailures > 0) { 398 summary.append(" cf=").append(mConfig.numConnectionFailures); 399 } 400 if (mConfig != null && mConfig.numAuthFailures > 0) { 401 summary.append(" authf=").append(mConfig.numAuthFailures); 402 } 403 if (mConfig != null && mConfig.numNoInternetAccessReports > 0) { 404 summary.append(" noInt=").append(mConfig.numNoInternetAccessReports); 405 } 406 } 407 return summary.toString(); 408 } 409 410 /** 411 * Returns the visibility status of the WifiConfiguration. 412 * 413 * @return autojoin debugging information 414 * TODO: use a string formatter 415 * ["rssi 5Ghz", "num results on 5GHz" / "rssi 5Ghz", "num results on 5GHz"] 416 * For instance [-40,5/-30,2] 417 */ 418 private String getVisibilityStatus() { 419 StringBuilder visibility = new StringBuilder(); 420 StringBuilder scans24GHz = null; 421 StringBuilder scans5GHz = null; 422 String bssid = null; 423 424 long now = System.currentTimeMillis(); 425 426 if (mInfo != null) { 427 bssid = mInfo.getBSSID(); 428 if (bssid != null) { 429 visibility.append(" ").append(bssid); 430 } 431 visibility.append(" rssi=").append(mInfo.getRssi()); 432 visibility.append(" "); 433 visibility.append(" score=").append(mInfo.score); 434 visibility.append(String.format(" tx=%.1f,", mInfo.txSuccessRate)); 435 visibility.append(String.format("%.1f,", mInfo.txRetriesRate)); 436 visibility.append(String.format("%.1f ", mInfo.txBadRate)); 437 visibility.append(String.format("rx=%.1f", mInfo.rxSuccessRate)); 438 } 439 440 if (mScanResultCache != null) { 441 int rssi5 = WifiConfiguration.INVALID_RSSI; 442 int rssi24 = WifiConfiguration.INVALID_RSSI; 443 int num5 = 0; 444 int num24 = 0; 445 int numBlackListed = 0; 446 int n24 = 0; // Number scan results we included in the string 447 int n5 = 0; // Number scan results we included in the string 448 Map<String, ScanResult> list = mScanResultCache.snapshot(); 449 // TODO: sort list by RSSI or age 450 for (ScanResult result : list.values()) { 451 if (result.seen == 0) 452 continue; 453 454 if (result.autoJoinStatus != ScanResult.ENABLED) numBlackListed++; 455 456 if (result.frequency >= LOWER_FREQ_5GHZ 457 && result.frequency <= HIGHER_FREQ_5GHZ) { 458 // Strictly speaking: [4915, 5825] 459 // number of known BSSID on 5GHz band 460 num5 = num5 + 1; 461 } else if (result.frequency >= LOWER_FREQ_24GHZ 462 && result.frequency <= HIGHER_FREQ_24GHZ) { 463 // Strictly speaking: [2412, 2482] 464 // number of known BSSID on 2.4Ghz band 465 num24 = num24 + 1; 466 } 467 468 // Ignore results seen, older than 20 seconds 469 if (now - result.seen > VISIBILITY_OUTDATED_AGE_IN_MILLI) continue; 470 471 if (result.frequency >= LOWER_FREQ_5GHZ 472 && result.frequency <= HIGHER_FREQ_5GHZ) { 473 if (result.level > rssi5) { 474 rssi5 = result.level; 475 } 476 if (n5 < 4) { 477 if (scans5GHz == null) scans5GHz = new StringBuilder(); 478 scans5GHz.append(" \n{").append(result.BSSID); 479 if (bssid != null && result.BSSID.equals(bssid)) scans5GHz.append("*"); 480 scans5GHz.append("=").append(result.frequency); 481 scans5GHz.append(",").append(result.level); 482 if (result.autoJoinStatus != 0) { 483 scans5GHz.append(",st=").append(result.autoJoinStatus); 484 } 485 if (result.numIpConfigFailures != 0) { 486 scans5GHz.append(",ipf=").append(result.numIpConfigFailures); 487 } 488 scans5GHz.append("}"); 489 n5++; 490 } 491 } else if (result.frequency >= LOWER_FREQ_24GHZ 492 && result.frequency <= HIGHER_FREQ_24GHZ) { 493 if (result.level > rssi24) { 494 rssi24 = result.level; 495 } 496 if (n24 < 4) { 497 if (scans24GHz == null) scans24GHz = new StringBuilder(); 498 scans24GHz.append(" \n{").append(result.BSSID); 499 if (bssid != null && result.BSSID.equals(bssid)) scans24GHz.append("*"); 500 scans24GHz.append("=").append(result.frequency); 501 scans24GHz.append(",").append(result.level); 502 if (result.autoJoinStatus != 0) { 503 scans24GHz.append(",st=").append(result.autoJoinStatus); 504 } 505 if (result.numIpConfigFailures != 0) { 506 scans24GHz.append(",ipf=").append(result.numIpConfigFailures); 507 } 508 scans24GHz.append("}"); 509 n24++; 510 } 511 } 512 } 513 visibility.append(" ["); 514 if (num24 > 0) { 515 visibility.append("(").append(num24).append(")"); 516 if (n24 <= 4) { 517 if (scans24GHz != null) { 518 visibility.append(scans24GHz.toString()); 519 } 520 } else { 521 visibility.append("max=").append(rssi24); 522 if (scans24GHz != null) { 523 visibility.append(",").append(scans24GHz.toString()); 524 } 525 } 526 } 527 visibility.append(";"); 528 if (num5 > 0) { 529 visibility.append("(").append(num5).append(")"); 530 if (n5 <= 4) { 531 if (scans5GHz != null) { 532 visibility.append(scans5GHz.toString()); 533 } 534 } else { 535 visibility.append("max=").append(rssi5); 536 if (scans5GHz != null) { 537 visibility.append(",").append(scans5GHz.toString()); 538 } 539 } 540 } 541 if (numBlackListed > 0) 542 visibility.append("!").append(numBlackListed); 543 visibility.append("]"); 544 } else { 545 if (mRssi != Integer.MAX_VALUE) { 546 visibility.append(" rssi="); 547 visibility.append(mRssi); 548 if (mScanResult != null) { 549 visibility.append(", f="); 550 visibility.append(mScanResult.frequency); 551 } 552 } 553 } 554 555 return visibility.toString(); 556 } 557 558 /** 559 * Return whether this is the active connection. 560 * For ephemeral connections (networkId is invalid), this returns false if the network is 561 * disconnected. 562 */ 563 public boolean isActive() { 564 return mNetworkInfo != null && 565 (networkId != WifiConfiguration.INVALID_NETWORK_ID || 566 mNetworkInfo.getState() != State.DISCONNECTED); 567 } 568 569 public boolean isConnectable() { 570 return getLevel() != -1 && getDetailedState() == null; 571 } 572 573 public boolean isEphemeral() { 574 return !isSaved() && mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED; 575 } 576 577 /** Return whether the given {@link WifiInfo} is for this access point. */ 578 private boolean isInfoForThisAccessPoint(WifiInfo info) { 579 if (networkId != WifiConfiguration.INVALID_NETWORK_ID) { 580 return networkId == info.getNetworkId(); 581 } else { 582 // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID. 583 // (Note that we only do this if the WifiConfiguration explicitly equals INVALID). 584 // TODO: Handle hex string SSIDs. 585 return ssid.equals(removeDoubleQuotes(info.getSSID())); 586 } 587 } 588 589 public boolean isSaved() { 590 return networkId != WifiConfiguration.INVALID_NETWORK_ID; 591 } 592 593 public Object getTag() { 594 return mTag; 595 } 596 597 public void setTag(Object tag) { 598 mTag = tag; 599 } 600 601 /** 602 * Generate and save a default wifiConfiguration with common values. 603 * Can only be called for unsecured networks. 604 */ 605 public void generateOpenNetworkConfig() { 606 if (security != SECURITY_NONE) 607 throw new IllegalStateException(); 608 if (mConfig != null) 609 return; 610 mConfig = new WifiConfiguration(); 611 mConfig.SSID = AccessPoint.convertToQuotedString(ssid); 612 mConfig.allowedKeyManagement.set(KeyMgmt.NONE); 613 } 614 615 void loadConfig(WifiConfiguration config) { 616 if (config.isPasspoint()) 617 ssid = config.providerFriendlyName; 618 else 619 ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); 620 621 security = getSecurity(config); 622 networkId = config.networkId; 623 mConfig = config; 624 } 625 626 private void loadResult(ScanResult result) { 627 ssid = result.SSID; 628 security = getSecurity(result); 629 if (security == SECURITY_PSK) 630 pskType = getPskType(result); 631 mRssi = result.level; 632 mScanResult = result; 633 if (result.seen > mSeen) { 634 mSeen = result.seen; 635 } 636 } 637 638 public void saveWifiState(Bundle savedState) { 639 savedState.putParcelable(KEY_CONFIG, mConfig); 640 savedState.putParcelable(KEY_SCANRESULT, mScanResult); 641 savedState.putParcelable(KEY_WIFIINFO, mInfo); 642 if (mNetworkInfo != null) { 643 savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo); 644 } 645 } 646 647 public void setListener(AccessPointListener listener) { 648 mAccessPointListener = listener; 649 } 650 651 boolean update(ScanResult result) { 652 if (result.seen > mSeen) { 653 mSeen = result.seen; 654 } 655 if (WifiTracker.sVerboseLogging > 0) { 656 if (mScanResultCache == null) { 657 mScanResultCache = new LruCache<String, ScanResult>(32); 658 } 659 mScanResultCache.put(result.BSSID, result); 660 } 661 662 if (ssid.equals(result.SSID) && security == getSecurity(result)) { 663 if (WifiManager.compareSignalLevel(result.level, mRssi) > 0) { 664 int oldLevel = getLevel(); 665 mRssi = result.level; 666 if (getLevel() != oldLevel && mAccessPointListener != null) { 667 mAccessPointListener.onLevelChanged(this); 668 } 669 } 670 // This flag only comes from scans, is not easily saved in config 671 if (security == SECURITY_PSK) { 672 pskType = getPskType(result); 673 } 674 mScanResult = result; 675 if (mAccessPointListener != null) { 676 mAccessPointListener.onAccessPointChanged(this); 677 } 678 return true; 679 } 680 return false; 681 } 682 683 boolean update(WifiInfo info, NetworkInfo networkInfo) { 684 boolean reorder = false; 685 if (info != null && isInfoForThisAccessPoint(info)) { 686 reorder = (mInfo == null); 687 mRssi = info.getRssi(); 688 mInfo = info; 689 mNetworkInfo = networkInfo; 690 if (mAccessPointListener != null) { 691 mAccessPointListener.onAccessPointChanged(this); 692 } 693 } else if (mInfo != null) { 694 reorder = true; 695 mInfo = null; 696 mNetworkInfo = null; 697 if (mAccessPointListener != null) { 698 mAccessPointListener.onAccessPointChanged(this); 699 } 700 } 701 return reorder; 702 } 703 704 void update(WifiConfiguration config) { 705 mConfig = config; 706 networkId = config.networkId; 707 if (mAccessPointListener != null) { 708 mAccessPointListener.onAccessPointChanged(this); 709 } 710 } 711 712 public static String getSummary(Context context, String ssid, DetailedState state, 713 boolean isEphemeral, String passpointProvider) { 714 if (state == DetailedState.CONNECTED && ssid == null) { 715 if (TextUtils.isEmpty(passpointProvider) == false) { 716 // Special case for connected + passpoint networks. 717 String format = context.getString(R.string.connected_via_passpoint); 718 return String.format(format, passpointProvider); 719 } else if (isEphemeral) { 720 // Special case for connected + ephemeral networks. 721 return context.getString(R.string.connected_via_wfa); 722 } 723 } 724 725 String[] formats = context.getResources().getStringArray((ssid == null) 726 ? R.array.wifi_status : R.array.wifi_status_with_ssid); 727 int index = state.ordinal(); 728 729 if (index >= formats.length || formats[index].length() == 0) { 730 return null; 731 } 732 return String.format(formats[index], ssid); 733 } 734 735 public static String getSummary(Context context, DetailedState state, boolean isEphemeral) { 736 return getSummary(context, null, state, isEphemeral, null); 737 } 738 739 public static String getSummary(Context context, DetailedState state, boolean isEphemeral, 740 String passpointProvider) { 741 return getSummary(context, null, state, isEphemeral, passpointProvider); 742 } 743 744 public static String convertToQuotedString(String string) { 745 return "\"" + string + "\""; 746 } 747 748 private static int getPskType(ScanResult result) { 749 boolean wpa = result.capabilities.contains("WPA-PSK"); 750 boolean wpa2 = result.capabilities.contains("WPA2-PSK"); 751 if (wpa2 && wpa) { 752 return PSK_WPA_WPA2; 753 } else if (wpa2) { 754 return PSK_WPA2; 755 } else if (wpa) { 756 return PSK_WPA; 757 } else { 758 Log.w(TAG, "Received abnormal flag string: " + result.capabilities); 759 return PSK_UNKNOWN; 760 } 761 } 762 763 private static int getSecurity(ScanResult result) { 764 if (result.capabilities.contains("WEP")) { 765 return SECURITY_WEP; 766 } else if (result.capabilities.contains("PSK")) { 767 return SECURITY_PSK; 768 } else if (result.capabilities.contains("EAP")) { 769 return SECURITY_EAP; 770 } 771 return SECURITY_NONE; 772 } 773 774 static int getSecurity(WifiConfiguration config) { 775 if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) { 776 return SECURITY_PSK; 777 } 778 if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || 779 config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) { 780 return SECURITY_EAP; 781 } 782 return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE; 783 } 784 785 public static String securityToString(int security, int pskType) { 786 if (security == SECURITY_WEP) { 787 return "WEP"; 788 } else if (security == SECURITY_PSK) { 789 if (pskType == PSK_WPA) { 790 return "WPA"; 791 } else if (pskType == PSK_WPA2) { 792 return "WPA2"; 793 } else if (pskType == PSK_WPA_WPA2) { 794 return "WPA_WPA2"; 795 } 796 return "PSK"; 797 } else if (security == SECURITY_EAP) { 798 return "EAP"; 799 } 800 return "NONE"; 801 } 802 803 static String removeDoubleQuotes(String string) { 804 int length = string.length(); 805 if ((length > 1) && (string.charAt(0) == '"') 806 && (string.charAt(length - 1) == '"')) { 807 return string.substring(1, length - 1); 808 } 809 return string; 810 } 811 812 public interface AccessPointListener { 813 void onAccessPointChanged(AccessPoint accessPoint); 814 void onLevelChanged(AccessPoint accessPoint); 815 } 816} 817