package com.example.androidapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.style.BulletSpan;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	public TextView tv_splash_version;
	public RelativeLayout rl_splash;
	
	
	//¶¨Òå³£ÁÁ
	private static final int GET_INFO_SUCCESS=10;
	private static final int SERVER_ERROR=11;
	private static final int Server_URL_ERROR=12;
	private static final int PROTOCOL_ERROR=13;
	private static final int IO_ERROR=14;
	private static final int XML_PARSE_ERROR=15;
	protected static final String TAG = "MainActivity";
	
	
	private PackageInfo info;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//ÉèÖÃÎÞ±êÌâ
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//È«ÆÁÄ£Ê½
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.activity_main);
		
		this.init();
		
		AlphaAnimation aa = new AlphaAnimation(0.3f,1.0f);
		aa.setDuration(2000);
		rl_splash.setAnimation(aa);
		
		//Á´½Ó·þÎñÆ÷£¬»ñÈ¡ÅäÖÃÐÅÏ¢
		
		
		
	}

	private void init() {
		// TODO Auto-generated method stub
		tv_splash_version = (TextView) findViewById(R.id.tv_splash_version);
		rl_splash = (RelativeLayout) findViewById(R.id.rl_splash);
		tv_splash_version.setText("°æ±¾ºÅ:"+getVersion());
	}

	private String getVersion() {
		// TODO Auto-generated method stub
		PackageManager pm = this.getPackageManager();
		try {
			info = pm.getPackageInfo(getPackageName(), 0);
			return info.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "°æ±¾Î´ÕÒµ½";
		}
		
		
	}
	
	private Handler handler = new Handler(){
		//Íê³ÉºóµÄ»Øµ÷
		 public void handleMessage(Message msg){
			 switch(msg.what){
			    case GET_INFO_SUCCESS :
			    	//³É¹¦»ñÈ¡°æ±¾ºÅÖ®ºóµÄ²Ù×÷  
			    	//ÔÚÄÃµ½·þÎñÆ÷µÄ°æ±¾Ö®ºóÓëÏÖÓÐµÄ°æ±¾½øÐÐ¶Ô±È¡£
			    	String currentPackageVersion = getVersion();
			    	String serverPackageVersion = "";
			    	if(currentPackageVersion.equals(serverPackageVersion)){
			    		//ÊÇ×îÐÂ°æ±¾
			    		Log.i(TAG, "°æ±¾ºÅÏàÍ¬£¬½øÈëÖ÷½çÃæ");
			    	}else{
			    		//·Ç×îÐÂ°æ±¾
			    		 Toast.makeText(getApplicationContext(), "ÓÐ×îÐÂ°æ±¾ºÅÁËÅ¶¡£", Toast.LENGTH_SHORT).show();
			    		 //ÈÃÓÃ»§Ñ¡ÔñÊÇ·ñÈ¥Éý¼¶¡£
			    		 Log.i(TAG, "°æ±¾ºÅ²»Í¬£¬Éý¼¶¶Ô»°¿ò¡£");
			    		 this.showUpdateDialog();
			    	}
			    	break;
			    case SERVER_ERROR :
			    	Toast.makeText(getApplicationContext(), "·þÎñÆ÷Òì³£", Toast.LENGTH_SHORT).show();
			    	break;
			    case Server_URL_ERROR :
			    	Toast.makeText(getApplicationContext(), "·þÎñÆ÷URLÒì³£", Toast.LENGTH_SHORT).show();
			    	break;
			    case PROTOCOL_ERROR :
			    	Toast.makeText(getApplicationContext(), "Ð­Òé²»Ö§³Ö", Toast.LENGTH_SHORT).show();
			    	break;
			    case IO_ERROR :
			    	Toast.makeText(getApplicationContext(), "I/OÒì³£", Toast.LENGTH_SHORT).show();
			    	break;
			    case XML_PARSE_ERROR :
			    	Toast.makeText(getApplicationContext(), "XML½âÎöÒì³£", Toast.LENGTH_SHORT).show();
			    	break;
			    default:
			    	break;
			 }
		 }

		private void showUpdateDialog() {
			// TODO Auto-generated method stub
//			AlertDialog alertDialog = new AlertDialog()
			//´´½¨builder
			Builder builder = new Builder(getApplicationContext());
			//ÉèÖÃ²ÎÊý
			builder.setIcon(R.drawable.bg);
			builder.setTitle("ÌáÊ¾");
			builder.setMessage("ÒÑ¾­·¢²¼ÐÂ°æ±¾£¬ÄúÐèÒª¸üÐÂÂð£¿");
			builder.setPositiveButton("", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			
			builder.setNegativeButton("", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			//´´½¨²¢ÏÔÊ¾
			builder.create().show();
		}
	};

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}

}
