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); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 312 | /** |
| 313 | * Initialize an AccessPoint object for a {@link PasspointConfiguration}. This is mainly |
| 314 | * used by "Saved Networks" page for managing the saved {@link PasspointConfiguration}. |
| 315 | */ |
| 316 | public AccessPoint(Context context, PasspointConfiguration config) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 317 | mContext = context; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 318 | mFqdn = config.getHomeSp().getFqdn(); |
| 319 | mProviderFriendlyName = config.getHomeSp().getFriendlyName(); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 323 | * Initialize an AccessPoint object for a Passpoint network. |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 324 | */ |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 325 | public AccessPoint(@NonNull Context context, @NonNull WifiConfiguration config, |
| 326 | @Nullable Collection<ScanResult> homeScans, |
| 327 | @Nullable Collection<ScanResult> roamingScans) { |
| 328 | mContext = context; |
| 329 | networkId = config.networkId; |
| 330 | mConfig = config; |
| 331 | setScanResultsPasspoint(homeScans, roamingScans); |
| 332 | updateKey(); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Initialize an AccessPoint object for a Passpoint OSU Provider. |
| 337 | */ |
| 338 | public AccessPoint(@NonNull Context context, @NonNull OsuProvider provider, |
| 339 | @NonNull Collection<ScanResult> results) { |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 340 | mContext = context; |
| 341 | mOsuProvider = provider; |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 342 | setScanResults(results); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 343 | updateKey(); |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 346 | AccessPoint(Context context, Collection<ScanResult> results) { |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 347 | mContext = context; |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 348 | setScanResults(results); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 349 | updateKey(); |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 352 | @VisibleForTesting void loadConfig(WifiConfiguration config) { |
| 353 | ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); |
| 354 | bssid = config.BSSID; |
| 355 | security = getSecurity(config); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 356 | networkId = config.networkId; |
| 357 | mConfig = config; |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 358 | updateKey(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** Updates {@link #mKey} and should only called upon object creation/initialization. */ |
| 362 | private void updateKey() { |
| 363 | // TODO(sghuman): Consolidate Key logic on ScanResultMatchInfo |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 364 | if (isPasspoint()) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 365 | mKey = getKey(mConfig); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 366 | } else if (isOsuProvider()) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 367 | mKey = getKey(mOsuProvider); |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 368 | } else { // Non-Passpoint AP |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 369 | mKey = getKey(getSsidStr(), getBssid(), getSecurity()); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 370 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Ajay Nadathur | d7b689a | 2016-08-31 15:07:56 -0700 | [diff] [blame] | 373 | /** |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 374 | * Returns a negative integer, zero, or a positive integer if this AccessPoint is less than, |
| 375 | * equal to, or greater than the other AccessPoint. |
| 376 | * |
| 377 | * Sort order rules for AccessPoints: |
| 378 | * 1. Active before inactive |
| 379 | * 2. Reachable before unreachable |
| 380 | * 3. Saved before unsaved |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 381 | * 4. Network speed value |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 382 | * 5. Stronger signal before weaker signal |
| 383 | * 6. SSID alphabetically |
| 384 | * |
| 385 | * Note that AccessPoints with a signal are usually also Reachable, |
| 386 | * and will thus appear before unreachable saved AccessPoints. |
| 387 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 388 | @Override |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 389 | public int compareTo(@NonNull AccessPoint other) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 390 | // Active one goes first. |
| 391 | if (isActive() && !other.isActive()) return -1; |
| 392 | if (!isActive() && other.isActive()) return 1; |
| 393 | |
| 394 | // Reachable one goes before unreachable one. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 395 | if (isReachable() && !other.isReachable()) return -1; |
| 396 | if (!isReachable() && other.isReachable()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 397 | |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 398 | // Configured (saved) one goes before unconfigured one. |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 399 | if (isSaved() && !other.isSaved()) return -1; |
| 400 | if (!isSaved() && other.isSaved()) return 1; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 401 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 402 | // 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] | 403 | if (getSpeed() != other.getSpeed()) { |
| 404 | return other.getSpeed() - getSpeed(); |
Sundeep Ghuman | 05c41e2 | 2017-02-01 13:27:56 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 407 | // Sort by signal strength, bucketed by level |
| 408 | int difference = WifiManager.calculateSignalLevel(other.mRssi, SIGNAL_LEVELS) |
| 409 | - WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 410 | if (difference != 0) { |
| 411 | return difference; |
| 412 | } |
Sundeep Ghuman | d44b8e4 | 2017-06-21 21:40:06 -0700 | [diff] [blame] | 413 | |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 414 | // Sort by title. |
| 415 | difference = getTitle().compareToIgnoreCase(other.getTitle()); |
Sundeep Ghuman | d44b8e4 | 2017-06-21 21:40:06 -0700 | [diff] [blame] | 416 | if (difference != 0) { |
| 417 | return difference; |
| 418 | } |
| 419 | |
| 420 | // Do a case sensitive comparison to distinguish SSIDs that differ in case only |
| 421 | return getSsidStr().compareTo(other.getSsidStr()); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public boolean equals(Object other) { |
| 426 | if (!(other instanceof AccessPoint)) return false; |
| 427 | return (this.compareTo((AccessPoint) other) == 0); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public int hashCode() { |
| 432 | int result = 0; |
| 433 | if (mInfo != null) result += 13 * mInfo.hashCode(); |
| 434 | result += 19 * mRssi; |
| 435 | result += 23 * networkId; |
| 436 | result += 29 * ssid.hashCode(); |
| 437 | return result; |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public String toString() { |
| 442 | StringBuilder builder = new StringBuilder().append("AccessPoint(") |
| 443 | .append(ssid); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 444 | if (bssid != null) { |
| 445 | builder.append(":").append(bssid); |
| 446 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 447 | if (isSaved()) { |
| 448 | builder.append(',').append("saved"); |
| 449 | } |
| 450 | if (isActive()) { |
| 451 | builder.append(',').append("active"); |
| 452 | } |
| 453 | if (isEphemeral()) { |
| 454 | builder.append(',').append("ephemeral"); |
| 455 | } |
| 456 | if (isConnectable()) { |
| 457 | builder.append(',').append("connectable"); |
| 458 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 459 | if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 460 | builder.append(',').append(securityToString(security, pskType)); |
| 461 | } |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 462 | builder.append(",level=").append(getLevel()); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 463 | if (mSpeed != Speed.NONE) { |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 464 | builder.append(",speed=").append(mSpeed); |
Sundeep Ghuman | 8920e9c | 2017-04-27 16:16:41 -0700 | [diff] [blame] | 465 | } |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 466 | builder.append(",metered=").append(isMetered()); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 467 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 468 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 469 | builder.append(",rssi=").append(mRssi); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 470 | builder.append(",scan cache size=").append(mScanResults.size() |
| 471 | + mExtraScanResults.size()); |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 474 | return builder.append(')').toString(); |
| 475 | } |
| 476 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 477 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 478 | * 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] | 479 | * changed. |
| 480 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 481 | * @param scoreCache The score cache to use to retrieve scores |
| 482 | * @param scoringUiEnabled Whether to show scoring and badging UI |
| 483 | * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when |
| 484 | * generating speed labels |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 485 | */ |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 486 | boolean update( |
| 487 | WifiNetworkScoreCache scoreCache, |
| 488 | boolean scoringUiEnabled, |
| 489 | long maxScoreCacheAgeMillis) { |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 490 | boolean scoreChanged = false; |
| 491 | if (scoringUiEnabled) { |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 492 | scoreChanged = updateScores(scoreCache, maxScoreCacheAgeMillis); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 493 | } |
| 494 | return updateMetered(scoreCache) || scoreChanged; |
| 495 | } |
| 496 | |
| 497 | /** |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 498 | * 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] | 499 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 500 | * <p>Any cached {@link TimestampedScoredNetwork} objects older than the given max age in millis |
| 501 | * will be removed when this method is invoked. |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 502 | * |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 503 | * <p>Precondition: {@link #mRssi} is up to date before invoking this method. |
| 504 | * |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 505 | * @param scoreCache The score cache to use to retrieve scores |
| 506 | * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when |
| 507 | * generating speed labels |
| 508 | * |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 509 | * @return true if the set speed has changed |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 510 | */ |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 511 | private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) { |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 512 | long nowMillis = SystemClock.elapsedRealtime(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 513 | for (ScanResult result : mScanResults) { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 514 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 515 | if (score == null) { |
| 516 | continue; |
| 517 | } |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 518 | TimestampedScoredNetwork timedScore = mScoredNetworkCache.get(result.BSSID); |
| 519 | if (timedScore == null) { |
| 520 | mScoredNetworkCache.put( |
| 521 | result.BSSID, new TimestampedScoredNetwork(score, nowMillis)); |
| 522 | } else { |
| 523 | // Update data since the has been seen in the score cache |
| 524 | timedScore.update(score, nowMillis); |
| 525 | } |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 528 | // Remove old cached networks |
Sundeep Ghuman | 9bb85d3 | 2017-08-28 17:04:16 -0700 | [diff] [blame] | 529 | long evictionCutoff = nowMillis - maxScoreCacheAgeMillis; |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 530 | Iterator<TimestampedScoredNetwork> iterator = mScoredNetworkCache.values().iterator(); |
| 531 | iterator.forEachRemaining(timestampedScoredNetwork -> { |
| 532 | if (timestampedScoredNetwork.getUpdatedTimestampMillis() < evictionCutoff) { |
| 533 | iterator.remove(); |
| 534 | } |
| 535 | }); |
| 536 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 537 | return updateSpeed(); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Updates the internal speed, returning true if the update resulted in a speed label change. |
| 542 | */ |
| 543 | private boolean updateSpeed() { |
| 544 | int oldSpeed = mSpeed; |
| 545 | mSpeed = generateAverageSpeedForSsid(); |
| 546 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 547 | boolean changed = oldSpeed != mSpeed; |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 548 | if(isVerboseLoggingEnabled() && changed) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 549 | Log.i(TAG, String.format("%s: Set speed to %d", ssid, mSpeed)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 550 | } |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 551 | return changed; |
| 552 | } |
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 | /** Creates a speed value for the current {@link #mRssi} by averaging all non zero badges. */ |
| 555 | @Speed private int generateAverageSpeedForSsid() { |
| 556 | if (mScoredNetworkCache.isEmpty()) { |
| 557 | return Speed.NONE; |
| 558 | } |
| 559 | |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 560 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 561 | Log.d(TAG, String.format("Generating fallbackspeed for %s using cache: %s", |
| 562 | getSsidStr(), mScoredNetworkCache)); |
| 563 | } |
| 564 | |
Sundeep Ghuman | a782569 | 2017-08-23 15:53:00 -0700 | [diff] [blame] | 565 | // TODO(b/63073866): If flickering issues persist, consider mapping using getLevel rather |
| 566 | // than specific rssi value so score doesn't change without a visible wifi bar change. This |
| 567 | // issue is likely to be more evident for the active AP whose RSSI value is not half-lifed. |
| 568 | |
Sundeep Ghuman | fedf461 | 2017-08-15 15:47:18 -0700 | [diff] [blame] | 569 | int count = 0; |
| 570 | int totalSpeed = 0; |
| 571 | for (TimestampedScoredNetwork timedScore : mScoredNetworkCache.values()) { |
| 572 | int speed = timedScore.getScore().calculateBadge(mRssi); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 573 | if (speed != Speed.NONE) { |
| 574 | count++; |
| 575 | totalSpeed += speed; |
| 576 | } |
| 577 | } |
| 578 | int speed = count == 0 ? Speed.NONE : totalSpeed / count; |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 579 | if (isVerboseLoggingEnabled()) { |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 580 | Log.i(TAG, String.format("%s generated fallback speed is: %d", getSsidStr(), speed)); |
| 581 | } |
| 582 | return roundToClosestSpeedEnum(speed); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 585 | /** |
| 586 | * Updates the AccessPoint's metering based on {@link ScoredNetwork#meteredHint}, returning |
| 587 | * true if the metering changed. |
| 588 | */ |
| 589 | private boolean updateMetered(WifiNetworkScoreCache scoreCache) { |
| 590 | boolean oldMetering = mIsScoredNetworkMetered; |
| 591 | mIsScoredNetworkMetered = false; |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 592 | |
| 593 | if (isActive() && mInfo != null) { |
Eric Schwarzenbach | 733ea26 | 2017-08-08 10:28:21 -0700 | [diff] [blame] | 594 | NetworkKey key = NetworkKey.createFromWifiInfo(mInfo); |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 595 | ScoredNetwork score = scoreCache.getScoredNetwork(key); |
| 596 | if (score != null) { |
| 597 | mIsScoredNetworkMetered |= score.meteredHint; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 598 | } |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 599 | } else { |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 600 | for (ScanResult result : mScanResults) { |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 601 | ScoredNetwork score = scoreCache.getScoredNetwork(result); |
| 602 | if (score == null) { |
| 603 | continue; |
| 604 | } |
| 605 | mIsScoredNetworkMetered |= score.meteredHint; |
| 606 | } |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 607 | } |
| 608 | return oldMetering == mIsScoredNetworkMetered; |
| 609 | } |
| 610 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 611 | public static String getKey(ScanResult result) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 612 | return getKey(result.SSID, result.BSSID, getSecurity(result)); |
Mitchell Wills | 18af493 | 2016-08-10 13:49:21 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 615 | /** |
| 616 | * Returns the AccessPoint key for a WifiConfiguration. |
| 617 | * This will return a special Passpoint key if the config is for Passpoint. |
| 618 | */ |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 619 | public static String getKey(WifiConfiguration config) { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 620 | if (config.isPasspoint()) { |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 621 | return new StringBuilder() |
| 622 | .append(KEY_PREFIX_FQDN) |
| 623 | .append(config.FQDN).toString(); |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 624 | } else { |
Quang Luong | c2c947c | 2019-01-18 12:51:35 -0800 | [diff] [blame] | 625 | return getKey(removeDoubleQuotes(config.SSID), config.BSSID, getSecurity(config)); |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 626 | } |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 627 | } |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 628 | |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 629 | /** |
| 630 | * Returns the AccessPoint key corresponding to the OsuProvider. |
| 631 | */ |
| 632 | public static String getKey(OsuProvider provider) { |
| 633 | return new StringBuilder() |
| 634 | .append(KEY_PREFIX_OSU) |
Quang Luong | 1a8fece | 2019-01-11 14:58:14 -0800 | [diff] [blame] | 635 | .append(provider.getFriendlyName()) |
Quang Luong | ac1026e | 2019-01-02 17:37:01 -0800 | [diff] [blame] | 636 | .append(',') |
| 637 | .append(provider.getServerUri()).toString(); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Returns the AccessPoint key for a normal non-Passpoint network by ssid/bssid and security. |
| 642 | */ |
| 643 | private static String getKey(String ssid, String bssid, int security) { |
| 644 | StringBuilder builder = new StringBuilder(); |
| 645 | builder.append(KEY_PREFIX_AP); |
| 646 | if (TextUtils.isEmpty(ssid)) { |
| 647 | builder.append(bssid); |
| 648 | } else { |
| 649 | builder.append(ssid); |
| 650 | } |
| 651 | builder.append(',').append(security); |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 652 | return builder.toString(); |
| 653 | } |
| 654 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 655 | public String getKey() { |
| 656 | return mKey; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | public boolean matches(WifiConfiguration config) { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 660 | if (config.isPasspoint()) { |
| 661 | return (isPasspoint() && config.FQDN.equals(mConfig.FQDN)); |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 662 | } else { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 663 | // Normal non-Passpoint network |
Bartosz Fabianowski | 6fb0756 | 2016-01-12 15:43:19 +0100 | [diff] [blame] | 664 | return ssid.equals(removeDoubleQuotes(config.SSID)) |
| 665 | && security == getSecurity(config) |
| 666 | && (mConfig == null || mConfig.shared == config.shared); |
| 667 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | public WifiConfiguration getConfig() { |
| 671 | return mConfig; |
| 672 | } |
| 673 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 674 | public String getPasspointFqdn() { |
| 675 | return mFqdn; |
| 676 | } |
| 677 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 678 | public void clearConfig() { |
| 679 | mConfig = null; |
| 680 | networkId = WifiConfiguration.INVALID_NETWORK_ID; |
| 681 | } |
| 682 | |
| 683 | public WifiInfo getInfo() { |
| 684 | return mInfo; |
| 685 | } |
| 686 | |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 687 | /** |
| 688 | * Returns the number of levels to show for a Wifi icon, from 0 to {@link #SIGNAL_LEVELS}-1. |
| 689 | * |
| 690 | * <p>Use {@#isReachable()} to determine if an AccessPoint is in range, as this method will |
| 691 | * always return at least 0. |
| 692 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 693 | public int getLevel() { |
Tony Mantler | a0e03dd | 2016-01-27 15:57:03 -0800 | [diff] [blame] | 694 | return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 695 | } |
| 696 | |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 697 | public int getRssi() { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 698 | return mRssi; |
| 699 | } |
| 700 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 701 | /** |
| 702 | * Returns the underlying scan result set. |
| 703 | * |
| 704 | * <p>Callers should not modify this set. |
| 705 | */ |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 706 | public Set<ScanResult> getScanResults() { |
| 707 | Set<ScanResult> allScans = new ArraySet<>(); |
| 708 | allScans.addAll(mScanResults); |
| 709 | allScans.addAll(mExtraScanResults); |
| 710 | return allScans; |
| 711 | } |
jackqdyulei | 9ee9b39 | 2017-12-22 11:31:49 -0800 | [diff] [blame] | 712 | |
| 713 | public Map<String, TimestampedScoredNetwork> getScoredNetworkCache() { |
| 714 | return mScoredNetworkCache; |
| 715 | } |
| 716 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 717 | /** |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 718 | * 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] | 719 | * |
| 720 | * <p>If the given connection is active, the existing value of {@link #mRssi} will be returned. |
| 721 | * If the given AccessPoint is not active, a value will be calculated from previous scan |
Sundeep Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 722 | * results, returning the best RSSI for all matching AccessPoints averaged with the previous |
| 723 | * value. If the access point is not connected and there are no scan results, the rssi will be |
| 724 | * set to {@link #UNREACHABLE_RSSI}. |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 725 | */ |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 726 | private void updateBestRssiInfo() { |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 727 | if (this.isActive()) { |
| 728 | return; |
| 729 | } |
| 730 | |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 731 | ScanResult bestResult = null; |
| 732 | int bestRssi = UNREACHABLE_RSSI; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 733 | for (ScanResult result : mScanResults) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 734 | if (result.level > bestRssi) { |
| 735 | bestRssi = result.level; |
| 736 | bestResult = result; |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 740 | // Set the rssi to the average of the current rssi and the previous rssi. |
| 741 | if (bestRssi != UNREACHABLE_RSSI && mRssi != UNREACHABLE_RSSI) { |
| 742 | mRssi = (mRssi + bestRssi) / 2; |
Sundeep Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 743 | } else { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 744 | mRssi = bestRssi; |
| 745 | } |
| 746 | |
| 747 | if (bestResult != null) { |
| 748 | ssid = bestResult.SSID; |
| 749 | bssid = bestResult.BSSID; |
| 750 | security = getSecurity(bestResult); |
| 751 | if (security == SECURITY_PSK) { |
| 752 | pskType = getPskType(bestResult); |
| 753 | } |
| 754 | mIsCarrierAp = bestResult.isCarrierAp; |
| 755 | mCarrierApEapType = bestResult.carrierApEapType; |
| 756 | mCarrierName = bestResult.carrierName; |
| 757 | } |
| 758 | // Update the config SSID of a Passpoint network to that of the best RSSI |
| 759 | if (isPasspoint()) { |
| 760 | mConfig.SSID = convertToQuotedString(ssid); |
Sundeep Ghuman | ce78a5f | 2017-03-15 19:06:14 -0700 | [diff] [blame] | 761 | } |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 764 | /** |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 765 | * Returns if the network should be considered metered. |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 766 | */ |
| 767 | public boolean isMetered() { |
| 768 | return mIsScoredNetworkMetered |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 769 | || WifiConfiguration.isMetered(mConfig, mInfo); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 772 | public NetworkInfo getNetworkInfo() { |
| 773 | return mNetworkInfo; |
| 774 | } |
| 775 | |
| 776 | public int getSecurity() { |
| 777 | return security; |
| 778 | } |
| 779 | |
| 780 | public String getSecurityString(boolean concise) { |
| 781 | Context context = mContext; |
Tomoharu Hatano | 2bde6e7 | 2017-05-30 18:38:56 +0900 | [diff] [blame] | 782 | if (isPasspoint() || isPasspointConfig()) { |
Sanket Padawe | 194cce6 | 2015-07-10 10:53:31 -0700 | [diff] [blame] | 783 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 784 | context.getString(R.string.wifi_security_eap); |
Sanket Padawe | d187880 | 2015-05-12 10:27:19 -0700 | [diff] [blame] | 785 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 786 | switch(security) { |
| 787 | case SECURITY_EAP: |
| 788 | return concise ? context.getString(R.string.wifi_security_short_eap) : |
| 789 | context.getString(R.string.wifi_security_eap); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 790 | case SECURITY_EAP_SUITE_B: |
| 791 | return concise ? context.getString(R.string.wifi_security_short_eap_suiteb) : |
| 792 | context.getString(R.string.wifi_security_eap_suiteb); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 793 | case SECURITY_PSK: |
| 794 | switch (pskType) { |
| 795 | case PSK_WPA: |
| 796 | return concise ? context.getString(R.string.wifi_security_short_wpa) : |
| 797 | context.getString(R.string.wifi_security_wpa); |
| 798 | case PSK_WPA2: |
| 799 | return concise ? context.getString(R.string.wifi_security_short_wpa2) : |
| 800 | context.getString(R.string.wifi_security_wpa2); |
| 801 | case PSK_WPA_WPA2: |
| 802 | return concise ? context.getString(R.string.wifi_security_short_wpa_wpa2) : |
| 803 | context.getString(R.string.wifi_security_wpa_wpa2); |
| 804 | case PSK_UNKNOWN: |
| 805 | default: |
| 806 | return concise ? context.getString(R.string.wifi_security_short_psk_generic) |
| 807 | : context.getString(R.string.wifi_security_psk_generic); |
| 808 | } |
| 809 | case SECURITY_WEP: |
| 810 | return concise ? context.getString(R.string.wifi_security_short_wep) : |
| 811 | context.getString(R.string.wifi_security_wep); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 812 | case SECURITY_SAE: |
| 813 | return concise ? context.getString(R.string.wifi_security_short_sae) : |
| 814 | context.getString(R.string.wifi_security_sae); |
| 815 | case SECURITY_OWE: |
| 816 | return concise ? context.getString(R.string.wifi_security_short_owe) : |
| 817 | context.getString(R.string.wifi_security_owe); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 818 | case SECURITY_NONE: |
| 819 | default: |
| 820 | return concise ? "" : context.getString(R.string.wifi_security_none); |
| 821 | } |
| 822 | } |
| 823 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 824 | public String getSsidStr() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 825 | return ssid; |
| 826 | } |
| 827 | |
Jason Monk | 60a82ff | 2016-02-25 13:55:03 -0500 | [diff] [blame] | 828 | public String getBssid() { |
| 829 | return bssid; |
| 830 | } |
| 831 | |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 832 | public CharSequence getSsid() { |
pastychang | a2998fc | 2018-06-26 13:37:34 +0800 | [diff] [blame] | 833 | return ssid; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 834 | } |
| 835 | |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 836 | /** |
| 837 | * Returns the name associated with the stored config. |
| 838 | * @deprecated Please use {@link #getTitle()} instead to get the display name of an AccessPoint. |
| 839 | */ |
| 840 | @Deprecated |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 841 | public String getConfigName() { |
| 842 | if (mConfig != null && mConfig.isPasspoint()) { |
| 843 | return mConfig.providerFriendlyName; |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 844 | } else if (mFqdn != null) { |
| 845 | return mProviderFriendlyName; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 846 | } else { |
| 847 | return ssid; |
| 848 | } |
| 849 | } |
| 850 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 851 | public DetailedState getDetailedState() { |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 852 | if (mNetworkInfo != null) { |
| 853 | return mNetworkInfo.getDetailedState(); |
| 854 | } |
| 855 | Log.w(TAG, "NetworkInfo is null, cannot return detailed state"); |
| 856 | return null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 857 | } |
| 858 | |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 859 | public boolean isCarrierAp() { |
| 860 | return mIsCarrierAp; |
| 861 | } |
| 862 | |
| 863 | public int getCarrierApEapType() { |
| 864 | return mCarrierApEapType; |
| 865 | } |
| 866 | |
| 867 | public String getCarrierName() { |
| 868 | return mCarrierName; |
| 869 | } |
| 870 | |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 871 | public String getSavedNetworkSummary() { |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 872 | WifiConfiguration config = mConfig; |
| 873 | if (config != null) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 874 | PackageManager pm = mContext.getPackageManager(); |
| 875 | String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 876 | int userId = UserHandle.getUserId(config.creatorUid); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 877 | ApplicationInfo appInfo = null; |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 878 | if (config.creatorName != null && config.creatorName.equals(systemName)) { |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 879 | appInfo = mContext.getApplicationInfo(); |
| 880 | } else { |
| 881 | try { |
| 882 | IPackageManager ipm = AppGlobals.getPackageManager(); |
Fan Zhang | 51365c3 | 2016-09-20 12:22:18 -0700 | [diff] [blame] | 883 | appInfo = ipm.getApplicationInfo(config.creatorName, 0 /* flags */, userId); |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 884 | } catch (RemoteException rex) { |
| 885 | } |
| 886 | } |
| 887 | if (appInfo != null && |
| 888 | !appInfo.packageName.equals(mContext.getString(R.string.settings_package)) && |
| 889 | !appInfo.packageName.equals( |
| 890 | mContext.getString(R.string.certinstaller_package))) { |
| 891 | return mContext.getString(R.string.saved_network, appInfo.loadLabel(pm)); |
| 892 | } |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 893 | } |
Sanket Padawe | 56cfbfb | 2015-05-05 20:10:46 -0700 | [diff] [blame] | 894 | return ""; |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 897 | /** |
| 898 | * Returns the display title for the AccessPoint, such as for an AccessPointPreference's title. |
| 899 | */ |
| 900 | public String getTitle() { |
| 901 | if (isPasspoint()) { |
| 902 | return mConfig.providerFriendlyName; |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 903 | } else if (isPasspointConfig()) { |
| 904 | return mProviderFriendlyName; |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 905 | } else if (isOsuProvider()) { |
| 906 | return mOsuProvider.getFriendlyName(); |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 907 | } else { |
| 908 | return getSsidStr(); |
| 909 | } |
| 910 | } |
| 911 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 912 | public String getSummary() { |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 913 | return getSettingsSummary(); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | public String getSettingsSummary() { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 917 | // Update to new summary |
| 918 | StringBuilder summary = new StringBuilder(); |
| 919 | |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 920 | if (isOsuProvider()) { |
| 921 | if (mOsuProvisioningComplete) { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 922 | summary.append(mContext.getString(R.string.osu_sign_up_complete)); |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 923 | } else if (mOsuFailure != null) { |
| 924 | summary.append(mOsuFailure); |
| 925 | } else if (mOsuStatus != null) { |
| 926 | summary.append(mOsuStatus); |
| 927 | } else { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 928 | summary.append(mContext.getString(R.string.tap_to_sign_up)); |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 929 | } |
| 930 | } else if (isActive()) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 931 | if (mConfig != null && getDetailedState() == DetailedState.CONNECTED |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 932 | && mIsCarrierAp) { |
| 933 | // This is the active connection on a carrier AP |
| 934 | summary.append(String.format(mContext.getString(R.string.connected_via_carrier), |
| 935 | mCarrierName)); |
| 936 | } else { |
Quang Luong | 98d06bd | 2019-01-30 13:42:26 -0800 | [diff] [blame] | 937 | summary.append(getSummary(mContext, /* ssid */ null, getDetailedState(), |
Roshan Pius | e9b8e50 | 2019-01-23 15:42:44 -0800 | [diff] [blame] | 938 | mInfo != null && mInfo.isEphemeral(), |
| 939 | mInfo != null ? mInfo.getNetworkSuggestionOrSpecifierPackageName() : null)); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 940 | } |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 941 | } else { // not active |
| 942 | if (mConfig != null && mConfig.hasNoInternetAccess()) { |
| 943 | int messageID = mConfig.getNetworkSelectionStatus().isNetworkPermanentlyDisabled() |
| 944 | ? R.string.wifi_no_internet_no_reconnect |
| 945 | : R.string.wifi_no_internet; |
| 946 | summary.append(mContext.getString(messageID)); |
| 947 | } else if (mConfig != null && !mConfig.getNetworkSelectionStatus().isNetworkEnabled()) { |
| 948 | WifiConfiguration.NetworkSelectionStatus networkStatus = |
| 949 | mConfig.getNetworkSelectionStatus(); |
| 950 | switch (networkStatus.getNetworkSelectionDisableReason()) { |
| 951 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE: |
| 952 | summary.append(mContext.getString(R.string.wifi_disabled_password_failure)); |
Glen Kuhne | 446afac | 2017-06-22 16:02:33 -0700 | [diff] [blame] | 953 | break; |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 954 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD: |
| 955 | summary.append(mContext.getString(R.string.wifi_check_password_try_again)); |
Glen Kuhne | 446afac | 2017-06-22 16:02:33 -0700 | [diff] [blame] | 956 | break; |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 957 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE: |
| 958 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_DNS_FAILURE: |
| 959 | summary.append(mContext.getString(R.string.wifi_disabled_network_failure)); |
| 960 | break; |
| 961 | case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION: |
| 962 | summary.append(mContext.getString(R.string.wifi_disabled_generic)); |
| 963 | break; |
| 964 | } |
| 965 | } else if (mConfig != null && mConfig.getNetworkSelectionStatus().isNotRecommended()) { |
| 966 | summary.append(mContext.getString( |
| 967 | R.string.wifi_disabled_by_recommendation_provider)); |
| 968 | } else if (mIsCarrierAp) { |
| 969 | summary.append(String.format(mContext.getString( |
| 970 | R.string.available_via_carrier), mCarrierName)); |
| 971 | } else if (!isReachable()) { // Wifi out of range |
| 972 | summary.append(mContext.getString(R.string.wifi_not_in_range)); |
| 973 | } else { // In range, not disabled. |
| 974 | if (mConfig != null) { // Is saved network |
| 975 | // Last attempt to connect to this failed. Show reason why |
| 976 | switch (mConfig.recentFailure.getAssociationStatus()) { |
| 977 | case WifiConfiguration.RecentFailure.STATUS_AP_UNABLE_TO_HANDLE_NEW_STA: |
| 978 | summary.append(mContext.getString( |
| 979 | R.string.wifi_ap_unable_to_handle_new_sta)); |
| 980 | break; |
| 981 | default: |
| 982 | // "Saved" |
| 983 | summary.append(mContext.getString(R.string.wifi_remembered)); |
| 984 | break; |
| 985 | } |
Glen Kuhne | 446afac | 2017-06-22 16:02:33 -0700 | [diff] [blame] | 986 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 990 | |
| 991 | |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 992 | if (isVerboseLoggingEnabled()) { |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 993 | summary.append(WifiUtils.buildLoggingSummary(this, mConfig)); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 994 | } |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 995 | |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 996 | if (mConfig != null && (WifiUtils.isMeteredOverridden(mConfig) || mConfig.meteredHint)) { |
Salvador Martinez | 6f6fe75 | 2018-03-21 16:33:53 -0700 | [diff] [blame] | 997 | return mContext.getResources().getString( |
| 998 | R.string.preference_summary_default_combination, |
Quang Luong | 19bae88c | 2018-12-14 16:05:05 -0800 | [diff] [blame] | 999 | WifiUtils.getMeteredLabel(mContext, mConfig), |
Salvador Martinez | 6f6fe75 | 2018-03-21 16:33:53 -0700 | [diff] [blame] | 1000 | summary.toString()); |
| 1001 | } |
| 1002 | |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 1003 | // If Speed label and summary are both present, use the preference combination to combine |
| 1004 | // the two, else return the non-null one. |
| 1005 | if (getSpeedLabel() != null && summary.length() != 0) { |
Sundeep Ghuman | d3171ca | 2017-07-12 23:02:24 -0700 | [diff] [blame] | 1006 | return mContext.getResources().getString( |
| 1007 | R.string.preference_summary_default_combination, |
| 1008 | getSpeedLabel(), |
| 1009 | summary.toString()); |
Eric Schwarzenbach | f4f3842b | 2017-07-17 16:45:04 -0700 | [diff] [blame] | 1010 | } else if (getSpeedLabel() != null) { |
| 1011 | return getSpeedLabel(); |
Sundeep Ghuman | d3171ca | 2017-07-12 23:02:24 -0700 | [diff] [blame] | 1012 | } else { |
| 1013 | return summary.toString(); |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1014 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /** |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1018 | * Return whether this is the active connection. |
| 1019 | * For ephemeral connections (networkId is invalid), this returns false if the network is |
| 1020 | * disconnected. |
| 1021 | */ |
| 1022 | public boolean isActive() { |
| 1023 | return mNetworkInfo != null && |
| 1024 | (networkId != WifiConfiguration.INVALID_NETWORK_ID || |
| 1025 | mNetworkInfo.getState() != State.DISCONNECTED); |
| 1026 | } |
| 1027 | |
| 1028 | public boolean isConnectable() { |
| 1029 | return getLevel() != -1 && getDetailedState() == null; |
| 1030 | } |
| 1031 | |
| 1032 | public boolean isEphemeral() { |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 1033 | return mInfo != null && mInfo.isEphemeral() && |
| 1034 | mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1035 | } |
| 1036 | |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1037 | /** |
| 1038 | * Return true if this AccessPoint represents a Passpoint AP. |
| 1039 | */ |
Vinit Deshpande | 5b7352c | 2015-07-09 16:53:12 -0700 | [diff] [blame] | 1040 | public boolean isPasspoint() { |
| 1041 | return mConfig != null && mConfig.isPasspoint(); |
| 1042 | } |
| 1043 | |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1044 | /** |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1045 | * Return true if this AccessPoint represents a Passpoint provider configuration. |
| 1046 | */ |
| 1047 | public boolean isPasspointConfig() { |
| 1048 | return mFqdn != null; |
| 1049 | } |
| 1050 | |
| 1051 | /** |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 1052 | * Return true if this AccessPoint represents an OSU Provider. |
| 1053 | */ |
| 1054 | public boolean isOsuProvider() { |
| 1055 | return mOsuProvider != null; |
| 1056 | } |
| 1057 | |
| 1058 | /** |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1059 | * Starts the OSU Provisioning flow. |
| 1060 | */ |
| 1061 | public void startOsuProvisioning() { |
| 1062 | mContext.getSystemService(WifiManager.class).startSubscriptionProvisioning( |
| 1063 | mOsuProvider, |
Ecco Park | e67533e | 2019-03-01 15:09:35 -0800 | [diff] [blame] | 1064 | mContext.getMainExecutor(), |
| 1065 | new AccessPointProvisioningCallback() |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1066 | ); |
| 1067 | } |
| 1068 | |
| 1069 | /** |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1070 | * Return whether the given {@link WifiInfo} is for this access point. |
| 1071 | * If the current AP does not have a network Id then the config is used to |
| 1072 | * match based on SSID and security. |
| 1073 | */ |
| 1074 | private boolean isInfoForThisAccessPoint(WifiConfiguration config, WifiInfo info) { |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1075 | if (info.isOsuAp() || mOsuStatus != null) { |
| 1076 | return (info.isOsuAp() && mOsuStatus != null); |
| 1077 | } else if (info.isPasspointAp() || isPasspoint()) { |
| 1078 | return (info.isPasspointAp() && isPasspoint() |
| 1079 | && TextUtils.equals(info.getFqdn(), mConfig.FQDN)); |
Quang Luong | 919d464 | 2019-01-03 14:46:06 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1082 | if (networkId != WifiConfiguration.INVALID_NETWORK_ID) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1083 | return networkId == info.getNetworkId(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1084 | } else if (config != null) { |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1085 | return TextUtils.equals(getKey(config), getKey()); |
| 1086 | } else { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1087 | // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID. |
| 1088 | // (Note that we only do this if the WifiConfiguration explicitly equals INVALID). |
| 1089 | // TODO: Handle hex string SSIDs. |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1090 | return TextUtils.equals(removeDoubleQuotes(info.getSSID()), ssid); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | public boolean isSaved() { |
Quang Luong | 48ad03a | 2019-02-14 11:51:01 -0800 | [diff] [blame] | 1095 | return mConfig != null; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | public Object getTag() { |
| 1099 | return mTag; |
| 1100 | } |
| 1101 | |
| 1102 | public void setTag(Object tag) { |
| 1103 | mTag = tag; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Generate and save a default wifiConfiguration with common values. |
| 1108 | * Can only be called for unsecured networks. |
| 1109 | */ |
| 1110 | public void generateOpenNetworkConfig() { |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1111 | if ((security != SECURITY_NONE) && (security != SECURITY_OWE)) { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1112 | throw new IllegalStateException(); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1113 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1114 | if (mConfig != null) |
| 1115 | return; |
| 1116 | mConfig = new WifiConfiguration(); |
| 1117 | mConfig.SSID = AccessPoint.convertToQuotedString(ssid); |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1118 | |
| 1119 | if (security == SECURITY_NONE) { |
| 1120 | mConfig.allowedKeyManagement.set(KeyMgmt.NONE); |
| 1121 | } else { |
| 1122 | mConfig.allowedKeyManagement.set(KeyMgmt.OWE); |
| 1123 | mConfig.requirePMF = true; |
| 1124 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1125 | } |
| 1126 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1127 | public void saveWifiState(Bundle savedState) { |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1128 | if (ssid != null) savedState.putString(KEY_SSID, getSsidStr()); |
| 1129 | savedState.putInt(KEY_SECURITY, security); |
Sundeep Ghuman | a28050a | 2017-07-12 22:09:25 -0700 | [diff] [blame] | 1130 | savedState.putInt(KEY_SPEED, mSpeed); |
Vinit Deshpande | fcd4612 | 2015-06-11 18:22:23 -0700 | [diff] [blame] | 1131 | savedState.putInt(KEY_PSKTYPE, pskType); |
| 1132 | if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1133 | savedState.putParcelable(KEY_WIFIINFO, mInfo); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1134 | savedState.putParcelableArray(KEY_SCANRESULTS, |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1135 | mScanResults.toArray(new Parcelable[mScanResults.size() |
| 1136 | + mExtraScanResults.size()])); |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1137 | savedState.putParcelableArrayList(KEY_SCOREDNETWORKCACHE, |
| 1138 | new ArrayList<>(mScoredNetworkCache.values())); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1139 | if (mNetworkInfo != null) { |
| 1140 | savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo); |
| 1141 | } |
Peter Qiu | ced37db | 2017-03-14 15:51:22 -0700 | [diff] [blame] | 1142 | if (mFqdn != null) { |
| 1143 | savedState.putString(KEY_FQDN, mFqdn); |
| 1144 | } |
| 1145 | if (mProviderFriendlyName != null) { |
| 1146 | savedState.putString(KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName); |
| 1147 | } |
Peter Qiu | 280581b | 2017-07-24 14:18:56 -0700 | [diff] [blame] | 1148 | savedState.putBoolean(KEY_IS_CARRIER_AP, mIsCarrierAp); |
| 1149 | savedState.putInt(KEY_CARRIER_AP_EAP_TYPE, mCarrierApEapType); |
| 1150 | savedState.putString(KEY_CARRIER_NAME, mCarrierName); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | public void setListener(AccessPointListener listener) { |
| 1154 | mAccessPointListener = listener; |
| 1155 | } |
| 1156 | |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1157 | /** |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1158 | * Sets {@link #mScanResults} to the given collection and updates info based on the best RSSI |
| 1159 | * scan result. |
Sundeep Ghuman | d1e4492 | 2017-08-07 11:21:38 -0700 | [diff] [blame] | 1160 | * |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1161 | * @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] | 1162 | */ |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1163 | void setScanResults(Collection<ScanResult> scanResults) { |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1164 | if (CollectionUtils.isEmpty(scanResults)) { |
| 1165 | Log.d(TAG, "Cannot set scan results to empty list"); |
| 1166 | return; |
| 1167 | } |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1168 | |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1169 | // Validate scan results are for current AP only by matching SSID/BSSID |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 1170 | // 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] | 1171 | if (mKey != null && !isPasspoint() && !isOsuProvider()) { |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1172 | for (ScanResult result : scanResults) { |
| 1173 | String scanResultKey = AccessPoint.getKey(result); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1174 | if (mKey != null && !mKey.equals(scanResultKey)) { |
| 1175 | Log.d(TAG, String.format( |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1176 | "ScanResult %s\nkey of %s did not match current AP key %s", |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1177 | result, scanResultKey, mKey)); |
| 1178 | return; |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1179 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1180 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1183 | int oldLevel = getLevel(); |
| 1184 | mScanResults.clear(); |
| 1185 | mScanResults.addAll(scanResults); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1186 | updateBestRssiInfo(); |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1187 | int newLevel = getLevel(); |
| 1188 | |
| 1189 | // If newLevel is 0, there will be no displayed Preference since the AP is unreachable |
| 1190 | if (newLevel > 0 && newLevel != oldLevel) { |
| 1191 | // Only update labels on visible rssi changes |
| 1192 | updateSpeed(); |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1193 | ThreadUtils.postOnMainThread(() -> { |
| 1194 | if (mAccessPointListener != null) { |
| 1195 | mAccessPointListener.onLevelChanged(this); |
| 1196 | } |
| 1197 | }); |
| 1198 | |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1201 | ThreadUtils.postOnMainThread(() -> { |
| 1202 | if (mAccessPointListener != null) { |
| 1203 | mAccessPointListener.onAccessPointChanged(this); |
| 1204 | } |
| 1205 | }); |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1206 | } |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 1207 | |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1208 | /** |
| 1209 | * Sets the internal scan result cache to the list of home scans. |
| 1210 | * If there are no home scans, then the roaming scan list is used, and the AccessPoint is |
| 1211 | * marked as roaming. |
| 1212 | */ |
| 1213 | void setScanResultsPasspoint( |
| 1214 | @Nullable Collection<ScanResult> homeScans, |
| 1215 | @Nullable Collection<ScanResult> roamingScans) { |
| 1216 | mExtraScanResults.clear(); |
| 1217 | if (!CollectionUtils.isEmpty(homeScans)) { |
| 1218 | if (!CollectionUtils.isEmpty(roamingScans)) { |
| 1219 | mExtraScanResults.addAll(roamingScans); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1220 | } |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1221 | mIsRoaming = false; |
| 1222 | setScanResults(homeScans); |
| 1223 | } else if (!CollectionUtils.isEmpty(roamingScans)) { |
| 1224 | mIsRoaming = true; |
| 1225 | setScanResults(roamingScans); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1226 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1227 | } |
| 1228 | |
Quang Luong | 9051dfd | 2018-12-12 12:49:24 -0800 | [diff] [blame] | 1229 | /** |
| 1230 | * Attempt to update the AccessPoint with the current connection info. |
| 1231 | * This is used to set an AccessPoint to the active one if the connection info matches, or |
| 1232 | * conversely to set an AccessPoint to inactive if the connection info does not match. The RSSI |
| 1233 | * is also updated upon a match. Listeners will be notified if an update occurred. |
| 1234 | * |
| 1235 | * This is called in {@link WifiTracker#updateAccessPoints} as well as in callbacks for handling |
| 1236 | * NETWORK_STATE_CHANGED_ACTION, RSSI_CHANGED_ACTION, and onCapabilitiesChanged in WifiTracker. |
| 1237 | * |
| 1238 | * Returns true if an update occurred. |
| 1239 | */ |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1240 | public boolean update( |
| 1241 | @Nullable WifiConfiguration config, WifiInfo info, NetworkInfo networkInfo) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1242 | boolean updated = false; |
| 1243 | final int oldLevel = getLevel(); |
Mitchell Wills | 5a42db2 | 2015-08-03 09:46:08 -0700 | [diff] [blame] | 1244 | if (info != null && isInfoForThisAccessPoint(config, info)) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1245 | updated = (mInfo == null); |
Quang Luong | 1f1207b | 2019-02-19 17:35:50 -0800 | [diff] [blame] | 1246 | if (!isPasspoint() && mConfig != config) { |
Sundeep Ghuman | f4d3302 | 2017-06-05 19:47:36 -0700 | [diff] [blame] | 1247 | // We do not set updated = true as we do not want to increase the amount of sorting |
| 1248 | // and copying performed in WifiTracker at this time. If issues involving refresh |
| 1249 | // are still seen, we will investigate further. |
| 1250 | update(config); // Notifies the AccessPointListener of the change |
| 1251 | } |
Glen Kuhne | d38708c | 2017-07-19 14:53:34 -0700 | [diff] [blame] | 1252 | if (mRssi != info.getRssi() && info.getRssi() != WifiInfo.INVALID_RSSI) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1253 | mRssi = info.getRssi(); |
| 1254 | updated = true; |
Sundeep Ghuman | 5c5cd7a | 2017-05-03 12:45:44 -0700 | [diff] [blame] | 1255 | } else if (mNetworkInfo != null && networkInfo != null |
| 1256 | && mNetworkInfo.getDetailedState() != networkInfo.getDetailedState()) { |
Sundeep Ghuman | 96a5357 | 2017-04-20 21:25:41 -0700 | [diff] [blame] | 1257 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1258 | } |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1259 | mInfo = info; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1260 | mNetworkInfo = networkInfo; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1261 | } else if (mInfo != null) { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1262 | updated = true; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1263 | mInfo = null; |
| 1264 | mNetworkInfo = null; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1265 | } |
| 1266 | if (updated && mAccessPointListener != null) { |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1267 | ThreadUtils.postOnMainThread(() -> { |
| 1268 | if (mAccessPointListener != null) { |
| 1269 | mAccessPointListener.onAccessPointChanged(this); |
| 1270 | } |
| 1271 | }); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1272 | |
| 1273 | if (oldLevel != getLevel() /* current level */) { |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1274 | ThreadUtils.postOnMainThread(() -> { |
| 1275 | if (mAccessPointListener != null) { |
| 1276 | mAccessPointListener.onLevelChanged(this); |
| 1277 | } |
| 1278 | }); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1279 | } |
| 1280 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 1281 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 1282 | return updated; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1283 | } |
| 1284 | |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1285 | void update(@Nullable WifiConfiguration config) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1286 | mConfig = config; |
Quang Luong | 7054e92 | 2018-12-07 16:44:18 -0800 | [diff] [blame] | 1287 | if (mConfig != null) { |
| 1288 | ssid = removeDoubleQuotes(mConfig.SSID); |
| 1289 | } |
Sundeep Ghuman | d911da3 | 2017-07-05 20:06:05 -0700 | [diff] [blame] | 1290 | networkId = config != null ? config.networkId : WifiConfiguration.INVALID_NETWORK_ID; |
Sundeep Ghuman | 5d14fd7 | 2018-02-15 14:49:15 -0800 | [diff] [blame] | 1291 | ThreadUtils.postOnMainThread(() -> { |
| 1292 | if (mAccessPointListener != null) { |
| 1293 | mAccessPointListener.onAccessPointChanged(this); |
| 1294 | } |
| 1295 | }); |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1296 | } |
Shirish Kalele | c7a38ef | 2015-06-25 13:55:33 -0700 | [diff] [blame] | 1297 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1298 | @VisibleForTesting |
Sanket Padawe | 0775a98 | 2015-08-19 14:57:46 -0700 | [diff] [blame] | 1299 | void setRssi(int rssi) { |
| 1300 | mRssi = rssi; |
| 1301 | } |
| 1302 | |
Sundeep Ghuman | 54bdcfa | 2017-03-08 19:52:05 -0800 | [diff] [blame] | 1303 | /** Sets the rssi to {@link #UNREACHABLE_RSSI}. */ |
| 1304 | void setUnreachable() { |
| 1305 | setRssi(AccessPoint.UNREACHABLE_RSSI); |
| 1306 | } |
| 1307 | |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 1308 | int getSpeed() { return mSpeed;} |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1309 | |
| 1310 | @Nullable |
| 1311 | String getSpeedLabel() { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 1312 | return getSpeedLabel(mSpeed); |
| 1313 | } |
| 1314 | |
| 1315 | @Nullable |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1316 | @Speed |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1317 | private static int roundToClosestSpeedEnum(int speed) { |
Sundeep Ghuman | be3a74c | 2017-08-01 17:50:59 -0700 | [diff] [blame] | 1318 | if (speed < Speed.SLOW) { |
| 1319 | return Speed.NONE; |
| 1320 | } else if (speed < (Speed.SLOW + Speed.MODERATE) / 2) { |
| 1321 | return Speed.SLOW; |
| 1322 | } else if (speed < (Speed.MODERATE + Speed.FAST) / 2) { |
| 1323 | return Speed.MODERATE; |
| 1324 | } else if (speed < (Speed.FAST + Speed.VERY_FAST) / 2) { |
| 1325 | return Speed.FAST; |
| 1326 | } else { |
| 1327 | return Speed.VERY_FAST; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | @Nullable |
jackqdyulei | 9ee9b39 | 2017-12-22 11:31:49 -0800 | [diff] [blame] | 1332 | String getSpeedLabel(@Speed int speed) { |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1333 | return getSpeedLabel(mContext, speed); |
| 1334 | } |
| 1335 | |
| 1336 | private static String getSpeedLabel(Context context, int speed) { |
Eric Schwarzenbach | 0b8700f | 2017-07-25 14:32:21 -0700 | [diff] [blame] | 1337 | switch (speed) { |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1338 | case Speed.VERY_FAST: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1339 | return context.getString(R.string.speed_label_very_fast); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1340 | case Speed.FAST: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1341 | return context.getString(R.string.speed_label_fast); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1342 | case Speed.MODERATE: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1343 | return context.getString(R.string.speed_label_okay); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1344 | case Speed.SLOW: |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1345 | return context.getString(R.string.speed_label_slow); |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 1346 | case Speed.NONE: |
Sundeep Ghuman | 271e5de | 2017-05-30 14:11:39 -0700 | [diff] [blame] | 1347 | default: |
| 1348 | return null; |
| 1349 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 1352 | /** Return the speed label for a {@link ScoredNetwork} at the specified {@code rssi} level. */ |
| 1353 | @Nullable |
| 1354 | public static String getSpeedLabel(Context context, ScoredNetwork scoredNetwork, int rssi) { |
| 1355 | return getSpeedLabel(context, roundToClosestSpeedEnum(scoredNetwork.calculateBadge(rssi))); |
| 1356 | } |
| 1357 | |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1358 | /** Return true if the current RSSI is reachable, and false otherwise. */ |
Sundeep Ghuman | aaa8a1b | 2017-03-13 14:40:56 -0700 | [diff] [blame] | 1359 | public boolean isReachable() { |
Dave Schaefer | 9853743 | 2017-02-08 11:26:08 -0800 | [diff] [blame] | 1360 | return mRssi != UNREACHABLE_RSSI; |
| 1361 | } |
| 1362 | |
Roshan Pius | e9b8e50 | 2019-01-23 15:42:44 -0800 | [diff] [blame] | 1363 | private static CharSequence getAppLabel(String packageName, PackageManager packageManager) { |
| 1364 | CharSequence appLabel = ""; |
| 1365 | ApplicationInfo appInfo = null; |
| 1366 | try { |
| 1367 | int userId = UserHandle.getUserId(UserHandle.USER_CURRENT); |
| 1368 | appInfo = packageManager.getApplicationInfoAsUser(packageName, 0 /* flags */, userId); |
| 1369 | } catch (PackageManager.NameNotFoundException e) { |
| 1370 | Log.e(TAG, "Failed to get app info", e); |
| 1371 | return appLabel; |
| 1372 | } |
| 1373 | if (appInfo != null) { |
| 1374 | appLabel = appInfo.loadLabel(packageManager); |
| 1375 | } |
| 1376 | return appLabel; |
| 1377 | } |
| 1378 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1379 | public static String getSummary(Context context, String ssid, DetailedState state, |
Quang Luong | 98d06bd | 2019-01-30 13:42:26 -0800 | [diff] [blame] | 1380 | boolean isEphemeral, String suggestionOrSpecifierPackageName) { |
Quang Luong | 0a3edf0 | 2018-12-17 14:50:58 -0800 | [diff] [blame] | 1381 | if (state == DetailedState.CONNECTED) { |
Quang Luong | 98d06bd | 2019-01-30 13:42:26 -0800 | [diff] [blame] | 1382 | if (isEphemeral && !TextUtils.isEmpty(suggestionOrSpecifierPackageName)) { |
Roshan Pius | e9b8e50 | 2019-01-23 15:42:44 -0800 | [diff] [blame] | 1383 | CharSequence appLabel = |
| 1384 | getAppLabel(suggestionOrSpecifierPackageName, context.getPackageManager()); |
| 1385 | return context.getString(R.string.connected_via_app, appLabel); |
Vinit Deshpande | dcf00c9 | 2015-04-15 18:32:09 -0700 | [diff] [blame] | 1386 | } else if (isEphemeral) { |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1387 | // Special case for connected + ephemeral networks. |
Stephen Chen | 36dd5cf1 | 2017-03-20 13:27:51 -0700 | [diff] [blame] | 1388 | final NetworkScoreManager networkScoreManager = context.getSystemService( |
| 1389 | NetworkScoreManager.class); |
| 1390 | NetworkScorerAppData scorer = networkScoreManager.getActiveScorer(); |
| 1391 | if (scorer != null && scorer.getRecommendationServiceLabel() != null) { |
| 1392 | String format = context.getString(R.string.connected_via_network_scorer); |
| 1393 | return String.format(format, scorer.getRecommendationServiceLabel()); |
| 1394 | } else { |
| 1395 | return context.getString(R.string.connected_via_network_scorer_default); |
| 1396 | } |
Vinit Deshpande | fc40600 | 2015-04-15 18:10:55 -0700 | [diff] [blame] | 1397 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1398 | } |
| 1399 | |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1400 | // Case when there is wifi connected without internet connectivity. |
| 1401 | final ConnectivityManager cm = (ConnectivityManager) |
| 1402 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 1403 | if (state == DetailedState.CONNECTED) { |
| 1404 | IWifiManager wifiManager = IWifiManager.Stub.asInterface( |
| 1405 | ServiceManager.getService(Context.WIFI_SERVICE)); |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1406 | NetworkCapabilities nc = null; |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1407 | |
| 1408 | try { |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1409 | nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork()); |
| 1410 | } catch (RemoteException e) {} |
| 1411 | |
| 1412 | if (nc != null) { |
| 1413 | if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) { |
Maurice Lam | 7f6fc86 | 2017-07-12 16:43:49 -0700 | [diff] [blame] | 1414 | int id = context.getResources() |
| 1415 | .getIdentifier("network_available_sign_in", "string", "android"); |
| 1416 | return context.getString(id); |
Lorenzo Colitti | 1317e04 | 2016-12-13 13:30:07 +0900 | [diff] [blame] | 1417 | } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) { |
| 1418 | return context.getString(R.string.wifi_connected_no_internet); |
| 1419 | } |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1420 | } |
| 1421 | } |
Fan Zhang | 6acb766 | 2016-10-17 12:40:03 -0700 | [diff] [blame] | 1422 | if (state == null) { |
| 1423 | Log.w(TAG, "state is null, returning empty summary"); |
| 1424 | return ""; |
| 1425 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1426 | String[] formats = context.getResources().getStringArray((ssid == null) |
| 1427 | ? R.array.wifi_status : R.array.wifi_status_with_ssid); |
| 1428 | int index = state.ordinal(); |
| 1429 | |
| 1430 | if (index >= formats.length || formats[index].length() == 0) { |
Sanket Padawe | 3e9e5fa | 2015-05-28 10:41:14 -0700 | [diff] [blame] | 1431 | return ""; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1432 | } |
| 1433 | return String.format(formats[index], ssid); |
| 1434 | } |
| 1435 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1436 | public static String convertToQuotedString(String string) { |
| 1437 | return "\"" + string + "\""; |
| 1438 | } |
| 1439 | |
| 1440 | private static int getPskType(ScanResult result) { |
| 1441 | boolean wpa = result.capabilities.contains("WPA-PSK"); |
| 1442 | boolean wpa2 = result.capabilities.contains("WPA2-PSK"); |
| 1443 | if (wpa2 && wpa) { |
| 1444 | return PSK_WPA_WPA2; |
| 1445 | } else if (wpa2) { |
| 1446 | return PSK_WPA2; |
| 1447 | } else if (wpa) { |
| 1448 | return PSK_WPA; |
| 1449 | } else { |
| 1450 | Log.w(TAG, "Received abnormal flag string: " + result.capabilities); |
| 1451 | return PSK_UNKNOWN; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | private static int getSecurity(ScanResult result) { |
| 1456 | if (result.capabilities.contains("WEP")) { |
| 1457 | return SECURITY_WEP; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1458 | } else if (result.capabilities.contains("SAE")) { |
| 1459 | return SECURITY_SAE; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1460 | } else if (result.capabilities.contains("PSK")) { |
| 1461 | return SECURITY_PSK; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1462 | } else if (result.capabilities.contains("EAP_SUITE_B_192")) { |
| 1463 | return SECURITY_EAP_SUITE_B; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1464 | } else if (result.capabilities.contains("EAP")) { |
| 1465 | return SECURITY_EAP; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1466 | } else if (result.capabilities.contains("OWE")) { |
| 1467 | return SECURITY_OWE; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1468 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1469 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1470 | return SECURITY_NONE; |
| 1471 | } |
| 1472 | |
| 1473 | static int getSecurity(WifiConfiguration config) { |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1474 | if (config.allowedKeyManagement.get(KeyMgmt.SAE)) { |
| 1475 | return SECURITY_SAE; |
| 1476 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1477 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) { |
| 1478 | return SECURITY_PSK; |
| 1479 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1480 | if (config.allowedKeyManagement.get(KeyMgmt.SUITE_B_192)) { |
| 1481 | return SECURITY_EAP_SUITE_B; |
| 1482 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1483 | if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || |
| 1484 | config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) { |
| 1485 | return SECURITY_EAP; |
| 1486 | } |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1487 | if (config.allowedKeyManagement.get(KeyMgmt.OWE)) { |
| 1488 | return SECURITY_OWE; |
| 1489 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1490 | return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE; |
| 1491 | } |
| 1492 | |
| 1493 | public static String securityToString(int security, int pskType) { |
| 1494 | if (security == SECURITY_WEP) { |
| 1495 | return "WEP"; |
| 1496 | } else if (security == SECURITY_PSK) { |
| 1497 | if (pskType == PSK_WPA) { |
| 1498 | return "WPA"; |
| 1499 | } else if (pskType == PSK_WPA2) { |
| 1500 | return "WPA2"; |
| 1501 | } else if (pskType == PSK_WPA_WPA2) { |
| 1502 | return "WPA_WPA2"; |
| 1503 | } |
| 1504 | return "PSK"; |
| 1505 | } else if (security == SECURITY_EAP) { |
| 1506 | return "EAP"; |
Hai Shalom | aa6f920 | 2018-10-16 14:30:02 -0700 | [diff] [blame] | 1507 | } else if (security == SECURITY_SAE) { |
| 1508 | return "SAE"; |
| 1509 | } else if (security == SECURITY_EAP_SUITE_B) { |
| 1510 | return "SUITE_B"; |
| 1511 | } else if (security == SECURITY_OWE) { |
| 1512 | return "OWE"; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1513 | } |
| 1514 | return "NONE"; |
| 1515 | } |
| 1516 | |
| 1517 | static String removeDoubleQuotes(String string) { |
Jason Monk | 2b51cc3 | 2015-05-13 11:07:53 -0400 | [diff] [blame] | 1518 | if (TextUtils.isEmpty(string)) { |
| 1519 | return ""; |
| 1520 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1521 | int length = string.length(); |
| 1522 | if ((length > 1) && (string.charAt(0) == '"') |
| 1523 | && (string.charAt(length - 1) == '"')) { |
| 1524 | return string.substring(1, length - 1); |
| 1525 | } |
| 1526 | return string; |
| 1527 | } |
| 1528 | |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1529 | /** |
| 1530 | * Callbacks relaying changes to the AccessPoint representation. |
| 1531 | * |
| 1532 | * <p>All methods are invoked on the Main Thread. |
| 1533 | */ |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1534 | public interface AccessPointListener { |
Sundeep Ghuman | bb39991 | 2018-01-29 18:31:15 -0800 | [diff] [blame] | 1535 | /** |
| 1536 | * Indicates a change to the externally visible state of the AccessPoint trigger by an |
| 1537 | * update of ScanResults, saved configuration state, connection state, or score |
| 1538 | * (labels/metered) state. |
| 1539 | * |
| 1540 | * <p>Clients should refresh their view of the AccessPoint to match the updated state when |
| 1541 | * this is invoked. Overall this method is extraneous if clients are listening to |
| 1542 | * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks. |
| 1543 | * |
| 1544 | * <p>Examples of changes include signal strength, connection state, speed label, and |
| 1545 | * generally anything that would impact the summary string. |
| 1546 | * |
| 1547 | * @param accessPoint The accessPoint object the listener was registered on which has |
| 1548 | * changed |
| 1549 | */ |
| 1550 | @MainThread void onAccessPointChanged(AccessPoint accessPoint); |
| 1551 | |
| 1552 | /** |
| 1553 | * Indicates the "wifi pie signal level" has changed, retrieved via calls to |
| 1554 | * {@link AccessPoint#getLevel()}. |
| 1555 | * |
| 1556 | * <p>This call is a subset of {@link #onAccessPointChanged(AccessPoint)} , hence is also |
| 1557 | * extraneous if the client is already reacting to that or the |
| 1558 | * {@link WifiTracker.WifiListener#onAccessPointsChanged()} callbacks. |
| 1559 | * |
| 1560 | * @param accessPoint The accessPoint object the listener was registered on whose level has |
| 1561 | * changed |
| 1562 | */ |
| 1563 | @MainThread void onLevelChanged(AccessPoint accessPoint); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1564 | } |
Sundeep Ghuman | c0cf848 | 2018-01-26 18:23:34 -0800 | [diff] [blame] | 1565 | |
| 1566 | private static boolean isVerboseLoggingEnabled() { |
| 1567 | return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE); |
| 1568 | } |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1569 | |
| 1570 | /** |
| 1571 | * Callbacks relaying changes to the OSU provisioning status started in startOsuProvisioning(). |
| 1572 | * |
| 1573 | * All methods are invoked on the Main Thread |
| 1574 | */ |
Quang Luong | 8123f77 | 2019-02-08 16:30:12 -0800 | [diff] [blame] | 1575 | @VisibleForTesting |
| 1576 | class AccessPointProvisioningCallback extends ProvisioningCallback { |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1577 | @Override |
| 1578 | @MainThread public void onProvisioningFailure(int status) { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1579 | if (TextUtils.equals(mOsuStatus, mContext.getString(R.string.osu_completing_sign_up))) { |
| 1580 | mOsuFailure = mContext.getString(R.string.osu_sign_up_failed); |
| 1581 | } else { |
| 1582 | mOsuFailure = mContext.getString(R.string.osu_connect_failed); |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1583 | } |
| 1584 | mOsuStatus = null; |
| 1585 | mOsuProvisioningComplete = false; |
| 1586 | ThreadUtils.postOnMainThread(() -> { |
| 1587 | if (mAccessPointListener != null) { |
| 1588 | mAccessPointListener.onAccessPointChanged(AccessPoint.this); |
| 1589 | } |
| 1590 | }); |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | @Override |
| 1594 | @MainThread public void onProvisioningStatus(int status) { |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1595 | String newStatus = null; |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1596 | switch (status) { |
| 1597 | case OSU_STATUS_AP_CONNECTING: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1598 | case OSU_STATUS_AP_CONNECTED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1599 | case OSU_STATUS_SERVER_CONNECTING: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1600 | case OSU_STATUS_SERVER_VALIDATED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1601 | case OSU_STATUS_SERVER_CONNECTED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1602 | case OSU_STATUS_INIT_SOAP_EXCHANGE: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1603 | case OSU_STATUS_WAITING_FOR_REDIRECT_RESPONSE: |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1604 | newStatus = String.format(mContext.getString(R.string.osu_opening_provider), |
| 1605 | mOsuProvider.getFriendlyName()); |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1606 | break; |
| 1607 | case OSU_STATUS_REDIRECT_RESPONSE_RECEIVED: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1608 | case OSU_STATUS_SECOND_SOAP_EXCHANGE: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1609 | case OSU_STATUS_THIRD_SOAP_EXCHANGE: |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1610 | case OSU_STATUS_RETRIEVING_TRUST_ROOT_CERTS: |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1611 | newStatus = mContext.getString( |
| 1612 | R.string.osu_completing_sign_up); |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1613 | break; |
| 1614 | } |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1615 | boolean updated = !TextUtils.equals(mOsuStatus, newStatus); |
| 1616 | mOsuStatus = newStatus; |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1617 | mOsuFailure = null; |
| 1618 | mOsuProvisioningComplete = false; |
Quang Luong | f005c88 | 2019-01-29 16:32:03 -0800 | [diff] [blame] | 1619 | if (updated) { |
| 1620 | ThreadUtils.postOnMainThread(() -> { |
| 1621 | if (mAccessPointListener != null) { |
| 1622 | mAccessPointListener.onAccessPointChanged(AccessPoint.this); |
| 1623 | } |
| 1624 | }); |
| 1625 | } |
Quang Luong | 8d40a8e | 2019-01-02 16:25:38 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | @Override |
| 1629 | @MainThread public void onProvisioningComplete() { |
| 1630 | mOsuProvisioningComplete = true; |
| 1631 | mOsuFailure = null; |
| 1632 | mOsuStatus = null; |
| 1633 | ThreadUtils.postOnMainThread(() -> { |
| 1634 | if (mAccessPointListener != null) { |
| 1635 | mAccessPointListener.onAccessPointChanged(AccessPoint.this); |
| 1636 | } |
| 1637 | }); |
Quang Luong | 4d19484 | 2018-12-18 16:59:49 -0800 | [diff] [blame] | 1638 | } |
| 1639 | } |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 1640 | } |