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; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 39 | import android.os.ConditionVariable; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 40 | import android.os.Handler; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 41 | import android.os.Looper; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 42 | import android.os.Message; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 43 | import android.provider.Settings; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 44 | import android.support.annotation.GuardedBy; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 45 | import android.util.ArraySet; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 46 | import android.util.Log; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 47 | import android.util.SparseArray; |
| 48 | import android.util.SparseIntArray; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 49 | import android.widget.Toast; |
| 50 | |
| 51 | import com.android.internal.annotations.VisibleForTesting; |
| 52 | import com.android.settingslib.R; |
| 53 | |
| 54 | import java.io.PrintWriter; |
| 55 | import java.util.ArrayList; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 56 | import java.util.Collection; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 57 | import java.util.Collections; |
| 58 | import java.util.HashMap; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 59 | import java.util.Iterator; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 60 | import java.util.List; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 61 | import java.util.Map; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 62 | import java.util.Set; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 63 | import java.util.concurrent.atomic.AtomicBoolean; |
| 64 | |
| 65 | /** |
| 66 | * Tracks saved or available wifi networks and their state. |
| 67 | */ |
| 68 | public class WifiTracker { |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 69 | // TODO(b/36733768): Remove flag includeSaved and includePasspoints. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 70 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 71 | private static final String TAG = "WifiTracker"; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 72 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 73 | |
| 74 | /** verbose logging flag. this flag is set thru developer debugging options |
| 75 | * and used so as to assist with in-the-field WiFi connectivity debugging */ |
| 76 | public static int sVerboseLogging = 0; |
| 77 | |
| 78 | // TODO: Allow control of this? |
| 79 | // Combo scans can take 5-6s to complete - set to 10s. |
| 80 | private static final int WIFI_RESCAN_INTERVAL_MS = 10 * 1000; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 81 | private static final int NUM_SCANS_TO_CONFIRM_AP_LOSS = 3; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 82 | |
| 83 | private final Context mContext; |
| 84 | private final WifiManager mWifiManager; |
| 85 | private final IntentFilter mFilter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 86 | private final ConnectivityManager mConnectivityManager; |
| 87 | private final NetworkRequest mNetworkRequest; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 88 | private final AtomicBoolean mConnected = new AtomicBoolean(false); |
| 89 | private final WifiListener mListener; |
| 90 | private final boolean mIncludeSaved; |
| 91 | private final boolean mIncludeScans; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 92 | private final boolean mIncludePasspoints; |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 93 | @VisibleForTesting final MainHandler mMainHandler; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 94 | private final WorkHandler mWorkHandler; |
| 95 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 96 | private WifiTrackerNetworkCallback mNetworkCallback; |
| 97 | |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 98 | private int mNumSavedNetworks; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 99 | |
| 100 | @GuardedBy("mLock") |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 101 | private boolean mRegistered; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 102 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 103 | /** |
| 104 | * The externally visible access point list. |
| 105 | * |
| 106 | * Updated using main handler. Clone of this collection is returned from |
| 107 | * {@link #getAccessPoints()} |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 108 | */ |
| 109 | private final List<AccessPoint> mAccessPoints = new ArrayList<>(); |
| 110 | |
| 111 | /** |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 112 | * The internal list of access points, synchronized on itself. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 113 | * |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 114 | * Never exposed outside this class. |
| 115 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 116 | @GuardedBy("mLock") |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 117 | private final List<AccessPoint> mInternalAccessPoints = new ArrayList<>(); |
| 118 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Synchronization lock for managing concurrency between main and worker threads. |
| 121 | * |
| 122 | * <p>This lock should be held for all background work. |
| 123 | * TODO(b/37674366): Remove the worker thread so synchronization is no longer necessary. |
| 124 | */ |
| 125 | private final Object mLock = new Object(); |
| 126 | |
Sundeep Ghuman | 221327d | 2017-05-02 15:58:39 -0700 | [diff] [blame] | 127 | //visible to both worker and main thread. |
| 128 | @GuardedBy("mLock") |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 129 | private final AccessPointListenerAdapter mAccessPointListenerAdapter |
| 130 | = new AccessPointListenerAdapter(); |
| 131 | |
| 132 | private final HashMap<String, Integer> mSeenBssids = new HashMap<>(); |
| 133 | private final HashMap<String, ScanResult> mScanResultCache = new HashMap<>(); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 134 | private Integer mScanId = 0; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 135 | |
| 136 | private NetworkInfo mLastNetworkInfo; |
| 137 | private WifiInfo mLastInfo; |
| 138 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 139 | private final NetworkScoreManager mNetworkScoreManager; |
| 140 | private final WifiNetworkScoreCache mScoreCache; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 141 | private boolean mNetworkScoringUiEnabled; |
| 142 | private final ContentObserver mObserver; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 143 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 144 | @GuardedBy("mLock") |
| 145 | private final Set<NetworkKey> mRequestedScores = new ArraySet<>(); |
| 146 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 147 | @VisibleForTesting |
| 148 | Scanner mScanner; |
| 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 | |
| 242 | /** |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 243 | * Synchronously update the list of access points with the latest information. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 244 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 245 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 246 | public void forceUpdate() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 247 | synchronized (mLock) { |
| 248 | mWorkHandler.removeMessages(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
| 249 | mLastInfo = mWifiManager.getConnectionInfo(); |
| 250 | mLastNetworkInfo = mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
| 251 | updateAccessPointsLocked(); |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 252 | |
Sundeep Ghuman | 047b299 | 2017-05-10 17:45:13 -0700 | [diff] [blame^] | 253 | if (DBG) { |
| 254 | Log.d(TAG, "force update - internal access point list:\n" + mInternalAccessPoints); |
| 255 | } |
| 256 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 257 | // Synchronously copy access points |
| 258 | mMainHandler.removeMessages(MainHandler.MSG_ACCESS_POINT_CHANGED); |
| 259 | mMainHandler.handleMessage( |
| 260 | Message.obtain(mMainHandler, MainHandler.MSG_ACCESS_POINT_CHANGED)); |
Sundeep Ghuman | 047b299 | 2017-05-10 17:45:13 -0700 | [diff] [blame^] | 261 | if (DBG) { |
| 262 | Log.d(TAG, "force update - external access point list:\n" + mAccessPoints); |
| 263 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 264 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Force a scan for wifi networks to happen now. |
| 269 | */ |
| 270 | public void forceScan() { |
| 271 | if (mWifiManager.isWifiEnabled() && mScanner != null) { |
| 272 | mScanner.forceScan(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Temporarily stop scanning for wifi networks. |
| 278 | */ |
| 279 | public void pauseScanning() { |
| 280 | if (mScanner != null) { |
| 281 | mScanner.pause(); |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 282 | mScanner = null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Resume scanning for wifi networks after it has been paused. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 288 | * |
| 289 | * <p>The score cache should be registered before this method is invoked. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 290 | */ |
| 291 | public void resumeScanning() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 292 | if (mScanner == null) { |
| 293 | mScanner = new Scanner(); |
| 294 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 295 | |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 296 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_RESUME); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 297 | if (mWifiManager.isWifiEnabled()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 298 | mScanner.resume(); |
| 299 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 303 | * Start tracking wifi networks and scores. |
| 304 | * |
| 305 | * <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] | 306 | * then forceUpdate() must be called to populate getAccessPoints(). |
| 307 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 308 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 309 | public void startTracking() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 310 | synchronized (mLock) { |
| 311 | registerScoreCache(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 312 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 313 | mContext.getContentResolver().registerContentObserver( |
| 314 | Settings.Global.getUriFor(Settings.Global.NETWORK_SCORING_UI_ENABLED), |
| 315 | false /* notifyForDescendants */, |
| 316 | mObserver); |
| 317 | mObserver.onChange(false /* selfChange */); // Set mScoringUiEnabled |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 318 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 319 | resumeScanning(); |
| 320 | if (!mRegistered) { |
| 321 | mContext.registerReceiver(mReceiver, mFilter); |
| 322 | // NetworkCallback objects cannot be reused. http://b/20701525 . |
| 323 | mNetworkCallback = new WifiTrackerNetworkCallback(); |
| 324 | mConnectivityManager.registerNetworkCallback(mNetworkRequest, mNetworkCallback); |
| 325 | mRegistered = true; |
| 326 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 330 | private void registerScoreCache() { |
| 331 | mNetworkScoreManager.registerNetworkScoreCache( |
| 332 | NetworkKey.TYPE_WIFI, |
| 333 | mScoreCache, |
| 334 | NetworkScoreManager.CACHE_FILTER_SCAN_RESULTS); |
| 335 | } |
| 336 | |
| 337 | private void requestScoresForNetworkKeys(Collection<NetworkKey> keys) { |
| 338 | if (keys.isEmpty()) return; |
| 339 | |
| 340 | if (DBG) { |
| 341 | Log.d(TAG, "Requesting scores for Network Keys: " + keys); |
| 342 | } |
| 343 | mNetworkScoreManager.requestScores(keys.toArray(new NetworkKey[keys.size()])); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 344 | synchronized (mLock) { |
| 345 | mRequestedScores.addAll(keys); |
| 346 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 349 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 350 | * Stop tracking wifi networks and scores. |
| 351 | * |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 352 | * <p>This should always be called when done with a WifiTracker (if startTracking was called) to |
| 353 | * ensure proper cleanup and prevent any further callbacks from occuring. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 354 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 355 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 356 | public void stopTracking() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 357 | synchronized (mLock) { |
| 358 | if (mRegistered) { |
| 359 | mContext.unregisterReceiver(mReceiver); |
| 360 | mConnectivityManager.unregisterNetworkCallback(mNetworkCallback); |
| 361 | mRegistered = false; |
| 362 | } |
| 363 | unregisterAndClearScoreCache(); |
| 364 | pauseScanning(); |
| 365 | mContext.getContentResolver().unregisterContentObserver(mObserver); |
| 366 | |
| 367 | mWorkHandler.removePendingMessages(); |
| 368 | mMainHandler.removePendingMessages(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 369 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 372 | private void unregisterAndClearScoreCache() { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 373 | mNetworkScoreManager.unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, mScoreCache); |
| 374 | mScoreCache.clearScores(); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 375 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 376 | // Synchronize on mLock to avoid concurrent modification during updateAccessPointsLocked |
| 377 | synchronized (mLock) { |
| 378 | mRequestedScores.clear(); |
| 379 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 383 | * Gets the current list of access points. Should be called from main thread, otherwise |
| 384 | * expect inconsistencies |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 385 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 386 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 387 | public List<AccessPoint> getAccessPoints() { |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 388 | return new ArrayList<>(mAccessPoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | public WifiManager getManager() { |
| 392 | return mWifiManager; |
| 393 | } |
| 394 | |
| 395 | public boolean isWifiEnabled() { |
| 396 | return mWifiManager.isWifiEnabled(); |
| 397 | } |
| 398 | |
| 399 | /** |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 400 | * Returns the number of saved networks on the device, regardless of whether the WifiTracker |
| 401 | * is tracking saved networks. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 402 | */ |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 403 | public int getNumSavedNetworks() { |
| 404 | return mNumSavedNetworks; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | public boolean isConnected() { |
| 408 | return mConnected.get(); |
| 409 | } |
| 410 | |
| 411 | public void dump(PrintWriter pw) { |
| 412 | pw.println(" - wifi tracker ------"); |
Jason Monk | 1904081 | 2015-06-01 15:00:57 -0400 | [diff] [blame] | 413 | for (AccessPoint accessPoint : getAccessPoints()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 414 | pw.println(" " + accessPoint); |
| 415 | } |
| 416 | } |
| 417 | |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 418 | private void handleResume() { |
| 419 | mScanResultCache.clear(); |
| 420 | mSeenBssids.clear(); |
| 421 | mScanId = 0; |
| 422 | } |
| 423 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 424 | private Collection<ScanResult> fetchScanResults() { |
| 425 | mScanId++; |
| 426 | final List<ScanResult> newResults = mWifiManager.getScanResults(); |
| 427 | for (ScanResult newResult : newResults) { |
Mitchell Wills | 9df7904 | 2015-09-01 12:21:59 -0700 | [diff] [blame] | 428 | if (newResult.SSID == null || newResult.SSID.isEmpty()) { |
| 429 | continue; |
| 430 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 431 | mScanResultCache.put(newResult.BSSID, newResult); |
| 432 | mSeenBssids.put(newResult.BSSID, mScanId); |
| 433 | } |
| 434 | |
| 435 | if (mScanId > NUM_SCANS_TO_CONFIRM_AP_LOSS) { |
| 436 | if (DBG) Log.d(TAG, "------ Dumping SSIDs that were expired on this scan ------"); |
| 437 | Integer threshold = mScanId - NUM_SCANS_TO_CONFIRM_AP_LOSS; |
| 438 | for (Iterator<Map.Entry<String, Integer>> it = mSeenBssids.entrySet().iterator(); |
| 439 | it.hasNext(); /* nothing */) { |
| 440 | Map.Entry<String, Integer> e = it.next(); |
| 441 | if (e.getValue() < threshold) { |
| 442 | ScanResult result = mScanResultCache.get(e.getKey()); |
| 443 | if (DBG) Log.d(TAG, "Removing " + e.getKey() + ":(" + result.SSID + ")"); |
| 444 | mScanResultCache.remove(e.getKey()); |
| 445 | it.remove(); |
| 446 | } |
| 447 | } |
| 448 | if (DBG) Log.d(TAG, "---- Done Dumping SSIDs that were expired on this scan ----"); |
| 449 | } |
| 450 | |
| 451 | return mScanResultCache.values(); |
| 452 | } |
| 453 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 454 | private WifiConfiguration getWifiConfigurationForNetworkId(int networkId) { |
| 455 | final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
| 456 | if (configs != null) { |
| 457 | for (WifiConfiguration config : configs) { |
| 458 | if (mLastInfo != null && networkId == config.networkId && |
| 459 | !(config.selfAdded && config.numAssociation == 0)) { |
| 460 | return config; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | return null; |
| 465 | } |
| 466 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 467 | /** Safely modify {@link #mInternalAccessPoints} by acquiring {@link #mLock} first. */ |
| 468 | private void updateAccessPointsLocked() { |
| 469 | synchronized (mLock) { |
| 470 | updateAccessPoints(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 471 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 472 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 473 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 474 | /** |
| 475 | * Update the internal list of access points. |
| 476 | * |
| 477 | * <p>Should never be called directly, use {@link #updateAccessPointsLocked()} instead. |
| 478 | */ |
| 479 | @GuardedBy("mLock") |
| 480 | private void updateAccessPoints() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 481 | // Swap the current access points into a cached list. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 482 | List<AccessPoint> cachedAccessPoints = new ArrayList<>(mInternalAccessPoints); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 483 | ArrayList<AccessPoint> accessPoints = new ArrayList<>(); |
| 484 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 485 | // 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] | 486 | for (AccessPoint accessPoint : cachedAccessPoints) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 487 | accessPoint.clearConfig(); |
| 488 | } |
| 489 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 490 | /* Lookup table to more quickly update AccessPoints by only considering objects with the |
| 491 | * correct SSID. Maps SSID -> List of AccessPoints with the given SSID. */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 492 | Multimap<String, AccessPoint> apMap = new Multimap<String, AccessPoint>(); |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 493 | WifiConfiguration connectionConfig = null; |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 494 | if (mLastInfo != null) { |
| 495 | connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId()); |
| 496 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 497 | |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 498 | final Collection<ScanResult> results = fetchScanResults(); |
| 499 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 500 | final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
| 501 | if (configs != null) { |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 502 | mNumSavedNetworks = 0; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 503 | for (WifiConfiguration config : configs) { |
| 504 | if (config.selfAdded && config.numAssociation == 0) { |
| 505 | continue; |
| 506 | } |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 507 | mNumSavedNetworks++; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 508 | AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 509 | if (mLastInfo != null && mLastNetworkInfo != null) { |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 510 | accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 511 | } |
| 512 | if (mIncludeSaved) { |
Sundeep Ghuman | dc6cf4b | 2017-03-08 16:18:29 -0800 | [diff] [blame] | 513 | // If saved network not present in scan result then set its Rssi to |
| 514 | // UNREACHABLE_RSSI |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 515 | boolean apFound = false; |
| 516 | for (ScanResult result : results) { |
| 517 | if (result.SSID.equals(accessPoint.getSsidStr())) { |
| 518 | apFound = true; |
| 519 | break; |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 520 | } |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 521 | } |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 522 | if (!apFound) { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 523 | accessPoint.setUnreachable(); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 524 | } |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 525 | accessPoints.add(accessPoint); |
| 526 | apMap.put(accessPoint.getSsidStr(), accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 527 | } else { |
| 528 | // If we aren't using saved networks, drop them into the cache so that |
| 529 | // we have access to their saved info. |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 530 | cachedAccessPoints.add(accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 535 | final List<NetworkKey> scoresToRequest = new ArrayList<>(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 536 | if (results != null) { |
| 537 | for (ScanResult result : results) { |
| 538 | // Ignore hidden and ad-hoc networks. |
| 539 | if (result.SSID == null || result.SSID.length() == 0 || |
| 540 | result.capabilities.contains("[IBSS]")) { |
| 541 | continue; |
| 542 | } |
| 543 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 544 | NetworkKey key = NetworkKey.createFromScanResult(result); |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 545 | if (key != null && !mRequestedScores.contains(key)) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 546 | scoresToRequest.add(key); |
| 547 | } |
| 548 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 549 | boolean found = false; |
| 550 | for (AccessPoint accessPoint : apMap.getAll(result.SSID)) { |
| 551 | if (accessPoint.update(result)) { |
| 552 | found = true; |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | if (!found && mIncludeScans) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 557 | AccessPoint accessPoint = getCachedOrCreate(result, cachedAccessPoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 558 | if (mLastInfo != null && mLastNetworkInfo != null) { |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 559 | accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 560 | } |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 561 | |
Vinit Deshpande | a0d929e | 2015-06-12 15:18:44 -0700 | [diff] [blame] | 562 | if (result.isPasspointNetwork()) { |
Peter Qiu | 2c3b5ee2 | 2017-02-01 11:49:15 -0800 | [diff] [blame] | 563 | // Retrieve a WifiConfiguration for a Passpoint provider that matches |
| 564 | // the given ScanResult. This is used for showing that a given AP |
| 565 | // (ScanResult) is available via a Passpoint provider (provider friendly |
| 566 | // name). |
Peter Qiu | 0ffb89d | 2017-03-27 13:45:25 -0700 | [diff] [blame] | 567 | try { |
| 568 | WifiConfiguration config = mWifiManager.getMatchingWifiConfig(result); |
| 569 | if (config != null) { |
| 570 | accessPoint.update(config); |
| 571 | } |
| 572 | } catch (UnsupportedOperationException e) { |
| 573 | // Passpoint not supported on the device. |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 577 | accessPoints.add(accessPoint); |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 578 | apMap.put(accessPoint.getSsidStr(), accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 583 | requestScoresForNetworkKeys(scoresToRequest); |
| 584 | for (AccessPoint ap : accessPoints) { |
| 585 | ap.update(mScoreCache, mNetworkScoringUiEnabled); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 588 | // Pre-sort accessPoints to speed preference insertion |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 589 | Collections.sort(accessPoints); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 590 | |
| 591 | // Log accesspoints that were deleted |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 592 | if (DBG) { |
| 593 | Log.d(TAG, "------ Dumping SSIDs that were not seen on this scan ------"); |
| 594 | for (AccessPoint prevAccessPoint : mInternalAccessPoints) { |
| 595 | if (prevAccessPoint.getSsid() == null) |
| 596 | continue; |
| 597 | String prevSsid = prevAccessPoint.getSsidStr(); |
| 598 | boolean found = false; |
| 599 | for (AccessPoint newAccessPoint : accessPoints) { |
| 600 | if (newAccessPoint.getSsid() != null && newAccessPoint.getSsid() |
| 601 | .equals(prevSsid)) { |
| 602 | found = true; |
| 603 | break; |
| 604 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 605 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 606 | if (!found) |
| 607 | Log.d(TAG, "Did not find " + prevSsid + " in this scan"); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 608 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 609 | 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] | 610 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 611 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 612 | mInternalAccessPoints.clear(); |
| 613 | mInternalAccessPoints.addAll(accessPoints); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 614 | |
| 615 | mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 616 | } |
| 617 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 618 | @VisibleForTesting |
| 619 | AccessPoint getCachedOrCreate(ScanResult result, List<AccessPoint> cache) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 620 | final int N = cache.size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 621 | for (int i = 0; i < N; i++) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 622 | if (cache.get(i).matches(result)) { |
| 623 | AccessPoint ret = cache.remove(i); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 624 | ret.update(result); |
| 625 | return ret; |
| 626 | } |
| 627 | } |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 628 | final AccessPoint accessPoint = new AccessPoint(mContext, result); |
| 629 | accessPoint.setListener(mAccessPointListenerAdapter); |
| 630 | return accessPoint; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 631 | } |
| 632 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 633 | @VisibleForTesting |
| 634 | AccessPoint getCachedOrCreate(WifiConfiguration config, List<AccessPoint> cache) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 635 | final int N = cache.size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 636 | for (int i = 0; i < N; i++) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 637 | if (cache.get(i).matches(config)) { |
| 638 | AccessPoint ret = cache.remove(i); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 639 | ret.loadConfig(config); |
| 640 | return ret; |
| 641 | } |
| 642 | } |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 643 | final AccessPoint accessPoint = new AccessPoint(mContext, config); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 644 | accessPoint.setListener(mAccessPointListenerAdapter); |
| 645 | return accessPoint; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | private void updateNetworkInfo(NetworkInfo networkInfo) { |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 649 | /* sticky broadcasts can call this when wifi is disabled */ |
| 650 | if (!mWifiManager.isWifiEnabled()) { |
| 651 | mMainHandler.sendEmptyMessage(MainHandler.MSG_PAUSE_SCANNING); |
| 652 | return; |
| 653 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 654 | |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 655 | if (networkInfo != null && |
| 656 | networkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR) { |
| 657 | mMainHandler.sendEmptyMessage(MainHandler.MSG_PAUSE_SCANNING); |
| 658 | } else { |
| 659 | mMainHandler.sendEmptyMessage(MainHandler.MSG_RESUME_SCANNING); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 662 | if (networkInfo != null) { |
| 663 | mLastNetworkInfo = networkInfo; |
| 664 | } |
| 665 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 666 | WifiConfiguration connectionConfig = null; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 667 | mLastInfo = mWifiManager.getConnectionInfo(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 668 | if (mLastInfo != null) { |
| 669 | connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId()); |
| 670 | } |
| 671 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 672 | boolean updated = false; |
| 673 | boolean reorder = false; // Only reorder if connected AP was changed |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 674 | |
| 675 | synchronized (mLock) { |
| 676 | for (int i = mInternalAccessPoints.size() - 1; i >= 0; --i) { |
| 677 | AccessPoint ap = mInternalAccessPoints.get(i); |
| 678 | boolean previouslyConnected = ap.isActive(); |
| 679 | if (ap.update(connectionConfig, mLastInfo, mLastNetworkInfo)) { |
| 680 | updated = true; |
| 681 | if (previouslyConnected != ap.isActive()) reorder = true; |
| 682 | } |
| 683 | if (ap.update(mScoreCache, mNetworkScoringUiEnabled)) { |
| 684 | reorder = true; |
| 685 | updated = true; |
| 686 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 687 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 688 | |
| 689 | if (reorder) Collections.sort(mInternalAccessPoints); |
| 690 | |
| 691 | if (updated) mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 692 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /** |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 696 | * Update all the internal access points rankingScores, badge and metering. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 697 | * |
| 698 | * <p>Will trigger a resort and notify listeners of changes if applicable. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 699 | * |
| 700 | * <p>Synchronized on {@link #mLock}. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 701 | */ |
| 702 | private void updateNetworkScores() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 703 | synchronized (mLock) { |
| 704 | boolean updated = false; |
| 705 | for (int i = 0; i < mInternalAccessPoints.size(); i++) { |
| 706 | if (mInternalAccessPoints.get(i).update(mScoreCache, mNetworkScoringUiEnabled)) { |
| 707 | updated = true; |
| 708 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 709 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 710 | if (updated) { |
| 711 | Collections.sort(mInternalAccessPoints); |
| 712 | mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED); |
| 713 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | private void updateWifiState(int state) { |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 718 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_WIFI_STATE, state, 0).sendToTarget(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | public static List<AccessPoint> getCurrentAccessPoints(Context context, boolean includeSaved, |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 722 | boolean includeScans, boolean includePasspoints) { |
| 723 | WifiTracker tracker = new WifiTracker(context, |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 724 | null, null, includeSaved, includeScans, includePasspoints); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 725 | tracker.forceUpdate(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 726 | tracker.copyAndNotifyListeners(false /*notifyListeners*/); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 727 | return tracker.getAccessPoints(); |
| 728 | } |
| 729 | |
| 730 | @VisibleForTesting |
| 731 | final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 732 | @Override |
| 733 | public void onReceive(Context context, Intent intent) { |
| 734 | String action = intent.getAction(); |
| 735 | if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { |
| 736 | updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, |
| 737 | WifiManager.WIFI_STATE_UNKNOWN)); |
| 738 | } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) || |
| 739 | WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) || |
| 740 | WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 741 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 742 | } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { |
| 743 | NetworkInfo info = (NetworkInfo) intent.getParcelableExtra( |
| 744 | WifiManager.EXTRA_NETWORK_INFO); |
| 745 | mConnected.set(info.isConnected()); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 746 | |
| 747 | mMainHandler.sendEmptyMessage(MainHandler.MSG_CONNECTED_CHANGED); |
| 748 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 749 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info) |
| 750 | .sendToTarget(); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 751 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 752 | } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 753 | NetworkInfo info = |
| 754 | mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
| 755 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info) |
| 756 | .sendToTarget(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | }; |
| 760 | |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 761 | private final class WifiTrackerNetworkCallback extends ConnectivityManager.NetworkCallback { |
| 762 | public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { |
| 763 | if (network.equals(mWifiManager.getCurrentNetwork())) { |
| 764 | // We don't send a NetworkInfo object along with this message, because even if we |
| 765 | // fetch one from ConnectivityManager, it might be older than the most recent |
| 766 | // NetworkInfo message we got via a WIFI_STATE_CHANGED broadcast. |
| 767 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO); |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 772 | @VisibleForTesting |
| 773 | final class MainHandler extends Handler { |
| 774 | @VisibleForTesting static final int MSG_CONNECTED_CHANGED = 0; |
| 775 | @VisibleForTesting static final int MSG_WIFI_STATE_CHANGED = 1; |
| 776 | @VisibleForTesting static final int MSG_ACCESS_POINT_CHANGED = 2; |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 777 | private static final int MSG_RESUME_SCANNING = 3; |
| 778 | private static final int MSG_PAUSE_SCANNING = 4; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 779 | |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 780 | public MainHandler(Looper looper) { |
| 781 | super(looper); |
| 782 | } |
| 783 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 784 | @Override |
| 785 | public void handleMessage(Message msg) { |
| 786 | if (mListener == null) { |
| 787 | return; |
| 788 | } |
| 789 | switch (msg.what) { |
| 790 | case MSG_CONNECTED_CHANGED: |
| 791 | mListener.onConnectedChanged(); |
| 792 | break; |
| 793 | case MSG_WIFI_STATE_CHANGED: |
| 794 | mListener.onWifiStateChanged(msg.arg1); |
| 795 | break; |
| 796 | case MSG_ACCESS_POINT_CHANGED: |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 797 | copyAndNotifyListeners(true /*notifyListeners*/); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 798 | mListener.onAccessPointsChanged(); |
| 799 | break; |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 800 | case MSG_RESUME_SCANNING: |
| 801 | if (mScanner != null) { |
| 802 | mScanner.resume(); |
| 803 | } |
| 804 | break; |
| 805 | case MSG_PAUSE_SCANNING: |
| 806 | if (mScanner != null) { |
| 807 | mScanner.pause(); |
| 808 | } |
| 809 | break; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 810 | } |
| 811 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 812 | |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 813 | void removePendingMessages() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 814 | removeMessages(MSG_ACCESS_POINT_CHANGED); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 815 | removeMessages(MSG_CONNECTED_CHANGED); |
| 816 | removeMessages(MSG_WIFI_STATE_CHANGED); |
| 817 | removeMessages(MSG_PAUSE_SCANNING); |
| 818 | removeMessages(MSG_RESUME_SCANNING); |
| 819 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | private final class WorkHandler extends Handler { |
| 823 | private static final int MSG_UPDATE_ACCESS_POINTS = 0; |
| 824 | private static final int MSG_UPDATE_NETWORK_INFO = 1; |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 825 | private static final int MSG_RESUME = 2; |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 826 | private static final int MSG_UPDATE_WIFI_STATE = 3; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 827 | |
| 828 | public WorkHandler(Looper looper) { |
| 829 | super(looper); |
| 830 | } |
| 831 | |
| 832 | @Override |
| 833 | public void handleMessage(Message msg) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 834 | synchronized (mLock) { |
| 835 | processMessage(msg); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | @GuardedBy("mLock") |
| 840 | private void processMessage(Message msg) { |
| 841 | if (!mRegistered) return; |
| 842 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 843 | switch (msg.what) { |
| 844 | case MSG_UPDATE_ACCESS_POINTS: |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 845 | updateAccessPointsLocked(); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 846 | break; |
| 847 | case MSG_UPDATE_NETWORK_INFO: |
| 848 | updateNetworkInfo((NetworkInfo) msg.obj); |
| 849 | break; |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 850 | case MSG_RESUME: |
| 851 | handleResume(); |
| 852 | break; |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 853 | case MSG_UPDATE_WIFI_STATE: |
| 854 | if (msg.arg1 == WifiManager.WIFI_STATE_ENABLED) { |
| 855 | if (mScanner != null) { |
| 856 | // We only need to resume if mScanner isn't null because |
| 857 | // that means we want to be scanning. |
| 858 | mScanner.resume(); |
| 859 | } |
| 860 | } else { |
| 861 | mLastInfo = null; |
| 862 | mLastNetworkInfo = null; |
| 863 | if (mScanner != null) { |
| 864 | mScanner.pause(); |
| 865 | } |
| 866 | } |
| 867 | mMainHandler.obtainMessage(MainHandler.MSG_WIFI_STATE_CHANGED, msg.arg1, 0) |
| 868 | .sendToTarget(); |
| 869 | break; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 870 | } |
| 871 | } |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 872 | |
| 873 | private void removePendingMessages() { |
| 874 | removeMessages(MSG_UPDATE_ACCESS_POINTS); |
| 875 | removeMessages(MSG_UPDATE_NETWORK_INFO); |
| 876 | removeMessages(MSG_RESUME); |
| 877 | removeMessages(MSG_UPDATE_WIFI_STATE); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 878 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 881 | @VisibleForTesting |
| 882 | class Scanner extends Handler { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 883 | static final int MSG_SCAN = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 884 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 885 | private int mRetry = 0; |
| 886 | |
| 887 | void resume() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 888 | if (!hasMessages(MSG_SCAN)) { |
| 889 | sendEmptyMessage(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | |
| 893 | void forceScan() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 894 | removeMessages(MSG_SCAN); |
| 895 | sendEmptyMessage(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | void pause() { |
| 899 | mRetry = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 900 | removeMessages(MSG_SCAN); |
| 901 | } |
| 902 | |
| 903 | @VisibleForTesting |
| 904 | boolean isScanning() { |
| 905 | return hasMessages(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | @Override |
| 909 | public void handleMessage(Message message) { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 910 | if (message.what != MSG_SCAN) return; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 911 | if (mWifiManager.startScan()) { |
| 912 | mRetry = 0; |
| 913 | } else if (++mRetry >= 3) { |
| 914 | mRetry = 0; |
| 915 | if (mContext != null) { |
| 916 | Toast.makeText(mContext, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show(); |
| 917 | } |
| 918 | return; |
| 919 | } |
| 920 | sendEmptyMessageDelayed(0, WIFI_RESCAN_INTERVAL_MS); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /** A restricted multimap for use in constructAccessPoints */ |
| 925 | private static class Multimap<K,V> { |
| 926 | private final HashMap<K,List<V>> store = new HashMap<K,List<V>>(); |
| 927 | /** retrieve a non-null list of values with key K */ |
| 928 | List<V> getAll(K key) { |
| 929 | List<V> values = store.get(key); |
| 930 | return values != null ? values : Collections.<V>emptyList(); |
| 931 | } |
| 932 | |
| 933 | void put(K key, V val) { |
| 934 | List<V> curVals = store.get(key); |
| 935 | if (curVals == null) { |
| 936 | curVals = new ArrayList<V>(3); |
| 937 | store.put(key, curVals); |
| 938 | } |
| 939 | curVals.add(val); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | public interface WifiListener { |
| 944 | /** |
| 945 | * Called when the state of Wifi has changed, the state will be one of |
| 946 | * the following. |
| 947 | * |
| 948 | * <li>{@link WifiManager#WIFI_STATE_DISABLED}</li> |
| 949 | * <li>{@link WifiManager#WIFI_STATE_ENABLED}</li> |
| 950 | * <li>{@link WifiManager#WIFI_STATE_DISABLING}</li> |
| 951 | * <li>{@link WifiManager#WIFI_STATE_ENABLING}</li> |
| 952 | * <li>{@link WifiManager#WIFI_STATE_UNKNOWN}</li> |
| 953 | * <p> |
| 954 | * |
| 955 | * @param state The new state of wifi. |
| 956 | */ |
| 957 | void onWifiStateChanged(int state); |
| 958 | |
| 959 | /** |
| 960 | * Called when the connection state of wifi has changed and isConnected |
| 961 | * should be called to get the updated state. |
| 962 | */ |
| 963 | void onConnectedChanged(); |
| 964 | |
| 965 | /** |
| 966 | * Called to indicate the list of AccessPoints has been updated and |
| 967 | * getAccessPoints should be called to get the latest information. |
| 968 | */ |
| 969 | void onAccessPointsChanged(); |
| 970 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 971 | |
| 972 | /** |
| 973 | * Helps capture notifications that were generated during AccessPoint modification. Used later |
| 974 | * on by {@link #copyAndNotifyListeners(boolean)} to send notifications. |
| 975 | */ |
| 976 | private static class AccessPointListenerAdapter implements AccessPoint.AccessPointListener { |
| 977 | static final int AP_CHANGED = 1; |
| 978 | static final int LEVEL_CHANGED = 2; |
| 979 | |
| 980 | final SparseIntArray mPendingNotifications = new SparseIntArray(); |
| 981 | |
| 982 | @Override |
| 983 | public void onAccessPointChanged(AccessPoint accessPoint) { |
| 984 | int type = mPendingNotifications.get(accessPoint.mId); |
| 985 | mPendingNotifications.put(accessPoint.mId, type | AP_CHANGED); |
| 986 | } |
| 987 | |
| 988 | @Override |
| 989 | public void onLevelChanged(AccessPoint accessPoint) { |
| 990 | int type = mPendingNotifications.get(accessPoint.mId); |
| 991 | mPendingNotifications.put(accessPoint.mId, type | LEVEL_CHANGED); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /** |
| 996 | * Responsible for copying access points from {@link #mInternalAccessPoints} and notifying |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 997 | * accesspoint listeners. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 998 | * |
| 999 | * @param notifyListeners if true, accesspoint listeners are notified, otherwise notifications |
| 1000 | * dropped. |
| 1001 | */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 1002 | @MainThread |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1003 | private void copyAndNotifyListeners(boolean notifyListeners) { |
| 1004 | // Need to watch out for memory allocations on main thread. |
| 1005 | SparseArray<AccessPoint> oldAccessPoints = new SparseArray<>(); |
| 1006 | SparseIntArray notificationMap = null; |
| 1007 | List<AccessPoint> updatedAccessPoints = new ArrayList<>(); |
| 1008 | |
| 1009 | for (AccessPoint accessPoint : mAccessPoints) { |
| 1010 | oldAccessPoints.put(accessPoint.mId, accessPoint); |
| 1011 | } |
| 1012 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 1013 | if (DBG) { |
| 1014 | Log.d(TAG, "Starting to copy AP items on the MainHandler"); |
| 1015 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 1016 | synchronized (mLock) { |
Sundeep Ghuman | 221327d | 2017-05-02 15:58:39 -0700 | [diff] [blame] | 1017 | if (notifyListeners) { |
| 1018 | notificationMap = mAccessPointListenerAdapter.mPendingNotifications.clone(); |
| 1019 | } |
| 1020 | |
| 1021 | mAccessPointListenerAdapter.mPendingNotifications.clear(); |
| 1022 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1023 | for (AccessPoint internalAccessPoint : mInternalAccessPoints) { |
| 1024 | AccessPoint accessPoint = oldAccessPoints.get(internalAccessPoint.mId); |
| 1025 | if (accessPoint == null) { |
| 1026 | accessPoint = new AccessPoint(mContext, internalAccessPoint); |
| 1027 | } else { |
| 1028 | accessPoint.copyFrom(internalAccessPoint); |
| 1029 | } |
| 1030 | updatedAccessPoints.add(accessPoint); |
| 1031 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | mAccessPoints.clear(); |
| 1035 | mAccessPoints.addAll(updatedAccessPoints); |
| 1036 | |
| 1037 | if (notificationMap != null && notificationMap.size() > 0) { |
| 1038 | for (AccessPoint accessPoint : updatedAccessPoints) { |
| 1039 | int notificationType = notificationMap.get(accessPoint.mId); |
| 1040 | AccessPoint.AccessPointListener listener = accessPoint.mAccessPointListener; |
| 1041 | if (notificationType == 0 || listener == null) { |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | if ((notificationType & AccessPointListenerAdapter.AP_CHANGED) != 0) { |
| 1046 | listener.onAccessPointChanged(accessPoint); |
| 1047 | } |
| 1048 | |
| 1049 | if ((notificationType & AccessPointListenerAdapter.LEVEL_CHANGED) != 0) { |
| 1050 | listener.onLevelChanged(accessPoint); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1055 | } |