|
|
@ -23,6 +23,8 @@ import kotlinx.coroutines.Dispatchers.IO |
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher |
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher |
|
|
|
import kotlinx.coroutines.isActive |
|
|
|
import kotlinx.coroutines.isActive |
|
|
|
import org.jetbrains.anko.toast |
|
|
|
import org.jetbrains.anko.toast |
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap |
|
|
|
|
|
|
|
import java.util.concurrent.CopyOnWriteArraySet |
|
|
|
import java.util.concurrent.Executors |
|
|
|
import java.util.concurrent.Executors |
|
|
|
|
|
|
|
|
|
|
|
class DownloadService : BaseService() { |
|
|
|
class DownloadService : BaseService() { |
|
|
@ -32,12 +34,12 @@ class DownloadService : BaseService() { |
|
|
|
private var tasks = CompositeCoroutine() |
|
|
|
private var tasks = CompositeCoroutine() |
|
|
|
private val handler = Handler() |
|
|
|
private val handler = Handler() |
|
|
|
private var runnable: Runnable = Runnable { upDownload() } |
|
|
|
private var runnable: Runnable = Runnable { upDownload() } |
|
|
|
private val bookMap = hashMapOf<String, Book>() |
|
|
|
private val bookMap = ConcurrentHashMap<String, Book>() |
|
|
|
private val webBookMap = hashMapOf<String, WebBook>() |
|
|
|
private val webBookMap = ConcurrentHashMap<String, WebBook>() |
|
|
|
private val downloadMap = hashMapOf<String, LinkedHashSet<BookChapter>>() |
|
|
|
private val downloadMap = ConcurrentHashMap<String, CopyOnWriteArraySet<BookChapter>>() |
|
|
|
private val downloadCount = hashMapOf<String, DownloadCount>() |
|
|
|
private val downloadCount = ConcurrentHashMap<String, DownloadCount>() |
|
|
|
private val finalMap = hashMapOf<String, LinkedHashSet<BookChapter>>() |
|
|
|
private val finalMap = ConcurrentHashMap<String, CopyOnWriteArraySet<BookChapter>>() |
|
|
|
private val downloadingList = arrayListOf<String>() |
|
|
|
private val downloadingList = CopyOnWriteArraySet<String>() |
|
|
|
|
|
|
|
|
|
|
|
@Volatile |
|
|
|
@Volatile |
|
|
|
private var downloadingCount = 0 |
|
|
|
private var downloadingCount = 0 |
|
|
@ -131,7 +133,7 @@ class DownloadService : BaseService() { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
App.db.bookChapterDao().getChapterList(bookUrl, start, end).let { |
|
|
|
App.db.bookChapterDao().getChapterList(bookUrl, start, end).let { |
|
|
|
if (it.isNotEmpty()) { |
|
|
|
if (it.isNotEmpty()) { |
|
|
|
val chapters = linkedSetOf<BookChapter>() |
|
|
|
val chapters = CopyOnWriteArraySet<BookChapter>() |
|
|
|
chapters.addAll(it) |
|
|
|
chapters.addAll(it) |
|
|
|
downloadMap[bookUrl] = chapters |
|
|
|
downloadMap[bookUrl] = chapters |
|
|
|
} |
|
|
|
} |
|
|
@ -202,7 +204,7 @@ class DownloadService : BaseService() { |
|
|
|
} |
|
|
|
} |
|
|
|
val chapterMap = |
|
|
|
val chapterMap = |
|
|
|
finalMap[book.bookUrl] |
|
|
|
finalMap[book.bookUrl] |
|
|
|
?: linkedSetOf<BookChapter>().apply { |
|
|
|
?: CopyOnWriteArraySet<BookChapter>().apply { |
|
|
|
finalMap[book.bookUrl] = this |
|
|
|
finalMap[book.bookUrl] = this |
|
|
|
} |
|
|
|
} |
|
|
|
chapterMap.add(bookChapter) |
|
|
|
chapterMap.add(bookChapter) |
|
|
|