package com.tjzhxx.union.public_store;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.text.TextUtils;

/**
 * 跟网络相关的工具类
 */
public class FNNetwork {
	
	private FNNetwork() {
		throw new UnsupportedOperationException("cannot be instantiated");
	}
	/**
	 * 判断网络是否连接
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isConnected(Context context) {

		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (null != connectivity) {

			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (null != info && info.isConnected()) {
				if (info.getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断是否是wifi连接
	 */
	public static boolean isWIFI(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		return (cm != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI);
	}

	public static boolean isMobile(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		return (cm != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_MOBILE);
	}
	/**
	 * 打开网络设置界面
	 */
	public static void openNetworkSetting(Activity activity) {
		Intent intent = null;
		// 判断手机系统的版本 即API大于10 就是3.0或以上版本
		if (Build.VERSION.SDK_INT > 10) {
			intent = new Intent(
					android.provider.Settings.ACTION_WIRELESS_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");
		}
		activity.startActivityForResult(intent, 0);
	}

	/*
	 * 判断设备 是否使用代理上网
	 * */
	public static boolean isWifiProxy(Context context) {

		final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;

		String proxyAddress;

		int proxyPort;

		if (IS_ICS_OR_LATER) {

			proxyAddress = System.getProperty("http.proxyHost");

			String portStr = System.getProperty("http.proxyPort");

			proxyPort = Integer.parseInt((portStr != null ? portStr : "-1"));

		} else {

			proxyAddress = android.net.Proxy.getHost(context);

			proxyPort = android.net.Proxy.getPort(context);

		}

		return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1);

	}
}
