122 lines
3.7 KiB
Java
122 lines
3.7 KiB
Java
package com.liulishuo.filedownloader;
|
|
|
|
import com.liulishuo.filedownloader.ITaskHunter;
|
|
import com.liulishuo.filedownloader.message.MessageSnapshotFlow;
|
|
import com.liulishuo.filedownloader.util.FileDownloadExecutors;
|
|
import com.liulishuo.filedownloader.util.FileDownloadLog;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
/* loaded from: classes.dex */
|
|
class FileDownloadTaskLauncher {
|
|
private final LaunchTaskPool a = new LaunchTaskPool();
|
|
|
|
private static class HolderClass {
|
|
private static final FileDownloadTaskLauncher a = new FileDownloadTaskLauncher();
|
|
|
|
static {
|
|
MessageSnapshotFlow.a().a(new MessageSnapshotGate());
|
|
}
|
|
}
|
|
|
|
private static class LaunchTaskPool {
|
|
private ThreadPoolExecutor a;
|
|
private LinkedBlockingQueue<Runnable> b;
|
|
|
|
LaunchTaskPool() {
|
|
a();
|
|
}
|
|
|
|
public void a(ITaskHunter.IStarter iStarter) {
|
|
this.a.execute(new LaunchTaskRunnable(iStarter));
|
|
}
|
|
|
|
public void b(ITaskHunter.IStarter iStarter) {
|
|
this.b.remove(iStarter);
|
|
}
|
|
|
|
public void a(FileDownloadListener fileDownloadListener) {
|
|
if (fileDownloadListener == null) {
|
|
FileDownloadLog.e(this, "want to expire by listener, but the listener provided is null", new Object[0]);
|
|
return;
|
|
}
|
|
ArrayList arrayList = new ArrayList();
|
|
Iterator<Runnable> it = this.b.iterator();
|
|
while (it.hasNext()) {
|
|
Runnable next = it.next();
|
|
LaunchTaskRunnable launchTaskRunnable = (LaunchTaskRunnable) next;
|
|
if (launchTaskRunnable.a(fileDownloadListener)) {
|
|
launchTaskRunnable.a();
|
|
arrayList.add(next);
|
|
}
|
|
}
|
|
if (arrayList.isEmpty()) {
|
|
return;
|
|
}
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "expire %d tasks with listener[%s]", Integer.valueOf(arrayList.size()), fileDownloadListener);
|
|
}
|
|
Iterator it2 = arrayList.iterator();
|
|
while (it2.hasNext()) {
|
|
this.a.remove((Runnable) it2.next());
|
|
}
|
|
}
|
|
|
|
private void a() {
|
|
this.b = new LinkedBlockingQueue<>();
|
|
this.a = FileDownloadExecutors.a(3, this.b, "LauncherTask");
|
|
}
|
|
}
|
|
|
|
private static class LaunchTaskRunnable implements Runnable {
|
|
private final ITaskHunter.IStarter a;
|
|
private boolean b = false;
|
|
|
|
LaunchTaskRunnable(ITaskHunter.IStarter iStarter) {
|
|
this.a = iStarter;
|
|
}
|
|
|
|
public boolean a(FileDownloadListener fileDownloadListener) {
|
|
ITaskHunter.IStarter iStarter = this.a;
|
|
return iStarter != null && iStarter.a(fileDownloadListener);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
return super.equals(obj) || obj == this.a;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
if (this.b) {
|
|
return;
|
|
}
|
|
this.a.start();
|
|
}
|
|
|
|
public void a() {
|
|
this.b = true;
|
|
}
|
|
}
|
|
|
|
FileDownloadTaskLauncher() {
|
|
}
|
|
|
|
public static FileDownloadTaskLauncher a() {
|
|
return HolderClass.a;
|
|
}
|
|
|
|
synchronized void b(ITaskHunter.IStarter iStarter) {
|
|
this.a.a(iStarter);
|
|
}
|
|
|
|
synchronized void a(ITaskHunter.IStarter iStarter) {
|
|
this.a.b(iStarter);
|
|
}
|
|
|
|
synchronized void a(FileDownloadListener fileDownloadListener) {
|
|
this.a.a(fileDownloadListener);
|
|
}
|
|
}
|