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