789娱乐城.apk(点击下载) / m.java


package com.tencent.smtt.sdk;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.tencent.smtt.sdk.TbsDownloadConfig;
import com.tencent.smtt.sdk.TbsListener;
import com.tencent.smtt.sdk.TbsLogReport;
import com.tencent.smtt.utils.Apn;
import com.tencent.smtt.utils.FileUtil;
import com.tencent.smtt.utils.TbsLog;
import com.tencent.smtt.utils.a;
import com.tencent.smtt.utils.b;
import com.tencent.smtt.utils.q;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import javax.net.ssl.SSLHandshakeException;

/* access modifiers changed from: package-private */
public class m {
    private static int e = 5;
    private static int f = 1;
    private static final String[] g = {"tbs_downloading_com.tencent.mtt", "tbs_downloading_com.tencent.mm", "tbs_downloading_com.tencent.mobileqq", "tbs_downloading_com.tencent.tbs", "tbs_downloading_com.qzone"};
    private boolean A;
    private Handler B;
    private Set<String> C;
    private int D = e;
    private boolean E;

    /* renamed from: a  reason: collision with root package name */
    String f3091a;
    String[] b = null;
    int c = 0;
    private boolean d = false;
    private Context h;
    private String i;
    private String j;
    private String k;
    private File l;
    private File m;
    private long n;
    private int o = 30000;
    private int p = 20000;
    private boolean q;
    private int r;
    private int s;
    private boolean t;
    private boolean u;
    private HttpURLConnection v;
    private String w;
    private TbsLogReport.TbsLogInfo x;
    private String y;
    private int z;

    public m(Context context) throws NullPointerException {
        this.h = context.getApplicationContext();
        this.x = TbsLogReport.getInstance(this.h).tbsLogInfo();
        this.C = new HashSet();
        this.w = "tbs_downloading_" + this.h.getPackageName();
        p.a();
        this.l = p.t(this.h);
        if (this.l != null) {
            try {
                Context packageContext = TbsShareManager.getPackageContext(context, TbsConfig.APP_WX, false);
                if (packageContext == null) {
                    this.m = new File(FileUtil.a(context, TbsConfig.APP_WX, 4, true));
                } else {
                    this.m = new File(FileUtil.a(packageContext, 4));
                }
            } catch (Throwable unused) {
            }
            g();
            this.y = null;
            this.z = -1;
            return;
        }
        throw new NullPointerException("TbsCorePrivateDir is null!");
    }

    private long a(long j2, long j3) {
        long currentTimeMillis = System.currentTimeMillis();
        this.x.setDownConsumeTime(currentTimeMillis - j2);
        this.x.setDownloadSize(j3);
        return currentTimeMillis;
    }

    private static File a(Context context, int i2) {
        File file = new File(FileUtil.a(context, i2));
        if (file.exists() && file.isDirectory()) {
            if (new File(file, TbsDownloader.getOverSea(context) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false)).exists()) {
                return file;
            }
        }
        return null;
    }

    private String a(Throwable th) {
        String stackTraceString = Log.getStackTraceString(th);
        return stackTraceString.length() > 1024 ? stackTraceString.substring(0, 1024) : stackTraceString;
    }

    private String a(URL url) {
        try {
            return InetAddress.getByName(url.getHost()).getHostAddress();
        } catch (Exception e2) {
            e2.printStackTrace();
        } catch (Error e3) {
            e3.printStackTrace();
        }
        return "";
    }

    private void a(int i2, String str, boolean z2) {
        if (z2 || this.r > this.D) {
            this.x.setErrorCode(i2);
            this.x.setFailDetail(str);
        }
    }

    private void a(long j2) {
        this.r++;
        if (j2 <= 0) {
            try {
                j2 = n();
            } catch (Exception unused) {
                return;
            }
        }
        Thread.sleep(j2);
    }

    public static void a(Context context) {
        try {
            TbsLog.i(TbsDownloader.LOGTAG, "clearDecoupleDirOld #00");
            File a2 = p.a().a(context, context.getDir("tbs_64", 0));
            FileUtil.b(a2);
            if (a2 != null) {
                TbsLog.i(TbsDownloader.LOGTAG, "clearDecoupleDirOld dir is " + a2.getAbsolutePath());
            }
            File a3 = p.a().a(context, context.getDir("tbs", 0));
            FileUtil.b(a3);
            if (a3 != null) {
                TbsLog.i(TbsDownloader.LOGTAG, "clearDecoupleDirOld dir is " + a3.getAbsolutePath());
            }
        } catch (Throwable th) {
            TbsLog.i(TbsDownloader.LOGTAG, "clearDecoupleDirOld stack is " + Log.getStackTraceString(th));
        }
    }

    private void a(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException unused) {
            }
        }
    }

    public static void a(File file, Context context) {
        synchronized (a.class) {
            if (file != null) {
                if (file.exists()) {
                    if (!TbsShareManager.isThirdPartyApp(context)) {
                        try {
                            File c2 = c(context);
                            if (c2 != null) {
                                File file2 = new File(c2, TbsDownloader.getOverSea(context) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false));
                                file2.delete();
                                FileUtil.b(file, file2);
                                TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.backupTbsApk]tbsApk is " + file.getAbsolutePath());
                                TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.backupTbsApk]backUpApk is " + file2.getAbsolutePath());
                                boolean contains = file2.getName().contains("tbs.org");
                                boolean contains2 = file2.getName().contains("x5.tbs.decouple");
                                if (contains2 || contains) {
                                    File[] listFiles = c2.listFiles();
                                    Pattern compile = Pattern.compile(a.a(contains2) + "(.*)");
                                    for (File file3 : listFiles) {
                                        if (compile.matcher(file3.getName()).find() && file3.isFile() && file3.exists()) {
                                            file3.delete();
                                        }
                                    }
                                    File file4 = new File(c2, a.a(contains2) + "." + TbsDownloadConfig.getInstance(context).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0));
                                    if (file4.exists()) {
                                        TbsLog.e(TbsDownloader.LOGTAG, "[TbsApkDownloader.backupTbsApk]delete bacup config file error ");
                                        return;
                                    }
                                    file4.createNewFile();
                                }
                            }
                        } catch (Exception unused) {
                        }
                    }
                }
            }
        }
    }

    private void a(String str) throws Exception {
        URL url = new URL(str);
        if (this.v != null) {
            try {
                this.v.disconnect();
            } catch (Throwable th) {
                TbsLog.e(TbsDownloader.LOGTAG, "[initHttpRequest] mHttpRequest.disconnect() Throwable:" + th.toString());
            }
        }
        this.v = (HttpURLConnection) url.openConnection();
        this.v.setRequestProperty("User-Agent", TbsDownloader.b(this.h));
        this.v.setRequestProperty("Accept-Encoding", "identity");
        this.v.setRequestMethod("GET");
        this.v.setInstanceFollowRedirects(false);
        this.v.setConnectTimeout(this.p);
        this.v.setReadTimeout(this.o);
    }

    private boolean a(File file) {
        int i2 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_USE_BACKUP_VERSION, 0);
        if (i2 == 0) {
            i2 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0);
        }
        return a.a(this.h, file, 0, i2);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:26:0x00a7, code lost:
        if (r7 != r5) goto L_0x00a9;
     */
    private boolean a(boolean z2, boolean z3, File file) {
        boolean z4;
        int i2;
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z2);
        if (file != null) {
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk]  verifyFile is " + file.getAbsolutePath());
        }
        File file2 = new File(this.l, !z2 ? "x5.tbs" : "x5.tbs.temp");
        if (file != null) {
            file2 = file;
        }
        if (!file2.exists()) {
            return false;
        }
        Exception exc = null;
        String string = TbsDownloadConfig.getInstance(this.h).mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPK_MD5, null);
        String a2 = a.a(file2);
        if (string == null || !string.equals(a2)) {
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z2 + " md5 failed");
            if (z2) {
                this.x.setCheckErrorDetail("fileMd5 not match");
            }
            return false;
        }
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] md5(" + a2 + ") successful!");
        long j2 = 0;
        if (z2) {
            long j3 = TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0);
            if (file2 != null && file2.exists()) {
                if (j3 > 0) {
                    j2 = file2.length();
                }
            }
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z2 + " filelength failed");
            TbsLogReport.TbsLogInfo tbsLogInfo = this.x;
            tbsLogInfo.setCheckErrorDetail("fileLength:" + j2 + ",contentLength:" + j3);
            return false;
        }
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] length(" + j2 + ") successful!");
        int i3 = -1;
        if (!z3 || z2 || (i2 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) == (i3 = a.a(this.h, file2))) {
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] tbsApkVersionCode(" + i3 + ") successful!");
            if (z3 && !z2) {
                String a3 = b.a(this.h, false, file2);
                if (!"3082023f308201a8a00302010202044c46914a300d06092a864886f70d01010505003064310b30090603550406130238363110300e060355040813074265696a696e673110300e060355040713074265696a696e673110300e060355040a130754656e63656e74310c300a060355040b13035753443111300f0603550403130873616d75656c6d6f301e170d3130303732313036313835305a170d3430303731333036313835305a3064310b30090603550406130238363110300e060355040813074265696a696e673110300e060355040713074265696a696e673110300e060355040a130754656e63656e74310c300a060355040b13035753443111300f0603550403130873616d75656c6d6f30819f300d06092a864886f70d010101050003818d0030818902818100c209077044bd0d63ea00ede5b839914cabcc912a87f0f8b390877e0f7a2583f0d5933443c40431c35a4433bc4c965800141961adc44c9625b1d321385221fd097e5bdc2f44a1840d643ab59dc070cf6c4b4b4d98bed5cbb8046e0a7078ae134da107cdf2bfc9b440fe5cb2f7549b44b73202cc6f7c2c55b8cfb0d333a021f01f0203010001300d06092a864886f70d010105050003818100b007db9922774ef4ccfee81ba514a8d57c410257e7a2eba64bfa17c9e690da08106d32f637ac41fbc9f205176c71bde238c872c3ee2f8313502bee44c80288ea4ef377a6f2cdfe4d3653c145c4acfedbfbadea23b559d41980cc3cdd35d79a68240693739aabf5c5ed26148756cf88264226de394c8a24ac35b712b120d4d23a".equals(a3)) {
                    TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z2 + " signature failed");
                    if (z2) {
                        TbsLogReport.TbsLogInfo tbsLogInfo2 = this.x;
                        StringBuilder sb = new StringBuilder();
                        sb.append("signature:");
                        sb.append(a3 == null ? "null" : Integer.valueOf(a3.length()));
                        tbsLogInfo2.setCheckErrorDetail(sb.toString());
                    }
                    return false;
                }
            }
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] signature successful!");
            if (!z2 || file != null) {
                z4 = false;
            } else {
                try {
                    z4 = file2.renameTo(new File(this.l, "x5.tbs"));
                } catch (Exception e2) {
                    exc = e2;
                    z4 = false;
                }
                if (!z4) {
                    a(109, a(exc), true);
                    return false;
                }
            }
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] rename(" + z4 + ") successful!");
            return true;
        }
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z2 + " versionCode failed");
        if (z2) {
            TbsLogReport.TbsLogInfo tbsLogInfo3 = this.x;
            tbsLogInfo3.setCheckErrorDetail("fileVersion:" + i3 + ",configVersion:" + i2);
        }
        return false;
    }

    public static void b(Context context) {
        try {
            if (!TbsShareManager.isThirdPartyApp(context)) {
                if (!q.g(context)) {
                    return;
                }
            }
            TbsLog.i(TbsDownloader.LOGTAG, "clearOldBackup #00");
            File file = new File(FileUtil.a(context, 3));
            FileUtil.b(file);
            TbsLog.i(TbsDownloader.LOGTAG, "clearOldBackup dir is " + file.getAbsolutePath());
        } catch (Throwable th) {
            TbsLog.i(TbsDownloader.LOGTAG, "clearOldBackup stack is " + Log.getStackTraceString(th));
        }
    }

    private boolean b(int i2) {
        try {
            File file = new File(this.l, "x5.tbs");
            File c2 = c(this.h);
            if (c2 == null) {
                return false;
            }
            File file2 = new File(c2, TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false));
            file.delete();
            FileUtil.b(file2, file);
            if (a.a(this.h, file, 0, i2)) {
                return true;
            }
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.copyTbsApkFromBackupToInstall] verifyTbsApk error!!");
            return false;
        } catch (Exception e2) {
            e2.printStackTrace();
            TbsLog.e(TbsDownloader.LOGTAG, "[TbsApkDownloader.copyTbsApkFromBackupToInstall] Exception is " + e2.getMessage());
            return false;
        }
    }

    @TargetApi(8)
    static File c(Context context) {
        try {
            File file = Build.VERSION.SDK_INT >= 8 ? new File(FileUtil.a(context, 4)) : null;
            if (file != null && !file.exists() && !file.isDirectory()) {
                file.mkdirs();
            }
            return file;
        } catch (Exception e2) {
            e2.printStackTrace();
            TbsLog.e(TbsDownloader.LOGTAG, "[TbsApkDownloader.backupApkPath] Exception is " + e2.getMessage());
            return null;
        }
    }

    private boolean c(boolean z2, boolean z3) {
        return a(z2, z3, (File) null);
    }

    @TargetApi(8)
    static File d(Context context) {
        try {
            if (Build.VERSION.SDK_INT < 8) {
                return null;
            }
            File a2 = a(context, 4);
            if (a2 == null) {
                a2 = a(context, 3);
            }
            if (a2 == null) {
                a2 = a(context, 2);
            }
            return a2 == null ? a(context, 1) : a2;
        } catch (Exception e2) {
            e2.printStackTrace();
            TbsLog.e(TbsDownloader.LOGTAG, "[TbsApkDownloader.backupApkPath] Exception is " + e2.getMessage());
            return null;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:35:0x01d9, code lost:
        if (com.tencent.smtt.utils.b.b() != false) goto L_0x01db;
     */
    private void d(boolean z2) {
        String str;
        String str2;
        File file;
        q.a(this.h);
        TbsDownloadConfig instance = TbsDownloadConfig.getInstance(this.h);
        instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_FULL_PACKAGE, false);
        instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD, false);
        instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_INTERRUPT_CODE_REASON, -123);
        instance.commit();
        QbSdk.n.onDownloadFinish(z2 ? 100 : TbsListener.ErrorCode.DOWNLOAD_HAS_LOCAL_TBS_ERROR);
        int i2 = instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_RESPONSECODE, 0);
        boolean a2 = TbsDownloader.a(this.h);
        if (a2) {
            q.b(this.h, "download_ok", ("downloadVersion=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) + "_" + "apkSize=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0));
        } else if (TbsShareManager.isThirdPartyApp(this.h)) {
            q.a(this.h, "download_ok", ("downloadVersion=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) + "_" + "apkSize=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0));
        }
        try {
            int i3 = instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_CPUTYPE_OTHER_STABLE_CORE, 0);
            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess #1  cpuTypeForOtherStableCore is " + i3 + " isDownloadDecoupleCore is " + a2);
            StringBuilder sb = new StringBuilder();
            sb.append("downloadSuccess #1  responseCode is ");
            sb.append(i2);
            TbsLog.i(TbsDownloader.LOGTAG, sb.toString());
            if (a2 && q.b(this.h) && i2 != 3) {
                File[] listFiles = this.m.listFiles();
                Pattern compile = Pattern.compile(a.a(false, i3));
                for (File file2 : listFiles) {
                    if (compile.matcher(file2.getName()).find() && file2.isFile() && file2.exists()) {
                        file2.delete();
                    }
                }
                File file3 = new File(this.m, a.a(false, i3) + "." + instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0));
                if (!file3.exists()) {
                    file3.createNewFile();
                }
                try {
                    boolean renameTo = new File(this.m, TbsDownloader.getBackupFileName(false) + "." + "tmp").renameTo(new File(this.m, TbsDownloader.getBackupFileName(false, i3)));
                    String str3 = "none";
                    if (i3 != 0) {
                        if (i3 == 32) {
                            str3 = "stable_32_tpatch_fail";
                            n.a(this.h).a(str3, 0);
                            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess setStatus " + str3 + " is 0");
                            StringBuilder sb2 = new StringBuilder();
                            sb2.append("downloadSuccess renameResult is ");
                            sb2.append(renameTo);
                            TbsLog.i(TbsDownloader.LOGTAG, sb2.toString());
                            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess getTbsStableCoreVersion is " + TbsShareManager.getTbsStableCoreVersion(this.h, i3));
                            q.b(this.h, "rename_whole_file_ok", "cpu=is=" + i3);
                        } else {
                            if (i3 == 64) {
                            }
                            n.a(this.h).a(str3, 0);
                            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess setStatus " + str3 + " is 0");
                            StringBuilder sb22 = new StringBuilder();
                            sb22.append("downloadSuccess renameResult is ");
                            sb22.append(renameTo);
                            TbsLog.i(TbsDownloader.LOGTAG, sb22.toString());
                            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess getTbsStableCoreVersion is " + TbsShareManager.getTbsStableCoreVersion(this.h, i3));
                            q.b(this.h, "rename_whole_file_ok", "cpu=is=" + i3);
                        }
                    }
                    str3 = "stable_64_tpatch_fail";
                    n.a(this.h).a(str3, 0);
                    TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess setStatus " + str3 + " is 0");
                    StringBuilder sb222 = new StringBuilder();
                    sb222.append("downloadSuccess renameResult is ");
                    sb222.append(renameTo);
                    TbsLog.i(TbsDownloader.LOGTAG, sb222.toString());
                } catch (Throwable th) {
                    TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess stack is " + Log.getStackTraceString(th));
                }
                TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess getTbsStableCoreVersion is " + TbsShareManager.getTbsStableCoreVersion(this.h, i3));
                q.b(this.h, "rename_whole_file_ok", "cpu=is=" + i3);
            }
        } catch (Throwable th2) {
            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess stack is " + Log.getStackTraceString(th2));
        }
        if (i2 == 5 || i2 == 3) {
            Bundle a3 = a(i2, a2);
            if (a3 == null) {
                str = TbsDownloader.LOGTAG;
                str2 = "downloadSuccess RESPONSECODE_TPATCH bundle is null ";
            } else {
                if (a2) {
                    q.b(this.h, "tpatch_pre_bundle_ok", (("downloadVersion=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) + "_" + "apkSize=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0)) + "_" + "responseCode=is=" + i2);
                } else if (TbsShareManager.isThirdPartyApp(this.h)) {
                    q.a(this.h, "tpatch_pre_bundle_ok", (("downloadVersion=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) + "_" + "apkSize=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0)) + "_" + "responseCode=is=" + i2);
                }
                TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess RESPONSECODE_TPATCH bundle is " + a3);
                p.a().b(this.h, a3);
                a(this.h);
                b(this.h);
            }
        } else if (i2 == 3 || i2 > 10000) {
            File c2 = c(this.h);
            if (c2 != null) {
                p.a().b(this.h, a(i2, c2, a2));
            } else {
                c();
                instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD, true);
                instance.commit();
            }
            a(this.h);
            b(this.h);
        } else {
            int i4 = instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0);
            p.a().a(this.h, new File(this.l, "x5.tbs").getAbsolutePath(), i4);
            if (!q.b(this.h)) {
                file = new File(this.l, "x5.tbs");
            } else {
                int i5 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, 0);
                TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess tbsCorVer is " + i4 + " stableCoreVersionServer is " + i5);
                if (i4 == i5) {
                    TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess backup");
                    file = new File(this.l, "x5.tbs");
                } else {
                    str = TbsDownloader.LOGTAG;
                    str2 = "downloadSuccess not backup";
                }
            }
            a(file, this.h);
            a(this.h);
            b(this.h);
        }
        TbsLog.i(str, str2);
        a(this.h);
        b(this.h);
    }

    public static void e(Context context) {
        try {
            p.a();
            File t2 = p.t(context);
            new File(t2, "x5.tbs").delete();
            new File(t2, "x5.tbs.temp").delete();
            File c2 = c(context);
            if (c2 != null) {
                new File(c2, TbsDownloader.getBackupFileName(false)).delete();
                new File(c2, "x5.oversea.tbs.org").delete();
                File[] listFiles = c2.listFiles();
                Pattern compile = Pattern.compile(a.a(true) + "(.*)");
                for (File file : listFiles) {
                    if (compile.matcher(file.getName()).find() && file.isFile() && file.exists()) {
                        file.delete();
                    }
                }
                Pattern compile2 = Pattern.compile(a.a(false) + "(.*)");
                for (File file2 : listFiles) {
                    if (compile2.matcher(file2.getName()).find() && file2.isFile() && file2.exists()) {
                        file2.delete();
                    }
                }
            }
        } catch (Exception unused) {
        }
    }

    private boolean e(boolean z2) {
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.deleteFile] isApk=" + z2);
        File file = z2 ? new File(this.l, "x5.tbs") : new File(this.l, "x5.tbs.temp");
        if (file.exists()) {
            FileUtil.b(file);
        }
        return true;
    }

    private void g() {
        this.r = 0;
        this.s = 0;
        this.n = -1;
        this.k = null;
        this.q = false;
        this.t = false;
        this.u = false;
        this.A = false;
    }

    private void h() {
        TbsLogReport instance;
        TbsLogReport.EventType eventType;
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.closeHttpRequest]");
        if (this.v != null) {
            if (!this.t) {
                this.x.setResolveIp(a(this.v.getURL()));
            }
            try {
                this.v.disconnect();
            } catch (Throwable th) {
                TbsLog.e(TbsDownloader.LOGTAG, "[closeHttpRequest] mHttpRequest.disconnect() Throwable:" + th.toString());
            }
            this.v = null;
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.closeHttpRequest] mHttpRequest set null");
        }
        int i2 = this.x.f3028a;
        if (!this.t && this.A) {
            this.x.setEventTime(System.currentTimeMillis());
            String apnInfo = Apn.getApnInfo(this.h);
            if (apnInfo == null) {
                apnInfo = "";
            }
            int apnType = Apn.getApnType(this.h);
            this.x.setApn(apnInfo);
            this.x.setNetworkType(apnType);
            if (apnType != this.z || !apnInfo.equals(this.y)) {
                this.x.setNetworkChange(0);
            }
            if ((this.x.f3028a == 0 || this.x.f3028a == 107) && this.x.getDownFinalFlag() == 0 && (!Apn.isNetworkAvailable(this.h) || !m())) {
                a(101, (String) null, true);
            }
            if (TbsDownloader.a(this.h)) {
                instance = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD_DECOUPLE;
            } else {
                instance = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD;
            }
            instance.eventReport(eventType, this.x);
            this.x.resetArgs();
            if (i2 != 100) {
                QbSdk.n.onDownloadFinish(i2);
            }
        } else if (!this.d) {
            TbsDownloader.f3018a = false;
        }
    }

    private File i() {
        return TbsDownloader.a(this.h) ? new File(FileUtil.a(this.h, 4), TbsDownloader.getBackupFileName(true)) : new File(FileUtil.a(this.h, 4), TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false));
    }

    private void j() {
        try {
            File i2 = i();
            if (i2 != null && i2.exists()) {
                FileUtil.b(i2);
                File[] listFiles = i2.getParentFile().listFiles();
                Pattern compile = Pattern.compile(a.a(TbsDownloader.a(this.h)) + "(.*)");
                for (File file : listFiles) {
                    if (compile.matcher(file.getName()).find() && file.isFile() && file.exists()) {
                        FileUtil.b(file);
                    }
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    private boolean k() {
        return new File(this.l, "x5.tbs.temp").exists();
    }

    private long l() {
        File file = new File(this.l, "x5.tbs.temp");
        if (file.exists()) {
            return file.length();
        }
        return 0;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r9v0, types: [com.tencent.smtt.sdk.m] */
    /* JADX WARN: Type inference failed for: r1v8, types: [java.io.Reader, java.io.InputStreamReader] */
    /* JADX WARNING: Unknown variable types count: 1 */
    private boolean m() {
        BufferedReader bufferedReader;
        Throwable th;
        InputStream inputStream;
        InputStream inputStream2;
        InputStream inputStream3;
        InputStream inputStream4;
        Throwable th2;
        boolean z2 = false;
        InputStream inputStream5 = null;
        try {
            inputStream = Runtime.getRuntime().exec("ping " + "www.qq.com").getInputStream();
            try {
                ?? inputStreamReader = new InputStreamReader(inputStream);
                try {
                    bufferedReader = new BufferedReader(inputStreamReader);
                    int i2 = 0;
                    while (true) {
                        try {
                            String readLine = bufferedReader.readLine();
                            if (readLine != null) {
                                if (!readLine.contains("TTL") && !readLine.contains("ttl")) {
                                    i2++;
                                    if (i2 >= 5) {
                                        break;
                                    }
                                } else {
                                    z2 = true;
                                }
                            } else {
                                break;
                            }
                        } catch (Throwable th3) {
                            th = th3;
                            inputStream2 = inputStreamReader;
                            inputStream5 = inputStream2;
                            a(inputStream);
                            a(inputStream5);
                            a(bufferedReader);
                            throw th;
                        }
                    }
                    z2 = true;
                    a(inputStream);
                    inputStream3 = inputStreamReader;
                } catch (Throwable th4) {
                    th = th4;
                    bufferedReader = null;
                    inputStream2 = inputStreamReader;
                    inputStream5 = inputStream2;
                    a(inputStream);
                    a(inputStream5);
                    a(bufferedReader);
                    throw th;
                }
            } catch (Throwable th5) {
                th = th5;
                bufferedReader = null;
                a(inputStream);
                a(inputStream5);
                a(bufferedReader);
                throw th;
            }
        } catch (Throwable th6) {
            th = th6;
            inputStream = null;
            bufferedReader = null;
            a(inputStream);
            a(inputStream5);
            a(bufferedReader);
            throw th;
        }
        a(inputStream3);
        a(bufferedReader);
        return z2;
    }

    private long n() {
        switch (this.r) {
            case 1:
            case 2:
                return ((long) this.r) * 20000;
            case 3:
            case 4:
                return 100000;
            default:
                return 200000;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    /* JADX WARNING: Removed duplicated region for block: B:27:0x0096  */
    /* JADX WARNING: Removed duplicated region for block: B:29:0x009c A[SYNTHETIC, Splitter:B:29:0x009c] */
    private boolean o() {
        String str;
        Throwable th;
        Throwable th2;
        boolean z2 = false;
        boolean z3 = Apn.getApnType(this.h) == 3;
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDwonloader.detectWifiNetworkAvailable] isWifi=" + z3);
        HttpURLConnection httpURLConnection = null;
        if (z3) {
            str = Apn.getWifiSSID(this.h);
            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDwonloader.detectWifiNetworkAvailable] localBSSID=" + str);
            try {
                HttpURLConnection httpURLConnection2 = (HttpURLConnection) new URL("https://pms.mb.qq.com/rsp204").openConnection();
                try {
                    httpURLConnection2.setInstanceFollowRedirects(false);
                    httpURLConnection2.setConnectTimeout(10000);
                    httpURLConnection2.setReadTimeout(10000);
                    httpURLConnection2.setUseCaches(false);
                    httpURLConnection2.getInputStream();
                    int responseCode = httpURLConnection2.getResponseCode();
                    TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDwonloader.detectWifiNetworkAvailable] responseCode=" + responseCode);
                    if (responseCode == 204) {
                        z2 = true;
                    }
                    if (httpURLConnection2 != null) {
                        try {
                            httpURLConnection2.disconnect();
                        } catch (Exception unused) {
                        }
                    }
                } catch (Throwable th3) {
                    th = th3;
                    if (httpURLConnection2 != null) {
                        try {
                            httpURLConnection2.disconnect();
                        } catch (Exception unused2) {
                        }
                    }
                    throw th;
                }
            } catch (Throwable th4) {
                th2 = th4;
                th2.printStackTrace();
                if (httpURLConnection != null) {
                }
                this.C.add(str);
                p();
                this.B.sendMessageDelayed(this.B.obtainMessage(150, str), 120000);
                this.C.remove(str);
                return z2;
            }
        } else {
            str = null;
        }
        if (!z2 && !TextUtils.isEmpty(str) && !this.C.contains(str)) {
            this.C.add(str);
            p();
            this.B.sendMessageDelayed(this.B.obtainMessage(150, str), 120000);
        }
        if (z2 && this.C.contains(str)) {
            this.C.remove(str);
        }
        return z2;
    }

    private void p() {
        if (this.B == null) {
            this.B = new Handler(o.a().getLooper()) {
                /* class com.tencent.smtt.sdk.m.AnonymousClass1 */

                public void handleMessage(Message message) {
                    if (message.what == 150) {
                        m.this.o();
                    }
                }
            };
        }
    }

    public Bundle a(int i2, File file, boolean z2) {
        File file2;
        if (z2) {
            file2 = new File(file, TbsDownloader.getBackupFileName(true));
        } else {
            file2 = new File(file, TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false));
        }
        int a2 = a.a(this.h, file2);
        File file3 = new File(this.l, "x5.tbs");
        String absolutePath = file3.exists() ? file3.getAbsolutePath() : null;
        int i3 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0);
        Bundle bundle = new Bundle();
        bundle.putInt("operation", i2);
        bundle.putInt("old_core_ver", a2);
        bundle.putInt("new_core_ver", i3);
        bundle.putString("old_apk_location", file2.getAbsolutePath());
        bundle.putString("new_apk_location", absolutePath);
        bundle.putString("diff_file_location", absolutePath);
        return bundle;
    }

    /* JADX WARNING: Removed duplicated region for block: B:31:0x016a  */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x019e  */
    public Bundle a(int i2, boolean z2) {
        int i3;
        File file;
        File file2;
        Bundle bundle;
        File file3;
        int i4;
        Context context;
        p pVar;
        String str = "not_stable";
        if (!z2) {
            file = p.a().r(this.h);
            i3 = p.a().j(this.h);
        } else if (q.b(this.h)) {
            int i5 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_CPUTYPE_OTHER_STABLE_CORE, 0);
            file = new File(this.m, TbsDownloader.getBackupFileName(false, i5));
            i3 = TbsShareManager.getTbsStableCoreVersion(this.h, i5);
            str = "stable_core_" + i5;
        } else {
            file = p.a().q(this.h);
            i3 = p.a().i(this.h);
        }
        File file4 = new File(this.l, "x5.tbs");
        String absolutePath = file4.exists() ? file4.getAbsolutePath() : null;
        if (z2 && q.b(this.h)) {
            file4 = new File(this.m, TbsDownloader.getBackupFileName(false) + "." + "tmp");
            absolutePath = file4.exists() ? file4.getAbsolutePath() : null;
        }
        if (absolutePath == null) {
            TbsLog.i(TbsDownloader.LOGTAG, "gettpatchBundle tbsApkFile is " + file4.getAbsolutePath());
            return null;
        }
        int i6 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0);
        if (!z2) {
            pVar = p.a();
            context = this.h;
            i4 = 5;
        } else if (q.b(this.h)) {
            file2 = new File(this.m, TbsDownloader.getBackupFileName(false) + "." + "tpatch" + "." + "tmp");
            bundle = new Bundle();
            bundle.putInt("operation", i2);
            bundle.putInt("old_core_ver", i3);
            bundle.putInt("new_core_ver", i6);
            bundle.putString("old_apk_location", file.getAbsolutePath());
            bundle.putString("new_apk_location", file2.getAbsolutePath());
            bundle.putString("diff_file_location", absolutePath);
            bundle.putString("core_type_tpatch", str);
            String a2 = FileUtil.a(this.h, 7);
            file3 = new File(a2);
            if (!file3.exists()) {
                file3.mkdirs();
            }
            bundle.putString("backup_apk", new File(a2, i6 + ".tbs").getAbsolutePath());
            if (z2 && q.b(this.h)) {
                bundle.putInt("for_stable_core", 1);
            }
            if (!z2) {
                bundle.putInt("for_self_core", 1);
            }
            return bundle;
        } else {
            pVar = p.a();
            context = this.h;
            i4 = 6;
        }
        file2 = pVar.f(context, i4);
        bundle = new Bundle();
        bundle.putInt("operation", i2);
        bundle.putInt("old_core_ver", i3);
        bundle.putInt("new_core_ver", i6);
        bundle.putString("old_apk_location", file.getAbsolutePath());
        bundle.putString("new_apk_location", file2.getAbsolutePath());
        bundle.putString("diff_file_location", absolutePath);
        bundle.putString("core_type_tpatch", str);
        String a22 = FileUtil.a(this.h, 7);
        file3 = new File(a22);
        if (!file3.exists()) {
        }
        bundle.putString("backup_apk", new File(a22, i6 + ".tbs").getAbsolutePath());
        bundle.putInt("for_stable_core", 1);
        if (!z2) {
        }
        return bundle;
    }

    public void a(int i2) {
        if (p.a().u(this.h)) {
            p.a().b();
            try {
                File file = new File(this.l, "x5.tbs");
                int a2 = a.a(this.h, file);
                if (-1 == a2 || (i2 > 0 && i2 == a2)) {
                    FileUtil.b(file);
                }
            } catch (Exception unused) {
            }
        }
    }

    public void a(boolean z2) {
        b(z2, false);
    }

    public boolean a() {
        String str;
        String str2;
        TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup #1");
        try {
            File file = q.b(this.h) ? new File(FileUtil.a(this.h, TbsConfig.APP_WX, 4, true), TbsDownloader.getBackupFileName(false)) : new File(FileUtil.a(this.h, 4), TbsDownloader.getBackupFileName(true));
            TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup backupFile is " + file.getAbsolutePath());
            if (file.exists()) {
                TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup #2");
            } else {
                File b2 = TbsDownloader.b(TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, -1));
                if (b2 != null) {
                    TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup getExistBackupCoreForDecouple is " + b2.getAbsolutePath());
                }
                if (b2 != null && b2.exists()) {
                    FileUtil.b(b2, file);
                }
                try {
                    String name = b2.getName();
                    TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup fileNameSource is " + name);
                    int i2 = 32;
                    if (name.contains("64")) {
                        i2 = 64;
                    }
                    if (q.b(this.h)) {
                        File[] listFiles = this.m.listFiles();
                        Pattern compile = Pattern.compile(a.a(false, i2));
                        for (File file2 : listFiles) {
                            if (compile.matcher(file2.getName()).find() && file2.isFile() && file2.exists()) {
                                file2.delete();
                                TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup delete file " + file2.getAbsolutePath());
                            }
                        }
                        File file3 = new File(this.m, a.a(false, i2) + "." + TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, -1));
                        if (file3.exists()) {
                            str = "TbsApkDownloader";
                            str2 = "verifyAndInstallDecoupleCoreFromBackup  file already exist " + file3.getAbsolutePath();
                        } else {
                            file3.createNewFile();
                            str = "TbsApkDownloader";
                            str2 = "verifyAndInstallDecoupleCoreFromBackup create file " + file3.getAbsolutePath();
                        }
                        TbsLog.i(str, str2);
                    }
                } catch (Throwable th) {
                    TbsLog.i("TbsApkDownloader", "stack is " + Log.getStackTraceString(th));
                }
            }
            if (a.a(this.h, file, 0, TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, -1))) {
                TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup #3");
                if (q.b(this.h)) {
                    TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup isStableCoreForHostEnable and return true");
                    return true;
                }
                TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup goto installDecoupleCoreFromBackup");
                return p.a().f(this.h);
            }
        } catch (Exception e2) {
            TbsLog.i("TbsApkDownloader", "stack is " + Log.getStackTraceString(e2));
        }
        return false;
    }

    /* JADX WARNING: Removed duplicated region for block: B:48:0x0100  */
    public boolean a(boolean z2, boolean z3) {
        String str;
        boolean z4;
        File i2;
        TbsLogReport instance;
        TbsLogReport.EventType eventType;
        if (Build.VERSION.SDK_INT == 23) {
            return false;
        }
        int i3 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_USE_BACKUP_VERSION, 0);
        int j2 = p.a().j(this.h);
        if (i3 == 0) {
            i3 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0);
            str = "by default key";
        } else {
            str = "by new key";
        }
        this.f3091a = str;
        if (!(i3 == 0 || i3 == j2)) {
            if (z3) {
                File a2 = TbsDownloader.a(i3);
                if (a2 != null && a2.exists()) {
                    File file = new File(FileUtil.a(this.h, 4), TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false));
                    try {
                        if (TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V_TYPE, 0) != 1) {
                            FileUtil.b(a2, file);
                            z4 = true;
                            i2 = i();
                            if (i2 != null || !i2.exists() || !a(i2)) {
                                if (!q.b(this.h)) {
                                    j();
                                }
                                if (a2 != null && a2.exists() && !a.a(this.h, a2, 0, i3) && a2 != null && a2.exists()) {
                                    FileUtil.b(a2);
                                }
                            } else if (b(i3)) {
                                TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_INTERRUPT_CODE_REASON, -214);
                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-214);
                                d(false);
                                if (z4) {
                                    a(100, "use local backup apk in startDownload" + this.f3091a, true);
                                    if (TbsDownloader.a(this.h)) {
                                        instance = TbsLogReport.getInstance(this.h);
                                        eventType = TbsLogReport.EventType.TYPE_DOWNLOAD_DECOUPLE;
                                    } else {
                                        instance = TbsLogReport.getInstance(this.h);
                                        eventType = TbsLogReport.EventType.TYPE_DOWNLOAD;
                                    }
                                    instance.eventReport(eventType, this.x);
                                    this.x.resetArgs();
                                }
                                return true;
                            }
                        }
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
                z4 = false;
                i2 = i();
                if (i2 != null) {
                }
                if (!q.b(this.h)) {
                }
                FileUtil.b(a2);
            }
            if (c(false, z3)) {
                TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_INTERRUPT_CODE_REASON, -214);
                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-214);
                d(false);
                return true;
            } else if (!e(true) && !e(true)) {
                TbsLog.e(TbsDownloader.LOGTAG, "[TbsApkDownloader] delete file failed!");
                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-301);
            }
        }
        return false;
    }

    public void b() {
        TbsLogReport instance;
        TbsLogReport.EventType eventType;
        this.t = true;
        if (TbsShareManager.isThirdPartyApp(this.h)) {
            TbsLogReport.TbsLogInfo tbsLogInfo = TbsLogReport.getInstance(this.h).tbsLogInfo();
            tbsLogInfo.setErrorCode(-309);
            tbsLogInfo.setFailDetail(new Exception());
            if (TbsDownloader.a(this.h)) {
                instance = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD_DECOUPLE;
            } else {
                instance = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD;
            }
            instance.eventReport(eventType, tbsLogInfo);
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:137:0x04d9, code lost:
        if (r36 == false) goto L_0x04db;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:138:0x04db, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).mSyncMap.put(com.tencent.smtt.sdk.TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, java.lang.Long.valueOf(r6));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:150:0x051f, code lost:
        if (r36 == false) goto L_0x05b8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:152:0x0535, code lost:
        if (r36 == false) goto L_0x04db;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:165:0x058f, code lost:
        if (r36 == false) goto L_0x04db;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:173:0x05ae, code lost:
        if (r36 == false) goto L_0x04db;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:176:0x05b6, code lost:
        if (r36 == false) goto L_0x05b8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:185:0x05fc, code lost:
        if (r36 == false) goto L_0x04db;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:186:0x0600, code lost:
        if (r36 == false) goto L_0x05b8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:200:0x062d, code lost:
        if (r36 == false) goto L_0x04db;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:201:0x0631, code lost:
        if (r36 == false) goto L_0x05b8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:209:0x0649, code lost:
        if (r36 == false) goto L_0x05b8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:211:0x0658, code lost:
        if (r36 != false) goto L_0x0732;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:228:0x06e2, code lost:
        if (r36 == false) goto L_0x05b8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:233:0x072e, code lost:
        if (r36 == false) goto L_0x04db;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:234:0x0732, code lost:
        r29 = r4;
        r8 = r23;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:459:0x0b95, code lost:
        if (r36 == false) goto L_0x0b97;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:460:0x0b97, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).mSyncMap.put(com.tencent.smtt.sdk.TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, java.lang.Long.valueOf(r6));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:467:0x0bda, code lost:
        if (r36 == false) goto L_0x0bfc;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:469:0x0bfa, code lost:
        if (r36 != false) goto L_0x0c16;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:470:0x0bfc, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).mSyncMap.put(com.tencent.smtt.sdk.TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, java.lang.Long.valueOf(r6));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:478:0x0c2e, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:479:0x0c2f, code lost:
        r3 = r0;
        r6 = r26;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:483:0x0c3b, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:484:0x0c3c, code lost:
        r29 = r4;
        r3 = r0;
        r8 = r23;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:485:0x0c46, code lost:
        r0 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:498:0x0c66, code lost:
        com.tencent.smtt.utils.TbsLog.e(com.tencent.smtt.sdk.TbsDownloader.LOGTAG, "[startdownload]url:" + r35.k + " download exception:" + r3.toString());
        a(com.tencent.smtt.sdk.TbsListener.ErrorCode.DOWNLOAD_THROWABLE, (java.lang.String) null, true);
        r4 = r29;
        r13 = 0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:501:0x0caf, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).setDownloadInterruptCode(-309);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:502:0x0cba, code lost:
        if (r36 == false) goto L_0x0b97;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:544:0x0dca, code lost:
        r4 = r29;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:545:0x0dcd, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).setDownloadInterruptCode(-316);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:546:0x0ddb, code lost:
        if (r36 == false) goto L_0x0ddd;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:547:0x0ddd, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).mSyncMap.put(com.tencent.smtt.sdk.TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, java.lang.Long.valueOf(r6));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:548:0x0df7, code lost:
        r14 = r13;
        r10 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:54:0x0220, code lost:
        r23 = r8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:550:0x0dfe, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).mSyncMap.put(com.tencent.smtt.sdk.TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, java.lang.Long.valueOf(r6));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:67:0x02c1, code lost:
        if (r36 == false) goto L_0x02c3;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:68:0x02c3, code lost:
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).mSyncMap.put(com.tencent.smtt.sdk.TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, java.lang.Long.valueOf(r6));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:72:0x0300, code lost:
        if (r36 == false) goto L_0x02c3;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:263:0x0792 A[SYNTHETIC, Splitter:B:263:0x0792] */
    /* JADX WARNING: Removed duplicated region for block: B:304:0x0851  */
    /* JADX WARNING: Removed duplicated region for block: B:382:0x0a0d A[SYNTHETIC, Splitter:B:382:0x0a0d] */
    /* JADX WARNING: Removed duplicated region for block: B:389:0x0a3c  */
    /* JADX WARNING: Removed duplicated region for block: B:450:0x0b44 A[Catch:{ all -> 0x0c1c }] */
    /* JADX WARNING: Removed duplicated region for block: B:478:0x0c2e A[ExcHandler: all (r0v5 'th' java.lang.Throwable A[CUSTOM_DECLARE]), PHI: r26 
      PHI: (r26v4 long) = (r26v5 long), (r26v9 long), (r26v25 long), (r26v25 long) binds: [B:475:0x0c24, B:432:0x0ae7, B:382:0x0a0d, B:383:?] A[DONT_GENERATE, DONT_INLINE], Splitter:B:475:0x0c24] */
    /* JADX WARNING: Removed duplicated region for block: B:485:0x0c46 A[ExcHandler: all (th java.lang.Throwable), Splitter:B:79:0x0310] */
    /* JADX WARNING: Removed duplicated region for block: B:493:0x0c59 A[ADDED_TO_REGION, Catch:{ all -> 0x0303 }] */
    /* JADX WARNING: Removed duplicated region for block: B:534:0x0d83  */
    /* JADX WARNING: Removed duplicated region for block: B:535:0x0d99  */
    /* JADX WARNING: Removed duplicated region for block: B:540:0x0dc2  */
    /* JADX WARNING: Removed duplicated region for block: B:544:0x0dca  */
    /* JADX WARNING: Removed duplicated region for block: B:550:0x0dfe  */
    /* JADX WARNING: Removed duplicated region for block: B:555:0x0caf A[SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:573:0x02e7 A[SYNTHETIC] */
    public void b(boolean z2, boolean z3) {
        boolean z4;
        int i2;
        int i3;
        boolean z5;
        int i4;
        TbsLogReport.TbsLogInfo tbsLogInfo;
        boolean z6;
        Throwable th;
        Throwable th2;
        long j2;
        long j3;
        InputStream inputStream;
        FileOutputStream fileOutputStream;
        InputStream inputStream2;
        Throwable th3;
        FileOutputStream fileOutputStream2;
        InputStream inputStream3;
        IOException iOException;
        boolean z7;
        int i5;
        String a2;
        boolean z8;
        Throwable th4;
        File file;
        IOException e2;
        Throwable th5;
        Closeable closeable;
        boolean z9;
        long j4;
        byte[] bArr;
        InputStream inputStream4;
        long j5;
        Throwable th6;
        if (!p.a().d(this.h) || z2) {
            int i6 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_RESPONSECODE, 0);
            boolean z10 = i6 == 1 || i6 == 2 || i6 == 4;
            if (z3 || !a(z2, z10)) {
                this.E = z2;
                String str = null;
                this.i = TbsDownloadConfig.getInstance(this.h).mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOADURL, null);
                String string = TbsDownloadConfig.getInstance(this.h).mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOADURL_LIST, null);
                TbsLog.i(TbsDownloader.LOGTAG, "backupUrlStrings:" + string, true);
                this.b = null;
                this.c = 0;
                if (!z2 && string != null && !"".equals(string.trim())) {
                    this.b = string.trim().split(";");
                }
                TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] mDownloadUrl=" + this.i + " backupUrlStrings=" + string + " mLocation=" + this.k + " mCanceled=" + this.t + " mHttpRequest=" + this.v);
                if (this.i == null && this.k == null) {
                    QbSdk.n.onDownloadFinish(110);
                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-302);
                } else if (this.v != null && !this.t) {
                    QbSdk.n.onDownloadFinish(110);
                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-303);
                } else if (z2 || !this.C.contains(Apn.getWifiSSID(this.h))) {
                    g();
                    TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...", true);
                    long downloadMaxflow = TbsDownloadConfig.getInstance(this.h).getDownloadMaxflow();
                    long j6 = 0;
                    long j7 = TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, 0);
                    this.D = z2 ? f : e;
                    if (z3) {
                        q.b(this.h, "start_download", ("downloadVersion=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) + "_" + "apkSize=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0));
                    } else if (TbsShareManager.isThirdPartyApp(this.h)) {
                        q.a(this.h, "start_download", ("downloadVersion=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) + "_" + "apkSize=is=" + TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0));
                    }
                    long j8 = j7;
                    boolean z11 = false;
                    while (true) {
                        if (this.r > this.D) {
                            break;
                        } else if (this.s > 8) {
                            a(TbsListener.ErrorCode.DOWNLOAD_RETRYTIMES302_EXCEED, str, true);
                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-306);
                            break;
                        } else {
                            long currentTimeMillis = System.currentTimeMillis();
                            if (!z2) {
                                try {
                                    if (currentTimeMillis - TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_STARTTIME, j6) > 86400000) {
                                        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] OVER DOWNLOAD_PERIOD");
                                        TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_STARTTIME, Long.valueOf(currentTimeMillis));
                                        TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j6));
                                        TbsDownloadConfig.getInstance(this.h).commit();
                                        j8 = j6;
                                        if (!FileUtil.b(this.h)) {
                                            TbsLog.i(TbsDownloader.LOGTAG, "DownloadBegin FreeSpace too small", true);
                                            a(105, (String) null, true);
                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308);
                                        }
                                    } else {
                                        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] downloadFlow=" + j8);
                                        if (j8 >= downloadMaxflow) {
                                            TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...failed because you exceeded max flow!", true);
                                            a(112, (String) null, true);
                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-307);
                                        }
                                        if (!FileUtil.b(this.h)) {
                                        }
                                    }
                                } catch (Throwable th7) {
                                    th = th7;
                                    th = th;
                                    z4 = z10;
                                    try {
                                        if (th instanceof SSLHandshakeException) {
                                        }
                                        th.printStackTrace();
                                        long j9 = 0;
                                        a(0L);
                                        a(107, a(th), false);
                                        if (this.t) {
                                        }
                                    } catch (Throwable th8) {
                                        th = th8;
                                        Throwable th9 = th;
                                        if (!z2) {
                                        }
                                        throw th9;
                                    }
                                }
                            }
                            try {
                                this.A = true;
                                String str2 = this.k != null ? this.k : this.i;
                                TbsLog.i(TbsDownloader.LOGTAG, "try url:" + str2 + ",mRetryTimes:" + this.r, true);
                                if (!str2.equals(this.j)) {
                                    this.x.setDownloadUrl(str2);
                                }
                                this.j = str2;
                                a(str2);
                                if (!this.q) {
                                    try {
                                        long l2 = l();
                                        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] range=" + l2);
                                        if (this.n <= 0) {
                                            TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...current" + l2, true);
                                            this.v.setRequestProperty("Range", "bytes=" + l2 + "-");
                                            z6 = z11;
                                            j2 = l2;
                                        } else {
                                            TbsLog.i(TbsDownloader.LOGTAG, "#1 STEP 1/2 begin downloading...current/total=" + l2 + "/" + this.n, true);
                                            Log.d(TbsDownloader.LOGTAG, "#1 STEP 1/2 begin downloading...current/total=" + l2 + "/" + this.n);
                                            HttpURLConnection httpURLConnection = this.v;
                                            StringBuilder sb = new StringBuilder();
                                            sb.append("bytes=");
                                            sb.append(l2);
                                            sb.append("-");
                                            z6 = z11;
                                            j2 = l2;
                                            try {
                                                sb.append(this.n);
                                                httpURLConnection.setRequestProperty("Range", sb.toString());
                                            } catch (Throwable th10) {
                                                th = th10;
                                                z4 = z10;
                                                z11 = z6;
                                            }
                                        }
                                    } catch (Throwable th11) {
                                        th = th11;
                                        th = th;
                                        z4 = z10;
                                        if (th instanceof SSLHandshakeException) {
                                        }
                                        th.printStackTrace();
                                        long j92 = 0;
                                        a(0L);
                                        a(107, a(th), false);
                                        if (this.t) {
                                        }
                                    }
                                } else {
                                    z6 = z11;
                                    j2 = 0;
                                }
                                this.x.setDownloadCancel(j2 == 0 ? 0 : 1);
                                int apnType = Apn.getApnType(this.h);
                                String apnInfo = Apn.getApnInfo(this.h);
                                if (this.y == null && this.z == -1) {
                                    this.y = apnInfo;
                                    this.z = apnType;
                                } else if (apnType != this.z || !apnInfo.equals(this.y)) {
                                    this.x.setNetworkChange(0);
                                    this.y = apnInfo;
                                    this.z = apnType;
                                }
                                if (this.r >= 1) {
                                    this.v.addRequestProperty("Referer", this.i);
                                }
                                int responseCode = this.v.getResponseCode();
                                TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] responseCode=" + responseCode);
                                this.x.setHttpCode(responseCode);
                                if (!z2 && !TbsDownloader.getOverSea(this.h) && ((Apn.getApnType(this.h) != 3 || Apn.getApnType(this.h) == 0) && !QbSdk.getDownloadWithoutWifi())) {
                                    b();
                                    if (QbSdk.n != null) {
                                        QbSdk.n.onDownloadFinish(111);
                                    }
                                    TbsLog.i(TbsDownloader.LOGTAG, "Download is canceled due to NOT_WIFI error!", false);
                                }
                                if (this.t) {
                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                } else {
                                    if (responseCode == 200 || responseCode == 206) {
                                        this.n = ((long) this.v.getContentLength()) + j2;
                                        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] mContentLength=" + this.n);
                                        this.x.setPkgSize(this.n);
                                        long j10 = TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0);
                                        if (j10 == 0 || this.n == j10) {
                                            TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] begin readResponse");
                                            try {
                                                inputStream = this.v.getInputStream();
                                                if (inputStream != null) {
                                                    try {
                                                        String contentEncoding = this.v.getContentEncoding();
                                                        if (contentEncoding != null) {
                                                            try {
                                                                if (contentEncoding.contains("gzip")) {
                                                                    inputStream2 = new GZIPInputStream(inputStream);
                                                                    byte[] bArr2 = new byte[8192];
                                                                    if (z3) {
                                                                        try {
                                                                            if (q.b(this.h)) {
                                                                                File file2 = this.m;
                                                                                StringBuilder sb2 = new StringBuilder();
                                                                                j3 = j8;
                                                                                try {
                                                                                    sb2.append(TbsDownloader.getBackupFileName(false));
                                                                                    sb2.append(".");
                                                                                    sb2.append("tmp");
                                                                                    file = new File(file2, sb2.toString());
                                                                                    if (file.exists()) {
                                                                                        file.delete();
                                                                                    }
                                                                                    TbsLog.i(TbsDownloader.LOGTAG, "fileDownloadApk is " + file.getAbsolutePath(), true);
                                                                                    fileOutputStream = new FileOutputStream(file, true);
                                                                                    long currentTimeMillis2 = System.currentTimeMillis();
                                                                                    closeable = inputStream;
                                                                                    long j11 = currentTimeMillis;
                                                                                    long j12 = j2;
                                                                                    while (!this.t) {
                                                                                        try {
                                                                                            int read = inputStream2.read(bArr2, 0, 8192);
                                                                                            if (read > 0) {
                                                                                                fileOutputStream.write(bArr2, 0, read);
                                                                                                fileOutputStream.flush();
                                                                                                if (!z2) {
                                                                                                    z4 = z10;
                                                                                                    long j13 = j3 + ((long) read);
                                                                                                    if (j13 >= downloadMaxflow) {
                                                                                                        try {
                                                                                                            TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...failed because you exceeded max flow!", true);
                                                                                                            a(112, "downloadFlow=" + j13 + " downloadMaxflow=" + downloadMaxflow, true);
                                                                                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-307);
                                                                                                            j5 = j13;
                                                                                                        } catch (IOException e3) {
                                                                                                            fileOutputStream2 = fileOutputStream;
                                                                                                            z11 = z6;
                                                                                                            j8 = j13;
                                                                                                            inputStream3 = closeable;
                                                                                                            iOException = e3;
                                                                                                            iOException.printStackTrace();
                                                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                            }
                                                                                                            this.o = 100000;
                                                                                                            a(0L);
                                                                                                            a(103, a(iOException), false);
                                                                                                            a(fileOutputStream2);
                                                                                                            a(inputStream2);
                                                                                                            a(inputStream3);
                                                                                                        } catch (Throwable th12) {
                                                                                                            th5 = th12;
                                                                                                            j3 = j13;
                                                                                                            inputStream = closeable;
                                                                                                            th3 = th5;
                                                                                                            a(fileOutputStream);
                                                                                                            a(inputStream2);
                                                                                                            a(inputStream);
                                                                                                            throw th3;
                                                                                                        }
                                                                                                    } else {
                                                                                                        j5 = j13;
                                                                                                        try {
                                                                                                            if (!FileUtil.b(this.h)) {
                                                                                                                TbsLog.i(TbsDownloader.LOGTAG, "DownloadEnd FreeSpace too small ", true);
                                                                                                                a(105, "freespace=" + q.a() + ",and minFreeSpace=" + TbsDownloadConfig.getInstance(this.h).getDownloadMinFreeSpace(), true);
                                                                                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308);
                                                                                                            } else {
                                                                                                                j3 = j5;
                                                                                                            }
                                                                                                        } catch (IOException e4) {
                                                                                                            iOException = e4;
                                                                                                            fileOutputStream2 = fileOutputStream;
                                                                                                            z11 = z6;
                                                                                                            inputStream3 = closeable;
                                                                                                            j8 = j5;
                                                                                                            iOException.printStackTrace();
                                                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                            }
                                                                                                            this.o = 100000;
                                                                                                            a(0L);
                                                                                                            a(103, a(iOException), false);
                                                                                                            a(fileOutputStream2);
                                                                                                            a(inputStream2);
                                                                                                            a(inputStream3);
                                                                                                        } catch (Throwable th13) {
                                                                                                            th3 = th13;
                                                                                                            inputStream = closeable;
                                                                                                            j3 = j5;
                                                                                                            a(fileOutputStream);
                                                                                                            a(inputStream2);
                                                                                                            a(inputStream);
                                                                                                            throw th3;
                                                                                                        }
                                                                                                    }
                                                                                                    z11 = z6;
                                                                                                    j3 = j5;
                                                                                                    break;
                                                                                                }
                                                                                                z4 = z10;
                                                                                                long j14 = (long) read;
                                                                                                try {
                                                                                                    long a3 = a(j11, j14);
                                                                                                    long currentTimeMillis3 = System.currentTimeMillis();
                                                                                                    long j15 = j2 + j14;
                                                                                                    if (currentTimeMillis3 - currentTimeMillis2 > 1000) {
                                                                                                        StringBuilder sb3 = new StringBuilder();
                                                                                                        sb3.append("#2 STEP 1/2 begin downloading...current/total=");
                                                                                                        sb3.append(j15);
                                                                                                        sb3.append("/");
                                                                                                        bArr = bArr2;
                                                                                                        j4 = a3;
                                                                                                        sb3.append(this.n);
                                                                                                        TbsLog.i(TbsDownloader.LOGTAG, sb3.toString(), true);
                                                                                                        if (QbSdk.n != null) {
                                                                                                            try {
                                                                                                                QbSdk.n.onDownloadProgress((int) ((((double) j15) / ((double) this.n)) * 100.0d));
                                                                                                            } catch (IOException e5) {
                                                                                                                iOException = e5;
                                                                                                            } catch (Throwable th14) {
                                                                                                                th3 = th14;
                                                                                                                inputStream = closeable;
                                                                                                                a(fileOutputStream);
                                                                                                                a(inputStream2);
                                                                                                                a(inputStream);
                                                                                                                throw th3;
                                                                                                            }
                                                                                                        }
                                                                                                        if (z2 || j15 - j12 <= 1048576) {
                                                                                                            inputStream4 = closeable;
                                                                                                        } else {
                                                                                                            if (TbsDownloader.getOverSea(this.h)) {
                                                                                                                inputStream4 = closeable;
                                                                                                            } else if ((Apn.getApnType(this.h) != 3 || Apn.getApnType(this.h) == 0) && !QbSdk.getDownloadWithoutWifi()) {
                                                                                                                b();
                                                                                                                if (QbSdk.n != null) {
                                                                                                                    QbSdk.n.onDownloadFinish(111);
                                                                                                                }
                                                                                                                TbsLog.i(TbsDownloader.LOGTAG, "Download is paused due to NOT_WIFI error!", false);
                                                                                                                try {
                                                                                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-304);
                                                                                                                    z11 = z6;
                                                                                                                    z9 = false;
                                                                                                                    if (z9) {
                                                                                                                    }
                                                                                                                } catch (IOException e6) {
                                                                                                                    e2 = e6;
                                                                                                                    inputStream = closeable;
                                                                                                                    iOException = e2;
                                                                                                                    inputStream3 = inputStream;
                                                                                                                    z11 = z6;
                                                                                                                    fileOutputStream2 = fileOutputStream;
                                                                                                                    j8 = j3;
                                                                                                                    iOException.printStackTrace();
                                                                                                                    if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                                    }
                                                                                                                    this.o = 100000;
                                                                                                                    a(0L);
                                                                                                                    a(103, a(iOException), false);
                                                                                                                    a(fileOutputStream2);
                                                                                                                    a(inputStream2);
                                                                                                                    a(inputStream3);
                                                                                                                } catch (Throwable th15) {
                                                                                                                    th5 = th15;
                                                                                                                    inputStream = closeable;
                                                                                                                    th3 = th5;
                                                                                                                    a(fileOutputStream);
                                                                                                                    a(inputStream2);
                                                                                                                    a(inputStream);
                                                                                                                    throw th3;
                                                                                                                }
                                                                                                            } else {
                                                                                                                inputStream4 = closeable;
                                                                                                            }
                                                                                                            j12 = j15;
                                                                                                        }
                                                                                                        currentTimeMillis2 = currentTimeMillis3;
                                                                                                    } else {
                                                                                                        bArr = bArr2;
                                                                                                        j4 = a3;
                                                                                                        inputStream4 = closeable;
                                                                                                    }
                                                                                                    j2 = j15;
                                                                                                    closeable = inputStream4;
                                                                                                    z10 = z4;
                                                                                                    bArr2 = bArr;
                                                                                                    j11 = j4;
                                                                                                } catch (IOException e7) {
                                                                                                    e2 = e7;
                                                                                                    inputStream = closeable;
                                                                                                    iOException = e2;
                                                                                                    inputStream3 = inputStream;
                                                                                                    z11 = z6;
                                                                                                    fileOutputStream2 = fileOutputStream;
                                                                                                    j8 = j3;
                                                                                                    iOException.printStackTrace();
                                                                                                    if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                    }
                                                                                                    this.o = 100000;
                                                                                                    a(0L);
                                                                                                    a(103, a(iOException), false);
                                                                                                    a(fileOutputStream2);
                                                                                                    a(inputStream2);
                                                                                                    a(inputStream3);
                                                                                                } catch (Throwable th16) {
                                                                                                    th5 = th16;
                                                                                                    inputStream = closeable;
                                                                                                    th3 = th5;
                                                                                                    a(fileOutputStream);
                                                                                                    a(inputStream2);
                                                                                                    a(inputStream);
                                                                                                    throw th3;
                                                                                                }
                                                                                            } else if (this.b == null || c(true, z10)) {
                                                                                                this.u = true;
                                                                                                z11 = this.b != null ? true : z6;
                                                                                                try {
                                                                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-311);
                                                                                                    z4 = z10;
                                                                                                } catch (IOException e8) {
                                                                                                    iOException = e8;
                                                                                                    z4 = z10;
                                                                                                    fileOutputStream2 = fileOutputStream;
                                                                                                    j8 = j3;
                                                                                                    inputStream3 = closeable;
                                                                                                    try {
                                                                                                        iOException.printStackTrace();
                                                                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                        }
                                                                                                        this.o = 100000;
                                                                                                        a(0L);
                                                                                                        a(103, a(iOException), false);
                                                                                                        a(fileOutputStream2);
                                                                                                        a(inputStream2);
                                                                                                        a(inputStream3);
                                                                                                    } catch (Throwable th17) {
                                                                                                        th3 = th17;
                                                                                                        j3 = j8;
                                                                                                        z6 = z11;
                                                                                                        fileOutputStream = fileOutputStream2;
                                                                                                        inputStream = inputStream3;
                                                                                                        try {
                                                                                                            a(fileOutputStream);
                                                                                                            a(inputStream2);
                                                                                                            a(inputStream);
                                                                                                            throw th3;
                                                                                                        } catch (Throwable th18) {
                                                                                                        }
                                                                                                    }
                                                                                                } catch (Throwable th19) {
                                                                                                    th3 = th19;
                                                                                                    z4 = z10;
                                                                                                    z6 = z11;
                                                                                                    inputStream = closeable;
                                                                                                    a(fileOutputStream);
                                                                                                    a(inputStream2);
                                                                                                    a(inputStream);
                                                                                                    throw th3;
                                                                                                }
                                                                                            } else {
                                                                                                if (z2 || !b(false)) {
                                                                                                    this.u = true;
                                                                                                    z4 = z10;
                                                                                                    z9 = false;
                                                                                                    z11 = false;
                                                                                                } else {
                                                                                                    z4 = z10;
                                                                                                    z11 = z6;
                                                                                                    z9 = true;
                                                                                                }
                                                                                                if (z9) {
                                                                                                    try {
                                                                                                        a(fileOutputStream);
                                                                                                        a(inputStream2);
                                                                                                        a(closeable);
                                                                                                        if (!z2) {
                                                                                                            TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j3));
                                                                                                            TbsDownloadConfig.getInstance(this.h).commit();
                                                                                                        }
                                                                                                        j8 = j3;
                                                                                                        z10 = z4;
                                                                                                        str = null;
                                                                                                        j6 = 0;
                                                                                                    } catch (Throwable th182) {
                                                                                                    }
                                                                                                } else {
                                                                                                    inputStream = closeable;
                                                                                                    z8 = z11;
                                                                                                }
                                                                                            }
                                                                                        } catch (IOException e9) {
                                                                                            e2 = e9;
                                                                                            z4 = z10;
                                                                                            inputStream = closeable;
                                                                                            iOException = e2;
                                                                                            inputStream3 = inputStream;
                                                                                            z11 = z6;
                                                                                            fileOutputStream2 = fileOutputStream;
                                                                                            j8 = j3;
                                                                                            iOException.printStackTrace();
                                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                            }
                                                                                            this.o = 100000;
                                                                                            a(0L);
                                                                                            a(103, a(iOException), false);
                                                                                            a(fileOutputStream2);
                                                                                            a(inputStream2);
                                                                                            a(inputStream3);
                                                                                        } catch (Throwable th20) {
                                                                                            th5 = th20;
                                                                                            z4 = z10;
                                                                                            inputStream = closeable;
                                                                                            th3 = th5;
                                                                                            a(fileOutputStream);
                                                                                            a(inputStream2);
                                                                                            a(inputStream);
                                                                                            throw th3;
                                                                                        }
                                                                                    }
                                                                                } catch (IOException e10) {
                                                                                    iOException = e10;
                                                                                    z4 = z10;
                                                                                    inputStream3 = inputStream;
                                                                                    z11 = z6;
                                                                                    j8 = j3;
                                                                                    fileOutputStream2 = null;
                                                                                    iOException.printStackTrace();
                                                                                    if (!(iOException instanceof SocketTimeoutException)) {
                                                                                    }
                                                                                    this.o = 100000;
                                                                                    a(0L);
                                                                                    a(103, a(iOException), false);
                                                                                    a(fileOutputStream2);
                                                                                    a(inputStream2);
                                                                                    a(inputStream3);
                                                                                } catch (Throwable th21) {
                                                                                    th6 = th21;
                                                                                    th3 = th6;
                                                                                    z4 = z10;
                                                                                    fileOutputStream = null;
                                                                                    a(fileOutputStream);
                                                                                    a(inputStream2);
                                                                                    a(inputStream);
                                                                                    throw th3;
                                                                                }
                                                                                try {
                                                                                    TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...Canceled!", true);
                                                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                                                    z4 = z10;
                                                                                    z11 = z6;
                                                                                    z9 = false;
                                                                                    if (z9) {
                                                                                    }
                                                                                } catch (IOException e11) {
                                                                                    iOException = e11;
                                                                                    z4 = z10;
                                                                                    fileOutputStream2 = fileOutputStream;
                                                                                    z11 = z6;
                                                                                    j8 = j3;
                                                                                    inputStream3 = closeable;
                                                                                    iOException.printStackTrace();
                                                                                    if (!(iOException instanceof SocketTimeoutException)) {
                                                                                    }
                                                                                    this.o = 100000;
                                                                                    a(0L);
                                                                                    a(103, a(iOException), false);
                                                                                    a(fileOutputStream2);
                                                                                    a(inputStream2);
                                                                                    a(inputStream3);
                                                                                } catch (Throwable th22) {
                                                                                    th3 = th22;
                                                                                    z4 = z10;
                                                                                    inputStream = closeable;
                                                                                    a(fileOutputStream);
                                                                                    a(inputStream2);
                                                                                    a(inputStream);
                                                                                    throw th3;
                                                                                }
                                                                            }
                                                                        } catch (IOException e12) {
                                                                            iOException = e12;
                                                                            z4 = z10;
                                                                            inputStream3 = inputStream;
                                                                            z11 = z6;
                                                                            fileOutputStream2 = null;
                                                                            iOException.printStackTrace();
                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                if (!(iOException instanceof SocketException)) {
                                                                                    if (z2 || FileUtil.b(this.h)) {
                                                                                        a(0L);
                                                                                        if (!k()) {
                                                                                            i5 = 106;
                                                                                            a2 = a(iOException);
                                                                                            z7 = false;
                                                                                        } else {
                                                                                            z7 = false;
                                                                                            i5 = 104;
                                                                                            a2 = a(iOException);
                                                                                        }
                                                                                        a(i5, a2, z7);
                                                                                        a(fileOutputStream2);
                                                                                        a(inputStream2);
                                                                                        a(inputStream3);
                                                                                    } else {
                                                                                        a(105, "freespace=" + q.a() + ",and minFreeSpace=" + TbsDownloadConfig.getInstance(this.h).getDownloadMinFreeSpace(), true);
                                                                                        TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308);
                                                                                        try {
                                                                                            a(fileOutputStream2);
                                                                                            a(inputStream2);
                                                                                            a(inputStream3);
                                                                                        } catch (Throwable th23) {
                                                                                            th2 = th23;
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                            this.o = 100000;
                                                                            a(0L);
                                                                            a(103, a(iOException), false);
                                                                            a(fileOutputStream2);
                                                                            a(inputStream2);
                                                                            a(inputStream3);
                                                                        } catch (Throwable th24) {
                                                                            th6 = th24;
                                                                            j3 = j8;
                                                                            th3 = th6;
                                                                            z4 = z10;
                                                                            fileOutputStream = null;
                                                                            a(fileOutputStream);
                                                                            a(inputStream2);
                                                                            a(inputStream);
                                                                            throw th3;
                                                                        }
                                                                    }
                                                                    j3 = j8;
                                                                    try {
                                                                        file = new File(this.l, "x5.tbs.temp");
                                                                        TbsLog.i(TbsDownloader.LOGTAG, "fileDownloadApk is " + file.getAbsolutePath(), true);
                                                                        fileOutputStream = new FileOutputStream(file, true);
                                                                    } catch (IOException e13) {
                                                                        z4 = z10;
                                                                        iOException = e13;
                                                                        inputStream3 = inputStream;
                                                                        z11 = z6;
                                                                        j8 = j3;
                                                                        fileOutputStream2 = null;
                                                                        iOException.printStackTrace();
                                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                                        }
                                                                        this.o = 100000;
                                                                        a(0L);
                                                                        a(103, a(iOException), false);
                                                                        a(fileOutputStream2);
                                                                        a(inputStream2);
                                                                        a(inputStream3);
                                                                    } catch (Throwable th25) {
                                                                        th4 = th25;
                                                                        z4 = z10;
                                                                        th3 = th4;
                                                                        fileOutputStream = null;
                                                                        a(fileOutputStream);
                                                                        a(inputStream2);
                                                                        a(inputStream);
                                                                        throw th3;
                                                                    }
                                                                    try {
                                                                        long currentTimeMillis22 = System.currentTimeMillis();
                                                                        closeable = inputStream;
                                                                        long j112 = currentTimeMillis;
                                                                        long j122 = j2;
                                                                        while (!this.t) {
                                                                        }
                                                                        TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...Canceled!", true);
                                                                        TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                                        z4 = z10;
                                                                        z11 = z6;
                                                                        z9 = false;
                                                                        if (z9) {
                                                                        }
                                                                    } catch (IOException e14) {
                                                                        e2 = e14;
                                                                        z4 = z10;
                                                                        iOException = e2;
                                                                        inputStream3 = inputStream;
                                                                        z11 = z6;
                                                                        fileOutputStream2 = fileOutputStream;
                                                                        j8 = j3;
                                                                        iOException.printStackTrace();
                                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                                        }
                                                                        this.o = 100000;
                                                                        a(0L);
                                                                        a(103, a(iOException), false);
                                                                        a(fileOutputStream2);
                                                                        a(inputStream2);
                                                                        a(inputStream3);
                                                                    } catch (Throwable th26) {
                                                                        th5 = th26;
                                                                        z4 = z10;
                                                                        th3 = th5;
                                                                        a(fileOutputStream);
                                                                        a(inputStream2);
                                                                        a(inputStream);
                                                                        throw th3;
                                                                    }
                                                                }
                                                            } catch (IOException e15) {
                                                                iOException = e15;
                                                                z4 = z10;
                                                                inputStream3 = inputStream;
                                                                z11 = z6;
                                                                inputStream2 = null;
                                                                fileOutputStream2 = null;
                                                                iOException.printStackTrace();
                                                                if (!(iOException instanceof SocketTimeoutException)) {
                                                                }
                                                                this.o = 100000;
                                                                a(0L);
                                                                a(103, a(iOException), false);
                                                                a(fileOutputStream2);
                                                                a(inputStream2);
                                                                a(inputStream3);
                                                            } catch (Throwable th27) {
                                                                th3 = th27;
                                                                z4 = z10;
                                                                j3 = j8;
                                                                inputStream2 = null;
                                                                fileOutputStream = null;
                                                                a(fileOutputStream);
                                                                a(inputStream2);
                                                                a(inputStream);
                                                                throw th3;
                                                            }
                                                        }
                                                        inputStream2 = (contentEncoding == null || !contentEncoding.contains("deflate")) ? inputStream : new InflaterInputStream(inputStream, new Inflater(true));
                                                        try {
                                                            byte[] bArr22 = new byte[8192];
                                                            if (z3) {
                                                            }
                                                            j3 = j8;
                                                            file = new File(this.l, "x5.tbs.temp");
                                                            TbsLog.i(TbsDownloader.LOGTAG, "fileDownloadApk is " + file.getAbsolutePath(), true);
                                                            fileOutputStream = new FileOutputStream(file, true);
                                                            long currentTimeMillis222 = System.currentTimeMillis();
                                                            closeable = inputStream;
                                                            long j1122 = currentTimeMillis;
                                                            long j1222 = j2;
                                                            while (!this.t) {
                                                            }
                                                            TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...Canceled!", true);
                                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                            z4 = z10;
                                                            z11 = z6;
                                                            z9 = false;
                                                            if (z9) {
                                                            }
                                                        } catch (IOException e16) {
                                                            z4 = z10;
                                                            iOException = e16;
                                                            inputStream3 = inputStream;
                                                            z11 = z6;
                                                            fileOutputStream2 = null;
                                                            iOException.printStackTrace();
                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                            }
                                                            this.o = 100000;
                                                            a(0L);
                                                            a(103, a(iOException), false);
                                                            a(fileOutputStream2);
                                                            a(inputStream2);
                                                            a(inputStream3);
                                                        } catch (Throwable th28) {
                                                            th4 = th28;
                                                            z4 = z10;
                                                            j3 = j8;
                                                            th3 = th4;
                                                            fileOutputStream = null;
                                                            a(fileOutputStream);
                                                            a(inputStream2);
                                                            a(inputStream);
                                                            throw th3;
                                                        }
                                                    } catch (IOException e17) {
                                                        z4 = z10;
                                                        iOException = e17;
                                                        inputStream3 = inputStream;
                                                        z11 = z6;
                                                        inputStream2 = null;
                                                        fileOutputStream2 = null;
                                                        iOException.printStackTrace();
                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                        }
                                                        this.o = 100000;
                                                        a(0L);
                                                        a(103, a(iOException), false);
                                                        a(fileOutputStream2);
                                                        a(inputStream2);
                                                        a(inputStream3);
                                                    } catch (Throwable th29) {
                                                        z4 = z10;
                                                        j3 = j8;
                                                        th3 = th29;
                                                        inputStream2 = null;
                                                        fileOutputStream = null;
                                                        a(fileOutputStream);
                                                        a(inputStream2);
                                                        a(inputStream);
                                                        throw th3;
                                                    }
                                                } else {
                                                    z4 = z10;
                                                    j3 = j8;
                                                    z8 = z6;
                                                    inputStream2 = null;
                                                    fileOutputStream = null;
                                                }
                                            } catch (IOException e18) {
                                                z4 = z10;
                                                iOException = e18;
                                                z11 = z6;
                                                inputStream3 = null;
                                                inputStream2 = null;
                                                fileOutputStream2 = null;
                                                iOException.printStackTrace();
                                                if (!(iOException instanceof SocketTimeoutException)) {
                                                }
                                                this.o = 100000;
                                                a(0L);
                                                a(103, a(iOException), false);
                                                a(fileOutputStream2);
                                                a(inputStream2);
                                                a(inputStream3);
                                            } catch (Throwable th30) {
                                                z4 = z10;
                                                j3 = j8;
                                                th3 = th30;
                                                inputStream2 = null;
                                                fileOutputStream = null;
                                                inputStream = null;
                                                a(fileOutputStream);
                                                a(inputStream2);
                                                a(inputStream);
                                                throw th3;
                                            }
                                            try {
                                                a(fileOutputStream);
                                                a(inputStream2);
                                                a(inputStream);
                                                if (!this.u) {
                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-319);
                                                }
                                                if (!z2) {
                                                    TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j3));
                                                    TbsDownloadConfig.getInstance(this.h).commit();
                                                }
                                                z11 = z8;
                                            } catch (Throwable th1822) {
                                            }
                                        } else {
                                            TbsLog.i(TbsDownloader.LOGTAG, "DownloadBegin tbsApkFileSize=" + j10 + "  but contentLength=" + this.n, true);
                                            if (z2 || (!o() && (!QbSdk.getDownloadWithoutWifi() || !Apn.isNetworkAvailable(this.h)))) {
                                                a(101, "WifiNetworkUnAvailable", true);
                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-304);
                                            } else if (this.b == null || !b(false)) {
                                                a(113, "tbsApkFileSize=" + j10 + "  but contentLength=" + this.n, true);
                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-310);
                                            }
                                        }
                                    } else if (responseCode < 300 || responseCode > 307) {
                                        a(102, String.valueOf(responseCode), false);
                                        if (responseCode == 416) {
                                            if (c(true, z10)) {
                                                try {
                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-214);
                                                    if (!z2) {
                                                        TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j8));
                                                        TbsDownloadConfig.getInstance(this.h).commit();
                                                    }
                                                    z4 = z10;
                                                    z11 = true;
                                                } catch (Throwable th31) {
                                                    th = th31;
                                                    z4 = z10;
                                                    z11 = true;
                                                }
                                            } else {
                                                e(false);
                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-313);
                                            }
                                        } else if ((responseCode == 403 || responseCode == 406) && this.n == -1) {
                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-314);
                                        } else if (responseCode != 202) {
                                            if (this.r < this.D && responseCode == 503) {
                                                a(Long.parseLong(this.v.getHeaderField("Retry-After")));
                                                if (this.t) {
                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                }
                                            } else if (this.r < this.D && (responseCode == 408 || responseCode == 504 || responseCode == 502 || responseCode == 408)) {
                                                a(0L);
                                                if (this.t) {
                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                }
                                            } else if (l() > 0 || this.q || responseCode == 410) {
                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-315);
                                            } else {
                                                this.q = true;
                                            }
                                        }
                                    } else {
                                        String headerField = this.v.getHeaderField("Location");
                                        if (!TextUtils.isEmpty(headerField)) {
                                            this.k = headerField;
                                            this.s++;
                                        } else {
                                            a(TbsListener.ErrorCode.DOWNLOAD_REDIRECT_EMPTY, (String) null, true);
                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-312);
                                        }
                                    }
                                    TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j8));
                                    TbsDownloadConfig.getInstance(this.h).commit();
                                    z11 = z6;
                                    str = null;
                                    j6 = 0;
                                }
                            } catch (Throwable th32) {
                            }
                        }
                    }
                    if (!this.t) {
                        if (this.u) {
                            if (this.b != null || z11) {
                                z5 = z4;
                            } else if (!z3 || !q.b(this.h)) {
                                z5 = z4;
                                z11 = c(true, z5);
                            } else {
                                z5 = z4;
                                z11 = a(true, z5, new File(this.m, TbsDownloader.getBackupFileName(false) + "." + "tmp"));
                            }
                            this.x.setUnpkgFlag(z11 ? 1 : 0);
                            q.a("10is" + z11);
                            if (!z5) {
                                tbsLogInfo = this.x;
                                i4 = z11 ? 1 : 2;
                            } else {
                                tbsLogInfo = this.x;
                                i4 = 0;
                            }
                            tbsLogInfo.setPatchUpdateFlag(i4);
                            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess is " + z11);
                            if (z11) {
                                h();
                                d(true);
                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-317);
                                a(100, "success", true);
                            } else {
                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-318);
                                i2 = 0;
                                e(false);
                                TbsDownloadConfig instance = TbsDownloadConfig.getInstance(this.h);
                                if (!z11) {
                                    i3 = 1;
                                    instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_SUCCESS_RETRYTIMES, Integer.valueOf(instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_SUCCESS_RETRYTIMES, i2) + 1));
                                } else {
                                    i3 = 1;
                                    int i7 = instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_FAILED_RETRYTIMES, i2) + 1;
                                    instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_FAILED_RETRYTIMES, Integer.valueOf(i7));
                                    if (i7 == instance.getDownloadFailedMaxRetrytimes()) {
                                        this.x.setDownloadCancel(2);
                                    }
                                }
                                instance.commit();
                                TbsLogReport.TbsLogInfo tbsLogInfo2 = this.x;
                                if (!z11) {
                                    i3 = 0;
                                }
                                tbsLogInfo2.setDownFinalFlag(i3);
                            }
                        }
                        i2 = 0;
                        TbsDownloadConfig instance2 = TbsDownloadConfig.getInstance(this.h);
                        if (!z11) {
                        }
                        instance2.commit();
                        TbsLogReport.TbsLogInfo tbsLogInfo22 = this.x;
                        if (!z11) {
                        }
                        tbsLogInfo22.setDownFinalFlag(i3);
                    }
                    h();
                } else {
                    TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] WIFI Unavailable");
                    QbSdk.n.onDownloadFinish(110);
                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-304);
                }
            } else {
                TbsDownloader.f3018a = false;
                QbSdk.n.onDownloadFinish(TbsListener.ErrorCode.SDCARD_HAS_BACKUP);
            }
        } else {
            TbsDownloader.f3018a = false;
            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-322);
            QbSdk.n.onDownloadFinish(-322);
        }
    }

    public boolean b(boolean z2) {
        if ((z2 && !o() && (!QbSdk.getDownloadWithoutWifi() || !Apn.isNetworkAvailable(this.h))) || this.b == null || this.c < 0 || this.c >= this.b.length) {
            return false;
        }
        String[] strArr = this.b;
        int i2 = this.c;
        this.c = i2 + 1;
        this.k = strArr[i2];
        this.r = 0;
        this.s = 0;
        this.n = -1;
        this.q = false;
        this.t = false;
        this.u = false;
        this.A = false;
        return true;
    }

    public int c(boolean z2) {
        File c2 = c(this.h);
        if (z2) {
            if (c2 == null) {
                return 0;
            }
            return a.a(this.h, new File(c2, TbsDownloader.getBackupFileName(true)));
        } else if (c2 == null) {
            return 0;
        } else {
            return a.a(this.h, new File(c2, TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false)));
        }
    }

    public void c() {
        b();
        e(false);
        e(true);
    }

    public boolean d() {
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.isDownloadForeground] mIsDownloadForeground=" + this.E);
        return this.E;
    }

    /* access modifiers changed from: package-private */
    public void e() {
        TbsLog.i(TbsDownloader.LOGTAG, "pauseDownload,isPause=" + this.d + "isDownloading=" + TbsDownloader.isDownloading());
        if (!this.d && TbsDownloader.isDownloading()) {
            b();
            this.d = true;
            this.A = false;
        }
    }

    /* access modifiers changed from: package-private */
    public void f() {
        TbsLog.i(TbsDownloader.LOGTAG, "resumeDownload,isPause=" + this.d + "isDownloading=" + TbsDownloader.isDownloading());
        if (this.d && TbsDownloader.isDownloading()) {
            this.d = false;
            a(false);
        }
    }
}