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