CaptivePortalLoginActivity.java revision 71b645fe9cb8106dfcbf025a3fd7f58698c051bb
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
17package com.android.captiveportallogin;
18
19import android.app.Activity;
20import android.app.LoadedApk;
21import android.content.Context;
22import android.content.Intent;
23import android.graphics.Bitmap;
24import android.net.ConnectivityManager;
25import android.net.ConnectivityManager.NetworkCallback;
26import android.net.LinkProperties;
27import android.net.Network;
28import android.net.NetworkCapabilities;
29import android.net.NetworkRequest;
30import android.net.Proxy;
31import android.net.ProxyInfo;
32import android.net.Uri;
33import android.os.Bundle;
34import android.provider.Settings;
35import android.provider.Settings.Global;
36import android.util.ArrayMap;
37import android.util.Log;
38import android.view.Menu;
39import android.view.MenuItem;
40import android.view.View;
41import android.view.Window;
42import android.webkit.WebChromeClient;
43import android.webkit.WebSettings;
44import android.webkit.WebView;
45import android.webkit.WebViewClient;
46import android.widget.ProgressBar;
47
48import java.io.IOException;
49import java.net.HttpURLConnection;
50import java.net.MalformedURLException;
51import java.net.URL;
52import java.lang.InterruptedException;
53import java.lang.reflect.Field;
54import java.lang.reflect.Method;
55
56public class CaptivePortalLoginActivity extends Activity {
57    private static final String TAG = "CaptivePortalLogin";
58    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
65    //     LOGGED_IN_RESULT = one of the CAPTIVE_PORTAL_APP_RETURN_* values below.
66    //     RESPONSE_TOKEN   = data fragment from launching Intent
67    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";
70    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";
74
75    private URL mURL;
76    private int mNetId;
77    private String mResponseToken;
78    private NetworkCallback mNetworkCallback;
79
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 {
87            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);
97        }
98
99        final Network network = new Network(mNetId);
100        ConnectivityManager.setProcessDefaultNetwork(network);
101
102        // 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
126        // 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) {
136                if (network.equals(lostNetwork)) done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED);
137            }
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);
144
145        final WebView myWebView = (WebView) findViewById(R.id.webview);
146        myWebView.clearCache(true);
147        WebSettings webSettings = myWebView.getSettings();
148        webSettings.setJavaScriptEnabled(true);
149        myWebView.setWebViewClient(new MyWebViewClient());
150        myWebView.setWebChromeClient(new MyWebChromeClient());
151        // 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        }
178    }
179
180    private void done(int result) {
181        if (mNetworkCallback != null) {
182            ConnectivityManager.from(this).unregisterNetworkCallback(mNetworkCallback);
183        }
184        Intent intent = new Intent(ACTION_CAPTIVE_PORTAL_LOGGED_IN);
185        intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(mNetId));
186        intent.putExtra(LOGGED_IN_RESULT, String.valueOf(result));
187        intent.putExtra(RESPONSE_TOKEN, mResponseToken);
188        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
199    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
209    public boolean onOptionsItemSelected(MenuItem item) {
210        int id = item.getItemId();
211        if (id == R.id.action_use_network) {
212            done(CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS);
213            return true;
214        }
215        if (id == R.id.action_do_not_use_network) {
216            done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED);
217            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) {
245                    done(CAPTIVE_PORTAL_APP_RETURN_APPEASED);
246                }
247            }
248        }).start();
249    }
250
251    private class MyWebViewClient extends WebViewClient {
252        private boolean firstPageLoad = true;
253
254        @Override
255        public void onPageStarted(WebView view, String url, Bitmap favicon) {
256            if (firstPageLoad) return;
257            testForCaptivePortal();
258        }
259
260        @Override
261        public void onPageFinished(WebView view, String url) {
262            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            }
271            testForCaptivePortal();
272        }
273    }
274
275    private class MyWebChromeClient extends WebChromeClient {
276        @Override
277        public void onProgressChanged(WebView view, int newProgress) {
278            ProgressBar myProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
279            myProgressBar.setProgress(newProgress);
280            myProgressBar.setVisibility(newProgress == 100 ? View.GONE : View.VISIBLE);
281        }
282    }
283}
284