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.content.pm.PackageManager; 24 import android.net.Uri; 25 import android.os.Bundle; 26 import android.provider.Browser; 27 import android.util.Log; 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 loadUrl(); 82 } 83 loadUrl()84 private void loadUrl() { 85 if (mIntent.hasExtra(Intent.EXTRA_TITLE)) { 86 setTitle(mIntent.getStringExtra(Intent.EXTRA_TITLE)); 87 } 88 mWebView.loadUrl(String.valueOf(mIntent.getData())); 89 } 90 91 @Override onDestroy()92 protected void onDestroy() { 93 super.onDestroy(); 94 mWebView.destroy(); 95 } 96 97 private class ChromeClient extends WebChromeClient { 98 @Override onReceivedTitle(WebView view, String title)99 public void onReceivedTitle(WebView view, String title) { 100 if (!getIntent().hasExtra(Intent.EXTRA_TITLE)) { 101 HTMLViewerActivity.this.setTitle(title); 102 } 103 } 104 } 105 106 private class ViewClient extends WebViewClient { 107 @Override onPageFinished(WebView view, String url)108 public void onPageFinished(WebView view, String url) { 109 mLoading.setVisibility(View.GONE); 110 } 111 112 @Override shouldOverrideUrlLoading(WebView view, WebResourceRequest request)113 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 114 String url = request.getUrl().toString(); 115 Intent intent; 116 // Perform generic parsing of the URI to turn it into an Intent. 117 try { 118 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); 119 } catch (URISyntaxException ex) { 120 Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage()); 121 Toast.makeText(HTMLViewerActivity.this, 122 R.string.cannot_open_link, Toast.LENGTH_SHORT).show(); 123 return true; 124 } 125 // Sanitize the Intent, ensuring web pages can not bypass browser 126 // security (only access to BROWSABLE activities). 127 intent.addCategory(Intent.CATEGORY_BROWSABLE); 128 intent.setComponent(null); 129 Intent selector = intent.getSelector(); 130 if (selector != null) { 131 selector.addCategory(Intent.CATEGORY_BROWSABLE); 132 selector.setComponent(null); 133 } 134 // Pass the package name as application ID so that the intent from the 135 // same application can be opened in the same tab. 136 intent.putExtra(Browser.EXTRA_APPLICATION_ID, 137 view.getContext().getPackageName()); 138 try { 139 view.getContext().startActivity(intent); 140 } catch (ActivityNotFoundException | SecurityException ex) { 141 Log.w(TAG, "No application can handle " + url); 142 Toast.makeText(HTMLViewerActivity.this, 143 R.string.cannot_open_link, Toast.LENGTH_SHORT).show(); 144 } 145 return true; 146 } 147 148 @Override shouldInterceptRequest(WebView view, WebResourceRequest request)149 public WebResourceResponse shouldInterceptRequest(WebView view, 150 WebResourceRequest request) { 151 final Uri uri = request.getUrl(); 152 if (ContentResolver.SCHEME_FILE.equals(uri.getScheme()) 153 && uri.getPath().endsWith(".gz")) { 154 Log.d(TAG, "Trying to decompress " + uri + " on the fly"); 155 try { 156 final InputStream in = new GZIPInputStream( 157 getContentResolver().openInputStream(uri)); 158 final WebResourceResponse resp = new WebResourceResponse( 159 getIntent().getType(), "utf-8", in); 160 resp.setStatusCodeAndReasonPhrase(200, "OK"); 161 return resp; 162 } catch (IOException e) { 163 Log.w(TAG, "Failed to decompress; falling back", e); 164 } 165 } 166 return null; 167 } 168 } 169 } 170