CaptivePortalLoginActivity.java revision 869868be653cb8eedd338e8347dfee1520d38cec
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 boolean onOptionsItemSelected(MenuItem item) {
101        int id = item.getItemId();
102        if (id == R.id.action_use_network) {
103            done(true);
104            return true;
105        }
106        if (id == R.id.action_do_not_use_network) {
107            done(false);
108            return true;
109        }
110        return super.onOptionsItemSelected(item);
111    }
112
113    private void testForCaptivePortal() {
114        new Thread(new Runnable() {
115            public void run() {
116                // Give time for captive portal to open.
117                try {
118                    Thread.sleep(1000);
119                } catch (InterruptedException e) {
120                }
121                HttpURLConnection urlConnection = null;
122                int httpResponseCode = 500;
123                try {
124                    urlConnection = (HttpURLConnection) mURL.openConnection();
125                    urlConnection.setInstanceFollowRedirects(false);
126                    urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
127                    urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
128                    urlConnection.setUseCaches(false);
129                    urlConnection.getInputStream();
130                    httpResponseCode = urlConnection.getResponseCode();
131                } catch (IOException e) {
132                } finally {
133                    if (urlConnection != null) urlConnection.disconnect();
134                }
135                if (httpResponseCode == 204) {
136                    done(true);
137                }
138            }
139        }).start();
140    }
141
142    private class MyWebViewClient extends WebViewClient {
143        @Override
144        public void onPageStarted(WebView view, String url, Bitmap favicon) {
145            testForCaptivePortal();
146        }
147
148        @Override
149        public void onPageFinished(WebView view, String url) {
150            testForCaptivePortal();
151        }
152    }
153
154    private class MyWebChromeClient extends WebChromeClient {
155        @Override
156        public void onProgressChanged(WebView view, int newProgress) {
157            setProgress(newProgress*100);
158        }
159    }
160}
161