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