package com.huawei.hms.availableupdate; import android.content.Context; import android.os.Environment; import android.text.TextUtils; import com.huawei.hms.support.log.HMSLog; import com.huawei.hms.update.provider.UpdateProvider; import com.huawei.hms.utils.Checker; import com.huawei.hms.utils.FileUtil; import com.huawei.hms.utils.IOUtils; import com.meizu.cloud.pushsdk.constants.PushConstants; import com.xiaomi.mipush.sdk.Constants; import java.io.File; import java.io.IOException; import java.io.OutputStream; /* compiled from: UpdateDownload */ public class e implements f { public final Context a; public final l b = new k(); public final b c = new b(); public g d; public File e; /* compiled from: UpdateDownload */ public class a extends c { public long b = 0; public int c = e.this.c.a(); public final /* synthetic */ int d; public final /* synthetic */ String e; /* JADX INFO: super call moved to the top of the method (can break code semantics) */ public a(File file, int i, int i2, String str) { super(file, i); this.d = i2; this.e = str; } public final void a(int i) { e.this.c.a(e.this.b(), i, this.e); e.this.a((e) 2100, i, this.d); } @Override // java.io.OutputStream, com.huawei.hms.availableupdate.c public void write(byte[] bArr, int i, int i2) throws IOException { super.write(bArr, i, i2); int i3 = this.c + i2; this.c = i3; if (i3 <= 209715200) { long currentTimeMillis = System.currentTimeMillis(); if (Math.abs(currentTimeMillis - this.b) > 1000) { this.b = currentTimeMillis; a(this.c); } int i4 = this.c; if (i4 == this.d) { a(i4); } } } } public e(Context context) { this.a = context.getApplicationContext(); } public Context b() { return this.a; } public final synchronized void a(g gVar) { this.d = gVar; } public final synchronized void a(int i, int i2, int i3) { g gVar = this.d; if (gVar != null) { gVar.a(i, i2, i3, this.e); } } @Override // com.huawei.hms.availableupdate.f public void a() { HMSLog.i("UpdateDownload", "Enter cancel."); a((g) null); this.b.a(); } @Override // com.huawei.hms.availableupdate.f public void a(g gVar, h hVar) { Checker.checkNonNull(gVar, "callback must not be null."); HMSLog.i("UpdateDownload", "Enter downloadPackage."); a(gVar); if (hVar == null || !hVar.a()) { HMSLog.e("UpdateDownload", "In downloadPackage, Invalid update info."); a(PushConstants.ON_TIME_NOTIFICATION, 0, 0); } else if (!"mounted".equals(Environment.getExternalStorageState())) { HMSLog.e("UpdateDownload", "In downloadPackage, Invalid external storage for downloading file."); a(2204, 0, 0); } else { String str = hVar.b; if (TextUtils.isEmpty(str)) { HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Download the package, packageName is null: "); a(PushConstants.ON_TIME_NOTIFICATION, 0, 0); return; } Context context = this.a; File localFile = UpdateProvider.getLocalFile(context, str + ".apk"); this.e = localFile; if (localFile == null) { HMSLog.e("UpdateDownload", "In downloadPackage, Failed to get local file for downloading."); a(2204, 0, 0); return; } File parentFile = localFile.getParentFile(); if (parentFile == null || (!parentFile.mkdirs() && !parentFile.isDirectory())) { HMSLog.e("UpdateDownload", "In downloadPackage, Failed to create directory for downloading file."); a(PushConstants.ON_TIME_NOTIFICATION, 0, 0); } else if (parentFile.getUsableSpace() < ((long) (hVar.d * 3))) { HMSLog.e("UpdateDownload", "In downloadPackage, No space for downloading file."); a(2203, 0, 0); } else { try { a(hVar); } catch (j unused) { HMSLog.w("UpdateDownload", "In downloadPackage, Canceled to download the update file."); a(2101, 0, 0); } } } } public void a(h hVar) throws j { HMSLog.i("UpdateDownload", "Enter downloadPackage."); c cVar = null; try { String str = hVar.b; if (TextUtils.isEmpty(str)) { HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Download the package, packageName is null: "); a(PushConstants.ON_TIME_NOTIFICATION, 0, 0); this.b.close(); IOUtils.closeQuietly((OutputStream) null); return; } this.c.a(b(), str); if (!this.c.b(hVar.c, hVar.d, hVar.e)) { this.c.a(hVar.c, hVar.d, hVar.e); cVar = a(this.e, hVar.d, str); } else if (this.c.a() != this.c.b()) { cVar = a(this.e, hVar.d, str); cVar.a((long) this.c.a()); } else if (FileUtil.verifyHash(hVar.e, this.e)) { a(Constants.ASSEMBLE_PUSH_RETRY_INTERVAL, 0, 0); this.b.close(); IOUtils.closeQuietly((OutputStream) null); return; } else { this.c.a(hVar.c, hVar.d, hVar.e); cVar = a(this.e, hVar.d, str); } int a2 = this.b.a(hVar.c, cVar, this.c.a(), this.c.b(), this.a); if (a2 != 200 && a2 != 206) { HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Download the package, HTTP code: " + a2); a(PushConstants.ON_TIME_NOTIFICATION, 0, 0); this.b.close(); IOUtils.closeQuietly((OutputStream) cVar); } else if (!FileUtil.verifyHash(hVar.e, this.e)) { a(PushConstants.DELAY_NOTIFICATION, 0, 0); this.b.close(); IOUtils.closeQuietly((OutputStream) cVar); } else { a(Constants.ASSEMBLE_PUSH_RETRY_INTERVAL, 0, 0); this.b.close(); IOUtils.closeQuietly((OutputStream) cVar); } } catch (IOException e2) { HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Failed to download." + e2.getMessage()); a(PushConstants.ON_TIME_NOTIFICATION, 0, 0); } catch (Throwable th) { this.b.close(); IOUtils.closeQuietly((OutputStream) null); throw th; } } public final c a(File file, int i, String str) throws IOException { return new a(file, i, i, str); } }