package com.tencent.smtt.sdk; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.util.Log; import com.google.firebase.analytics.FirebaseAnalytics; import com.lzy.okgo.model.HttpHeaders; import com.tencent.smtt.sdk.TbsDownloadConfig; 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 java.io.BufferedReader; import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.URL; import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; import javax.net.ssl.SSLHandshakeException; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes3.dex */ public class l { /* renamed from: e reason: collision with root package name */ private static int f6818e = 5; /* renamed from: f reason: collision with root package name */ private static int f6819f = 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 Handler A; private boolean D; String a; private Context h; private String i; private String j; private String k; private File l; private long m; private boolean p; private int q; private int r; private boolean s; private boolean t; private HttpURLConnection u; private String v; private TbsLogReport.TbsLogInfo w; private String x; private int y; private boolean z; /* renamed from: d reason: collision with root package name */ private boolean f6822d = false; private int n = 30000; private int o = 20000; private int C = f6818e; /* renamed from: b reason: collision with root package name */ String[] f6820b = null; /* renamed from: c reason: collision with root package name */ int f6821c = 0; private Set<String> B = new HashSet(); public l(Context context) throws NullPointerException { this.h = context.getApplicationContext(); this.w = TbsLogReport.getInstance(this.h).tbsLogInfo(); this.v = "tbs_downloading_" + this.h.getPackageName(); o.a(); this.l = o.s(this.h); if (this.l == null) { throw new NullPointerException("TbsCorePrivateDir is null!"); } g(); this.x = null; this.y = -1; } private long a(long j, long j2) { long currentTimeMillis = System.currentTimeMillis(); this.w.setDownConsumeTime(currentTimeMillis - j); this.w.setDownloadSize(j2); return currentTimeMillis; } @TargetApi(8) static File a(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 static File a(Context context, int i) { File file = new File(FileUtil.a(context, i)); 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 (Error e2) { e2.printStackTrace(); return ""; } catch (Exception e3) { e3.printStackTrace(); return ""; } } private void a(int i, String str, boolean z) { if (z || this.q > this.C) { this.w.setErrorCode(i); this.w.setFailDetail(str); } } private void a(long j) { this.q++; if (j <= 0) { try { j = n(); } catch (Exception unused) { return; } } Thread.sleep(j); } private void a(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException unused) { } } } public static void a(File file, Context context) { boolean contains; synchronized (com.tencent.smtt.utils.a.class) { if (file != null) { if (file.exists()) { if (TbsShareManager.isThirdPartyApp(context)) { return; } try { File a = a(context); if (a != null) { File file2 = TbsDownloadConfig.getInstance(context).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V_TYPE, 0) == 1 ? new File(a, TbsDownloader.getBackupFileName(true)) : new File(a, TbsDownloader.getOverSea(context) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false)); file2.delete(); FileUtil.b(file, file2); boolean contains2 = file2.getName().contains("tbs.org"); if (file2.getName().contains("x5.tbs.decouple") || contains2) { File[] listFiles = a.listFiles(); Pattern compile = Pattern.compile(com.tencent.smtt.utils.a.a(contains) + "(.*)"); for (File file3 : listFiles) { if (compile.matcher(file3.getName()).find() && file3.isFile() && file3.exists()) { file3.delete(); } } File file4 = new File(a, com.tencent.smtt.utils.a.a(contains) + "." + 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(); } if (TbsDownloadConfig.getInstance(context).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V_TYPE, 0) != 1 && TbsDownloadConfig.getInstance(context).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, 0) == com.tencent.smtt.utils.a.a(context, file)) { int i = TbsDownloadConfig.getInstance(context).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_RESPONSECODE, 0); if (i == 5 || i == 3) { TbsLog.i("TbsApkDownloader", "response code=" + i + "return backup decouple apk"); } File file5 = new File(a, TbsDownloader.getBackupFileName(true)); if (com.tencent.smtt.utils.a.a(context, file) != com.tencent.smtt.utils.a.a(context, file5)) { file5.delete(); FileUtil.b(file, file5); } } } } catch (Exception unused) { } } } } } private void a(String str) throws Exception { URL url = new URL(str); HttpURLConnection httpURLConnection = this.u; if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Throwable th) { TbsLog.e(TbsDownloader.LOGTAG, "[initHttpRequest] mHttpRequest.disconnect() Throwable:" + th.toString()); } } this.u = (HttpURLConnection) url.openConnection(); this.u.setRequestProperty(HttpHeaders.HEAD_KEY_USER_AGENT, TbsDownloader.b(this.h)); this.u.setRequestProperty(HttpHeaders.HEAD_KEY_ACCEPT_ENCODING, "identity"); this.u.setRequestMethod("GET"); this.u.setInstanceFollowRedirects(false); this.u.setConnectTimeout(this.o); this.u.setReadTimeout(this.n); } private boolean a(File file) { int i = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_USE_BACKUP_VERSION, 0); if (i == 0) { i = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0); } return com.tencent.smtt.utils.a.a(this.h, file, 0L, i); } /* JADX INFO: Access modifiers changed from: package-private */ @TargetApi(8) public static File b(Context context) { try { if (Build.VERSION.SDK_INT >= 8) { File a = a(context, 4); if (a == null) { a = a(context, 3); } if (a == null) { a = a(context, 2); } return a == null ? a(context, 1) : a; } return null; } catch (Exception e2) { e2.printStackTrace(); TbsLog.e(TbsDownloader.LOGTAG, "[TbsApkDownloader.backupApkPath] Exception is " + e2.getMessage()); return null; } } private boolean b(int i) { try { File file = new File(this.l, "x5.tbs"); File a = a(this.h); if (a != null) { File file2 = new File(a, TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false)); file.delete(); FileUtil.b(file2, file); if (com.tencent.smtt.utils.a.a(this.h, file, 0L, i)) { return true; } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.copyTbsApkFromBackupToInstall] verifyTbsApk error!!"); return false; } return false; } catch (Exception e2) { e2.printStackTrace(); TbsLog.e(TbsDownloader.LOGTAG, "[TbsApkDownloader.copyTbsApkFromBackupToInstall] Exception is " + e2.getMessage()); return false; } } public static void c(Context context) { try { o.a(); File s = o.s(context); new File(s, "x5.tbs").delete(); new File(s, "x5.tbs.temp").delete(); File a = a(context); if (a != null) { new File(a, TbsDownloader.getBackupFileName(false)).delete(); new File(a, "x5.oversea.tbs.org").delete(); File[] listFiles = a.listFiles(); Pattern compile = Pattern.compile(com.tencent.smtt.utils.a.a(true) + "(.*)"); for (File file : listFiles) { if (compile.matcher(file.getName()).find() && file.isFile() && file.exists()) { file.delete(); } } Pattern compile2 = Pattern.compile(com.tencent.smtt.utils.a.a(false) + "(.*)"); for (File file2 : listFiles) { if (compile2.matcher(file2.getName()).find() && file2.isFile() && file2.exists()) { file2.delete(); } } } } catch (Exception unused) { } } /* JADX WARN: Code restructure failed: missing block: B:21:0x0085, code lost: if (r10 != r8) goto L20; */ /* Code decompiled incorrectly, please refer to instructions dump. */ private boolean c(boolean z, boolean z2) { boolean z3; int i; TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z); File file = new File(this.l, !z ? "x5.tbs" : "x5.tbs.temp"); if (file.exists()) { Exception exc = null; String string = TbsDownloadConfig.getInstance(this.h).mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPK_MD5, null); String a = com.tencent.smtt.utils.a.a(file); if (string == null || !string.equals(a)) { TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z + " md5 failed"); if (z) { this.w.setCheckErrorDetail("fileMd5 not match"); } return false; } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] md5(" + a + ") successful!"); long j = 0; if (z) { long j2 = TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0L); if (file.exists()) { if (j2 > 0) { j = file.length(); } } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z + " filelength failed"); TbsLogReport.TbsLogInfo tbsLogInfo = this.w; tbsLogInfo.setCheckErrorDetail("fileLength:" + j + ",contentLength:" + j2); return false; } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] length(" + j + ") successful!"); int i2 = -1; if (z2 && !z && (i = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)) != (i2 = com.tencent.smtt.utils.a.a(this.h, file))) { TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z + " versionCode failed"); if (z) { TbsLogReport.TbsLogInfo tbsLogInfo2 = this.w; tbsLogInfo2.setCheckErrorDetail("fileVersion:" + i2 + ",configVersion:" + i); } return false; } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] tbsApkVersionCode(" + i2 + ") successful!"); if (z2 && !z) { String a2 = com.tencent.smtt.utils.b.a(this.h, false, file); if (!"3082023f308201a8a00302010202044c46914a300d06092a864886f70d01010505003064310b30090603550406130238363110300e060355040813074265696a696e673110300e060355040713074265696a696e673110300e060355040a130754656e63656e74310c300a060355040b13035753443111300f0603550403130873616d75656c6d6f301e170d3130303732313036313835305a170d3430303731333036313835305a3064310b30090603550406130238363110300e060355040813074265696a696e673110300e060355040713074265696a696e673110300e060355040a130754656e63656e74310c300a060355040b13035753443111300f0603550403130873616d75656c6d6f30819f300d06092a864886f70d010101050003818d0030818902818100c209077044bd0d63ea00ede5b839914cabcc912a87f0f8b390877e0f7a2583f0d5933443c40431c35a4433bc4c965800141961adc44c9625b1d321385221fd097e5bdc2f44a1840d643ab59dc070cf6c4b4b4d98bed5cbb8046e0a7078ae134da107cdf2bfc9b440fe5cb2f7549b44b73202cc6f7c2c55b8cfb0d333a021f01f0203010001300d06092a864886f70d010105050003818100b007db9922774ef4ccfee81ba514a8d57c410257e7a2eba64bfa17c9e690da08106d32f637ac41fbc9f205176c71bde238c872c3ee2f8313502bee44c80288ea4ef377a6f2cdfe4d3653c145c4acfedbfbadea23b559d41980cc3cdd35d79a68240693739aabf5c5ed26148756cf88264226de394c8a24ac35b712b120d4d23a".equals(a2)) { TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] isTempFile=" + z + " signature failed"); if (z) { TbsLogReport.TbsLogInfo tbsLogInfo3 = this.w; StringBuilder sb = new StringBuilder(); sb.append("signature:"); sb.append(a2 == null ? "null" : Integer.valueOf(a2.length())); tbsLogInfo3.setCheckErrorDetail(sb.toString()); } return false; } } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] signature successful!"); if (z) { try { z3 = file.renameTo(new File(this.l, "x5.tbs")); } catch (Exception e2) { exc = e2; z3 = false; } if (!z3) { a(109, a(exc), true); return false; } } else { z3 = false; } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.verifyTbsApk] rename(" + z3 + ") successful!"); return true; } return false; } private void d(boolean z) { Bundle a; com.tencent.smtt.utils.p.a(this.h); TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(this.h); tbsDownloadConfig.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_FULL_PACKAGE, false); tbsDownloadConfig.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD, false); tbsDownloadConfig.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_INTERRUPT_CODE_REASON, -123); tbsDownloadConfig.commit(); QbSdk.m.onDownloadFinish(z ? 100 : 120); int i = tbsDownloadConfig.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_RESPONSECODE, 0); boolean a2 = TbsDownloader.a(this.h); if (i == 5) { a = a(i, a2); if (a == null) { return; } } else if (i != 3 && i <= 10000) { o.a().a(this.h, new File(this.l, "x5.tbs").getAbsolutePath(), tbsDownloadConfig.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0)); a(new File(this.l, "x5.tbs"), this.h); return; } else { File a3 = a(this.h); if (a3 == null) { c(); tbsDownloadConfig.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD, true); tbsDownloadConfig.commit(); return; } a = a(i, a3, a2); } o.a().b(this.h, a); } private boolean e(boolean z) { TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.deleteFile] isApk=" + z); File file = z ? 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.q = 0; this.r = 0; this.m = -1L; this.k = null; this.p = false; this.s = false; this.t = false; this.z = false; } private void h() { TbsLogReport tbsLogReport; TbsLogReport.EventType eventType; TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.closeHttpRequest]"); HttpURLConnection httpURLConnection = this.u; if (httpURLConnection != null) { if (!this.s) { this.w.setResolveIp(a(httpURLConnection.getURL())); } try { this.u.disconnect(); } catch (Throwable th) { TbsLog.e(TbsDownloader.LOGTAG, "[closeHttpRequest] mHttpRequest.disconnect() Throwable:" + th.toString()); } this.u = null; } TbsLogReport.TbsLogInfo tbsLogInfo = this.w; int i = tbsLogInfo.a; if (this.s || !this.z) { if (this.f6822d) { return; } TbsDownloader.a = false; return; } tbsLogInfo.setEventTime(System.currentTimeMillis()); String apnInfo = Apn.getApnInfo(this.h); if (apnInfo == null) { apnInfo = ""; } int apnType = Apn.getApnType(this.h); this.w.setApn(apnInfo); this.w.setNetworkType(apnType); if (apnType != this.y || !apnInfo.equals(this.x)) { this.w.setNetworkChange(0); } int i2 = this.w.a; if ((i2 == 0 || i2 == 107) && this.w.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.w); this.w.resetArgs(); if (i != 100) { QbSdk.m.onDownloadFinish(i); } } 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 i = i(); if (i == null || !i.exists()) { return; } FileUtil.b(i); File[] listFiles = i.getParentFile().listFiles(); Pattern compile = Pattern.compile(com.tencent.smtt.utils.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 0L; } private boolean m() { Throwable th; InputStream inputStream; InputStreamReader inputStreamReader; BufferedReader bufferedReader; boolean z = false; try { inputStream = Runtime.getRuntime().exec("ping www.qq.com").getInputStream(); try { inputStreamReader = new InputStreamReader(inputStream); try { bufferedReader = new BufferedReader(inputStreamReader); int i = 0; do { try { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } else if (readLine.contains("TTL") || readLine.contains("ttl")) { z = true; break; } else { i++; } } catch (Throwable th2) { th = th2; try { th.printStackTrace(); return z; } finally { a(inputStream); a(inputStreamReader); a(bufferedReader); } } } while (i < 5); } catch (Throwable th3) { th = th3; bufferedReader = null; } } catch (Throwable th4) { th = th4; inputStreamReader = null; bufferedReader = null; th.printStackTrace(); return z; } } catch (Throwable th5) { th = th5; inputStream = null; } return z; } private long n() { int i = this.q; return (i == 1 || i == 2) ? i * 20000 : (i == 3 || i == 4) ? 100000L : 200000L; } /* JADX INFO: Access modifiers changed from: private */ /* JADX WARN: Code restructure failed: missing block: B:14:0x007c, code lost: if (r5 != null) goto L28; */ /* JADX WARN: Code restructure failed: missing block: B:21:0x0086, code lost: if (r5 == null) goto L16; */ /* JADX WARN: Code restructure failed: missing block: B:22:0x0088, code lost: r5.disconnect(); */ /* Code decompiled incorrectly, please refer to instructions dump. */ public boolean o() { String str; HttpURLConnection httpURLConnection; boolean z = Apn.getApnType(this.h) == 3; TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDwonloader.detectWifiNetworkAvailable] isWifi=" + z); if (z) { str = Apn.getWifiSSID(this.h); TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDwonloader.detectWifiNetworkAvailable] localBSSID=" + str); try { httpURLConnection = (HttpURLConnection) new URL("https://pms.mb.qq.com/rsp204").openConnection(); } catch (Throwable th) { th = th; httpURLConnection = null; } try { httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); httpURLConnection.setUseCaches(false); httpURLConnection.getInputStream(); int responseCode = httpURLConnection.getResponseCode(); TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDwonloader.detectWifiNetworkAvailable] responseCode=" + responseCode); r2 = responseCode == 204; } catch (Throwable th2) { th = th2; try { th.printStackTrace(); } catch (Throwable th3) { if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Exception unused) { } } throw th3; } } } else { str = null; } if (!r2 && !TextUtils.isEmpty(str) && !this.B.contains(str)) { this.B.add(str); p(); this.A.sendMessageDelayed(this.A.obtainMessage(150, str), 120000L); } if (r2 && this.B.contains(str)) { this.B.remove(str); } return r2; } private void p() { if (this.A == null) { this.A = new Handler(n.a().getLooper()) { // from class: com.tencent.smtt.sdk.l.1 @Override // android.os.Handler public void handleMessage(Message message) { if (message.what == 150) { l.this.o(); } } }; } } public Bundle a(int i, File file, boolean z) { File file2; if (z) { file2 = new File(file, TbsDownloader.getBackupFileName(true)); } else { file2 = new File(file, TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false)); } int a = com.tencent.smtt.utils.a.a(this.h, file2); File file3 = new File(this.l, "x5.tbs"); String absolutePath = file3.exists() ? file3.getAbsolutePath() : null; int i2 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0); Bundle bundle = new Bundle(); bundle.putInt("operation", i); bundle.putInt("old_core_ver", a); bundle.putInt("new_core_ver", i2); bundle.putString("old_apk_location", file2.getAbsolutePath()); bundle.putString("new_apk_location", absolutePath); bundle.putString("diff_file_location", absolutePath); return bundle; } public Bundle a(int i, boolean z) { File q; int i2; o a; Context context; int i3; o a2 = o.a(); Context context2 = this.h; if (z) { q = a2.p(context2); i2 = o.a().h(this.h); } else { q = a2.q(context2); i2 = o.a().i(this.h); } File file = new File(this.l, "x5.tbs"); String absolutePath = file.exists() ? file.getAbsolutePath() : null; if (absolutePath == null) { return null; } int i4 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0); if (z) { a = o.a(); context = this.h; i3 = 6; } else { a = o.a(); context = this.h; i3 = 5; } File f2 = a.f(context, i3); Bundle bundle = new Bundle(); bundle.putInt("operation", i); bundle.putInt("old_core_ver", i2); bundle.putInt("new_core_ver", i4); bundle.putString("old_apk_location", q.getAbsolutePath()); bundle.putString("new_apk_location", f2.getAbsolutePath()); bundle.putString("diff_file_location", absolutePath); String a3 = FileUtil.a(this.h, 7); File file2 = new File(a3); if (!file2.exists()) { file2.mkdirs(); } bundle.putString("backup_apk", new File(a3, i4 + ".tbs").getAbsolutePath()); return bundle; } public void a(int i) { if (o.a().t(this.h)) { o.a().b(); try { File file = new File(this.l, "x5.tbs"); int a = com.tencent.smtt.utils.a.a(this.h, file); if (-1 == a || (i > 0 && i == a)) { FileUtil.b(file); } } catch (Exception unused) { } } } public void a(boolean z) { b(z, false); } public boolean a() { TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup #1"); try { File file = new File(FileUtil.a(this.h, 4), TbsDownloader.getBackupFileName(true)); 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 && b2.exists()) { FileUtil.b(b2, file); } } if (com.tencent.smtt.utils.a.a(this.h, file, 0L, TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, -1))) { TbsLog.i("TbsApkDownloader", "verifyAndInstallDecoupleCoreFromBackup #3"); return o.a().e(this.h); } return false; } catch (Exception unused) { return false; } } public boolean a(boolean z, boolean z2) { String str; boolean z3; File i; TbsLogReport tbsLogReport; TbsLogReport.EventType eventType; if (Build.VERSION.SDK_INT == 23) { return false; } int i2 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_USE_BACKUP_VERSION, 0); int i3 = o.a().i(this.h); if (i2 == 0) { i2 = 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 (i2 != 0 && i2 != i3) { if (z2) { File a = TbsDownloader.a(i2); if (a != null && a.exists()) { File file = new File(FileUtil.a(this.h, 4), TbsDownloader.getOverSea(this.h) ? "x5.oversea.tbs.org" : TbsDownloader.getBackupFileName(false)); try { } catch (Exception e2) { e2.printStackTrace(); } if (TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V_TYPE, 0) != 1) { FileUtil.b(a, file); z3 = true; i = i(); if (i != null || !i.exists() || !a(i)) { j(); if (a != null && a.exists() && !com.tencent.smtt.utils.a.a(this.h, a, 0L, i2) && a != null && a.exists()) { FileUtil.b(a); } } else if (b(i2)) { TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_INTERRUPT_CODE_REASON, -214); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-214); d(false); if (z3) { 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.w); this.w.resetArgs(); } return true; } } } z3 = false; i = i(); if (i != null) { } j(); if (a != null) { FileUtil.b(a); } } if (c(false, z2)) { 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 tbsLogReport; TbsLogReport.EventType eventType; this.s = 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 WARN: Code restructure failed: missing block: B:238:0x051f, code lost: if (r39 != false) goto L152; */ /* JADX WARN: Code restructure failed: missing block: B:260:0x05c3, code lost: a(113, "tbsApkFileSize=" + r10 + " but contentLength=" + r38.m, true); com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r38.h).setDownloadInterruptCode(-310); */ /* JADX WARN: Code restructure failed: missing block: B:262:0x0603, code lost: if (r39 == false) goto L120; */ /* JADX WARN: Code restructure failed: missing block: B:263:0x0605, code lost: r29 = r4; */ /* JADX WARN: Code restructure failed: missing block: B:285:0x0676, code lost: r34 = r3; r29 = r4; */ /* JADX WARN: Code restructure failed: missing block: B:286:0x067a, code lost: r27 = r12; r10 = false; */ /* JADX WARN: Code restructure failed: missing block: B:308:0x06dd, code lost: com.tencent.smtt.utils.TbsLog.i(r8, "STEP 1/2 begin downloading...failed because you exceeded max flow!", true); a(112, "downloadFlow=" + r3 + " downloadMaxflow=" + r12, true); com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r38.h).setDownloadInterruptCode(-307); */ /* JADX WARN: Code restructure failed: missing block: B:312:0x0754, code lost: r18 = r3; */ /* JADX WARN: Code restructure failed: missing block: B:344:0x07e8, code lost: b(); */ /* JADX WARN: Code restructure failed: missing block: B:345:0x07ed, code lost: if (com.tencent.smtt.sdk.QbSdk.m == null) goto L421; */ /* JADX WARN: Code restructure failed: missing block: B:346:0x07ef, code lost: com.tencent.smtt.sdk.QbSdk.m.onDownloadFinish(111); */ /* JADX WARN: Code restructure failed: missing block: B:347:0x07f6, code lost: com.tencent.smtt.utils.TbsLog.i(r8, "Download is paused due to NOT_WIFI error!", false); */ /* JADX WARN: Code restructure failed: missing block: B:349:0x0804, code lost: com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r38.h).setDownloadInterruptCode(-304); */ /* JADX WARN: Code restructure failed: missing block: B:350:0x0807, code lost: r10 = false; */ /* JADX WARN: Code restructure failed: missing block: B:355:0x0817, code lost: r0 = th; */ /* JADX WARN: Code restructure failed: missing block: B:357:0x0819, code lost: r0 = e; */ /* JADX WARN: Code restructure failed: missing block: B:392:0x088d, code lost: a(r7); a(r5); a(r9); */ /* JADX WARN: Code restructure failed: missing block: B:393:0x0898, code lost: if (r38.t != false) goto L338; */ /* JADX WARN: Code restructure failed: missing block: B:394:0x089a, code lost: com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r38.h).setDownloadInterruptCode(-319); */ /* JADX WARN: Code restructure failed: missing block: B:395:0x08a5, code lost: if (r39 != false) goto L123; */ /* JADX WARN: Code restructure failed: missing block: B:415:0x091e, code lost: if (r39 == false) goto L121; */ /* JADX WARN: Code restructure failed: missing block: B:458:0x0a01, code lost: if (r39 == false) goto L121; */ /* JADX WARN: Code restructure failed: missing block: B:459:0x0a03, code lost: r7 = r26; */ /* JADX WARN: Code restructure failed: missing block: B:460:0x0a05, code lost: com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r38.h).mSyncMap.put(com.tencent.smtt.sdk.TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, java.lang.Long.valueOf(r18)); com.tencent.smtt.sdk.TbsDownloadConfig.getInstance(r38.h).commit(); r26 = r7; */ /* JADX WARN: Code restructure failed: missing block: B:493:0x0ad0, code lost: if (r39 != false) goto L114; */ /* JADX WARN: Code restructure failed: missing block: B:51:0x016a, code lost: r26 = r10; */ /* JADX WARN: Code restructure failed: missing block: B:69:0x0202, code lost: r7 = r10; */ /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Removed duplicated region for block: B:133:0x0375 A[Catch: all -> 0x098b, TRY_LEAVE, TryCatch #18 {all -> 0x098b, blocks: (B:105:0x0317, B:114:0x032c, B:118:0x0337, B:120:0x034a, B:122:0x034f, B:130:0x036e, B:131:0x0370, B:133:0x0375, B:135:0x0379, B:136:0x037e, B:138:0x039f, B:140:0x03a7, B:142:0x03b0, B:144:0x03b8, B:146:0x03be, B:148:0x03c5, B:149:0x03cc, B:150:0x03d2, B:152:0x03d6, B:164:0x03f7, B:166:0x0405, B:169:0x0411, B:172:0x0427, B:174:0x0435, B:184:0x0460, B:200:0x0499, B:204:0x04a3, B:206:0x04b6, B:211:0x04c9, B:221:0x04df, B:223:0x04e8, B:228:0x04fb, B:230:0x0505, B:234:0x050d, B:237:0x0514, B:191:0x047a, B:193:0x0482, B:240:0x0523, B:242:0x0560, B:244:0x0566, B:246:0x0588, B:248:0x058e, B:250:0x0594, B:252:0x059c, B:254:0x05a0, B:260:0x05c3, B:261:0x05f0, B:265:0x060d, B:123:0x0354, B:125:0x0358, B:129:0x0364, B:103:0x030c), top: B:512:0x0317 }] */ /* JADX WARN: Removed duplicated region for block: B:148:0x03c5 A[Catch: all -> 0x098b, TryCatch #18 {all -> 0x098b, blocks: (B:105:0x0317, B:114:0x032c, B:118:0x0337, B:120:0x034a, B:122:0x034f, B:130:0x036e, B:131:0x0370, B:133:0x0375, B:135:0x0379, B:136:0x037e, B:138:0x039f, B:140:0x03a7, B:142:0x03b0, B:144:0x03b8, B:146:0x03be, B:148:0x03c5, B:149:0x03cc, B:150:0x03d2, B:152:0x03d6, B:164:0x03f7, B:166:0x0405, B:169:0x0411, B:172:0x0427, B:174:0x0435, B:184:0x0460, B:200:0x0499, B:204:0x04a3, B:206:0x04b6, B:211:0x04c9, B:221:0x04df, B:223:0x04e8, B:228:0x04fb, B:230:0x0505, B:234:0x050d, B:237:0x0514, B:191:0x047a, B:193:0x0482, B:240:0x0523, B:242:0x0560, B:244:0x0566, B:246:0x0588, B:248:0x058e, B:250:0x0594, B:252:0x059c, B:254:0x05a0, B:260:0x05c3, B:261:0x05f0, B:265:0x060d, B:123:0x0354, B:125:0x0358, B:129:0x0364, B:103:0x030c), top: B:512:0x0317 }] */ /* JADX WARN: Removed duplicated region for block: B:155:0x03e5 */ /* JADX WARN: Removed duplicated region for block: B:407:0x08cd A[Catch: all -> 0x097b, TryCatch #14 {all -> 0x097b, blocks: (B:405:0x08c6, B:407:0x08cd, B:411:0x08d5, B:413:0x08dd, B:418:0x0924, B:421:0x092f, B:423:0x0938, B:428:0x094a), top: B:509:0x08c6 }] */ /* JADX WARN: Removed duplicated region for block: B:420:0x092d */ /* JADX WARN: Removed duplicated region for block: B:422:0x0935 */ /* JADX WARN: Removed duplicated region for block: B:449:0x09a2 A[ADDED_TO_REGION] */ /* JADX WARN: Removed duplicated region for block: B:490:0x0ac0 */ /* JADX WARN: Removed duplicated region for block: B:560:0x0970 A[SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:561:0x0970 A[SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:570:0x03d6 A[SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:572:0x09f6 A[EDGE_INSN: B:572:0x09f6->B:457:0x09f6 ?: BREAK , SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:581:0x0966 A[SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:582:0x0966 A[SYNTHETIC] */ /* JADX WARN: Type inference failed for: r2v3 */ /* JADX WARN: Type inference failed for: r2v34 */ /* JADX WARN: Type inference failed for: r2v35 */ /* JADX WARN: Type inference failed for: r2v4, types: [int] */ /* JADX WARN: Type inference failed for: r38v0, types: [com.tencent.smtt.sdk.l] */ /* JADX WARN: Type inference failed for: r3v7, types: [com.tencent.smtt.sdk.TbsLogReport$TbsLogInfo] */ /* Code decompiled incorrectly, please refer to instructions dump. */ public void b(boolean z, boolean z2) { boolean z3; boolean z4; int i; ?? r2; boolean z5; String str; String str2; long j; Throwable th; String str3; boolean z6; long l; HttpURLConnection httpURLConnection; String str4; String str5; Throwable th2; InputStream inputStream; IOException iOException; InputStream inputStream2; InputStream inputStream3; FileOutputStream fileOutputStream; FileOutputStream fileOutputStream2; boolean z7; String str6 = TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_STARTTIME; if (o.a().c(this.h) && !z) { TbsDownloader.a = false; TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-322); return; } int i2 = TbsDownloadConfig.getInstance(this.h).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_RESPONSECODE, 0); boolean z8 = true; boolean z9 = i2 == 1 || i2 == 2 || i2 == 4; if (!z2 && a(z, z9)) { TbsDownloader.a = false; return; } this.D = z; String str7 = 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); String str8 = TbsDownloader.LOGTAG; TbsLog.i(TbsDownloader.LOGTAG, "backupUrlStrings:" + string, true); this.f6820b = null; this.f6821c = 0; if (!z && string != null && !"".equals(string.trim())) { this.f6820b = string.trim().split(com.alipay.sdk.util.g.f556b); } TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] mDownloadUrl=" + this.i + " backupUrlStrings=" + string + " mLocation=" + this.k + " mCanceled=" + this.s + " mHttpRequest=" + this.u); if (this.i == null && this.k == null) { QbSdk.m.onDownloadFinish(110); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-302); } else if (this.u != null && !this.s) { QbSdk.m.onDownloadFinish(110); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-303); } else if (!z && this.B.contains(Apn.getWifiSSID(this.h))) { TbsLog.i(TbsDownloader.LOGTAG, "[TbsApkDownloader.startDownload] WIFI Unavailable"); QbSdk.m.onDownloadFinish(110); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-304); } else { g(); TbsLog.i(TbsDownloader.LOGTAG, "STEP 1/2 begin downloading...", true); long downloadMaxflow = TbsDownloadConfig.getInstance(this.h).getDownloadMaxflow(); long j2 = 0; long j3 = TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, 0L); this.C = z ? f6819f : f6818e; long j4 = j3; boolean z10 = false; while (true) { if (this.q > this.C) { break; } else if (this.r > 8) { a(123, str7, z8); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-306); break; } else { long currentTimeMillis = System.currentTimeMillis(); if (!z) { try { if (currentTimeMillis - TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(str6, j2) > 86400000) { try { TbsLog.i(str8, "[TbsApkDownloader.startDownload] OVER DOWNLOAD_PERIOD"); TbsDownloadConfig.getInstance(this.h).mSyncMap.put(str6, Long.valueOf(currentTimeMillis)); TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j2)); TbsDownloadConfig.getInstance(this.h).commit(); } catch (Throwable th3) { th = th3; str = str6; z4 = z9; z3 = z10; str2 = str8; j = downloadMaxflow; th = th; try { if (th instanceof SSLHandshakeException) { } str3 = null; th.printStackTrace(); j2 = 0; a(0L); a(107, a(th), false); if (this.s) { } } catch (Throwable th4) { if (!z) { TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); } throw th4; } } } else { StringBuilder sb = new StringBuilder(); sb.append("[TbsApkDownloader.startDownload] downloadFlow="); j2 = j4; try { sb.append(j2); TbsLog.i(str8, sb.toString()); if (j2 >= downloadMaxflow) { TbsLog.i(str8, "STEP 1/2 begin downloading...failed because you exceeded max flow!", z8); a(112, str7, z8); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-307); if (!z) { z4 = z9; j4 = j2; } } } catch (Throwable th5) { th = th5; str = str6; z4 = z9; j4 = j2; z3 = z10; str2 = str8; j = downloadMaxflow; th = th; if ((th instanceof SSLHandshakeException) || z || this.f6820b == null || !b(false)) { str3 = null; th.printStackTrace(); j2 = 0; a(0L); a(107, a(th), false); if (this.s) { TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309); } else { z6 = z4; } } else { TbsLog.e(str2, "[startdownload]url:" + this.k + " download exception:" + th.toString()); str3 = null; a(125, null, true); z6 = z4; j2 = 0; } TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-316); } } j4 = j2; if (!FileUtil.b(this.h)) { TbsLog.i(str8, "DownloadBegin FreeSpace too small", z8); a(105, str7, z8); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308); if (!z) { z4 = z9; } } } catch (Throwable th6) { th = th6; } } this.z = z8; String str9 = this.k != null ? this.k : this.i; TbsLog.i(str8, "try url:" + str9 + ",mRetryTimes:" + this.q, z8); if (!str9.equals(this.j)) { this.w.setDownloadUrl(str9); } this.j = str9; a(str9); if (this.p) { z3 = z10; str2 = str8; l = 0; } else { try { l = l(); TbsLog.i(str8, "[TbsApkDownloader.startDownload] range=" + l); boolean z11 = z10; try { if (this.m <= 0) { try { TbsLog.i(str8, "STEP 1/2 begin downloading...current" + l, z8); httpURLConnection = this.u; str4 = "bytes=" + l + "-"; str5 = HttpHeaders.HEAD_KEY_RANGE; z3 = z11; str2 = str8; } catch (Throwable th7) { th = th7; str = str6; z4 = z9; z3 = z11; str2 = str8; j = downloadMaxflow; th = th; if (th instanceof SSLHandshakeException) { } str3 = null; th.printStackTrace(); j2 = 0; a(0L); a(107, a(th), false); if (this.s) { } } } else { StringBuilder sb2 = new StringBuilder(); sb2.append("#1 STEP 1/2 begin downloading...current/total="); sb2.append(l); sb2.append("/"); z3 = z11; try { sb2.append(this.m); TbsLog.i(str8, sb2.toString(), true); httpURLConnection = this.u; StringBuilder sb3 = new StringBuilder(); sb3.append("bytes="); sb3.append(l); sb3.append("-"); str2 = str8; sb3.append(this.m); str4 = sb3.toString(); str5 = HttpHeaders.HEAD_KEY_RANGE; } catch (Throwable th8) { th = th8; str2 = str8; str = str6; z4 = z9; j = downloadMaxflow; th = th; if (th instanceof SSLHandshakeException) { } str3 = null; th.printStackTrace(); j2 = 0; a(0L); a(107, a(th), false); if (this.s) { } } } try { httpURLConnection.setRequestProperty(str5, str4); } catch (Throwable th9) { th = th9; str = str6; z4 = z9; j = downloadMaxflow; th = th; if (th instanceof SSLHandshakeException) { } str3 = null; th.printStackTrace(); j2 = 0; a(0L); a(107, a(th), false); if (this.s) { } } } catch (Throwable th10) { th = th10; z3 = z11; } } catch (Throwable th11) { th = th11; z3 = z10; } } this.w.setDownloadCancel(l == 0 ? 0 : 1); int apnType = Apn.getApnType(this.h); String apnInfo = Apn.getApnInfo(this.h); if (this.x == null && this.y == -1) { this.x = apnInfo; } else { if (apnType == this.y && apnInfo.equals(this.x)) { if (this.q >= 1) { this.u.addRequestProperty("Referer", this.i); } int responseCode = this.u.getResponseCode(); TbsLog.i(str2, "[TbsApkDownloader.startDownload] responseCode=" + responseCode); this.w.setHttpCode(responseCode); if (!z && !TbsDownloader.getOverSea(this.h) && ((Apn.getApnType(this.h) != 3 || Apn.getApnType(this.h) == 0) && !QbSdk.getDownloadWithoutWifi())) { b(); if (QbSdk.m != null) { QbSdk.m.onDownloadFinish(111); } TbsLog.i(str2, "Download is canceled due to NOT_WIFI error!", false); } if (this.s) { if (responseCode != 200 && responseCode != 206) { 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); z4 = z9; if (z) { z3 = true; } else { boolean z12 = true; } } catch (Throwable th12) { th = th12; str = str6; z4 = z9; j = downloadMaxflow; z3 = true; th = th; if (th instanceof SSLHandshakeException) { } str3 = null; th.printStackTrace(); j2 = 0; a(0L); a(107, a(th), false); if (this.s) { } } } else { e(false); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-313); if (!z) { } } } else if ((responseCode == 403 || responseCode == 406) && this.m == -1) { TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-314); if (!z) { } } else if (responseCode == 202) { if (z) { str8 = str2; z10 = z3; j2 = 0; z8 = true; str7 = null; } else { str = str6; z6 = z9; j = downloadMaxflow; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } else if (this.q < this.C && responseCode == 503) { a(Long.parseLong(this.u.getHeaderField("Retry-After"))); if (this.s) { TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309); if (!z) { } } else if (z) { str8 = str2; z10 = z3; j2 = 0; z8 = true; str7 = null; } else { str = str6; z6 = z9; j = downloadMaxflow; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } else if (this.q < this.C && (responseCode == 408 || responseCode == 504 || responseCode == 502 || responseCode == 408)) { a(0L); if (this.s) { TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309); if (!z) { } } else if (z) { str8 = str2; z10 = z3; j2 = 0; z8 = true; str7 = null; } else { str = str6; z6 = z9; j = downloadMaxflow; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } else if (l() > 0 || this.p || responseCode == 410) { break; } else { this.p = true; if (z) { str8 = str2; z10 = z3; j2 = 0; z8 = true; str7 = null; } else { str = str6; z6 = z9; j = downloadMaxflow; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } } else { String headerField = this.u.getHeaderField(HttpHeaders.HEAD_KEY_LOCATION); if (TextUtils.isEmpty(headerField)) { a(124, null, true); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-312); if (!z) { } } else { this.k = headerField; this.r++; if (z) { str8 = str2; z10 = z3; j2 = 0; z8 = true; str7 = null; } else { str = str6; z6 = z9; j = downloadMaxflow; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } } } this.m = this.u.getContentLength() + l; TbsLog.i(str2, "[TbsApkDownloader.startDownload] mContentLength=" + this.m); this.w.setPkgSize(this.m); long j5 = l; long j6 = TbsDownloadConfig.getInstance(this.h).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSAPKFILESIZE, 0L); if (j6 == 0 || this.m == j6) { TbsLog.i(str2, "[TbsApkDownloader.startDownload] begin readResponse"); try { inputStream = this.u.getInputStream(); } catch (IOException e2) { str = str6; z4 = z9; j = downloadMaxflow; iOException = e2; inputStream = null; } catch (Throwable th13) { th2 = th13; inputStream = null; } if (inputStream == null) { z4 = z9; inputStream2 = null; fileOutputStream = null; break; } try { String contentEncoding = this.u.getContentEncoding(); inputStream2 = (contentEncoding == null || !contentEncoding.contains("gzip")) ? (contentEncoding == null || !contentEncoding.contains("deflate")) ? inputStream : new InflaterInputStream(inputStream, new Inflater(true)) : new GZIPInputStream(inputStream); } catch (IOException e3) { str = str6; z4 = z9; j = downloadMaxflow; iOException = e3; inputStream3 = inputStream; inputStream2 = null; fileOutputStream2 = null; try { iOException.printStackTrace(); if (!(iOException instanceof SocketTimeoutException)) { } this.n = com.google.android.exoplayer2.e0.q.a.q; a(0L); a(103, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { } } catch (Throwable th14) { th2 = th14; fileOutputStream = fileOutputStream2; inputStream = inputStream3; a(fileOutputStream); a(inputStream2); a(inputStream); throw th2; } } catch (Throwable th15) { th2 = th15; inputStream2 = null; fileOutputStream = null; a(fileOutputStream); a(inputStream2); a(inputStream); throw th2; } try { byte[] bArr = new byte[8192]; boolean z13 = true; fileOutputStream = new FileOutputStream(new File(this.l, "x5.tbs.temp"), true); try { long currentTimeMillis2 = System.currentTimeMillis(); long j7 = currentTimeMillis; long j8 = j5; while (true) { if (this.s) { TbsLog.i(str2, "STEP 1/2 begin downloading...Canceled!", z13); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309); break; } int read = inputStream2.read(bArr, 0, 8192); if (read > 0) { fileOutputStream.write(bArr, 0, read); fileOutputStream.flush(); str = str6; z4 = z9; if (!z) { long j9 = j4 + read; if (j9 >= downloadMaxflow) { try { break; } catch (IOException e4) { e = e4; j4 = j9; j = downloadMaxflow; iOException = e; inputStream3 = inputStream; fileOutputStream2 = fileOutputStream; iOException.printStackTrace(); if (!(iOException instanceof SocketTimeoutException)) { } this.n = com.google.android.exoplayer2.e0.q.a.q; a(0L); a(103, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { } } catch (Throwable th16) { th = th16; th2 = th; a(fileOutputStream); a(inputStream2); a(inputStream); throw th2; } } else if (!FileUtil.b(this.h)) { TbsLog.i(str2, "DownloadEnd FreeSpace too small ", true); a(105, "freespace=" + com.tencent.smtt.utils.p.a() + ",and minFreeSpace=" + TbsDownloadConfig.getInstance(this.h).getDownloadMinFreeSpace(), true); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308); break; } else { j4 = j9; } } long j10 = read; try { j7 = a(j7, j10); long currentTimeMillis3 = System.currentTimeMillis(); long j11 = j5 + j10; if (currentTimeMillis3 - currentTimeMillis2 > 1000) { StringBuilder sb4 = new StringBuilder(); sb4.append("#2 STEP 1/2 begin downloading...current/total="); sb4.append(j11); sb4.append("/"); j = downloadMaxflow; try { sb4.append(this.m); TbsLog.i(str2, sb4.toString(), true); if (QbSdk.m != null) { try { QbSdk.m.onDownloadProgress((int) ((j11 / this.m) * 100.0d)); } catch (IOException e5) { iOException = e5; inputStream3 = inputStream; fileOutputStream2 = fileOutputStream; iOException.printStackTrace(); if (!(iOException instanceof SocketTimeoutException)) { if (z) { } a(0L); a(k() ? 106 : 104, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { } } this.n = com.google.android.exoplayer2.e0.q.a.q; a(0L); a(103, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { } } catch (Throwable th17) { th2 = th17; a(fileOutputStream); a(inputStream2); a(inputStream); throw th2; } } if (!z && j11 - j8 > 1048576) { if (!TbsDownloader.getOverSea(this.h) && ((Apn.getApnType(this.h) != 3 || Apn.getApnType(this.h) == 0) && !QbSdk.getDownloadWithoutWifi())) { break; } j8 = j11; } currentTimeMillis2 = currentTimeMillis3; } catch (IOException e6) { e = e6; iOException = e; inputStream3 = inputStream; fileOutputStream2 = fileOutputStream; iOException.printStackTrace(); if (!(iOException instanceof SocketTimeoutException)) { } this.n = com.google.android.exoplayer2.e0.q.a.q; a(0L); a(103, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { } } catch (Throwable th18) { th = th18; th2 = th; a(fileOutputStream); a(inputStream2); a(inputStream); throw th2; } } else { j = downloadMaxflow; } downloadMaxflow = j; z13 = true; j5 = j11; z9 = z4; str6 = str; } catch (IOException e7) { e = e7; j = downloadMaxflow; iOException = e; inputStream3 = inputStream; fileOutputStream2 = fileOutputStream; iOException.printStackTrace(); if (!(iOException instanceof SocketTimeoutException) && !(iOException instanceof SocketException)) { if (!z || FileUtil.b(this.h)) { a(0L); a(k() ? 106 : 104, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { z6 = z4; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } else { str8 = str2; z10 = z3; downloadMaxflow = j; z9 = z4; str6 = str; j2 = 0; z8 = true; str7 = null; } } else { a(105, "freespace=" + com.tencent.smtt.utils.p.a() + ",and minFreeSpace=" + TbsDownloadConfig.getInstance(this.h).getDownloadMinFreeSpace(), true); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-308); a(fileOutputStream2); a(inputStream2); a(inputStream3); } } this.n = com.google.android.exoplayer2.e0.q.a.q; a(0L); a(103, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { str8 = str2; z10 = z3; downloadMaxflow = j; z9 = z4; str6 = str; j2 = 0; z8 = true; str7 = null; } else { z6 = z4; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } catch (Throwable th19) { th = th19; th2 = th; a(fileOutputStream); a(inputStream2); a(inputStream); throw th2; } } else if (this.f6820b == null || c(true, z9)) { this.t = true; if (this.f6820b != null) { z3 = true; } TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-311); } else if (z || !b(false)) { this.t = true; str = str6; z4 = z9; j = downloadMaxflow; z7 = false; z3 = false; } else { str = str6; z4 = z9; j = downloadMaxflow; z7 = true; } } } catch (IOException e8) { e = e8; str = str6; z4 = z9; } catch (Throwable th20) { th = th20; } } catch (IOException e9) { str = str6; z4 = z9; j = downloadMaxflow; iOException = e9; inputStream3 = inputStream; fileOutputStream2 = null; iOException.printStackTrace(); if (!(iOException instanceof SocketTimeoutException)) { } this.n = com.google.android.exoplayer2.e0.q.a.q; a(0L); a(103, a(iOException), false); a(fileOutputStream2); a(inputStream2); a(inputStream3); if (z) { } } catch (Throwable th21) { th2 = th21; fileOutputStream = null; a(fileOutputStream); a(inputStream2); a(inputStream); throw th2; } if (!z7) { break; } try { a(fileOutputStream); a(inputStream2); a(inputStream); } catch (Throwable th22) { th = th22; th = th; if (th instanceof SSLHandshakeException) { } str3 = null; th.printStackTrace(); j2 = 0; a(0L); a(107, a(th), false); if (this.s) { } } if (z) { str8 = str2; z10 = z3; downloadMaxflow = j; z9 = z4; str6 = str; j2 = 0; z8 = true; str7 = null; } else { z6 = z4; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } else { TbsLog.i(str2, "DownloadBegin tbsApkFileSize=" + j6 + " but contentLength=" + this.m, true); if (z || !(o() || (QbSdk.getDownloadWithoutWifi() && Apn.isNetworkAvailable(this.h)))) { break; } else if (this.f6820b == null || !b(false)) { break; } else if (z) { str8 = str2; z10 = z3; j2 = 0; z8 = true; str7 = null; } else { str = str6; z6 = z9; j = downloadMaxflow; str3 = null; j2 = 0; TbsDownloadConfig.getInstance(this.h).mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, Long.valueOf(j4)); TbsDownloadConfig.getInstance(this.h).commit(); str7 = str3; z9 = z6; str8 = str2; z10 = z3; downloadMaxflow = j; str6 = str; z8 = true; } } } else { TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-309); if (!z) { } } } this.w.setNetworkChange(0); this.x = apnInfo; } this.y = apnType; if (this.q >= 1) { } int responseCode2 = this.u.getResponseCode(); TbsLog.i(str2, "[TbsApkDownloader.startDownload] responseCode=" + responseCode2); this.w.setHttpCode(responseCode2); if (!z) { b(); if (QbSdk.m != null) { } TbsLog.i(str2, "Download is canceled due to NOT_WIFI error!", false); } if (this.s) { } } } z4 = z9; if (!this.s) { if (this.t) { if (this.f6820b != null || z3) { z5 = z4; } else { z5 = z4; z3 = c(true, z5); } boolean z14 = z3; this.w.setUnpkgFlag(z14 ? 1 : 0); this.w.setPatchUpdateFlag(!z5 ? z14 ? 1 : 2 : 0); if (z14) { d(true); TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-317); a(100, FirebaseAnalytics.b.F, true); i = 0; r2 = z14; } else { TbsDownloadConfig.getInstance(this.h).setDownloadInterruptCode(-318); i = 0; e(false); r2 = z14; } } else { i = 0; r2 = z3; } TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(this.h); if (r2 != 0) { tbsDownloadConfig.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_SUCCESS_RETRYTIMES, Integer.valueOf(tbsDownloadConfig.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_SUCCESS_RETRYTIMES, i) + 1)); } else { int i3 = tbsDownloadConfig.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_FAILED_RETRYTIMES, i) + 1; tbsDownloadConfig.mSyncMap.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_FAILED_RETRYTIMES, Integer.valueOf(i3)); if (i3 == tbsDownloadConfig.getDownloadFailedMaxRetrytimes()) { this.w.setDownloadCancel(2); } } tbsDownloadConfig.commit(); this.w.setDownFinalFlag(r2); } h(); } } public boolean b(boolean z) { String[] strArr; int i; if ((!z || o() || (QbSdk.getDownloadWithoutWifi() && Apn.isNetworkAvailable(this.h))) && (strArr = this.f6820b) != null && (i = this.f6821c) >= 0 && i < strArr.length) { this.f6821c = i + 1; this.k = strArr[i]; this.q = 0; this.r = 0; this.m = -1L; this.p = false; this.s = false; this.t = false; this.z = false; return true; } return false; } public int c(boolean z) { File a = a(this.h); if (z) { if (a == null) { return 0; } return com.tencent.smtt.utils.a.a(this.h, new File(a, TbsDownloader.getBackupFileName(true))); } else if (a == null) { return 0; } else { Context context = this.h; return com.tencent.smtt.utils.a.a(context, new File(a, 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.D); return this.D; } /* JADX INFO: Access modifiers changed from: package-private */ public void e() { TbsLog.i(TbsDownloader.LOGTAG, "pauseDownload,isPause=" + this.f6822d + "isDownloading=" + TbsDownloader.isDownloading()); if (this.f6822d || !TbsDownloader.isDownloading()) { return; } b(); this.f6822d = true; this.z = false; } /* JADX INFO: Access modifiers changed from: package-private */ public void f() { TbsLog.i(TbsDownloader.LOGTAG, "resumeDownload,isPause=" + this.f6822d + "isDownloading=" + TbsDownloader.isDownloading()); if (this.f6822d && TbsDownloader.isDownloading()) { this.f6822d = false; a(false); } } }