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