454 lines
14 KiB
Java
454 lines
14 KiB
Java
package com.liulishuo.filedownloader;
|
|
|
|
import android.text.TextUtils;
|
|
import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
import com.liulishuo.filedownloader.DownloadTaskHunter;
|
|
import com.liulishuo.filedownloader.ITaskHunter;
|
|
import com.liulishuo.filedownloader.model.FileDownloadHeader;
|
|
import com.liulishuo.filedownloader.model.FileDownloadStatus;
|
|
import com.liulishuo.filedownloader.util.FileDownloadLog;
|
|
import com.liulishuo.filedownloader.util.FileDownloadUtils;
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class DownloadTask implements BaseDownloadTask, BaseDownloadTask.IRunningTask, DownloadTaskHunter.ICaptureTask {
|
|
private final ITaskHunter a;
|
|
private final ITaskHunter.IMessageHandler b;
|
|
private int c;
|
|
private ArrayList<BaseDownloadTask.FinishListener> d;
|
|
private final String e;
|
|
private String f;
|
|
private String g;
|
|
private boolean h;
|
|
private FileDownloadHeader i;
|
|
private FileDownloadListener j;
|
|
private Object k;
|
|
private int l = 0;
|
|
private boolean m = false;
|
|
private boolean n = false;
|
|
private int o = 100;
|
|
private int p = 10;
|
|
private boolean q = false;
|
|
volatile int r = 0;
|
|
private boolean s = false;
|
|
private final Object u = new Object();
|
|
private volatile boolean v = false;
|
|
private final Object t = new Object();
|
|
|
|
private static final class InQueueTaskImpl implements BaseDownloadTask.InQueueTask {
|
|
private final DownloadTask a;
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.InQueueTask
|
|
public int a() {
|
|
int id = this.a.getId();
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "add the task[%d] to the queue", Integer.valueOf(id));
|
|
}
|
|
FileDownloadList.b().b(this.a);
|
|
return id;
|
|
}
|
|
|
|
private InQueueTaskImpl(DownloadTask downloadTask) {
|
|
this.a = downloadTask;
|
|
this.a.s = true;
|
|
}
|
|
}
|
|
|
|
DownloadTask(String str) {
|
|
this.e = str;
|
|
DownloadTaskHunter downloadTaskHunter = new DownloadTaskHunter(this, this.t);
|
|
this.a = downloadTaskHunter;
|
|
this.b = downloadTaskHunter;
|
|
}
|
|
|
|
private void M() {
|
|
if (this.i == null) {
|
|
synchronized (this.u) {
|
|
if (this.i == null) {
|
|
this.i = new FileDownloadHeader();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private int N() {
|
|
if (!L()) {
|
|
if (!m()) {
|
|
u();
|
|
}
|
|
this.a.a();
|
|
return getId();
|
|
}
|
|
if (K()) {
|
|
throw new IllegalStateException(FileDownloadUtils.a("This task is running %d, if you want to start the same task, please create a new one by FileDownloader.create", Integer.valueOf(getId())));
|
|
}
|
|
throw new IllegalStateException("This task is dirty to restart, If you want to reuse this task, please invoke #reuse method manually and retry to restart again." + this.a.toString());
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public void A() {
|
|
N();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean B() {
|
|
return this.q;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.DownloadTaskHunter.ICaptureTask
|
|
public FileDownloadHeader C() {
|
|
return this.i;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public boolean D() {
|
|
return FileDownloadStatus.b(getStatus());
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean E() {
|
|
return this.h;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public BaseDownloadTask F() {
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public boolean G() {
|
|
ArrayList<BaseDownloadTask.FinishListener> arrayList = this.d;
|
|
return arrayList != null && arrayList.size() > 0;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public void H() {
|
|
this.v = true;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean I() {
|
|
return this.m;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public String J() {
|
|
return this.g;
|
|
}
|
|
|
|
public boolean K() {
|
|
if (FileDownloader.e().b().a(this)) {
|
|
return true;
|
|
}
|
|
return FileDownloadStatus.a(getStatus());
|
|
}
|
|
|
|
public boolean L() {
|
|
return this.a.getStatus() != 0;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask b(String str) {
|
|
a(str, false);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask c(int i) {
|
|
this.l = i;
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask d(int i) {
|
|
this.o = i;
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask e(int i) {
|
|
this.p = i;
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int f() {
|
|
if (this.a.g() > 2147483647L) {
|
|
return Integer.MAX_VALUE;
|
|
}
|
|
return (int) this.a.g();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public void g() {
|
|
N();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int getId() {
|
|
int i = this.c;
|
|
if (i != 0) {
|
|
return i;
|
|
}
|
|
if (TextUtils.isEmpty(this.f) || TextUtils.isEmpty(this.e)) {
|
|
return 0;
|
|
}
|
|
int a = FileDownloadUtils.a(this.e, this.f, this.h);
|
|
this.c = a;
|
|
return a;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public byte getStatus() {
|
|
return this.a.getStatus();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public String getUrl() {
|
|
return this.e;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public String h() {
|
|
return FileDownloadUtils.a(w(), E(), J());
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public int i() {
|
|
return this.r;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask.InQueueTask j() {
|
|
return new InQueueTaskImpl();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public ITaskHunter.IMessageHandler k() {
|
|
return this.b;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public long l() {
|
|
return this.a.f();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean m() {
|
|
return this.r != 0;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int n() {
|
|
return this.p;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean o() {
|
|
return this.n;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.DownloadTaskHunter.ICaptureTask
|
|
public BaseDownloadTask.IRunningTask p() {
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean pause() {
|
|
boolean pause;
|
|
synchronized (this.t) {
|
|
pause = this.a.pause();
|
|
}
|
|
return pause;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int q() {
|
|
return this.l;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int r() {
|
|
if (this.a.f() > 2147483647L) {
|
|
return Integer.MAX_VALUE;
|
|
}
|
|
return (int) this.a.f();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.DownloadTaskHunter.ICaptureTask
|
|
public ArrayList<BaseDownloadTask.FinishListener> s() {
|
|
return this.d;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int start() {
|
|
if (this.s) {
|
|
throw new IllegalStateException("If you start the task manually, it means this task doesn't belong to a queue, so you must not invoke BaseDownloadTask#ready() or InQueueTask#enqueue() before you start() this method. For detail: If this task doesn't belong to a queue, what is just an isolated task, you just need to invoke BaseDownloadTask#start() to start this task, that's all. In other words, If this task doesn't belong to a queue, you must not invoke BaseDownloadTask#ready() method or InQueueTask#enqueue() method before invoke BaseDownloadTask#start(), If you do that and if there is the same listener object to start a queue in another thread, this task may be assembled by the queue, in that case, when you invoke BaseDownloadTask#start() manually to start this task or this task is started by the queue, there is an exception buried in there, because this task object is started two times without declare BaseDownloadTask#reuse() : 1. you invoke BaseDownloadTask#start() manually; 2. the queue start this task automatically.");
|
|
}
|
|
return N();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public long t() {
|
|
return this.a.g();
|
|
}
|
|
|
|
public String toString() {
|
|
return FileDownloadUtils.a("%d@%s", Integer.valueOf(getId()), super.toString());
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public void u() {
|
|
this.r = v() != null ? v().hashCode() : hashCode();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public FileDownloadListener v() {
|
|
return this.j;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public String w() {
|
|
return this.f;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public boolean x() {
|
|
return this.v;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public Object y() {
|
|
return this.t;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int z() {
|
|
return this.o;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask a(String str, boolean z) {
|
|
this.f = str;
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "setPath %s", str);
|
|
}
|
|
this.h = z;
|
|
if (z) {
|
|
this.g = null;
|
|
} else {
|
|
this.g = new File(str).getName();
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask b(FileDownloadListener fileDownloadListener) {
|
|
this.j = fileDownloadListener;
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "setListener %s", fileDownloadListener);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask c(boolean z) {
|
|
this.m = z;
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public Throwable d() {
|
|
return this.a.d();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean e() {
|
|
return this.a.e();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public int c() {
|
|
return this.a.c();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public boolean b(BaseDownloadTask.FinishListener finishListener) {
|
|
ArrayList<BaseDownloadTask.FinishListener> arrayList = this.d;
|
|
return arrayList != null && arrayList.remove(finishListener);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask b(boolean z) {
|
|
this.n = z;
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public void b() {
|
|
this.a.b();
|
|
if (FileDownloadList.b().c(this)) {
|
|
this.v = false;
|
|
}
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask a(Object obj) {
|
|
this.k = obj;
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "setTag %s", obj);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public void b(int i) {
|
|
this.r = i;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask a(boolean z) {
|
|
this.q = z;
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask a(BaseDownloadTask.FinishListener finishListener) {
|
|
if (this.d == null) {
|
|
this.d = new ArrayList<>();
|
|
}
|
|
if (!this.d.contains(finishListener)) {
|
|
this.d.add(finishListener);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public BaseDownloadTask a(String str, String str2) {
|
|
M();
|
|
this.i.add(str, str2);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask
|
|
public Object a() {
|
|
return this.k;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.DownloadTaskHunter.ICaptureTask
|
|
public void a(String str) {
|
|
this.g = str;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public boolean a(int i) {
|
|
return getId() == i;
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.BaseDownloadTask.IRunningTask
|
|
public boolean a(FileDownloadListener fileDownloadListener) {
|
|
return v() == fileDownloadListener;
|
|
}
|
|
}
|