Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.settingslib.wifi; |
| 17 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 18 | import android.annotation.AnyThread; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 19 | import android.annotation.MainThread; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
| 25 | import android.net.Network; |
| 26 | import android.net.NetworkCapabilities; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 27 | import android.net.NetworkInfo; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 28 | import android.net.NetworkKey; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 29 | import android.net.NetworkRequest; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 30 | import android.net.NetworkScoreManager; |
| 31 | import android.net.ScoredNetwork; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 32 | import android.net.wifi.ScanResult; |
| 33 | import android.net.wifi.WifiConfiguration; |
| 34 | import android.net.wifi.WifiInfo; |
| 35 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 36 | import android.net.wifi.WifiNetworkScoreCache; |
| 37 | import android.net.wifi.WifiNetworkScoreCache.CacheListener; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 38 | import android.os.Handler; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 39 | import android.os.HandlerThread; |
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; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 42 | import android.os.Process; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 43 | import android.os.SystemClock; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 44 | import android.provider.Settings; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 45 | import android.support.annotation.GuardedBy; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 46 | import android.support.annotation.NonNull; |
| 47 | import android.support.annotation.VisibleForTesting; |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 48 | import android.text.format.DateUtils; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 49 | import android.util.ArrayMap; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 50 | import android.util.ArraySet; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 51 | import android.util.Log; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 52 | import android.widget.Toast; |
| 53 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 54 | import com.android.settingslib.R; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 55 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| 56 | import com.android.settingslib.core.lifecycle.LifecycleObserver; |
| 57 | import com.android.settingslib.core.lifecycle.events.OnDestroy; |
| 58 | import com.android.settingslib.core.lifecycle.events.OnStart; |
| 59 | import com.android.settingslib.core.lifecycle.events.OnStop; |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 60 | import com.android.settingslib.utils.ThreadUtils; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 61 | |
| 62 | import java.io.PrintWriter; |
| 63 | import java.util.ArrayList; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 64 | import java.util.Collection; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 65 | import java.util.Collections; |
| 66 | import java.util.HashMap; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 67 | import java.util.Iterator; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 68 | import java.util.List; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 69 | import java.util.Map; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 70 | import java.util.Set; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 71 | import java.util.concurrent.atomic.AtomicBoolean; |
| 72 | |
| 73 | /** |
| 74 | * Tracks saved or available wifi networks and their state. |
| 75 | */ |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 76 | public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestroy { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Default maximum age in millis of cached scored networks in |
| 79 | * {@link AccessPoint#mScoredNetworkCache} to be used for speed label generation. |
| 80 | */ |
Sundeep Ghuman | 64bf090 | 2017-09-11 13:00:53 -0700 | [diff] [blame] | 81 | private static final long DEFAULT_MAX_CACHED_SCORE_AGE_MILLIS = 20 * DateUtils.MINUTE_IN_MILLIS; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 82 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 83 | /** Maximum age of scan results to hold onto while actively scanning. **/ |
| 84 | private static final long MAX_SCAN_RESULT_AGE_MILLIS = 25000; |
| 85 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 86 | private static final String TAG = "WifiTracker"; |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 87 | private static final boolean DBG() { |
| 88 | return Log.isLoggable(TAG, Log.DEBUG); |
| 89 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 90 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 91 | private static boolean isVerboseLoggingEnabled() { |
| 92 | return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Verbose logging flag set thru developer debugging options and used so as to assist with |
| 97 | * in-the-field WiFi connectivity debugging. |
| 98 | * |
| 99 | * <p>{@link #isVerboseLoggingEnabled()} should be read rather than referencing this value |
| 100 | * directly, to ensure adb TAG level verbose settings are respected. |
| 101 | */ |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 102 | public static boolean sVerboseLogging; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 103 | |
| 104 | // TODO: Allow control of this? |
| 105 | // Combo scans can take 5-6s to complete - set to 10s. |
| 106 | private static final int WIFI_RESCAN_INTERVAL_MS = 10 * 1000; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 107 | private static final int NUM_SCANS_TO_CONFIRM_AP_LOSS = 3; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 108 | |
| 109 | private final Context mContext; |
| 110 | private final WifiManager mWifiManager; |
| 111 | private final IntentFilter mFilter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 112 | private final ConnectivityManager mConnectivityManager; |
| 113 | private final NetworkRequest mNetworkRequest; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 114 | private final AtomicBoolean mConnected = new AtomicBoolean(false); |
| 115 | private final WifiListener mListener; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 116 | @VisibleForTesting WorkHandler mWorkHandler; |
| 117 | private HandlerThread mWorkThread; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 118 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 119 | private WifiTrackerNetworkCallback mNetworkCallback; |
| 120 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 121 | @GuardedBy("mLock") |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 122 | private boolean mRegistered; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 123 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 124 | /** The list of AccessPoints, aggregated visible ScanResults with metadata. */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 125 | @GuardedBy("mLock") |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 126 | private final List<AccessPoint> mInternalAccessPoints = new ArrayList<>(); |
| 127 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Synchronization lock for managing concurrency between main and worker threads. |
| 130 | * |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 131 | * <p>This lock should be held for all modifications to {@link #mInternalAccessPoints}. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 132 | */ |
| 133 | private final Object mLock = new Object(); |
| 134 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 135 | private final HashMap<String, Integer> mSeenBssids = new HashMap<>(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 136 | |
| 137 | // TODO(sghuman): Change this to be keyed on AccessPoint.getKey |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 138 | private final HashMap<String, ScanResult> mScanResultCache = new HashMap<>(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 139 | |
| 140 | private NetworkInfo mLastNetworkInfo; |
| 141 | private WifiInfo mLastInfo; |
| 142 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 143 | private final NetworkScoreManager mNetworkScoreManager; |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 144 | private WifiNetworkScoreCache mScoreCache; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 145 | private boolean mNetworkScoringUiEnabled; |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 146 | private long mMaxSpeedLabelScoreCacheAge; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 147 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 148 | @GuardedBy("mLock") |
| 149 | private final Set<NetworkKey> mRequestedScores = new ArraySet<>(); |
| 150 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 151 | @VisibleForTesting |
| 152 | Scanner mScanner; |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 153 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Tracks whether fresh scan results have been received since scanning start. |
| 156 | * |
| 157 | * <p>If this variable is false, we will not evict the scan result cache or invoke callbacks |
| 158 | * so that we do not update the UI with stale data / clear out existing UI elements prematurely. |
| 159 | */ |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 160 | @GuardedBy("mLock") |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 161 | private boolean mStaleScanResults = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 162 | |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 163 | private static IntentFilter newIntentFilter() { |
| 164 | IntentFilter filter = new IntentFilter(); |
| 165 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 166 | filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 167 | filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); |
| 168 | filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); |
| 169 | filter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); |
| 170 | filter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION); |
| 171 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 172 | filter.addAction(WifiManager.RSSI_CHANGED_ACTION); |
| 173 | |
| 174 | return filter; |
| 175 | } |
| 176 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Use the lifecycle constructor below whenever possible |
| 179 | */ |
| 180 | @Deprecated |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 181 | public WifiTracker(Context context, WifiListener wifiListener, |
| 182 | boolean includeSaved, boolean includeScans) { |
Sundeep Ghuman | 8d157e3 | 2018-01-24 20:32:35 -0800 | [diff] [blame] | 183 | this(context, wifiListener, |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 184 | context.getSystemService(WifiManager.class), |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 185 | context.getSystemService(ConnectivityManager.class), |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 186 | context.getSystemService(NetworkScoreManager.class), |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 187 | newIntentFilter()); |
| 188 | } |
| 189 | |
Sundeep Ghuman | 8d157e3 | 2018-01-24 20:32:35 -0800 | [diff] [blame] | 190 | // TODO(Sghuman): Clean up includeSaved and includeScans from all constructors and linked |
| 191 | // calling apps once IC window is complete |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 192 | public WifiTracker(Context context, WifiListener wifiListener, |
| 193 | @NonNull Lifecycle lifecycle, boolean includeSaved, boolean includeScans) { |
Sundeep Ghuman | 8d157e3 | 2018-01-24 20:32:35 -0800 | [diff] [blame] | 194 | this(context, wifiListener, |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 195 | context.getSystemService(WifiManager.class), |
| 196 | context.getSystemService(ConnectivityManager.class), |
| 197 | context.getSystemService(NetworkScoreManager.class), |
| 198 | newIntentFilter()); |
| 199 | lifecycle.addObserver(this); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @VisibleForTesting |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 203 | WifiTracker(Context context, WifiListener wifiListener, |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 204 | WifiManager wifiManager, ConnectivityManager connectivityManager, |
| 205 | NetworkScoreManager networkScoreManager, |
| 206 | IntentFilter filter) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 207 | mContext = context; |
| 208 | mWifiManager = wifiManager; |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 209 | mListener = new WifiListenerWrapper(wifiListener); |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 210 | mConnectivityManager = connectivityManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 211 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 212 | // check if verbose logging developer option has been turned on or off |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 213 | sVerboseLogging = (mWifiManager.getVerboseLoggingLevel() > 0); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 214 | |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 215 | mFilter = filter; |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 216 | |
| 217 | mNetworkRequest = new NetworkRequest.Builder() |
| 218 | .clearCapabilities() |
| 219 | .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) |
| 220 | .build(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 221 | |
| 222 | mNetworkScoreManager = networkScoreManager; |
| 223 | |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 224 | // TODO(sghuman): Remove this and create less hacky solution for testing |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 225 | final HandlerThread workThread = new HandlerThread(TAG |
| 226 | + "{" + Integer.toHexString(System.identityHashCode(this)) + "}", |
| 227 | Process.THREAD_PRIORITY_BACKGROUND); |
| 228 | workThread.start(); |
| 229 | setWorkThread(workThread); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Sanity warning: this wipes out mScoreCache, so use with extreme caution |
| 234 | * @param workThread substitute Handler thread, for testing purposes only |
| 235 | */ |
| 236 | @VisibleForTesting |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 237 | // TODO(sghuman): Remove this method, this needs to happen in a factory method and be passed in |
| 238 | // during construction |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 239 | void setWorkThread(HandlerThread workThread) { |
| 240 | mWorkThread = workThread; |
| 241 | mWorkHandler = new WorkHandler(workThread.getLooper()); |
| 242 | mScoreCache = new WifiNetworkScoreCache(mContext, new CacheListener(mWorkHandler) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 243 | @Override |
| 244 | public void networkCacheUpdated(List<ScoredNetwork> networks) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 245 | synchronized (mLock) { |
| 246 | if (!mRegistered) return; |
| 247 | } |
| 248 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 249 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 250 | Log.v(TAG, "Score cache was updated with networks: " + networks); |
| 251 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 252 | updateNetworkScores(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 253 | } |
| 254 | }); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 255 | } |
| 256 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 257 | @Override |
| 258 | public void onDestroy() { |
| 259 | mWorkThread.quit(); |
| 260 | } |
| 261 | |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 262 | /** Synchronously update the list of access points with the latest information. */ |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 263 | @MainThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 264 | public void forceUpdate() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 265 | synchronized (mLock) { |
| 266 | mWorkHandler.removeMessages(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
| 267 | mLastInfo = mWifiManager.getConnectionInfo(); |
| 268 | mLastNetworkInfo = mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 269 | |
| 270 | final List<ScanResult> newScanResults = mWifiManager.getScanResults(); |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 271 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 272 | Log.i(TAG, "Fetched scan results: " + newScanResults); |
| 273 | } |
| 274 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 275 | List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 276 | mInternalAccessPoints.clear(); |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 277 | updateAccessPointsLocked(newScanResults, configs); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 278 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 282 | * Temporarily stop scanning for wifi networks. |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 283 | * |
| 284 | * <p>Sets {@link #mStaleScanResults} to true. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 285 | */ |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 286 | private void pauseScanning() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 287 | if (mScanner != null) { |
| 288 | mScanner.pause(); |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 289 | mScanner = null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 290 | } |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 291 | synchronized (mLock) { |
| 292 | mStaleScanResults = true; |
| 293 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Resume scanning for wifi networks after it has been paused. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 298 | * |
| 299 | * <p>The score cache should be registered before this method is invoked. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 300 | */ |
| 301 | public void resumeScanning() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 302 | if (mScanner == null) { |
| 303 | mScanner = new Scanner(); |
| 304 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 305 | |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 306 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_RESUME); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 307 | if (mWifiManager.isWifiEnabled()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 308 | mScanner.resume(); |
| 309 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 313 | * Start tracking wifi networks and scores. |
| 314 | * |
| 315 | * <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] | 316 | * then forceUpdate() must be called to populate getAccessPoints(). |
| 317 | */ |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 318 | @Override |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 319 | @MainThread |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 320 | public void onStart() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 321 | synchronized (mLock) { |
| 322 | registerScoreCache(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 323 | |
Sundeep Ghuman | 64bf090 | 2017-09-11 13:00:53 -0700 | [diff] [blame] | 324 | mNetworkScoringUiEnabled = |
| 325 | Settings.Global.getInt( |
| 326 | mContext.getContentResolver(), |
| 327 | Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1; |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 328 | |
Sundeep Ghuman | 64bf090 | 2017-09-11 13:00:53 -0700 | [diff] [blame] | 329 | mMaxSpeedLabelScoreCacheAge = |
| 330 | Settings.Global.getLong( |
| 331 | mContext.getContentResolver(), |
| 332 | Settings.Global.SPEED_LABEL_CACHE_EVICTION_AGE_MILLIS, |
| 333 | DEFAULT_MAX_CACHED_SCORE_AGE_MILLIS); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 334 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 335 | resumeScanning(); |
| 336 | if (!mRegistered) { |
| 337 | mContext.registerReceiver(mReceiver, mFilter); |
| 338 | // NetworkCallback objects cannot be reused. http://b/20701525 . |
| 339 | mNetworkCallback = new WifiTrackerNetworkCallback(); |
| 340 | mConnectivityManager.registerNetworkCallback(mNetworkRequest, mNetworkCallback); |
| 341 | mRegistered = true; |
| 342 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 346 | private void registerScoreCache() { |
| 347 | mNetworkScoreManager.registerNetworkScoreCache( |
| 348 | NetworkKey.TYPE_WIFI, |
| 349 | mScoreCache, |
| 350 | NetworkScoreManager.CACHE_FILTER_SCAN_RESULTS); |
| 351 | } |
| 352 | |
| 353 | private void requestScoresForNetworkKeys(Collection<NetworkKey> keys) { |
| 354 | if (keys.isEmpty()) return; |
| 355 | |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 356 | if (DBG()) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 357 | Log.d(TAG, "Requesting scores for Network Keys: " + keys); |
| 358 | } |
| 359 | mNetworkScoreManager.requestScores(keys.toArray(new NetworkKey[keys.size()])); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 360 | synchronized (mLock) { |
| 361 | mRequestedScores.addAll(keys); |
| 362 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 365 | /** |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 366 | * Stop tracking wifi networks and scores. |
| 367 | * |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 368 | * <p>This should always be called when done with a WifiTracker (if onStart was called) to |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 369 | * ensure proper cleanup and prevent any further callbacks from occurring. |
| 370 | * |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 371 | * <p>Calling this method will set the {@link #mStaleScanResults} bit, which prevents |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 372 | * {@link WifiListener#onAccessPointsChanged()} callbacks from being invoked (until the bit |
| 373 | * is unset on the next SCAN_RESULTS_AVAILABLE_ACTION). |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 374 | */ |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 375 | @Override |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 376 | @MainThread |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 377 | public void onStop() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 378 | synchronized (mLock) { |
| 379 | if (mRegistered) { |
| 380 | mContext.unregisterReceiver(mReceiver); |
| 381 | mConnectivityManager.unregisterNetworkCallback(mNetworkCallback); |
| 382 | mRegistered = false; |
| 383 | } |
Sundeep Ghuman | 4bb8411 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 384 | unregisterScoreCache(); |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 385 | pauseScanning(); // and set mStaleScanResults |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 386 | |
| 387 | mWorkHandler.removePendingMessages(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 388 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Sundeep Ghuman | 4bb8411 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 391 | private void unregisterScoreCache() { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 392 | mNetworkScoreManager.unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, mScoreCache); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 393 | |
Sundeep Ghuman | 4bb8411 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 394 | // We do not want to clear the existing scores in the cache, as this method is called during |
| 395 | // stop tracking on activity pause. Hence, on resumption we want the ability to show the |
| 396 | // last known, potentially stale, scores. However, by clearing requested scores, the scores |
| 397 | // will be requested again upon resumption of tracking, and if any changes have occurred |
| 398 | // the listeners (UI) will be updated accordingly. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 399 | synchronized (mLock) { |
| 400 | mRequestedScores.clear(); |
| 401 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 405 | * Gets the current list of access points. |
| 406 | * |
| 407 | * <p>This method is can be called on an abitrary thread by clients, but is normally called on |
| 408 | * the UI Thread by the rendering App. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 409 | */ |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 410 | @AnyThread |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 411 | public List<AccessPoint> getAccessPoints() { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 412 | // TODO(sghuman): Investigate how to eliminate or reduce the need for locking now that we |
| 413 | // have transitioned to a single worker thread model. |
| 414 | |
| 415 | synchronized (mLock) { |
| 416 | return new ArrayList<>(mInternalAccessPoints); |
| 417 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | public WifiManager getManager() { |
| 421 | return mWifiManager; |
| 422 | } |
| 423 | |
| 424 | public boolean isWifiEnabled() { |
| 425 | return mWifiManager.isWifiEnabled(); |
| 426 | } |
| 427 | |
| 428 | /** |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 429 | * Returns the number of saved networks on the device, regardless of whether the WifiTracker |
| 430 | * is tracking saved networks. |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 431 | * TODO(b/62292448): remove this function and update callsites to use WifiSavedConfigUtils |
| 432 | * directly. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 433 | */ |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 434 | public int getNumSavedNetworks() { |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 435 | return WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | public boolean isConnected() { |
| 439 | return mConnected.get(); |
| 440 | } |
| 441 | |
| 442 | public void dump(PrintWriter pw) { |
| 443 | pw.println(" - wifi tracker ------"); |
Jason Monk | 1904081 | 2015-06-01 15:00:57 -0400 | [diff] [blame] | 444 | for (AccessPoint accessPoint : getAccessPoints()) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 445 | pw.println(" " + accessPoint); |
| 446 | } |
| 447 | } |
| 448 | |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 449 | private void handleResume() { |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 450 | // TODO(sghuman): Investigate removing this and replacing it with a cache eviction call |
| 451 | // instead. |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 452 | mScanResultCache.clear(); |
| 453 | mSeenBssids.clear(); |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 454 | } |
| 455 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 456 | private Collection<ScanResult> updateScanResultCache(final List<ScanResult> newResults) { |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 457 | // TODO(sghuman): Delete this and replace it with the Map of Ap Keys to ScanResults |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 458 | for (ScanResult newResult : newResults) { |
Mitchell Wills | 9df7904 | 2015-09-01 12:21:59 -0700 | [diff] [blame] | 459 | if (newResult.SSID == null || newResult.SSID.isEmpty()) { |
| 460 | continue; |
| 461 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 462 | mScanResultCache.put(newResult.BSSID, newResult); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 465 | // Don't evict old results if no new scan results |
| 466 | if (!mStaleScanResults) { |
| 467 | evictOldScans(); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 470 | // TODO(sghuman): Update a Map<ApKey, List<ScanResults>> variable to be reused later after |
| 471 | // double threads have been removed. |
| 472 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 473 | return mScanResultCache.values(); |
| 474 | } |
| 475 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 476 | /** |
| 477 | * Remove old scan results from the cache. |
| 478 | * |
| 479 | * <p>Should only ever be invoked from {@link #updateScanResultCache(List)} when |
| 480 | * {@link #mStaleScanResults} is false. |
| 481 | */ |
| 482 | private void evictOldScans() { |
| 483 | long nowMs = SystemClock.elapsedRealtime(); |
| 484 | for (Iterator<ScanResult> iter = mScanResultCache.values().iterator(); iter.hasNext(); ) { |
| 485 | ScanResult result = iter.next(); |
| 486 | // result timestamp is in microseconds |
| 487 | if (nowMs - result.timestamp / 1000 > MAX_SCAN_RESULT_AGE_MILLIS) { |
| 488 | iter.remove(); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 493 | private WifiConfiguration getWifiConfigurationForNetworkId( |
| 494 | int networkId, final List<WifiConfiguration> configs) { |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 495 | if (configs != null) { |
| 496 | for (WifiConfiguration config : configs) { |
| 497 | if (mLastInfo != null && networkId == config.networkId && |
| 498 | !(config.selfAdded && config.numAssociation == 0)) { |
| 499 | return config; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | return null; |
| 504 | } |
| 505 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 506 | /** |
| 507 | * Safely modify {@link #mInternalAccessPoints} by acquiring {@link #mLock} first. |
| 508 | * |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 509 | * <p>Will not perform the update if {@link #mStaleScanResults} is true |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 510 | */ |
| 511 | private void updateAccessPoints() { |
| 512 | List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
| 513 | final List<ScanResult> newScanResults = mWifiManager.getScanResults(); |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 514 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 515 | Log.i(TAG, "Fetched scan results: " + newScanResults); |
| 516 | } |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 517 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 518 | synchronized (mLock) { |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 519 | if(!mStaleScanResults) { |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 520 | updateAccessPointsLocked(newScanResults, configs); |
| 521 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 522 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 523 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 524 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 525 | /** |
| 526 | * Update the internal list of access points. |
| 527 | * |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 528 | * <p>Do not call directly (except for forceUpdate), use {@link #updateAccessPoints()} which |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 529 | * acquires the lock first. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 530 | */ |
| 531 | @GuardedBy("mLock") |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 532 | private void updateAccessPointsLocked(final List<ScanResult> newScanResults, |
| 533 | List<WifiConfiguration> configs) { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 534 | // TODO(sghuman): Reduce the synchronization time by only holding the lock when |
| 535 | // modifying lists exposed to operations on the MainThread (getAccessPoints, stopTracking, |
| 536 | // startTracking, etc). |
| 537 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 538 | WifiConfiguration connectionConfig = null; |
| 539 | if (mLastInfo != null) { |
| 540 | connectionConfig = getWifiConfigurationForNetworkId( |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 541 | mLastInfo.getNetworkId(), configs); |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 544 | // Swap the current access points into a cached list. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 545 | List<AccessPoint> cachedAccessPoints = new ArrayList<>(mInternalAccessPoints); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 546 | ArrayList<AccessPoint> accessPoints = new ArrayList<>(); |
| 547 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 548 | // 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] | 549 | for (AccessPoint accessPoint : cachedAccessPoints) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 550 | accessPoint.clearConfig(); |
| 551 | } |
| 552 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 553 | final Collection<ScanResult> results = updateScanResultCache(newScanResults); |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 554 | |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 555 | final Map<String, WifiConfiguration> configsByKey = new ArrayMap(configs.size()); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 556 | if (configs != null) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 557 | for (WifiConfiguration config : configs) { |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 558 | configsByKey.put(AccessPoint.getKey(config), config); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 562 | final List<NetworkKey> scoresToRequest = new ArrayList<>(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 563 | if (results != null) { |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 564 | // TODO(sghuman): Move this loop to updateScanResultCache and make instance variable |
| 565 | // after double handlers are removed. |
| 566 | ArrayMap<String, List<ScanResult>> scanResultsByApKey = new ArrayMap<>(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 567 | for (ScanResult result : results) { |
| 568 | // Ignore hidden and ad-hoc networks. |
| 569 | if (result.SSID == null || result.SSID.length() == 0 || |
| 570 | result.capabilities.contains("[IBSS]")) { |
| 571 | continue; |
| 572 | } |
| 573 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 574 | NetworkKey key = NetworkKey.createFromScanResult(result); |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 575 | if (key != null && !mRequestedScores.contains(key)) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 576 | scoresToRequest.add(key); |
| 577 | } |
| 578 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 579 | String apKey = AccessPoint.getKey(result); |
| 580 | List<ScanResult> resultList; |
| 581 | if (scanResultsByApKey.containsKey(apKey)) { |
| 582 | resultList = scanResultsByApKey.get(apKey); |
| 583 | } else { |
| 584 | resultList = new ArrayList<>(); |
| 585 | scanResultsByApKey.put(apKey, resultList); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 586 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 587 | |
| 588 | resultList.add(result); |
| 589 | } |
| 590 | |
| 591 | for (Map.Entry<String, List<ScanResult>> entry : scanResultsByApKey.entrySet()) { |
| 592 | // List can not be empty as it is dynamically constructed on each iteration |
| 593 | ScanResult firstResult = entry.getValue().get(0); |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 594 | |
| 595 | AccessPoint accessPoint = |
| 596 | getCachedOrCreate(entry.getValue(), cachedAccessPoints); |
| 597 | if (mLastInfo != null && mLastNetworkInfo != null) { |
| 598 | accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 601 | // Update the matching config if there is one, to populate saved network info |
| 602 | WifiConfiguration config = configsByKey.get(entry.getKey()); |
| 603 | if (config != null) { |
| 604 | accessPoint.update(config); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 605 | } |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 606 | |
| 607 | accessPoints.add(accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 611 | requestScoresForNetworkKeys(scoresToRequest); |
| 612 | for (AccessPoint ap : accessPoints) { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 613 | ap.update(mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 616 | // Pre-sort accessPoints to speed preference insertion |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 617 | Collections.sort(accessPoints); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 618 | |
| 619 | // Log accesspoints that were deleted |
Sundeep Ghuman | 39ad5d8 | 2017-07-11 19:03:51 -0700 | [diff] [blame] | 620 | if (DBG()) { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 621 | Log.d(TAG, "------ Dumping SSIDs that were not seen on this scan ------"); |
| 622 | for (AccessPoint prevAccessPoint : mInternalAccessPoints) { |
| 623 | if (prevAccessPoint.getSsid() == null) |
| 624 | continue; |
| 625 | String prevSsid = prevAccessPoint.getSsidStr(); |
| 626 | boolean found = false; |
| 627 | for (AccessPoint newAccessPoint : accessPoints) { |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 628 | if (newAccessPoint.getSsidStr() != null && newAccessPoint.getSsidStr() |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 629 | .equals(prevSsid)) { |
| 630 | found = true; |
| 631 | break; |
| 632 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 633 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 634 | if (!found) |
| 635 | Log.d(TAG, "Did not find " + prevSsid + " in this scan"); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 636 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 637 | 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] | 638 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 639 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 640 | mInternalAccessPoints.clear(); |
| 641 | mInternalAccessPoints.addAll(accessPoints); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 642 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 643 | conditionallyNotifyListeners(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 644 | } |
| 645 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 646 | @VisibleForTesting |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 647 | AccessPoint getCachedOrCreate( |
| 648 | List<ScanResult> scanResults, |
| 649 | List<AccessPoint> cache) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 650 | final int N = cache.size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 651 | for (int i = 0; i < N; i++) { |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 652 | if (cache.get(i).getKey().equals(AccessPoint.getKey(scanResults.get(0)))) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 653 | AccessPoint ret = cache.remove(i); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 654 | ret.setScanResults(scanResults); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 658 | final AccessPoint accessPoint = new AccessPoint(mContext, scanResults); |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 659 | return accessPoint; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 662 | @VisibleForTesting |
| 663 | AccessPoint getCachedOrCreate(WifiConfiguration config, List<AccessPoint> cache) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 664 | final int N = cache.size(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 665 | for (int i = 0; i < N; i++) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 666 | if (cache.get(i).matches(config)) { |
| 667 | AccessPoint ret = cache.remove(i); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 668 | ret.loadConfig(config); |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 669 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 670 | return ret; |
| 671 | } |
| 672 | } |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 673 | final AccessPoint accessPoint = new AccessPoint(mContext, config); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 674 | return accessPoint; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | private void updateNetworkInfo(NetworkInfo networkInfo) { |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 678 | |
| 679 | /* Sticky broadcasts can call this when wifi is disabled */ |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 680 | if (!mWifiManager.isWifiEnabled()) { |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 681 | clearAccessPointsAndConditionallyUpdate(); |
Jason Monk | e04ae8a | 2015-06-03 14:06:34 -0400 | [diff] [blame] | 682 | return; |
| 683 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 684 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 685 | if (networkInfo != null) { |
| 686 | mLastNetworkInfo = networkInfo; |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 687 | if (DBG()) { |
| 688 | Log.d(TAG, "mLastNetworkInfo set: " + mLastNetworkInfo); |
| 689 | } |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 690 | |
| 691 | if(networkInfo.isConnected() != mConnected.getAndSet(networkInfo.isConnected())) { |
| 692 | mListener.onConnectedChanged(); |
| 693 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 694 | } |
| 695 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 696 | WifiConfiguration connectionConfig = null; |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 697 | |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 698 | mLastInfo = mWifiManager.getConnectionInfo(); |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 699 | if (DBG()) { |
| 700 | Log.d(TAG, "mLastInfo set as: " + mLastInfo); |
| 701 | } |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 702 | if (mLastInfo != null) { |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 703 | connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId(), |
| 704 | mWifiManager.getConfiguredNetworks()); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 707 | boolean updated = false; |
| 708 | boolean reorder = false; // Only reorder if connected AP was changed |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 709 | |
| 710 | synchronized (mLock) { |
| 711 | for (int i = mInternalAccessPoints.size() - 1; i >= 0; --i) { |
| 712 | AccessPoint ap = mInternalAccessPoints.get(i); |
| 713 | boolean previouslyConnected = ap.isActive(); |
| 714 | if (ap.update(connectionConfig, mLastInfo, mLastNetworkInfo)) { |
| 715 | updated = true; |
| 716 | if (previouslyConnected != ap.isActive()) reorder = true; |
| 717 | } |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 718 | if (ap.update(mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge)) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 719 | reorder = true; |
| 720 | updated = true; |
| 721 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 722 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 723 | |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 724 | if (reorder) { |
| 725 | Collections.sort(mInternalAccessPoints); |
| 726 | } |
| 727 | if (updated) { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 728 | conditionallyNotifyListeners(); |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 729 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 730 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 733 | /** |
| 734 | * Clears the access point list and conditionally invokes |
| 735 | * {@link WifiListener#onAccessPointsChanged()} if required (i.e. the list was not already |
| 736 | * empty). |
| 737 | */ |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 738 | private void clearAccessPointsAndConditionallyUpdate() { |
| 739 | synchronized (mLock) { |
| 740 | if (!mInternalAccessPoints.isEmpty()) { |
| 741 | mInternalAccessPoints.clear(); |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 742 | mListener.onAccessPointsChanged(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 747 | /** |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 748 | * Update all the internal access points rankingScores, badge and metering. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 749 | * |
| 750 | * <p>Will trigger a resort and notify listeners of changes if applicable. |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 751 | * |
| 752 | * <p>Synchronized on {@link #mLock}. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 753 | */ |
| 754 | private void updateNetworkScores() { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 755 | synchronized (mLock) { |
| 756 | boolean updated = false; |
| 757 | for (int i = 0; i < mInternalAccessPoints.size(); i++) { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 758 | if (mInternalAccessPoints.get(i).update( |
| 759 | mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge)) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 760 | updated = true; |
| 761 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 762 | } |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 763 | if (updated) { |
| 764 | Collections.sort(mInternalAccessPoints); |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 765 | conditionallyNotifyListeners(); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 766 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 770 | @VisibleForTesting |
| 771 | final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 772 | @Override |
| 773 | public void onReceive(Context context, Intent intent) { |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 774 | // No work should be performed in this Receiver, instead all operations should be passed |
| 775 | // off to the WorkHandler to avoid concurrent modification exceptions. |
| 776 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 777 | String action = intent.getAction(); |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 778 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 779 | if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 780 | mWorkHandler.obtainMessage( |
| 781 | WorkHandler.MSG_UPDATE_WIFI_STATE, |
| 782 | intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, |
| 783 | WifiManager.WIFI_STATE_UNKNOWN), |
| 784 | 0).sendToTarget(); |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 785 | } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) { |
| 786 | mWorkHandler |
| 787 | .obtainMessage( |
| 788 | WorkHandler.MSG_UPDATE_ACCESS_POINTS, |
| 789 | WorkHandler.CLEAR_STALE_SCAN_RESULTS, |
| 790 | 0) |
| 791 | .sendToTarget(); |
| 792 | } else if (WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) |
| 793 | || WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) { |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 794 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 795 | } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 796 | NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 797 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info) |
| 798 | .sendToTarget(); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 799 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 800 | } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 801 | NetworkInfo info = |
| 802 | mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork()); |
| 803 | mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info) |
| 804 | .sendToTarget(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | }; |
| 808 | |
Lorenzo Colitti | ab313f8 | 2015-11-26 16:55:15 +0900 | [diff] [blame] | 809 | private final class WifiTrackerNetworkCallback extends ConnectivityManager.NetworkCallback { |
| 810 | public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { |
| 811 | if (network.equals(mWifiManager.getCurrentNetwork())) { |
| 812 | // We don't send a NetworkInfo object along with this message, because even if we |
| 813 | // fetch one from ConnectivityManager, it might be older than the most recent |
| 814 | // NetworkInfo message we got via a WIFI_STATE_CHANGED broadcast. |
| 815 | mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 820 | @VisibleForTesting |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 821 | final class WorkHandler extends Handler { |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 822 | @VisibleForTesting static final int MSG_UPDATE_ACCESS_POINTS = 0; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 823 | private static final int MSG_UPDATE_NETWORK_INFO = 1; |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 824 | private static final int MSG_RESUME = 2; |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 825 | private static final int MSG_UPDATE_WIFI_STATE = 3; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 826 | |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 827 | private static final int CLEAR_STALE_SCAN_RESULTS = 1; |
| 828 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 829 | public WorkHandler(Looper looper) { |
| 830 | super(looper); |
| 831 | } |
| 832 | |
| 833 | @Override |
| 834 | public void handleMessage(Message msg) { |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 835 | // TODO(sghuman): Clean up synchronization to only be used when modifying collections |
| 836 | // exposed to the MainThread (through onStart, onStop, forceUpdate). |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 837 | synchronized (mLock) { |
| 838 | processMessage(msg); |
| 839 | } |
| 840 | } |
| 841 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 842 | private void processMessage(Message msg) { |
| 843 | if (!mRegistered) return; |
| 844 | |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 845 | switch (msg.what) { |
| 846 | case MSG_UPDATE_ACCESS_POINTS: |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 847 | if (msg.arg1 == CLEAR_STALE_SCAN_RESULTS) { |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 848 | mStaleScanResults = false; |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 849 | } |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 850 | updateAccessPoints(); |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 851 | break; |
| 852 | case MSG_UPDATE_NETWORK_INFO: |
| 853 | updateNetworkInfo((NetworkInfo) msg.obj); |
| 854 | break; |
Jason Monk | dbd05a9 | 2015-07-06 12:55:48 -0400 | [diff] [blame] | 855 | case MSG_RESUME: |
| 856 | handleResume(); |
| 857 | break; |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 858 | case MSG_UPDATE_WIFI_STATE: |
| 859 | if (msg.arg1 == WifiManager.WIFI_STATE_ENABLED) { |
| 860 | if (mScanner != null) { |
| 861 | // We only need to resume if mScanner isn't null because |
| 862 | // that means we want to be scanning. |
| 863 | mScanner.resume(); |
| 864 | } |
| 865 | } else { |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 866 | clearAccessPointsAndConditionallyUpdate(); |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 867 | mLastInfo = null; |
| 868 | mLastNetworkInfo = null; |
| 869 | if (mScanner != null) { |
| 870 | mScanner.pause(); |
| 871 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 872 | synchronized (mLock) { |
| 873 | mStaleScanResults = true; |
| 874 | } |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 875 | } |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 876 | mListener.onWifiStateChanged(msg.arg1); |
Mitchell Wills | cf0875a | 2016-04-18 14:54:53 -0700 | [diff] [blame] | 877 | break; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 878 | } |
| 879 | } |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 880 | |
| 881 | private void removePendingMessages() { |
| 882 | removeMessages(MSG_UPDATE_ACCESS_POINTS); |
| 883 | removeMessages(MSG_UPDATE_NETWORK_INFO); |
| 884 | removeMessages(MSG_RESUME); |
| 885 | removeMessages(MSG_UPDATE_WIFI_STATE); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 886 | } |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 889 | @VisibleForTesting |
| 890 | class Scanner extends Handler { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 891 | static final int MSG_SCAN = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 892 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 893 | private int mRetry = 0; |
| 894 | |
| 895 | void resume() { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 896 | if (!hasMessages(MSG_SCAN)) { |
| 897 | sendEmptyMessage(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 901 | void pause() { |
| 902 | mRetry = 0; |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 903 | removeMessages(MSG_SCAN); |
| 904 | } |
| 905 | |
| 906 | @VisibleForTesting |
| 907 | boolean isScanning() { |
| 908 | return hasMessages(MSG_SCAN); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | @Override |
| 912 | public void handleMessage(Message message) { |
Jason Monk | 6572eae | 2015-02-05 10:34:20 -0500 | [diff] [blame] | 913 | if (message.what != MSG_SCAN) return; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 914 | if (mWifiManager.startScan()) { |
| 915 | mRetry = 0; |
| 916 | } else if (++mRetry >= 3) { |
| 917 | mRetry = 0; |
| 918 | if (mContext != null) { |
| 919 | Toast.makeText(mContext, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show(); |
| 920 | } |
| 921 | return; |
| 922 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 923 | sendEmptyMessageDelayed(MSG_SCAN, WIFI_RESCAN_INTERVAL_MS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | /** A restricted multimap for use in constructAccessPoints */ |
| 928 | private static class Multimap<K,V> { |
| 929 | private final HashMap<K,List<V>> store = new HashMap<K,List<V>>(); |
| 930 | /** retrieve a non-null list of values with key K */ |
| 931 | List<V> getAll(K key) { |
| 932 | List<V> values = store.get(key); |
| 933 | return values != null ? values : Collections.<V>emptyList(); |
| 934 | } |
| 935 | |
| 936 | void put(K key, V val) { |
| 937 | List<V> curVals = store.get(key); |
| 938 | if (curVals == null) { |
| 939 | curVals = new ArrayList<V>(3); |
| 940 | store.put(key, curVals); |
| 941 | } |
| 942 | curVals.add(val); |
| 943 | } |
| 944 | } |
| 945 | |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 946 | /** |
| 947 | * Wraps the given {@link WifiListener} instance and executes it's methods on the Main Thread. |
| 948 | * |
| 949 | * <p>This mechanism allows us to no longer need a separate MainHandler and WorkHandler, which |
| 950 | * were previously both performing work, while avoiding errors which occur from executing |
| 951 | * callbacks which manipulate UI elements from a different thread than the MainThread. |
| 952 | */ |
| 953 | private static class WifiListenerWrapper implements WifiListener { |
| 954 | |
| 955 | private final Handler mHandler; |
| 956 | private final WifiListener mDelegatee; |
| 957 | |
| 958 | public WifiListenerWrapper(WifiListener listener) { |
| 959 | mHandler = new Handler(Looper.getMainLooper()); |
| 960 | mDelegatee = listener; |
| 961 | } |
| 962 | |
| 963 | @Override |
| 964 | public void onWifiStateChanged(int state) { |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 965 | if (isVerboseLoggingEnabled()) { |
| 966 | Log.i(TAG, |
| 967 | String.format("Invoking onWifiStateChanged callback with state %d", state)); |
| 968 | } |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 969 | mHandler.post(() -> mDelegatee.onWifiStateChanged(state)); |
| 970 | } |
| 971 | |
| 972 | @Override |
| 973 | public void onConnectedChanged() { |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 974 | if (isVerboseLoggingEnabled()) { |
| 975 | Log.i(TAG, "Invoking onConnectedChanged callback"); |
| 976 | } |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 977 | mHandler.post(() -> mDelegatee.onConnectedChanged()); |
| 978 | } |
| 979 | |
| 980 | @Override |
| 981 | public void onAccessPointsChanged() { |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 982 | if (isVerboseLoggingEnabled()) { |
| 983 | Log.i(TAG, "Invoking onAccessPointsChanged callback"); |
| 984 | } |
Sundeep Ghuman | 86cc9b7 | 2018-01-24 20:08:39 -0800 | [diff] [blame] | 985 | mHandler.post(() -> mDelegatee.onAccessPointsChanged()); |
| 986 | } |
| 987 | } |
| 988 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 989 | public interface WifiListener { |
| 990 | /** |
| 991 | * Called when the state of Wifi has changed, the state will be one of |
| 992 | * the following. |
| 993 | * |
| 994 | * <li>{@link WifiManager#WIFI_STATE_DISABLED}</li> |
| 995 | * <li>{@link WifiManager#WIFI_STATE_ENABLED}</li> |
| 996 | * <li>{@link WifiManager#WIFI_STATE_DISABLING}</li> |
| 997 | * <li>{@link WifiManager#WIFI_STATE_ENABLING}</li> |
| 998 | * <li>{@link WifiManager#WIFI_STATE_UNKNOWN}</li> |
| 999 | * <p> |
| 1000 | * |
| 1001 | * @param state The new state of wifi. |
| 1002 | */ |
| 1003 | void onWifiStateChanged(int state); |
| 1004 | |
| 1005 | /** |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1006 | * Called when the connection state of wifi has changed and |
| 1007 | * {@link WifiTracker#isConnected()} should be called to get the updated state. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1008 | */ |
| 1009 | void onConnectedChanged(); |
| 1010 | |
| 1011 | /** |
| 1012 | * Called to indicate the list of AccessPoints has been updated and |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1013 | * {@link WifiTracker#getAccessPoints()} should be called to get the updated list. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1014 | */ |
| 1015 | void onAccessPointsChanged(); |
| 1016 | } |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1017 | |
| 1018 | /** |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1019 | * Invokes {@link WifiListenerWrapper#onAccessPointsChanged()} if {@link #mStaleScanResults} |
| 1020 | * is false. |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1021 | */ |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1022 | private void conditionallyNotifyListeners() { |
| 1023 | if (mStaleScanResults) { |
| 1024 | return; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1027 | ThreadUtils.postOnMainThread(() -> mListener.onAccessPointsChanged()); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 1028 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1029 | } |