小火龙传媒 v1.0.0版本的 MD5 值为:dd3a50c7559a03f984655651fce26f32

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


package im.ethwijlwwe.tgnet;

import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.SystemClock;
import android.text.TextUtils;
import com.google.android.exoplayer2.util.Log;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.king.zxing.util.LogUtils;
import com.litesuits.orm.db.assit.SQLBuilder;
import im.ethwijlwwe.messenger.AccountInstance;
import im.ethwijlwwe.messenger.AndroidUtilities;
import im.ethwijlwwe.messenger.ApplicationLoader;
import im.ethwijlwwe.messenger.BaseController;
import im.ethwijlwwe.messenger.BuildVars;
import im.ethwijlwwe.messenger.FileLog;
import im.ethwijlwwe.messenger.KeepAliveJob;
import im.ethwijlwwe.messenger.LocaleController;
import im.ethwijlwwe.messenger.MessagesController;
import im.ethwijlwwe.messenger.NotificationCenter;
import im.ethwijlwwe.messenger.SharedConfig;
import im.ethwijlwwe.messenger.StatsController;
import im.ethwijlwwe.messenger.Utilities;
import im.ethwijlwwe.tgnet.ConnectionsManager;
import im.ethwijlwwe.tgnet.TLRPC;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONArray;
import org.json.JSONObject;

public class ConnectionsManager extends BaseController {
    private static final int CORE_POOL_SIZE;
    public static final int CPU_COUNT;
    public static final int ConnectionStateConnected = 3;
    public static final int ConnectionStateConnecting = 1;
    public static final int ConnectionStateConnectingToProxy = 4;
    public static final int ConnectionStateUpdating = 5;
    public static final int ConnectionStateWaitingForNetwork = 2;
    public static final int ConnectionTypeDownload = 2;
    public static final int ConnectionTypeDownload2 = 65538;
    public static final int ConnectionTypeGeneric = 1;
    public static final int ConnectionTypePush = 8;
    public static final int ConnectionTypeUpload = 4;
    public static final int DEFAULT_DATACENTER_ID = Integer.MAX_VALUE;
    public static final Executor DNS_THREAD_POOL_EXECUTOR;
    public static final int FileTypeAudio = 50331648;
    public static final int FileTypeFile = 67108864;
    public static final int FileTypePhoto = 16777216;
    public static final int FileTypeVideo = 33554432;
    private static volatile ConnectionsManager[] Instance = null;
    private static final int KEEP_ALIVE_SECONDS = 30;
    private static final int MAXIMUM_POOL_SIZE;
    public static final int RequestFlagCanCompress = 4;
    public static final int RequestFlagEnableUnauthorized = 1;
    public static final int RequestFlagFailOnServerErrors = 2;
    public static final int RequestFlagForceDownload = 32;
    public static final int RequestFlagInvokeAfter = 64;
    public static final int RequestFlagNeedQuickAck = 128;
    public static final int RequestFlagTryDifferentDc = 16;
    public static final int RequestFlagWithoutLogin = 8;
    private static AsyncTask currentTask;
    private static HashMap<String, ResolvedDomain> dnsCache;
    private static int lastClassGuid;
    private static long lastDnsRequestTime;
    private static HashMap<String, ResolveHostByNameTask> resolvingHostnameTasks = new HashMap<>();
    private static final BlockingQueue<Runnable> sPoolWorkQueue;
    private static final ThreadFactory sThreadFactory;
    private boolean appPaused;
    private int appResumeCount;
    private int connectionState;
    private String currentAddress;
    private boolean isUpdating;
    private long lastPauseTime;
    private AtomicInteger lastRequestToken;

    public static native void native_applyBackupConfig(int i, long j);

    public static native void native_applyBackupIp(int i, String str, int i2, int i3);

    public static native void native_applyDatacenterAddress(int i, int i2, String str, int i3);

    public static native void native_applyDnsConfig(int i, long j, String str, int i2);

    public static native void native_bindRequestToGuid(int i, int i2, int i3);

    public static native void native_cancelRequest(int i, int i2, boolean z);

    public static native void native_cancelRequestsForGuid(int i, int i2);

    public static native long native_checkProxy(int i, String str, int i2, String str2, String str3, String str4, RequestTimeDelegate requestTimeDelegate);

    public static native void native_cleanUp(int i, boolean z);

    public static native long native_getAuthKeyId(int i);

    public static native int native_getConnectionState(int i);

    public static native int native_getCurrentTime(int i);

    public static native long native_getCurrentTimeMillis(int i);

    public static native int native_getTimeDifference(int i);

    public static native void native_init(int i, int i2, int i3, int i4, String str, String str2, String str3, String str4, String str5, String str6, String str7, String str8, int i5, boolean z, boolean z2, int i6, String str9);

    public static native int native_isTestBackend(int i);

    public static native void native_onHostNameResolved(String str, long j, String str2);

    public static native void native_pauseNetwork(int i);

    public static native void native_resumeNetwork(int i, boolean z);

    public static native void native_seSystemLangCode(int i, String str);

    public static native void native_sendRequest(int i, long j, RequestDelegateInternal requestDelegateInternal, QuickAckDelegate quickAckDelegate, WriteToSocketDelegate writeToSocketDelegate, int i2, int i3, int i4, boolean z, int i5);

    public static native void native_setIpPortDefaultAddress(int i, String str, int i2);

    public static native void native_setJava(boolean z);

    public static native void native_setLangCode(int i, String str);

    public static native void native_setNetworkAvailable(int i, boolean z, int i2, boolean z2);

    public static native void native_setProxySettings(int i, String str, int i2, String str2, String str3, String str4);

    public static native void native_setPushConnectionEnabled(int i, boolean z);

    public static native void native_setRegId(int i, String str);

    public static native void native_setSystemLangCode(int i, String str);

    public static native void native_setUseIpv6(int i, boolean z);

    public static native void native_setUserId(int i, int i2);

    public static native void native_switchBackend(int i);

    public static native void native_updateDcSettings(int i);

    static {
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        CPU_COUNT = availableProcessors;
        CORE_POOL_SIZE = Math.max(2, Math.min(availableProcessors - 1, 4));
        MAXIMUM_POOL_SIZE = (CPU_COUNT * 2) + 1;
        sPoolWorkQueue = new LinkedBlockingQueue(128);
        sThreadFactory = new ThreadFactory() {
            private final AtomicInteger mCount = new AtomicInteger(1);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "DnsAsyncTask #" + this.mCount.getAndIncrement());
            }
        };
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, 30L, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        DNS_THREAD_POOL_EXECUTOR = threadPoolExecutor;
        dnsCache = new HashMap<>();
        lastClassGuid = 1;
        Instance = new ConnectionsManager[3];
    }

    public static class ResolvedDomain {
        public ArrayList<String> addresses;
        long ttl;

        public ResolvedDomain(ArrayList<String> a, long t) {
            this.addresses = a;
            this.ttl = t;
        }

        public String getAddress() {
            return this.addresses.get(Utilities.random.nextInt(this.addresses.size()));
        }
    }

    public static ConnectionsManager getInstance(int num) {
        ConnectionsManager localInstance = Instance[num];
        if (localInstance == null) {
            synchronized (ConnectionsManager.class) {
                localInstance = Instance[num];
                if (localInstance == null) {
                    ConnectionsManager[] connectionsManagerArr = Instance;
                    ConnectionsManager connectionsManager = new ConnectionsManager(num);
                    localInstance = connectionsManager;
                    connectionsManagerArr[num] = connectionsManager;
                }
            }
        }
        return localInstance;
    }

    public ConnectionsManager(int instance) {
        super(instance);
        File config;
        String appVersion;
        String systemVersion;
        String systemLangCode;
        String langCode;
        String langCode2;
        String deviceModel;
        String appVersion2;
        String systemVersion2;
        String pushString;
        this.lastPauseTime = System.currentTimeMillis();
        this.appPaused = true;
        this.lastRequestToken = new AtomicInteger(1);
        if (BuildVars.DEBUG_VERSION) {
            FileLog.d("ConnectionsManager.java ===> constructor , currentAccount=" + this.currentAccount + " ,newAccount=" + instance);
        }
        this.connectionState = native_getConnectionState(this.currentAccount);
        File config2 = ApplicationLoader.getFilesDirFixed();
        if (instance == 0) {
            config = config2;
        } else {
            File config3 = new File(config2, "account" + instance);
            config3.mkdirs();
            config = config3;
        }
        String configPath = config.toString();
        SharedPreferences preferences = MessagesController.getGlobalNotificationsSettings();
        boolean enablePushConnection = preferences.getBoolean("pushConnection", true);
        try {
            systemLangCode = LocaleController.getSystemLocaleStringIso639().toLowerCase();
            String langCode3 = LocaleController.getLocaleStringIso639().toLowerCase();
            langCode2 = Build.MANUFACTURER + Build.MODEL;
            PackageInfo pInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
            appVersion = pInfo.versionName + " (" + pInfo.versionCode + SQLBuilder.PARENTHESES_RIGHT;
            systemVersion = "SDK " + Build.VERSION.SDK_INT;
            langCode = langCode3;
        } catch (Exception e) {
            appVersion = "App version unknown";
            systemVersion = "SDK " + Build.VERSION.SDK_INT;
            systemLangCode = "en";
            langCode = "";
            langCode2 = "Android unknown";
        }
        systemLangCode = systemLangCode.trim().length() == 0 ? "en" : systemLangCode;
        if (langCode2.trim().length() != 0) {
            deviceModel = langCode2;
        } else {
            deviceModel = "Android unknown";
        }
        if (appVersion.trim().length() != 0) {
            appVersion2 = appVersion;
        } else {
            appVersion2 = "App version unknown";
        }
        if (systemVersion.trim().length() != 0) {
            systemVersion2 = systemVersion;
        } else {
            systemVersion2 = "SDK Unknown";
        }
        getUserConfig().loadConfig();
        String pushString2 = SharedConfig.pushString;
        if (TextUtils.isEmpty(pushString2) && !TextUtils.isEmpty(SharedConfig.pushStringStatus)) {
            pushString = SharedConfig.pushStringStatus;
        } else {
            pushString = pushString2;
        }
        init(BuildVars.BUILD_VERSION, 105, BuildVars.APP_ID, deviceModel, systemVersion2, appVersion2, langCode, systemLangCode, configPath, FileLog.getNetworkLogPath(), pushString, getUserConfig().getClientUserId(), enablePushConnection);
    }

    public long getCurrentTimeMillis() {
        return native_getCurrentTimeMillis(this.currentAccount);
    }

    public int getCurrentTime() {
        return native_getCurrentTime(this.currentAccount);
    }

    public int getTimeDifference() {
        return native_getTimeDifference(this.currentAccount);
    }

    public int sendRequest(TLObject object, RequestDelegate completionBlock) {
        return sendRequest(object, completionBlock, (QuickAckDelegate) null, 0);
    }

    public int sendRequest(TLObject object, RequestDelegate completionBlock, int flags) {
        return sendRequest(object, completionBlock, null, null, flags, Integer.MAX_VALUE, 1, true);
    }

    public int sendRequest(TLObject object, RequestDelegate completionBlock, int flags, int connetionType) {
        return sendRequest(object, completionBlock, null, null, flags, Integer.MAX_VALUE, connetionType, true);
    }

    public int sendRequest(TLObject object, RequestDelegate completionBlock, QuickAckDelegate quickAckBlock, int flags) {
        return sendRequest(object, completionBlock, quickAckBlock, null, flags, Integer.MAX_VALUE, 1, true);
    }

    public int sendRequest(final TLObject object, final RequestDelegate onComplete, final QuickAckDelegate onQuickAck, final WriteToSocketDelegate onWriteToSocket, final int flags, final int datacenterId, final int connetionType, final boolean immediate) {
        final int requestToken = this.lastRequestToken.getAndIncrement();
        Utilities.stageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ConnectionsManager.this.lambda$sendRequest$2$ConnectionsManager(object, requestToken, onComplete, onQuickAck, onWriteToSocket, flags, datacenterId, connetionType, immediate);
            }
        });
        return requestToken;
    }

    public void lambda$sendRequest$2$ConnectionsManager(final TLObject object, int requestToken, final RequestDelegate onComplete, QuickAckDelegate onQuickAck, WriteToSocketDelegate onWriteToSocket, int flags, int datacenterId, int connetionType, boolean immediate) {
        NativeByteBuffer buffer;
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("send request " + object + " with token = " + requestToken);
        }
        try {
            buffer = new NativeByteBuffer(object.getObjectSize());
            object.serializeToStream(buffer);
            object.freeResources();
        } catch (Exception e) {
            e = e;
        }
        try {
        } catch (Exception e2) {
            e = e2;
            FileLog.e(e);
            if (!BuildVars.LOGS_ENABLED) {
                FileLog.e("java request outer layer exception " + e.toString());
                return;
            }
            return;
        }
        try {
            native_sendRequest(this.currentAccount, buffer.address, new RequestDelegateInternal() {
                @Override
                public final void run(long j, int i, String str, int i2) {
                    ConnectionsManager.lambda$null$1(TLObject.this, onComplete, j, i, str, i2);
                }
            }, onQuickAck, onWriteToSocket, flags, datacenterId, connetionType, immediate, requestToken);
        } catch (Exception e3) {
            e = e3;
            FileLog.e(e);
            if (!BuildVars.LOGS_ENABLED) {
            }
        }
    }

    public static void lambda$null$1(TLObject object, final RequestDelegate onComplete, long response, int errorCode, String errorText, int networkType) {
        TLObject resp = null;
        TLRPC.TL_error error = null;
        try {
            if (response != 0) {
                NativeByteBuffer buff = NativeByteBuffer.wrap(response);
                buff.reused = true;
                resp = object.deserializeResponse(buff, buff.readInt32(true), true);
            } else if (errorText != null) {
                error = new TLRPC.TL_error();
                error.code = errorCode;
                error.text = errorText;
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.e(object + " got error " + error.code + " " + error.text);
                }
            }
            if (resp != null) {
                resp.networkType = networkType;
            }
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("java received " + resp + " error = " + error);
            }
            final TLObject finalResponse = resp;
            final TLRPC.TL_error finalError = error;
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ConnectionsManager.lambda$null$0(RequestDelegate.this, finalResponse, finalError);
                }
            });
        } catch (Exception e) {
            FileLog.e(e);
            if (BuildVars.LOGS_ENABLED) {
                FileLog.e("java parse inner layer exception " + e.toString());
            }
        }
    }

    public static void lambda$null$0(RequestDelegate onComplete, TLObject finalResponse, TLRPC.TL_error finalError) {
        onComplete.run(finalResponse, finalError);
        if (finalResponse != null) {
            finalResponse.freeResources();
        }
    }

    public void cancelRequest(int token, boolean notifyServer) {
        native_cancelRequest(this.currentAccount, token, notifyServer);
    }

    public void cleanup(boolean resetKeys) {
        native_cleanUp(this.currentAccount, resetKeys);
    }

    public void cancelRequestsForGuid(int guid) {
        native_cancelRequestsForGuid(this.currentAccount, guid);
    }

    public void bindRequestToGuid(int requestToken, int guid) {
        native_bindRequestToGuid(this.currentAccount, requestToken, guid);
    }

    public void applyDatacenterAddress(int datacenterId, String ipAddress, int port) {
        this.currentAddress = ipAddress + LogUtils.COLON + port;
        native_applyDatacenterAddress(this.currentAccount, datacenterId, ipAddress, port);
    }

    public int getConnectionState() {
        if (this.connectionState == 3 && this.isUpdating) {
            return 5;
        }
        return this.connectionState;
    }

    public void setUserId(int id) {
        native_setUserId(this.currentAccount, id);
    }

    public void checkConnection() {
        native_setUseIpv6(this.currentAccount, useIpv6Address());
        native_setNetworkAvailable(this.currentAccount, ApplicationLoader.isNetworkOnline(), ApplicationLoader.getCurrentNetworkType(), ApplicationLoader.isConnectionSlow());
    }

    public void setPushConnectionEnabled(boolean value) {
        native_setPushConnectionEnabled(this.currentAccount, value);
    }

    public void init(int version, int layer, int apiId, String deviceModel, String systemVersion, String appVersion, String langCode, String systemLangCode, String configPath, String logPath, String regId, int userId, boolean enablePushConnection) {
        SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", 0);
        String proxyAddress = preferences.getString("proxy_ip", "");
        String proxyUsername = preferences.getString("proxy_user", "");
        String proxyPassword = preferences.getString("proxy_pass", "");
        String proxySecret = preferences.getString("proxy_secret", "");
        int proxyPort = preferences.getInt("proxy_port", 1080);
        if (preferences.getBoolean("proxy_enabled", false) && !TextUtils.isEmpty(proxyAddress)) {
            native_setProxySettings(this.currentAccount, proxyAddress, proxyPort, proxyUsername, proxyPassword, proxySecret);
        }
        native_init(this.currentAccount, version, layer, apiId, deviceModel, systemVersion, appVersion, langCode, systemLangCode, configPath, logPath, regId, userId, enablePushConnection, ApplicationLoader.isNetworkOnline(), ApplicationLoader.getCurrentNetworkType(), "Kccc");
        checkConnection();
    }

    public static void setLangCode(String langCode) {
        String langCode2 = langCode.replace('_', '-').toLowerCase();
        for (int a = 0; a < 3; a++) {
            native_setLangCode(a, langCode2);
        }
    }

    public static void setRegId(String regId, String status) {
        String pushString = regId;
        if (TextUtils.isEmpty(pushString) && !TextUtils.isEmpty(status)) {
            pushString = status;
        }
        for (int a = 0; a < 3; a++) {
            native_setRegId(a, pushString);
        }
    }

    public static void setSystemLangCode(String langCode) {
        String langCode2 = langCode.replace('_', '-').toLowerCase();
        for (int a = 0; a < 3; a++) {
            native_setSystemLangCode(a, langCode2);
        }
    }

    public void switchBackend() {
        SharedPreferences preferences = MessagesController.getGlobalMainSettings();
        preferences.edit().remove("language_showed2").commit();
        native_switchBackend(this.currentAccount);
    }

    public void resumeNetworkMaybe() {
        native_resumeNetwork(this.currentAccount, true);
    }

    public void updateDcSettings() {
        native_updateDcSettings(this.currentAccount);
    }

    public long getPauseTime() {
        return this.lastPauseTime;
    }

    public long checkProxy(String address, int port, String username, String password, String secret, RequestTimeDelegate requestTimeDelegate) {
        if (TextUtils.isEmpty(address)) {
            return 0L;
        }
        if (address == null) {
            address = "";
        }
        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }
        if (secret == null) {
            secret = "";
        }
        return native_checkProxy(this.currentAccount, address, port, username, password, secret, requestTimeDelegate);
    }

    public void setAppPaused(boolean value, boolean byScreenState) {
        if (!byScreenState) {
            this.appPaused = value;
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("app paused = " + value);
            }
            if (value) {
                this.appResumeCount--;
            } else {
                this.appResumeCount++;
            }
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("app resume count " + this.appResumeCount);
            }
            if (this.appResumeCount < 0) {
                this.appResumeCount = 0;
            }
        }
        if (this.appResumeCount == 0) {
            if (this.lastPauseTime == 0) {
                this.lastPauseTime = System.currentTimeMillis();
            }
            native_pauseNetwork(this.currentAccount);
        } else {
            if (this.appPaused) {
                return;
            }
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("reset app pause time");
            }
            this.lastPauseTime = 0L;
            native_resumeNetwork(this.currentAccount, false);
        }
    }

    public static void onUnparsedMessageReceived(long address, final int currentAccount) {
        try {
            NativeByteBuffer buff = NativeByteBuffer.wrap(address);
            buff.reused = true;
            int constructor = buff.readInt32(true);
            final TLObject message = TLClassStore.Instance().TLdeserialize(buff, constructor, true);
            if (message instanceof TLRPC.Updates) {
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("java received " + message);
                }
                KeepAliveJob.finishJob();
                Utilities.stageQueue.postRunnable(new Runnable() {
                    @Override
                    public final void run() {
                        AccountInstance.getInstance(currentAccount).getMessagesController().processUpdates((TLRPC.Updates) message, false);
                    }
                });
                return;
            }
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d(String.format("java received unknown constructor 0x%x", Integer.valueOf(constructor)));
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void onUpdate(final int currentAccount) {
        Utilities.stageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                AccountInstance.getInstance(currentAccount).getMessagesController().updateTimerProc();
            }
        });
    }

    public static void onSessionCreated(final int currentAccount) {
        Utilities.stageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                AccountInstance.getInstance(currentAccount).getMessagesController().getDifference();
            }
        });
    }

    public static void onConnectionStateChanged(final int state, final int currentAccount) {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                ConnectionsManager.lambda$onConnectionStateChanged$6(currentAccount, state);
            }
        });
    }

    public static void lambda$onConnectionStateChanged$6(int currentAccount, int state) {
        getInstance(currentAccount).connectionState = state;
        AccountInstance.getInstance(currentAccount).getNotificationCenter().postNotificationName(NotificationCenter.didUpdateConnectionState, new Object[0]);
    }

    public static void onLogout(final int currentAccount) {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                ConnectionsManager.lambda$onLogout$7(currentAccount);
            }
        });
    }

    public static void lambda$onLogout$7(int currentAccount) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("ConnectionManager.java received signal of logout.");
        }
        AccountInstance accountInstance = AccountInstance.getInstance(currentAccount);
        if (accountInstance.getUserConfig().getClientUserId() != 0) {
            accountInstance.getUserConfig().clearConfig();
            accountInstance.getMessagesController().performLogout(0);
        }
    }

    public static int getInitFlags() {
        return 0;
    }

    public static void onBytesSent(int amount, int networkType, int currentAccount) {
        try {
            AccountInstance.getInstance(currentAccount).getStatsController().incrementSentBytesCount(networkType, 6, amount);
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void onRequestNewServerIpAndPort(final int second, final int currentAccount) {
        Utilities.stageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ConnectionsManager.lambda$onRequestNewServerIpAndPort$8(second, currentAccount);
            }
        });
    }

    public static void lambda$onRequestNewServerIpAndPort$8(int second, int currentAccount) {
        if (currentTask != null || ((second == 0 && Math.abs(lastDnsRequestTime - System.currentTimeMillis()) < 6000) || !ApplicationLoader.isNetworkOnline())) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("don't start task, current task = " + currentTask + " next task = " + second + " time diff = " + Math.abs(lastDnsRequestTime - System.currentTimeMillis()) + " network = " + ApplicationLoader.isNetworkOnline());
                return;
            }
            return;
        }
        lastDnsRequestTime = System.currentTimeMillis();
        if (second == 1) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("start dns txt task");
            }
            DnsTxtLoadTask task = new DnsTxtLoadTask(currentAccount);
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
            currentTask = task;
            return;
        }
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("start firebase task");
        }
        FirebaseTask task2 = new FirebaseTask(currentAccount);
        task2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
        currentTask = task2;
    }

    public static void onProxyError() {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.needShowAlert, 3);
            }
        });
    }

    public static void getHostByName(final String hostName, final long address) {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                ConnectionsManager.lambda$getHostByName$10(hostName, address);
            }
        });
    }

    public static void lambda$getHostByName$10(String hostName, long address) {
        ResolvedDomain resolvedDomain = dnsCache.get(hostName);
        if (resolvedDomain != null && SystemClock.elapsedRealtime() - resolvedDomain.ttl < 300000) {
            native_onHostNameResolved(hostName, address, resolvedDomain.getAddress());
            return;
        }
        ResolveHostByNameTask task = resolvingHostnameTasks.get(hostName);
        if (task == null) {
            task = new ResolveHostByNameTask(hostName);
            try {
                task.executeOnExecutor(DNS_THREAD_POOL_EXECUTOR, null, null, null);
                resolvingHostnameTasks.put(hostName, task);
            } catch (Throwable e) {
                FileLog.e(e);
                native_onHostNameResolved(hostName, address, "");
                return;
            }
        }
        task.addAddress(address);
    }

    public static void onBytesReceived(int amount, int networkType, int currentAccount) {
        try {
            StatsController.getInstance(currentAccount).incrementReceivedBytesCount(networkType, 6, amount);
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void onUpdateConfig(long address, final int currentAccount) {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                ConnectionsManager.lambda$onUpdateConfig$11(currentAccount);
            }
        });
        try {
            NativeByteBuffer buff = NativeByteBuffer.wrap(address);
            buff.reused = true;
            final TLRPC.TL_config message = TLRPC.TL_config.TLdeserialize(buff, buff.readInt32(true), true);
            if (message != null) {
                Utilities.stageQueue.postRunnable(new Runnable() {
                    @Override
                    public final void run() {
                        AccountInstance.getInstance(currentAccount).getMessagesController().updateConfig(message);
                    }
                });
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void lambda$onUpdateConfig$11(int currentAccount) {
        NetworkConfig.serverIndex = 3;
        NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.getBackupIpStatus, "server 3");
    }

    public static void onInternalPushReceived(int currentAccount) {
        KeepAliveJob.startJob();
    }

    public static void setProxySettings(boolean enabled, String address, int port, String username, String password, String secret) {
        if (address == null) {
            address = "";
        }
        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }
        if (secret == null) {
            secret = "";
        }
        for (int a = 0; a < 3; a++) {
            if (enabled && !TextUtils.isEmpty(address)) {
                native_setProxySettings(a, address, port, username, password, secret);
            } else {
                native_setProxySettings(a, "", 1080, "", "", "");
            }
            AccountInstance accountInstance = AccountInstance.getInstance(a);
            if (accountInstance.getUserConfig().isClientActivated()) {
                accountInstance.getMessagesController().checkProxyInfo(true);
            }
        }
    }

    public long getAuthKeyId(int currentAccount) {
        return native_getAuthKeyId(currentAccount);
    }

    public static int generateClassGuid() {
        int i = lastClassGuid;
        lastClassGuid = i + 1;
        return i;
    }

    public void setIsUpdating(final boolean value) {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                ConnectionsManager.this.lambda$setIsUpdating$13$ConnectionsManager(value);
            }
        });
    }

    public void lambda$setIsUpdating$13$ConnectionsManager(boolean value) {
        if (this.isUpdating == value) {
            return;
        }
        this.isUpdating = value;
        if (this.connectionState == 3) {
            AccountInstance.getInstance(this.currentAccount).getNotificationCenter().postNotificationName(NotificationCenter.didUpdateConnectionState, new Object[0]);
        }
    }

    protected static boolean useIpv6Address() {
        boolean hasIpv4;
        boolean hasIpv6;
        if (Build.VERSION.SDK_INT < 19) {
            return false;
        }
        if (BuildVars.LOGS_ENABLED) {
            try {
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = networkInterfaces.nextElement();
                    if (networkInterface.isUp() && !networkInterface.isLoopback() && !networkInterface.getInterfaceAddresses().isEmpty()) {
                        if (BuildVars.LOGS_ENABLED) {
                            FileLog.d("valid interface: " + networkInterface);
                        }
                        List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
                        for (int a = 0; a < interfaceAddresses.size(); a++) {
                            InterfaceAddress address = interfaceAddresses.get(a);
                            InetAddress inetAddress = address.getAddress();
                            if (BuildVars.LOGS_ENABLED) {
                                FileLog.d("address: " + inetAddress.getHostAddress());
                            }
                            if (!inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress() && !inetAddress.isMulticastAddress() && BuildVars.LOGS_ENABLED) {
                                FileLog.d("address is good");
                            }
                        }
                    }
                }
            } catch (Throwable e) {
                FileLog.e(e);
            }
        }
        try {
            Enumeration<NetworkInterface> networkInterfaces2 = NetworkInterface.getNetworkInterfaces();
            hasIpv4 = false;
            hasIpv6 = false;
            while (networkInterfaces2.hasMoreElements()) {
                NetworkInterface networkInterface2 = networkInterfaces2.nextElement();
                if (networkInterface2.isUp() && !networkInterface2.isLoopback()) {
                    List<InterfaceAddress> interfaceAddresses2 = networkInterface2.getInterfaceAddresses();
                    for (int a2 = 0; a2 < interfaceAddresses2.size(); a2++) {
                        InterfaceAddress address2 = interfaceAddresses2.get(a2);
                        InetAddress inetAddress2 = address2.getAddress();
                        if (!inetAddress2.isLinkLocalAddress() && !inetAddress2.isLoopbackAddress() && !inetAddress2.isMulticastAddress()) {
                            if (inetAddress2 instanceof Inet6Address) {
                                hasIpv6 = true;
                            } else if (inetAddress2 instanceof Inet4Address) {
                                String addrr = inetAddress2.getHostAddress();
                                if (!addrr.startsWith("192.0.0.")) {
                                    hasIpv4 = true;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Throwable e2) {
            FileLog.e(e2);
        }
        return !hasIpv4 && hasIpv6;
    }

    public static class ResolveHostByNameTask extends AsyncTask<Void, Void, ResolvedDomain> {
        private ArrayList<Long> addresses = new ArrayList<>();
        private String currentHostName;

        public ResolveHostByNameTask(String hostName) {
            this.currentHostName = hostName;
        }

        public void addAddress(long address) {
            if (this.addresses.contains(Long.valueOf(address))) {
                return;
            }
            this.addresses.add(Long.valueOf(address));
        }

        @Override
        public ResolvedDomain doInBackground(Void... voids) {
            int read;
            JSONObject jsonObject;
            JSONArray array;
            int len;
            ByteArrayOutputStream outbuf = null;
            InputStream httpConnectionStream = null;
            boolean done = false;
            try {
                try {
                    URL downloadUrl = new URL("https://www.google.com/resolve?name=" + this.currentHostName + "&type=A");
                    URLConnection httpConnection = downloadUrl.openConnection();
                    httpConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A5297c Safari/602.1");
                    httpConnection.addRequestProperty("Host", "dns.google.com");
                    httpConnection.setConnectTimeout(1000);
                    httpConnection.setReadTimeout(2000);
                    httpConnection.connect();
                    httpConnectionStream = httpConnection.getInputStream();
                    outbuf = new ByteArrayOutputStream();
                    byte[] data = new byte[32768];
                    while (true) {
                        read = httpConnectionStream.read(data);
                        if (read <= 0) {
                            break;
                        }
                        outbuf.write(data, 0, read);
                    }
                    if (read == -1) {
                    }
                    jsonObject = new JSONObject(new String(outbuf.toByteArray()));
                } catch (Throwable e) {
                    try {
                        FileLog.e(e);
                        if (httpConnectionStream != null) {
                            try {
                                httpConnectionStream.close();
                            } catch (Throwable e2) {
                                FileLog.e(e2);
                            }
                        }
                        if (outbuf != null) {
                            outbuf.close();
                        }
                    } finally {
                    }
                }
            } catch (Exception e3) {
            }
            if (jsonObject.has("Answer") && (len = (array = jsonObject.getJSONArray("Answer")).length()) > 0) {
                ArrayList<String> addresses = new ArrayList<>(len);
                for (int a = 0; a < len; a++) {
                    addresses.add(array.getJSONObject(a).getString("data"));
                }
                ResolvedDomain resolvedDomain = new ResolvedDomain(addresses, SystemClock.elapsedRealtime());
                if (httpConnectionStream != null) {
                    try {
                        httpConnectionStream.close();
                    } catch (Throwable e4) {
                        FileLog.e(e4);
                    }
                }
                try {
                    outbuf.close();
                } catch (Exception e5) {
                }
                return resolvedDomain;
            }
            done = true;
            if (httpConnectionStream != null) {
                try {
                    httpConnectionStream.close();
                } catch (Throwable e6) {
                    FileLog.e(e6);
                }
            }
            outbuf.close();
            if (done) {
                return null;
            }
            try {
                InetAddress address = InetAddress.getByName(this.currentHostName);
                ArrayList<String> addresses2 = new ArrayList<>(1);
                addresses2.add(address.getHostAddress());
                return new ResolvedDomain(addresses2, SystemClock.elapsedRealtime());
            } catch (Exception e7) {
                FileLog.e(e7);
                return null;
            }
        }

        @Override
        public void onPostExecute(ResolvedDomain result) {
            if (result != null) {
                ConnectionsManager.dnsCache.put(this.currentHostName, result);
                int N = this.addresses.size();
                for (int a = 0; a < N; a++) {
                    ConnectionsManager.native_onHostNameResolved(this.currentHostName, this.addresses.get(a).longValue(), result.getAddress());
                }
            } else {
                int N2 = this.addresses.size();
                for (int a2 = 0; a2 < N2; a2++) {
                    ConnectionsManager.native_onHostNameResolved(this.currentHostName, this.addresses.get(a2).longValue(), "");
                }
            }
            ConnectionsManager.resolvingHostnameTasks.remove(this.currentHostName);
        }
    }

    public static class DnsTxtLoadTask extends AsyncTask<Void, Void, NativeByteBuffer> {
        private int currentAccount;
        private int responseDate;

        public DnsTxtLoadTask(int instance) {
            this.currentAccount = instance;
        }

        @Override
        public NativeByteBuffer doInBackground(Void... voids) {
            if (BuildVars.DEBUG_VERSION) {
                Log.i("connection", "java DnsTxtLoadTask doInBackground ===> ");
            }
            NetworkConfig.getInstance().applyNetconfig(this.currentAccount);
            return null;
        }

        @Override
        public void onPostExecute(final NativeByteBuffer result) {
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ConnectionsManager.DnsTxtLoadTask.this.lambda$onPostExecute$0$ConnectionsManager$DnsTxtLoadTask(result);
                }
            });
        }

        public void lambda$onPostExecute$0$ConnectionsManager$DnsTxtLoadTask(NativeByteBuffer result) {
            if (result != null) {
                ConnectionsManager.native_applyBackupConfig(this.currentAccount, result.address);
            } else if (BuildVars.LOGS_ENABLED) {
                FileLog.d("failed to get dns txt result");
            }
            AsyncTask unused = ConnectionsManager.currentTask = null;
        }
    }

    public static class FirebaseTask extends AsyncTask<Void, Void, NativeByteBuffer> {
        private int currentAccount;
        private FirebaseRemoteConfig firebaseRemoteConfig;

        public FirebaseTask(int instance) {
            this.currentAccount = instance;
        }

        @Override
        public NativeByteBuffer doInBackground(Void... voids) {
            if (BuildVars.DEBUG_VERSION) {
                Log.i("connection", "java FirebaseTask doInBackground ===> ");
            }
            NetworkConfig.getInstance().applyNetconfig(this.currentAccount);
            return null;
        }

        @Override
        public void onPostExecute(final NativeByteBuffer result) {
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ConnectionsManager.FirebaseTask.this.lambda$onPostExecute$0$ConnectionsManager$FirebaseTask(result);
                }
            });
        }

        public void lambda$onPostExecute$0$ConnectionsManager$FirebaseTask(NativeByteBuffer result) {
            if (result != null) {
                ConnectionsManager.native_applyBackupConfig(this.currentAccount, result.address);
            } else if (BuildVars.LOGS_ENABLED) {
                FileLog.d("failed to get dns txt result");
            }
            AsyncTask unused = ConnectionsManager.currentTask = null;
        }
    }
}