package com.liulishuo.filedownloader.download; import com.liulishuo.filedownloader.IThreadPoolMonitor; import com.liulishuo.filedownloader.connection.FileDownloadConnection; import com.liulishuo.filedownloader.database.FileDownloadDatabase; import com.liulishuo.filedownloader.download.ConnectTask; import com.liulishuo.filedownloader.download.ConnectionProfile; import com.liulishuo.filedownloader.download.DownloadRunnable; import com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException; import com.liulishuo.filedownloader.exception.FileDownloadHttpException; import com.liulishuo.filedownloader.exception.FileDownloadNetworkPolicyException; import com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException; import com.liulishuo.filedownloader.exception.FileDownloadSecurityException; import com.liulishuo.filedownloader.model.ConnectionModel; import com.liulishuo.filedownloader.model.FileDownloadHeader; import com.liulishuo.filedownloader.model.FileDownloadModel; import com.liulishuo.filedownloader.stream.FileDownloadOutputStream; import com.liulishuo.filedownloader.util.FileDownloadExecutors; import com.liulishuo.filedownloader.util.FileDownloadHelper; import com.liulishuo.filedownloader.util.FileDownloadLog; import com.liulishuo.filedownloader.util.FileDownloadProperties; import com.liulishuo.filedownloader.util.FileDownloadUtils; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.atomic.AtomicBoolean; /* loaded from: classes.dex */ public class DownloadLaunchRunnable implements Runnable, ProcessCallback { private static final ThreadPoolExecutor x = FileDownloadExecutors.a("ConnectionBlock"); private final DownloadStatusCallback a; private final FileDownloadModel b; private final FileDownloadHeader c; private final boolean d; private final boolean e; private final FileDownloadDatabase f; private final IThreadPoolMonitor g; private boolean h; int i; private boolean j; private final boolean k; private final ArrayList l; private DownloadRunnable m; private boolean n; private boolean o; private boolean p; private boolean q; private final AtomicBoolean r; private volatile boolean s; private volatile boolean t; private volatile Exception u; private String v; public static class Builder { private FileDownloadModel a; private FileDownloadHeader b; private IThreadPoolMonitor c; private Integer d; private Integer e; private Boolean f; private Boolean g; private Integer h; public Builder a(FileDownloadModel fileDownloadModel) { this.a = fileDownloadModel; return this; } public Builder b(Boolean bool) { this.g = bool; return this; } public Builder c(Integer num) { this.d = num; return this; } public Builder a(FileDownloadHeader fileDownloadHeader) { this.b = fileDownloadHeader; return this; } public Builder b(Integer num) { this.h = num; return this; } public Builder a(IThreadPoolMonitor iThreadPoolMonitor) { this.c = iThreadPoolMonitor; return this; } public Builder a(Integer num) { this.e = num; return this; } public Builder a(Boolean bool) { this.f = bool; return this; } public DownloadLaunchRunnable a() { IThreadPoolMonitor iThreadPoolMonitor; Integer num; FileDownloadModel fileDownloadModel = this.a; if (fileDownloadModel != null && (iThreadPoolMonitor = this.c) != null && (num = this.d) != null && this.e != null && this.f != null && this.g != null && this.h != null) { return new DownloadLaunchRunnable(fileDownloadModel, this.b, iThreadPoolMonitor, num.intValue(), this.e.intValue(), this.f.booleanValue(), this.g.booleanValue(), this.h.intValue()); } throw new IllegalArgumentException(); } } class DiscardSafely extends Throwable { DiscardSafely(DownloadLaunchRunnable downloadLaunchRunnable) { } } class RetryDirectly extends Throwable { RetryDirectly(DownloadLaunchRunnable downloadLaunchRunnable) { } } private int b(long j) { if (k()) { return this.o ? this.b.getConnectionCount() : CustomComponentHolder.i().a(this.b.getId(), this.b.getUrl(), this.b.getPath(), j); } return 1; } private void c(long j) throws IOException, IllegalAccessException { ConnectionProfile a; if (this.p) { a = ConnectionProfile.ConnectionProfileBuild.a(this.b.getSoFar(), this.b.getSoFar(), j - this.b.getSoFar()); } else { this.b.setSoFar(0L); a = ConnectionProfile.ConnectionProfileBuild.a(j); } DownloadRunnable.Builder builder = new DownloadRunnable.Builder(); builder.a(this.b.getId()); builder.a((Integer) (-1)); builder.a(this); builder.c(this.b.getUrl()); builder.a(this.b.getETag()); builder.a(this.c); builder.a(this.e); builder.a(a); builder.b(this.b.getTempFilePath()); this.m = builder.a(); this.b.setConnectionCount(1); this.f.a(this.b.getId(), 1); if (!this.s) { this.m.run(); } else { this.b.setStatus((byte) -2); this.m.pause(); } } private void f() throws RetryDirectly, DiscardSafely { int id = this.b.getId(); if (this.b.isPathAsDirectory()) { String targetFilePath = this.b.getTargetFilePath(); int c = FileDownloadUtils.c(this.b.getUrl(), targetFilePath); if (FileDownloadHelper.a(id, targetFilePath, this.d, false)) { this.f.remove(id); this.f.b(id); throw new DiscardSafely(this); } FileDownloadModel e = this.f.e(c); if (e != null) { if (FileDownloadHelper.a(id, e, this.g, false)) { this.f.remove(id); this.f.b(id); throw new DiscardSafely(this); } List d = this.f.d(c); this.f.remove(c); this.f.b(c); FileDownloadUtils.d(this.b.getTargetFilePath()); if (FileDownloadUtils.a(c, e)) { this.b.setSoFar(e.getSoFar()); this.b.setTotal(e.getTotal()); this.b.setETag(e.getETag()); this.b.setConnectionCount(e.getConnectionCount()); this.f.a(this.b); if (d != null) { for (ConnectionModel connectionModel : d) { connectionModel.a(id); this.f.a(connectionModel); } } throw new RetryDirectly(this); } } if (FileDownloadHelper.a(id, this.b.getSoFar(), this.b.getTempFilePath(), targetFilePath, this.g)) { this.f.remove(id); this.f.b(id); throw new DiscardSafely(this); } } } private void j() throws FileDownloadGiveUpRetryException { if (this.e && !FileDownloadUtils.a("android.permission.ACCESS_NETWORK_STATE")) { throw new FileDownloadGiveUpRetryException(FileDownloadUtils.a("Task[%d] can't start the download runnable, because this task require wifi, but user application nor current process has %s, so we can't check whether the network type connection.", Integer.valueOf(this.b.getId()), "android.permission.ACCESS_NETWORK_STATE")); } if (this.e && FileDownloadUtils.e()) { throw new FileDownloadNetworkPolicyException(); } } private boolean k() { return (!this.o || this.b.getConnectionCount() > 1) && this.p && this.k && !this.q; } private void l() throws IOException, RetryDirectly, IllegalAccessException, FileDownloadSecurityException { FileDownloadConnection fileDownloadConnection = null; try { ConnectionProfile b = this.j ? ConnectionProfile.ConnectionProfileBuild.b() : ConnectionProfile.ConnectionProfileBuild.a(); ConnectTask.Builder builder = new ConnectTask.Builder(); builder.a(this.b.getId()); builder.b(this.b.getUrl()); builder.a(this.b.getETag()); builder.a(this.c); builder.a(b); ConnectTask a = builder.a(); fileDownloadConnection = a.a(); a(a.d(), a, fileDownloadConnection); } finally { if (fileDownloadConnection != null) { fileDownloadConnection.d(); } } } /* JADX WARN: Removed duplicated region for block: B:12:0x0068 */ /* JADX WARN: Removed duplicated region for block: B:15:? A[RETURN, SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:9:0x0061 */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct code enable 'Show inconsistent code' option in preferences */ void a(java.util.List r11) { /* r10 = this; com.liulishuo.filedownloader.model.FileDownloadModel r0 = r10.b int r0 = r0.getConnectionCount() com.liulishuo.filedownloader.model.FileDownloadModel r1 = r10.b java.lang.String r1 = r1.getTempFilePath() com.liulishuo.filedownloader.model.FileDownloadModel r2 = r10.b java.lang.String r2 = r2.getTargetFilePath() r3 = 0 r4 = 1 if (r0 <= r4) goto L18 r5 = 1 goto L19 L18: r5 = 0 L19: boolean r6 = r10.j r7 = 0 if (r6 == 0) goto L21 L1f: r5 = r7 goto L58 L21: if (r5 == 0) goto L28 boolean r6 = r10.k if (r6 != 0) goto L28 goto L1f L28: com.liulishuo.filedownloader.model.FileDownloadModel r6 = r10.b int r6 = r6.getId() com.liulishuo.filedownloader.model.FileDownloadModel r9 = r10.b boolean r6 = com.liulishuo.filedownloader.util.FileDownloadUtils.a(r6, r9) if (r6 == 0) goto L1f boolean r6 = r10.k if (r6 != 0) goto L44 java.io.File r11 = new java.io.File r11.(r1) long r5 = r11.length() goto L58 L44: if (r5 == 0) goto L52 int r5 = r11.size() if (r0 == r5) goto L4d goto L1f L4d: long r5 = com.liulishuo.filedownloader.model.ConnectionModel.a(r11) goto L58 L52: com.liulishuo.filedownloader.model.FileDownloadModel r11 = r10.b long r5 = r11.getSoFar() L58: com.liulishuo.filedownloader.model.FileDownloadModel r11 = r10.b r11.setSoFar(r5) int r11 = (r5 > r7 ? 1 : (r5 == r7 ? 0 : -1)) if (r11 <= 0) goto L62 r3 = 1 L62: r10.o = r3 boolean r11 = r10.o if (r11 != 0) goto L76 com.liulishuo.filedownloader.database.FileDownloadDatabase r11 = r10.f com.liulishuo.filedownloader.model.FileDownloadModel r0 = r10.b int r0 = r0.getId() r11.b(r0) com.liulishuo.filedownloader.util.FileDownloadUtils.a(r2, r1) L76: return */ throw new UnsupportedOperationException("Method not decompiled: com.liulishuo.filedownloader.download.DownloadLaunchRunnable.a(java.util.List):void"); } public boolean d() { return this.r.get() || this.a.b(); } public void e() { a(this.f.d(this.b.getId())); this.a.f(); } public void pause() { this.s = true; DownloadRunnable downloadRunnable = this.m; if (downloadRunnable != null) { downloadRunnable.pause(); } Iterator it = ((ArrayList) this.l.clone()).iterator(); while (it.hasNext()) { DownloadRunnable downloadRunnable2 = (DownloadRunnable) it.next(); if (downloadRunnable2 != null) { downloadRunnable2.pause(); } } } /* JADX WARN: Removed duplicated region for block: B:106:0x01c3 A[Catch: all -> 0x01f8, TryCatch #13 {all -> 0x01f8, blocks: (B:3:0x0003, B:6:0x0012, B:8:0x001a, B:10:0x001e, B:25:0x0030, B:26:0x008d, B:28:0x0091, B:30:0x0096, B:116:0x009a, B:118:0x009e, B:33:0x00c6, B:35:0x00e2, B:44:0x0100, B:56:0x0136, B:58:0x013a, B:69:0x015f, B:71:0x0165, B:85:0x0169, B:87:0x0172, B:88:0x0176, B:90:0x017a, B:91:0x018d, B:100:0x018e, B:104:0x01bd, B:106:0x01c3, B:109:0x01c8), top: B:2:0x0003, inners: #15, #12, #11 }] */ /* JADX WARN: Removed duplicated region for block: B:108:0x01c8 A[SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:74:0x01d4 */ /* JADX WARN: Removed duplicated region for block: B:77:0x01da */ @Override // java.lang.Runnable /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct code enable 'Show inconsistent code' option in preferences */ public void run() { /* Method dump skipped, instructions count: 550 To view this dump change 'Code comments level' option to 'DEBUG' */ throw new UnsupportedOperationException("Method not decompiled: com.liulishuo.filedownloader.download.DownloadLaunchRunnable.run():void"); } private DownloadLaunchRunnable(FileDownloadModel fileDownloadModel, FileDownloadHeader fileDownloadHeader, IThreadPoolMonitor iThreadPoolMonitor, int i, int i2, boolean z, boolean z2, int i3) { this.j = false; this.l = new ArrayList<>(5); this.r = new AtomicBoolean(true); this.s = false; this.h = false; this.b = fileDownloadModel; this.c = fileDownloadHeader; this.d = z; this.e = z2; this.f = CustomComponentHolder.i().a(); this.k = CustomComponentHolder.i().e(); this.g = iThreadPoolMonitor; this.i = i3; this.a = new DownloadStatusCallback(fileDownloadModel, i3, i, i2); } @Override // com.liulishuo.filedownloader.download.ProcessCallback public void b(Exception exc) { if (this.s) { if (FileDownloadLog.a) { FileDownloadLog.a(this, "the task[%d] has already been paused, so pass the retry callback", Integer.valueOf(this.b.getId())); } } else { int i = this.i; this.i = i - 1; if (i < 0) { FileDownloadLog.b(this, "valid retry times is less than 0(%d) for download task(%d)", Integer.valueOf(this.i), Integer.valueOf(this.b.getId())); } this.a.a(exc, this.i); } } /* JADX WARN: Removed duplicated region for block: B:11:0x008a */ /* JADX WARN: Removed duplicated region for block: B:22:0x011b */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct code enable 'Show inconsistent code' option in preferences */ private void a(java.util.Map> r18, com.liulishuo.filedownloader.download.ConnectTask r19, com.liulishuo.filedownloader.connection.FileDownloadConnection r20) throws java.io.IOException, com.liulishuo.filedownloader.download.DownloadLaunchRunnable.RetryDirectly, java.lang.IllegalArgumentException, com.liulishuo.filedownloader.exception.FileDownloadSecurityException { /* Method dump skipped, instructions count: 357 To view this dump change 'Code comments level' option to 'DEBUG' */ throw new UnsupportedOperationException("Method not decompiled: com.liulishuo.filedownloader.download.DownloadLaunchRunnable.a(java.util.Map, com.liulishuo.filedownloader.download.ConnectTask, com.liulishuo.filedownloader.connection.FileDownloadConnection):void"); } public int b() { return this.b.getId(); } @Override // com.liulishuo.filedownloader.download.ProcessCallback public boolean c(Exception exc) { if (exc instanceof FileDownloadHttpException) { int code = ((FileDownloadHttpException) exc).getCode(); if (this.n && code == 416 && !this.h) { FileDownloadUtils.a(this.b.getTargetFilePath(), this.b.getTempFilePath()); this.h = true; return true; } } return this.i > 0 && !(exc instanceof FileDownloadGiveUpRetryException); } public String c() { return this.b.getTempFilePath(); } private void a(int i, List list) throws InterruptedException { if (i > 1 && list.size() == i) { a(list, this.b.getTotal()); return; } throw new IllegalArgumentException(); } private void a(long j, int i) throws InterruptedException { long j2 = j / i; int id = this.b.getId(); ArrayList arrayList = new ArrayList(); long j3 = 0; int i2 = 0; while (i2 < i) { long j4 = i2 == i + (-1) ? -1L : (j3 + j2) - 1; ConnectionModel connectionModel = new ConnectionModel(); connectionModel.a(id); connectionModel.b(i2); connectionModel.c(j3); connectionModel.a(j3); connectionModel.b(j4); arrayList.add(connectionModel); this.f.a(connectionModel); j3 += j2; i2++; } this.b.setConnectionCount(i); this.f.a(id, i); a(arrayList, j); } private void a(List list, long j) throws InterruptedException { long b; int id = this.b.getId(); String eTag = this.b.getETag(); String str = this.v; if (str == null) { str = this.b.getUrl(); } String tempFilePath = this.b.getTempFilePath(); if (FileDownloadLog.a) { FileDownloadLog.a(this, "fetch data with multiple connection(count: [%d]) for task[%d] totalLength[%d]", Integer.valueOf(list.size()), Integer.valueOf(id), Long.valueOf(j)); } boolean z = this.o; long j2 = 0; long j3 = 0; for (ConnectionModel connectionModel : list) { if (connectionModel.b() == -1) { b = j - connectionModel.a(); } else { b = (connectionModel.b() - connectionModel.a()) + 1; } long j4 = b; j3 += connectionModel.a() - connectionModel.e(); if (j4 == j2) { if (FileDownloadLog.a) { FileDownloadLog.a(this, "pass connection[%d-%d], because it has been completed", Integer.valueOf(connectionModel.c()), Integer.valueOf(connectionModel.d())); } } else { DownloadRunnable.Builder builder = new DownloadRunnable.Builder(); ConnectionProfile a = ConnectionProfile.ConnectionProfileBuild.a(connectionModel.e(), connectionModel.a(), connectionModel.b(), j4); builder.a(id); builder.a(Integer.valueOf(connectionModel.d())); builder.a(this); builder.c(str); builder.a(z ? eTag : null); builder.a(this.c); builder.a(this.e); builder.a(a); builder.b(tempFilePath); DownloadRunnable a2 = builder.a(); if (FileDownloadLog.a) { FileDownloadLog.a(this, "enable multiple connection: %s", connectionModel); } if (a2 != null) { this.l.add(a2); } else { throw new IllegalArgumentException("the download runnable must not be null!"); } } j2 = 0; } if (j3 != this.b.getSoFar()) { FileDownloadLog.e(this, "correct the sofar[%d] from connection table[%d]", Long.valueOf(this.b.getSoFar()), Long.valueOf(j3)); this.b.setSoFar(j3); } ArrayList arrayList = new ArrayList(this.l.size()); Iterator it = this.l.iterator(); while (it.hasNext()) { DownloadRunnable next = it.next(); if (this.s) { next.pause(); } else { arrayList.add(Executors.callable(next)); } } if (this.s) { this.b.setStatus((byte) -2); return; } List invokeAll = x.invokeAll(arrayList); if (FileDownloadLog.a) { for (Future future : invokeAll) { FileDownloadLog.a(this, "finish sub-task for [%d] %B %B", Integer.valueOf(id), Boolean.valueOf(future.isDone()), Boolean.valueOf(future.isCancelled())); } } } private void a(long j, String str) throws IOException, IllegalAccessException { FileDownloadOutputStream fileDownloadOutputStream = null; if (j != -1) { try { fileDownloadOutputStream = FileDownloadUtils.c(this.b.getTempFilePath()); long length = new File(str).length(); long j2 = j - length; long h = FileDownloadUtils.h(str); if (h >= j2) { if (!FileDownloadProperties.a().f) { fileDownloadOutputStream.a(j); } } else { throw new FileDownloadOutOfSpaceException(h, j2, length); } } finally { if (0 != 0) { fileDownloadOutputStream.close(); } } } } @Override // com.liulishuo.filedownloader.download.ProcessCallback public void a(long j) { if (this.s) { return; } this.a.a(j); } @Override // com.liulishuo.filedownloader.download.ProcessCallback public void a(DownloadRunnable downloadRunnable, long j, long j2) { if (this.s) { if (FileDownloadLog.a) { FileDownloadLog.a(this, "the task[%d] has already been paused, so pass the completed callback", Integer.valueOf(this.b.getId())); return; } return; } int i = downloadRunnable.h; if (FileDownloadLog.a) { FileDownloadLog.a(this, "the connection has been completed(%d): [%d, %d) %d", Integer.valueOf(i), Long.valueOf(j), Long.valueOf(j2), Long.valueOf(this.b.getTotal())); } if (!this.n) { synchronized (this.l) { this.l.remove(downloadRunnable); } } else { if (j == 0 || j2 == this.b.getTotal()) { return; } FileDownloadLog.b(this, "the single task not completed corrected(%d, %d != %d) for task(%d)", Long.valueOf(j), Long.valueOf(j2), Long.valueOf(this.b.getTotal()), Integer.valueOf(this.b.getId())); } } @Override // com.liulishuo.filedownloader.download.ProcessCallback public void a(Exception exc) { this.t = true; this.u = exc; if (this.s) { if (FileDownloadLog.a) { FileDownloadLog.a(this, "the task[%d] has already been paused, so pass the error callback", Integer.valueOf(this.b.getId())); } } else { Iterator it = ((ArrayList) this.l.clone()).iterator(); while (it.hasNext()) { DownloadRunnable downloadRunnable = (DownloadRunnable) it.next(); if (downloadRunnable != null) { downloadRunnable.a(); } } } } @Override // com.liulishuo.filedownloader.download.ProcessCallback public void a() { this.f.b(this.b.getId(), this.b.getSoFar()); } }