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 java.io.IOException; 38 import java.io.InputStream; 39 import java.net.URISyntaxException; 40 import java.util.zip.GZIPInputStream; 41 42 /** 43 * Simple activity that shows the requested HTML page. This utility is 44 * purposefully very limited in what it supports, including no network or 45 * JavaScript. 46 */ 47 public class HTMLViewerActivity extends Activity { 48 private static final String TAG = "HTMLViewer"; 49 50 private WebView mWebView; 51 private View mLoading; 52 private Intent mIntent; 53 54 @Override onCreate(Bundle savedInstanceState)55 protected void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 58 setContentView(R.layout.main); 59 60 mWebView = findViewById(R.id.webview); 61 mLoading = findViewById(R.id.loading); 62 63 mWebView.setWebChromeClient(new ChromeClient()); 64 mWebView.setWebViewClient(new ViewClient()); 65 66 WebSettings s = mWebView.getSettings(); 67 s.setUseWideViewPort(true); 68 s.setSupportZoom(true); 69 s.setBuiltInZoomControls(true); 70 s.setDisplayZoomControls(false); 71 s.setSavePassword(false); 72 s.setSaveFormData(false); 73 s.setBlockNetworkLoads(true); 74 75 // Javascript is purposely disabled, so that nothing can be 76 // automatically run. 77 s.setJavaScriptEnabled(false); 78 s.setDefaultTextEncodingName("utf-8"); 79 80 mIntent = getIntent(); 81 setBackButton(); 82 loadUrl(); 83 } 84 loadUrl()85 private void loadUrl() { 86 if (mIntent.hasExtra(Intent.EXTRA_TITLE)) { 87 setTitle(mIntent.getStringExtra(Intent.EXTRA_TITLE)); 88 } 89 mWebView.loadUrl(String.valueOf(mIntent.getData())); 90 } 91 setBackButton()92 private void setBackButton() { 93 if (getActionBar() != null) { 94 getActionBar().setDisplayHomeAsUpEnabled(true); 95 } 96 } 97 98 @Override onOptionsItemSelected(MenuItem item)99 public boolean onOptionsItemSelected(MenuItem item) { 100 if (item.getItemId() == android.R.id.home) { 101 finish(); 102 return true; 103 } 104 return super.onOptionsItemSelected(item); 105 } 106 107 @Override onDestroy()108 protected void onDestroy() { 109 super.onDestroy(); 110 mWebView.destroy(); 111 } 112 113 private class ChromeClient extends WebChromeClient { 114 @Override onReceivedTitle(WebView view, String title)115 public void onReceivedTitle(WebView view, String title) { 116 if (!getIntent().hasExtra(Intent.EXTRA_TITLE)) { 117 HTMLViewerActivity.this.setTitle(title); 118 } 119 } 120 } 121 122 private class ViewClient extends WebViewClient { 123 @Override onPageFinished(WebView view, String url)124 public void onPageFinished(WebView view, String url) { 125 mLoading.setVisibility(View.GONE); 126 } 127 128 @Override shouldOverrideUrlLoading(WebView view, WebResourceRequest request)129 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 130 String url = request.getUrl().toString(); 131 Intent intent; 132 // Perform generic parsing of the URI to turn it into an Intent. 133 try { 134 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); 135 } catch (URISyntaxException ex) { 136 Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage()); 137 Toast.makeText(HTMLViewerActivity.this, 138 R.string.cannot_open_link, Toast.LENGTH_SHORT).show(); 139 return true; 140 } 141 // Sanitize the Intent, ensuring web pages can not bypass browser 142 // security (only access to BROWSABLE activities). 143 intent.addCategory(Intent.CATEGORY_BROWSABLE); 144 intent.setComponent(null); 145 Intent selector = intent.getSelector(); 146 if (selector != null) { 147 selector.addCategory(Intent.CATEGORY_BROWSABLE); 148 selector.setComponent(null); 149 } 150 // Pass the package name as application ID so that the intent from the 151 // same application can be opened in the same tab. 152 intent.putExtra(Browser.EXTRA_APPLICATION_ID, 153 view.getContext().getPackageName()); 154 try { 155 view.getContext().startActivity(intent); 156 } catch (ActivityNotFoundException | SecurityException ex) { 157 Log.w(TAG, "No application can handle " + url); 158 Toast.makeText(HTMLViewerActivity.this, 159 R.string.cannot_open_link, Toast.LENGTH_SHORT).show(); 160 } 161 return true; 162 } 163 164 @Override shouldInterceptRequest(WebView view, WebResourceRequest request)165 public WebResourceResponse shouldInterceptRequest(WebView view, 166 WebResourceRequest request) { 167 final Uri uri = request.getUrl(); 168 if (ContentResolver.SCHEME_FILE.equals(uri.getScheme()) 169 && uri.getPath().endsWith(".gz")) { 170 Log.d(TAG, "Trying to decompress " + uri + " on the fly"); 171 try { 172 final InputStream in = new GZIPInputStream( 173 getContentResolver().openInputStream(uri)); 174 final WebResourceResponse resp = new WebResourceResponse( 175 getIntent().getType(), "utf-8", in); 176 resp.setStatusCodeAndReasonPhrase(200, "OK"); 177 return resp; 178 } catch (IOException e) { 179 Log.w(TAG, "Failed to decompress; falling back", e); 180 } 181 } 182 return null; 183 } 184 } 185 } 186