blob: f8e2f8bbb1468a4a51f855cbe97febfb4190f1dd [file] [log] [blame]
Jason Monkd52356a2015-01-28 10:40:41 -05001/*
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 */
16package com.android.settingslib.wifi;
17
Sundeep Ghumance6bab82017-04-19 16:21:46 -070018import android.annotation.MainThread;
Jason Monkd52356a2015-01-28 10:40:41 -050019import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Sundeep Ghumane869d832017-01-25 16:23:43 -080023import android.database.ContentObserver;
Lorenzo Colittiab313f82015-11-26 16:55:15 +090024import android.net.ConnectivityManager;
25import android.net.Network;
26import android.net.NetworkCapabilities;
Jason Monkd52356a2015-01-28 10:40:41 -050027import android.net.NetworkInfo;
28import android.net.NetworkInfo.DetailedState;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080029import android.net.NetworkKey;
Lorenzo Colittiab313f82015-11-26 16:55:15 +090030import android.net.NetworkRequest;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080031import android.net.NetworkScoreManager;
32import android.net.ScoredNetwork;
Jason Monkd52356a2015-01-28 10:40:41 -050033import android.net.wifi.ScanResult;
34import android.net.wifi.WifiConfiguration;
35import android.net.wifi.WifiInfo;
36import android.net.wifi.WifiManager;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080037import android.net.wifi.WifiNetworkScoreCache;
38import android.net.wifi.WifiNetworkScoreCache.CacheListener;
Jason Monkd52356a2015-01-28 10:40:41 -050039import android.os.Handler;
Jason Monk30d80042015-05-08 16:54:18 -040040import android.os.Looper;
Jason Monkd52356a2015-01-28 10:40:41 -050041import android.os.Message;
Sundeep Ghumane869d832017-01-25 16:23:43 -080042import android.provider.Settings;
Sundeep Ghumance6bab82017-04-19 16:21:46 -070043import android.support.annotation.GuardedBy;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080044import android.util.ArraySet;
Vinit Deshpandefcd46122015-06-11 18:22:23 -070045import android.util.Log;
Ajay Nadathurd7b689a2016-08-31 15:07:56 -070046import android.util.SparseArray;
47import android.util.SparseIntArray;
Jason Monkd52356a2015-01-28 10:40:41 -050048import android.widget.Toast;
49
50import com.android.internal.annotations.VisibleForTesting;
51import com.android.settingslib.R;
52
53import java.io.PrintWriter;
54import java.util.ArrayList;
Vinit Deshpandefcd46122015-06-11 18:22:23 -070055import java.util.Collection;
Jason Monkd52356a2015-01-28 10:40:41 -050056import java.util.Collections;
57import java.util.HashMap;
Vinit Deshpandefcd46122015-06-11 18:22:23 -070058import java.util.Iterator;
Jason Monkd52356a2015-01-28 10:40:41 -050059import java.util.List;
Vinit Deshpandefcd46122015-06-11 18:22:23 -070060import java.util.Map;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080061import java.util.Set;
Jason Monkd52356a2015-01-28 10:40:41 -050062import java.util.concurrent.atomic.AtomicBoolean;
63
64/**
65 * Tracks saved or available wifi networks and their state.
66 */
67public class WifiTracker {
Peter Qiuced37db2017-03-14 15:51:22 -070068 // TODO(b/36733768): Remove flag includeSaved and includePasspoints.
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080069
Jason Monkd52356a2015-01-28 10:40:41 -050070 private static final String TAG = "WifiTracker";
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080071 private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
Jason Monkd52356a2015-01-28 10:40:41 -050072
73 /** verbose logging flag. this flag is set thru developer debugging options
74 * and used so as to assist with in-the-field WiFi connectivity debugging */
75 public static int sVerboseLogging = 0;
76
77 // TODO: Allow control of this?
78 // Combo scans can take 5-6s to complete - set to 10s.
79 private static final int WIFI_RESCAN_INTERVAL_MS = 10 * 1000;
Ajay Nadathurd7b689a2016-08-31 15:07:56 -070080 private static final int NUM_SCANS_TO_CONFIRM_AP_LOSS = 3;
Jason Monkd52356a2015-01-28 10:40:41 -050081
82 private final Context mContext;
83 private final WifiManager mWifiManager;
84 private final IntentFilter mFilter;
Lorenzo Colittiab313f82015-11-26 16:55:15 +090085 private final ConnectivityManager mConnectivityManager;
86 private final NetworkRequest mNetworkRequest;
Jason Monkd52356a2015-01-28 10:40:41 -050087 private final AtomicBoolean mConnected = new AtomicBoolean(false);
88 private final WifiListener mListener;
89 private final boolean mIncludeSaved;
90 private final boolean mIncludeScans;
Vinit Deshpandedcf00c92015-04-15 18:32:09 -070091 private final boolean mIncludePasspoints;
Sundeep Ghuman71f4a822017-04-18 19:51:46 -070092 @VisibleForTesting final MainHandler mMainHandler;
Sundeep Ghumane8013092017-06-21 22:35:30 -070093 @VisibleForTesting final WorkHandler mWorkHandler;
Jason Monk30d80042015-05-08 16:54:18 -040094
Ajay Nadathurd7b689a2016-08-31 15:07:56 -070095 private WifiTrackerNetworkCallback mNetworkCallback;
96
Sundeep Ghumance6bab82017-04-19 16:21:46 -070097 @GuardedBy("mLock")
Jason Monkd52356a2015-01-28 10:40:41 -050098 private boolean mRegistered;
Ajay Nadathurd7b689a2016-08-31 15:07:56 -070099
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700100 /**
101 * The externally visible access point list.
102 *
103 * Updated using main handler. Clone of this collection is returned from
104 * {@link #getAccessPoints()}
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700105 */
106 private final List<AccessPoint> mAccessPoints = new ArrayList<>();
107
108 /**
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700109 * The internal list of access points, synchronized on itself.
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700110 *
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700111 * Never exposed outside this class.
112 */
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700113 @GuardedBy("mLock")
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700114 private final List<AccessPoint> mInternalAccessPoints = new ArrayList<>();
115
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700116 /**
117 * Synchronization lock for managing concurrency between main and worker threads.
118 *
119 * <p>This lock should be held for all background work.
120 * TODO(b/37674366): Remove the worker thread so synchronization is no longer necessary.
121 */
122 private final Object mLock = new Object();
123
Sundeep Ghuman221327d2017-05-02 15:58:39 -0700124 //visible to both worker and main thread.
125 @GuardedBy("mLock")
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700126 private final AccessPointListenerAdapter mAccessPointListenerAdapter
127 = new AccessPointListenerAdapter();
128
129 private final HashMap<String, Integer> mSeenBssids = new HashMap<>();
130 private final HashMap<String, ScanResult> mScanResultCache = new HashMap<>();
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700131 private Integer mScanId = 0;
Jason Monkd52356a2015-01-28 10:40:41 -0500132
133 private NetworkInfo mLastNetworkInfo;
134 private WifiInfo mLastInfo;
135
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800136 private final NetworkScoreManager mNetworkScoreManager;
137 private final WifiNetworkScoreCache mScoreCache;
Sundeep Ghumane869d832017-01-25 16:23:43 -0800138 private boolean mNetworkScoringUiEnabled;
139 private final ContentObserver mObserver;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800140
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700141 @GuardedBy("mLock")
142 private final Set<NetworkKey> mRequestedScores = new ArraySet<>();
143
Jason Monkd52356a2015-01-28 10:40:41 -0500144 @VisibleForTesting
145 Scanner mScanner;
Sundeep Ghuman42058742017-07-21 18:42:10 -0700146
147 @GuardedBy("mLock")
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700148 private boolean mStaleScanResults = true;
Jason Monkd52356a2015-01-28 10:40:41 -0500149
Jason Monk30d80042015-05-08 16:54:18 -0400150 public WifiTracker(Context context, WifiListener wifiListener,
151 boolean includeSaved, boolean includeScans) {
152 this(context, wifiListener, null, includeSaved, includeScans);
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700153 }
Jason Monk30d80042015-05-08 16:54:18 -0400154
155 public WifiTracker(Context context, WifiListener wifiListener, Looper workerLooper,
156 boolean includeSaved, boolean includeScans) {
157 this(context, wifiListener, workerLooper, includeSaved, includeScans, false);
158 }
159
160 public WifiTracker(Context context, WifiListener wifiListener,
161 boolean includeSaved, boolean includeScans, boolean includePasspoints) {
162 this(context, wifiListener, null, includeSaved, includeScans, includePasspoints);
163 }
164
165 public WifiTracker(Context context, WifiListener wifiListener, Looper workerLooper,
166 boolean includeSaved, boolean includeScans, boolean includePasspoints) {
167 this(context, wifiListener, workerLooper, includeSaved, includeScans, includePasspoints,
Lorenzo Colittiab313f82015-11-26 16:55:15 +0900168 context.getSystemService(WifiManager.class),
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800169 context.getSystemService(ConnectivityManager.class),
170 context.getSystemService(NetworkScoreManager.class), Looper.myLooper()
171 );
Jason Monkd52356a2015-01-28 10:40:41 -0500172 }
173
174 @VisibleForTesting
Jason Monk30d80042015-05-08 16:54:18 -0400175 WifiTracker(Context context, WifiListener wifiListener, Looper workerLooper,
176 boolean includeSaved, boolean includeScans, boolean includePasspoints,
Lorenzo Colittiab313f82015-11-26 16:55:15 +0900177 WifiManager wifiManager, ConnectivityManager connectivityManager,
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800178 NetworkScoreManager networkScoreManager, Looper currentLooper) {
Jason Monkd52356a2015-01-28 10:40:41 -0500179 if (!includeSaved && !includeScans) {
180 throw new IllegalArgumentException("Must include either saved or scans");
181 }
182 mContext = context;
Jason Monkbf3d1af2015-05-22 11:25:04 -0400183 if (currentLooper == null) {
184 // When we aren't on a looper thread, default to the main.
185 currentLooper = Looper.getMainLooper();
186 }
Jason Monk2b51cc32015-05-13 11:07:53 -0400187 mMainHandler = new MainHandler(currentLooper);
Jason Monk30d80042015-05-08 16:54:18 -0400188 mWorkHandler = new WorkHandler(
Jason Monk2b51cc32015-05-13 11:07:53 -0400189 workerLooper != null ? workerLooper : currentLooper);
Jason Monkd52356a2015-01-28 10:40:41 -0500190 mWifiManager = wifiManager;
191 mIncludeSaved = includeSaved;
192 mIncludeScans = includeScans;
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700193 mIncludePasspoints = includePasspoints;
Jason Monkd52356a2015-01-28 10:40:41 -0500194 mListener = wifiListener;
Lorenzo Colittiab313f82015-11-26 16:55:15 +0900195 mConnectivityManager = connectivityManager;
Jason Monkd52356a2015-01-28 10:40:41 -0500196
197 // check if verbose logging has been turned on or off
198 sVerboseLogging = mWifiManager.getVerboseLoggingLevel();
199
200 mFilter = new IntentFilter();
201 mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
202 mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
203 mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
204 mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
205 mFilter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
206 mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
207 mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
Sundeep Ghuman8c792882017-04-04 17:23:29 -0700208 mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
Lorenzo Colittiab313f82015-11-26 16:55:15 +0900209
210 mNetworkRequest = new NetworkRequest.Builder()
211 .clearCapabilities()
212 .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
213 .build();
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800214
215 mNetworkScoreManager = networkScoreManager;
216
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700217 mScoreCache = new WifiNetworkScoreCache(context, new CacheListener(mWorkHandler) {
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800218 @Override
219 public void networkCacheUpdated(List<ScoredNetwork> networks) {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700220 synchronized (mLock) {
221 if (!mRegistered) return;
222 }
223
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800224 if (Log.isLoggable(TAG, Log.VERBOSE)) {
225 Log.v(TAG, "Score cache was updated with networks: " + networks);
226 }
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700227 updateNetworkScores();
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800228 }
229 });
Sundeep Ghumane869d832017-01-25 16:23:43 -0800230
231 mObserver = new ContentObserver(mWorkHandler) {
232 @Override
233 public void onChange(boolean selfChange) {
234 mNetworkScoringUiEnabled =
235 Settings.Global.getInt(
236 mContext.getContentResolver(),
237 Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1;
238 }
239 };
Jason Monkd52356a2015-01-28 10:40:41 -0500240 }
241
Sundeep Ghuman42058742017-07-21 18:42:10 -0700242 /** Synchronously update the list of access points with the latest information. */
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700243 @MainThread
Jason Monkd52356a2015-01-28 10:40:41 -0500244 public void forceUpdate() {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700245 synchronized (mLock) {
246 mWorkHandler.removeMessages(WorkHandler.MSG_UPDATE_ACCESS_POINTS);
247 mLastInfo = mWifiManager.getConnectionInfo();
248 mLastNetworkInfo = mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork());
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700249
250 final List<ScanResult> newScanResults = mWifiManager.getScanResults();
251 List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
Sundeep Ghuman42058742017-07-21 18:42:10 -0700252 mInternalAccessPoints.clear();
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700253 updateAccessPointsLocked(newScanResults, configs);
Sundeep Ghuman9c0b97e2017-04-13 16:05:46 -0700254
Sundeep Ghuman047b2992017-05-10 17:45:13 -0700255 if (DBG) {
256 Log.d(TAG, "force update - internal access point list:\n" + mInternalAccessPoints);
257 }
258
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700259 // Synchronously copy access points
260 mMainHandler.removeMessages(MainHandler.MSG_ACCESS_POINT_CHANGED);
261 mMainHandler.handleMessage(
262 Message.obtain(mMainHandler, MainHandler.MSG_ACCESS_POINT_CHANGED));
Sundeep Ghuman047b2992017-05-10 17:45:13 -0700263 if (DBG) {
264 Log.d(TAG, "force update - external access point list:\n" + mAccessPoints);
265 }
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700266 }
Jason Monkd52356a2015-01-28 10:40:41 -0500267 }
268
269 /**
270 * Force a scan for wifi networks to happen now.
271 */
272 public void forceScan() {
273 if (mWifiManager.isWifiEnabled() && mScanner != null) {
274 mScanner.forceScan();
275 }
276 }
277
278 /**
279 * Temporarily stop scanning for wifi networks.
280 */
281 public void pauseScanning() {
282 if (mScanner != null) {
283 mScanner.pause();
Jason Monk6572eae2015-02-05 10:34:20 -0500284 mScanner = null;
Jason Monkd52356a2015-01-28 10:40:41 -0500285 }
286 }
287
288 /**
289 * Resume scanning for wifi networks after it has been paused.
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800290 *
291 * <p>The score cache should be registered before this method is invoked.
Jason Monkd52356a2015-01-28 10:40:41 -0500292 */
293 public void resumeScanning() {
Jason Monk6572eae2015-02-05 10:34:20 -0500294 if (mScanner == null) {
295 mScanner = new Scanner();
296 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700297
Jason Monkdbd05a92015-07-06 12:55:48 -0400298 mWorkHandler.sendEmptyMessage(WorkHandler.MSG_RESUME);
Jason Monkd52356a2015-01-28 10:40:41 -0500299 if (mWifiManager.isWifiEnabled()) {
Jason Monkd52356a2015-01-28 10:40:41 -0500300 mScanner.resume();
301 }
Jason Monkd52356a2015-01-28 10:40:41 -0500302 }
303
304 /**
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800305 * Start tracking wifi networks and scores.
306 *
307 * <p>Registers listeners and starts scanning for wifi networks. If this is not called
Jason Monkd52356a2015-01-28 10:40:41 -0500308 * then forceUpdate() must be called to populate getAccessPoints().
309 */
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700310 @MainThread
Jason Monkd52356a2015-01-28 10:40:41 -0500311 public void startTracking() {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700312 synchronized (mLock) {
313 registerScoreCache();
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800314
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700315 mContext.getContentResolver().registerContentObserver(
316 Settings.Global.getUriFor(Settings.Global.NETWORK_SCORING_UI_ENABLED),
317 false /* notifyForDescendants */,
318 mObserver);
319 mObserver.onChange(false /* selfChange */); // Set mScoringUiEnabled
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800320
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700321 resumeScanning();
322 if (!mRegistered) {
323 mContext.registerReceiver(mReceiver, mFilter);
324 // NetworkCallback objects cannot be reused. http://b/20701525 .
325 mNetworkCallback = new WifiTrackerNetworkCallback();
326 mConnectivityManager.registerNetworkCallback(mNetworkRequest, mNetworkCallback);
327 mRegistered = true;
328 }
Jason Monkd52356a2015-01-28 10:40:41 -0500329 }
330 }
331
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800332 private void registerScoreCache() {
333 mNetworkScoreManager.registerNetworkScoreCache(
334 NetworkKey.TYPE_WIFI,
335 mScoreCache,
336 NetworkScoreManager.CACHE_FILTER_SCAN_RESULTS);
337 }
338
339 private void requestScoresForNetworkKeys(Collection<NetworkKey> keys) {
340 if (keys.isEmpty()) return;
341
342 if (DBG) {
343 Log.d(TAG, "Requesting scores for Network Keys: " + keys);
344 }
345 mNetworkScoreManager.requestScores(keys.toArray(new NetworkKey[keys.size()]));
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700346 synchronized (mLock) {
347 mRequestedScores.addAll(keys);
348 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800349 }
350
Jason Monkd52356a2015-01-28 10:40:41 -0500351 /**
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800352 * Stop tracking wifi networks and scores.
353 *
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700354 * <p>This should always be called when done with a WifiTracker (if startTracking was called) to
Sundeep Ghumand4a79ac2017-06-07 18:11:39 -0700355 * ensure proper cleanup and prevent any further callbacks from occurring.
356 *
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700357 * <p>Calling this method will set the {@link #mStaleScanResults} bit, which prevents
Sundeep Ghumand4a79ac2017-06-07 18:11:39 -0700358 * {@link WifiListener#onAccessPointsChanged()} callbacks from being invoked (until the bit
359 * is unset on the next SCAN_RESULTS_AVAILABLE_ACTION).
Jason Monkd52356a2015-01-28 10:40:41 -0500360 */
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700361 @MainThread
Jason Monkd52356a2015-01-28 10:40:41 -0500362 public void stopTracking() {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700363 synchronized (mLock) {
364 if (mRegistered) {
365 mContext.unregisterReceiver(mReceiver);
366 mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
367 mRegistered = false;
368 }
Sundeep Ghuman75f94802017-06-26 16:05:40 -0700369 unregisterScoreCache();
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700370 pauseScanning();
371 mContext.getContentResolver().unregisterContentObserver(mObserver);
372
373 mWorkHandler.removePendingMessages();
374 mMainHandler.removePendingMessages();
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700375 mStaleScanResults = true;
Jason Monkd52356a2015-01-28 10:40:41 -0500376 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800377 }
378
Sundeep Ghuman75f94802017-06-26 16:05:40 -0700379 private void unregisterScoreCache() {
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800380 mNetworkScoreManager.unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, mScoreCache);
Sundeep Ghumanac7b4362017-02-08 17:19:27 -0800381
Sundeep Ghuman75f94802017-06-26 16:05:40 -0700382 // We do not want to clear the existing scores in the cache, as this method is called during
383 // stop tracking on activity pause. Hence, on resumption we want the ability to show the
384 // last known, potentially stale, scores. However, by clearing requested scores, the scores
385 // will be requested again upon resumption of tracking, and if any changes have occurred
386 // the listeners (UI) will be updated accordingly.
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700387 synchronized (mLock) {
388 mRequestedScores.clear();
389 }
Jason Monkd52356a2015-01-28 10:40:41 -0500390 }
391
392 /**
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700393 * Gets the current list of access points. Should be called from main thread, otherwise
394 * expect inconsistencies
Jason Monkd52356a2015-01-28 10:40:41 -0500395 */
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700396 @MainThread
Jason Monkd52356a2015-01-28 10:40:41 -0500397 public List<AccessPoint> getAccessPoints() {
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700398 return new ArrayList<>(mAccessPoints);
Jason Monkd52356a2015-01-28 10:40:41 -0500399 }
400
401 public WifiManager getManager() {
402 return mWifiManager;
403 }
404
405 public boolean isWifiEnabled() {
406 return mWifiManager.isWifiEnabled();
407 }
408
409 /**
Amin Shaikha80ae0c2017-03-23 16:18:30 -0700410 * Returns the number of saved networks on the device, regardless of whether the WifiTracker
411 * is tracking saved networks.
Peter Qiuabea7262017-05-31 10:18:17 -0700412 * TODO(b/62292448): remove this function and update callsites to use WifiSavedConfigUtils
413 * directly.
Jason Monkd52356a2015-01-28 10:40:41 -0500414 */
Amin Shaikha80ae0c2017-03-23 16:18:30 -0700415 public int getNumSavedNetworks() {
Peter Qiuabea7262017-05-31 10:18:17 -0700416 return WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).size();
Jason Monkd52356a2015-01-28 10:40:41 -0500417 }
418
419 public boolean isConnected() {
420 return mConnected.get();
421 }
422
423 public void dump(PrintWriter pw) {
424 pw.println(" - wifi tracker ------");
Jason Monk19040812015-06-01 15:00:57 -0400425 for (AccessPoint accessPoint : getAccessPoints()) {
Jason Monkd52356a2015-01-28 10:40:41 -0500426 pw.println(" " + accessPoint);
427 }
428 }
429
Jason Monkdbd05a92015-07-06 12:55:48 -0400430 private void handleResume() {
431 mScanResultCache.clear();
432 mSeenBssids.clear();
433 mScanId = 0;
434 }
435
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700436 private Collection<ScanResult> updateScanResultCache(final List<ScanResult> newResults) {
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700437 mScanId++;
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700438 for (ScanResult newResult : newResults) {
Mitchell Wills9df79042015-09-01 12:21:59 -0700439 if (newResult.SSID == null || newResult.SSID.isEmpty()) {
440 continue;
441 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700442 mScanResultCache.put(newResult.BSSID, newResult);
443 mSeenBssids.put(newResult.BSSID, mScanId);
444 }
445
446 if (mScanId > NUM_SCANS_TO_CONFIRM_AP_LOSS) {
447 if (DBG) Log.d(TAG, "------ Dumping SSIDs that were expired on this scan ------");
448 Integer threshold = mScanId - NUM_SCANS_TO_CONFIRM_AP_LOSS;
449 for (Iterator<Map.Entry<String, Integer>> it = mSeenBssids.entrySet().iterator();
450 it.hasNext(); /* nothing */) {
451 Map.Entry<String, Integer> e = it.next();
452 if (e.getValue() < threshold) {
453 ScanResult result = mScanResultCache.get(e.getKey());
454 if (DBG) Log.d(TAG, "Removing " + e.getKey() + ":(" + result.SSID + ")");
455 mScanResultCache.remove(e.getKey());
456 it.remove();
457 }
458 }
459 if (DBG) Log.d(TAG, "---- Done Dumping SSIDs that were expired on this scan ----");
460 }
461
462 return mScanResultCache.values();
463 }
464
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700465 private WifiConfiguration getWifiConfigurationForNetworkId(
466 int networkId, final List<WifiConfiguration> configs) {
Mitchell Wills5a42db22015-08-03 09:46:08 -0700467 if (configs != null) {
468 for (WifiConfiguration config : configs) {
469 if (mLastInfo != null && networkId == config.networkId &&
470 !(config.selfAdded && config.numAssociation == 0)) {
471 return config;
472 }
473 }
474 }
475 return null;
476 }
477
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700478 /**
479 * Safely modify {@link #mInternalAccessPoints} by acquiring {@link #mLock} first.
480 *
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700481 * <p>Will not perform the update if {@link #mStaleScanResults} is true
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700482 */
483 private void updateAccessPoints() {
484 List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
485 final List<ScanResult> newScanResults = mWifiManager.getScanResults();
486
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700487 synchronized (mLock) {
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700488 if(!mStaleScanResults) {
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700489 updateAccessPointsLocked(newScanResults, configs);
490 }
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700491 }
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700492 }
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700493
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700494 /**
495 * Update the internal list of access points.
496 *
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700497 * <p>Do not called directly (except for forceUpdate), use {@link #updateAccessPoints()} which
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700498 * respects {@link #mStaleScanResults}.
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700499 */
500 @GuardedBy("mLock")
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700501 private void updateAccessPointsLocked(final List<ScanResult> newScanResults,
502 List<WifiConfiguration> configs) {
503 WifiConfiguration connectionConfig = null;
504 if (mLastInfo != null) {
505 connectionConfig = getWifiConfigurationForNetworkId(
506 mLastInfo.getNetworkId(), mWifiManager.getConfiguredNetworks());
507 }
508
Jason Monkd52356a2015-01-28 10:40:41 -0500509 // Swap the current access points into a cached list.
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700510 List<AccessPoint> cachedAccessPoints = new ArrayList<>(mInternalAccessPoints);
Jason Monk30d80042015-05-08 16:54:18 -0400511 ArrayList<AccessPoint> accessPoints = new ArrayList<>();
512
Jason Monkd52356a2015-01-28 10:40:41 -0500513 // Clear out the configs so we don't think something is saved when it isn't.
Jason Monk30d80042015-05-08 16:54:18 -0400514 for (AccessPoint accessPoint : cachedAccessPoints) {
Jason Monkd52356a2015-01-28 10:40:41 -0500515 accessPoint.clearConfig();
516 }
517
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700518 /* Lookup table to more quickly update AccessPoints by only considering objects with the
519 * correct SSID. Maps SSID -> List of AccessPoints with the given SSID. */
Jason Monkd52356a2015-01-28 10:40:41 -0500520 Multimap<String, AccessPoint> apMap = new Multimap<String, AccessPoint>();
521
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700522 final Collection<ScanResult> results = updateScanResultCache(newScanResults);
Sanket Padawe0775a982015-08-19 14:57:46 -0700523
Jason Monkd52356a2015-01-28 10:40:41 -0500524 if (configs != null) {
Jason Monkd52356a2015-01-28 10:40:41 -0500525 for (WifiConfiguration config : configs) {
526 if (config.selfAdded && config.numAssociation == 0) {
527 continue;
528 }
Jason Monk30d80042015-05-08 16:54:18 -0400529 AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints);
Jason Monkd52356a2015-01-28 10:40:41 -0500530 if (mLastInfo != null && mLastNetworkInfo != null) {
Peter Qiu2c3b5ee22017-02-01 11:49:15 -0800531 accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
Jason Monkd52356a2015-01-28 10:40:41 -0500532 }
533 if (mIncludeSaved) {
Sundeep Ghumandc6cf4b2017-03-08 16:18:29 -0800534 // If saved network not present in scan result then set its Rssi to
535 // UNREACHABLE_RSSI
Peter Qiu2c3b5ee22017-02-01 11:49:15 -0800536 boolean apFound = false;
537 for (ScanResult result : results) {
538 if (result.SSID.equals(accessPoint.getSsidStr())) {
539 apFound = true;
540 break;
Sanket Padawe0775a982015-08-19 14:57:46 -0700541 }
Sanket Padawe0775a982015-08-19 14:57:46 -0700542 }
Peter Qiu2c3b5ee22017-02-01 11:49:15 -0800543 if (!apFound) {
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800544 accessPoint.setUnreachable();
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700545 }
Peter Qiu2c3b5ee22017-02-01 11:49:15 -0800546 accessPoints.add(accessPoint);
547 apMap.put(accessPoint.getSsidStr(), accessPoint);
Jason Monkd52356a2015-01-28 10:40:41 -0500548 } else {
549 // If we aren't using saved networks, drop them into the cache so that
550 // we have access to their saved info.
Jason Monk30d80042015-05-08 16:54:18 -0400551 cachedAccessPoints.add(accessPoint);
Jason Monkd52356a2015-01-28 10:40:41 -0500552 }
553 }
554 }
555
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800556 final List<NetworkKey> scoresToRequest = new ArrayList<>();
Jason Monkd52356a2015-01-28 10:40:41 -0500557 if (results != null) {
558 for (ScanResult result : results) {
559 // Ignore hidden and ad-hoc networks.
560 if (result.SSID == null || result.SSID.length() == 0 ||
561 result.capabilities.contains("[IBSS]")) {
562 continue;
563 }
564
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800565 NetworkKey key = NetworkKey.createFromScanResult(result);
Stephen Chenfde900d2017-02-14 16:40:21 -0800566 if (key != null && !mRequestedScores.contains(key)) {
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800567 scoresToRequest.add(key);
568 }
569
Jason Monkd52356a2015-01-28 10:40:41 -0500570 boolean found = false;
571 for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700572 // We want to evict old scan results if are current results are not stale
573 if (accessPoint.update(result, !mStaleScanResults)) {
Jason Monkd52356a2015-01-28 10:40:41 -0500574 found = true;
575 break;
576 }
577 }
578 if (!found && mIncludeScans) {
Jason Monk30d80042015-05-08 16:54:18 -0400579 AccessPoint accessPoint = getCachedOrCreate(result, cachedAccessPoints);
Jason Monkd52356a2015-01-28 10:40:41 -0500580 if (mLastInfo != null && mLastNetworkInfo != null) {
Mitchell Wills5a42db22015-08-03 09:46:08 -0700581 accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
Jason Monkd52356a2015-01-28 10:40:41 -0500582 }
Vinit Deshpandefc406002015-04-15 18:10:55 -0700583
Vinit Deshpandea0d929e2015-06-12 15:18:44 -0700584 if (result.isPasspointNetwork()) {
Peter Qiu2c3b5ee22017-02-01 11:49:15 -0800585 // Retrieve a WifiConfiguration for a Passpoint provider that matches
586 // the given ScanResult. This is used for showing that a given AP
587 // (ScanResult) is available via a Passpoint provider (provider friendly
588 // name).
Peter Qiu0ffb89d2017-03-27 13:45:25 -0700589 try {
590 WifiConfiguration config = mWifiManager.getMatchingWifiConfig(result);
591 if (config != null) {
592 accessPoint.update(config);
593 }
594 } catch (UnsupportedOperationException e) {
595 // Passpoint not supported on the device.
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700596 }
597 }
598
Jason Monk30d80042015-05-08 16:54:18 -0400599 accessPoints.add(accessPoint);
Jason Monk6980d122015-06-15 10:07:55 -0400600 apMap.put(accessPoint.getSsidStr(), accessPoint);
Jason Monkd52356a2015-01-28 10:40:41 -0500601 }
602 }
603 }
604
Stephen Chen21f68682017-04-04 13:23:31 -0700605 requestScoresForNetworkKeys(scoresToRequest);
606 for (AccessPoint ap : accessPoints) {
607 ap.update(mScoreCache, mNetworkScoringUiEnabled);
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800608 }
609
Jason Monkd52356a2015-01-28 10:40:41 -0500610 // Pre-sort accessPoints to speed preference insertion
Jason Monk30d80042015-05-08 16:54:18 -0400611 Collections.sort(accessPoints);
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700612
613 // Log accesspoints that were deleted
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800614 if (DBG) {
615 Log.d(TAG, "------ Dumping SSIDs that were not seen on this scan ------");
616 for (AccessPoint prevAccessPoint : mInternalAccessPoints) {
617 if (prevAccessPoint.getSsid() == null)
618 continue;
619 String prevSsid = prevAccessPoint.getSsidStr();
620 boolean found = false;
621 for (AccessPoint newAccessPoint : accessPoints) {
622 if (newAccessPoint.getSsid() != null && newAccessPoint.getSsid()
623 .equals(prevSsid)) {
624 found = true;
625 break;
626 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700627 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800628 if (!found)
629 Log.d(TAG, "Did not find " + prevSsid + " in this scan");
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700630 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800631 Log.d(TAG, "---- Done dumping SSIDs that were not seen on this scan ----");
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700632 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700633
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700634 mInternalAccessPoints.clear();
635 mInternalAccessPoints.addAll(accessPoints);
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700636
637 mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED);
Jason Monkd52356a2015-01-28 10:40:41 -0500638 }
639
Ajay Nadathur7d176bc2016-10-24 16:55:24 -0700640 @VisibleForTesting
641 AccessPoint getCachedOrCreate(ScanResult result, List<AccessPoint> cache) {
Jason Monk30d80042015-05-08 16:54:18 -0400642 final int N = cache.size();
Jason Monkd52356a2015-01-28 10:40:41 -0500643 for (int i = 0; i < N; i++) {
Jason Monk30d80042015-05-08 16:54:18 -0400644 if (cache.get(i).matches(result)) {
645 AccessPoint ret = cache.remove(i);
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700646 // evict old scan results only if we have fresh results
647 ret.update(result, !mStaleScanResults);
Jason Monkd52356a2015-01-28 10:40:41 -0500648 return ret;
649 }
650 }
Ajay Nadathur7d176bc2016-10-24 16:55:24 -0700651 final AccessPoint accessPoint = new AccessPoint(mContext, result);
652 accessPoint.setListener(mAccessPointListenerAdapter);
653 return accessPoint;
Jason Monkd52356a2015-01-28 10:40:41 -0500654 }
655
Ajay Nadathur7d176bc2016-10-24 16:55:24 -0700656 @VisibleForTesting
657 AccessPoint getCachedOrCreate(WifiConfiguration config, List<AccessPoint> cache) {
Jason Monk30d80042015-05-08 16:54:18 -0400658 final int N = cache.size();
Jason Monkd52356a2015-01-28 10:40:41 -0500659 for (int i = 0; i < N; i++) {
Jason Monk30d80042015-05-08 16:54:18 -0400660 if (cache.get(i).matches(config)) {
661 AccessPoint ret = cache.remove(i);
Jason Monkd52356a2015-01-28 10:40:41 -0500662 ret.loadConfig(config);
663 return ret;
664 }
665 }
Ajay Nadathur7d176bc2016-10-24 16:55:24 -0700666 final AccessPoint accessPoint = new AccessPoint(mContext, config);
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700667 accessPoint.setListener(mAccessPointListenerAdapter);
668 return accessPoint;
Jason Monkd52356a2015-01-28 10:40:41 -0500669 }
670
671 private void updateNetworkInfo(NetworkInfo networkInfo) {
Jason Monke04ae8a2015-06-03 14:06:34 -0400672 /* sticky broadcasts can call this when wifi is disabled */
673 if (!mWifiManager.isWifiEnabled()) {
674 mMainHandler.sendEmptyMessage(MainHandler.MSG_PAUSE_SCANNING);
Sundeep Ghumane8013092017-06-21 22:35:30 -0700675 clearAccessPointsAndConditionallyUpdate();
Jason Monke04ae8a2015-06-03 14:06:34 -0400676 return;
677 }
Jason Monkd52356a2015-01-28 10:40:41 -0500678
Jason Monke04ae8a2015-06-03 14:06:34 -0400679 if (networkInfo != null &&
680 networkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR) {
681 mMainHandler.sendEmptyMessage(MainHandler.MSG_PAUSE_SCANNING);
682 } else {
683 mMainHandler.sendEmptyMessage(MainHandler.MSG_RESUME_SCANNING);
Jason Monkd52356a2015-01-28 10:40:41 -0500684 }
685
Jason Monkd52356a2015-01-28 10:40:41 -0500686 if (networkInfo != null) {
687 mLastNetworkInfo = networkInfo;
688 }
689
Mitchell Wills5a42db22015-08-03 09:46:08 -0700690 WifiConfiguration connectionConfig = null;
Lorenzo Colittiab313f82015-11-26 16:55:15 +0900691 mLastInfo = mWifiManager.getConnectionInfo();
Mitchell Wills5a42db22015-08-03 09:46:08 -0700692 if (mLastInfo != null) {
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700693 connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId(),
694 mWifiManager.getConfiguredNetworks());
Mitchell Wills5a42db22015-08-03 09:46:08 -0700695 }
696
Sundeep Ghuman8c792882017-04-04 17:23:29 -0700697 boolean updated = false;
698 boolean reorder = false; // Only reorder if connected AP was changed
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700699
700 synchronized (mLock) {
701 for (int i = mInternalAccessPoints.size() - 1; i >= 0; --i) {
702 AccessPoint ap = mInternalAccessPoints.get(i);
703 boolean previouslyConnected = ap.isActive();
704 if (ap.update(connectionConfig, mLastInfo, mLastNetworkInfo)) {
705 updated = true;
706 if (previouslyConnected != ap.isActive()) reorder = true;
707 }
708 if (ap.update(mScoreCache, mNetworkScoringUiEnabled)) {
709 reorder = true;
710 updated = true;
711 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800712 }
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700713
714 if (reorder) Collections.sort(mInternalAccessPoints);
715
716 if (updated) mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED);
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800717 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800718 }
719
Sundeep Ghumane8013092017-06-21 22:35:30 -0700720 private void clearAccessPointsAndConditionallyUpdate() {
721 synchronized (mLock) {
722 if (!mInternalAccessPoints.isEmpty()) {
723 mInternalAccessPoints.clear();
724 if (!mMainHandler.hasMessages(MainHandler.MSG_ACCESS_POINT_CHANGED)) {
725 mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED);
726 }
727 }
728 }
729 }
730
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800731 /**
Stephen Chen21f68682017-04-04 13:23:31 -0700732 * Update all the internal access points rankingScores, badge and metering.
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800733 *
734 * <p>Will trigger a resort and notify listeners of changes if applicable.
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700735 *
736 * <p>Synchronized on {@link #mLock}.
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800737 */
738 private void updateNetworkScores() {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700739 synchronized (mLock) {
740 boolean updated = false;
741 for (int i = 0; i < mInternalAccessPoints.size(); i++) {
742 if (mInternalAccessPoints.get(i).update(mScoreCache, mNetworkScoringUiEnabled)) {
743 updated = true;
744 }
Jason Monkd52356a2015-01-28 10:40:41 -0500745 }
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700746 if (updated) {
747 Collections.sort(mInternalAccessPoints);
748 mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED);
749 }
Jason Monkd52356a2015-01-28 10:40:41 -0500750 }
751 }
752
753 private void updateWifiState(int state) {
Mitchell Willscf0875a2016-04-18 14:54:53 -0700754 mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_WIFI_STATE, state, 0).sendToTarget();
Sundeep Ghumane8013092017-06-21 22:35:30 -0700755 if (!mWifiManager.isWifiEnabled()) {
756 clearAccessPointsAndConditionallyUpdate();
757 }
Jason Monkd52356a2015-01-28 10:40:41 -0500758 }
759
760 public static List<AccessPoint> getCurrentAccessPoints(Context context, boolean includeSaved,
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700761 boolean includeScans, boolean includePasspoints) {
762 WifiTracker tracker = new WifiTracker(context,
Jason Monk30d80042015-05-08 16:54:18 -0400763 null, null, includeSaved, includeScans, includePasspoints);
Jason Monkd52356a2015-01-28 10:40:41 -0500764 tracker.forceUpdate();
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700765 tracker.copyAndNotifyListeners(false /*notifyListeners*/);
Jason Monkd52356a2015-01-28 10:40:41 -0500766 return tracker.getAccessPoints();
767 }
768
769 @VisibleForTesting
770 final BroadcastReceiver mReceiver = new BroadcastReceiver() {
771 @Override
772 public void onReceive(Context context, Intent intent) {
773 String action = intent.getAction();
Sundeep Ghumand4a79ac2017-06-07 18:11:39 -0700774
Jason Monkd52356a2015-01-28 10:40:41 -0500775 if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
776 updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
777 WifiManager.WIFI_STATE_UNKNOWN));
Sundeep Ghumand2b84a82017-07-05 14:19:30 -0700778 } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
779 mWorkHandler
780 .obtainMessage(
781 WorkHandler.MSG_UPDATE_ACCESS_POINTS,
782 WorkHandler.CLEAR_STALE_SCAN_RESULTS,
783 0)
784 .sendToTarget();
785 } else if (WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action)
786 || WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) {
Jason Monk30d80042015-05-08 16:54:18 -0400787 mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS);
Jason Monkd52356a2015-01-28 10:40:41 -0500788 } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
Sundeep Ghumand2b84a82017-07-05 14:19:30 -0700789 NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
Jason Monk30d80042015-05-08 16:54:18 -0400790
Sundeep Ghuman42058742017-07-21 18:42:10 -0700791 if(mConnected.get() != info.isConnected()) {
792 mConnected.set(info.isConnected());
793 mMainHandler.sendEmptyMessage(MainHandler.MSG_CONNECTED_CHANGED);
794 }
Jason Monk30d80042015-05-08 16:54:18 -0400795
Jason Monk30d80042015-05-08 16:54:18 -0400796 mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info)
797 .sendToTarget();
Sundeep Ghuman2b489902017-02-22 18:17:29 -0800798 mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS);
Sundeep Ghuman8c792882017-04-04 17:23:29 -0700799 } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700800 NetworkInfo info =
801 mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork());
802 mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info)
803 .sendToTarget();
Jason Monkd52356a2015-01-28 10:40:41 -0500804 }
805 }
806 };
807
Lorenzo Colittiab313f82015-11-26 16:55:15 +0900808 private final class WifiTrackerNetworkCallback extends ConnectivityManager.NetworkCallback {
809 public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) {
810 if (network.equals(mWifiManager.getCurrentNetwork())) {
811 // We don't send a NetworkInfo object along with this message, because even if we
812 // fetch one from ConnectivityManager, it might be older than the most recent
813 // NetworkInfo message we got via a WIFI_STATE_CHANGED broadcast.
814 mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO);
815 }
816 }
817 }
818
Sundeep Ghuman71f4a822017-04-18 19:51:46 -0700819 @VisibleForTesting
820 final class MainHandler extends Handler {
821 @VisibleForTesting static final int MSG_CONNECTED_CHANGED = 0;
822 @VisibleForTesting static final int MSG_WIFI_STATE_CHANGED = 1;
823 @VisibleForTesting static final int MSG_ACCESS_POINT_CHANGED = 2;
Jason Monke04ae8a2015-06-03 14:06:34 -0400824 private static final int MSG_RESUME_SCANNING = 3;
825 private static final int MSG_PAUSE_SCANNING = 4;
Jason Monk30d80042015-05-08 16:54:18 -0400826
Jason Monk2b51cc32015-05-13 11:07:53 -0400827 public MainHandler(Looper looper) {
828 super(looper);
829 }
830
Jason Monk30d80042015-05-08 16:54:18 -0400831 @Override
832 public void handleMessage(Message msg) {
833 if (mListener == null) {
834 return;
835 }
836 switch (msg.what) {
837 case MSG_CONNECTED_CHANGED:
838 mListener.onConnectedChanged();
839 break;
840 case MSG_WIFI_STATE_CHANGED:
841 mListener.onWifiStateChanged(msg.arg1);
842 break;
843 case MSG_ACCESS_POINT_CHANGED:
Sundeep Ghumane8013092017-06-21 22:35:30 -0700844 // Only notify listeners of changes if we have fresh scan results, otherwise the
845 // UI will be updated with stale results. We want to copy the APs regardless,
846 // for instances where forceUpdate was invoked by the caller.
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700847 if (mStaleScanResults) {
Sundeep Ghumane8013092017-06-21 22:35:30 -0700848 copyAndNotifyListeners(false /*notifyListeners*/);
849 } else {
850 copyAndNotifyListeners(true /*notifyListeners*/);
851 mListener.onAccessPointsChanged();
852 }
Jason Monk30d80042015-05-08 16:54:18 -0400853 break;
Jason Monke04ae8a2015-06-03 14:06:34 -0400854 case MSG_RESUME_SCANNING:
855 if (mScanner != null) {
856 mScanner.resume();
857 }
858 break;
859 case MSG_PAUSE_SCANNING:
860 if (mScanner != null) {
861 mScanner.pause();
862 }
863 break;
Jason Monk30d80042015-05-08 16:54:18 -0400864 }
865 }
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700866
Sundeep Ghuman71f4a822017-04-18 19:51:46 -0700867 void removePendingMessages() {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700868 removeMessages(MSG_ACCESS_POINT_CHANGED);
Sundeep Ghuman71f4a822017-04-18 19:51:46 -0700869 removeMessages(MSG_CONNECTED_CHANGED);
870 removeMessages(MSG_WIFI_STATE_CHANGED);
871 removeMessages(MSG_PAUSE_SCANNING);
872 removeMessages(MSG_RESUME_SCANNING);
873 }
Jason Monk30d80042015-05-08 16:54:18 -0400874 }
875
Sundeep Ghumane8013092017-06-21 22:35:30 -0700876 @VisibleForTesting
877 final class WorkHandler extends Handler {
Jason Monk30d80042015-05-08 16:54:18 -0400878 private static final int MSG_UPDATE_ACCESS_POINTS = 0;
879 private static final int MSG_UPDATE_NETWORK_INFO = 1;
Jason Monkdbd05a92015-07-06 12:55:48 -0400880 private static final int MSG_RESUME = 2;
Mitchell Willscf0875a2016-04-18 14:54:53 -0700881 private static final int MSG_UPDATE_WIFI_STATE = 3;
Jason Monk30d80042015-05-08 16:54:18 -0400882
Sundeep Ghumand2b84a82017-07-05 14:19:30 -0700883 private static final int CLEAR_STALE_SCAN_RESULTS = 1;
884
Jason Monk30d80042015-05-08 16:54:18 -0400885 public WorkHandler(Looper looper) {
886 super(looper);
887 }
888
889 @Override
890 public void handleMessage(Message msg) {
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700891 synchronized (mLock) {
892 processMessage(msg);
893 }
894 }
895
Sundeep Ghumance6bab82017-04-19 16:21:46 -0700896 private void processMessage(Message msg) {
897 if (!mRegistered) return;
898
Jason Monk30d80042015-05-08 16:54:18 -0400899 switch (msg.what) {
900 case MSG_UPDATE_ACCESS_POINTS:
Sundeep Ghumand2b84a82017-07-05 14:19:30 -0700901 if (msg.arg1 == CLEAR_STALE_SCAN_RESULTS) {
Sundeep Ghumana580215f2017-08-07 11:21:38 -0700902 mStaleScanResults = false;
Sundeep Ghumand2b84a82017-07-05 14:19:30 -0700903 }
Sundeep Ghuman266b4092017-06-16 15:12:56 -0700904 updateAccessPoints();
Jason Monk30d80042015-05-08 16:54:18 -0400905 break;
906 case MSG_UPDATE_NETWORK_INFO:
907 updateNetworkInfo((NetworkInfo) msg.obj);
908 break;
Jason Monkdbd05a92015-07-06 12:55:48 -0400909 case MSG_RESUME:
910 handleResume();
911 break;
Mitchell Willscf0875a2016-04-18 14:54:53 -0700912 case MSG_UPDATE_WIFI_STATE:
913 if (msg.arg1 == WifiManager.WIFI_STATE_ENABLED) {
914 if (mScanner != null) {
915 // We only need to resume if mScanner isn't null because
916 // that means we want to be scanning.
917 mScanner.resume();
918 }
919 } else {
920 mLastInfo = null;
921 mLastNetworkInfo = null;
922 if (mScanner != null) {
923 mScanner.pause();
924 }
925 }
926 mMainHandler.obtainMessage(MainHandler.MSG_WIFI_STATE_CHANGED, msg.arg1, 0)
927 .sendToTarget();
928 break;
Jason Monk30d80042015-05-08 16:54:18 -0400929 }
930 }
Sundeep Ghuman71f4a822017-04-18 19:51:46 -0700931
932 private void removePendingMessages() {
933 removeMessages(MSG_UPDATE_ACCESS_POINTS);
934 removeMessages(MSG_UPDATE_NETWORK_INFO);
935 removeMessages(MSG_RESUME);
936 removeMessages(MSG_UPDATE_WIFI_STATE);
Sundeep Ghuman71f4a822017-04-18 19:51:46 -0700937 }
Jason Monk30d80042015-05-08 16:54:18 -0400938 }
939
Jason Monkd52356a2015-01-28 10:40:41 -0500940 @VisibleForTesting
941 class Scanner extends Handler {
Jason Monk2b51cc32015-05-13 11:07:53 -0400942 static final int MSG_SCAN = 0;
Jason Monk6572eae2015-02-05 10:34:20 -0500943
Jason Monkd52356a2015-01-28 10:40:41 -0500944 private int mRetry = 0;
945
946 void resume() {
Jason Monk6572eae2015-02-05 10:34:20 -0500947 if (!hasMessages(MSG_SCAN)) {
948 sendEmptyMessage(MSG_SCAN);
Jason Monkd52356a2015-01-28 10:40:41 -0500949 }
950 }
951
952 void forceScan() {
Jason Monk6572eae2015-02-05 10:34:20 -0500953 removeMessages(MSG_SCAN);
954 sendEmptyMessage(MSG_SCAN);
Jason Monkd52356a2015-01-28 10:40:41 -0500955 }
956
957 void pause() {
958 mRetry = 0;
Jason Monk6572eae2015-02-05 10:34:20 -0500959 removeMessages(MSG_SCAN);
960 }
961
962 @VisibleForTesting
963 boolean isScanning() {
964 return hasMessages(MSG_SCAN);
Jason Monkd52356a2015-01-28 10:40:41 -0500965 }
966
967 @Override
968 public void handleMessage(Message message) {
Jason Monk6572eae2015-02-05 10:34:20 -0500969 if (message.what != MSG_SCAN) return;
Jason Monkd52356a2015-01-28 10:40:41 -0500970 if (mWifiManager.startScan()) {
971 mRetry = 0;
972 } else if (++mRetry >= 3) {
973 mRetry = 0;
974 if (mContext != null) {
975 Toast.makeText(mContext, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show();
976 }
977 return;
978 }
979 sendEmptyMessageDelayed(0, WIFI_RESCAN_INTERVAL_MS);
980 }
981 }
982
983 /** A restricted multimap for use in constructAccessPoints */
984 private static class Multimap<K,V> {
985 private final HashMap<K,List<V>> store = new HashMap<K,List<V>>();
986 /** retrieve a non-null list of values with key K */
987 List<V> getAll(K key) {
988 List<V> values = store.get(key);
989 return values != null ? values : Collections.<V>emptyList();
990 }
991
992 void put(K key, V val) {
993 List<V> curVals = store.get(key);
994 if (curVals == null) {
995 curVals = new ArrayList<V>(3);
996 store.put(key, curVals);
997 }
998 curVals.add(val);
999 }
1000 }
1001
1002 public interface WifiListener {
1003 /**
1004 * Called when the state of Wifi has changed, the state will be one of
1005 * the following.
1006 *
1007 * <li>{@link WifiManager#WIFI_STATE_DISABLED}</li>
1008 * <li>{@link WifiManager#WIFI_STATE_ENABLED}</li>
1009 * <li>{@link WifiManager#WIFI_STATE_DISABLING}</li>
1010 * <li>{@link WifiManager#WIFI_STATE_ENABLING}</li>
1011 * <li>{@link WifiManager#WIFI_STATE_UNKNOWN}</li>
1012 * <p>
1013 *
1014 * @param state The new state of wifi.
1015 */
1016 void onWifiStateChanged(int state);
1017
1018 /**
1019 * Called when the connection state of wifi has changed and isConnected
1020 * should be called to get the updated state.
1021 */
1022 void onConnectedChanged();
1023
1024 /**
1025 * Called to indicate the list of AccessPoints has been updated and
1026 * getAccessPoints should be called to get the latest information.
1027 */
1028 void onAccessPointsChanged();
1029 }
Ajay Nadathurd7b689a2016-08-31 15:07:56 -07001030
1031 /**
1032 * Helps capture notifications that were generated during AccessPoint modification. Used later
1033 * on by {@link #copyAndNotifyListeners(boolean)} to send notifications.
1034 */
1035 private static class AccessPointListenerAdapter implements AccessPoint.AccessPointListener {
1036 static final int AP_CHANGED = 1;
1037 static final int LEVEL_CHANGED = 2;
1038
1039 final SparseIntArray mPendingNotifications = new SparseIntArray();
1040
1041 @Override
1042 public void onAccessPointChanged(AccessPoint accessPoint) {
1043 int type = mPendingNotifications.get(accessPoint.mId);
1044 mPendingNotifications.put(accessPoint.mId, type | AP_CHANGED);
1045 }
1046
1047 @Override
1048 public void onLevelChanged(AccessPoint accessPoint) {
1049 int type = mPendingNotifications.get(accessPoint.mId);
1050 mPendingNotifications.put(accessPoint.mId, type | LEVEL_CHANGED);
1051 }
1052 }
1053
1054 /**
1055 * Responsible for copying access points from {@link #mInternalAccessPoints} and notifying
Sundeep Ghumance6bab82017-04-19 16:21:46 -07001056 * accesspoint listeners.
Ajay Nadathurd7b689a2016-08-31 15:07:56 -07001057 *
1058 * @param notifyListeners if true, accesspoint listeners are notified, otherwise notifications
1059 * dropped.
1060 */
Sundeep Ghumance6bab82017-04-19 16:21:46 -07001061 @MainThread
Ajay Nadathurd7b689a2016-08-31 15:07:56 -07001062 private void copyAndNotifyListeners(boolean notifyListeners) {
1063 // Need to watch out for memory allocations on main thread.
1064 SparseArray<AccessPoint> oldAccessPoints = new SparseArray<>();
1065 SparseIntArray notificationMap = null;
1066 List<AccessPoint> updatedAccessPoints = new ArrayList<>();
1067
1068 for (AccessPoint accessPoint : mAccessPoints) {
1069 oldAccessPoints.put(accessPoint.mId, accessPoint);
1070 }
1071
Sundeep Ghumance6bab82017-04-19 16:21:46 -07001072 if (DBG) {
1073 Log.d(TAG, "Starting to copy AP items on the MainHandler");
1074 }
Sundeep Ghumance6bab82017-04-19 16:21:46 -07001075 synchronized (mLock) {
Sundeep Ghuman221327d2017-05-02 15:58:39 -07001076 if (notifyListeners) {
1077 notificationMap = mAccessPointListenerAdapter.mPendingNotifications.clone();
1078 }
1079
1080 mAccessPointListenerAdapter.mPendingNotifications.clear();
1081
Ajay Nadathurd7b689a2016-08-31 15:07:56 -07001082 for (AccessPoint internalAccessPoint : mInternalAccessPoints) {
1083 AccessPoint accessPoint = oldAccessPoints.get(internalAccessPoint.mId);
1084 if (accessPoint == null) {
1085 accessPoint = new AccessPoint(mContext, internalAccessPoint);
1086 } else {
1087 accessPoint.copyFrom(internalAccessPoint);
1088 }
1089 updatedAccessPoints.add(accessPoint);
1090 }
Ajay Nadathurd7b689a2016-08-31 15:07:56 -07001091 }
1092
1093 mAccessPoints.clear();
1094 mAccessPoints.addAll(updatedAccessPoints);
1095
1096 if (notificationMap != null && notificationMap.size() > 0) {
1097 for (AccessPoint accessPoint : updatedAccessPoints) {
1098 int notificationType = notificationMap.get(accessPoint.mId);
1099 AccessPoint.AccessPointListener listener = accessPoint.mAccessPointListener;
1100 if (notificationType == 0 || listener == null) {
1101 continue;
1102 }
1103
1104 if ((notificationType & AccessPointListenerAdapter.AP_CHANGED) != 0) {
1105 listener.onAccessPointChanged(accessPoint);
1106 }
1107
1108 if ((notificationType & AccessPointListenerAdapter.LEVEL_CHANGED) != 0) {
1109 listener.onLevelChanged(accessPoint);
1110 }
1111 }
1112 }
1113 }
Jason Monkd52356a2015-01-28 10:40:41 -05001114}