Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.server.connectivity; |
| 18 | |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 19 | import android.app.AlarmManager; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 20 | import android.app.PendingIntent; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ComponentName; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 23 | import android.content.Context; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.net.ConnectivityManager; |
| 27 | import android.net.Network; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 28 | import android.net.NetworkCapabilities; |
| 29 | import android.net.NetworkInfo; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame^] | 30 | import android.net.NetworkRequest; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 31 | import android.net.TrafficStats; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 32 | import android.net.wifi.WifiInfo; |
| 33 | import android.net.wifi.WifiManager; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.Message; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 36 | import android.os.SystemClock; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 37 | import android.os.SystemProperties; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 38 | import android.os.UserHandle; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 39 | import android.provider.Settings; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 40 | import android.telephony.CellIdentityCdma; |
| 41 | import android.telephony.CellIdentityGsm; |
| 42 | import android.telephony.CellIdentityLte; |
| 43 | import android.telephony.CellIdentityWcdma; |
| 44 | import android.telephony.CellInfo; |
| 45 | import android.telephony.CellInfoCdma; |
| 46 | import android.telephony.CellInfoGsm; |
| 47 | import android.telephony.CellInfoLte; |
| 48 | import android.telephony.CellInfoWcdma; |
| 49 | import android.telephony.TelephonyManager; |
Paul Jensen | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 50 | import android.util.Log; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 51 | |
| 52 | import com.android.internal.util.Protocol; |
| 53 | import com.android.internal.util.State; |
| 54 | import com.android.internal.util.StateMachine; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 55 | import com.android.server.ConnectivityService; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 56 | import com.android.server.connectivity.NetworkAgentInfo; |
| 57 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 58 | import java.io.IOException; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 59 | import java.net.HttpURLConnection; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 60 | import java.net.URL; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 61 | import java.util.List; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * {@hide} |
| 65 | */ |
| 66 | public class NetworkMonitor extends StateMachine { |
| 67 | private static final boolean DBG = true; |
| 68 | private static final String TAG = "NetworkMonitor"; |
| 69 | private static final String DEFAULT_SERVER = "clients3.google.com"; |
| 70 | private static final int SOCKET_TIMEOUT_MS = 10000; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 71 | public static final String ACTION_NETWORK_CONDITIONS_MEASURED = |
| 72 | "android.net.conn.NETWORK_CONDITIONS_MEASURED"; |
| 73 | public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type"; |
| 74 | public static final String EXTRA_NETWORK_TYPE = "extra_network_type"; |
| 75 | public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received"; |
| 76 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal"; |
| 77 | public static final String EXTRA_CELL_ID = "extra_cellid"; |
| 78 | public static final String EXTRA_SSID = "extra_ssid"; |
| 79 | public static final String EXTRA_BSSID = "extra_bssid"; |
| 80 | /** real time since boot */ |
| 81 | public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms"; |
| 82 | public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms"; |
| 83 | |
| 84 | private static final String PERMISSION_ACCESS_NETWORK_CONDITIONS = |
| 85 | "android.permission.ACCESS_NETWORK_CONDITIONS"; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 86 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 87 | // Keep these in sync with CaptivePortalLoginActivity.java. |
| 88 | // Intent broadcast from CaptivePortalLogin indicating sign-in is complete. |
| 89 | // Extras: |
| 90 | // EXTRA_TEXT = netId |
| 91 | // LOGGED_IN_RESULT = "1" if we should use network, "0" if not. |
| 92 | private static final String ACTION_CAPTIVE_PORTAL_LOGGED_IN = |
| 93 | "android.net.netmon.captive_portal_logged_in"; |
| 94 | private static final String LOGGED_IN_RESULT = "result"; |
| 95 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 96 | // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED. |
| 97 | // The network should be used as a default internet connection. It was found to be: |
| 98 | // 1. a functioning network providing internet access, or |
| 99 | // 2. a captive portal and the user decided to use it as is. |
| 100 | public static final int NETWORK_TEST_RESULT_VALID = 0; |
| 101 | // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED. |
| 102 | // The network should not be used as a default internet connection. It was found to be: |
| 103 | // 1. a captive portal and the user is prompted to sign-in, or |
| 104 | // 2. a captive portal and the user did not want to use it, or |
| 105 | // 3. a broken network (e.g. DNS failed, connect failed, HTTP request failed). |
| 106 | public static final int NETWORK_TEST_RESULT_INVALID = 1; |
| 107 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 108 | private static final int BASE = Protocol.BASE_NETWORK_MONITOR; |
| 109 | |
| 110 | /** |
| 111 | * Inform NetworkMonitor that their network is connected. |
| 112 | * Initiates Network Validation. |
| 113 | */ |
| 114 | public static final int CMD_NETWORK_CONNECTED = BASE + 1; |
| 115 | |
| 116 | /** |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 117 | * Inform ConnectivityService that the network has been tested. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 118 | * obj = NetworkAgentInfo |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 119 | * arg1 = One of the NETWORK_TESTED_RESULT_* constants. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 120 | */ |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 121 | public static final int EVENT_NETWORK_TESTED = BASE + 2; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Inform NetworkMonitor to linger a network. The Monitor should |
| 125 | * start a timer and/or start watching for zero live connections while |
| 126 | * moving towards LINGER_COMPLETE. After the Linger period expires |
| 127 | * (or other events mark the end of the linger state) the LINGER_COMPLETE |
| 128 | * event should be sent and the network will be shut down. If a |
| 129 | * CMD_NETWORK_CONNECTED happens before the LINGER completes |
| 130 | * it indicates further desire to keep the network alive and so |
| 131 | * the LINGER is aborted. |
| 132 | */ |
| 133 | public static final int CMD_NETWORK_LINGER = BASE + 3; |
| 134 | |
| 135 | /** |
| 136 | * Message to self indicating linger delay has expired. |
| 137 | * arg1 = Token to ignore old messages. |
| 138 | */ |
| 139 | private static final int CMD_LINGER_EXPIRED = BASE + 4; |
| 140 | |
| 141 | /** |
| 142 | * Inform ConnectivityService that the network LINGER period has |
| 143 | * expired. |
| 144 | * obj = NetworkAgentInfo |
| 145 | */ |
| 146 | public static final int EVENT_NETWORK_LINGER_COMPLETE = BASE + 5; |
| 147 | |
| 148 | /** |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 149 | * Message to self indicating it's time to evaluate a network's connectivity. |
| 150 | * arg1 = Token to ignore old messages. |
| 151 | */ |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 152 | private static final int CMD_REEVALUATE = BASE + 6; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 153 | |
| 154 | /** |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 155 | * Inform NetworkMonitor that the network has disconnected. |
| 156 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 157 | public static final int CMD_NETWORK_DISCONNECTED = BASE + 7; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Force evaluation even if it has succeeded in the past. |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 161 | * arg1 = UID responsible for requesting this reeval. Will be billed for data. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 162 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 163 | public static final int CMD_FORCE_REEVALUATION = BASE + 8; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Message to self indicating captive portal login is complete. |
| 167 | * arg1 = Token to ignore old messages. |
| 168 | * arg2 = 1 if we should use this network, 0 otherwise. |
| 169 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 170 | private static final int CMD_CAPTIVE_PORTAL_LOGGED_IN = BASE + 9; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Message to self indicating user desires to log into captive portal. |
| 174 | * arg1 = Token to ignore old messages. |
| 175 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 176 | private static final int CMD_USER_WANTS_SIGN_IN = BASE + 10; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Request ConnectivityService display provisioning notification. |
| 180 | * arg1 = Whether to make the notification visible. |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 181 | * arg2 = NetID. |
| 182 | * obj = Intent to be launched when notification selected by user, null if !arg1. |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 183 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 184 | public static final int EVENT_PROVISIONING_NOTIFICATION = BASE + 11; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Message to self indicating sign-in app bypassed captive portal. |
| 188 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 189 | private static final int EVENT_APP_BYPASSED_CAPTIVE_PORTAL = BASE + 12; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Message to self indicating no sign-in app responded. |
| 193 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 194 | private static final int EVENT_NO_APP_RESPONSE = BASE + 13; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Message to self indicating sign-in app indicates sign-in is not possible. |
| 198 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 199 | private static final int EVENT_APP_INDICATES_SIGN_IN_IMPOSSIBLE = BASE + 14; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 200 | |
| 201 | private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger"; |
| 202 | // Default to 30s linger time-out. |
| 203 | private static final int DEFAULT_LINGER_DELAY_MS = 30000; |
| 204 | private final int mLingerDelayMs; |
| 205 | private int mLingerToken = 0; |
| 206 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 207 | // Negative values disable reevaluation. |
| 208 | private static final String REEVALUATE_DELAY_PROPERTY = "persist.netmon.reeval_delay"; |
| 209 | // Default to 5s reevaluation delay. |
| 210 | private static final int DEFAULT_REEVALUATE_DELAY_MS = 5000; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 211 | private static final int MAX_RETRIES = 10; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 212 | private final int mReevaluateDelayMs; |
| 213 | private int mReevaluateToken = 0; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 214 | private static final int INVALID_UID = -1; |
| 215 | private int mUidResponsibleForReeval = INVALID_UID; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 216 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 217 | private int mCaptivePortalLoggedInToken = 0; |
| 218 | private int mUserPromptedToken = 0; |
| 219 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 220 | private final Context mContext; |
| 221 | private final Handler mConnectivityServiceHandler; |
| 222 | private final NetworkAgentInfo mNetworkAgentInfo; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 223 | private final TelephonyManager mTelephonyManager; |
| 224 | private final WifiManager mWifiManager; |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 225 | private final AlarmManager mAlarmManager; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame^] | 226 | private final NetworkRequest mDefaultRequest; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 227 | |
| 228 | private String mServer; |
| 229 | private boolean mIsCaptivePortalCheckEnabled = false; |
| 230 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 231 | // Set if the user explicitly selected "Do not use this network" in captive portal sign-in app. |
| 232 | private boolean mUserDoesNotWant = false; |
| 233 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 234 | public boolean systemReady = false; |
| 235 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 236 | private State mDefaultState = new DefaultState(); |
| 237 | private State mOfflineState = new OfflineState(); |
| 238 | private State mValidatedState = new ValidatedState(); |
| 239 | private State mEvaluatingState = new EvaluatingState(); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 240 | private State mUserPromptedState = new UserPromptedState(); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 241 | private State mCaptivePortalState = new CaptivePortalState(); |
| 242 | private State mLingeringState = new LingeringState(); |
| 243 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame^] | 244 | public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo, |
| 245 | NetworkRequest defaultRequest) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 246 | // Add suffix indicating which NetworkMonitor we're talking about. |
| 247 | super(TAG + networkAgentInfo.name()); |
| 248 | |
| 249 | mContext = context; |
| 250 | mConnectivityServiceHandler = handler; |
| 251 | mNetworkAgentInfo = networkAgentInfo; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 252 | mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 253 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 254 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame^] | 255 | mDefaultRequest = defaultRequest; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 256 | |
| 257 | addState(mDefaultState); |
| 258 | addState(mOfflineState, mDefaultState); |
| 259 | addState(mValidatedState, mDefaultState); |
| 260 | addState(mEvaluatingState, mDefaultState); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 261 | addState(mUserPromptedState, mDefaultState); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 262 | addState(mCaptivePortalState, mDefaultState); |
| 263 | addState(mLingeringState, mDefaultState); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 264 | setInitialState(mDefaultState); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 265 | |
| 266 | mServer = Settings.Global.getString(mContext.getContentResolver(), |
| 267 | Settings.Global.CAPTIVE_PORTAL_SERVER); |
| 268 | if (mServer == null) mServer = DEFAULT_SERVER; |
| 269 | |
| 270 | mLingerDelayMs = SystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS); |
| 271 | mReevaluateDelayMs = SystemProperties.getInt(REEVALUATE_DELAY_PROPERTY, |
| 272 | DEFAULT_REEVALUATE_DELAY_MS); |
| 273 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 274 | mIsCaptivePortalCheckEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 275 | Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 1) == 1; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 276 | |
| 277 | start(); |
| 278 | } |
| 279 | |
Paul Jensen | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 280 | @Override |
| 281 | protected void log(String s) { |
| 282 | Log.d(TAG + mNetworkAgentInfo.name(), s); |
| 283 | } |
| 284 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 285 | private class DefaultState extends State { |
| 286 | @Override |
| 287 | public boolean processMessage(Message message) { |
| 288 | if (DBG) log(getName() + message.toString()); |
| 289 | switch (message.what) { |
| 290 | case CMD_NETWORK_LINGER: |
| 291 | if (DBG) log("Lingering"); |
| 292 | transitionTo(mLingeringState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 293 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 294 | case CMD_NETWORK_CONNECTED: |
| 295 | if (DBG) log("Connected"); |
| 296 | transitionTo(mEvaluatingState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 297 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 298 | case CMD_NETWORK_DISCONNECTED: |
Robert Greenwalt | 1fd9aee | 2014-07-17 16:11:38 -0700 | [diff] [blame] | 299 | if (DBG) log("Disconnected - quitting"); |
| 300 | quit(); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 301 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 302 | case CMD_FORCE_REEVALUATION: |
| 303 | if (DBG) log("Forcing reevaluation"); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 304 | mUidResponsibleForReeval = message.arg1; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 305 | transitionTo(mEvaluatingState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 306 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 307 | default: |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 308 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 309 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | private class OfflineState extends State { |
| 314 | @Override |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 315 | public void enter() { |
| 316 | mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, |
| 317 | NETWORK_TEST_RESULT_INVALID, 0, mNetworkAgentInfo)); |
| 318 | } |
| 319 | |
| 320 | @Override |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 321 | public boolean processMessage(Message message) { |
| 322 | if (DBG) log(getName() + message.toString()); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 323 | switch (message.what) { |
| 324 | case CMD_FORCE_REEVALUATION: |
| 325 | // If the user has indicated they explicitly do not want to use this network, |
| 326 | // don't allow a reevaluation as this will be pointless and could result in |
| 327 | // the user being annoyed with repeated unwanted notifications. |
| 328 | return mUserDoesNotWant ? HANDLED : NOT_HANDLED; |
| 329 | default: |
| 330 | return NOT_HANDLED; |
| 331 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | private class ValidatedState extends State { |
| 336 | @Override |
| 337 | public void enter() { |
| 338 | if (DBG) log("Validated"); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 339 | mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, |
| 340 | NETWORK_TEST_RESULT_VALID, 0, mNetworkAgentInfo)); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean processMessage(Message message) { |
| 345 | if (DBG) log(getName() + message.toString()); |
| 346 | switch (message.what) { |
| 347 | case CMD_NETWORK_CONNECTED: |
| 348 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 349 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 350 | default: |
| 351 | return NOT_HANDLED; |
| 352 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | private class EvaluatingState extends State { |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 357 | private int mRetries; |
| 358 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 359 | @Override |
| 360 | public void enter() { |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 361 | mRetries = 0; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 362 | sendMessage(CMD_REEVALUATE, ++mReevaluateToken, 0); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 363 | if (mUidResponsibleForReeval != INVALID_UID) { |
| 364 | TrafficStats.setThreadStatsUid(mUidResponsibleForReeval); |
| 365 | mUidResponsibleForReeval = INVALID_UID; |
| 366 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public boolean processMessage(Message message) { |
| 371 | if (DBG) log(getName() + message.toString()); |
| 372 | switch (message.what) { |
| 373 | case CMD_REEVALUATE: |
| 374 | if (message.arg1 != mReevaluateToken) |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 375 | return HANDLED; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame^] | 376 | // Don't bother validating networks that don't satisify the default request. |
| 377 | // This includes: |
| 378 | // - VPNs which can be considered explicitly desired by the user and the |
| 379 | // user's desire trumps whether the network validates. |
| 380 | // - Networks that don't provide internet access. It's unclear how to |
| 381 | // validate such networks. |
| 382 | // - Untrusted networks. It's unsafe to prompt the user to sign-in to |
| 383 | // such networks and the user didn't express interest in connecting to |
| 384 | // such networks (an app did) so the user may be unhappily surprised when |
| 385 | // asked to sign-in to a network they didn't want to connect to in the |
| 386 | // first place. Validation could be done to adjust the network scores |
| 387 | // however these networks are app-requested and may not be intended for |
| 388 | // general usage, in which case general validation may not be an accurate |
| 389 | // measure of the network's quality. Only the app knows how to evaluate |
| 390 | // the network so don't bother validating here. Furthermore sending HTTP |
| 391 | // packets over the network may be undesirable, for example an extremely |
| 392 | // expensive metered network, or unwanted leaking of the User Agent string. |
| 393 | if (!mDefaultRequest.networkCapabilities.satisfiedByNetworkCapabilities( |
| 394 | mNetworkAgentInfo.networkCapabilities)) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 395 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 396 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 397 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 398 | int httpResponseCode = isCaptivePortal(); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 399 | if (httpResponseCode == 204) { |
| 400 | transitionTo(mValidatedState); |
| 401 | } else if (httpResponseCode >= 200 && httpResponseCode <= 399) { |
Paul Jensen | a68d213 | 2014-08-22 09:32:07 -0400 | [diff] [blame] | 402 | transitionTo(mUserPromptedState); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 403 | } else if (++mRetries > MAX_RETRIES) { |
| 404 | transitionTo(mOfflineState); |
| 405 | } else if (mReevaluateDelayMs >= 0) { |
| 406 | Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0); |
| 407 | sendMessageDelayed(msg, mReevaluateDelayMs); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 408 | } |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 409 | return HANDLED; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 410 | case CMD_FORCE_REEVALUATION: |
| 411 | // Ignore duplicate requests. |
| 412 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 413 | default: |
| 414 | return NOT_HANDLED; |
| 415 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 416 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 417 | |
| 418 | @Override |
| 419 | public void exit() { |
| 420 | TrafficStats.clearThreadStatsUid(); |
| 421 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 424 | // BroadcastReceiver that waits for a particular Intent and then posts a message. |
| 425 | private class CustomIntentReceiver extends BroadcastReceiver { |
| 426 | private final Message mMessage; |
| 427 | private final String mAction; |
| 428 | CustomIntentReceiver(String action, int token, int message) { |
| 429 | mMessage = obtainMessage(message, token); |
| 430 | mAction = action + "_" + mNetworkAgentInfo.network.netId + "_" + token; |
| 431 | mContext.registerReceiver(this, new IntentFilter(mAction)); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 432 | } |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 433 | public PendingIntent getPendingIntent() { |
| 434 | return PendingIntent.getBroadcast(mContext, 0, new Intent(mAction), 0); |
| 435 | } |
| 436 | @Override |
| 437 | public void onReceive(Context context, Intent intent) { |
| 438 | if (intent.getAction().equals(mAction)) sendMessage(mMessage); |
| 439 | } |
| 440 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 441 | |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 442 | private class UserPromptedState extends State { |
| 443 | // Intent broadcast when user selects sign-in notification. |
| 444 | private static final String ACTION_SIGN_IN_REQUESTED = |
| 445 | "android.net.netmon.sign_in_requested"; |
| 446 | |
| 447 | private CustomIntentReceiver mUserRespondedBroadcastReceiver; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 448 | |
| 449 | @Override |
| 450 | public void enter() { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 451 | mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, |
| 452 | NETWORK_TEST_RESULT_INVALID, 0, mNetworkAgentInfo)); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 453 | // Wait for user to select sign-in notifcation. |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 454 | mUserRespondedBroadcastReceiver = new CustomIntentReceiver(ACTION_SIGN_IN_REQUESTED, |
| 455 | ++mUserPromptedToken, CMD_USER_WANTS_SIGN_IN); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 456 | // Initiate notification to sign-in. |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 457 | Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 1, |
| 458 | mNetworkAgentInfo.network.netId, |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 459 | mUserRespondedBroadcastReceiver.getPendingIntent()); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 460 | mConnectivityServiceHandler.sendMessage(message); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public boolean processMessage(Message message) { |
| 465 | if (DBG) log(getName() + message.toString()); |
| 466 | switch (message.what) { |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 467 | case CMD_USER_WANTS_SIGN_IN: |
| 468 | if (message.arg1 != mUserPromptedToken) |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 469 | return HANDLED; |
Paul Jensen | a68d213 | 2014-08-22 09:32:07 -0400 | [diff] [blame] | 470 | transitionTo(mCaptivePortalState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 471 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 472 | default: |
| 473 | return NOT_HANDLED; |
| 474 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 475 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 476 | |
| 477 | @Override |
| 478 | public void exit() { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 479 | Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 0, |
| 480 | mNetworkAgentInfo.network.netId, null); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 481 | mConnectivityServiceHandler.sendMessage(message); |
| 482 | mContext.unregisterReceiver(mUserRespondedBroadcastReceiver); |
| 483 | mUserRespondedBroadcastReceiver = null; |
| 484 | } |
| 485 | } |
| 486 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 487 | private class CaptivePortalState extends State { |
| 488 | private class CaptivePortalLoggedInBroadcastReceiver extends BroadcastReceiver { |
| 489 | private final int mToken; |
| 490 | |
| 491 | CaptivePortalLoggedInBroadcastReceiver(int token) { |
| 492 | mToken = token; |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public void onReceive(Context context, Intent intent) { |
| 497 | if (Integer.parseInt(intent.getStringExtra(Intent.EXTRA_TEXT)) == |
| 498 | mNetworkAgentInfo.network.netId) { |
| 499 | sendMessage(obtainMessage(CMD_CAPTIVE_PORTAL_LOGGED_IN, mToken, |
| 500 | Integer.parseInt(intent.getStringExtra(LOGGED_IN_RESULT)))); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | private CaptivePortalLoggedInBroadcastReceiver mCaptivePortalLoggedInBroadcastReceiver; |
| 506 | |
| 507 | @Override |
| 508 | public void enter() { |
| 509 | Intent intent = new Intent(Intent.ACTION_SEND); |
| 510 | intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(mNetworkAgentInfo.network.netId)); |
| 511 | intent.setType("text/plain"); |
| 512 | intent.setComponent(new ComponentName("com.android.captiveportallogin", |
| 513 | "com.android.captiveportallogin.CaptivePortalLoginActivity")); |
| 514 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 515 | |
| 516 | // Wait for result. |
| 517 | mCaptivePortalLoggedInBroadcastReceiver = new CaptivePortalLoggedInBroadcastReceiver( |
| 518 | ++mCaptivePortalLoggedInToken); |
| 519 | IntentFilter filter = new IntentFilter(ACTION_CAPTIVE_PORTAL_LOGGED_IN); |
| 520 | mContext.registerReceiver(mCaptivePortalLoggedInBroadcastReceiver, filter); |
| 521 | // Initiate app to log in. |
| 522 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public boolean processMessage(Message message) { |
| 527 | if (DBG) log(getName() + message.toString()); |
| 528 | switch (message.what) { |
| 529 | case CMD_CAPTIVE_PORTAL_LOGGED_IN: |
| 530 | if (message.arg1 != mCaptivePortalLoggedInToken) |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 531 | return HANDLED; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 532 | if (message.arg2 == 0) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 533 | mUserDoesNotWant = true; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 534 | // TODO: Should teardown network. |
| 535 | transitionTo(mOfflineState); |
| 536 | } else { |
| 537 | transitionTo(mValidatedState); |
| 538 | } |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 539 | return HANDLED; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 540 | default: |
| 541 | return NOT_HANDLED; |
| 542 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void exit() { |
| 547 | mContext.unregisterReceiver(mCaptivePortalLoggedInBroadcastReceiver); |
| 548 | mCaptivePortalLoggedInBroadcastReceiver = null; |
| 549 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | private class LingeringState extends State { |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 553 | private static final String ACTION_LINGER_EXPIRED = "android.net.netmon.lingerExpired"; |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 554 | |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 555 | private CustomIntentReceiver mBroadcastReceiver; |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 556 | private PendingIntent mIntent; |
| 557 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 558 | @Override |
| 559 | public void enter() { |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 560 | mBroadcastReceiver = new CustomIntentReceiver(ACTION_LINGER_EXPIRED, ++mLingerToken, |
| 561 | CMD_LINGER_EXPIRED); |
| 562 | mIntent = mBroadcastReceiver.getPendingIntent(); |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 563 | long wakeupTime = SystemClock.elapsedRealtime() + mLingerDelayMs; |
| 564 | mAlarmManager.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime, |
| 565 | // Give a specific window so we aren't subject to unknown inexactitude. |
| 566 | mLingerDelayMs / 6, mIntent); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public boolean processMessage(Message message) { |
| 571 | if (DBG) log(getName() + message.toString()); |
| 572 | switch (message.what) { |
| 573 | case CMD_NETWORK_CONNECTED: |
| 574 | // Go straight to active as we've already evaluated. |
| 575 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 576 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 577 | case CMD_LINGER_EXPIRED: |
| 578 | if (message.arg1 != mLingerToken) |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 579 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 580 | mConnectivityServiceHandler.sendMessage( |
| 581 | obtainMessage(EVENT_NETWORK_LINGER_COMPLETE, mNetworkAgentInfo)); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 582 | return HANDLED; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 583 | case CMD_FORCE_REEVALUATION: |
| 584 | // Ignore reevaluation attempts when lingering. A reevaluation could result |
| 585 | // in a transition to the validated state which would abort the linger |
| 586 | // timeout. Lingering is the result of score assessment; validity is |
| 587 | // irrelevant. |
| 588 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 589 | default: |
| 590 | return NOT_HANDLED; |
| 591 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 592 | } |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 593 | |
| 594 | @Override |
| 595 | public void exit() { |
| 596 | mAlarmManager.cancel(mIntent); |
| 597 | mContext.unregisterReceiver(mBroadcastReceiver); |
| 598 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Do a URL fetch on a known server to see if we get the data we expect. |
| 603 | * Returns HTTP response code. |
| 604 | */ |
| 605 | private int isCaptivePortal() { |
| 606 | if (!mIsCaptivePortalCheckEnabled) return 204; |
| 607 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 608 | HttpURLConnection urlConnection = null; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 609 | int httpResponseCode = 599; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 610 | try { |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 611 | URL url = new URL("http", mServer, "/generate_204"); |
| 612 | if (DBG) { |
| 613 | log("Checking " + url.toString() + " on " + |
| 614 | mNetworkAgentInfo.networkInfo.getExtraInfo()); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 615 | } |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 616 | urlConnection = (HttpURLConnection) mNetworkAgentInfo.network.openConnection(url); |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 617 | urlConnection.setInstanceFollowRedirects(false); |
| 618 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 619 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 620 | urlConnection.setUseCaches(false); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 621 | |
| 622 | // Time how long it takes to get a response to our request |
| 623 | long requestTimestamp = SystemClock.elapsedRealtime(); |
| 624 | |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 625 | urlConnection.getInputStream(); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 626 | |
| 627 | // Time how long it takes to get a response to our request |
| 628 | long responseTimestamp = SystemClock.elapsedRealtime(); |
| 629 | |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 630 | httpResponseCode = urlConnection.getResponseCode(); |
| 631 | if (DBG) { |
| 632 | log("isCaptivePortal: ret=" + httpResponseCode + |
| 633 | " headers=" + urlConnection.getHeaderFields()); |
| 634 | } |
| 635 | // NOTE: We may want to consider an "HTTP/1.0 204" response to be a captive |
| 636 | // portal. The only example of this seen so far was a captive portal. For |
| 637 | // the time being go with prior behavior of assuming it's not a captive |
| 638 | // portal. If it is considered a captive portal, a different sign-in URL |
| 639 | // is needed (i.e. can't browse a 204). This could be the result of an HTTP |
| 640 | // proxy server. |
| 641 | |
| 642 | // Consider 200 response with "Content-length=0" to not be a captive portal. |
| 643 | // There's no point in considering this a captive portal as the user cannot |
| 644 | // sign-in to an empty page. Probably the result of a broken transparent proxy. |
| 645 | // See http://b/9972012. |
| 646 | if (httpResponseCode == 200 && urlConnection.getContentLength() == 0) { |
| 647 | if (DBG) log("Empty 200 response interpreted as 204 response."); |
| 648 | httpResponseCode = 204; |
| 649 | } |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 650 | |
| 651 | sendNetworkConditionsBroadcast(true /* response received */, httpResponseCode == 204, |
| 652 | requestTimestamp, responseTimestamp); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 653 | } catch (IOException e) { |
| 654 | if (DBG) log("Probably not a portal: exception " + e); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 655 | if (httpResponseCode == 599) { |
| 656 | // TODO: Ping gateway and DNS server and log results. |
| 657 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 658 | } finally { |
| 659 | if (urlConnection != null) { |
| 660 | urlConnection.disconnect(); |
| 661 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 662 | } |
| 663 | return httpResponseCode; |
| 664 | } |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 665 | |
| 666 | /** |
| 667 | * @param responseReceived - whether or not we received a valid HTTP response to our request. |
| 668 | * If false, isCaptivePortal and responseTimestampMs are ignored |
| 669 | * TODO: This should be moved to the transports. The latency could be passed to the transports |
| 670 | * along with the captive portal result. Currently the TYPE_MOBILE broadcasts appear unused so |
| 671 | * perhaps this could just be added to the WiFi transport only. |
| 672 | */ |
| 673 | private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal, |
| 674 | long requestTimestampMs, long responseTimestampMs) { |
| 675 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 676 | Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 0) { |
| 677 | if (DBG) log("Don't send network conditions - lacking user consent."); |
| 678 | return; |
| 679 | } |
| 680 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 681 | if (systemReady == false) return; |
| 682 | |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 683 | Intent latencyBroadcast = new Intent(ACTION_NETWORK_CONDITIONS_MEASURED); |
| 684 | switch (mNetworkAgentInfo.networkInfo.getType()) { |
| 685 | case ConnectivityManager.TYPE_WIFI: |
| 686 | WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo(); |
| 687 | if (currentWifiInfo != null) { |
| 688 | // NOTE: getSSID()'s behavior changed in API 17; before that, SSIDs were not |
| 689 | // surrounded by double quotation marks (thus violating the Javadoc), but this |
| 690 | // was changed to match the Javadoc in API 17. Since clients may have started |
| 691 | // sanitizing the output of this method since API 17 was released, we should |
| 692 | // not change it here as it would become impossible to tell whether the SSID is |
| 693 | // simply being surrounded by quotes due to the API, or whether those quotes |
| 694 | // are actually part of the SSID. |
| 695 | latencyBroadcast.putExtra(EXTRA_SSID, currentWifiInfo.getSSID()); |
| 696 | latencyBroadcast.putExtra(EXTRA_BSSID, currentWifiInfo.getBSSID()); |
| 697 | } else { |
| 698 | if (DBG) logw("network info is TYPE_WIFI but no ConnectionInfo found"); |
| 699 | return; |
| 700 | } |
| 701 | break; |
| 702 | case ConnectivityManager.TYPE_MOBILE: |
| 703 | latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType()); |
| 704 | List<CellInfo> info = mTelephonyManager.getAllCellInfo(); |
| 705 | if (info == null) return; |
| 706 | int numRegisteredCellInfo = 0; |
| 707 | for (CellInfo cellInfo : info) { |
| 708 | if (cellInfo.isRegistered()) { |
| 709 | numRegisteredCellInfo++; |
| 710 | if (numRegisteredCellInfo > 1) { |
| 711 | if (DBG) log("more than one registered CellInfo. Can't " + |
| 712 | "tell which is active. Bailing."); |
| 713 | return; |
| 714 | } |
| 715 | if (cellInfo instanceof CellInfoCdma) { |
| 716 | CellIdentityCdma cellId = ((CellInfoCdma) cellInfo).getCellIdentity(); |
| 717 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 718 | } else if (cellInfo instanceof CellInfoGsm) { |
| 719 | CellIdentityGsm cellId = ((CellInfoGsm) cellInfo).getCellIdentity(); |
| 720 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 721 | } else if (cellInfo instanceof CellInfoLte) { |
| 722 | CellIdentityLte cellId = ((CellInfoLte) cellInfo).getCellIdentity(); |
| 723 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 724 | } else if (cellInfo instanceof CellInfoWcdma) { |
| 725 | CellIdentityWcdma cellId = ((CellInfoWcdma) cellInfo).getCellIdentity(); |
| 726 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 727 | } else { |
| 728 | if (DBG) logw("Registered cellinfo is unrecognized"); |
| 729 | return; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | break; |
| 734 | default: |
| 735 | return; |
| 736 | } |
| 737 | latencyBroadcast.putExtra(EXTRA_CONNECTIVITY_TYPE, mNetworkAgentInfo.networkInfo.getType()); |
| 738 | latencyBroadcast.putExtra(EXTRA_RESPONSE_RECEIVED, responseReceived); |
| 739 | latencyBroadcast.putExtra(EXTRA_REQUEST_TIMESTAMP_MS, requestTimestampMs); |
| 740 | |
| 741 | if (responseReceived) { |
| 742 | latencyBroadcast.putExtra(EXTRA_IS_CAPTIVE_PORTAL, isCaptivePortal); |
| 743 | latencyBroadcast.putExtra(EXTRA_RESPONSE_TIMESTAMP_MS, responseTimestampMs); |
| 744 | } |
Paul Jensen | 5529858 | 2014-08-20 11:01:41 -0400 | [diff] [blame] | 745 | mContext.sendBroadcastAsUser(latencyBroadcast, UserHandle.CURRENT, |
| 746 | PERMISSION_ACCESS_NETWORK_CONDITIONS); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 747 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 748 | } |