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 | package com.android.settingslib.wifi; |
| 17 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 18 | import android.annotation.AnyThread; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 19 | import android.annotation.MainThread; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
| 25 | import android.net.Network; |
| 26 | import android.net.NetworkCapabilities; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 27 | import android.net.NetworkInfo; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 28 | import android.net.NetworkKey; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 29 | import android.net.NetworkRequest; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 30 | import android.net.NetworkScoreManager; |
| 31 | import android.net.ScoredNetwork; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 32 | import android.net.wifi.ScanResult; |
| 33 | import android.net.wifi.WifiConfiguration; |
| 34 | import android.net.wifi.WifiInfo; |
| 35 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 36 | import android.net.wifi.WifiNetworkScoreCache; |
| 37 | import android.net.wifi.WifiNetworkScoreCache.CacheListener; |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 38 | import android.net.wifi.hotspot2.OsuProvider; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 39 | import android.os.Handler; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 40 | import android.os.HandlerThread; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 41 | import android.os.Message; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 42 | import android.os.Process; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 43 | import android.os.SystemClock; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 44 | import android.provider.Settings; |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 45 | import android.text.format.DateUtils; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 46 | import android.util.ArrayMap; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 47 | import android.util.ArraySet; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 49 | import android.util.Pair; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 50 | import android.widget.Toast; |
| 51 | |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 52 | import androidx.annotation.GuardedBy; |
| 53 | import androidx.annotation.NonNull; |
| 54 | import androidx.annotation.VisibleForTesting; |
| 55 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 56 | import com.android.settingslib.R; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 57 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| 58 | import com.android.settingslib.core.lifecycle.LifecycleObserver; |
| 59 | import com.android.settingslib.core.lifecycle.events.OnDestroy; |
| 60 | import com.android.settingslib.core.lifecycle.events.OnStart; |
| 61 | import com.android.settingslib.core.lifecycle.events.OnStop; |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 62 | import com.android.settingslib.utils.ThreadUtils; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 63 | |
| 64 | import java.io.PrintWriter; |
| 65 | import java.util.ArrayList; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 66 | import java.util.Collection; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 67 | import java.util.Collections; |
| 68 | import java.util.HashMap; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 69 | import java.util.Iterator; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 70 | import java.util.List; |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 71 | import java.util.ListIterator; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 72 | import java.util.Map; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 73 | import java.util.Set; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 74 | import java.util.concurrent.atomic.AtomicBoolean; |
| 75 | |
| 76 | /** |
| 77 | * Tracks saved or available wifi networks and their state. |
| 78 | */ |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 79 | public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestroy { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Default maximum age in millis of cached scored networks in |
| 82 | * {@link AccessPoint#mScoredNetworkCache} to be used for speed label generation. |
| 83 | */ |
Sundeep Ghuman | 64bf090 | 2017-09-11 13:00:53 -0700 | [diff] [blame] | 84 | private static final long DEFAULT_MAX_CACHED_SCORE_AGE_MILLIS = 20 * DateUtils.MINUTE_IN_MILLIS; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 85 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 86 | /** Maximum age of scan results to hold onto while actively scanning. **/ |
Quang Luong | 85bbc6e | 2019-03-28 15:35:22 -0700 | [diff] [blame] | 87 | @VisibleForTesting static final long MAX_SCAN_RESULT_AGE_MILLIS = 15000; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 88 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 89 | private static final String TAG = "WifiTracker"; |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 90 | private static final boolean DBG() { |
| 91 | return Log.isLoggable(TAG, Log.DEBUG); |
| 92 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 93 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 94 | private static boolean isVerboseLoggingEnabled() { |
| 95 | return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Verbose logging flag set thru developer debugging options and used so as to assist with |
| 100 | * in-the-field WiFi connectivity debugging. |
| 101 | * |
| 102 | * <p>{@link #isVerboseLoggingEnabled()} should be read rather than referencing this value |
| 103 | * directly, to ensure adb TAG level verbose settings are respected. |
| 104 | */ |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 105 | public static boolean sVerboseLogging; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 106 | |
| 107 | // TODO: Allow control of this? |
| 108 | // Combo scans can take 5-6s to complete - set to 10s. |
| 109 | private static final int WIFI_RESCAN_INTERVAL_MS = 10 * 1000; |
| 110 | |
| 111 | private final Context mContext; |
| 112 | private final WifiManager mWifiManager; |
| 113 | private final IntentFilter mFilter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 114 | private final ConnectivityManager mConnectivityManager; |
| 115 | private final NetworkRequest mNetworkRequest; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 116 | private final AtomicBoolean mConnected = new AtomicBoolean(false); |
Sundeep Ghuman | dc67f4d | 2018-01-31 16:03:33 -0800 | [diff] [blame] | 117 | private final WifiListenerExecutor mListener; |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 118 | @VisibleForTesting Handler mWorkHandler; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 119 | private HandlerThread mWorkThread; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 120 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 121 | private WifiTrackerNetworkCallback mNetworkCallback; |
| 122 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Synchronization lock for managing concurrency between main and worker threads. |
| 125 | * |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 126 | * <p>This lock should be held for all modifications to {@link #mInternalAccessPoints} and |
| 127 | * {@link #mScanner}. |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 128 | */ |
| 129 | private final Object mLock = new Object(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 130 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 131 | /** The list of AccessPoints, aggregated visible ScanResults with metadata. */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 132 | @GuardedBy("mLock") |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 133 | private final List<AccessPoint> mInternalAccessPoints = new ArrayList<>(); |
| 134 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 135 | @GuardedBy("mLock") |
| 136 | private final Set<NetworkKey> mRequestedScores = new ArraySet<>(); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 137 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Tracks whether fresh scan results have been received since scanning start. |
| 140 | * |
jackqdyulei | 396e52d | 2018-09-19 17:28:27 -0700 | [diff] [blame] | 141 | * <p>If this variable is false, we will not invoke callbacks so that we do not |
| 142 | * update the UI with stale data / clear out existing UI elements prematurely. |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 143 | */ |
| 144 | private boolean mStaleScanResults = true; |
| 145 | |
Quang Luong | 85bbc6e | 2019-03-28 15:35:22 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Tracks whether the latest SCAN_RESULTS_AVAILABLE_ACTION contained new scans. If not, then |
| 148 | * we treat the last scan as an aborted scan and increase the eviction timeout window to avoid |
| 149 | * completely flushing the AP list before the next successful scan completes. |
| 150 | */ |
| 151 | private boolean mLastScanSucceeded = true; |
| 152 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 153 | // Does not need to be locked as it only updated on the worker thread, with the exception of |
| 154 | // during onStart, which occurs before the receiver is registered on the work handler. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 155 | private final HashMap<String, ScanResult> mScanResultCache = new HashMap<>(); |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 156 | private boolean mRegistered; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 157 | |
| 158 | private NetworkInfo mLastNetworkInfo; |
| 159 | private WifiInfo mLastInfo; |
| 160 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 161 | private final NetworkScoreManager mNetworkScoreManager; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 162 | private WifiNetworkScoreCache mScoreCache; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 163 | private boolean mNetworkScoringUiEnabled; |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 164 | private long mMaxSpeedLabelScoreCacheAge; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 165 | |
Hai Shalom | e3cc068 | 2018-11-08 14:21:58 -0800 | [diff] [blame] | 166 | private static final String WIFI_SECURITY_PSK = "PSK"; |
| 167 | private static final String WIFI_SECURITY_EAP = "EAP"; |
| 168 | private static final String WIFI_SECURITY_SAE = "SAE"; |
| 169 | private static final String WIFI_SECURITY_OWE = "OWE"; |
| 170 | private static final String WIFI_SECURITY_SUITE_B_192 = "SUITE_B_192"; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 171 | |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 172 | @GuardedBy("mLock") |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 173 | @VisibleForTesting |
| 174 | Scanner mScanner; |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 175 | |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 176 | private static IntentFilter newIntentFilter() { |
| 177 | IntentFilter filter = new IntentFilter(); |
| 178 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 179 | filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 180 | filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); |
| 181 | filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); |
| 182 | filter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); |
| 183 | filter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION); |
| 184 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 185 | filter.addAction(WifiManager.RSSI_CHANGED_ACTION); |
| 186 | |
| 187 | return filter; |
| 188 | } |
| 189 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Use the lifecycle constructor below whenever possible |
| 192 | */ |
| 193 | @Deprecated |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 194 | public WifiTracker(Context context, WifiListener wifiListener, |
| 195 | boolean includeSaved, boolean includeScans) { |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 196 | this(context, wifiListener, |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 197 | context.getSystemService(WifiManager.class), |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 198 | context.getSystemService(ConnectivityManager.class), |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 199 | context.getSystemService(NetworkScoreManager.class), |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 200 | newIntentFilter()); |
| 201 | } |
| 202 | |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 203 | // TODO(sghuman): Clean up includeSaved and includeScans from all constructors and linked |
Sundeep Ghuman | 8d157e3 | 2018-01-24 20:32:35 -0800 | [diff] [blame] | 204 | // calling apps once IC window is complete |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 205 | public WifiTracker(Context context, WifiListener wifiListener, |
| 206 | @NonNull Lifecycle lifecycle, boolean includeSaved, boolean includeScans) { |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 207 | this(context, wifiListener, |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 208 | context.getSystemService(WifiManager.class), |
| 209 | context.getSystemService(ConnectivityManager.class), |
| 210 | context.getSystemService(NetworkScoreManager.class), |
| 211 | newIntentFilter()); |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 212 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 213 | lifecycle.addObserver(this); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @VisibleForTesting |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 217 | WifiTracker(Context context, WifiListener wifiListener, |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 218 | WifiManager wifiManager, ConnectivityManager connectivityManager, |
| 219 | NetworkScoreManager networkScoreManager, |
| 220 | IntentFilter filter) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 221 | mContext = context; |
| 222 | mWifiManager = wifiManager; |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 223 | mListener = new WifiListenerExecutor(wifiListener); |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 224 | mConnectivityManager = connectivityManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 225 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 226 | // check if verbose logging developer option has been turned on or off |
Amin Shaikh | bc38add | 2018-12-26 15:31:45 -0500 | [diff] [blame] | 227 | sVerboseLogging = mWifiManager != null && (mWifiManager.getVerboseLoggingLevel() > 0); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 228 | |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 229 | mFilter = filter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 230 | |
| 231 | mNetworkRequest = new NetworkRequest.Builder() |
| 232 | .clearCapabilities() |
Jeff Sharkey | c159d52 | 2018-03-28 10:54:07 -0600 | [diff] [blame] | 233 | .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 234 | .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) |
| 235 | .build(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 236 | |
| 237 | mNetworkScoreManager = networkScoreManager; |
| 238 | |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 239 | // TODO(sghuman): Remove this and create less hacky solution for testing |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 240 | final HandlerThread workThread = new HandlerThread(TAG |
| 241 | + "{" + Integer.toHexString(System.identityHashCode(this)) + "}", |
| 242 | Process.THREAD_PRIORITY_BACKGROUND); |
| 243 | workThread.start(); |
| 244 | setWorkThread(workThread); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Sanity warning: this wipes out mScoreCache, so use with extreme caution |
| 249 | * @param workThread substitute Handler thread, for testing purposes only |
| 250 | */ |
| 251 | @VisibleForTesting |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 252 | // TODO(sghuman): Remove this method, this needs to happen in a factory method and be passed in |
| 253 | // during construction |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 254 | void setWorkThread(HandlerThread workThread) { |
| 255 | mWorkThread = workThread; |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 256 | mWorkHandler = new Handler(workThread.getLooper()); |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 257 | mScoreCache = new WifiNetworkScoreCache(mContext, new CacheListener(mWorkHandler) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 258 | @Override |
| 259 | public void networkCacheUpdated(List<ScoredNetwork> networks) { |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 260 | if (!mRegistered) return; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 261 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 262 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 263 | Log.v(TAG, "Score cache was updated with networks: " + networks); |
| 264 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 265 | updateNetworkScores(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 266 | } |
| 267 | }); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 270 | @Override |
| 271 | public void onDestroy() { |
| 272 | mWorkThread.quit(); |
| 273 | } |
| 274 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 275 | /** |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 276 | * Temporarily stop scanning for wifi networks. |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 277 | * |
| 278 | * <p>Sets {@link #mStaleScanResults} to true. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 279 | */ |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 280 | private void pauseScanning() { |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 281 | synchronized (mLock) { |
| 282 | if (mScanner != null) { |
| 283 | mScanner.pause(); |
| 284 | mScanner = null; |
| 285 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 286 | } |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 287 | mStaleScanResults = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Resume scanning for wifi networks after it has been paused. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 292 | * |
| 293 | * <p>The score cache should be registered before this method is invoked. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 294 | */ |
| 295 | public void resumeScanning() { |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 296 | synchronized (mLock) { |
| 297 | if (mScanner == null) { |
| 298 | mScanner = new Scanner(); |
| 299 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 300 | |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 301 | if (isWifiEnabled()) { |
| 302 | mScanner.resume(); |
| 303 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 304 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 308 | * Start tracking wifi networks and scores. |
| 309 | * |
| 310 | * <p>Registers listeners and starts scanning for wifi networks. If this is not called |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 311 | * then forceUpdate() must be called to populate getAccessPoints(). |
| 312 | */ |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 313 | @Override |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 314 | @MainThread |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 315 | public void onStart() { |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 316 | // fetch current ScanResults instead of waiting for broadcast of fresh results |
| 317 | forceUpdate(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 318 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 319 | registerScoreCache(); |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 320 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 321 | mNetworkScoringUiEnabled = |
| 322 | Settings.Global.getInt( |
| 323 | mContext.getContentResolver(), |
| 324 | Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 325 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 326 | mMaxSpeedLabelScoreCacheAge = |
| 327 | Settings.Global.getLong( |
| 328 | mContext.getContentResolver(), |
| 329 | Settings.Global.SPEED_LABEL_CACHE_EVICTION_AGE_MILLIS, |
| 330 | DEFAULT_MAX_CACHED_SCORE_AGE_MILLIS); |
Sundeep Ghuman | f342174 | 2018-02-08 11:18:42 -0800 | [diff] [blame] | 331 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 332 | resumeScanning(); |
| 333 | if (!mRegistered) { |
| 334 | mContext.registerReceiver(mReceiver, mFilter, null /* permission */, mWorkHandler); |
| 335 | // NetworkCallback objects cannot be reused. http://b/20701525 . |
| 336 | mNetworkCallback = new WifiTrackerNetworkCallback(); |
Erik Kline | 04ed430 | 2018-04-05 23:28:00 -0700 | [diff] [blame] | 337 | mConnectivityManager.registerNetworkCallback( |
| 338 | mNetworkRequest, mNetworkCallback, mWorkHandler); |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 339 | mRegistered = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 343 | |
| 344 | /** |
| 345 | * Synchronously update the list of access points with the latest information. |
| 346 | * |
| 347 | * <p>Intended to only be invoked within {@link #onStart()}. |
| 348 | */ |
| 349 | @MainThread |
jackqdyulei | 396e52d | 2018-09-19 17:28:27 -0700 | [diff] [blame] | 350 | @VisibleForTesting |
| 351 | void forceUpdate() { |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 352 | mLastInfo = mWifiManager.getConnectionInfo(); |
| 353 | mLastNetworkInfo = mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
| 354 | |
| 355 | fetchScansAndConfigsAndUpdateAccessPoints(); |
| 356 | } |
| 357 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 358 | private void registerScoreCache() { |
| 359 | mNetworkScoreManager.registerNetworkScoreCache( |
| 360 | NetworkKey.TYPE_WIFI, |
| 361 | mScoreCache, |
David Su | eea0921 | 2020-01-13 17:12:27 -0800 | [diff] [blame^] | 362 | NetworkScoreManager.SCORE_FILTER_SCAN_RESULTS); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | private void requestScoresForNetworkKeys(Collection<NetworkKey> keys) { |
| 366 | if (keys.isEmpty()) return; |
| 367 | |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 368 | if (DBG()) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 369 | Log.d(TAG, "Requesting scores for Network Keys: " + keys); |
| 370 | } |
| 371 | mNetworkScoreManager.requestScores(keys.toArray(new NetworkKey[keys.size()])); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 372 | synchronized (mLock) { |
| 373 | mRequestedScores.addAll(keys); |
| 374 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 377 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 378 | * Stop tracking wifi networks and scores. |
| 379 | * |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 380 | * <p>This should always be called when done with a WifiTracker (if onStart was called) to |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 381 | * ensure proper cleanup and prevent any further callbacks from occurring. |
| 382 | * |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 383 | * <p>Calling this method will set the {@link #mStaleScanResults} bit, which prevents |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 384 | * {@link WifiListener#onAccessPointsChanged()} callbacks from being invoked (until the bit |
| 385 | * is unset on the next SCAN_RESULTS_AVAILABLE_ACTION). |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 386 | */ |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 387 | @Override |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 388 | @MainThread |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 389 | public void onStop() { |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 390 | if (mRegistered) { |
| 391 | mContext.unregisterReceiver(mReceiver); |
| 392 | mConnectivityManager.unregisterNetworkCallback(mNetworkCallback); |
| 393 | mRegistered = false; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 394 | } |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 395 | unregisterScoreCache(); |
| 396 | pauseScanning(); // and set mStaleScanResults |
| 397 | |
| 398 | mWorkHandler.removeCallbacksAndMessages(null /* remove all */); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Sundeep Ghuman | 4bb8411 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 401 | private void unregisterScoreCache() { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 402 | mNetworkScoreManager.unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, mScoreCache); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 403 | |
Sundeep Ghuman | 4bb8411 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 404 | // We do not want to clear the existing scores in the cache, as this method is called during |
| 405 | // stop tracking on activity pause. Hence, on resumption we want the ability to show the |
| 406 | // last known, potentially stale, scores. However, by clearing requested scores, the scores |
| 407 | // will be requested again upon resumption of tracking, and if any changes have occurred |
| 408 | // the listeners (UI) will be updated accordingly. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 409 | synchronized (mLock) { |
| 410 | mRequestedScores.clear(); |
| 411 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 415 | * Gets the current list of access points. |
| 416 | * |
| 417 | * <p>This method is can be called on an abitrary thread by clients, but is normally called on |
| 418 | * the UI Thread by the rendering App. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 419 | */ |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 420 | @AnyThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 421 | public List<AccessPoint> getAccessPoints() { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 422 | synchronized (mLock) { |
| 423 | return new ArrayList<>(mInternalAccessPoints); |
| 424 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | public WifiManager getManager() { |
| 428 | return mWifiManager; |
| 429 | } |
| 430 | |
| 431 | public boolean isWifiEnabled() { |
Amin Shaikh | bc38add | 2018-12-26 15:31:45 -0500 | [diff] [blame] | 432 | return mWifiManager != null && mWifiManager.isWifiEnabled(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 436 | * Returns the number of saved networks on the device, regardless of whether the WifiTracker |
| 437 | * is tracking saved networks. |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 438 | * TODO(b/62292448): remove this function and update callsites to use WifiSavedConfigUtils |
| 439 | * directly. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 440 | */ |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 441 | public int getNumSavedNetworks() { |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 442 | return WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | public boolean isConnected() { |
| 446 | return mConnected.get(); |
| 447 | } |
| 448 | |
| 449 | public void dump(PrintWriter pw) { |
| 450 | pw.println(" - wifi tracker ------"); |
Jason Monk | 1904081 | 2015-06-01 15:00:57 -0400 | [diff] [blame] | 451 | for (AccessPoint accessPoint : getAccessPoints()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 452 | pw.println(" " + accessPoint); |
| 453 | } |
| 454 | } |
| 455 | |
Sundeep Ghuman | cf114b6 | 2018-01-29 20:07:16 -0800 | [diff] [blame] | 456 | private ArrayMap<String, List<ScanResult>> updateScanResultCache( |
| 457 | final List<ScanResult> newResults) { |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 458 | // TODO(sghuman): Delete this and replace it with the Map of Ap Keys to ScanResults for |
| 459 | // memory efficiency |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 460 | for (ScanResult newResult : newResults) { |
Mitchell Wills | 9df7904 | 2015-09-01 12:21:59 -0700 | [diff] [blame] | 461 | if (newResult.SSID == null || newResult.SSID.isEmpty()) { |
| 462 | continue; |
| 463 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 464 | mScanResultCache.put(newResult.BSSID, newResult); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 465 | } |
| 466 | |
jackqdyulei | 396e52d | 2018-09-19 17:28:27 -0700 | [diff] [blame] | 467 | // Evict old results in all conditions |
| 468 | evictOldScans(); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 469 | |
Sundeep Ghuman | cf114b6 | 2018-01-29 20:07:16 -0800 | [diff] [blame] | 470 | ArrayMap<String, List<ScanResult>> scanResultsByApKey = new ArrayMap<>(); |
| 471 | for (ScanResult result : mScanResultCache.values()) { |
| 472 | // Ignore hidden and ad-hoc networks. |
| 473 | if (result.SSID == null || result.SSID.length() == 0 || |
| 474 | result.capabilities.contains("[IBSS]")) { |
| 475 | continue; |
| 476 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 477 | |
Sundeep Ghuman | cf114b6 | 2018-01-29 20:07:16 -0800 | [diff] [blame] | 478 | String apKey = AccessPoint.getKey(result); |
| 479 | List<ScanResult> resultList; |
| 480 | if (scanResultsByApKey.containsKey(apKey)) { |
| 481 | resultList = scanResultsByApKey.get(apKey); |
| 482 | } else { |
| 483 | resultList = new ArrayList<>(); |
| 484 | scanResultsByApKey.put(apKey, resultList); |
| 485 | } |
| 486 | |
| 487 | resultList.add(result); |
| 488 | } |
| 489 | |
| 490 | return scanResultsByApKey; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 493 | /** |
Quang Luong | 85bbc6e | 2019-03-28 15:35:22 -0700 | [diff] [blame] | 494 | * Remove old scan results from the cache. If {@link #mLastScanSucceeded} is false, then |
| 495 | * increase the timeout window to avoid completely flushing the AP list before the next |
| 496 | * successful scan completes. |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 497 | * |
| 498 | * <p>Should only ever be invoked from {@link #updateScanResultCache(List)} when |
| 499 | * {@link #mStaleScanResults} is false. |
| 500 | */ |
| 501 | private void evictOldScans() { |
Quang Luong | 85bbc6e | 2019-03-28 15:35:22 -0700 | [diff] [blame] | 502 | long evictionTimeoutMillis = mLastScanSucceeded ? MAX_SCAN_RESULT_AGE_MILLIS |
| 503 | : MAX_SCAN_RESULT_AGE_MILLIS * 2; |
| 504 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 505 | long nowMs = SystemClock.elapsedRealtime(); |
| 506 | for (Iterator<ScanResult> iter = mScanResultCache.values().iterator(); iter.hasNext(); ) { |
| 507 | ScanResult result = iter.next(); |
| 508 | // result timestamp is in microseconds |
Quang Luong | 85bbc6e | 2019-03-28 15:35:22 -0700 | [diff] [blame] | 509 | if (nowMs - result.timestamp / 1000 > evictionTimeoutMillis) { |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 510 | iter.remove(); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 515 | private WifiConfiguration getWifiConfigurationForNetworkId( |
| 516 | int networkId, final List<WifiConfiguration> configs) { |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 517 | if (configs != null) { |
| 518 | for (WifiConfiguration config : configs) { |
| 519 | if (mLastInfo != null && networkId == config.networkId && |
| 520 | !(config.selfAdded && config.numAssociation == 0)) { |
| 521 | return config; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | return null; |
| 526 | } |
| 527 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 528 | /** |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 529 | * Retrieves latest scan results and wifi configs, then calls |
| 530 | * {@link #updateAccessPoints(List, List)}. |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 531 | */ |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 532 | private void fetchScansAndConfigsAndUpdateAccessPoints() { |
Hai Shalom | e3cc068 | 2018-11-08 14:21:58 -0800 | [diff] [blame] | 533 | List<ScanResult> newScanResults = mWifiManager.getScanResults(); |
| 534 | |
| 535 | // Filter all unsupported networks from the scan result list |
| 536 | final List<ScanResult> filteredScanResults = |
| 537 | filterScanResultsByCapabilities(newScanResults); |
| 538 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 539 | if (isVerboseLoggingEnabled()) { |
Hai Shalom | e3cc068 | 2018-11-08 14:21:58 -0800 | [diff] [blame] | 540 | Log.i(TAG, "Fetched scan results: " + filteredScanResults); |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 541 | } |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 542 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 543 | List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
Hai Shalom | e3cc068 | 2018-11-08 14:21:58 -0800 | [diff] [blame] | 544 | updateAccessPoints(filteredScanResults, configs); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 545 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 546 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 547 | /** Update the internal list of access points. */ |
| 548 | private void updateAccessPoints(final List<ScanResult> newScanResults, |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 549 | List<WifiConfiguration> configs) { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 550 | |
Sundeep Ghuman | cf114b6 | 2018-01-29 20:07:16 -0800 | [diff] [blame] | 551 | // Map configs and scan results necessary to make AccessPoints |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 552 | final Map<String, WifiConfiguration> configsByKey = new ArrayMap(configs.size()); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 553 | if (configs != null) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 554 | for (WifiConfiguration config : configs) { |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 555 | configsByKey.put(AccessPoint.getKey(config), config); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 556 | } |
| 557 | } |
Sundeep Ghuman | cf114b6 | 2018-01-29 20:07:16 -0800 | [diff] [blame] | 558 | |
| 559 | WifiConfiguration connectionConfig = null; |
| 560 | if (mLastInfo != null) { |
Sundeep Ghuman | 06ec3be | 2018-02-20 19:51:15 -0800 | [diff] [blame] | 561 | connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId(), configs); |
Sundeep Ghuman | cf114b6 | 2018-01-29 20:07:16 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 564 | // Rather than dropping and reacquiring the lock multiple times in this method, we lock |
| 565 | // once for efficiency of lock acquisition time and readability |
| 566 | synchronized (mLock) { |
Quang Luong | af15d3d | 2019-05-06 16:39:08 -0700 | [diff] [blame] | 567 | ArrayMap<String, List<ScanResult>> scanResultsByApKey = |
| 568 | updateScanResultCache(newScanResults); |
| 569 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 570 | // Swap the current access points into a cached list for maintaining AP listeners |
| 571 | List<AccessPoint> cachedAccessPoints; |
| 572 | cachedAccessPoints = new ArrayList<>(mInternalAccessPoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 573 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 574 | ArrayList<AccessPoint> accessPoints = new ArrayList<>(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 575 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 576 | final List<NetworkKey> scoresToRequest = new ArrayList<>(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 577 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 578 | for (Map.Entry<String, List<ScanResult>> entry : scanResultsByApKey.entrySet()) { |
| 579 | for (ScanResult result : entry.getValue()) { |
| 580 | NetworkKey key = NetworkKey.createFromScanResult(result); |
| 581 | if (key != null && !mRequestedScores.contains(key)) { |
| 582 | scoresToRequest.add(key); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 583 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 584 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 585 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 586 | AccessPoint accessPoint = |
| 587 | getCachedOrCreate(entry.getValue(), cachedAccessPoints); |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 588 | |
| 589 | // Update the matching config if there is one, to populate saved network info |
| 590 | accessPoint.update(configsByKey.get(entry.getKey())); |
| 591 | |
| 592 | accessPoints.add(accessPoint); |
| 593 | } |
| 594 | |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 595 | List<ScanResult> cachedScanResults = new ArrayList<>(mScanResultCache.values()); |
| 596 | |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 597 | // Add a unique Passpoint AccessPoint for each Passpoint profile's FQDN. |
| 598 | accessPoints.addAll(updatePasspointAccessPoints( |
| 599 | mWifiManager.getAllMatchingWifiConfigs(cachedScanResults), cachedAccessPoints)); |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 600 | |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 601 | // Add OSU Provider AccessPoints |
| 602 | accessPoints.addAll(updateOsuAccessPoints( |
| 603 | mWifiManager.getMatchingOsuProviders(cachedScanResults), cachedAccessPoints)); |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 604 | |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 605 | if (mLastInfo != null && mLastNetworkInfo != null) { |
| 606 | for (AccessPoint ap : accessPoints) { |
| 607 | ap.update(connectionConfig, mLastInfo, mLastNetworkInfo); |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Sundeep Ghuman | 06ec3be | 2018-02-20 19:51:15 -0800 | [diff] [blame] | 611 | // If there were no scan results, create an AP for the currently connected network (if |
| 612 | // it exists). |
Sundeep Ghuman | 06ec3be | 2018-02-20 19:51:15 -0800 | [diff] [blame] | 613 | if (accessPoints.isEmpty() && connectionConfig != null) { |
| 614 | AccessPoint activeAp = new AccessPoint(mContext, connectionConfig); |
| 615 | activeAp.update(connectionConfig, mLastInfo, mLastNetworkInfo); |
| 616 | accessPoints.add(activeAp); |
| 617 | scoresToRequest.add(NetworkKey.createFromWifiInfo(mLastInfo)); |
| 618 | } |
| 619 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 620 | requestScoresForNetworkKeys(scoresToRequest); |
| 621 | for (AccessPoint ap : accessPoints) { |
| 622 | ap.update(mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge); |
| 623 | } |
| 624 | |
| 625 | // Pre-sort accessPoints to speed preference insertion |
| 626 | Collections.sort(accessPoints); |
| 627 | |
| 628 | // Log accesspoints that are being removed |
| 629 | if (DBG()) { |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 630 | Log.d(TAG, |
| 631 | "------ Dumping AccessPoints that were not seen on this scan ------"); |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 632 | for (AccessPoint prevAccessPoint : mInternalAccessPoints) { |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 633 | String prevTitle = prevAccessPoint.getTitle(); |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 634 | boolean found = false; |
| 635 | for (AccessPoint newAccessPoint : accessPoints) { |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 636 | if (newAccessPoint.getTitle() != null && newAccessPoint.getTitle() |
| 637 | .equals(prevTitle)) { |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 638 | found = true; |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | if (!found) |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 643 | Log.d(TAG, "Did not find " + prevTitle + " in this scan"); |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 644 | } |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 645 | Log.d(TAG, |
| 646 | "---- Done dumping AccessPoints that were not seen on this scan ----"); |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | mInternalAccessPoints.clear(); |
| 650 | mInternalAccessPoints.addAll(accessPoints); |
| 651 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 652 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 653 | conditionallyNotifyListeners(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 654 | } |
| 655 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 656 | @VisibleForTesting |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 657 | List<AccessPoint> updatePasspointAccessPoints( |
| 658 | List<Pair<WifiConfiguration, Map<Integer, List<ScanResult>>>> passpointConfigsAndScans, |
| 659 | List<AccessPoint> accessPointCache) { |
| 660 | List<AccessPoint> accessPoints = new ArrayList<>(); |
| 661 | |
| 662 | Set<String> seenFQDNs = new ArraySet<>(); |
| 663 | for (Pair<WifiConfiguration, |
| 664 | Map<Integer, List<ScanResult>>> pairing : passpointConfigsAndScans) { |
| 665 | WifiConfiguration config = pairing.first; |
| 666 | if (seenFQDNs.add(config.FQDN)) { |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 667 | List<ScanResult> homeScans = |
| 668 | pairing.second.get(WifiManager.PASSPOINT_HOME_NETWORK); |
| 669 | List<ScanResult> roamingScans = |
| 670 | pairing.second.get(WifiManager.PASSPOINT_ROAMING_NETWORK); |
| 671 | |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 672 | AccessPoint accessPoint = |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 673 | getCachedOrCreatePasspoint(config, homeScans, roamingScans, |
| 674 | accessPointCache); |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 675 | accessPoints.add(accessPoint); |
| 676 | } |
| 677 | } |
| 678 | return accessPoints; |
| 679 | } |
| 680 | |
| 681 | @VisibleForTesting |
| 682 | List<AccessPoint> updateOsuAccessPoints( |
| 683 | Map<OsuProvider, List<ScanResult>> providersAndScans, |
| 684 | List<AccessPoint> accessPointCache) { |
| 685 | List<AccessPoint> accessPoints = new ArrayList<>(); |
| 686 | |
| 687 | Set<OsuProvider> alreadyProvisioned = mWifiManager |
| 688 | .getMatchingPasspointConfigsForOsuProviders( |
| 689 | providersAndScans.keySet()).keySet(); |
| 690 | for (OsuProvider provider : providersAndScans.keySet()) { |
| 691 | if (!alreadyProvisioned.contains(provider)) { |
| 692 | AccessPoint accessPointOsu = |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 693 | getCachedOrCreateOsu(provider, providersAndScans.get(provider), |
| 694 | accessPointCache); |
Quang Luong | da49e82 | 2019-01-15 14:48:25 -0800 | [diff] [blame] | 695 | accessPoints.add(accessPointOsu); |
| 696 | } |
| 697 | } |
| 698 | return accessPoints; |
| 699 | } |
| 700 | |
| 701 | private AccessPoint getCachedOrCreate( |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 702 | List<ScanResult> scanResults, |
| 703 | List<AccessPoint> cache) { |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 704 | AccessPoint accessPoint = getCachedByKey(cache, AccessPoint.getKey(scanResults.get(0))); |
| 705 | if (accessPoint == null) { |
| 706 | accessPoint = new AccessPoint(mContext, scanResults); |
| 707 | } else { |
| 708 | accessPoint.setScanResults(scanResults); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 709 | } |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 710 | return accessPoint; |
| 711 | } |
| 712 | |
| 713 | private AccessPoint getCachedOrCreatePasspoint( |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 714 | WifiConfiguration config, |
| 715 | List<ScanResult> homeScans, |
| 716 | List<ScanResult> roamingScans, |
| 717 | List<AccessPoint> cache) { |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 718 | AccessPoint accessPoint = getCachedByKey(cache, AccessPoint.getKey(config)); |
| 719 | if (accessPoint == null) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 720 | accessPoint = new AccessPoint(mContext, config, homeScans, roamingScans); |
| 721 | } else { |
Quang Luong | e747f59 | 2019-05-02 11:22:33 -0700 | [diff] [blame] | 722 | accessPoint.update(config); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 723 | accessPoint.setScanResultsPasspoint(homeScans, roamingScans); |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 724 | } |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 725 | return accessPoint; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 726 | } |
| 727 | |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 728 | private AccessPoint getCachedOrCreateOsu( |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 729 | OsuProvider provider, |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 730 | List<ScanResult> scanResults, |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 731 | List<AccessPoint> cache) { |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 732 | AccessPoint accessPoint = getCachedByKey(cache, AccessPoint.getKey(provider)); |
| 733 | if (accessPoint == null) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 734 | accessPoint = new AccessPoint(mContext, provider, scanResults); |
| 735 | } else { |
| 736 | accessPoint.setScanResults(scanResults); |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 737 | } |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 738 | return accessPoint; |
| 739 | } |
| 740 | |
| 741 | private AccessPoint getCachedByKey(List<AccessPoint> cache, String key) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 742 | ListIterator<AccessPoint> lit = cache.listIterator(); |
| 743 | while (lit.hasNext()) { |
Quang Luong | 550f7d4 | 2019-01-09 17:07:19 -0800 | [diff] [blame] | 744 | AccessPoint currentAccessPoint = lit.next(); |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 745 | if (currentAccessPoint.getKey().equals(key)) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 746 | lit.remove(); |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 747 | return currentAccessPoint; |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 748 | } |
| 749 | } |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 750 | return null; |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 753 | private void updateNetworkInfo(NetworkInfo networkInfo) { |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 754 | /* Sticky broadcasts can call this when wifi is disabled */ |
Amin Shaikh | bc38add | 2018-12-26 15:31:45 -0500 | [diff] [blame] | 755 | if (!isWifiEnabled()) { |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 756 | clearAccessPointsAndConditionallyUpdate(); |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 757 | return; |
| 758 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 759 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 760 | if (networkInfo != null) { |
| 761 | mLastNetworkInfo = networkInfo; |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 762 | if (DBG()) { |
| 763 | Log.d(TAG, "mLastNetworkInfo set: " + mLastNetworkInfo); |
| 764 | } |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 765 | |
| 766 | if(networkInfo.isConnected() != mConnected.getAndSet(networkInfo.isConnected())) { |
| 767 | mListener.onConnectedChanged(); |
| 768 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 769 | } |
| 770 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 771 | WifiConfiguration connectionConfig = null; |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 772 | |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 773 | mLastInfo = mWifiManager.getConnectionInfo(); |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 774 | if (DBG()) { |
| 775 | Log.d(TAG, "mLastInfo set as: " + mLastInfo); |
| 776 | } |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 777 | if (mLastInfo != null) { |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 778 | connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId(), |
| 779 | mWifiManager.getConfiguredNetworks()); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 782 | boolean updated = false; |
| 783 | boolean reorder = false; // Only reorder if connected AP was changed |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 784 | |
| 785 | synchronized (mLock) { |
| 786 | for (int i = mInternalAccessPoints.size() - 1; i >= 0; --i) { |
| 787 | AccessPoint ap = mInternalAccessPoints.get(i); |
| 788 | boolean previouslyConnected = ap.isActive(); |
| 789 | if (ap.update(connectionConfig, mLastInfo, mLastNetworkInfo)) { |
| 790 | updated = true; |
| 791 | if (previouslyConnected != ap.isActive()) reorder = true; |
| 792 | } |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 793 | if (ap.update(mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge)) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 794 | reorder = true; |
| 795 | updated = true; |
| 796 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 797 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 798 | |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 799 | if (reorder) { |
| 800 | Collections.sort(mInternalAccessPoints); |
| 801 | } |
| 802 | if (updated) { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 803 | conditionallyNotifyListeners(); |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 804 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 805 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 808 | /** |
| 809 | * Clears the access point list and conditionally invokes |
| 810 | * {@link WifiListener#onAccessPointsChanged()} if required (i.e. the list was not already |
| 811 | * empty). |
| 812 | */ |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 813 | private void clearAccessPointsAndConditionallyUpdate() { |
| 814 | synchronized (mLock) { |
| 815 | if (!mInternalAccessPoints.isEmpty()) { |
| 816 | mInternalAccessPoints.clear(); |
Sundeep Ghuman | 062bf5a | 2018-02-21 14:17:57 -0800 | [diff] [blame] | 817 | conditionallyNotifyListeners(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 822 | /** |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 823 | * Update all the internal access points rankingScores, badge and metering. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 824 | * |
| 825 | * <p>Will trigger a resort and notify listeners of changes if applicable. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 826 | * |
| 827 | * <p>Synchronized on {@link #mLock}. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 828 | */ |
| 829 | private void updateNetworkScores() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 830 | synchronized (mLock) { |
| 831 | boolean updated = false; |
| 832 | for (int i = 0; i < mInternalAccessPoints.size(); i++) { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 833 | if (mInternalAccessPoints.get(i).update( |
| 834 | mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge)) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 835 | updated = true; |
| 836 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 837 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 838 | if (updated) { |
| 839 | Collections.sort(mInternalAccessPoints); |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 840 | conditionallyNotifyListeners(); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 841 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 845 | /** |
| 846 | * Receiver for handling broadcasts. |
| 847 | * |
| 848 | * This receiver is registered on the WorkHandler. |
| 849 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 850 | @VisibleForTesting |
| 851 | final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 852 | @Override |
| 853 | public void onReceive(Context context, Intent intent) { |
| 854 | String action = intent.getAction(); |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 855 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 856 | if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { |
| 857 | updateWifiState( |
| 858 | intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, |
| 859 | WifiManager.WIFI_STATE_UNKNOWN)); |
| 860 | } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) { |
| 861 | mStaleScanResults = false; |
Quang Luong | 85bbc6e | 2019-03-28 15:35:22 -0700 | [diff] [blame] | 862 | mLastScanSucceeded = |
| 863 | intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, true); |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 864 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 865 | fetchScansAndConfigsAndUpdateAccessPoints(); |
| 866 | } else if (WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) |
| 867 | || WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) { |
| 868 | fetchScansAndConfigsAndUpdateAccessPoints(); |
| 869 | } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { |
| 870 | // TODO(sghuman): Refactor these methods so they cannot result in duplicate |
| 871 | // onAccessPointsChanged updates being called from this intent. |
| 872 | NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); |
| 873 | updateNetworkInfo(info); |
| 874 | fetchScansAndConfigsAndUpdateAccessPoints(); |
| 875 | } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) { |
| 876 | NetworkInfo info = |
| 877 | mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
| 878 | updateNetworkInfo(info); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | }; |
| 882 | |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 883 | /** |
| 884 | * Handles updates to WifiState. |
| 885 | * |
| 886 | * <p>If Wifi is not enabled in the enabled state, {@link #mStaleScanResults} will be set to |
| 887 | * true. |
| 888 | */ |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 889 | private void updateWifiState(int state) { |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 890 | if (isVerboseLoggingEnabled()) { |
| 891 | Log.d(TAG, "updateWifiState: " + state); |
| 892 | } |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 893 | if (state == WifiManager.WIFI_STATE_ENABLED) { |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 894 | synchronized (mLock) { |
| 895 | if (mScanner != null) { |
| 896 | // We only need to resume if mScanner isn't null because |
| 897 | // that means we want to be scanning. |
| 898 | mScanner.resume(); |
| 899 | } |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 900 | } |
| 901 | } else { |
| 902 | clearAccessPointsAndConditionallyUpdate(); |
| 903 | mLastInfo = null; |
| 904 | mLastNetworkInfo = null; |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 905 | synchronized (mLock) { |
| 906 | if (mScanner != null) { |
| 907 | mScanner.pause(); |
| 908 | } |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 909 | } |
Sundeep Ghuman | 5886a94 | 2018-02-06 19:58:09 -0800 | [diff] [blame] | 910 | mStaleScanResults = true; |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 911 | } |
| 912 | mListener.onWifiStateChanged(state); |
| 913 | } |
| 914 | |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 915 | private final class WifiTrackerNetworkCallback extends ConnectivityManager.NetworkCallback { |
| 916 | public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { |
| 917 | if (network.equals(mWifiManager.getCurrentNetwork())) { |
Sundeep Ghuman | bcb5373 | 2018-01-31 12:42:16 -0800 | [diff] [blame] | 918 | // TODO(sghuman): Investigate whether this comment still holds true and if it makes |
| 919 | // more sense fetch the latest network info here: |
| 920 | |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 921 | // We don't send a NetworkInfo object along with this message, because even if we |
| 922 | // fetch one from ConnectivityManager, it might be older than the most recent |
| 923 | // NetworkInfo message we got via a WIFI_STATE_CHANGED broadcast. |
Erik Kline | 04ed430 | 2018-04-05 23:28:00 -0700 | [diff] [blame] | 924 | updateNetworkInfo(null); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 929 | @VisibleForTesting |
| 930 | class Scanner extends Handler { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 931 | static final int MSG_SCAN = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 932 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 933 | private int mRetry = 0; |
| 934 | |
| 935 | void resume() { |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 936 | if (isVerboseLoggingEnabled()) { |
| 937 | Log.d(TAG, "Scanner resume"); |
| 938 | } |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 939 | if (!hasMessages(MSG_SCAN)) { |
| 940 | sendEmptyMessage(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 944 | void pause() { |
cosmohsieh | 9c227d9 | 2019-04-29 14:22:51 +0800 | [diff] [blame] | 945 | if (isVerboseLoggingEnabled()) { |
| 946 | Log.d(TAG, "Scanner pause"); |
| 947 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 948 | mRetry = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 949 | removeMessages(MSG_SCAN); |
| 950 | } |
| 951 | |
| 952 | @VisibleForTesting |
| 953 | boolean isScanning() { |
| 954 | return hasMessages(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | @Override |
| 958 | public void handleMessage(Message message) { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 959 | if (message.what != MSG_SCAN) return; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 960 | if (mWifiManager.startScan()) { |
| 961 | mRetry = 0; |
| 962 | } else if (++mRetry >= 3) { |
| 963 | mRetry = 0; |
| 964 | if (mContext != null) { |
| 965 | Toast.makeText(mContext, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show(); |
| 966 | } |
| 967 | return; |
| 968 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 969 | sendEmptyMessageDelayed(MSG_SCAN, WIFI_RESCAN_INTERVAL_MS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | /** A restricted multimap for use in constructAccessPoints */ |
| 974 | private static class Multimap<K,V> { |
| 975 | private final HashMap<K,List<V>> store = new HashMap<K,List<V>>(); |
| 976 | /** retrieve a non-null list of values with key K */ |
| 977 | List<V> getAll(K key) { |
| 978 | List<V> values = store.get(key); |
| 979 | return values != null ? values : Collections.<V>emptyList(); |
| 980 | } |
| 981 | |
| 982 | void put(K key, V val) { |
| 983 | List<V> curVals = store.get(key); |
| 984 | if (curVals == null) { |
| 985 | curVals = new ArrayList<V>(3); |
| 986 | store.put(key, curVals); |
| 987 | } |
| 988 | curVals.add(val); |
| 989 | } |
| 990 | } |
| 991 | |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 992 | /** |
Sundeep Ghuman | dc67f4d | 2018-01-31 16:03:33 -0800 | [diff] [blame] | 993 | * Wraps the given {@link WifiListener} instance and executes its methods on the Main Thread. |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 994 | * |
Sundeep Ghuman | dc67f4d | 2018-01-31 16:03:33 -0800 | [diff] [blame] | 995 | * <p>Also logs all callbacks invocations when verbose logging is enabled. |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 996 | */ |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 997 | @VisibleForTesting class WifiListenerExecutor implements WifiListener { |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 998 | |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 999 | private final WifiListener mDelegatee; |
| 1000 | |
Sundeep Ghuman | dc67f4d | 2018-01-31 16:03:33 -0800 | [diff] [blame] | 1001 | public WifiListenerExecutor(WifiListener listener) { |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 1002 | mDelegatee = listener; |
| 1003 | } |
| 1004 | |
| 1005 | @Override |
| 1006 | public void onWifiStateChanged(int state) { |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 1007 | runAndLog(() -> mDelegatee.onWifiStateChanged(state), |
| 1008 | String.format("Invoking onWifiStateChanged callback with state %d", state)); |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | @Override |
| 1012 | public void onConnectedChanged() { |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 1013 | runAndLog(mDelegatee::onConnectedChanged, "Invoking onConnectedChanged callback"); |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | @Override |
| 1017 | public void onAccessPointsChanged() { |
Sundeep Ghuman | 93d591b | 2018-03-19 14:21:56 -0700 | [diff] [blame] | 1018 | runAndLog(mDelegatee::onAccessPointsChanged, "Invoking onAccessPointsChanged callback"); |
| 1019 | } |
| 1020 | |
| 1021 | private void runAndLog(Runnable r, String verboseLog) { |
| 1022 | ThreadUtils.postOnMainThread(() -> { |
| 1023 | if (mRegistered) { |
| 1024 | if (isVerboseLoggingEnabled()) { |
| 1025 | Log.i(TAG, verboseLog); |
| 1026 | } |
| 1027 | r.run(); |
| 1028 | } |
| 1029 | }); |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Sundeep Ghuman | dc67f4d | 2018-01-31 16:03:33 -0800 | [diff] [blame] | 1033 | /** |
| 1034 | * WifiListener interface that defines callbacks indicating state changes in WifiTracker. |
| 1035 | * |
| 1036 | * <p>All callbacks are invoked on the MainThread. |
| 1037 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1038 | public interface WifiListener { |
| 1039 | /** |
| 1040 | * Called when the state of Wifi has changed, the state will be one of |
| 1041 | * the following. |
| 1042 | * |
| 1043 | * <li>{@link WifiManager#WIFI_STATE_DISABLED}</li> |
| 1044 | * <li>{@link WifiManager#WIFI_STATE_ENABLED}</li> |
| 1045 | * <li>{@link WifiManager#WIFI_STATE_DISABLING}</li> |
| 1046 | * <li>{@link WifiManager#WIFI_STATE_ENABLING}</li> |
| 1047 | * <li>{@link WifiManager#WIFI_STATE_UNKNOWN}</li> |
| 1048 | * <p> |
| 1049 | * |
| 1050 | * @param state The new state of wifi. |
| 1051 | */ |
| 1052 | void onWifiStateChanged(int state); |
| 1053 | |
| 1054 | /** |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1055 | * Called when the connection state of wifi has changed and |
| 1056 | * {@link WifiTracker#isConnected()} should be called to get the updated state. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1057 | */ |
| 1058 | void onConnectedChanged(); |
| 1059 | |
| 1060 | /** |
| 1061 | * Called to indicate the list of AccessPoints has been updated and |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1062 | * {@link WifiTracker#getAccessPoints()} should be called to get the updated list. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1063 | */ |
| 1064 | void onAccessPointsChanged(); |
| 1065 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1066 | |
| 1067 | /** |
Sundeep Ghuman | 062bf5a | 2018-02-21 14:17:57 -0800 | [diff] [blame] | 1068 | * Invokes {@link WifiListenerExecutor#onAccessPointsChanged()} iif {@link #mStaleScanResults} |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1069 | * is false. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1070 | */ |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1071 | private void conditionallyNotifyListeners() { |
| 1072 | if (mStaleScanResults) { |
| 1073 | return; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Sundeep Ghuman | 062bf5a | 2018-02-21 14:17:57 -0800 | [diff] [blame] | 1076 | mListener.onAccessPointsChanged(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1077 | } |
Hai Shalom | e3cc068 | 2018-11-08 14:21:58 -0800 | [diff] [blame] | 1078 | |
| 1079 | /** |
| 1080 | * Filters unsupported networks from scan results. New WPA3 networks and OWE networks |
| 1081 | * may not be compatible with the device HW/SW. |
| 1082 | * @param scanResults List of scan results |
| 1083 | * @return List of filtered scan results based on local device capabilities |
| 1084 | */ |
| 1085 | private List<ScanResult> filterScanResultsByCapabilities(List<ScanResult> scanResults) { |
| 1086 | if (scanResults == null) { |
| 1087 | return null; |
| 1088 | } |
| 1089 | |
| 1090 | // Get and cache advanced capabilities |
Hai Shalom | 68cf696 | 2019-02-28 12:40:40 -0800 | [diff] [blame] | 1091 | final boolean isOweSupported = mWifiManager.isEnhancedOpenSupported(); |
Hai Shalom | e3cc068 | 2018-11-08 14:21:58 -0800 | [diff] [blame] | 1092 | final boolean isSaeSupported = mWifiManager.isWpa3SaeSupported(); |
| 1093 | final boolean isSuiteBSupported = mWifiManager.isWpa3SuiteBSupported(); |
| 1094 | |
| 1095 | List<ScanResult> filteredScanResultList = new ArrayList<>(); |
| 1096 | |
| 1097 | // Iterate through the list of scan results and filter out APs which are not |
| 1098 | // compatible with our device. |
| 1099 | for (ScanResult scanResult : scanResults) { |
| 1100 | if (scanResult.capabilities.contains(WIFI_SECURITY_PSK)) { |
| 1101 | // All devices (today) support RSN-PSK or WPA-PSK |
| 1102 | // Add this here because some APs may support both PSK and SAE and the check |
| 1103 | // below will filter it out. |
| 1104 | filteredScanResultList.add(scanResult); |
| 1105 | continue; |
| 1106 | } |
| 1107 | |
| 1108 | if ((scanResult.capabilities.contains(WIFI_SECURITY_SUITE_B_192) && !isSuiteBSupported) |
| 1109 | || (scanResult.capabilities.contains(WIFI_SECURITY_SAE) && !isSaeSupported) |
| 1110 | || (scanResult.capabilities.contains(WIFI_SECURITY_OWE) && !isOweSupported)) { |
| 1111 | if (isVerboseLoggingEnabled()) { |
| 1112 | Log.v(TAG, "filterScanResultsByCapabilities: Filtering SSID " |
| 1113 | + scanResult.SSID + " with capabilities: " + scanResult.capabilities); |
| 1114 | } |
| 1115 | } else { |
| 1116 | // Safe to add |
| 1117 | filteredScanResultList.add(scanResult); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | return filteredScanResultList; |
| 1122 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1123 | } |