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