Files
2025-05-13 19:24:51 +02:00

256 lines
7.3 KiB
Java

package com.baidu.license.download.a;
import com.baidu.license.download.DownloadRequest;
import com.baidu.license.download.base.DownloadResponse;
import com.baidu.license.download.base.DownloadTask;
import com.baidu.license.download.base.Downloader;
import com.baidu.license.download.base.HttpConnectTask;
import com.baidu.license.download.exception.DownloadException;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;
/* compiled from: DownloaderImpl.java */
/* loaded from: classes.dex */
public final class a implements DownloadTask.OnDownloadListener, Downloader, HttpConnectTask.OnConnectListener {
private DownloadRequest a;
private DownloadResponse b;
private Executor c;
private String d;
private com.baidu.license.download.oi e;
private Downloader.OnDownloaderDestroyedListener f;
private int g;
private oi h;
private HttpConnectTask i;
private List<DownloadTask> j = new LinkedList();
public a(DownloadRequest downloadRequest, DownloadResponse downloadResponse, Executor executor, String str, com.baidu.license.download.oi oiVar, Downloader.OnDownloaderDestroyedListener onDownloaderDestroyedListener) {
this.a = downloadRequest;
this.b = downloadResponse;
this.c = executor;
this.d = str;
this.e = oiVar;
this.f = onDownloaderDestroyedListener;
this.h = new oi(this.a.b().toString(), this.a.c(), this.a.a());
}
private void c() {
File file = new File(this.h.b(), this.h.a());
if (file.exists() && file.isFile()) {
file.delete();
}
}
@Override // com.baidu.license.download.base.HttpConnectTask.OnConnectListener
public final void a(long j, long j2, boolean z) {
if (this.i.isCanceled()) {
l();
return;
}
this.g = 103;
this.b.a(j, j2, z);
this.h.a(z);
this.h.a(j2);
this.g = 104;
this.j.clear();
if (z) {
ArrayList arrayList = new ArrayList();
int b = this.e.b();
int i = 0;
int i2 = 0;
while (i2 < b) {
long j3 = j2 / b;
long j4 = j3 * i2;
arrayList.add(new e(i2, this.d, this.a.c(), j4, i2 == b + (-1) ? j2 : (j3 + j4) - 1));
i2++;
}
Iterator it = arrayList.iterator();
while (it.hasNext()) {
i = (int) (i + ((e) it.next()).d());
}
this.h.b(i);
Iterator it2 = arrayList.iterator();
while (it2.hasNext()) {
this.j.add(new c(this.h, (e) it2.next(), this));
}
} else {
this.j.add(new d(this.h, new e(this.d, this.a.c()), this));
}
Iterator<DownloadTask> it3 = this.j.iterator();
while (it3.hasNext()) {
this.c.execute(it3.next());
}
}
public final void b() {
this.f.a(this.d, this);
}
@Override // com.baidu.license.download.base.Downloader
public final void cancel() {
HttpConnectTask httpConnectTask = this.i;
if (httpConnectTask != null) {
httpConnectTask.cancel();
}
Iterator<DownloadTask> it = this.j.iterator();
while (it.hasNext()) {
it.next().cancel();
}
if (this.g != 104) {
j();
}
}
@Override // com.baidu.license.download.base.HttpConnectTask.OnConnectListener
public final void i() {
this.g = 102;
this.b.i();
}
@Override // com.baidu.license.download.base.Downloader
public final boolean isRunning() {
int i = this.g;
return i == 101 || i == 102 || i == 103 || i == 104;
}
@Override // com.baidu.license.download.base.DownloadTask.OnDownloadListener
public final void j() {
boolean z;
Iterator<DownloadTask> it = this.j.iterator();
while (true) {
if (!it.hasNext()) {
z = true;
break;
} else if (it.next().h()) {
z = false;
break;
}
}
if (z) {
c();
this.g = 107;
this.b.j();
b();
}
}
@Override // com.baidu.license.download.base.DownloadTask.OnDownloadListener
public final void k() {
boolean z;
Iterator<DownloadTask> it = this.j.iterator();
while (true) {
if (!it.hasNext()) {
z = true;
break;
} else if (it.next().h()) {
z = false;
break;
}
}
if (z) {
this.g = 106;
this.b.k();
b();
}
}
@Override // com.baidu.license.download.base.HttpConnectTask.OnConnectListener
public final void l() {
c();
this.g = 107;
this.b.l();
b();
}
@Override // com.baidu.license.download.base.Downloader
public final void pause() {
HttpConnectTask httpConnectTask = this.i;
if (httpConnectTask != null) {
httpConnectTask.pause();
}
Iterator<DownloadTask> it = this.j.iterator();
while (it.hasNext()) {
it.next().pause();
}
if (this.g != 104) {
k();
}
}
@Override // com.baidu.license.download.base.Downloader
public final void start() {
this.g = 101;
this.b.m();
this.i = new b(this.a.c(), this);
this.c.execute(this.i);
}
@Override // com.baidu.license.download.base.DownloadTask.OnDownloadListener
public final void b(DownloadException downloadException) {
boolean z;
Iterator<DownloadTask> it = this.j.iterator();
while (true) {
if (!it.hasNext()) {
z = true;
break;
} else if (it.next().h()) {
z = false;
break;
}
}
if (z) {
this.g = 108;
this.b.b(downloadException);
b();
}
}
@Override // com.baidu.license.download.base.HttpConnectTask.OnConnectListener
public final void a() {
k();
}
@Override // com.baidu.license.download.base.HttpConnectTask.OnConnectListener
public final void a(DownloadException downloadException) {
if (this.i.isCanceled()) {
l();
} else {
if (this.i.i()) {
k();
return;
}
this.g = 108;
this.b.a(downloadException);
b();
}
}
@Override // com.baidu.license.download.base.DownloadTask.OnDownloadListener
public final void a(long j, long j2) {
this.b.a(j, j2, (int) ((100 * j) / j2));
}
@Override // com.baidu.license.download.base.DownloadTask.OnDownloadListener
public final void a(String str) {
boolean z;
Iterator<DownloadTask> it = this.j.iterator();
while (true) {
if (!it.hasNext()) {
z = true;
break;
} else if (!it.next().g()) {
z = false;
break;
}
}
if (z) {
this.g = 105;
this.b.a(str);
b();
}
}
}