CaptivePortalLoginActivity.java revision b6ea9ee6fe6fc205f4f8be593ca993d594e8d504
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.content.Intent;
21import android.graphics.Bitmap;
22import android.net.ConnectivityManager;
23import android.net.Network;
24import android.os.Bundle;
25import android.provider.Settings;
26import android.provider.Settings.Global;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.view.Window;
30import android.webkit.WebChromeClient;
31import android.webkit.WebSettings;
32import android.webkit.WebView;
33import android.webkit.WebViewClient;
34
35import java.io.IOException;
36import java.net.HttpURLConnection;
37import java.net.MalformedURLException;
38import java.net.URL;
39import java.lang.InterruptedException;
40
41public class CaptivePortalLoginActivity extends Activity {
42    private static final String DEFAULT_SERVER = "clients3.google.com";
43    private static final int SOCKET_TIMEOUT_MS = 10000;
44
45    // Keep this in sync with NetworkMonitor.
46    // Intent broadcast to ConnectivityService indicating sign-in is complete.
47    // Extras:
48    //     EXTRA_TEXT       = netId
49    //     LOGGED_IN_RESULT = "1" if we should use network, "0" if not.
50    private static final String ACTION_CAPTIVE_PORTAL_LOGGED_IN =
51            "android.net.netmon.captive_portal_logged_in";
52    private static final String LOGGED_IN_RESULT = "result";
53
54    private URL mURL;
55    private int mNetId;
56
57    @Override
58    protected void onCreate(Bundle savedInstanceState) {
59        super.onCreate(savedInstanceState);
60
61        String server = Settings.Global.getString(getContentResolver(), "captive_portal_server");
62        if (server == null) server = DEFAULT_SERVER;
63        try {
64            mURL = new URL("http://" + server + "/generate_204");
65        } catch (MalformedURLException e) {
66            done(true);
67        }
68
69        requestWindowFeature(Window.FEATURE_PROGRESS);
70        setContentView(R.layout.activity_captive_portal_login);
71
72        getActionBar().setDisplayShowHomeEnabled(false);
73
74        mNetId = Integer.parseInt(getIntent().getStringExtra(Intent.EXTRA_TEXT));
75        ConnectivityManager.setProcessDefaultNetwork(new Network(mNetId));
76
77        WebView myWebView = (WebView) findViewById(R.id.webview);
78        WebSettings webSettings = myWebView.getSettings();
79        webSettings.setJavaScriptEnabled(true);
80        myWebView.setWebViewClient(new MyWebViewClient());
81        myWebView.setWebChromeClient(new MyWebChromeClient());
82        myWebView.loadUrl(mURL.toString());
83    }
84
85    private void done(boolean use_network) {
86        Intent intent = new Intent(ACTION_CAPTIVE_PORTAL_LOGGED_IN);
87        intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(mNetId));
88        intent.putExtra(LOGGED_IN_RESULT, use_network ? "1" : "0");
89        sendBroadcast(intent);
90        finish();
91    }
92
93    @Override
94    public boolean onCreateOptionsMenu(Menu menu) {
95        getMenuInflater().inflate(R.menu.captive_portal_login, menu);
96        return true;
97    }
98
99    @Override
100    public void onBackPressed() {
101        WebView myWebView = (WebView) findViewById(R.id.webview);
102        if (myWebView.canGoBack()) {
103            myWebView.goBack();
104        } else {
105            super.onBackPressed();
106        }
107    }
108
109    @Override
110    public boolean onOptionsItemSelected(MenuItem item) {
111        int id = item.getItemId();
112        if (id == R.id.action_use_network) {
113            done(true);
114            return true;
115        }
116        if (id == R.id.action_do_not_use_network) {
117            done(false);
118            return true;
119        }
120        return super.onOptionsItemSelected(item);
121    }
122
123    private void testForCaptivePortal() {
124        new Thread(new Runnable() {
125            public void run() {
126                // Give time for captive portal to open.
127                try {
128                    Thread.sleep(1000);
129                } catch (InterruptedException e) {
130                }
131                HttpURLConnection urlConnection = null;
132                int httpResponseCode = 500;
133                try {
134                    urlConnection = (HttpURLConnection) mURL.openConnection();
135                    urlConnection.setInstanceFollowRedirects(false);
136                    urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
137                    urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
138                    urlConnection.setUseCaches(false);
139                    urlConnection.getInputStream();
140                    httpResponseCode = urlConnection.getResponseCode();
141                } catch (IOException e) {
142                } finally {
143                    if (urlConnection != null) urlConnection.disconnect();
144                }
145                if (httpResponseCode == 204) {
146                    done(true);
147                }
148            }
149        }).start();
150    }
151
152    private class MyWebViewClient extends WebViewClient {
153        @Override
154        public void onPageStarted(WebView view, String url, Bitmap favicon) {
155            testForCaptivePortal();
156        }
157
158        @Override
159        public void onPageFinished(WebView view, String url) {
160            testForCaptivePortal();
161        }
162    }
163
164    private class MyWebChromeClient extends WebChromeClient {
165        @Override
166        public void onProgressChanged(WebView view, int newProgress) {
167            setProgress(newProgress*100);
168        }
169    }
170}
171