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