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 | |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.EXTRA_CAPTIVE_PORTAL_PROBE_SPEC; |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 20 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 21 | import android.app.Activity; |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 22 | import android.app.AlertDialog; |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 23 | import android.app.Application; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 24 | import android.content.Context; |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 25 | import android.content.DialogInterface; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.graphics.Bitmap; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 28 | import android.net.CaptivePortal; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 29 | import android.net.ConnectivityManager; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 30 | import android.net.ConnectivityManager.NetworkCallback; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 31 | import android.net.Network; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 32 | import android.net.NetworkCapabilities; |
| 33 | import android.net.NetworkRequest; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 34 | import android.net.Proxy; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 35 | import android.net.Uri; |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 36 | import android.net.captiveportal.CaptivePortalProbeSpec; |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 37 | import android.net.http.SslCertificate; |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 38 | import android.net.http.SslError; |
Chalard Jean | 3962024 | 2018-04-12 11:52:37 +0900 | [diff] [blame] | 39 | import android.net.wifi.WifiInfo; |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 40 | import android.net.wifi.WifiManager; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 41 | import android.os.Bundle; |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 42 | import android.os.SystemProperties; |
Chalard Jean | 8fa6ea3 | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 43 | import android.support.v4.widget.SwipeRefreshLayout; |
Chalard Jean | 3962024 | 2018-04-12 11:52:37 +0900 | [diff] [blame] | 44 | import android.text.TextUtils; |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 45 | import android.util.ArrayMap; |
| 46 | import android.util.Log; |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 47 | import android.util.SparseArray; |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 48 | import android.util.TypedValue; |
| 49 | import android.view.LayoutInflater; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 50 | import android.view.Menu; |
| 51 | import android.view.MenuItem; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 52 | import android.view.View; |
Lorenzo Colitti | a039850 | 2018-03-26 01:32:33 +0900 | [diff] [blame] | 53 | import android.webkit.CookieManager; |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 54 | import android.webkit.SslErrorHandler; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 55 | import android.webkit.WebChromeClient; |
lucaslin | 8efd38b | 2019-03-19 19:15:54 +0800 | [diff] [blame] | 56 | import android.webkit.WebResourceRequest; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 57 | import android.webkit.WebSettings; |
| 58 | import android.webkit.WebView; |
| 59 | import android.webkit.WebViewClient; |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 60 | import android.widget.LinearLayout; |
Paul Jensen | 8f333f1 | 2014-08-05 22:52:16 -0400 | [diff] [blame] | 61 | import android.widget.ProgressBar; |
Paul Jensen | 5344a4a | 2015-05-06 07:39:36 -0400 | [diff] [blame] | 62 | import android.widget.TextView; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 63 | |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 64 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 65 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 66 | import java.io.IOException; |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 67 | import java.lang.reflect.Field; |
| 68 | import java.lang.reflect.Method; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 69 | import java.net.HttpURLConnection; |
| 70 | import java.net.MalformedURLException; |
| 71 | import java.net.URL; |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 72 | import java.util.Objects; |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 73 | import java.util.Random; |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 74 | import java.util.concurrent.atomic.AtomicBoolean; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 75 | |
| 76 | public class CaptivePortalLoginActivity extends Activity { |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 77 | private static final String TAG = CaptivePortalLoginActivity.class.getSimpleName(); |
| 78 | private static final boolean DBG = true; |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 79 | private static final boolean VDBG = false; |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 80 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 81 | private static final int SOCKET_TIMEOUT_MS = 10000; |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 82 | public static final String HTTP_LOCATION_HEADER_NAME = "Location"; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 83 | |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 84 | private enum Result { |
| 85 | DISMISSED(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_RESULT_DISMISSED), |
| 86 | UNWANTED(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_RESULT_UNWANTED), |
| 87 | WANTED_AS_IS(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_RESULT_WANTED_AS_IS); |
| 88 | |
| 89 | final int metricsEvent; |
| 90 | Result(int metricsEvent) { this.metricsEvent = metricsEvent; } |
| 91 | }; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 92 | |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 93 | private URL mUrl; |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 94 | private CaptivePortalProbeSpec mProbeSpec; |
Hugo Benichi | 2c02197 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 95 | private String mUserAgent; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 96 | private Network mNetwork; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 97 | private CaptivePortal mCaptivePortal; |
Paul Jensen | 8df099d | 2014-09-26 15:19:17 -0400 | [diff] [blame] | 98 | private NetworkCallback mNetworkCallback; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 99 | private ConnectivityManager mCm; |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 100 | private WifiManager mWifiManager; |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 101 | private boolean mLaunchBrowser = false; |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 102 | private MyWebViewClient mWebViewClient; |
Chalard Jean | 8fa6ea3 | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 103 | private SwipeRefreshLayout mSwipeRefreshLayout; |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 104 | // Ensures that done() happens once exactly, handling concurrent callers with atomic operations. |
| 105 | private final AtomicBoolean isDone = new AtomicBoolean(false); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 106 | |
| 107 | @Override |
| 108 | protected void onCreate(Bundle savedInstanceState) { |
| 109 | super.onCreate(savedInstanceState); |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 110 | |
Remi NGUYEN VAN | de60221 | 2019-01-30 15:22:01 +0900 | [diff] [blame] | 111 | mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL); |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 112 | logMetricsEvent(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_ACTIVITY); |
| 113 | |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 114 | mCm = getSystemService(ConnectivityManager.class); |
| 115 | mWifiManager = getSystemService(WifiManager.class); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 116 | mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK); |
Hugo Benichi | ec88fd6 | 2017-03-07 15:10:03 +0900 | [diff] [blame] | 117 | mUserAgent = |
| 118 | getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT); |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 119 | mUrl = getUrl(); |
| 120 | if (mUrl == null) { |
| 121 | // getUrl() failed to parse the url provided in the intent: bail out in a way that |
| 122 | // at least provides network access. |
| 123 | done(Result.WANTED_AS_IS); |
| 124 | return; |
| 125 | } |
| 126 | if (DBG) { |
| 127 | Log.d(TAG, String.format("onCreate for %s", mUrl.toString())); |
| 128 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 129 | |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 130 | final String spec = getIntent().getStringExtra(EXTRA_CAPTIVE_PORTAL_PROBE_SPEC); |
| 131 | try { |
| 132 | mProbeSpec = CaptivePortalProbeSpec.parseSpecOrNull(spec); |
| 133 | } catch (Exception e) { |
| 134 | // Make extra sure that invalid configurations do not cause crashes |
| 135 | mProbeSpec = null; |
| 136 | } |
| 137 | |
Hisanobu, Watanabe | 67c1d26 | 2018-08-20 17:46:54 +0900 | [diff] [blame] | 138 | mNetworkCallback = new NetworkCallback() { |
| 139 | @Override |
| 140 | public void onLost(Network lostNetwork) { |
| 141 | // If the network disappears while the app is up, exit. |
| 142 | if (mNetwork.equals(lostNetwork)) done(Result.UNWANTED); |
| 143 | } |
| 144 | }; |
| 145 | mCm.registerNetworkCallback(new NetworkRequest.Builder().build(), mNetworkCallback); |
| 146 | |
| 147 | // If the network has disappeared, exit. |
| 148 | final NetworkCapabilities networkCapabilities = mCm.getNetworkCapabilities(mNetwork); |
| 149 | if (networkCapabilities == null) { |
| 150 | finishAndRemoveTask(); |
| 151 | return; |
| 152 | } |
| 153 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 154 | // Also initializes proxy system properties. |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 155 | mNetwork = mNetwork.getPrivateDnsBypassingCopy(); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 156 | mCm.bindProcessToNetwork(mNetwork); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 157 | |
| 158 | // Proxy system properties must be initialized before setContentView is called because |
| 159 | // setContentView initializes the WebView logic which in turn reads the system properties. |
| 160 | setContentView(R.layout.activity_captive_portal_login); |
| 161 | |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 162 | getActionBar().setDisplayShowHomeEnabled(false); |
| 163 | getActionBar().setElevation(0); // remove shadow |
| 164 | getActionBar().setTitle(getHeaderTitle()); |
| 165 | getActionBar().setSubtitle(""); |
| 166 | |
| 167 | final WebView webview = getWebview(); |
| 168 | webview.clearCache(true); |
Lorenzo Colitti | a039850 | 2018-03-26 01:32:33 +0900 | [diff] [blame] | 169 | CookieManager.getInstance().setAcceptThirdPartyCookies(webview, true); |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 170 | WebSettings webSettings = webview.getSettings(); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 171 | webSettings.setJavaScriptEnabled(true); |
Lorenzo Colitti | b55bf38 | 2016-10-21 18:41:25 +0900 | [diff] [blame] | 172 | webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); |
Hugo Benichi | 94c5fb3 | 2017-05-11 09:58:14 +0900 | [diff] [blame] | 173 | webSettings.setUseWideViewPort(true); |
| 174 | webSettings.setLoadWithOverviewMode(true); |
| 175 | webSettings.setSupportZoom(true); |
| 176 | webSettings.setBuiltInZoomControls(true); |
| 177 | webSettings.setDisplayZoomControls(false); |
Sehee Park | c2e966d | 2018-11-15 16:46:27 +0900 | [diff] [blame] | 178 | webSettings.setDomStorageEnabled(true); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 179 | mWebViewClient = new MyWebViewClient(); |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 180 | webview.setWebViewClient(mWebViewClient); |
| 181 | webview.setWebChromeClient(new MyWebChromeClient()); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 182 | // Start initial page load so WebView finishes loading proxy settings. |
| 183 | // Actual load of mUrl is initiated by MyWebViewClient. |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 184 | webview.loadData("", "text/html", null); |
Chalard Jean | 8fa6ea3 | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 185 | |
| 186 | mSwipeRefreshLayout = findViewById(R.id.swipe_refresh); |
| 187 | mSwipeRefreshLayout.setOnRefreshListener(() -> { |
| 188 | webview.reload(); |
| 189 | mSwipeRefreshLayout.setRefreshing(true); |
| 190 | }); |
| 191 | |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Find WebView's proxy BroadcastReceiver and prompt it to read proxy system properties. |
| 195 | private void setWebViewProxy() { |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 196 | // TODO: migrate to androidx WebView proxy setting API as soon as it is finalized |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 197 | try { |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 198 | final Field loadedApkField = Application.class.getDeclaredField("mLoadedApk"); |
| 199 | final Class<?> loadedApkClass = loadedApkField.getType(); |
| 200 | final Object loadedApk = loadedApkField.get(getApplication()); |
| 201 | Field receiversField = loadedApkClass.getDeclaredField("mReceivers"); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 202 | receiversField.setAccessible(true); |
| 203 | ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); |
| 204 | for (Object receiverMap : receivers.values()) { |
| 205 | for (Object rec : ((ArrayMap) receiverMap).keySet()) { |
| 206 | Class clazz = rec.getClass(); |
| 207 | if (clazz.getName().contains("ProxyChangeListener")) { |
| 208 | Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, |
| 209 | Intent.class); |
| 210 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
| 211 | onReceiveMethod.invoke(rec, getApplicationContext(), intent); |
| 212 | Log.v(TAG, "Prompting WebView proxy reload."); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } catch (Exception e) { |
| 217 | Log.e(TAG, "Exception while setting WebView proxy: " + e); |
| 218 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 221 | private void done(Result result) { |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 222 | if (isDone.getAndSet(true)) { |
| 223 | // isDone was already true: done() already called |
| 224 | return; |
| 225 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 226 | if (DBG) { |
| 227 | Log.d(TAG, String.format("Result %s for %s", result.name(), mUrl.toString())); |
| 228 | } |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 229 | logMetricsEvent(result.metricsEvent); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 230 | switch (result) { |
| 231 | case DISMISSED: |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 232 | mCaptivePortal.reportCaptivePortalDismissed(); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 233 | break; |
| 234 | case UNWANTED: |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 235 | mCaptivePortal.ignoreNetwork(); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 236 | break; |
| 237 | case WANTED_AS_IS: |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 238 | mCaptivePortal.useNetwork(); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 239 | break; |
| 240 | } |
Paul Jensen | 6a776c83 | 2016-07-19 13:19:39 -0400 | [diff] [blame] | 241 | finishAndRemoveTask(); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public boolean onCreateOptionsMenu(Menu menu) { |
| 246 | getMenuInflater().inflate(R.menu.captive_portal_login, menu); |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | @Override |
Paul Jensen | b6ea9ee | 2014-07-18 12:27:23 -0400 | [diff] [blame] | 251 | public void onBackPressed() { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 252 | WebView myWebView = findViewById(R.id.webview); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 253 | if (myWebView.canGoBack() && mWebViewClient.allowBack()) { |
Paul Jensen | b6ea9ee | 2014-07-18 12:27:23 -0400 | [diff] [blame] | 254 | myWebView.goBack(); |
| 255 | } else { |
| 256 | super.onBackPressed(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | @Override |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 261 | public boolean onOptionsItemSelected(MenuItem item) { |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 262 | final Result result; |
| 263 | final String action; |
| 264 | final int id = item.getItemId(); |
| 265 | switch (id) { |
| 266 | case R.id.action_use_network: |
| 267 | result = Result.WANTED_AS_IS; |
| 268 | action = "USE_NETWORK"; |
| 269 | break; |
| 270 | case R.id.action_do_not_use_network: |
| 271 | result = Result.UNWANTED; |
| 272 | action = "DO_NOT_USE_NETWORK"; |
| 273 | break; |
| 274 | default: |
| 275 | return super.onOptionsItemSelected(item); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 276 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 277 | if (DBG) { |
| 278 | Log.d(TAG, String.format("onOptionsItemSelect %s for %s", action, mUrl.toString())); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 279 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 280 | done(result); |
| 281 | return true; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 284 | @Override |
| 285 | public void onDestroy() { |
| 286 | super.onDestroy(); |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 287 | final WebView webview = (WebView) findViewById(R.id.webview); |
| 288 | if (webview != null) { |
| 289 | webview.stopLoading(); |
| 290 | webview.setWebViewClient(null); |
| 291 | webview.setWebChromeClient(null); |
| 292 | webview.destroy(); |
| 293 | } |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 294 | if (mNetworkCallback != null) { |
Hugo Benichi | a173a63 | 2017-06-17 12:47:33 +0900 | [diff] [blame] | 295 | // mNetworkCallback is not null if mUrl is not null. |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 296 | mCm.unregisterNetworkCallback(mNetworkCallback); |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 297 | } |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 298 | if (mLaunchBrowser) { |
| 299 | // Give time for this network to become default. After 500ms just proceed. |
| 300 | for (int i = 0; i < 5; i++) { |
| 301 | // TODO: This misses when mNetwork underlies a VPN. |
| 302 | if (mNetwork.equals(mCm.getActiveNetwork())) break; |
| 303 | try { |
| 304 | Thread.sleep(100); |
| 305 | } catch (InterruptedException e) { |
| 306 | } |
| 307 | } |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 308 | final String url = mUrl.toString(); |
| 309 | if (DBG) { |
| 310 | Log.d(TAG, "starting activity with intent ACTION_VIEW for " + url); |
| 311 | } |
| 312 | startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 313 | } |
Paul Jensen | 868f624 | 2015-05-18 12:48:28 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 316 | private URL getUrl() { |
| 317 | String url = getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL); |
| 318 | if (url == null) { |
| 319 | url = mCm.getCaptivePortalServerUrl(); |
| 320 | } |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 321 | return makeURL(url); |
| 322 | } |
| 323 | |
| 324 | private static URL makeURL(String url) { |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 325 | try { |
| 326 | return new URL(url); |
| 327 | } catch (MalformedURLException e) { |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 328 | Log.e(TAG, "Invalid URL " + url); |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 329 | } |
| 330 | return null; |
| 331 | } |
| 332 | |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 333 | private static String host(URL url) { |
| 334 | if (url == null) { |
| 335 | return null; |
| 336 | } |
| 337 | return url.getHost(); |
| 338 | } |
| 339 | |
| 340 | private static String sanitizeURL(URL url) { |
| 341 | // In non-Debug build, only show host to avoid leaking private info. |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 342 | return isDebuggable() ? Objects.toString(url) : host(url); |
| 343 | } |
| 344 | |
| 345 | private static boolean isDebuggable() { |
| 346 | return SystemProperties.getInt("ro.debuggable", 0) == 1; |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 347 | } |
| 348 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 349 | private void testForCaptivePortal() { |
Hugo Benichi | 2c02197 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 350 | // TODO: reuse NetworkMonitor facilities for consistent captive portal detection. |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 351 | new Thread(new Runnable() { |
| 352 | public void run() { |
| 353 | // Give time for captive portal to open. |
| 354 | try { |
| 355 | Thread.sleep(1000); |
| 356 | } catch (InterruptedException e) { |
| 357 | } |
| 358 | HttpURLConnection urlConnection = null; |
| 359 | int httpResponseCode = 500; |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 360 | String locationHeader = null; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 361 | try { |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 362 | urlConnection = (HttpURLConnection) mNetwork.openConnection(mUrl); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 363 | urlConnection.setInstanceFollowRedirects(false); |
| 364 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 365 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 366 | urlConnection.setUseCaches(false); |
Hugo Benichi | 2c02197 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 367 | if (mUserAgent != null) { |
| 368 | urlConnection.setRequestProperty("User-Agent", mUserAgent); |
| 369 | } |
Hugo Benichi | ec88fd6 | 2017-03-07 15:10:03 +0900 | [diff] [blame] | 370 | // cannot read request header after connection |
| 371 | String requestHeader = urlConnection.getRequestProperties().toString(); |
| 372 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 373 | urlConnection.getInputStream(); |
| 374 | httpResponseCode = urlConnection.getResponseCode(); |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 375 | locationHeader = urlConnection.getHeaderField(HTTP_LOCATION_HEADER_NAME); |
Hugo Benichi | ec88fd6 | 2017-03-07 15:10:03 +0900 | [diff] [blame] | 376 | if (DBG) { |
| 377 | Log.d(TAG, "probe at " + mUrl + |
| 378 | " ret=" + httpResponseCode + |
| 379 | " request=" + requestHeader + |
| 380 | " headers=" + urlConnection.getHeaderFields()); |
| 381 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 382 | } catch (IOException e) { |
| 383 | } finally { |
| 384 | if (urlConnection != null) urlConnection.disconnect(); |
| 385 | } |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 386 | if (isDismissed(httpResponseCode, locationHeader, mProbeSpec)) { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 387 | done(Result.DISMISSED); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | }).start(); |
| 391 | } |
| 392 | |
Remi NGUYEN VAN | 9f85505 | 2018-05-22 17:04:17 +0900 | [diff] [blame] | 393 | private static boolean isDismissed( |
| 394 | int httpResponseCode, String locationHeader, CaptivePortalProbeSpec probeSpec) { |
| 395 | return (probeSpec != null) |
| 396 | ? probeSpec.getResult(httpResponseCode, locationHeader).isSuccessful() |
| 397 | : (httpResponseCode == 204); |
| 398 | } |
| 399 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 400 | private class MyWebViewClient extends WebViewClient { |
Paul Jensen | 5344a4a | 2015-05-06 07:39:36 -0400 | [diff] [blame] | 401 | private static final String INTERNAL_ASSETS = "file:///android_asset/"; |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 402 | |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 403 | private final String mBrowserBailOutToken = Long.toString(new Random().nextLong()); |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 404 | private final String mCertificateOutToken = Long.toString(new Random().nextLong()); |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 405 | // How many Android device-independent-pixels per scaled-pixel |
| 406 | // dp/sp = (px/sp) / (px/dp) = (1/sp) / (1/dp) |
| 407 | private final float mDpPerSp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 1, |
| 408 | getResources().getDisplayMetrics()) / |
| 409 | TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, |
| 410 | getResources().getDisplayMetrics()); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 411 | private int mPagesLoaded; |
lucaslin | 8efd38b | 2019-03-19 19:15:54 +0800 | [diff] [blame] | 412 | private String mMainFrameUrl; |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 413 | |
| 414 | // If we haven't finished cleaning up the history, don't allow going back. |
| 415 | public boolean allowBack() { |
| 416 | return mPagesLoaded > 1; |
| 417 | } |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 418 | |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 419 | private String mSslErrorTitle = null; |
| 420 | private SslErrorHandler mSslErrorHandler = null; |
| 421 | private SslError mSslError = null; |
| 422 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 423 | @Override |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 424 | public void onPageStarted(WebView view, String urlString, Bitmap favicon) { |
| 425 | if (urlString.contains(mBrowserBailOutToken)) { |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 426 | mLaunchBrowser = true; |
| 427 | done(Result.WANTED_AS_IS); |
| 428 | return; |
| 429 | } |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 430 | // The first page load is used only to cause the WebView to |
| 431 | // fetch the proxy settings. Don't update the URL bar, and |
| 432 | // don't check if the captive portal is still there. |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 433 | if (mPagesLoaded == 0) { |
| 434 | return; |
| 435 | } |
| 436 | final URL url = makeURL(urlString); |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 437 | Log.d(TAG, "onPageStarted: " + sanitizeURL(url)); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 438 | // For internally generated pages, leave URL bar listing prior URL as this is the URL |
| 439 | // the page refers to. |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 440 | if (!urlString.startsWith(INTERNAL_ASSETS)) { |
| 441 | String subtitle = (url != null) ? getHeaderSubtitle(url) : urlString; |
| 442 | getActionBar().setSubtitle(subtitle); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 443 | } |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 444 | getProgressBar().setVisibility(View.VISIBLE); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 445 | testForCaptivePortal(); |
| 446 | } |
| 447 | |
| 448 | @Override |
| 449 | public void onPageFinished(WebView view, String url) { |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 450 | mPagesLoaded++; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 451 | getProgressBar().setVisibility(View.INVISIBLE); |
Chalard Jean | 8fa6ea3 | 2018-03-09 22:28:51 +0900 | [diff] [blame] | 452 | mSwipeRefreshLayout.setRefreshing(false); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 453 | if (mPagesLoaded == 1) { |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 454 | // Now that WebView has loaded at least one page we know it has read in the proxy |
| 455 | // settings. Now prompt the WebView read the Network-specific proxy settings. |
| 456 | setWebViewProxy(); |
| 457 | // Load the real page. |
Hugo Benichi | 7f086e1 | 2016-12-06 15:36:30 +0900 | [diff] [blame] | 458 | view.loadUrl(mUrl.toString()); |
Paul Jensen | 88eb0fa | 2014-10-02 13:43:39 -0400 | [diff] [blame] | 459 | return; |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 460 | } else if (mPagesLoaded == 2) { |
| 461 | // Prevent going back to empty first page. |
susnata | 9764040 | 2017-06-23 09:13:05 -0700 | [diff] [blame] | 462 | // Fix for missing focus, see b/62449959 for details. Remove it once we get a |
| 463 | // newer version of WebView (60.x.y). |
| 464 | view.requestFocus(); |
Paul Jensen | e836b68 | 2015-05-19 12:30:56 -0400 | [diff] [blame] | 465 | view.clearHistory(); |
Paul Jensen | 5344a4a | 2015-05-06 07:39:36 -0400 | [diff] [blame] | 466 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 467 | testForCaptivePortal(); |
| 468 | } |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 469 | |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 470 | // Convert Android scaled-pixels (sp) to HTML size. |
| 471 | private String sp(int sp) { |
| 472 | // Convert sp to dp's. |
| 473 | float dp = sp * mDpPerSp; |
| 474 | // Apply a scale factor to make things look right. |
| 475 | dp *= 1.3; |
| 476 | // Convert dp's to HTML size. |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 477 | // HTML px's are scaled just like dp's, so just add "px" suffix. |
| 478 | return Integer.toString((int)dp) + "px"; |
Paul Jensen | 65636fb | 2015-05-06 14:40:59 -0400 | [diff] [blame] | 479 | } |
| 480 | |
lucaslin | 8efd38b | 2019-03-19 19:15:54 +0800 | [diff] [blame] | 481 | // Check if webview is trying to load the main frame and record its url. |
| 482 | @Override |
| 483 | public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { |
| 484 | if (request.isForMainFrame()) { |
| 485 | mMainFrameUrl = request.getUrl().toString(); |
| 486 | } |
| 487 | return false; |
| 488 | } |
| 489 | |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 490 | // 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] | 491 | |
| 492 | @Override |
| 493 | public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { |
lucaslin | 8efd38b | 2019-03-19 19:15:54 +0800 | [diff] [blame] | 494 | final URL errorUrl = makeURL(error.getUrl()); |
| 495 | final URL mainFrameUrl = makeURL(mMainFrameUrl); |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 496 | Log.d(TAG, String.format("SSL error: %s, url: %s, certificate: %s", |
lucaslin | 8efd38b | 2019-03-19 19:15:54 +0800 | [diff] [blame] | 497 | sslErrorName(error), sanitizeURL(errorUrl), error.getCertificate())); |
| 498 | if (errorUrl == null |
| 499 | // Ignore SSL errors from resources by comparing the main frame url with SSL |
| 500 | // error url. |
| 501 | || !errorUrl.equals(mainFrameUrl)) { |
| 502 | Log.d(TAG, "onReceivedSslError: mMainFrameUrl = " + mMainFrameUrl); |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 503 | handler.cancel(); |
| 504 | return; |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 505 | } |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 506 | logMetricsEvent(MetricsEvent.CAPTIVE_PORTAL_LOGIN_ACTIVITY_SSL_ERROR); |
| 507 | final String sslErrorPage = makeSslErrorPage(); |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 508 | view.loadDataWithBaseURL(INTERNAL_ASSETS, sslErrorPage, "text/HTML", "UTF-8", null); |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 509 | mSslErrorTitle = view.getTitle() == null ? "" : view.getTitle(); |
| 510 | mSslErrorHandler = handler; |
| 511 | mSslError = error; |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | private String makeSslErrorPage() { |
| 515 | final String warningMsg = getString(R.string.ssl_error_warning); |
| 516 | final String exampleMsg = getString(R.string.ssl_error_example); |
| 517 | final String continueMsg = getString(R.string.ssl_error_continue); |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 518 | final String certificateMsg = getString(R.string.ssl_error_view_certificate); |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 519 | return String.join("\n", |
| 520 | "<html>", |
| 521 | "<head>", |
| 522 | " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">", |
| 523 | " <style>", |
| 524 | " body {", |
| 525 | " background-color:#fafafa;", |
| 526 | " margin:auto;", |
| 527 | " width:80%;", |
| 528 | " margin-top: 96px", |
| 529 | " }", |
| 530 | " img {", |
| 531 | " height:48px;", |
| 532 | " width:48px;", |
| 533 | " }", |
| 534 | " div.warn {", |
| 535 | " font-size:" + sp(16) + ";", |
| 536 | " line-height:1.28;", |
| 537 | " margin-top:16px;", |
| 538 | " opacity:0.87;", |
| 539 | " }", |
| 540 | " div.example {", |
| 541 | " font-size:" + sp(14) + ";", |
| 542 | " line-height:1.21905;", |
| 543 | " margin-top:16px;", |
| 544 | " opacity:0.54;", |
| 545 | " }", |
| 546 | " a {", |
| 547 | " color:#4285F4;", |
| 548 | " display:inline-block;", |
| 549 | " font-size:" + sp(14) + ";", |
| 550 | " font-weight:bold;", |
| 551 | " height:48px;", |
| 552 | " margin-top:24px;", |
| 553 | " text-decoration:none;", |
| 554 | " text-transform:uppercase;", |
| 555 | " }", |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 556 | " a.certificate {", |
| 557 | " margin-top:0px;", |
| 558 | " }", |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 559 | " </style>", |
| 560 | "</head>", |
| 561 | "<body>", |
| 562 | " <p><img src=quantum_ic_warning_amber_96.png><br>", |
| 563 | " <div class=warn>" + warningMsg + "</div>", |
| 564 | " <div class=example>" + exampleMsg + "</div>", |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 565 | " <a href=" + mBrowserBailOutToken + ">" + continueMsg + "</a><br>", |
| 566 | " <a class=certificate href=" + mCertificateOutToken + ">" + certificateMsg + |
| 567 | "</a>", |
Hugo Benichi | 60d5f46 | 2017-06-02 10:12:09 +0900 | [diff] [blame] | 568 | "</body>", |
| 569 | "</html>"); |
Paul Jensen | fc8022f | 2014-12-09 15:18:40 -0500 | [diff] [blame] | 570 | } |
Paul Jensen | fd54da9 | 2015-06-09 07:50:51 -0400 | [diff] [blame] | 571 | |
| 572 | @Override |
| 573 | public boolean shouldOverrideUrlLoading (WebView view, String url) { |
| 574 | if (url.startsWith("tel:")) { |
| 575 | startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); |
| 576 | return true; |
| 577 | } |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 578 | if (url.contains(mCertificateOutToken) && mSslError != null) { |
| 579 | showSslAlertDialog(mSslErrorHandler, mSslError, mSslErrorTitle); |
| 580 | return true; |
| 581 | } |
Paul Jensen | fd54da9 | 2015-06-09 07:50:51 -0400 | [diff] [blame] | 582 | return false; |
| 583 | } |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 584 | private void showSslAlertDialog(SslErrorHandler handler, SslError error, String title) { |
| 585 | final LayoutInflater factory = LayoutInflater.from(CaptivePortalLoginActivity.this); |
| 586 | final View sslWarningView = factory.inflate(R.layout.ssl_warning, null); |
| 587 | |
| 588 | // Set Security certificate |
| 589 | setViewSecurityCertificate(sslWarningView.findViewById(R.id.certificate_layout), error); |
| 590 | ((TextView) sslWarningView.findViewById(R.id.ssl_error_type)) |
| 591 | .setText(sslErrorName(error)); |
| 592 | ((TextView) sslWarningView.findViewById(R.id.title)).setText(mSslErrorTitle); |
| 593 | ((TextView) sslWarningView.findViewById(R.id.address)).setText(error.getUrl()); |
| 594 | |
| 595 | AlertDialog sslAlertDialog = new AlertDialog.Builder(CaptivePortalLoginActivity.this) |
| 596 | .setTitle(R.string.ssl_security_warning_title) |
| 597 | .setView(sslWarningView) |
| 598 | .setPositiveButton(R.string.ok, (DialogInterface dialog, int whichButton) -> { |
| 599 | // handler.cancel is called via OnCancelListener. |
| 600 | dialog.cancel(); |
| 601 | }) |
| 602 | .setOnCancelListener((DialogInterface dialogInterface) -> handler.cancel()) |
| 603 | .create(); |
| 604 | sslAlertDialog.show(); |
| 605 | } |
| 606 | |
| 607 | private void setViewSecurityCertificate(LinearLayout certificateLayout, SslError error) { |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 608 | ((TextView) certificateLayout.findViewById(R.id.ssl_error_msg)) |
| 609 | .setText(sslErrorMessage(error)); |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 610 | SslCertificate cert = error.getCertificate(); |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 611 | // TODO: call the method directly once inflateCertificateView is @SystemApi |
| 612 | try { |
| 613 | final View certificateView = (View) SslCertificate.class.getMethod( |
| 614 | "inflateCertificateView", Context.class) |
| 615 | .invoke(cert, CaptivePortalLoginActivity.this); |
| 616 | certificateLayout.addView(certificateView); |
| 617 | } catch (ReflectiveOperationException | SecurityException e) { |
| 618 | Log.e(TAG, "Could not create certificate view", e); |
| 619 | } |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 620 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | private class MyWebChromeClient extends WebChromeClient { |
| 624 | @Override |
| 625 | public void onProgressChanged(WebView view, int newProgress) { |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 626 | getProgressBar().setProgress(newProgress); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 627 | } |
| 628 | } |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 629 | |
| 630 | private ProgressBar getProgressBar() { |
| 631 | return findViewById(R.id.progress_bar); |
| 632 | } |
| 633 | |
| 634 | private WebView getWebview() { |
| 635 | return findViewById(R.id.webview); |
| 636 | } |
| 637 | |
| 638 | private String getHeaderTitle() { |
Hugo Benichi | 3a22297 | 2017-06-01 12:58:49 +0900 | [diff] [blame] | 639 | NetworkCapabilities nc = mCm.getNetworkCapabilities(mNetwork); |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 640 | final String ssid = getSsid(); |
| 641 | if (TextUtils.isEmpty(ssid) |
| 642 | || nc == null || !nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
Hugo Benichi | 3a22297 | 2017-06-01 12:58:49 +0900 | [diff] [blame] | 643 | return getString(R.string.action_bar_label); |
| 644 | } |
Remi NGUYEN VAN | 4727427 | 2019-01-30 23:39:24 +0900 | [diff] [blame] | 645 | return getString(R.string.action_bar_title, ssid); |
| 646 | } |
| 647 | |
| 648 | // TODO: remove once SSID is obtained from NetworkCapabilities |
| 649 | private String getSsid() { |
| 650 | if (mWifiManager == null) { |
| 651 | return null; |
| 652 | } |
| 653 | final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); |
| 654 | return removeDoubleQuotes(wifiInfo.getSSID()); |
| 655 | } |
| 656 | |
| 657 | private static String removeDoubleQuotes(String string) { |
| 658 | if (string == null) return null; |
| 659 | final int length = string.length(); |
| 660 | if ((length > 1) && (string.charAt(0) == '"') && (string.charAt(length - 1) == '"')) { |
| 661 | return string.substring(1, length - 1); |
| 662 | } |
| 663 | return string; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 664 | } |
| 665 | |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 666 | private String getHeaderSubtitle(URL url) { |
| 667 | String host = host(url); |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 668 | final String https = "https"; |
| 669 | if (https.equals(url.getProtocol())) { |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 670 | return https + "://" + host; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 671 | } |
Hugo Benichi | 12df465 | 2017-06-17 13:36:35 +0900 | [diff] [blame] | 672 | return host; |
Hugo Benichi | a206649 | 2017-05-17 09:26:30 +0900 | [diff] [blame] | 673 | } |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 674 | |
| 675 | private void logMetricsEvent(int event) { |
Remi NGUYEN VAN | de60221 | 2019-01-30 15:22:01 +0900 | [diff] [blame] | 676 | mCaptivePortal.logEvent(event, getPackageName()); |
Hugo Benichi | 9e8ab43 | 2017-06-05 14:52:24 +0900 | [diff] [blame] | 677 | } |
Hugo Benichi | 04d7860 | 2017-07-19 21:20:53 +0900 | [diff] [blame] | 678 | |
| 679 | private static final SparseArray<String> SSL_ERRORS = new SparseArray<>(); |
| 680 | static { |
| 681 | SSL_ERRORS.put(SslError.SSL_NOTYETVALID, "SSL_NOTYETVALID"); |
| 682 | SSL_ERRORS.put(SslError.SSL_EXPIRED, "SSL_EXPIRED"); |
| 683 | SSL_ERRORS.put(SslError.SSL_IDMISMATCH, "SSL_IDMISMATCH"); |
| 684 | SSL_ERRORS.put(SslError.SSL_UNTRUSTED, "SSL_UNTRUSTED"); |
| 685 | SSL_ERRORS.put(SslError.SSL_DATE_INVALID, "SSL_DATE_INVALID"); |
| 686 | SSL_ERRORS.put(SslError.SSL_INVALID, "SSL_INVALID"); |
| 687 | } |
| 688 | |
| 689 | private static String sslErrorName(SslError error) { |
| 690 | return SSL_ERRORS.get(error.getPrimaryError(), "UNKNOWN"); |
| 691 | } |
Takayuki, Ogura | cc38781 | 2018-09-13 12:40:28 +0900 | [diff] [blame] | 692 | |
| 693 | private static final SparseArray<Integer> SSL_ERROR_MSGS = new SparseArray<>(); |
| 694 | static { |
| 695 | SSL_ERROR_MSGS.put(SslError.SSL_NOTYETVALID, R.string.ssl_error_not_yet_valid); |
| 696 | SSL_ERROR_MSGS.put(SslError.SSL_EXPIRED, R.string.ssl_error_expired); |
| 697 | SSL_ERROR_MSGS.put(SslError.SSL_IDMISMATCH, R.string.ssl_error_mismatch); |
| 698 | SSL_ERROR_MSGS.put(SslError.SSL_UNTRUSTED, R.string.ssl_error_untrusted); |
| 699 | SSL_ERROR_MSGS.put(SslError.SSL_DATE_INVALID, R.string.ssl_error_date_invalid); |
| 700 | SSL_ERROR_MSGS.put(SslError.SSL_INVALID, R.string.ssl_error_invalid); |
| 701 | } |
| 702 | |
| 703 | private static Integer sslErrorMessage(SslError error) { |
| 704 | return SSL_ERROR_MSGS.get(error.getPrimaryError(), R.string.ssl_error_unknown); |
| 705 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 706 | } |