Merge branch 'devel' into feature/nextgen
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package org.koitharu.kotatsu.download.domain
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.webkit.MimeTypeMap
|
||||
import coil.ImageLoader
|
||||
import coil.request.ImageRequest
|
||||
@@ -20,6 +19,7 @@ import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.network.CommonHeaders
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.download.ui.service.PausingHandle
|
||||
import org.koitharu.kotatsu.local.data.PagesCache
|
||||
import org.koitharu.kotatsu.local.domain.CbzMangaOutput
|
||||
import org.koitharu.kotatsu.local.domain.LocalMangaRepository
|
||||
@@ -29,10 +29,9 @@ import org.koitharu.kotatsu.parsers.util.await
|
||||
import org.koitharu.kotatsu.utils.ext.deleteAwait
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.referer
|
||||
import org.koitharu.kotatsu.utils.ext.waitForNetwork
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressJob
|
||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
||||
|
||||
private const val MAX_DOWNLOAD_ATTEMPTS = 3
|
||||
private const val MAX_FAILSAFE_ATTEMPTS = 2
|
||||
private const val DOWNLOAD_ERROR_DELAY = 500L
|
||||
private const val SLOWDOWN_DELAY = 200L
|
||||
|
||||
@@ -47,9 +46,6 @@ class DownloadManager @AssistedInject constructor(
|
||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||
) {
|
||||
|
||||
private val connectivityManager = context.getSystemService(
|
||||
Context.CONNECTIVITY_SERVICE,
|
||||
) as ConnectivityManager
|
||||
private val coverWidth = context.resources.getDimensionPixelSize(
|
||||
androidx.core.R.dimen.compat_notification_large_icon_max_width,
|
||||
)
|
||||
@@ -62,18 +58,20 @@ class DownloadManager @AssistedInject constructor(
|
||||
manga: Manga,
|
||||
chaptersIds: LongArray?,
|
||||
startId: Int,
|
||||
): ProgressJob<DownloadState> {
|
||||
): PausingProgressJob<DownloadState> {
|
||||
val stateFlow = MutableStateFlow<DownloadState>(
|
||||
DownloadState.Queued(startId = startId, manga = manga, cover = null),
|
||||
)
|
||||
val job = downloadMangaImpl(manga, chaptersIds?.takeUnless { it.isEmpty() }, stateFlow, startId)
|
||||
return ProgressJob(job, stateFlow)
|
||||
val pausingHandle = PausingHandle()
|
||||
val job = downloadMangaImpl(manga, chaptersIds?.takeUnless { it.isEmpty() }, stateFlow, pausingHandle, startId)
|
||||
return PausingProgressJob(job, stateFlow, pausingHandle)
|
||||
}
|
||||
|
||||
private fun downloadMangaImpl(
|
||||
manga: Manga,
|
||||
chaptersIds: LongArray?,
|
||||
outState: MutableStateFlow<DownloadState>,
|
||||
pausingHandle: PausingHandle,
|
||||
startId: Int,
|
||||
): Job = coroutineScope.launch(Dispatchers.Default + errorStateHandler(outState)) {
|
||||
@Suppress("NAME_SHADOWING")
|
||||
@@ -113,36 +111,24 @@ class DownloadManager @AssistedInject constructor(
|
||||
"${chaptersIdsSet?.size} of ${chaptersIds?.size} requested chapters not found in manga"
|
||||
}
|
||||
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
||||
val pages = repo.getPages(chapter)
|
||||
val pages = runFailsafe(outState, pausingHandle) {
|
||||
repo.getPages(chapter)
|
||||
}
|
||||
for ((pageIndex, page) in pages.withIndex()) {
|
||||
var retryCounter = 0
|
||||
failsafe@ while (true) {
|
||||
try {
|
||||
val url = repo.getPageUrl(page)
|
||||
val file = cache[url] ?: downloadFile(url, page.referer, destination, tempFileName)
|
||||
output.addPage(
|
||||
chapter = chapter,
|
||||
file = file,
|
||||
pageNumber = pageIndex,
|
||||
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
||||
)
|
||||
break@failsafe
|
||||
} catch (e: IOException) {
|
||||
if (retryCounter < MAX_DOWNLOAD_ATTEMPTS) {
|
||||
outState.value = DownloadState.WaitingForNetwork(startId, data, cover)
|
||||
delay(DOWNLOAD_ERROR_DELAY)
|
||||
connectivityManager.waitForNetwork()
|
||||
retryCounter++
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
runFailsafe(outState, pausingHandle) {
|
||||
val url = repo.getPageUrl(page)
|
||||
val file = cache[url] ?: downloadFile(url, page.referer, destination, tempFileName)
|
||||
output.addPage(
|
||||
chapter = chapter,
|
||||
file = file,
|
||||
pageNumber = pageIndex,
|
||||
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
||||
)
|
||||
}
|
||||
|
||||
outState.value = DownloadState.Progress(
|
||||
startId,
|
||||
data,
|
||||
cover,
|
||||
startId = startId,
|
||||
manga = data,
|
||||
cover = cover,
|
||||
totalChapters = chapters.size,
|
||||
currentChapter = chapterIndex,
|
||||
totalPages = pages.size,
|
||||
@@ -164,15 +150,40 @@ class DownloadManager @AssistedInject constructor(
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
e.printStackTraceDebug()
|
||||
outState.value = DownloadState.Error(startId, manga, cover, e)
|
||||
outState.value = DownloadState.Error(startId, manga, cover, e, false)
|
||||
} finally {
|
||||
withContext(NonCancellable) {
|
||||
output?.cleanup()
|
||||
File(destination, tempFileName).deleteAwait()
|
||||
coroutineContext[WakeLockNode]?.release()
|
||||
semaphore.release()
|
||||
localMangaRepository.unlockManga(manga.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun <R> runFailsafe(
|
||||
outState: MutableStateFlow<DownloadState>,
|
||||
pausingHandle: PausingHandle,
|
||||
block: suspend () -> R,
|
||||
): R {
|
||||
var countDown = MAX_FAILSAFE_ATTEMPTS
|
||||
failsafe@ while (true) {
|
||||
try {
|
||||
return block()
|
||||
} catch (e: IOException) {
|
||||
if (countDown <= 0) {
|
||||
val state = outState.value
|
||||
outState.value = DownloadState.Error(state.startId, state.manga, state.cover, e, true)
|
||||
countDown = MAX_FAILSAFE_ATTEMPTS
|
||||
pausingHandle.pause()
|
||||
pausingHandle.awaitResumed()
|
||||
outState.value = state
|
||||
} else {
|
||||
countDown--
|
||||
delay(DOWNLOAD_ERROR_DELAY)
|
||||
}
|
||||
}
|
||||
coroutineContext[WakeLockNode]?.release()
|
||||
semaphore.release()
|
||||
localMangaRepository.unlockManga(manga.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,6 +213,7 @@ class DownloadManager @AssistedInject constructor(
|
||||
manga = prevValue.manga,
|
||||
cover = prevValue.cover,
|
||||
error = throwable,
|
||||
canRetry = false,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +108,7 @@ sealed interface DownloadState {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated("TODO: remove")
|
||||
class WaitingForNetwork(
|
||||
override val startId: Int,
|
||||
override val manga: Manga,
|
||||
@@ -170,6 +171,7 @@ sealed interface DownloadState {
|
||||
override val manga: Manga,
|
||||
override val cover: Drawable?,
|
||||
val error: Throwable,
|
||||
val canRetry: Boolean,
|
||||
) : DownloadState {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
@@ -182,6 +184,7 @@ sealed interface DownloadState {
|
||||
if (manga != other.manga) return false
|
||||
if (cover != other.cover) return false
|
||||
if (error != other.error) return false
|
||||
if (canRetry != other.canRetry) return false
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -191,6 +194,7 @@ sealed interface DownloadState {
|
||||
result = 31 * result + manga.hashCode()
|
||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
||||
result = 31 * result + error.hashCode()
|
||||
result = 31 * result + canRetry.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,16 +29,26 @@ class DownloadNotification(private val context: Context, startId: Int) {
|
||||
context.getString(android.R.string.cancel),
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
startId,
|
||||
startId * 2,
|
||||
DownloadService.getCancelIntent(startId),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE,
|
||||
),
|
||||
)
|
||||
private val retryAction = NotificationCompat.Action(
|
||||
R.drawable.ic_restart_black,
|
||||
context.getString(R.string.try_again),
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
startId * 2 + 1,
|
||||
DownloadService.getResumeIntent(startId),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
private val listIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
REQUEST_LIST,
|
||||
DownloadsActivity.newIntent(context),
|
||||
PendingIntentCompat.FLAG_IMMUTABLE,
|
||||
PendingIntentCompat.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
init {
|
||||
@@ -89,10 +99,14 @@ class DownloadNotification(private val context: Context, startId: Int) {
|
||||
builder.setSmallIcon(android.R.drawable.stat_notify_error)
|
||||
builder.setSubText(context.getString(R.string.error))
|
||||
builder.setContentText(message)
|
||||
builder.setAutoCancel(true)
|
||||
builder.setOngoing(false)
|
||||
builder.setAutoCancel(!state.canRetry)
|
||||
builder.setOngoing(state.canRetry)
|
||||
builder.setCategory(NotificationCompat.CATEGORY_ERROR)
|
||||
builder.setStyle(NotificationCompat.BigTextStyle().bigText(message))
|
||||
if (state.canRetry) {
|
||||
builder.addAction(cancelAction)
|
||||
builder.addAction(retryAction)
|
||||
}
|
||||
}
|
||||
is DownloadState.PostProcessing -> {
|
||||
builder.setProgress(1, 0, true)
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.koitharu.kotatsu.download.domain.DownloadState
|
||||
import org.koitharu.kotatsu.download.domain.WakeLockNode
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.utils.ext.throttle
|
||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressJob
|
||||
import org.koitharu.kotatsu.utils.progress.TimeLeftEstimator
|
||||
|
||||
@@ -39,7 +40,7 @@ class DownloadService : BaseService() {
|
||||
@Inject
|
||||
lateinit var downloadManagerFactory: DownloadManager.Factory
|
||||
|
||||
private val jobs = LinkedHashMap<Int, ProgressJob<DownloadState>>()
|
||||
private val jobs = LinkedHashMap<Int, PausingProgressJob<DownloadState>>()
|
||||
private val jobCount = MutableStateFlow(0)
|
||||
private val controlReceiver = ControlReceiver()
|
||||
private var binder: DownloadBinder? = null
|
||||
@@ -54,7 +55,10 @@ class DownloadService : BaseService() {
|
||||
coroutineScope = lifecycleScope + WakeLockNode(wakeLock, TimeUnit.HOURS.toMillis(1)),
|
||||
)
|
||||
DownloadNotification.createChannel(this)
|
||||
registerReceiver(controlReceiver, IntentFilter(ACTION_DOWNLOAD_CANCEL))
|
||||
val intentFilter = IntentFilter()
|
||||
intentFilter.addAction(ACTION_DOWNLOAD_CANCEL)
|
||||
intentFilter.addAction(ACTION_DOWNLOAD_RESUME)
|
||||
registerReceiver(controlReceiver, intentFilter)
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
@@ -92,7 +96,7 @@ class DownloadService : BaseService() {
|
||||
startId: Int,
|
||||
manga: Manga,
|
||||
chaptersIds: LongArray?,
|
||||
): ProgressJob<DownloadState> {
|
||||
): PausingProgressJob<DownloadState> {
|
||||
val job = downloadManager.downloadManga(manga, chaptersIds, startId)
|
||||
listenJob(job)
|
||||
return job
|
||||
@@ -146,7 +150,7 @@ class DownloadService : BaseService() {
|
||||
}
|
||||
|
||||
private val DownloadState.isTerminal: Boolean
|
||||
get() = this is DownloadState.Done || this is DownloadState.Error || this is DownloadState.Cancelled
|
||||
get() = this is DownloadState.Done || this is DownloadState.Cancelled || (this is DownloadState.Error && !canRetry)
|
||||
|
||||
inner class ControlReceiver : BroadcastReceiver() {
|
||||
|
||||
@@ -157,6 +161,10 @@ class DownloadService : BaseService() {
|
||||
jobs.remove(cancelId)?.cancel()
|
||||
jobCount.value = jobs.size
|
||||
}
|
||||
ACTION_DOWNLOAD_RESUME -> {
|
||||
val cancelId = intent.getIntExtra(EXTRA_CANCEL_ID, 0)
|
||||
jobs[cancelId]?.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,6 +183,7 @@ class DownloadService : BaseService() {
|
||||
const val ACTION_DOWNLOAD_COMPLETE = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_COMPLETE"
|
||||
|
||||
private const val ACTION_DOWNLOAD_CANCEL = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_CANCEL"
|
||||
private const val ACTION_DOWNLOAD_RESUME = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_RESUME"
|
||||
|
||||
private const val EXTRA_MANGA = "manga"
|
||||
private const val EXTRA_CHAPTERS_IDS = "chapters_ids"
|
||||
@@ -217,6 +226,9 @@ class DownloadService : BaseService() {
|
||||
fun getCancelIntent(startId: Int) = Intent(ACTION_DOWNLOAD_CANCEL)
|
||||
.putExtra(EXTRA_CANCEL_ID, startId)
|
||||
|
||||
fun getResumeIntent(startId: Int) = Intent(ACTION_DOWNLOAD_RESUME)
|
||||
.putExtra(EXTRA_CANCEL_ID, startId)
|
||||
|
||||
fun getDownloadedManga(intent: Intent?): Manga? {
|
||||
if (intent?.action == ACTION_DOWNLOAD_COMPLETE) {
|
||||
return intent.getParcelableExtra<ParcelableManga>(EXTRA_MANGA)?.manga
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.koitharu.kotatsu.download.ui.service
|
||||
|
||||
import androidx.annotation.AnyThread
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
class PausingHandle {
|
||||
|
||||
private val paused = MutableStateFlow(false)
|
||||
|
||||
@get:AnyThread
|
||||
val isPaused: Boolean
|
||||
get() = paused.value
|
||||
|
||||
@AnyThread
|
||||
suspend fun awaitResumed() {
|
||||
paused.filter { !it }.first()
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
fun pause() {
|
||||
paused.value = true
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
fun resume() {
|
||||
paused.value = false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user