• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ContentResolver;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.View;
26 import android.webkit.WebChromeClient;
27 import android.webkit.WebResourceRequest;
28 import android.webkit.WebResourceResponse;
29 import android.webkit.WebSettings;
30 import android.webkit.WebView;
31 import android.webkit.WebViewClient;
32 
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.zip.GZIPInputStream;
36 
37 /**
38  * Simple activity that shows the requested HTML page. This utility is
39  * purposefully very limited in what it supports, including no network or
40  * JavaScript.
41  */
42 public class HTMLViewerActivity extends Activity {
43     private static final String TAG = "HTMLViewer";
44 
45     private WebView mWebView;
46     private View mLoading;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51 
52         setContentView(R.layout.main);
53 
54         mWebView = (WebView) findViewById(R.id.webview);
55         mLoading = findViewById(R.id.loading);
56 
57         mWebView.setWebChromeClient(new ChromeClient());
58         mWebView.setWebViewClient(new ViewClient());
59 
60         WebSettings s = mWebView.getSettings();
61         s.setUseWideViewPort(true);
62         s.setSupportZoom(true);
63         s.setBuiltInZoomControls(true);
64         s.setDisplayZoomControls(false);
65         s.setSavePassword(false);
66         s.setSaveFormData(false);
67         s.setBlockNetworkLoads(true);
68 
69         // Javascript is purposely disabled, so that nothing can be
70         // automatically run.
71         s.setJavaScriptEnabled(false);
72         s.setDefaultTextEncodingName("utf-8");
73 
74         final Intent intent = getIntent();
75         if (intent.hasExtra(Intent.EXTRA_TITLE)) {
76             setTitle(intent.getStringExtra(Intent.EXTRA_TITLE));
77         }
78 
79         mWebView.loadUrl(String.valueOf(intent.getData()));
80     }
81 
82     @Override
onDestroy()83     protected void onDestroy() {
84         super.onDestroy();
85         mWebView.destroy();
86     }
87 
88     private class ChromeClient extends WebChromeClient {
89         @Override
onReceivedTitle(WebView view, String title)90         public void onReceivedTitle(WebView view, String title) {
91             if (!getIntent().hasExtra(Intent.EXTRA_TITLE)) {
92                 HTMLViewerActivity.this.setTitle(title);
93             }
94         }
95     }
96 
97     private class ViewClient extends WebViewClient {
98         @Override
onPageFinished(WebView view, String url)99         public void onPageFinished(WebView view, String url) {
100             mLoading.setVisibility(View.GONE);
101         }
102 
103         @Override
shouldInterceptRequest(WebView view, WebResourceRequest request)104         public WebResourceResponse shouldInterceptRequest(WebView view,
105                 WebResourceRequest request) {
106             final Uri uri = request.getUrl();
107             if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())
108                     && uri.getPath().endsWith(".gz")) {
109                 Log.d(TAG, "Trying to decompress " + uri + " on the fly");
110                 try {
111                     final InputStream in = new GZIPInputStream(
112                             getContentResolver().openInputStream(uri));
113                     final WebResourceResponse resp = new WebResourceResponse(
114                             getIntent().getType(), "utf-8", in);
115                     resp.setStatusCodeAndReasonPhrase(200, "OK");
116                     return resp;
117                 } catch (IOException e) {
118                     Log.w(TAG, "Failed to decompress; falling back", e);
119                 }
120             }
121             return null;
122         }
123     }
124 }
125