blob: 3acbcd3f6b41a15d215c7387aecf0b3b45bda1af [file] [log] [blame]
Jason Monkd52356a2015-01-28 10:40:41 -05001/*
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
17package com.android.settingslib.wifi;
18
Sundeep Ghuman53200ed2017-06-21 16:54:36 -070019import android.annotation.IntDef;
Sundeep Ghumanbb399912018-01-29 18:31:15 -080020import android.annotation.MainThread;
Sundeep Ghuman271e5de2017-05-30 14:11:39 -070021import android.annotation.Nullable;
Jason Monk6980d122015-06-15 10:07:55 -040022import android.app.AppGlobals;
Jason Monkd52356a2015-01-28 10:40:41 -050023import android.content.Context;
Jason Monk6980d122015-06-15 10:07:55 -040024import android.content.pm.ApplicationInfo;
25import android.content.pm.IPackageManager;
26import android.content.pm.PackageManager;
Sanket Padawe7094d222015-05-01 16:55:00 -070027import android.net.ConnectivityManager;
Sanket Padawe7094d222015-05-01 16:55:00 -070028import android.net.NetworkCapabilities;
Jason Monkd52356a2015-01-28 10:40:41 -050029import android.net.NetworkInfo;
30import android.net.NetworkInfo.DetailedState;
31import android.net.NetworkInfo.State;
Sundeep Ghumana28050a2017-07-12 22:09:25 -070032import android.net.NetworkKey;
Stephen Chen36dd5cf12017-03-20 13:27:51 -070033import android.net.NetworkScoreManager;
34import android.net.NetworkScorerAppData;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080035import android.net.ScoredNetwork;
Sanket Padawe7094d222015-05-01 16:55:00 -070036import android.net.wifi.IWifiManager;
Jason Monkd52356a2015-01-28 10:40:41 -050037import android.net.wifi.ScanResult;
38import android.net.wifi.WifiConfiguration;
39import android.net.wifi.WifiConfiguration.KeyMgmt;
Peter Qiu280581b2017-07-24 14:18:56 -070040import android.net.wifi.WifiEnterpriseConfig;
Jason Monkd52356a2015-01-28 10:40:41 -050041import android.net.wifi.WifiInfo;
42import android.net.wifi.WifiManager;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -080043import android.net.wifi.WifiNetworkScoreCache;
Quang Luong9051dfd2018-12-12 12:49:24 -080044import android.net.wifi.hotspot2.OsuProvider;
Peter Qiuced37db2017-03-14 15:51:22 -070045import android.net.wifi.hotspot2.PasspointConfiguration;
Quang Luong4d194842018-12-18 16:59:49 -080046import android.net.wifi.hotspot2.ProvisioningCallback;
Jason Monkd52356a2015-01-28 10:40:41 -050047import android.os.Bundle;
Sundeep Ghuman04f7f342018-01-23 19:18:31 -080048import android.os.Parcelable;
Sanket Padawe7094d222015-05-01 16:55:00 -070049import android.os.RemoteException;
50import android.os.ServiceManager;
Mitchell Wills18af4932016-08-10 13:49:21 -070051import android.os.SystemClock;
Jason Monk6980d122015-06-15 10:07:55 -040052import android.os.UserHandle;
Jason Monk6980d122015-06-15 10:07:55 -040053import android.text.TextUtils;
Sundeep Ghuman04f7f342018-01-23 19:18:31 -080054import android.util.ArraySet;
Jason Monkd52356a2015-01-28 10:40:41 -050055import android.util.Log;
Jason Monkd52356a2015-01-28 10:40:41 -050056
Fan Zhangf7802ea2018-08-28 15:15:19 -070057import androidx.annotation.NonNull;
58
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -080059import com.android.internal.annotations.VisibleForTesting;
Quang Luong8123f772019-02-08 16:30:12 -080060import com.android.internal.util.CollectionUtils;
Jason Monkd52356a2015-01-28 10:40:41 -050061import com.android.settingslib.R;
Sundeep Ghumanbb399912018-01-29 18:31:15 -080062import com.android.settingslib.utils.ThreadUtils;
Jason Monkd52356a2015-01-28 10:40:41 -050063
Sundeep Ghuman53200ed2017-06-21 16:54:36 -070064import java.lang.annotation.Retention;
65import java.lang.annotation.RetentionPolicy;
Vinit Deshpandefcd46122015-06-11 18:22:23 -070066import java.util.ArrayList;
Sundeep Ghuman04f7f342018-01-23 19:18:31 -080067import java.util.Collection;
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -070068import java.util.HashMap;
Mitchell Wills18af4932016-08-10 13:49:21 -070069import java.util.Iterator;
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -070070import java.util.Map;
Sundeep Ghuman04f7f342018-01-23 19:18:31 -080071import java.util.Set;
Ajay Nadathurd7b689a2016-08-31 15:07:56 -070072import java.util.concurrent.atomic.AtomicInteger;
Jason Monkd52356a2015-01-28 10:40:41 -050073
Sundeep Ghumanbb399912018-01-29 18:31:15 -080074/**
75 * Represents a selectable Wifi Network for use in various wifi selection menus backed by
76 * {@link WifiTracker}.
77 *
78 * <p>An AccessPoint, which would be more fittingly named "WifiNetwork", is an aggregation of
79 * {@link ScanResult ScanResults} along with pertinent metadata (e.g. current connection info,
80 * network scores) required to successfully render the network to the user.
81 */
Jason Monkd52356a2015-01-28 10:40:41 -050082public class AccessPoint implements Comparable<AccessPoint> {
83 static final String TAG = "SettingsLib.AccessPoint";
84
85 /**
86 * Lower bound on the 2.4 GHz (802.11b/g/n) WLAN channels
87 */
88 public static final int LOWER_FREQ_24GHZ = 2400;
89
90 /**
91 * Upper bound on the 2.4 GHz (802.11b/g/n) WLAN channels
92 */
93 public static final int HIGHER_FREQ_24GHZ = 2500;
94
95 /**
96 * Lower bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels
97 */
98 public static final int LOWER_FREQ_5GHZ = 4900;
99
100 /**
101 * Upper bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels
102 */
103 public static final int HIGHER_FREQ_5GHZ = 5900;
104
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800105 /** The key which identifies this AccessPoint grouping. */
106 private String mKey;
107
Sundeep Ghuman53200ed2017-06-21 16:54:36 -0700108 @IntDef({Speed.NONE, Speed.SLOW, Speed.MODERATE, Speed.FAST, Speed.VERY_FAST})
109 @Retention(RetentionPolicy.SOURCE)
110 public @interface Speed {
111 /**
112 * Constant value representing an unlabeled / unscored network.
113 */
114 int NONE = 0;
115 /**
116 * Constant value representing a slow speed network connection.
117 */
118 int SLOW = 5;
119 /**
120 * Constant value representing a medium speed network connection.
121 */
122 int MODERATE = 10;
123 /**
124 * Constant value representing a fast speed network connection.
125 */
126 int FAST = 20;
127 /**
128 * Constant value representing a very fast speed network connection.
129 */
130 int VERY_FAST = 30;
131 }
Jason Monkd52356a2015-01-28 10:40:41 -0500132
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800133 /** The underlying set of scan results comprising this AccessPoint. */
134 private final ArraySet<ScanResult> mScanResults = new ArraySet<>();
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -0700135
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700136 /**
Quang Luong8123f772019-02-08 16:30:12 -0800137 * Extra set of unused scan results corresponding to this AccessPoint for verbose logging
138 * purposes, such as a set of Passpoint roaming scan results when home scans are available.
139 */
140 private final ArraySet<ScanResult> mExtraScanResults = new ArraySet<>();
141
142 /**
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700143 * Map of BSSIDs to scored networks for individual bssids.
144 *
145 * <p>This cache should not be evicted with scan results, as the values here are used to
146 * generate a fallback in the absence of scores for the visible APs.
147 */
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700148 private final Map<String, TimestampedScoredNetwork> mScoredNetworkCache = new HashMap<>();
149
Dave Schaefer98537432017-02-08 11:26:08 -0800150 static final String KEY_NETWORKINFO = "key_networkinfo";
151 static final String KEY_WIFIINFO = "key_wifiinfo";
Dave Schaefer98537432017-02-08 11:26:08 -0800152 static final String KEY_SSID = "key_ssid";
153 static final String KEY_SECURITY = "key_security";
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700154 static final String KEY_SPEED = "key_speed";
Dave Schaefer98537432017-02-08 11:26:08 -0800155 static final String KEY_PSKTYPE = "key_psktype";
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800156 static final String KEY_SCANRESULTS = "key_scanresults";
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700157 static final String KEY_SCOREDNETWORKCACHE = "key_scorednetworkcache";
Dave Schaefer98537432017-02-08 11:26:08 -0800158 static final String KEY_CONFIG = "key_config";
Peter Qiuced37db2017-03-14 15:51:22 -0700159 static final String KEY_FQDN = "key_fqdn";
160 static final String KEY_PROVIDER_FRIENDLY_NAME = "key_provider_friendly_name";
Peter Qiu280581b2017-07-24 14:18:56 -0700161 static final String KEY_IS_CARRIER_AP = "key_is_carrier_ap";
162 static final String KEY_CARRIER_AP_EAP_TYPE = "key_carrier_ap_eap_type";
163 static final String KEY_CARRIER_NAME = "key_carrier_name";
Dave Schaefer98537432017-02-08 11:26:08 -0800164 static final AtomicInteger sLastId = new AtomicInteger(0);
Jason Monkd52356a2015-01-28 10:40:41 -0500165
Maurice Lam7cdbe192018-11-13 13:52:50 -0800166 /*
167 * NOTE: These constants for security and PSK types are saved to the bundle in saveWifiState,
168 * and sent across IPC. The numeric values should remain stable, otherwise the changes will need
169 * to be synced with other unbundled users of this library.
Jason Monkd52356a2015-01-28 10:40:41 -0500170 */
171 public static final int SECURITY_NONE = 0;
Hai Shalom9effa642018-11-12 11:43:52 -0800172 public static final int SECURITY_WEP = 1;
173 public static final int SECURITY_PSK = 2;
174 public static final int SECURITY_EAP = 3;
175 public static final int SECURITY_OWE = 4;
176 public static final int SECURITY_SAE = 5;
Hai Shalomaa6f9202018-10-16 14:30:02 -0700177 public static final int SECURITY_EAP_SUITE_B = 6;
Hai Shalom828b13c2018-11-13 16:43:14 -0800178 public static final int SECURITY_MAX_VAL = 7; // Has to be the last
Jason Monkd52356a2015-01-28 10:40:41 -0500179
180 private static final int PSK_UNKNOWN = 0;
181 private static final int PSK_WPA = 1;
182 private static final int PSK_WPA2 = 2;
183 private static final int PSK_WPA_WPA2 = 3;
184
Sundeep Ghumanaaa8a1b2017-03-13 14:40:56 -0700185 /**
186 * The number of distinct wifi levels.
187 *
188 * <p>Must keep in sync with {@link R.array.wifi_signal} and {@link WifiManager#RSSI_LEVELS}.
189 */
190 public static final int SIGNAL_LEVELS = 5;
Tony Mantlera0e03dd2016-01-27 15:57:03 -0800191
Sundeep Ghumanaaa8a1b2017-03-13 14:40:56 -0700192 public static final int UNREACHABLE_RSSI = Integer.MIN_VALUE;
Dave Schaefer98537432017-02-08 11:26:08 -0800193
Quang Luong9051dfd2018-12-12 12:49:24 -0800194 public static final String KEY_PREFIX_AP = "AP:";
195 public static final String KEY_PREFIX_FQDN = "FQDN:";
196 public static final String KEY_PREFIX_OSU = "OSU:";
197
Jason Monkd52356a2015-01-28 10:40:41 -0500198 private final Context mContext;
199
200 private String ssid;
Jason Monk60a82ff2016-02-25 13:55:03 -0500201 private String bssid;
Jason Monkd52356a2015-01-28 10:40:41 -0500202 private int security;
203 private int networkId = WifiConfiguration.INVALID_NETWORK_ID;
204
205 private int pskType = PSK_UNKNOWN;
206
207 private WifiConfiguration mConfig;
Jason Monkd52356a2015-01-28 10:40:41 -0500208
Dave Schaefer98537432017-02-08 11:26:08 -0800209 private int mRssi = UNREACHABLE_RSSI;
Jason Monkd52356a2015-01-28 10:40:41 -0500210
211 private WifiInfo mInfo;
212 private NetworkInfo mNetworkInfo;
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700213 AccessPointListener mAccessPointListener;
Jason Monkd52356a2015-01-28 10:40:41 -0500214
215 private Object mTag;
216
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700217 @Speed private int mSpeed = Speed.NONE;
Stephen Chen21f68682017-04-04 13:23:31 -0700218 private boolean mIsScoredNetworkMetered = false;
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800219
Peter Qiuced37db2017-03-14 15:51:22 -0700220 /**
221 * Information associated with the {@link PasspointConfiguration}. Only maintaining
222 * the relevant info to preserve spaces.
223 */
224 private String mFqdn;
225 private String mProviderFriendlyName;
Quang Luong8123f772019-02-08 16:30:12 -0800226 private boolean mIsRoaming = false;
Peter Qiuced37db2017-03-14 15:51:22 -0700227
Peter Qiu280581b2017-07-24 14:18:56 -0700228 private boolean mIsCarrierAp = false;
Quang Luong9051dfd2018-12-12 12:49:24 -0800229
230 private OsuProvider mOsuProvider;
Quang Luong8d40a8e2019-01-02 16:25:38 -0800231
232 private String mOsuStatus;
233 private String mOsuFailure;
234 private boolean mOsuProvisioningComplete = false;
235
Peter Qiu280581b2017-07-24 14:18:56 -0700236 /**
237 * The EAP type {@link WifiEnterpriseConfig.Eap} associated with this AP if it is a carrier AP.
238 */
239 private int mCarrierApEapType = WifiEnterpriseConfig.Eap.NONE;
240 private String mCarrierName = null;
241
Jason Monkd52356a2015-01-28 10:40:41 -0500242 public AccessPoint(Context context, Bundle savedState) {
243 mContext = context;
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800244
245 if (savedState.containsKey(KEY_CONFIG)) {
246 mConfig = savedState.getParcelable(KEY_CONFIG);
247 }
Jason Monkd52356a2015-01-28 10:40:41 -0500248 if (mConfig != null) {
249 loadConfig(mConfig);
250 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700251 if (savedState.containsKey(KEY_SSID)) {
252 ssid = savedState.getString(KEY_SSID);
253 }
254 if (savedState.containsKey(KEY_SECURITY)) {
255 security = savedState.getInt(KEY_SECURITY);
256 }
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700257 if (savedState.containsKey(KEY_SPEED)) {
258 mSpeed = savedState.getInt(KEY_SPEED);
259 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700260 if (savedState.containsKey(KEY_PSKTYPE)) {
261 pskType = savedState.getInt(KEY_PSKTYPE);
Jason Monkd52356a2015-01-28 10:40:41 -0500262 }
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700263 mInfo = savedState.getParcelable(KEY_WIFIINFO);
Jason Monkd52356a2015-01-28 10:40:41 -0500264 if (savedState.containsKey(KEY_NETWORKINFO)) {
265 mNetworkInfo = savedState.getParcelable(KEY_NETWORKINFO);
266 }
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800267 if (savedState.containsKey(KEY_SCANRESULTS)) {
268 Parcelable[] scanResults = savedState.getParcelableArray(KEY_SCANRESULTS);
269 mScanResults.clear();
270 for (Parcelable result : scanResults) {
271 mScanResults.add((ScanResult) result);
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700272 }
273 }
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700274 if (savedState.containsKey(KEY_SCOREDNETWORKCACHE)) {
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700275 ArrayList<TimestampedScoredNetwork> scoredNetworkArrayList =
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700276 savedState.getParcelableArrayList(KEY_SCOREDNETWORKCACHE);
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700277 for (TimestampedScoredNetwork timedScore : scoredNetworkArrayList) {
278 mScoredNetworkCache.put(timedScore.getScore().networkKey.wifiKey.bssid, timedScore);
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700279 }
280 }
Peter Qiuced37db2017-03-14 15:51:22 -0700281 if (savedState.containsKey(KEY_FQDN)) {
282 mFqdn = savedState.getString(KEY_FQDN);
283 }
284 if (savedState.containsKey(KEY_PROVIDER_FRIENDLY_NAME)) {
285 mProviderFriendlyName = savedState.getString(KEY_PROVIDER_FRIENDLY_NAME);
286 }
Peter Qiu280581b2017-07-24 14:18:56 -0700287 if (savedState.containsKey(KEY_IS_CARRIER_AP)) {
288 mIsCarrierAp = savedState.getBoolean(KEY_IS_CARRIER_AP);
289 }
290 if (savedState.containsKey(KEY_CARRIER_AP_EAP_TYPE)) {
291 mCarrierApEapType = savedState.getInt(KEY_CARRIER_AP_EAP_TYPE);
292 }
293 if (savedState.containsKey(KEY_CARRIER_NAME)) {
294 mCarrierName = savedState.getString(KEY_CARRIER_NAME);
295 }
Mitchell Wills5a42db22015-08-03 09:46:08 -0700296 update(mConfig, mInfo, mNetworkInfo);
Sundeep Ghumand1e44922017-08-07 11:21:38 -0700297
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800298 // Calculate required fields
299 updateKey();
Quang Luong8123f772019-02-08 16:30:12 -0800300 updateBestRssiInfo();
Jason Monkd52356a2015-01-28 10:40:41 -0500301 }
302
Quang Luong9051dfd2018-12-12 12:49:24 -0800303 /**
304 * Creates an AccessPoint with only a WifiConfiguration. This is used for the saved networks
305 * page.
Quang Luong9051dfd2018-12-12 12:49:24 -0800306 */
Peter Qiuced37db2017-03-14 15:51:22 -0700307 public AccessPoint(Context context, WifiConfiguration config) {
Jason Monkd52356a2015-01-28 10:40:41 -0500308 mContext = context;
Peter Qiuced37db2017-03-14 15:51:22 -0700309 loadConfig(config);
Quang Luong6e711c82019-03-07 14:52:55 -0800310 updateKey();
Jason Monkd52356a2015-01-28 10:40:41 -0500311 }
312
Peter Qiuced37db2017-03-14 15:51:22 -0700313 /**
314 * Initialize an AccessPoint object for a {@link PasspointConfiguration}. This is mainly
315 * used by "Saved Networks" page for managing the saved {@link PasspointConfiguration}.
316 */
317 public AccessPoint(Context context, PasspointConfiguration config) {
Jason Monkd52356a2015-01-28 10:40:41 -0500318 mContext = context;
Peter Qiuced37db2017-03-14 15:51:22 -0700319 mFqdn = config.getHomeSp().getFqdn();
320 mProviderFriendlyName = config.getHomeSp().getFriendlyName();
Quang Luong6e711c82019-03-07 14:52:55 -0800321 updateKey();
Quang Luong9051dfd2018-12-12 12:49:24 -0800322 }
323
324 /**
Quang Luong8123f772019-02-08 16:30:12 -0800325 * Initialize an AccessPoint object for a Passpoint network.
Quang Luong9051dfd2018-12-12 12:49:24 -0800326 */
Quang Luong8123f772019-02-08 16:30:12 -0800327 public AccessPoint(@NonNull Context context, @NonNull WifiConfiguration config,
328 @Nullable Collection<ScanResult> homeScans,
329 @Nullable Collection<ScanResult> roamingScans) {
330 mContext = context;
331 networkId = config.networkId;
332 mConfig = config;
333 setScanResultsPasspoint(homeScans, roamingScans);
334 updateKey();
335 }
336
337 /**
338 * Initialize an AccessPoint object for a Passpoint OSU Provider.
339 */
340 public AccessPoint(@NonNull Context context, @NonNull OsuProvider provider,
341 @NonNull Collection<ScanResult> results) {
Quang Luong9051dfd2018-12-12 12:49:24 -0800342 mContext = context;
343 mOsuProvider = provider;
Quang Luong8123f772019-02-08 16:30:12 -0800344 setScanResults(results);
Quang Luong9051dfd2018-12-12 12:49:24 -0800345 updateKey();
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700346 }
347
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800348 AccessPoint(Context context, Collection<ScanResult> results) {
Peter Qiuced37db2017-03-14 15:51:22 -0700349 mContext = context;
Quang Luong8123f772019-02-08 16:30:12 -0800350 setScanResults(results);
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800351 updateKey();
Peter Qiuced37db2017-03-14 15:51:22 -0700352 }
353
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800354 @VisibleForTesting void loadConfig(WifiConfiguration config) {
355 ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID));
356 bssid = config.BSSID;
357 security = getSecurity(config);
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800358 networkId = config.networkId;
359 mConfig = config;
360 }
361
362 /** Updates {@link #mKey} and should only called upon object creation/initialization. */
363 private void updateKey() {
364 // TODO(sghuman): Consolidate Key logic on ScanResultMatchInfo
Quang Luong7054e922018-12-07 16:44:18 -0800365 if (isPasspoint()) {
Quang Luongac1026e2019-01-02 17:37:01 -0800366 mKey = getKey(mConfig);
Quang Luong6e711c82019-03-07 14:52:55 -0800367 } else if (isPasspointConfig()) {
368 mKey = getKey(mFqdn);
Quang Luong9051dfd2018-12-12 12:49:24 -0800369 } else if (isOsuProvider()) {
Quang Luongac1026e2019-01-02 17:37:01 -0800370 mKey = getKey(mOsuProvider);
Quang Luong9051dfd2018-12-12 12:49:24 -0800371 } else { // Non-Passpoint AP
Quang Luongac1026e2019-01-02 17:37:01 -0800372 mKey = getKey(getSsidStr(), getBssid(), getSecurity());
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800373 }
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800374 }
375
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700376 /**
Dave Schaefer98537432017-02-08 11:26:08 -0800377 * Returns a negative integer, zero, or a positive integer if this AccessPoint is less than,
378 * equal to, or greater than the other AccessPoint.
379 *
380 * Sort order rules for AccessPoints:
381 * 1. Active before inactive
382 * 2. Reachable before unreachable
383 * 3. Saved before unsaved
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -0700384 * 4. Network speed value
Dave Schaefer98537432017-02-08 11:26:08 -0800385 * 5. Stronger signal before weaker signal
386 * 6. SSID alphabetically
387 *
388 * Note that AccessPoints with a signal are usually also Reachable,
389 * and will thus appear before unreachable saved AccessPoints.
390 */
Jason Monkd52356a2015-01-28 10:40:41 -0500391 @Override
Tony Mantlera0e03dd2016-01-27 15:57:03 -0800392 public int compareTo(@NonNull AccessPoint other) {
Jason Monkd52356a2015-01-28 10:40:41 -0500393 // Active one goes first.
394 if (isActive() && !other.isActive()) return -1;
395 if (!isActive() && other.isActive()) return 1;
396
397 // Reachable one goes before unreachable one.
Dave Schaefer98537432017-02-08 11:26:08 -0800398 if (isReachable() && !other.isReachable()) return -1;
399 if (!isReachable() && other.isReachable()) return 1;
Jason Monkd52356a2015-01-28 10:40:41 -0500400
Sundeep Ghuman05c41e22017-02-01 13:27:56 -0800401 // Configured (saved) one goes before unconfigured one.
Dave Schaefer98537432017-02-08 11:26:08 -0800402 if (isSaved() && !other.isSaved()) return -1;
403 if (!isSaved() && other.isSaved()) return 1;
Jason Monkd52356a2015-01-28 10:40:41 -0500404
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700405 // Faster speeds go before slower speeds - but only if visible change in speed label
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -0700406 if (getSpeed() != other.getSpeed()) {
407 return other.getSpeed() - getSpeed();
Sundeep Ghuman05c41e22017-02-01 13:27:56 -0800408 }
409
Tony Mantlera0e03dd2016-01-27 15:57:03 -0800410 // Sort by signal strength, bucketed by level
411 int difference = WifiManager.calculateSignalLevel(other.mRssi, SIGNAL_LEVELS)
412 - WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS);
Jason Monkd52356a2015-01-28 10:40:41 -0500413 if (difference != 0) {
414 return difference;
415 }
Sundeep Ghumand44b8e42017-06-21 21:40:06 -0700416
Quang Luong9051dfd2018-12-12 12:49:24 -0800417 // Sort by title.
418 difference = getTitle().compareToIgnoreCase(other.getTitle());
Sundeep Ghumand44b8e42017-06-21 21:40:06 -0700419 if (difference != 0) {
420 return difference;
421 }
422
423 // Do a case sensitive comparison to distinguish SSIDs that differ in case only
424 return getSsidStr().compareTo(other.getSsidStr());
Jason Monkd52356a2015-01-28 10:40:41 -0500425 }
426
427 @Override
428 public boolean equals(Object other) {
429 if (!(other instanceof AccessPoint)) return false;
430 return (this.compareTo((AccessPoint) other) == 0);
431 }
432
433 @Override
434 public int hashCode() {
435 int result = 0;
436 if (mInfo != null) result += 13 * mInfo.hashCode();
437 result += 19 * mRssi;
438 result += 23 * networkId;
439 result += 29 * ssid.hashCode();
440 return result;
441 }
442
443 @Override
444 public String toString() {
445 StringBuilder builder = new StringBuilder().append("AccessPoint(")
446 .append(ssid);
Sundeep Ghuman2b489902017-02-22 18:17:29 -0800447 if (bssid != null) {
448 builder.append(":").append(bssid);
449 }
Jason Monkd52356a2015-01-28 10:40:41 -0500450 if (isSaved()) {
451 builder.append(',').append("saved");
452 }
453 if (isActive()) {
454 builder.append(',').append("active");
455 }
456 if (isEphemeral()) {
457 builder.append(',').append("ephemeral");
458 }
459 if (isConnectable()) {
460 builder.append(',').append("connectable");
461 }
Hai Shalomaa6f9202018-10-16 14:30:02 -0700462 if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) {
Jason Monkd52356a2015-01-28 10:40:41 -0500463 builder.append(',').append(securityToString(security, pskType));
464 }
Sundeep Ghuman2b489902017-02-22 18:17:29 -0800465 builder.append(",level=").append(getLevel());
Sundeep Ghuman53200ed2017-06-21 16:54:36 -0700466 if (mSpeed != Speed.NONE) {
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -0700467 builder.append(",speed=").append(mSpeed);
Sundeep Ghuman8920e9c2017-04-27 16:16:41 -0700468 }
Stephen Chen21f68682017-04-04 13:23:31 -0700469 builder.append(",metered=").append(isMetered());
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800470
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -0800471 if (isVerboseLoggingEnabled()) {
Sundeep Ghumandb9b94c2017-09-06 11:46:21 -0700472 builder.append(",rssi=").append(mRssi);
Quang Luong8123f772019-02-08 16:30:12 -0800473 builder.append(",scan cache size=").append(mScanResults.size()
474 + mExtraScanResults.size());
Sundeep Ghumandb9b94c2017-09-06 11:46:21 -0700475 }
476
Jason Monkd52356a2015-01-28 10:40:41 -0500477 return builder.append(')').toString();
478 }
479
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800480 /**
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -0700481 * Updates the AccessPoint rankingScore, metering, and speed, returning true if the data has
Stephen Chen21f68682017-04-04 13:23:31 -0700482 * changed.
483 *
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700484 * @param scoreCache The score cache to use to retrieve scores
485 * @param scoringUiEnabled Whether to show scoring and badging UI
486 * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when
487 * generating speed labels
Stephen Chen21f68682017-04-04 13:23:31 -0700488 */
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700489 boolean update(
490 WifiNetworkScoreCache scoreCache,
491 boolean scoringUiEnabled,
492 long maxScoreCacheAgeMillis) {
Stephen Chen21f68682017-04-04 13:23:31 -0700493 boolean scoreChanged = false;
494 if (scoringUiEnabled) {
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700495 scoreChanged = updateScores(scoreCache, maxScoreCacheAgeMillis);
Stephen Chen21f68682017-04-04 13:23:31 -0700496 }
497 return updateMetered(scoreCache) || scoreChanged;
498 }
499
500 /**
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -0700501 * Updates the AccessPoint rankingScore and speed, returning true if the data has changed.
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800502 *
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700503 * <p>Any cached {@link TimestampedScoredNetwork} objects older than the given max age in millis
504 * will be removed when this method is invoked.
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700505 *
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700506 * <p>Precondition: {@link #mRssi} is up to date before invoking this method.
507 *
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700508 * @param scoreCache The score cache to use to retrieve scores
509 * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when
510 * generating speed labels
511 *
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700512 * @return true if the set speed has changed
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800513 */
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700514 private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) {
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700515 long nowMillis = SystemClock.elapsedRealtime();
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800516 for (ScanResult result : mScanResults) {
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -0700517 ScoredNetwork score = scoreCache.getScoredNetwork(result);
518 if (score == null) {
519 continue;
520 }
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700521 TimestampedScoredNetwork timedScore = mScoredNetworkCache.get(result.BSSID);
522 if (timedScore == null) {
523 mScoredNetworkCache.put(
524 result.BSSID, new TimestampedScoredNetwork(score, nowMillis));
525 } else {
526 // Update data since the has been seen in the score cache
527 timedScore.update(score, nowMillis);
528 }
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -0700529 }
530
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700531 // Remove old cached networks
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700532 long evictionCutoff = nowMillis - maxScoreCacheAgeMillis;
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700533 Iterator<TimestampedScoredNetwork> iterator = mScoredNetworkCache.values().iterator();
534 iterator.forEachRemaining(timestampedScoredNetwork -> {
535 if (timestampedScoredNetwork.getUpdatedTimestampMillis() < evictionCutoff) {
536 iterator.remove();
537 }
538 });
539
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700540 return updateSpeed();
541 }
542
543 /**
544 * Updates the internal speed, returning true if the update resulted in a speed label change.
545 */
546 private boolean updateSpeed() {
547 int oldSpeed = mSpeed;
548 mSpeed = generateAverageSpeedForSsid();
549
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700550 boolean changed = oldSpeed != mSpeed;
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -0800551 if(isVerboseLoggingEnabled() && changed) {
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700552 Log.i(TAG, String.format("%s: Set speed to %d", ssid, mSpeed));
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800553 }
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700554 return changed;
555 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800556
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700557 /** Creates a speed value for the current {@link #mRssi} by averaging all non zero badges. */
558 @Speed private int generateAverageSpeedForSsid() {
559 if (mScoredNetworkCache.isEmpty()) {
560 return Speed.NONE;
561 }
562
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700563 if (Log.isLoggable(TAG, Log.DEBUG)) {
564 Log.d(TAG, String.format("Generating fallbackspeed for %s using cache: %s",
565 getSsidStr(), mScoredNetworkCache));
566 }
567
Sundeep Ghumana7825692017-08-23 15:53:00 -0700568 // TODO(b/63073866): If flickering issues persist, consider mapping using getLevel rather
569 // than specific rssi value so score doesn't change without a visible wifi bar change. This
570 // issue is likely to be more evident for the active AP whose RSSI value is not half-lifed.
571
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700572 int count = 0;
573 int totalSpeed = 0;
574 for (TimestampedScoredNetwork timedScore : mScoredNetworkCache.values()) {
575 int speed = timedScore.getScore().calculateBadge(mRssi);
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700576 if (speed != Speed.NONE) {
577 count++;
578 totalSpeed += speed;
579 }
580 }
581 int speed = count == 0 ? Speed.NONE : totalSpeed / count;
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -0800582 if (isVerboseLoggingEnabled()) {
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700583 Log.i(TAG, String.format("%s generated fallback speed is: %d", getSsidStr(), speed));
584 }
585 return roundToClosestSpeedEnum(speed);
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800586 }
587
Stephen Chen21f68682017-04-04 13:23:31 -0700588 /**
589 * Updates the AccessPoint's metering based on {@link ScoredNetwork#meteredHint}, returning
590 * true if the metering changed.
591 */
592 private boolean updateMetered(WifiNetworkScoreCache scoreCache) {
593 boolean oldMetering = mIsScoredNetworkMetered;
594 mIsScoredNetworkMetered = false;
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700595
596 if (isActive() && mInfo != null) {
Eric Schwarzenbach733ea262017-08-08 10:28:21 -0700597 NetworkKey key = NetworkKey.createFromWifiInfo(mInfo);
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700598 ScoredNetwork score = scoreCache.getScoredNetwork(key);
599 if (score != null) {
600 mIsScoredNetworkMetered |= score.meteredHint;
Stephen Chen21f68682017-04-04 13:23:31 -0700601 }
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700602 } else {
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800603 for (ScanResult result : mScanResults) {
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700604 ScoredNetwork score = scoreCache.getScoredNetwork(result);
605 if (score == null) {
606 continue;
607 }
608 mIsScoredNetworkMetered |= score.meteredHint;
609 }
Stephen Chen21f68682017-04-04 13:23:31 -0700610 }
611 return oldMetering == mIsScoredNetworkMetered;
612 }
613
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800614 public static String getKey(ScanResult result) {
Quang Luongac1026e2019-01-02 17:37:01 -0800615 return getKey(result.SSID, result.BSSID, getSecurity(result));
Mitchell Wills18af4932016-08-10 13:49:21 -0700616 }
617
Quang Luongac1026e2019-01-02 17:37:01 -0800618 /**
619 * Returns the AccessPoint key for a WifiConfiguration.
620 * This will return a special Passpoint key if the config is for Passpoint.
621 */
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800622 public static String getKey(WifiConfiguration config) {
Quang Luong7054e922018-12-07 16:44:18 -0800623 if (config.isPasspoint()) {
Quang Luong6e711c82019-03-07 14:52:55 -0800624 return getKey(config.FQDN);
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800625 } else {
Quang Luongc2c947c2019-01-18 12:51:35 -0800626 return getKey(removeDoubleQuotes(config.SSID), config.BSSID, getSecurity(config));
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800627 }
Quang Luongac1026e2019-01-02 17:37:01 -0800628 }
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800629
Quang Luongac1026e2019-01-02 17:37:01 -0800630 /**
Quang Luong6e711c82019-03-07 14:52:55 -0800631 * Returns the AccessPoint key corresponding to a Passpoint network by its FQDN.
632 */
633 public static String getKey(String fqdn) {
634 return new StringBuilder()
635 .append(KEY_PREFIX_FQDN)
636 .append(fqdn).toString();
637 }
638
639 /**
Quang Luongac1026e2019-01-02 17:37:01 -0800640 * Returns the AccessPoint key corresponding to the OsuProvider.
641 */
642 public static String getKey(OsuProvider provider) {
643 return new StringBuilder()
644 .append(KEY_PREFIX_OSU)
Quang Luong1a8fece2019-01-11 14:58:14 -0800645 .append(provider.getFriendlyName())
Quang Luongac1026e2019-01-02 17:37:01 -0800646 .append(',')
647 .append(provider.getServerUri()).toString();
648 }
649
650 /**
651 * Returns the AccessPoint key for a normal non-Passpoint network by ssid/bssid and security.
652 */
653 private static String getKey(String ssid, String bssid, int security) {
654 StringBuilder builder = new StringBuilder();
655 builder.append(KEY_PREFIX_AP);
656 if (TextUtils.isEmpty(ssid)) {
657 builder.append(bssid);
658 } else {
659 builder.append(ssid);
660 }
661 builder.append(',').append(security);
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800662 return builder.toString();
663 }
664
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800665 public String getKey() {
666 return mKey;
Jason Monkd52356a2015-01-28 10:40:41 -0500667 }
668
669 public boolean matches(WifiConfiguration config) {
Quang Luong7054e922018-12-07 16:44:18 -0800670 if (config.isPasspoint()) {
671 return (isPasspoint() && config.FQDN.equals(mConfig.FQDN));
Bartosz Fabianowski6fb07562016-01-12 15:43:19 +0100672 } else {
Quang Luong7054e922018-12-07 16:44:18 -0800673 // Normal non-Passpoint network
Bartosz Fabianowski6fb07562016-01-12 15:43:19 +0100674 return ssid.equals(removeDoubleQuotes(config.SSID))
675 && security == getSecurity(config)
676 && (mConfig == null || mConfig.shared == config.shared);
677 }
Jason Monkd52356a2015-01-28 10:40:41 -0500678 }
679
680 public WifiConfiguration getConfig() {
681 return mConfig;
682 }
683
Peter Qiuced37db2017-03-14 15:51:22 -0700684 public String getPasspointFqdn() {
685 return mFqdn;
686 }
687
Jason Monkd52356a2015-01-28 10:40:41 -0500688 public void clearConfig() {
689 mConfig = null;
690 networkId = WifiConfiguration.INVALID_NETWORK_ID;
691 }
692
693 public WifiInfo getInfo() {
694 return mInfo;
695 }
696
Sundeep Ghumanaaa8a1b2017-03-13 14:40:56 -0700697 /**
698 * Returns the number of levels to show for a Wifi icon, from 0 to {@link #SIGNAL_LEVELS}-1.
699 *
700 * <p>Use {@#isReachable()} to determine if an AccessPoint is in range, as this method will
701 * always return at least 0.
702 */
Jason Monkd52356a2015-01-28 10:40:41 -0500703 public int getLevel() {
Tony Mantlera0e03dd2016-01-27 15:57:03 -0800704 return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS);
Jason Monkd52356a2015-01-28 10:40:41 -0500705 }
706
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700707 public int getRssi() {
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800708 return mRssi;
709 }
710
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800711 /**
712 * Returns the underlying scan result set.
713 *
714 * <p>Callers should not modify this set.
715 */
Quang Luong8123f772019-02-08 16:30:12 -0800716 public Set<ScanResult> getScanResults() {
717 Set<ScanResult> allScans = new ArraySet<>();
718 allScans.addAll(mScanResults);
719 allScans.addAll(mExtraScanResults);
720 return allScans;
721 }
jackqdyulei9ee9b392017-12-22 11:31:49 -0800722
723 public Map<String, TimestampedScoredNetwork> getScoredNetworkCache() {
724 return mScoredNetworkCache;
725 }
726
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800727 /**
Quang Luong8123f772019-02-08 16:30:12 -0800728 * Updates {@link #mRssi} and sets scan result information to that of the best RSSI scan result.
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800729 *
730 * <p>If the given connection is active, the existing value of {@link #mRssi} will be returned.
731 * If the given AccessPoint is not active, a value will be calculated from previous scan
Sundeep Ghumance78a5f2017-03-15 19:06:14 -0700732 * results, returning the best RSSI for all matching AccessPoints averaged with the previous
733 * value. If the access point is not connected and there are no scan results, the rssi will be
734 * set to {@link #UNREACHABLE_RSSI}.
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800735 */
Quang Luong8123f772019-02-08 16:30:12 -0800736 private void updateBestRssiInfo() {
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800737 if (this.isActive()) {
738 return;
739 }
740
Quang Luong8123f772019-02-08 16:30:12 -0800741 ScanResult bestResult = null;
742 int bestRssi = UNREACHABLE_RSSI;
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800743 for (ScanResult result : mScanResults) {
Quang Luong8123f772019-02-08 16:30:12 -0800744 if (result.level > bestRssi) {
745 bestRssi = result.level;
746 bestResult = result;
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700747 }
748 }
749
Quang Luong8123f772019-02-08 16:30:12 -0800750 // Set the rssi to the average of the current rssi and the previous rssi.
751 if (bestRssi != UNREACHABLE_RSSI && mRssi != UNREACHABLE_RSSI) {
752 mRssi = (mRssi + bestRssi) / 2;
Sundeep Ghumance78a5f2017-03-15 19:06:14 -0700753 } else {
Quang Luong8123f772019-02-08 16:30:12 -0800754 mRssi = bestRssi;
755 }
756
757 if (bestResult != null) {
758 ssid = bestResult.SSID;
759 bssid = bestResult.BSSID;
760 security = getSecurity(bestResult);
761 if (security == SECURITY_PSK) {
762 pskType = getPskType(bestResult);
763 }
764 mIsCarrierAp = bestResult.isCarrierAp;
765 mCarrierApEapType = bestResult.carrierApEapType;
766 mCarrierName = bestResult.carrierName;
767 }
768 // Update the config SSID of a Passpoint network to that of the best RSSI
769 if (isPasspoint()) {
770 mConfig.SSID = convertToQuotedString(ssid);
Sundeep Ghumance78a5f2017-03-15 19:06:14 -0700771 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700772 }
773
Stephen Chen21f68682017-04-04 13:23:31 -0700774 /**
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600775 * Returns if the network should be considered metered.
Stephen Chen21f68682017-04-04 13:23:31 -0700776 */
777 public boolean isMetered() {
778 return mIsScoredNetworkMetered
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600779 || WifiConfiguration.isMetered(mConfig, mInfo);
Stephen Chen21f68682017-04-04 13:23:31 -0700780 }
781
Jason Monkd52356a2015-01-28 10:40:41 -0500782 public NetworkInfo getNetworkInfo() {
783 return mNetworkInfo;
784 }
785
786 public int getSecurity() {
787 return security;
788 }
789
790 public String getSecurityString(boolean concise) {
791 Context context = mContext;
Tomoharu Hatano2bde6e72017-05-30 18:38:56 +0900792 if (isPasspoint() || isPasspointConfig()) {
Sanket Padawe194cce62015-07-10 10:53:31 -0700793 return concise ? context.getString(R.string.wifi_security_short_eap) :
794 context.getString(R.string.wifi_security_eap);
Sanket Padawed1878802015-05-12 10:27:19 -0700795 }
Jason Monkd52356a2015-01-28 10:40:41 -0500796 switch(security) {
797 case SECURITY_EAP:
798 return concise ? context.getString(R.string.wifi_security_short_eap) :
799 context.getString(R.string.wifi_security_eap);
Hai Shalomaa6f9202018-10-16 14:30:02 -0700800 case SECURITY_EAP_SUITE_B:
801 return concise ? context.getString(R.string.wifi_security_short_eap_suiteb) :
802 context.getString(R.string.wifi_security_eap_suiteb);
Jason Monkd52356a2015-01-28 10:40:41 -0500803 case SECURITY_PSK:
804 switch (pskType) {
805 case PSK_WPA:
806 return concise ? context.getString(R.string.wifi_security_short_wpa) :
807 context.getString(R.string.wifi_security_wpa);
808 case PSK_WPA2:
809 return concise ? context.getString(R.string.wifi_security_short_wpa2) :
810 context.getString(R.string.wifi_security_wpa2);
811 case PSK_WPA_WPA2:
812 return concise ? context.getString(R.string.wifi_security_short_wpa_wpa2) :
813 context.getString(R.string.wifi_security_wpa_wpa2);
814 case PSK_UNKNOWN:
815 default:
816 return concise ? context.getString(R.string.wifi_security_short_psk_generic)
817 : context.getString(R.string.wifi_security_psk_generic);
818 }
819 case SECURITY_WEP:
820 return concise ? context.getString(R.string.wifi_security_short_wep) :
821 context.getString(R.string.wifi_security_wep);
Hai Shalomaa6f9202018-10-16 14:30:02 -0700822 case SECURITY_SAE:
823 return concise ? context.getString(R.string.wifi_security_short_sae) :
824 context.getString(R.string.wifi_security_sae);
825 case SECURITY_OWE:
826 return concise ? context.getString(R.string.wifi_security_short_owe) :
827 context.getString(R.string.wifi_security_owe);
Jason Monkd52356a2015-01-28 10:40:41 -0500828 case SECURITY_NONE:
829 default:
830 return concise ? "" : context.getString(R.string.wifi_security_none);
831 }
832 }
833
Jason Monk6980d122015-06-15 10:07:55 -0400834 public String getSsidStr() {
Jason Monkd52356a2015-01-28 10:40:41 -0500835 return ssid;
836 }
837
Jason Monk60a82ff2016-02-25 13:55:03 -0500838 public String getBssid() {
839 return bssid;
840 }
841
Jason Monk6980d122015-06-15 10:07:55 -0400842 public CharSequence getSsid() {
pastychanga2998fc2018-06-26 13:37:34 +0800843 return ssid;
Jason Monk6980d122015-06-15 10:07:55 -0400844 }
845
Quang Luong19429e42019-02-20 17:10:51 -0800846 /**
847 * Returns the name associated with the stored config.
848 * @deprecated Please use {@link #getTitle()} instead to get the display name of an AccessPoint.
849 */
850 @Deprecated
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700851 public String getConfigName() {
852 if (mConfig != null && mConfig.isPasspoint()) {
853 return mConfig.providerFriendlyName;
Peter Qiuced37db2017-03-14 15:51:22 -0700854 } else if (mFqdn != null) {
855 return mProviderFriendlyName;
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700856 } else {
857 return ssid;
858 }
859 }
860
Jason Monkd52356a2015-01-28 10:40:41 -0500861 public DetailedState getDetailedState() {
Fan Zhang6acb7662016-10-17 12:40:03 -0700862 if (mNetworkInfo != null) {
863 return mNetworkInfo.getDetailedState();
864 }
865 Log.w(TAG, "NetworkInfo is null, cannot return detailed state");
866 return null;
Jason Monkd52356a2015-01-28 10:40:41 -0500867 }
868
Peter Qiu280581b2017-07-24 14:18:56 -0700869 public boolean isCarrierAp() {
870 return mIsCarrierAp;
871 }
872
873 public int getCarrierApEapType() {
874 return mCarrierApEapType;
875 }
876
877 public String getCarrierName() {
878 return mCarrierName;
879 }
880
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700881 public String getSavedNetworkSummary() {
Fan Zhang51365c32016-09-20 12:22:18 -0700882 WifiConfiguration config = mConfig;
883 if (config != null) {
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700884 PackageManager pm = mContext.getPackageManager();
885 String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID);
Fan Zhang51365c32016-09-20 12:22:18 -0700886 int userId = UserHandle.getUserId(config.creatorUid);
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700887 ApplicationInfo appInfo = null;
Fan Zhang51365c32016-09-20 12:22:18 -0700888 if (config.creatorName != null && config.creatorName.equals(systemName)) {
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700889 appInfo = mContext.getApplicationInfo();
890 } else {
891 try {
892 IPackageManager ipm = AppGlobals.getPackageManager();
Fan Zhang51365c32016-09-20 12:22:18 -0700893 appInfo = ipm.getApplicationInfo(config.creatorName, 0 /* flags */, userId);
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700894 } catch (RemoteException rex) {
895 }
896 }
897 if (appInfo != null &&
898 !appInfo.packageName.equals(mContext.getString(R.string.settings_package)) &&
899 !appInfo.packageName.equals(
900 mContext.getString(R.string.certinstaller_package))) {
901 return mContext.getString(R.string.saved_network, appInfo.loadLabel(pm));
902 }
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700903 }
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700904 return "";
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700905 }
906
Quang Luong7054e922018-12-07 16:44:18 -0800907 /**
908 * Returns the display title for the AccessPoint, such as for an AccessPointPreference's title.
909 */
910 public String getTitle() {
911 if (isPasspoint()) {
912 return mConfig.providerFriendlyName;
Quang Luong19429e42019-02-20 17:10:51 -0800913 } else if (isPasspointConfig()) {
914 return mProviderFriendlyName;
Quang Luong9051dfd2018-12-12 12:49:24 -0800915 } else if (isOsuProvider()) {
916 return mOsuProvider.getFriendlyName();
Quang Luong7054e922018-12-07 16:44:18 -0800917 } else {
918 return getSsidStr();
919 }
920 }
921
Jason Monkd52356a2015-01-28 10:40:41 -0500922 public String getSummary() {
Quang Luong19bae88c2018-12-14 16:05:05 -0800923 return getSettingsSummary();
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700924 }
925
926 public String getSettingsSummary() {
Jason Monkd52356a2015-01-28 10:40:41 -0500927 // Update to new summary
928 StringBuilder summary = new StringBuilder();
929
Quang Luong919d4642019-01-03 14:46:06 -0800930 if (isOsuProvider()) {
931 if (mOsuProvisioningComplete) {
Quang Luongf005c882019-01-29 16:32:03 -0800932 summary.append(mContext.getString(R.string.osu_sign_up_complete));
Quang Luong919d4642019-01-03 14:46:06 -0800933 } else if (mOsuFailure != null) {
934 summary.append(mOsuFailure);
935 } else if (mOsuStatus != null) {
936 summary.append(mOsuStatus);
937 } else {
Quang Luongf005c882019-01-29 16:32:03 -0800938 summary.append(mContext.getString(R.string.tap_to_sign_up));
Quang Luong919d4642019-01-03 14:46:06 -0800939 }
940 } else if (isActive()) {
Quang Luong8123f772019-02-08 16:30:12 -0800941 if (mConfig != null && getDetailedState() == DetailedState.CONNECTED
Quang Luong19bae88c2018-12-14 16:05:05 -0800942 && mIsCarrierAp) {
943 // This is the active connection on a carrier AP
944 summary.append(String.format(mContext.getString(R.string.connected_via_carrier),
945 mCarrierName));
946 } else {
Quang Luong98d06bd2019-01-30 13:42:26 -0800947 summary.append(getSummary(mContext, /* ssid */ null, getDetailedState(),
Roshan Piuse9b8e502019-01-23 15:42:44 -0800948 mInfo != null && mInfo.isEphemeral(),
949 mInfo != null ? mInfo.getNetworkSuggestionOrSpecifierPackageName() : null));
Jason Monkd52356a2015-01-28 10:40:41 -0500950 }
Quang Luong19bae88c2018-12-14 16:05:05 -0800951 } else { // not active
952 if (mConfig != null && mConfig.hasNoInternetAccess()) {
953 int messageID = mConfig.getNetworkSelectionStatus().isNetworkPermanentlyDisabled()
954 ? R.string.wifi_no_internet_no_reconnect
955 : R.string.wifi_no_internet;
956 summary.append(mContext.getString(messageID));
957 } else if (mConfig != null && !mConfig.getNetworkSelectionStatus().isNetworkEnabled()) {
958 WifiConfiguration.NetworkSelectionStatus networkStatus =
959 mConfig.getNetworkSelectionStatus();
960 switch (networkStatus.getNetworkSelectionDisableReason()) {
961 case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE:
962 summary.append(mContext.getString(R.string.wifi_disabled_password_failure));
Glen Kuhne446afac2017-06-22 16:02:33 -0700963 break;
Quang Luong19bae88c2018-12-14 16:05:05 -0800964 case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD:
965 summary.append(mContext.getString(R.string.wifi_check_password_try_again));
Glen Kuhne446afac2017-06-22 16:02:33 -0700966 break;
Quang Luong19bae88c2018-12-14 16:05:05 -0800967 case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE:
968 case WifiConfiguration.NetworkSelectionStatus.DISABLED_DNS_FAILURE:
969 summary.append(mContext.getString(R.string.wifi_disabled_network_failure));
970 break;
971 case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION:
972 summary.append(mContext.getString(R.string.wifi_disabled_generic));
973 break;
974 }
975 } else if (mConfig != null && mConfig.getNetworkSelectionStatus().isNotRecommended()) {
976 summary.append(mContext.getString(
977 R.string.wifi_disabled_by_recommendation_provider));
978 } else if (mIsCarrierAp) {
979 summary.append(String.format(mContext.getString(
980 R.string.available_via_carrier), mCarrierName));
981 } else if (!isReachable()) { // Wifi out of range
982 summary.append(mContext.getString(R.string.wifi_not_in_range));
983 } else { // In range, not disabled.
984 if (mConfig != null) { // Is saved network
985 // Last attempt to connect to this failed. Show reason why
986 switch (mConfig.recentFailure.getAssociationStatus()) {
987 case WifiConfiguration.RecentFailure.STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
988 summary.append(mContext.getString(
989 R.string.wifi_ap_unable_to_handle_new_sta));
990 break;
991 default:
992 // "Saved"
993 summary.append(mContext.getString(R.string.wifi_remembered));
994 break;
995 }
Glen Kuhne446afac2017-06-22 16:02:33 -0700996 }
Jason Monkd52356a2015-01-28 10:40:41 -0500997 }
998 }
999
Quang Luong19bae88c2018-12-14 16:05:05 -08001000
1001
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -08001002 if (isVerboseLoggingEnabled()) {
Quang Luong19bae88c2018-12-14 16:05:05 -08001003 summary.append(WifiUtils.buildLoggingSummary(this, mConfig));
Jason Monkd52356a2015-01-28 10:40:41 -05001004 }
Sundeep Ghuman271e5de2017-05-30 14:11:39 -07001005
Quang Luong19bae88c2018-12-14 16:05:05 -08001006 if (mConfig != null && (WifiUtils.isMeteredOverridden(mConfig) || mConfig.meteredHint)) {
Salvador Martinez6f6fe752018-03-21 16:33:53 -07001007 return mContext.getResources().getString(
1008 R.string.preference_summary_default_combination,
Quang Luong19bae88c2018-12-14 16:05:05 -08001009 WifiUtils.getMeteredLabel(mContext, mConfig),
Salvador Martinez6f6fe752018-03-21 16:33:53 -07001010 summary.toString());
1011 }
1012
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -07001013 // If Speed label and summary are both present, use the preference combination to combine
1014 // the two, else return the non-null one.
1015 if (getSpeedLabel() != null && summary.length() != 0) {
Sundeep Ghumand3171ca2017-07-12 23:02:24 -07001016 return mContext.getResources().getString(
1017 R.string.preference_summary_default_combination,
1018 getSpeedLabel(),
1019 summary.toString());
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -07001020 } else if (getSpeedLabel() != null) {
1021 return getSpeedLabel();
Sundeep Ghumand3171ca2017-07-12 23:02:24 -07001022 } else {
1023 return summary.toString();
Sundeep Ghuman271e5de2017-05-30 14:11:39 -07001024 }
Jason Monkd52356a2015-01-28 10:40:41 -05001025 }
1026
1027 /**
Jason Monkd52356a2015-01-28 10:40:41 -05001028 * Return whether this is the active connection.
1029 * For ephemeral connections (networkId is invalid), this returns false if the network is
1030 * disconnected.
1031 */
1032 public boolean isActive() {
1033 return mNetworkInfo != null &&
1034 (networkId != WifiConfiguration.INVALID_NETWORK_ID ||
1035 mNetworkInfo.getState() != State.DISCONNECTED);
1036 }
1037
1038 public boolean isConnectable() {
1039 return getLevel() != -1 && getDetailedState() == null;
1040 }
1041
1042 public boolean isEphemeral() {
Shirish Kalelec7a38ef2015-06-25 13:55:33 -07001043 return mInfo != null && mInfo.isEphemeral() &&
1044 mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED;
Jason Monkd52356a2015-01-28 10:40:41 -05001045 }
1046
Peter Qiuced37db2017-03-14 15:51:22 -07001047 /**
1048 * Return true if this AccessPoint represents a Passpoint AP.
1049 */
Vinit Deshpande5b7352c2015-07-09 16:53:12 -07001050 public boolean isPasspoint() {
1051 return mConfig != null && mConfig.isPasspoint();
1052 }
1053
Mitchell Wills5a42db22015-08-03 09:46:08 -07001054 /**
Peter Qiuced37db2017-03-14 15:51:22 -07001055 * Return true if this AccessPoint represents a Passpoint provider configuration.
1056 */
1057 public boolean isPasspointConfig() {
1058 return mFqdn != null;
1059 }
1060
1061 /**
Quang Luong9051dfd2018-12-12 12:49:24 -08001062 * Return true if this AccessPoint represents an OSU Provider.
1063 */
1064 public boolean isOsuProvider() {
1065 return mOsuProvider != null;
1066 }
1067
1068 /**
Quang Luong4d194842018-12-18 16:59:49 -08001069 * Starts the OSU Provisioning flow.
1070 */
1071 public void startOsuProvisioning() {
1072 mContext.getSystemService(WifiManager.class).startSubscriptionProvisioning(
1073 mOsuProvider,
Ecco Parke67533e2019-03-01 15:09:35 -08001074 mContext.getMainExecutor(),
1075 new AccessPointProvisioningCallback()
Quang Luong4d194842018-12-18 16:59:49 -08001076 );
1077 }
1078
1079 /**
Mitchell Wills5a42db22015-08-03 09:46:08 -07001080 * Return whether the given {@link WifiInfo} is for this access point.
1081 * If the current AP does not have a network Id then the config is used to
1082 * match based on SSID and security.
1083 */
1084 private boolean isInfoForThisAccessPoint(WifiConfiguration config, WifiInfo info) {
Quang Luong1f1207b2019-02-19 17:35:50 -08001085 if (info.isOsuAp() || mOsuStatus != null) {
1086 return (info.isOsuAp() && mOsuStatus != null);
1087 } else if (info.isPasspointAp() || isPasspoint()) {
1088 return (info.isPasspointAp() && isPasspoint()
Quang Luong2a6cdc52019-03-05 17:35:45 -08001089 && TextUtils.equals(info.getPasspointFqdn(), mConfig.FQDN));
Quang Luong919d4642019-01-03 14:46:06 -08001090 }
1091
Quang Luong1f1207b2019-02-19 17:35:50 -08001092 if (networkId != WifiConfiguration.INVALID_NETWORK_ID) {
Jason Monkd52356a2015-01-28 10:40:41 -05001093 return networkId == info.getNetworkId();
Mitchell Wills5a42db22015-08-03 09:46:08 -07001094 } else if (config != null) {
Quang Luong1f1207b2019-02-19 17:35:50 -08001095 return TextUtils.equals(getKey(config), getKey());
1096 } else {
Jason Monkd52356a2015-01-28 10:40:41 -05001097 // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID.
1098 // (Note that we only do this if the WifiConfiguration explicitly equals INVALID).
1099 // TODO: Handle hex string SSIDs.
Quang Luong1f1207b2019-02-19 17:35:50 -08001100 return TextUtils.equals(removeDoubleQuotes(info.getSSID()), ssid);
Jason Monkd52356a2015-01-28 10:40:41 -05001101 }
1102 }
1103
1104 public boolean isSaved() {
Quang Luong48ad03a2019-02-14 11:51:01 -08001105 return mConfig != null;
Jason Monkd52356a2015-01-28 10:40:41 -05001106 }
1107
1108 public Object getTag() {
1109 return mTag;
1110 }
1111
1112 public void setTag(Object tag) {
1113 mTag = tag;
1114 }
1115
1116 /**
1117 * Generate and save a default wifiConfiguration with common values.
1118 * Can only be called for unsecured networks.
1119 */
1120 public void generateOpenNetworkConfig() {
Hai Shalomaa6f9202018-10-16 14:30:02 -07001121 if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) {
Jason Monkd52356a2015-01-28 10:40:41 -05001122 throw new IllegalStateException();
Hai Shalomaa6f9202018-10-16 14:30:02 -07001123 }
Jason Monkd52356a2015-01-28 10:40:41 -05001124 if (mConfig != null)
1125 return;
1126 mConfig = new WifiConfiguration();
1127 mConfig.SSID = AccessPoint.convertToQuotedString(ssid);
Hai Shalomaa6f9202018-10-16 14:30:02 -07001128
1129 if (security == SECURITY_NONE) {
1130 mConfig.allowedKeyManagement.set(KeyMgmt.NONE);
1131 } else {
1132 mConfig.allowedKeyManagement.set(KeyMgmt.OWE);
1133 mConfig.requirePMF = true;
1134 }
Jason Monkd52356a2015-01-28 10:40:41 -05001135 }
1136
Jason Monkd52356a2015-01-28 10:40:41 -05001137 public void saveWifiState(Bundle savedState) {
Vinit Deshpandefcd46122015-06-11 18:22:23 -07001138 if (ssid != null) savedState.putString(KEY_SSID, getSsidStr());
1139 savedState.putInt(KEY_SECURITY, security);
Sundeep Ghumana28050a2017-07-12 22:09:25 -07001140 savedState.putInt(KEY_SPEED, mSpeed);
Vinit Deshpandefcd46122015-06-11 18:22:23 -07001141 savedState.putInt(KEY_PSKTYPE, pskType);
1142 if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig);
Jason Monkd52356a2015-01-28 10:40:41 -05001143 savedState.putParcelable(KEY_WIFIINFO, mInfo);
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001144 savedState.putParcelableArray(KEY_SCANRESULTS,
Quang Luong8123f772019-02-08 16:30:12 -08001145 mScanResults.toArray(new Parcelable[mScanResults.size()
1146 + mExtraScanResults.size()]));
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -07001147 savedState.putParcelableArrayList(KEY_SCOREDNETWORKCACHE,
1148 new ArrayList<>(mScoredNetworkCache.values()));
Jason Monkd52356a2015-01-28 10:40:41 -05001149 if (mNetworkInfo != null) {
1150 savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo);
1151 }
Peter Qiuced37db2017-03-14 15:51:22 -07001152 if (mFqdn != null) {
1153 savedState.putString(KEY_FQDN, mFqdn);
1154 }
1155 if (mProviderFriendlyName != null) {
1156 savedState.putString(KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName);
1157 }
Peter Qiu280581b2017-07-24 14:18:56 -07001158 savedState.putBoolean(KEY_IS_CARRIER_AP, mIsCarrierAp);
1159 savedState.putInt(KEY_CARRIER_AP_EAP_TYPE, mCarrierApEapType);
1160 savedState.putString(KEY_CARRIER_NAME, mCarrierName);
Jason Monkd52356a2015-01-28 10:40:41 -05001161 }
1162
1163 public void setListener(AccessPointListener listener) {
1164 mAccessPointListener = listener;
1165 }
1166
Sundeep Ghumand1e44922017-08-07 11:21:38 -07001167 /**
Quang Luong8123f772019-02-08 16:30:12 -08001168 * Sets {@link #mScanResults} to the given collection and updates info based on the best RSSI
1169 * scan result.
Sundeep Ghumand1e44922017-08-07 11:21:38 -07001170 *
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001171 * @param scanResults a collection of scan results to add to the internal set
Sundeep Ghumand1e44922017-08-07 11:21:38 -07001172 */
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001173 void setScanResults(Collection<ScanResult> scanResults) {
Quang Luong8123f772019-02-08 16:30:12 -08001174 if (CollectionUtils.isEmpty(scanResults)) {
1175 Log.d(TAG, "Cannot set scan results to empty list");
1176 return;
1177 }
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -08001178
Quang Luong7054e922018-12-07 16:44:18 -08001179 // Validate scan results are for current AP only by matching SSID/BSSID
Quang Luong9051dfd2018-12-12 12:49:24 -08001180 // Passpoint networks are not bound to a specific SSID/BSSID, so skip this for passpoint.
Quang Luong8123f772019-02-08 16:30:12 -08001181 if (mKey != null && !isPasspoint() && !isOsuProvider()) {
Quang Luong7054e922018-12-07 16:44:18 -08001182 for (ScanResult result : scanResults) {
1183 String scanResultKey = AccessPoint.getKey(result);
Quang Luong8123f772019-02-08 16:30:12 -08001184 if (mKey != null && !mKey.equals(scanResultKey)) {
1185 Log.d(TAG, String.format(
Quang Luong7054e922018-12-07 16:44:18 -08001186 "ScanResult %s\nkey of %s did not match current AP key %s",
Quang Luong8123f772019-02-08 16:30:12 -08001187 result, scanResultKey, mKey));
1188 return;
Quang Luong7054e922018-12-07 16:44:18 -08001189 }
Jason Monkd52356a2015-01-28 10:40:41 -05001190 }
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001191 }
1192
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001193 int oldLevel = getLevel();
1194 mScanResults.clear();
1195 mScanResults.addAll(scanResults);
Quang Luong8123f772019-02-08 16:30:12 -08001196 updateBestRssiInfo();
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001197 int newLevel = getLevel();
1198
1199 // If newLevel is 0, there will be no displayed Preference since the AP is unreachable
1200 if (newLevel > 0 && newLevel != oldLevel) {
1201 // Only update labels on visible rssi changes
1202 updateSpeed();
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001203 ThreadUtils.postOnMainThread(() -> {
1204 if (mAccessPointListener != null) {
1205 mAccessPointListener.onLevelChanged(this);
1206 }
1207 });
1208
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001209 }
1210
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001211 ThreadUtils.postOnMainThread(() -> {
1212 if (mAccessPointListener != null) {
1213 mAccessPointListener.onAccessPointChanged(this);
1214 }
1215 });
Quang Luong8123f772019-02-08 16:30:12 -08001216 }
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001217
Quang Luong8123f772019-02-08 16:30:12 -08001218 /**
1219 * Sets the internal scan result cache to the list of home scans.
1220 * If there are no home scans, then the roaming scan list is used, and the AccessPoint is
1221 * marked as roaming.
1222 */
1223 void setScanResultsPasspoint(
1224 @Nullable Collection<ScanResult> homeScans,
1225 @Nullable Collection<ScanResult> roamingScans) {
1226 mExtraScanResults.clear();
1227 if (!CollectionUtils.isEmpty(homeScans)) {
1228 if (!CollectionUtils.isEmpty(roamingScans)) {
1229 mExtraScanResults.addAll(roamingScans);
Jason Monkd52356a2015-01-28 10:40:41 -05001230 }
Quang Luong8123f772019-02-08 16:30:12 -08001231 mIsRoaming = false;
1232 setScanResults(homeScans);
1233 } else if (!CollectionUtils.isEmpty(roamingScans)) {
1234 mIsRoaming = true;
1235 setScanResults(roamingScans);
Jason Monkd52356a2015-01-28 10:40:41 -05001236 }
Jason Monkd52356a2015-01-28 10:40:41 -05001237 }
1238
Quang Luong9051dfd2018-12-12 12:49:24 -08001239 /**
1240 * Attempt to update the AccessPoint with the current connection info.
1241 * This is used to set an AccessPoint to the active one if the connection info matches, or
1242 * conversely to set an AccessPoint to inactive if the connection info does not match. The RSSI
1243 * is also updated upon a match. Listeners will be notified if an update occurred.
1244 *
1245 * This is called in {@link WifiTracker#updateAccessPoints} as well as in callbacks for handling
1246 * NETWORK_STATE_CHANGED_ACTION, RSSI_CHANGED_ACTION, and onCapabilitiesChanged in WifiTracker.
1247 *
1248 * Returns true if an update occurred.
1249 */
Sundeep Ghumand911da32017-07-05 20:06:05 -07001250 public boolean update(
1251 @Nullable WifiConfiguration config, WifiInfo info, NetworkInfo networkInfo) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001252 boolean updated = false;
1253 final int oldLevel = getLevel();
Mitchell Wills5a42db22015-08-03 09:46:08 -07001254 if (info != null && isInfoForThisAccessPoint(config, info)) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001255 updated = (mInfo == null);
Quang Luong1f1207b2019-02-19 17:35:50 -08001256 if (!isPasspoint() && mConfig != config) {
Sundeep Ghumanf4d33022017-06-05 19:47:36 -07001257 // We do not set updated = true as we do not want to increase the amount of sorting
1258 // and copying performed in WifiTracker at this time. If issues involving refresh
1259 // are still seen, we will investigate further.
1260 update(config); // Notifies the AccessPointListener of the change
1261 }
Glen Kuhned38708c2017-07-19 14:53:34 -07001262 if (mRssi != info.getRssi() && info.getRssi() != WifiInfo.INVALID_RSSI) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001263 mRssi = info.getRssi();
1264 updated = true;
Sundeep Ghuman5c5cd7a2017-05-03 12:45:44 -07001265 } else if (mNetworkInfo != null && networkInfo != null
1266 && mNetworkInfo.getDetailedState() != networkInfo.getDetailedState()) {
Sundeep Ghuman96a53572017-04-20 21:25:41 -07001267 updated = true;
Jason Monkd52356a2015-01-28 10:40:41 -05001268 }
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001269 mInfo = info;
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001270 mNetworkInfo = networkInfo;
Jason Monkd52356a2015-01-28 10:40:41 -05001271 } else if (mInfo != null) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001272 updated = true;
Jason Monkd52356a2015-01-28 10:40:41 -05001273 mInfo = null;
1274 mNetworkInfo = null;
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001275 }
1276 if (updated && mAccessPointListener != null) {
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001277 ThreadUtils.postOnMainThread(() -> {
1278 if (mAccessPointListener != null) {
1279 mAccessPointListener.onAccessPointChanged(this);
1280 }
1281 });
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001282
1283 if (oldLevel != getLevel() /* current level */) {
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001284 ThreadUtils.postOnMainThread(() -> {
1285 if (mAccessPointListener != null) {
1286 mAccessPointListener.onLevelChanged(this);
1287 }
1288 });
Jason Monkd52356a2015-01-28 10:40:41 -05001289 }
1290 }
Sundeep Ghumandb9b94c2017-09-06 11:46:21 -07001291
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001292 return updated;
Jason Monkd52356a2015-01-28 10:40:41 -05001293 }
1294
Sundeep Ghumand911da32017-07-05 20:06:05 -07001295 void update(@Nullable WifiConfiguration config) {
Vinit Deshpandefc406002015-04-15 18:10:55 -07001296 mConfig = config;
Quang Luong7054e922018-12-07 16:44:18 -08001297 if (mConfig != null) {
1298 ssid = removeDoubleQuotes(mConfig.SSID);
1299 }
Sundeep Ghumand911da32017-07-05 20:06:05 -07001300 networkId = config != null ? config.networkId : WifiConfiguration.INVALID_NETWORK_ID;
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001301 ThreadUtils.postOnMainThread(() -> {
1302 if (mAccessPointListener != null) {
1303 mAccessPointListener.onAccessPointChanged(this);
1304 }
1305 });
Vinit Deshpandefc406002015-04-15 18:10:55 -07001306 }
Shirish Kalelec7a38ef2015-06-25 13:55:33 -07001307
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -08001308 @VisibleForTesting
Sanket Padawe0775a982015-08-19 14:57:46 -07001309 void setRssi(int rssi) {
1310 mRssi = rssi;
1311 }
1312
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -08001313 /** Sets the rssi to {@link #UNREACHABLE_RSSI}. */
1314 void setUnreachable() {
1315 setRssi(AccessPoint.UNREACHABLE_RSSI);
1316 }
1317
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -07001318 int getSpeed() { return mSpeed;}
Sundeep Ghuman271e5de2017-05-30 14:11:39 -07001319
1320 @Nullable
1321 String getSpeedLabel() {
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -07001322 return getSpeedLabel(mSpeed);
1323 }
1324
1325 @Nullable
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -07001326 @Speed
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001327 private static int roundToClosestSpeedEnum(int speed) {
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -07001328 if (speed < Speed.SLOW) {
1329 return Speed.NONE;
1330 } else if (speed < (Speed.SLOW + Speed.MODERATE) / 2) {
1331 return Speed.SLOW;
1332 } else if (speed < (Speed.MODERATE + Speed.FAST) / 2) {
1333 return Speed.MODERATE;
1334 } else if (speed < (Speed.FAST + Speed.VERY_FAST) / 2) {
1335 return Speed.FAST;
1336 } else {
1337 return Speed.VERY_FAST;
1338 }
1339 }
1340
1341 @Nullable
jackqdyulei9ee9b392017-12-22 11:31:49 -08001342 String getSpeedLabel(@Speed int speed) {
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001343 return getSpeedLabel(mContext, speed);
1344 }
1345
1346 private static String getSpeedLabel(Context context, int speed) {
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -07001347 switch (speed) {
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001348 case Speed.VERY_FAST:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001349 return context.getString(R.string.speed_label_very_fast);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001350 case Speed.FAST:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001351 return context.getString(R.string.speed_label_fast);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001352 case Speed.MODERATE:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001353 return context.getString(R.string.speed_label_okay);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001354 case Speed.SLOW:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001355 return context.getString(R.string.speed_label_slow);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001356 case Speed.NONE:
Sundeep Ghuman271e5de2017-05-30 14:11:39 -07001357 default:
1358 return null;
1359 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -08001360 }
1361
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001362 /** Return the speed label for a {@link ScoredNetwork} at the specified {@code rssi} level. */
1363 @Nullable
1364 public static String getSpeedLabel(Context context, ScoredNetwork scoredNetwork, int rssi) {
1365 return getSpeedLabel(context, roundToClosestSpeedEnum(scoredNetwork.calculateBadge(rssi)));
1366 }
1367
Dave Schaefer98537432017-02-08 11:26:08 -08001368 /** Return true if the current RSSI is reachable, and false otherwise. */
Sundeep Ghumanaaa8a1b2017-03-13 14:40:56 -07001369 public boolean isReachable() {
Dave Schaefer98537432017-02-08 11:26:08 -08001370 return mRssi != UNREACHABLE_RSSI;
1371 }
1372
Roshan Piuse9b8e502019-01-23 15:42:44 -08001373 private static CharSequence getAppLabel(String packageName, PackageManager packageManager) {
1374 CharSequence appLabel = "";
1375 ApplicationInfo appInfo = null;
1376 try {
1377 int userId = UserHandle.getUserId(UserHandle.USER_CURRENT);
1378 appInfo = packageManager.getApplicationInfoAsUser(packageName, 0 /* flags */, userId);
1379 } catch (PackageManager.NameNotFoundException e) {
1380 Log.e(TAG, "Failed to get app info", e);
1381 return appLabel;
1382 }
1383 if (appInfo != null) {
1384 appLabel = appInfo.loadLabel(packageManager);
1385 }
1386 return appLabel;
1387 }
1388
Jason Monkd52356a2015-01-28 10:40:41 -05001389 public static String getSummary(Context context, String ssid, DetailedState state,
Quang Luong98d06bd2019-01-30 13:42:26 -08001390 boolean isEphemeral, String suggestionOrSpecifierPackageName) {
Quang Luong0a3edf02018-12-17 14:50:58 -08001391 if (state == DetailedState.CONNECTED) {
Quang Luong98d06bd2019-01-30 13:42:26 -08001392 if (isEphemeral && !TextUtils.isEmpty(suggestionOrSpecifierPackageName)) {
Roshan Piuse9b8e502019-01-23 15:42:44 -08001393 CharSequence appLabel =
1394 getAppLabel(suggestionOrSpecifierPackageName, context.getPackageManager());
1395 return context.getString(R.string.connected_via_app, appLabel);
Vinit Deshpandedcf00c92015-04-15 18:32:09 -07001396 } else if (isEphemeral) {
Vinit Deshpandefc406002015-04-15 18:10:55 -07001397 // Special case for connected + ephemeral networks.
Stephen Chen36dd5cf12017-03-20 13:27:51 -07001398 final NetworkScoreManager networkScoreManager = context.getSystemService(
1399 NetworkScoreManager.class);
1400 NetworkScorerAppData scorer = networkScoreManager.getActiveScorer();
1401 if (scorer != null && scorer.getRecommendationServiceLabel() != null) {
1402 String format = context.getString(R.string.connected_via_network_scorer);
1403 return String.format(format, scorer.getRecommendationServiceLabel());
1404 } else {
1405 return context.getString(R.string.connected_via_network_scorer_default);
1406 }
Vinit Deshpandefc406002015-04-15 18:10:55 -07001407 }
Jason Monkd52356a2015-01-28 10:40:41 -05001408 }
1409
Sanket Padawe7094d222015-05-01 16:55:00 -07001410 // Case when there is wifi connected without internet connectivity.
1411 final ConnectivityManager cm = (ConnectivityManager)
1412 context.getSystemService(Context.CONNECTIVITY_SERVICE);
1413 if (state == DetailedState.CONNECTED) {
1414 IWifiManager wifiManager = IWifiManager.Stub.asInterface(
1415 ServiceManager.getService(Context.WIFI_SERVICE));
Lorenzo Colitti1317e042016-12-13 13:30:07 +09001416 NetworkCapabilities nc = null;
Sanket Padawe7094d222015-05-01 16:55:00 -07001417
1418 try {
Lorenzo Colitti1317e042016-12-13 13:30:07 +09001419 nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork());
1420 } catch (RemoteException e) {}
1421
1422 if (nc != null) {
1423 if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) {
Maurice Lam7f6fc862017-07-12 16:43:49 -07001424 int id = context.getResources()
1425 .getIdentifier("network_available_sign_in", "string", "android");
1426 return context.getString(id);
lucaslinfc731ab2019-03-12 17:52:15 +08001427 } else if (nc.hasCapability(
1428 NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY)) {
1429 return context.getString(R.string.wifi_limited_connection);
Lorenzo Colitti1317e042016-12-13 13:30:07 +09001430 } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
1431 return context.getString(R.string.wifi_connected_no_internet);
1432 }
Sanket Padawe7094d222015-05-01 16:55:00 -07001433 }
1434 }
Fan Zhang6acb7662016-10-17 12:40:03 -07001435 if (state == null) {
1436 Log.w(TAG, "state is null, returning empty summary");
1437 return "";
1438 }
Jason Monkd52356a2015-01-28 10:40:41 -05001439 String[] formats = context.getResources().getStringArray((ssid == null)
1440 ? R.array.wifi_status : R.array.wifi_status_with_ssid);
1441 int index = state.ordinal();
1442
1443 if (index >= formats.length || formats[index].length() == 0) {
Sanket Padawe3e9e5fa2015-05-28 10:41:14 -07001444 return "";
Jason Monkd52356a2015-01-28 10:40:41 -05001445 }
1446 return String.format(formats[index], ssid);
1447 }
1448
Jason Monkd52356a2015-01-28 10:40:41 -05001449 public static String convertToQuotedString(String string) {
1450 return "\"" + string + "\"";
1451 }
1452
1453 private static int getPskType(ScanResult result) {
1454 boolean wpa = result.capabilities.contains("WPA-PSK");
1455 boolean wpa2 = result.capabilities.contains("WPA2-PSK");
1456 if (wpa2 && wpa) {
1457 return PSK_WPA_WPA2;
1458 } else if (wpa2) {
1459 return PSK_WPA2;
1460 } else if (wpa) {
1461 return PSK_WPA;
1462 } else {
1463 Log.w(TAG, "Received abnormal flag string: " + result.capabilities);
1464 return PSK_UNKNOWN;
1465 }
1466 }
1467
1468 private static int getSecurity(ScanResult result) {
1469 if (result.capabilities.contains("WEP")) {
1470 return SECURITY_WEP;
Hai Shalomaa6f9202018-10-16 14:30:02 -07001471 } else if (result.capabilities.contains("SAE")) {
1472 return SECURITY_SAE;
Jason Monkd52356a2015-01-28 10:40:41 -05001473 } else if (result.capabilities.contains("PSK")) {
1474 return SECURITY_PSK;
Hai Shalomaa6f9202018-10-16 14:30:02 -07001475 } else if (result.capabilities.contains("EAP_SUITE_B_192")) {
1476 return SECURITY_EAP_SUITE_B;
Jason Monkd52356a2015-01-28 10:40:41 -05001477 } else if (result.capabilities.contains("EAP")) {
1478 return SECURITY_EAP;
Hai Shalomaa6f9202018-10-16 14:30:02 -07001479 } else if (result.capabilities.contains("OWE")) {
1480 return SECURITY_OWE;
Jason Monkd52356a2015-01-28 10:40:41 -05001481 }
Hai Shalomaa6f9202018-10-16 14:30:02 -07001482
Jason Monkd52356a2015-01-28 10:40:41 -05001483 return SECURITY_NONE;
1484 }
1485
1486 static int getSecurity(WifiConfiguration config) {
Hai Shalomaa6f9202018-10-16 14:30:02 -07001487 if (config.allowedKeyManagement.get(KeyMgmt.SAE)) {
1488 return SECURITY_SAE;
1489 }
Jason Monkd52356a2015-01-28 10:40:41 -05001490 if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
1491 return SECURITY_PSK;
1492 }
Hai Shalomaa6f9202018-10-16 14:30:02 -07001493 if (config.allowedKeyManagement.get(KeyMgmt.SUITE_B_192)) {
1494 return SECURITY_EAP_SUITE_B;
1495 }
Jason Monkd52356a2015-01-28 10:40:41 -05001496 if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) ||
1497 config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
1498 return SECURITY_EAP;
1499 }
Hai Shalomaa6f9202018-10-16 14:30:02 -07001500 if (config.allowedKeyManagement.get(KeyMgmt.OWE)) {
1501 return SECURITY_OWE;
1502 }
Jason Monkd52356a2015-01-28 10:40:41 -05001503 return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
1504 }
1505
1506 public static String securityToString(int security, int pskType) {
1507 if (security == SECURITY_WEP) {
1508 return "WEP";
1509 } else if (security == SECURITY_PSK) {
1510 if (pskType == PSK_WPA) {
1511 return "WPA";
1512 } else if (pskType == PSK_WPA2) {
1513 return "WPA2";
1514 } else if (pskType == PSK_WPA_WPA2) {
1515 return "WPA_WPA2";
1516 }
1517 return "PSK";
1518 } else if (security == SECURITY_EAP) {
1519 return "EAP";
Hai Shalomaa6f9202018-10-16 14:30:02 -07001520 } else if (security == SECURITY_SAE) {
1521 return "SAE";
1522 } else if (security == SECURITY_EAP_SUITE_B) {
1523 return "SUITE_B";
1524 } else if (security == SECURITY_OWE) {
1525 return "OWE";
Jason Monkd52356a2015-01-28 10:40:41 -05001526 }
1527 return "NONE";
1528 }
1529
1530 static String removeDoubleQuotes(String string) {
Jason Monk2b51cc32015-05-13 11:07:53 -04001531 if (TextUtils.isEmpty(string)) {
1532 return "";
1533 }
Jason Monkd52356a2015-01-28 10:40:41 -05001534 int length = string.length();
1535 if ((length > 1) && (string.charAt(0) == '"')
1536 && (string.charAt(length - 1) == '"')) {
1537 return string.substring(1, length - 1);
1538 }
1539 return string;
1540 }
1541
Sundeep Ghumanbb399912018-01-29 18:31:15 -08001542 /**
1543 * Callbacks relaying changes to the AccessPoint representation.
1544 *
1545 * <p>All methods are invoked on the Main Thread.
1546 */
Jason Monkd52356a2015-01-28 10:40:41 -05001547 public interface AccessPointListener {
Sundeep Ghumanbb399912018-01-29 18:31:15 -08001548 /**
1549 * Indicates a change to the externally visible state of the AccessPoint trigger by an
1550 * update of ScanResults, saved configuration state, connection state, or score
1551 * (labels/metered) state.
1552 *
1553 * <p>Clients should refresh their view of the AccessPoint to match the updated state when
1554 * this is invoked. Overall this method is extraneous if clients are listening to
1555 * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks.
1556 *
1557 * <p>Examples of changes include signal strength, connection state, speed label, and
1558 * generally anything that would impact the summary string.
1559 *
1560 * @param accessPoint The accessPoint object the listener was registered on which has
1561 * changed
1562 */
1563 @MainThread void onAccessPointChanged(AccessPoint accessPoint);
1564
1565 /**
1566 * Indicates the "wifi pie signal level" has changed, retrieved via calls to
1567 * {@link AccessPoint#getLevel()}.
1568 *
1569 * <p>This call is a subset of {@link #onAccessPointChanged(AccessPoint)} , hence is also
1570 * extraneous if the client is already reacting to that or the
1571 * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks.
1572 *
1573 * @param accessPoint The accessPoint object the listener was registered on whose level has
1574 * changed
1575 */
1576 @MainThread void onLevelChanged(AccessPoint accessPoint);
Jason Monkd52356a2015-01-28 10:40:41 -05001577 }
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -08001578
1579 private static boolean isVerboseLoggingEnabled() {
1580 return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE);
1581 }
Quang Luong4d194842018-12-18 16:59:49 -08001582
1583 /**
1584 * Callbacks relaying changes to the OSU provisioning status started in startOsuProvisioning().
1585 *
1586 * All methods are invoked on the Main Thread
1587 */
Quang Luong8123f772019-02-08 16:30:12 -08001588 @VisibleForTesting
1589 class AccessPointProvisioningCallback extends ProvisioningCallback {
Quang Luong4d194842018-12-18 16:59:49 -08001590 @Override
1591 @MainThread public void onProvisioningFailure(int status) {
Quang Luongf005c882019-01-29 16:32:03 -08001592 if (TextUtils.equals(mOsuStatus, mContext.getString(R.string.osu_completing_sign_up))) {
1593 mOsuFailure = mContext.getString(R.string.osu_sign_up_failed);
1594 } else {
1595 mOsuFailure = mContext.getString(R.string.osu_connect_failed);
Quang Luong8d40a8e2019-01-02 16:25:38 -08001596 }
1597 mOsuStatus = null;
1598 mOsuProvisioningComplete = false;
1599 ThreadUtils.postOnMainThread(() -> {
1600 if (mAccessPointListener != null) {
1601 mAccessPointListener.onAccessPointChanged(AccessPoint.this);
1602 }
1603 });
Quang Luong4d194842018-12-18 16:59:49 -08001604 }
1605
1606 @Override
1607 @MainThread public void onProvisioningStatus(int status) {
Quang Luongf005c882019-01-29 16:32:03 -08001608 String newStatus = null;
Quang Luong8d40a8e2019-01-02 16:25:38 -08001609 switch (status) {
1610 case OSU_STATUS_AP_CONNECTING:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001611 case OSU_STATUS_AP_CONNECTED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001612 case OSU_STATUS_SERVER_CONNECTING:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001613 case OSU_STATUS_SERVER_VALIDATED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001614 case OSU_STATUS_SERVER_CONNECTED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001615 case OSU_STATUS_INIT_SOAP_EXCHANGE:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001616 case OSU_STATUS_WAITING_FOR_REDIRECT_RESPONSE:
Quang Luongf005c882019-01-29 16:32:03 -08001617 newStatus = String.format(mContext.getString(R.string.osu_opening_provider),
1618 mOsuProvider.getFriendlyName());
Quang Luong8d40a8e2019-01-02 16:25:38 -08001619 break;
1620 case OSU_STATUS_REDIRECT_RESPONSE_RECEIVED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001621 case OSU_STATUS_SECOND_SOAP_EXCHANGE:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001622 case OSU_STATUS_THIRD_SOAP_EXCHANGE:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001623 case OSU_STATUS_RETRIEVING_TRUST_ROOT_CERTS:
Quang Luongf005c882019-01-29 16:32:03 -08001624 newStatus = mContext.getString(
1625 R.string.osu_completing_sign_up);
Quang Luong8d40a8e2019-01-02 16:25:38 -08001626 break;
1627 }
Quang Luongf005c882019-01-29 16:32:03 -08001628 boolean updated = !TextUtils.equals(mOsuStatus, newStatus);
1629 mOsuStatus = newStatus;
Quang Luong8d40a8e2019-01-02 16:25:38 -08001630 mOsuFailure = null;
1631 mOsuProvisioningComplete = false;
Quang Luongf005c882019-01-29 16:32:03 -08001632 if (updated) {
1633 ThreadUtils.postOnMainThread(() -> {
1634 if (mAccessPointListener != null) {
1635 mAccessPointListener.onAccessPointChanged(AccessPoint.this);
1636 }
1637 });
1638 }
Quang Luong8d40a8e2019-01-02 16:25:38 -08001639 }
1640
1641 @Override
1642 @MainThread public void onProvisioningComplete() {
1643 mOsuProvisioningComplete = true;
1644 mOsuFailure = null;
1645 mOsuStatus = null;
1646 ThreadUtils.postOnMainThread(() -> {
1647 if (mAccessPointListener != null) {
1648 mAccessPointListener.onAccessPointChanged(AccessPoint.this);
1649 }
1650 });
Quang Luong4d194842018-12-18 16:59:49 -08001651 }
1652 }
Jason Monkd52356a2015-01-28 10:40:41 -05001653}