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