blob: fc50e2c0179254a6281548b107ec1715cb31ad6a [file] [log] [blame]
Paul Jensenca8f16a2014-05-09 12:47:55 -04001/*
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
17package com.android.server.connectivity;
18
Paul Jensen79a08052014-08-21 12:44:07 -040019import android.app.AlarmManager;
Paul Jensen869868be2014-05-15 10:33:05 -040020import android.app.PendingIntent;
21import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Paul Jensenca8f16a2014-05-09 12:47:55 -040023import android.content.Context;
Paul Jensen869868be2014-05-15 10:33:05 -040024import android.content.Intent;
25import android.content.IntentFilter;
26import android.net.ConnectivityManager;
Paul Jensen2c311d62014-11-17 12:34:51 -050027import android.net.NetworkRequest;
Paul Jensen8fe17422015-02-02 11:03:03 -050028import android.net.ProxyInfo;
Paul Jensen7ccd3df2014-08-29 09:54:01 -040029import android.net.TrafficStats;
Paul Jensen71b645f2014-10-13 14:13:07 -040030import android.net.Uri;
Paul Jensen306f1a42014-08-04 10:59:01 -040031import android.net.wifi.WifiInfo;
32import android.net.wifi.WifiManager;
Paul Jensenca8f16a2014-05-09 12:47:55 -040033import android.os.Handler;
34import android.os.Message;
Paul Jensend7b6ca92015-05-13 14:05:12 -040035import android.os.Process;
Paul Jensen306f1a42014-08-04 10:59:01 -040036import android.os.SystemClock;
Paul Jensenca8f16a2014-05-09 12:47:55 -040037import android.os.SystemProperties;
Paul Jensen869868be2014-05-15 10:33:05 -040038import android.os.UserHandle;
Paul Jensenca8f16a2014-05-09 12:47:55 -040039import android.provider.Settings;
Paul Jensen306f1a42014-08-04 10:59:01 -040040import android.telephony.CellIdentityCdma;
41import android.telephony.CellIdentityGsm;
42import android.telephony.CellIdentityLte;
43import android.telephony.CellIdentityWcdma;
44import android.telephony.CellInfo;
45import android.telephony.CellInfoCdma;
46import android.telephony.CellInfoGsm;
47import android.telephony.CellInfoLte;
48import android.telephony.CellInfoWcdma;
49import android.telephony.TelephonyManager;
Paul Jensen532b61432014-11-10 09:50:02 -050050import android.util.Log;
Paul Jensenca8f16a2014-05-09 12:47:55 -040051
Paul Jensend7b6ca92015-05-13 14:05:12 -040052import com.android.internal.annotations.VisibleForTesting;
Paul Jensenca8f16a2014-05-09 12:47:55 -040053import com.android.internal.util.Protocol;
54import com.android.internal.util.State;
55import com.android.internal.util.StateMachine;
56import com.android.server.connectivity.NetworkAgentInfo;
57
Paul Jensenca8f16a2014-05-09 12:47:55 -040058import java.io.IOException;
Paul Jensenca8f16a2014-05-09 12:47:55 -040059import java.net.HttpURLConnection;
Paul Jensenca8f16a2014-05-09 12:47:55 -040060import java.net.URL;
Paul Jensen306f1a42014-08-04 10:59:01 -040061import java.util.List;
Paul Jensen71b645f2014-10-13 14:13:07 -040062import java.util.Random;
Paul Jensenca8f16a2014-05-09 12:47:55 -040063
64/**
65 * {@hide}
66 */
67public class NetworkMonitor extends StateMachine {
68 private static final boolean DBG = true;
69 private static final String TAG = "NetworkMonitor";
Lorenzo Colitticd29cb62015-01-14 00:16:03 +090070 private static final String DEFAULT_SERVER = "connectivitycheck.android.com";
Paul Jensenca8f16a2014-05-09 12:47:55 -040071 private static final int SOCKET_TIMEOUT_MS = 10000;
Paul Jensen306f1a42014-08-04 10:59:01 -040072 public static final String ACTION_NETWORK_CONDITIONS_MEASURED =
73 "android.net.conn.NETWORK_CONDITIONS_MEASURED";
74 public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type";
75 public static final String EXTRA_NETWORK_TYPE = "extra_network_type";
76 public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received";
77 public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal";
78 public static final String EXTRA_CELL_ID = "extra_cellid";
79 public static final String EXTRA_SSID = "extra_ssid";
80 public static final String EXTRA_BSSID = "extra_bssid";
81 /** real time since boot */
82 public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms";
83 public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms";
84
85 private static final String PERMISSION_ACCESS_NETWORK_CONDITIONS =
86 "android.permission.ACCESS_NETWORK_CONDITIONS";
Paul Jensenca8f16a2014-05-09 12:47:55 -040087
Paul Jensenad50a1f2014-09-05 12:06:44 -040088 // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED.
89 // The network should be used as a default internet connection. It was found to be:
90 // 1. a functioning network providing internet access, or
91 // 2. a captive portal and the user decided to use it as is.
92 public static final int NETWORK_TEST_RESULT_VALID = 0;
93 // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED.
94 // The network should not be used as a default internet connection. It was found to be:
95 // 1. a captive portal and the user is prompted to sign-in, or
96 // 2. a captive portal and the user did not want to use it, or
97 // 3. a broken network (e.g. DNS failed, connect failed, HTTP request failed).
98 public static final int NETWORK_TEST_RESULT_INVALID = 1;
99
Paul Jensenca8f16a2014-05-09 12:47:55 -0400100 private static final int BASE = Protocol.BASE_NETWORK_MONITOR;
101
102 /**
103 * Inform NetworkMonitor that their network is connected.
104 * Initiates Network Validation.
105 */
106 public static final int CMD_NETWORK_CONNECTED = BASE + 1;
107
108 /**
Paul Jensenad50a1f2014-09-05 12:06:44 -0400109 * Inform ConnectivityService that the network has been tested.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400110 * obj = NetworkAgentInfo
Paul Jensenad50a1f2014-09-05 12:06:44 -0400111 * arg1 = One of the NETWORK_TESTED_RESULT_* constants.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400112 */
Paul Jensenad50a1f2014-09-05 12:06:44 -0400113 public static final int EVENT_NETWORK_TESTED = BASE + 2;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400114
115 /**
116 * Inform NetworkMonitor to linger a network. The Monitor should
117 * start a timer and/or start watching for zero live connections while
118 * moving towards LINGER_COMPLETE. After the Linger period expires
119 * (or other events mark the end of the linger state) the LINGER_COMPLETE
120 * event should be sent and the network will be shut down. If a
121 * CMD_NETWORK_CONNECTED happens before the LINGER completes
122 * it indicates further desire to keep the network alive and so
123 * the LINGER is aborted.
124 */
125 public static final int CMD_NETWORK_LINGER = BASE + 3;
126
127 /**
128 * Message to self indicating linger delay has expired.
129 * arg1 = Token to ignore old messages.
130 */
131 private static final int CMD_LINGER_EXPIRED = BASE + 4;
132
133 /**
134 * Inform ConnectivityService that the network LINGER period has
135 * expired.
136 * obj = NetworkAgentInfo
137 */
138 public static final int EVENT_NETWORK_LINGER_COMPLETE = BASE + 5;
139
140 /**
Paul Jensenca8f16a2014-05-09 12:47:55 -0400141 * Message to self indicating it's time to evaluate a network's connectivity.
142 * arg1 = Token to ignore old messages.
143 */
Paul Jensen869868be2014-05-15 10:33:05 -0400144 private static final int CMD_REEVALUATE = BASE + 6;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400145
146 /**
Paul Jensenca8f16a2014-05-09 12:47:55 -0400147 * Inform NetworkMonitor that the network has disconnected.
148 */
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400149 public static final int CMD_NETWORK_DISCONNECTED = BASE + 7;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400150
151 /**
152 * Force evaluation even if it has succeeded in the past.
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400153 * arg1 = UID responsible for requesting this reeval. Will be billed for data.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400154 */
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400155 public static final int CMD_FORCE_REEVALUATION = BASE + 8;
Paul Jensen869868be2014-05-15 10:33:05 -0400156
157 /**
Paul Jensen71b645f2014-10-13 14:13:07 -0400158 * Message to self indicating captive portal app finished.
Paul Jensen25a217c2015-02-27 22:55:47 -0500159 * arg1 = one of: CAPTIVE_PORTAL_APP_RETURN_DISMISSED,
Paul Jensen71b645f2014-10-13 14:13:07 -0400160 * CAPTIVE_PORTAL_APP_RETURN_UNWANTED,
161 * CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS
Paul Jensen25a217c2015-02-27 22:55:47 -0500162 * obj = mCaptivePortalLoggedInResponseToken as String
Paul Jensen869868be2014-05-15 10:33:05 -0400163 */
Paul Jensen25a217c2015-02-27 22:55:47 -0500164 public static final int CMD_CAPTIVE_PORTAL_APP_FINISHED = BASE + 9;
Paul Jensen869868be2014-05-15 10:33:05 -0400165
166 /**
167 * Request ConnectivityService display provisioning notification.
168 * arg1 = Whether to make the notification visible.
Paul Jensenfdc4e4a2014-07-15 12:07:36 -0400169 * arg2 = NetID.
170 * obj = Intent to be launched when notification selected by user, null if !arg1.
Paul Jensen869868be2014-05-15 10:33:05 -0400171 */
Paul Jensen71b645f2014-10-13 14:13:07 -0400172 public static final int EVENT_PROVISIONING_NOTIFICATION = BASE + 10;
Paul Jensen869868be2014-05-15 10:33:05 -0400173
174 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500175 * Message to self indicating sign-in app should be launched.
176 * Sent by mLaunchCaptivePortalAppBroadcastReceiver when the
177 * user touches the sign in notification.
Paul Jensen869868be2014-05-15 10:33:05 -0400178 */
Paul Jensen25a217c2015-02-27 22:55:47 -0500179 private static final int CMD_LAUNCH_CAPTIVE_PORTAL_APP = BASE + 11;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400180
181 private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger";
Paul Jensend7b6ca92015-05-13 14:05:12 -0400182 // Default to 30s linger time-out. Modifyable only for testing.
183 private static int DEFAULT_LINGER_DELAY_MS = 30000;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400184 private final int mLingerDelayMs;
185 private int mLingerToken = 0;
186
Paul Jensend0491e9a2015-05-05 14:52:22 -0400187 // Start mReevaluateDelayMs at this value and double.
188 private static final int INITIAL_REEVALUATE_DELAY_MS = 1000;
189 private static final int MAX_REEVALUATE_DELAY_MS = 10*60*1000;
190 // Before network has been evaluated this many times, ignore repeated reevaluate requests.
191 private static final int IGNORE_REEVALUATE_ATTEMPTS = 5;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400192 private int mReevaluateToken = 0;
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400193 private static final int INVALID_UID = -1;
194 private int mUidResponsibleForReeval = INVALID_UID;
Paul Jensend9be23f2015-05-19 14:51:47 -0400195 // Stop blaming UID that requested re-evaluation after this many attempts.
196 private static final int BLAME_FOR_EVALUATION_ATTEMPTS = 5;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400197
198 private final Context mContext;
199 private final Handler mConnectivityServiceHandler;
200 private final NetworkAgentInfo mNetworkAgentInfo;
Paul Jensen306f1a42014-08-04 10:59:01 -0400201 private final TelephonyManager mTelephonyManager;
202 private final WifiManager mWifiManager;
Paul Jensen79a08052014-08-21 12:44:07 -0400203 private final AlarmManager mAlarmManager;
Paul Jensen2c311d62014-11-17 12:34:51 -0500204 private final NetworkRequest mDefaultRequest;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400205
206 private String mServer;
207 private boolean mIsCaptivePortalCheckEnabled = false;
208
Paul Jensenad50a1f2014-09-05 12:06:44 -0400209 // Set if the user explicitly selected "Do not use this network" in captive portal sign-in app.
210 private boolean mUserDoesNotWant = false;
Paul Jensen700f2362015-05-05 14:56:10 -0400211 // Avoids surfacing "Sign in to network" notification.
212 private boolean mDontDisplaySigninNotification = false;
Paul Jensenad50a1f2014-09-05 12:06:44 -0400213
Robert Greenwaltfb68f8f2014-08-13 13:43:32 -0700214 public boolean systemReady = false;
215
Paul Jensen71b645f2014-10-13 14:13:07 -0400216 private final State mDefaultState = new DefaultState();
Paul Jensen71b645f2014-10-13 14:13:07 -0400217 private final State mValidatedState = new ValidatedState();
218 private final State mMaybeNotifyState = new MaybeNotifyState();
219 private final State mEvaluatingState = new EvaluatingState();
220 private final State mCaptivePortalState = new CaptivePortalState();
221 private final State mLingeringState = new LingeringState();
222
Paul Jensen25a217c2015-02-27 22:55:47 -0500223 private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null;
Paul Jensen71b645f2014-10-13 14:13:07 -0400224 private String mCaptivePortalLoggedInResponseToken = null;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400225
Paul Jensen2c311d62014-11-17 12:34:51 -0500226 public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo,
227 NetworkRequest defaultRequest) {
Paul Jensenca8f16a2014-05-09 12:47:55 -0400228 // Add suffix indicating which NetworkMonitor we're talking about.
229 super(TAG + networkAgentInfo.name());
230
231 mContext = context;
232 mConnectivityServiceHandler = handler;
233 mNetworkAgentInfo = networkAgentInfo;
Paul Jensen306f1a42014-08-04 10:59:01 -0400234 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
235 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Paul Jensen79a08052014-08-21 12:44:07 -0400236 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Paul Jensen2c311d62014-11-17 12:34:51 -0500237 mDefaultRequest = defaultRequest;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400238
239 addState(mDefaultState);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400240 addState(mValidatedState, mDefaultState);
Paul Jensen71b645f2014-10-13 14:13:07 -0400241 addState(mMaybeNotifyState, mDefaultState);
242 addState(mEvaluatingState, mMaybeNotifyState);
243 addState(mCaptivePortalState, mMaybeNotifyState);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400244 addState(mLingeringState, mDefaultState);
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700245 setInitialState(mDefaultState);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400246
247 mServer = Settings.Global.getString(mContext.getContentResolver(),
248 Settings.Global.CAPTIVE_PORTAL_SERVER);
249 if (mServer == null) mServer = DEFAULT_SERVER;
250
251 mLingerDelayMs = SystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400252
Paul Jensen869868be2014-05-15 10:33:05 -0400253 mIsCaptivePortalCheckEnabled = Settings.Global.getInt(mContext.getContentResolver(),
254 Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 1) == 1;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400255
Paul Jensen71b645f2014-10-13 14:13:07 -0400256 mCaptivePortalLoggedInResponseToken = String.valueOf(new Random().nextLong());
257
Paul Jensenca8f16a2014-05-09 12:47:55 -0400258 start();
259 }
260
Paul Jensen532b61432014-11-10 09:50:02 -0500261 @Override
262 protected void log(String s) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400263 Log.d(TAG + "/" + mNetworkAgentInfo.name(), s);
Paul Jensen532b61432014-11-10 09:50:02 -0500264 }
265
Paul Jensen71b645f2014-10-13 14:13:07 -0400266 // DefaultState is the parent of all States. It exists only to handle CMD_* messages but
267 // does not entail any real state (hence no enter() or exit() routines).
Paul Jensenca8f16a2014-05-09 12:47:55 -0400268 private class DefaultState extends State {
269 @Override
270 public boolean processMessage(Message message) {
271 if (DBG) log(getName() + message.toString());
272 switch (message.what) {
273 case CMD_NETWORK_LINGER:
274 if (DBG) log("Lingering");
275 transitionTo(mLingeringState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400276 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400277 case CMD_NETWORK_CONNECTED:
278 if (DBG) log("Connected");
279 transitionTo(mEvaluatingState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400280 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400281 case CMD_NETWORK_DISCONNECTED:
Robert Greenwalt1fd9aee2014-07-17 16:11:38 -0700282 if (DBG) log("Disconnected - quitting");
Paul Jensen25a217c2015-02-27 22:55:47 -0500283 if (mLaunchCaptivePortalAppBroadcastReceiver != null) {
284 mContext.unregisterReceiver(mLaunchCaptivePortalAppBroadcastReceiver);
285 mLaunchCaptivePortalAppBroadcastReceiver = null;
Paul Jensen71b645f2014-10-13 14:13:07 -0400286 }
Robert Greenwalt1fd9aee2014-07-17 16:11:38 -0700287 quit();
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400288 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400289 case CMD_FORCE_REEVALUATION:
290 if (DBG) log("Forcing reevaluation");
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400291 mUidResponsibleForReeval = message.arg1;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400292 transitionTo(mEvaluatingState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400293 return HANDLED;
Paul Jensen71b645f2014-10-13 14:13:07 -0400294 case CMD_CAPTIVE_PORTAL_APP_FINISHED:
Paul Jensen25a217c2015-02-27 22:55:47 -0500295 if (!mCaptivePortalLoggedInResponseToken.equals((String)message.obj))
296 return HANDLED;
297 // Previous token was sent out, come up with a new one.
Paul Jensen71b645f2014-10-13 14:13:07 -0400298 mCaptivePortalLoggedInResponseToken = String.valueOf(new Random().nextLong());
299 switch (message.arg1) {
Paul Jensen25a217c2015-02-27 22:55:47 -0500300 case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_DISMISSED:
Paul Jensend0491e9a2015-05-05 14:52:22 -0400301 sendMessage(CMD_FORCE_REEVALUATION, 0 /* no UID */, 0);
Paul Jensen25a217c2015-02-27 22:55:47 -0500302 break;
303 case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS:
Paul Jensen700f2362015-05-05 14:56:10 -0400304 mDontDisplaySigninNotification = true;
Paul Jensen25a217c2015-02-27 22:55:47 -0500305 // TODO: Distinguish this from a network that actually validates.
306 // Displaying the "!" on the system UI icon may still be a good idea.
Paul Jensen71b645f2014-10-13 14:13:07 -0400307 transitionTo(mValidatedState);
308 break;
Paul Jensen25a217c2015-02-27 22:55:47 -0500309 case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_UNWANTED:
Paul Jensen700f2362015-05-05 14:56:10 -0400310 mDontDisplaySigninNotification = true;
Paul Jensen71b645f2014-10-13 14:13:07 -0400311 mUserDoesNotWant = true;
Paul Jensend0491e9a2015-05-05 14:52:22 -0400312 mConnectivityServiceHandler.sendMessage(obtainMessage(
313 EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0,
314 mNetworkAgentInfo));
Paul Jensen71b645f2014-10-13 14:13:07 -0400315 // TODO: Should teardown network.
Paul Jensend0491e9a2015-05-05 14:52:22 -0400316 mUidResponsibleForReeval = 0;
317 transitionTo(mEvaluatingState);
Paul Jensen71b645f2014-10-13 14:13:07 -0400318 break;
319 }
320 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400321 default:
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400322 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400323 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400324 }
325 }
326
Paul Jensen71b645f2014-10-13 14:13:07 -0400327 // Being in the ValidatedState State indicates a Network is:
328 // - Successfully validated, or
329 // - Wanted "as is" by the user, or
330 // - Does not satsify the default NetworkRequest and so validation has been skipped.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400331 private class ValidatedState extends State {
332 @Override
333 public void enter() {
334 if (DBG) log("Validated");
Paul Jensenad50a1f2014-09-05 12:06:44 -0400335 mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
336 NETWORK_TEST_RESULT_VALID, 0, mNetworkAgentInfo));
Paul Jensenca8f16a2014-05-09 12:47:55 -0400337 }
338
339 @Override
340 public boolean processMessage(Message message) {
341 if (DBG) log(getName() + message.toString());
342 switch (message.what) {
343 case CMD_NETWORK_CONNECTED:
344 transitionTo(mValidatedState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400345 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400346 default:
347 return NOT_HANDLED;
348 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400349 }
350 }
351
Paul Jensen71b645f2014-10-13 14:13:07 -0400352 // Being in the MaybeNotifyState State indicates the user may have been notified that sign-in
353 // is required. This State takes care to clear the notification upon exit from the State.
354 private class MaybeNotifyState extends State {
355 @Override
Paul Jensen25a217c2015-02-27 22:55:47 -0500356 public boolean processMessage(Message message) {
357 if (DBG) log(getName() + message.toString());
358 switch (message.what) {
359 case CMD_LAUNCH_CAPTIVE_PORTAL_APP:
360 final Intent intent = new Intent(
361 ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN);
362 intent.putExtra(ConnectivityManager.EXTRA_NETWORK, mNetworkAgentInfo.network);
363 intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_TOKEN,
364 mCaptivePortalLoggedInResponseToken);
365 intent.setFlags(
366 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
367 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
368 return HANDLED;
369 default:
370 return NOT_HANDLED;
371 }
372 }
373
374 @Override
Paul Jensen71b645f2014-10-13 14:13:07 -0400375 public void exit() {
376 Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 0,
377 mNetworkAgentInfo.network.netId, null);
378 mConnectivityServiceHandler.sendMessage(message);
379 }
380 }
381
382 // Being in the EvaluatingState State indicates the Network is being evaluated for internet
Paul Jensend0491e9a2015-05-05 14:52:22 -0400383 // connectivity, or that the user has indicated that this network is unwanted.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400384 private class EvaluatingState extends State {
Paul Jensend0491e9a2015-05-05 14:52:22 -0400385 private int mReevaluateDelayMs;
386 private int mAttempts;
Paul Jensen869868be2014-05-15 10:33:05 -0400387
Paul Jensenca8f16a2014-05-09 12:47:55 -0400388 @Override
389 public void enter() {
390 sendMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400391 if (mUidResponsibleForReeval != INVALID_UID) {
392 TrafficStats.setThreadStatsUid(mUidResponsibleForReeval);
393 mUidResponsibleForReeval = INVALID_UID;
394 }
Paul Jensend0491e9a2015-05-05 14:52:22 -0400395 mReevaluateDelayMs = INITIAL_REEVALUATE_DELAY_MS;
396 mAttempts = 0;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400397 }
398
399 @Override
400 public boolean processMessage(Message message) {
401 if (DBG) log(getName() + message.toString());
402 switch (message.what) {
403 case CMD_REEVALUATE:
Paul Jensend0491e9a2015-05-05 14:52:22 -0400404 if (message.arg1 != mReevaluateToken || mUserDoesNotWant)
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400405 return HANDLED;
Paul Jensen2c311d62014-11-17 12:34:51 -0500406 // Don't bother validating networks that don't satisify the default request.
407 // This includes:
408 // - VPNs which can be considered explicitly desired by the user and the
409 // user's desire trumps whether the network validates.
410 // - Networks that don't provide internet access. It's unclear how to
411 // validate such networks.
412 // - Untrusted networks. It's unsafe to prompt the user to sign-in to
413 // such networks and the user didn't express interest in connecting to
414 // such networks (an app did) so the user may be unhappily surprised when
415 // asked to sign-in to a network they didn't want to connect to in the
416 // first place. Validation could be done to adjust the network scores
417 // however these networks are app-requested and may not be intended for
418 // general usage, in which case general validation may not be an accurate
419 // measure of the network's quality. Only the app knows how to evaluate
420 // the network so don't bother validating here. Furthermore sending HTTP
421 // packets over the network may be undesirable, for example an extremely
422 // expensive metered network, or unwanted leaking of the User Agent string.
423 if (!mDefaultRequest.networkCapabilities.satisfiedByNetworkCapabilities(
424 mNetworkAgentInfo.networkCapabilities)) {
Paul Jensenca8f16a2014-05-09 12:47:55 -0400425 transitionTo(mValidatedState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400426 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400427 }
Paul Jensend0491e9a2015-05-05 14:52:22 -0400428 mAttempts++;
Lorenzo Colitti351bfad2015-01-22 22:36:50 +0900429 // Note: This call to isCaptivePortal() could take up to a minute. Resolving the
430 // server's IP addresses could hit the DNS timeout, and attempting connections
431 // to each of the server's several IP addresses (currently one IPv4 and one
432 // IPv6) could each take SOCKET_TIMEOUT_MS. During this time this StateMachine
433 // will be unresponsive. isCaptivePortal() could be executed on another Thread
434 // if this is found to cause problems.
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400435 int httpResponseCode = isCaptivePortal();
Paul Jensenca8f16a2014-05-09 12:47:55 -0400436 if (httpResponseCode == 204) {
437 transitionTo(mValidatedState);
438 } else if (httpResponseCode >= 200 && httpResponseCode <= 399) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400439 transitionTo(mCaptivePortalState);
Paul Jensend0491e9a2015-05-05 14:52:22 -0400440 } else {
Paul Jensend9be23f2015-05-19 14:51:47 -0400441 final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
Paul Jensen869868be2014-05-15 10:33:05 -0400442 sendMessageDelayed(msg, mReevaluateDelayMs);
Paul Jensend9be23f2015-05-19 14:51:47 -0400443 mConnectivityServiceHandler.sendMessage(obtainMessage(
444 EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0,
445 mNetworkAgentInfo));
446 if (mAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) {
Paul Jensend0491e9a2015-05-05 14:52:22 -0400447 // Don't continue to blame UID forever.
448 TrafficStats.clearThreadStatsUid();
449 }
450 mReevaluateDelayMs *= 2;
451 if (mReevaluateDelayMs > MAX_REEVALUATE_DELAY_MS) {
452 mReevaluateDelayMs = MAX_REEVALUATE_DELAY_MS;
453 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400454 }
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400455 return HANDLED;
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400456 case CMD_FORCE_REEVALUATION:
Paul Jensend0491e9a2015-05-05 14:52:22 -0400457 // Before IGNORE_REEVALUATE_ATTEMPTS attempts are made,
458 // ignore any re-evaluation requests. After, restart the
459 // evaluation process via EvaluatingState#enter.
460 return mAttempts < IGNORE_REEVALUATE_ATTEMPTS ? HANDLED : NOT_HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400461 default:
462 return NOT_HANDLED;
463 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400464 }
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400465
466 @Override
467 public void exit() {
468 TrafficStats.clearThreadStatsUid();
469 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400470 }
471
Paul Jensendcbe8352014-09-16 16:28:34 -0400472 // BroadcastReceiver that waits for a particular Intent and then posts a message.
473 private class CustomIntentReceiver extends BroadcastReceiver {
Paul Jensen71b645f2014-10-13 14:13:07 -0400474 private final int mToken;
475 private final int mWhat;
Paul Jensendcbe8352014-09-16 16:28:34 -0400476 private final String mAction;
Paul Jensen71b645f2014-10-13 14:13:07 -0400477 CustomIntentReceiver(String action, int token, int what) {
478 mToken = token;
479 mWhat = what;
Paul Jensendcbe8352014-09-16 16:28:34 -0400480 mAction = action + "_" + mNetworkAgentInfo.network.netId + "_" + token;
481 mContext.registerReceiver(this, new IntentFilter(mAction));
Paul Jensen869868be2014-05-15 10:33:05 -0400482 }
Paul Jensendcbe8352014-09-16 16:28:34 -0400483 public PendingIntent getPendingIntent() {
Paul Jensen25a217c2015-02-27 22:55:47 -0500484 final Intent intent = new Intent(mAction);
485 intent.setPackage(mContext.getPackageName());
486 return PendingIntent.getBroadcast(mContext, 0, intent, 0);
Paul Jensendcbe8352014-09-16 16:28:34 -0400487 }
488 @Override
489 public void onReceive(Context context, Intent intent) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400490 if (intent.getAction().equals(mAction)) sendMessage(obtainMessage(mWhat, mToken));
Paul Jensendcbe8352014-09-16 16:28:34 -0400491 }
492 }
Paul Jensen869868be2014-05-15 10:33:05 -0400493
Paul Jensen71b645f2014-10-13 14:13:07 -0400494 // Being in the CaptivePortalState State indicates a captive portal was detected and the user
495 // has been shown a notification to sign-in.
496 private class CaptivePortalState extends State {
Paul Jensen25a217c2015-02-27 22:55:47 -0500497 private static final String ACTION_LAUNCH_CAPTIVE_PORTAL_APP =
498 "android.net.netmon.launchCaptivePortalApp";
499
Paul Jensen869868be2014-05-15 10:33:05 -0400500 @Override
501 public void enter() {
Paul Jensenad50a1f2014-09-05 12:06:44 -0400502 mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
503 NETWORK_TEST_RESULT_INVALID, 0, mNetworkAgentInfo));
Paul Jensend0491e9a2015-05-05 14:52:22 -0400504 // Don't annoy user with sign-in notifications.
Paul Jensen700f2362015-05-05 14:56:10 -0400505 if (mDontDisplaySigninNotification) return;
Paul Jensen25a217c2015-02-27 22:55:47 -0500506 // Create a CustomIntentReceiver that sends us a
507 // CMD_LAUNCH_CAPTIVE_PORTAL_APP message when the user
508 // touches the notification.
509 if (mLaunchCaptivePortalAppBroadcastReceiver == null) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400510 // Wait for result.
Paul Jensen25a217c2015-02-27 22:55:47 -0500511 mLaunchCaptivePortalAppBroadcastReceiver = new CustomIntentReceiver(
512 ACTION_LAUNCH_CAPTIVE_PORTAL_APP, new Random().nextInt(),
513 CMD_LAUNCH_CAPTIVE_PORTAL_APP);
Paul Jensen71b645f2014-10-13 14:13:07 -0400514 }
Paul Jensen25a217c2015-02-27 22:55:47 -0500515 // Display the sign in notification.
Paul Jensen71b645f2014-10-13 14:13:07 -0400516 Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 1,
517 mNetworkAgentInfo.network.netId,
Paul Jensen25a217c2015-02-27 22:55:47 -0500518 mLaunchCaptivePortalAppBroadcastReceiver.getPendingIntent());
Paul Jensen71b645f2014-10-13 14:13:07 -0400519 mConnectivityServiceHandler.sendMessage(message);
Paul Jensen869868be2014-05-15 10:33:05 -0400520 }
521
522 @Override
523 public boolean processMessage(Message message) {
524 if (DBG) log(getName() + message.toString());
Paul Jensen71b645f2014-10-13 14:13:07 -0400525 return NOT_HANDLED;
Paul Jensen869868be2014-05-15 10:33:05 -0400526 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400527 }
528
Paul Jensen71b645f2014-10-13 14:13:07 -0400529 // Being in the LingeringState State indicates a Network's validated bit is true and it once
530 // was the highest scoring Network satisfying a particular NetworkRequest, but since then
531 // another Network satsified the NetworkRequest with a higher score and hence this Network
532 // is "lingered" for a fixed period of time before it is disconnected. This period of time
533 // allows apps to wrap up communication and allows for seamless reactivation if the other
534 // higher scoring Network happens to disconnect.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400535 private class LingeringState extends State {
Paul Jensen79a08052014-08-21 12:44:07 -0400536 private static final String ACTION_LINGER_EXPIRED = "android.net.netmon.lingerExpired";
Paul Jensen79a08052014-08-21 12:44:07 -0400537
Paul Jensendcbe8352014-09-16 16:28:34 -0400538 private CustomIntentReceiver mBroadcastReceiver;
Paul Jensen79a08052014-08-21 12:44:07 -0400539 private PendingIntent mIntent;
540
Paul Jensenca8f16a2014-05-09 12:47:55 -0400541 @Override
542 public void enter() {
Paul Jensen71b645f2014-10-13 14:13:07 -0400543 mLingerToken = new Random().nextInt();
544 mBroadcastReceiver = new CustomIntentReceiver(ACTION_LINGER_EXPIRED, mLingerToken,
Paul Jensendcbe8352014-09-16 16:28:34 -0400545 CMD_LINGER_EXPIRED);
546 mIntent = mBroadcastReceiver.getPendingIntent();
Paul Jensen79a08052014-08-21 12:44:07 -0400547 long wakeupTime = SystemClock.elapsedRealtime() + mLingerDelayMs;
548 mAlarmManager.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime,
549 // Give a specific window so we aren't subject to unknown inexactitude.
550 mLingerDelayMs / 6, mIntent);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400551 }
552
553 @Override
554 public boolean processMessage(Message message) {
555 if (DBG) log(getName() + message.toString());
556 switch (message.what) {
557 case CMD_NETWORK_CONNECTED:
558 // Go straight to active as we've already evaluated.
559 transitionTo(mValidatedState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400560 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400561 case CMD_LINGER_EXPIRED:
562 if (message.arg1 != mLingerToken)
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400563 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400564 mConnectivityServiceHandler.sendMessage(
565 obtainMessage(EVENT_NETWORK_LINGER_COMPLETE, mNetworkAgentInfo));
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400566 return HANDLED;
Paul Jensenad50a1f2014-09-05 12:06:44 -0400567 case CMD_FORCE_REEVALUATION:
568 // Ignore reevaluation attempts when lingering. A reevaluation could result
569 // in a transition to the validated state which would abort the linger
570 // timeout. Lingering is the result of score assessment; validity is
571 // irrelevant.
572 return HANDLED;
Paul Jensen71b645f2014-10-13 14:13:07 -0400573 case CMD_CAPTIVE_PORTAL_APP_FINISHED:
574 // Ignore user network determination as this could abort linger timeout.
575 // Networks are only lingered once validated because:
576 // - Unvalidated networks are never lingered (see rematchNetworkAndRequests).
577 // - Once validated, a Network's validated bit is never cleared.
578 // Since networks are only lingered after being validated a user's
579 // determination will not change the death sentence that lingering entails:
580 // - If the user wants to use the network or bypasses the captive portal,
581 // the network's score will not be increased beyond its current value
582 // because it is already validated. Without a score increase there is no
583 // chance of reactivation (i.e. aborting linger timeout).
584 // - If the user does not want the network, lingering will disconnect the
585 // network anyhow.
586 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400587 default:
588 return NOT_HANDLED;
589 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400590 }
Paul Jensen79a08052014-08-21 12:44:07 -0400591
592 @Override
593 public void exit() {
594 mAlarmManager.cancel(mIntent);
595 mContext.unregisterReceiver(mBroadcastReceiver);
596 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400597 }
598
599 /**
600 * Do a URL fetch on a known server to see if we get the data we expect.
601 * Returns HTTP response code.
602 */
603 private int isCaptivePortal() {
604 if (!mIsCaptivePortalCheckEnabled) return 204;
605
Paul Jensenca8f16a2014-05-09 12:47:55 -0400606 HttpURLConnection urlConnection = null;
Paul Jensen869868be2014-05-15 10:33:05 -0400607 int httpResponseCode = 599;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400608 try {
Paul Jensene547ff22014-08-04 09:12:24 -0400609 URL url = new URL("http", mServer, "/generate_204");
Paul Jensen8fe17422015-02-02 11:03:03 -0500610 // On networks with a PAC instead of fetching a URL that should result in a 204
611 // reponse, we instead simply fetch the PAC script. This is done for a few reasons:
612 // 1. At present our PAC code does not yet handle multiple PACs on multiple networks
613 // until something like https://android-review.googlesource.com/#/c/115180/ lands.
614 // Network.openConnection() will ignore network-specific PACs and instead fetch
615 // using NO_PROXY. If a PAC is in place, the only fetch we know will succeed with
616 // NO_PROXY is the fetch of the PAC itself.
617 // 2. To proxy the generate_204 fetch through a PAC would require a number of things
618 // happen before the fetch can commence, namely:
619 // a) the PAC script be fetched
620 // b) a PAC script resolver service be fired up and resolve mServer
621 // Network validation could be delayed until these prerequisities are satisifed or
622 // could simply be left to race them. Neither is an optimal solution.
623 // 3. PAC scripts are sometimes used to block or restrict Internet access and may in
624 // fact block fetching of the generate_204 URL which would lead to false negative
625 // results for network validation.
626 boolean fetchPac = false;
627 {
628 final ProxyInfo proxyInfo = mNetworkAgentInfo.linkProperties.getHttpProxy();
629 if (proxyInfo != null && !Uri.EMPTY.equals(proxyInfo.getPacFileUrl())) {
630 url = new URL(proxyInfo.getPacFileUrl().toString());
631 fetchPac = true;
632 }
633 }
Paul Jensene547ff22014-08-04 09:12:24 -0400634 if (DBG) {
635 log("Checking " + url.toString() + " on " +
636 mNetworkAgentInfo.networkInfo.getExtraInfo());
Paul Jensenca8f16a2014-05-09 12:47:55 -0400637 }
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700638 urlConnection = (HttpURLConnection) mNetworkAgentInfo.network.openConnection(url);
Paul Jensen8fe17422015-02-02 11:03:03 -0500639 urlConnection.setInstanceFollowRedirects(fetchPac);
Paul Jensene547ff22014-08-04 09:12:24 -0400640 urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
641 urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
642 urlConnection.setUseCaches(false);
Paul Jensen306f1a42014-08-04 10:59:01 -0400643
644 // Time how long it takes to get a response to our request
645 long requestTimestamp = SystemClock.elapsedRealtime();
646
Paul Jensene547ff22014-08-04 09:12:24 -0400647 urlConnection.getInputStream();
Paul Jensen306f1a42014-08-04 10:59:01 -0400648
649 // Time how long it takes to get a response to our request
650 long responseTimestamp = SystemClock.elapsedRealtime();
651
Paul Jensene547ff22014-08-04 09:12:24 -0400652 httpResponseCode = urlConnection.getResponseCode();
653 if (DBG) {
654 log("isCaptivePortal: ret=" + httpResponseCode +
655 " headers=" + urlConnection.getHeaderFields());
656 }
657 // NOTE: We may want to consider an "HTTP/1.0 204" response to be a captive
658 // portal. The only example of this seen so far was a captive portal. For
659 // the time being go with prior behavior of assuming it's not a captive
660 // portal. If it is considered a captive portal, a different sign-in URL
661 // is needed (i.e. can't browse a 204). This could be the result of an HTTP
662 // proxy server.
663
664 // Consider 200 response with "Content-length=0" to not be a captive portal.
665 // There's no point in considering this a captive portal as the user cannot
666 // sign-in to an empty page. Probably the result of a broken transparent proxy.
667 // See http://b/9972012.
668 if (httpResponseCode == 200 && urlConnection.getContentLength() == 0) {
669 if (DBG) log("Empty 200 response interpreted as 204 response.");
670 httpResponseCode = 204;
671 }
Paul Jensen306f1a42014-08-04 10:59:01 -0400672
Paul Jensen8fe17422015-02-02 11:03:03 -0500673 if (httpResponseCode == 200 && fetchPac) {
674 if (DBG) log("PAC fetch 200 response interpreted as 204 response.");
675 httpResponseCode = 204;
676 }
677
Jeff Davidsonf9fff922014-12-05 16:56:08 -0800678 sendNetworkConditionsBroadcast(true /* response received */,
679 httpResponseCode != 204 /* isCaptivePortal */,
Paul Jensen306f1a42014-08-04 10:59:01 -0400680 requestTimestamp, responseTimestamp);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400681 } catch (IOException e) {
682 if (DBG) log("Probably not a portal: exception " + e);
Paul Jensen869868be2014-05-15 10:33:05 -0400683 if (httpResponseCode == 599) {
684 // TODO: Ping gateway and DNS server and log results.
685 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400686 } finally {
687 if (urlConnection != null) {
688 urlConnection.disconnect();
689 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400690 }
691 return httpResponseCode;
692 }
Paul Jensen306f1a42014-08-04 10:59:01 -0400693
694 /**
695 * @param responseReceived - whether or not we received a valid HTTP response to our request.
696 * If false, isCaptivePortal and responseTimestampMs are ignored
697 * TODO: This should be moved to the transports. The latency could be passed to the transports
698 * along with the captive portal result. Currently the TYPE_MOBILE broadcasts appear unused so
699 * perhaps this could just be added to the WiFi transport only.
700 */
701 private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal,
702 long requestTimestampMs, long responseTimestampMs) {
703 if (Settings.Global.getInt(mContext.getContentResolver(),
704 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 0) {
705 if (DBG) log("Don't send network conditions - lacking user consent.");
706 return;
707 }
708
Robert Greenwaltfb68f8f2014-08-13 13:43:32 -0700709 if (systemReady == false) return;
710
Paul Jensen306f1a42014-08-04 10:59:01 -0400711 Intent latencyBroadcast = new Intent(ACTION_NETWORK_CONDITIONS_MEASURED);
712 switch (mNetworkAgentInfo.networkInfo.getType()) {
713 case ConnectivityManager.TYPE_WIFI:
714 WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo();
715 if (currentWifiInfo != null) {
716 // NOTE: getSSID()'s behavior changed in API 17; before that, SSIDs were not
717 // surrounded by double quotation marks (thus violating the Javadoc), but this
718 // was changed to match the Javadoc in API 17. Since clients may have started
719 // sanitizing the output of this method since API 17 was released, we should
720 // not change it here as it would become impossible to tell whether the SSID is
721 // simply being surrounded by quotes due to the API, or whether those quotes
722 // are actually part of the SSID.
723 latencyBroadcast.putExtra(EXTRA_SSID, currentWifiInfo.getSSID());
724 latencyBroadcast.putExtra(EXTRA_BSSID, currentWifiInfo.getBSSID());
725 } else {
726 if (DBG) logw("network info is TYPE_WIFI but no ConnectionInfo found");
727 return;
728 }
729 break;
730 case ConnectivityManager.TYPE_MOBILE:
731 latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType());
732 List<CellInfo> info = mTelephonyManager.getAllCellInfo();
733 if (info == null) return;
734 int numRegisteredCellInfo = 0;
735 for (CellInfo cellInfo : info) {
736 if (cellInfo.isRegistered()) {
737 numRegisteredCellInfo++;
738 if (numRegisteredCellInfo > 1) {
739 if (DBG) log("more than one registered CellInfo. Can't " +
740 "tell which is active. Bailing.");
741 return;
742 }
743 if (cellInfo instanceof CellInfoCdma) {
744 CellIdentityCdma cellId = ((CellInfoCdma) cellInfo).getCellIdentity();
745 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
746 } else if (cellInfo instanceof CellInfoGsm) {
747 CellIdentityGsm cellId = ((CellInfoGsm) cellInfo).getCellIdentity();
748 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
749 } else if (cellInfo instanceof CellInfoLte) {
750 CellIdentityLte cellId = ((CellInfoLte) cellInfo).getCellIdentity();
751 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
752 } else if (cellInfo instanceof CellInfoWcdma) {
753 CellIdentityWcdma cellId = ((CellInfoWcdma) cellInfo).getCellIdentity();
754 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
755 } else {
756 if (DBG) logw("Registered cellinfo is unrecognized");
757 return;
758 }
759 }
760 }
761 break;
762 default:
763 return;
764 }
765 latencyBroadcast.putExtra(EXTRA_CONNECTIVITY_TYPE, mNetworkAgentInfo.networkInfo.getType());
766 latencyBroadcast.putExtra(EXTRA_RESPONSE_RECEIVED, responseReceived);
767 latencyBroadcast.putExtra(EXTRA_REQUEST_TIMESTAMP_MS, requestTimestampMs);
768
769 if (responseReceived) {
770 latencyBroadcast.putExtra(EXTRA_IS_CAPTIVE_PORTAL, isCaptivePortal);
771 latencyBroadcast.putExtra(EXTRA_RESPONSE_TIMESTAMP_MS, responseTimestampMs);
772 }
Paul Jensen55298582014-08-20 11:01:41 -0400773 mContext.sendBroadcastAsUser(latencyBroadcast, UserHandle.CURRENT,
774 PERMISSION_ACCESS_NETWORK_CONDITIONS);
Paul Jensen306f1a42014-08-04 10:59:01 -0400775 }
Paul Jensend7b6ca92015-05-13 14:05:12 -0400776
777 // Allow tests to override linger time.
778 @VisibleForTesting
779 public static void SetDefaultLingerTime(int time_ms) {
780 if (Process.myUid() == Process.SYSTEM_UID) {
781 throw new SecurityException("SetDefaultLingerTime only for internal testing.");
782 }
783 DEFAULT_LINGER_DELAY_MS = time_ms;
784 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400785}