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