blob: d9b4885d1c81e92f83fd832fab5c554e55309805 [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);
Jason Monkd52356a2015-01-28 10:40:41 -0500310 }
311
Peter Qiuced37db2017-03-14 15:51:22 -0700312 /**
313 * Initialize an AccessPoint object for a {@link PasspointConfiguration}. This is mainly
314 * used by "Saved Networks" page for managing the saved {@link PasspointConfiguration}.
315 */
316 public AccessPoint(Context context, PasspointConfiguration config) {
Jason Monkd52356a2015-01-28 10:40:41 -0500317 mContext = context;
Peter Qiuced37db2017-03-14 15:51:22 -0700318 mFqdn = config.getHomeSp().getFqdn();
319 mProviderFriendlyName = config.getHomeSp().getFriendlyName();
Quang Luong9051dfd2018-12-12 12:49:24 -0800320 }
321
322 /**
Quang Luong8123f772019-02-08 16:30:12 -0800323 * Initialize an AccessPoint object for a Passpoint network.
Quang Luong9051dfd2018-12-12 12:49:24 -0800324 */
Quang Luong8123f772019-02-08 16:30:12 -0800325 public AccessPoint(@NonNull Context context, @NonNull WifiConfiguration config,
326 @Nullable Collection<ScanResult> homeScans,
327 @Nullable Collection<ScanResult> roamingScans) {
328 mContext = context;
329 networkId = config.networkId;
330 mConfig = config;
331 setScanResultsPasspoint(homeScans, roamingScans);
332 updateKey();
333 }
334
335 /**
336 * Initialize an AccessPoint object for a Passpoint OSU Provider.
337 */
338 public AccessPoint(@NonNull Context context, @NonNull OsuProvider provider,
339 @NonNull Collection<ScanResult> results) {
Quang Luong9051dfd2018-12-12 12:49:24 -0800340 mContext = context;
341 mOsuProvider = provider;
Quang Luong8123f772019-02-08 16:30:12 -0800342 setScanResults(results);
Quang Luong9051dfd2018-12-12 12:49:24 -0800343 updateKey();
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700344 }
345
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800346 AccessPoint(Context context, Collection<ScanResult> results) {
Peter Qiuced37db2017-03-14 15:51:22 -0700347 mContext = context;
Quang Luong8123f772019-02-08 16:30:12 -0800348 setScanResults(results);
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800349 updateKey();
Peter Qiuced37db2017-03-14 15:51:22 -0700350 }
351
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800352 @VisibleForTesting void loadConfig(WifiConfiguration config) {
353 ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID));
354 bssid = config.BSSID;
355 security = getSecurity(config);
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800356 networkId = config.networkId;
357 mConfig = config;
Quang Luong7054e922018-12-07 16:44:18 -0800358 updateKey();
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800359 }
360
361 /** Updates {@link #mKey} and should only called upon object creation/initialization. */
362 private void updateKey() {
363 // TODO(sghuman): Consolidate Key logic on ScanResultMatchInfo
Quang Luong7054e922018-12-07 16:44:18 -0800364 if (isPasspoint()) {
Quang Luongac1026e2019-01-02 17:37:01 -0800365 mKey = getKey(mConfig);
Quang Luong9051dfd2018-12-12 12:49:24 -0800366 } else if (isOsuProvider()) {
Quang Luongac1026e2019-01-02 17:37:01 -0800367 mKey = getKey(mOsuProvider);
Quang Luong9051dfd2018-12-12 12:49:24 -0800368 } else { // Non-Passpoint AP
Quang Luongac1026e2019-01-02 17:37:01 -0800369 mKey = getKey(getSsidStr(), getBssid(), getSecurity());
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800370 }
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800371 }
372
Ajay Nadathurd7b689a2016-08-31 15:07:56 -0700373 /**
Dave Schaefer98537432017-02-08 11:26:08 -0800374 * Returns a negative integer, zero, or a positive integer if this AccessPoint is less than,
375 * equal to, or greater than the other AccessPoint.
376 *
377 * Sort order rules for AccessPoints:
378 * 1. Active before inactive
379 * 2. Reachable before unreachable
380 * 3. Saved before unsaved
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -0700381 * 4. Network speed value
Dave Schaefer98537432017-02-08 11:26:08 -0800382 * 5. Stronger signal before weaker signal
383 * 6. SSID alphabetically
384 *
385 * Note that AccessPoints with a signal are usually also Reachable,
386 * and will thus appear before unreachable saved AccessPoints.
387 */
Jason Monkd52356a2015-01-28 10:40:41 -0500388 @Override
Tony Mantlera0e03dd2016-01-27 15:57:03 -0800389 public int compareTo(@NonNull AccessPoint other) {
Jason Monkd52356a2015-01-28 10:40:41 -0500390 // Active one goes first.
391 if (isActive() && !other.isActive()) return -1;
392 if (!isActive() && other.isActive()) return 1;
393
394 // Reachable one goes before unreachable one.
Dave Schaefer98537432017-02-08 11:26:08 -0800395 if (isReachable() && !other.isReachable()) return -1;
396 if (!isReachable() && other.isReachable()) return 1;
Jason Monkd52356a2015-01-28 10:40:41 -0500397
Sundeep Ghuman05c41e22017-02-01 13:27:56 -0800398 // Configured (saved) one goes before unconfigured one.
Dave Schaefer98537432017-02-08 11:26:08 -0800399 if (isSaved() && !other.isSaved()) return -1;
400 if (!isSaved() && other.isSaved()) return 1;
Jason Monkd52356a2015-01-28 10:40:41 -0500401
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700402 // Faster speeds go before slower speeds - but only if visible change in speed label
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -0700403 if (getSpeed() != other.getSpeed()) {
404 return other.getSpeed() - getSpeed();
Sundeep Ghuman05c41e22017-02-01 13:27:56 -0800405 }
406
Tony Mantlera0e03dd2016-01-27 15:57:03 -0800407 // Sort by signal strength, bucketed by level
408 int difference = WifiManager.calculateSignalLevel(other.mRssi, SIGNAL_LEVELS)
409 - WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS);
Jason Monkd52356a2015-01-28 10:40:41 -0500410 if (difference != 0) {
411 return difference;
412 }
Sundeep Ghumand44b8e42017-06-21 21:40:06 -0700413
Quang Luong9051dfd2018-12-12 12:49:24 -0800414 // Sort by title.
415 difference = getTitle().compareToIgnoreCase(other.getTitle());
Sundeep Ghumand44b8e42017-06-21 21:40:06 -0700416 if (difference != 0) {
417 return difference;
418 }
419
420 // Do a case sensitive comparison to distinguish SSIDs that differ in case only
421 return getSsidStr().compareTo(other.getSsidStr());
Jason Monkd52356a2015-01-28 10:40:41 -0500422 }
423
424 @Override
425 public boolean equals(Object other) {
426 if (!(other instanceof AccessPoint)) return false;
427 return (this.compareTo((AccessPoint) other) == 0);
428 }
429
430 @Override
431 public int hashCode() {
432 int result = 0;
433 if (mInfo != null) result += 13 * mInfo.hashCode();
434 result += 19 * mRssi;
435 result += 23 * networkId;
436 result += 29 * ssid.hashCode();
437 return result;
438 }
439
440 @Override
441 public String toString() {
442 StringBuilder builder = new StringBuilder().append("AccessPoint(")
443 .append(ssid);
Sundeep Ghuman2b489902017-02-22 18:17:29 -0800444 if (bssid != null) {
445 builder.append(":").append(bssid);
446 }
Jason Monkd52356a2015-01-28 10:40:41 -0500447 if (isSaved()) {
448 builder.append(',').append("saved");
449 }
450 if (isActive()) {
451 builder.append(',').append("active");
452 }
453 if (isEphemeral()) {
454 builder.append(',').append("ephemeral");
455 }
456 if (isConnectable()) {
457 builder.append(',').append("connectable");
458 }
Hai Shalomaa6f9202018-10-16 14:30:02 -0700459 if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) {
Jason Monkd52356a2015-01-28 10:40:41 -0500460 builder.append(',').append(securityToString(security, pskType));
461 }
Sundeep Ghuman2b489902017-02-22 18:17:29 -0800462 builder.append(",level=").append(getLevel());
Sundeep Ghuman53200ed2017-06-21 16:54:36 -0700463 if (mSpeed != Speed.NONE) {
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -0700464 builder.append(",speed=").append(mSpeed);
Sundeep Ghuman8920e9c2017-04-27 16:16:41 -0700465 }
Stephen Chen21f68682017-04-04 13:23:31 -0700466 builder.append(",metered=").append(isMetered());
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800467
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -0800468 if (isVerboseLoggingEnabled()) {
Sundeep Ghumandb9b94c2017-09-06 11:46:21 -0700469 builder.append(",rssi=").append(mRssi);
Quang Luong8123f772019-02-08 16:30:12 -0800470 builder.append(",scan cache size=").append(mScanResults.size()
471 + mExtraScanResults.size());
Sundeep Ghumandb9b94c2017-09-06 11:46:21 -0700472 }
473
Jason Monkd52356a2015-01-28 10:40:41 -0500474 return builder.append(')').toString();
475 }
476
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800477 /**
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -0700478 * Updates the AccessPoint rankingScore, metering, and speed, returning true if the data has
Stephen Chen21f68682017-04-04 13:23:31 -0700479 * changed.
480 *
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700481 * @param scoreCache The score cache to use to retrieve scores
482 * @param scoringUiEnabled Whether to show scoring and badging UI
483 * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when
484 * generating speed labels
Stephen Chen21f68682017-04-04 13:23:31 -0700485 */
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700486 boolean update(
487 WifiNetworkScoreCache scoreCache,
488 boolean scoringUiEnabled,
489 long maxScoreCacheAgeMillis) {
Stephen Chen21f68682017-04-04 13:23:31 -0700490 boolean scoreChanged = false;
491 if (scoringUiEnabled) {
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700492 scoreChanged = updateScores(scoreCache, maxScoreCacheAgeMillis);
Stephen Chen21f68682017-04-04 13:23:31 -0700493 }
494 return updateMetered(scoreCache) || scoreChanged;
495 }
496
497 /**
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -0700498 * Updates the AccessPoint rankingScore and speed, returning true if the data has changed.
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800499 *
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700500 * <p>Any cached {@link TimestampedScoredNetwork} objects older than the given max age in millis
501 * will be removed when this method is invoked.
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700502 *
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700503 * <p>Precondition: {@link #mRssi} is up to date before invoking this method.
504 *
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700505 * @param scoreCache The score cache to use to retrieve scores
506 * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when
507 * generating speed labels
508 *
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700509 * @return true if the set speed has changed
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800510 */
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700511 private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) {
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700512 long nowMillis = SystemClock.elapsedRealtime();
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800513 for (ScanResult result : mScanResults) {
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -0700514 ScoredNetwork score = scoreCache.getScoredNetwork(result);
515 if (score == null) {
516 continue;
517 }
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700518 TimestampedScoredNetwork timedScore = mScoredNetworkCache.get(result.BSSID);
519 if (timedScore == null) {
520 mScoredNetworkCache.put(
521 result.BSSID, new TimestampedScoredNetwork(score, nowMillis));
522 } else {
523 // Update data since the has been seen in the score cache
524 timedScore.update(score, nowMillis);
525 }
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -0700526 }
527
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700528 // Remove old cached networks
Sundeep Ghuman9bb85d32017-08-28 17:04:16 -0700529 long evictionCutoff = nowMillis - maxScoreCacheAgeMillis;
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700530 Iterator<TimestampedScoredNetwork> iterator = mScoredNetworkCache.values().iterator();
531 iterator.forEachRemaining(timestampedScoredNetwork -> {
532 if (timestampedScoredNetwork.getUpdatedTimestampMillis() < evictionCutoff) {
533 iterator.remove();
534 }
535 });
536
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700537 return updateSpeed();
538 }
539
540 /**
541 * Updates the internal speed, returning true if the update resulted in a speed label change.
542 */
543 private boolean updateSpeed() {
544 int oldSpeed = mSpeed;
545 mSpeed = generateAverageSpeedForSsid();
546
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700547 boolean changed = oldSpeed != mSpeed;
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -0800548 if(isVerboseLoggingEnabled() && changed) {
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700549 Log.i(TAG, String.format("%s: Set speed to %d", ssid, mSpeed));
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800550 }
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700551 return changed;
552 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800553
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700554 /** Creates a speed value for the current {@link #mRssi} by averaging all non zero badges. */
555 @Speed private int generateAverageSpeedForSsid() {
556 if (mScoredNetworkCache.isEmpty()) {
557 return Speed.NONE;
558 }
559
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700560 if (Log.isLoggable(TAG, Log.DEBUG)) {
561 Log.d(TAG, String.format("Generating fallbackspeed for %s using cache: %s",
562 getSsidStr(), mScoredNetworkCache));
563 }
564
Sundeep Ghumana7825692017-08-23 15:53:00 -0700565 // TODO(b/63073866): If flickering issues persist, consider mapping using getLevel rather
566 // than specific rssi value so score doesn't change without a visible wifi bar change. This
567 // issue is likely to be more evident for the active AP whose RSSI value is not half-lifed.
568
Sundeep Ghumanfedf4612017-08-15 15:47:18 -0700569 int count = 0;
570 int totalSpeed = 0;
571 for (TimestampedScoredNetwork timedScore : mScoredNetworkCache.values()) {
572 int speed = timedScore.getScore().calculateBadge(mRssi);
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700573 if (speed != Speed.NONE) {
574 count++;
575 totalSpeed += speed;
576 }
577 }
578 int speed = count == 0 ? Speed.NONE : totalSpeed / count;
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -0800579 if (isVerboseLoggingEnabled()) {
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -0700580 Log.i(TAG, String.format("%s generated fallback speed is: %d", getSsidStr(), speed));
581 }
582 return roundToClosestSpeedEnum(speed);
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -0800583 }
584
Stephen Chen21f68682017-04-04 13:23:31 -0700585 /**
586 * Updates the AccessPoint's metering based on {@link ScoredNetwork#meteredHint}, returning
587 * true if the metering changed.
588 */
589 private boolean updateMetered(WifiNetworkScoreCache scoreCache) {
590 boolean oldMetering = mIsScoredNetworkMetered;
591 mIsScoredNetworkMetered = false;
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700592
593 if (isActive() && mInfo != null) {
Eric Schwarzenbach733ea262017-08-08 10:28:21 -0700594 NetworkKey key = NetworkKey.createFromWifiInfo(mInfo);
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700595 ScoredNetwork score = scoreCache.getScoredNetwork(key);
596 if (score != null) {
597 mIsScoredNetworkMetered |= score.meteredHint;
Stephen Chen21f68682017-04-04 13:23:31 -0700598 }
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700599 } else {
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800600 for (ScanResult result : mScanResults) {
Sundeep Ghumana28050a2017-07-12 22:09:25 -0700601 ScoredNetwork score = scoreCache.getScoredNetwork(result);
602 if (score == null) {
603 continue;
604 }
605 mIsScoredNetworkMetered |= score.meteredHint;
606 }
Stephen Chen21f68682017-04-04 13:23:31 -0700607 }
608 return oldMetering == mIsScoredNetworkMetered;
609 }
610
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800611 public static String getKey(ScanResult result) {
Quang Luongac1026e2019-01-02 17:37:01 -0800612 return getKey(result.SSID, result.BSSID, getSecurity(result));
Mitchell Wills18af4932016-08-10 13:49:21 -0700613 }
614
Quang Luongac1026e2019-01-02 17:37:01 -0800615 /**
616 * Returns the AccessPoint key for a WifiConfiguration.
617 * This will return a special Passpoint key if the config is for Passpoint.
618 */
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800619 public static String getKey(WifiConfiguration config) {
Quang Luong7054e922018-12-07 16:44:18 -0800620 if (config.isPasspoint()) {
Quang Luongac1026e2019-01-02 17:37:01 -0800621 return new StringBuilder()
622 .append(KEY_PREFIX_FQDN)
623 .append(config.FQDN).toString();
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800624 } else {
Quang Luongc2c947c2019-01-18 12:51:35 -0800625 return getKey(removeDoubleQuotes(config.SSID), config.BSSID, getSecurity(config));
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800626 }
Quang Luongac1026e2019-01-02 17:37:01 -0800627 }
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800628
Quang Luongac1026e2019-01-02 17:37:01 -0800629 /**
630 * Returns the AccessPoint key corresponding to the OsuProvider.
631 */
632 public static String getKey(OsuProvider provider) {
633 return new StringBuilder()
634 .append(KEY_PREFIX_OSU)
Quang Luong1a8fece2019-01-11 14:58:14 -0800635 .append(provider.getFriendlyName())
Quang Luongac1026e2019-01-02 17:37:01 -0800636 .append(',')
637 .append(provider.getServerUri()).toString();
638 }
639
640 /**
641 * Returns the AccessPoint key for a normal non-Passpoint network by ssid/bssid and security.
642 */
643 private static String getKey(String ssid, String bssid, int security) {
644 StringBuilder builder = new StringBuilder();
645 builder.append(KEY_PREFIX_AP);
646 if (TextUtils.isEmpty(ssid)) {
647 builder.append(bssid);
648 } else {
649 builder.append(ssid);
650 }
651 builder.append(',').append(security);
Sundeep Ghuman0d492e82018-01-26 12:45:02 -0800652 return builder.toString();
653 }
654
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800655 public String getKey() {
656 return mKey;
Jason Monkd52356a2015-01-28 10:40:41 -0500657 }
658
659 public boolean matches(WifiConfiguration config) {
Quang Luong7054e922018-12-07 16:44:18 -0800660 if (config.isPasspoint()) {
661 return (isPasspoint() && config.FQDN.equals(mConfig.FQDN));
Bartosz Fabianowski6fb07562016-01-12 15:43:19 +0100662 } else {
Quang Luong7054e922018-12-07 16:44:18 -0800663 // Normal non-Passpoint network
Bartosz Fabianowski6fb07562016-01-12 15:43:19 +0100664 return ssid.equals(removeDoubleQuotes(config.SSID))
665 && security == getSecurity(config)
666 && (mConfig == null || mConfig.shared == config.shared);
667 }
Jason Monkd52356a2015-01-28 10:40:41 -0500668 }
669
670 public WifiConfiguration getConfig() {
671 return mConfig;
672 }
673
Peter Qiuced37db2017-03-14 15:51:22 -0700674 public String getPasspointFqdn() {
675 return mFqdn;
676 }
677
Jason Monkd52356a2015-01-28 10:40:41 -0500678 public void clearConfig() {
679 mConfig = null;
680 networkId = WifiConfiguration.INVALID_NETWORK_ID;
681 }
682
683 public WifiInfo getInfo() {
684 return mInfo;
685 }
686
Sundeep Ghumanaaa8a1b2017-03-13 14:40:56 -0700687 /**
688 * Returns the number of levels to show for a Wifi icon, from 0 to {@link #SIGNAL_LEVELS}-1.
689 *
690 * <p>Use {@#isReachable()} to determine if an AccessPoint is in range, as this method will
691 * always return at least 0.
692 */
Jason Monkd52356a2015-01-28 10:40:41 -0500693 public int getLevel() {
Tony Mantlera0e03dd2016-01-27 15:57:03 -0800694 return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS);
Jason Monkd52356a2015-01-28 10:40:41 -0500695 }
696
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700697 public int getRssi() {
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800698 return mRssi;
699 }
700
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800701 /**
702 * Returns the underlying scan result set.
703 *
704 * <p>Callers should not modify this set.
705 */
Quang Luong8123f772019-02-08 16:30:12 -0800706 public Set<ScanResult> getScanResults() {
707 Set<ScanResult> allScans = new ArraySet<>();
708 allScans.addAll(mScanResults);
709 allScans.addAll(mExtraScanResults);
710 return allScans;
711 }
jackqdyulei9ee9b392017-12-22 11:31:49 -0800712
713 public Map<String, TimestampedScoredNetwork> getScoredNetworkCache() {
714 return mScoredNetworkCache;
715 }
716
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800717 /**
Quang Luong8123f772019-02-08 16:30:12 -0800718 * Updates {@link #mRssi} and sets scan result information to that of the best RSSI scan result.
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800719 *
720 * <p>If the given connection is active, the existing value of {@link #mRssi} will be returned.
721 * If the given AccessPoint is not active, a value will be calculated from previous scan
Sundeep Ghumance78a5f2017-03-15 19:06:14 -0700722 * results, returning the best RSSI for all matching AccessPoints averaged with the previous
723 * value. If the access point is not connected and there are no scan results, the rssi will be
724 * set to {@link #UNREACHABLE_RSSI}.
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800725 */
Quang Luong8123f772019-02-08 16:30:12 -0800726 private void updateBestRssiInfo() {
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -0800727 if (this.isActive()) {
728 return;
729 }
730
Quang Luong8123f772019-02-08 16:30:12 -0800731 ScanResult bestResult = null;
732 int bestRssi = UNREACHABLE_RSSI;
Sundeep Ghuman04f7f342018-01-23 19:18:31 -0800733 for (ScanResult result : mScanResults) {
Quang Luong8123f772019-02-08 16:30:12 -0800734 if (result.level > bestRssi) {
735 bestRssi = result.level;
736 bestResult = result;
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700737 }
738 }
739
Quang Luong8123f772019-02-08 16:30:12 -0800740 // Set the rssi to the average of the current rssi and the previous rssi.
741 if (bestRssi != UNREACHABLE_RSSI && mRssi != UNREACHABLE_RSSI) {
742 mRssi = (mRssi + bestRssi) / 2;
Sundeep Ghumance78a5f2017-03-15 19:06:14 -0700743 } else {
Quang Luong8123f772019-02-08 16:30:12 -0800744 mRssi = bestRssi;
745 }
746
747 if (bestResult != null) {
748 ssid = bestResult.SSID;
749 bssid = bestResult.BSSID;
750 security = getSecurity(bestResult);
751 if (security == SECURITY_PSK) {
752 pskType = getPskType(bestResult);
753 }
754 mIsCarrierAp = bestResult.isCarrierAp;
755 mCarrierApEapType = bestResult.carrierApEapType;
756 mCarrierName = bestResult.carrierName;
757 }
758 // Update the config SSID of a Passpoint network to that of the best RSSI
759 if (isPasspoint()) {
760 mConfig.SSID = convertToQuotedString(ssid);
Sundeep Ghumance78a5f2017-03-15 19:06:14 -0700761 }
Vinit Deshpandefcd46122015-06-11 18:22:23 -0700762 }
763
Stephen Chen21f68682017-04-04 13:23:31 -0700764 /**
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600765 * Returns if the network should be considered metered.
Stephen Chen21f68682017-04-04 13:23:31 -0700766 */
767 public boolean isMetered() {
768 return mIsScoredNetworkMetered
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600769 || WifiConfiguration.isMetered(mConfig, mInfo);
Stephen Chen21f68682017-04-04 13:23:31 -0700770 }
771
Jason Monkd52356a2015-01-28 10:40:41 -0500772 public NetworkInfo getNetworkInfo() {
773 return mNetworkInfo;
774 }
775
776 public int getSecurity() {
777 return security;
778 }
779
780 public String getSecurityString(boolean concise) {
781 Context context = mContext;
Tomoharu Hatano2bde6e72017-05-30 18:38:56 +0900782 if (isPasspoint() || isPasspointConfig()) {
Sanket Padawe194cce62015-07-10 10:53:31 -0700783 return concise ? context.getString(R.string.wifi_security_short_eap) :
784 context.getString(R.string.wifi_security_eap);
Sanket Padawed1878802015-05-12 10:27:19 -0700785 }
Jason Monkd52356a2015-01-28 10:40:41 -0500786 switch(security) {
787 case SECURITY_EAP:
788 return concise ? context.getString(R.string.wifi_security_short_eap) :
789 context.getString(R.string.wifi_security_eap);
Hai Shalomaa6f9202018-10-16 14:30:02 -0700790 case SECURITY_EAP_SUITE_B:
791 return concise ? context.getString(R.string.wifi_security_short_eap_suiteb) :
792 context.getString(R.string.wifi_security_eap_suiteb);
Jason Monkd52356a2015-01-28 10:40:41 -0500793 case SECURITY_PSK:
794 switch (pskType) {
795 case PSK_WPA:
796 return concise ? context.getString(R.string.wifi_security_short_wpa) :
797 context.getString(R.string.wifi_security_wpa);
798 case PSK_WPA2:
799 return concise ? context.getString(R.string.wifi_security_short_wpa2) :
800 context.getString(R.string.wifi_security_wpa2);
801 case PSK_WPA_WPA2:
802 return concise ? context.getString(R.string.wifi_security_short_wpa_wpa2) :
803 context.getString(R.string.wifi_security_wpa_wpa2);
804 case PSK_UNKNOWN:
805 default:
806 return concise ? context.getString(R.string.wifi_security_short_psk_generic)
807 : context.getString(R.string.wifi_security_psk_generic);
808 }
809 case SECURITY_WEP:
810 return concise ? context.getString(R.string.wifi_security_short_wep) :
811 context.getString(R.string.wifi_security_wep);
Hai Shalomaa6f9202018-10-16 14:30:02 -0700812 case SECURITY_SAE:
813 return concise ? context.getString(R.string.wifi_security_short_sae) :
814 context.getString(R.string.wifi_security_sae);
815 case SECURITY_OWE:
816 return concise ? context.getString(R.string.wifi_security_short_owe) :
817 context.getString(R.string.wifi_security_owe);
Jason Monkd52356a2015-01-28 10:40:41 -0500818 case SECURITY_NONE:
819 default:
820 return concise ? "" : context.getString(R.string.wifi_security_none);
821 }
822 }
823
Jason Monk6980d122015-06-15 10:07:55 -0400824 public String getSsidStr() {
Jason Monkd52356a2015-01-28 10:40:41 -0500825 return ssid;
826 }
827
Jason Monk60a82ff2016-02-25 13:55:03 -0500828 public String getBssid() {
829 return bssid;
830 }
831
Jason Monk6980d122015-06-15 10:07:55 -0400832 public CharSequence getSsid() {
pastychanga2998fc2018-06-26 13:37:34 +0800833 return ssid;
Jason Monk6980d122015-06-15 10:07:55 -0400834 }
835
Quang Luong19429e42019-02-20 17:10:51 -0800836 /**
837 * Returns the name associated with the stored config.
838 * @deprecated Please use {@link #getTitle()} instead to get the display name of an AccessPoint.
839 */
840 @Deprecated
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700841 public String getConfigName() {
842 if (mConfig != null && mConfig.isPasspoint()) {
843 return mConfig.providerFriendlyName;
Peter Qiuced37db2017-03-14 15:51:22 -0700844 } else if (mFqdn != null) {
845 return mProviderFriendlyName;
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700846 } else {
847 return ssid;
848 }
849 }
850
Jason Monkd52356a2015-01-28 10:40:41 -0500851 public DetailedState getDetailedState() {
Fan Zhang6acb7662016-10-17 12:40:03 -0700852 if (mNetworkInfo != null) {
853 return mNetworkInfo.getDetailedState();
854 }
855 Log.w(TAG, "NetworkInfo is null, cannot return detailed state");
856 return null;
Jason Monkd52356a2015-01-28 10:40:41 -0500857 }
858
Peter Qiu280581b2017-07-24 14:18:56 -0700859 public boolean isCarrierAp() {
860 return mIsCarrierAp;
861 }
862
863 public int getCarrierApEapType() {
864 return mCarrierApEapType;
865 }
866
867 public String getCarrierName() {
868 return mCarrierName;
869 }
870
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700871 public String getSavedNetworkSummary() {
Fan Zhang51365c32016-09-20 12:22:18 -0700872 WifiConfiguration config = mConfig;
873 if (config != null) {
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700874 PackageManager pm = mContext.getPackageManager();
875 String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID);
Fan Zhang51365c32016-09-20 12:22:18 -0700876 int userId = UserHandle.getUserId(config.creatorUid);
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700877 ApplicationInfo appInfo = null;
Fan Zhang51365c32016-09-20 12:22:18 -0700878 if (config.creatorName != null && config.creatorName.equals(systemName)) {
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700879 appInfo = mContext.getApplicationInfo();
880 } else {
881 try {
882 IPackageManager ipm = AppGlobals.getPackageManager();
Fan Zhang51365c32016-09-20 12:22:18 -0700883 appInfo = ipm.getApplicationInfo(config.creatorName, 0 /* flags */, userId);
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700884 } catch (RemoteException rex) {
885 }
886 }
887 if (appInfo != null &&
888 !appInfo.packageName.equals(mContext.getString(R.string.settings_package)) &&
889 !appInfo.packageName.equals(
890 mContext.getString(R.string.certinstaller_package))) {
891 return mContext.getString(R.string.saved_network, appInfo.loadLabel(pm));
892 }
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700893 }
Sanket Padawe56cfbfb2015-05-05 20:10:46 -0700894 return "";
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700895 }
896
Quang Luong7054e922018-12-07 16:44:18 -0800897 /**
898 * Returns the display title for the AccessPoint, such as for an AccessPointPreference's title.
899 */
900 public String getTitle() {
901 if (isPasspoint()) {
902 return mConfig.providerFriendlyName;
Quang Luong19429e42019-02-20 17:10:51 -0800903 } else if (isPasspointConfig()) {
904 return mProviderFriendlyName;
Quang Luong9051dfd2018-12-12 12:49:24 -0800905 } else if (isOsuProvider()) {
906 return mOsuProvider.getFriendlyName();
Quang Luong7054e922018-12-07 16:44:18 -0800907 } else {
908 return getSsidStr();
909 }
910 }
911
Jason Monkd52356a2015-01-28 10:40:41 -0500912 public String getSummary() {
Quang Luong19bae88c2018-12-14 16:05:05 -0800913 return getSettingsSummary();
Vinit Deshpandedcf00c92015-04-15 18:32:09 -0700914 }
915
916 public String getSettingsSummary() {
Jason Monkd52356a2015-01-28 10:40:41 -0500917 // Update to new summary
918 StringBuilder summary = new StringBuilder();
919
Quang Luong919d4642019-01-03 14:46:06 -0800920 if (isOsuProvider()) {
921 if (mOsuProvisioningComplete) {
Quang Luongf005c882019-01-29 16:32:03 -0800922 summary.append(mContext.getString(R.string.osu_sign_up_complete));
Quang Luong919d4642019-01-03 14:46:06 -0800923 } else if (mOsuFailure != null) {
924 summary.append(mOsuFailure);
925 } else if (mOsuStatus != null) {
926 summary.append(mOsuStatus);
927 } else {
Quang Luongf005c882019-01-29 16:32:03 -0800928 summary.append(mContext.getString(R.string.tap_to_sign_up));
Quang Luong919d4642019-01-03 14:46:06 -0800929 }
930 } else if (isActive()) {
Quang Luong8123f772019-02-08 16:30:12 -0800931 if (mConfig != null && getDetailedState() == DetailedState.CONNECTED
Quang Luong19bae88c2018-12-14 16:05:05 -0800932 && mIsCarrierAp) {
933 // This is the active connection on a carrier AP
934 summary.append(String.format(mContext.getString(R.string.connected_via_carrier),
935 mCarrierName));
936 } else {
Quang Luong98d06bd2019-01-30 13:42:26 -0800937 summary.append(getSummary(mContext, /* ssid */ null, getDetailedState(),
Roshan Piuse9b8e502019-01-23 15:42:44 -0800938 mInfo != null && mInfo.isEphemeral(),
939 mInfo != null ? mInfo.getNetworkSuggestionOrSpecifierPackageName() : null));
Jason Monkd52356a2015-01-28 10:40:41 -0500940 }
Quang Luong19bae88c2018-12-14 16:05:05 -0800941 } else { // not active
942 if (mConfig != null && mConfig.hasNoInternetAccess()) {
943 int messageID = mConfig.getNetworkSelectionStatus().isNetworkPermanentlyDisabled()
944 ? R.string.wifi_no_internet_no_reconnect
945 : R.string.wifi_no_internet;
946 summary.append(mContext.getString(messageID));
947 } else if (mConfig != null && !mConfig.getNetworkSelectionStatus().isNetworkEnabled()) {
948 WifiConfiguration.NetworkSelectionStatus networkStatus =
949 mConfig.getNetworkSelectionStatus();
950 switch (networkStatus.getNetworkSelectionDisableReason()) {
951 case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE:
952 summary.append(mContext.getString(R.string.wifi_disabled_password_failure));
Glen Kuhne446afac2017-06-22 16:02:33 -0700953 break;
Quang Luong19bae88c2018-12-14 16:05:05 -0800954 case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD:
955 summary.append(mContext.getString(R.string.wifi_check_password_try_again));
Glen Kuhne446afac2017-06-22 16:02:33 -0700956 break;
Quang Luong19bae88c2018-12-14 16:05:05 -0800957 case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE:
958 case WifiConfiguration.NetworkSelectionStatus.DISABLED_DNS_FAILURE:
959 summary.append(mContext.getString(R.string.wifi_disabled_network_failure));
960 break;
961 case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION:
962 summary.append(mContext.getString(R.string.wifi_disabled_generic));
963 break;
964 }
965 } else if (mConfig != null && mConfig.getNetworkSelectionStatus().isNotRecommended()) {
966 summary.append(mContext.getString(
967 R.string.wifi_disabled_by_recommendation_provider));
968 } else if (mIsCarrierAp) {
969 summary.append(String.format(mContext.getString(
970 R.string.available_via_carrier), mCarrierName));
971 } else if (!isReachable()) { // Wifi out of range
972 summary.append(mContext.getString(R.string.wifi_not_in_range));
973 } else { // In range, not disabled.
974 if (mConfig != null) { // Is saved network
975 // Last attempt to connect to this failed. Show reason why
976 switch (mConfig.recentFailure.getAssociationStatus()) {
977 case WifiConfiguration.RecentFailure.STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
978 summary.append(mContext.getString(
979 R.string.wifi_ap_unable_to_handle_new_sta));
980 break;
981 default:
982 // "Saved"
983 summary.append(mContext.getString(R.string.wifi_remembered));
984 break;
985 }
Glen Kuhne446afac2017-06-22 16:02:33 -0700986 }
Jason Monkd52356a2015-01-28 10:40:41 -0500987 }
988 }
989
Quang Luong19bae88c2018-12-14 16:05:05 -0800990
991
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -0800992 if (isVerboseLoggingEnabled()) {
Quang Luong19bae88c2018-12-14 16:05:05 -0800993 summary.append(WifiUtils.buildLoggingSummary(this, mConfig));
Jason Monkd52356a2015-01-28 10:40:41 -0500994 }
Sundeep Ghuman271e5de2017-05-30 14:11:39 -0700995
Quang Luong19bae88c2018-12-14 16:05:05 -0800996 if (mConfig != null && (WifiUtils.isMeteredOverridden(mConfig) || mConfig.meteredHint)) {
Salvador Martinez6f6fe752018-03-21 16:33:53 -0700997 return mContext.getResources().getString(
998 R.string.preference_summary_default_combination,
Quang Luong19bae88c2018-12-14 16:05:05 -0800999 WifiUtils.getMeteredLabel(mContext, mConfig),
Salvador Martinez6f6fe752018-03-21 16:33:53 -07001000 summary.toString());
1001 }
1002
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -07001003 // If Speed label and summary are both present, use the preference combination to combine
1004 // the two, else return the non-null one.
1005 if (getSpeedLabel() != null && summary.length() != 0) {
Sundeep Ghumand3171ca2017-07-12 23:02:24 -07001006 return mContext.getResources().getString(
1007 R.string.preference_summary_default_combination,
1008 getSpeedLabel(),
1009 summary.toString());
Eric Schwarzenbachf4f3842b2017-07-17 16:45:04 -07001010 } else if (getSpeedLabel() != null) {
1011 return getSpeedLabel();
Sundeep Ghumand3171ca2017-07-12 23:02:24 -07001012 } else {
1013 return summary.toString();
Sundeep Ghuman271e5de2017-05-30 14:11:39 -07001014 }
Jason Monkd52356a2015-01-28 10:40:41 -05001015 }
1016
1017 /**
Jason Monkd52356a2015-01-28 10:40:41 -05001018 * Return whether this is the active connection.
1019 * For ephemeral connections (networkId is invalid), this returns false if the network is
1020 * disconnected.
1021 */
1022 public boolean isActive() {
1023 return mNetworkInfo != null &&
1024 (networkId != WifiConfiguration.INVALID_NETWORK_ID ||
1025 mNetworkInfo.getState() != State.DISCONNECTED);
1026 }
1027
1028 public boolean isConnectable() {
1029 return getLevel() != -1 && getDetailedState() == null;
1030 }
1031
1032 public boolean isEphemeral() {
Shirish Kalelec7a38ef2015-06-25 13:55:33 -07001033 return mInfo != null && mInfo.isEphemeral() &&
1034 mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED;
Jason Monkd52356a2015-01-28 10:40:41 -05001035 }
1036
Peter Qiuced37db2017-03-14 15:51:22 -07001037 /**
1038 * Return true if this AccessPoint represents a Passpoint AP.
1039 */
Vinit Deshpande5b7352c2015-07-09 16:53:12 -07001040 public boolean isPasspoint() {
1041 return mConfig != null && mConfig.isPasspoint();
1042 }
1043
Mitchell Wills5a42db22015-08-03 09:46:08 -07001044 /**
Peter Qiuced37db2017-03-14 15:51:22 -07001045 * Return true if this AccessPoint represents a Passpoint provider configuration.
1046 */
1047 public boolean isPasspointConfig() {
1048 return mFqdn != null;
1049 }
1050
1051 /**
Quang Luong9051dfd2018-12-12 12:49:24 -08001052 * Return true if this AccessPoint represents an OSU Provider.
1053 */
1054 public boolean isOsuProvider() {
1055 return mOsuProvider != null;
1056 }
1057
1058 /**
Quang Luong4d194842018-12-18 16:59:49 -08001059 * Starts the OSU Provisioning flow.
1060 */
1061 public void startOsuProvisioning() {
1062 mContext.getSystemService(WifiManager.class).startSubscriptionProvisioning(
1063 mOsuProvider,
Ecco Parke67533e2019-03-01 15:09:35 -08001064 mContext.getMainExecutor(),
1065 new AccessPointProvisioningCallback()
Quang Luong4d194842018-12-18 16:59:49 -08001066 );
1067 }
1068
1069 /**
Mitchell Wills5a42db22015-08-03 09:46:08 -07001070 * Return whether the given {@link WifiInfo} is for this access point.
1071 * If the current AP does not have a network Id then the config is used to
1072 * match based on SSID and security.
1073 */
1074 private boolean isInfoForThisAccessPoint(WifiConfiguration config, WifiInfo info) {
Quang Luong1f1207b2019-02-19 17:35:50 -08001075 if (info.isOsuAp() || mOsuStatus != null) {
1076 return (info.isOsuAp() && mOsuStatus != null);
1077 } else if (info.isPasspointAp() || isPasspoint()) {
1078 return (info.isPasspointAp() && isPasspoint()
1079 && TextUtils.equals(info.getFqdn(), mConfig.FQDN));
Quang Luong919d4642019-01-03 14:46:06 -08001080 }
1081
Quang Luong1f1207b2019-02-19 17:35:50 -08001082 if (networkId != WifiConfiguration.INVALID_NETWORK_ID) {
Jason Monkd52356a2015-01-28 10:40:41 -05001083 return networkId == info.getNetworkId();
Mitchell Wills5a42db22015-08-03 09:46:08 -07001084 } else if (config != null) {
Quang Luong1f1207b2019-02-19 17:35:50 -08001085 return TextUtils.equals(getKey(config), getKey());
1086 } else {
Jason Monkd52356a2015-01-28 10:40:41 -05001087 // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID.
1088 // (Note that we only do this if the WifiConfiguration explicitly equals INVALID).
1089 // TODO: Handle hex string SSIDs.
Quang Luong1f1207b2019-02-19 17:35:50 -08001090 return TextUtils.equals(removeDoubleQuotes(info.getSSID()), ssid);
Jason Monkd52356a2015-01-28 10:40:41 -05001091 }
1092 }
1093
1094 public boolean isSaved() {
Quang Luong48ad03a2019-02-14 11:51:01 -08001095 return mConfig != null;
Jason Monkd52356a2015-01-28 10:40:41 -05001096 }
1097
1098 public Object getTag() {
1099 return mTag;
1100 }
1101
1102 public void setTag(Object tag) {
1103 mTag = tag;
1104 }
1105
1106 /**
1107 * Generate and save a default wifiConfiguration with common values.
1108 * Can only be called for unsecured networks.
1109 */
1110 public void generateOpenNetworkConfig() {
Hai Shalomaa6f9202018-10-16 14:30:02 -07001111 if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) {
Jason Monkd52356a2015-01-28 10:40:41 -05001112 throw new IllegalStateException();
Hai Shalomaa6f9202018-10-16 14:30:02 -07001113 }
Jason Monkd52356a2015-01-28 10:40:41 -05001114 if (mConfig != null)
1115 return;
1116 mConfig = new WifiConfiguration();
1117 mConfig.SSID = AccessPoint.convertToQuotedString(ssid);
Hai Shalomaa6f9202018-10-16 14:30:02 -07001118
1119 if (security == SECURITY_NONE) {
1120 mConfig.allowedKeyManagement.set(KeyMgmt.NONE);
1121 } else {
1122 mConfig.allowedKeyManagement.set(KeyMgmt.OWE);
1123 mConfig.requirePMF = true;
1124 }
Jason Monkd52356a2015-01-28 10:40:41 -05001125 }
1126
Jason Monkd52356a2015-01-28 10:40:41 -05001127 public void saveWifiState(Bundle savedState) {
Vinit Deshpandefcd46122015-06-11 18:22:23 -07001128 if (ssid != null) savedState.putString(KEY_SSID, getSsidStr());
1129 savedState.putInt(KEY_SECURITY, security);
Sundeep Ghumana28050a2017-07-12 22:09:25 -07001130 savedState.putInt(KEY_SPEED, mSpeed);
Vinit Deshpandefcd46122015-06-11 18:22:23 -07001131 savedState.putInt(KEY_PSKTYPE, pskType);
1132 if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig);
Jason Monkd52356a2015-01-28 10:40:41 -05001133 savedState.putParcelable(KEY_WIFIINFO, mInfo);
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001134 savedState.putParcelableArray(KEY_SCANRESULTS,
Quang Luong8123f772019-02-08 16:30:12 -08001135 mScanResults.toArray(new Parcelable[mScanResults.size()
1136 + mExtraScanResults.size()]));
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -07001137 savedState.putParcelableArrayList(KEY_SCOREDNETWORKCACHE,
1138 new ArrayList<>(mScoredNetworkCache.values()));
Jason Monkd52356a2015-01-28 10:40:41 -05001139 if (mNetworkInfo != null) {
1140 savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo);
1141 }
Peter Qiuced37db2017-03-14 15:51:22 -07001142 if (mFqdn != null) {
1143 savedState.putString(KEY_FQDN, mFqdn);
1144 }
1145 if (mProviderFriendlyName != null) {
1146 savedState.putString(KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName);
1147 }
Peter Qiu280581b2017-07-24 14:18:56 -07001148 savedState.putBoolean(KEY_IS_CARRIER_AP, mIsCarrierAp);
1149 savedState.putInt(KEY_CARRIER_AP_EAP_TYPE, mCarrierApEapType);
1150 savedState.putString(KEY_CARRIER_NAME, mCarrierName);
Jason Monkd52356a2015-01-28 10:40:41 -05001151 }
1152
1153 public void setListener(AccessPointListener listener) {
1154 mAccessPointListener = listener;
1155 }
1156
Sundeep Ghumand1e44922017-08-07 11:21:38 -07001157 /**
Quang Luong8123f772019-02-08 16:30:12 -08001158 * Sets {@link #mScanResults} to the given collection and updates info based on the best RSSI
1159 * scan result.
Sundeep Ghumand1e44922017-08-07 11:21:38 -07001160 *
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001161 * @param scanResults a collection of scan results to add to the internal set
Sundeep Ghumand1e44922017-08-07 11:21:38 -07001162 */
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001163 void setScanResults(Collection<ScanResult> scanResults) {
Quang Luong8123f772019-02-08 16:30:12 -08001164 if (CollectionUtils.isEmpty(scanResults)) {
1165 Log.d(TAG, "Cannot set scan results to empty list");
1166 return;
1167 }
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -08001168
Quang Luong7054e922018-12-07 16:44:18 -08001169 // Validate scan results are for current AP only by matching SSID/BSSID
Quang Luong9051dfd2018-12-12 12:49:24 -08001170 // Passpoint networks are not bound to a specific SSID/BSSID, so skip this for passpoint.
Quang Luong8123f772019-02-08 16:30:12 -08001171 if (mKey != null && !isPasspoint() && !isOsuProvider()) {
Quang Luong7054e922018-12-07 16:44:18 -08001172 for (ScanResult result : scanResults) {
1173 String scanResultKey = AccessPoint.getKey(result);
Quang Luong8123f772019-02-08 16:30:12 -08001174 if (mKey != null && !mKey.equals(scanResultKey)) {
1175 Log.d(TAG, String.format(
Quang Luong7054e922018-12-07 16:44:18 -08001176 "ScanResult %s\nkey of %s did not match current AP key %s",
Quang Luong8123f772019-02-08 16:30:12 -08001177 result, scanResultKey, mKey));
1178 return;
Quang Luong7054e922018-12-07 16:44:18 -08001179 }
Jason Monkd52356a2015-01-28 10:40:41 -05001180 }
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001181 }
1182
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001183 int oldLevel = getLevel();
1184 mScanResults.clear();
1185 mScanResults.addAll(scanResults);
Quang Luong8123f772019-02-08 16:30:12 -08001186 updateBestRssiInfo();
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001187 int newLevel = getLevel();
1188
1189 // If newLevel is 0, there will be no displayed Preference since the AP is unreachable
1190 if (newLevel > 0 && newLevel != oldLevel) {
1191 // Only update labels on visible rssi changes
1192 updateSpeed();
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001193 ThreadUtils.postOnMainThread(() -> {
1194 if (mAccessPointListener != null) {
1195 mAccessPointListener.onLevelChanged(this);
1196 }
1197 });
1198
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001199 }
1200
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001201 ThreadUtils.postOnMainThread(() -> {
1202 if (mAccessPointListener != null) {
1203 mAccessPointListener.onAccessPointChanged(this);
1204 }
1205 });
Quang Luong8123f772019-02-08 16:30:12 -08001206 }
Sundeep Ghuman04f7f342018-01-23 19:18:31 -08001207
Quang Luong8123f772019-02-08 16:30:12 -08001208 /**
1209 * Sets the internal scan result cache to the list of home scans.
1210 * If there are no home scans, then the roaming scan list is used, and the AccessPoint is
1211 * marked as roaming.
1212 */
1213 void setScanResultsPasspoint(
1214 @Nullable Collection<ScanResult> homeScans,
1215 @Nullable Collection<ScanResult> roamingScans) {
1216 mExtraScanResults.clear();
1217 if (!CollectionUtils.isEmpty(homeScans)) {
1218 if (!CollectionUtils.isEmpty(roamingScans)) {
1219 mExtraScanResults.addAll(roamingScans);
Jason Monkd52356a2015-01-28 10:40:41 -05001220 }
Quang Luong8123f772019-02-08 16:30:12 -08001221 mIsRoaming = false;
1222 setScanResults(homeScans);
1223 } else if (!CollectionUtils.isEmpty(roamingScans)) {
1224 mIsRoaming = true;
1225 setScanResults(roamingScans);
Jason Monkd52356a2015-01-28 10:40:41 -05001226 }
Jason Monkd52356a2015-01-28 10:40:41 -05001227 }
1228
Quang Luong9051dfd2018-12-12 12:49:24 -08001229 /**
1230 * Attempt to update the AccessPoint with the current connection info.
1231 * This is used to set an AccessPoint to the active one if the connection info matches, or
1232 * conversely to set an AccessPoint to inactive if the connection info does not match. The RSSI
1233 * is also updated upon a match. Listeners will be notified if an update occurred.
1234 *
1235 * This is called in {@link WifiTracker#updateAccessPoints} as well as in callbacks for handling
1236 * NETWORK_STATE_CHANGED_ACTION, RSSI_CHANGED_ACTION, and onCapabilitiesChanged in WifiTracker.
1237 *
1238 * Returns true if an update occurred.
1239 */
Sundeep Ghumand911da32017-07-05 20:06:05 -07001240 public boolean update(
1241 @Nullable WifiConfiguration config, WifiInfo info, NetworkInfo networkInfo) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001242 boolean updated = false;
1243 final int oldLevel = getLevel();
Mitchell Wills5a42db22015-08-03 09:46:08 -07001244 if (info != null && isInfoForThisAccessPoint(config, info)) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001245 updated = (mInfo == null);
Quang Luong1f1207b2019-02-19 17:35:50 -08001246 if (!isPasspoint() && mConfig != config) {
Sundeep Ghumanf4d33022017-06-05 19:47:36 -07001247 // We do not set updated = true as we do not want to increase the amount of sorting
1248 // and copying performed in WifiTracker at this time. If issues involving refresh
1249 // are still seen, we will investigate further.
1250 update(config); // Notifies the AccessPointListener of the change
1251 }
Glen Kuhned38708c2017-07-19 14:53:34 -07001252 if (mRssi != info.getRssi() && info.getRssi() != WifiInfo.INVALID_RSSI) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001253 mRssi = info.getRssi();
1254 updated = true;
Sundeep Ghuman5c5cd7a2017-05-03 12:45:44 -07001255 } else if (mNetworkInfo != null && networkInfo != null
1256 && mNetworkInfo.getDetailedState() != networkInfo.getDetailedState()) {
Sundeep Ghuman96a53572017-04-20 21:25:41 -07001257 updated = true;
Jason Monkd52356a2015-01-28 10:40:41 -05001258 }
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001259 mInfo = info;
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001260 mNetworkInfo = networkInfo;
Jason Monkd52356a2015-01-28 10:40:41 -05001261 } else if (mInfo != null) {
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001262 updated = true;
Jason Monkd52356a2015-01-28 10:40:41 -05001263 mInfo = null;
1264 mNetworkInfo = null;
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001265 }
1266 if (updated && mAccessPointListener != null) {
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001267 ThreadUtils.postOnMainThread(() -> {
1268 if (mAccessPointListener != null) {
1269 mAccessPointListener.onAccessPointChanged(this);
1270 }
1271 });
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001272
1273 if (oldLevel != getLevel() /* current level */) {
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001274 ThreadUtils.postOnMainThread(() -> {
1275 if (mAccessPointListener != null) {
1276 mAccessPointListener.onLevelChanged(this);
1277 }
1278 });
Jason Monkd52356a2015-01-28 10:40:41 -05001279 }
1280 }
Sundeep Ghumandb9b94c2017-09-06 11:46:21 -07001281
Sundeep Ghuman8c792882017-04-04 17:23:29 -07001282 return updated;
Jason Monkd52356a2015-01-28 10:40:41 -05001283 }
1284
Sundeep Ghumand911da32017-07-05 20:06:05 -07001285 void update(@Nullable WifiConfiguration config) {
Vinit Deshpandefc406002015-04-15 18:10:55 -07001286 mConfig = config;
Quang Luong7054e922018-12-07 16:44:18 -08001287 if (mConfig != null) {
1288 ssid = removeDoubleQuotes(mConfig.SSID);
1289 }
Sundeep Ghumand911da32017-07-05 20:06:05 -07001290 networkId = config != null ? config.networkId : WifiConfiguration.INVALID_NETWORK_ID;
Sundeep Ghuman5d14fd72018-02-15 14:49:15 -08001291 ThreadUtils.postOnMainThread(() -> {
1292 if (mAccessPointListener != null) {
1293 mAccessPointListener.onAccessPointChanged(this);
1294 }
1295 });
Vinit Deshpandefc406002015-04-15 18:10:55 -07001296 }
Shirish Kalelec7a38ef2015-06-25 13:55:33 -07001297
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -08001298 @VisibleForTesting
Sanket Padawe0775a982015-08-19 14:57:46 -07001299 void setRssi(int rssi) {
1300 mRssi = rssi;
1301 }
1302
Sundeep Ghuman54bdcfa2017-03-08 19:52:05 -08001303 /** Sets the rssi to {@link #UNREACHABLE_RSSI}. */
1304 void setUnreachable() {
1305 setRssi(AccessPoint.UNREACHABLE_RSSI);
1306 }
1307
Sundeep Ghuman55adc6b2017-06-05 16:46:59 -07001308 int getSpeed() { return mSpeed;}
Sundeep Ghuman271e5de2017-05-30 14:11:39 -07001309
1310 @Nullable
1311 String getSpeedLabel() {
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -07001312 return getSpeedLabel(mSpeed);
1313 }
1314
1315 @Nullable
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -07001316 @Speed
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001317 private static int roundToClosestSpeedEnum(int speed) {
Sundeep Ghumanbe3a74c2017-08-01 17:50:59 -07001318 if (speed < Speed.SLOW) {
1319 return Speed.NONE;
1320 } else if (speed < (Speed.SLOW + Speed.MODERATE) / 2) {
1321 return Speed.SLOW;
1322 } else if (speed < (Speed.MODERATE + Speed.FAST) / 2) {
1323 return Speed.MODERATE;
1324 } else if (speed < (Speed.FAST + Speed.VERY_FAST) / 2) {
1325 return Speed.FAST;
1326 } else {
1327 return Speed.VERY_FAST;
1328 }
1329 }
1330
1331 @Nullable
jackqdyulei9ee9b392017-12-22 11:31:49 -08001332 String getSpeedLabel(@Speed int speed) {
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001333 return getSpeedLabel(mContext, speed);
1334 }
1335
1336 private static String getSpeedLabel(Context context, int speed) {
Eric Schwarzenbach0b8700f2017-07-25 14:32:21 -07001337 switch (speed) {
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001338 case Speed.VERY_FAST:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001339 return context.getString(R.string.speed_label_very_fast);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001340 case Speed.FAST:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001341 return context.getString(R.string.speed_label_fast);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001342 case Speed.MODERATE:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001343 return context.getString(R.string.speed_label_okay);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001344 case Speed.SLOW:
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001345 return context.getString(R.string.speed_label_slow);
Sundeep Ghuman53200ed2017-06-21 16:54:36 -07001346 case Speed.NONE:
Sundeep Ghuman271e5de2017-05-30 14:11:39 -07001347 default:
1348 return null;
1349 }
Sundeep Ghuman5519b7b2016-12-14 17:53:31 -08001350 }
1351
Amin Shaikhe74dbdd2018-03-09 16:05:30 -05001352 /** Return the speed label for a {@link ScoredNetwork} at the specified {@code rssi} level. */
1353 @Nullable
1354 public static String getSpeedLabel(Context context, ScoredNetwork scoredNetwork, int rssi) {
1355 return getSpeedLabel(context, roundToClosestSpeedEnum(scoredNetwork.calculateBadge(rssi)));
1356 }
1357
Dave Schaefer98537432017-02-08 11:26:08 -08001358 /** Return true if the current RSSI is reachable, and false otherwise. */
Sundeep Ghumanaaa8a1b2017-03-13 14:40:56 -07001359 public boolean isReachable() {
Dave Schaefer98537432017-02-08 11:26:08 -08001360 return mRssi != UNREACHABLE_RSSI;
1361 }
1362
Roshan Piuse9b8e502019-01-23 15:42:44 -08001363 private static CharSequence getAppLabel(String packageName, PackageManager packageManager) {
1364 CharSequence appLabel = "";
1365 ApplicationInfo appInfo = null;
1366 try {
1367 int userId = UserHandle.getUserId(UserHandle.USER_CURRENT);
1368 appInfo = packageManager.getApplicationInfoAsUser(packageName, 0 /* flags */, userId);
1369 } catch (PackageManager.NameNotFoundException e) {
1370 Log.e(TAG, "Failed to get app info", e);
1371 return appLabel;
1372 }
1373 if (appInfo != null) {
1374 appLabel = appInfo.loadLabel(packageManager);
1375 }
1376 return appLabel;
1377 }
1378
Jason Monkd52356a2015-01-28 10:40:41 -05001379 public static String getSummary(Context context, String ssid, DetailedState state,
Quang Luong98d06bd2019-01-30 13:42:26 -08001380 boolean isEphemeral, String suggestionOrSpecifierPackageName) {
Quang Luong0a3edf02018-12-17 14:50:58 -08001381 if (state == DetailedState.CONNECTED) {
Quang Luong98d06bd2019-01-30 13:42:26 -08001382 if (isEphemeral && !TextUtils.isEmpty(suggestionOrSpecifierPackageName)) {
Roshan Piuse9b8e502019-01-23 15:42:44 -08001383 CharSequence appLabel =
1384 getAppLabel(suggestionOrSpecifierPackageName, context.getPackageManager());
1385 return context.getString(R.string.connected_via_app, appLabel);
Vinit Deshpandedcf00c92015-04-15 18:32:09 -07001386 } else if (isEphemeral) {
Vinit Deshpandefc406002015-04-15 18:10:55 -07001387 // Special case for connected + ephemeral networks.
Stephen Chen36dd5cf12017-03-20 13:27:51 -07001388 final NetworkScoreManager networkScoreManager = context.getSystemService(
1389 NetworkScoreManager.class);
1390 NetworkScorerAppData scorer = networkScoreManager.getActiveScorer();
1391 if (scorer != null && scorer.getRecommendationServiceLabel() != null) {
1392 String format = context.getString(R.string.connected_via_network_scorer);
1393 return String.format(format, scorer.getRecommendationServiceLabel());
1394 } else {
1395 return context.getString(R.string.connected_via_network_scorer_default);
1396 }
Vinit Deshpandefc406002015-04-15 18:10:55 -07001397 }
Jason Monkd52356a2015-01-28 10:40:41 -05001398 }
1399
Sanket Padawe7094d222015-05-01 16:55:00 -07001400 // Case when there is wifi connected without internet connectivity.
1401 final ConnectivityManager cm = (ConnectivityManager)
1402 context.getSystemService(Context.CONNECTIVITY_SERVICE);
1403 if (state == DetailedState.CONNECTED) {
1404 IWifiManager wifiManager = IWifiManager.Stub.asInterface(
1405 ServiceManager.getService(Context.WIFI_SERVICE));
Lorenzo Colitti1317e042016-12-13 13:30:07 +09001406 NetworkCapabilities nc = null;
Sanket Padawe7094d222015-05-01 16:55:00 -07001407
1408 try {
Lorenzo Colitti1317e042016-12-13 13:30:07 +09001409 nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork());
1410 } catch (RemoteException e) {}
1411
1412 if (nc != null) {
1413 if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) {
Maurice Lam7f6fc862017-07-12 16:43:49 -07001414 int id = context.getResources()
1415 .getIdentifier("network_available_sign_in", "string", "android");
1416 return context.getString(id);
Lorenzo Colitti1317e042016-12-13 13:30:07 +09001417 } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
1418 return context.getString(R.string.wifi_connected_no_internet);
1419 }
Sanket Padawe7094d222015-05-01 16:55:00 -07001420 }
1421 }
Fan Zhang6acb7662016-10-17 12:40:03 -07001422 if (state == null) {
1423 Log.w(TAG, "state is null, returning empty summary");
1424 return "";
1425 }
Jason Monkd52356a2015-01-28 10:40:41 -05001426 String[] formats = context.getResources().getStringArray((ssid == null)
1427 ? R.array.wifi_status : R.array.wifi_status_with_ssid);
1428 int index = state.ordinal();
1429
1430 if (index >= formats.length || formats[index].length() == 0) {
Sanket Padawe3e9e5fa2015-05-28 10:41:14 -07001431 return "";
Jason Monkd52356a2015-01-28 10:40:41 -05001432 }
1433 return String.format(formats[index], ssid);
1434 }
1435
Jason Monkd52356a2015-01-28 10:40:41 -05001436 public static String convertToQuotedString(String string) {
1437 return "\"" + string + "\"";
1438 }
1439
1440 private static int getPskType(ScanResult result) {
1441 boolean wpa = result.capabilities.contains("WPA-PSK");
1442 boolean wpa2 = result.capabilities.contains("WPA2-PSK");
1443 if (wpa2 && wpa) {
1444 return PSK_WPA_WPA2;
1445 } else if (wpa2) {
1446 return PSK_WPA2;
1447 } else if (wpa) {
1448 return PSK_WPA;
1449 } else {
1450 Log.w(TAG, "Received abnormal flag string: " + result.capabilities);
1451 return PSK_UNKNOWN;
1452 }
1453 }
1454
1455 private static int getSecurity(ScanResult result) {
1456 if (result.capabilities.contains("WEP")) {
1457 return SECURITY_WEP;
Hai Shalomaa6f9202018-10-16 14:30:02 -07001458 } else if (result.capabilities.contains("SAE")) {
1459 return SECURITY_SAE;
Jason Monkd52356a2015-01-28 10:40:41 -05001460 } else if (result.capabilities.contains("PSK")) {
1461 return SECURITY_PSK;
Hai Shalomaa6f9202018-10-16 14:30:02 -07001462 } else if (result.capabilities.contains("EAP_SUITE_B_192")) {
1463 return SECURITY_EAP_SUITE_B;
Jason Monkd52356a2015-01-28 10:40:41 -05001464 } else if (result.capabilities.contains("EAP")) {
1465 return SECURITY_EAP;
Hai Shalomaa6f9202018-10-16 14:30:02 -07001466 } else if (result.capabilities.contains("OWE")) {
1467 return SECURITY_OWE;
Jason Monkd52356a2015-01-28 10:40:41 -05001468 }
Hai Shalomaa6f9202018-10-16 14:30:02 -07001469
Jason Monkd52356a2015-01-28 10:40:41 -05001470 return SECURITY_NONE;
1471 }
1472
1473 static int getSecurity(WifiConfiguration config) {
Hai Shalomaa6f9202018-10-16 14:30:02 -07001474 if (config.allowedKeyManagement.get(KeyMgmt.SAE)) {
1475 return SECURITY_SAE;
1476 }
Jason Monkd52356a2015-01-28 10:40:41 -05001477 if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
1478 return SECURITY_PSK;
1479 }
Hai Shalomaa6f9202018-10-16 14:30:02 -07001480 if (config.allowedKeyManagement.get(KeyMgmt.SUITE_B_192)) {
1481 return SECURITY_EAP_SUITE_B;
1482 }
Jason Monkd52356a2015-01-28 10:40:41 -05001483 if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) ||
1484 config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
1485 return SECURITY_EAP;
1486 }
Hai Shalomaa6f9202018-10-16 14:30:02 -07001487 if (config.allowedKeyManagement.get(KeyMgmt.OWE)) {
1488 return SECURITY_OWE;
1489 }
Jason Monkd52356a2015-01-28 10:40:41 -05001490 return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
1491 }
1492
1493 public static String securityToString(int security, int pskType) {
1494 if (security == SECURITY_WEP) {
1495 return "WEP";
1496 } else if (security == SECURITY_PSK) {
1497 if (pskType == PSK_WPA) {
1498 return "WPA";
1499 } else if (pskType == PSK_WPA2) {
1500 return "WPA2";
1501 } else if (pskType == PSK_WPA_WPA2) {
1502 return "WPA_WPA2";
1503 }
1504 return "PSK";
1505 } else if (security == SECURITY_EAP) {
1506 return "EAP";
Hai Shalomaa6f9202018-10-16 14:30:02 -07001507 } else if (security == SECURITY_SAE) {
1508 return "SAE";
1509 } else if (security == SECURITY_EAP_SUITE_B) {
1510 return "SUITE_B";
1511 } else if (security == SECURITY_OWE) {
1512 return "OWE";
Jason Monkd52356a2015-01-28 10:40:41 -05001513 }
1514 return "NONE";
1515 }
1516
1517 static String removeDoubleQuotes(String string) {
Jason Monk2b51cc32015-05-13 11:07:53 -04001518 if (TextUtils.isEmpty(string)) {
1519 return "";
1520 }
Jason Monkd52356a2015-01-28 10:40:41 -05001521 int length = string.length();
1522 if ((length > 1) && (string.charAt(0) == '"')
1523 && (string.charAt(length - 1) == '"')) {
1524 return string.substring(1, length - 1);
1525 }
1526 return string;
1527 }
1528
Sundeep Ghumanbb399912018-01-29 18:31:15 -08001529 /**
1530 * Callbacks relaying changes to the AccessPoint representation.
1531 *
1532 * <p>All methods are invoked on the Main Thread.
1533 */
Jason Monkd52356a2015-01-28 10:40:41 -05001534 public interface AccessPointListener {
Sundeep Ghumanbb399912018-01-29 18:31:15 -08001535 /**
1536 * Indicates a change to the externally visible state of the AccessPoint trigger by an
1537 * update of ScanResults, saved configuration state, connection state, or score
1538 * (labels/metered) state.
1539 *
1540 * <p>Clients should refresh their view of the AccessPoint to match the updated state when
1541 * this is invoked. Overall this method is extraneous if clients are listening to
1542 * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks.
1543 *
1544 * <p>Examples of changes include signal strength, connection state, speed label, and
1545 * generally anything that would impact the summary string.
1546 *
1547 * @param accessPoint The accessPoint object the listener was registered on which has
1548 * changed
1549 */
1550 @MainThread void onAccessPointChanged(AccessPoint accessPoint);
1551
1552 /**
1553 * Indicates the "wifi pie signal level" has changed, retrieved via calls to
1554 * {@link AccessPoint#getLevel()}.
1555 *
1556 * <p>This call is a subset of {@link #onAccessPointChanged(AccessPoint)} , hence is also
1557 * extraneous if the client is already reacting to that or the
1558 * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks.
1559 *
1560 * @param accessPoint The accessPoint object the listener was registered on whose level has
1561 * changed
1562 */
1563 @MainThread void onLevelChanged(AccessPoint accessPoint);
Jason Monkd52356a2015-01-28 10:40:41 -05001564 }
Sundeep Ghumanc0cf8482018-01-26 18:23:34 -08001565
1566 private static boolean isVerboseLoggingEnabled() {
1567 return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE);
1568 }
Quang Luong4d194842018-12-18 16:59:49 -08001569
1570 /**
1571 * Callbacks relaying changes to the OSU provisioning status started in startOsuProvisioning().
1572 *
1573 * All methods are invoked on the Main Thread
1574 */
Quang Luong8123f772019-02-08 16:30:12 -08001575 @VisibleForTesting
1576 class AccessPointProvisioningCallback extends ProvisioningCallback {
Quang Luong4d194842018-12-18 16:59:49 -08001577 @Override
1578 @MainThread public void onProvisioningFailure(int status) {
Quang Luongf005c882019-01-29 16:32:03 -08001579 if (TextUtils.equals(mOsuStatus, mContext.getString(R.string.osu_completing_sign_up))) {
1580 mOsuFailure = mContext.getString(R.string.osu_sign_up_failed);
1581 } else {
1582 mOsuFailure = mContext.getString(R.string.osu_connect_failed);
Quang Luong8d40a8e2019-01-02 16:25:38 -08001583 }
1584 mOsuStatus = null;
1585 mOsuProvisioningComplete = false;
1586 ThreadUtils.postOnMainThread(() -> {
1587 if (mAccessPointListener != null) {
1588 mAccessPointListener.onAccessPointChanged(AccessPoint.this);
1589 }
1590 });
Quang Luong4d194842018-12-18 16:59:49 -08001591 }
1592
1593 @Override
1594 @MainThread public void onProvisioningStatus(int status) {
Quang Luongf005c882019-01-29 16:32:03 -08001595 String newStatus = null;
Quang Luong8d40a8e2019-01-02 16:25:38 -08001596 switch (status) {
1597 case OSU_STATUS_AP_CONNECTING:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001598 case OSU_STATUS_AP_CONNECTED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001599 case OSU_STATUS_SERVER_CONNECTING:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001600 case OSU_STATUS_SERVER_VALIDATED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001601 case OSU_STATUS_SERVER_CONNECTED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001602 case OSU_STATUS_INIT_SOAP_EXCHANGE:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001603 case OSU_STATUS_WAITING_FOR_REDIRECT_RESPONSE:
Quang Luongf005c882019-01-29 16:32:03 -08001604 newStatus = String.format(mContext.getString(R.string.osu_opening_provider),
1605 mOsuProvider.getFriendlyName());
Quang Luong8d40a8e2019-01-02 16:25:38 -08001606 break;
1607 case OSU_STATUS_REDIRECT_RESPONSE_RECEIVED:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001608 case OSU_STATUS_SECOND_SOAP_EXCHANGE:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001609 case OSU_STATUS_THIRD_SOAP_EXCHANGE:
Quang Luong8d40a8e2019-01-02 16:25:38 -08001610 case OSU_STATUS_RETRIEVING_TRUST_ROOT_CERTS:
Quang Luongf005c882019-01-29 16:32:03 -08001611 newStatus = mContext.getString(
1612 R.string.osu_completing_sign_up);
Quang Luong8d40a8e2019-01-02 16:25:38 -08001613 break;
1614 }
Quang Luongf005c882019-01-29 16:32:03 -08001615 boolean updated = !TextUtils.equals(mOsuStatus, newStatus);
1616 mOsuStatus = newStatus;
Quang Luong8d40a8e2019-01-02 16:25:38 -08001617 mOsuFailure = null;
1618 mOsuProvisioningComplete = false;
Quang Luongf005c882019-01-29 16:32:03 -08001619 if (updated) {
1620 ThreadUtils.postOnMainThread(() -> {
1621 if (mAccessPointListener != null) {
1622 mAccessPointListener.onAccessPointChanged(AccessPoint.this);
1623 }
1624 });
1625 }
Quang Luong8d40a8e2019-01-02 16:25:38 -08001626 }
1627
1628 @Override
1629 @MainThread public void onProvisioningComplete() {
1630 mOsuProvisioningComplete = true;
1631 mOsuFailure = null;
1632 mOsuStatus = null;
1633 ThreadUtils.postOnMainThread(() -> {
1634 if (mAccessPointListener != null) {
1635 mAccessPointListener.onAccessPointChanged(AccessPoint.this);
1636 }
1637 });
Quang Luong4d194842018-12-18 16:59:49 -08001638 }
1639 }
Jason Monkd52356a2015-01-28 10:40:41 -05001640}