package com.flygis.gold;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.flygis.gold.R;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
//import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
public class main extends Activity implements OnTouchListener{
private DBhelp db;
private static final int INSERT_ID_Quit = 0;
private static final int INSERT_ID_Prev = 1;
private static final int INSERT_ID_Next = 2;
private static final int INSERT_ID_Update = 3;
private static final int INSERT_ID_Config = 4;
private static final String goldDir = "/mnt/sdcard/gold/";
private ProgressBar downloadbar;
//同时更新多个文件,进度叠加
private static int iallmax=0;
private static int icurpro=0;
private ImageView img;
private int fileSize = 0;
private Map<Integer, String> PicData;
private int iCur = 0;
private int icount = 0;
private static int ii=0;
private Bitmap nopicbit;
private EditText edt;
private Button badd;
private Button bCls;
private Button bbck;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
int size = msg.getData().getInt("size");
icurpro += size;
//float result = (float)size/ (float)downloadbar.getMax();
//int p = (int)(result*100);
//Toast.makeText(main.this,p+"%", 1).show();
downloadbar.setProgress(icurpro);
//Toast.makeText(main.this,downloadbar.getProgress() + "//" + downloadbar.getMax(),1).show();
if(downloadbar.getProgress()==iallmax){//downloadbar.getMax()){
//downloadbar.setVisibility(4);
downloadbar.setProgress(0);
iallmax = 0;
icurpro = 0;
downloadbar.setMax(0);
}
break;
case 2:
int isize = msg.getData().getInt("size");
iallmax += isize;
downloadbar.setMax(iallmax);
//Toast.makeText(main.this,"文件大小:"+isize+"字节", 1).show();
break;
case 3:
String errostr = msg.getData().getString("info");
String urlpath = msg.getData().getString("path");
Toast.makeText(main.this,urlpath + " 更新错误:" + errostr, 1).show();
break;
case 4:
String urlpath1 = msg.getData().getString("path");
Toast.makeText(main.this, urlpath1 + " 更新完成", 1).show();
break;
case -1:
break;
}
}
};
private GestureDetector gd; //用于捕捉touch的详细手势(gesture)
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);;
HideStatusBar();
setContentView(R.layout.main);
img = (ImageView)this.findViewById(R.id.img);
img.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View arg0) {
openOptionsMenu();
}
});
gd = new GestureDetector (this, new GDetector()); //创建一个GestureDetector实例,下文重写其onFling()方法
//tv_log = (TextView) findViewById(R.id.tv_log); //没啥特别,用于显示调试信息的TextView
//img = (ImageView) findViewById(R.id.ImageView01); //我要用手指蹂躏的那张图片
img.setOnTouchListener(this); //这里如果没有 implements OnTouchListener 会报错
img.setLongClickable(true); //不加这个滑动就没反应了
nopicbit = BitmapFactory.decodeResource(getResources(), R.drawable.wutu);
downloadbar = (ProgressBar)this.findViewById(R.id.progress);
PicData = new HashMap<Integer, String>();
PicData.clear();
ReadXML();
icount = PicData.size();
try {
ShowPic();
} catch (Exception e) {
Toast.makeText(main.this, "没有图片", 1).show();
//throw new RuntimeException("没有图片");
}
}
// 创建OptionsMenu publi c bool ean onCreateOpti onsMenu(Menu menu)
// 处理选择事件publi c bool ean onOpti onsIt emSel ect ed(MenuIt em i t em)
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID_Quit, 0, "退出");
menu.add(0, INSERT_ID_Prev, 0, "上一个");
menu.add(0, INSERT_ID_Next, 0, "下一个");
menu.add(0, INSERT_ID_Update, 0, "更新");
//menu.add(0, INSERT_ID_Config, 0, "配置");
return result;
}// 创建菜单
public boolean onTouch(View v, MotionEvent event) { //把touch的event传递给gestureListener处理
return gd.onTouchEvent(event);
}
class GDetector extends SimpleOnGestureListener { //GDetector 名字是随便起的
@Override
public boolean onFling( MotionEvent e1, //按下时的状态,位置
MotionEvent e2, //松手时的状态,位置
float vx, //x坐标的移动速度,单位: px/秒
float vy){ //y坐标的移动速度
if((e1.getX()-e2.getX()>200) && (Math.abs(vx)>50)) { //滑动速度足够快至少50点/秒,手指起落点减起点是正值且>200 判断属于向左滑动
//tv_log.setText("Fling to Left.\n"+tv_log.getText()); //debug.输出到 tv_log
//iv.setImageResouce(...); //向左划的时候如何怎样切换图片..略..
if (iCur < icount - 1){
iCur ++;
ShowPic();
}
else{
Toast.makeText(main.this, "已经最后一个了", 1).show();
}
}else if ((e2.getX()-e1.getX()>200) && (Math.abs(vx)>50)) { //同理判断是向右滑动
//tv_log.setText("Fling to Right.\n"+tv_log.getText()); //debug
if (iCur > 0){
iCur --;
String imgname = PicData.get(iCur);
imgname = imgname.substring(imgname.lastIndexOf('/') + 1);
File f=new File(goldDir + imgname);
if(f.exists())
{
Bitmap bit = BitmapFactory.decodeFile(goldDir + imgname);
img.setImageBitmap(bit);
}else{
img.setImageBitmap(nopicbit);
}
}
else{
Toast.makeText(main.this, "已经第一个了", 1).show();
}
}
return true;
}
//随着我指尖在屏幕上的游走不断地输出onScroll 的信息,
//最后onScroll() 和 onFling() 我都用上了, Scroll 的时候随手指的移动切换,当手指松开之后用Fling实现惯性的切换,这样就显得相当的自然了 :-)
public boolean onScroll( MotionEvent e1, //按下时的状态,位置
MotionEvent e2, //松手时的状态,位置
float dx, //注意!这里不是速度,是距离了!
float dy){ //y的距离,单位px
return true;
//tv_log.setText("moved"+(e1.getX()-e2.getX())+" distanceX "+ dx + ".\n"+tv_log.getText()); //debug
}
}
private void UpdatePic() {
if( !isNetworkAvailable( this) ){
Toast.makeText(main.this, "没有网络连接可用", 1).show();
return;
}
if (icount<=0){
Toast.makeText(main.this, "Config Error", 1).show();
return;