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.Intent;
     21 import android.net.Uri;
     22 import android.os.Bundle;
     23 import android.util.Log;
     24 import android.view.Window;
     25 import android.webkit.CookieSyncManager;
     26 import android.webkit.WebChromeClient;
     27 import android.webkit.WebSettings;
     28 import android.webkit.WebView;
     29 
     30 import java.io.File;
     31 import java.io.FileInputStream;
     32 import java.io.FileNotFoundException;
     33 import java.io.IOException;
     34 import java.io.InputStream;
     35 
     36 /**
     37  * Wraps a WebView widget within an Activity. When launched, it uses the
     38  * URI from the intent as the URL to load into the WebView.
     39  * It supports all URLs schemes that a standard WebView supports, as well as
     40  * loading the top level markup using the file scheme.
     41  * The WebView default settings are used with the exception of normal layout
     42  * is set.
     43  * This activity shows a loading progress bar in the window title and sets
     44  * the window title to the title of the content.
     45  *
     46  */
     47 public class HTMLViewerActivity extends Activity {
     48 
     49     /*
     50      * The WebView that is placed in this Activity
     51      */
     52     private WebView mWebView;
     53 
     54     /*
     55      * As the file content is loaded completely into RAM first, set
     56      * a limitation on the file size so we don't use too much RAM. If someone
     57      * wants to load content that is larger than this, then a content
     58      * provider should be used.
     59      */
     60     static final int MAXFILESIZE = 8096;
     61 
     62     static final String LOGTAG = "HTMLViewerActivity";
     63 
     64     @Override
     65     protected void onCreate(Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67 
     68         // Call createInstance() explicitly. createInstance() is called in
     69         // BrowserFrame by WebView. As it is called in WebCore thread, it can
     70         // happen after onResume() is called. To use getInstance() in onResume,
     71         // createInstance() needs to be called first.
     72         CookieSyncManager.createInstance(this);
     73 
     74         requestWindowFeature(Window.FEATURE_PROGRESS);
     75 
     76         mWebView = new WebView(this);
     77         setContentView(mWebView);
     78 
     79         // Setup callback support for title and progress bar
     80         mWebView.setWebChromeClient( new WebChrome() );
     81 
     82         // Configure the webview
     83         WebSettings s = mWebView.getSettings();
     84         s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
     85         s.setUseWideViewPort(true);
     86         s.setSavePassword(false);
     87         s.setSaveFormData(false);
     88         s.setBlockNetworkLoads(true);
     89 
     90         // Javascript is purposely disabled, so that nothing can be
     91         // automatically run.
     92         s.setJavaScriptEnabled(false);
     93 
     94         // Restore a webview if we are meant to restore
     95         if (savedInstanceState != null) {
     96             mWebView.restoreState(savedInstanceState);
     97         } else {
     98             // Check the intent for the content to view
     99             Intent intent = getIntent();
    100             if (intent.getData() != null) {
    101                 Uri uri = intent.getData();
    102                 String contentUri = "file".equals(uri.getScheme())
    103                         ? FileContentProvider.BASE_URI + uri.getEncodedPath()
    104                         : uri.toString();
    105                 String intentType = intent.getType();
    106                 if (intentType != null) {
    107                     contentUri += "?" + intentType;
    108                 }
    109                 mWebView.loadUrl(contentUri);
    110             }
    111         }
    112     }
    113 
    114     @Override
    115     protected void onResume() {
    116         super.onResume();
    117         CookieSyncManager.getInstance().startSync();
    118     }
    119 
    120     @Override
    121     protected void onSaveInstanceState(Bundle outState) {
    122         // the default implementation requires each view to have an id. As the
    123         // browser handles the state itself and it doesn't use id for the views,
    124         // don't call the default implementation. Otherwise it will trigger the
    125         // warning like this, "couldn't save which view has focus because the
    126         // focused view XXX has no id".
    127         mWebView.saveState(outState);
    128     }
    129 
    130     @Override
    131     protected void onStop() {
    132         super.onStop();
    133 
    134         CookieSyncManager.getInstance().stopSync();
    135         mWebView.stopLoading();
    136     }
    137 
    138     @Override
    139     protected void onDestroy() {
    140         super.onDestroy();
    141         mWebView.destroy();
    142     }
    143 
    144     class WebChrome extends WebChromeClient {
    145 
    146         @Override
    147         public void onReceivedTitle(WebView view, String title) {
    148             HTMLViewerActivity.this.setTitle(title);
    149         }
    150 
    151         @Override
    152         public void onProgressChanged(WebView view, int newProgress) {
    153             getWindow().setFeatureInt(
    154                     Window.FEATURE_PROGRESS, newProgress*100);
    155             if (newProgress == 100) {
    156                 CookieSyncManager.getInstance().sync();
    157             }
    158         }
    159     }
    160 
    161 }
    162