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