CEEX v1.0.1版本的 MD5 值为:d20246587215ec3471b0d051724c6d34

以下内容为反编译后的 NetworkUtils.java 源代码,内容仅作参考


package io.bhex.baselib.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import io.bhex.baselib.R;
import io.bhex.baselib.constant.Fields;
import io.bhex.baselib.core.CApplication;
import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.URL;
import java.util.Enumeration;
public class NetworkUtils {
    public static final int NETWORK_2G = 2;
    public static final int NETWORK_3G = 3;
    public static final int NETWORK_4G = 4;
    public static final int NETWORK_MOBILE = 5;
    public static final int NETWORK_NONE = 0;
    public static final int NETWORK_WIFI = 1;
    public static final int NET_MOBILE = 1;
    public static final int NET_OTHER = 2;
    public static final int NET_WIFI = 0;

    private NetworkUtils() {
    }

    public static void changeWIFIStatus(Context context, boolean z) {
        ((WifiManager) context.getSystemService("wifi")).setWifiEnabled(z);
    }

    public static ConnectivityManager getConnManager(Context context) {
        return (ConnectivityManager) context.getSystemService("connectivity");
    }

    public static int getConnectedType(Context context) {
        try {
            NetworkInfo activeNetworkInfo = getConnManager(context).getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                int type = activeNetworkInfo.getType();
                if (type != 0) {
                    return type != 1 ? 2 : 0;
                }
                return 1;
            }
        } catch (Exception unused) {
        }
        return 2;
    }

    public static String getHostAddressByDomain(final String str) {
        String str2 = SP.get("cache_ip", "");
        long j = SP.get("cache_ip_time", 0L);
        if (TextUtils.isEmpty(str2) || System.currentTimeMillis() - j > 3000000) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String hostAddress = InetAddress.getByName(str).getHostAddress();
                        if (TextUtils.isEmpty(hostAddress)) {
                            hostAddress = "";
                        }
                        SP.set("cache_ip_time", System.currentTimeMillis());
                        SP.set("cache_ip", hostAddress);
                        DebugLog.e("IP ::: " + hostAddress);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        return str2;
    }

    public static String getHostFromUrl(String str) {
        try {
            return new URL(str).getHost();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getIpAddress(Context context) {
        try {
            int connectedType = getConnectedType(context);
            if (1 == connectedType) {
                return getMobileIPAddress();
            }
            return connectedType == 0 ? getWifiIPAddress(context) : "127.0.0.1";
        } catch (Exception e) {
            DebugLog.e("getIp-->Error::", e);
            return "127.0.0.1";
        }
    }

    public static String getMobileIPAddress() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                Enumeration<InetAddress> inetAddresses = networkInterfaces.nextElement().getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress nextElement = inetAddresses.nextElement();
                    if (!nextElement.isLoopbackAddress() && (nextElement instanceof Inet4Address)) {
                        return nextElement.getHostAddress();
                    }
                }
            }
            return "127.0.0.1";
        } catch (Exception e) {
            DebugLog.e("getIp-->Error::", e);
            return "127.0.0.1";
        }
    }

    public static int getNetworkState(Context context) {
        NetworkInfo activeNetworkInfo;
        NetworkInfo.State state;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null || (activeNetworkInfo = connectivityManager.getActiveNetworkInfo()) == null || !activeNetworkInfo.isAvailable()) {
            return 0;
        }
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(1);
        if (networkInfo == null || (state = networkInfo.getState()) == null || !(state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)) {
            switch (((TelephonyManager) context.getSystemService("phone")).getNetworkType()) {
                case 1:
                case 2:
                case 4:
                case 7:
                case 11:
                    return 2;
                case 3:
                case 5:
                case 6:
                case 8:
                case 9:
                case 10:
                case 12:
                case 14:
                case 15:
                    return 3;
                case 13:
                    return 4;
                default:
                    return 5;
            }
        }
        return 1;
    }

    public static String getNetworkType(Context context) {
        int connectedType = getConnectedType(context);
        return connectedType != 0 ? connectedType != 1 ? "unknow" : Fields.FIELD_MOILE : "wifi";
    }

    public static String getOperatorName(Context context) {
        return ((TelephonyManager) context.getSystemService("phone")).getSimOperatorName();
    }

    public static String getWifiIPAddress(Context context) {
        try {
            int ipAddress = ((WifiManager) context.getSystemService("wifi")).getConnectionInfo().getIpAddress();
            return String.format("%d.%d.%d.%d", Integer.valueOf(ipAddress & 255), Integer.valueOf((ipAddress >> 8) & 255), Integer.valueOf((ipAddress >> 16) & 255), Integer.valueOf((ipAddress >> 24) & 255));
        } catch (Exception e) {
            DebugLog.e("getIp-->Error::", e);
            return "127.0.0.1";
        }
    }

    public static boolean isAvailable(Context context) {
        return isWifiAvailable(context) || (isMobileAvailable(context) && isMobileEnabled(context));
    }

    public static boolean isAvaliable() {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) CApplication.getInstance().getSystemService("connectivity")).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public static boolean isConnected(Context context) {
        try {
            NetworkInfo activeNetworkInfo = getConnManager(context).getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                return activeNetworkInfo.isConnected();
            }
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static boolean isConnectedOrConnecting(Context context) {
        NetworkInfo[] allNetworkInfo = getConnManager(context).getAllNetworkInfo();
        if (allNetworkInfo != null) {
            for (NetworkInfo networkInfo : allNetworkInfo) {
                if (networkInfo.isConnectedOrConnecting()) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isMobileAvailable(Context context) {
        NetworkInfo[] allNetworkInfo = getConnManager(context).getAllNetworkInfo();
        if (allNetworkInfo != null) {
            for (NetworkInfo networkInfo : allNetworkInfo) {
                if (networkInfo.getType() == 0) {
                    return networkInfo.isAvailable();
                }
            }
        }
        return false;
    }

    public static boolean isMobileConnected(Context context) {
        NetworkInfo activeNetworkInfo = getConnManager(context).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.getType() == 0 && activeNetworkInfo.isConnected();
    }

    public static boolean isMobileEnabled(Context context) {
        try {
            Method declaredMethod = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled", new Class[0]);
            declaredMethod.setAccessible(true);
            return ((Boolean) declaredMethod.invoke(getConnManager(context), new Object[0])).booleanValue();
        } catch (Exception e) {
            DebugLog.i(e.getMessage());
            return true;
        }
    }

    public static boolean isWIFIActivate(Context context) {
        return ((WifiManager) context.getSystemService("wifi")).isWifiEnabled();
    }

    public static boolean isWifiAvailable(Context context) {
        NetworkInfo[] allNetworkInfo = getConnManager(context).getAllNetworkInfo();
        if (allNetworkInfo != null) {
            for (NetworkInfo networkInfo : allNetworkInfo) {
                if (networkInfo.getType() == 1) {
                    return networkInfo.isAvailable();
                }
            }
        }
        return false;
    }

    public static boolean isWifiConnected(Context context) {
        NetworkInfo activeNetworkInfo = getConnManager(context).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.getType() == 1 && activeNetworkInfo.isConnected();
    }

    public static void netWorkErrorTips() {
        if (isAvaliable()) {
            return;
        }
        ToastUtils.showShort(CApplication.getInstance().getResources().getString(R.string.no_internet_use));
    }

    public static boolean printNetworkInfo(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            DebugLog.i("getActiveNetworkInfo: " + activeNetworkInfo);
            NetworkInfo[] allNetworkInfo = connectivityManager.getAllNetworkInfo();
            if (allNetworkInfo != null) {
                for (int i = 0; i < allNetworkInfo.length; i++) {
                    DebugLog.i("NetworkInfo[" + i + "]isAvailable : " + allNetworkInfo[i].isAvailable());
                    DebugLog.i("NetworkInfo[" + i + "]isConnected : " + allNetworkInfo[i].isConnected());
                    DebugLog.i("NetworkInfo[" + i + "]isConnectedOrConnecting : " + allNetworkInfo[i].isConnectedOrConnecting());
                    DebugLog.i("NetworkInfo[" + i + "]: " + allNetworkInfo[i]);
                }
                DebugLog.i(org.apache.commons.lang3.StringUtils.LF);
            } else {
                DebugLog.i("getAllNetworkInfo is null");
            }
        }
        return false;
    }
}