package com.smart.gtstrvl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import com.example.mountaintravel.base.BaseActivity;
import com.example.mountaintravel.variable.Variable;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class StartActivity extends BaseActivity implements OnClickListener {
	ImageView start_image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_start);
		start_image = (ImageView) findViewById(R.id.start_image);
		start_image.setOnClickListener(this);
		copyFilesFassets(StartActivity.this, "shapfile", getSDPath()
				+ "/shapfile");
		final Intent localIntent = new Intent(this, MainActivity.class);
		Timer timer = new Timer();
		TimerTask tast = new TimerTask() {
			@Override
			public void run() {
				startActivity(localIntent);
				finish();
			}
		};
		timer.schedule(tast, 3000);

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		return false;
	}

	public String getSDPath() {
		File sdDir = null;
		// 判断SD卡是否存在
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取根目录
		}
		return sdDir.toString();
	}

	/**
	 * 从assets目录中复制整个文件夹内容 如：复制assets下aa文件夹里所有文件
	 * copyFilesFassets(context,"aa","sd卡")
	 * 
	 * @param context
	 *            Context 使用CopyFiles类的Activity
	 * @param oldPath
	 *            String assets路径如：aa
	 * @param newPath
	 *            String 复制后路径 如：xx:/bb/cc
	 */
	public void copyFilesFassets(Context context, String oldPath, String newPath) {
		try {
			File file = new File(newPath);
			if (!file.exists()) {
				String fileNames[] = context.getAssets().list(oldPath);// 获取assets目录下的所有文件及目录名
				if (fileNames.length > 0) {// 如果是目录
					file.mkdirs();// 如果文件夹不存在，则递归
					for (String fileName : fileNames) {
						copyFilesFassets(context, oldPath + "/" + fileName,
								newPath + "/" + fileName);
					}
				} else {// 如果是文件
					InputStream is = context.getAssets().open(oldPath);
					FileOutputStream fos = new FileOutputStream(new File(
							newPath));
					byte[] buffer = new byte[1024];
					int byteCount = 0;
					while ((byteCount = is.read(buffer)) != -1) {// 循环从输入流读取
																	// buffer字节
						fos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
					}
					fos.flush();// 刷新缓冲区
					is.close();
					fos.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
