package com.tsgame.tsgame_niuniu.util;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.jx.jyhd.R;
import com.tsgame.tsgame_niuniu.system.Myapplication;

public class apputil {


	public static int photoPort1=4479;

	public static int photoPort=4480;

	public static int wxtype = 1;

	
	public static int code = 0;
	
    public static int shaketype=1;    //默认开启震动声
	
	public static int Voicetype=1;    //默认开启声音
	
	//记录Activity
	public  static String activityname="";
	ConnectivityManager manager;
	public  static String Showmessage="";
	/**
	 * 判断网络是否连通
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		return info != null && info.isConnected();
	}

	
	
	/**
	 * 检测网络是否连接
	 * 
	 * @return
	 */
	public boolean checkNetworkState(Context context) {
		boolean flag = false;
		// 得到网络连接信息
		manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// 去进行判断网络是否连接
		if (manager.getActiveNetworkInfo() != null) {
			flag = manager.getActiveNetworkInfo().isAvailable();
		}
		if (!flag) {
			// setNetwork();
		} else {
			isNetworkAvailable();
		}

		return flag;
	}

	/**
	 * 网络未连接时，调用设置方法
	 */
	private void setNetwork() {
		// Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show();

		AlertDialog.Builder builder = new AlertDialog.Builder(
				Myapplication.application);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("网络提示信息");
		builder.setMessage("网络不可用，如果继续，请先设置网络！");
		builder.setPositiveButton("设置", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = null;
				/**
				 * 判断手机系统的版本！如果API大于10 就是3.0+ 因为3.0以上的版本的设置和3.0以下的设置不一样，调用的方法不同
				 */
				
				if (android.os.Build.VERSION.SDK_INT > 10) {
					intent = new Intent(
							android.provider.Settings.ACTION_WIFI_SETTINGS);
				} else {
					intent = new Intent();
					
					ComponentName component = new ComponentName(
							"com.android.settings",
							"com.android.settings.WirelessSettings");
					intent.setComponent(component);
					intent.setAction("android.intent.action.VIEW");
				}
				Myapplication.application.startActivity(intent);
			}
		});

		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.create();
		builder.show();
	}

	/**
	 * 网络已经连接，然后去判断是wifi连接还是GPRS连接 设置一些自己的逻辑调用
	 */

	private void isNetworkAvailable() {
		NetworkInfo networkINfo = manager.getActiveNetworkInfo();
		if (networkINfo != null
				&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
			// return true;
		}

		// State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
		// .getState();
		// State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
		// .getState();
		// if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
		// // Toast.makeText(this, "wifi is open! gprs",
		// // Toast.LENGTH_SHORT).show();
		// }
		// // 判断为wifi状态下才加载广告，如果是GPRS手机网络则不加载！
		// if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
		// // Toast.makeText(this, "wifi is open! wifi",
		// // Toast.LENGTH_SHORT).show();
		// }

	}




	/**
	 * 网络已经连接，然后去判断是wifi连接还是GPRS连接 设置一些自己的逻辑调用
	 */

	public static  boolean isNetworkAvailable(Context context) {
		// 得到网络连接信息
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkINfo = manager.getActiveNetworkInfo();
		if (networkINfo != null
				&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		}
		
		return false;
	}
}
