android 编写简单浏览器带下载 支持多触点 支持下载功能
转自:http://www./html/2182.html
- package org.badboy.browser;
-
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
-
- import org.metalev.multitouch.controller.MultiTouchController;
- import org.metalev.multitouch.controller.MultiTouchController.MultiTouchObjectCanvas;
- import org.metalev.multitouch.controller.MultiTouchController.PointInfo;
- import org.metalev.multitouch.controller.MultiTouchController.PositionAndScale;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.app.SearchManager;
- import android.content.Context;
- import android.content.Intent;
- import android.content.res.Configuration;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.webkit.DownloadListener;
- import android.webkit.URLUtil;
- import android.webkit.WebChromeClient;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.Toast;
-
-
-
-
-
-
- public class BrowserActivity extends Activity implements MultiTouchObjectCanvas<Object>{
-
- private final static int MENU_NEW_WEB = Menu.FIRST;
- private final static int MENU_SET_MAIN_PAGE = MENU_NEW_WEB + 1;
-
- private String defaultGoogleUrl = "";
- private String defaultUCWebUrl = "";
- private String defaultUrl = "";
- private WebView mWebView;
-
- private String fileUrl = "fileUrl.txt";
-
- private final static int PROGRESS_DIALOG = 110;
- private final static int SET_DEFAULT_URL_DIALOG = 111;
-
- private ProgressDialog mDialog;
- private ProgressBar bar;
-
- private MultiTouchController<Object> mMultiTouchController;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
- this.requestWindowFeature(Window.FEATURE_RIGHT_ICON);
- this.requestWindowFeature(Window.FEATURE_PROGRESS);
- this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
-
- setContentView(R.layout.main);
- initWebView();
- performSearchIntent(getIntent());
-
-
- }
-
- @Override
- protected void onNewIntent(Intent intent) {
- setIntent(intent);
- performSearchIntent(intent);
- }
-
-
-
- private void performSearchIntent(Intent intent) {
- if(Intent.ACTION_SEARCH.equals(intent.getAction())){
- String query = intent.getStringExtra(SearchManager.QUERY);
-
- if(!query.startsWith("http://")){
- mWebView.loadUrl("http://"+query);
- }
- if(query.startsWith("http://")){
- mWebView.loadUrl(query);
- }
-
-
-
- }
- }
-
- private void initWebView() {
- defaultGoogleUrl = this.getResources().getString(R.string.default_google_url);
- defaultUCWebUrl = this.getResources().getString(R.string.default_ucweb_url);
- setDefaultURL();
-
- bar = (ProgressBar)findViewById(R.id.progress_bar);
-
- mMultiTouchController = new MultiTouchController<Object>(this,false);
-
- mWebView = (WebView)findViewById(R.id.webview);
-
- mWebView.getSettings().setJavaScriptEnabled(true);
- mWebView.setScrollBarStyle(0);
-
- mWebView.loadUrl(defaultUrl);
-
-
-
- mWebView.setWebViewClient(new MyWebViewClient());
-
- mWebView.setWebChromeClient(new MyWebChromeClient());
-
- mWebView.setDownloadListener(new DownloadListener(){
-
- public void onDownloadStart(String url, String userAgent, String contentDisposition,
- String mimetype, long contentLength) {
-
-
-
-
- downloadFile(url, mimetype, mimetype, mimetype, contentLength);
- }
-
- });
- }
-
-
-
-
-
-
-
- private void downloadFile(String url,String userAgent, String contentDisposition,
- String mimetype, long contentLength) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- try {
- String filename = URLUtil.guessFileName(url,
- contentDisposition, mimetype);
- URL url2 = new URL(url);
- HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
- conn.setDoInput(true);
- conn.connect();
- if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
- InputStream is = conn.getInputStream();
- FileOutputStream fos = this.openFileOutput(filename, Context.MODE_APPEND);
- int len = 0;
- byte[] buf = new byte[1024];
- while ((len = is.read(buf)) != -1) {
- fos.write(buf, 0, len);
- }
- is.close();
- fos.close();
- } else {
- Toast.makeText(this, R.string.net_error, Toast.LENGTH_SHORT).show();
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- private void setDefaultURL() {
- try {
-
- FileInputStream fis = this.openFileInput(fileUrl);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while((len = fis.read(buffer))!=-1){
- baos.write(buffer, 0, len);
- }
-
- defaultUrl = baos.toString();
- fis.close();
- baos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- if(defaultUrl.equals("")){
- defaultUrl = defaultUCWebUrl;
- }
- }
-
-
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
-
- if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()){
- mWebView.goBack();
- return true;
- }
-
- if(keyCode == KeyEvent.KEYCODE_SEARCH){
- onSearchRequested();
- }
- return super.onKeyDown(keyCode, event);
- }
-
-
-
- private class MyWebChromeClient extends WebChromeClient {
-
- @Override
- public void onProgressChanged(WebView webview, int newProgress){
- BrowserActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, 100);
-
-
-
-
-
-
-
- bar.setProgress(newProgress);
- bar.setVisibility(View.VISIBLE);
- if(bar.getProgress()==100){
- bar.setVisibility(View.GONE);
- }
- super.onProgressChanged(webview, newProgress);
- }
- }
-
-
-
- private class MyWebViewClient extends WebViewClient {
-
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.loadUrl(url);
- return true;
- }
-
- }
-
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- menu.add(0,MENU_NEW_WEB,0,R.string.new_web_page)
- .setIcon(R.drawable.search_icon);
-
- menu.add(0,MENU_SET_MAIN_PAGE,0,R.string.set_default_url)
- .setIcon(R.drawable.browser_icon);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onMenuItemSelected(int featureId, MenuItem item) {
- switch(item.getItemId()){
- case MENU_NEW_WEB:
- onSearchRequested();
- return true;
- case MENU_SET_MAIN_PAGE:
- this.showDialog(SET_DEFAULT_URL_DIALOG);
- break;
- }
- return super.onMenuItemSelected(featureId, item);
- }
-
-
-
-
- @Override
- public boolean onSearchRequested() {
-
-
-
-
- this.startSearch(null, false, null, false);
- return true;
- }
-
-
-
- @Override
- protected Dialog onCreateDialog(int id) {
- switch(id){
- case PROGRESS_DIALOG:
- mDialog = new ProgressDialog(this);
-
- mDialog.setMessage("Page is loading...");
- return mDialog;
- case SET_DEFAULT_URL_DIALOG:
- return createDefaultUrlDialog();
- default:
- return super.onCreateDialog(id);
- }
-
- }
- private Dialog createDefaultUrlDialog() {
- final Dialog dialog = new Dialog(this);
- dialog.setContentView(R.layout.default_url_dialog);
- dialog.setTitle(R.string.dialog_title);
- final EditText url = (EditText)dialog.findViewById(R.id.url);
- Button btnOK = (Button)dialog.findViewById(R.id.ok_btn);
- btnOK.setOnClickListener(new View.OnClickListener() {
-
- public void onClick(View arg0) {
- defaultUrl = url.getText().toString();
- try {
- FileOutputStream fos = BrowserActivity.this.openFileOutput(fileUrl, Context.MODE_PRIVATE);
- byte[] buffer = defaultUrl.getBytes();
- fos.write(buffer);
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- dialog.dismiss();
- mWebView.loadUrl(defaultUrl);
- }
- });
- Button btnCancel = (Button)dialog.findViewById(R.id.cancel_btn);
- btnCancel.setOnClickListener(new View.OnClickListener() {
-
- public void onClick(View arg0) {
- dialog.dismiss();
- }
- });
- return dialog;
- }
-
-
-
-
-
-
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
-
-
-
-
-
-
-
- super.onConfigurationChanged(newConfig);
- }
-
-
-
- private int mCurrentZoom = 0;
- private static final double ZOOM_SENSITIVITY = 1.6;
- private static final float ZOOM_LOG_BASE_INV = 1.0f / (float) Math.log(2.0 / ZOOM_SENSITIVITY);
- private boolean isMultiTouchScale = false;
- @Override
- public boolean dispatchTouchEvent(MotionEvent event) {
- if (mMultiTouchController.onTouchEvent(event)) {
- if (!isMultiTouchScale) {
- event.setAction(MotionEvent.ACTION_CANCEL);
- super.dispatchTouchEvent(event);
- }
- }else{
- isMultiTouchScale = true;
- if (super.dispatchTouchEvent(event)) {
- return true;
- }
- return false;
- }
- return true;
- }
- public Object getDraggableObjectAtPoint(PointInfo touchPoint) {
- return new Object();
- }
-
- public void getPositionAndScale(Object obj,
- PositionAndScale objPosAndScaleOut) {
- objPosAndScaleOut.set(0.0f, 0.0f, true, 1.0f, false, 0.0f, 0.0f, false, 0.0f);
- mCurrentZoom = 0;
- }
-
- public void selectObject(Object obj, PointInfo touchPoint) {
-
- }
-
- public boolean setPositionAndScale(Object obj,
- PositionAndScale newObjPosAndScale, PointInfo touchPoint) {
- float newRelativeScale = newObjPosAndScale.getScale();
- int targetZoom = (int) Math.round(Math.log(newRelativeScale)
- * ZOOM_LOG_BASE_INV);
- while (mCurrentZoom > targetZoom) {
- mCurrentZoom--;
- mWebView.zoomOut();
- }
- while (mCurrentZoom < targetZoom) {
- mCurrentZoom++;
- mWebView.zoomIn();
- }
- return true;
- }
-
- }