Snaptube v4.40.0.4401610版本的 MD5 值为:ca2b95dcff63007195db02a6c93091b9

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


package com.wandoujia.base.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.wandoujia.base.config.GlobalConfig;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.concurrent.Callable;
import o.axx;
import o.cdz;
import rx.Observable;
import rx.functions.Action1;
public final class NetworkUtil {
    private static final String ANDROID_HOTSPOT_IP_ADDRESS = "192.168.43.1";
    private static final String DEFAULT_NET_CHECK_IP = "8.8.8.8";
    private static final int DEFAULT_NET_CHECK_PORT = 53;
    private static final String IOS_HOTSPOT_IP_ADDRESS = "172.20.10.1";
    private static final String KEY_ENABLE_CHECK_HTTP_ACCESS = "key.enable_check_http_access";
    private static final String KEY_NET_CHECK_HOST = "net_check_host";
    private static final String KEY_NET_CHECK_PORT = "net_check_port";
    public static final int NETWORK_TYPE_MOBILE = 0;
    public static final int NETWORK_TYPE_NONE = -1;
    public static final int NETWORK_TYPE_PC = -2;
    public static final int NETWORK_TYPE_WIFI = 1;
    private static final String PREF_SWITCHES = "pref.switches";
    private static final String SITE_YOUTUBE = "https://m.youtube.com";
    private static final int SOCKET_TIMEOUT_MS = 20000;
    private static boolean reverseProxyOn = false;
    private static String sNetCheckHost;
    private static int sNetCheckPort;

    static Void access$000() {
        return requestYouTube();
    }

    private NetworkUtil() {
    }

    @SuppressLint({"MissingPermission"})
    public static boolean isNetworkConnected(Context context) {
        NetworkInfo networkInfo;
        if (context == null) {
            return false;
        }
        if (reverseProxyOn) {
            return true;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager != null) {
            try {
                networkInfo = connectivityManager.getActiveNetworkInfo();
            } catch (Exception e) {
                e.printStackTrace();
                networkInfo = null;
            }
            return networkInfo != null && networkInfo.isConnected();
        }
        return false;
    }

    public static boolean isInternetAccessible(Context context) {
        return isNetworkConnected(context);
    }

    private static void detectNetworkUseHttp(Context context) {
        if (context.getSharedPreferences("pref.switches", 0).getBoolean(KEY_ENABLE_CHECK_HTTP_ACCESS, false)) {
            Observable.fromCallable(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    return NetworkUtil.access$000();
                }
            }).subscribeOn(axx.f15917).subscribe(new Action1<Void>() {
                @Override
                public void call(Void r1) {
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable th) {
                }
            });
        }
    }

    private static Void requestYouTube() {
        int i;
        HttpURLConnection httpURLConnection;
        int i2;
        long j;
        HttpURLConnection httpURLConnection2 = null;
        int i3 = -1;
        long currentTimeMillis = System.currentTimeMillis();
        try {
            HttpURLConnection httpURLConnection3 = (HttpURLConnection) new URL(SITE_YOUTUBE).openConnection();
            try {
                httpURLConnection3.setInstanceFollowRedirects(false);
                httpURLConnection3.setConnectTimeout(SOCKET_TIMEOUT_MS);
                httpURLConnection3.setReadTimeout(SOCKET_TIMEOUT_MS);
                httpURLConnection3.setUseCaches(false);
                httpURLConnection3.getInputStream();
                i3 = httpURLConnection3.getResponseCode();
                long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
                if (httpURLConnection3 != null) {
                    httpURLConnection3.disconnect();
                    i2 = i3;
                    j = currentTimeMillis2;
                } else {
                    i2 = i3;
                    j = currentTimeMillis2;
                }
            } catch (IOException e) {
                int i4 = i3;
                httpURLConnection = httpURLConnection3;
                i = i4;
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                    i2 = i;
                    j = currentTimeMillis;
                } else {
                    i2 = i;
                    j = currentTimeMillis;
                }
                if (i2 > 0) {
                }
                return null;
            } catch (Throwable th) {
                httpURLConnection2 = httpURLConnection3;
                th = th;
                if (httpURLConnection2 != null) {
                    httpURLConnection2.disconnect();
                }
                throw th;
            }
        } catch (IOException e2) {
            i = -1;
            httpURLConnection = null;
        } catch (Throwable th2) {
            th = th2;
        }
        if (i2 > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Network access after: ").append(j).append(" ms").append(", ResponseCode: ").append(i2);
            if (GlobalConfig.getAppContext() != null) {
                sb.append(", NetworkType: ").append(getNetworkTypeName(GlobalConfig.getAppContext()));
            }
            cdz.m20416(new IllegalStateException(sb.toString()));
        }
        return null;
    }

    private static void ensureNetCheckHost(Context context) {
        if (TextUtils.isEmpty(sNetCheckHost) || sNetCheckPort == 0) {
            SharedPreferences sharedPreferences = context.getSharedPreferences(GlobalConfig.PREF_CONTENT_CONFIG, 0);
            sNetCheckHost = sharedPreferences.getString(KEY_NET_CHECK_HOST, DEFAULT_NET_CHECK_IP);
            sNetCheckPort = sharedPreferences.getInt(KEY_NET_CHECK_PORT, 53);
        }
    }

    public static boolean isMobileNetworkConnected(Context context) {
        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getNetworkInfo(0);
        return networkInfo != null && networkInfo.isConnected();
    }

    public static boolean isWifiConnected(Context context) {
        NetworkInfo networkInfo;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null) {
            return false;
        }
        try {
            networkInfo = connectivityManager.getNetworkInfo(1);
        } catch (Exception e) {
            e.printStackTrace();
            networkInfo = null;
        }
        return networkInfo != null && networkInfo.isConnected();
    }

    private static InetAddress intToInetAddress(int i) {
        try {
            return InetAddress.getByAddress(new byte[]{(byte) (i & 255), (byte) ((i >> 8) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 24) & 255)});
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public static boolean checkWifiIsHotSpot(Context context) {
        DhcpInfo dhcpInfo;
        InetAddress intToInetAddress;
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        if (wifiManager == null || (dhcpInfo = wifiManager.getDhcpInfo()) == null || (intToInetAddress = intToInetAddress(dhcpInfo.gateway)) == null) {
            return false;
        }
        String hostAddress = intToInetAddress.getHostAddress();
        return TextUtils.equals(hostAddress, ANDROID_HOTSPOT_IP_ADDRESS) || TextUtils.equals(hostAddress, IOS_HOTSPOT_IP_ADDRESS);
    }

    public static boolean isReverseProxyOn() {
        return reverseProxyOn;
    }

    public static int getNetworkType() {
        try {
            return parseNetworkType(((ConnectivityManager) GlobalConfig.getAppContext().getSystemService("connectivity")).getActiveNetworkInfo());
        } catch (NullPointerException e) {
            e.printStackTrace();
            return -1;
        }
    }

    private static int parseNetworkType(NetworkInfo networkInfo) {
        if (networkInfo == null || !networkInfo.isConnected()) {
            return -1;
        }
        if (networkInfo.getType() == 0) {
            return 0;
        }
        return 1;
    }

    public static String getNetworkTypeName(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null) {
            return null;
        }
        try {
            return getNetworkTypeName(context, connectivityManager.getActiveNetworkInfo());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getNetworkTypeName(Context context, NetworkInfo networkInfo) {
        if (networkInfo == null) {
            if (reverseProxyOn) {
                return "PC";
            }
            return "NONE";
        } else if (networkInfo.getType() == 0) {
            return getNetworkTypeName(((TelephonyManager) context.getSystemService("phone")).getNetworkType());
        } else {
            return "WIFI";
        }
    }

    private static String getNetworkTypeName(int i) {
        switch (i) {
            case 1:
                return "GPRS";
            case 2:
                return "EDGE";
            case 3:
                return "UMTS";
            case 4:
                return "CDMA";
            case 5:
                return "CDMA - EvDo rev. 0";
            case 6:
                return "CDMA - EvDo rev. A";
            case 7:
                return "CDMA - 1xRTT";
            case 8:
                return "HSDPA";
            case 9:
                return "HSUPA";
            case 10:
                return "HSPA";
            case 11:
                return "iDEN";
            case 12:
                return "CDMA - EvDo rev. B";
            case 13:
                return "LTE";
            case 14:
                return "CDMA - eHRPD";
            case 15:
                return "HSPA+";
            default:
                return "UNKNOWN";
        }
    }

    public static String getNetworkCategoryName(Context context) {
        NetworkInfo activeNetworkInfo;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null) {
            return "INVALID";
        }
        try {
            activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (activeNetworkInfo.getType() == 1) {
            return "WiFi";
        }
        if (activeNetworkInfo.getType() == 0) {
            return getNetworkCategoryName(((TelephonyManager) context.getSystemService("phone")).getNetworkType());
        }
        return "INVALID";
    }

    private static String getNetworkCategoryName(int i) {
        switch (i) {
            case 1:
            case 2:
            case 4:
            case 7:
            case 11:
                return "2G";
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 14:
            case 15:
                return "3G";
            case 13:
                return "4G";
            default:
                return "UNKNOWN";
        }
    }

    public static boolean isFastMobileNetwork(Context context) {
        switch (((TelephonyManager) context.getSystemService("phone")).getNetworkType()) {
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 13:
            case 14:
            case 15:
                return true;
            case 4:
            case 7:
            case 11:
            default:
                return false;
        }
    }

    public static String getWifiIPAddress(Context context) {
        WifiInfo connectionInfo;
        int ipAddress;
        try {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService("wifi");
            if (wifiManager != null && (connectionInfo = wifiManager.getConnectionInfo()) != null && (ipAddress = connectionInfo.getIpAddress()) != 0) {
                return intToInetAddress(ipAddress).getHostAddress();
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    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()) {
                        return nextElement.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
        }
        return null;
    }

    public static String getLocalIpAddress(Context context) {
        if (isWifiConnected(context)) {
            return getWifiIPAddress(context);
        }
        if (isMobileNetworkConnected(context)) {
            return getMobileIpAddress();
        }
        return null;
    }

    public static boolean isNetworkStateEquals(NetworkInfo networkInfo, NetworkInfo networkInfo2) {
        if (networkInfo == networkInfo2) {
            return true;
        }
        if (networkInfo == null || networkInfo2 == null) {
            return false;
        }
        return networkInfo.isConnected() == networkInfo2.isConnected() && networkInfo.getType() == networkInfo2.getType() && networkInfo.getSubtype() == networkInfo2.getSubtype() && TextUtils.equals(networkInfo.getExtraInfo(), networkInfo2.getExtraInfo());
    }
}