智能工厂.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 cn.jiguang.internal.JConstants;
import com.baidu.tts.loopj.AsyncHttpClient;
import com.baidu.tts.loopj.HttpGet;
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.r;
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.Map;
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;
    String a;
    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();
            return "";
        } 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);
        HttpURLConnection httpURLConnection = this.v;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.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(AsyncHttpClient.HEADER_ACCEPT_ENCODING, "identity");
        this.v.setRequestMethod(HttpGet.METHOD_NAME);
        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(TbsListener.ErrorCode.FILE_RENAME_ERROR, 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 (!r.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;
        r.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) {
            r.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)) {
            r.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 && r.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));
                            r.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));
                            r.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));
                r.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) {
                    r.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)) {
                    r.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 (!r.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.EventType eventType;
        TbsLogReport tbsLogReport;
        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.closeHttpRequest]");
        HttpURLConnection httpURLConnection = this.v;
        if (httpURLConnection != null) {
            if (!this.t) {
                this.x.setResolveIp(a(httpURLConnection.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.a;
        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.a == 0 || this.x.a == 107) && this.x.getDownFinalFlag() == 0 && (!Apn.isNetworkAvailable(this.h) || !m())) {
                a(101, (String) null, true);
            }
            if (TbsDownloader.a(this.h)) {
                tbsLogReport = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD_DECOUPLE;
            } else {
                tbsLogReport = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD;
            }
            tbsLogReport.eventReport(eventType, this.x);
            this.x.resetArgs();
            if (i2 != 100) {
                QbSdk.n.onDownloadFinish(i2);
            }
        } else if (!this.d) {
            TbsDownloader.a = 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() {
        int i2 = this.r;
        switch (i2) {
            case 1:
            case 2:
                return ((long) i2) * 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 (r.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 && r.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 (r.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 && r.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 = r.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 (r.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 (r.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:47:0x00fe  */
    public boolean a(boolean z2, boolean z3) {
        String str;
        boolean z4;
        File i2;
        TbsLogReport.EventType eventType;
        TbsLogReport tbsLogReport;
        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.a = 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 (!r.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.a, true);
                                    if (TbsDownloader.a(this.h)) {
                                        tbsLogReport = TbsLogReport.getInstance(this.h);
                                        eventType = TbsLogReport.EventType.TYPE_DOWNLOAD_DECOUPLE;
                                    } else {
                                        tbsLogReport = TbsLogReport.getInstance(this.h);
                                        eventType = TbsLogReport.EventType.TYPE_DOWNLOAD;
                                    }
                                    tbsLogReport.eventReport(eventType, this.x);
                                    this.x.resetArgs();
                                }
                                return true;
                            }
                        }
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
                z4 = false;
                i2 = i();
                if (i2 != null) {
                }
                if (!r.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.EventType eventType;
        TbsLogReport tbsLogReport;
        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)) {
                tbsLogReport = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD_DECOUPLE;
            } else {
                tbsLogReport = TbsLogReport.getInstance(this.h);
                eventType = TbsLogReport.EventType.TYPE_DOWNLOAD;
            }
            tbsLogReport.eventReport(eventType, tbsLogInfo);
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:136:0x04cf, code lost:
        if (r36 == false) goto L_0x04d1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:137:0x04d1, 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(r5));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:149:0x0515, code lost:
        if (r36 == false) goto L_0x05ac;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:151:0x052b, code lost:
        if (r36 == false) goto L_0x04d1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:164:0x0583, code lost:
        if (r36 == false) goto L_0x04d1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:172:0x05a2, code lost:
        if (r36 == false) goto L_0x04d1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:175:0x05aa, code lost:
        if (r36 == false) goto L_0x05ac;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:184:0x05f0, code lost:
        if (r36 == false) goto L_0x04d1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:185:0x05f4, code lost:
        if (r36 == false) goto L_0x05ac;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:199:0x0621, code lost:
        if (r36 == false) goto L_0x04d1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:200:0x0625, code lost:
        if (r36 == false) goto L_0x05ac;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:208:0x063d, code lost:
        if (r36 == false) goto L_0x05ac;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:210:0x064c, code lost:
        if (r36 != false) goto L_0x0725;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:227:0x06d6, code lost:
        if (r36 == false) goto L_0x05ac;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:232:0x0721, code lost:
        if (r36 == false) goto L_0x04d1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:233:0x0725, code lost:
        r7 = r14;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:379:0x09e7, code lost:
        if (r36 == false) goto L_0x0ba6;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:394:0x0a1e, code lost:
        r0 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:395:0x0a1f, code lost:
        r16 = r6;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:397:0x0a24, code lost:
        r0 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:398:0x0a25, code lost:
        r16 = r6;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:458:0x0b84, code lost:
        if (r36 == false) goto L_0x0ba6;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:460:0x0ba4, code lost:
        if (r36 == false) goto L_0x0ba6;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:461:0x0ba6, 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(r25));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:462:0x0bc0, code lost:
        r10 = r23;
        r5 = r25;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:470:0x0bdd, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:471:0x0bde, code lost:
        r3 = r0;
        r5 = r25;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:477:0x0bf7, code lost:
        r0 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:480:0x0bfd, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:481:0x0bfe, code lost:
        r23 = r10;
        r4 = r0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:542:0x0db4, 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(r5));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:54:0x021e, code lost:
        r14 = r7;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:67:0x02bd, code lost:
        if (r36 == false) goto L_0x02bf;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:68:0x02bf, 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(r5));
        com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r35.h).commit();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:72:0x02fc, code lost:
        if (r36 == false) goto L_0x02bf;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:259:0x0786 A[SYNTHETIC, Splitter:B:259:0x0786] */
    /* JADX WARNING: Removed duplicated region for block: B:298:0x0836  */
    /* JADX WARNING: Removed duplicated region for block: B:377:0x09de A[SYNTHETIC, Splitter:B:377:0x09de] */
    /* JADX WARNING: Removed duplicated region for block: B:380:0x09eb  */
    /* JADX WARNING: Removed duplicated region for block: B:394:0x0a1e A[ExcHandler: all (th java.lang.Throwable), Splitter:B:284:0x080e] */
    /* JADX WARNING: Removed duplicated region for block: B:441:0x0aee A[Catch:{ all -> 0x0bc9 }] */
    /* JADX WARNING: Removed duplicated region for block: B:470:0x0bdd A[ExcHandler: all (r0v5 'th' java.lang.Throwable A[CUSTOM_DECLARE]), PHI: r25 
      PHI: (r25v4 long) = (r25v5 long), (r25v12 long), (r25v30 long), (r25v30 long) binds: [B:467:0x0bd3, B:424:0x0a8d, B:377:0x09de, B:378:?] A[DONT_GENERATE, DONT_INLINE], Splitter:B:467:0x0bd3] */
    /* JADX WARNING: Removed duplicated region for block: B:477:0x0bf7 A[ExcHandler: all (th java.lang.Throwable), Splitter:B:78:0x030b] */
    /* JADX WARNING: Removed duplicated region for block: B:485:0x0c0b A[ADDED_TO_REGION, Catch:{ all -> 0x02ff }] */
    /* JADX WARNING: Removed duplicated region for block: B:498:0x0c82  */
    /* JADX WARNING: Removed duplicated region for block: B:526:0x0d3c  */
    /* JADX WARNING: Removed duplicated region for block: B:527:0x0d52  */
    /* JADX WARNING: Removed duplicated region for block: B:532:0x0d7a  */
    /* JADX WARNING: Removed duplicated region for block: B:536:0x0d82  */
    /* JADX WARNING: Removed duplicated region for block: B:542:0x0db4  */
    /* JADX WARNING: Removed duplicated region for block: B:546:0x0c61 A[SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:564:0x02e3 A[SYNTHETIC] */
    public void b(boolean z2, boolean z3) {
        int i2;
        int i3;
        TbsLogReport.TbsLogInfo tbsLogInfo;
        boolean z4;
        long j2;
        Throwable th;
        long j3;
        Map<String, Object> map;
        String str;
        Long valueOf;
        long j4;
        long j5;
        boolean z5;
        InputStream inputStream;
        Closeable closeable;
        Closeable closeable2;
        Throwable th2;
        Closeable closeable3;
        Closeable closeable4;
        Closeable closeable5;
        IOException iOException;
        boolean z6;
        int i4;
        String a2;
        boolean z7;
        InputStream inputStream2;
        IOException e2;
        Throwable th3;
        File file;
        boolean z8;
        byte[] bArr;
        if (!p.a().d(this.h) || z2) {
            int i5 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_RESPONSECODE, 0);
            boolean z9 = i5 == 1 || i5 == 2 || i5 == 4;
            if (z3 || !a(z2, z9)) {
                this.E = z2;
                String str2 = 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(TbsListener.ErrorCode.NONEEDTODOWN_ERROR);
                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-302);
                } else if (this.v != null && !this.t) {
                    QbSdk.n.onDownloadFinish(TbsListener.ErrorCode.NONEEDTODOWN_ERROR);
                    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) {
                        r.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)) {
                        r.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 z10 = false;
                    while (true) {
                        if (this.r > this.D) {
                            break;
                        } else if (this.s > 8) {
                            a(TbsListener.ErrorCode.DOWNLOAD_RETRYTIMES302_EXCEED, str2, 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) > JConstants.DAY) {
                                        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(TbsListener.ErrorCode.DISK_FULL, (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(TbsListener.ErrorCode.DOWNLOAD_OVER_FLOW, (String) null, true);
                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-307);
                                        }
                                        if (!FileUtil.b(this.h)) {
                                        }
                                    }
                                } catch (Throwable th4) {
                                    th = th4;
                                    th = th;
                                    j2 = downloadMaxflow;
                                    try {
                                        if ((th instanceof SSLHandshakeException) || z2 || this.b == null || !b(false)) {
                                            str2 = null;
                                            th.printStackTrace();
                                            j3 = 0;
                                            a(0L);
                                            a(TbsListener.ErrorCode.UNKNOWN_ERROR, a(th), false);
                                            if (this.t) {
                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                if (!z2) {
                                                    map = TbsDownloadConfig.getInstance(this.h).mSyncMap;
                                                    str = TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW;
                                                    valueOf = Long.valueOf(j8);
                                                    map.put(str, valueOf);
                                                    TbsDownloadConfig.getInstance(this.h).commit();
                                                }
                                                if (!this.t) {
                                                }
                                                h();
                                            }
                                        } else {
                                            TbsLog.e(TbsDownloader.LOGTAG, "[startdownload]url:" + this.k + " download exception:" + th.toString());
                                            str2 = null;
                                            a(TbsListener.ErrorCode.DOWNLOAD_THROWABLE, (String) null, true);
                                            j3 = 0;
                                        }
                                        TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-316);
                                        if (!z2) {
                                            TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j8));
                                            TbsDownloadConfig.getInstance(this.h).commit();
                                        }
                                        j6 = j3;
                                        downloadMaxflow = j2;
                                    } catch (Throwable th5) {
                                        th = th5;
                                        Throwable th6 = th;
                                        if (!z2) {
                                        }
                                        throw th6;
                                    }
                                }
                            }
                            try {
                                this.A = true;
                                String str3 = this.k != null ? this.k : this.i;
                                TbsLog.i(TbsDownloader.LOGTAG, "try url:" + str3 + ",mRetryTimes:" + this.r, true);
                                if (!str3.equals(this.j)) {
                                    this.x.setDownloadUrl(str3);
                                }
                                this.j = str3;
                                a(str3);
                                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 + "-");
                                            z4 = z10;
                                            j4 = 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("-");
                                            z4 = z10;
                                            j4 = l2;
                                            try {
                                                sb.append(this.n);
                                                httpURLConnection.setRequestProperty("Range", sb.toString());
                                            } catch (Throwable th7) {
                                                th = th7;
                                                j2 = downloadMaxflow;
                                                z10 = z4;
                                            }
                                        }
                                    } catch (Throwable th8) {
                                        th = th8;
                                        th = th;
                                        j2 = downloadMaxflow;
                                        if (th instanceof SSLHandshakeException) {
                                        }
                                        str2 = null;
                                        th.printStackTrace();
                                        j3 = 0;
                                        a(0L);
                                        a(TbsListener.ErrorCode.UNKNOWN_ERROR, a(th), false);
                                        if (this.t) {
                                        }
                                    }
                                } else {
                                    z4 = z10;
                                    j4 = 0;
                                }
                                this.x.setDownloadCancel(j4 == 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(TbsListener.ErrorCode.NETWORK_NOT_WIFI_ERROR);
                                    }
                                    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()) + j4;
                                        TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] mContentLength=" + this.n);
                                        this.x.setPkgSize(this.n);
                                        long j9 = TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0);
                                        if (j9 == 0 || this.n == j9) {
                                            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(AsyncHttpClient.ENCODING_GZIP)) {
                                                                    inputStream2 = new GZIPInputStream(inputStream);
                                                                    byte[] bArr2 = new byte[8192];
                                                                    if (z3) {
                                                                        try {
                                                                            if (r.b(this.h)) {
                                                                                File file2 = this.m;
                                                                                StringBuilder sb2 = new StringBuilder();
                                                                                j5 = 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);
                                                                                    closeable2 = new FileOutputStream(file, true);
                                                                                    long currentTimeMillis2 = System.currentTimeMillis();
                                                                                    boolean z11 = z4;
                                                                                    long j10 = currentTimeMillis;
                                                                                    long j11 = j4;
                                                                                    while (!this.t) {
                                                                                        try {
                                                                                            z5 = z11;
                                                                                            try {
                                                                                                int read = inputStream2.read(bArr2, 0, 8192);
                                                                                                if (read <= 0) {
                                                                                                    try {
                                                                                                        if (this.b == null || c(true, z9)) {
                                                                                                            this.u = true;
                                                                                                            z10 = this.b != null ? true : z5;
                                                                                                            try {
                                                                                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-311);
                                                                                                                j2 = downloadMaxflow;
                                                                                                            } catch (IOException e3) {
                                                                                                                e2 = e3;
                                                                                                            } catch (Throwable th9) {
                                                                                                                th3 = th9;
                                                                                                                closeable = inputStream2;
                                                                                                                z5 = z10;
                                                                                                                j2 = downloadMaxflow;
                                                                                                                th2 = th3;
                                                                                                                try {
                                                                                                                    a(closeable2);
                                                                                                                    a(closeable);
                                                                                                                    a(inputStream);
                                                                                                                    throw th2;
                                                                                                                } catch (Throwable th10) {
                                                                                                                }
                                                                                                            }
                                                                                                        } else {
                                                                                                            if (z2 || !b(false)) {
                                                                                                                this.u = true;
                                                                                                                j2 = downloadMaxflow;
                                                                                                                z8 = false;
                                                                                                                z10 = false;
                                                                                                            } else {
                                                                                                                j2 = downloadMaxflow;
                                                                                                                z10 = z5;
                                                                                                                z8 = true;
                                                                                                            }
                                                                                                            if (z8) {
                                                                                                                try {
                                                                                                                    a(closeable2);
                                                                                                                    a(inputStream2);
                                                                                                                    a(inputStream);
                                                                                                                } catch (Throwable th102) {
                                                                                                                }
                                                                                                            } else {
                                                                                                                z7 = z10;
                                                                                                            }
                                                                                                        }
                                                                                                    } catch (IOException e4) {
                                                                                                        e2 = e4;
                                                                                                        closeable4 = inputStream;
                                                                                                        j2 = downloadMaxflow;
                                                                                                        z10 = z5;
                                                                                                        closeable3 = closeable2;
                                                                                                        closeable5 = inputStream2;
                                                                                                        iOException = e2;
                                                                                                        try {
                                                                                                            iOException.printStackTrace();
                                                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                            }
                                                                                                            this.o = 100000;
                                                                                                            a(0L);
                                                                                                            a(103, a(iOException), false);
                                                                                                            a(closeable3);
                                                                                                            a(closeable5);
                                                                                                            a(closeable4);
                                                                                                        } catch (Throwable th11) {
                                                                                                            th2 = th11;
                                                                                                            z5 = z10;
                                                                                                            closeable = closeable5;
                                                                                                            closeable2 = closeable3;
                                                                                                            inputStream = closeable4;
                                                                                                            a(closeable2);
                                                                                                            a(closeable);
                                                                                                            a(inputStream);
                                                                                                            throw th2;
                                                                                                        }
                                                                                                    } catch (Throwable th12) {
                                                                                                        th3 = th12;
                                                                                                        closeable = inputStream2;
                                                                                                        j2 = downloadMaxflow;
                                                                                                        th2 = th3;
                                                                                                        a(closeable2);
                                                                                                        a(closeable);
                                                                                                        a(inputStream);
                                                                                                        throw th2;
                                                                                                    }
                                                                                                } else {
                                                                                                    closeable2.write(bArr2, 0, read);
                                                                                                    closeable2.flush();
                                                                                                    if (!z2) {
                                                                                                        bArr = bArr2;
                                                                                                        long j12 = j5 + ((long) read);
                                                                                                        if (j12 >= downloadMaxflow) {
                                                                                                            try {
                                                                                                                TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...failed because you exceeded max flow!", true);
                                                                                                                a(TbsListener.ErrorCode.DOWNLOAD_OVER_FLOW, "downloadFlow=" + j12 + " downloadMaxflow=" + downloadMaxflow, true);
                                                                                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-307);
                                                                                                                j5 = j12;
                                                                                                            } catch (IOException e5) {
                                                                                                                e2 = e5;
                                                                                                                j5 = j12;
                                                                                                                closeable4 = inputStream;
                                                                                                                j2 = downloadMaxflow;
                                                                                                                z10 = z5;
                                                                                                                closeable3 = closeable2;
                                                                                                                closeable5 = inputStream2;
                                                                                                                iOException = e2;
                                                                                                                iOException.printStackTrace();
                                                                                                                if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                                }
                                                                                                                this.o = 100000;
                                                                                                                a(0L);
                                                                                                                a(103, a(iOException), false);
                                                                                                                a(closeable3);
                                                                                                                a(closeable5);
                                                                                                                a(closeable4);
                                                                                                            } catch (Throwable th13) {
                                                                                                                th3 = th13;
                                                                                                                j5 = j12;
                                                                                                                j2 = downloadMaxflow;
                                                                                                                closeable = inputStream2;
                                                                                                                th2 = th3;
                                                                                                                a(closeable2);
                                                                                                                a(closeable);
                                                                                                                a(inputStream);
                                                                                                                throw th2;
                                                                                                            }
                                                                                                        } else {
                                                                                                            j5 = j12;
                                                                                                            if (!FileUtil.b(this.h)) {
                                                                                                                TbsLog.i(TbsDownloader.LOGTAG, "DownloadEnd FreeSpace too small ", true);
                                                                                                                a(TbsListener.ErrorCode.DISK_FULL, "freespace=" + r.a() + ",and minFreeSpace=" + TbsDownloadConfig.getInstance(this.h).getDownloadMinFreeSpace(), true);
                                                                                                                TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308);
                                                                                                            }
                                                                                                        }
                                                                                                        j2 = downloadMaxflow;
                                                                                                        z10 = z5;
                                                                                                        break;
                                                                                                    }
                                                                                                    bArr = bArr2;
                                                                                                    long j13 = (long) read;
                                                                                                    j10 = a(j10, j13);
                                                                                                    long currentTimeMillis3 = System.currentTimeMillis();
                                                                                                    long j14 = j4 + j13;
                                                                                                    if (currentTimeMillis3 - currentTimeMillis2 > 1000) {
                                                                                                        StringBuilder sb3 = new StringBuilder();
                                                                                                        sb3.append("#2 STEP 1/2 begin downloading...current/total=");
                                                                                                        sb3.append(j14);
                                                                                                        sb3.append("/");
                                                                                                        j2 = downloadMaxflow;
                                                                                                        try {
                                                                                                            sb3.append(this.n);
                                                                                                            TbsLog.i(TbsDownloader.LOGTAG, sb3.toString(), true);
                                                                                                            if (QbSdk.n != null) {
                                                                                                                double d2 = (double) j14;
                                                                                                                try {
                                                                                                                    double d3 = (double) this.n;
                                                                                                                    Double.isNaN(d2);
                                                                                                                    Double.isNaN(d3);
                                                                                                                    QbSdk.n.onDownloadProgress((int) ((d2 / d3) * 100.0d));
                                                                                                                } catch (IOException e6) {
                                                                                                                    e2 = e6;
                                                                                                                    closeable4 = inputStream;
                                                                                                                    z10 = z5;
                                                                                                                    closeable3 = closeable2;
                                                                                                                    closeable5 = inputStream2;
                                                                                                                    iOException = e2;
                                                                                                                    iOException.printStackTrace();
                                                                                                                    if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                                    }
                                                                                                                    this.o = 100000;
                                                                                                                    a(0L);
                                                                                                                    a(103, a(iOException), false);
                                                                                                                    a(closeable3);
                                                                                                                    a(closeable5);
                                                                                                                    a(closeable4);
                                                                                                                } catch (Throwable th14) {
                                                                                                                    th3 = th14;
                                                                                                                    closeable = inputStream2;
                                                                                                                    th2 = th3;
                                                                                                                    a(closeable2);
                                                                                                                    a(closeable);
                                                                                                                    a(inputStream);
                                                                                                                    throw th2;
                                                                                                                }
                                                                                                            }
                                                                                                            if (!z2 && j14 - j11 > 1048576) {
                                                                                                                if (!TbsDownloader.getOverSea(this.h)) {
                                                                                                                    if ((Apn.getApnType(this.h) != 3 || Apn.getApnType(this.h) == 0) && !QbSdk.getDownloadWithoutWifi()) {
                                                                                                                        b();
                                                                                                                        if (QbSdk.n != null) {
                                                                                                                            QbSdk.n.onDownloadFinish(TbsListener.ErrorCode.NETWORK_NOT_WIFI_ERROR);
                                                                                                                        }
                                                                                                                        TbsLog.i(TbsDownloader.LOGTAG, "Download is paused due to NOT_WIFI error!", false);
                                                                                                                        try {
                                                                                                                            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-304);
                                                                                                                            z10 = z5;
                                                                                                                            z8 = false;
                                                                                                                            if (z8) {
                                                                                                                            }
                                                                                                                        } catch (IOException e7) {
                                                                                                                            e2 = e7;
                                                                                                                            closeable4 = inputStream;
                                                                                                                            z10 = z5;
                                                                                                                            closeable3 = closeable2;
                                                                                                                            closeable5 = inputStream2;
                                                                                                                            iOException = e2;
                                                                                                                            iOException.printStackTrace();
                                                                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                                            }
                                                                                                                            this.o = 100000;
                                                                                                                            a(0L);
                                                                                                                            a(103, a(iOException), false);
                                                                                                                            a(closeable3);
                                                                                                                            a(closeable5);
                                                                                                                            a(closeable4);
                                                                                                                        } catch (Throwable th15) {
                                                                                                                            th3 = th15;
                                                                                                                            closeable = inputStream2;
                                                                                                                            th2 = th3;
                                                                                                                            a(closeable2);
                                                                                                                            a(closeable);
                                                                                                                            a(inputStream);
                                                                                                                            throw th2;
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                                j11 = j14;
                                                                                                            }
                                                                                                            currentTimeMillis2 = currentTimeMillis3;
                                                                                                        } catch (IOException e8) {
                                                                                                            e2 = e8;
                                                                                                            closeable4 = inputStream;
                                                                                                            z10 = z5;
                                                                                                            closeable3 = closeable2;
                                                                                                            closeable5 = inputStream2;
                                                                                                            iOException = e2;
                                                                                                            iOException.printStackTrace();
                                                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                            }
                                                                                                            this.o = 100000;
                                                                                                            a(0L);
                                                                                                            a(103, a(iOException), false);
                                                                                                            a(closeable3);
                                                                                                            a(closeable5);
                                                                                                            a(closeable4);
                                                                                                        } catch (Throwable th16) {
                                                                                                            th3 = th16;
                                                                                                            closeable = inputStream2;
                                                                                                            th2 = th3;
                                                                                                            a(closeable2);
                                                                                                            a(closeable);
                                                                                                            a(inputStream);
                                                                                                            throw th2;
                                                                                                        }
                                                                                                    } else {
                                                                                                        j2 = downloadMaxflow;
                                                                                                    }
                                                                                                    downloadMaxflow = j2;
                                                                                                    j4 = j14;
                                                                                                    z11 = z5;
                                                                                                    bArr2 = bArr;
                                                                                                }
                                                                                            } catch (IOException e9) {
                                                                                                e2 = e9;
                                                                                                j2 = downloadMaxflow;
                                                                                                closeable4 = inputStream;
                                                                                                z10 = z5;
                                                                                                closeable3 = closeable2;
                                                                                                closeable5 = inputStream2;
                                                                                                iOException = e2;
                                                                                                iOException.printStackTrace();
                                                                                                if (!(iOException instanceof SocketTimeoutException)) {
                                                                                                }
                                                                                                this.o = 100000;
                                                                                                a(0L);
                                                                                                a(103, a(iOException), false);
                                                                                                a(closeable3);
                                                                                                a(closeable5);
                                                                                                a(closeable4);
                                                                                            } catch (Throwable th17) {
                                                                                                th3 = th17;
                                                                                                j2 = downloadMaxflow;
                                                                                                closeable = inputStream2;
                                                                                                th2 = th3;
                                                                                                a(closeable2);
                                                                                                a(closeable);
                                                                                                a(inputStream);
                                                                                                throw th2;
                                                                                            }
                                                                                        } catch (IOException e10) {
                                                                                            e2 = e10;
                                                                                            z10 = z11;
                                                                                            closeable4 = inputStream;
                                                                                            j2 = downloadMaxflow;
                                                                                            closeable3 = closeable2;
                                                                                            closeable5 = inputStream2;
                                                                                            iOException = e2;
                                                                                            iOException.printStackTrace();
                                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                                            }
                                                                                            this.o = 100000;
                                                                                            a(0L);
                                                                                            a(103, a(iOException), false);
                                                                                            a(closeable3);
                                                                                            a(closeable5);
                                                                                            a(closeable4);
                                                                                        } catch (Throwable th18) {
                                                                                        }
                                                                                    }
                                                                                    TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...Canceled!", true);
                                                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                                                    z10 = z11;
                                                                                    j2 = downloadMaxflow;
                                                                                    z8 = false;
                                                                                    if (z8) {
                                                                                    }
                                                                                } catch (IOException e11) {
                                                                                    e2 = e11;
                                                                                    closeable5 = inputStream2;
                                                                                    closeable4 = inputStream;
                                                                                    j2 = downloadMaxflow;
                                                                                    z10 = z4;
                                                                                    closeable3 = null;
                                                                                    iOException = e2;
                                                                                    iOException.printStackTrace();
                                                                                    if (!(iOException instanceof SocketTimeoutException)) {
                                                                                    }
                                                                                    this.o = 100000;
                                                                                    a(0L);
                                                                                    a(103, a(iOException), false);
                                                                                    a(closeable3);
                                                                                    a(closeable5);
                                                                                    a(closeable4);
                                                                                } catch (Throwable th19) {
                                                                                    th3 = th19;
                                                                                    closeable = inputStream2;
                                                                                    j2 = downloadMaxflow;
                                                                                    z5 = z4;
                                                                                    closeable2 = null;
                                                                                    th2 = th3;
                                                                                    a(closeable2);
                                                                                    a(closeable);
                                                                                    a(inputStream);
                                                                                    throw th2;
                                                                                }
                                                                            }
                                                                        } catch (IOException e12) {
                                                                            e2 = e12;
                                                                            j5 = j8;
                                                                            closeable5 = inputStream2;
                                                                            closeable4 = inputStream;
                                                                            j2 = downloadMaxflow;
                                                                            z10 = z4;
                                                                            closeable3 = null;
                                                                            iOException = e2;
                                                                            iOException.printStackTrace();
                                                                            if (!(iOException instanceof SocketTimeoutException)) {
                                                                            }
                                                                            this.o = 100000;
                                                                            a(0L);
                                                                            a(103, a(iOException), false);
                                                                            a(closeable3);
                                                                            a(closeable5);
                                                                            a(closeable4);
                                                                        } catch (Throwable th20) {
                                                                            th3 = th20;
                                                                            j5 = j8;
                                                                            closeable = inputStream2;
                                                                            j2 = downloadMaxflow;
                                                                            z5 = z4;
                                                                            closeable2 = null;
                                                                            th2 = th3;
                                                                            a(closeable2);
                                                                            a(closeable);
                                                                            a(inputStream);
                                                                            throw th2;
                                                                        }
                                                                    }
                                                                    j5 = j8;
                                                                    try {
                                                                        file = new File(this.l, "x5.tbs.temp");
                                                                        TbsLog.i(TbsDownloader.LOGTAG, "fileDownloadApk is " + file.getAbsolutePath(), true);
                                                                        closeable2 = new FileOutputStream(file, true);
                                                                    } catch (IOException e13) {
                                                                        e2 = e13;
                                                                        j2 = downloadMaxflow;
                                                                        closeable5 = inputStream2;
                                                                        closeable4 = inputStream;
                                                                        z10 = z4;
                                                                        closeable3 = null;
                                                                        iOException = e2;
                                                                        iOException.printStackTrace();
                                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                                        }
                                                                        this.o = 100000;
                                                                        a(0L);
                                                                        a(103, a(iOException), false);
                                                                        a(closeable3);
                                                                        a(closeable5);
                                                                        a(closeable4);
                                                                    } catch (Throwable th21) {
                                                                        th3 = th21;
                                                                        j2 = downloadMaxflow;
                                                                        z5 = z4;
                                                                        closeable = inputStream2;
                                                                        closeable2 = null;
                                                                        th2 = th3;
                                                                        a(closeable2);
                                                                        a(closeable);
                                                                        a(inputStream);
                                                                        throw th2;
                                                                    }
                                                                    try {
                                                                        long currentTimeMillis22 = System.currentTimeMillis();
                                                                        boolean z112 = z4;
                                                                        long j102 = currentTimeMillis;
                                                                        long j112 = j4;
                                                                        while (!this.t) {
                                                                        }
                                                                        TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...Canceled!", true);
                                                                        TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                                        z10 = z112;
                                                                        j2 = downloadMaxflow;
                                                                        z8 = false;
                                                                        if (z8) {
                                                                        }
                                                                    } catch (IOException e14) {
                                                                        e2 = e14;
                                                                        j2 = downloadMaxflow;
                                                                        z5 = z4;
                                                                        closeable4 = inputStream;
                                                                        z10 = z5;
                                                                        closeable3 = closeable2;
                                                                        closeable5 = inputStream2;
                                                                        iOException = e2;
                                                                        iOException.printStackTrace();
                                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                                            if (!(iOException instanceof SocketException)) {
                                                                                if (z2 || FileUtil.b(this.h)) {
                                                                                    a(0L);
                                                                                    if (!k()) {
                                                                                        i4 = TbsListener.ErrorCode.FILE_DELETED;
                                                                                        a2 = a(iOException);
                                                                                        z6 = false;
                                                                                    } else {
                                                                                        z6 = false;
                                                                                        i4 = 104;
                                                                                        a2 = a(iOException);
                                                                                    }
                                                                                    a(i4, a2, z6);
                                                                                    a(closeable3);
                                                                                    a(closeable5);
                                                                                    a(closeable4);
                                                                                } else {
                                                                                    a(TbsListener.ErrorCode.DISK_FULL, "freespace=" + r.a() + ",and minFreeSpace=" + TbsDownloadConfig.getInstance(this.h).getDownloadMinFreeSpace(), true);
                                                                                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308);
                                                                                    a(closeable3);
                                                                                    a(closeable5);
                                                                                    a(closeable4);
                                                                                    if (!z2) {
                                                                                        map = TbsDownloadConfig.getInstance(this.h).mSyncMap;
                                                                                        str = TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW;
                                                                                        valueOf = Long.valueOf(j5);
                                                                                        map.put(str, valueOf);
                                                                                        TbsDownloadConfig.getInstance(this.h).commit();
                                                                                    }
                                                                                    if (!this.t) {
                                                                                    }
                                                                                    h();
                                                                                }
                                                                            }
                                                                        }
                                                                        this.o = 100000;
                                                                        a(0L);
                                                                        a(103, a(iOException), false);
                                                                        a(closeable3);
                                                                        a(closeable5);
                                                                        a(closeable4);
                                                                    } catch (Throwable th22) {
                                                                        th3 = th22;
                                                                        j2 = downloadMaxflow;
                                                                        z5 = z4;
                                                                        closeable = inputStream2;
                                                                        th2 = th3;
                                                                        a(closeable2);
                                                                        a(closeable);
                                                                        a(inputStream);
                                                                        throw th2;
                                                                    }
                                                                }
                                                            } catch (IOException e15) {
                                                                iOException = e15;
                                                                j5 = j8;
                                                                closeable4 = inputStream;
                                                                j2 = downloadMaxflow;
                                                                z10 = z4;
                                                                closeable5 = null;
                                                                closeable3 = null;
                                                                iOException.printStackTrace();
                                                                if (!(iOException instanceof SocketTimeoutException)) {
                                                                }
                                                                this.o = 100000;
                                                                a(0L);
                                                                a(103, a(iOException), false);
                                                                a(closeable3);
                                                                a(closeable5);
                                                                a(closeable4);
                                                            } catch (Throwable th23) {
                                                                th2 = th23;
                                                                j5 = j8;
                                                                j2 = downloadMaxflow;
                                                                z5 = z4;
                                                                closeable2 = null;
                                                                closeable = null;
                                                                a(closeable2);
                                                                a(closeable);
                                                                a(inputStream);
                                                                throw th2;
                                                            }
                                                        }
                                                        inputStream2 = (contentEncoding == null || !contentEncoding.contains("deflate")) ? inputStream : new InflaterInputStream(inputStream, new Inflater(true));
                                                    } catch (IOException e16) {
                                                        j5 = j8;
                                                        j2 = downloadMaxflow;
                                                        iOException = e16;
                                                        closeable4 = inputStream;
                                                        z10 = z4;
                                                        closeable5 = null;
                                                        closeable3 = null;
                                                        iOException.printStackTrace();
                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                        }
                                                        this.o = 100000;
                                                        a(0L);
                                                        a(103, a(iOException), false);
                                                        a(closeable3);
                                                        a(closeable5);
                                                        a(closeable4);
                                                    } catch (Throwable th24) {
                                                        j5 = j8;
                                                        j2 = downloadMaxflow;
                                                        z5 = z4;
                                                        th2 = th24;
                                                        closeable2 = null;
                                                        closeable = null;
                                                        a(closeable2);
                                                        a(closeable);
                                                        a(inputStream);
                                                        throw th2;
                                                    }
                                                    try {
                                                        byte[] bArr22 = new byte[8192];
                                                        if (z3) {
                                                        }
                                                        j5 = j8;
                                                        file = new File(this.l, "x5.tbs.temp");
                                                        TbsLog.i(TbsDownloader.LOGTAG, "fileDownloadApk is " + file.getAbsolutePath(), true);
                                                        closeable2 = new FileOutputStream(file, true);
                                                        long currentTimeMillis222 = System.currentTimeMillis();
                                                        boolean z1122 = z4;
                                                        long j1022 = currentTimeMillis;
                                                        long j1122 = j4;
                                                        while (!this.t) {
                                                        }
                                                        TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...Canceled!", true);
                                                        TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309);
                                                        z10 = z1122;
                                                        j2 = downloadMaxflow;
                                                        z8 = false;
                                                        if (z8) {
                                                        }
                                                    } catch (IOException e17) {
                                                        e2 = e17;
                                                        j5 = j8;
                                                        j2 = downloadMaxflow;
                                                        closeable5 = inputStream2;
                                                        closeable4 = inputStream;
                                                        z10 = z4;
                                                        closeable3 = null;
                                                        iOException = e2;
                                                        iOException.printStackTrace();
                                                        if (!(iOException instanceof SocketTimeoutException)) {
                                                        }
                                                        this.o = 100000;
                                                        a(0L);
                                                        a(103, a(iOException), false);
                                                        a(closeable3);
                                                        a(closeable5);
                                                        a(closeable4);
                                                    } catch (Throwable th25) {
                                                        th3 = th25;
                                                        j5 = j8;
                                                        j2 = downloadMaxflow;
                                                        z5 = z4;
                                                        closeable = inputStream2;
                                                        closeable2 = null;
                                                        th2 = th3;
                                                        a(closeable2);
                                                        a(closeable);
                                                        a(inputStream);
                                                        throw th2;
                                                    }
                                                } else {
                                                    j5 = j8;
                                                    j2 = downloadMaxflow;
                                                    z7 = z4;
                                                    inputStream2 = null;
                                                    closeable2 = null;
                                                }
                                            } catch (IOException e18) {
                                                j5 = j8;
                                                j2 = downloadMaxflow;
                                                iOException = e18;
                                                z10 = z4;
                                                closeable5 = null;
                                                closeable4 = null;
                                                closeable3 = null;
                                                iOException.printStackTrace();
                                                if (!(iOException instanceof SocketTimeoutException)) {
                                                }
                                                this.o = 100000;
                                                a(0L);
                                                a(103, a(iOException), false);
                                                a(closeable3);
                                                a(closeable5);
                                                a(closeable4);
                                            } catch (Throwable th26) {
                                                j5 = j8;
                                                j2 = downloadMaxflow;
                                                z5 = z4;
                                                th2 = th26;
                                                closeable2 = null;
                                                closeable = null;
                                                inputStream = null;
                                                a(closeable2);
                                                a(closeable);
                                                a(inputStream);
                                                throw th2;
                                            }
                                            try {
                                                a(closeable2);
                                                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(j5));
                                                    TbsDownloadConfig.getInstance(this.h).commit();
                                                }
                                                z10 = z7;
                                            } catch (Throwable th1022) {
                                            }
                                        } else {
                                            TbsLog.i(TbsDownloader.LOGTAG, "DownloadBegin tbsApkFileSize=" + j9 + "  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(TbsListener.ErrorCode.DOWNLOAD_FILE_CONTENTLENGTH_NOT_MATCH, "tbsApkFileSize=" + j9 + "  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, z9)) {
                                                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();
                                                    }
                                                    z10 = true;
                                                } catch (Throwable th27) {
                                                    th = th27;
                                                    j2 = downloadMaxflow;
                                                    z10 = 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();
                                    z10 = z4;
                                    str2 = null;
                                    j6 = 0;
                                }
                            } catch (Throwable th28) {
                            }
                        }
                    }
                    if (!this.t) {
                        if (this.u) {
                            if (this.b == null && !z10) {
                                if (!z3 || !r.b(this.h)) {
                                    z10 = c(true, z9);
                                } else {
                                    z10 = a(true, z9, new File(this.m, TbsDownloader.getBackupFileName(false) + "." + "tmp"));
                                }
                            }
                            this.x.setUnpkgFlag(z10 ? 1 : 0);
                            r.a("10is" + z10);
                            if (!z9) {
                                tbsLogInfo = this.x;
                                i3 = z10 ? 1 : 2;
                            } else {
                                tbsLogInfo = this.x;
                                i3 = 0;
                            }
                            tbsLogInfo.setPatchUpdateFlag(i3);
                            TbsLog.i(TbsDownloader.LOGTAG, "downloadSuccess is " + z10);
                            if (z10) {
                                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 (!z10) {
                                    instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_SUCCESS_RETRYTIMES, Integer.valueOf(instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_SUCCESS_RETRYTIMES, i2) + 1));
                                } else {
                                    int i6 = instance.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_FAILED_RETRYTIMES, i2) + 1;
                                    instance.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_FAILED_RETRYTIMES, Integer.valueOf(i6));
                                    if (i6 == instance.getDownloadFailedMaxRetrytimes()) {
                                        this.x.setDownloadCancel(2);
                                    }
                                }
                                instance.commit();
                                TbsLogReport.TbsLogInfo tbsLogInfo2 = this.x;
                                if (z10) {
                                    i2 = 1;
                                }
                                tbsLogInfo2.setDownFinalFlag(i2);
                            }
                        }
                        i2 = 0;
                        TbsDownloadConfig instance2 = TbsDownloadConfig.getInstance(this.h);
                        if (!z10) {
                        }
                        instance2.commit();
                        TbsLogReport.TbsLogInfo tbsLogInfo22 = this.x;
                        if (z10) {
                        }
                        tbsLogInfo22.setDownFinalFlag(i2);
                    }
                    h();
                } else {
                    TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] WIFI Unavailable");
                    QbSdk.n.onDownloadFinish(TbsListener.ErrorCode.NONEEDTODOWN_ERROR);
                    TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-304);
                }
            } else {
                TbsDownloader.a = false;
                QbSdk.n.onDownloadFinish(TbsListener.ErrorCode.SDCARD_HAS_BACKUP);
            }
        } else {
            TbsDownloader.a = false;
            TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-322);
            QbSdk.n.onDownloadFinish(-322);
        }
    }

    public boolean b(boolean z2) {
        String[] strArr;
        int i2;
        if ((z2 && !o() && (!QbSdk.getDownloadWithoutWifi() || !Apn.isNetworkAvailable(this.h))) || (strArr = this.b) == null || (i2 = this.c) < 0 || i2 >= strArr.length) {
            return false;
        }
        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 {
            Context context = this.h;
            return a.a(context, new File(c2, TbsDownloader.getOverSea(context) ? "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);
        }
    }
}