package com.xty.base.utils;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

 public final class NetworkUtils {

	public static int NET_ETHERNET = 1;
	public static int NET_WIFI = 2;
	public static int NET_NOCONNECT = 0;

	/**
	 * 检查当前网络是否可用
	 * 
	 * @param
	 * @return
	 */
	public static boolean isNetworkAvailable(Context activity) {
		Context context = activity;
		if (activity instanceof Activity) {
			context = activity.getApplicationContext();
		}
		//获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivityManager == null) {
			return false;
		} else {
			// 获取NetworkInfo对象
			NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

			if (networkInfo != null && networkInfo.length > 0) {
				for (int i = 0; i < networkInfo.length; i++) {
					/**
					System.out.println(i + "===状态==="
							+ networkInfo[i].getState());
					System.out.println(i + "===类型==="
							+ networkInfo[i].getTypeName());
					**/
					//判断当前网络状态是否为连接状态
					if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static int isNetworkType(Context context) {
		ConnectivityManager connectMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ethNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
		NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


		if (ethNetInfo != null && ethNetInfo.isConnected()) {
			return NET_ETHERNET;
		} else if (wifiNetInfo != null && wifiNetInfo.isConnected()) {
			return NET_WIFI;
		} else {
			return NET_NOCONNECT;
		}
	}

	
	/**
		 * 判断GPS是否开启
     * @param
     * @return
     */
    public static boolean isGpsEnabled(Context context){
        //获取位置服务
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        //返回开启状态
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
}
