fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.carrierdefaultapp; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.LoadedApk; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 23 | import android.content.pm.ActivityInfo; |
| 24 | import android.content.pm.PackageInfo; |
| 25 | import android.content.pm.PackageManager; |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
| 27 | import android.net.ConnectivityManager; |
| 28 | import android.net.ConnectivityManager.NetworkCallback; |
| 29 | import android.net.Network; |
| 30 | import android.net.NetworkCapabilities; |
| 31 | import android.net.NetworkRequest; |
| 32 | import android.net.Proxy; |
| 33 | import android.net.TrafficStats; |
| 34 | import android.net.Uri; |
| 35 | import android.net.http.SslError; |
| 36 | import android.os.Bundle; |
| 37 | import android.telephony.CarrierConfigManager; |
| 38 | import android.telephony.Rlog; |
| 39 | import android.telephony.SubscriptionManager; |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 40 | import android.text.TextUtils; |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 41 | import android.util.ArrayMap; |
| 42 | import android.util.Log; |
| 43 | import android.util.TypedValue; |
| 44 | import android.webkit.SslErrorHandler; |
| 45 | import android.webkit.WebChromeClient; |
| 46 | import android.webkit.WebSettings; |
| 47 | import android.webkit.WebView; |
| 48 | import android.webkit.WebViewClient; |
| 49 | import android.widget.ProgressBar; |
| 50 | import android.widget.TextView; |
| 51 | |
| 52 | import com.android.internal.telephony.PhoneConstants; |
| 53 | import com.android.internal.telephony.TelephonyIntents; |
| 54 | import com.android.internal.util.ArrayUtils; |
| 55 | |
| 56 | import java.io.IOException; |
| 57 | import java.lang.reflect.Field; |
| 58 | import java.lang.reflect.Method; |
| 59 | import java.net.HttpURLConnection; |
| 60 | import java.net.MalformedURLException; |
| 61 | import java.net.URL; |
| 62 | import java.util.Random; |
| 63 | |
| 64 | /** |
| 65 | * Activity that launches in response to the captive portal notification |
| 66 | * @see com.android.carrierdefaultapp.CarrierActionUtils#CARRIER_ACTION_SHOW_PORTAL_NOTIFICATION |
| 67 | * This activity requests network connection if there is no available one before loading the real |
| 68 | * portal page and apply carrier actions on the portal activation result. |
| 69 | */ |
| 70 | public class CaptivePortalLoginActivity extends Activity { |
| 71 | private static final String TAG = CaptivePortalLoginActivity.class.getSimpleName(); |
| 72 | private static final boolean DBG = true; |
| 73 | |
| 74 | private static final int SOCKET_TIMEOUT_MS = 10 * 1000; |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 75 | private static final int NETWORK_REQUEST_TIMEOUT_MS = 5 * 1000; |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 76 | |
| 77 | private URL mUrl; |
| 78 | private Network mNetwork; |
| 79 | private NetworkCallback mNetworkCallback; |
| 80 | private ConnectivityManager mCm; |
| 81 | private WebView mWebView; |
| 82 | private MyWebViewClient mWebViewClient; |
| 83 | private boolean mLaunchBrowser = false; |
fionaxu | f62e4cc | 2017-07-21 18:02:30 -0700 | [diff] [blame] | 84 | private Thread mTestingThread = null; |
fionaxu | 1c0a86f | 2017-08-31 14:40:41 -0700 | [diff] [blame] | 85 | private boolean mReload = false; |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | protected void onCreate(Bundle savedInstanceState) { |
| 89 | super.onCreate(savedInstanceState); |
| 90 | mCm = ConnectivityManager.from(this); |
| 91 | mUrl = getUrlForCaptivePortal(); |
| 92 | if (mUrl == null) { |
| 93 | done(false); |
| 94 | return; |
| 95 | } |
| 96 | if (DBG) logd(String.format("onCreate for %s", mUrl.toString())); |
| 97 | setContentView(R.layout.activity_captive_portal_login); |
| 98 | getActionBar().setDisplayShowHomeEnabled(false); |
| 99 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 100 | mWebView = findViewById(R.id.webview); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 101 | mWebView.clearCache(true); |
| 102 | WebSettings webSettings = mWebView.getSettings(); |
| 103 | webSettings.setJavaScriptEnabled(true); |
| 104 | webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); |
fionaxu | 2e0915f | 2017-05-03 23:19:25 -0700 | [diff] [blame] | 105 | webSettings.setUseWideViewPort(true); |
| 106 | webSettings.setLoadWithOverviewMode(true); |
| 107 | webSettings.setSupportZoom(true); |
| 108 | webSettings.setBuiltInZoomControls(true); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 109 | mWebViewClient = new MyWebViewClient(); |
| 110 | mWebView.setWebViewClient(mWebViewClient); |
| 111 | mWebView.setWebChromeClient(new MyWebChromeClient()); |
| 112 | |
Erik Kline | 6a1707a | 2018-04-26 12:04:40 +0900 | [diff] [blame] | 113 | final Network network = getNetworkForCaptivePortal(); |
| 114 | if (network == null) { |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 115 | requestNetworkForCaptivePortal(); |
| 116 | } else { |
Erik Kline | 6a1707a | 2018-04-26 12:04:40 +0900 | [diff] [blame] | 117 | setNetwork(network); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 118 | // Start initial page load so WebView finishes loading proxy settings. |
| 119 | // Actual load of mUrl is initiated by MyWebViewClient. |
| 120 | mWebView.loadData("", "text/html", null); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onBackPressed() { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 126 | WebView myWebView = findViewById(R.id.webview); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 127 | if (myWebView.canGoBack() && mWebViewClient.allowBack()) { |
| 128 | myWebView.goBack(); |
| 129 | } else { |
| 130 | super.onBackPressed(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void onDestroy() { |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 136 | if (mLaunchBrowser) { |
| 137 | // Give time for this network to become default. After 500ms just proceed. |
| 138 | for (int i = 0; i < 5; i++) { |
| 139 | // TODO: This misses when mNetwork underlies a VPN. |
| 140 | if (mNetwork.equals(mCm.getActiveNetwork())) break; |
| 141 | try { |
| 142 | Thread.sleep(100); |
| 143 | } catch (InterruptedException e) { |
| 144 | } |
| 145 | } |
| 146 | final String url = mUrl.toString(); |
| 147 | if (DBG) logd("starting activity with intent ACTION_VIEW for " + url); |
| 148 | startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); |
| 149 | } |
fionaxu | f62e4cc | 2017-07-21 18:02:30 -0700 | [diff] [blame] | 150 | |
| 151 | if (mTestingThread != null) { |
| 152 | mTestingThread.interrupt(); |
| 153 | } |
| 154 | mWebView.destroy(); |
| 155 | releaseNetworkRequest(); |
| 156 | super.onDestroy(); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Erik Kline | 6a1707a | 2018-04-26 12:04:40 +0900 | [diff] [blame] | 159 | private void setNetwork(Network network) { |
| 160 | if (network != null) { |
Erik Kline | 95999bd | 2018-09-12 14:37:11 +0900 | [diff] [blame] | 161 | network = network.getPrivateDnsBypassingCopy(); |
Erik Kline | 6a1707a | 2018-04-26 12:04:40 +0900 | [diff] [blame] | 162 | mCm.bindProcessToNetwork(network); |
Erik Kline | 95999bd | 2018-09-12 14:37:11 +0900 | [diff] [blame] | 163 | mCm.setProcessDefaultNetworkForHostResolution(network); |
Erik Kline | 6a1707a | 2018-04-26 12:04:40 +0900 | [diff] [blame] | 164 | } |
| 165 | mNetwork = network; |
| 166 | } |
| 167 | |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 168 | // Find WebView's proxy BroadcastReceiver and prompt it to read proxy system properties. |
| 169 | private void setWebViewProxy() { |
| 170 | LoadedApk loadedApk = getApplication().mLoadedApk; |
| 171 | try { |
| 172 | Field receiversField = LoadedApk.class.getDeclaredField("mReceivers"); |
| 173 | receiversField.setAccessible(true); |
| 174 | ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); |
| 175 | for (Object receiverMap : receivers.values()) { |
| 176 | for (Object rec : ((ArrayMap) receiverMap).keySet()) { |
| 177 | Class clazz = rec.getClass(); |
| 178 | if (clazz.getName().contains("ProxyChangeListener")) { |
| 179 | Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, |
| 180 | Intent.class); |
| 181 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
| 182 | onReceiveMethod.invoke(rec, getApplicationContext(), intent); |
| 183 | Log.v(TAG, "Prompting WebView proxy reload."); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } catch (Exception e) { |
| 188 | loge("Exception while setting WebView proxy: " + e); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private void done(boolean success) { |
Qiongcheng Luo | fd11ce5 | 2017-08-04 14:34:34 +0800 | [diff] [blame] | 193 | if (DBG) logd(String.format("Result success %b for %s", success, |
| 194 | mUrl != null ? mUrl.toString() : "null")); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 195 | if (success) { |
| 196 | // Trigger re-evaluation upon success http response code |
| 197 | CarrierActionUtils.applyCarrierAction( |
fionaxu | 79d77bb | 2017-07-19 11:11:54 -0700 | [diff] [blame] | 198 | CarrierActionUtils.CARRIER_ACTION_RESET_ALL, getIntent(), |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 199 | getApplicationContext()); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 200 | } |
| 201 | finishAndRemoveTask(); |
| 202 | } |
| 203 | |
| 204 | private URL getUrlForCaptivePortal() { |
| 205 | String url = getIntent().getStringExtra(TelephonyIntents.EXTRA_REDIRECTION_URL_KEY); |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 206 | if (TextUtils.isEmpty(url)) url = mCm.getCaptivePortalServerUrl(); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 207 | final CarrierConfigManager configManager = getApplicationContext() |
| 208 | .getSystemService(CarrierConfigManager.class); |
| 209 | final int subId = getIntent().getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 210 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 211 | final String[] portalURLs = configManager.getConfigForSubId(subId).getStringArray( |
| 212 | CarrierConfigManager.KEY_CARRIER_DEFAULT_REDIRECTION_URL_STRING_ARRAY); |
| 213 | if (!ArrayUtils.isEmpty(portalURLs)) { |
| 214 | for (String portalUrl : portalURLs) { |
| 215 | if (url.startsWith(portalUrl)) { |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | url = null; |
| 220 | } |
| 221 | try { |
| 222 | return new URL(url); |
| 223 | } catch (MalformedURLException e) { |
| 224 | loge("Invalid captive portal URL " + url); |
| 225 | } |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | private void testForCaptivePortal() { |
fionaxu | f62e4cc | 2017-07-21 18:02:30 -0700 | [diff] [blame] | 230 | mTestingThread = new Thread(new Runnable() { |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 231 | public void run() { |
| 232 | // Give time for captive portal to open. |
| 233 | try { |
| 234 | Thread.sleep(1000); |
| 235 | } catch (InterruptedException e) { |
| 236 | } |
fionaxu | f62e4cc | 2017-07-21 18:02:30 -0700 | [diff] [blame] | 237 | if (isFinishing() || isDestroyed()) return; |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 238 | HttpURLConnection urlConnection = null; |
| 239 | int httpResponseCode = 500; |
| 240 | int oldTag = TrafficStats.getAndSetThreadStatsTag(TrafficStats.TAG_SYSTEM_PROBE); |
| 241 | try { |
Erik Kline | 95999bd | 2018-09-12 14:37:11 +0900 | [diff] [blame] | 242 | urlConnection = (HttpURLConnection) mNetwork.openConnection( |
fionaxu | c457a04 | 2017-07-10 09:34:07 -0700 | [diff] [blame] | 243 | new URL(mCm.getCaptivePortalServerUrl())); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 244 | urlConnection.setInstanceFollowRedirects(false); |
| 245 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 246 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 247 | urlConnection.setUseCaches(false); |
| 248 | urlConnection.getInputStream(); |
| 249 | httpResponseCode = urlConnection.getResponseCode(); |
| 250 | } catch (IOException e) { |
fionaxu | c457a04 | 2017-07-10 09:34:07 -0700 | [diff] [blame] | 251 | loge(e.getMessage()); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 252 | } finally { |
| 253 | if (urlConnection != null) urlConnection.disconnect(); |
| 254 | TrafficStats.setThreadStatsTag(oldTag); |
| 255 | } |
| 256 | if (httpResponseCode == 204) { |
| 257 | done(true); |
| 258 | } |
| 259 | } |
fionaxu | f62e4cc | 2017-07-21 18:02:30 -0700 | [diff] [blame] | 260 | }); |
| 261 | mTestingThread.start(); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private Network getNetworkForCaptivePortal() { |
| 265 | Network[] info = mCm.getAllNetworks(); |
| 266 | if (!ArrayUtils.isEmpty(info)) { |
| 267 | for (Network nw : info) { |
| 268 | final NetworkCapabilities nc = mCm.getNetworkCapabilities(nw); |
| 269 | if (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 270 | && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 271 | return nw; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | return null; |
| 276 | } |
| 277 | |
| 278 | private void requestNetworkForCaptivePortal() { |
| 279 | NetworkRequest request = new NetworkRequest.Builder() |
| 280 | .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) |
| 281 | .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) |
| 282 | .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) |
| 283 | .build(); |
| 284 | |
| 285 | mNetworkCallback = new ConnectivityManager.NetworkCallback() { |
| 286 | @Override |
| 287 | public void onAvailable(Network network) { |
| 288 | if (DBG) logd("Network available: " + network); |
Erik Kline | 6a1707a | 2018-04-26 12:04:40 +0900 | [diff] [blame] | 289 | setNetwork(network); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 290 | runOnUiThreadIfNotFinishing(() -> { |
fionaxu | 1c0a86f | 2017-08-31 14:40:41 -0700 | [diff] [blame] | 291 | if (mReload) { |
| 292 | mWebView.reload(); |
| 293 | } else { |
| 294 | // Start initial page load so WebView finishes loading proxy settings. |
| 295 | // Actual load of mUrl is initiated by MyWebViewClient. |
| 296 | mWebView.loadData("", "text/html", null); |
| 297 | } |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 298 | }); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public void onUnavailable() { |
| 303 | if (DBG) logd("Network unavailable"); |
| 304 | runOnUiThreadIfNotFinishing(() -> { |
| 305 | // Instead of not loading anything in webview, simply load the page and return |
| 306 | // HTTP error page in the absence of network connection. |
| 307 | mWebView.loadUrl(mUrl.toString()); |
| 308 | }); |
| 309 | } |
fionaxu | 1c0a86f | 2017-08-31 14:40:41 -0700 | [diff] [blame] | 310 | |
| 311 | @Override |
| 312 | public void onLost(Network lostNetwork) { |
| 313 | if (DBG) logd("Network lost"); |
| 314 | mReload = true; |
| 315 | } |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 316 | }; |
| 317 | logd("request Network for captive portal"); |
| 318 | mCm.requestNetwork(request, mNetworkCallback, NETWORK_REQUEST_TIMEOUT_MS); |
| 319 | } |
| 320 | |
| 321 | private void releaseNetworkRequest() { |
| 322 | logd("release Network for captive portal"); |
| 323 | if (mNetworkCallback != null) { |
| 324 | mCm.unregisterNetworkCallback(mNetworkCallback); |
| 325 | mNetworkCallback = null; |
| 326 | mNetwork = null; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | private class MyWebViewClient extends WebViewClient { |
| 331 | private static final String INTERNAL_ASSETS = "file:///android_asset/"; |
| 332 | private final String mBrowserBailOutToken = Long.toString(new Random().nextLong()); |
| 333 | // How many Android device-independent-pixels per scaled-pixel |
| 334 | // dp/sp = (px/sp) / (px/dp) = (1/sp) / (1/dp) |
| 335 | private final float mDpPerSp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 1, |
| 336 | getResources().getDisplayMetrics()) |
| 337 | / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, |
| 338 | getResources().getDisplayMetrics()); |
| 339 | private int mPagesLoaded; |
| 340 | |
| 341 | // If we haven't finished cleaning up the history, don't allow going back. |
| 342 | public boolean allowBack() { |
| 343 | return mPagesLoaded > 1; |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 348 | if (url.contains(mBrowserBailOutToken)) { |
| 349 | mLaunchBrowser = true; |
| 350 | done(false); |
| 351 | return; |
| 352 | } |
| 353 | // The first page load is used only to cause the WebView to |
| 354 | // fetch the proxy settings. Don't update the URL bar, and |
| 355 | // don't check if the captive portal is still there. |
| 356 | if (mPagesLoaded == 0) return; |
| 357 | // For internally generated pages, leave URL bar listing prior URL as this is the URL |
| 358 | // the page refers to. |
| 359 | if (!url.startsWith(INTERNAL_ASSETS)) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 360 | final TextView myUrlBar = findViewById(R.id.url_bar); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 361 | myUrlBar.setText(url); |
| 362 | } |
| 363 | if (mNetwork != null) { |
| 364 | testForCaptivePortal(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public void onPageFinished(WebView view, String url) { |
| 370 | mPagesLoaded++; |
| 371 | if (mPagesLoaded == 1) { |
| 372 | // Now that WebView has loaded at least one page we know it has read in the proxy |
| 373 | // settings. Now prompt the WebView read the Network-specific proxy settings. |
| 374 | setWebViewProxy(); |
| 375 | // Load the real page. |
| 376 | view.loadUrl(mUrl.toString()); |
| 377 | return; |
| 378 | } else if (mPagesLoaded == 2) { |
| 379 | // Prevent going back to empty first page. |
| 380 | view.clearHistory(); |
| 381 | } |
| 382 | if (mNetwork != null) { |
| 383 | testForCaptivePortal(); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // Convert Android device-independent-pixels (dp) to HTML size. |
| 388 | private String dp(int dp) { |
| 389 | // HTML px's are scaled just like dp's, so just add "px" suffix. |
| 390 | return Integer.toString(dp) + "px"; |
| 391 | } |
| 392 | |
| 393 | // Convert Android scaled-pixels (sp) to HTML size. |
| 394 | private String sp(int sp) { |
| 395 | // Convert sp to dp's. |
| 396 | float dp = sp * mDpPerSp; |
| 397 | // Apply a scale factor to make things look right. |
| 398 | dp *= 1.3; |
| 399 | // Convert dp's to HTML size. |
| 400 | return dp((int) dp); |
| 401 | } |
| 402 | |
| 403 | // A web page consisting of a large broken lock icon to indicate SSL failure. |
| 404 | private final String SSL_ERROR_HTML = "<html><head><style>" |
| 405 | + "body { margin-left:" + dp(48) + "; margin-right:" + dp(48) + "; " |
| 406 | + "margin-top:" + dp(96) + "; background-color:#fafafa; }" |
| 407 | + "img { width:" + dp(48) + "; height:" + dp(48) + "; }" |
| 408 | + "div.warn { font-size:" + sp(16) + "; margin-top:" + dp(16) + "; " |
| 409 | + " opacity:0.87; line-height:1.28; }" |
| 410 | + "div.example { font-size:" + sp(14) + "; margin-top:" + dp(16) + "; " |
| 411 | + " opacity:0.54; line-height:1.21905; }" |
| 412 | + "a { font-size:" + sp(14) + "; text-decoration:none; text-transform:uppercase; " |
| 413 | + " margin-top:" + dp(24) + "; display:inline-block; color:#4285F4; " |
| 414 | + " height:" + dp(48) + "; font-weight:bold; }" |
| 415 | + "</style></head><body><p><img src=quantum_ic_warning_amber_96.png><br>" |
| 416 | + "<div class=warn>%s</div>" |
| 417 | + "<div class=example>%s</div>" + "<a href=%s>%s</a></body></html>"; |
| 418 | |
| 419 | @Override |
| 420 | public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { |
| 421 | Log.w(TAG, "SSL error (error: " + error.getPrimaryError() + " host: " |
| 422 | // Only show host to avoid leaking private info. |
| 423 | + Uri.parse(error.getUrl()).getHost() + " certificate: " |
| 424 | + error.getCertificate() + "); displaying SSL warning."); |
| 425 | final String html = String.format(SSL_ERROR_HTML, getString(R.string.ssl_error_warning), |
| 426 | getString(R.string.ssl_error_example), mBrowserBailOutToken, |
| 427 | getString(R.string.ssl_error_continue)); |
| 428 | view.loadDataWithBaseURL(INTERNAL_ASSETS, html, "text/HTML", "UTF-8", null); |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 433 | if (url.startsWith("tel:")) { |
| 434 | startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); |
| 435 | return true; |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | private class MyWebChromeClient extends WebChromeClient { |
| 442 | @Override |
| 443 | public void onProgressChanged(WebView view, int newProgress) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 444 | final ProgressBar myProgressBar = findViewById(R.id.progress_bar); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 445 | myProgressBar.setProgress(newProgress); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | private void runOnUiThreadIfNotFinishing(Runnable r) { |
| 450 | if (!isFinishing()) { |
| 451 | runOnUiThread(r); |
| 452 | } |
| 453 | } |
| 454 | |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 455 | /** |
| 456 | * This alias presents the target activity, CaptivePortalLoginActivity, as a independent |
| 457 | * entity with its own intent filter to handle URL links. This alias will be enabled/disabled |
| 458 | * dynamically to handle url links based on the network conditions. |
| 459 | */ |
| 460 | public static String getAlias(Context context) { |
| 461 | try { |
| 462 | PackageInfo p = context.getPackageManager().getPackageInfo(context.getPackageName(), |
| 463 | PackageManager.GET_ACTIVITIES | PackageManager.MATCH_DISABLED_COMPONENTS); |
| 464 | for (ActivityInfo activityInfo : p.activities) { |
| 465 | String targetActivity = activityInfo.targetActivity; |
| 466 | if (CaptivePortalLoginActivity.class.getName().equals(targetActivity)) { |
| 467 | return activityInfo.name; |
| 468 | } |
| 469 | } |
| 470 | } catch (PackageManager.NameNotFoundException e) { |
| 471 | e.printStackTrace(); |
| 472 | } |
| 473 | return null; |
| 474 | } |
| 475 | |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 476 | private static void logd(String s) { |
| 477 | Rlog.d(TAG, s); |
| 478 | } |
| 479 | |
| 480 | private static void loge(String s) { |
| 481 | Rlog.d(TAG, s); |
| 482 | } |
| 483 | |
| 484 | } |