2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.captiveportallogin;
19 import android.app.Activity;
20 import android.app.LoadedApk;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.graphics.Bitmap;
24 import android.net.CaptivePortal;
25 import android.net.ConnectivityManager;
26 import android.net.ConnectivityManager.NetworkCallback;
27 import android.net.Network;
28 import android.net.NetworkCapabilities;
29 import android.net.NetworkRequest;
30 import android.net.Proxy;
31 import android.net.Uri;
32 import android.net.http.SslError;
33 import android.os.Bundle;
34 import android.provider.Settings;
35 import android.util.ArrayMap;
36 import android.util.Log;
37 import android.util.TypedValue;
38 import android.view.Menu;
39 import android.view.MenuItem;
40 import android.view.View;
41 import android.webkit.SslErrorHandler;
42 import android.webkit.WebChromeClient;
43 import android.webkit.WebSettings;
44 import android.webkit.WebView;
45 import android.webkit.WebViewClient;
46 import android.widget.ProgressBar;
47 import android.widget.TextView;
49 import java.io.IOException;
50 import java.net.HttpURLConnection;
51 import java.net.MalformedURLException;
53 import java.lang.InterruptedException;
54 import java.lang.reflect.Field;
55 import java.lang.reflect.Method;
56 import java.util.Random;
58 public class CaptivePortalLoginActivity extends Activity {
59 private static final String TAG = CaptivePortalLoginActivity.class.getSimpleName();
60 private static final boolean DBG = true;
61 private static final boolean VDBG = false;
63 private static final int SOCKET_TIMEOUT_MS = 10000;
65 private enum Result { DISMISSED, UNWANTED, WANTED_AS_IS };
68 private String mUserAgent;
69 private Network mNetwork;
70 private CaptivePortal mCaptivePortal;
71 private NetworkCallback mNetworkCallback;
72 private ConnectivityManager mCm;
73 private boolean mLaunchBrowser = false;
74 private MyWebViewClient mWebViewClient;
77 protected void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
79 mCm = ConnectivityManager.from(this);
80 mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
81 mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
83 getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT);
86 // getUrl() failed to parse the url provided in the intent: bail out in a way that
87 // at least provides network access.
88 done(Result.WANTED_AS_IS);
92 Log.d(TAG, String.format("onCreate for %s", mUrl.toString()));
95 // Also initializes proxy system properties.
96 mCm.bindProcessToNetwork(mNetwork);
98 // Proxy system properties must be initialized before setContentView is called because
99 // setContentView initializes the WebView logic which in turn reads the system properties.
100 setContentView(R.layout.activity_captive_portal_login);
102 // Exit app if Network disappears.
103 final NetworkCapabilities networkCapabilities = mCm.getNetworkCapabilities(mNetwork);
104 if (networkCapabilities == null) {
105 finishAndRemoveTask();
108 mNetworkCallback = new NetworkCallback() {
110 public void onLost(Network lostNetwork) {
111 if (mNetwork.equals(lostNetwork)) done(Result.UNWANTED);
114 final NetworkRequest.Builder builder = new NetworkRequest.Builder();
115 for (int transportType : networkCapabilities.getTransportTypes()) {
116 builder.addTransportType(transportType);
118 mCm.registerNetworkCallback(builder.build(), mNetworkCallback);
120 getActionBar().setDisplayShowHomeEnabled(false);
121 getActionBar().setElevation(0); // remove shadow
122 getActionBar().setTitle(getHeaderTitle());
123 getActionBar().setSubtitle("");
125 final WebView webview = getWebview();
126 webview.clearCache(true);
127 WebSettings webSettings = webview.getSettings();
128 webSettings.setJavaScriptEnabled(true);
129 webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
130 webSettings.setUseWideViewPort(true);
131 webSettings.setLoadWithOverviewMode(true);
132 webSettings.setSupportZoom(true);
133 webSettings.setBuiltInZoomControls(true);
134 webSettings.setDisplayZoomControls(false);
135 mWebViewClient = new MyWebViewClient();
136 webview.setWebViewClient(mWebViewClient);
137 webview.setWebChromeClient(new MyWebChromeClient());
138 // Start initial page load so WebView finishes loading proxy settings.
139 // Actual load of mUrl is initiated by MyWebViewClient.
140 webview.loadData("", "text/html", null);
143 // Find WebView's proxy BroadcastReceiver and prompt it to read proxy system properties.
144 private void setWebViewProxy() {
145 LoadedApk loadedApk = getApplication().mLoadedApk;
147 Field receiversField = LoadedApk.class.getDeclaredField("mReceivers");
148 receiversField.setAccessible(true);
149 ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
150 for (Object receiverMap : receivers.values()) {
151 for (Object rec : ((ArrayMap) receiverMap).keySet()) {
152 Class clazz = rec.getClass();
153 if (clazz.getName().contains("ProxyChangeListener")) {
154 Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class,
156 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
157 onReceiveMethod.invoke(rec, getApplicationContext(), intent);
158 Log.v(TAG, "Prompting WebView proxy reload.");
162 } catch (Exception e) {
163 Log.e(TAG, "Exception while setting WebView proxy: " + e);
167 private void done(Result result) {
169 Log.d(TAG, String.format("Result %s for %s", result.name(), mUrl.toString()));
171 if (mNetworkCallback != null) {
172 mCm.unregisterNetworkCallback(mNetworkCallback);
173 mNetworkCallback = null;
177 mCaptivePortal.reportCaptivePortalDismissed();
180 mCaptivePortal.ignoreNetwork();
183 mCaptivePortal.useNetwork();
186 finishAndRemoveTask();
190 public boolean onCreateOptionsMenu(Menu menu) {
191 getMenuInflater().inflate(R.menu.captive_portal_login, menu);
196 public void onBackPressed() {
197 WebView myWebView = findViewById(R.id.webview);
198 if (myWebView.canGoBack() && mWebViewClient.allowBack()) {
201 super.onBackPressed();
206 public boolean onOptionsItemSelected(MenuItem item) {
209 final int id = item.getItemId();
211 case R.id.action_use_network:
212 result = Result.WANTED_AS_IS;
213 action = "USE_NETWORK";
215 case R.id.action_do_not_use_network:
216 result = Result.UNWANTED;
217 action = "DO_NOT_USE_NETWORK";
220 return super.onOptionsItemSelected(item);
223 Log.d(TAG, String.format("onOptionsItemSelect %s for %s", action, mUrl.toString()));
230 public void onDestroy() {
232 if (mNetworkCallback != null) {
233 mCm.unregisterNetworkCallback(mNetworkCallback);
234 mNetworkCallback = null;
236 if (mLaunchBrowser) {
237 // Give time for this network to become default. After 500ms just proceed.
238 for (int i = 0; i < 5; i++) {
239 // TODO: This misses when mNetwork underlies a VPN.
240 if (mNetwork.equals(mCm.getActiveNetwork())) break;
243 } catch (InterruptedException e) {
246 final String url = mUrl.toString();
248 Log.d(TAG, "starting activity with intent ACTION_VIEW for " + url);
250 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
254 private URL getUrl() {
255 String url = getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL);
257 url = mCm.getCaptivePortalServerUrl();
262 private static URL makeURL(String url) {
265 } catch (MalformedURLException e) {
266 Log.e(TAG, "Invalid URL " + url);
271 private void testForCaptivePortal() {
272 // TODO: reuse NetworkMonitor facilities for consistent captive portal detection.
273 new Thread(new Runnable() {
275 // Give time for captive portal to open.
278 } catch (InterruptedException e) {
280 HttpURLConnection urlConnection = null;
281 int httpResponseCode = 500;
283 urlConnection = (HttpURLConnection) mNetwork.openConnection(mUrl);
284 urlConnection.setInstanceFollowRedirects(false);
285 urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
286 urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
287 urlConnection.setUseCaches(false);
288 if (mUserAgent != null) {
289 urlConnection.setRequestProperty("User-Agent", mUserAgent);
291 // cannot read request header after connection
292 String requestHeader = urlConnection.getRequestProperties().toString();
294 urlConnection.getInputStream();
295 httpResponseCode = urlConnection.getResponseCode();
297 Log.d(TAG, "probe at " + mUrl +
298 " ret=" + httpResponseCode +
299 " request=" + requestHeader +
300 " headers=" + urlConnection.getHeaderFields());
302 } catch (IOException e) {
304 if (urlConnection != null) urlConnection.disconnect();
306 if (httpResponseCode == 204) {
307 done(Result.DISMISSED);
313 private class MyWebViewClient extends WebViewClient {
314 private static final String INTERNAL_ASSETS = "file:///android_asset/";
316 private final String mBrowserBailOutToken = Long.toString(new Random().nextLong());
317 // How many Android device-independent-pixels per scaled-pixel
318 // dp/sp = (px/sp) / (px/dp) = (1/sp) / (1/dp)
319 private final float mDpPerSp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 1,
320 getResources().getDisplayMetrics()) /
321 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
322 getResources().getDisplayMetrics());
323 private int mPagesLoaded;
325 // If we haven't finished cleaning up the history, don't allow going back.
326 public boolean allowBack() {
327 return mPagesLoaded > 1;
331 public void onPageStarted(WebView view, String url, Bitmap favicon) {
332 if (url.contains(mBrowserBailOutToken)) {
333 mLaunchBrowser = true;
334 done(Result.WANTED_AS_IS);
337 // The first page load is used only to cause the WebView to
338 // fetch the proxy settings. Don't update the URL bar, and
339 // don't check if the captive portal is still there.
340 if (mPagesLoaded == 0) return;
341 // For internally generated pages, leave URL bar listing prior URL as this is the URL
342 // the page refers to.
343 if (!url.startsWith(INTERNAL_ASSETS)) {
344 getActionBar().setSubtitle(getHeaderSubtitle(url));
346 getProgressBar().setVisibility(View.VISIBLE);
347 testForCaptivePortal();
351 public void onPageFinished(WebView view, String url) {
353 getProgressBar().setVisibility(View.INVISIBLE);
354 if (mPagesLoaded == 1) {
355 // Now that WebView has loaded at least one page we know it has read in the proxy
356 // settings. Now prompt the WebView read the Network-specific proxy settings.
358 // Load the real page.
359 view.loadUrl(mUrl.toString());
361 } else if (mPagesLoaded == 2) {
362 // Prevent going back to empty first page.
365 testForCaptivePortal();
368 // Convert Android scaled-pixels (sp) to HTML size.
369 private String sp(int sp) {
370 // Convert sp to dp's.
371 float dp = sp * mDpPerSp;
372 // Apply a scale factor to make things look right.
374 // Convert dp's to HTML size.
375 // HTML px's are scaled just like dp's, so just add "px" suffix.
376 return Integer.toString((int)dp) + "px";
379 // A web page consisting of a large broken lock icon to indicate SSL failure.
382 public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
383 Log.w(TAG, "SSL error (error: " + error.getPrimaryError() + " host: " +
384 // Only show host to avoid leaking private info.
385 Uri.parse(error.getUrl()).getHost() + " certificate: " +
386 error.getCertificate() + "); displaying SSL warning.");
387 final String sslErrorPage = makeSslErrorPage();
389 Log.d(TAG, sslErrorPage);
391 view.loadDataWithBaseURL(INTERNAL_ASSETS, sslErrorPage, "text/HTML", "UTF-8", null);
394 private String makeSslErrorPage() {
395 final String warningMsg = getString(R.string.ssl_error_warning);
396 final String exampleMsg = getString(R.string.ssl_error_example);
397 final String continueMsg = getString(R.string.ssl_error_continue);
398 return String.join("\n",
401 " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">",
404 " background-color:#fafafa;",
414 " font-size:" + sp(16) + ";",
415 " line-height:1.28;",
420 " font-size:" + sp(14) + ";",
421 " line-height:1.21905;",
427 " display:inline-block;",
428 " font-size:" + sp(14) + ";",
429 " font-weight:bold;",
432 " text-decoration:none;",
433 " text-transform:uppercase;",
438 " <p><img src=quantum_ic_warning_amber_96.png><br>",
439 " <div class=warn>" + warningMsg + "</div>",
440 " <div class=example>" + exampleMsg + "</div>",
441 " <a href=" + mBrowserBailOutToken + ">" + continueMsg + "</a>",
447 public boolean shouldOverrideUrlLoading (WebView view, String url) {
448 if (url.startsWith("tel:")) {
449 startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
456 private class MyWebChromeClient extends WebChromeClient {
458 public void onProgressChanged(WebView view, int newProgress) {
459 getProgressBar().setProgress(newProgress);
463 private ProgressBar getProgressBar() {
464 return findViewById(R.id.progress_bar);
467 private WebView getWebview() {
468 return findViewById(R.id.webview);
471 private String getHeaderTitle() {
472 return getString(R.string.action_bar_label);
475 private String getHeaderSubtitle(String urlString) {
476 URL url = makeURL(urlString);
480 final String https = "https";
481 if (https.equals(url.getProtocol())) {
482 return https + "://" + url.getHost();
484 return url.getHost();