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