Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settingslib.wifi; |
| 18 | |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 20 | import android.annotation.MainThread; |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 23 | import android.content.Context; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 24 | import android.content.pm.ApplicationInfo; |
| 25 | import android.content.pm.IPackageManager; |
| 26 | import android.content.pm.PackageManager; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 27 | import android.net.ConnectivityManager; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 28 | import android.net.NetworkCapabilities; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 29 | import android.net.NetworkInfo; |
| 30 | import android.net.NetworkInfo.DetailedState; |
| 31 | import android.net.NetworkInfo.State; |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 32 | import android.net.NetworkKey; |
Stephen Chen | 36dd5cf1 | 2017-03-20 13:27:51 -0700 | [diff] [blame] | 33 | import android.net.NetworkScoreManager; |
| 34 | import android.net.NetworkScorerAppData; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 35 | import android.net.ScoredNetwork; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 36 | import android.net.wifi.IWifiManager; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 37 | import android.net.wifi.ScanResult; |
| 38 | import android.net.wifi.WifiConfiguration; |
| 39 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 40 | import android.net.wifi.WifiEnterpriseConfig; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 41 | import android.net.wifi.WifiInfo; |
| 42 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 43 | import android.net.wifi.WifiNetworkScoreCache; |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 44 | import android.net.wifi.hotspot2.OsuProvider; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 45 | import android.net.wifi.hotspot2.PasspointConfiguration; |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 46 | import android.net.wifi.hotspot2.ProvisioningCallback; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 47 | import android.os.Bundle; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 48 | import android.os.Parcelable; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 49 | import android.os.RemoteException; |
| 50 | import android.os.ServiceManager; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 51 | import android.os.SystemClock; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 52 | import android.os.UserHandle; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 53 | import android.text.TextUtils; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 54 | import android.util.ArraySet; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 55 | import android.util.Log; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 56 | |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 57 | import androidx.annotation.NonNull; |
| 58 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 59 | import com.android.internal.annotations.VisibleForTesting; |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 60 | import com.android.internal.util.CollectionUtils; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 61 | import com.android.settingslib.R; |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 62 | import com.android.settingslib.utils.ThreadUtils; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 63 | |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 64 | import java.lang.annotation.Retention; |
| 65 | import java.lang.annotation.RetentionPolicy; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 66 | import java.util.ArrayList; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 67 | import java.util.Collection; |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 68 | import java.util.HashMap; |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 69 | import java.util.Iterator; |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 70 | import java.util.Map; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 71 | import java.util.Set; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 72 | import java.util.concurrent.atomic.AtomicInteger; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 73 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 74 | /** |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 82 | public 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 Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 105 | /** The key which identifies this AccessPoint grouping. */ |
| 106 | private String mKey; |
| 107 | |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 108 | @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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 132 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 133 | /** The underlying set of scan results comprising this AccessPoint. */ |
| 134 | private final ArraySet<ScanResult> mScanResults = new ArraySet<>(); |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 135 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 136 | /** |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 137 | * 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 Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 143 | * 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 Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 148 | private final Map<String, TimestampedScoredNetwork> mScoredNetworkCache = new HashMap<>(); |
| 149 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 150 | static final String KEY_NETWORKINFO = "key_networkinfo"; |
| 151 | static final String KEY_WIFIINFO = "key_wifiinfo"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 152 | static final String KEY_SSID = "key_ssid"; |
| 153 | static final String KEY_SECURITY = "key_security"; |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 154 | static final String KEY_SPEED = "key_speed"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 155 | static final String KEY_PSKTYPE = "key_psktype"; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 156 | static final String KEY_SCANRESULTS = "key_scanresults"; |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 157 | static final String KEY_SCOREDNETWORKCACHE = "key_scorednetworkcache"; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 158 | static final String KEY_CONFIG = "key_config"; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 159 | static final String KEY_FQDN = "key_fqdn"; |
| 160 | static final String KEY_PROVIDER_FRIENDLY_NAME = "key_provider_friendly_name"; |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 161 | 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 Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 164 | static final AtomicInteger sLastId = new AtomicInteger(0); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 165 | |
Maurice Lam | 7cdbe19 | 2018-11-13 13:52:50 -0800 | [diff] [blame] | 166 | /* |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 170 | */ |
| 171 | public static final int SECURITY_NONE = 0; |
Hai Shalom | 9effa64 | 2018-11-12 11:43:52 -0800 | [diff] [blame] | 172 | 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 Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 177 | public static final int SECURITY_EAP_SUITE_B = 6; |
Hai Shalom | 828b13c | 2018-11-13 16:43:14 -0800 | [diff] [blame] | 178 | public static final int SECURITY_MAX_VAL = 7; // Has to be the last |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 179 | |
| 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 Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 185 | /** |
| 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 Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 191 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 192 | public static final int UNREACHABLE_RSSI = Integer.MIN_VALUE; |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 193 | |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 194 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 198 | private final Context mContext; |
| 199 | |
| 200 | private String ssid; |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 201 | private String bssid; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 202 | private int security; |
| 203 | private int networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 204 | |
| 205 | private int pskType = PSK_UNKNOWN; |
| 206 | |
| 207 | private WifiConfiguration mConfig; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 208 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 209 | private int mRssi = UNREACHABLE_RSSI; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 210 | |
| 211 | private WifiInfo mInfo; |
| 212 | private NetworkInfo mNetworkInfo; |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 213 | AccessPointListener mAccessPointListener; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 214 | |
| 215 | private Object mTag; |
| 216 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 217 | @Speed private int mSpeed = Speed.NONE; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 218 | private boolean mIsScoredNetworkMetered = false; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 219 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 220 | /** |
| 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 Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 226 | private boolean mIsRoaming = false; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 227 | |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 228 | private boolean mIsCarrierAp = false; |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 229 | |
| 230 | private OsuProvider mOsuProvider; |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 231 | |
| 232 | private String mOsuStatus; |
| 233 | private String mOsuFailure; |
| 234 | private boolean mOsuProvisioningComplete = false; |
| 235 | |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 236 | /** |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 242 | public AccessPoint(Context context, Bundle savedState) { |
| 243 | mContext = context; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 244 | |
| 245 | if (savedState.containsKey(KEY_CONFIG)) { |
| 246 | mConfig = savedState.getParcelable(KEY_CONFIG); |
| 247 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 248 | if (mConfig != null) { |
| 249 | loadConfig(mConfig); |
| 250 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 251 | 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 Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 257 | if (savedState.containsKey(KEY_SPEED)) { |
| 258 | mSpeed = savedState.getInt(KEY_SPEED); |
| 259 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 260 | if (savedState.containsKey(KEY_PSKTYPE)) { |
| 261 | pskType = savedState.getInt(KEY_PSKTYPE); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 262 | } |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 263 | mInfo = savedState.getParcelable(KEY_WIFIINFO); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 264 | if (savedState.containsKey(KEY_NETWORKINFO)) { |
| 265 | mNetworkInfo = savedState.getParcelable(KEY_NETWORKINFO); |
| 266 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 267 | 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 Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 274 | if (savedState.containsKey(KEY_SCOREDNETWORKCACHE)) { |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 275 | ArrayList<TimestampedScoredNetwork> scoredNetworkArrayList = |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 276 | savedState.getParcelableArrayList(KEY_SCOREDNETWORKCACHE); |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 277 | for (TimestampedScoredNetwork timedScore : scoredNetworkArrayList) { |
| 278 | mScoredNetworkCache.put(timedScore.getScore().networkKey.wifiKey.bssid, timedScore); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 281 | 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 Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 287 | 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 Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 296 | update(mConfig, mInfo, mNetworkInfo); |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 297 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 298 | // Calculate required fields |
| 299 | updateKey(); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 300 | updateBestRssiInfo(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 301 | } |
| 302 | |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 303 | /** |
| 304 | * Creates an AccessPoint with only a WifiConfiguration. This is used for the saved networks |
| 305 | * page. |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 306 | */ |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 307 | public AccessPoint(Context context, WifiConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 308 | mContext = context; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 309 | loadConfig(config); |
Quang Luong | 6e711c8 | 2019-03-07 14:52:55 -0800 | [diff] [blame] | 310 | updateKey(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 313 | /** |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 318 | mContext = context; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 319 | mFqdn = config.getHomeSp().getFqdn(); |
| 320 | mProviderFriendlyName = config.getHomeSp().getFriendlyName(); |
Quang Luong | 6e711c8 | 2019-03-07 14:52:55 -0800 | [diff] [blame] | 321 | updateKey(); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 325 | * Initialize an AccessPoint object for a Passpoint network. |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 326 | */ |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 327 | 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 Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 342 | mContext = context; |
| 343 | mOsuProvider = provider; |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 344 | setScanResults(results); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 345 | updateKey(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 348 | AccessPoint(Context context, Collection<ScanResult> results) { |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 349 | mContext = context; |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 350 | setScanResults(results); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 351 | updateKey(); |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 354 | @VisibleForTesting void loadConfig(WifiConfiguration config) { |
| 355 | ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); |
| 356 | bssid = config.BSSID; |
| 357 | security = getSecurity(config); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 358 | 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 Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 365 | if (isPasspoint()) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 366 | mKey = getKey(mConfig); |
Quang Luong | 6e711c8 | 2019-03-07 14:52:55 -0800 | [diff] [blame] | 367 | } else if (isPasspointConfig()) { |
| 368 | mKey = getKey(mFqdn); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 369 | } else if (isOsuProvider()) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 370 | mKey = getKey(mOsuProvider); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 371 | } else { // Non-Passpoint AP |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 372 | mKey = getKey(getSsidStr(), getBssid(), getSecurity()); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 373 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 376 | /** |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 377 | * 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 Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 384 | * 4. Network speed value |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 385 | * 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 391 | @Override |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 392 | public int compareTo(@NonNull AccessPoint other) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 393 | // 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 Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 398 | if (isReachable() && !other.isReachable()) return -1; |
| 399 | if (!isReachable() && other.isReachable()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 400 | |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 401 | // Configured (saved) one goes before unconfigured one. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 402 | if (isSaved() && !other.isSaved()) return -1; |
| 403 | if (!isSaved() && other.isSaved()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 404 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 405 | // Faster speeds go before slower speeds - but only if visible change in speed label |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 406 | if (getSpeed() != other.getSpeed()) { |
| 407 | return other.getSpeed() - getSpeed(); |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 410 | // Sort by signal strength, bucketed by level |
| 411 | int difference = WifiManager.calculateSignalLevel(other.mRssi, SIGNAL_LEVELS) |
| 412 | - WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 413 | if (difference != 0) { |
| 414 | return difference; |
| 415 | } |
Sundeep Ghuman | d44b8e4 | 2017-06-21 21:40:06 -0700 | [diff] [blame] | 416 | |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 417 | // Sort by title. |
| 418 | difference = getTitle().compareToIgnoreCase(other.getTitle()); |
Sundeep Ghuman | d44b8e4 | 2017-06-21 21:40:06 -0700 | [diff] [blame] | 419 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 425 | } |
| 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 Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 447 | if (bssid != null) { |
| 448 | builder.append(":").append(bssid); |
| 449 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 450 | 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 Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 462 | if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 463 | builder.append(',').append(securityToString(security, pskType)); |
| 464 | } |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 465 | builder.append(",level=").append(getLevel()); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 466 | if (mSpeed != Speed.NONE) { |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 467 | builder.append(",speed=").append(mSpeed); |
Sundeep Ghuman | 8920e9c | 2017-04-27 16:16:41 -0700 | [diff] [blame] | 468 | } |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 469 | builder.append(",metered=").append(isMetered()); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 470 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 471 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 472 | builder.append(",rssi=").append(mRssi); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 473 | builder.append(",scan cache size=").append(mScanResults.size() |
| 474 | + mExtraScanResults.size()); |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 477 | return builder.append(')').toString(); |
| 478 | } |
| 479 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 480 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 481 | * Updates the AccessPoint rankingScore, metering, and speed, returning true if the data has |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 482 | * changed. |
| 483 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 484 | * @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 Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 488 | */ |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 489 | boolean update( |
| 490 | WifiNetworkScoreCache scoreCache, |
| 491 | boolean scoringUiEnabled, |
| 492 | long maxScoreCacheAgeMillis) { |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 493 | boolean scoreChanged = false; |
| 494 | if (scoringUiEnabled) { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 495 | scoreChanged = updateScores(scoreCache, maxScoreCacheAgeMillis); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 496 | } |
| 497 | return updateMetered(scoreCache) || scoreChanged; |
| 498 | } |
| 499 | |
| 500 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 501 | * Updates the AccessPoint rankingScore and speed, returning true if the data has changed. |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 502 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 503 | * <p>Any cached {@link TimestampedScoredNetwork} objects older than the given max age in millis |
| 504 | * will be removed when this method is invoked. |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 505 | * |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 506 | * <p>Precondition: {@link #mRssi} is up to date before invoking this method. |
| 507 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 508 | * @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 Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 512 | * @return true if the set speed has changed |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 513 | */ |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 514 | private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) { |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 515 | long nowMillis = SystemClock.elapsedRealtime(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 516 | for (ScanResult result : mScanResults) { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 517 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 518 | if (score == null) { |
| 519 | continue; |
| 520 | } |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 521 | 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 Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 531 | // Remove old cached networks |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 532 | long evictionCutoff = nowMillis - maxScoreCacheAgeMillis; |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 533 | Iterator<TimestampedScoredNetwork> iterator = mScoredNetworkCache.values().iterator(); |
| 534 | iterator.forEachRemaining(timestampedScoredNetwork -> { |
| 535 | if (timestampedScoredNetwork.getUpdatedTimestampMillis() < evictionCutoff) { |
| 536 | iterator.remove(); |
| 537 | } |
| 538 | }); |
| 539 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 540 | 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 Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 550 | boolean changed = oldSpeed != mSpeed; |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 551 | if(isVerboseLoggingEnabled() && changed) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 552 | Log.i(TAG, String.format("%s: Set speed to %d", ssid, mSpeed)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 553 | } |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 554 | return changed; |
| 555 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 556 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 557 | /** 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 Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 563 | 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 Ghuman | a782569 | 2017-08-23 15:53:00 -0700 | [diff] [blame] | 568 | // 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 Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 572 | int count = 0; |
| 573 | int totalSpeed = 0; |
| 574 | for (TimestampedScoredNetwork timedScore : mScoredNetworkCache.values()) { |
| 575 | int speed = timedScore.getScore().calculateBadge(mRssi); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 576 | if (speed != Speed.NONE) { |
| 577 | count++; |
| 578 | totalSpeed += speed; |
| 579 | } |
| 580 | } |
| 581 | int speed = count == 0 ? Speed.NONE : totalSpeed / count; |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 582 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 583 | Log.i(TAG, String.format("%s generated fallback speed is: %d", getSsidStr(), speed)); |
| 584 | } |
| 585 | return roundToClosestSpeedEnum(speed); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 588 | /** |
| 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 Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 595 | |
| 596 | if (isActive() && mInfo != null) { |
Eric Schwarzenbach | 733ea26 | 2017-08-08 10:28:21 -0700 | [diff] [blame] | 597 | NetworkKey key = NetworkKey.createFromWifiInfo(mInfo); |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 598 | ScoredNetwork score = scoreCache.getScoredNetwork(key); |
| 599 | if (score != null) { |
| 600 | mIsScoredNetworkMetered |= score.meteredHint; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 601 | } |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 602 | } else { |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 603 | for (ScanResult result : mScanResults) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 604 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 605 | if (score == null) { |
| 606 | continue; |
| 607 | } |
| 608 | mIsScoredNetworkMetered |= score.meteredHint; |
| 609 | } |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 610 | } |
| 611 | return oldMetering == mIsScoredNetworkMetered; |
| 612 | } |
| 613 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 614 | public static String getKey(ScanResult result) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 615 | return getKey(result.SSID, result.BSSID, getSecurity(result)); |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 618 | /** |
| 619 | * Returns the AccessPoint key for a WifiConfiguration. |
| 620 | * This will return a special Passpoint key if the config is for Passpoint. |
| 621 | */ |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 622 | public static String getKey(WifiConfiguration config) { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 623 | if (config.isPasspoint()) { |
Quang Luong | 6e711c8 | 2019-03-07 14:52:55 -0800 | [diff] [blame] | 624 | return getKey(config.FQDN); |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 625 | } else { |
Quang Luong | c2c947c | 2019-01-18 12:51:35 -0800 | [diff] [blame] | 626 | return getKey(removeDoubleQuotes(config.SSID), config.BSSID, getSecurity(config)); |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 627 | } |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 628 | } |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 629 | |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 630 | /** |
Quang Luong | 6e711c8 | 2019-03-07 14:52:55 -0800 | [diff] [blame] | 631 | * 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 Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 640 | * 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 Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 645 | .append(provider.getFriendlyName()) |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 646 | .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 Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 662 | return builder.toString(); |
| 663 | } |
| 664 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 665 | public String getKey() { |
| 666 | return mKey; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | public boolean matches(WifiConfiguration config) { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 670 | if (config.isPasspoint()) { |
| 671 | return (isPasspoint() && config.FQDN.equals(mConfig.FQDN)); |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 672 | } else { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 673 | // Normal non-Passpoint network |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 674 | return ssid.equals(removeDoubleQuotes(config.SSID)) |
| 675 | && security == getSecurity(config) |
| 676 | && (mConfig == null || mConfig.shared == config.shared); |
| 677 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | public WifiConfiguration getConfig() { |
| 681 | return mConfig; |
| 682 | } |
| 683 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 684 | public String getPasspointFqdn() { |
| 685 | return mFqdn; |
| 686 | } |
| 687 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 688 | public void clearConfig() { |
| 689 | mConfig = null; |
| 690 | networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 691 | } |
| 692 | |
| 693 | public WifiInfo getInfo() { |
| 694 | return mInfo; |
| 695 | } |
| 696 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 697 | /** |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 703 | public int getLevel() { |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 704 | return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 705 | } |
| 706 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 707 | public int getRssi() { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 708 | return mRssi; |
| 709 | } |
| 710 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 711 | /** |
| 712 | * Returns the underlying scan result set. |
| 713 | * |
| 714 | * <p>Callers should not modify this set. |
| 715 | */ |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 716 | public Set<ScanResult> getScanResults() { |
| 717 | Set<ScanResult> allScans = new ArraySet<>(); |
| 718 | allScans.addAll(mScanResults); |
| 719 | allScans.addAll(mExtraScanResults); |
| 720 | return allScans; |
| 721 | } |
jackqdyulei | 9ee9b39 | 2017-12-22 11:31:49 -0800 | [diff] [blame] | 722 | |
| 723 | public Map<String, TimestampedScoredNetwork> getScoredNetworkCache() { |
| 724 | return mScoredNetworkCache; |
| 725 | } |
| 726 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 727 | /** |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 728 | * Updates {@link #mRssi} and sets scan result information to that of the best RSSI scan result. |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 729 | * |
| 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 Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 732 | * 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 Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 735 | */ |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 736 | private void updateBestRssiInfo() { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 737 | if (this.isActive()) { |
| 738 | return; |
| 739 | } |
| 740 | |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 741 | ScanResult bestResult = null; |
| 742 | int bestRssi = UNREACHABLE_RSSI; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 743 | for (ScanResult result : mScanResults) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 744 | if (result.level > bestRssi) { |
| 745 | bestRssi = result.level; |
| 746 | bestResult = result; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 750 | // 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 Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 753 | } else { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 754 | 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 Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 771 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 774 | /** |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 775 | * Returns if the network should be considered metered. |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 776 | */ |
| 777 | public boolean isMetered() { |
| 778 | return mIsScoredNetworkMetered |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 779 | || WifiConfiguration.isMetered(mConfig, mInfo); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 782 | 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 Hatano | 2bde6e7 | 2017-05-30 18:38:56 +0900 | [diff] [blame] | 792 | if (isPasspoint() || isPasspointConfig()) { |
Sanket Padawe | 194cce6 | 2015-07-10 10:53:31 -0700 | [diff] [blame] | 793 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 794 | context.getString(R.string.wifi_security_eap); |
Sanket Padawe | d187880 | 2015-05-12 10:27:19 -0700 | [diff] [blame] | 795 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 796 | 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 Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 800 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 803 | 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 Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 822 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 828 | case SECURITY_NONE: |
| 829 | default: |
| 830 | return concise ? "" : context.getString(R.string.wifi_security_none); |
| 831 | } |
| 832 | } |
| 833 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 834 | public String getSsidStr() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 835 | return ssid; |
| 836 | } |
| 837 | |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 838 | public String getBssid() { |
| 839 | return bssid; |
| 840 | } |
| 841 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 842 | public CharSequence getSsid() { |
pastychang | a2998fc | 2018-06-26 13:37:34 +0800 | [diff] [blame] | 843 | return ssid; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 844 | } |
| 845 | |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 846 | /** |
| 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 Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 851 | public String getConfigName() { |
| 852 | if (mConfig != null && mConfig.isPasspoint()) { |
| 853 | return mConfig.providerFriendlyName; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 854 | } else if (mFqdn != null) { |
| 855 | return mProviderFriendlyName; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 856 | } else { |
| 857 | return ssid; |
| 858 | } |
| 859 | } |
| 860 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 861 | public DetailedState getDetailedState() { |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 862 | if (mNetworkInfo != null) { |
| 863 | return mNetworkInfo.getDetailedState(); |
| 864 | } |
| 865 | Log.w(TAG, "NetworkInfo is null, cannot return detailed state"); |
| 866 | return null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 867 | } |
| 868 | |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 869 | 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 Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 881 | public String getSavedNetworkSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 882 | WifiConfiguration config = mConfig; |
| 883 | if (config != null) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 884 | PackageManager pm = mContext.getPackageManager(); |
| 885 | String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 886 | int userId = UserHandle.getUserId(config.creatorUid); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 887 | ApplicationInfo appInfo = null; |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 888 | if (config.creatorName != null && config.creatorName.equals(systemName)) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 889 | appInfo = mContext.getApplicationInfo(); |
| 890 | } else { |
| 891 | try { |
| 892 | IPackageManager ipm = AppGlobals.getPackageManager(); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 893 | appInfo = ipm.getApplicationInfo(config.creatorName, 0 /* flags */, userId); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 894 | } 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 Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 903 | } |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 904 | return ""; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 907 | /** |
| 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 Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 913 | } else if (isPasspointConfig()) { |
| 914 | return mProviderFriendlyName; |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 915 | } else if (isOsuProvider()) { |
| 916 | return mOsuProvider.getFriendlyName(); |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 917 | } else { |
| 918 | return getSsidStr(); |
| 919 | } |
| 920 | } |
| 921 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 922 | public String getSummary() { |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 923 | return getSettingsSummary(); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | public String getSettingsSummary() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 927 | // Update to new summary |
| 928 | StringBuilder summary = new StringBuilder(); |
| 929 | |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 930 | if (isOsuProvider()) { |
| 931 | if (mOsuProvisioningComplete) { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 932 | summary.append(mContext.getString(R.string.osu_sign_up_complete)); |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 933 | } else if (mOsuFailure != null) { |
| 934 | summary.append(mOsuFailure); |
| 935 | } else if (mOsuStatus != null) { |
| 936 | summary.append(mOsuStatus); |
| 937 | } else { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 938 | summary.append(mContext.getString(R.string.tap_to_sign_up)); |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 939 | } |
| 940 | } else if (isActive()) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 941 | if (mConfig != null && getDetailedState() == DetailedState.CONNECTED |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 942 | && 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 Luong | 98d06bd | 2019-01-30 13:42:26 -0800 | [diff] [blame] | 947 | summary.append(getSummary(mContext, /* ssid */ null, getDetailedState(), |
Roshan Pius | e9b8e50 | 2019-01-23 15:42:44 -0800 | [diff] [blame] | 948 | mInfo != null && mInfo.isEphemeral(), |
| 949 | mInfo != null ? mInfo.getNetworkSuggestionOrSpecifierPackageName() : null)); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 950 | } |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 951 | } 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 Kuhne | 446afac | 2017-06-22 16:02:33 -0700 | [diff] [blame] | 963 | break; |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 964 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD: |
| 965 | summary.append(mContext.getString(R.string.wifi_check_password_try_again)); |
Glen Kuhne | 446afac | 2017-06-22 16:02:33 -0700 | [diff] [blame] | 966 | break; |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 967 | 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 Kuhne | 446afac | 2017-06-22 16:02:33 -0700 | [diff] [blame] | 996 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 1000 | |
| 1001 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 1002 | if (isVerboseLoggingEnabled()) { |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 1003 | summary.append(WifiUtils.buildLoggingSummary(this, mConfig)); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1004 | } |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1005 | |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 1006 | if (mConfig != null && (WifiUtils.isMeteredOverridden(mConfig) || mConfig.meteredHint)) { |
Salvador Martinez | 6f6fe75 | 2018-03-21 16:33:53 -0700 | [diff] [blame] | 1007 | return mContext.getResources().getString( |
| 1008 | R.string.preference_summary_default_combination, |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 1009 | WifiUtils.getMeteredLabel(mContext, mConfig), |
Salvador Martinez | 6f6fe75 | 2018-03-21 16:33:53 -0700 | [diff] [blame] | 1010 | summary.toString()); |
| 1011 | } |
| 1012 | |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 1013 | // 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 Ghuman | d3171ca | 2017-07-12 23:02:24 -0700 | [diff] [blame] | 1016 | return mContext.getResources().getString( |
| 1017 | R.string.preference_summary_default_combination, |
| 1018 | getSpeedLabel(), |
| 1019 | summary.toString()); |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 1020 | } else if (getSpeedLabel() != null) { |
| 1021 | return getSpeedLabel(); |
Sundeep Ghuman | d3171ca | 2017-07-12 23:02:24 -0700 | [diff] [blame] | 1022 | } else { |
| 1023 | return summary.toString(); |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1024 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /** |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1028 | * 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 Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 1043 | return mInfo != null && mInfo.isEphemeral() && |
| 1044 | mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1047 | /** |
| 1048 | * Return true if this AccessPoint represents a Passpoint AP. |
| 1049 | */ |
Vinit Deshpande | 5b7352c | 2015-07-09 16:53:12 -0700 | [diff] [blame] | 1050 | public boolean isPasspoint() { |
| 1051 | return mConfig != null && mConfig.isPasspoint(); |
| 1052 | } |
| 1053 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1054 | /** |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1055 | * Return true if this AccessPoint represents a Passpoint provider configuration. |
| 1056 | */ |
| 1057 | public boolean isPasspointConfig() { |
| 1058 | return mFqdn != null; |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 1062 | * Return true if this AccessPoint represents an OSU Provider. |
| 1063 | */ |
| 1064 | public boolean isOsuProvider() { |
| 1065 | return mOsuProvider != null; |
| 1066 | } |
| 1067 | |
| 1068 | /** |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1069 | * Starts the OSU Provisioning flow. |
| 1070 | */ |
| 1071 | public void startOsuProvisioning() { |
| 1072 | mContext.getSystemService(WifiManager.class).startSubscriptionProvisioning( |
| 1073 | mOsuProvider, |
Ecco Park | e67533e | 2019-03-01 15:09:35 -0800 | [diff] [blame] | 1074 | mContext.getMainExecutor(), |
| 1075 | new AccessPointProvisioningCallback() |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1076 | ); |
| 1077 | } |
| 1078 | |
| 1079 | /** |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1080 | * 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 Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1085 | if (info.isOsuAp() || mOsuStatus != null) { |
| 1086 | return (info.isOsuAp() && mOsuStatus != null); |
| 1087 | } else if (info.isPasspointAp() || isPasspoint()) { |
| 1088 | return (info.isPasspointAp() && isPasspoint() |
Quang Luong | 2a6cdc5 | 2019-03-05 17:35:45 -0800 | [diff] [blame] | 1089 | && TextUtils.equals(info.getPasspointFqdn(), mConfig.FQDN)); |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1092 | if (networkId != WifiConfiguration.INVALID_NETWORK_ID) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1093 | return networkId == info.getNetworkId(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1094 | } else if (config != null) { |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1095 | return TextUtils.equals(getKey(config), getKey()); |
| 1096 | } else { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1097 | // 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 Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1100 | return TextUtils.equals(removeDoubleQuotes(info.getSSID()), ssid); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | public boolean isSaved() { |
Quang Luong | 48ad03a | 2019-02-14 11:51:01 -0800 | [diff] [blame] | 1105 | return mConfig != null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1106 | } |
| 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 Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1121 | if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1122 | throw new IllegalStateException(); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1123 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1124 | if (mConfig != null) |
| 1125 | return; |
| 1126 | mConfig = new WifiConfiguration(); |
| 1127 | mConfig.SSID = AccessPoint.convertToQuotedString(ssid); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1128 | |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1135 | } |
| 1136 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1137 | public void saveWifiState(Bundle savedState) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1138 | if (ssid != null) savedState.putString(KEY_SSID, getSsidStr()); |
| 1139 | savedState.putInt(KEY_SECURITY, security); |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 1140 | savedState.putInt(KEY_SPEED, mSpeed); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1141 | savedState.putInt(KEY_PSKTYPE, pskType); |
| 1142 | if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1143 | savedState.putParcelable(KEY_WIFIINFO, mInfo); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1144 | savedState.putParcelableArray(KEY_SCANRESULTS, |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1145 | mScanResults.toArray(new Parcelable[mScanResults.size() |
| 1146 | + mExtraScanResults.size()])); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1147 | savedState.putParcelableArrayList(KEY_SCOREDNETWORKCACHE, |
| 1148 | new ArrayList<>(mScoredNetworkCache.values())); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1149 | if (mNetworkInfo != null) { |
| 1150 | savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo); |
| 1151 | } |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1152 | if (mFqdn != null) { |
| 1153 | savedState.putString(KEY_FQDN, mFqdn); |
| 1154 | } |
| 1155 | if (mProviderFriendlyName != null) { |
| 1156 | savedState.putString(KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName); |
| 1157 | } |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 1158 | savedState.putBoolean(KEY_IS_CARRIER_AP, mIsCarrierAp); |
| 1159 | savedState.putInt(KEY_CARRIER_AP_EAP_TYPE, mCarrierApEapType); |
| 1160 | savedState.putString(KEY_CARRIER_NAME, mCarrierName); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | public void setListener(AccessPointListener listener) { |
| 1164 | mAccessPointListener = listener; |
| 1165 | } |
| 1166 | |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1167 | /** |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1168 | * Sets {@link #mScanResults} to the given collection and updates info based on the best RSSI |
| 1169 | * scan result. |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1170 | * |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1171 | * @param scanResults a collection of scan results to add to the internal set |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1172 | */ |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1173 | void setScanResults(Collection<ScanResult> scanResults) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1174 | if (CollectionUtils.isEmpty(scanResults)) { |
| 1175 | Log.d(TAG, "Cannot set scan results to empty list"); |
| 1176 | return; |
| 1177 | } |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1178 | |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1179 | // Validate scan results are for current AP only by matching SSID/BSSID |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 1180 | // Passpoint networks are not bound to a specific SSID/BSSID, so skip this for passpoint. |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1181 | if (mKey != null && !isPasspoint() && !isOsuProvider()) { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1182 | for (ScanResult result : scanResults) { |
| 1183 | String scanResultKey = AccessPoint.getKey(result); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1184 | if (mKey != null && !mKey.equals(scanResultKey)) { |
| 1185 | Log.d(TAG, String.format( |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1186 | "ScanResult %s\nkey of %s did not match current AP key %s", |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1187 | result, scanResultKey, mKey)); |
| 1188 | return; |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1189 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1190 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1193 | int oldLevel = getLevel(); |
| 1194 | mScanResults.clear(); |
| 1195 | mScanResults.addAll(scanResults); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1196 | updateBestRssiInfo(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1197 | 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 Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1203 | ThreadUtils.postOnMainThread(() -> { |
| 1204 | if (mAccessPointListener != null) { |
| 1205 | mAccessPointListener.onLevelChanged(this); |
| 1206 | } |
| 1207 | }); |
| 1208 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1211 | ThreadUtils.postOnMainThread(() -> { |
| 1212 | if (mAccessPointListener != null) { |
| 1213 | mAccessPointListener.onAccessPointChanged(this); |
| 1214 | } |
| 1215 | }); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1216 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1217 | |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1218 | /** |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1230 | } |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1231 | mIsRoaming = false; |
| 1232 | setScanResults(homeScans); |
| 1233 | } else if (!CollectionUtils.isEmpty(roamingScans)) { |
| 1234 | mIsRoaming = true; |
| 1235 | setScanResults(roamingScans); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1236 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1237 | } |
| 1238 | |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 1239 | /** |
| 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 Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1250 | public boolean update( |
| 1251 | @Nullable WifiConfiguration config, WifiInfo info, NetworkInfo networkInfo) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1252 | boolean updated = false; |
| 1253 | final int oldLevel = getLevel(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1254 | if (info != null && isInfoForThisAccessPoint(config, info)) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1255 | updated = (mInfo == null); |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1256 | if (!isPasspoint() && mConfig != config) { |
Sundeep Ghuman | f4d3302 | 2017-06-05 19:47:36 -0700 | [diff] [blame] | 1257 | // 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 Kuhne | d38708c | 2017-07-19 14:53:34 -0700 | [diff] [blame] | 1262 | if (mRssi != info.getRssi() && info.getRssi() != WifiInfo.INVALID_RSSI) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1263 | mRssi = info.getRssi(); |
| 1264 | updated = true; |
Sundeep Ghuman | 5c5cd7a | 2017-05-03 12:45:44 -0700 | [diff] [blame] | 1265 | } else if (mNetworkInfo != null && networkInfo != null |
| 1266 | && mNetworkInfo.getDetailedState() != networkInfo.getDetailedState()) { |
Sundeep Ghuman | 96a5357 | 2017-04-20 21:25:41 -0700 | [diff] [blame] | 1267 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1268 | } |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1269 | mInfo = info; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1270 | mNetworkInfo = networkInfo; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1271 | } else if (mInfo != null) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1272 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1273 | mInfo = null; |
| 1274 | mNetworkInfo = null; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1275 | } |
| 1276 | if (updated && mAccessPointListener != null) { |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1277 | ThreadUtils.postOnMainThread(() -> { |
| 1278 | if (mAccessPointListener != null) { |
| 1279 | mAccessPointListener.onAccessPointChanged(this); |
| 1280 | } |
| 1281 | }); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1282 | |
| 1283 | if (oldLevel != getLevel() /* current level */) { |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1284 | ThreadUtils.postOnMainThread(() -> { |
| 1285 | if (mAccessPointListener != null) { |
| 1286 | mAccessPointListener.onLevelChanged(this); |
| 1287 | } |
| 1288 | }); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1289 | } |
| 1290 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 1291 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1292 | return updated; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1293 | } |
| 1294 | |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1295 | void update(@Nullable WifiConfiguration config) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1296 | mConfig = config; |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1297 | if (mConfig != null) { |
| 1298 | ssid = removeDoubleQuotes(mConfig.SSID); |
| 1299 | } |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1300 | networkId = config != null ? config.networkId : WifiConfiguration.INVALID_NETWORK_ID; |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1301 | ThreadUtils.postOnMainThread(() -> { |
| 1302 | if (mAccessPointListener != null) { |
| 1303 | mAccessPointListener.onAccessPointChanged(this); |
| 1304 | } |
| 1305 | }); |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1306 | } |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 1307 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1308 | @VisibleForTesting |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 1309 | void setRssi(int rssi) { |
| 1310 | mRssi = rssi; |
| 1311 | } |
| 1312 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1313 | /** Sets the rssi to {@link #UNREACHABLE_RSSI}. */ |
| 1314 | void setUnreachable() { |
| 1315 | setRssi(AccessPoint.UNREACHABLE_RSSI); |
| 1316 | } |
| 1317 | |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1318 | int getSpeed() { return mSpeed;} |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1319 | |
| 1320 | @Nullable |
| 1321 | String getSpeedLabel() { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 1322 | return getSpeedLabel(mSpeed); |
| 1323 | } |
| 1324 | |
| 1325 | @Nullable |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1326 | @Speed |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1327 | private static int roundToClosestSpeedEnum(int speed) { |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1328 | 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 |
jackqdyulei | 9ee9b39 | 2017-12-22 11:31:49 -0800 | [diff] [blame] | 1342 | String getSpeedLabel(@Speed int speed) { |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1343 | return getSpeedLabel(mContext, speed); |
| 1344 | } |
| 1345 | |
| 1346 | private static String getSpeedLabel(Context context, int speed) { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 1347 | switch (speed) { |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1348 | case Speed.VERY_FAST: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1349 | return context.getString(R.string.speed_label_very_fast); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1350 | case Speed.FAST: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1351 | return context.getString(R.string.speed_label_fast); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1352 | case Speed.MODERATE: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1353 | return context.getString(R.string.speed_label_okay); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1354 | case Speed.SLOW: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1355 | return context.getString(R.string.speed_label_slow); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1356 | case Speed.NONE: |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1357 | default: |
| 1358 | return null; |
| 1359 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1362 | /** 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 Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1368 | /** Return true if the current RSSI is reachable, and false otherwise. */ |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 1369 | public boolean isReachable() { |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1370 | return mRssi != UNREACHABLE_RSSI; |
| 1371 | } |
| 1372 | |
Roshan Pius | e9b8e50 | 2019-01-23 15:42:44 -0800 | [diff] [blame] | 1373 | 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1389 | public static String getSummary(Context context, String ssid, DetailedState state, |
Quang Luong | 98d06bd | 2019-01-30 13:42:26 -0800 | [diff] [blame] | 1390 | boolean isEphemeral, String suggestionOrSpecifierPackageName) { |
Quang Luong | 0a3edf0 | 2018-12-17 14:50:58 -0800 | [diff] [blame] | 1391 | if (state == DetailedState.CONNECTED) { |
Quang Luong | 98d06bd | 2019-01-30 13:42:26 -0800 | [diff] [blame] | 1392 | if (isEphemeral && !TextUtils.isEmpty(suggestionOrSpecifierPackageName)) { |
Roshan Pius | e9b8e50 | 2019-01-23 15:42:44 -0800 | [diff] [blame] | 1393 | CharSequence appLabel = |
| 1394 | getAppLabel(suggestionOrSpecifierPackageName, context.getPackageManager()); |
| 1395 | return context.getString(R.string.connected_via_app, appLabel); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1396 | } else if (isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1397 | // Special case for connected + ephemeral networks. |
Stephen Chen | 36dd5cf1 | 2017-03-20 13:27:51 -0700 | [diff] [blame] | 1398 | 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 Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1407 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1408 | } |
| 1409 | |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1410 | // 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 Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1416 | NetworkCapabilities nc = null; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1417 | |
| 1418 | try { |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1419 | nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork()); |
| 1420 | } catch (RemoteException e) {} |
| 1421 | |
| 1422 | if (nc != null) { |
| 1423 | if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) { |
Maurice Lam | 7f6fc86 | 2017-07-12 16:43:49 -0700 | [diff] [blame] | 1424 | int id = context.getResources() |
| 1425 | .getIdentifier("network_available_sign_in", "string", "android"); |
| 1426 | return context.getString(id); |
lucaslin | fc731ab | 2019-03-12 17:52:15 +0800 | [diff] [blame] | 1427 | } else if (nc.hasCapability( |
| 1428 | NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY)) { |
| 1429 | return context.getString(R.string.wifi_limited_connection); |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1430 | } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) { |
| 1431 | return context.getString(R.string.wifi_connected_no_internet); |
| 1432 | } |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1433 | } |
| 1434 | } |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 1435 | if (state == null) { |
| 1436 | Log.w(TAG, "state is null, returning empty summary"); |
| 1437 | return ""; |
| 1438 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1439 | 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 Padawe | 3e9e5fa | 2015-05-28 10:41:14 -0700 | [diff] [blame] | 1444 | return ""; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1445 | } |
| 1446 | return String.format(formats[index], ssid); |
| 1447 | } |
| 1448 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1449 | 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 Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1471 | } else if (result.capabilities.contains("SAE")) { |
| 1472 | return SECURITY_SAE; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1473 | } else if (result.capabilities.contains("PSK")) { |
| 1474 | return SECURITY_PSK; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1475 | } else if (result.capabilities.contains("EAP_SUITE_B_192")) { |
| 1476 | return SECURITY_EAP_SUITE_B; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1477 | } else if (result.capabilities.contains("EAP")) { |
| 1478 | return SECURITY_EAP; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1479 | } else if (result.capabilities.contains("OWE")) { |
| 1480 | return SECURITY_OWE; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1481 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1482 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1483 | return SECURITY_NONE; |
| 1484 | } |
| 1485 | |
| 1486 | static int getSecurity(WifiConfiguration config) { |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1487 | if (config.allowedKeyManagement.get(KeyMgmt.SAE)) { |
| 1488 | return SECURITY_SAE; |
| 1489 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1490 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) { |
| 1491 | return SECURITY_PSK; |
| 1492 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1493 | if (config.allowedKeyManagement.get(KeyMgmt.SUITE_B_192)) { |
| 1494 | return SECURITY_EAP_SUITE_B; |
| 1495 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1496 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || |
| 1497 | config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) { |
| 1498 | return SECURITY_EAP; |
| 1499 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1500 | if (config.allowedKeyManagement.get(KeyMgmt.OWE)) { |
| 1501 | return SECURITY_OWE; |
| 1502 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1503 | 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 Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1520 | } 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1526 | } |
| 1527 | return "NONE"; |
| 1528 | } |
| 1529 | |
| 1530 | static String removeDoubleQuotes(String string) { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 1531 | if (TextUtils.isEmpty(string)) { |
| 1532 | return ""; |
| 1533 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1534 | 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 Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1542 | /** |
| 1543 | * Callbacks relaying changes to the AccessPoint representation. |
| 1544 | * |
| 1545 | * <p>All methods are invoked on the Main Thread. |
| 1546 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1547 | public interface AccessPointListener { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1548 | /** |
| 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 Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1577 | } |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 1578 | |
| 1579 | private static boolean isVerboseLoggingEnabled() { |
| 1580 | return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE); |
| 1581 | } |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1582 | |
| 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 Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1588 | @VisibleForTesting |
| 1589 | class AccessPointProvisioningCallback extends ProvisioningCallback { |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1590 | @Override |
| 1591 | @MainThread public void onProvisioningFailure(int status) { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1592 | 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 Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1596 | } |
| 1597 | mOsuStatus = null; |
| 1598 | mOsuProvisioningComplete = false; |
| 1599 | ThreadUtils.postOnMainThread(() -> { |
| 1600 | if (mAccessPointListener != null) { |
| 1601 | mAccessPointListener.onAccessPointChanged(AccessPoint.this); |
| 1602 | } |
| 1603 | }); |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | @Override |
| 1607 | @MainThread public void onProvisioningStatus(int status) { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1608 | String newStatus = null; |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1609 | switch (status) { |
| 1610 | case OSU_STATUS_AP_CONNECTING: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1611 | case OSU_STATUS_AP_CONNECTED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1612 | case OSU_STATUS_SERVER_CONNECTING: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1613 | case OSU_STATUS_SERVER_VALIDATED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1614 | case OSU_STATUS_SERVER_CONNECTED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1615 | case OSU_STATUS_INIT_SOAP_EXCHANGE: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1616 | case OSU_STATUS_WAITING_FOR_REDIRECT_RESPONSE: |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1617 | newStatus = String.format(mContext.getString(R.string.osu_opening_provider), |
| 1618 | mOsuProvider.getFriendlyName()); |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1619 | break; |
| 1620 | case OSU_STATUS_REDIRECT_RESPONSE_RECEIVED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1621 | case OSU_STATUS_SECOND_SOAP_EXCHANGE: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1622 | case OSU_STATUS_THIRD_SOAP_EXCHANGE: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1623 | case OSU_STATUS_RETRIEVING_TRUST_ROOT_CERTS: |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1624 | newStatus = mContext.getString( |
| 1625 | R.string.osu_completing_sign_up); |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1626 | break; |
| 1627 | } |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1628 | boolean updated = !TextUtils.equals(mOsuStatus, newStatus); |
| 1629 | mOsuStatus = newStatus; |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1630 | mOsuFailure = null; |
| 1631 | mOsuProvisioningComplete = false; |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1632 | if (updated) { |
| 1633 | ThreadUtils.postOnMainThread(() -> { |
| 1634 | if (mAccessPointListener != null) { |
| 1635 | mAccessPointListener.onAccessPointChanged(AccessPoint.this); |
| 1636 | } |
| 1637 | }); |
| 1638 | } |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1639 | } |
| 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 Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1651 | } |
| 1652 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1653 | } |