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; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 24 | import android.net.CaptivePortal; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 25 | import android.net.ConnectivityManager; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 26 | import android.net.ConnectivityManager.NetworkCallback; |
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; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 31 | import android.net.Uri; |
Erik Kline | c43d2f5 | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 32 | import android.net.dns.ResolvUtil; |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 33 | import android.net.http.SslError; |
Chalard Jean | 2dcccbc | 2018-04-12 11:52:37 +0900 | [diff] [blame] | 34 | import android.net.wifi.WifiInfo; |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 35 | import android.os.Build; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 36 | import android.os.Bundle; |
| 37 | import android.provider.Settings; |
Chalard Jean | 94bc48f | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 38 | import android.support.v4.widget.SwipeRefreshLayout; |
Adam Newman | c1b4efb | 2018-03-27 10:10:43 -0700 | [diff] [blame] | 39 | import android.text.TextUtils; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 40 | import android.util.ArrayMap; |
| 41 | import android.util.Log; |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 42 | import android.util.TypedValue; |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 43 | import android.util.SparseArray; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 44 | import android.view.Menu; |
| 45 | import android.view.MenuItem; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 46 | import android.view.View; |
Lorenzo Colitti | c3c95ba | 2018-03-26 01:32:33 +0900 | [diff] [blame] | 47 | import android.webkit.CookieManager; |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 48 | import android.webkit.SslErrorHandler; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 49 | import android.webkit.WebChromeClient; |
| 50 | import android.webkit.WebSettings; |
| 51 | import android.webkit.WebView; |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 52 | import android.webkit.WebView; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 53 | import android.webkit.WebViewClient; |
Paul Jensen | 8f333f1 | 2014-08-05 22:52:16 -0400 | [diff] [blame] | 54 | import android.widget.ProgressBar; |
Paul Jensen | 5344a4a | 2015-05-06 07:39:36 -0400 | [diff] [blame] | 55 | import android.widget.TextView; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 56 | |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 57 | import com.android.internal.logging.MetricsLogger; |
| 58 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 59 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 60 | import java.io.IOException; |
| 61 | import java.net.HttpURLConnection; |
| 62 | import java.net.MalformedURLException; |
| 63 | import java.net.URL; |
| 64 | import java.lang.InterruptedException; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 65 | import java.lang.reflect.Field; |
| 66 | import java.lang.reflect.Method; |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 67 | import java.util.Objects; |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 68 | import java.util.Random; |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 69 | import java.util.concurrent.atomic.AtomicBoolean; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 70 | |
| 71 | public class CaptivePortalLoginActivity extends Activity { |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 72 | private static final String TAG = CaptivePortalLoginActivity.class.getSimpleName(); |
| 73 | private static final boolean DBG = true; |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 74 | private static final boolean VDBG = false; |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 75 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 76 | private static final int SOCKET_TIMEOUT_MS = 10000; |
| 77 | |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 78 | private enum Result { |
| 79 | DISMISSED(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_RESULT_DISMISSED), |
| 80 | UNWANTED(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_RESULT_UNWANTED), |
| 81 | WANTED_AS_IS(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_RESULT_WANTED_AS_IS); |
| 82 | |
| 83 | final int metricsEvent; |
| 84 | Result(int metricsEvent) { this.metricsEvent = metricsEvent; } |
| 85 | }; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 86 | |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 87 | private URL mUrl; |
Hugo Benichi | 2c02197 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 88 | private String mUserAgent; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 89 | private Network mNetwork; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 90 | private CaptivePortal mCaptivePortal; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 91 | private NetworkCallback mNetworkCallback; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 92 | private ConnectivityManager mCm; |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 93 | private boolean mLaunchBrowser = false; |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 94 | private MyWebViewClient mWebViewClient; |
Chalard Jean | 94bc48f | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 95 | private SwipeRefreshLayout mSwipeRefreshLayout; |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 96 | // Ensures that done() happens once exactly, handling concurrent callers with atomic operations. |
| 97 | private final AtomicBoolean isDone = new AtomicBoolean(false); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 98 | |
| 99 | @Override |
| 100 | protected void onCreate(Bundle savedInstanceState) { |
| 101 | super.onCreate(savedInstanceState); |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 102 | |
| 103 | logMetricsEvent(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_ACTIVITY); |
| 104 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 105 | mCm = ConnectivityManager.from(this); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 106 | mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK); |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 107 | mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL); |
Hugo Benichi | ec88fd6 | 2017-03-07 15:10:03 +0900 | [diff] [blame] | 108 | mUserAgent = |
| 109 | getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT); |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 110 | mUrl = getUrl(); |
| 111 | if (mUrl == null) { |
| 112 | // getUrl() failed to parse the url provided in the intent: bail out in a way that |
| 113 | // at least provides network access. |
| 114 | done(Result.WANTED_AS_IS); |
| 115 | return; |
| 116 | } |
| 117 | if (DBG) { |
| 118 | Log.d(TAG, String.format("onCreate for %s", mUrl.toString())); |
| 119 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 120 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 121 | // Also initializes proxy system properties. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 122 | mCm.bindProcessToNetwork(mNetwork); |
Erik Kline | c43d2f5 | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 123 | mCm.setProcessDefaultNetworkForHostResolution( |
| 124 | ResolvUtil.getNetworkWithUseLocalNameserversFlag(mNetwork)); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 125 | |
| 126 | // Proxy system properties must be initialized before setContentView is called because |
| 127 | // setContentView initializes the WebView logic which in turn reads the system properties. |
| 128 | setContentView(R.layout.activity_captive_portal_login); |
| 129 | |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 130 | // Exit app if Network disappears. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 131 | final NetworkCapabilities networkCapabilities = mCm.getNetworkCapabilities(mNetwork); |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 132 | if (networkCapabilities == null) { |
Paul Jensen | 6a776c83 | 2016-07-19 13:19:39 -0400 | [diff] [blame] | 133 | finishAndRemoveTask(); |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | mNetworkCallback = new NetworkCallback() { |
| 137 | @Override |
| 138 | public void onLost(Network lostNetwork) { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 139 | if (mNetwork.equals(lostNetwork)) done(Result.UNWANTED); |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | final NetworkRequest.Builder builder = new NetworkRequest.Builder(); |
| 143 | for (int transportType : networkCapabilities.getTransportTypes()) { |
| 144 | builder.addTransportType(transportType); |
| 145 | } |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 146 | mCm.registerNetworkCallback(builder.build(), mNetworkCallback); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 147 | |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 148 | getActionBar().setDisplayShowHomeEnabled(false); |
| 149 | getActionBar().setElevation(0); // remove shadow |
| 150 | getActionBar().setTitle(getHeaderTitle()); |
| 151 | getActionBar().setSubtitle(""); |
| 152 | |
| 153 | final WebView webview = getWebview(); |
| 154 | webview.clearCache(true); |
Lorenzo Colitti | c3c95ba | 2018-03-26 01:32:33 +0900 | [diff] [blame] | 155 | CookieManager.getInstance().setAcceptThirdPartyCookies(webview, true); |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 156 | WebSettings webSettings = webview.getSettings(); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 157 | webSettings.setJavaScriptEnabled(true); |
Lorenzo Colitti | b55bf38 | 2016-10-21 18:41:25 +0900 | [diff] [blame] | 158 | webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); |
Hugo Benichi | 94c5fb3 | 2017-05-11 09:58:14 +0900 | [diff] [blame] | 159 | webSettings.setUseWideViewPort(true); |
| 160 | webSettings.setLoadWithOverviewMode(true); |
| 161 | webSettings.setSupportZoom(true); |
| 162 | webSettings.setBuiltInZoomControls(true); |
| 163 | webSettings.setDisplayZoomControls(false); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 164 | mWebViewClient = new MyWebViewClient(); |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 165 | webview.setWebViewClient(mWebViewClient); |
| 166 | webview.setWebChromeClient(new MyWebChromeClient()); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 167 | // Start initial page load so WebView finishes loading proxy settings. |
| 168 | // Actual load of mUrl is initiated by MyWebViewClient. |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 169 | webview.loadData("", "text/html", null); |
Chalard Jean | 94bc48f | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 170 | |
| 171 | mSwipeRefreshLayout = findViewById(R.id.swipe_refresh); |
| 172 | mSwipeRefreshLayout.setOnRefreshListener(() -> { |
| 173 | webview.reload(); |
| 174 | mSwipeRefreshLayout.setRefreshing(true); |
| 175 | }); |
| 176 | |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Find WebView's proxy BroadcastReceiver and prompt it to read proxy system properties. |
| 180 | private void setWebViewProxy() { |
| 181 | LoadedApk loadedApk = getApplication().mLoadedApk; |
| 182 | try { |
| 183 | Field receiversField = LoadedApk.class.getDeclaredField("mReceivers"); |
| 184 | receiversField.setAccessible(true); |
| 185 | ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); |
| 186 | for (Object receiverMap : receivers.values()) { |
| 187 | for (Object rec : ((ArrayMap) receiverMap).keySet()) { |
| 188 | Class clazz = rec.getClass(); |
| 189 | if (clazz.getName().contains("ProxyChangeListener")) { |
| 190 | Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, |
| 191 | Intent.class); |
| 192 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
| 193 | onReceiveMethod.invoke(rec, getApplicationContext(), intent); |
| 194 | Log.v(TAG, "Prompting WebView proxy reload."); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } catch (Exception e) { |
| 199 | Log.e(TAG, "Exception while setting WebView proxy: " + e); |
| 200 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 203 | private void done(Result result) { |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 204 | if (isDone.getAndSet(true)) { |
| 205 | // isDone was already true: done() already called |
| 206 | return; |
| 207 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 208 | if (DBG) { |
| 209 | Log.d(TAG, String.format("Result %s for %s", result.name(), mUrl.toString())); |
| 210 | } |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 211 | logMetricsEvent(result.metricsEvent); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 212 | switch (result) { |
| 213 | case DISMISSED: |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 214 | mCaptivePortal.reportCaptivePortalDismissed(); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 215 | break; |
| 216 | case UNWANTED: |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 217 | mCaptivePortal.ignoreNetwork(); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 218 | break; |
| 219 | case WANTED_AS_IS: |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 220 | mCaptivePortal.useNetwork(); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 221 | break; |
| 222 | } |
Paul Jensen | 6a776c83 | 2016-07-19 13:19:39 -0400 | [diff] [blame] | 223 | finishAndRemoveTask(); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean onCreateOptionsMenu(Menu menu) { |
| 228 | getMenuInflater().inflate(R.menu.captive_portal_login, menu); |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | @Override |
Paul Jensen | b6ea9ee | 2014-07-18 12:27:23 -0400 | [diff] [blame] | 233 | public void onBackPressed() { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 234 | WebView myWebView = findViewById(R.id.webview); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 235 | if (myWebView.canGoBack() && mWebViewClient.allowBack()) { |
Paul Jensen | b6ea9ee | 2014-07-18 12:27:23 -0400 | [diff] [blame] | 236 | myWebView.goBack(); |
| 237 | } else { |
| 238 | super.onBackPressed(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | @Override |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 243 | public boolean onOptionsItemSelected(MenuItem item) { |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 244 | final Result result; |
| 245 | final String action; |
| 246 | final int id = item.getItemId(); |
| 247 | switch (id) { |
| 248 | case R.id.action_use_network: |
| 249 | result = Result.WANTED_AS_IS; |
| 250 | action = "USE_NETWORK"; |
| 251 | break; |
| 252 | case R.id.action_do_not_use_network: |
| 253 | result = Result.UNWANTED; |
| 254 | action = "DO_NOT_USE_NETWORK"; |
| 255 | break; |
| 256 | default: |
| 257 | return super.onOptionsItemSelected(item); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 258 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 259 | if (DBG) { |
| 260 | Log.d(TAG, String.format("onOptionsItemSelect %s for %s", action, mUrl.toString())); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 261 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 262 | done(result); |
| 263 | return true; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 266 | @Override |
| 267 | public void onDestroy() { |
| 268 | super.onDestroy(); |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 269 | if (mNetworkCallback != null) { |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 270 | // mNetworkCallback is not null if mUrl is not null. |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 271 | mCm.unregisterNetworkCallback(mNetworkCallback); |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 272 | } |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 273 | if (mLaunchBrowser) { |
| 274 | // Give time for this network to become default. After 500ms just proceed. |
| 275 | for (int i = 0; i < 5; i++) { |
| 276 | // TODO: This misses when mNetwork underlies a VPN. |
| 277 | if (mNetwork.equals(mCm.getActiveNetwork())) break; |
| 278 | try { |
| 279 | Thread.sleep(100); |
| 280 | } catch (InterruptedException e) { |
| 281 | } |
| 282 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 283 | final String url = mUrl.toString(); |
| 284 | if (DBG) { |
| 285 | Log.d(TAG, "starting activity with intent ACTION_VIEW for " + url); |
| 286 | } |
| 287 | startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 288 | } |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 291 | private URL getUrl() { |
| 292 | String url = getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL); |
| 293 | if (url == null) { |
| 294 | url = mCm.getCaptivePortalServerUrl(); |
| 295 | } |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 296 | return makeURL(url); |
| 297 | } |
| 298 | |
| 299 | private static URL makeURL(String url) { |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 300 | try { |
| 301 | return new URL(url); |
| 302 | } catch (MalformedURLException e) { |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 303 | Log.e(TAG, "Invalid URL " + url); |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 304 | } |
| 305 | return null; |
| 306 | } |
| 307 | |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 308 | private static String host(URL url) { |
| 309 | if (url == null) { |
| 310 | return null; |
| 311 | } |
| 312 | return url.getHost(); |
| 313 | } |
| 314 | |
| 315 | private static String sanitizeURL(URL url) { |
| 316 | // In non-Debug build, only show host to avoid leaking private info. |
| 317 | return Build.IS_DEBUGGABLE ? Objects.toString(url) : host(url); |
| 318 | } |
| 319 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 320 | private void testForCaptivePortal() { |
Hugo Benichi | 2c02197 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 321 | // TODO: reuse NetworkMonitor facilities for consistent captive portal detection. |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 322 | new Thread(new Runnable() { |
| 323 | public void run() { |
Erik Kline | d324dce | 2018-04-25 17:27:28 +0900 | [diff] [blame] | 324 | final Network network = ResolvUtil.makeNetworkWithPrivateDnsBypass(mNetwork); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 325 | // Give time for captive portal to open. |
| 326 | try { |
| 327 | Thread.sleep(1000); |
| 328 | } catch (InterruptedException e) { |
| 329 | } |
| 330 | HttpURLConnection urlConnection = null; |
| 331 | int httpResponseCode = 500; |
| 332 | try { |
Erik Kline | d324dce | 2018-04-25 17:27:28 +0900 | [diff] [blame] | 333 | urlConnection = (HttpURLConnection) network.openConnection(mUrl); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 334 | urlConnection.setInstanceFollowRedirects(false); |
| 335 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 336 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 337 | urlConnection.setUseCaches(false); |
Hugo Benichi | 2c02197 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 338 | if (mUserAgent != null) { |
| 339 | urlConnection.setRequestProperty("User-Agent", mUserAgent); |
| 340 | } |
Hugo Benichi | ec88fd6 | 2017-03-07 15:10:03 +0900 | [diff] [blame] | 341 | // cannot read request header after connection |
| 342 | String requestHeader = urlConnection.getRequestProperties().toString(); |
| 343 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 344 | urlConnection.getInputStream(); |
| 345 | httpResponseCode = urlConnection.getResponseCode(); |
Hugo Benichi | ec88fd6 | 2017-03-07 15:10:03 +0900 | [diff] [blame] | 346 | if (DBG) { |
| 347 | Log.d(TAG, "probe at " + mUrl + |
| 348 | " ret=" + httpResponseCode + |
| 349 | " request=" + requestHeader + |
| 350 | " headers=" + urlConnection.getHeaderFields()); |
| 351 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 352 | } catch (IOException e) { |
| 353 | } finally { |
| 354 | if (urlConnection != null) urlConnection.disconnect(); |
| 355 | } |
| 356 | if (httpResponseCode == 204) { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 357 | done(Result.DISMISSED); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | }).start(); |
| 361 | } |
| 362 | |
| 363 | private class MyWebViewClient extends WebViewClient { |
Paul Jensen | 5344a4a | 2015-05-06 07:39:36 -0400 | [diff] [blame] | 364 | private static final String INTERNAL_ASSETS = "file:///android_asset/"; |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 365 | |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 366 | private final String mBrowserBailOutToken = Long.toString(new Random().nextLong()); |
| 367 | // How many Android device-independent-pixels per scaled-pixel |
| 368 | // dp/sp = (px/sp) / (px/dp) = (1/sp) / (1/dp) |
| 369 | private final float mDpPerSp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 1, |
| 370 | getResources().getDisplayMetrics()) / |
| 371 | TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, |
| 372 | getResources().getDisplayMetrics()); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 373 | private int mPagesLoaded; |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 374 | // the host of the page that this webview is currently loading. Can be null when undefined. |
| 375 | private String mHostname; |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 376 | |
| 377 | // If we haven't finished cleaning up the history, don't allow going back. |
| 378 | public boolean allowBack() { |
| 379 | return mPagesLoaded > 1; |
| 380 | } |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 381 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 382 | @Override |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 383 | public void onPageStarted(WebView view, String urlString, Bitmap favicon) { |
| 384 | if (urlString.contains(mBrowserBailOutToken)) { |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 385 | mLaunchBrowser = true; |
| 386 | done(Result.WANTED_AS_IS); |
| 387 | return; |
| 388 | } |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 389 | // The first page load is used only to cause the WebView to |
| 390 | // fetch the proxy settings. Don't update the URL bar, and |
| 391 | // don't check if the captive portal is still there. |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 392 | if (mPagesLoaded == 0) { |
| 393 | return; |
| 394 | } |
| 395 | final URL url = makeURL(urlString); |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 396 | Log.d(TAG, "onPageStarted: " + sanitizeURL(url)); |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 397 | mHostname = host(url); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 398 | // For internally generated pages, leave URL bar listing prior URL as this is the URL |
| 399 | // the page refers to. |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 400 | if (!urlString.startsWith(INTERNAL_ASSETS)) { |
| 401 | String subtitle = (url != null) ? getHeaderSubtitle(url) : urlString; |
| 402 | getActionBar().setSubtitle(subtitle); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 403 | } |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 404 | getProgressBar().setVisibility(View.VISIBLE); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 405 | testForCaptivePortal(); |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void onPageFinished(WebView view, String url) { |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 410 | mPagesLoaded++; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 411 | getProgressBar().setVisibility(View.INVISIBLE); |
Chalard Jean | 94bc48f | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 412 | mSwipeRefreshLayout.setRefreshing(false); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 413 | if (mPagesLoaded == 1) { |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 414 | // Now that WebView has loaded at least one page we know it has read in the proxy |
| 415 | // settings. Now prompt the WebView read the Network-specific proxy settings. |
| 416 | setWebViewProxy(); |
| 417 | // Load the real page. |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 418 | view.loadUrl(mUrl.toString()); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 419 | return; |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 420 | } else if (mPagesLoaded == 2) { |
| 421 | // Prevent going back to empty first page. |
susnata | 9764040 | 2017-06-23 09:13:05 -0700 | [diff] [blame] | 422 | // Fix for missing focus, see b/62449959 for details. Remove it once we get a |
| 423 | // newer version of WebView (60.x.y). |
| 424 | view.requestFocus(); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 425 | view.clearHistory(); |
Paul Jensen | 5344a4a | 2015-05-06 07:39:36 -0400 | [diff] [blame] | 426 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 427 | testForCaptivePortal(); |
| 428 | } |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 429 | |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 430 | // Convert Android scaled-pixels (sp) to HTML size. |
| 431 | private String sp(int sp) { |
| 432 | // Convert sp to dp's. |
| 433 | float dp = sp * mDpPerSp; |
| 434 | // Apply a scale factor to make things look right. |
| 435 | dp *= 1.3; |
| 436 | // Convert dp's to HTML size. |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 437 | // HTML px's are scaled just like dp's, so just add "px" suffix. |
| 438 | return Integer.toString((int)dp) + "px"; |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 441 | // A web page consisting of a large broken lock icon to indicate SSL failure. |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 442 | |
| 443 | @Override |
| 444 | public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 445 | final URL url = makeURL(error.getUrl()); |
| 446 | final String host = host(url); |
| 447 | Log.d(TAG, String.format("SSL error: %s, url: %s, certificate: %s", |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 448 | sslErrorName(error), sanitizeURL(url), error.getCertificate())); |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 449 | if (url == null || !Objects.equals(host, mHostname)) { |
| 450 | // Ignore ssl errors for resources coming from a different hostname than the page |
| 451 | // that we are currently loading, and only cancel the request. |
| 452 | handler.cancel(); |
| 453 | return; |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 454 | } |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 455 | logMetricsEvent(MetricsEvent.CAPTIVE_PORTAL_LOGIN_ACTIVITY_SSL_ERROR); |
| 456 | final String sslErrorPage = makeSslErrorPage(); |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 457 | view.loadDataWithBaseURL(INTERNAL_ASSETS, sslErrorPage, "text/HTML", "UTF-8", null); |
| 458 | } |
| 459 | |
| 460 | private String makeSslErrorPage() { |
| 461 | final String warningMsg = getString(R.string.ssl_error_warning); |
| 462 | final String exampleMsg = getString(R.string.ssl_error_example); |
| 463 | final String continueMsg = getString(R.string.ssl_error_continue); |
| 464 | return String.join("\n", |
| 465 | "<html>", |
| 466 | "<head>", |
| 467 | " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">", |
| 468 | " <style>", |
| 469 | " body {", |
| 470 | " background-color:#fafafa;", |
| 471 | " margin:auto;", |
| 472 | " width:80%;", |
| 473 | " margin-top: 96px", |
| 474 | " }", |
| 475 | " img {", |
| 476 | " height:48px;", |
| 477 | " width:48px;", |
| 478 | " }", |
| 479 | " div.warn {", |
| 480 | " font-size:" + sp(16) + ";", |
| 481 | " line-height:1.28;", |
| 482 | " margin-top:16px;", |
| 483 | " opacity:0.87;", |
| 484 | " }", |
| 485 | " div.example {", |
| 486 | " font-size:" + sp(14) + ";", |
| 487 | " line-height:1.21905;", |
| 488 | " margin-top:16px;", |
| 489 | " opacity:0.54;", |
| 490 | " }", |
| 491 | " a {", |
| 492 | " color:#4285F4;", |
| 493 | " display:inline-block;", |
| 494 | " font-size:" + sp(14) + ";", |
| 495 | " font-weight:bold;", |
| 496 | " height:48px;", |
| 497 | " margin-top:24px;", |
| 498 | " text-decoration:none;", |
| 499 | " text-transform:uppercase;", |
| 500 | " }", |
| 501 | " </style>", |
| 502 | "</head>", |
| 503 | "<body>", |
| 504 | " <p><img src=quantum_ic_warning_amber_96.png><br>", |
| 505 | " <div class=warn>" + warningMsg + "</div>", |
| 506 | " <div class=example>" + exampleMsg + "</div>", |
| 507 | " <a href=" + mBrowserBailOutToken + ">" + continueMsg + "</a>", |
| 508 | "</body>", |
| 509 | "</html>"); |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 510 | } |
Paul Jensen | fd54da9 | 2015-06-09 07:50:51 -0400 | [diff] [blame] | 511 | |
| 512 | @Override |
| 513 | public boolean shouldOverrideUrlLoading (WebView view, String url) { |
| 514 | if (url.startsWith("tel:")) { |
| 515 | startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); |
| 516 | return true; |
| 517 | } |
| 518 | return false; |
| 519 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | private class MyWebChromeClient extends WebChromeClient { |
| 523 | @Override |
| 524 | public void onProgressChanged(WebView view, int newProgress) { |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 525 | getProgressBar().setProgress(newProgress); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 526 | } |
| 527 | } |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 528 | |
| 529 | private ProgressBar getProgressBar() { |
| 530 | return findViewById(R.id.progress_bar); |
| 531 | } |
| 532 | |
| 533 | private WebView getWebview() { |
| 534 | return findViewById(R.id.webview); |
| 535 | } |
| 536 | |
| 537 | private String getHeaderTitle() { |
Hugo Benichi | 3a22297 | 2017-06-01 12:58:49 +0900 | [diff] [blame] | 538 | NetworkCapabilities nc = mCm.getNetworkCapabilities(mNetwork); |
Chalard Jean | 2dcccbc | 2018-04-12 11:52:37 +0900 | [diff] [blame] | 539 | if (nc == null || TextUtils.isEmpty(nc.getSSID()) |
| 540 | || !nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
Hugo Benichi | 3a22297 | 2017-06-01 12:58:49 +0900 | [diff] [blame] | 541 | return getString(R.string.action_bar_label); |
| 542 | } |
Chalard Jean | 2dcccbc | 2018-04-12 11:52:37 +0900 | [diff] [blame] | 543 | return getString(R.string.action_bar_title, WifiInfo.removeDoubleQuotes(nc.getSSID())); |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 544 | } |
| 545 | |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 546 | private String getHeaderSubtitle(URL url) { |
| 547 | String host = host(url); |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 548 | final String https = "https"; |
| 549 | if (https.equals(url.getProtocol())) { |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 550 | return https + "://" + host; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 551 | } |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 552 | return host; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 553 | } |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 554 | |
| 555 | private void logMetricsEvent(int event) { |
| 556 | MetricsLogger.action(this, event, getPackageName()); |
| 557 | } |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 558 | |
| 559 | private static final SparseArray<String> SSL_ERRORS = new SparseArray<>(); |
| 560 | static { |
| 561 | SSL_ERRORS.put(SslError.SSL_NOTYETVALID, "SSL_NOTYETVALID"); |
| 562 | SSL_ERRORS.put(SslError.SSL_EXPIRED, "SSL_EXPIRED"); |
| 563 | SSL_ERRORS.put(SslError.SSL_IDMISMATCH, "SSL_IDMISMATCH"); |
| 564 | SSL_ERRORS.put(SslError.SSL_UNTRUSTED, "SSL_UNTRUSTED"); |
| 565 | SSL_ERRORS.put(SslError.SSL_DATE_INVALID, "SSL_DATE_INVALID"); |
| 566 | SSL_ERRORS.put(SslError.SSL_INVALID, "SSL_INVALID"); |
| 567 | } |
| 568 | |
| 569 | private static String sslErrorName(SslError error) { |
| 570 | return SSL_ERRORS.get(error.getPrimaryError(), "UNKNOWN"); |
| 571 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 572 | } |