pull/1319/head
gedoor 3 years ago
parent a95804c153
commit 9f92344b97
  1. 30
      app/src/main/java/io/legado/app/model/CacheBook.kt
  2. 19
      app/src/main/java/io/legado/app/service/CacheBookService.kt

@ -92,9 +92,19 @@ class CacheBook(var bookSource: BookSource, var book: Book) {
}
}
val isRun: Boolean get() = waitDownloadCount > 0 || onDownloadCount > 0
val downloadSummary =
"正在下载:${onDownloadCount}|等待中:${waitDownloadCount}|失败:${errorCount}|成功:${successDownloadCount}"
val waitDownloadCount: Int
val isRun: Boolean
get() {
var isRun = false
cacheBookMap.forEach {
isRun = isRun || it.value.isRun()
}
return isRun
}
private val waitDownloadCount: Int
get() {
var count = 0
cacheBookMap.forEach {
@ -103,7 +113,7 @@ class CacheBook(var bookSource: BookSource, var book: Book) {
return count
}
val successDownloadCount: Int
private val successDownloadCount: Int
get() {
var count = 0
cacheBookMap.forEach {
@ -121,7 +131,7 @@ class CacheBook(var bookSource: BookSource, var book: Book) {
return count
}
val errorCount: Int
private val errorCount: Int
get() {
var count = 0
cacheBookMap.forEach {
@ -146,6 +156,11 @@ class CacheBook(var bookSource: BookSource, var book: Book) {
return waitDownloadSet.size > 0 || onDownloadSet.size > 0
}
@Synchronized
fun stop() {
waitDownloadSet.clear()
}
@Synchronized
fun addDownload(start: Int, end: Int) {
for (i in start..end) {
@ -224,11 +239,8 @@ class CacheBook(var bookSource: BookSource, var book: Book) {
onSuccess(chapterIndex)
downloadFinish(chapter, content)
}.onError {
//出现错误等待后重新加入待下载列表
when (it) {
is ConcurrentException -> delay(it.waitTime)
else -> delay(1000)
}
//出现错误等待一秒后重新加入待下载列表
delay(1000)
onError(chapterIndex, it, chapter.title)
downloadFinish(chapter, "error:${it.localizedMessage}")
}.onCancel {

@ -14,7 +14,6 @@ import io.legado.app.utils.activityPendingIntent
import io.legado.app.utils.postEvent
import io.legado.app.utils.servicePendingIntent
import kotlinx.coroutines.*
import splitties.init.appCtx
import java.util.concurrent.Executors
import kotlin.math.min
@ -24,8 +23,6 @@ class CacheBookService : BaseService() {
Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
private var downloadJob: Job? = null
private var notificationContent = appCtx.getString(R.string.starting_download)
private val notificationBuilder by lazy {
val builder = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download)
@ -42,11 +39,11 @@ class CacheBookService : BaseService() {
override fun onCreate() {
super.onCreate()
upNotification()
upNotification(getString(R.string.starting_download))
launch {
while (isActive) {
delay(1000)
upNotificationContent()
upNotification(CacheBook.downloadSummary)
postEvent(EventBus.UP_DOWNLOAD, "")
}
}
@ -86,7 +83,7 @@ class CacheBookService : BaseService() {
}
private fun removeDownload(bookUrl: String?) {
CacheBook.cacheBookMap.remove(bookUrl)
CacheBook.cacheBookMap[bookUrl]?.stop()
if (CacheBook.cacheBookMap.isEmpty()) {
stopSelf()
}
@ -95,7 +92,7 @@ class CacheBookService : BaseService() {
private fun download() {
downloadJob = launch(cachePool) {
while (isActive) {
if (CacheBook.cacheBookMap.isEmpty()) {
if (!CacheBook.isRun) {
CacheBook.stop(this@CacheBookService)
return@launch
}
@ -109,16 +106,10 @@ class CacheBookService : BaseService() {
}
}
private fun upNotificationContent() {
notificationContent =
"正在下载:${CacheBook.onDownloadCount}|等待中:${CacheBook.waitDownloadCount}|失败:${CacheBook.errorCount}|成功:${CacheBook.successDownloadCount}"
upNotification()
}
/**
* 更新通知
*/
private fun upNotification() {
private fun upNotification(notificationContent: String) {
notificationBuilder.setContentText(notificationContent)
val notification = notificationBuilder.build()
startForeground(AppConst.notificationIdDownload, notification)

Loading…
Cancel
Save