Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 android.net; |
| 18 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 20 | import android.app.Notification; |
| 21 | import android.app.NotificationManager; |
| 22 | import android.app.PendingIntent; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.content.res.Resources; |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 28 | import android.database.ContentObserver; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 29 | import android.net.ConnectivityManager; |
| 30 | import android.net.IConnectivityManager; |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 31 | import android.net.wifi.WifiInfo; |
| 32 | import android.net.wifi.WifiManager; |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 33 | import android.os.Handler; |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 34 | import android.os.UserHandle; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 35 | import android.os.Message; |
| 36 | import android.os.RemoteException; |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 37 | import android.os.SystemClock; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 38 | import android.provider.Settings; |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 39 | import android.telephony.CellIdentityCdma; |
| 40 | import android.telephony.CellIdentityGsm; |
| 41 | import android.telephony.CellIdentityLte; |
| 42 | import android.telephony.CellIdentityWcdma; |
| 43 | import android.telephony.CellInfo; |
| 44 | import android.telephony.CellInfoCdma; |
| 45 | import android.telephony.CellInfoGsm; |
| 46 | import android.telephony.CellInfoLte; |
| 47 | import android.telephony.CellInfoWcdma; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 48 | import android.telephony.TelephonyManager; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 49 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 50 | import com.android.internal.util.State; |
| 51 | import com.android.internal.util.StateMachine; |
| 52 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 53 | import java.io.IOException; |
| 54 | import java.net.HttpURLConnection; |
| 55 | import java.net.InetAddress; |
| 56 | import java.net.Inet4Address; |
Wink Saville | bf34122 | 2013-08-02 11:25:23 -0700 | [diff] [blame] | 57 | import java.net.SocketTimeoutException; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 58 | import java.net.URL; |
| 59 | import java.net.UnknownHostException; |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 60 | import java.util.List; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 61 | |
| 62 | import com.android.internal.R; |
| 63 | |
| 64 | /** |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 65 | * This class allows captive portal detection on a network. |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 66 | * @hide |
| 67 | */ |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 68 | public class CaptivePortalTracker extends StateMachine { |
Wink Saville | bf34122 | 2013-08-02 11:25:23 -0700 | [diff] [blame] | 69 | private static final boolean DBG = true; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 70 | private static final String TAG = "CaptivePortalTracker"; |
| 71 | |
| 72 | private static final String DEFAULT_SERVER = "clients3.google.com"; |
| 73 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
| 74 | |
| 75 | private static final int SOCKET_TIMEOUT_MS = 10000; |
| 76 | |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 77 | public static final String ACTION_NETWORK_CONDITIONS_MEASURED = |
| 78 | "android.net.conn.NETWORK_CONDITIONS_MEASURED"; |
| 79 | public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type"; |
| 80 | public static final String EXTRA_NETWORK_TYPE = "extra_network_type"; |
| 81 | public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received"; |
| 82 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal"; |
| 83 | public static final String EXTRA_CELL_ID = "extra_cellid"; |
| 84 | public static final String EXTRA_SSID = "extra_ssid"; |
| 85 | public static final String EXTRA_BSSID = "extra_bssid"; |
| 86 | /** real time since boot */ |
| 87 | public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms"; |
| 88 | public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms"; |
| 89 | |
| 90 | private static final String PERMISSION_ACCESS_NETWORK_CONDITIONS = |
| 91 | "android.permission.ACCESS_NETWORK_CONDITIONS"; |
| 92 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 93 | private String mServer; |
| 94 | private String mUrl; |
| 95 | private boolean mNotificationShown = false; |
| 96 | private boolean mIsCaptivePortalCheckEnabled = false; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 97 | private IConnectivityManager mConnService; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 98 | private TelephonyManager mTelephonyManager; |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 99 | private WifiManager mWifiManager; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 100 | private Context mContext; |
| 101 | private NetworkInfo mNetworkInfo; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 102 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 103 | private static final int CMD_DETECT_PORTAL = 0; |
| 104 | private static final int CMD_CONNECTIVITY_CHANGE = 1; |
| 105 | private static final int CMD_DELAYED_CAPTIVE_CHECK = 2; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 106 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 107 | /* This delay happens every time before we do a captive check on a network */ |
| 108 | private static final int DELAYED_CHECK_INTERVAL_MS = 10000; |
| 109 | private int mDelayedCheckToken = 0; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 110 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 111 | private State mDefaultState = new DefaultState(); |
| 112 | private State mNoActiveNetworkState = new NoActiveNetworkState(); |
| 113 | private State mActiveNetworkState = new ActiveNetworkState(); |
| 114 | private State mDelayedCaptiveCheckState = new DelayedCaptiveCheckState(); |
| 115 | |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 116 | private static final String SETUP_WIZARD_PACKAGE = "com.google.android.setupwizard"; |
| 117 | private boolean mDeviceProvisioned = false; |
| 118 | private ProvisioningObserver mProvisioningObserver; |
| 119 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 120 | private CaptivePortalTracker(Context context, IConnectivityManager cs) { |
| 121 | super(TAG); |
| 122 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 123 | mContext = context; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 124 | mConnService = cs; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 125 | mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 126 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 127 | mProvisioningObserver = new ProvisioningObserver(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 128 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 129 | IntentFilter filter = new IntentFilter(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 130 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 131 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 132 | mContext.registerReceiver(mReceiver, filter); |
| 133 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 134 | mServer = Settings.Global.getString(mContext.getContentResolver(), |
| 135 | Settings.Global.CAPTIVE_PORTAL_SERVER); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 136 | if (mServer == null) mServer = DEFAULT_SERVER; |
| 137 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 138 | mIsCaptivePortalCheckEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 139 | Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 1) == 1; |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 140 | |
| 141 | addState(mDefaultState); |
| 142 | addState(mNoActiveNetworkState, mDefaultState); |
| 143 | addState(mActiveNetworkState, mDefaultState); |
| 144 | addState(mDelayedCaptiveCheckState, mActiveNetworkState); |
| 145 | setInitialState(mNoActiveNetworkState); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 148 | private class ProvisioningObserver extends ContentObserver { |
| 149 | ProvisioningObserver() { |
| 150 | super(new Handler()); |
| 151 | mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor( |
| 152 | Settings.Global.DEVICE_PROVISIONED), false, this); |
| 153 | onChange(false); // load initial value |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void onChange(boolean selfChange) { |
| 158 | mDeviceProvisioned = Settings.Global.getInt(mContext.getContentResolver(), |
| 159 | Settings.Global.DEVICE_PROVISIONED, 0) != 0; |
| 160 | } |
| 161 | } |
| 162 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 163 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 164 | @Override |
| 165 | public void onReceive(Context context, Intent intent) { |
| 166 | String action = intent.getAction(); |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 167 | // Normally, we respond to CONNECTIVITY_ACTION, allowing time for the change in |
| 168 | // connectivity to stabilize, but if the device is not yet provisioned, respond |
| 169 | // immediately to speed up transit through the setup wizard. |
| 170 | if ((mDeviceProvisioned && action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) |
| 171 | || (!mDeviceProvisioned |
| 172 | && action.equals(ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE))) { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 173 | NetworkInfo info = intent.getParcelableExtra( |
| 174 | ConnectivityManager.EXTRA_NETWORK_INFO); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 175 | sendMessage(obtainMessage(CMD_CONNECTIVITY_CHANGE, info)); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | }; |
| 179 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 180 | public static CaptivePortalTracker makeCaptivePortalTracker(Context context, |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 181 | IConnectivityManager cs) { |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 182 | CaptivePortalTracker captivePortal = new CaptivePortalTracker(context, cs); |
| 183 | captivePortal.start(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 184 | return captivePortal; |
| 185 | } |
| 186 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 187 | public void detectCaptivePortal(NetworkInfo info) { |
| 188 | sendMessage(obtainMessage(CMD_DETECT_PORTAL, info)); |
| 189 | } |
| 190 | |
| 191 | private class DefaultState extends State { |
| 192 | @Override |
| 193 | public void enter() { |
| 194 | if (DBG) log(getName() + "\n"); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 198 | public boolean processMessage(Message message) { |
| 199 | if (DBG) log(getName() + message.toString() + "\n"); |
| 200 | switch (message.what) { |
| 201 | case CMD_DETECT_PORTAL: |
| 202 | NetworkInfo info = (NetworkInfo) message.obj; |
| 203 | // Checking on a secondary connection is not supported |
| 204 | // yet |
| 205 | notifyPortalCheckComplete(info); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 206 | break; |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 207 | case CMD_CONNECTIVITY_CHANGE: |
| 208 | case CMD_DELAYED_CAPTIVE_CHECK: |
| 209 | break; |
| 210 | default: |
| 211 | loge("Ignoring " + message); |
| 212 | break; |
| 213 | } |
| 214 | return HANDLED; |
| 215 | } |
| 216 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 217 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 218 | private class NoActiveNetworkState extends State { |
| 219 | @Override |
| 220 | public void enter() { |
| 221 | if (DBG) log(getName() + "\n"); |
| 222 | mNetworkInfo = null; |
| 223 | /* Clear any previous notification */ |
| 224 | setNotificationVisible(false); |
| 225 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 226 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 227 | @Override |
| 228 | public boolean processMessage(Message message) { |
| 229 | if (DBG) log(getName() + message.toString() + "\n"); |
| 230 | InetAddress server; |
| 231 | NetworkInfo info; |
| 232 | switch (message.what) { |
| 233 | case CMD_CONNECTIVITY_CHANGE: |
| 234 | info = (NetworkInfo) message.obj; |
| 235 | if (info.isConnected() && isActiveNetwork(info)) { |
| 236 | mNetworkInfo = info; |
| 237 | transitionTo(mDelayedCaptiveCheckState); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 238 | } |
| 239 | break; |
| 240 | default: |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 241 | return NOT_HANDLED; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 242 | } |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 243 | return HANDLED; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 247 | private class ActiveNetworkState extends State { |
| 248 | @Override |
| 249 | public void enter() { |
| 250 | if (DBG) log(getName() + "\n"); |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public boolean processMessage(Message message) { |
| 255 | NetworkInfo info; |
| 256 | switch (message.what) { |
| 257 | case CMD_CONNECTIVITY_CHANGE: |
| 258 | info = (NetworkInfo) message.obj; |
| 259 | if (!info.isConnected() |
| 260 | && info.getType() == mNetworkInfo.getType()) { |
| 261 | if (DBG) log("Disconnected from active network " + info); |
| 262 | transitionTo(mNoActiveNetworkState); |
| 263 | } else if (info.getType() != mNetworkInfo.getType() && |
| 264 | info.isConnected() && |
| 265 | isActiveNetwork(info)) { |
| 266 | if (DBG) log("Active network switched " + info); |
| 267 | deferMessage(message); |
| 268 | transitionTo(mNoActiveNetworkState); |
| 269 | } |
| 270 | break; |
| 271 | default: |
| 272 | return NOT_HANDLED; |
| 273 | } |
| 274 | return HANDLED; |
| 275 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 278 | |
| 279 | |
| 280 | private class DelayedCaptiveCheckState extends State { |
| 281 | @Override |
| 282 | public void enter() { |
| 283 | if (DBG) log(getName() + "\n"); |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 284 | Message message = obtainMessage(CMD_DELAYED_CAPTIVE_CHECK, ++mDelayedCheckToken, 0); |
| 285 | if (mDeviceProvisioned) { |
| 286 | sendMessageDelayed(message, DELAYED_CHECK_INTERVAL_MS); |
| 287 | } else { |
| 288 | sendMessage(message); |
| 289 | } |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public boolean processMessage(Message message) { |
| 294 | if (DBG) log(getName() + message.toString() + "\n"); |
| 295 | switch (message.what) { |
| 296 | case CMD_DELAYED_CAPTIVE_CHECK: |
| 297 | if (message.arg1 == mDelayedCheckToken) { |
| 298 | InetAddress server = lookupHost(mServer); |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 299 | boolean captive = server != null && isCaptivePortal(server); |
| 300 | if (captive) { |
| 301 | if (DBG) log("Captive network " + mNetworkInfo); |
| 302 | } else { |
| 303 | if (DBG) log("Not captive network " + mNetworkInfo); |
| 304 | } |
| 305 | if (mDeviceProvisioned) { |
| 306 | if (captive) { |
| 307 | // Setup Wizard will assist the user in connecting to a captive |
| 308 | // portal, so make the notification visible unless during setup |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 309 | setNotificationVisible(true); |
| 310 | } |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 311 | } else { |
| 312 | Intent intent = new Intent( |
| 313 | ConnectivityManager.ACTION_CAPTIVE_PORTAL_TEST_COMPLETED); |
| 314 | intent.putExtra(ConnectivityManager.EXTRA_IS_CAPTIVE_PORTAL, captive); |
| 315 | intent.setPackage(SETUP_WIZARD_PACKAGE); |
| 316 | mContext.sendBroadcast(intent); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 317 | } |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 318 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 319 | transitionTo(mActiveNetworkState); |
| 320 | } |
| 321 | break; |
| 322 | default: |
| 323 | return NOT_HANDLED; |
| 324 | } |
| 325 | return HANDLED; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | private void notifyPortalCheckComplete(NetworkInfo info) { |
| 330 | if (info == null) { |
| 331 | loge("notifyPortalCheckComplete on null"); |
| 332 | return; |
| 333 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 334 | try { |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 335 | mConnService.captivePortalCheckComplete(info); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 336 | } catch(RemoteException e) { |
| 337 | e.printStackTrace(); |
| 338 | } |
| 339 | } |
| 340 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 341 | private boolean isActiveNetwork(NetworkInfo info) { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 342 | try { |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 343 | NetworkInfo active = mConnService.getActiveNetworkInfo(); |
| 344 | if (active != null && active.getType() == info.getType()) { |
| 345 | return true; |
| 346 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 347 | } catch (RemoteException e) { |
| 348 | e.printStackTrace(); |
| 349 | } |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 350 | return false; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 354 | * Do a URL fetch on a known server to see if we get the data we expect. |
| 355 | * Measure the response time and broadcast that. |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 356 | */ |
| 357 | private boolean isCaptivePortal(InetAddress server) { |
| 358 | HttpURLConnection urlConnection = null; |
| 359 | if (!mIsCaptivePortalCheckEnabled) return false; |
| 360 | |
| 361 | mUrl = "http://" + server.getHostAddress() + "/generate_204"; |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 362 | if (DBG) log("Checking " + mUrl); |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 363 | long requestTimestamp = -1; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 364 | try { |
| 365 | URL url = new URL(mUrl); |
| 366 | urlConnection = (HttpURLConnection) url.openConnection(); |
| 367 | urlConnection.setInstanceFollowRedirects(false); |
| 368 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 369 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 370 | urlConnection.setUseCaches(false); |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 371 | |
| 372 | // Time how long it takes to get a response to our request |
| 373 | requestTimestamp = SystemClock.elapsedRealtime(); |
| 374 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 375 | urlConnection.getInputStream(); |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 376 | |
| 377 | // Time how long it takes to get a response to our request |
| 378 | long responseTimestamp = SystemClock.elapsedRealtime(); |
| 379 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 380 | // we got a valid response, but not from the real google |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 381 | boolean isCaptivePortal = urlConnection.getResponseCode() != 204; |
| 382 | |
| 383 | sendNetworkConditionsBroadcast(true /* response received */, isCaptivePortal, |
| 384 | requestTimestamp, responseTimestamp); |
| 385 | return isCaptivePortal; |
Wink Saville | bf34122 | 2013-08-02 11:25:23 -0700 | [diff] [blame] | 386 | } catch (SocketTimeoutException e) { |
| 387 | if (DBG) log("Probably a portal: exception " + e); |
Wink Saville | f35f320 | 2013-08-08 16:51:46 -0700 | [diff] [blame] | 388 | if (requestTimestamp != -1) { |
| 389 | sendFailedCaptivePortalCheckBroadcast(requestTimestamp); |
| 390 | } // else something went wrong with setting up the urlConnection |
Wink Saville | bf34122 | 2013-08-02 11:25:23 -0700 | [diff] [blame] | 391 | return true; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 392 | } catch (IOException e) { |
| 393 | if (DBG) log("Probably not a portal: exception " + e); |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 394 | if (requestTimestamp != -1) { |
| 395 | sendFailedCaptivePortalCheckBroadcast(requestTimestamp); |
| 396 | } // else something went wrong with setting up the urlConnection |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 397 | return false; |
| 398 | } finally { |
| 399 | if (urlConnection != null) { |
| 400 | urlConnection.disconnect(); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private InetAddress lookupHost(String hostname) { |
| 406 | InetAddress inetAddress[]; |
| 407 | try { |
| 408 | inetAddress = InetAddress.getAllByName(hostname); |
| 409 | } catch (UnknownHostException e) { |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 410 | sendFailedCaptivePortalCheckBroadcast(SystemClock.elapsedRealtime()); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 411 | return null; |
| 412 | } |
| 413 | |
| 414 | for (InetAddress a : inetAddress) { |
| 415 | if (a instanceof Inet4Address) return a; |
| 416 | } |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 417 | |
| 418 | sendFailedCaptivePortalCheckBroadcast(SystemClock.elapsedRealtime()); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 419 | return null; |
| 420 | } |
| 421 | |
| 422 | private void setNotificationVisible(boolean visible) { |
| 423 | // if it should be hidden and it is already hidden, then noop |
| 424 | if (!visible && !mNotificationShown) { |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | Resources r = Resources.getSystem(); |
| 429 | NotificationManager notificationManager = (NotificationManager) mContext |
| 430 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 431 | |
| 432 | if (visible) { |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 433 | CharSequence title; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 434 | CharSequence details; |
Irfan Sheriff | ebb8f41 | 2012-10-02 16:12:05 -0700 | [diff] [blame] | 435 | int icon; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 436 | switch (mNetworkInfo.getType()) { |
| 437 | case ConnectivityManager.TYPE_WIFI: |
| 438 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 439 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 440 | mNetworkInfo.getExtraInfo()); |
Irfan Sheriff | ebb8f41 | 2012-10-02 16:12:05 -0700 | [diff] [blame] | 441 | icon = R.drawable.stat_notify_wifi_in_range; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 442 | break; |
| 443 | case ConnectivityManager.TYPE_MOBILE: |
| 444 | title = r.getString(R.string.network_available_sign_in, 0); |
| 445 | // TODO: Change this to pull from NetworkInfo once a printable |
| 446 | // name has been added to it |
| 447 | details = mTelephonyManager.getNetworkOperatorName(); |
Irfan Sheriff | ebb8f41 | 2012-10-02 16:12:05 -0700 | [diff] [blame] | 448 | icon = R.drawable.stat_notify_rssi_in_range; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 449 | break; |
| 450 | default: |
| 451 | title = r.getString(R.string.network_available_sign_in, 0); |
| 452 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 453 | mNetworkInfo.getExtraInfo()); |
Irfan Sheriff | ebb8f41 | 2012-10-02 16:12:05 -0700 | [diff] [blame] | 454 | icon = R.drawable.stat_notify_rssi_in_range; |
Irfan Sheriff | b8aad91f | 2012-10-02 14:01:28 -0700 | [diff] [blame] | 455 | break; |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 456 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 457 | |
| 458 | Notification notification = new Notification(); |
| 459 | notification.when = 0; |
Irfan Sheriff | ebb8f41 | 2012-10-02 16:12:05 -0700 | [diff] [blame] | 460 | notification.icon = icon; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 461 | notification.flags = Notification.FLAG_AUTO_CANCEL; |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 462 | Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mUrl)); |
| 463 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 464 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 465 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 466 | notification.tickerText = title; |
| 467 | notification.setLatestEventInfo(mContext, title, details, notification.contentIntent); |
| 468 | |
| 469 | notificationManager.notify(NOTIFICATION_ID, 1, notification); |
| 470 | } else { |
| 471 | notificationManager.cancel(NOTIFICATION_ID, 1); |
| 472 | } |
| 473 | mNotificationShown = visible; |
| 474 | } |
Brian Williammee | 1ed5162 | 2013-07-31 17:07:33 -0700 | [diff] [blame] | 475 | |
| 476 | private void sendFailedCaptivePortalCheckBroadcast(long requestTimestampMs) { |
| 477 | sendNetworkConditionsBroadcast(false /* response received */, false /* ignored */, |
| 478 | requestTimestampMs, 0 /* ignored */); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * @param responseReceived - whether or not we received a valid HTTP response to our request. |
| 483 | * If false, isCaptivePortal and responseTimestampMs are ignored |
| 484 | */ |
| 485 | private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal, |
| 486 | long requestTimestampMs, long responseTimestampMs) { |
| 487 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 488 | Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 0) { |
| 489 | if (DBG) log("Don't send network conditions - lacking user consent."); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | Intent latencyBroadcast = new Intent(ACTION_NETWORK_CONDITIONS_MEASURED); |
| 494 | switch (mNetworkInfo.getType()) { |
| 495 | case ConnectivityManager.TYPE_WIFI: |
| 496 | WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo(); |
| 497 | if (currentWifiInfo != null) { |
| 498 | latencyBroadcast.putExtra(EXTRA_SSID, currentWifiInfo.getSSID()); |
| 499 | latencyBroadcast.putExtra(EXTRA_BSSID, currentWifiInfo.getBSSID()); |
| 500 | } else { |
| 501 | if (DBG) logw("network info is TYPE_WIFI but no ConnectionInfo found"); |
| 502 | return; |
| 503 | } |
| 504 | break; |
| 505 | case ConnectivityManager.TYPE_MOBILE: |
| 506 | latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType()); |
| 507 | List<CellInfo> info = mTelephonyManager.getAllCellInfo(); |
| 508 | if (info == null) return; |
| 509 | StringBuffer uniqueCellId = new StringBuffer(); |
| 510 | int numRegisteredCellInfo = 0; |
| 511 | for (CellInfo cellInfo : info) { |
| 512 | if (cellInfo.isRegistered()) { |
| 513 | numRegisteredCellInfo++; |
| 514 | if (numRegisteredCellInfo > 1) { |
| 515 | if (DBG) log("more than one registered CellInfo. Can't " + |
| 516 | "tell which is active. Bailing."); |
| 517 | return; |
| 518 | } |
| 519 | if (cellInfo instanceof CellInfoCdma) { |
| 520 | CellIdentityCdma cellId = ((CellInfoCdma) cellInfo).getCellIdentity(); |
| 521 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 522 | } else if (cellInfo instanceof CellInfoGsm) { |
| 523 | CellIdentityGsm cellId = ((CellInfoGsm) cellInfo).getCellIdentity(); |
| 524 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 525 | } else if (cellInfo instanceof CellInfoLte) { |
| 526 | CellIdentityLte cellId = ((CellInfoLte) cellInfo).getCellIdentity(); |
| 527 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 528 | } else if (cellInfo instanceof CellInfoWcdma) { |
| 529 | CellIdentityWcdma cellId = ((CellInfoWcdma) cellInfo).getCellIdentity(); |
| 530 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 531 | } else { |
| 532 | if (DBG) logw("Registered cellinfo is unrecognized"); |
| 533 | return; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | break; |
| 538 | default: |
| 539 | return; |
| 540 | } |
| 541 | latencyBroadcast.putExtra(EXTRA_CONNECTIVITY_TYPE, mNetworkInfo.getType()); |
| 542 | latencyBroadcast.putExtra(EXTRA_RESPONSE_RECEIVED, responseReceived); |
| 543 | latencyBroadcast.putExtra(EXTRA_REQUEST_TIMESTAMP_MS, requestTimestampMs); |
| 544 | |
| 545 | if (responseReceived) { |
| 546 | latencyBroadcast.putExtra(EXTRA_IS_CAPTIVE_PORTAL, isCaptivePortal); |
| 547 | latencyBroadcast.putExtra(EXTRA_RESPONSE_TIMESTAMP_MS, responseTimestampMs); |
| 548 | } |
| 549 | mContext.sendBroadcast(latencyBroadcast, PERMISSION_ACCESS_NETWORK_CONDITIONS); |
| 550 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 551 | } |