package com.imofan.android.basic.update;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;


/**
 * 网络判断工具类
 */
public class MFNetworkUtils {
    
    /**
     * 无网络状态
     */
    public final static int STATE_NONE = 0;
    /**
     * WIFI网络状态
     */
    public final static int STATE_WIFI = 1;
    /**
     * 移动数据状态
     */
    public final static int STATE_MOBILE = 2;
    
    /**
     * WIFI关闭
     */
    public final static int WIFI_OFF = -1;
    /**
     * WIFI打开
     */
    public final static int WIFI_ON = 0;
    
    private static WifiManager wifiManager;

    /**
     * 获取当前设备的WIFI打开状态
     * @param context
     * @return
     */
    public static int getWifiIsEnabled(Context context){
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if(wifiManager.isWifiEnabled()){
            return WIFI_ON;
        }
        return WIFI_OFF;
    }
    
    /**
     * 获取当前网络状态(是否可用)
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context){
        boolean netWorkStatus = false;
        
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        if(connManager.getActiveNetworkInfo()!=null){
            netWorkStatus = connManager.getActiveNetworkInfo().isAvailable();
        }
        
        return netWorkStatus;
    }
    
    /**
     * 获取当前网络（移动数据/WIFI）
     * @param context
     * @return
     */
    public static int getNetworkState(Context context){
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        
        //Wifi网络判断
        State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if(state == State.CONNECTED||state == State.CONNECTING){
            return STATE_WIFI;
        }
        
        //3G网络判断
        state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if(state == State.CONNECTED||state == State.CONNECTING){
            return STATE_MOBILE;
        }

        return STATE_NONE;
    }
}
