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.provider.Browser;
     28 import android.util.Log;
     29 import android.view.View;
     30 import android.webkit.WebChromeClient;
     31 import android.webkit.WebResourceRequest;
     32 import android.webkit.WebResourceResponse;
     33 import android.webkit.WebSettings;
     34 import android.webkit.WebView;
     35 import android.webkit.WebViewClient;
     36 import android.widget.Toast;
     37 
     38 import java.io.IOException;
     39 import java.io.InputStream;
     40 import java.net.URISyntaxException;
     41 import java.util.zip.GZIPInputStream;
     42 
     43 /**
     44  * Simple activity that shows the requested HTML page. This utility is
     45  * purposefully very limited in what it supports, including no network or
     46  * JavaScript.
     47  */
     48 public class HTMLViewerActivity extends Activity {
     49     private static final String TAG = "HTMLViewer";
     50 
     51     private WebView mWebView;
     52     private View mLoading;
     53     private Intent mIntent;
     54 
     55     @Override
     56     protected void onCreate(Bundle savedInstanceState) {
     57         super.onCreate(savedInstanceState);
     58 
     59         setContentView(R.layout.main);
     60 
     61         mWebView = (WebView) findViewById(R.id.webview);
     62         mLoading = findViewById(R.id.loading);
     63 
     64         mWebView.setWebChromeClient(new ChromeClient());
     65         mWebView.setWebViewClient(new ViewClient());
     66 
     67         WebSettings s = mWebView.getSettings();
     68         s.setUseWideViewPort(true);
     69         s.setSupportZoom(true);
     70         s.setBuiltInZoomControls(true);
     71         s.setDisplayZoomControls(false);
     72         s.setSavePassword(false);
     73         s.setSaveFormData(false);
     74         s.setBlockNetworkLoads(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         requestPermissionAndLoad();
     83     }
     84 
     85     private void loadUrl() {
     86         if (mIntent.hasExtra(Intent.EXTRA_TITLE)) {
     87             setTitle(mIntent.getStringExtra(Intent.EXTRA_TITLE));
     88         }
     89         mWebView.loadUrl(String.valueOf(mIntent.getData()));
     90     }
     91 
     92     private void requestPermissionAndLoad() {
     93         Uri destination = mIntent.getData();
     94         if (destination != null) {
     95             // Is this a local file?
     96             if ("file".equals(destination.getScheme())
     97                         && PackageManager.PERMISSION_DENIED ==
     98                                 checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
     99                 requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
    100             } else {
    101                 loadUrl();
    102             }
    103         }
    104     }
    105 
    106     @Override
    107     public void onRequestPermissionsResult(int requestCode,
    108             String permissions[], int[] grantResults) {
    109         // We only ever request 1 permission, so these arguments should always have the same form.
    110         assert permissions.length == 1;
    111         assert Manifest.permission.READ_EXTERNAL_STORAGE.equals(permissions[0]);
    112 
    113         if (grantResults.length == 1 && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
    114             // Try again now that we have the permission.
    115             loadUrl();
    116         } else {
    117             Toast.makeText(HTMLViewerActivity.this,
    118                     R.string.turn_on_storage_permission, Toast.LENGTH_SHORT).show();
    119             finish();
    120         }
    121     }
    122 
    123     @Override
    124     protected void onDestroy() {
    125         super.onDestroy();
    126         mWebView.destroy();
    127     }
    128 
    129     private class ChromeClient extends WebChromeClient {
    130         @Override
    131         public void onReceivedTitle(WebView view, String title) {
    132             if (!getIntent().hasExtra(Intent.EXTRA_TITLE)) {
    133                 HTMLViewerActivity.this.setTitle(title);
    134             }
    135         }
    136     }
    137 
    138     private class ViewClient extends WebViewClient {
    139         @Override
    140         public void onPageFinished(WebView view, String url) {
    141             mLoading.setVisibility(View.GONE);
    142         }
    143 
    144         @Override
    145         public boolean shouldOverrideUrlLoading(WebView view, String url) {
    146             Intent intent;
    147             // Perform generic parsing of the URI to turn it into an Intent.
    148             try {
    149                 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
    150             } catch (URISyntaxException ex) {
    151                 Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage());
    152                 Toast.makeText(HTMLViewerActivity.this,
    153                         R.string.cannot_open_link, Toast.LENGTH_SHORT).show();
    154                 return true;
    155             }
    156             // Sanitize the Intent, ensuring web pages can not bypass browser
    157             // security (only access to BROWSABLE activities).
    158             intent.addCategory(Intent.CATEGORY_BROWSABLE);
    159             intent.setComponent(null);
    160             Intent selector = intent.getSelector();
    161             if (selector != null) {
    162                 selector.addCategory(Intent.CATEGORY_BROWSABLE);
    163                 selector.setComponent(null);
    164             }
    165             // Pass the package name as application ID so that the intent from the
    166             // same application can be opened in the same tab.
    167             intent.putExtra(Browser.EXTRA_APPLICATION_ID,
    168                             view.getContext().getPackageName());
    169 
    170             try {
    171                 view.getContext().startActivity(intent);
    172             } catch (ActivityNotFoundException ex) {
    173                 Log.w(TAG, "No application can handle " + url);
    174                 Toast.makeText(HTMLViewerActivity.this,
    175                         R.string.cannot_open_link, Toast.LENGTH_SHORT).show();
    176             }
    177             return true;
    178         }
    179 
    180         @Override
    181         public WebResourceResponse shouldInterceptRequest(WebView view,
    182                 WebResourceRequest request) {
    183             final Uri uri = request.getUrl();
    184             if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())
    185                     && uri.getPath().endsWith(".gz")) {
    186                 Log.d(TAG, "Trying to decompress " + uri + " on the fly");
    187                 try {
    188                     final InputStream in = new GZIPInputStream(
    189                             getContentResolver().openInputStream(uri));
    190                     final WebResourceResponse resp = new WebResourceResponse(
    191                             getIntent().getType(), "utf-8", in);
    192                     resp.setStatusCodeAndReasonPhrase(200, "OK");
    193                     return resp;
    194                 } catch (IOException e) {
    195                     Log.w(TAG, "Failed to decompress; falling back", e);
    196                 }
    197             }
    198             return null;
    199         }
    200     }
    201 }
    202