diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:32:24 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:32:24 -0800 |
commit | 931fd3728855b03bcab825ce10475d7447e9097a (patch) | |
tree | 902c3db239f91bdfc3e26c80a43d87220b3695bf /src | |
parent | 0664dd747f36abcc122a3d3e7c6ab1472681f510 (diff) | |
download | android_packages_apps_HTMLViewer-931fd3728855b03bcab825ce10475d7447e9097a.tar.gz android_packages_apps_HTMLViewer-931fd3728855b03bcab825ce10475d7447e9097a.tar.bz2 android_packages_apps_HTMLViewer-931fd3728855b03bcab825ce10475d7447e9097a.zip |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/htmlviewer/FileContentProvider.java | 85 | ||||
-rw-r--r-- | src/com/android/htmlviewer/HTMLViewerActivity.java | 164 |
2 files changed, 249 insertions, 0 deletions
diff --git a/src/com/android/htmlviewer/FileContentProvider.java b/src/com/android/htmlviewer/FileContentProvider.java new file mode 100644 index 0000000..1344819 --- /dev/null +++ b/src/com/android/htmlviewer/FileContentProvider.java @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.android.htmlviewer; + +import java.io.FileNotFoundException; +import java.io.File; +import java.lang.UnsupportedOperationException; + +import android.content.ContentProvider; +import android.content.ContentValues; +import android.database.Cursor; +import android.net.Uri; +import android.os.ParcelFileDescriptor; + +/** + * WebView does not support file: loading. This class wraps a file load + * with a content provider. + * As HTMLViewer does not have internet access nor does it allow + * Javascript to be run, it is safe to load file based HTML content. +*/ +public class FileContentProvider extends ContentProvider { + + public static final String BASE_URI = + "content://com.android.htmlfileprovider"; + public static final int BASE_URI_LEN = BASE_URI.length(); + + @Override + public String getType(Uri uri) { + // If the mimetype is not appended to the uri, then return an empty string + String mimetype = uri.getQuery(); + return mimetype == null ? "" : mimetype; + } + + @Override + public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { + if (!"r".equals(mode)) { + throw new FileNotFoundException("Bad mode for " + uri + ": " + mode); + } + String filename = uri.toString().substring(BASE_URI_LEN); + return ParcelFileDescriptor.open(new File(filename), + ParcelFileDescriptor.MODE_READ_ONLY); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean onCreate() { + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, + String[] selectionArgs, String sortOrder) { + throw new UnsupportedOperationException(); + } + + @Override + public int update(Uri uri, ContentValues values, String selection, + String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + +} diff --git a/src/com/android/htmlviewer/HTMLViewerActivity.java b/src/com/android/htmlviewer/HTMLViewerActivity.java new file mode 100644 index 0000000..152e2b3 --- /dev/null +++ b/src/com/android/htmlviewer/HTMLViewerActivity.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.htmlviewer; + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.util.Log; +import android.view.Window; +import android.webkit.CookieSyncManager; +import android.webkit.WebChromeClient; +import android.webkit.WebSettings; +import android.webkit.WebView; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +/** + * Wraps a WebView widget within an Activity. When launched, it uses the + * URI from the intent as the URL to load into the WebView. + * It supports all URLs schemes that a standard WebView supports, as well as + * loading the top level markup using the file scheme. + * The WebView default settings are used with the exception of normal layout + * is set. + * This activity shows a loading progress bar in the window title and sets + * the window title to the title of the content. + * + */ +public class HTMLViewerActivity extends Activity { + + /* + * The WebView that is placed in this Activity + */ + private WebView mWebView; + + /* + * As the file content is loaded completely into RAM first, set + * a limitation on the file size so we don't use too much RAM. If someone + * wants to load content that is larger than this, then a content + * provider should be used. + */ + static final int MAXFILESIZE = 8096; + + static final String LOGTAG = "HTMLViewerActivity"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Call createInstance() explicitly. createInstance() is called in + // BrowserFrame by WebView. As it is called in WebCore thread, it can + // happen after onResume() is called. To use getInstance() in onResume, + // createInstance() needs to be called first. + CookieSyncManager.createInstance(this); + + requestWindowFeature(Window.FEATURE_PROGRESS); + + mWebView = new WebView(this); + setContentView(mWebView); + + // Setup callback support for title and progress bar + mWebView.setWebChromeClient( new WebChrome() ); + + // Configure the webview + WebSettings s = mWebView.getSettings(); + s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); + s.setUseWideViewPort(true); + s.setSavePassword(false); + s.setSaveFormData(false); + s.setBlockNetworkLoads(true); + + // Javascript is purposely disabled, so that nothing can be + // automatically run. + s.setJavaScriptEnabled(false); + + // Restore a webview if we are meant to restore + if (savedInstanceState != null) { + mWebView.restoreState(savedInstanceState); + } else { + // Check the intent for the content to view + Intent intent = getIntent(); + if (intent.getData() != null) { + Uri uri = intent.getData(); + if ("file".equals(uri.getScheme())) { + String contentUri = + FileContentProvider.BASE_URI + + uri.getEncodedPath() + + "?" + + intent.getType(); + mWebView.loadUrl(contentUri); + } else { + mWebView.loadUrl(intent.getData().toString() + + "?" + intent.getType()); + } + } + } + } + + @Override + protected void onResume() { + super.onResume(); + CookieSyncManager.getInstance().startSync(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + // the default implementation requires each view to have an id. As the + // browser handles the state itself and it doesn't use id for the views, + // don't call the default implementation. Otherwise it will trigger the + // warning like this, "couldn't save which view has focus because the + // focused view XXX has no id". + mWebView.saveState(outState); + } + + @Override + protected void onStop() { + super.onStop(); + + CookieSyncManager.getInstance().stopSync(); + mWebView.stopLoading(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + mWebView.destroy(); + } + + class WebChrome extends WebChromeClient { + + @Override + public void onReceivedTitle(WebView view, String title) { + HTMLViewerActivity.this.setTitle(title); + } + + @Override + public void onProgressChanged(WebView view, int newProgress) { + getWindow().setFeatureInt( + Window.FEATURE_PROGRESS, newProgress*100); + if (newProgress == 100) { + CookieSyncManager.getInstance().sync(); + } + } + } + +} |