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 | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 18 | import android.annotation.MainThread; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 23 | import android.database.ContentObserver; |
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; |
| 28 | import android.net.NetworkInfo.DetailedState; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 29 | import android.net.NetworkKey; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 30 | import android.net.NetworkRequest; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 31 | import android.net.NetworkScoreManager; |
| 32 | import android.net.ScoredNetwork; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 33 | import android.net.wifi.ScanResult; |
| 34 | import android.net.wifi.WifiConfiguration; |
| 35 | import android.net.wifi.WifiInfo; |
| 36 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 37 | import android.net.wifi.WifiNetworkScoreCache; |
| 38 | import android.net.wifi.WifiNetworkScoreCache.CacheListener; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 39 | import android.os.Handler; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 40 | import android.os.Looper; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 41 | import android.os.Message; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 42 | import android.provider.Settings; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 43 | import android.support.annotation.GuardedBy; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 44 | import android.util.ArraySet; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 45 | import android.util.Log; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 46 | import android.util.SparseArray; |
| 47 | import android.util.SparseIntArray; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 48 | import android.widget.Toast; |
| 49 | |
| 50 | import com.android.internal.annotations.VisibleForTesting; |
| 51 | import com.android.settingslib.R; |
| 52 | |
| 53 | import java.io.PrintWriter; |
| 54 | import java.util.ArrayList; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 55 | import java.util.Collection; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 56 | import java.util.Collections; |
| 57 | import java.util.HashMap; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 58 | import java.util.Iterator; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 59 | import java.util.List; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 60 | import java.util.Map; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 61 | import java.util.Set; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 62 | import java.util.concurrent.atomic.AtomicBoolean; |
| 63 | |
| 64 | /** |
| 65 | * Tracks saved or available wifi networks and their state. |
| 66 | */ |
| 67 | public class WifiTracker { |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 68 | // TODO(b/36733768): Remove flag includeSaved and includePasspoints. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 69 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 70 | private static final String TAG = "WifiTracker"; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 71 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 72 | |
| 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 Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 80 | private static final int NUM_SCANS_TO_CONFIRM_AP_LOSS = 3; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 81 | |
| 82 | private final Context mContext; |
| 83 | private final WifiManager mWifiManager; |
| 84 | private final IntentFilter mFilter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 85 | private final ConnectivityManager mConnectivityManager; |
| 86 | private final NetworkRequest mNetworkRequest; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 87 | private final AtomicBoolean mConnected = new AtomicBoolean(false); |
| 88 | private final WifiListener mListener; |
| 89 | private final boolean mIncludeSaved; |
| 90 | private final boolean mIncludeScans; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 91 | private final boolean mIncludePasspoints; |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 92 | @VisibleForTesting final MainHandler mMainHandler; |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 93 | @VisibleForTesting final WorkHandler mWorkHandler; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 94 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 95 | private WifiTrackerNetworkCallback mNetworkCallback; |
| 96 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 97 | @GuardedBy("mLock") |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 98 | private boolean mRegistered; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 99 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 100 | /** |
| 101 | * The externally visible access point list. |
| 102 | * |
| 103 | * Updated using main handler. Clone of this collection is returned from |
| 104 | * {@link #getAccessPoints()} |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 105 | */ |
| 106 | private final List<AccessPoint> mAccessPoints = new ArrayList<>(); |
| 107 | |
| 108 | /** |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 109 | * The internal list of access points, synchronized on itself. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 110 | * |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 111 | * Never exposed outside this class. |
| 112 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 113 | @GuardedBy("mLock") |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 114 | private final List<AccessPoint> mInternalAccessPoints = new ArrayList<>(); |
| 115 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 116 | /** |
| 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 Ghuman | 221327d | 2017-05-02 15:58:39 -0700 | [diff] [blame] | 124 | //visible to both worker and main thread. |
| 125 | @GuardedBy("mLock") |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 126 | 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 Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 131 | private Integer mScanId = 0; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 132 | |
| 133 | private NetworkInfo mLastNetworkInfo; |
| 134 | private WifiInfo mLastInfo; |
| 135 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 136 | private final NetworkScoreManager mNetworkScoreManager; |
| 137 | private final WifiNetworkScoreCache mScoreCache; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 138 | private boolean mNetworkScoringUiEnabled; |
| 139 | private final ContentObserver mObserver; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 140 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 141 | @GuardedBy("mLock") |
| 142 | private final Set<NetworkKey> mRequestedScores = new ArraySet<>(); |
| 143 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 144 | @VisibleForTesting |
| 145 | Scanner mScanner; |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 146 | |
| 147 | @GuardedBy("mLock") |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 148 | private boolean mStaleScanResults = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 149 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 150 | public WifiTracker(Context context, WifiListener wifiListener, |
| 151 | boolean includeSaved, boolean includeScans) { |
| 152 | this(context, wifiListener, null, includeSaved, includeScans); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 153 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 154 | |
| 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 Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 168 | context.getSystemService(WifiManager.class), |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 169 | context.getSystemService(ConnectivityManager.class), |
| 170 | context.getSystemService(NetworkScoreManager.class), Looper.myLooper() |
| 171 | ); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @VisibleForTesting |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 175 | WifiTracker(Context context, WifiListener wifiListener, Looper workerLooper, |
| 176 | boolean includeSaved, boolean includeScans, boolean includePasspoints, |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 177 | WifiManager wifiManager, ConnectivityManager connectivityManager, |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 178 | NetworkScoreManager networkScoreManager, Looper currentLooper) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 179 | if (!includeSaved && !includeScans) { |
| 180 | throw new IllegalArgumentException("Must include either saved or scans"); |
| 181 | } |
| 182 | mContext = context; |
Jason Monk | bf3d1af | 2015-05-22 11:25:04 -0400 | [diff] [blame] | 183 | if (currentLooper == null) { |
| 184 | // When we aren't on a looper thread, default to the main. |
| 185 | currentLooper = Looper.getMainLooper(); |
| 186 | } |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 187 | mMainHandler = new MainHandler(currentLooper); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 188 | mWorkHandler = new WorkHandler( |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 189 | workerLooper != null ? workerLooper : currentLooper); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 190 | mWifiManager = wifiManager; |
| 191 | mIncludeSaved = includeSaved; |
| 192 | mIncludeScans = includeScans; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 193 | mIncludePasspoints = includePasspoints; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 194 | mListener = wifiListener; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 195 | mConnectivityManager = connectivityManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 196 | |
| 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 Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 208 | mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION); |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 209 | |
| 210 | mNetworkRequest = new NetworkRequest.Builder() |
| 211 | .clearCapabilities() |
| 212 | .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) |
| 213 | .build(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 214 | |
| 215 | mNetworkScoreManager = networkScoreManager; |
| 216 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 217 | mScoreCache = new WifiNetworkScoreCache(context, new CacheListener(mWorkHandler) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 218 | @Override |
| 219 | public void networkCacheUpdated(List<ScoredNetwork> networks) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 220 | synchronized (mLock) { |
| 221 | if (!mRegistered) return; |
| 222 | } |
| 223 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 224 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 225 | Log.v(TAG, "Score cache was updated with networks: " + networks); |
| 226 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 227 | updateNetworkScores(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 228 | } |
| 229 | }); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 230 | |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 240 | } |
| 241 | |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 242 | /** Synchronously update the list of access points with the latest information. */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 243 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 244 | public void forceUpdate() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 245 | synchronized (mLock) { |
| 246 | mWorkHandler.removeMessages(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
| 247 | mLastInfo = mWifiManager.getConnectionInfo(); |
| 248 | mLastNetworkInfo = mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 249 | |
| 250 | final List<ScanResult> newScanResults = mWifiManager.getScanResults(); |
| 251 | List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 252 | mInternalAccessPoints.clear(); |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 253 | updateAccessPointsLocked(newScanResults, configs); |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 254 | |
Sundeep Ghuman | 047b299 | 2017-05-10 17:45:13 -0700 | [diff] [blame] | 255 | if (DBG) { |
| 256 | Log.d(TAG, "force update - internal access point list:\n" + mInternalAccessPoints); |
| 257 | } |
| 258 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 259 | // 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 Ghuman | 047b299 | 2017-05-10 17:45:13 -0700 | [diff] [blame] | 263 | if (DBG) { |
| 264 | Log.d(TAG, "force update - external access point list:\n" + mAccessPoints); |
| 265 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 266 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 267 | } |
| 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 Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 284 | mScanner = null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Resume scanning for wifi networks after it has been paused. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 290 | * |
| 291 | * <p>The score cache should be registered before this method is invoked. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 292 | */ |
| 293 | public void resumeScanning() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 294 | if (mScanner == null) { |
| 295 | mScanner = new Scanner(); |
| 296 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 297 | |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 298 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_RESUME); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 299 | if (mWifiManager.isWifiEnabled()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 300 | mScanner.resume(); |
| 301 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 305 | * Start tracking wifi networks and scores. |
| 306 | * |
| 307 | * <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] | 308 | * then forceUpdate() must be called to populate getAccessPoints(). |
| 309 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 310 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 311 | public void startTracking() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 312 | synchronized (mLock) { |
| 313 | registerScoreCache(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 314 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 315 | 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 Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 320 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 321 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 332 | 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 Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 346 | synchronized (mLock) { |
| 347 | mRequestedScores.addAll(keys); |
| 348 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 351 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 352 | * Stop tracking wifi networks and scores. |
| 353 | * |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 354 | * <p>This should always be called when done with a WifiTracker (if startTracking was called) to |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 355 | * ensure proper cleanup and prevent any further callbacks from occurring. |
| 356 | * |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 357 | * <p>Calling this method will set the {@link #mStaleScanResults} bit, which prevents |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 358 | * {@link WifiListener#onAccessPointsChanged()} callbacks from being invoked (until the bit |
| 359 | * is unset on the next SCAN_RESULTS_AVAILABLE_ACTION). |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 360 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 361 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 362 | public void stopTracking() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 363 | synchronized (mLock) { |
| 364 | if (mRegistered) { |
| 365 | mContext.unregisterReceiver(mReceiver); |
| 366 | mConnectivityManager.unregisterNetworkCallback(mNetworkCallback); |
| 367 | mRegistered = false; |
| 368 | } |
Sundeep Ghuman | 75f9480 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 369 | unregisterScoreCache(); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 370 | pauseScanning(); |
| 371 | mContext.getContentResolver().unregisterContentObserver(mObserver); |
| 372 | |
| 373 | mWorkHandler.removePendingMessages(); |
| 374 | mMainHandler.removePendingMessages(); |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 375 | mStaleScanResults = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 376 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Sundeep Ghuman | 75f9480 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 379 | private void unregisterScoreCache() { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 380 | mNetworkScoreManager.unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, mScoreCache); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 381 | |
Sundeep Ghuman | 75f9480 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 382 | // 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 Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 387 | synchronized (mLock) { |
| 388 | mRequestedScores.clear(); |
| 389 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 393 | * Gets the current list of access points. Should be called from main thread, otherwise |
| 394 | * expect inconsistencies |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 395 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 396 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 397 | public List<AccessPoint> getAccessPoints() { |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 398 | return new ArrayList<>(mAccessPoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | public WifiManager getManager() { |
| 402 | return mWifiManager; |
| 403 | } |
| 404 | |
| 405 | public boolean isWifiEnabled() { |
| 406 | return mWifiManager.isWifiEnabled(); |
| 407 | } |
| 408 | |
| 409 | /** |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 410 | * Returns the number of saved networks on the device, regardless of whether the WifiTracker |
| 411 | * is tracking saved networks. |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 412 | * TODO(b/62292448): remove this function and update callsites to use WifiSavedConfigUtils |
| 413 | * directly. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 414 | */ |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 415 | public int getNumSavedNetworks() { |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 416 | return WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | public boolean isConnected() { |
| 420 | return mConnected.get(); |
| 421 | } |
| 422 | |
| 423 | public void dump(PrintWriter pw) { |
| 424 | pw.println(" - wifi tracker ------"); |
Jason Monk | 1904081 | 2015-06-01 15:00:57 -0400 | [diff] [blame] | 425 | for (AccessPoint accessPoint : getAccessPoints()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 426 | pw.println(" " + accessPoint); |
| 427 | } |
| 428 | } |
| 429 | |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 430 | private void handleResume() { |
| 431 | mScanResultCache.clear(); |
| 432 | mSeenBssids.clear(); |
| 433 | mScanId = 0; |
| 434 | } |
| 435 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 436 | private Collection<ScanResult> updateScanResultCache(final List<ScanResult> newResults) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 437 | mScanId++; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 438 | for (ScanResult newResult : newResults) { |
Mitchell Wills | 9df7904 | 2015-09-01 12:21:59 -0700 | [diff] [blame] | 439 | if (newResult.SSID == null || newResult.SSID.isEmpty()) { |
| 440 | continue; |
| 441 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 442 | 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 Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 465 | private WifiConfiguration getWifiConfigurationForNetworkId( |
| 466 | int networkId, final List<WifiConfiguration> configs) { |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 467 | 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 Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Safely modify {@link #mInternalAccessPoints} by acquiring {@link #mLock} first. |
| 480 | * |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 481 | * <p>Will not perform the update if {@link #mStaleScanResults} is true |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 482 | */ |
| 483 | private void updateAccessPoints() { |
| 484 | List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
| 485 | final List<ScanResult> newScanResults = mWifiManager.getScanResults(); |
| 486 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 487 | synchronized (mLock) { |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 488 | if(!mStaleScanResults) { |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 489 | updateAccessPointsLocked(newScanResults, configs); |
| 490 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 491 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 492 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 493 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 494 | /** |
| 495 | * Update the internal list of access points. |
| 496 | * |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 497 | * <p>Do not called directly (except for forceUpdate), use {@link #updateAccessPoints()} which |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 498 | * respects {@link #mStaleScanResults}. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 499 | */ |
| 500 | @GuardedBy("mLock") |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 501 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 509 | // Swap the current access points into a cached list. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 510 | List<AccessPoint> cachedAccessPoints = new ArrayList<>(mInternalAccessPoints); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 511 | ArrayList<AccessPoint> accessPoints = new ArrayList<>(); |
| 512 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 513 | // Clear out the configs so we don't think something is saved when it isn't. |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 514 | for (AccessPoint accessPoint : cachedAccessPoints) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 515 | accessPoint.clearConfig(); |
| 516 | } |
| 517 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 518 | /* 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 520 | Multimap<String, AccessPoint> apMap = new Multimap<String, AccessPoint>(); |
| 521 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 522 | final Collection<ScanResult> results = updateScanResultCache(newScanResults); |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 523 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 524 | if (configs != null) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 525 | for (WifiConfiguration config : configs) { |
| 526 | if (config.selfAdded && config.numAssociation == 0) { |
| 527 | continue; |
| 528 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 529 | AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 530 | if (mLastInfo != null && mLastNetworkInfo != null) { |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 531 | accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 532 | } |
| 533 | if (mIncludeSaved) { |
Sundeep Ghuman | dc6cf4b | 2017-03-08 16:18:29 -0800 | [diff] [blame] | 534 | // If saved network not present in scan result then set its Rssi to |
| 535 | // UNREACHABLE_RSSI |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 536 | boolean apFound = false; |
| 537 | for (ScanResult result : results) { |
| 538 | if (result.SSID.equals(accessPoint.getSsidStr())) { |
| 539 | apFound = true; |
| 540 | break; |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 541 | } |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 542 | } |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 543 | if (!apFound) { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 544 | accessPoint.setUnreachable(); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 545 | } |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 546 | accessPoints.add(accessPoint); |
| 547 | apMap.put(accessPoint.getSsidStr(), accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 548 | } 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 Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 551 | cachedAccessPoints.add(accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 556 | final List<NetworkKey> scoresToRequest = new ArrayList<>(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 557 | 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 Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 565 | NetworkKey key = NetworkKey.createFromScanResult(result); |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 566 | if (key != null && !mRequestedScores.contains(key)) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 567 | scoresToRequest.add(key); |
| 568 | } |
| 569 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 570 | boolean found = false; |
| 571 | for (AccessPoint accessPoint : apMap.getAll(result.SSID)) { |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 572 | // We want to evict old scan results if are current results are not stale |
| 573 | if (accessPoint.update(result, !mStaleScanResults)) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 574 | found = true; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | if (!found && mIncludeScans) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 579 | AccessPoint accessPoint = getCachedOrCreate(result, cachedAccessPoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 580 | if (mLastInfo != null && mLastNetworkInfo != null) { |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 581 | accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 582 | } |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 583 | |
Vinit Deshpande | a0d929e | 2015-06-12 15:18:44 -0700 | [diff] [blame] | 584 | if (result.isPasspointNetwork()) { |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 585 | // 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 Qiu | 0ffb89d | 2017-03-27 13:45:25 -0700 | [diff] [blame] | 589 | 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 Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 599 | accessPoints.add(accessPoint); |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 600 | apMap.put(accessPoint.getSsidStr(), accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 605 | requestScoresForNetworkKeys(scoresToRequest); |
| 606 | for (AccessPoint ap : accessPoints) { |
| 607 | ap.update(mScoreCache, mNetworkScoringUiEnabled); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 610 | // Pre-sort accessPoints to speed preference insertion |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 611 | Collections.sort(accessPoints); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 612 | |
| 613 | // Log accesspoints that were deleted |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 614 | 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 Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 627 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 628 | if (!found) |
| 629 | Log.d(TAG, "Did not find " + prevSsid + " in this scan"); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 630 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 631 | Log.d(TAG, "---- Done dumping SSIDs that were not seen on this scan ----"); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 632 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 633 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 634 | mInternalAccessPoints.clear(); |
| 635 | mInternalAccessPoints.addAll(accessPoints); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 636 | |
| 637 | mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 638 | } |
| 639 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 640 | @VisibleForTesting |
| 641 | AccessPoint getCachedOrCreate(ScanResult result, List<AccessPoint> cache) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 642 | final int N = cache.size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 643 | for (int i = 0; i < N; i++) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 644 | if (cache.get(i).matches(result)) { |
| 645 | AccessPoint ret = cache.remove(i); |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 646 | // evict old scan results only if we have fresh results |
| 647 | ret.update(result, !mStaleScanResults); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 648 | return ret; |
| 649 | } |
| 650 | } |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 651 | final AccessPoint accessPoint = new AccessPoint(mContext, result); |
| 652 | accessPoint.setListener(mAccessPointListenerAdapter); |
| 653 | return accessPoint; |
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 |
| 657 | AccessPoint getCachedOrCreate(WifiConfiguration config, List<AccessPoint> cache) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 658 | final int N = cache.size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 659 | for (int i = 0; i < N; i++) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 660 | if (cache.get(i).matches(config)) { |
| 661 | AccessPoint ret = cache.remove(i); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 662 | ret.loadConfig(config); |
| 663 | return ret; |
| 664 | } |
| 665 | } |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 666 | final AccessPoint accessPoint = new AccessPoint(mContext, config); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 667 | accessPoint.setListener(mAccessPointListenerAdapter); |
| 668 | return accessPoint; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | private void updateNetworkInfo(NetworkInfo networkInfo) { |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 672 | /* sticky broadcasts can call this when wifi is disabled */ |
| 673 | if (!mWifiManager.isWifiEnabled()) { |
| 674 | mMainHandler.sendEmptyMessage(MainHandler.MSG_PAUSE_SCANNING); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 675 | clearAccessPointsAndConditionallyUpdate(); |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 676 | return; |
| 677 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 678 | |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 679 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 684 | } |
| 685 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 686 | if (networkInfo != null) { |
| 687 | mLastNetworkInfo = networkInfo; |
| 688 | } |
| 689 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 690 | WifiConfiguration connectionConfig = null; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 691 | mLastInfo = mWifiManager.getConnectionInfo(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 692 | if (mLastInfo != null) { |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 693 | connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId(), |
| 694 | mWifiManager.getConfiguredNetworks()); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 697 | boolean updated = false; |
| 698 | boolean reorder = false; // Only reorder if connected AP was changed |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 699 | |
| 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 Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 712 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 713 | |
| 714 | if (reorder) Collections.sort(mInternalAccessPoints); |
| 715 | |
| 716 | if (updated) mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 717 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 720 | 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 Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 731 | /** |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 732 | * Update all the internal access points rankingScores, badge and metering. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 733 | * |
| 734 | * <p>Will trigger a resort and notify listeners of changes if applicable. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 735 | * |
| 736 | * <p>Synchronized on {@link #mLock}. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 737 | */ |
| 738 | private void updateNetworkScores() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 739 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 745 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 746 | if (updated) { |
| 747 | Collections.sort(mInternalAccessPoints); |
| 748 | mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED); |
| 749 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | private void updateWifiState(int state) { |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 754 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_WIFI_STATE, state, 0).sendToTarget(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 755 | if (!mWifiManager.isWifiEnabled()) { |
| 756 | clearAccessPointsAndConditionallyUpdate(); |
| 757 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | public static List<AccessPoint> getCurrentAccessPoints(Context context, boolean includeSaved, |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 761 | boolean includeScans, boolean includePasspoints) { |
| 762 | WifiTracker tracker = new WifiTracker(context, |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 763 | null, null, includeSaved, includeScans, includePasspoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 764 | tracker.forceUpdate(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 765 | tracker.copyAndNotifyListeners(false /*notifyListeners*/); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 766 | 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 Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 774 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 775 | if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { |
| 776 | updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, |
| 777 | WifiManager.WIFI_STATE_UNKNOWN)); |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 778 | } 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 Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 787 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 788 | } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 789 | NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 790 | |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 791 | if(mConnected.get() != info.isConnected()) { |
| 792 | mConnected.set(info.isConnected()); |
| 793 | mMainHandler.sendEmptyMessage(MainHandler.MSG_CONNECTED_CHANGED); |
| 794 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 795 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 796 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info) |
| 797 | .sendToTarget(); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 798 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 799 | } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 800 | NetworkInfo info = |
| 801 | mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
| 802 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info) |
| 803 | .sendToTarget(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | }; |
| 807 | |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 808 | 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 Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 819 | @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 Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 824 | private static final int MSG_RESUME_SCANNING = 3; |
| 825 | private static final int MSG_PAUSE_SCANNING = 4; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 826 | |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 827 | public MainHandler(Looper looper) { |
| 828 | super(looper); |
| 829 | } |
| 830 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 831 | @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 Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 844 | // 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 Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 847 | if (mStaleScanResults) { |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 848 | copyAndNotifyListeners(false /*notifyListeners*/); |
| 849 | } else { |
| 850 | copyAndNotifyListeners(true /*notifyListeners*/); |
| 851 | mListener.onAccessPointsChanged(); |
| 852 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 853 | break; |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 854 | 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 Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 864 | } |
| 865 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 866 | |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 867 | void removePendingMessages() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 868 | removeMessages(MSG_ACCESS_POINT_CHANGED); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 869 | removeMessages(MSG_CONNECTED_CHANGED); |
| 870 | removeMessages(MSG_WIFI_STATE_CHANGED); |
| 871 | removeMessages(MSG_PAUSE_SCANNING); |
| 872 | removeMessages(MSG_RESUME_SCANNING); |
| 873 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 874 | } |
| 875 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 876 | @VisibleForTesting |
| 877 | final class WorkHandler extends Handler { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 878 | private static final int MSG_UPDATE_ACCESS_POINTS = 0; |
| 879 | private static final int MSG_UPDATE_NETWORK_INFO = 1; |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 880 | private static final int MSG_RESUME = 2; |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 881 | private static final int MSG_UPDATE_WIFI_STATE = 3; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 882 | |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 883 | private static final int CLEAR_STALE_SCAN_RESULTS = 1; |
| 884 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 885 | public WorkHandler(Looper looper) { |
| 886 | super(looper); |
| 887 | } |
| 888 | |
| 889 | @Override |
| 890 | public void handleMessage(Message msg) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 891 | synchronized (mLock) { |
| 892 | processMessage(msg); |
| 893 | } |
| 894 | } |
| 895 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 896 | private void processMessage(Message msg) { |
| 897 | if (!mRegistered) return; |
| 898 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 899 | switch (msg.what) { |
| 900 | case MSG_UPDATE_ACCESS_POINTS: |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 901 | if (msg.arg1 == CLEAR_STALE_SCAN_RESULTS) { |
Sundeep Ghuman | a580215f | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 902 | mStaleScanResults = false; |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 903 | } |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 904 | updateAccessPoints(); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 905 | break; |
| 906 | case MSG_UPDATE_NETWORK_INFO: |
| 907 | updateNetworkInfo((NetworkInfo) msg.obj); |
| 908 | break; |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 909 | case MSG_RESUME: |
| 910 | handleResume(); |
| 911 | break; |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 912 | 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 Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 929 | } |
| 930 | } |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 931 | |
| 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 Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 937 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 938 | } |
| 939 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 940 | @VisibleForTesting |
| 941 | class Scanner extends Handler { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 942 | static final int MSG_SCAN = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 943 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 944 | private int mRetry = 0; |
| 945 | |
| 946 | void resume() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 947 | if (!hasMessages(MSG_SCAN)) { |
| 948 | sendEmptyMessage(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
| 952 | void forceScan() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 953 | removeMessages(MSG_SCAN); |
| 954 | sendEmptyMessage(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | void pause() { |
| 958 | mRetry = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 959 | removeMessages(MSG_SCAN); |
| 960 | } |
| 961 | |
| 962 | @VisibleForTesting |
| 963 | boolean isScanning() { |
| 964 | return hasMessages(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | @Override |
| 968 | public void handleMessage(Message message) { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 969 | if (message.what != MSG_SCAN) return; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 970 | 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 Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1030 | |
| 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 Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 1056 | * accesspoint listeners. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1057 | * |
| 1058 | * @param notifyListeners if true, accesspoint listeners are notified, otherwise notifications |
| 1059 | * dropped. |
| 1060 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 1061 | @MainThread |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1062 | 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 Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 1072 | if (DBG) { |
| 1073 | Log.d(TAG, "Starting to copy AP items on the MainHandler"); |
| 1074 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 1075 | synchronized (mLock) { |
Sundeep Ghuman | 221327d | 2017-05-02 15:58:39 -0700 | [diff] [blame] | 1076 | if (notifyListeners) { |
| 1077 | notificationMap = mAccessPointListenerAdapter.mPendingNotifications.clone(); |
| 1078 | } |
| 1079 | |
| 1080 | mAccessPointListenerAdapter.mPendingNotifications.clear(); |
| 1081 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1082 | 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 Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1091 | } |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1114 | } |