• 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.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
onCreate(Bundle savedInstanceState)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.setUseWideViewPort(true);
85         s.setSupportZoom(true);
86         s.setBuiltInZoomControls(true);
87         s.setSavePassword(false);
88         s.setSaveFormData(false);
89         s.setBlockNetworkLoads(true);
90 
91         // Javascript is purposely disabled, so that nothing can be
92         // automatically run.
93         s.setJavaScriptEnabled(false);
94 
95         // Restore a webview if we are meant to restore
96         if (savedInstanceState != null) {
97             mWebView.restoreState(savedInstanceState);
98         } else {
99             // Check the intent for the content to view
100             Intent intent = getIntent();
101             if (intent.getData() != null) {
102                 Uri uri = intent.getData();
103                 String contentUri = "file".equals(uri.getScheme())
104                         ? FileContentProvider.BASE_URI + uri.getEncodedPath()
105                         : uri.toString();
106                 mWebView.loadUrl(contentUri);
107             }
108         }
109     }
110 
111     @Override
onResume()112     protected void onResume() {
113         super.onResume();
114         CookieSyncManager.getInstance().startSync();
115     }
116 
117     @Override
onSaveInstanceState(Bundle outState)118     protected void onSaveInstanceState(Bundle outState) {
119         // the default implementation requires each view to have an id. As the
120         // browser handles the state itself and it doesn't use id for the views,
121         // don't call the default implementation. Otherwise it will trigger the
122         // warning like this, "couldn't save which view has focus because the
123         // focused view XXX has no id".
124         mWebView.saveState(outState);
125     }
126 
127     @Override
onStop()128     protected void onStop() {
129         super.onStop();
130 
131         CookieSyncManager.getInstance().stopSync();
132     }
133 
134     @Override
onDestroy()135     protected void onDestroy() {
136         super.onDestroy();
137         mWebView.destroy();
138     }
139 
140     class WebChrome extends WebChromeClient {
141 
142         @Override
onReceivedTitle(WebView view, String title)143         public void onReceivedTitle(WebView view, String title) {
144             HTMLViewerActivity.this.setTitle(title);
145         }
146 
147         @Override
onProgressChanged(WebView view, int newProgress)148         public void onProgressChanged(WebView view, int newProgress) {
149             getWindow().setFeatureInt(
150                     Window.FEATURE_PROGRESS, newProgress*100);
151             if (newProgress == 100) {
152                 CookieSyncManager.getInstance().sync();
153             }
154         }
155     }
156 
157 }
158