blob: 1c7b033b2d53cf7b12b1e0a62b5a3afd13c3581f [file] [log] [blame]
Paul Jensen869868be2014-05-15 10:33:05 -04001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.captiveportallogin;
18
19import android.app.Activity;
Paul Jensen88eb0fa2014-10-02 13:43:39 -040020import android.app.LoadedApk;
21import android.content.Context;
Paul Jensen869868be2014-05-15 10:33:05 -040022import android.content.Intent;
23import android.graphics.Bitmap;
24import android.net.ConnectivityManager;
Paul Jensen8df099d2014-09-26 15:19:17 -040025import android.net.ConnectivityManager.NetworkCallback;
Paul Jensen88eb0fa2014-10-02 13:43:39 -040026import android.net.LinkProperties;
Paul Jensen869868be2014-05-15 10:33:05 -040027import android.net.Network;
Paul Jensen8df099d2014-09-26 15:19:17 -040028import android.net.NetworkCapabilities;
29import android.net.NetworkRequest;
Paul Jensen88eb0fa2014-10-02 13:43:39 -040030import android.net.Proxy;
31import android.net.ProxyInfo;
32import android.net.Uri;
Paul Jensen869868be2014-05-15 10:33:05 -040033import android.os.Bundle;
34import android.provider.Settings;
35import android.provider.Settings.Global;
Paul Jensen88eb0fa2014-10-02 13:43:39 -040036import android.util.ArrayMap;
37import android.util.Log;
Paul Jensen869868be2014-05-15 10:33:05 -040038import android.view.Menu;
39import android.view.MenuItem;
Paul Jensen8f333f12014-08-05 22:52:16 -040040import android.view.View;
Paul Jensen869868be2014-05-15 10:33:05 -040041import android.view.Window;
42import android.webkit.WebChromeClient;
43import android.webkit.WebSettings;
44import android.webkit.WebView;
45import android.webkit.WebViewClient;
Paul Jensen8f333f12014-08-05 22:52:16 -040046import android.widget.ProgressBar;
Paul Jensen869868be2014-05-15 10:33:05 -040047
48import java.io.IOException;
49import java.net.HttpURLConnection;
50import java.net.MalformedURLException;
51import java.net.URL;
52import java.lang.InterruptedException;
Paul Jensen88eb0fa2014-10-02 13:43:39 -040053import java.lang.reflect.Field;
54import java.lang.reflect.Method;
Paul Jensen869868be2014-05-15 10:33:05 -040055
56public class CaptivePortalLoginActivity extends Activity {
Paul Jensen88eb0fa2014-10-02 13:43:39 -040057 private static final String TAG = "CaptivePortalLogin";
Paul Jensen869868be2014-05-15 10:33:05 -040058 private static final String DEFAULT_SERVER = "clients3.google.com";
59 private static final int SOCKET_TIMEOUT_MS = 10000;
60
61 // Keep this in sync with NetworkMonitor.
62 // Intent broadcast to ConnectivityService indicating sign-in is complete.
63 // Extras:
64 // EXTRA_TEXT = netId
Paul Jensen71b645f2014-10-13 14:13:07 -040065 // LOGGED_IN_RESULT = one of the CAPTIVE_PORTAL_APP_RETURN_* values below.
66 // RESPONSE_TOKEN = data fragment from launching Intent
Paul Jensen869868be2014-05-15 10:33:05 -040067 private static final String ACTION_CAPTIVE_PORTAL_LOGGED_IN =
68 "android.net.netmon.captive_portal_logged_in";
69 private static final String LOGGED_IN_RESULT = "result";
Paul Jensen71b645f2014-10-13 14:13:07 -040070 private static final int CAPTIVE_PORTAL_APP_RETURN_APPEASED = 0;
71 private static final int CAPTIVE_PORTAL_APP_RETURN_UNWANTED = 1;
72 private static final int CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS = 2;
73 private static final String RESPONSE_TOKEN = "response_token";
Paul Jensen869868be2014-05-15 10:33:05 -040074
75 private URL mURL;
76 private int mNetId;
Paul Jensen71b645f2014-10-13 14:13:07 -040077 private String mResponseToken;
Paul Jensen8df099d2014-09-26 15:19:17 -040078 private NetworkCallback mNetworkCallback;
Paul Jensen869868be2014-05-15 10:33:05 -040079
80 @Override
81 protected void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
83
84 String server = Settings.Global.getString(getContentResolver(), "captive_portal_server");
85 if (server == null) server = DEFAULT_SERVER;
86 try {
Paul Jensen71b645f2014-10-13 14:13:07 -040087 mURL = new URL("http", server, "/generate_204");
88 final Uri dataUri = getIntent().getData();
89 if (!dataUri.getScheme().equals("netid")) {
90 throw new MalformedURLException();
91 }
92 mNetId = Integer.parseInt(dataUri.getSchemeSpecificPart());
93 mResponseToken = dataUri.getFragment();
94 } catch (MalformedURLException|NumberFormatException e) {
95 // System misconfigured, bail out in a way that at least provides network access.
96 done(CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS);
Paul Jensen869868be2014-05-15 10:33:05 -040097 }
98
Paul Jensen8df099d2014-09-26 15:19:17 -040099 final Network network = new Network(mNetId);
100 ConnectivityManager.setProcessDefaultNetwork(network);
101
Paul Jensen88eb0fa2014-10-02 13:43:39 -0400102 // Set HTTP proxy system properties to those of the selected Network.
103 final LinkProperties lp = ConnectivityManager.from(this).getLinkProperties(network);
104 if (lp != null) {
105 final ProxyInfo proxyInfo = lp.getHttpProxy();
106 String host = "";
107 String port = "";
108 String exclList = "";
109 Uri pacFileUrl = Uri.EMPTY;
110 if (proxyInfo != null) {
111 host = proxyInfo.getHost();
112 port = Integer.toString(proxyInfo.getPort());
113 exclList = proxyInfo.getExclusionListAsString();
114 pacFileUrl = proxyInfo.getPacFileUrl();
115 }
116 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
117 Log.v(TAG, "Set proxy system properties to " + proxyInfo);
118 }
119
120 // Proxy system properties must be initialized before setContentView is called because
121 // setContentView initializes the WebView logic which in turn reads the system properties.
122 setContentView(R.layout.activity_captive_portal_login);
123
124 getActionBar().setDisplayShowHomeEnabled(false);
125
Paul Jensen8df099d2014-09-26 15:19:17 -0400126 // Exit app if Network disappears.
127 final NetworkCapabilities networkCapabilities =
128 ConnectivityManager.from(this).getNetworkCapabilities(network);
129 if (networkCapabilities == null) {
130 finish();
131 return;
132 }
133 mNetworkCallback = new NetworkCallback() {
134 @Override
135 public void onLost(Network lostNetwork) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400136 if (network.equals(lostNetwork)) done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED);
Paul Jensen8df099d2014-09-26 15:19:17 -0400137 }
138 };
139 final NetworkRequest.Builder builder = new NetworkRequest.Builder();
140 for (int transportType : networkCapabilities.getTransportTypes()) {
141 builder.addTransportType(transportType);
142 }
143 ConnectivityManager.from(this).registerNetworkCallback(builder.build(), mNetworkCallback);
Paul Jensen869868be2014-05-15 10:33:05 -0400144
Paul Jensen88eb0fa2014-10-02 13:43:39 -0400145 final WebView myWebView = (WebView) findViewById(R.id.webview);
146 myWebView.clearCache(true);
Paul Jensen869868be2014-05-15 10:33:05 -0400147 WebSettings webSettings = myWebView.getSettings();
148 webSettings.setJavaScriptEnabled(true);
149 myWebView.setWebViewClient(new MyWebViewClient());
150 myWebView.setWebChromeClient(new MyWebChromeClient());
Paul Jensen88eb0fa2014-10-02 13:43:39 -0400151 // Start initial page load so WebView finishes loading proxy settings.
152 // Actual load of mUrl is initiated by MyWebViewClient.
153 myWebView.loadData("", "text/html", null);
154 }
155
156 // Find WebView's proxy BroadcastReceiver and prompt it to read proxy system properties.
157 private void setWebViewProxy() {
158 LoadedApk loadedApk = getApplication().mLoadedApk;
159 try {
160 Field receiversField = LoadedApk.class.getDeclaredField("mReceivers");
161 receiversField.setAccessible(true);
162 ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
163 for (Object receiverMap : receivers.values()) {
164 for (Object rec : ((ArrayMap) receiverMap).keySet()) {
165 Class clazz = rec.getClass();
166 if (clazz.getName().contains("ProxyChangeListener")) {
167 Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class,
168 Intent.class);
169 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
170 onReceiveMethod.invoke(rec, getApplicationContext(), intent);
171 Log.v(TAG, "Prompting WebView proxy reload.");
172 }
173 }
174 }
175 } catch (Exception e) {
176 Log.e(TAG, "Exception while setting WebView proxy: " + e);
177 }
Paul Jensen869868be2014-05-15 10:33:05 -0400178 }
179
Paul Jensen71b645f2014-10-13 14:13:07 -0400180 private void done(int result) {
181 if (mNetworkCallback != null) {
182 ConnectivityManager.from(this).unregisterNetworkCallback(mNetworkCallback);
183 }
Paul Jensen869868be2014-05-15 10:33:05 -0400184 Intent intent = new Intent(ACTION_CAPTIVE_PORTAL_LOGGED_IN);
185 intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(mNetId));
Paul Jensen71b645f2014-10-13 14:13:07 -0400186 intent.putExtra(LOGGED_IN_RESULT, String.valueOf(result));
187 intent.putExtra(RESPONSE_TOKEN, mResponseToken);
Paul Jensen869868be2014-05-15 10:33:05 -0400188 sendBroadcast(intent);
189 finish();
190 }
191
192 @Override
193 public boolean onCreateOptionsMenu(Menu menu) {
194 getMenuInflater().inflate(R.menu.captive_portal_login, menu);
195 return true;
196 }
197
198 @Override
Paul Jensenb6ea9ee2014-07-18 12:27:23 -0400199 public void onBackPressed() {
200 WebView myWebView = (WebView) findViewById(R.id.webview);
201 if (myWebView.canGoBack()) {
202 myWebView.goBack();
203 } else {
204 super.onBackPressed();
205 }
206 }
207
208 @Override
Paul Jensen869868be2014-05-15 10:33:05 -0400209 public boolean onOptionsItemSelected(MenuItem item) {
210 int id = item.getItemId();
211 if (id == R.id.action_use_network) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400212 done(CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS);
Paul Jensen869868be2014-05-15 10:33:05 -0400213 return true;
214 }
215 if (id == R.id.action_do_not_use_network) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400216 done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED);
Paul Jensen869868be2014-05-15 10:33:05 -0400217 return true;
218 }
219 return super.onOptionsItemSelected(item);
220 }
221
222 private void testForCaptivePortal() {
223 new Thread(new Runnable() {
224 public void run() {
225 // Give time for captive portal to open.
226 try {
227 Thread.sleep(1000);
228 } catch (InterruptedException e) {
229 }
230 HttpURLConnection urlConnection = null;
231 int httpResponseCode = 500;
232 try {
233 urlConnection = (HttpURLConnection) mURL.openConnection();
234 urlConnection.setInstanceFollowRedirects(false);
235 urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
236 urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
237 urlConnection.setUseCaches(false);
238 urlConnection.getInputStream();
239 httpResponseCode = urlConnection.getResponseCode();
240 } catch (IOException e) {
241 } finally {
242 if (urlConnection != null) urlConnection.disconnect();
243 }
244 if (httpResponseCode == 204) {
Paul Jensen71b645f2014-10-13 14:13:07 -0400245 done(CAPTIVE_PORTAL_APP_RETURN_APPEASED);
Paul Jensen869868be2014-05-15 10:33:05 -0400246 }
247 }
248 }).start();
249 }
250
251 private class MyWebViewClient extends WebViewClient {
Paul Jensen88eb0fa2014-10-02 13:43:39 -0400252 private boolean firstPageLoad = true;
253
Paul Jensen869868be2014-05-15 10:33:05 -0400254 @Override
255 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Paul Jensen88eb0fa2014-10-02 13:43:39 -0400256 if (firstPageLoad) return;
Paul Jensen869868be2014-05-15 10:33:05 -0400257 testForCaptivePortal();
258 }
259
260 @Override
261 public void onPageFinished(WebView view, String url) {
Paul Jensen88eb0fa2014-10-02 13:43:39 -0400262 if (firstPageLoad) {
263 firstPageLoad = false;
264 // Now that WebView has loaded at least one page we know it has read in the proxy
265 // settings. Now prompt the WebView read the Network-specific proxy settings.
266 setWebViewProxy();
267 // Load the real page.
268 view.loadUrl(mURL.toString());
269 return;
270 }
Paul Jensen869868be2014-05-15 10:33:05 -0400271 testForCaptivePortal();
272 }
273 }
274
275 private class MyWebChromeClient extends WebChromeClient {
276 @Override
277 public void onProgressChanged(WebView view, int newProgress) {
Paul Jensen8f333f12014-08-05 22:52:16 -0400278 ProgressBar myProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
279 myProgressBar.setProgress(newProgress);
280 myProgressBar.setVisibility(newProgress == 100 ? View.GONE : View.VISIBLE);
Paul Jensen869868be2014-05-15 10:33:05 -0400281 }
282 }
283}