|
|
@ -2,6 +2,7 @@ package io.legado.app.model |
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint |
|
|
|
import android.annotation.SuppressLint |
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
|
|
|
|
import io.legado.app.constant.EventBus |
|
|
|
import io.legado.app.constant.IntentAction |
|
|
|
import io.legado.app.constant.IntentAction |
|
|
|
import io.legado.app.data.appDb |
|
|
|
import io.legado.app.data.appDb |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.Book |
|
|
@ -10,10 +11,12 @@ import io.legado.app.data.entities.BookSource |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.model.webBook.WebBook |
|
|
|
import io.legado.app.model.webBook.WebBook |
|
|
|
import io.legado.app.service.CacheBookService |
|
|
|
import io.legado.app.service.CacheBookService |
|
|
|
|
|
|
|
import io.legado.app.utils.postEvent |
|
|
|
import io.legado.app.utils.startService |
|
|
|
import io.legado.app.utils.startService |
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
import java.text.SimpleDateFormat |
|
|
|
import java.text.SimpleDateFormat |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap |
|
|
|
import java.util.concurrent.CopyOnWriteArraySet |
|
|
|
import java.util.concurrent.CopyOnWriteArraySet |
|
|
|
import kotlin.coroutines.CoroutineContext |
|
|
|
import kotlin.coroutines.CoroutineContext |
|
|
|
|
|
|
|
|
|
|
@ -22,7 +25,7 @@ class CacheBook(val bookSource: BookSource, val book: Book) { |
|
|
|
companion object { |
|
|
|
companion object { |
|
|
|
|
|
|
|
|
|
|
|
val logs = arrayListOf<String>() |
|
|
|
val logs = arrayListOf<String>() |
|
|
|
val cacheBookMap = hashMapOf<String, CacheBook>() |
|
|
|
val cacheBookMap = ConcurrentHashMap<String, CacheBook>() |
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("ConstantLocale") |
|
|
|
@SuppressLint("ConstantLocale") |
|
|
|
private val logTimeFormat = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) |
|
|
|
private val logTimeFormat = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) |
|
|
@ -83,6 +86,8 @@ class CacheBook(val bookSource: BookSource, val book: Book) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val isRun: Boolean get() = waitDownloadCount > 0 || onDownloadCount > 0 |
|
|
|
|
|
|
|
|
|
|
|
val waitDownloadCount: Int |
|
|
|
val waitDownloadCount: Int |
|
|
|
get() { |
|
|
|
get() { |
|
|
|
var count = 0 |
|
|
|
var count = 0 |
|
|
@ -113,8 +118,8 @@ class CacheBook(val bookSource: BookSource, val book: Book) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val waitDownloadSet = CopyOnWriteArraySet<Int>() |
|
|
|
val waitDownloadSet = CopyOnWriteArraySet<Int>() |
|
|
|
val successDownloadSet = CopyOnWriteArraySet<Int>() |
|
|
|
|
|
|
|
val onDownloadSet = CopyOnWriteArraySet<Int>() |
|
|
|
val onDownloadSet = CopyOnWriteArraySet<Int>() |
|
|
|
|
|
|
|
val successDownloadSet = CopyOnWriteArraySet<Int>() |
|
|
|
|
|
|
|
|
|
|
|
fun addDownload(start: Int, end: Int) { |
|
|
|
fun addDownload(start: Int, end: Int) { |
|
|
|
for (i in start..end) { |
|
|
|
for (i in start..end) { |
|
|
@ -122,6 +127,10 @@ class CacheBook(val bookSource: BookSource, val book: Book) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun isRun(): Boolean { |
|
|
|
|
|
|
|
return waitDownloadSet.size > 0 || onDownloadSet.size > 0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
fun download(scope: CoroutineScope, context: CoroutineContext): Boolean { |
|
|
|
fun download(scope: CoroutineScope, context: CoroutineContext): Boolean { |
|
|
|
val chapterIndex = waitDownloadSet.firstOrNull() ?: return false |
|
|
|
val chapterIndex = waitDownloadSet.firstOrNull() ?: return false |
|
|
@ -158,6 +167,10 @@ class CacheBook(val bookSource: BookSource, val book: Book) { |
|
|
|
}.onCancel { |
|
|
|
}.onCancel { |
|
|
|
onDownloadSet.remove(chapterIndex) |
|
|
|
onDownloadSet.remove(chapterIndex) |
|
|
|
waitDownloadSet.add(chapterIndex) |
|
|
|
waitDownloadSet.add(chapterIndex) |
|
|
|
|
|
|
|
}.onFinally { |
|
|
|
|
|
|
|
if (waitDownloadSet.isEmpty() && onDownloadSet.isEmpty()) { |
|
|
|
|
|
|
|
postEvent(EventBus.UP_DOWNLOAD, "") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|