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