|
|
@ -31,8 +31,8 @@ import kotlin.math.min |
|
|
|
|
|
|
|
|
|
|
|
class CacheBookService : BaseService() { |
|
|
|
class CacheBookService : BaseService() { |
|
|
|
private val threadCount = AppConfig.threadCount |
|
|
|
private val threadCount = AppConfig.threadCount |
|
|
|
private var searchPool = |
|
|
|
private var cachePool = |
|
|
|
Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher() |
|
|
|
Executors.newFixedThreadPool(min(threadCount, 8)).asCoroutineDispatcher() |
|
|
|
private var tasks = CompositeCoroutine() |
|
|
|
private var tasks = CompositeCoroutine() |
|
|
|
private val handler = Handler(Looper.getMainLooper()) |
|
|
|
private val handler = Handler(Looper.getMainLooper()) |
|
|
|
private var runnable: Runnable = Runnable { upDownload() } |
|
|
|
private var runnable: Runnable = Runnable { upDownload() } |
|
|
@ -83,7 +83,7 @@ class CacheBookService : BaseService() { |
|
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() { |
|
|
|
override fun onDestroy() { |
|
|
|
tasks.clear() |
|
|
|
tasks.clear() |
|
|
|
searchPool.close() |
|
|
|
cachePool.close() |
|
|
|
handler.removeCallbacks(runnable) |
|
|
|
handler.removeCallbacks(runnable) |
|
|
|
downloadMap.clear() |
|
|
|
downloadMap.clear() |
|
|
|
finalMap.clear() |
|
|
|
finalMap.clear() |
|
|
@ -134,7 +134,7 @@ class CacheBookService : BaseService() { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
downloadCount[bookUrl] = DownloadCount() |
|
|
|
downloadCount[bookUrl] = DownloadCount() |
|
|
|
execute { |
|
|
|
execute(context = cachePool) { |
|
|
|
appDb.bookChapterDao.getChapterList(bookUrl, start, end).let { |
|
|
|
appDb.bookChapterDao.getChapterList(bookUrl, start, end).let { |
|
|
|
if (it.isNotEmpty()) { |
|
|
|
if (it.isNotEmpty()) { |
|
|
|
val chapters = CopyOnWriteArraySet<BookChapter>() |
|
|
|
val chapters = CopyOnWriteArraySet<BookChapter>() |
|
|
@ -159,7 +159,7 @@ class CacheBookService : BaseService() { |
|
|
|
|
|
|
|
|
|
|
|
private fun download() { |
|
|
|
private fun download() { |
|
|
|
downloadingCount += 1 |
|
|
|
downloadingCount += 1 |
|
|
|
val task = Coroutine.async(this, context = searchPool) { |
|
|
|
val task = Coroutine.async(this, context = cachePool) { |
|
|
|
if (!isActive) return@async |
|
|
|
if (!isActive) return@async |
|
|
|
val bookChapter: BookChapter? = synchronized(this@CacheBookService) { |
|
|
|
val bookChapter: BookChapter? = synchronized(this@CacheBookService) { |
|
|
|
downloadMap.forEach { |
|
|
|
downloadMap.forEach { |
|
|
@ -186,16 +186,16 @@ class CacheBookService : BaseService() { |
|
|
|
return@async |
|
|
|
return@async |
|
|
|
} |
|
|
|
} |
|
|
|
if (!BookHelp.hasImageContent(book, bookChapter)) { |
|
|
|
if (!BookHelp.hasImageContent(book, bookChapter)) { |
|
|
|
webBook.getContent(this, book, bookChapter, context = searchPool) |
|
|
|
webBook.getContent(this, book, bookChapter, context = cachePool) |
|
|
|
.timeout(60000L) |
|
|
|
.timeout(60000L) |
|
|
|
.onError { |
|
|
|
.onError(cachePool) { |
|
|
|
synchronized(this) { |
|
|
|
synchronized(this) { |
|
|
|
downloadingList.remove(bookChapter.url) |
|
|
|
downloadingList.remove(bookChapter.url) |
|
|
|
} |
|
|
|
} |
|
|
|
notificationContent = "getContentError${it.localizedMessage}" |
|
|
|
notificationContent = "getContentError${it.localizedMessage}" |
|
|
|
upNotification() |
|
|
|
upNotification() |
|
|
|
} |
|
|
|
} |
|
|
|
.onSuccess { |
|
|
|
.onSuccess(cachePool) { |
|
|
|
synchronized(this@CacheBookService) { |
|
|
|
synchronized(this@CacheBookService) { |
|
|
|
downloadCount[book.bookUrl]?.increaseSuccess() |
|
|
|
downloadCount[book.bookUrl]?.increaseSuccess() |
|
|
|
downloadCount[book.bookUrl]?.increaseFinished() |
|
|
|
downloadCount[book.bookUrl]?.increaseFinished() |
|
|
@ -218,7 +218,7 @@ class CacheBookService : BaseService() { |
|
|
|
downloadCount.remove(book.bookUrl) |
|
|
|
downloadCount.remove(book.bookUrl) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}.onFinally { |
|
|
|
}.onFinally(cachePool) { |
|
|
|
postDownloading(true) |
|
|
|
postDownloading(true) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -228,7 +228,7 @@ class CacheBookService : BaseService() { |
|
|
|
postDownloading(true) |
|
|
|
postDownloading(true) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
}.onError(cachePool) { |
|
|
|
notificationContent = "ERROR:${it.localizedMessage}" |
|
|
|
notificationContent = "ERROR:${it.localizedMessage}" |
|
|
|
CacheBook.addLog(notificationContent) |
|
|
|
CacheBook.addLog(notificationContent) |
|
|
|
upNotification() |
|
|
|
upNotification() |
|
|
|