package com.lianxin.pubqq.main;

import java.util.ArrayList;
import java.util.List;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;

import com.lianxin.panqq.common.Utils;
import com.lianxin.pubqq.R;

public class LoadingActivity extends Activity {
    //启动模块
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
	
		if (Build.VERSION.SDK_INT >= 23
	               && getApplicationInfo().targetSdkVersion >= 23)		
		checkPermission();	
		if(!isNeedCheck)
		mHandler.sendEmptyMessageDelayed(0, 200);
		else if(nNeedCheck<=1)
		mHandler.sendEmptyMessageDelayed(0, 1000);		
		else
		mHandler.sendEmptyMessageDelayed(0, 2000);		
	}



	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			//Boolean isLogin = Utils.getBooleanValue(PqActivity.this,
			//		Constants.LoginState);
			Intent intent = new Intent();
			Boolean isLogin = true;
			if (isLogin) {
				intent.setClass(LoadingActivity.this, MainActivity.class);
			} else {
				intent.setClass(LoadingActivity.this, LoginActivity.class);
			}
			//startActivity(intent);
			Utils.start_Activity(LoadingActivity.this,intent); 
			finish();
		}
	};

	   /**
	    * 需要进行检测的权限数组
	    */
	   protected String[]  needPermissions = {
	         Manifest.permission.ACCESS_COARSE_LOCATION,
	         Manifest.permission.ACCESS_FINE_LOCATION,
	         Manifest.permission.WRITE_EXTERNAL_STORAGE,
	         Manifest.permission.READ_EXTERNAL_STORAGE,
	         Manifest.permission.READ_PHONE_STATE
	         };
	   
	  // private static final int PERMISSON_REQUESTCODE = 0;
	   
	   /**
	    * 判断是否需要检测，防止不停的弹框
	    */
	   private boolean isNeedCheck = false;
	   private int nNeedCheck = 0;	
	    List<String> mPermissionList = new ArrayList<String>();
	    private static final int PERMISSION_REQUEST = 1;
		private void checkPermission(){
	        mPermissionList.clear();
	        //判断哪些权限未授予
	        for (int i = 0; i < needPermissions.length; i++) {
	            if (ContextCompat.checkSelfPermission(this, needPermissions[i]) != PackageManager.PERMISSION_GRANTED) {
	                mPermissionList.add(needPermissions[i]);
	                nNeedCheck++;	                
	            }
	        }
	        // 判断是否为空
	        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
	        } else {//请求权限方法
	            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
	            ActivityCompat.requestPermissions(LoadingActivity.this, permissions, PERMISSION_REQUEST);
	            isNeedCheck = true;	            
	        }
	    }	   


	   @Override
	   public boolean onKeyDown(int keyCode, KeyEvent event) {
	      if(keyCode == KeyEvent.KEYCODE_BACK){
	         this.finish();
	         return true;
	      }
	      return super.onKeyDown(keyCode, event);
	   }	   
}
