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