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