package com.huawei.updatesdk.b.d; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import com.huawei.updatesdk.a.a.d.d; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.net.ssl.HttpsURLConnection; public class c extends AsyncTask<Void, Void, Void> { private String a; private HttpsURLConnection b; private boolean c = false; private b d; private Handler e = new b(null); /* access modifiers changed from: package-private */ public class a implements Runnable { a() { } public void run() { if (c.this.b != null) { c.this.c = true; c.this.b.disconnect(); } d.a(new File(c.e())); } } private static class b extends Handler { private b() { } /* synthetic */ b(a aVar) { this(); } public void handleMessage(Message message) { super.handleMessage(message); Object obj = message.obj; if (obj instanceof b) { b bVar = (b) obj; Intent intent = new Intent(); com.huawei.updatesdk.a.b.b.b a = com.huawei.updatesdk.a.b.b.b.a(intent); int i = message.what; if (i != 2) { if (i != 3) { if (i == 4) { Context a2 = com.huawei.updatesdk.a.b.a.a.c().a(); e.a(a2, c.e() + "/appmarket.apk", bVar.c()); } else if (i != 5) { return; } } Bundle bundle = new Bundle(); bundle.putInt("download_status_param", message.what); intent.putExtras(bundle); com.huawei.updatesdk.b.f.c.b().b(a); return; } intent.putExtra("download_apk_size", bVar.e()); intent.putExtra("download_apk_already", bVar.a()); com.huawei.updatesdk.b.f.c.b().a(a); } } } public c(b bVar) { this.d = bVar; if (!TextUtils.isEmpty(bVar.c())) { this.a = bVar.c(); } } private void a(int i, long j, String str, int i2) { b bVar = new b(); bVar.a(i); bVar.a(j); bVar.a(str); Handler handler = this.e; if (handler != null) { handler.sendMessage(handler.obtainMessage(i2, bVar)); } } /* JADX WARNING: Removed duplicated region for block: B:25:0x0065 */ /* JADX WARNING: Removed duplicated region for block: B:30:0x0081 */ /* JADX WARNING: Removed duplicated region for block: B:34:0x008b A[SYNTHETIC, Splitter:B:34:0x008b] */ /* JADX WARNING: Removed duplicated region for block: B:40:? A[RETURN, SYNTHETIC] */ /* JADX WARNING: Removed duplicated region for block: B:41:? A[RETURN, SYNTHETIC] */ private void a(b bVar, String str) { Throwable th; FileNotFoundException e2; Exception e3; if (bVar != null && !TextUtils.isEmpty(bVar.b())) { String str2 = str + "/appmarket.apk"; FileOutputStream fileOutputStream = null; try { FileOutputStream fileOutputStream2 = new FileOutputStream(new File(str2)); try { if (!a(bVar, fileOutputStream2, str2)) { d.a(new File(e())); } try { fileOutputStream2.close(); } catch (IOException unused) { com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "Close FileOutputStream failed!"); } } catch (FileNotFoundException e4) { e2 = e4; fileOutputStream = fileOutputStream2; com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "DOWNLOAD market package FileNotFoundException error:" + e2.toString()); if (fileOutputStream == null) { fileOutputStream.close(); } } catch (Exception e5) { e3 = e5; fileOutputStream = fileOutputStream2; try { com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "DOWNLOAD market package Exception error:" + e3.toString()); if (fileOutputStream == null) { fileOutputStream.close(); } } catch (Throwable th2) { th = th2; if (fileOutputStream != null) { } throw th; } } catch (Throwable th3) { th = th3; fileOutputStream = fileOutputStream2; if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException unused2) { com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "Close FileOutputStream failed!"); } } throw th; } } catch (FileNotFoundException e6) { e2 = e6; com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "DOWNLOAD market package FileNotFoundException error:" + e2.toString()); if (fileOutputStream == null) { } } catch (Exception e7) { e3 = e7; com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "DOWNLOAD market package Exception error:" + e3.toString()); if (fileOutputStream == null) { } } } } private void a(InputStream inputStream, OutputStream outputStream) { if (inputStream != null) { try { inputStream.close(); } catch (IOException e2) { com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e2.toString()); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e3) { com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e3.toString()); } } } /* JADX WARNING: Removed duplicated region for block: B:120:0x0186 */ /* JADX WARNING: Removed duplicated region for block: B:131:0x01a1 */ /* JADX WARNING: Removed duplicated region for block: B:64:0x00f3 A[SYNTHETIC, Splitter:B:64:0x00f3] */ /* JADX WARNING: Removed duplicated region for block: B:78:0x0119 */ /* JADX WARNING: Removed duplicated region for block: B:80:0x011d */ private boolean a(b bVar, OutputStream outputStream, String str) { InputStream inputStream; long j; int i; Throwable th; int i2; long j2; int i3; boolean z; Exception e2; BufferedOutputStream bufferedOutputStream; Throwable th2; boolean z2; int i4; int i5; String str2; int i6; long j3; int i7; boolean z3 = false; OutputStream outputStream2 = null; try { HttpsURLConnection a2 = d.a().a(bVar.b()); this.b = a2; if (a2 == null) { boolean z4 = this.c; str2 = this.a; i6 = 0; j3 = -1; i7 = z4 ? 3 : 5; } else { a2.connect(); int responseCode = this.b.getResponseCode(); if (200 == responseCode || 206 == responseCode) { long j4 = bVar.e(); try { inputStream = new BufferedInputStream(this.b.getInputStream(), 8192); try { bufferedOutputStream = new BufferedOutputStream(outputStream, 8192); try { byte[] bArr = new byte[8192]; long j5 = 0; i2 = 0; while (true) { try { int read = inputStream.read(bArr); if (read != -1) { try { bufferedOutputStream.write(bArr, 0, read); i2 += read; if (Math.abs(System.currentTimeMillis() - j5) > 1000) { long currentTimeMillis = System.currentTimeMillis(); if (((long) i2) != j4) { i5 = i2; try { a(i2, j4, this.a, 2); } catch (Throwable th3) { th2 = th3; i2 = i5; } } else { i5 = i2; } j5 = currentTimeMillis; i2 = i5; } } catch (Throwable th4) { th2 = th4; try { a(inputStream, bufferedOutputStream); try { throw th2; } catch (Exception e3) { e2 = e3; inputStream = null; } catch (Throwable th5) { th = th5; inputStream = null; i = i2; j = j4; if (!z3) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } } catch (Exception e4) { e2 = e4; outputStream2 = bufferedOutputStream; i3 = i2; j2 = j4; z = false; try { com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e2.toString()); if (!z) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); return false; } catch (Throwable th6) { th = th6; z3 = z; i2 = i3; j4 = j2; i = i2; j = j4; if (!z3) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } } catch (Throwable th7) { th = th7; outputStream2 = bufferedOutputStream; i = i2; j = j4; if (!z3) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } } } else { try { break; } catch (Exception e5) { e2 = e5; outputStream2 = bufferedOutputStream; i3 = i2; j2 = j4; z = false; com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e2.toString()); if (!z) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); return false; } catch (Throwable th8) { th = th8; outputStream2 = bufferedOutputStream; i = i2; j = j4; if (!z3) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } } } catch (Throwable th9) { th2 = th9; a(inputStream, bufferedOutputStream); throw th2; } } a(inputStream, bufferedOutputStream); if (((long) i2) == j4 && j4 > 0) { try { if (!TextUtils.isEmpty(bVar.d()) && bVar.d().equalsIgnoreCase(d.a(str, "SHA-256"))) { z2 = true; if (!z2) { try { i4 = i2; try { a(i2, j4, this.a, 4); } catch (Exception e6) { e2 = e6; } catch (Throwable th10) { th = th10; z3 = z2; i2 = i4; inputStream = null; i = i2; j = j4; if (!z3) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } } catch (Exception e7) { e2 = e7; i4 = i2; j2 = j4; z = z2; i3 = i4; inputStream = null; com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e2.toString()); if (!z) { a(i3, j2, this.a, this.c ? 3 : 5); } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); return false; } catch (Throwable th11) { th = th11; z3 = z2; inputStream = null; i = i2; j = j4; if (!z3) { a(i, j, this.a, this.c ? 3 : 5); } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } } else { i4 = i2; } if (!z2) { a(i4, j4, this.a, this.c ? 3 : 5); } a((InputStream) null, (OutputStream) null); com.huawei.updatesdk.b.g.b.a().remove(this); d(); return z2; } } catch (Exception e8) { e2 = e8; i3 = i2; inputStream = null; j2 = j4; z = false; com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e2.toString()); if (!z) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); return false; } catch (Throwable th12) { th = th12; i = i2; inputStream = null; j = j4; if (!z3) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } } z2 = false; if (!z2) { } if (!z2) { } a((InputStream) null, (OutputStream) null); com.huawei.updatesdk.b.g.b.a().remove(this); d(); return z2; } catch (Throwable th13) { th2 = th13; i2 = 0; a(inputStream, bufferedOutputStream); throw th2; } } catch (Throwable th14) { th2 = th14; bufferedOutputStream = null; i2 = 0; a(inputStream, bufferedOutputStream); throw th2; } } catch (Throwable th15) { th2 = th15; inputStream = null; bufferedOutputStream = null; i2 = 0; a(inputStream, bufferedOutputStream); throw th2; } } else if (this.c) { i6 = 0; j3 = -1; str2 = this.a; i7 = 3; } else { str2 = this.a; i6 = 0; j3 = -1; i7 = 5; } } a(i6, j3, str2, i7); a((InputStream) null, (OutputStream) null); } catch (Exception e9) { e2 = e9; j2 = -1; inputStream = null; z = false; i3 = 0; com.huawei.updatesdk.a.a.c.a.a.a.b("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e2.toString()); if (!z) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); return false; } catch (Throwable th16) { th = th16; j = -1; inputStream = null; i = 0; if (!z3) { } a(inputStream, outputStream2); com.huawei.updatesdk.b.g.b.a().remove(this); d(); throw th; } com.huawei.updatesdk.b.g.b.a().remove(this); d(); return false; } private void d() { HttpsURLConnection httpsURLConnection = this.b; if (httpsURLConnection != null) { httpsURLConnection.disconnect(); } } /* access modifiers changed from: private */ public static String e() { String b2 = com.huawei.updatesdk.a.b.a.a.c().b(); if (TextUtils.isEmpty(b2)) { return ""; } String str = b2 + "/updatesdk"; File file = new File(str); return (file.exists() || file.mkdirs()) ? str : ""; } /* access modifiers changed from: protected */ /* renamed from: a */ public Void doInBackground(Void... voidArr) { com.huawei.updatesdk.b.g.b.a(this); a(this.d, e()); return null; } public void a() { new Thread(new a()).start(); } public void b() { this.e = null; } /* access modifiers changed from: protected */ public void onCancelled() { super.onCancelled(); a(); } }