From c01468f529bc3fc297af7375daea4158a3c5a6ba Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 3 Dec 2019 16:37:01 +0800 Subject: [PATCH] up --- .../io/legado/app/service/DownloadService.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index eaa34ed1b..db7b1a664 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -9,14 +9,14 @@ import io.legado.app.constant.Action import io.legado.app.constant.AppConst import io.legado.app.help.BookHelp import io.legado.app.help.IntentHelp +import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook -import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher -import kotlinx.coroutines.launch import java.util.concurrent.Executors class DownloadService : BaseService() { private var searchPool = Executors.newFixedThreadPool(16).asCoroutineDispatcher() + private var tasks: ArrayList> = arrayListOf() override fun onCreate() { super.onCreate() @@ -44,9 +44,9 @@ class DownloadService : BaseService() { private fun download(bookUrl: String?, start: Int, end: Int) { if (bookUrl == null) return - launch(IO) { - val book = App.db.bookDao().getBook(bookUrl) ?: return@launch - val bookSource = App.db.bookSourceDao().getBookSource(book.origin) ?: return@launch + val task = Coroutine.async { + val book = App.db.bookDao().getBook(bookUrl) ?: return@async + val bookSource = App.db.bookSourceDao().getBookSource(book.origin) ?: return@async val webBook = WebBook(bookSource) for (index in start..end) { App.db.bookChapterDao().getChapter(bookUrl, index)?.let { chapter -> @@ -64,6 +64,13 @@ class DownloadService : BaseService() { } } } + tasks.add(task) + task.invokeOnCompletion { + tasks.remove(task) + if (tasks.isEmpty()) { + stopSelf() + } + } } /**