From 9f92344b97b51c7d88ce804562d00cc7f6edf31a Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 9 Sep 2021 22:16:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/model/CacheBook.kt | 30 +++++++++++++------ .../io/legado/app/service/CacheBookService.kt | 19 ++++-------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/CacheBook.kt b/app/src/main/java/io/legado/app/model/CacheBook.kt index efb1a05af..ab88bbfab 100644 --- a/app/src/main/java/io/legado/app/model/CacheBook.kt +++ b/app/src/main/java/io/legado/app/model/CacheBook.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 { diff --git a/app/src/main/java/io/legado/app/service/CacheBookService.kt b/app/src/main/java/io/legado/app/service/CacheBookService.kt index 600b23f23..7d411775d 100644 --- a/app/src/main/java/io/legado/app/service/CacheBookService.kt +++ b/app/src/main/java/io/legado/app/service/CacheBookService.kt @@ -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)