jimu-decompiled/sources/com/liulishuo/filedownloader/DownloadTaskHunter.java
2025-05-13 19:24:51 +02:00

405 lines
15 KiB
Java

package com.liulishuo.filedownloader;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.ITaskHunter;
import com.liulishuo.filedownloader.message.MessageSnapshot;
import com.liulishuo.filedownloader.message.MessageSnapshotTaker;
import com.liulishuo.filedownloader.model.FileDownloadHeader;
import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.liulishuo.filedownloader.util.FileDownloadHelper;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.liulishuo.filedownloader.util.FileDownloadUtils;
import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
/* loaded from: classes.dex */
public class DownloadTaskHunter implements ITaskHunter, ITaskHunter.IStarter, ITaskHunter.IMessageHandler, BaseDownloadTask.LifeCycleCallback {
private IFileDownloadMessenger a;
private final Object b;
private final ICaptureTask c;
private volatile byte d = 0;
private Throwable e = null;
private final IDownloadSpeed$Monitor f = new DownloadSpeedMonitor();
private long g;
private long h;
private int i;
private boolean j;
interface ICaptureTask {
FileDownloadHeader C();
void a(String str);
BaseDownloadTask.IRunningTask p();
ArrayList<BaseDownloadTask.FinishListener> s();
}
DownloadTaskHunter(ICaptureTask iCaptureTask, Object obj) {
this.b = obj;
this.c = iCaptureTask;
this.a = new FileDownloadMessenger(iCaptureTask.p(), this);
}
/* JADX WARN: Multi-variable type inference failed */
private void e(MessageSnapshot messageSnapshot) {
BaseDownloadTask F = this.c.p().F();
byte status = messageSnapshot.getStatus();
this.d = status;
this.j = messageSnapshot.isLargeFile();
if (status == -4) {
this.f.reset();
int a = FileDownloadList.b().a(F.getId());
if (a + ((a > 1 || !F.E()) ? 0 : FileDownloadList.b().a(FileDownloadUtils.c(F.getUrl(), F.h()))) <= 1) {
byte a2 = FileDownloadServiceProxy.c().a(F.getId());
FileDownloadLog.e(this, "warn, but no mListener to receive, switch to pending %d %d", Integer.valueOf(F.getId()), Integer.valueOf(a2));
if (FileDownloadStatus.a(a2)) {
this.d = (byte) 1;
this.h = messageSnapshot.getLargeTotalBytes();
this.g = messageSnapshot.getLargeSofarBytes();
this.f.b(this.g);
this.a.a(((MessageSnapshot.IWarnMessageSnapshot) messageSnapshot).turnToPending());
return;
}
}
FileDownloadList.b().a(this.c.p(), messageSnapshot);
return;
}
if (status == -3) {
messageSnapshot.isReusedDownloadedFile();
this.g = messageSnapshot.getLargeTotalBytes();
this.h = messageSnapshot.getLargeTotalBytes();
FileDownloadList.b().a(this.c.p(), messageSnapshot);
return;
}
if (status != -2) {
if (status == -1) {
this.e = messageSnapshot.getThrowable();
this.g = messageSnapshot.getLargeSofarBytes();
FileDownloadList.b().a(this.c.p(), messageSnapshot);
return;
}
if (status == 1) {
this.g = messageSnapshot.getLargeSofarBytes();
this.h = messageSnapshot.getLargeTotalBytes();
this.a.a(messageSnapshot);
return;
}
if (status == 2) {
this.h = messageSnapshot.getLargeTotalBytes();
messageSnapshot.isResuming();
messageSnapshot.getEtag();
String fileName = messageSnapshot.getFileName();
if (fileName != null) {
if (F.J() != null) {
FileDownloadLog.e(this, "already has mFilename[%s], but assign mFilename[%s] again", F.J(), fileName);
}
this.c.a(fileName);
}
this.f.b(this.g);
this.a.g(messageSnapshot);
return;
}
if (status == 3) {
this.g = messageSnapshot.getLargeSofarBytes();
this.f.c(messageSnapshot.getLargeSofarBytes());
this.a.e(messageSnapshot);
} else if (status != 5) {
if (status != 6) {
return;
}
this.a.i(messageSnapshot);
} else {
this.g = messageSnapshot.getLargeSofarBytes();
this.e = messageSnapshot.getThrowable();
this.i = messageSnapshot.getRetryingTimes();
this.f.reset();
this.a.d(messageSnapshot);
}
}
}
private int l() {
return this.c.p().F().getId();
}
private void m() throws IOException {
File file;
BaseDownloadTask F = this.c.p().F();
if (F.w() == null) {
F.b(FileDownloadUtils.g(F.getUrl()));
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "save Path is null to %s", F.w());
}
}
if (F.E()) {
file = new File(F.w());
} else {
String i = FileDownloadUtils.i(F.w());
if (i == null) {
throw new InvalidParameterException(FileDownloadUtils.a("the provided mPath[%s] is invalid, can't find its directory", F.w()));
}
file = new File(i);
}
if (!file.exists() && !file.mkdirs() && !file.exists()) {
throw new IOException(FileDownloadUtils.a("Create parent directory failed, please make sure you have permission to create file or directory on the path: %s", file.getAbsolutePath()));
}
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IMessageHandler
public boolean a(MessageSnapshot messageSnapshot) {
if (!this.c.p().F().E() || messageSnapshot.getStatus() != -4 || getStatus() != 2) {
return false;
}
e(messageSnapshot);
return true;
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IMessageHandler
public boolean b(MessageSnapshot messageSnapshot) {
if (FileDownloadStatus.a(getStatus(), messageSnapshot.getStatus())) {
e(messageSnapshot);
return true;
}
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "can't update mStatus change by keep ahead, %d, but the current mStatus is %d, %d", Byte.valueOf(this.d), Byte.valueOf(getStatus()), Integer.valueOf(l()));
}
return false;
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IMessageHandler
public boolean c(MessageSnapshot messageSnapshot) {
if (!FileDownloadStatus.a(this.c.p().F())) {
return false;
}
e(messageSnapshot);
return true;
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IMessageHandler
public boolean d(MessageSnapshot messageSnapshot) {
byte status = getStatus();
byte status2 = messageSnapshot.getStatus();
if (-2 == status && FileDownloadStatus.a(status2)) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "High concurrent cause, callback pending, but has already be paused %d", Integer.valueOf(l()));
}
return true;
}
if (FileDownloadStatus.b(status, status2)) {
e(messageSnapshot);
return true;
}
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "can't update mStatus change by keep flow, %d, but the current mStatus is %d, %d", Byte.valueOf(this.d), Byte.valueOf(getStatus()), Integer.valueOf(l()));
}
return false;
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public long f() {
return this.g;
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public long g() {
return this.h;
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public byte getStatus() {
return this.d;
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IMessageHandler
public IFileDownloadMessenger h() {
return this.a;
}
@Override // com.liulishuo.filedownloader.BaseDownloadTask.LifeCycleCallback
public void i() {
BaseDownloadTask F = this.c.p().F();
if (FileDownloadMonitor.b()) {
FileDownloadMonitor.a().b(F);
}
if (FileDownloadLog.a) {
FileDownloadLog.d(this, "filedownloader:lifecycle:over %s by %d ", toString(), Byte.valueOf(getStatus()));
}
this.f.a(this.g);
if (this.c.s() != null) {
ArrayList arrayList = (ArrayList) this.c.s().clone();
int size = arrayList.size();
for (int i = 0; i < size; i++) {
((BaseDownloadTask.FinishListener) arrayList.get(i)).a(F);
}
}
FileDownloader.e().b().c(this.c.p());
}
@Override // com.liulishuo.filedownloader.BaseDownloadTask.LifeCycleCallback
public void j() {
if (FileDownloadMonitor.b() && getStatus() == 6) {
FileDownloadMonitor.a().d(this.c.p().F());
}
}
@Override // com.liulishuo.filedownloader.BaseDownloadTask.LifeCycleCallback
public void k() {
if (FileDownloadMonitor.b()) {
FileDownloadMonitor.a().c(this.c.p().F());
}
if (FileDownloadLog.a) {
FileDownloadLog.d(this, "filedownloader:lifecycle:start %s by %d ", toString(), Byte.valueOf(getStatus()));
}
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public boolean pause() {
if (FileDownloadStatus.b(getStatus())) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "High concurrent cause, Already is over, can't pause again, %d %d", Byte.valueOf(getStatus()), Integer.valueOf(this.c.p().F().getId()));
}
return false;
}
this.d = (byte) -2;
BaseDownloadTask.IRunningTask p = this.c.p();
BaseDownloadTask F = p.F();
FileDownloadTaskLauncher.a().a(this);
if (FileDownloadLog.a) {
FileDownloadLog.d(this, "the task[%d] has been expired from the launch pool.", Integer.valueOf(l()));
}
if (FileDownloader.e().d()) {
FileDownloadServiceProxy.c().b(F.getId());
} else if (FileDownloadLog.a) {
FileDownloadLog.a(this, "request pause the task[%d] to the download service, but the download service isn't connected yet.", Integer.valueOf(F.getId()));
}
FileDownloadList.b().a(p);
FileDownloadList.b().a(p, MessageSnapshotTaker.a(F));
FileDownloader.e().b().c(p);
return true;
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IStarter
public void start() {
if (this.d != 10) {
FileDownloadLog.e(this, "High concurrent cause, this task %d will not start, because the of status isn't toLaunchPool: %d", Integer.valueOf(l()), Byte.valueOf(this.d));
return;
}
BaseDownloadTask.IRunningTask p = this.c.p();
BaseDownloadTask F = p.F();
ILostServiceConnectedHandler b = FileDownloader.e().b();
try {
if (b.b(p)) {
return;
}
synchronized (this.b) {
if (this.d != 10) {
FileDownloadLog.e(this, "High concurrent cause, this task %d will not start, the status can't assign to toFileDownloadService, because the status isn't toLaunchPool: %d", Integer.valueOf(l()), Byte.valueOf(this.d));
return;
}
this.d = (byte) 11;
FileDownloadList.b().a(p);
if (FileDownloadHelper.a(F.getId(), F.h(), F.B(), true)) {
return;
}
boolean a = FileDownloadServiceProxy.c().a(F.getUrl(), F.w(), F.E(), F.z(), F.n(), F.q(), F.B(), this.c.C(), F.o());
if (this.d == -2) {
FileDownloadLog.e(this, "High concurrent cause, this task %d will be paused,because of the status is paused, so the pause action must be applied", Integer.valueOf(l()));
if (a) {
FileDownloadServiceProxy.c().b(l());
return;
}
return;
}
if (a) {
b.c(p);
return;
}
if (b.b(p)) {
return;
}
MessageSnapshot a2 = a(new RuntimeException("Occur Unknown Error, when request to start maybe some problem in binder, maybe the process was killed in unexpected."));
if (FileDownloadList.b().c(p)) {
b.c(p);
FileDownloadList.b().a(p);
}
FileDownloadList.b().a(p, a2);
}
} catch (Throwable th) {
th.printStackTrace();
FileDownloadList.b().a(p, a(th));
}
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public int c() {
return this.i;
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IMessageHandler
public MessageSnapshot a(Throwable th) {
this.d = (byte) -1;
this.e = th;
return MessageSnapshotTaker.a(l(), f(), th);
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public void b() {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "free the task %d, when the status is %d", Integer.valueOf(l()), Byte.valueOf(this.d));
}
this.d = (byte) 0;
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public void a() {
boolean z;
synchronized (this.b) {
if (this.d != 0) {
FileDownloadLog.e(this, "High concurrent cause, this task %d will not input to launch pool, because of the status isn't idle : %d", Integer.valueOf(l()), Byte.valueOf(this.d));
return;
}
this.d = (byte) 10;
BaseDownloadTask.IRunningTask p = this.c.p();
BaseDownloadTask F = p.F();
if (FileDownloadMonitor.b()) {
FileDownloadMonitor.a().a(F);
}
if (FileDownloadLog.a) {
FileDownloadLog.d(this, "call start Url[%s], Path[%s] Listener[%s], Tag[%s]", F.getUrl(), F.w(), F.v(), F.a());
}
try {
m();
z = true;
} catch (Throwable th) {
FileDownloadList.b().a(p);
FileDownloadList.b().a(p, a(th));
z = false;
}
if (z) {
FileDownloadTaskLauncher.a().b(this);
}
if (FileDownloadLog.a) {
FileDownloadLog.d(this, "the task[%d] has been into the launch pool.", Integer.valueOf(l()));
}
}
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public Throwable d() {
return this.e;
}
@Override // com.liulishuo.filedownloader.ITaskHunter.IStarter
public boolean a(FileDownloadListener fileDownloadListener) {
return this.c.p().F().v() == fileDownloadListener;
}
@Override // com.liulishuo.filedownloader.ITaskHunter
public boolean e() {
return this.j;
}
}