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