139 lines
4.2 KiB
Java
139 lines
4.2 KiB
Java
package com.liulishuo.filedownloader;
|
|
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.Message;
|
|
import com.liulishuo.filedownloader.util.FileDownloadExecutors;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class FileDownloadMessageStation {
|
|
static int f = 10;
|
|
static int g = 5;
|
|
private final Executor a;
|
|
private final Handler b;
|
|
private final LinkedBlockingQueue<IFileDownloadMessenger> c;
|
|
private final Object d;
|
|
private final ArrayList<IFileDownloadMessenger> e;
|
|
|
|
private static final class HolderClass {
|
|
private static final FileDownloadMessageStation a = new FileDownloadMessageStation();
|
|
}
|
|
|
|
private static class UIHandlerCallback implements Handler.Callback {
|
|
private UIHandlerCallback() {
|
|
}
|
|
|
|
private void a(ArrayList<IFileDownloadMessenger> arrayList) {
|
|
Iterator<IFileDownloadMessenger> it = arrayList.iterator();
|
|
while (it.hasNext()) {
|
|
it.next().d();
|
|
}
|
|
arrayList.clear();
|
|
}
|
|
|
|
@Override // android.os.Handler.Callback
|
|
public boolean handleMessage(Message message) {
|
|
int i = message.what;
|
|
if (i == 1) {
|
|
((IFileDownloadMessenger) message.obj).d();
|
|
} else if (i == 2) {
|
|
a((ArrayList) message.obj);
|
|
FileDownloadMessageStation.a().c();
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private void b(IFileDownloadMessenger iFileDownloadMessenger) {
|
|
synchronized (this.d) {
|
|
this.c.offer(iFileDownloadMessenger);
|
|
}
|
|
c();
|
|
}
|
|
|
|
private void c(IFileDownloadMessenger iFileDownloadMessenger) {
|
|
Handler handler = this.b;
|
|
handler.sendMessage(handler.obtainMessage(1, iFileDownloadMessenger));
|
|
}
|
|
|
|
private FileDownloadMessageStation() {
|
|
this.a = FileDownloadExecutors.a(5, "BlockCompleted");
|
|
this.d = new Object();
|
|
this.e = new ArrayList<>();
|
|
this.b = new Handler(Looper.getMainLooper(), new UIHandlerCallback());
|
|
this.c = new LinkedBlockingQueue<>();
|
|
}
|
|
|
|
public static FileDownloadMessageStation a() {
|
|
return HolderClass.a;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void c() {
|
|
int i;
|
|
synchronized (this.d) {
|
|
if (this.e.isEmpty()) {
|
|
if (this.c.isEmpty()) {
|
|
return;
|
|
}
|
|
if (b()) {
|
|
i = f;
|
|
int min = Math.min(this.c.size(), g);
|
|
for (int i2 = 0; i2 < min; i2++) {
|
|
this.e.add(this.c.remove());
|
|
}
|
|
} else {
|
|
this.c.drainTo(this.e);
|
|
i = 0;
|
|
}
|
|
Handler handler = this.b;
|
|
handler.sendMessageDelayed(handler.obtainMessage(2, this.e), i);
|
|
}
|
|
}
|
|
}
|
|
|
|
void a(IFileDownloadMessenger iFileDownloadMessenger) {
|
|
a(iFileDownloadMessenger, false);
|
|
}
|
|
|
|
void a(final IFileDownloadMessenger iFileDownloadMessenger, boolean z) {
|
|
if (iFileDownloadMessenger.a()) {
|
|
iFileDownloadMessenger.d();
|
|
return;
|
|
}
|
|
if (iFileDownloadMessenger.c()) {
|
|
this.a.execute(new Runnable(this) { // from class: com.liulishuo.filedownloader.FileDownloadMessageStation.1
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
iFileDownloadMessenger.d();
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
if (!b() && !this.c.isEmpty()) {
|
|
synchronized (this.d) {
|
|
if (!this.c.isEmpty()) {
|
|
Iterator<IFileDownloadMessenger> it = this.c.iterator();
|
|
while (it.hasNext()) {
|
|
c(it.next());
|
|
}
|
|
}
|
|
this.c.clear();
|
|
}
|
|
}
|
|
if (b() && !z) {
|
|
b(iFileDownloadMessenger);
|
|
} else {
|
|
c(iFileDownloadMessenger);
|
|
}
|
|
}
|
|
|
|
public static boolean b() {
|
|
return f > 0;
|
|
}
|
|
}
|