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