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

279 lines
10 KiB
Java

package com.liulishuo.filedownloader;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.ITaskHunter;
import com.liulishuo.filedownloader.message.BlockCompleteMessage;
import com.liulishuo.filedownloader.message.MessageSnapshot;
import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.liulishuo.filedownloader.util.FileDownloadUtils;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
/* loaded from: classes.dex */
class FileDownloadMessenger implements IFileDownloadMessenger {
private BaseDownloadTask.IRunningTask a;
private BaseDownloadTask.LifeCycleCallback b;
private Queue<MessageSnapshot> c;
private boolean d = false;
FileDownloadMessenger(BaseDownloadTask.IRunningTask iRunningTask, BaseDownloadTask.LifeCycleCallback lifeCycleCallback) {
a(iRunningTask, lifeCycleCallback);
}
private void a(BaseDownloadTask.IRunningTask iRunningTask, BaseDownloadTask.LifeCycleCallback lifeCycleCallback) {
this.a = iRunningTask;
this.b = lifeCycleCallback;
this.c = new LinkedBlockingQueue();
}
private void k(MessageSnapshot messageSnapshot) {
BaseDownloadTask.IRunningTask iRunningTask = this.a;
if (iRunningTask == null) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "occur this case, it would be the host task of this messenger has been over(paused/warn/completed/error) on the other thread before receiving the snapshot(id[%d], status[%d])", Integer.valueOf(messageSnapshot.getId()), Byte.valueOf(messageSnapshot.getStatus()));
}
} else {
if (!this.d && iRunningTask.F().v() != null) {
this.c.offer(messageSnapshot);
FileDownloadMessageStation.a().a(this);
return;
}
if ((FileDownloadMonitor.b() || this.a.G()) && messageSnapshot.getStatus() == 4) {
this.b.i();
}
a(messageSnapshot.getStatus());
}
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public boolean b() {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify begin %s", this.a);
}
if (this.a == null) {
FileDownloadLog.e(this, "can't begin the task, the holder fo the messenger is nil, %d", Integer.valueOf(this.c.size()));
return false;
}
this.b.k();
return true;
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void c(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
BaseDownloadTask.IRunningTask iRunningTask = this.a;
FileDownloadLog.a(this, "notify error %s %s", iRunningTask, iRunningTask.F().d());
}
this.b.i();
k(messageSnapshot);
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void d(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
BaseDownloadTask F = this.a.F();
FileDownloadLog.a(this, "notify retry %s %d %d %s", this.a, Integer.valueOf(F.q()), Integer.valueOf(F.c()), F.d());
}
this.b.j();
k(messageSnapshot);
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void e(MessageSnapshot messageSnapshot) {
BaseDownloadTask F = this.a.F();
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify progress %s %d %d", F, Long.valueOf(F.l()), Long.valueOf(F.t()));
}
if (F.z() > 0) {
this.b.j();
k(messageSnapshot);
} else if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify progress but client not request notify %s", this.a);
}
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void f(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify warn %s", this.a);
}
this.b.i();
k(messageSnapshot);
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void g(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify connected %s", this.a);
}
this.b.j();
k(messageSnapshot);
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void h(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify block completed %s %s", this.a, Thread.currentThread().getName());
}
this.b.j();
k(messageSnapshot);
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void i(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify started %s", this.a);
}
this.b.j();
k(messageSnapshot);
}
public void j(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify completed %s", this.a);
}
this.b.i();
k(messageSnapshot);
}
public String toString() {
Object[] objArr = new Object[2];
BaseDownloadTask.IRunningTask iRunningTask = this.a;
objArr[0] = Integer.valueOf(iRunningTask == null ? -1 : iRunningTask.F().getId());
objArr[1] = super.toString();
return FileDownloadUtils.a("%d:%s", objArr);
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void a(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify pending %s", this.a);
}
this.b.j();
k(messageSnapshot);
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public boolean c() {
return this.c.peek().getStatus() == 4;
}
private void a(int i) {
if (FileDownloadStatus.b(i)) {
if (!this.c.isEmpty()) {
MessageSnapshot peek = this.c.peek();
FileDownloadLog.e(this, "the messenger[%s](with id[%d]) has already accomplished all his job, but there still are some messages in parcel queue[%d] queue-top-status[%d]", this, Integer.valueOf(peek.getId()), Integer.valueOf(this.c.size()), Byte.valueOf(peek.getStatus()));
}
this.a = null;
}
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void b(MessageSnapshot messageSnapshot) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "notify paused %s", this.a);
}
this.b.i();
k(messageSnapshot);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public void d() {
if (this.d) {
return;
}
MessageSnapshot poll = this.c.poll();
byte status = poll.getStatus();
BaseDownloadTask.IRunningTask iRunningTask = this.a;
if (iRunningTask != null) {
BaseDownloadTask F = iRunningTask.F();
FileDownloadListener v = F.v();
ITaskHunter.IMessageHandler k = iRunningTask.k();
a(status);
if (v == null || v.isInvalid()) {
return;
}
if (status == 4) {
try {
v.blockComplete(F);
j(((BlockCompleteMessage) poll).transmitToCompleted());
return;
} catch (Throwable th) {
c(k.a(th));
return;
}
}
FileDownloadLargeFileListener fileDownloadLargeFileListener = v instanceof FileDownloadLargeFileListener ? (FileDownloadLargeFileListener) v : null;
if (status == -4) {
v.warn(F);
return;
}
if (status == -3) {
v.completed(F);
return;
}
if (status == -2) {
if (fileDownloadLargeFileListener != null) {
fileDownloadLargeFileListener.a(F, poll.getLargeSofarBytes(), poll.getLargeTotalBytes());
return;
} else {
v.paused(F, poll.getSmallSofarBytes(), poll.getSmallTotalBytes());
return;
}
}
if (status == -1) {
v.error(F, poll.getThrowable());
return;
}
if (status == 1) {
if (fileDownloadLargeFileListener != null) {
fileDownloadLargeFileListener.b(F, poll.getLargeSofarBytes(), poll.getLargeTotalBytes());
return;
} else {
v.pending(F, poll.getSmallSofarBytes(), poll.getSmallTotalBytes());
return;
}
}
if (status == 2) {
if (fileDownloadLargeFileListener != null) {
fileDownloadLargeFileListener.a(F, poll.getEtag(), poll.isResuming(), F.l(), poll.getLargeTotalBytes());
return;
} else {
v.connected(F, poll.getEtag(), poll.isResuming(), F.r(), poll.getSmallTotalBytes());
return;
}
}
if (status == 3) {
if (fileDownloadLargeFileListener != null) {
fileDownloadLargeFileListener.c(F, poll.getLargeSofarBytes(), F.t());
return;
} else {
v.progress(F, poll.getSmallSofarBytes(), F.f());
return;
}
}
if (status != 5) {
if (status != 6) {
return;
}
v.started(F);
return;
} else if (fileDownloadLargeFileListener != null) {
fileDownloadLargeFileListener.a(F, poll.getThrowable(), poll.getRetryingTimes(), poll.getLargeSofarBytes());
return;
} else {
v.retry(F, poll.getThrowable(), poll.getRetryingTimes(), poll.getSmallSofarBytes());
return;
}
}
throw new IllegalArgumentException(FileDownloadUtils.a("can't handover the message, no master to receive this message(status[%d]) size[%d]", Integer.valueOf(status), Integer.valueOf(this.c.size())));
}
@Override // com.liulishuo.filedownloader.IFileDownloadMessenger
public boolean a() {
return this.a.F().I();
}
}