package org.senydevpkg.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * NetWork Utils
 * <ul>
 * <strong>Attentions</strong>
 * <li>You should add <strong>android.permission.ACCESS_NETWORK_STATE</strong> in manifest, to get network status.</li>
 * </ul>
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-11-03
 */
public class NetworkUtils {

    public static final String NETWORK_TYPE_WIFI = "wifi";
    public static final String NETWORK_TYPE_3G = "eg";
    public static final String NETWORK_TYPE_2G = "2g";
    public static final String NETWORK_TYPE_WAP = "wap";
    public static final String NETWORK_TYPE_UNKNOWN = "unknown";
    public static final String NETWORK_TYPE_DISCONNECT = "disconnect";

    /**
     * Get network type
     *
     * @param context
     * @return
     */
    public static int getNetworkType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
        return networkInfo == null ? -1 : networkInfo.getType();
    }

    /**
     * Check network
     *
     * @param context
     * @return
     */
    public static boolean checkNetwork(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }

    /**
     * Get network type name
     *
     * @param context
     * @return
     */
    public static String getNetworkTypeName(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo;
        String type = NETWORK_TYPE_DISCONNECT;
        if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) {
            return type;
        }
        ;

        if (networkInfo.isConnected()) {
            String typeName = networkInfo.getTypeName();
            if ("WIFI".equalsIgnoreCase(typeName)) {
                type = NETWORK_TYPE_WIFI;
            } else if ("MOBILE".equalsIgnoreCase(typeName)) {
                String proxyHost = android.net.Proxy.getDefaultHost();
                type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G)
                        : NETWORK_TYPE_WAP;
            } else {
                type = NETWORK_TYPE_UNKNOWN;
            }
        }
        return type;
    }

    /**
     * Whether is fast mobile network
     *
     * @param context
     * @return
     */
    private static boolean isFastMobileNetwork(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager == null) {
            return false;
        }

        switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false;
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false;
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true;
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true;
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false;
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true;
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true;
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true;
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true;
            case TelephonyManager.NETWORK_TYPE_EHRPD:
                return true;
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                return true;
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return true;
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return false;
            case TelephonyManager.NETWORK_TYPE_LTE:
                return true;
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                return false;
            default:
                return false;
        }
    }
}
@@ -3367,461 +3367,70 @@
 YPE_
-1xRTT:%0A                return false;%0A            case TelephonyManager.NETWORK_TYPE_CDMA:%0A                return false;%0A            case TelephonyManager.NETWORK_TYPE_EDGE:%0A                return false;%0A            case TelephonyManager.NETWORK_TYPE_EVDO_0:%0A                return true;%0A            case TelephonyManager.NETWORK_TYPE_EVDO_A:%0A                return true;%0A            case TelephonyManager.NETWORK_TYPE_GPRS:%0A                return false;
+EVDO_0:%0A            case TelephonyManager.NETWORK_TYPE_EVDO_A:
 %0A   
@@ -3484,37 +3484,8 @@
 PA:%0A
-                return true;%0A
     
@@ -3537,37 +3537,8 @@
 PA:%0A
-                return true;%0A
     
@@ -3591,37 +3591,8 @@
 PA:%0A
-                return true;%0A
     
@@ -3644,37 +3644,8 @@
 TS:%0A
-                return true;%0A
     
@@ -3698,37 +3698,8 @@
 PD:%0A
-                return true;%0A
     
@@ -3753,37 +3753,8 @@
 _B:%0A
-                return true;%0A
     
@@ -3803,16 +3803,68 @@
 _HSPAP:%0A
+            case TelephonyManager.NETWORK_TYPE_LTE:%0A
         
@@ -3935,12 +3935,13 @@
 YPE_
-IDEN
+1xRTT
 :%0A  
@@ -3950,33 +3950,56 @@
         
-    return false;
+case TelephonyManager.NETWORK_TYPE_CDMA:
 %0A       
@@ -4030,34 +4030,35 @@
 er.NETWORK_TYPE_
-LT
+EDG
 E:%0A             
@@ -4056,32 +4056,56 @@
         
-    return true;
+case TelephonyManager.NETWORK_TYPE_GPRS:
 %0A       
@@ -4144,22 +4144,19 @@
 RK_TYPE_
-UNKNOW
+IDE
 N:%0A     
@@ -4162,33 +4162,59 @@
         
-    return false;
+case TelephonyManager.NETWORK_TYPE_UNKNOWN:
 %0A       

重构isFastMobileNetwork方法的switch语句逻辑。