1 /* 2 * Copyright (C) 2008 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.htmlviewer; 18 19 import android.app.Activity; 20 import android.content.ActivityNotFoundException; 21 import android.content.ContentResolver; 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.provider.Browser; 26 import android.util.Log; 27 import android.view.MenuItem; 28 import android.view.View; 29 import android.webkit.WebChromeClient; 30 import android.webkit.WebResourceRequest; 31 import android.webkit.WebResourceResponse; 32 import android.webkit.WebSettings; 33 import android.webkit.WebView; 34 import android.webkit.WebViewClient; 35 import android.widget.Toast; 36 37 import androidx.core.graphics.Insets; 38 import androidx.core.view.ViewCompat; 39 import androidx.core.view.WindowCompat; 40 import androidx.core.view.WindowInsetsCompat; 41 42 import java.io.IOException; 43 import java.io.InputStream; 44 import java.net.URISyntaxException; 45 import java.util.zip.GZIPInputStream; 46 47 /** 48 * Simple activity that shows the requested HTML page. This utility is 49 * purposefully very limited in what it supports, including no network or 50 * JavaScript. 51 */ 52 public class HTMLViewerActivity extends Activity { 53 private static final String TAG = "HTMLViewer"; 54 55 private WebView mWebView; 56 private View mLoading; 57 private Intent mIntent; 58 59 @Override onCreate(Bundle savedInstanceState)60 protected void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 63 setContentView(); 64 65 mWebView = findViewById(R.id.webview); 66 mLoading = findViewById(R.id.loading); 67 68 mWebView.setWebChromeClient(new ChromeClient()); 69 mWebView.setWebViewClient(new ViewClient()); 70 71 WebSettings s = mWebView.getSettings(); 72 s.setUseWideViewPort(true); 73 s.setSupportZoom(true); 74 s.setBuiltInZoomControls(true); 75 s.setDisplayZoomControls(false); 76 s.setSavePassword(false); 77 s.setSaveFormData(false); 78 s.setBlockNetworkLoads(true); 79 s.setAllowFileAccess(true); 80 81 // Javascript is purposely disabled, so that nothing can be 82 // automatically run. 83 s.setJavaScriptEnabled(false); 84 s.setDefaultTextEncodingName("utf-8"); 85 86 mIntent = getIntent(); 87 setBackButton(); 88 loadUrl(); 89 } 90 setContentView()91 protected void setContentView() { 92 setupEdgeToEdge(); 93 setContentView(R.layout.main); 94 } 95 loadUrl()96 private void loadUrl() { 97 if (mIntent.hasExtra(Intent.EXTRA_TITLE)) { 98 setTitle(mIntent.getStringExtra(Intent.EXTRA_TITLE)); 99 } 100 mWebView.loadUrl(String.valueOf(mIntent.getData())); 101 } 102 setBackButton()103 private void setBackButton() { 104 if (getActionBar() != null) { 105 getActionBar().setDisplayHomeAsUpEnabled(true); 106 } 107 } 108 109 @Override onOptionsItemSelected(MenuItem item)110 public boolean onOptionsItemSelected(MenuItem item) { 111 if (item.getItemId() == android.R.id.home) { 112 finish(); 113 return true; 114 } 115 return super.onOptionsItemSelected(item); 116 } 117 118 @Override onDestroy()119 protected void onDestroy() { 120 super.onDestroy(); 121 mWebView.destroy(); 122 } 123 124 private class ChromeClient extends WebChromeClient { 125 @Override onReceivedTitle(WebView view, String title)126 public void onReceivedTitle(WebView view, String title) { 127 if (!getIntent().hasExtra(Intent.EXTRA_TITLE)) { 128 HTMLViewerActivity.this.setTitle(title); 129 } 130 } 131 } 132 133 private class ViewClient extends WebViewClient { 134 @Override onPageFinished(WebView view, String url)135 public void onPageFinished(WebView view, String url) { 136 mLoading.setVisibility(View.GONE); 137 } 138 139 @Override shouldOverrideUrlLoading(WebView view, WebResourceRequest request)140 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 141 String url = request.getUrl().toString(); 142 Intent intent; 143 // Perform generic parsing of the URI to turn it into an Intent. 144 try { 145 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); 146 } catch (URISyntaxException ex) { 147 Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage()); 148 Toast.makeText(HTMLViewerActivity.this, 149 R.string.cannot_open_link, Toast.LENGTH_SHORT).show(); 150 return true; 151 } 152 // Sanitize the Intent, ensuring web pages can not bypass browser 153 // security (only access to BROWSABLE activities). 154 intent.addCategory(Intent.CATEGORY_BROWSABLE); 155 intent.setComponent(null); 156 Intent selector = intent.getSelector(); 157 if (selector != null) { 158 selector.addCategory(Intent.CATEGORY_BROWSABLE); 159 selector.setComponent(null); 160 } 161 // Pass the package name as application ID so that the intent from the 162 // same application can be opened in the same tab. 163 intent.putExtra(Browser.EXTRA_APPLICATION_ID, 164 view.getContext().getPackageName()); 165 try { 166 view.getContext().startActivity(intent); 167 } catch (ActivityNotFoundException | SecurityException ex) { 168 Log.w(TAG, "No application can handle " + url); 169 Toast.makeText(HTMLViewerActivity.this, 170 R.string.cannot_open_link, Toast.LENGTH_SHORT).show(); 171 } 172 return true; 173 } 174 175 @Override shouldInterceptRequest(WebView view, WebResourceRequest request)176 public WebResourceResponse shouldInterceptRequest(WebView view, 177 WebResourceRequest request) { 178 final Uri uri = request.getUrl(); 179 if (ContentResolver.SCHEME_FILE.equals(uri.getScheme()) 180 && uri.getPath().endsWith(".gz")) { 181 Log.d(TAG, "Trying to decompress " + uri + " on the fly"); 182 try { 183 final InputStream in = new GZIPInputStream( 184 getContentResolver().openInputStream(uri)); 185 final WebResourceResponse resp = new WebResourceResponse( 186 getIntent().getType(), "utf-8", in); 187 resp.setStatusCodeAndReasonPhrase(200, "OK"); 188 return resp; 189 } catch (IOException e) { 190 Log.w(TAG, "Failed to decompress; falling back", e); 191 } 192 } 193 return null; 194 } 195 } 196 setupEdgeToEdge()197 private void setupEdgeToEdge() { 198 // Shamelessly copied from SettingsHomepageActivity 199 // https://cs.android.com/android/platform/superproject/main/+/main:packages/apps/Settings/src/com/android/settings/homepage/SettingsHomepageActivity.java;l=355;drc=6b4b7336bc0bdbbdbd144429b8dfb006503d6a7b 200 WindowCompat.setDecorFitsSystemWindows(getWindow(), false); 201 ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content), 202 (v, windowInsets) -> { 203 Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()); 204 // Apply the insets paddings to the view. 205 v.setPadding(insets.left, insets.top, insets.right, insets.bottom); 206 207 // Return CONSUMED if you don't want the window insets to keep being 208 // passed down to descendant views. 209 return WindowInsetsCompat.CONSUMED; 210 }); 211 } 212 } 213