Home | History | Annotate | Download | only in htmlviewer
      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.Manifest;
     25 import android.net.Uri;
     26 import android.os.Bundle;
     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
     55     protected void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57 
     58         setContentView(R.layout.main);
     59 
     60         mWebView = (WebView) 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         requestPermissionAndLoad();
     82     }
     83 
     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     private void requestPermissionAndLoad() {
     92         Uri destination = mIntent.getData();
     93         if (destination != null) {
     94             // Is this a local file?
     95             if ("file".equals(destination.getScheme())
     96                         && PackageManager.PERMISSION_DENIED ==
     97                                 checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
     98                 requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
     99             } else {
    100                 loadUrl();
    101             }
    102         }
    103     }
    104 
    105     @Override
    106     public void onRequestPermissionsResult(int requestCode,
    107             String permissions[], int[] grantResults) {
    108         // We only ever request 1 permission, so these arguments should always have the same form.
    109         assert permissions.length == 1;
    110         assert Manifest.permission.READ_EXTERNAL_STORAGE.equals(permissions[0]);
    111 
    112         if (grantResults.length == 1 && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
    113             // Try again now that we have the permission.
    114             loadUrl();
    115         } else {
    116             Toast.makeText(HTMLViewerActivity.this,
    117                     R.string.turn_on_storage_permission, Toast.LENGTH_SHORT).show();
    118             finish();
    119         }
    120     }
    121 
    122     @Override
    123     protected void onDestroy() {
    124         super.onDestroy();
    125         mWebView.destroy();
    126     }
    127 
    128     private class ChromeClient extends WebChromeClient {
    129         @Override
    130         public void onReceivedTitle(WebView view, String title) {
    131             if (!getIntent().hasExtra(Intent.EXTRA_TITLE)) {
    132                 HTMLViewerActivity.this.setTitle(title);
    133             }
    134         }
    135     }
    136 
    137     private class ViewClient extends WebViewClient {
    138         @Override
    139         public void onPageFinished(WebView view, String url) {
    140             mLoading.setVisibility(View.GONE);
    141         }
    142 
    143         @Override
    144         public boolean shouldOverrideUrlLoading(WebView view, String url) {
    145             Intent intent;
    146             // Perform generic parsing of the URI to turn it into an Intent.
    147             try {
    148                 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
    149             } catch (URISyntaxException ex) {
    150                 Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage());
    151                 Toast.makeText(HTMLViewerActivity.this,
    152                         R.string.cannot_open_link, Toast.LENGTH_SHORT).show();
    153                 return true;
    154             }
    155             // Sanitize the Intent, ensuring web pages can not bypass browser
    156             // security (only access to BROWSABLE activities).
    157             intent.addCategory(Intent.CATEGORY_BROWSABLE);
    158             intent.setComponent(null);
    159             Intent selector = intent.getSelector();
    160             if (selector != null) {
    161                 selector.addCategory(Intent.CATEGORY_BROWSABLE);
    162                 selector.setComponent(null);
    163             }
    164 
    165             try {
    166                 view.getContext().startActivity(intent);
    167             } catch (ActivityNotFoundException ex) {
    168                 Log.w(TAG, "No application can handle " + url);
    169                 Toast.makeText(HTMLViewerActivity.this,
    170                         R.string.cannot_open_link, Toast.LENGTH_SHORT).show();
    171             }
    172             return true;
    173         }
    174 
    175         @Override
    176         public WebResourceResponse shouldInterceptRequest(WebView view,
    177                 WebResourceRequest request) {
    178             final Uri uri = request.getUrl();
    179             if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())
    180                     && uri.getPath().endsWith(".gz")) {
    181                 Log.d(TAG, "Trying to decompress " + uri + " on the fly");
    182                 try {
    183                     final InputStream in = new GZIPInputStream(
    184                             getContentResolver().openInputStream(uri));
    185                     final WebResourceResponse resp = new WebResourceResponse(
    186                             getIntent().getType(), "utf-8", in);
    187                     resp.setStatusCodeAndReasonPhrase(200, "OK");
    188                     return resp;
    189                 } catch (IOException e) {
    190                     Log.w(TAG, "Failed to decompress; falling back", e);
    191                 }
    192             }
    193             return null;
    194         }
    195     }
    196 }
    197