package com.liulishuo.filedownloader.services; import android.util.SparseArray; import com.liulishuo.filedownloader.download.DownloadLaunchRunnable; import com.liulishuo.filedownloader.util.FileDownloadExecutors; import com.liulishuo.filedownloader.util.FileDownloadLog; import com.liulishuo.filedownloader.util.FileDownloadProperties; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadPoolExecutor; /* loaded from: classes.dex */ class FileDownloadThreadPool { private ThreadPoolExecutor b; private int c; private SparseArray a = new SparseArray<>(); private int d = 0; FileDownloadThreadPool(int i) { this.b = FileDownloadExecutors.a(i, "Network"); this.c = i; } public void a(DownloadLaunchRunnable downloadLaunchRunnable) { downloadLaunchRunnable.e(); synchronized (this) { this.a.put(downloadLaunchRunnable.b(), downloadLaunchRunnable); } this.b.execute(downloadLaunchRunnable); int i = this.d; if (i < 600) { this.d = i + 1; } else { c(); this.d = 0; } } public boolean b(int i) { DownloadLaunchRunnable downloadLaunchRunnable = this.a.get(i); return downloadLaunchRunnable != null && downloadLaunchRunnable.d(); } public synchronized boolean c(int i) { if (a() > 0) { FileDownloadLog.e(this, "Can't change the max network thread count, because the network thread pool isn't in IDLE, please try again after all running tasks are completed or invoking FileDownloader#pauseAll directly.", new Object[0]); return false; } int a = FileDownloadProperties.a(i); if (FileDownloadLog.a) { FileDownloadLog.a(this, "change the max network thread count, from %d to %d", Integer.valueOf(this.c), Integer.valueOf(a)); } List shutdownNow = this.b.shutdownNow(); this.b = FileDownloadExecutors.a(a, "Network"); if (shutdownNow.size() > 0) { FileDownloadLog.e(this, "recreate the network thread pool and discard %d tasks", Integer.valueOf(shutdownNow.size())); } this.c = a; return true; } public synchronized List b() { ArrayList arrayList; c(); arrayList = new ArrayList(); for (int i = 0; i < this.a.size(); i++) { arrayList.add(Integer.valueOf(this.a.get(this.a.keyAt(i)).b())); } return arrayList; } public void a(int i) { c(); synchronized (this) { DownloadLaunchRunnable downloadLaunchRunnable = this.a.get(i); if (downloadLaunchRunnable != null) { downloadLaunchRunnable.pause(); boolean remove = this.b.remove(downloadLaunchRunnable); if (FileDownloadLog.a) { FileDownloadLog.a(this, "successful cancel %d %B", Integer.valueOf(i), Boolean.valueOf(remove)); } } this.a.remove(i); } } private synchronized void c() { SparseArray sparseArray = new SparseArray<>(); int size = this.a.size(); for (int i = 0; i < size; i++) { int keyAt = this.a.keyAt(i); DownloadLaunchRunnable downloadLaunchRunnable = this.a.get(keyAt); if (downloadLaunchRunnable.d()) { sparseArray.put(keyAt, downloadLaunchRunnable); } } this.a = sparseArray; } public int a(String str, int i) { if (str == null) { return 0; } int size = this.a.size(); for (int i2 = 0; i2 < size; i2++) { DownloadLaunchRunnable valueAt = this.a.valueAt(i2); if (valueAt != null && valueAt.d() && valueAt.b() != i && str.equals(valueAt.c())) { return valueAt.b(); } } return 0; } public synchronized int a() { c(); return this.a.size(); } }