package com.gh.common.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {

	/**
	 * 判断是否有网络连接
	 * 
	 * @param context 上下文
	 * @return true 有网络连接 false 无网络连接
	 */
	public static boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断WIFI网络是否可用
	 * 
	 * @param context 上下文
	 * @return true wifi可用 false wifi不可用
	 */
	public static boolean isWifiConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mWiFiNetworkInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mWiFiNetworkInfo != null) {
				return mWiFiNetworkInfo.isConnected();
			}
		}
		return false;
	}
	
	/**
	 * 判断MOBILE网络是否可用
	 * 
	 * @param context 上下文
	 * @return true mobile可用 false mobile不可用
	 */
	public static boolean isMobileConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mMobileNetworkInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (mMobileNetworkInfo != null) {
				return mMobileNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 获取当前网络连接的类型信息
	 * @param context 上下文
	 * @return 当前网络连接的类型信息
	 */
	public static String getConnectedType(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
				switch (mNetworkInfo.getType()) {
				case ConnectivityManager.TYPE_BLUETOOTH:
					return "BLUETOOTH";
				case ConnectivityManager.TYPE_DUMMY:
					return "DUMMY";
				case ConnectivityManager.TYPE_ETHERNET:
					return "ETHERNET";
				case ConnectivityManager.TYPE_MOBILE:
					return "MOBILE";
				case ConnectivityManager.TYPE_MOBILE_DUN:
					return "MOBILE_DUN";
				case ConnectivityManager.TYPE_MOBILE_HIPRI:
					return "MOBILE_HIPRI";
				case ConnectivityManager.TYPE_MOBILE_MMS:
					return "MOBILE_MMS";
				case ConnectivityManager.TYPE_MOBILE_SUPL:
					return "MOBILE_SUPL";
				case ConnectivityManager.TYPE_WIFI:
					return "WIFI";
				case ConnectivityManager.TYPE_WIMAX:
					return "WIMAX";
				default:
					break;
				}
			}
		}
		return "NONE";
	}

}
