|
|
@ -135,6 +135,8 @@ object CacheBook { |
|
|
|
private val onDownloadSet = linkedSetOf<Int>() |
|
|
|
private val onDownloadSet = linkedSetOf<Int>() |
|
|
|
private val successDownloadSet = linkedSetOf<Int>() |
|
|
|
private val successDownloadSet = linkedSetOf<Int>() |
|
|
|
private val errorDownloadMap = hashMapOf<Int, Int>() |
|
|
|
private val errorDownloadMap = hashMapOf<Int, Int>() |
|
|
|
|
|
|
|
private var isStopped = false |
|
|
|
|
|
|
|
private var waitingRetry = false |
|
|
|
|
|
|
|
|
|
|
|
val waitCount get() = waitDownloadSet.size |
|
|
|
val waitCount get() = waitDownloadSet.size |
|
|
|
val onDownloadCount get() = onDownloadSet.size |
|
|
|
val onDownloadCount get() = onDownloadSet.size |
|
|
@ -146,13 +148,20 @@ object CacheBook { |
|
|
|
return waitDownloadSet.size > 0 || onDownloadSet.size > 0 |
|
|
|
return waitDownloadSet.size > 0 || onDownloadSet.size > 0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
fun isStop(): Boolean { |
|
|
|
|
|
|
|
return isStopped || (!isRun() && !waitingRetry) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
fun stop() { |
|
|
|
fun stop() { |
|
|
|
waitDownloadSet.clear() |
|
|
|
waitDownloadSet.clear() |
|
|
|
|
|
|
|
isStopped = true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
fun addDownload(start: Int, end: Int) { |
|
|
|
fun addDownload(start: Int, end: Int) { |
|
|
|
|
|
|
|
isStopped = false |
|
|
|
for (i in start..end) { |
|
|
|
for (i in start..end) { |
|
|
|
if (!onDownloadSet.contains(i)) { |
|
|
|
if (!onDownloadSet.contains(i)) { |
|
|
|
waitDownloadSet.add(i) |
|
|
|
waitDownloadSet.add(i) |
|
|
@ -168,13 +177,18 @@ object CacheBook { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
private fun onError(index: Int, error: Throwable, chapterTitle: String) { |
|
|
|
private fun onPreError(index: Int, error: Throwable) { |
|
|
|
|
|
|
|
waitingRetry = true |
|
|
|
if (error !is ConcurrentException) { |
|
|
|
if (error !is ConcurrentException) { |
|
|
|
errorDownloadMap[index] = (errorDownloadMap[index] ?: 0) + 1 |
|
|
|
errorDownloadMap[index] = (errorDownloadMap[index] ?: 0) + 1 |
|
|
|
} |
|
|
|
} |
|
|
|
onDownloadSet.remove(index) |
|
|
|
onDownloadSet.remove(index) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
private fun onPostError(index: Int, error: Throwable, chapterTitle: String) { |
|
|
|
//重试3次 |
|
|
|
//重试3次 |
|
|
|
if ((errorDownloadMap[index] ?: 0) < 3) { |
|
|
|
if ((errorDownloadMap[index] ?: 0) < 3 && !isStopped) { |
|
|
|
waitDownloadSet.add(index) |
|
|
|
waitDownloadSet.add(index) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
AppLog.put( |
|
|
|
AppLog.put( |
|
|
@ -182,12 +196,19 @@ object CacheBook { |
|
|
|
error |
|
|
|
error |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
waitingRetry = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
private fun onError(index: Int, error: Throwable, chapterTitle: String) { |
|
|
|
|
|
|
|
onPreError(index, error) |
|
|
|
|
|
|
|
onPostError(index, error, chapterTitle) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
private fun onCancel(index: Int) { |
|
|
|
private fun onCancel(index: Int) { |
|
|
|
onDownloadSet.remove(index) |
|
|
|
onDownloadSet.remove(index) |
|
|
|
waitDownloadSet.add(index) |
|
|
|
if (!isStopped) waitDownloadSet.add(index) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
@ -234,9 +255,10 @@ object CacheBook { |
|
|
|
onSuccess(chapterIndex) |
|
|
|
onSuccess(chapterIndex) |
|
|
|
downloadFinish(chapter, content) |
|
|
|
downloadFinish(chapter, content) |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
|
|
|
|
onPreError(chapterIndex, it) |
|
|
|
//出现错误等待一秒后重新加入待下载列表 |
|
|
|
//出现错误等待一秒后重新加入待下载列表 |
|
|
|
delay(1000) |
|
|
|
delay(1000) |
|
|
|
onError(chapterIndex, it, chapter.title) |
|
|
|
onPostError(chapterIndex, it, chapter.title) |
|
|
|
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}") |
|
|
|
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}") |
|
|
|
}.onCancel { |
|
|
|
}.onCancel { |
|
|
|
onCancel(chapterIndex) |
|
|
|
onCancel(chapterIndex) |
|
|
|