Fix download

This commit is contained in:
tarsin committed 2023-02-05 13:16:53 +08:00
1 parent 933a95023c
commit 4a4c447bd2
2 files changed
+17 -13

No files matched your search

@@ -49,7 +49,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -94,7 +93,6 @@ public final class SpiderQueen implements Runnable {
private final Thread[] mDecodeThreadArray = new Thread[DECODE_THREAD_NUM];
private final int[] mDecodeIndexArray = new int[DECODE_THREAD_NUM];
private final Queue<Integer> mDecodeRequestQueue = new LinkedList<>();
private final Queue<Integer> mRequestPTokenQueue = new ConcurrentLinkedQueue<>();
private final Object mPageStateLock = new Object();
private final AtomicInteger mDownloadedPages = new AtomicInteger(0);
private final AtomicInteger mFinishedPages = new AtomicInteger(0);
@@ -108,8 +106,6 @@ public final class SpiderQueen implements Runnable {
@Nullable
private volatile Thread mQueenThread;
private SpiderQueenWorker mWorkerScope;
// For download, when it go to mPageStateArray.size(), done
private volatile int mDownloadPage = -1;
private boolean mStoped = false;
private SpiderQueen(@NonNull GalleryInfo galleryInfo) {
@@ -278,6 +274,8 @@ public final class SpiderQueen implements Runnable {
}
}
private boolean shouldIntoDownload = false;
private void updateMode() {
int mode;
if (mDownloadReference > 0) {
@@ -289,15 +287,7 @@ public final class SpiderQueen implements Runnable {
mSpiderDen.setMode(mode);
// Update download page
boolean intoDownloadMode = false;
if (mode == MODE_DOWNLOAD) {
if (mDownloadPage < 0) {
mDownloadPage = 0;
intoDownloadMode = true;
}
} else {
mDownloadPage = -1;
}
boolean intoDownloadMode = mode == MODE_DOWNLOAD;
if (intoDownloadMode && mPageStateArray != null) {
// Clear download state
@@ -314,6 +304,8 @@ public final class SpiderQueen implements Runnable {
mPageErrorMap.clear();
mPagePercentMap.clear();
}
} else if (intoDownloadMode && mPageStateArray == null) {
shouldIntoDownload = true;
}
}
@@ -691,6 +683,8 @@ public final class SpiderQueen implements Runnable {
// Notify get pages
notifyGetPages(spiderInfo.getPages());
if (shouldIntoDownload) mWorkerScope.enterDownloadMode();
// Start decoder
for (int i = 0; i < DECODE_THREAD_NUM; i++) {
Thread decoderThread = new PriorityThread(new SpiderDecoder(i),
@@ -37,6 +37,7 @@ class SpiderQueenWorker(private val queen: SpiderQueen) : CoroutineScope {
private var showKey: String? = null
private val showKeyLock = Any()
private val mDownloadDelay = Settings.getDownloadDelay()
private var isDownloadMode = false
private val size
get() = queen.size()
override val coroutineContext: CoroutineContext
@@ -48,7 +49,15 @@ class SpiderQueenWorker(private val queen: SpiderQueen) : CoroutineScope {
}
}
@Synchronized
fun enterDownloadMode() {
if (isDownloadMode) return
updateRAList((0 until size).toList())
isDownloadMode = true
}
fun updateRAList(list: List<Int>) {
if (isDownloadMode) return
synchronized(mJobMap) {
sequence {
mJobMap.forEach { (i, job) ->
@@ -67,6 +76,7 @@ class SpiderQueenWorker(private val queen: SpiderQueen) : CoroutineScope {
@JvmOverloads
fun launch(index: Int, force: Boolean = false) {
if (isDownloadMode) return
check(index in 0 until size)
val state = queen.mPageStateArray[index]
if (!force && state == STATE_FINISHED) return