Singletonize DownloadManager

This commit is contained in:
FooIbar authored and tarsin committed 2023-02-15 02:18:57 +08:00
1 parent 83b40d2ccb
commit f234dc61bc
3 files changed
+52 -61

No files matched your search

@@ -301,7 +301,7 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
} }
@JvmStatic @JvmStatic
val downloadManager by lazy { DownloadManager() } val downloadManager by lazy { DownloadManager }
@JvmStatic @JvmStatic
val hosts by lazy { Hosts(application, "hosts.db") } val hosts by lazy { Hosts(application, "hosts.db") }
@@ -15,13 +15,11 @@
*/ */
package com.hippo.ehviewer.download package com.hippo.ehviewer.download
import android.annotation.SuppressLint
import android.os.AsyncTask
import android.util.Log import android.util.Log
import android.util.SparseLongArray import android.util.SparseLongArray
import androidx.collection.LongSparseArray import androidx.collection.LongSparseArray
import androidx.collection.keyIterator
import com.hippo.ehviewer.EhDB import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.client.data.BaseGalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.dao.DownloadInfo import com.hippo.ehviewer.dao.DownloadInfo
import com.hippo.ehviewer.dao.DownloadLabel import com.hippo.ehviewer.dao.DownloadLabel
@@ -31,16 +29,18 @@ import com.hippo.ehviewer.spider.SpiderQueen.OnSpiderListener
import com.hippo.ehviewer.spider.readCompatFromUniFile import com.hippo.ehviewer.spider.readCompatFromUniFile
import com.hippo.ehviewer.spider.write import com.hippo.ehviewer.spider.write
import com.hippo.image.Image import com.hippo.image.Image
import com.hippo.util.IoThreadPoolExecutor
import com.hippo.yorozuya.ConcurrentPool import com.hippo.yorozuya.ConcurrentPool
import com.hippo.yorozuya.MathUtils import com.hippo.yorozuya.MathUtils
import com.hippo.yorozuya.ObjectUtils import com.hippo.yorozuya.ObjectUtils
import com.hippo.yorozuya.SimpleHandler import com.hippo.yorozuya.SimpleHandler
import com.hippo.yorozuya.collect.LongList import com.hippo.yorozuya.collect.LongList
import java.io.IOException import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import java.util.LinkedList import java.util.LinkedList
class DownloadManager : OnSpiderListener { object DownloadManager : OnSpiderListener {
private val downloadManager = this
// All download info list // All download info list
private val mAllInfoList: LinkedList<DownloadInfo> private val mAllInfoList: LinkedList<DownloadInfo>
@@ -564,35 +564,25 @@ class DownloadManager : OnSpiderListener {
} }
} }
@SuppressLint("StaticFieldLeak") suspend fun resetAllReadingProgress() = coroutineScope {
fun resetAllReadingProgress() { mAllInfoMap.keyIterator().forEach { gid ->
val list = LinkedList(mAllInfoList) launch {
object : AsyncTask<Void?, Void?, Void?>() { runCatching {
@Deprecated("Deprecated in Java") resetReadingProgress(gid)
override fun doInBackground(vararg voids: Void?): Void? { Log.d(TAG, "Current thread: ${Thread.currentThread().name}")
val galleryInfo: GalleryInfo = BaseGalleryInfo() }.onFailure {
for (downloadInfo in list) { Log.e(TAG, "Can't write SpiderInfo", it)
galleryInfo.gid = downloadInfo.gid
galleryInfo.token = downloadInfo.token
galleryInfo.title = downloadInfo.title
galleryInfo.thumb = downloadInfo.thumb
galleryInfo.category = downloadInfo.category
galleryInfo.posted = downloadInfo.posted
galleryInfo.uploader = downloadInfo.uploader
galleryInfo.rating = downloadInfo.rating
val downloadDir = SpiderDen.getGalleryDownloadDir(galleryInfo.gid) ?: continue
val file = downloadDir.findFile(SpiderQueen.SPIDER_INFO_FILENAME) ?: continue
val spiderInfo = readCompatFromUniFile(file) ?: continue
spiderInfo.startPage = 0
try {
spiderInfo.write(file)
} catch (e: IOException) {
Log.e(TAG, "Can't write SpiderInfo", e)
}
} }
return null
} }
}.executeOnExecutor(IoThreadPoolExecutor.getInstance()) }
}
private fun resetReadingProgress(gid: Long) {
val downloadDir = SpiderDen.getGalleryDownloadDir(gid) ?: return
val file = downloadDir.findFile(SpiderQueen.SPIDER_INFO_FILENAME) ?: return
val spiderInfo = readCompatFromUniFile(file) ?: return
spiderInfo.startPage = 0
spiderInfo.write(file)
} }
// Update in DB // Update in DB
@@ -948,7 +938,7 @@ class DownloadManager : OnSpiderListener {
fun onCancel(info: DownloadInfo) fun onCancel(info: DownloadInfo)
} }
private inner class NotifyTask : Runnable { private class NotifyTask : Runnable {
private var mType = 0 private var mType = 0
private var mPages = 0 private var mPages = 0
private var mIndex = 0 private var mIndex = 0
@@ -960,12 +950,12 @@ class DownloadManager : OnSpiderListener {
private var mDownloaded = 0 private var mDownloaded = 0
private var mTotal = 0 private var mTotal = 0
fun setOnGetPagesData(pages: Int) { fun setOnGetPagesData(pages: Int) {
mType = Companion.TYPE_ON_GET_PAGES mType = TYPE_ON_GET_PAGES
mPages = pages mPages = pages
} }
fun setOnGet509Data(index: Int) { fun setOnGet509Data(index: Int) {
mType = Companion.TYPE_ON_GET_509 mType = TYPE_ON_GET_509
mIndex = index mIndex = index
} }
@@ -975,7 +965,7 @@ class DownloadManager : OnSpiderListener {
receivedSize: Long, receivedSize: Long,
bytesRead: Int bytesRead: Int
) { ) {
mType = Companion.TYPE_ON_PAGE_DOWNLOAD mType = TYPE_ON_PAGE_DOWNLOAD
mIndex = index mIndex = index
mContentLength = contentLength mContentLength = contentLength
mReceivedSize = receivedSize mReceivedSize = receivedSize
@@ -983,7 +973,7 @@ class DownloadManager : OnSpiderListener {
} }
fun setOnPageSuccessData(index: Int, finished: Int, downloaded: Int, total: Int) { fun setOnPageSuccessData(index: Int, finished: Int, downloaded: Int, total: Int) {
mType = Companion.TYPE_ON_PAGE_SUCCESS mType = TYPE_ON_PAGE_SUCCESS
mIndex = index mIndex = index
mFinished = finished mFinished = finished
mDownloaded = downloaded mDownloaded = downloaded
@@ -997,7 +987,7 @@ class DownloadManager : OnSpiderListener {
downloaded: Int, downloaded: Int,
total: Int total: Int
) { ) {
mType = Companion.TYPE_ON_PAGE_FAILURE mType = TYPE_ON_PAGE_FAILURE
mIndex = index mIndex = index
mError = error mError = error
mFinished = finished mFinished = finished
@@ -1006,7 +996,7 @@ class DownloadManager : OnSpiderListener {
} }
fun setOnFinishDate(finished: Int, downloaded: Int, total: Int) { fun setOnFinishDate(finished: Int, downloaded: Int, total: Int) {
mType = Companion.TYPE_ON_FINISH mType = TYPE_ON_FINISH
mFinished = finished mFinished = finished
mDownloaded = downloaded mDownloaded = downloaded
mTotal = total mTotal = total
@@ -1014,7 +1004,7 @@ class DownloadManager : OnSpiderListener {
override fun run() { override fun run() {
when (mType) { when (mType) {
Companion.TYPE_ON_GET_PAGES -> { TYPE_ON_GET_PAGES -> {
val info = mCurrentTask val info = mCurrentTask
if (info == null) { if (info == null) {
Log.e(TAG, "Current task is null, but it should not be") Log.e(TAG, "Current task is null, but it should not be")
@@ -1029,20 +1019,20 @@ class DownloadManager : OnSpiderListener {
} }
} }
Companion.TYPE_ON_GET_509 -> { TYPE_ON_GET_509 -> {
if (mDownloadListener != null) { if (mDownloadListener != null) {
mDownloadListener!!.onGet509() mDownloadListener!!.onGet509()
} }
} }
Companion.TYPE_ON_PAGE_DOWNLOAD -> mSpeedReminder.onDownload( TYPE_ON_PAGE_DOWNLOAD -> mSpeedReminder.onDownload(
mIndex, mIndex,
mContentLength, mContentLength,
mReceivedSize, mReceivedSize,
mBytesRead mBytesRead
) )
Companion.TYPE_ON_PAGE_SUCCESS -> { TYPE_ON_PAGE_SUCCESS -> {
mSpeedReminder.onDone(mIndex) mSpeedReminder.onDone(mIndex)
val info = mCurrentTask val info = mCurrentTask
if (info == null) { if (info == null) {
@@ -1063,7 +1053,7 @@ class DownloadManager : OnSpiderListener {
} }
} }
Companion.TYPE_ON_PAGE_FAILURE -> { TYPE_ON_PAGE_FAILURE -> {
mSpeedReminder.onDone(mIndex) mSpeedReminder.onDone(mIndex)
val info = mCurrentTask val info = mCurrentTask
if (info == null) { if (info == null) {
@@ -1081,7 +1071,7 @@ class DownloadManager : OnSpiderListener {
} }
} }
Companion.TYPE_ON_FINISH -> { TYPE_ON_FINISH -> {
mSpeedReminder.onFinish() mSpeedReminder.onFinish()
// Download done // Download done
val info = mCurrentTask val info = mCurrentTask
@@ -1090,7 +1080,7 @@ class DownloadManager : OnSpiderListener {
mCurrentSpider = null mCurrentSpider = null
// Release spider // Release spider
if (spider != null) { if (spider != null) {
spider.removeOnSpiderListener(this@DownloadManager) spider.removeOnSpiderListener(downloadManager)
SpiderQueen.releaseSpiderQueen(spider, SpiderQueen.MODE_DOWNLOAD) SpiderQueen.releaseSpiderQueen(spider, SpiderQueen.MODE_DOWNLOAD)
} }
// Check null // Check null
@@ -1130,7 +1120,7 @@ class DownloadManager : OnSpiderListener {
} }
} }
internal inner class SpeedReminder : Runnable { internal class SpeedReminder : Runnable {
private val mContentLengthMap = SparseLongArray() private val mContentLengthMap = SparseLongArray()
private val mReceivedSizeMap = SparseLongArray() private val mReceivedSizeMap = SparseLongArray()
private var mStop = true private var mStop = true
@@ -1219,17 +1209,15 @@ class DownloadManager : OnSpiderListener {
} }
} }
companion object { private val TAG = DownloadManager::class.java.simpleName
private val TAG = DownloadManager::class.java.simpleName private const val TYPE_ON_GET_PAGES = 0
private const val TYPE_ON_GET_PAGES = 0 private const val TYPE_ON_GET_509 = 1
private const val TYPE_ON_GET_509 = 1 private const val TYPE_ON_PAGE_DOWNLOAD = 2
private const val TYPE_ON_PAGE_DOWNLOAD = 2 private const val TYPE_ON_PAGE_SUCCESS = 3
private const val TYPE_ON_PAGE_SUCCESS = 3 private const val TYPE_ON_PAGE_FAILURE = 4
private const val TYPE_ON_PAGE_FAILURE = 4 private const val TYPE_ON_FINISH = 5
private const val TYPE_ON_FINISH = 5
fun MutableList<DownloadInfo>.sortByDateDescending() { private fun MutableList<DownloadInfo>.sortByDateDescending() {
sortByDescending { it.time } sortByDescending { it.time }
}
} }
} }
@@ -77,6 +77,7 @@ import com.hippo.yorozuya.ViewUtils
import com.hippo.yorozuya.collect.LongList import com.hippo.yorozuya.collect.LongList
import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchNonCancellable
import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.lang.withUIContext
import rikka.core.res.resolveColor import rikka.core.res.resolveColor
@@ -377,7 +378,9 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int -> .setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int ->
if (mDownloadManager != null) { if (mDownloadManager != null) {
mDownloadManager!!.resetAllReadingProgress() lifecycleScope.launchNonCancellable {
mDownloadManager!!.resetAllReadingProgress()
}
} }
}.show() }.show()
return true return true