盛康基办.apk(点击下载) / ConnectionsManager.java


package im.guobwnxjuc.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.litesuits.orm.db.assit.SQLBuilder;
import im.guobwnxjuc.messenger.AccountInstance;
import im.guobwnxjuc.messenger.AndroidUtilities;
import im.guobwnxjuc.messenger.ApplicationLoader;
import im.guobwnxjuc.messenger.BaseController;
import im.guobwnxjuc.messenger.BuildVars;
import im.guobwnxjuc.messenger.FileLog;
import im.guobwnxjuc.messenger.KeepAliveJob;
import im.guobwnxjuc.messenger.LocaleController;
import im.guobwnxjuc.messenger.MessagesController;
import im.guobwnxjuc.messenger.NotificationCenter;
import im.guobwnxjuc.messenger.SharedConfig;
import im.guobwnxjuc.messenger.StatsController;
import im.guobwnxjuc.messenger.Utilities;
import im.guobwnxjuc.tgnet.ConnectionsManager;
import im.guobwnxjuc.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 = new ConnectionsManager[3];
    private static final int KEEP_ALIVE_SECONDS = 30;
    private static final int MAXIMUM_POOL_SIZE = ((CPU_COUNT * 2) + 1);
    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 = new HashMap<>();
    private static int lastClassGuid = 1;
    private static long lastDnsRequestTime;
    private static HashMap<String, ResolveHostByNameTask> resolvingHostnameTasks = new HashMap<>();
    private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue(128);
    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        /* class im.guobwnxjuc.tgnet.ConnectionsManager.AnonymousClass1 */
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable runnable) {
            return new Thread(runnable, "DnsAsyncTask #" + this.mCount.getAndIncrement());
        }
    };
    private boolean appPaused = true;
    private int appResumeCount;
    private int connectionState;
    private boolean isUpdating;
    private long lastPauseTime = System.currentTimeMillis();
    private AtomicInteger lastRequestToken = new AtomicInteger(1);

    public static int getInitFlags() {
        return 0;
    }

    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));
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, 30, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        DNS_THREAD_POOL_EXECUTOR = threadPoolExecutor;
    }

    /* access modifiers changed from: private */
    public static class ResolvedDomain {
        public ArrayList<String> addresses;
        long ttl;

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

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

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

    public ConnectionsManager(int i) {
        super(i);
        String str;
        String str2;
        String str3;
        String str4;
        String str5;
        String str6;
        String str7;
        String str8;
        if (BuildVars.DEBUG_VERSION) {
            FileLog.d("ConnectionsManager.java ===> constructor , currentAccount=" + this.currentAccount + " ,newAccount=" + i);
        }
        this.connectionState = native_getConnectionState(this.currentAccount);
        File filesDirFixed = ApplicationLoader.getFilesDirFixed();
        if (i != 0) {
            File file = new File(filesDirFixed, "account" + i);
            file.mkdirs();
            filesDirFixed = file;
        }
        String file2 = filesDirFixed.toString();
        boolean z = MessagesController.getGlobalNotificationsSettings().getBoolean("pushConnection", true);
        try {
            str5 = LocaleController.getSystemLocaleStringIso639().toLowerCase();
            String lowerCase = LocaleController.getLocaleStringIso639().toLowerCase();
            str3 = Build.MANUFACTURER + Build.MODEL;
            PackageInfo packageInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
            str2 = packageInfo.versionName + " (" + packageInfo.versionCode + SQLBuilder.PARENTHESES_RIGHT;
            str4 = "SDK " + Build.VERSION.SDK_INT;
            str = lowerCase;
        } catch (Exception unused) {
            str4 = "SDK " + Build.VERSION.SDK_INT;
            str = "";
            str2 = "App version unknown";
            str3 = "Android unknown";
            str5 = "en";
        }
        if (str5.trim().length() == 0) {
            str6 = "en";
        } else {
            str6 = str5;
        }
        if (str3.trim().length() == 0) {
            str7 = "Android unknown";
        } else {
            str7 = str3;
        }
        if (str2.trim().length() == 0) {
            str8 = "App version unknown";
        } else {
            str8 = str2;
        }
        String str9 = str4.trim().length() == 0 ? "SDK Unknown" : str4;
        getUserConfig().loadConfig();
        String str10 = SharedConfig.pushString;
        if (TextUtils.isEmpty(str10) && !TextUtils.isEmpty(SharedConfig.pushStringStatus)) {
            str10 = SharedConfig.pushStringStatus;
        }
        init(BuildVars.BUILD_VERSION, 105, BuildVars.APP_ID, str7, str9, str8, str, str6, file2, FileLog.getNetworkLogPath(), str10, getUserConfig().getClientUserId(), z);
    }

    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 tLObject, RequestDelegate requestDelegate) {
        return sendRequest(tLObject, requestDelegate, (QuickAckDelegate) null, 0);
    }

    public int sendRequest(TLObject tLObject, RequestDelegate requestDelegate, int i) {
        return sendRequest(tLObject, requestDelegate, null, null, i, Integer.MAX_VALUE, 1, true);
    }

    public int sendRequest(TLObject tLObject, RequestDelegate requestDelegate, int i, int i2) {
        return sendRequest(tLObject, requestDelegate, null, null, i, Integer.MAX_VALUE, i2, true);
    }

    public int sendRequest(TLObject tLObject, RequestDelegate requestDelegate, QuickAckDelegate quickAckDelegate, int i) {
        return sendRequest(tLObject, requestDelegate, quickAckDelegate, null, i, Integer.MAX_VALUE, 1, true);
    }

    public int sendRequest(TLObject tLObject, RequestDelegate requestDelegate, QuickAckDelegate quickAckDelegate, WriteToSocketDelegate writeToSocketDelegate, int i, int i2, int i3, boolean z) {
        int andIncrement = this.lastRequestToken.getAndIncrement();
        Utilities.stageQueue.postRunnable(new Runnable(tLObject, andIncrement, requestDelegate, quickAckDelegate, writeToSocketDelegate, i, i2, i3, z) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$EAxSi1_SV_ku3XCFO9MTekM4GTU */
            private final /* synthetic */ TLObject f$1;
            private final /* synthetic */ int f$2;
            private final /* synthetic */ RequestDelegate f$3;
            private final /* synthetic */ QuickAckDelegate f$4;
            private final /* synthetic */ WriteToSocketDelegate f$5;
            private final /* synthetic */ int f$6;
            private final /* synthetic */ int f$7;
            private final /* synthetic */ int f$8;
            private final /* synthetic */ boolean f$9;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
                this.f$4 = r5;
                this.f$5 = r6;
                this.f$6 = r7;
                this.f$7 = r8;
                this.f$8 = r9;
                this.f$9 = r10;
            }

            public final void run() {
                ConnectionsManager.this.lambda$sendRequest$2$ConnectionsManager(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6, this.f$7, this.f$8, this.f$9);
            }
        });
        return andIncrement;
    }

    public /* synthetic */ void lambda$sendRequest$2$ConnectionsManager(TLObject tLObject, int i, RequestDelegate requestDelegate, QuickAckDelegate quickAckDelegate, WriteToSocketDelegate writeToSocketDelegate, int i2, int i3, int i4, boolean z) {
        Exception e;
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("send request " + tLObject + " with token = " + i);
        }
        try {
            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLObject.getObjectSize());
            tLObject.serializeToStream(nativeByteBuffer);
            tLObject.freeResources();
            try {
                native_sendRequest(this.currentAccount, nativeByteBuffer.address, new RequestDelegateInternal(requestDelegate) {
                    /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$rJLvAg4EnH0XOsuhgz6eqM6YKQ */
                    private final /* synthetic */ RequestDelegate f$1;

                    {
                        this.f$1 = r2;
                    }

                    @Override // im.guobwnxjuc.tgnet.RequestDelegateInternal
                    public final void run(long j, int i, String str, int i2) {
                        ConnectionsManager.lambda$null$1(TLObject.this, this.f$1, j, i, str, i2);
                    }
                }, quickAckDelegate, writeToSocketDelegate, i2, i3, i4, z, i);
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Exception e3) {
            e = e3;
            FileLog.e(e);
            if (BuildVars.LOGS_ENABLED) {
                FileLog.e("java request outer layer exception " + e.toString());
            }
        }
    }

    static /* synthetic */ void lambda$null$1(TLObject tLObject, RequestDelegate requestDelegate, long j, int i, String str, int i2) {
        TLRPC.TL_error tL_error;
        TLObject tLObject2 = null;
        if (j != 0) {
            try {
                NativeByteBuffer wrap = NativeByteBuffer.wrap(j);
                wrap.reused = true;
                TLObject deserializeResponse = tLObject.deserializeResponse(wrap, wrap.readInt32(true), true);
                tL_error = null;
                tLObject2 = deserializeResponse;
            } catch (Exception e) {
                FileLog.e(e);
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.e("java parse inner layer exception " + e.toString());
                    return;
                }
                return;
            }
        } else if (str != null) {
            tL_error = new TLRPC.TL_error();
            tL_error.code = i;
            tL_error.text = str;
            if (BuildVars.LOGS_ENABLED) {
                FileLog.e(tLObject + " got error " + tL_error.code + " " + tL_error.text);
            }
        } else {
            tL_error = null;
        }
        if (tLObject2 != null) {
            tLObject2.networkType = i2;
        }
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("java received " + tLObject2 + " error = " + tL_error);
        }
        Utilities.stageQueue.postRunnable(new Runnable(tLObject2, tL_error) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$WGrXT5DjLMgifw3nCr7CojLVHmE */
            private final /* synthetic */ TLObject f$1;
            private final /* synthetic */ TLRPC.TL_error f$2;

            {
                this.f$1 = r2;
                this.f$2 = r3;
            }

            public final void run() {
                ConnectionsManager.lambda$null$0(RequestDelegate.this, this.f$1, this.f$2);
            }
        });
    }

    static /* synthetic */ void lambda$null$0(RequestDelegate requestDelegate, TLObject tLObject, TLRPC.TL_error tL_error) {
        requestDelegate.run(tLObject, tL_error);
        if (tLObject != null) {
            tLObject.freeResources();
        }
    }

    public void cancelRequest(int i, boolean z) {
        native_cancelRequest(this.currentAccount, i, z);
    }

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

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

    public void bindRequestToGuid(int i, int i2) {
        native_bindRequestToGuid(this.currentAccount, i, i2);
    }

    public void applyDatacenterAddress(int i, String str, int i2) {
        native_applyDatacenterAddress(this.currentAccount, i, str, i2);
    }

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

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

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

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

    public void init(int i, int i2, int i3, String str, String str2, String str3, String str4, String str5, String str6, String str7, String str8, int i4, boolean z) {
        SharedPreferences sharedPreferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", 0);
        String string = sharedPreferences.getString("proxy_ip", "");
        String string2 = sharedPreferences.getString("proxy_user", "");
        String string3 = sharedPreferences.getString("proxy_pass", "");
        String string4 = sharedPreferences.getString("proxy_secret", "");
        int i5 = sharedPreferences.getInt("proxy_port", 1080);
        if (sharedPreferences.getBoolean("proxy_enabled", false) && !TextUtils.isEmpty(string)) {
            native_setProxySettings(this.currentAccount, string, i5, string2, string3, string4);
        }
        native_init(this.currentAccount, i, i2, i3, str, str2, str3, str4, str5, str6, str7, str8, i4, z, ApplicationLoader.isNetworkOnline(), ApplicationLoader.getCurrentNetworkType(), "Hong");
        checkConnection();
    }

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

    public static void setRegId(String str, String str2) {
        if (TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
            str = str2;
        }
        for (int i = 0; i < 3; i++) {
            native_setRegId(i, str);
        }
    }

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

    public void switchBackend() {
        MessagesController.getGlobalMainSettings().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 str, int i, String str2, String str3, String str4, RequestTimeDelegate requestTimeDelegate) {
        if (TextUtils.isEmpty(str)) {
            return 0;
        }
        return native_checkProxy(this.currentAccount, str == null ? "" : str, i, str2 == null ? "" : str2, str3 == null ? "" : str3, str4 == null ? "" : str4, requestTimeDelegate);
    }

    public void setAppPaused(boolean z, boolean z2) {
        if (!z2) {
            this.appPaused = z;
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("app paused = " + z);
            }
            if (z) {
                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) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("reset app pause time");
            }
            this.lastPauseTime = 0;
            native_resumeNetwork(this.currentAccount, false);
        }
    }

    public static void onUnparsedMessageReceived(long j, int i) {
        try {
            NativeByteBuffer wrap = NativeByteBuffer.wrap(j);
            wrap.reused = true;
            int readInt32 = wrap.readInt32(true);
            TLObject TLdeserialize = TLClassStore.Instance().TLdeserialize(wrap, readInt32, true);
            if (TLdeserialize instanceof TLRPC.Updates) {
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("java received " + TLdeserialize);
                }
                KeepAliveJob.finishJob();
                Utilities.stageQueue.postRunnable(new Runnable(i, TLdeserialize) {
                    /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$OAf_e2NMSVxRf3E2cj8B43YYm8 */
                    private final /* synthetic */ int f$0;
                    private final /* synthetic */ TLObject f$1;

                    {
                        this.f$0 = r1;
                        this.f$1 = r2;
                    }

                    public final void run() {
                        AccountInstance.getInstance(this.f$0).getMessagesController().processUpdates((TLRPC.Updates) this.f$1, false);
                    }
                });
            } else if (BuildVars.LOGS_ENABLED) {
                FileLog.d(String.format("java received unknown constructor 0x%x", Integer.valueOf(readInt32)));
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void onUpdate(int i) {
        Utilities.stageQueue.postRunnable(new Runnable(i) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$P7KVoGOkwc_Gc8gWszNktbQxQEs */
            private final /* synthetic */ int f$0;

            {
                this.f$0 = r1;
            }

            public final void run() {
                AccountInstance.getInstance(this.f$0).getMessagesController().updateTimerProc();
            }
        });
    }

    public static void onSessionCreated(int i) {
        Utilities.stageQueue.postRunnable(new Runnable(i) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$F2gVkCZf79l4Qb5syIvncBo9N8k */
            private final /* synthetic */ int f$0;

            {
                this.f$0 = r1;
            }

            public final void run() {
                AccountInstance.getInstance(this.f$0).getMessagesController().getDifference();
            }
        });
    }

    public static void onConnectionStateChanged(int i, int i2) {
        AndroidUtilities.runOnUIThread(new Runnable(i2, i) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$69wBdiXYa6l9PeR3wX7eS_qgQgg */
            private final /* synthetic */ int f$0;
            private final /* synthetic */ int f$1;

            {
                this.f$0 = r1;
                this.f$1 = r2;
            }

            public final void run() {
                ConnectionsManager.lambda$onConnectionStateChanged$6(this.f$0, this.f$1);
            }
        });
    }

    static /* synthetic */ void lambda$onConnectionStateChanged$6(int i, int i2) {
        getInstance(i).connectionState = i2;
        AccountInstance.getInstance(i).getNotificationCenter().postNotificationName(NotificationCenter.didUpdateConnectionState, new Object[0]);
    }

    public static void onLogout(int i) {
        AndroidUtilities.runOnUIThread(new Runnable(i) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$GelvtBJc6HbsQRaXq5P2CNXqEI */
            private final /* synthetic */ int f$0;

            {
                this.f$0 = r1;
            }

            public final void run() {
                ConnectionsManager.lambda$onLogout$7(this.f$0);
            }
        });
    }

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

    public static void onBytesSent(int i, int i2, int i3) {
        try {
            AccountInstance.getInstance(i3).getStatsController().incrementSentBytesCount(i2, 6, (long) i);
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void onRequestNewServerIpAndPort(int i, int i2) {
        Utilities.stageQueue.postRunnable(new Runnable(i, i2) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$lKh7Jj_PKGf4kV7PDM_mVX57C4U */
            private final /* synthetic */ int f$0;
            private final /* synthetic */ int f$1;

            {
                this.f$0 = r1;
                this.f$1 = r2;
            }

            public final void run() {
                ConnectionsManager.lambda$onRequestNewServerIpAndPort$8(this.f$0, this.f$1);
            }
        });
    }

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

    public static void onProxyError() {
        AndroidUtilities.runOnUIThread($$Lambda$ConnectionsManager$Ood4pp43mK1fTdzclwwjLJJZyaQ.INSTANCE);
    }

    public static void getHostByName(String str, long j) {
        AndroidUtilities.runOnUIThread(new Runnable(str, j) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$TNWnqWMJycD2MBtBBsTljaDTtZk */
            private final /* synthetic */ String f$0;
            private final /* synthetic */ long f$1;

            {
                this.f$0 = r1;
                this.f$1 = r2;
            }

            public final void run() {
                ConnectionsManager.lambda$getHostByName$10(this.f$0, this.f$1);
            }
        });
    }

    static /* synthetic */ void lambda$getHostByName$10(String str, long j) {
        ResolvedDomain resolvedDomain = dnsCache.get(str);
        if (resolvedDomain == null || SystemClock.elapsedRealtime() - resolvedDomain.ttl >= 300000) {
            ResolveHostByNameTask resolveHostByNameTask = resolvingHostnameTasks.get(str);
            if (resolveHostByNameTask == null) {
                resolveHostByNameTask = new ResolveHostByNameTask(str);
                try {
                    resolveHostByNameTask.executeOnExecutor(DNS_THREAD_POOL_EXECUTOR, null, null, null);
                    resolvingHostnameTasks.put(str, resolveHostByNameTask);
                } catch (Throwable th) {
                    FileLog.e(th);
                    native_onHostNameResolved(str, j, "");
                    return;
                }
            }
            resolveHostByNameTask.addAddress(j);
            return;
        }
        native_onHostNameResolved(str, j, resolvedDomain.getAddress());
    }

    public static void onBytesReceived(int i, int i2, int i3) {
        try {
            StatsController.getInstance(i3).incrementReceivedBytesCount(i2, 6, (long) i);
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    public static void onUpdateConfig(long j, int i) {
        try {
            NativeByteBuffer wrap = NativeByteBuffer.wrap(j);
            wrap.reused = true;
            TLRPC.TL_config TLdeserialize = TLRPC.TL_config.TLdeserialize(wrap, wrap.readInt32(true), true);
            if (TLdeserialize != null) {
                Utilities.stageQueue.postRunnable(new Runnable(i, TLdeserialize) {
                    /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$lL24zLFxAvmgRMSDlov34UP3Qoo */
                    private final /* synthetic */ int f$0;
                    private final /* synthetic */ TLRPC.TL_config f$1;

                    {
                        this.f$0 = r1;
                        this.f$1 = r2;
                    }

                    public final void run() {
                        AccountInstance.getInstance(this.f$0).getMessagesController().updateConfig(this.f$1);
                    }
                });
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

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

    public static void setProxySettings(boolean z, String str, int i, String str2, String str3, String str4) {
        if (str == null) {
            str = "";
        }
        if (str2 == null) {
            str2 = "";
        }
        if (str3 == null) {
            str3 = "";
        }
        if (str4 == null) {
            str4 = "";
        }
        for (int i2 = 0; i2 < 3; i2++) {
            if (!z || TextUtils.isEmpty(str)) {
                native_setProxySettings(i2, "", 1080, "", "", "");
            } else {
                native_setProxySettings(i2, str, i, str2, str3, str4);
            }
            AccountInstance instance = AccountInstance.getInstance(i2);
            if (instance.getUserConfig().isClientActivated()) {
                instance.getMessagesController().checkProxyInfo(true);
            }
        }
    }

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

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

    public void setIsUpdating(boolean z) {
        AndroidUtilities.runOnUIThread(new Runnable(z) {
            /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$rgmJJDz9IMAMG6Ua0LLlDKutYTk */
            private final /* synthetic */ boolean f$1;

            {
                this.f$1 = r2;
            }

            public final void run() {
                ConnectionsManager.this.lambda$setIsUpdating$12$ConnectionsManager(this.f$1);
            }
        });
    }

    public /* synthetic */ void lambda$setIsUpdating$12$ConnectionsManager(boolean z) {
        if (this.isUpdating != z) {
            this.isUpdating = z;
            if (this.connectionState == 3) {
                AccountInstance.getInstance(this.currentAccount).getNotificationCenter().postNotificationName(NotificationCenter.didUpdateConnectionState, new Object[0]);
            }
        }
    }

    protected static boolean useIpv6Address() {
        if (Build.VERSION.SDK_INT < 19) {
            return false;
        }
        if (BuildVars.LOGS_ENABLED) {
            try {
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface nextElement = networkInterfaces.nextElement();
                    if (nextElement.isUp() && !nextElement.isLoopback()) {
                        if (!nextElement.getInterfaceAddresses().isEmpty()) {
                            if (BuildVars.LOGS_ENABLED) {
                                FileLog.d("valid interface: " + nextElement);
                            }
                            List<InterfaceAddress> interfaceAddresses = nextElement.getInterfaceAddresses();
                            for (int i = 0; i < interfaceAddresses.size(); i++) {
                                InetAddress address = interfaceAddresses.get(i).getAddress();
                                if (BuildVars.LOGS_ENABLED) {
                                    FileLog.d("address: " + address.getHostAddress());
                                }
                                if (!address.isLinkLocalAddress() && !address.isLoopbackAddress()) {
                                    if (!address.isMulticastAddress()) {
                                        if (BuildVars.LOGS_ENABLED) {
                                            FileLog.d("address is good");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Throwable th) {
                FileLog.e(th);
            }
        }
        try {
            Enumeration<NetworkInterface> networkInterfaces2 = NetworkInterface.getNetworkInterfaces();
            boolean z = false;
            boolean z2 = false;
            while (networkInterfaces2.hasMoreElements()) {
                NetworkInterface nextElement2 = networkInterfaces2.nextElement();
                if (nextElement2.isUp()) {
                    if (!nextElement2.isLoopback()) {
                        List<InterfaceAddress> interfaceAddresses2 = nextElement2.getInterfaceAddresses();
                        for (int i2 = 0; i2 < interfaceAddresses2.size(); i2++) {
                            InetAddress address2 = interfaceAddresses2.get(i2).getAddress();
                            if (!address2.isLinkLocalAddress() && !address2.isLoopbackAddress()) {
                                if (!address2.isMulticastAddress()) {
                                    if (address2 instanceof Inet6Address) {
                                        z2 = true;
                                    } else if ((address2 instanceof Inet4Address) && !address2.getHostAddress().startsWith("192.0.0.")) {
                                        z = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (z || !z2) {
                return false;
            }
            return true;
        } catch (Throwable th2) {
            FileLog.e(th2);
        }
    }

    /* access modifiers changed from: private */
    public static class ResolveHostByNameTask extends AsyncTask<Void, Void, ResolvedDomain> {
        private ArrayList<Long> addresses = new ArrayList<>();
        private String currentHostName;

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

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

        /* access modifiers changed from: protected */
        /* JADX WARNING: Removed duplicated region for block: B:45:0x00c3 A[SYNTHETIC, Splitter:B:45:0x00c3] */
        /* JADX WARNING: Removed duplicated region for block: B:50:0x00cd A[SYNTHETIC, Splitter:B:50:0x00cd] */
        /* JADX WARNING: Removed duplicated region for block: B:54:0x00d4 A[SYNTHETIC, Splitter:B:54:0x00d4] */
        public ResolvedDomain doInBackground(Void... voidArr) {
            Throwable th;
            ByteArrayOutputStream byteArrayOutputStream;
            InputStream inputStream;
            JSONArray jSONArray;
            int length;
            boolean z = false;
            try {
                URLConnection openConnection = new URL("https://www.google.com/resolve?name=" + this.currentHostName + "&type=A").openConnection();
                openConnection.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");
                openConnection.addRequestProperty("Host", "dns.google.com");
                openConnection.setConnectTimeout(1000);
                openConnection.setReadTimeout(2000);
                openConnection.connect();
                inputStream = openConnection.getInputStream();
                try {
                    byteArrayOutputStream = new ByteArrayOutputStream();
                    try {
                        byte[] bArr = new byte[32768];
                        while (true) {
                            int read = inputStream.read(bArr);
                            if (read <= 0) {
                                break;
                            }
                            byteArrayOutputStream.write(bArr, 0, read);
                        }
                        JSONObject jSONObject = new JSONObject(new String(byteArrayOutputStream.toByteArray()));
                        if (!jSONObject.has("Answer") || (length = (jSONArray = jSONObject.getJSONArray("Answer")).length()) <= 0) {
                            if (inputStream != null) {
                                try {
                                    inputStream.close();
                                } catch (Throwable th2) {
                                    FileLog.e(th2);
                                }
                            }
                            try {
                                byteArrayOutputStream.close();
                            } catch (Exception unused) {
                            }
                            z = true;
                            if (!z) {
                                try {
                                    InetAddress byName = InetAddress.getByName(this.currentHostName);
                                    ArrayList arrayList = new ArrayList(1);
                                    arrayList.add(byName.getHostAddress());
                                    return new ResolvedDomain(arrayList, SystemClock.elapsedRealtime());
                                } catch (Exception e) {
                                    FileLog.e(e);
                                }
                            }
                            return null;
                        }
                        ArrayList arrayList2 = new ArrayList(length);
                        for (int i = 0; i < length; i++) {
                            arrayList2.add(jSONArray.getJSONObject(i).getString("data"));
                        }
                        ResolvedDomain resolvedDomain = new ResolvedDomain(arrayList2, SystemClock.elapsedRealtime());
                        if (inputStream != null) {
                            try {
                                inputStream.close();
                            } catch (Throwable th3) {
                                FileLog.e(th3);
                            }
                        }
                        try {
                            byteArrayOutputStream.close();
                        } catch (Exception unused2) {
                        }
                        return resolvedDomain;
                    } catch (Throwable th4) {
                        th = th4;
                        try {
                            FileLog.e(th);
                            if (inputStream != null) {
                                try {
                                    inputStream.close();
                                } catch (Throwable th5) {
                                    FileLog.e(th5);
                                }
                            }
                            if (byteArrayOutputStream != null) {
                                try {
                                    byteArrayOutputStream.close();
                                } catch (Exception unused3) {
                                }
                            }
                            if (!z) {
                            }
                            return null;
                        } catch (Throwable th6) {
                            FileLog.e(th6);
                        }
                    }
                } catch (Throwable th7) {
                    th = th7;
                    byteArrayOutputStream = null;
                    FileLog.e(th);
                    if (inputStream != null) {
                    }
                    if (byteArrayOutputStream != null) {
                    }
                    if (!z) {
                    }
                    return null;
                }
            } catch (Throwable th8) {
                th = th8;
                inputStream = null;
                byteArrayOutputStream = null;
                FileLog.e(th);
                if (inputStream != null) {
                }
                if (byteArrayOutputStream != null) {
                }
                if (!z) {
                }
                return null;
            }
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (Exception unused4) {
                }
            }
            throw th;
            throw th;
        }

        /* access modifiers changed from: protected */
        public void onPostExecute(ResolvedDomain resolvedDomain) {
            int i = 0;
            if (resolvedDomain != null) {
                ConnectionsManager.dnsCache.put(this.currentHostName, resolvedDomain);
                int size = this.addresses.size();
                while (i < size) {
                    ConnectionsManager.native_onHostNameResolved(this.currentHostName, this.addresses.get(i).longValue(), resolvedDomain.getAddress());
                    i++;
                }
            } else {
                int size2 = this.addresses.size();
                while (i < size2) {
                    ConnectionsManager.native_onHostNameResolved(this.currentHostName, this.addresses.get(i).longValue(), "");
                    i++;
                }
            }
            ConnectionsManager.resolvingHostnameTasks.remove(this.currentHostName);
        }
    }

    /* access modifiers changed from: private */
    public static class DnsTxtLoadTask extends AsyncTask<Void, Void, NativeByteBuffer> {
        private int currentAccount;
        private int responseDate;

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

        /* access modifiers changed from: protected */
        public NativeByteBuffer doInBackground(Void... voidArr) {
            if (BuildVars.DEBUG_VERSION) {
                Log.i("connection", "java DnsTxtLoadTask doInBackground ===> ");
            }
            NetworkConfig.getInstance().applyNetconfig(this.currentAccount);
            return null;
        }

        /* access modifiers changed from: protected */
        public void onPostExecute(NativeByteBuffer nativeByteBuffer) {
            Utilities.stageQueue.postRunnable(new Runnable(nativeByteBuffer) {
                /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$DnsTxtLoadTask$IgB569BT4NkMxmC2agSxMCBNsJQ */
                private final /* synthetic */ NativeByteBuffer f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    ConnectionsManager.DnsTxtLoadTask.this.lambda$onPostExecute$0$ConnectionsManager$DnsTxtLoadTask(this.f$1);
                }
            });
        }

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

    /* access modifiers changed from: private */
    public static class FirebaseTask extends AsyncTask<Void, Void, NativeByteBuffer> {
        private int currentAccount;
        private FirebaseRemoteConfig firebaseRemoteConfig;

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

        /* access modifiers changed from: protected */
        public NativeByteBuffer doInBackground(Void... voidArr) {
            if (BuildVars.DEBUG_VERSION) {
                Log.i("connection", "java FirebaseTask doInBackground ===> ");
            }
            NetworkConfig.getInstance().applyNetconfig(this.currentAccount);
            return null;
        }

        /* access modifiers changed from: protected */
        public void onPostExecute(NativeByteBuffer nativeByteBuffer) {
            Utilities.stageQueue.postRunnable(new Runnable(nativeByteBuffer) {
                /* class im.guobwnxjuc.tgnet.$$Lambda$ConnectionsManager$FirebaseTask$9aSvXUeNGV2iuLHVsVED7U8aqbg */
                private final /* synthetic */ NativeByteBuffer f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    ConnectionsManager.FirebaseTask.this.lambda$onPostExecute$0$ConnectionsManager$FirebaseTask(this.f$1);
                }
            });
        }

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