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; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 27 | import android.net.NetworkRequest; |
Paul Jensen | 8fe1742 | 2015-02-02 11:03:03 -0500 | [diff] [blame] | 28 | import android.net.ProxyInfo; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 29 | import android.net.TrafficStats; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 30 | import android.net.Uri; |
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 | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 49 | import android.util.Log; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 50 | |
| 51 | import com.android.internal.util.Protocol; |
| 52 | import com.android.internal.util.State; |
| 53 | import com.android.internal.util.StateMachine; |
| 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 | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 60 | import java.util.Random; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * {@hide} |
| 64 | */ |
| 65 | public class NetworkMonitor extends StateMachine { |
| 66 | private static final boolean DBG = true; |
| 67 | private static final String TAG = "NetworkMonitor"; |
Lorenzo Colitti | cd29cb6 | 2015-01-14 00:16:03 +0900 | [diff] [blame] | 68 | private static final String DEFAULT_SERVER = "connectivitycheck.android.com"; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 69 | private static final int SOCKET_TIMEOUT_MS = 10000; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 70 | public static final String ACTION_NETWORK_CONDITIONS_MEASURED = |
| 71 | "android.net.conn.NETWORK_CONDITIONS_MEASURED"; |
| 72 | public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type"; |
| 73 | public static final String EXTRA_NETWORK_TYPE = "extra_network_type"; |
| 74 | public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received"; |
| 75 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal"; |
| 76 | public static final String EXTRA_CELL_ID = "extra_cellid"; |
| 77 | public static final String EXTRA_SSID = "extra_ssid"; |
| 78 | public static final String EXTRA_BSSID = "extra_bssid"; |
| 79 | /** real time since boot */ |
| 80 | public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms"; |
| 81 | public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms"; |
| 82 | |
| 83 | private static final String PERMISSION_ACCESS_NETWORK_CONDITIONS = |
| 84 | "android.permission.ACCESS_NETWORK_CONDITIONS"; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 85 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 86 | // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED. |
| 87 | // The network should be used as a default internet connection. It was found to be: |
| 88 | // 1. a functioning network providing internet access, or |
| 89 | // 2. a captive portal and the user decided to use it as is. |
| 90 | public static final int NETWORK_TEST_RESULT_VALID = 0; |
| 91 | // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED. |
| 92 | // The network should not be used as a default internet connection. It was found to be: |
| 93 | // 1. a captive portal and the user is prompted to sign-in, or |
| 94 | // 2. a captive portal and the user did not want to use it, or |
| 95 | // 3. a broken network (e.g. DNS failed, connect failed, HTTP request failed). |
| 96 | public static final int NETWORK_TEST_RESULT_INVALID = 1; |
| 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 | /** |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 107 | * Inform ConnectivityService that the network has been tested. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 108 | * obj = NetworkAgentInfo |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 109 | * arg1 = One of the NETWORK_TESTED_RESULT_* constants. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 110 | */ |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 111 | public static final int EVENT_NETWORK_TESTED = BASE + 2; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Inform NetworkMonitor to linger a network. The Monitor should |
| 115 | * start a timer and/or start watching for zero live connections while |
| 116 | * moving towards LINGER_COMPLETE. After the Linger period expires |
| 117 | * (or other events mark the end of the linger state) the LINGER_COMPLETE |
| 118 | * event should be sent and the network will be shut down. If a |
| 119 | * CMD_NETWORK_CONNECTED happens before the LINGER completes |
| 120 | * it indicates further desire to keep the network alive and so |
| 121 | * the LINGER is aborted. |
| 122 | */ |
| 123 | public static final int CMD_NETWORK_LINGER = BASE + 3; |
| 124 | |
| 125 | /** |
| 126 | * Message to self indicating linger delay has expired. |
| 127 | * arg1 = Token to ignore old messages. |
| 128 | */ |
| 129 | private static final int CMD_LINGER_EXPIRED = BASE + 4; |
| 130 | |
| 131 | /** |
| 132 | * Inform ConnectivityService that the network LINGER period has |
| 133 | * expired. |
| 134 | * obj = NetworkAgentInfo |
| 135 | */ |
| 136 | public static final int EVENT_NETWORK_LINGER_COMPLETE = BASE + 5; |
| 137 | |
| 138 | /** |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 139 | * Message to self indicating it's time to evaluate a network's connectivity. |
| 140 | * arg1 = Token to ignore old messages. |
| 141 | */ |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 142 | private static final int CMD_REEVALUATE = BASE + 6; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 143 | |
| 144 | /** |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 145 | * Inform NetworkMonitor that the network has disconnected. |
| 146 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 147 | public static final int CMD_NETWORK_DISCONNECTED = BASE + 7; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Force evaluation even if it has succeeded in the past. |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 151 | * arg1 = UID responsible for requesting this reeval. Will be billed for data. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 152 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 153 | public static final int CMD_FORCE_REEVALUATION = BASE + 8; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 154 | |
| 155 | /** |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 156 | * Message to self indicating captive portal app finished. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 157 | * arg1 = one of: CAPTIVE_PORTAL_APP_RETURN_DISMISSED, |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 158 | * CAPTIVE_PORTAL_APP_RETURN_UNWANTED, |
| 159 | * CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 160 | * obj = mCaptivePortalLoggedInResponseToken as String |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 161 | */ |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 162 | public static final int CMD_CAPTIVE_PORTAL_APP_FINISHED = BASE + 9; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Request ConnectivityService display provisioning notification. |
| 166 | * arg1 = Whether to make the notification visible. |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 167 | * arg2 = NetID. |
| 168 | * 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] | 169 | */ |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 170 | public static final int EVENT_PROVISIONING_NOTIFICATION = BASE + 10; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 171 | |
| 172 | /** |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 173 | * Message to self indicating sign-in app should be launched. |
| 174 | * Sent by mLaunchCaptivePortalAppBroadcastReceiver when the |
| 175 | * user touches the sign in notification. |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 176 | */ |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 177 | private static final int CMD_LAUNCH_CAPTIVE_PORTAL_APP = BASE + 11; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 178 | |
| 179 | private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger"; |
| 180 | // Default to 30s linger time-out. |
| 181 | private static final int DEFAULT_LINGER_DELAY_MS = 30000; |
| 182 | private final int mLingerDelayMs; |
| 183 | private int mLingerToken = 0; |
| 184 | |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 185 | // Start mReevaluateDelayMs at this value and double. |
| 186 | private static final int INITIAL_REEVALUATE_DELAY_MS = 1000; |
| 187 | private static final int MAX_REEVALUATE_DELAY_MS = 10*60*1000; |
| 188 | // Before network has been evaluated this many times, ignore repeated reevaluate requests. |
| 189 | private static final int IGNORE_REEVALUATE_ATTEMPTS = 5; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 190 | private int mReevaluateToken = 0; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 191 | private static final int INVALID_UID = -1; |
| 192 | private int mUidResponsibleForReeval = INVALID_UID; |
Paul Jensen | d9be23f | 2015-05-19 14:51:47 -0400 | [diff] [blame] | 193 | // Stop blaming UID that requested re-evaluation after this many attempts. |
| 194 | private static final int BLAME_FOR_EVALUATION_ATTEMPTS = 5; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 195 | |
| 196 | private final Context mContext; |
| 197 | private final Handler mConnectivityServiceHandler; |
| 198 | private final NetworkAgentInfo mNetworkAgentInfo; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 199 | private final TelephonyManager mTelephonyManager; |
| 200 | private final WifiManager mWifiManager; |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 201 | private final AlarmManager mAlarmManager; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 202 | private final NetworkRequest mDefaultRequest; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 203 | |
| 204 | private String mServer; |
| 205 | private boolean mIsCaptivePortalCheckEnabled = false; |
| 206 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 207 | // Set if the user explicitly selected "Do not use this network" in captive portal sign-in app. |
| 208 | private boolean mUserDoesNotWant = false; |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 209 | // Avoids surfacing "Sign in to network" notification. |
| 210 | private boolean mDontDisplaySigninNotification = false; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 211 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 212 | public boolean systemReady = false; |
| 213 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 214 | private final State mDefaultState = new DefaultState(); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 215 | private final State mValidatedState = new ValidatedState(); |
| 216 | private final State mMaybeNotifyState = new MaybeNotifyState(); |
| 217 | private final State mEvaluatingState = new EvaluatingState(); |
| 218 | private final State mCaptivePortalState = new CaptivePortalState(); |
| 219 | private final State mLingeringState = new LingeringState(); |
| 220 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 221 | private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 222 | private String mCaptivePortalLoggedInResponseToken = null; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 223 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 224 | public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo, |
| 225 | NetworkRequest defaultRequest) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 226 | // Add suffix indicating which NetworkMonitor we're talking about. |
| 227 | super(TAG + networkAgentInfo.name()); |
| 228 | |
| 229 | mContext = context; |
| 230 | mConnectivityServiceHandler = handler; |
| 231 | mNetworkAgentInfo = networkAgentInfo; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 232 | mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 233 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 234 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 235 | mDefaultRequest = defaultRequest; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 236 | |
| 237 | addState(mDefaultState); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 238 | addState(mValidatedState, mDefaultState); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 239 | addState(mMaybeNotifyState, mDefaultState); |
| 240 | addState(mEvaluatingState, mMaybeNotifyState); |
| 241 | addState(mCaptivePortalState, mMaybeNotifyState); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 242 | addState(mLingeringState, mDefaultState); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 243 | setInitialState(mDefaultState); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 244 | |
| 245 | mServer = Settings.Global.getString(mContext.getContentResolver(), |
| 246 | Settings.Global.CAPTIVE_PORTAL_SERVER); |
| 247 | if (mServer == null) mServer = DEFAULT_SERVER; |
| 248 | |
| 249 | mLingerDelayMs = SystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 250 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 251 | mIsCaptivePortalCheckEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 252 | Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 1) == 1; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 253 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 254 | mCaptivePortalLoggedInResponseToken = String.valueOf(new Random().nextLong()); |
| 255 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 256 | start(); |
| 257 | } |
| 258 | |
Paul Jensen | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 259 | @Override |
| 260 | protected void log(String s) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 261 | Log.d(TAG + "/" + mNetworkAgentInfo.name(), s); |
Paul Jensen | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 264 | // DefaultState is the parent of all States. It exists only to handle CMD_* messages but |
| 265 | // does not entail any real state (hence no enter() or exit() routines). |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 266 | private class DefaultState extends State { |
| 267 | @Override |
| 268 | public boolean processMessage(Message message) { |
| 269 | if (DBG) log(getName() + message.toString()); |
| 270 | switch (message.what) { |
| 271 | case CMD_NETWORK_LINGER: |
| 272 | if (DBG) log("Lingering"); |
| 273 | transitionTo(mLingeringState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 274 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 275 | case CMD_NETWORK_CONNECTED: |
| 276 | if (DBG) log("Connected"); |
| 277 | transitionTo(mEvaluatingState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 278 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 279 | case CMD_NETWORK_DISCONNECTED: |
Robert Greenwalt | 1fd9aee | 2014-07-17 16:11:38 -0700 | [diff] [blame] | 280 | if (DBG) log("Disconnected - quitting"); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 281 | if (mLaunchCaptivePortalAppBroadcastReceiver != null) { |
| 282 | mContext.unregisterReceiver(mLaunchCaptivePortalAppBroadcastReceiver); |
| 283 | mLaunchCaptivePortalAppBroadcastReceiver = null; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 284 | } |
Robert Greenwalt | 1fd9aee | 2014-07-17 16:11:38 -0700 | [diff] [blame] | 285 | quit(); |
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 | case CMD_FORCE_REEVALUATION: |
| 288 | if (DBG) log("Forcing reevaluation"); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 289 | mUidResponsibleForReeval = message.arg1; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 290 | transitionTo(mEvaluatingState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 291 | return HANDLED; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 292 | case CMD_CAPTIVE_PORTAL_APP_FINISHED: |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 293 | if (!mCaptivePortalLoggedInResponseToken.equals((String)message.obj)) |
| 294 | return HANDLED; |
| 295 | // Previous token was sent out, come up with a new one. |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 296 | mCaptivePortalLoggedInResponseToken = String.valueOf(new Random().nextLong()); |
| 297 | switch (message.arg1) { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 298 | case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_DISMISSED: |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 299 | sendMessage(CMD_FORCE_REEVALUATION, 0 /* no UID */, 0); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 300 | break; |
| 301 | case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS: |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 302 | mDontDisplaySigninNotification = true; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 303 | // TODO: Distinguish this from a network that actually validates. |
| 304 | // Displaying the "!" on the system UI icon may still be a good idea. |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 305 | transitionTo(mValidatedState); |
| 306 | break; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 307 | case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_UNWANTED: |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 308 | mDontDisplaySigninNotification = true; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 309 | mUserDoesNotWant = true; |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 310 | mConnectivityServiceHandler.sendMessage(obtainMessage( |
| 311 | EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0, |
| 312 | mNetworkAgentInfo)); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 313 | // TODO: Should teardown network. |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 314 | mUidResponsibleForReeval = 0; |
| 315 | transitionTo(mEvaluatingState); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 319 | default: |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 320 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 321 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 325 | // Being in the ValidatedState State indicates a Network is: |
| 326 | // - Successfully validated, or |
| 327 | // - Wanted "as is" by the user, or |
| 328 | // - Does not satsify the default NetworkRequest and so validation has been skipped. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 329 | private class ValidatedState extends State { |
| 330 | @Override |
| 331 | public void enter() { |
| 332 | if (DBG) log("Validated"); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 333 | mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, |
| 334 | NETWORK_TEST_RESULT_VALID, 0, mNetworkAgentInfo)); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public boolean processMessage(Message message) { |
| 339 | if (DBG) log(getName() + message.toString()); |
| 340 | switch (message.what) { |
| 341 | case CMD_NETWORK_CONNECTED: |
| 342 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 343 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 344 | default: |
| 345 | return NOT_HANDLED; |
| 346 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 350 | // Being in the MaybeNotifyState State indicates the user may have been notified that sign-in |
| 351 | // is required. This State takes care to clear the notification upon exit from the State. |
| 352 | private class MaybeNotifyState extends State { |
| 353 | @Override |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 354 | public boolean processMessage(Message message) { |
| 355 | if (DBG) log(getName() + message.toString()); |
| 356 | switch (message.what) { |
| 357 | case CMD_LAUNCH_CAPTIVE_PORTAL_APP: |
| 358 | final Intent intent = new Intent( |
| 359 | ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN); |
| 360 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, mNetworkAgentInfo.network); |
| 361 | intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_TOKEN, |
| 362 | mCaptivePortalLoggedInResponseToken); |
| 363 | intent.setFlags( |
| 364 | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 365 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 366 | return HANDLED; |
| 367 | default: |
| 368 | return NOT_HANDLED; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | @Override |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 373 | public void exit() { |
| 374 | Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 0, |
| 375 | mNetworkAgentInfo.network.netId, null); |
| 376 | mConnectivityServiceHandler.sendMessage(message); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Being in the EvaluatingState State indicates the Network is being evaluated for internet |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 381 | // connectivity, or that the user has indicated that this network is unwanted. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 382 | private class EvaluatingState extends State { |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 383 | private int mReevaluateDelayMs; |
| 384 | private int mAttempts; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 385 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 386 | @Override |
| 387 | public void enter() { |
| 388 | sendMessage(CMD_REEVALUATE, ++mReevaluateToken, 0); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 389 | if (mUidResponsibleForReeval != INVALID_UID) { |
| 390 | TrafficStats.setThreadStatsUid(mUidResponsibleForReeval); |
| 391 | mUidResponsibleForReeval = INVALID_UID; |
| 392 | } |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 393 | mReevaluateDelayMs = INITIAL_REEVALUATE_DELAY_MS; |
| 394 | mAttempts = 0; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public boolean processMessage(Message message) { |
| 399 | if (DBG) log(getName() + message.toString()); |
| 400 | switch (message.what) { |
| 401 | case CMD_REEVALUATE: |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 402 | if (message.arg1 != mReevaluateToken || mUserDoesNotWant) |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 403 | return HANDLED; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 404 | // Don't bother validating networks that don't satisify the default request. |
| 405 | // This includes: |
| 406 | // - VPNs which can be considered explicitly desired by the user and the |
| 407 | // user's desire trumps whether the network validates. |
| 408 | // - Networks that don't provide internet access. It's unclear how to |
| 409 | // validate such networks. |
| 410 | // - Untrusted networks. It's unsafe to prompt the user to sign-in to |
| 411 | // such networks and the user didn't express interest in connecting to |
| 412 | // such networks (an app did) so the user may be unhappily surprised when |
| 413 | // asked to sign-in to a network they didn't want to connect to in the |
| 414 | // first place. Validation could be done to adjust the network scores |
| 415 | // however these networks are app-requested and may not be intended for |
| 416 | // general usage, in which case general validation may not be an accurate |
| 417 | // measure of the network's quality. Only the app knows how to evaluate |
| 418 | // the network so don't bother validating here. Furthermore sending HTTP |
| 419 | // packets over the network may be undesirable, for example an extremely |
| 420 | // expensive metered network, or unwanted leaking of the User Agent string. |
| 421 | if (!mDefaultRequest.networkCapabilities.satisfiedByNetworkCapabilities( |
| 422 | mNetworkAgentInfo.networkCapabilities)) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 423 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 424 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 425 | } |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 426 | mAttempts++; |
Lorenzo Colitti | 351bfad | 2015-01-22 22:36:50 +0900 | [diff] [blame] | 427 | // Note: This call to isCaptivePortal() could take up to a minute. Resolving the |
| 428 | // server's IP addresses could hit the DNS timeout, and attempting connections |
| 429 | // to each of the server's several IP addresses (currently one IPv4 and one |
| 430 | // IPv6) could each take SOCKET_TIMEOUT_MS. During this time this StateMachine |
| 431 | // will be unresponsive. isCaptivePortal() could be executed on another Thread |
| 432 | // if this is found to cause problems. |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 433 | int httpResponseCode = isCaptivePortal(); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 434 | if (httpResponseCode == 204) { |
| 435 | transitionTo(mValidatedState); |
| 436 | } else if (httpResponseCode >= 200 && httpResponseCode <= 399) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 437 | transitionTo(mCaptivePortalState); |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 438 | } else { |
Paul Jensen | d9be23f | 2015-05-19 14:51:47 -0400 | [diff] [blame] | 439 | final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 440 | sendMessageDelayed(msg, mReevaluateDelayMs); |
Paul Jensen | d9be23f | 2015-05-19 14:51:47 -0400 | [diff] [blame] | 441 | mConnectivityServiceHandler.sendMessage(obtainMessage( |
| 442 | EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0, |
| 443 | mNetworkAgentInfo)); |
| 444 | if (mAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) { |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 445 | // Don't continue to blame UID forever. |
| 446 | TrafficStats.clearThreadStatsUid(); |
| 447 | } |
| 448 | mReevaluateDelayMs *= 2; |
| 449 | if (mReevaluateDelayMs > MAX_REEVALUATE_DELAY_MS) { |
| 450 | mReevaluateDelayMs = MAX_REEVALUATE_DELAY_MS; |
| 451 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 452 | } |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 453 | return HANDLED; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 454 | case CMD_FORCE_REEVALUATION: |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 455 | // Before IGNORE_REEVALUATE_ATTEMPTS attempts are made, |
| 456 | // ignore any re-evaluation requests. After, restart the |
| 457 | // evaluation process via EvaluatingState#enter. |
| 458 | return mAttempts < IGNORE_REEVALUATE_ATTEMPTS ? HANDLED : NOT_HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 459 | default: |
| 460 | return NOT_HANDLED; |
| 461 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 462 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 463 | |
| 464 | @Override |
| 465 | public void exit() { |
| 466 | TrafficStats.clearThreadStatsUid(); |
| 467 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 470 | // BroadcastReceiver that waits for a particular Intent and then posts a message. |
| 471 | private class CustomIntentReceiver extends BroadcastReceiver { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 472 | private final int mToken; |
| 473 | private final int mWhat; |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 474 | private final String mAction; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 475 | CustomIntentReceiver(String action, int token, int what) { |
| 476 | mToken = token; |
| 477 | mWhat = what; |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 478 | mAction = action + "_" + mNetworkAgentInfo.network.netId + "_" + token; |
| 479 | mContext.registerReceiver(this, new IntentFilter(mAction)); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 480 | } |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 481 | public PendingIntent getPendingIntent() { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 482 | final Intent intent = new Intent(mAction); |
| 483 | intent.setPackage(mContext.getPackageName()); |
| 484 | return PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 485 | } |
| 486 | @Override |
| 487 | public void onReceive(Context context, Intent intent) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 488 | if (intent.getAction().equals(mAction)) sendMessage(obtainMessage(mWhat, mToken)); |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 489 | } |
| 490 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 491 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 492 | // Being in the CaptivePortalState State indicates a captive portal was detected and the user |
| 493 | // has been shown a notification to sign-in. |
| 494 | private class CaptivePortalState extends State { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 495 | private static final String ACTION_LAUNCH_CAPTIVE_PORTAL_APP = |
| 496 | "android.net.netmon.launchCaptivePortalApp"; |
| 497 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 498 | @Override |
| 499 | public void enter() { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 500 | mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, |
| 501 | NETWORK_TEST_RESULT_INVALID, 0, mNetworkAgentInfo)); |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 502 | // Don't annoy user with sign-in notifications. |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 503 | if (mDontDisplaySigninNotification) return; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 504 | // Create a CustomIntentReceiver that sends us a |
| 505 | // CMD_LAUNCH_CAPTIVE_PORTAL_APP message when the user |
| 506 | // touches the notification. |
| 507 | if (mLaunchCaptivePortalAppBroadcastReceiver == null) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 508 | // Wait for result. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 509 | mLaunchCaptivePortalAppBroadcastReceiver = new CustomIntentReceiver( |
| 510 | ACTION_LAUNCH_CAPTIVE_PORTAL_APP, new Random().nextInt(), |
| 511 | CMD_LAUNCH_CAPTIVE_PORTAL_APP); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 512 | } |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 513 | // Display the sign in notification. |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 514 | Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 1, |
| 515 | mNetworkAgentInfo.network.netId, |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 516 | mLaunchCaptivePortalAppBroadcastReceiver.getPendingIntent()); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 517 | mConnectivityServiceHandler.sendMessage(message); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public boolean processMessage(Message message) { |
| 522 | if (DBG) log(getName() + message.toString()); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 523 | return NOT_HANDLED; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 524 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 525 | } |
| 526 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 527 | // Being in the LingeringState State indicates a Network's validated bit is true and it once |
| 528 | // was the highest scoring Network satisfying a particular NetworkRequest, but since then |
| 529 | // another Network satsified the NetworkRequest with a higher score and hence this Network |
| 530 | // is "lingered" for a fixed period of time before it is disconnected. This period of time |
| 531 | // allows apps to wrap up communication and allows for seamless reactivation if the other |
| 532 | // higher scoring Network happens to disconnect. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 533 | private class LingeringState extends State { |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 534 | private static final String ACTION_LINGER_EXPIRED = "android.net.netmon.lingerExpired"; |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 535 | |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 536 | private CustomIntentReceiver mBroadcastReceiver; |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 537 | private PendingIntent mIntent; |
| 538 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 539 | @Override |
| 540 | public void enter() { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 541 | mLingerToken = new Random().nextInt(); |
| 542 | mBroadcastReceiver = new CustomIntentReceiver(ACTION_LINGER_EXPIRED, mLingerToken, |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 543 | CMD_LINGER_EXPIRED); |
| 544 | mIntent = mBroadcastReceiver.getPendingIntent(); |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 545 | long wakeupTime = SystemClock.elapsedRealtime() + mLingerDelayMs; |
| 546 | mAlarmManager.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime, |
| 547 | // Give a specific window so we aren't subject to unknown inexactitude. |
| 548 | mLingerDelayMs / 6, mIntent); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | @Override |
| 552 | public boolean processMessage(Message message) { |
| 553 | if (DBG) log(getName() + message.toString()); |
| 554 | switch (message.what) { |
| 555 | case CMD_NETWORK_CONNECTED: |
| 556 | // Go straight to active as we've already evaluated. |
| 557 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 558 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 559 | case CMD_LINGER_EXPIRED: |
| 560 | if (message.arg1 != mLingerToken) |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 561 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 562 | mConnectivityServiceHandler.sendMessage( |
| 563 | obtainMessage(EVENT_NETWORK_LINGER_COMPLETE, mNetworkAgentInfo)); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 564 | return HANDLED; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 565 | case CMD_FORCE_REEVALUATION: |
| 566 | // Ignore reevaluation attempts when lingering. A reevaluation could result |
| 567 | // in a transition to the validated state which would abort the linger |
| 568 | // timeout. Lingering is the result of score assessment; validity is |
| 569 | // irrelevant. |
| 570 | return HANDLED; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 571 | case CMD_CAPTIVE_PORTAL_APP_FINISHED: |
| 572 | // Ignore user network determination as this could abort linger timeout. |
| 573 | // Networks are only lingered once validated because: |
| 574 | // - Unvalidated networks are never lingered (see rematchNetworkAndRequests). |
| 575 | // - Once validated, a Network's validated bit is never cleared. |
| 576 | // Since networks are only lingered after being validated a user's |
| 577 | // determination will not change the death sentence that lingering entails: |
| 578 | // - If the user wants to use the network or bypasses the captive portal, |
| 579 | // the network's score will not be increased beyond its current value |
| 580 | // because it is already validated. Without a score increase there is no |
| 581 | // chance of reactivation (i.e. aborting linger timeout). |
| 582 | // - If the user does not want the network, lingering will disconnect the |
| 583 | // network anyhow. |
| 584 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 585 | default: |
| 586 | return NOT_HANDLED; |
| 587 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 588 | } |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 589 | |
| 590 | @Override |
| 591 | public void exit() { |
| 592 | mAlarmManager.cancel(mIntent); |
| 593 | mContext.unregisterReceiver(mBroadcastReceiver); |
| 594 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Do a URL fetch on a known server to see if we get the data we expect. |
| 599 | * Returns HTTP response code. |
| 600 | */ |
| 601 | private int isCaptivePortal() { |
| 602 | if (!mIsCaptivePortalCheckEnabled) return 204; |
| 603 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 604 | HttpURLConnection urlConnection = null; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 605 | int httpResponseCode = 599; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 606 | try { |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 607 | URL url = new URL("http", mServer, "/generate_204"); |
Paul Jensen | 8fe1742 | 2015-02-02 11:03:03 -0500 | [diff] [blame] | 608 | // On networks with a PAC instead of fetching a URL that should result in a 204 |
| 609 | // reponse, we instead simply fetch the PAC script. This is done for a few reasons: |
| 610 | // 1. At present our PAC code does not yet handle multiple PACs on multiple networks |
| 611 | // until something like https://android-review.googlesource.com/#/c/115180/ lands. |
| 612 | // Network.openConnection() will ignore network-specific PACs and instead fetch |
| 613 | // using NO_PROXY. If a PAC is in place, the only fetch we know will succeed with |
| 614 | // NO_PROXY is the fetch of the PAC itself. |
| 615 | // 2. To proxy the generate_204 fetch through a PAC would require a number of things |
| 616 | // happen before the fetch can commence, namely: |
| 617 | // a) the PAC script be fetched |
| 618 | // b) a PAC script resolver service be fired up and resolve mServer |
| 619 | // Network validation could be delayed until these prerequisities are satisifed or |
| 620 | // could simply be left to race them. Neither is an optimal solution. |
| 621 | // 3. PAC scripts are sometimes used to block or restrict Internet access and may in |
| 622 | // fact block fetching of the generate_204 URL which would lead to false negative |
| 623 | // results for network validation. |
| 624 | boolean fetchPac = false; |
| 625 | { |
| 626 | final ProxyInfo proxyInfo = mNetworkAgentInfo.linkProperties.getHttpProxy(); |
| 627 | if (proxyInfo != null && !Uri.EMPTY.equals(proxyInfo.getPacFileUrl())) { |
| 628 | url = new URL(proxyInfo.getPacFileUrl().toString()); |
| 629 | fetchPac = true; |
| 630 | } |
| 631 | } |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 632 | if (DBG) { |
| 633 | log("Checking " + url.toString() + " on " + |
| 634 | mNetworkAgentInfo.networkInfo.getExtraInfo()); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 635 | } |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 636 | urlConnection = (HttpURLConnection) mNetworkAgentInfo.network.openConnection(url); |
Paul Jensen | 8fe1742 | 2015-02-02 11:03:03 -0500 | [diff] [blame] | 637 | urlConnection.setInstanceFollowRedirects(fetchPac); |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 638 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 639 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 640 | urlConnection.setUseCaches(false); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 641 | |
| 642 | // Time how long it takes to get a response to our request |
| 643 | long requestTimestamp = SystemClock.elapsedRealtime(); |
| 644 | |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 645 | urlConnection.getInputStream(); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 646 | |
| 647 | // Time how long it takes to get a response to our request |
| 648 | long responseTimestamp = SystemClock.elapsedRealtime(); |
| 649 | |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 650 | httpResponseCode = urlConnection.getResponseCode(); |
| 651 | if (DBG) { |
| 652 | log("isCaptivePortal: ret=" + httpResponseCode + |
| 653 | " headers=" + urlConnection.getHeaderFields()); |
| 654 | } |
| 655 | // NOTE: We may want to consider an "HTTP/1.0 204" response to be a captive |
| 656 | // portal. The only example of this seen so far was a captive portal. For |
| 657 | // the time being go with prior behavior of assuming it's not a captive |
| 658 | // portal. If it is considered a captive portal, a different sign-in URL |
| 659 | // is needed (i.e. can't browse a 204). This could be the result of an HTTP |
| 660 | // proxy server. |
| 661 | |
| 662 | // Consider 200 response with "Content-length=0" to not be a captive portal. |
| 663 | // There's no point in considering this a captive portal as the user cannot |
| 664 | // sign-in to an empty page. Probably the result of a broken transparent proxy. |
| 665 | // See http://b/9972012. |
| 666 | if (httpResponseCode == 200 && urlConnection.getContentLength() == 0) { |
| 667 | if (DBG) log("Empty 200 response interpreted as 204 response."); |
| 668 | httpResponseCode = 204; |
| 669 | } |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 670 | |
Paul Jensen | 8fe1742 | 2015-02-02 11:03:03 -0500 | [diff] [blame] | 671 | if (httpResponseCode == 200 && fetchPac) { |
| 672 | if (DBG) log("PAC fetch 200 response interpreted as 204 response."); |
| 673 | httpResponseCode = 204; |
| 674 | } |
| 675 | |
Jeff Davidson | f9fff92 | 2014-12-05 16:56:08 -0800 | [diff] [blame] | 676 | sendNetworkConditionsBroadcast(true /* response received */, |
| 677 | httpResponseCode != 204 /* isCaptivePortal */, |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 678 | requestTimestamp, responseTimestamp); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 679 | } catch (IOException e) { |
| 680 | if (DBG) log("Probably not a portal: exception " + e); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 681 | if (httpResponseCode == 599) { |
| 682 | // TODO: Ping gateway and DNS server and log results. |
| 683 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 684 | } finally { |
| 685 | if (urlConnection != null) { |
| 686 | urlConnection.disconnect(); |
| 687 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 688 | } |
| 689 | return httpResponseCode; |
| 690 | } |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 691 | |
| 692 | /** |
| 693 | * @param responseReceived - whether or not we received a valid HTTP response to our request. |
| 694 | * If false, isCaptivePortal and responseTimestampMs are ignored |
| 695 | * TODO: This should be moved to the transports. The latency could be passed to the transports |
| 696 | * along with the captive portal result. Currently the TYPE_MOBILE broadcasts appear unused so |
| 697 | * perhaps this could just be added to the WiFi transport only. |
| 698 | */ |
| 699 | private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal, |
| 700 | long requestTimestampMs, long responseTimestampMs) { |
| 701 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 702 | Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 0) { |
| 703 | if (DBG) log("Don't send network conditions - lacking user consent."); |
| 704 | return; |
| 705 | } |
| 706 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 707 | if (systemReady == false) return; |
| 708 | |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 709 | Intent latencyBroadcast = new Intent(ACTION_NETWORK_CONDITIONS_MEASURED); |
| 710 | switch (mNetworkAgentInfo.networkInfo.getType()) { |
| 711 | case ConnectivityManager.TYPE_WIFI: |
| 712 | WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo(); |
| 713 | if (currentWifiInfo != null) { |
| 714 | // NOTE: getSSID()'s behavior changed in API 17; before that, SSIDs were not |
| 715 | // surrounded by double quotation marks (thus violating the Javadoc), but this |
| 716 | // was changed to match the Javadoc in API 17. Since clients may have started |
| 717 | // sanitizing the output of this method since API 17 was released, we should |
| 718 | // not change it here as it would become impossible to tell whether the SSID is |
| 719 | // simply being surrounded by quotes due to the API, or whether those quotes |
| 720 | // are actually part of the SSID. |
| 721 | latencyBroadcast.putExtra(EXTRA_SSID, currentWifiInfo.getSSID()); |
| 722 | latencyBroadcast.putExtra(EXTRA_BSSID, currentWifiInfo.getBSSID()); |
| 723 | } else { |
| 724 | if (DBG) logw("network info is TYPE_WIFI but no ConnectionInfo found"); |
| 725 | return; |
| 726 | } |
| 727 | break; |
| 728 | case ConnectivityManager.TYPE_MOBILE: |
| 729 | latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType()); |
| 730 | List<CellInfo> info = mTelephonyManager.getAllCellInfo(); |
| 731 | if (info == null) return; |
| 732 | int numRegisteredCellInfo = 0; |
| 733 | for (CellInfo cellInfo : info) { |
| 734 | if (cellInfo.isRegistered()) { |
| 735 | numRegisteredCellInfo++; |
| 736 | if (numRegisteredCellInfo > 1) { |
| 737 | if (DBG) log("more than one registered CellInfo. Can't " + |
| 738 | "tell which is active. Bailing."); |
| 739 | return; |
| 740 | } |
| 741 | if (cellInfo instanceof CellInfoCdma) { |
| 742 | CellIdentityCdma cellId = ((CellInfoCdma) cellInfo).getCellIdentity(); |
| 743 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 744 | } else if (cellInfo instanceof CellInfoGsm) { |
| 745 | CellIdentityGsm cellId = ((CellInfoGsm) cellInfo).getCellIdentity(); |
| 746 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 747 | } else if (cellInfo instanceof CellInfoLte) { |
| 748 | CellIdentityLte cellId = ((CellInfoLte) cellInfo).getCellIdentity(); |
| 749 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 750 | } else if (cellInfo instanceof CellInfoWcdma) { |
| 751 | CellIdentityWcdma cellId = ((CellInfoWcdma) cellInfo).getCellIdentity(); |
| 752 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 753 | } else { |
| 754 | if (DBG) logw("Registered cellinfo is unrecognized"); |
| 755 | return; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | break; |
| 760 | default: |
| 761 | return; |
| 762 | } |
| 763 | latencyBroadcast.putExtra(EXTRA_CONNECTIVITY_TYPE, mNetworkAgentInfo.networkInfo.getType()); |
| 764 | latencyBroadcast.putExtra(EXTRA_RESPONSE_RECEIVED, responseReceived); |
| 765 | latencyBroadcast.putExtra(EXTRA_REQUEST_TIMESTAMP_MS, requestTimestampMs); |
| 766 | |
| 767 | if (responseReceived) { |
| 768 | latencyBroadcast.putExtra(EXTRA_IS_CAPTIVE_PORTAL, isCaptivePortal); |
| 769 | latencyBroadcast.putExtra(EXTRA_RESPONSE_TIMESTAMP_MS, responseTimestampMs); |
| 770 | } |
Paul Jensen | 5529858 | 2014-08-20 11:01:41 -0400 | [diff] [blame] | 771 | mContext.sendBroadcastAsUser(latencyBroadcast, UserHandle.CURRENT, |
| 772 | PERMISSION_ACCESS_NETWORK_CONDITIONS); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 773 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 774 | } |