HTMLViewerActivity.java revision 931fd3728855b03bcab825ce10475d7447e9097a
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 17package com.android.htmlviewer; 18 19import android.app.Activity; 20import android.content.Intent; 21import android.net.Uri; 22import android.os.Bundle; 23import android.util.Log; 24import android.view.Window; 25import android.webkit.CookieSyncManager; 26import android.webkit.WebChromeClient; 27import android.webkit.WebSettings; 28import android.webkit.WebView; 29 30import java.io.File; 31import java.io.FileInputStream; 32import java.io.FileNotFoundException; 33import java.io.IOException; 34import 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 */ 47public 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 if ("file".equals(uri.getScheme())) { 103 String contentUri = 104 FileContentProvider.BASE_URI + 105 uri.getEncodedPath() + 106 "?" + 107 intent.getType(); 108 mWebView.loadUrl(contentUri); 109 } else { 110 mWebView.loadUrl(intent.getData().toString() + 111 "?" + intent.getType()); 112 } 113 } 114 } 115 } 116 117 @Override 118 protected void onResume() { 119 super.onResume(); 120 CookieSyncManager.getInstance().startSync(); 121 } 122 123 @Override 124 protected void onSaveInstanceState(Bundle outState) { 125 // the default implementation requires each view to have an id. As the 126 // browser handles the state itself and it doesn't use id for the views, 127 // don't call the default implementation. Otherwise it will trigger the 128 // warning like this, "couldn't save which view has focus because the 129 // focused view XXX has no id". 130 mWebView.saveState(outState); 131 } 132 133 @Override 134 protected void onStop() { 135 super.onStop(); 136 137 CookieSyncManager.getInstance().stopSync(); 138 mWebView.stopLoading(); 139 } 140 141 @Override 142 protected void onDestroy() { 143 super.onDestroy(); 144 mWebView.destroy(); 145 } 146 147 class WebChrome extends WebChromeClient { 148 149 @Override 150 public void onReceivedTitle(WebView view, String title) { 151 HTMLViewerActivity.this.setTitle(title); 152 } 153 154 @Override 155 public void onProgressChanged(WebView view, int newProgress) { 156 getWindow().setFeatureInt( 157 Window.FEATURE_PROGRESS, newProgress*100); 158 if (newProgress == 100) { 159 CookieSyncManager.getInstance().sync(); 160 } 161 } 162 } 163 164} 165