龙岗云视频 v2.7版本的 MD5 值为:9298cfa7a85a45936abaf845b6f6bd5c

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


package com.videogo.main;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.SystemClock;
import android.text.TextUtils;
import com.amap.api.services.district.DistrictSearchQuery;
import com.ezviz.jna.EZStreamSDKJNA;
import com.ezviz.stream.EZStreamClientManager;
import com.ezviz.stream.JsonUtils;
import com.hik.CASClient.CASClient;
import com.hik.stunclient.StunClient;
import com.hikvision.dxopensdk.constants.DX_HttpConstants;
import com.videogo.constant.RobolectricConfig;
import com.videogo.debug.TestParams;
import com.videogo.exception.BaseException;
import com.videogo.ezdclog.EZDcLogManager;
import com.videogo.ezdclog.params.BaseParams;
import com.videogo.ezdclog.params.EZLogLocalInfoParams;
import com.videogo.ezdclog.params.EZLogStreamDirectDetectParams;
import com.videogo.ezdclog.params.EZLogStreamP2pPreParams;
import com.videogo.openapi.EzvizAPI;
import com.videogo.openapi.bean.EZSDKConfiguration;
import com.videogo.openapi.bean.EZServerInfo;
import com.videogo.util.ConnectionDetector;
import com.videogo.util.HttpUtils;
import com.videogo.util.LocalInfo;
import com.videogo.util.LogUtil;
import com.videogo.util.Utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;

public class AppManager {
    public static int ISP_TYPE_DIANXIN = 0;
    public static int ISP_TYPE_HUASHU = 4;
    public static int ISP_TYPE_LIANTONG = 1;
    public static int ISP_TYPE_TIETONG = 3;
    public static int ISP_TYPE_YIDONG = 2;
    private static final String TAG = "AppManager";
    private static HashMap<String, String> mInetAddressMap = new HashMap<>();
    private static AppManager mInstance;
    private EZLogLocalInfoParams localInfoParams;
    private CASClient mCASClientSDK;
    private StunClient mStunClient;
    private volatile EZServerInfo mServerInfo = null;
    private volatile EZSDKConfiguration mEZSDKConfiguration = null;
    private StreamServerData mStreamerServerInfo = null;
    private int mNatType = 0;
    private String mNetIP = "";
    private int mISPType = -1;
    private String mISP = "";
    private String mISPAddress = null;
    private String mWifiMacAddress = null;

    private AppManager() {
        this.mCASClientSDK = null;
        this.mStunClient = null;
        if (!RobolectricConfig.ROBOLECRITIC_TEST) {
            this.mStunClient = StunClient.getInstance();
            this.mCASClientSDK = CASClient.getInstance();
            initLibs();
        }
        EZStreamClientManager.create(EzvizAPI.mApplication.getApplicationContext()).setGlobalListener(new EZStreamClientManager.OnGlobalListener() {
            @Override
            public void onData(int i) {
                LogUtil.d(AppManager.TAG, "onData ");
            }

            @Override
            public void onDevInfoUpdated(String str, EZStreamSDKJNA.EZ_DEV_INFO ez_dev_info) {
            }

            @Override
            public void onDirectPreConnectStatistics(String str, String str2) {
                LogUtil.d(AppManager.TAG, "onDirectPreConnectStatistics devSerial  = ".concat(String.valueOf(str2)));
                EZDcLogManager.getInstance().submit((BaseParams) new EZLogStreamDirectDetectParams(), str2);
            }

            @Override
            public void onEvent(String str, int i, String str2) {
                LogUtil.d(AppManager.TAG, "onEvent devSerial  = ".concat(String.valueOf(str)));
            }

            @Override
            public void onGlobalEventStatistics(int i, String str) {
                LogUtil.d(AppManager.TAG, "onGlobalEventStatistics szDevSerial");
            }

            @Override
            public void onP2PPreConnectStatistics(String str, String str2) {
                StringBuilder sb = new StringBuilder("onP2PPreConnectStatistics devSerial  = ");
                sb.append(TextUtils.isEmpty(str2) ? "" : str2);
                LogUtil.d(AppManager.TAG, sb.toString());
                EZLogStreamP2pPreParams eZLogStreamP2pPreParams = new EZLogStreamP2pPreParams();
                eZLogStreamP2pPreParams.cnt = AppManager.this.getNatType();
                EZDcLogManager.getInstance().submit((BaseParams) eZLogStreamP2pPreParams, str2);
            }

            @Override
            public void onPreconnectResult(String str, int i, boolean z) {
                LogUtil.d(AppManager.TAG, "onPreconnectResult szDevSerial");
            }

            @Override
            public void onReverseDirectUpnpStatistics(String str) {
            }
        });
        this.localInfoParams = new EZLogLocalInfoParams();
        this.localInfoParams.os = String.valueOf(Build.VERSION.RELEASE);
        this.localInfoParams.phoneType = Build.MODEL;
    }

    private static void cacheInetAddress(String str, String str2) {
        mInetAddressMap.put(str, str2 + "|" + System.currentTimeMillis());
    }

    public static boolean checkNetworkState(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null || connectivityManager.getNetworkInfo(0) == null) {
            return false;
        }
        NetworkInfo.State state = connectivityManager.getNetworkInfo(0).getState();
        boolean z = true;
        NetworkInfo.State state2 = connectivityManager.getNetworkInfo(1).getState();
        if (state != NetworkInfo.State.CONNECTED && state != NetworkInfo.State.CONNECTING) {
            z = false;
        }
        if (state2 == NetworkInfo.State.CONNECTED || state2 == NetworkInfo.State.CONNECTING) {
            return false;
        }
        return z;
    }

    private static String getCacheInetAddress(String str) {
        String[] split;
        String str2 = mInetAddressMap.get(str);
        if (!TextUtils.isEmpty(str2) && (split = str2.split("\\|")) != null && split.length == 2) {
            try {
                if (System.currentTimeMillis() - Long.parseLong(split[1]) < 86400000) {
                    return split[0];
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        mInetAddressMap.remove(str);
        return null;
    }

    public static String getInetAddress(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        String replace = str.replace(DX_HttpConstants.DX_REQ_URL_PREFIX_HTTP, "");
        String cacheInetAddress = getCacheInetAddress(replace);
        if (!TextUtils.isEmpty(cacheInetAddress)) {
            return cacheInetAddress;
        }
        String str2 = cacheInetAddress;
        InetAddress[] inetAddressArr = null;
        int i = 0;
        while (true) {
            if (i >= 2) {
                break;
            }
            i++;
            try {
                inetAddressArr = InetAddress.getAllByName(replace);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (inetAddressArr != null) {
                int length = inetAddressArr.length;
                String str3 = null;
                int i2 = 0;
                while (true) {
                    if (i2 >= length) {
                        break;
                    }
                    InetAddress inetAddress = inetAddressArr[i2];
                    if (!TextUtils.isEmpty(inetAddress.getHostAddress())) {
                        if (inetAddress instanceof Inet4Address) {
                            str2 = inetAddress.getHostAddress();
                            break;
                        }
                        if (str3 == null) {
                            str3 = inetAddress.getHostAddress();
                        }
                    }
                    i2++;
                }
                if (TextUtils.isEmpty(str2) && str3 != null) {
                    str2 = str3;
                    break;
                }
            }
        }
        if (TextUtils.isEmpty(str2)) {
            return null;
        }
        cacheInetAddress(replace, str2);
        return str2;
    }

    public static synchronized AppManager getInstance() {
        AppManager appManager;
        synchronized (AppManager.class) {
            if (mInstance == null) {
                mInstance = new AppManager();
            }
            appManager = mInstance;
        }
        return appManager;
    }

    private String getLocalIpAddress() {
        String wifiIpAddress = ConnectionDetector.getConnectionType(EzvizAPI.mApplication) == 3 ? getWifiIpAddress(EzvizAPI.mApplication) : get3GIpAddress();
        if (wifiIpAddress == null) {
            try {
                wifiIpAddress = new Socket(LocalInfo.getInstance().getServAddr(), 80).getLocalAddress().getHostAddress();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return wifiIpAddress == null ? "172.0.0.1" : wifiIpAddress;
    }

    public static String getNetworkIp() {
        HttpURLConnection httpURLConnection;
        InputStream inputStream;
        InputStream inputStream2;
        IOException e;
        MalformedURLException e2;
        String str = "";
        try {
            try {
                try {
                    httpURLConnection = (HttpURLConnection) new URL("http://city.ip138.com/ip2city.asp").openConnection();
                } catch (Throwable th) {
                    th = th;
                }
            } catch (MalformedURLException e3) {
                inputStream2 = null;
                e2 = e3;
                httpURLConnection = null;
            } catch (IOException e4) {
                inputStream2 = null;
                e = e4;
                httpURLConnection = null;
            } catch (Throwable th2) {
                th = th2;
                httpURLConnection = null;
                inputStream = null;
            }
            try {
                if (httpURLConnection.getResponseCode() == 200) {
                    inputStream2 = httpURLConnection.getInputStream();
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream2, "gbk"));
                        StringBuilder sb = new StringBuilder();
                        while (true) {
                            String readLine = bufferedReader.readLine();
                            if (readLine == null) {
                                break;
                            }
                            sb.append(readLine + "\n");
                        }
                        Matcher matcher = Pattern.compile("(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}").matcher(sb.toString());
                        if (matcher.find()) {
                            str = matcher.group();
                        }
                    } catch (MalformedURLException e5) {
                        e2 = e5;
                        LogUtil.printErrStackTrace(TAG, e2.fillInStackTrace());
                        if (inputStream2 != null) {
                            inputStream2.close();
                        }
                        if (httpURLConnection != null) {
                            httpURLConnection.disconnect();
                        }
                        return str;
                    } catch (IOException e6) {
                        e = e6;
                        LogUtil.printErrStackTrace(TAG, e.fillInStackTrace());
                        if (inputStream2 != null) {
                            inputStream2.close();
                        }
                        if (httpURLConnection != null) {
                            httpURLConnection.disconnect();
                        }
                        return str;
                    }
                } else {
                    inputStream2 = null;
                }
                if (inputStream2 != null) {
                    inputStream2.close();
                }
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
            } catch (MalformedURLException e7) {
                inputStream2 = null;
                e2 = e7;
            } catch (IOException e8) {
                inputStream2 = null;
                e = e8;
            } catch (Throwable th3) {
                th = th3;
                inputStream = null;
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e9) {
                        LogUtil.printErrStackTrace(TAG, e9.fillInStackTrace());
                        throw th;
                    }
                }
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
                throw th;
            }
        } catch (IOException e10) {
            LogUtil.printErrStackTrace(TAG, e10.fillInStackTrace());
        }
        return str;
    }

    private StreamServerData getStreamServerData() {
        if (this.mStreamerServerInfo == null) {
            int iSPType = getISPType();
            if (iSPType == -1) {
                getNetworkISPInfo();
                iSPType = getISPType();
            }
            this.mStreamerServerInfo = EzvizAPI.getInstance().getStreamServer(iSPType);
        }
        return this.mStreamerServerInfo;
    }

    private 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) {
            LogUtil.printErrStackTrace(TAG, e.fillInStackTrace());
            return null;
        }
    }

    public void clearAllStreamServer() {
        this.mStreamerServerInfo = null;
    }

    public void clearServerInfo() {
        this.mServerInfo = null;
    }

    public void finiLibs() {
        try {
            StunClient.getInstance().finit();
        } catch (Exception unused) {
        }
    }

    public String get3GIpAddress() {
        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() && Utils.isIp(nextElement.getHostAddress())) {
                        return nextElement.getHostAddress();
                    }
                }
            }
            return null;
        } catch (NullPointerException e) {
            e.printStackTrace();
            return null;
        } catch (OutOfMemoryError e2) {
            e2.printStackTrace();
            return null;
        } catch (SocketException e3) {
            LogUtil.errorLog(TAG, "WifiPreference IpAddress " + e3.toString());
            return null;
        } catch (Exception e4) {
            e4.printStackTrace();
            return null;
        }
    }

    public void getAllStreamServer() {
        getStreamServerData();
    }

    public CASClient getCASClientSDKInstance() {
        return this.mCASClientSDK;
    }

    public synchronized EZSDKConfiguration getEZSDKConfiguration(boolean z) {
        if (this.mEZSDKConfiguration == null || (this.mEZSDKConfiguration != null && z)) {
            int i = 0;
            while (i <= 3) {
                try {
                    this.mEZSDKConfiguration = EzvizAPI.getInstance().getConfiguration();
                    if (this.mEZSDKConfiguration == null) {
                        break;
                    }
                    EZDcLogManager.getInstance().setNeedUpLoadLog(this.mEZSDKConfiguration.getDataCollect() != 0);
                    break;
                } catch (BaseException e) {
                    LogUtil.printErrStackTrace(TAG, e.fillInStackTrace());
                    i++;
                    SystemClock.sleep(i * 5);
                }
            }
        }
        return this.mEZSDKConfiguration;
    }

    public void getEZSDKConfigurationSyn() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (AppManager.this.mEZSDKConfiguration == null) {
                    AppManager.this.getEZSDKConfiguration(false);
                }
            }
        }).start();
    }

    public String getISP() {
        return this.mISP;
    }

    public String getISPAddress() {
        return this.mISPAddress;
    }

    public int getISPType() {
        return this.mISPType;
    }

    public int getNatType() {
        return this.mNatType;
    }

    public String getNetIP() {
        return this.mNetIP;
    }

    public void getNetworkISPInfo() {
        String netIP = getNetIP();
        if (TextUtils.isEmpty(netIP)) {
            return;
        }
        String sendPostRequest = HttpUtils.sendPostRequest("http://ip.taobao.com/service/getIpInfo.php?ip=".concat(String.valueOf(netIP)));
        LogUtil.debugLog("getNetworkISP", "netIp=".concat(String.valueOf(netIP)));
        if (sendPostRequest == null) {
            this.mISPType = 0;
            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(sendPostRequest);
            if (jSONObject.optInt("code") == 1) {
                this.mISPType = ISP_TYPE_DIANXIN;
            }
            String optString = jSONObject.optString("data");
            if (optString == null) {
                this.mISPType = ISP_TYPE_DIANXIN;
            }
            JSONObject jSONObject2 = new JSONObject(optString);
            this.mISP = jSONObject2.getString("isp");
            this.mISPType = this.mISP == null ? ISP_TYPE_DIANXIN : this.mISP.equals("联通") ? ISP_TYPE_LIANTONG : this.mISP.equals("移动") ? ISP_TYPE_YIDONG : this.mISP.equals("铁通") ? ISP_TYPE_TIETONG : this.mISP.equals("华数") ? ISP_TYPE_HUASHU : ISP_TYPE_DIANXIN;
            this.mISPAddress = jSONObject2.getString("region") + jSONObject2.getString(DistrictSearchQuery.KEYWORDS_CITY);
        } catch (JSONException e) {
            LogUtil.printErrStackTrace(TAG, e.fillInStackTrace());
        }
    }

    public synchronized EZServerInfo getServerInfo() {
        if (this.mServerInfo == null) {
            this.mServerInfo = EzvizAPI.getInstance().getServerInfo();
            if (this.mServerInfo != null) {
                LogUtil.d(TAG, "getServerInfo  = " + JsonUtils.toJson(this.mServerInfo));
                EZDcLogManager.getInstance().setEZLOG_URL(TestParams.isUse() ? TestParams.LOG_URL : this.mServerInfo.getLogAddr());
                if (this.localInfoParams != null) {
                    EZDcLogManager.getInstance().submit(this.localInfoParams);
                    this.localInfoParams = null;
                }
            }
            refreshNetInfo();
        }
        return this.mServerInfo;
    }

    public void getServerInfoSyn() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    AppManager.this.getServerInfo();
                } catch (BaseException e) {
                    LogUtil.printErrStackTrace(AppManager.TAG, e.fillInStackTrace());
                }
            }
        }).start();
    }

    public String getWifiMacAddress() {
        return this.mWifiMacAddress;
    }

    public void initLibs() {
        try {
            StunClient.getInstance().init();
        } catch (UnsatisfiedLinkError e) {
            LogUtil.printErrStackTrace(TAG, e.fillInStackTrace());
        }
    }

    public void refreshNetInfo() {
        if (this.mServerInfo == null) {
            this.mServerInfo = EzvizAPI.getInstance().getServerInfo();
        }
        if (this.mServerInfo != null) {
            refreshNetTypeSyn();
        }
    }

    public void refreshNetType() {
        try {
            if (this.mServerInfo == null || TextUtils.isEmpty(this.mServerInfo.getStun1Addr()) || TextUtils.isEmpty(this.mServerInfo.getStun2Addr()) || this.mStunClient == null) {
                LogUtil.errorLog(TAG, "refreshNetInfo fail");
                return;
            }
            int nATType = this.mStunClient.getNATType(getLocalIpAddress(), this.mServerInfo.getStun1Addr(), (short) this.mServerInfo.getStun1Port(), this.mServerInfo.getStun2Addr(), (short) this.mServerInfo.getStun2Port());
            LogUtil.infoLog(TAG, "网络变更, 客户端nat类型改变");
            LogUtil.infoLog(TAG, "网络变更前:" + this.mNatType);
            LogUtil.infoLog(TAG, "网络变更后:".concat(String.valueOf(nATType)));
            setNatType(nATType);
            setNetIP(this.mStunClient.getNATIP());
            if (TextUtils.isEmpty(this.mNetIP)) {
                setNetIP(getNetworkIp());
            }
        } catch (Exception e) {
            LogUtil.printErrStackTrace(TAG, e.fillInStackTrace());
        }
    }

    public void refreshNetTypeSyn() {
        if (this.mServerInfo == null) {
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                AppManager.this.refreshNetType();
                try {
                    EZStreamClientManager.create(EzvizAPI.mApplication.getApplicationContext()).setP2PPublicParam(AppManager.this.getNatType());
                } catch (Exception e) {
                    LogUtil.printErrStackTrace(AppManager.TAG, e.fillInStackTrace());
                }
            }
        }).start();
    }

    public void setISP(String str) {
        this.mISP = str;
    }

    public void setNatType(int i) {
        this.mNatType = i;
    }

    public void setNetIP(String str) {
        this.mNetIP = str;
    }

    public void setWifiMacAddress(String str) {
        this.mWifiMacAddress = str;
    }
}