package cn.android.browser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.android.browser.HelpTabAct;
import cn.android.browser.HistoryBean;
import cn.android.browser.HttpData;
import cn.android.browser.SQLiteHelper;
import cn.android.browser.WriteFavoriteXml;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity
{
private WebView mWebView = null;
final Activity context = this;
private SQLiteHelper mOpenHelper;
public static Cursor myCursor_one;
Intent directCall;
private WriteFavoriteXml writeXml = new WriteFavoriteXml();
private ImageButton btn = null;
private EditText edit = null;
private ImageButton forwardBtn = null;
private ImageButton backBtn = null;
private ListView list = null;
private Button go_back = null;
private ImageButton menuBtn = null;
private final static int HISTORY_ITEM = 0; //历史记录
private final static int HTTP_ITEM = 1; //关于
private final static int SHORTCUT_ITEM = 2; //快捷方式
private final static int ADD_FAVORITE = 3; //加入收藏夹
private final static int FAVORITE_ITEM = 4; //收藏夹
private final static int PREFERENCE_ITEM = 5; //帮助网页
private final static int EXIT_ITEM = 7; //退出
private String cur_url = "http://m.hao123.com";
private final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
List<Map<String, Object>> history_data = new ArrayList<Map<String, Object>>();
List<HistoryBean> xml_data = new ArrayList<HistoryBean>();
String[] dialog_data = new String[]{};
public int selectId = 0;
SharedPreferences sp;
Drawable drawable;
private static String SAVE_KEY = "save-view";
public static Main instance;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w("debug.onCreate","onCreate");
requestWindowFeature(Window.FEATURE_PROGRESS);//让进度条显示在标题栏上
mOpenHelper = new SQLiteHelper(this);
directCall = new Intent(Intent.ACTION_MAIN);//快捷方式
onInit();
instance = this;
if (savedInstanceState == null)
{
deleteTable();
}
else
{
Bundle map = savedInstanceState.getBundle(SAVE_KEY);
if (map != null)
{
restoreState(map);
}
}
}
private void onInit() {
setContentView(R.layout.main);
edit = (EditText)findViewById(R.id.edit_1);
mWebView = (WebView) findViewById(R.id.wv1);
btn = (ImageButton)findViewById(R.id.button_1);
forwardBtn = (ImageButton)findViewById(R.id.forward_btn);
backBtn = (ImageButton)findViewById(R.id.back_btn);
menuBtn = (ImageButton)findViewById(R.id.menu_btn);
btn.setOnClickListener( new Button.OnClickListener()
{
public void onClick( View v )
{
// TODO Auto-generated method stub
String str = edit.getText().toString();
if(str != "")
{
cur_url = str;
setTitle();
mWebView.loadUrl(str);
}
}
} );
forwardBtn.setOnClickListener( new Button.OnClickListener()
{
public void onClick( View v )
{
// TODO Auto-generated method stub
if(mWebView.canGoForward())
mWebView.goForward();
}
} );
backBtn.setOnClickListener( new Button.OnClickListener()
{
public void onClick( View v )
{
// TODO Auto-generated method stub
if(mWebView.canGoBack())
mWebView.goBack();
}
} );
menuBtn.setOnClickListener( new Button.OnClickListener()
{
public void onClick( View v )
{
context.openOptionsMenu();
}
} );
mWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
cur_url = url;
setTitle();
//insertTable(url,1,mWebView.getTitle());
return true;
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//Activity和Webview根据加载程度决定进度条的进度大小
//当加载到100%的时候 进度条自动消失
context.setProgress(progress * 100);
if(progress>=100)
{
insertTable(cur_url,1,view.getTitle());
}
//Log.d("TTTTTTTTT",progress+","+view.getTitle());
}
});
mWebView.loadUrl(cur_url);
setTitle();
Log.i("debug.Init",cur_url);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.i("onCreateContextMenu",v.toString());
Log.i("onCreateContextMenu",String.valueOf(v.getId()));
}
private void setTitle()
{
Bitmap bitmap = mWebView.getFavicon();
drawable = new BitmapDrawable(bitmap);
//edit.setCompoundDrawables(drawable, null, null, null);
drawable = this.getResources().getDrawable(R.drawable.history);
edit.setCompoundDrawablesWithIntrinsicBounds(drawable, null,null,null);
edit.setText(cur_url);
//edit.setMaxLines(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, HISTORY_ITEM, HISTORY_ITEM, R.string.history).setIcon(R.drawable.history); //setIcon,setText
menu.add(0, HTTP_ITEM, HTTP_ITEM, R.string.http_name).setIcon(R.drawable.about);
menu.add(0, SHORTCUT_ITEM, SHORTCUT_ITEM, R.string.shortcut).setIcon(R.drawable.icon);
menu.add(0, ADD_FAVORITE, ADD_FAVORITE, R.string.addFavorite).setIcon(R.drawable.add_favorite);
menu.add(0, FAVORITE_ITEM, FAVORITE_ITEM, R.string.favorite).setIcon(R.drawable.favorite);
menu.add(1, PREFERENCE_ITEM, PREFERENCE_ITEM, R.string.preference).setIcon(R.drawable.help);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()){
case HISTORY_ITEM:
goto_history_view();
break;
case HTTP_ITEM:
showDialog(HTTP_ITEM);
break;
case SHORTCUT_ITEM:
createShortcut();