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

231 lines
8.2 KiB
Java

package com.liulishuo.filedownloader.services;
import android.text.TextUtils;
import com.liulishuo.filedownloader.IThreadPoolMonitor;
import com.liulishuo.filedownloader.database.FileDownloadDatabase;
import com.liulishuo.filedownloader.download.CustomComponentHolder;
import com.liulishuo.filedownloader.download.DownloadLaunchRunnable;
import com.liulishuo.filedownloader.model.ConnectionModel;
import com.liulishuo.filedownloader.model.FileDownloadHeader;
import com.liulishuo.filedownloader.model.FileDownloadModel;
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.util.Iterator;
import java.util.List;
/* loaded from: classes.dex */
class FileDownloadManager implements IThreadPoolMonitor {
private final FileDownloadDatabase a;
private final FileDownloadThreadPool b;
FileDownloadManager() {
CustomComponentHolder i = CustomComponentHolder.i();
this.a = i.a();
this.b = new FileDownloadThreadPool(i.d());
}
public synchronized void a(String str, String str2, boolean z, int i, int i2, int i3, boolean z2, FileDownloadHeader fileDownloadHeader, boolean z3) {
FileDownloadModel fileDownloadModel;
List<ConnectionModel> list;
boolean z4 = true;
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "request start the task with url(%s) path(%s) isDirectory(%B)", str, str2, Boolean.valueOf(z));
}
int a = FileDownloadUtils.a(str, str2, z);
FileDownloadModel e = this.a.e(a);
if (z || e != null) {
fileDownloadModel = e;
list = null;
} else {
int a2 = FileDownloadUtils.a(str, FileDownloadUtils.i(str2), true);
FileDownloadModel e2 = this.a.e(a2);
if (e2 == null || !str2.equals(e2.getTargetFilePath())) {
list = null;
} else {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "task[%d] find model by dirCaseId[%d]", Integer.valueOf(a), Integer.valueOf(a2));
}
list = this.a.d(a2);
}
fileDownloadModel = e2;
}
if (FileDownloadHelper.a(a, fileDownloadModel, (IThreadPoolMonitor) this, true)) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "has already started download %d", Integer.valueOf(a));
}
return;
}
String targetFilePath = fileDownloadModel != null ? fileDownloadModel.getTargetFilePath() : FileDownloadUtils.a(str2, z, (String) null);
if (FileDownloadHelper.a(a, targetFilePath, z2, true)) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "has already completed downloading %d", Integer.valueOf(a));
}
return;
}
if (FileDownloadHelper.a(a, fileDownloadModel != null ? fileDownloadModel.getSoFar() : 0L, fileDownloadModel != null ? fileDownloadModel.getTempFilePath() : FileDownloadUtils.j(targetFilePath), targetFilePath, this)) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "there is an another task with the same target-file-path %d %s", Integer.valueOf(a), targetFilePath);
}
if (fileDownloadModel != null) {
this.a.remove(a);
this.a.b(a);
}
return;
}
if (fileDownloadModel == null || !(fileDownloadModel.getStatus() == -2 || fileDownloadModel.getStatus() == -1 || fileDownloadModel.getStatus() == 1 || fileDownloadModel.getStatus() == 6 || fileDownloadModel.getStatus() == 2)) {
if (fileDownloadModel == null) {
fileDownloadModel = new FileDownloadModel();
}
fileDownloadModel.setUrl(str);
fileDownloadModel.setPath(str2, z);
fileDownloadModel.setId(a);
fileDownloadModel.setSoFar(0L);
fileDownloadModel.setTotal(0L);
fileDownloadModel.setStatus((byte) 1);
fileDownloadModel.setConnectionCount(1);
} else if (fileDownloadModel.getId() != a) {
this.a.remove(fileDownloadModel.getId());
this.a.b(fileDownloadModel.getId());
fileDownloadModel.setId(a);
fileDownloadModel.setPath(str2, z);
if (list != null) {
for (ConnectionModel connectionModel : list) {
connectionModel.a(a);
this.a.a(connectionModel);
}
}
} else if (TextUtils.equals(str, fileDownloadModel.getUrl())) {
z4 = false;
} else {
fileDownloadModel.setUrl(str);
}
if (z4) {
this.a.a(fileDownloadModel);
}
DownloadLaunchRunnable.Builder builder = new DownloadLaunchRunnable.Builder();
builder.a(fileDownloadModel);
builder.a(fileDownloadHeader);
builder.a(this);
builder.c(Integer.valueOf(i2));
builder.a(Integer.valueOf(i));
builder.a(Boolean.valueOf(z2));
builder.b(Boolean.valueOf(z3));
builder.b(Integer.valueOf(i3));
this.b.a(builder.a());
}
public long b(int i) {
FileDownloadModel e = this.a.e(i);
if (e == null) {
return 0L;
}
int connectionCount = e.getConnectionCount();
if (connectionCount <= 1) {
return e.getSoFar();
}
List<ConnectionModel> d = this.a.d(i);
if (d == null || d.size() != connectionCount) {
return 0L;
}
return ConnectionModel.a(d);
}
public void c() {
List<Integer> b = this.b.b();
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "pause all tasks %d", Integer.valueOf(b.size()));
}
Iterator<Integer> it = b.iterator();
while (it.hasNext()) {
f(it.next().intValue());
}
}
public long d(int i) {
FileDownloadModel e = this.a.e(i);
if (e == null) {
return 0L;
}
return e.getTotal();
}
public boolean e(int i) {
return a(this.a.e(i));
}
public boolean f(int i) {
if (FileDownloadLog.a) {
FileDownloadLog.a(this, "request pause the task %d", Integer.valueOf(i));
}
FileDownloadModel e = this.a.e(i);
if (e == null) {
return false;
}
e.setStatus((byte) -2);
this.b.a(i);
return true;
}
public synchronized boolean g(int i) {
return this.b.c(i);
}
public byte c(int i) {
FileDownloadModel e = this.a.e(i);
if (e == null) {
return (byte) 0;
}
return e.getStatus();
}
public boolean b() {
return this.b.a() <= 0;
}
public boolean a(String str, String str2) {
return e(FileDownloadUtils.c(str, str2));
}
@Override // com.liulishuo.filedownloader.IThreadPoolMonitor
public boolean a(FileDownloadModel fileDownloadModel) {
if (fileDownloadModel == null) {
return false;
}
boolean b = this.b.b(fileDownloadModel.getId());
if (FileDownloadStatus.b(fileDownloadModel.getStatus())) {
if (!b) {
return false;
}
} else if (!b) {
FileDownloadLog.b(this, "%d status is[%s](not finish) & but not in the pool", Integer.valueOf(fileDownloadModel.getId()), Byte.valueOf(fileDownloadModel.getStatus()));
return false;
}
return true;
}
@Override // com.liulishuo.filedownloader.IThreadPoolMonitor
public int a(String str, int i) {
return this.b.a(str, i);
}
public boolean a(int i) {
if (i == 0) {
FileDownloadLog.e(this, "The task[%d] id is invalid, can't clear it.", Integer.valueOf(i));
return false;
}
if (e(i)) {
FileDownloadLog.e(this, "The task[%d] is downloading, can't clear it.", Integer.valueOf(i));
return false;
}
this.a.remove(i);
this.a.b(i);
return true;
}
public void a() {
this.a.clear();
}
}