Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.captiveportallogin; |
| 18 | |
| 19 | import android.app.Activity; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 20 | import android.app.LoadedApk; |
| 21 | import android.content.Context; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.net.ConnectivityManager; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 25 | import android.net.ConnectivityManager.NetworkCallback; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 26 | import android.net.LinkProperties; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 27 | import android.net.Network; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 28 | import android.net.NetworkCapabilities; |
| 29 | import android.net.NetworkRequest; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 30 | import android.net.Proxy; |
| 31 | import android.net.ProxyInfo; |
| 32 | import android.net.Uri; |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame^] | 33 | import android.net.http.SslError; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 34 | import android.os.Bundle; |
| 35 | import android.provider.Settings; |
| 36 | import android.provider.Settings.Global; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 37 | import android.util.ArrayMap; |
| 38 | import android.util.Log; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 39 | import android.view.Menu; |
| 40 | import android.view.MenuItem; |
Paul Jensen | 8f333f1 | 2014-08-05 22:52:16 -0400 | [diff] [blame] | 41 | import android.view.View; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 42 | import android.view.Window; |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame^] | 43 | import android.webkit.SslErrorHandler; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 44 | import android.webkit.WebChromeClient; |
| 45 | import android.webkit.WebSettings; |
| 46 | import android.webkit.WebView; |
| 47 | import android.webkit.WebViewClient; |
Paul Jensen | 8f333f1 | 2014-08-05 22:52:16 -0400 | [diff] [blame] | 48 | import android.widget.ProgressBar; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 49 | |
| 50 | import java.io.IOException; |
| 51 | import java.net.HttpURLConnection; |
| 52 | import java.net.MalformedURLException; |
| 53 | import java.net.URL; |
| 54 | import java.lang.InterruptedException; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 55 | import java.lang.reflect.Field; |
| 56 | import java.lang.reflect.Method; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 57 | |
| 58 | public class CaptivePortalLoginActivity extends Activity { |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 59 | private static final String TAG = "CaptivePortalLogin"; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 60 | private static final String DEFAULT_SERVER = "clients3.google.com"; |
| 61 | private static final int SOCKET_TIMEOUT_MS = 10000; |
| 62 | |
| 63 | // Keep this in sync with NetworkMonitor. |
| 64 | // Intent broadcast to ConnectivityService indicating sign-in is complete. |
| 65 | // Extras: |
| 66 | // EXTRA_TEXT = netId |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 67 | // LOGGED_IN_RESULT = one of the CAPTIVE_PORTAL_APP_RETURN_* values below. |
| 68 | // RESPONSE_TOKEN = data fragment from launching Intent |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 69 | private static final String ACTION_CAPTIVE_PORTAL_LOGGED_IN = |
| 70 | "android.net.netmon.captive_portal_logged_in"; |
| 71 | private static final String LOGGED_IN_RESULT = "result"; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 72 | private static final int CAPTIVE_PORTAL_APP_RETURN_APPEASED = 0; |
| 73 | private static final int CAPTIVE_PORTAL_APP_RETURN_UNWANTED = 1; |
| 74 | private static final int CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS = 2; |
| 75 | private static final String RESPONSE_TOKEN = "response_token"; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 76 | |
| 77 | private URL mURL; |
| 78 | private int mNetId; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 79 | private String mResponseToken; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 80 | private NetworkCallback mNetworkCallback; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | protected void onCreate(Bundle savedInstanceState) { |
| 84 | super.onCreate(savedInstanceState); |
| 85 | |
| 86 | String server = Settings.Global.getString(getContentResolver(), "captive_portal_server"); |
| 87 | if (server == null) server = DEFAULT_SERVER; |
| 88 | try { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 89 | mURL = new URL("http", server, "/generate_204"); |
| 90 | final Uri dataUri = getIntent().getData(); |
| 91 | if (!dataUri.getScheme().equals("netid")) { |
| 92 | throw new MalformedURLException(); |
| 93 | } |
| 94 | mNetId = Integer.parseInt(dataUri.getSchemeSpecificPart()); |
| 95 | mResponseToken = dataUri.getFragment(); |
| 96 | } catch (MalformedURLException|NumberFormatException e) { |
| 97 | // System misconfigured, bail out in a way that at least provides network access. |
| 98 | done(CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 101 | final Network network = new Network(mNetId); |
| 102 | ConnectivityManager.setProcessDefaultNetwork(network); |
| 103 | |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 104 | // Set HTTP proxy system properties to those of the selected Network. |
| 105 | final LinkProperties lp = ConnectivityManager.from(this).getLinkProperties(network); |
| 106 | if (lp != null) { |
| 107 | final ProxyInfo proxyInfo = lp.getHttpProxy(); |
| 108 | String host = ""; |
| 109 | String port = ""; |
| 110 | String exclList = ""; |
| 111 | Uri pacFileUrl = Uri.EMPTY; |
| 112 | if (proxyInfo != null) { |
| 113 | host = proxyInfo.getHost(); |
| 114 | port = Integer.toString(proxyInfo.getPort()); |
| 115 | exclList = proxyInfo.getExclusionListAsString(); |
| 116 | pacFileUrl = proxyInfo.getPacFileUrl(); |
| 117 | } |
| 118 | Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl); |
| 119 | Log.v(TAG, "Set proxy system properties to " + proxyInfo); |
| 120 | } |
| 121 | |
| 122 | // Proxy system properties must be initialized before setContentView is called because |
| 123 | // setContentView initializes the WebView logic which in turn reads the system properties. |
| 124 | setContentView(R.layout.activity_captive_portal_login); |
| 125 | |
| 126 | getActionBar().setDisplayShowHomeEnabled(false); |
| 127 | |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 128 | // Exit app if Network disappears. |
| 129 | final NetworkCapabilities networkCapabilities = |
| 130 | ConnectivityManager.from(this).getNetworkCapabilities(network); |
| 131 | if (networkCapabilities == null) { |
| 132 | finish(); |
| 133 | return; |
| 134 | } |
| 135 | mNetworkCallback = new NetworkCallback() { |
| 136 | @Override |
| 137 | public void onLost(Network lostNetwork) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 138 | if (network.equals(lostNetwork)) done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED); |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 139 | } |
| 140 | }; |
| 141 | final NetworkRequest.Builder builder = new NetworkRequest.Builder(); |
| 142 | for (int transportType : networkCapabilities.getTransportTypes()) { |
| 143 | builder.addTransportType(transportType); |
| 144 | } |
| 145 | ConnectivityManager.from(this).registerNetworkCallback(builder.build(), mNetworkCallback); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 146 | |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 147 | final WebView myWebView = (WebView) findViewById(R.id.webview); |
| 148 | myWebView.clearCache(true); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 149 | WebSettings webSettings = myWebView.getSettings(); |
| 150 | webSettings.setJavaScriptEnabled(true); |
| 151 | myWebView.setWebViewClient(new MyWebViewClient()); |
| 152 | myWebView.setWebChromeClient(new MyWebChromeClient()); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 153 | // Start initial page load so WebView finishes loading proxy settings. |
| 154 | // Actual load of mUrl is initiated by MyWebViewClient. |
| 155 | myWebView.loadData("", "text/html", null); |
| 156 | } |
| 157 | |
| 158 | // Find WebView's proxy BroadcastReceiver and prompt it to read proxy system properties. |
| 159 | private void setWebViewProxy() { |
| 160 | LoadedApk loadedApk = getApplication().mLoadedApk; |
| 161 | try { |
| 162 | Field receiversField = LoadedApk.class.getDeclaredField("mReceivers"); |
| 163 | receiversField.setAccessible(true); |
| 164 | ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); |
| 165 | for (Object receiverMap : receivers.values()) { |
| 166 | for (Object rec : ((ArrayMap) receiverMap).keySet()) { |
| 167 | Class clazz = rec.getClass(); |
| 168 | if (clazz.getName().contains("ProxyChangeListener")) { |
| 169 | Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, |
| 170 | Intent.class); |
| 171 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
| 172 | onReceiveMethod.invoke(rec, getApplicationContext(), intent); |
| 173 | Log.v(TAG, "Prompting WebView proxy reload."); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } catch (Exception e) { |
| 178 | Log.e(TAG, "Exception while setting WebView proxy: " + e); |
| 179 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 182 | private void done(int result) { |
| 183 | if (mNetworkCallback != null) { |
| 184 | ConnectivityManager.from(this).unregisterNetworkCallback(mNetworkCallback); |
| 185 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 186 | Intent intent = new Intent(ACTION_CAPTIVE_PORTAL_LOGGED_IN); |
| 187 | intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(mNetId)); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 188 | intent.putExtra(LOGGED_IN_RESULT, String.valueOf(result)); |
| 189 | intent.putExtra(RESPONSE_TOKEN, mResponseToken); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 190 | sendBroadcast(intent); |
| 191 | finish(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean onCreateOptionsMenu(Menu menu) { |
| 196 | getMenuInflater().inflate(R.menu.captive_portal_login, menu); |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | @Override |
Paul Jensen | b6ea9ee | 2014-07-18 12:27:23 -0400 | [diff] [blame] | 201 | public void onBackPressed() { |
| 202 | WebView myWebView = (WebView) findViewById(R.id.webview); |
| 203 | if (myWebView.canGoBack()) { |
| 204 | myWebView.goBack(); |
| 205 | } else { |
| 206 | super.onBackPressed(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | @Override |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 211 | public boolean onOptionsItemSelected(MenuItem item) { |
| 212 | int id = item.getItemId(); |
| 213 | if (id == R.id.action_use_network) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 214 | done(CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | if (id == R.id.action_do_not_use_network) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 218 | done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | return super.onOptionsItemSelected(item); |
| 222 | } |
| 223 | |
| 224 | private void testForCaptivePortal() { |
| 225 | new Thread(new Runnable() { |
| 226 | public void run() { |
| 227 | // Give time for captive portal to open. |
| 228 | try { |
| 229 | Thread.sleep(1000); |
| 230 | } catch (InterruptedException e) { |
| 231 | } |
| 232 | HttpURLConnection urlConnection = null; |
| 233 | int httpResponseCode = 500; |
| 234 | try { |
| 235 | urlConnection = (HttpURLConnection) mURL.openConnection(); |
| 236 | urlConnection.setInstanceFollowRedirects(false); |
| 237 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 238 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 239 | urlConnection.setUseCaches(false); |
| 240 | urlConnection.getInputStream(); |
| 241 | httpResponseCode = urlConnection.getResponseCode(); |
| 242 | } catch (IOException e) { |
| 243 | } finally { |
| 244 | if (urlConnection != null) urlConnection.disconnect(); |
| 245 | } |
| 246 | if (httpResponseCode == 204) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 247 | done(CAPTIVE_PORTAL_APP_RETURN_APPEASED); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | }).start(); |
| 251 | } |
| 252 | |
| 253 | private class MyWebViewClient extends WebViewClient { |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 254 | private boolean firstPageLoad = true; |
| 255 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 256 | @Override |
| 257 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 258 | if (firstPageLoad) return; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 259 | testForCaptivePortal(); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void onPageFinished(WebView view, String url) { |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 264 | if (firstPageLoad) { |
| 265 | firstPageLoad = false; |
| 266 | // Now that WebView has loaded at least one page we know it has read in the proxy |
| 267 | // settings. Now prompt the WebView read the Network-specific proxy settings. |
| 268 | setWebViewProxy(); |
| 269 | // Load the real page. |
| 270 | view.loadUrl(mURL.toString()); |
| 271 | return; |
| 272 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 273 | testForCaptivePortal(); |
| 274 | } |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame^] | 275 | |
| 276 | // A web page consisting of a large broken lock icon to indicate SSL failure. |
| 277 | final static String SSL_ERROR_HTML = "<!DOCTYPE html><html><head><style>" + |
| 278 | "html { width:100%; height:100%; " + |
| 279 | " background:url(locked_page.png) center center no-repeat; }" + |
| 280 | "</style></head><body></body></html>"; |
| 281 | |
| 282 | @Override |
| 283 | public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { |
| 284 | Log.w(TAG, "SSL error; displaying broken lock icon."); |
| 285 | view.loadDataWithBaseURL("file:///android_asset/", SSL_ERROR_HTML, "text/HTML", |
| 286 | "UTF-8", null); |
| 287 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | private class MyWebChromeClient extends WebChromeClient { |
| 291 | @Override |
| 292 | public void onProgressChanged(WebView view, int newProgress) { |
Paul Jensen | 8f333f1 | 2014-08-05 22:52:16 -0400 | [diff] [blame] | 293 | ProgressBar myProgressBar = (ProgressBar) findViewById(R.id.progress_bar); |
| 294 | myProgressBar.setProgress(newProgress); |
| 295 | myProgressBar.setVisibility(newProgress == 100 ? View.GONE : View.VISIBLE); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |