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