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