blob: bf896a50065d72f3863344739d6af649247370a6 [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 Jensen306f1a42014-08-04 10:59:01 -040035import android.os.SystemClock;
Paul Jensenca8f16a2014-05-09 12:47:55 -040036import android.os.SystemProperties;
Paul Jensen869868be2014-05-15 10:33:05 -040037import android.os.UserHandle;
Paul Jensenca8f16a2014-05-09 12:47:55 -040038import android.provider.Settings;
Paul Jensen306f1a42014-08-04 10:59:01 -040039import android.telephony.CellIdentityCdma;
40import android.telephony.CellIdentityGsm;
41import android.telephony.CellIdentityLte;
42import android.telephony.CellIdentityWcdma;
43import android.telephony.CellInfo;
44import android.telephony.CellInfoCdma;
45import android.telephony.CellInfoGsm;
46import android.telephony.CellInfoLte;
47import android.telephony.CellInfoWcdma;
48import android.telephony.TelephonyManager;
Paul Jensen532b61432014-11-10 09:50:02 -050049import android.util.Log;
Paul Jensenca8f16a2014-05-09 12:47:55 -040050
51import com.android.internal.util.Protocol;
52import com.android.internal.util.State;
53import com.android.internal.util.StateMachine;
54import com.android.server.connectivity.NetworkAgentInfo;
55
Paul Jensenca8f16a2014-05-09 12:47:55 -040056import java.io.IOException;
Paul Jensenca8f16a2014-05-09 12:47:55 -040057import java.net.HttpURLConnection;
Paul Jensenca8f16a2014-05-09 12:47:55 -040058import java.net.URL;
Paul Jensen306f1a42014-08-04 10:59:01 -040059import java.util.List;
Paul Jensen71b645f2014-10-13 14:13:07 -040060import java.util.Random;
Paul Jensenca8f16a2014-05-09 12:47:55 -040061
62/**
63 * {@hide}
64 */
65public class NetworkMonitor extends StateMachine {
66 private static final boolean DBG = true;
67 private static final String TAG = "NetworkMonitor";
Lorenzo Colitticd29cb62015-01-14 00:16:03 +090068 private static final String DEFAULT_SERVER = "connectivitycheck.android.com";
Paul Jensenca8f16a2014-05-09 12:47:55 -040069 private static final int SOCKET_TIMEOUT_MS = 10000;
Paul Jensen306f1a42014-08-04 10:59:01 -040070 public static final String ACTION_NETWORK_CONDITIONS_MEASURED =
71 "android.net.conn.NETWORK_CONDITIONS_MEASURED";
72 public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type";
73 public static final String EXTRA_NETWORK_TYPE = "extra_network_type";
74 public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received";
75 public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal";
76 public static final String EXTRA_CELL_ID = "extra_cellid";
77 public static final String EXTRA_SSID = "extra_ssid";
78 public static final String EXTRA_BSSID = "extra_bssid";
79 /** real time since boot */
80 public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms";
81 public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms";
82
83 private static final String PERMISSION_ACCESS_NETWORK_CONDITIONS =
84 "android.permission.ACCESS_NETWORK_CONDITIONS";
Paul Jensenca8f16a2014-05-09 12:47:55 -040085
Paul Jensenad50a1f2014-09-05 12:06:44 -040086 // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED.
87 // The network should be used as a default internet connection. It was found to be:
88 // 1. a functioning network providing internet access, or
89 // 2. a captive portal and the user decided to use it as is.
90 public static final int NETWORK_TEST_RESULT_VALID = 0;
91 // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED.
92 // The network should not be used as a default internet connection. It was found to be:
93 // 1. a captive portal and the user is prompted to sign-in, or
94 // 2. a captive portal and the user did not want to use it, or
95 // 3. a broken network (e.g. DNS failed, connect failed, HTTP request failed).
96 public static final int NETWORK_TEST_RESULT_INVALID = 1;
97
Paul Jensenca8f16a2014-05-09 12:47:55 -040098 private static final int BASE = Protocol.BASE_NETWORK_MONITOR;
99
100 /**
101 * Inform NetworkMonitor that their network is connected.
102 * Initiates Network Validation.
103 */
104 public static final int CMD_NETWORK_CONNECTED = BASE + 1;
105
106 /**
Paul Jensenad50a1f2014-09-05 12:06:44 -0400107 * Inform ConnectivityService that the network has been tested.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400108 * obj = NetworkAgentInfo
Paul Jensenad50a1f2014-09-05 12:06:44 -0400109 * arg1 = One of the NETWORK_TESTED_RESULT_* constants.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400110 */
Paul Jensenad50a1f2014-09-05 12:06:44 -0400111 public static final int EVENT_NETWORK_TESTED = BASE + 2;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400112
113 /**
114 * Inform NetworkMonitor to linger a network. The Monitor should
115 * start a timer and/or start watching for zero live connections while
116 * moving towards LINGER_COMPLETE. After the Linger period expires
117 * (or other events mark the end of the linger state) the LINGER_COMPLETE
118 * event should be sent and the network will be shut down. If a
119 * CMD_NETWORK_CONNECTED happens before the LINGER completes
120 * it indicates further desire to keep the network alive and so
121 * the LINGER is aborted.
122 */
123 public static final int CMD_NETWORK_LINGER = BASE + 3;
124
125 /**
126 * Message to self indicating linger delay has expired.
127 * arg1 = Token to ignore old messages.
128 */
129 private static final int CMD_LINGER_EXPIRED = BASE + 4;
130
131 /**
132 * Inform ConnectivityService that the network LINGER period has
133 * expired.
134 * obj = NetworkAgentInfo
135 */
136 public static final int EVENT_NETWORK_LINGER_COMPLETE = BASE + 5;
137
138 /**
Paul Jensenca8f16a2014-05-09 12:47:55 -0400139 * Message to self indicating it's time to evaluate a network's connectivity.
140 * arg1 = Token to ignore old messages.
141 */
Paul Jensen869868be2014-05-15 10:33:05 -0400142 private static final int CMD_REEVALUATE = BASE + 6;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400143
144 /**
Paul Jensenca8f16a2014-05-09 12:47:55 -0400145 * Inform NetworkMonitor that the network has disconnected.
146 */
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400147 public static final int CMD_NETWORK_DISCONNECTED = BASE + 7;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400148
149 /**
150 * Force evaluation even if it has succeeded in the past.
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400151 * arg1 = UID responsible for requesting this reeval. Will be billed for data.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400152 */
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400153 public static final int CMD_FORCE_REEVALUATION = BASE + 8;
Paul Jensen869868be2014-05-15 10:33:05 -0400154
155 /**
Paul Jensen71b645f2014-10-13 14:13:07 -0400156 * Message to self indicating captive portal app finished.
Paul Jensen25a217c2015-02-27 22:55:47 -0500157 * arg1 = one of: CAPTIVE_PORTAL_APP_RETURN_DISMISSED,
Paul Jensen71b645f2014-10-13 14:13:07 -0400158 * CAPTIVE_PORTAL_APP_RETURN_UNWANTED,
159 * CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS
Paul Jensen25a217c2015-02-27 22:55:47 -0500160 * obj = mCaptivePortalLoggedInResponseToken as String
Paul Jensen869868be2014-05-15 10:33:05 -0400161 */
Paul Jensen25a217c2015-02-27 22:55:47 -0500162 public static final int CMD_CAPTIVE_PORTAL_APP_FINISHED = BASE + 9;
Paul Jensen869868be2014-05-15 10:33:05 -0400163
164 /**
165 * Request ConnectivityService display provisioning notification.
166 * arg1 = Whether to make the notification visible.
Paul Jensenfdc4e4a2014-07-15 12:07:36 -0400167 * arg2 = NetID.
168 * obj = Intent to be launched when notification selected by user, null if !arg1.
Paul Jensen869868be2014-05-15 10:33:05 -0400169 */
Paul Jensen71b645f2014-10-13 14:13:07 -0400170 public static final int EVENT_PROVISIONING_NOTIFICATION = BASE + 10;
Paul Jensen869868be2014-05-15 10:33:05 -0400171
172 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500173 * Message to self indicating sign-in app should be launched.
174 * Sent by mLaunchCaptivePortalAppBroadcastReceiver when the
175 * user touches the sign in notification.
Paul Jensen869868be2014-05-15 10:33:05 -0400176 */
Paul Jensen25a217c2015-02-27 22:55:47 -0500177 private static final int CMD_LAUNCH_CAPTIVE_PORTAL_APP = BASE + 11;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400178
179 private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger";
180 // Default to 30s linger time-out.
181 private static final int DEFAULT_LINGER_DELAY_MS = 30000;
182 private final int mLingerDelayMs;
183 private int mLingerToken = 0;
184
Paul Jensend0491e9a2015-05-05 14:52:22 -0400185 // Start mReevaluateDelayMs at this value and double.
186 private static final int INITIAL_REEVALUATE_DELAY_MS = 1000;
187 private static final int MAX_REEVALUATE_DELAY_MS = 10*60*1000;
188 // Before network has been evaluated this many times, ignore repeated reevaluate requests.
189 private static final int IGNORE_REEVALUATE_ATTEMPTS = 5;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400190 private int mReevaluateToken = 0;
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400191 private static final int INVALID_UID = -1;
192 private int mUidResponsibleForReeval = INVALID_UID;
Paul Jensend9be23f2015-05-19 14:51:47 -0400193 // Stop blaming UID that requested re-evaluation after this many attempts.
194 private static final int BLAME_FOR_EVALUATION_ATTEMPTS = 5;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400195
196 private final Context mContext;
197 private final Handler mConnectivityServiceHandler;
198 private final NetworkAgentInfo mNetworkAgentInfo;
Paul Jensen306f1a42014-08-04 10:59:01 -0400199 private final TelephonyManager mTelephonyManager;
200 private final WifiManager mWifiManager;
Paul Jensen79a08052014-08-21 12:44:07 -0400201 private final AlarmManager mAlarmManager;
Paul Jensen2c311d62014-11-17 12:34:51 -0500202 private final NetworkRequest mDefaultRequest;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400203
204 private String mServer;
205 private boolean mIsCaptivePortalCheckEnabled = false;
206
Paul Jensenad50a1f2014-09-05 12:06:44 -0400207 // Set if the user explicitly selected "Do not use this network" in captive portal sign-in app.
208 private boolean mUserDoesNotWant = false;
Paul Jensen700f2362015-05-05 14:56:10 -0400209 // Avoids surfacing "Sign in to network" notification.
210 private boolean mDontDisplaySigninNotification = false;
Paul Jensenad50a1f2014-09-05 12:06:44 -0400211
Robert Greenwaltfb68f8f2014-08-13 13:43:32 -0700212 public boolean systemReady = false;
213
Paul Jensen71b645f2014-10-13 14:13:07 -0400214 private final State mDefaultState = new DefaultState();
Paul Jensen71b645f2014-10-13 14:13:07 -0400215 private final State mValidatedState = new ValidatedState();
216 private final State mMaybeNotifyState = new MaybeNotifyState();
217 private final State mEvaluatingState = new EvaluatingState();
218 private final State mCaptivePortalState = new CaptivePortalState();
219 private final State mLingeringState = new LingeringState();
220
Paul Jensen25a217c2015-02-27 22:55:47 -0500221 private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null;
Paul Jensen71b645f2014-10-13 14:13:07 -0400222 private String mCaptivePortalLoggedInResponseToken = null;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400223
Paul Jensen2c311d62014-11-17 12:34:51 -0500224 public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo,
225 NetworkRequest defaultRequest) {
Paul Jensenca8f16a2014-05-09 12:47:55 -0400226 // Add suffix indicating which NetworkMonitor we're talking about.
227 super(TAG + networkAgentInfo.name());
228
229 mContext = context;
230 mConnectivityServiceHandler = handler;
231 mNetworkAgentInfo = networkAgentInfo;
Paul Jensen306f1a42014-08-04 10:59:01 -0400232 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
233 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Paul Jensen79a08052014-08-21 12:44:07 -0400234 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Paul Jensen2c311d62014-11-17 12:34:51 -0500235 mDefaultRequest = defaultRequest;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400236
237 addState(mDefaultState);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400238 addState(mValidatedState, mDefaultState);
Paul Jensen71b645f2014-10-13 14:13:07 -0400239 addState(mMaybeNotifyState, mDefaultState);
240 addState(mEvaluatingState, mMaybeNotifyState);
241 addState(mCaptivePortalState, mMaybeNotifyState);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400242 addState(mLingeringState, mDefaultState);
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700243 setInitialState(mDefaultState);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400244
245 mServer = Settings.Global.getString(mContext.getContentResolver(),
246 Settings.Global.CAPTIVE_PORTAL_SERVER);
247 if (mServer == null) mServer = DEFAULT_SERVER;
248
249 mLingerDelayMs = SystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400250
Paul Jensen869868be2014-05-15 10:33:05 -0400251 mIsCaptivePortalCheckEnabled = Settings.Global.getInt(mContext.getContentResolver(),
252 Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 1) == 1;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400253
Paul Jensen71b645f2014-10-13 14:13:07 -0400254 mCaptivePortalLoggedInResponseToken = String.valueOf(new Random().nextLong());
255
Paul Jensenca8f16a2014-05-09 12:47:55 -0400256 start();
257 }
258
Paul Jensen532b61432014-11-10 09:50:02 -0500259 @Override
260 protected void log(String s) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400261 Log.d(TAG + "/" + mNetworkAgentInfo.name(), s);
Paul Jensen532b61432014-11-10 09:50:02 -0500262 }
263
Paul Jensen71b645f2014-10-13 14:13:07 -0400264 // DefaultState is the parent of all States. It exists only to handle CMD_* messages but
265 // does not entail any real state (hence no enter() or exit() routines).
Paul Jensenca8f16a2014-05-09 12:47:55 -0400266 private class DefaultState extends State {
267 @Override
268 public boolean processMessage(Message message) {
269 if (DBG) log(getName() + message.toString());
270 switch (message.what) {
271 case CMD_NETWORK_LINGER:
272 if (DBG) log("Lingering");
273 transitionTo(mLingeringState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400274 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400275 case CMD_NETWORK_CONNECTED:
276 if (DBG) log("Connected");
277 transitionTo(mEvaluatingState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400278 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400279 case CMD_NETWORK_DISCONNECTED:
Robert Greenwalt1fd9aee2014-07-17 16:11:38 -0700280 if (DBG) log("Disconnected - quitting");
Paul Jensen25a217c2015-02-27 22:55:47 -0500281 if (mLaunchCaptivePortalAppBroadcastReceiver != null) {
282 mContext.unregisterReceiver(mLaunchCaptivePortalAppBroadcastReceiver);
283 mLaunchCaptivePortalAppBroadcastReceiver = null;
Paul Jensen71b645f2014-10-13 14:13:07 -0400284 }
Robert Greenwalt1fd9aee2014-07-17 16:11:38 -0700285 quit();
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400286 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400287 case CMD_FORCE_REEVALUATION:
288 if (DBG) log("Forcing reevaluation");
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400289 mUidResponsibleForReeval = message.arg1;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400290 transitionTo(mEvaluatingState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400291 return HANDLED;
Paul Jensen71b645f2014-10-13 14:13:07 -0400292 case CMD_CAPTIVE_PORTAL_APP_FINISHED:
Paul Jensen25a217c2015-02-27 22:55:47 -0500293 if (!mCaptivePortalLoggedInResponseToken.equals((String)message.obj))
294 return HANDLED;
295 // Previous token was sent out, come up with a new one.
Paul Jensen71b645f2014-10-13 14:13:07 -0400296 mCaptivePortalLoggedInResponseToken = String.valueOf(new Random().nextLong());
297 switch (message.arg1) {
Paul Jensen25a217c2015-02-27 22:55:47 -0500298 case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_DISMISSED:
Paul Jensend0491e9a2015-05-05 14:52:22 -0400299 sendMessage(CMD_FORCE_REEVALUATION, 0 /* no UID */, 0);
Paul Jensen25a217c2015-02-27 22:55:47 -0500300 break;
301 case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS:
Paul Jensen700f2362015-05-05 14:56:10 -0400302 mDontDisplaySigninNotification = true;
Paul Jensen25a217c2015-02-27 22:55:47 -0500303 // TODO: Distinguish this from a network that actually validates.
304 // Displaying the "!" on the system UI icon may still be a good idea.
Paul Jensen71b645f2014-10-13 14:13:07 -0400305 transitionTo(mValidatedState);
306 break;
Paul Jensen25a217c2015-02-27 22:55:47 -0500307 case ConnectivityManager.CAPTIVE_PORTAL_APP_RETURN_UNWANTED:
Paul Jensen700f2362015-05-05 14:56:10 -0400308 mDontDisplaySigninNotification = true;
Paul Jensen71b645f2014-10-13 14:13:07 -0400309 mUserDoesNotWant = true;
Paul Jensend0491e9a2015-05-05 14:52:22 -0400310 mConnectivityServiceHandler.sendMessage(obtainMessage(
311 EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0,
312 mNetworkAgentInfo));
Paul Jensen71b645f2014-10-13 14:13:07 -0400313 // TODO: Should teardown network.
Paul Jensend0491e9a2015-05-05 14:52:22 -0400314 mUidResponsibleForReeval = 0;
315 transitionTo(mEvaluatingState);
Paul Jensen71b645f2014-10-13 14:13:07 -0400316 break;
317 }
318 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400319 default:
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400320 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400321 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400322 }
323 }
324
Paul Jensen71b645f2014-10-13 14:13:07 -0400325 // Being in the ValidatedState State indicates a Network is:
326 // - Successfully validated, or
327 // - Wanted "as is" by the user, or
328 // - Does not satsify the default NetworkRequest and so validation has been skipped.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400329 private class ValidatedState extends State {
330 @Override
331 public void enter() {
332 if (DBG) log("Validated");
Paul Jensenad50a1f2014-09-05 12:06:44 -0400333 mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
334 NETWORK_TEST_RESULT_VALID, 0, mNetworkAgentInfo));
Paul Jensenca8f16a2014-05-09 12:47:55 -0400335 }
336
337 @Override
338 public boolean processMessage(Message message) {
339 if (DBG) log(getName() + message.toString());
340 switch (message.what) {
341 case CMD_NETWORK_CONNECTED:
342 transitionTo(mValidatedState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400343 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400344 default:
345 return NOT_HANDLED;
346 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400347 }
348 }
349
Paul Jensen71b645f2014-10-13 14:13:07 -0400350 // Being in the MaybeNotifyState State indicates the user may have been notified that sign-in
351 // is required. This State takes care to clear the notification upon exit from the State.
352 private class MaybeNotifyState extends State {
353 @Override
Paul Jensen25a217c2015-02-27 22:55:47 -0500354 public boolean processMessage(Message message) {
355 if (DBG) log(getName() + message.toString());
356 switch (message.what) {
357 case CMD_LAUNCH_CAPTIVE_PORTAL_APP:
358 final Intent intent = new Intent(
359 ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN);
360 intent.putExtra(ConnectivityManager.EXTRA_NETWORK, mNetworkAgentInfo.network);
361 intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_TOKEN,
362 mCaptivePortalLoggedInResponseToken);
363 intent.setFlags(
364 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
365 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
366 return HANDLED;
367 default:
368 return NOT_HANDLED;
369 }
370 }
371
372 @Override
Paul Jensen71b645f2014-10-13 14:13:07 -0400373 public void exit() {
374 Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 0,
375 mNetworkAgentInfo.network.netId, null);
376 mConnectivityServiceHandler.sendMessage(message);
377 }
378 }
379
380 // Being in the EvaluatingState State indicates the Network is being evaluated for internet
Paul Jensend0491e9a2015-05-05 14:52:22 -0400381 // connectivity, or that the user has indicated that this network is unwanted.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400382 private class EvaluatingState extends State {
Paul Jensend0491e9a2015-05-05 14:52:22 -0400383 private int mReevaluateDelayMs;
384 private int mAttempts;
Paul Jensen869868be2014-05-15 10:33:05 -0400385
Paul Jensenca8f16a2014-05-09 12:47:55 -0400386 @Override
387 public void enter() {
388 sendMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400389 if (mUidResponsibleForReeval != INVALID_UID) {
390 TrafficStats.setThreadStatsUid(mUidResponsibleForReeval);
391 mUidResponsibleForReeval = INVALID_UID;
392 }
Paul Jensend0491e9a2015-05-05 14:52:22 -0400393 mReevaluateDelayMs = INITIAL_REEVALUATE_DELAY_MS;
394 mAttempts = 0;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400395 }
396
397 @Override
398 public boolean processMessage(Message message) {
399 if (DBG) log(getName() + message.toString());
400 switch (message.what) {
401 case CMD_REEVALUATE:
Paul Jensend0491e9a2015-05-05 14:52:22 -0400402 if (message.arg1 != mReevaluateToken || mUserDoesNotWant)
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400403 return HANDLED;
Paul Jensen2c311d62014-11-17 12:34:51 -0500404 // Don't bother validating networks that don't satisify the default request.
405 // This includes:
406 // - VPNs which can be considered explicitly desired by the user and the
407 // user's desire trumps whether the network validates.
408 // - Networks that don't provide internet access. It's unclear how to
409 // validate such networks.
410 // - Untrusted networks. It's unsafe to prompt the user to sign-in to
411 // such networks and the user didn't express interest in connecting to
412 // such networks (an app did) so the user may be unhappily surprised when
413 // asked to sign-in to a network they didn't want to connect to in the
414 // first place. Validation could be done to adjust the network scores
415 // however these networks are app-requested and may not be intended for
416 // general usage, in which case general validation may not be an accurate
417 // measure of the network's quality. Only the app knows how to evaluate
418 // the network so don't bother validating here. Furthermore sending HTTP
419 // packets over the network may be undesirable, for example an extremely
420 // expensive metered network, or unwanted leaking of the User Agent string.
421 if (!mDefaultRequest.networkCapabilities.satisfiedByNetworkCapabilities(
422 mNetworkAgentInfo.networkCapabilities)) {
Paul Jensenca8f16a2014-05-09 12:47:55 -0400423 transitionTo(mValidatedState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400424 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400425 }
Paul Jensend0491e9a2015-05-05 14:52:22 -0400426 mAttempts++;
Lorenzo Colitti351bfad2015-01-22 22:36:50 +0900427 // Note: This call to isCaptivePortal() could take up to a minute. Resolving the
428 // server's IP addresses could hit the DNS timeout, and attempting connections
429 // to each of the server's several IP addresses (currently one IPv4 and one
430 // IPv6) could each take SOCKET_TIMEOUT_MS. During this time this StateMachine
431 // will be unresponsive. isCaptivePortal() could be executed on another Thread
432 // if this is found to cause problems.
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400433 int httpResponseCode = isCaptivePortal();
Paul Jensenca8f16a2014-05-09 12:47:55 -0400434 if (httpResponseCode == 204) {
435 transitionTo(mValidatedState);
436 } else if (httpResponseCode >= 200 && httpResponseCode <= 399) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400437 transitionTo(mCaptivePortalState);
Paul Jensend0491e9a2015-05-05 14:52:22 -0400438 } else {
Paul Jensend9be23f2015-05-19 14:51:47 -0400439 final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
Paul Jensen869868be2014-05-15 10:33:05 -0400440 sendMessageDelayed(msg, mReevaluateDelayMs);
Paul Jensend9be23f2015-05-19 14:51:47 -0400441 mConnectivityServiceHandler.sendMessage(obtainMessage(
442 EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0,
443 mNetworkAgentInfo));
444 if (mAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) {
Paul Jensend0491e9a2015-05-05 14:52:22 -0400445 // Don't continue to blame UID forever.
446 TrafficStats.clearThreadStatsUid();
447 }
448 mReevaluateDelayMs *= 2;
449 if (mReevaluateDelayMs > MAX_REEVALUATE_DELAY_MS) {
450 mReevaluateDelayMs = MAX_REEVALUATE_DELAY_MS;
451 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400452 }
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400453 return HANDLED;
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400454 case CMD_FORCE_REEVALUATION:
Paul Jensend0491e9a2015-05-05 14:52:22 -0400455 // Before IGNORE_REEVALUATE_ATTEMPTS attempts are made,
456 // ignore any re-evaluation requests. After, restart the
457 // evaluation process via EvaluatingState#enter.
458 return mAttempts < IGNORE_REEVALUATE_ATTEMPTS ? HANDLED : NOT_HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400459 default:
460 return NOT_HANDLED;
461 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400462 }
Paul Jensen7ccd3df2014-08-29 09:54:01 -0400463
464 @Override
465 public void exit() {
466 TrafficStats.clearThreadStatsUid();
467 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400468 }
469
Paul Jensendcbe8352014-09-16 16:28:34 -0400470 // BroadcastReceiver that waits for a particular Intent and then posts a message.
471 private class CustomIntentReceiver extends BroadcastReceiver {
Paul Jensen71b645f2014-10-13 14:13:07 -0400472 private final int mToken;
473 private final int mWhat;
Paul Jensendcbe8352014-09-16 16:28:34 -0400474 private final String mAction;
Paul Jensen71b645f2014-10-13 14:13:07 -0400475 CustomIntentReceiver(String action, int token, int what) {
476 mToken = token;
477 mWhat = what;
Paul Jensendcbe8352014-09-16 16:28:34 -0400478 mAction = action + "_" + mNetworkAgentInfo.network.netId + "_" + token;
479 mContext.registerReceiver(this, new IntentFilter(mAction));
Paul Jensen869868be2014-05-15 10:33:05 -0400480 }
Paul Jensendcbe8352014-09-16 16:28:34 -0400481 public PendingIntent getPendingIntent() {
Paul Jensen25a217c2015-02-27 22:55:47 -0500482 final Intent intent = new Intent(mAction);
483 intent.setPackage(mContext.getPackageName());
484 return PendingIntent.getBroadcast(mContext, 0, intent, 0);
Paul Jensendcbe8352014-09-16 16:28:34 -0400485 }
486 @Override
487 public void onReceive(Context context, Intent intent) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400488 if (intent.getAction().equals(mAction)) sendMessage(obtainMessage(mWhat, mToken));
Paul Jensendcbe8352014-09-16 16:28:34 -0400489 }
490 }
Paul Jensen869868be2014-05-15 10:33:05 -0400491
Paul Jensen71b645f2014-10-13 14:13:07 -0400492 // Being in the CaptivePortalState State indicates a captive portal was detected and the user
493 // has been shown a notification to sign-in.
494 private class CaptivePortalState extends State {
Paul Jensen25a217c2015-02-27 22:55:47 -0500495 private static final String ACTION_LAUNCH_CAPTIVE_PORTAL_APP =
496 "android.net.netmon.launchCaptivePortalApp";
497
Paul Jensen869868be2014-05-15 10:33:05 -0400498 @Override
499 public void enter() {
Paul Jensenad50a1f2014-09-05 12:06:44 -0400500 mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
501 NETWORK_TEST_RESULT_INVALID, 0, mNetworkAgentInfo));
Paul Jensend0491e9a2015-05-05 14:52:22 -0400502 // Don't annoy user with sign-in notifications.
Paul Jensen700f2362015-05-05 14:56:10 -0400503 if (mDontDisplaySigninNotification) return;
Paul Jensen25a217c2015-02-27 22:55:47 -0500504 // Create a CustomIntentReceiver that sends us a
505 // CMD_LAUNCH_CAPTIVE_PORTAL_APP message when the user
506 // touches the notification.
507 if (mLaunchCaptivePortalAppBroadcastReceiver == null) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400508 // Wait for result.
Paul Jensen25a217c2015-02-27 22:55:47 -0500509 mLaunchCaptivePortalAppBroadcastReceiver = new CustomIntentReceiver(
510 ACTION_LAUNCH_CAPTIVE_PORTAL_APP, new Random().nextInt(),
511 CMD_LAUNCH_CAPTIVE_PORTAL_APP);
Paul Jensen71b645f2014-10-13 14:13:07 -0400512 }
Paul Jensen25a217c2015-02-27 22:55:47 -0500513 // Display the sign in notification.
Paul Jensen71b645f2014-10-13 14:13:07 -0400514 Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 1,
515 mNetworkAgentInfo.network.netId,
Paul Jensen25a217c2015-02-27 22:55:47 -0500516 mLaunchCaptivePortalAppBroadcastReceiver.getPendingIntent());
Paul Jensen71b645f2014-10-13 14:13:07 -0400517 mConnectivityServiceHandler.sendMessage(message);
Paul Jensen869868be2014-05-15 10:33:05 -0400518 }
519
520 @Override
521 public boolean processMessage(Message message) {
522 if (DBG) log(getName() + message.toString());
Paul Jensen71b645f2014-10-13 14:13:07 -0400523 return NOT_HANDLED;
Paul Jensen869868be2014-05-15 10:33:05 -0400524 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400525 }
526
Paul Jensen71b645f2014-10-13 14:13:07 -0400527 // Being in the LingeringState State indicates a Network's validated bit is true and it once
528 // was the highest scoring Network satisfying a particular NetworkRequest, but since then
529 // another Network satsified the NetworkRequest with a higher score and hence this Network
530 // is "lingered" for a fixed period of time before it is disconnected. This period of time
531 // allows apps to wrap up communication and allows for seamless reactivation if the other
532 // higher scoring Network happens to disconnect.
Paul Jensenca8f16a2014-05-09 12:47:55 -0400533 private class LingeringState extends State {
Paul Jensen79a08052014-08-21 12:44:07 -0400534 private static final String ACTION_LINGER_EXPIRED = "android.net.netmon.lingerExpired";
Paul Jensen79a08052014-08-21 12:44:07 -0400535
Paul Jensendcbe8352014-09-16 16:28:34 -0400536 private CustomIntentReceiver mBroadcastReceiver;
Paul Jensen79a08052014-08-21 12:44:07 -0400537 private PendingIntent mIntent;
538
Paul Jensenca8f16a2014-05-09 12:47:55 -0400539 @Override
540 public void enter() {
Paul Jensen71b645f2014-10-13 14:13:07 -0400541 mLingerToken = new Random().nextInt();
542 mBroadcastReceiver = new CustomIntentReceiver(ACTION_LINGER_EXPIRED, mLingerToken,
Paul Jensendcbe8352014-09-16 16:28:34 -0400543 CMD_LINGER_EXPIRED);
544 mIntent = mBroadcastReceiver.getPendingIntent();
Paul Jensen79a08052014-08-21 12:44:07 -0400545 long wakeupTime = SystemClock.elapsedRealtime() + mLingerDelayMs;
546 mAlarmManager.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime,
547 // Give a specific window so we aren't subject to unknown inexactitude.
548 mLingerDelayMs / 6, mIntent);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400549 }
550
551 @Override
552 public boolean processMessage(Message message) {
553 if (DBG) log(getName() + message.toString());
554 switch (message.what) {
555 case CMD_NETWORK_CONNECTED:
556 // Go straight to active as we've already evaluated.
557 transitionTo(mValidatedState);
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400558 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400559 case CMD_LINGER_EXPIRED:
560 if (message.arg1 != mLingerToken)
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400561 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400562 mConnectivityServiceHandler.sendMessage(
563 obtainMessage(EVENT_NETWORK_LINGER_COMPLETE, mNetworkAgentInfo));
Paul Jensend6a3f7e2014-08-19 09:40:11 -0400564 return HANDLED;
Paul Jensenad50a1f2014-09-05 12:06:44 -0400565 case CMD_FORCE_REEVALUATION:
566 // Ignore reevaluation attempts when lingering. A reevaluation could result
567 // in a transition to the validated state which would abort the linger
568 // timeout. Lingering is the result of score assessment; validity is
569 // irrelevant.
570 return HANDLED;
Paul Jensen71b645f2014-10-13 14:13:07 -0400571 case CMD_CAPTIVE_PORTAL_APP_FINISHED:
572 // Ignore user network determination as this could abort linger timeout.
573 // Networks are only lingered once validated because:
574 // - Unvalidated networks are never lingered (see rematchNetworkAndRequests).
575 // - Once validated, a Network's validated bit is never cleared.
576 // Since networks are only lingered after being validated a user's
577 // determination will not change the death sentence that lingering entails:
578 // - If the user wants to use the network or bypasses the captive portal,
579 // the network's score will not be increased beyond its current value
580 // because it is already validated. Without a score increase there is no
581 // chance of reactivation (i.e. aborting linger timeout).
582 // - If the user does not want the network, lingering will disconnect the
583 // network anyhow.
584 return HANDLED;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400585 default:
586 return NOT_HANDLED;
587 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400588 }
Paul Jensen79a08052014-08-21 12:44:07 -0400589
590 @Override
591 public void exit() {
592 mAlarmManager.cancel(mIntent);
593 mContext.unregisterReceiver(mBroadcastReceiver);
594 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400595 }
596
597 /**
598 * Do a URL fetch on a known server to see if we get the data we expect.
599 * Returns HTTP response code.
600 */
601 private int isCaptivePortal() {
602 if (!mIsCaptivePortalCheckEnabled) return 204;
603
Paul Jensenca8f16a2014-05-09 12:47:55 -0400604 HttpURLConnection urlConnection = null;
Paul Jensen869868be2014-05-15 10:33:05 -0400605 int httpResponseCode = 599;
Paul Jensenca8f16a2014-05-09 12:47:55 -0400606 try {
Paul Jensene547ff22014-08-04 09:12:24 -0400607 URL url = new URL("http", mServer, "/generate_204");
Paul Jensen8fe17422015-02-02 11:03:03 -0500608 // On networks with a PAC instead of fetching a URL that should result in a 204
609 // reponse, we instead simply fetch the PAC script. This is done for a few reasons:
610 // 1. At present our PAC code does not yet handle multiple PACs on multiple networks
611 // until something like https://android-review.googlesource.com/#/c/115180/ lands.
612 // Network.openConnection() will ignore network-specific PACs and instead fetch
613 // using NO_PROXY. If a PAC is in place, the only fetch we know will succeed with
614 // NO_PROXY is the fetch of the PAC itself.
615 // 2. To proxy the generate_204 fetch through a PAC would require a number of things
616 // happen before the fetch can commence, namely:
617 // a) the PAC script be fetched
618 // b) a PAC script resolver service be fired up and resolve mServer
619 // Network validation could be delayed until these prerequisities are satisifed or
620 // could simply be left to race them. Neither is an optimal solution.
621 // 3. PAC scripts are sometimes used to block or restrict Internet access and may in
622 // fact block fetching of the generate_204 URL which would lead to false negative
623 // results for network validation.
624 boolean fetchPac = false;
625 {
626 final ProxyInfo proxyInfo = mNetworkAgentInfo.linkProperties.getHttpProxy();
627 if (proxyInfo != null && !Uri.EMPTY.equals(proxyInfo.getPacFileUrl())) {
628 url = new URL(proxyInfo.getPacFileUrl().toString());
629 fetchPac = true;
630 }
631 }
Paul Jensene547ff22014-08-04 09:12:24 -0400632 if (DBG) {
633 log("Checking " + url.toString() + " on " +
634 mNetworkAgentInfo.networkInfo.getExtraInfo());
Paul Jensenca8f16a2014-05-09 12:47:55 -0400635 }
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700636 urlConnection = (HttpURLConnection) mNetworkAgentInfo.network.openConnection(url);
Paul Jensen8fe17422015-02-02 11:03:03 -0500637 urlConnection.setInstanceFollowRedirects(fetchPac);
Paul Jensene547ff22014-08-04 09:12:24 -0400638 urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
639 urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
640 urlConnection.setUseCaches(false);
Paul Jensen306f1a42014-08-04 10:59:01 -0400641
642 // Time how long it takes to get a response to our request
643 long requestTimestamp = SystemClock.elapsedRealtime();
644
Paul Jensene547ff22014-08-04 09:12:24 -0400645 urlConnection.getInputStream();
Paul Jensen306f1a42014-08-04 10:59:01 -0400646
647 // Time how long it takes to get a response to our request
648 long responseTimestamp = SystemClock.elapsedRealtime();
649
Paul Jensene547ff22014-08-04 09:12:24 -0400650 httpResponseCode = urlConnection.getResponseCode();
651 if (DBG) {
652 log("isCaptivePortal: ret=" + httpResponseCode +
653 " headers=" + urlConnection.getHeaderFields());
654 }
655 // NOTE: We may want to consider an "HTTP/1.0 204" response to be a captive
656 // portal. The only example of this seen so far was a captive portal. For
657 // the time being go with prior behavior of assuming it's not a captive
658 // portal. If it is considered a captive portal, a different sign-in URL
659 // is needed (i.e. can't browse a 204). This could be the result of an HTTP
660 // proxy server.
661
662 // Consider 200 response with "Content-length=0" to not be a captive portal.
663 // There's no point in considering this a captive portal as the user cannot
664 // sign-in to an empty page. Probably the result of a broken transparent proxy.
665 // See http://b/9972012.
666 if (httpResponseCode == 200 && urlConnection.getContentLength() == 0) {
667 if (DBG) log("Empty 200 response interpreted as 204 response.");
668 httpResponseCode = 204;
669 }
Paul Jensen306f1a42014-08-04 10:59:01 -0400670
Paul Jensen8fe17422015-02-02 11:03:03 -0500671 if (httpResponseCode == 200 && fetchPac) {
672 if (DBG) log("PAC fetch 200 response interpreted as 204 response.");
673 httpResponseCode = 204;
674 }
675
Jeff Davidsonf9fff922014-12-05 16:56:08 -0800676 sendNetworkConditionsBroadcast(true /* response received */,
677 httpResponseCode != 204 /* isCaptivePortal */,
Paul Jensen306f1a42014-08-04 10:59:01 -0400678 requestTimestamp, responseTimestamp);
Paul Jensenca8f16a2014-05-09 12:47:55 -0400679 } catch (IOException e) {
680 if (DBG) log("Probably not a portal: exception " + e);
Paul Jensen869868be2014-05-15 10:33:05 -0400681 if (httpResponseCode == 599) {
682 // TODO: Ping gateway and DNS server and log results.
683 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400684 } finally {
685 if (urlConnection != null) {
686 urlConnection.disconnect();
687 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400688 }
689 return httpResponseCode;
690 }
Paul Jensen306f1a42014-08-04 10:59:01 -0400691
692 /**
693 * @param responseReceived - whether or not we received a valid HTTP response to our request.
694 * If false, isCaptivePortal and responseTimestampMs are ignored
695 * TODO: This should be moved to the transports. The latency could be passed to the transports
696 * along with the captive portal result. Currently the TYPE_MOBILE broadcasts appear unused so
697 * perhaps this could just be added to the WiFi transport only.
698 */
699 private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal,
700 long requestTimestampMs, long responseTimestampMs) {
701 if (Settings.Global.getInt(mContext.getContentResolver(),
702 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 0) {
703 if (DBG) log("Don't send network conditions - lacking user consent.");
704 return;
705 }
706
Robert Greenwaltfb68f8f2014-08-13 13:43:32 -0700707 if (systemReady == false) return;
708
Paul Jensen306f1a42014-08-04 10:59:01 -0400709 Intent latencyBroadcast = new Intent(ACTION_NETWORK_CONDITIONS_MEASURED);
710 switch (mNetworkAgentInfo.networkInfo.getType()) {
711 case ConnectivityManager.TYPE_WIFI:
712 WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo();
713 if (currentWifiInfo != null) {
714 // NOTE: getSSID()'s behavior changed in API 17; before that, SSIDs were not
715 // surrounded by double quotation marks (thus violating the Javadoc), but this
716 // was changed to match the Javadoc in API 17. Since clients may have started
717 // sanitizing the output of this method since API 17 was released, we should
718 // not change it here as it would become impossible to tell whether the SSID is
719 // simply being surrounded by quotes due to the API, or whether those quotes
720 // are actually part of the SSID.
721 latencyBroadcast.putExtra(EXTRA_SSID, currentWifiInfo.getSSID());
722 latencyBroadcast.putExtra(EXTRA_BSSID, currentWifiInfo.getBSSID());
723 } else {
724 if (DBG) logw("network info is TYPE_WIFI but no ConnectionInfo found");
725 return;
726 }
727 break;
728 case ConnectivityManager.TYPE_MOBILE:
729 latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType());
730 List<CellInfo> info = mTelephonyManager.getAllCellInfo();
731 if (info == null) return;
732 int numRegisteredCellInfo = 0;
733 for (CellInfo cellInfo : info) {
734 if (cellInfo.isRegistered()) {
735 numRegisteredCellInfo++;
736 if (numRegisteredCellInfo > 1) {
737 if (DBG) log("more than one registered CellInfo. Can't " +
738 "tell which is active. Bailing.");
739 return;
740 }
741 if (cellInfo instanceof CellInfoCdma) {
742 CellIdentityCdma cellId = ((CellInfoCdma) cellInfo).getCellIdentity();
743 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
744 } else if (cellInfo instanceof CellInfoGsm) {
745 CellIdentityGsm cellId = ((CellInfoGsm) cellInfo).getCellIdentity();
746 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
747 } else if (cellInfo instanceof CellInfoLte) {
748 CellIdentityLte cellId = ((CellInfoLte) cellInfo).getCellIdentity();
749 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
750 } else if (cellInfo instanceof CellInfoWcdma) {
751 CellIdentityWcdma cellId = ((CellInfoWcdma) cellInfo).getCellIdentity();
752 latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
753 } else {
754 if (DBG) logw("Registered cellinfo is unrecognized");
755 return;
756 }
757 }
758 }
759 break;
760 default:
761 return;
762 }
763 latencyBroadcast.putExtra(EXTRA_CONNECTIVITY_TYPE, mNetworkAgentInfo.networkInfo.getType());
764 latencyBroadcast.putExtra(EXTRA_RESPONSE_RECEIVED, responseReceived);
765 latencyBroadcast.putExtra(EXTRA_REQUEST_TIMESTAMP_MS, requestTimestampMs);
766
767 if (responseReceived) {
768 latencyBroadcast.putExtra(EXTRA_IS_CAPTIVE_PORTAL, isCaptivePortal);
769 latencyBroadcast.putExtra(EXTRA_RESPONSE_TIMESTAMP_MS, responseTimestampMs);
770 }
Paul Jensen55298582014-08-20 11:01:41 -0400771 mContext.sendBroadcastAsUser(latencyBroadcast, UserHandle.CURRENT,
772 PERMISSION_ACCESS_NETWORK_CONDITIONS);
Paul Jensen306f1a42014-08-04 10:59:01 -0400773 }
Paul Jensenca8f16a2014-05-09 12:47:55 -0400774}