|
|
|
@ -6,9 +6,10 @@ import androidx.core.app.NotificationCompat |
|
|
|
|
import io.legado.app.App |
|
|
|
|
import io.legado.app.R |
|
|
|
|
import io.legado.app.base.BaseService |
|
|
|
|
import io.legado.app.constant.IntentAction |
|
|
|
|
import io.legado.app.constant.AppConst |
|
|
|
|
import io.legado.app.constant.EventBus |
|
|
|
|
import io.legado.app.constant.IntentAction |
|
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
|
import io.legado.app.help.AppConfig |
|
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
|
import io.legado.app.help.IntentHelp |
|
|
|
@ -17,6 +18,7 @@ import io.legado.app.model.WebBook |
|
|
|
|
import io.legado.app.utils.postEvent |
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher |
|
|
|
|
import org.jetbrains.anko.toast |
|
|
|
|
import java.util.concurrent.Executors |
|
|
|
|
|
|
|
|
|
class DownloadService : BaseService() { |
|
|
|
@ -25,6 +27,8 @@ class DownloadService : BaseService() { |
|
|
|
|
private var tasks: ArrayList<Coroutine<*>> = arrayListOf() |
|
|
|
|
private val handler = Handler() |
|
|
|
|
private var runnable: Runnable = Runnable { upDownload() } |
|
|
|
|
private val downloadMap = hashMapOf<String, LinkedHashSet<BookChapter>>() |
|
|
|
|
private val finalMap = hashMapOf<String, LinkedHashSet<BookChapter>>() |
|
|
|
|
private var notificationContent = "正在启动下载" |
|
|
|
|
private val notificationBuilder by lazy { |
|
|
|
|
val builder = NotificationCompat.Builder(this, AppConst.channelIdDownload) |
|
|
|
@ -48,7 +52,7 @@ class DownloadService : BaseService() { |
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
|
|
|
|
intent?.action?.let { action -> |
|
|
|
|
when (action) { |
|
|
|
|
IntentAction.start -> download( |
|
|
|
|
IntentAction.start -> addDownloadData( |
|
|
|
|
intent.getStringExtra("bookUrl"), |
|
|
|
|
intent.getIntExtra("start", 0), |
|
|
|
|
intent.getIntExtra("end", 0) |
|
|
|
@ -67,14 +71,32 @@ class DownloadService : BaseService() { |
|
|
|
|
postEvent(EventBus.UP_DOWNLOAD, false) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun download(bookUrl: String?, start: Int, end: Int) { |
|
|
|
|
if (bookUrl == null) return |
|
|
|
|
val task = Coroutine.async(this) { |
|
|
|
|
val book = App.db.bookDao().getBook(bookUrl) ?: return@async |
|
|
|
|
val bookSource = App.db.bookSourceDao().getBookSource(book.origin) ?: return@async |
|
|
|
|
private fun addDownloadData(bookUrl: String?, start: Int, end: Int) { |
|
|
|
|
bookUrl ?: return |
|
|
|
|
if (downloadMap.containsKey(bookUrl)) { |
|
|
|
|
toast("该书已在下载列表") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
execute { |
|
|
|
|
val chapterMap = downloadMap[bookUrl] ?: linkedSetOf<BookChapter>().apply { |
|
|
|
|
downloadMap[bookUrl] = this |
|
|
|
|
} |
|
|
|
|
App.db.bookChapterDao().getChapterList(bookUrl, start, end).let { |
|
|
|
|
chapterMap.addAll(it) |
|
|
|
|
} |
|
|
|
|
download() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun download() { |
|
|
|
|
val task = Coroutine.async(this, context = searchPool) { |
|
|
|
|
downloadMap.forEach { entry -> |
|
|
|
|
if (!finalMap.containsKey(entry.key)) { |
|
|
|
|
val book = App.db.bookDao().getBook(entry.key) ?: 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 -> |
|
|
|
|
entry.value.forEach { chapter -> |
|
|
|
|
if (!BookHelp.hasContent(book, chapter)) { |
|
|
|
|
webBook.getContent(book, chapter, scope = this, context = searchPool) |
|
|
|
|
.onStart { |
|
|
|
@ -85,10 +107,27 @@ class DownloadService : BaseService() { |
|
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.onFinally(IO) { |
|
|
|
|
synchronized(this@DownloadService) { |
|
|
|
|
val chapterMap = |
|
|
|
|
finalMap[book.bookUrl] |
|
|
|
|
?: linkedSetOf<BookChapter>().apply { |
|
|
|
|
finalMap[book.bookUrl] = this |
|
|
|
|
} |
|
|
|
|
chapterMap.add(chapter) |
|
|
|
|
if (chapterMap.size == entry.value.size) { |
|
|
|
|
downloadMap.remove(book.bookUrl) |
|
|
|
|
finalMap.remove(book.bookUrl) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tasks.add(task) |
|
|
|
|
task.invokeOnCompletion { |
|
|
|
|
tasks.remove(task) |
|
|
|
|