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