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 // 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 String intentType = intent.getType(); 107 if (intentType != null) { 108 contentUri += "?" + intentType; 109 } 110 mWebView.loadUrl(contentUri); 111 } 112 } 113 } 114 115 @Override 116 protected void onResume() { 117 super.onResume(); 118 CookieSyncManager.getInstance().startSync(); 119 } 120 121 @Override 122 protected void onSaveInstanceState(Bundle outState) { 123 // the default implementation requires each view to have an id. As the 124 // browser handles the state itself and it doesn't use id for the views, 125 // don't call the default implementation. Otherwise it will trigger the 126 // warning like this, "couldn't save which view has focus because the 127 // focused view XXX has no id". 128 mWebView.saveState(outState); 129 } 130 131 @Override 132 protected void onStop() { 133 super.onStop(); 134 135 CookieSyncManager.getInstance().stopSync(); 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