Merge branch 'feature/downloads_worker' into devel
commit
2d171657dc
@ -1,281 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.domain
|
|
||||||
|
|
||||||
import android.app.Service
|
|
||||||
import android.content.Context
|
|
||||||
import android.webkit.MimeTypeMap
|
|
||||||
import androidx.lifecycle.LifecycleService
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import coil.ImageLoader
|
|
||||||
import coil.request.ImageRequest
|
|
||||||
import coil.size.Scale
|
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
||||||
import dagger.hilt.android.scopes.ServiceScoped
|
|
||||||
import kotlinx.coroutines.CancellationException
|
|
||||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.NonCancellable
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.sync.Semaphore
|
|
||||||
import kotlinx.coroutines.sync.withPermit
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import okhttp3.OkHttpClient
|
|
||||||
import okhttp3.Request
|
|
||||||
import okhttp3.internal.closeQuietly
|
|
||||||
import okio.IOException
|
|
||||||
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.LocalManga
|
|
||||||
import org.koitharu.kotatsu.local.data.LocalStorageChanges
|
|
||||||
import org.koitharu.kotatsu.local.data.PagesCache
|
|
||||||
import org.koitharu.kotatsu.local.data.input.LocalMangaInput
|
|
||||||
import org.koitharu.kotatsu.local.data.output.LocalMangaOutput
|
|
||||||
import org.koitharu.kotatsu.local.domain.LocalMangaRepository
|
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
||||||
import org.koitharu.kotatsu.parsers.util.await
|
|
||||||
import org.koitharu.kotatsu.utils.ext.copyToSuspending
|
|
||||||
import org.koitharu.kotatsu.utils.ext.deleteAwait
|
|
||||||
import org.koitharu.kotatsu.utils.ext.ifNullOrEmpty
|
|
||||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
|
||||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
|
||||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
|
||||||
import java.io.File
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
private const val MAX_FAILSAFE_ATTEMPTS = 2
|
|
||||||
private const val DOWNLOAD_ERROR_DELAY = 500L
|
|
||||||
private const val SLOWDOWN_DELAY = 150L
|
|
||||||
|
|
||||||
@ServiceScoped
|
|
||||||
class DownloadManager @Inject constructor(
|
|
||||||
service: Service,
|
|
||||||
@ApplicationContext private val context: Context,
|
|
||||||
private val imageLoader: ImageLoader,
|
|
||||||
private val okHttp: OkHttpClient,
|
|
||||||
private val cache: PagesCache,
|
|
||||||
private val localMangaRepository: LocalMangaRepository,
|
|
||||||
private val settings: AppSettings,
|
|
||||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
|
||||||
@LocalStorageChanges private val localStorageChanges: MutableSharedFlow<LocalManga?>,
|
|
||||||
) {
|
|
||||||
|
|
||||||
private val coverWidth = context.resources.getDimensionPixelSize(
|
|
||||||
androidx.core.R.dimen.compat_notification_large_icon_max_width,
|
|
||||||
)
|
|
||||||
private val coverHeight = context.resources.getDimensionPixelSize(
|
|
||||||
androidx.core.R.dimen.compat_notification_large_icon_max_height,
|
|
||||||
)
|
|
||||||
private val semaphore = Semaphore(settings.downloadsParallelism)
|
|
||||||
private val coroutineScope = (service as LifecycleService).lifecycleScope
|
|
||||||
|
|
||||||
fun downloadManga(
|
|
||||||
manga: Manga,
|
|
||||||
chaptersIds: LongArray?,
|
|
||||||
startId: Int,
|
|
||||||
): PausingProgressJob<DownloadState> {
|
|
||||||
val stateFlow = MutableStateFlow<DownloadState>(
|
|
||||||
DownloadState.Queued(startId = startId, manga = manga, cover = null),
|
|
||||||
)
|
|
||||||
val pausingHandle = PausingHandle()
|
|
||||||
val job = coroutineScope.launch(Dispatchers.Default + errorStateHandler(stateFlow)) {
|
|
||||||
try {
|
|
||||||
downloadMangaImpl(manga, chaptersIds?.takeUnless { it.isEmpty() }, stateFlow, pausingHandle, startId)
|
|
||||||
} catch (e: CancellationException) { // handle cancellation if not handled already
|
|
||||||
val state = stateFlow.value
|
|
||||||
if (state !is DownloadState.Cancelled) {
|
|
||||||
stateFlow.value = DownloadState.Cancelled(startId, state.manga, state.cover)
|
|
||||||
}
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return PausingProgressJob(job, stateFlow, pausingHandle)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun downloadMangaImpl(
|
|
||||||
manga: Manga,
|
|
||||||
chaptersIds: LongArray?,
|
|
||||||
outState: MutableStateFlow<DownloadState>,
|
|
||||||
pausingHandle: PausingHandle,
|
|
||||||
startId: Int,
|
|
||||||
) {
|
|
||||||
@Suppress("NAME_SHADOWING")
|
|
||||||
var manga = manga
|
|
||||||
val chaptersIdsSet = chaptersIds?.toMutableSet()
|
|
||||||
val cover = loadCover(manga)
|
|
||||||
outState.value = DownloadState.Queued(startId, manga, cover)
|
|
||||||
withMangaLock(manga) {
|
|
||||||
semaphore.withPermit {
|
|
||||||
outState.value = DownloadState.Preparing(startId, manga, null)
|
|
||||||
val destination = localMangaRepository.getOutputDir(manga)
|
|
||||||
checkNotNull(destination) { context.getString(R.string.cannot_find_available_storage) }
|
|
||||||
val tempFileName = "${manga.id}_$startId.tmp"
|
|
||||||
var output: LocalMangaOutput? = null
|
|
||||||
try {
|
|
||||||
if (manga.source == MangaSource.LOCAL) {
|
|
||||||
manga = localMangaRepository.getRemoteManga(manga)
|
|
||||||
?: error("Cannot obtain remote manga instance")
|
|
||||||
}
|
|
||||||
val repo = mangaRepositoryFactory.create(manga.source)
|
|
||||||
outState.value = DownloadState.Preparing(startId, manga, cover)
|
|
||||||
val data = if (manga.chapters.isNullOrEmpty()) repo.getDetails(manga) else manga
|
|
||||||
output = LocalMangaOutput.getOrCreate(destination, data)
|
|
||||||
val coverUrl = data.largeCoverUrl.ifNullOrEmpty { data.coverUrl }
|
|
||||||
if (coverUrl.isNotEmpty()) {
|
|
||||||
downloadFile(coverUrl, destination, tempFileName, repo.source).let { file ->
|
|
||||||
output.addCover(file, MimeTypeMap.getFileExtensionFromUrl(coverUrl))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val chapters = checkNotNull(
|
|
||||||
if (chaptersIdsSet == null) {
|
|
||||||
data.chapters
|
|
||||||
} else {
|
|
||||||
data.chapters?.filter { x -> chaptersIdsSet.remove(x.id) }
|
|
||||||
},
|
|
||||||
) { "Chapters list must not be null" }
|
|
||||||
check(chapters.isNotEmpty()) { "Chapters list must not be empty" }
|
|
||||||
check(chaptersIdsSet.isNullOrEmpty()) {
|
|
||||||
"${chaptersIdsSet?.size} of ${chaptersIds?.size} requested chapters not found in manga"
|
|
||||||
}
|
|
||||||
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
|
||||||
val pages = runFailsafe(outState, pausingHandle) {
|
|
||||||
repo.getPages(chapter)
|
|
||||||
}
|
|
||||||
for ((pageIndex, page) in pages.withIndex()) {
|
|
||||||
runFailsafe(outState, pausingHandle) {
|
|
||||||
val url = repo.getPageUrl(page)
|
|
||||||
val file = cache.get(url)
|
|
||||||
?: downloadFile(url, destination, tempFileName, repo.source)
|
|
||||||
output.addPage(
|
|
||||||
chapter = chapter,
|
|
||||||
file = file,
|
|
||||||
pageNumber = pageIndex,
|
|
||||||
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
outState.value = DownloadState.Progress(
|
|
||||||
startId = startId,
|
|
||||||
manga = data,
|
|
||||||
cover = cover,
|
|
||||||
totalChapters = chapters.size,
|
|
||||||
currentChapter = chapterIndex,
|
|
||||||
totalPages = pages.size,
|
|
||||||
currentPage = pageIndex,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (settings.isDownloadsSlowdownEnabled) {
|
|
||||||
delay(SLOWDOWN_DELAY)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (output.flushChapter(chapter)) {
|
|
||||||
runCatchingCancellable {
|
|
||||||
localStorageChanges.emit(LocalMangaInput.of(output.rootFile).getManga())
|
|
||||||
}.onFailure(Throwable::printStackTraceDebug)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
outState.value = DownloadState.PostProcessing(startId, data, cover)
|
|
||||||
output.mergeWithExisting()
|
|
||||||
output.finish()
|
|
||||||
val localManga = LocalMangaInput.of(output.rootFile).getManga()
|
|
||||||
localStorageChanges.emit(localManga)
|
|
||||||
outState.value = DownloadState.Done(startId, data, cover, localManga.manga)
|
|
||||||
} catch (e: CancellationException) {
|
|
||||||
outState.value = DownloadState.Cancelled(startId, manga, cover)
|
|
||||||
throw e
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
e.printStackTraceDebug()
|
|
||||||
outState.value = DownloadState.Error(startId, manga, cover, e, false)
|
|
||||||
} finally {
|
|
||||||
withContext(NonCancellable) {
|
|
||||||
output?.closeQuietly()
|
|
||||||
output?.cleanup()
|
|
||||||
File(destination, tempFileName).deleteAwait()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun downloadFile(
|
|
||||||
url: String,
|
|
||||||
destination: File,
|
|
||||||
tempFileName: String,
|
|
||||||
source: MangaSource,
|
|
||||||
): File {
|
|
||||||
val request = Request.Builder()
|
|
||||||
.url(url)
|
|
||||||
.tag(MangaSource::class.java, source)
|
|
||||||
.cacheControl(CommonHeaders.CACHE_CONTROL_NO_STORE)
|
|
||||||
.get()
|
|
||||||
.build()
|
|
||||||
val call = okHttp.newCall(request)
|
|
||||||
val file = File(destination, tempFileName)
|
|
||||||
val response = call.clone().await()
|
|
||||||
file.outputStream().use { out ->
|
|
||||||
checkNotNull(response.body).byteStream().copyToSuspending(out)
|
|
||||||
}
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun errorStateHandler(outState: MutableStateFlow<DownloadState>) =
|
|
||||||
CoroutineExceptionHandler { _, throwable ->
|
|
||||||
throwable.printStackTraceDebug()
|
|
||||||
val prevValue = outState.value
|
|
||||||
outState.value = DownloadState.Error(
|
|
||||||
startId = prevValue.startId,
|
|
||||||
manga = prevValue.manga,
|
|
||||||
cover = prevValue.cover,
|
|
||||||
error = throwable,
|
|
||||||
canRetry = false,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun loadCover(manga: Manga) = runCatchingCancellable {
|
|
||||||
imageLoader.execute(
|
|
||||||
ImageRequest.Builder(context)
|
|
||||||
.data(manga.coverUrl)
|
|
||||||
.allowHardware(false)
|
|
||||||
.tag(manga.source)
|
|
||||||
.size(coverWidth, coverHeight)
|
|
||||||
.scale(Scale.FILL)
|
|
||||||
.build(),
|
|
||||||
).drawable
|
|
||||||
}.getOrNull()
|
|
||||||
|
|
||||||
private suspend inline fun <T> withMangaLock(manga: Manga, block: () -> T) = try {
|
|
||||||
localMangaRepository.lockManga(manga.id)
|
|
||||||
block()
|
|
||||||
} finally {
|
|
||||||
localMangaRepository.unlockManga(manga.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,234 +1,123 @@
|
|||||||
package org.koitharu.kotatsu.download.domain
|
package org.koitharu.kotatsu.download.domain
|
||||||
|
|
||||||
import android.graphics.drawable.Drawable
|
import androidx.work.Data
|
||||||
|
import org.koitharu.kotatsu.history.domain.PROGRESS_NONE
|
||||||
|
import org.koitharu.kotatsu.local.data.LocalManga
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import java.util.Date
|
||||||
sealed interface DownloadState {
|
|
||||||
|
data class DownloadState(
|
||||||
val startId: Int
|
val manga: Manga,
|
||||||
val manga: Manga
|
val isIndeterminate: Boolean,
|
||||||
val cover: Drawable?
|
val isPaused: Boolean = false,
|
||||||
|
val isStopped: Boolean = false,
|
||||||
override fun equals(other: Any?): Boolean
|
val error: String? = null,
|
||||||
|
val totalChapters: Int = 0,
|
||||||
override fun hashCode(): Int
|
val currentChapter: Int = 0,
|
||||||
|
val totalPages: Int = 0,
|
||||||
val isTerminal: Boolean
|
val currentPage: Int = 0,
|
||||||
get() = this is Done || this is Cancelled || (this is Error && !canRetry)
|
val eta: Long = -1L,
|
||||||
|
val localManga: LocalManga? = null,
|
||||||
class Queued(
|
val downloadedChapters: LongArray = LongArray(0),
|
||||||
override val startId: Int,
|
val timestamp: Long = System.currentTimeMillis(),
|
||||||
override val manga: Manga,
|
) {
|
||||||
override val cover: Drawable?,
|
|
||||||
) : DownloadState {
|
val max: Int = totalChapters * totalPages
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
val progress: Int = totalPages * currentChapter + currentPage + 1
|
||||||
if (this === other) return true
|
|
||||||
if (javaClass != other?.javaClass) return false
|
val percent: Float = if (max > 0) progress.toFloat() / max else PROGRESS_NONE
|
||||||
|
|
||||||
other as Queued
|
val isFinalState: Boolean
|
||||||
|
get() = localManga != null || (error != null && !isPaused)
|
||||||
if (startId != other.startId) return false
|
|
||||||
if (manga != other.manga) return false
|
val isParticularProgress: Boolean
|
||||||
if (cover != other.cover) return false
|
get() = localManga == null && error == null && !isPaused && !isStopped && max > 0 && !isIndeterminate
|
||||||
|
|
||||||
return true
|
fun toWorkData() = Data.Builder()
|
||||||
}
|
.putLong(DATA_MANGA_ID, manga.id)
|
||||||
|
.putInt(DATA_MAX, max)
|
||||||
override fun hashCode(): Int {
|
.putInt(DATA_PROGRESS, progress)
|
||||||
var result = startId
|
.putLong(DATA_ETA, eta)
|
||||||
result = 31 * result + manga.hashCode()
|
.putLong(DATA_TIMESTAMP, timestamp)
|
||||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
.putString(DATA_ERROR, error)
|
||||||
return result
|
.putLongArray(DATA_CHAPTERS, downloadedChapters)
|
||||||
}
|
.putBoolean(DATA_INDETERMINATE, isIndeterminate)
|
||||||
}
|
.putBoolean(DATA_PAUSED, isPaused)
|
||||||
|
.build()
|
||||||
class Preparing(
|
|
||||||
override val startId: Int,
|
override fun equals(other: Any?): Boolean {
|
||||||
override val manga: Manga,
|
if (this === other) return true
|
||||||
override val cover: Drawable?,
|
if (javaClass != other?.javaClass) return false
|
||||||
) : DownloadState {
|
|
||||||
|
other as DownloadState
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
if (manga != other.manga) return false
|
||||||
if (javaClass != other?.javaClass) return false
|
if (isIndeterminate != other.isIndeterminate) return false
|
||||||
|
if (isPaused != other.isPaused) return false
|
||||||
other as Preparing
|
if (isStopped != other.isStopped) return false
|
||||||
|
if (error != other.error) return false
|
||||||
if (startId != other.startId) return false
|
if (totalChapters != other.totalChapters) return false
|
||||||
if (manga != other.manga) return false
|
if (currentChapter != other.currentChapter) return false
|
||||||
if (cover != other.cover) return false
|
if (totalPages != other.totalPages) return false
|
||||||
|
if (currentPage != other.currentPage) return false
|
||||||
return true
|
if (eta != other.eta) return false
|
||||||
}
|
if (localManga != other.localManga) return false
|
||||||
|
if (!downloadedChapters.contentEquals(other.downloadedChapters)) return false
|
||||||
override fun hashCode(): Int {
|
if (timestamp != other.timestamp) return false
|
||||||
var result = startId
|
if (max != other.max) return false
|
||||||
result = 31 * result + manga.hashCode()
|
if (progress != other.progress) return false
|
||||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
return percent == other.percent
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Progress(
|
override fun hashCode(): Int {
|
||||||
override val startId: Int,
|
var result = manga.hashCode()
|
||||||
override val manga: Manga,
|
result = 31 * result + isIndeterminate.hashCode()
|
||||||
override val cover: Drawable?,
|
result = 31 * result + isPaused.hashCode()
|
||||||
val totalChapters: Int,
|
result = 31 * result + isStopped.hashCode()
|
||||||
val currentChapter: Int,
|
result = 31 * result + (error?.hashCode() ?: 0)
|
||||||
val totalPages: Int,
|
result = 31 * result + totalChapters
|
||||||
val currentPage: Int,
|
result = 31 * result + currentChapter
|
||||||
) : DownloadState {
|
result = 31 * result + totalPages
|
||||||
|
result = 31 * result + currentPage
|
||||||
val max: Int = totalChapters * totalPages
|
result = 31 * result + eta.hashCode()
|
||||||
|
result = 31 * result + (localManga?.hashCode() ?: 0)
|
||||||
val progress: Int = totalPages * currentChapter + currentPage + 1
|
result = 31 * result + downloadedChapters.contentHashCode()
|
||||||
|
result = 31 * result + timestamp.hashCode()
|
||||||
val percent: Float = progress.toFloat() / max
|
result = 31 * result + max
|
||||||
|
result = 31 * result + progress
|
||||||
override fun equals(other: Any?): Boolean {
|
result = 31 * result + percent.hashCode()
|
||||||
if (this === other) return true
|
return result
|
||||||
if (javaClass != other?.javaClass) return false
|
|
||||||
|
|
||||||
other as Progress
|
|
||||||
|
|
||||||
if (startId != other.startId) return false
|
|
||||||
if (manga != other.manga) return false
|
|
||||||
if (cover != other.cover) return false
|
|
||||||
if (totalChapters != other.totalChapters) return false
|
|
||||||
if (currentChapter != other.currentChapter) return false
|
|
||||||
if (totalPages != other.totalPages) return false
|
|
||||||
if (currentPage != other.currentPage) return false
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
var result = startId
|
|
||||||
result = 31 * result + manga.hashCode()
|
|
||||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
|
||||||
result = 31 * result + totalChapters
|
|
||||||
result = 31 * result + currentChapter
|
|
||||||
result = 31 * result + totalPages
|
|
||||||
result = 31 * result + currentPage
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Done(
|
companion object {
|
||||||
override val startId: Int,
|
|
||||||
override val manga: Manga,
|
|
||||||
override val cover: Drawable?,
|
|
||||||
val localManga: Manga,
|
|
||||||
) : DownloadState {
|
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
|
||||||
if (javaClass != other?.javaClass) return false
|
|
||||||
|
|
||||||
other as Done
|
|
||||||
|
|
||||||
if (startId != other.startId) return false
|
|
||||||
if (manga != other.manga) return false
|
|
||||||
if (cover != other.cover) return false
|
|
||||||
if (localManga != other.localManga) return false
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
var result = startId
|
|
||||||
result = 31 * result + manga.hashCode()
|
|
||||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
|
||||||
result = 31 * result + localManga.hashCode()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Error(
|
|
||||||
override val startId: Int,
|
|
||||||
override val manga: Manga,
|
|
||||||
override val cover: Drawable?,
|
|
||||||
val error: Throwable,
|
|
||||||
val canRetry: Boolean,
|
|
||||||
) : DownloadState {
|
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
|
||||||
if (javaClass != other?.javaClass) return false
|
|
||||||
|
|
||||||
other as Error
|
|
||||||
|
|
||||||
if (startId != other.startId) return false
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
var result = startId
|
|
||||||
result = 31 * result + manga.hashCode()
|
|
||||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
|
||||||
result = 31 * result + error.hashCode()
|
|
||||||
result = 31 * result + canRetry.hashCode()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Cancelled(
|
private const val DATA_MANGA_ID = "manga_id"
|
||||||
override val startId: Int,
|
private const val DATA_MAX = "max"
|
||||||
override val manga: Manga,
|
private const val DATA_PROGRESS = "progress"
|
||||||
override val cover: Drawable?,
|
private const val DATA_CHAPTERS = "chapter"
|
||||||
) : DownloadState {
|
private const val DATA_ETA = "eta"
|
||||||
|
private const val DATA_TIMESTAMP = "timestamp"
|
||||||
|
private const val DATA_ERROR = "error"
|
||||||
|
private const val DATA_INDETERMINATE = "indeterminate"
|
||||||
|
private const val DATA_PAUSED = "paused"
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
fun getMangaId(data: Data): Long = data.getLong(DATA_MANGA_ID, 0L)
|
||||||
if (this === other) return true
|
|
||||||
if (javaClass != other?.javaClass) return false
|
|
||||||
|
|
||||||
other as Cancelled
|
fun isIndeterminate(data: Data): Boolean = data.getBoolean(DATA_INDETERMINATE, false)
|
||||||
|
|
||||||
if (startId != other.startId) return false
|
fun isPaused(data: Data): Boolean = data.getBoolean(DATA_PAUSED, false)
|
||||||
if (manga != other.manga) return false
|
|
||||||
if (cover != other.cover) return false
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
var result = startId
|
|
||||||
result = 31 * result + manga.hashCode()
|
|
||||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class PostProcessing(
|
fun getMax(data: Data): Int = data.getInt(DATA_MAX, 0)
|
||||||
override val startId: Int,
|
|
||||||
override val manga: Manga,
|
|
||||||
override val cover: Drawable?,
|
|
||||||
) : DownloadState {
|
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
fun getError(data: Data): String? = data.getString(DATA_ERROR)
|
||||||
if (this === other) return true
|
|
||||||
if (javaClass != other?.javaClass) return false
|
|
||||||
|
|
||||||
other as PostProcessing
|
fun getProgress(data: Data): Int = data.getInt(DATA_PROGRESS, 0)
|
||||||
|
|
||||||
if (startId != other.startId) return false
|
fun getEta(data: Data): Long = data.getLong(DATA_ETA, -1L)
|
||||||
if (manga != other.manga) return false
|
|
||||||
if (cover != other.cover) return false
|
|
||||||
|
|
||||||
return true
|
fun getTimestamp(data: Data): Date = Date(data.getLong(DATA_TIMESTAMP, 0L))
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
fun getDownloadedChapters(data: Data): LongArray = data.getLongArray(DATA_CHAPTERS) ?: LongArray(0)
|
||||||
var result = startId
|
|
||||||
result = 31 * result + manga.hashCode()
|
|
||||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,140 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui
|
|
||||||
|
|
||||||
import android.view.View
|
|
||||||
import androidx.core.view.isVisible
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import coil.ImageLoader
|
|
||||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.databinding.ItemDownloadBinding
|
|
||||||
import org.koitharu.kotatsu.details.ui.DetailsActivity
|
|
||||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
|
||||||
import org.koitharu.kotatsu.parsers.util.format
|
|
||||||
import org.koitharu.kotatsu.utils.ext.enqueueWith
|
|
||||||
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
|
||||||
import org.koitharu.kotatsu.utils.ext.newImageRequest
|
|
||||||
import org.koitharu.kotatsu.utils.ext.onFirst
|
|
||||||
import org.koitharu.kotatsu.utils.ext.source
|
|
||||||
|
|
||||||
fun downloadItemAD(
|
|
||||||
lifecycleOwner: LifecycleOwner,
|
|
||||||
coil: ImageLoader,
|
|
||||||
) = adapterDelegateViewBinding<DownloadItem, DownloadItem, ItemDownloadBinding>(
|
|
||||||
{ inflater, parent -> ItemDownloadBinding.inflate(inflater, parent, false) },
|
|
||||||
) {
|
|
||||||
var job: Job? = null
|
|
||||||
val percentPattern = context.resources.getString(R.string.percent_string_pattern)
|
|
||||||
|
|
||||||
val clickListener = View.OnClickListener { v ->
|
|
||||||
when (v.id) {
|
|
||||||
R.id.button_cancel -> item.cancel()
|
|
||||||
R.id.button_resume -> item.resume()
|
|
||||||
else -> context.startActivity(
|
|
||||||
DetailsActivity.newIntent(context, item.progressValue.manga),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
binding.buttonCancel.setOnClickListener(clickListener)
|
|
||||||
binding.buttonResume.setOnClickListener(clickListener)
|
|
||||||
itemView.setOnClickListener(clickListener)
|
|
||||||
|
|
||||||
bind {
|
|
||||||
job?.cancel()
|
|
||||||
job = item.progressAsFlow().onFirst { state ->
|
|
||||||
binding.imageViewCover.newImageRequest(lifecycleOwner, state.manga.coverUrl)?.run {
|
|
||||||
placeholder(state.cover)
|
|
||||||
fallback(R.drawable.ic_placeholder)
|
|
||||||
error(R.drawable.ic_error_placeholder)
|
|
||||||
source(state.manga.source)
|
|
||||||
allowRgb565(true)
|
|
||||||
enqueueWith(coil)
|
|
||||||
}
|
|
||||||
}.onEach { state ->
|
|
||||||
binding.textViewTitle.text = state.manga.title
|
|
||||||
when (state) {
|
|
||||||
is DownloadState.Cancelled -> {
|
|
||||||
binding.textViewStatus.setText(R.string.cancelling_)
|
|
||||||
binding.progressBar.isIndeterminate = true
|
|
||||||
binding.progressBar.isVisible = true
|
|
||||||
binding.textViewPercent.isVisible = false
|
|
||||||
binding.textViewDetails.isVisible = false
|
|
||||||
binding.buttonCancel.isVisible = false
|
|
||||||
binding.buttonResume.isVisible = false
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Done -> {
|
|
||||||
binding.textViewStatus.setText(R.string.download_complete)
|
|
||||||
binding.progressBar.isIndeterminate = false
|
|
||||||
binding.progressBar.isVisible = false
|
|
||||||
binding.textViewPercent.isVisible = false
|
|
||||||
binding.textViewDetails.isVisible = false
|
|
||||||
binding.buttonCancel.isVisible = false
|
|
||||||
binding.buttonResume.isVisible = false
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Error -> {
|
|
||||||
binding.textViewStatus.setText(R.string.error_occurred)
|
|
||||||
binding.progressBar.isIndeterminate = false
|
|
||||||
binding.progressBar.isVisible = false
|
|
||||||
binding.textViewPercent.isVisible = false
|
|
||||||
binding.textViewDetails.text = state.error.getDisplayMessage(context.resources)
|
|
||||||
binding.textViewDetails.isVisible = true
|
|
||||||
binding.buttonCancel.isVisible = state.canRetry
|
|
||||||
binding.buttonResume.isVisible = state.canRetry
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.PostProcessing -> {
|
|
||||||
binding.textViewStatus.setText(R.string.processing_)
|
|
||||||
binding.progressBar.isIndeterminate = true
|
|
||||||
binding.progressBar.isVisible = true
|
|
||||||
binding.textViewPercent.isVisible = false
|
|
||||||
binding.textViewDetails.isVisible = false
|
|
||||||
binding.buttonCancel.isVisible = false
|
|
||||||
binding.buttonResume.isVisible = false
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Preparing -> {
|
|
||||||
binding.textViewStatus.setText(R.string.preparing_)
|
|
||||||
binding.progressBar.isIndeterminate = true
|
|
||||||
binding.progressBar.isVisible = true
|
|
||||||
binding.textViewPercent.isVisible = false
|
|
||||||
binding.textViewDetails.isVisible = false
|
|
||||||
binding.buttonCancel.isVisible = true
|
|
||||||
binding.buttonResume.isVisible = false
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Progress -> {
|
|
||||||
binding.textViewStatus.setText(R.string.manga_downloading_)
|
|
||||||
binding.progressBar.isIndeterminate = false
|
|
||||||
binding.progressBar.isVisible = true
|
|
||||||
binding.progressBar.max = state.max
|
|
||||||
binding.progressBar.setProgressCompat(state.progress, true)
|
|
||||||
binding.textViewPercent.text = percentPattern.format((state.percent * 100f).format(1))
|
|
||||||
binding.textViewPercent.isVisible = true
|
|
||||||
binding.textViewDetails.isVisible = false
|
|
||||||
binding.buttonCancel.isVisible = true
|
|
||||||
binding.buttonResume.isVisible = false
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Queued -> {
|
|
||||||
binding.textViewStatus.setText(R.string.queued)
|
|
||||||
binding.progressBar.isIndeterminate = false
|
|
||||||
binding.progressBar.isVisible = false
|
|
||||||
binding.textViewPercent.isVisible = false
|
|
||||||
binding.textViewDetails.isVisible = false
|
|
||||||
binding.buttonCancel.isVisible = true
|
|
||||||
binding.buttonResume.isVisible = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.launchIn(lifecycleOwner.lifecycleScope)
|
|
||||||
}
|
|
||||||
|
|
||||||
onViewRecycled {
|
|
||||||
job?.cancel()
|
|
||||||
job = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,58 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Bundle
|
|
||||||
import androidx.core.graphics.Insets
|
|
||||||
import androidx.core.view.isVisible
|
|
||||||
import androidx.core.view.updatePadding
|
|
||||||
import coil.ImageLoader
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.base.ui.BaseActivity
|
|
||||||
import org.koitharu.kotatsu.base.ui.list.decor.SpacingItemDecoration
|
|
||||||
import org.koitharu.kotatsu.databinding.ActivityDownloadsBinding
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class DownloadsActivity : BaseActivity<ActivityDownloadsBinding>() {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var coil: ImageLoader
|
|
||||||
|
|
||||||
private lateinit var serviceConnection: DownloadsConnection
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
setContentView(ActivityDownloadsBinding.inflate(layoutInflater))
|
|
||||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
||||||
val adapter = DownloadsAdapter(this, coil)
|
|
||||||
val spacing = resources.getDimensionPixelOffset(R.dimen.list_spacing)
|
|
||||||
binding.recyclerView.addItemDecoration(SpacingItemDecoration(spacing))
|
|
||||||
binding.recyclerView.setHasFixedSize(true)
|
|
||||||
binding.recyclerView.adapter = adapter
|
|
||||||
serviceConnection = DownloadsConnection(this, this)
|
|
||||||
serviceConnection.items.observe(this) { items ->
|
|
||||||
adapter.items = items
|
|
||||||
binding.textViewHolder.isVisible = items.isNullOrEmpty()
|
|
||||||
}
|
|
||||||
serviceConnection.bind()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onWindowInsetsChanged(insets: Insets) {
|
|
||||||
binding.recyclerView.updatePadding(
|
|
||||||
left = insets.left,
|
|
||||||
right = insets.right,
|
|
||||||
bottom = insets.bottom,
|
|
||||||
)
|
|
||||||
binding.toolbar.updatePadding(
|
|
||||||
left = insets.left,
|
|
||||||
right = insets.right,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
fun newIntent(context: Context) = Intent(context, DownloadsActivity::class.java)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,46 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui
|
|
||||||
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
|
||||||
import coil.ImageLoader
|
|
||||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
|
||||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
|
||||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
|
||||||
|
|
||||||
typealias DownloadItem = PausingProgressJob<DownloadState>
|
|
||||||
|
|
||||||
class DownloadsAdapter(
|
|
||||||
lifecycleOwner: LifecycleOwner,
|
|
||||||
coil: ImageLoader,
|
|
||||||
) : AsyncListDifferDelegationAdapter<DownloadItem>(DiffCallback()) {
|
|
||||||
|
|
||||||
init {
|
|
||||||
delegatesManager.addDelegate(downloadItemAD(lifecycleOwner, coil))
|
|
||||||
setHasStableIds(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getItemId(position: Int): Long {
|
|
||||||
return items[position].progressValue.startId.toLong()
|
|
||||||
}
|
|
||||||
|
|
||||||
private class DiffCallback : DiffUtil.ItemCallback<DownloadItem>() {
|
|
||||||
|
|
||||||
override fun areItemsTheSame(
|
|
||||||
oldItem: DownloadItem,
|
|
||||||
newItem: DownloadItem,
|
|
||||||
): Boolean {
|
|
||||||
return oldItem.progressValue.startId == newItem.progressValue.startId
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun areContentsTheSame(
|
|
||||||
oldItem: DownloadItem,
|
|
||||||
newItem: DownloadItem,
|
|
||||||
): Boolean {
|
|
||||||
return oldItem.progressValue == newItem.progressValue && oldItem.isPaused == newItem.isPaused
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChangePayload(oldItem: DownloadItem, newItem: DownloadItem): Any {
|
|
||||||
return Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,76 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui
|
|
||||||
|
|
||||||
import android.content.ComponentName
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.content.ServiceConnection
|
|
||||||
import android.os.IBinder
|
|
||||||
import androidx.lifecycle.DefaultLifecycleObserver
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
|
||||||
import org.koitharu.kotatsu.download.ui.service.DownloadService
|
|
||||||
import org.koitharu.kotatsu.utils.asFlowLiveData
|
|
||||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
|
||||||
|
|
||||||
class DownloadsConnection(
|
|
||||||
private val context: Context,
|
|
||||||
private val lifecycleOwner: LifecycleOwner,
|
|
||||||
) : ServiceConnection {
|
|
||||||
|
|
||||||
private var bindingObserver: BindingLifecycleObserver? = null
|
|
||||||
private var collectJob: Job? = null
|
|
||||||
private val itemsFlow = MutableStateFlow<List<PausingProgressJob<DownloadState>>>(emptyList())
|
|
||||||
|
|
||||||
val items
|
|
||||||
get() = itemsFlow.asFlowLiveData()
|
|
||||||
|
|
||||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
|
||||||
collectJob?.cancel()
|
|
||||||
val binder = (service as? DownloadService.DownloadBinder)
|
|
||||||
collectJob = if (binder == null) {
|
|
||||||
null
|
|
||||||
} else {
|
|
||||||
lifecycleOwner.lifecycleScope.launch {
|
|
||||||
binder.downloads.collect {
|
|
||||||
itemsFlow.value = it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onServiceDisconnected(name: ComponentName?) {
|
|
||||||
collectJob?.cancel()
|
|
||||||
collectJob = null
|
|
||||||
itemsFlow.value = itemsFlow.value.filter { it.progressValue.isTerminal }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun bind() {
|
|
||||||
if (bindingObserver != null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
bindingObserver = BindingLifecycleObserver().also {
|
|
||||||
lifecycleOwner.lifecycle.addObserver(it)
|
|
||||||
}
|
|
||||||
context.bindService(Intent(context, DownloadService::class.java), this, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun unbind() {
|
|
||||||
bindingObserver?.let {
|
|
||||||
lifecycleOwner.lifecycle.removeObserver(it)
|
|
||||||
}
|
|
||||||
bindingObserver = null
|
|
||||||
context.unbindService(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
private inner class BindingLifecycleObserver : DefaultLifecycleObserver {
|
|
||||||
|
|
||||||
override fun onDestroy(owner: LifecycleOwner) {
|
|
||||||
super.onDestroy(owner)
|
|
||||||
unbind()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,140 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.work.WorkInfo
|
||||||
|
import coil.ImageLoader
|
||||||
|
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.databinding.ItemDownloadBinding
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||||
|
import org.koitharu.kotatsu.parsers.util.format
|
||||||
|
import org.koitharu.kotatsu.utils.ext.disposeImageRequest
|
||||||
|
import org.koitharu.kotatsu.utils.ext.enqueueWith
|
||||||
|
import org.koitharu.kotatsu.utils.ext.newImageRequest
|
||||||
|
import org.koitharu.kotatsu.utils.ext.source
|
||||||
|
import org.koitharu.kotatsu.utils.ext.textAndVisible
|
||||||
|
|
||||||
|
fun downloadItemAD(
|
||||||
|
lifecycleOwner: LifecycleOwner,
|
||||||
|
coil: ImageLoader,
|
||||||
|
listener: DownloadItemListener,
|
||||||
|
) = adapterDelegateViewBinding<DownloadItemModel, ListModel, ItemDownloadBinding>(
|
||||||
|
{ inflater, parent -> ItemDownloadBinding.inflate(inflater, parent, false) },
|
||||||
|
) {
|
||||||
|
|
||||||
|
val percentPattern = context.resources.getString(R.string.percent_string_pattern)
|
||||||
|
|
||||||
|
val clickListener = object : View.OnClickListener, View.OnLongClickListener {
|
||||||
|
override fun onClick(v: View) {
|
||||||
|
when (v.id) {
|
||||||
|
R.id.button_cancel -> listener.onCancelClick(item)
|
||||||
|
R.id.button_resume -> listener.onResumeClick(item)
|
||||||
|
R.id.button_pause -> listener.onPauseClick(item)
|
||||||
|
else -> listener.onItemClick(item, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLongClick(v: View): Boolean {
|
||||||
|
return listener.onItemLongClick(item, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
binding.buttonCancel.setOnClickListener(clickListener)
|
||||||
|
binding.buttonPause.setOnClickListener(clickListener)
|
||||||
|
binding.buttonResume.setOnClickListener(clickListener)
|
||||||
|
itemView.setOnClickListener(clickListener)
|
||||||
|
itemView.setOnLongClickListener(clickListener)
|
||||||
|
|
||||||
|
bind { payloads ->
|
||||||
|
binding.textViewTitle.text = item.manga.title
|
||||||
|
binding.imageViewCover.newImageRequest(lifecycleOwner, item.manga.coverUrl)?.apply {
|
||||||
|
placeholder(R.drawable.ic_placeholder)
|
||||||
|
fallback(R.drawable.ic_placeholder)
|
||||||
|
error(R.drawable.ic_error_placeholder)
|
||||||
|
allowRgb565(true)
|
||||||
|
source(item.manga.source)
|
||||||
|
enqueueWith(coil)
|
||||||
|
}
|
||||||
|
when (item.workState) {
|
||||||
|
WorkInfo.State.ENQUEUED,
|
||||||
|
WorkInfo.State.BLOCKED -> {
|
||||||
|
binding.textViewStatus.setText(R.string.queued)
|
||||||
|
binding.progressBar.isIndeterminate = false
|
||||||
|
binding.progressBar.isVisible = false
|
||||||
|
binding.progressBar.isEnabled = true
|
||||||
|
binding.textViewPercent.isVisible = false
|
||||||
|
binding.textViewDetails.isVisible = false
|
||||||
|
binding.buttonCancel.isVisible = true
|
||||||
|
binding.buttonResume.isVisible = false
|
||||||
|
binding.buttonPause.isVisible = false
|
||||||
|
}
|
||||||
|
|
||||||
|
WorkInfo.State.RUNNING -> {
|
||||||
|
binding.textViewStatus.setText(
|
||||||
|
if (item.isPaused) R.string.paused else R.string.manga_downloading_,
|
||||||
|
)
|
||||||
|
binding.progressBar.isIndeterminate = item.isIndeterminate
|
||||||
|
binding.progressBar.isVisible = true
|
||||||
|
binding.progressBar.max = item.max
|
||||||
|
binding.progressBar.isEnabled = !item.isPaused
|
||||||
|
binding.progressBar.setProgressCompat(item.progress, payloads.isNotEmpty())
|
||||||
|
binding.textViewPercent.text = percentPattern.format((item.percent * 100f).format(1))
|
||||||
|
binding.textViewPercent.isVisible = true
|
||||||
|
binding.textViewDetails.textAndVisible = item.getEtaString()
|
||||||
|
binding.buttonCancel.isVisible = true
|
||||||
|
binding.buttonResume.isVisible = item.isPaused
|
||||||
|
binding.buttonPause.isVisible = item.canPause
|
||||||
|
}
|
||||||
|
|
||||||
|
WorkInfo.State.SUCCEEDED -> {
|
||||||
|
binding.textViewStatus.setText(R.string.download_complete)
|
||||||
|
binding.progressBar.isIndeterminate = false
|
||||||
|
binding.progressBar.isVisible = false
|
||||||
|
binding.progressBar.isEnabled = true
|
||||||
|
binding.textViewPercent.isVisible = false
|
||||||
|
if (item.totalChapters > 0) {
|
||||||
|
binding.textViewDetails.text = context.resources.getQuantityString(
|
||||||
|
R.plurals.chapters,
|
||||||
|
item.totalChapters,
|
||||||
|
item.totalChapters,
|
||||||
|
)
|
||||||
|
binding.textViewDetails.isVisible = true
|
||||||
|
} else {
|
||||||
|
binding.textViewDetails.isVisible = false
|
||||||
|
}
|
||||||
|
binding.buttonCancel.isVisible = false
|
||||||
|
binding.buttonResume.isVisible = false
|
||||||
|
binding.buttonPause.isVisible = false
|
||||||
|
}
|
||||||
|
|
||||||
|
WorkInfo.State.FAILED -> {
|
||||||
|
binding.textViewStatus.setText(R.string.error_occurred)
|
||||||
|
binding.progressBar.isIndeterminate = false
|
||||||
|
binding.progressBar.isVisible = false
|
||||||
|
binding.progressBar.isEnabled = true
|
||||||
|
binding.textViewPercent.isVisible = false
|
||||||
|
binding.textViewDetails.textAndVisible = item.error
|
||||||
|
binding.buttonCancel.isVisible = false
|
||||||
|
binding.buttonResume.isVisible = false
|
||||||
|
binding.buttonPause.isVisible = false
|
||||||
|
}
|
||||||
|
|
||||||
|
WorkInfo.State.CANCELLED -> {
|
||||||
|
binding.textViewStatus.setText(R.string.canceled)
|
||||||
|
binding.progressBar.isIndeterminate = false
|
||||||
|
binding.progressBar.isVisible = false
|
||||||
|
binding.progressBar.isEnabled = true
|
||||||
|
binding.textViewPercent.isVisible = false
|
||||||
|
binding.textViewDetails.isVisible = false
|
||||||
|
binding.buttonCancel.isVisible = false
|
||||||
|
binding.buttonResume.isVisible = false
|
||||||
|
binding.buttonPause.isVisible = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onViewRecycled {
|
||||||
|
binding.imageViewCover.disposeImageRequest()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
||||||
|
|
||||||
|
interface DownloadItemListener : OnListItemClickListener<DownloadItemModel> {
|
||||||
|
|
||||||
|
fun onCancelClick(item: DownloadItemModel)
|
||||||
|
|
||||||
|
fun onPauseClick(item: DownloadItemModel)
|
||||||
|
|
||||||
|
fun onResumeClick(item: DownloadItemModel)
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import android.text.format.DateUtils
|
||||||
|
import androidx.work.WorkInfo
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class DownloadItemModel(
|
||||||
|
val id: UUID,
|
||||||
|
val workState: WorkInfo.State,
|
||||||
|
val isIndeterminate: Boolean,
|
||||||
|
val isPaused: Boolean,
|
||||||
|
val manga: Manga,
|
||||||
|
val error: String?,
|
||||||
|
val max: Int,
|
||||||
|
val totalChapters: Int,
|
||||||
|
val progress: Int,
|
||||||
|
val eta: Long,
|
||||||
|
val timestamp: Date,
|
||||||
|
) : ListModel, Comparable<DownloadItemModel> {
|
||||||
|
|
||||||
|
val percent: Float
|
||||||
|
get() = if (max > 0) progress / max.toFloat() else 0f
|
||||||
|
|
||||||
|
val hasEta: Boolean
|
||||||
|
get() = workState == WorkInfo.State.RUNNING && !isPaused && eta > 0L
|
||||||
|
|
||||||
|
val canPause: Boolean
|
||||||
|
get() = workState == WorkInfo.State.RUNNING && !isPaused && error == null
|
||||||
|
|
||||||
|
val canResume: Boolean
|
||||||
|
get() = workState == WorkInfo.State.RUNNING && isPaused
|
||||||
|
|
||||||
|
fun getEtaString(): CharSequence? = if (hasEta) {
|
||||||
|
DateUtils.getRelativeTimeSpanString(
|
||||||
|
eta,
|
||||||
|
System.currentTimeMillis(),
|
||||||
|
DateUtils.SECOND_IN_MILLIS,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun compareTo(other: DownloadItemModel): Int {
|
||||||
|
return timestamp.compareTo(other.timestamp)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (javaClass != other?.javaClass) return false
|
||||||
|
|
||||||
|
other as DownloadItemModel
|
||||||
|
|
||||||
|
if (id != other.id) return false
|
||||||
|
if (workState != other.workState) return false
|
||||||
|
if (isIndeterminate != other.isIndeterminate) return false
|
||||||
|
if (isPaused != other.isPaused) return false
|
||||||
|
if (manga != other.manga) return false
|
||||||
|
if (error != other.error) return false
|
||||||
|
if (max != other.max) return false
|
||||||
|
if (totalChapters != other.totalChapters) return false
|
||||||
|
if (progress != other.progress) return false
|
||||||
|
if (eta != other.eta) return false
|
||||||
|
return timestamp == other.timestamp
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = id.hashCode()
|
||||||
|
result = 31 * result + workState.hashCode()
|
||||||
|
result = 31 * result + isIndeterminate.hashCode()
|
||||||
|
result = 31 * result + isPaused.hashCode()
|
||||||
|
result = 31 * result + manga.hashCode()
|
||||||
|
result = 31 * result + (error?.hashCode() ?: 0)
|
||||||
|
result = 31 * result + max
|
||||||
|
result = 31 * result + totalChapters
|
||||||
|
result = 31 * result + progress
|
||||||
|
result = 31 * result + eta.hashCode()
|
||||||
|
result = 31 * result + timestamp.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,171 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.view.View
|
||||||
|
import androidx.activity.viewModels
|
||||||
|
import androidx.annotation.Px
|
||||||
|
import androidx.appcompat.view.ActionMode
|
||||||
|
import androidx.core.graphics.Insets
|
||||||
|
import androidx.core.view.updatePadding
|
||||||
|
import androidx.lifecycle.Observer
|
||||||
|
import coil.ImageLoader
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.base.ui.BaseActivity
|
||||||
|
import org.koitharu.kotatsu.base.ui.list.ListSelectionController
|
||||||
|
import org.koitharu.kotatsu.base.ui.list.decor.SpacingItemDecoration
|
||||||
|
import org.koitharu.kotatsu.databinding.ActivityDownloadsBinding
|
||||||
|
import org.koitharu.kotatsu.details.ui.DetailsActivity
|
||||||
|
import org.koitharu.kotatsu.download.ui.worker.PausingReceiver
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class DownloadsActivity : BaseActivity<ActivityDownloadsBinding>(),
|
||||||
|
DownloadItemListener,
|
||||||
|
ListSelectionController.Callback2 {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var coil: ImageLoader
|
||||||
|
|
||||||
|
private val viewModel by viewModels<DownloadsViewModel>()
|
||||||
|
private lateinit var selectionController: ListSelectionController
|
||||||
|
|
||||||
|
@Px
|
||||||
|
private var listSpacing = 0
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setContentView(ActivityDownloadsBinding.inflate(layoutInflater))
|
||||||
|
listSpacing = resources.getDimensionPixelOffset(R.dimen.list_spacing)
|
||||||
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
|
val downloadsAdapter = DownloadsAdapter(this, coil, this)
|
||||||
|
val decoration = SpacingItemDecoration(listSpacing)
|
||||||
|
selectionController = ListSelectionController(
|
||||||
|
activity = this,
|
||||||
|
decoration = DownloadsSelectionDecoration(this),
|
||||||
|
registryOwner = this,
|
||||||
|
callback = this,
|
||||||
|
)
|
||||||
|
with(binding.recyclerView) {
|
||||||
|
setHasFixedSize(true)
|
||||||
|
addItemDecoration(decoration)
|
||||||
|
adapter = downloadsAdapter
|
||||||
|
selectionController.attachToRecyclerView(this)
|
||||||
|
}
|
||||||
|
addMenuProvider(DownloadsMenuProvider(this, viewModel))
|
||||||
|
viewModel.items.observe(this) {
|
||||||
|
downloadsAdapter.items = it
|
||||||
|
}
|
||||||
|
val menuObserver = Observer<Any> { _ -> invalidateOptionsMenu() }
|
||||||
|
viewModel.hasActiveWorks.observe(this, menuObserver)
|
||||||
|
viewModel.hasPausedWorks.observe(this, menuObserver)
|
||||||
|
viewModel.hasCancellableWorks.observe(this, menuObserver)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onWindowInsetsChanged(insets: Insets) {
|
||||||
|
binding.recyclerView.updatePadding(
|
||||||
|
left = insets.left + listSpacing,
|
||||||
|
right = insets.right + listSpacing,
|
||||||
|
bottom = insets.bottom,
|
||||||
|
)
|
||||||
|
binding.toolbar.updatePadding(
|
||||||
|
left = insets.left,
|
||||||
|
right = insets.right,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemClick(item: DownloadItemModel, view: View) {
|
||||||
|
if (selectionController.onItemClick(item.id.mostSignificantBits)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
startActivity(DetailsActivity.newIntent(view.context, item.manga))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemLongClick(item: DownloadItemModel, view: View): Boolean {
|
||||||
|
return selectionController.onItemLongClick(item.id.mostSignificantBits)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCancelClick(item: DownloadItemModel) {
|
||||||
|
viewModel.cancel(item.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPauseClick(item: DownloadItemModel) {
|
||||||
|
sendBroadcast(PausingReceiver.getPauseIntent(item.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResumeClick(item: DownloadItemModel) {
|
||||||
|
sendBroadcast(PausingReceiver.getResumeIntent(item.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSelectionChanged(controller: ListSelectionController, count: Int) {
|
||||||
|
binding.recyclerView.invalidateItemDecorations()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateActionMode(controller: ListSelectionController, mode: ActionMode, menu: Menu): Boolean {
|
||||||
|
mode.menuInflater.inflate(R.menu.mode_downloads, menu)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActionItemClicked(controller: ListSelectionController, mode: ActionMode, item: MenuItem): Boolean {
|
||||||
|
return when (item.itemId) {
|
||||||
|
R.id.action_resume -> {
|
||||||
|
viewModel.resume(controller.snapshot())
|
||||||
|
mode.finish()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.action_pause -> {
|
||||||
|
viewModel.pause(controller.snapshot())
|
||||||
|
mode.finish()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.action_cancel -> {
|
||||||
|
viewModel.cancel(controller.snapshot())
|
||||||
|
mode.finish()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.action_remove -> {
|
||||||
|
viewModel.remove(controller.snapshot())
|
||||||
|
mode.finish()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.action_select_all -> {
|
||||||
|
controller.addAll(viewModel.allIds())
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPrepareActionMode(controller: ListSelectionController, mode: ActionMode, menu: Menu): Boolean {
|
||||||
|
val snapshot = viewModel.snapshot(controller.peekCheckedIds())
|
||||||
|
var canPause = true
|
||||||
|
var canResume = true
|
||||||
|
var canCancel = true
|
||||||
|
var canRemove = true
|
||||||
|
for (item in snapshot) {
|
||||||
|
canPause = canPause and item.canPause
|
||||||
|
canResume = canResume and item.canResume
|
||||||
|
canCancel = canCancel and !item.workState.isFinished
|
||||||
|
canRemove = canRemove and item.workState.isFinished
|
||||||
|
}
|
||||||
|
menu.findItem(R.id.action_pause)?.isVisible = canPause
|
||||||
|
menu.findItem(R.id.action_resume)?.isVisible = canResume
|
||||||
|
menu.findItem(R.id.action_cancel)?.isVisible = canCancel
|
||||||
|
menu.findItem(R.id.action_remove)?.isVisible = canRemove
|
||||||
|
return super.onPrepareActionMode(controller, mode, menu)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
fun newIntent(context: Context) = Intent(context, DownloadsActivity::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
|
import coil.ImageLoader
|
||||||
|
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||||
|
import org.koitharu.kotatsu.core.ui.DateTimeAgo
|
||||||
|
import org.koitharu.kotatsu.list.ui.adapter.emptyStateListAD
|
||||||
|
import org.koitharu.kotatsu.list.ui.adapter.loadingStateAD
|
||||||
|
import org.koitharu.kotatsu.list.ui.adapter.relatedDateItemAD
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||||
|
import kotlin.jvm.internal.Intrinsics
|
||||||
|
|
||||||
|
class DownloadsAdapter(
|
||||||
|
lifecycleOwner: LifecycleOwner,
|
||||||
|
coil: ImageLoader,
|
||||||
|
listener: DownloadItemListener,
|
||||||
|
) : AsyncListDifferDelegationAdapter<ListModel>(DiffCallback()) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
delegatesManager.addDelegate(ITEM_TYPE_DOWNLOAD, downloadItemAD(lifecycleOwner, coil, listener))
|
||||||
|
.addDelegate(loadingStateAD())
|
||||||
|
.addDelegate(emptyStateListAD(coil, lifecycleOwner, null))
|
||||||
|
.addDelegate(relatedDateItemAD())
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DiffCallback : DiffUtil.ItemCallback<ListModel>() {
|
||||||
|
|
||||||
|
override fun areItemsTheSame(oldItem: ListModel, newItem: ListModel) = when {
|
||||||
|
|
||||||
|
oldItem is DownloadItemModel && newItem is DownloadItemModel -> {
|
||||||
|
oldItem.id == newItem.id
|
||||||
|
}
|
||||||
|
|
||||||
|
oldItem is DateTimeAgo && newItem is DateTimeAgo -> {
|
||||||
|
oldItem == newItem
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> oldItem.javaClass == newItem.javaClass
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItem: ListModel, newItem: ListModel): Boolean {
|
||||||
|
return Intrinsics.areEqual(oldItem, newItem)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getChangePayload(oldItem: ListModel, newItem: ListModel): Any? {
|
||||||
|
return when (newItem) {
|
||||||
|
is DownloadItemModel -> {
|
||||||
|
oldItem as DownloadItemModel
|
||||||
|
if (oldItem.workState == newItem.workState) {
|
||||||
|
Unit
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> super.getChangePayload(oldItem, newItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val ITEM_TYPE_DOWNLOAD = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuInflater
|
||||||
|
import android.view.MenuItem
|
||||||
|
import androidx.core.view.MenuProvider
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
|
||||||
|
class DownloadsMenuProvider(
|
||||||
|
private val context: Context,
|
||||||
|
private val viewModel: DownloadsViewModel,
|
||||||
|
) : MenuProvider {
|
||||||
|
|
||||||
|
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||||
|
menuInflater.inflate(R.menu.opt_downloads, menu)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
|
||||||
|
when (menuItem.itemId) {
|
||||||
|
R.id.action_pause -> viewModel.pauseAll()
|
||||||
|
R.id.action_resume -> viewModel.resumeAll()
|
||||||
|
R.id.action_cancel_all -> viewModel.cancelAll()
|
||||||
|
R.id.action_remove_completed -> viewModel.removeCompleted()
|
||||||
|
else -> return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPrepareMenu(menu: Menu) {
|
||||||
|
super.onPrepareMenu(menu)
|
||||||
|
menu.findItem(R.id.action_pause)?.isVisible = viewModel.hasActiveWorks.value == true
|
||||||
|
menu.findItem(R.id.action_resume)?.isVisible = viewModel.hasPausedWorks.value == true
|
||||||
|
menu.findItem(R.id.action_cancel_all)?.isVisible = viewModel.hasCancellableWorks.value == true
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Canvas
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.graphics.Paint
|
||||||
|
import android.graphics.RectF
|
||||||
|
import android.view.View
|
||||||
|
import androidx.cardview.widget.CardView
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.graphics.ColorUtils
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import androidx.recyclerview.widget.RecyclerView.NO_ID
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.base.ui.list.decor.AbstractSelectionItemDecoration
|
||||||
|
import org.koitharu.kotatsu.utils.ext.getItem
|
||||||
|
import org.koitharu.kotatsu.utils.ext.getThemeColor
|
||||||
|
import com.google.android.material.R as materialR
|
||||||
|
|
||||||
|
class DownloadsSelectionDecoration(context: Context) : AbstractSelectionItemDecoration() {
|
||||||
|
|
||||||
|
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||||
|
private val checkIcon = ContextCompat.getDrawable(context, materialR.drawable.ic_mtrl_checked_circle)
|
||||||
|
private val iconOffset = context.resources.getDimensionPixelOffset(R.dimen.card_indicator_offset)
|
||||||
|
private val iconSize = context.resources.getDimensionPixelOffset(R.dimen.card_indicator_size)
|
||||||
|
private val strokeColor = context.getThemeColor(materialR.attr.colorPrimary, Color.RED)
|
||||||
|
private val fillColor = ColorUtils.setAlphaComponent(
|
||||||
|
ColorUtils.blendARGB(strokeColor, context.getThemeColor(materialR.attr.colorSurface), 0.8f),
|
||||||
|
0x74,
|
||||||
|
)
|
||||||
|
private val defaultRadius = context.resources.getDimension(R.dimen.list_selector_corner)
|
||||||
|
|
||||||
|
init {
|
||||||
|
hasBackground = false
|
||||||
|
hasForeground = true
|
||||||
|
isIncludeDecorAndMargins = false
|
||||||
|
|
||||||
|
paint.strokeWidth = context.resources.getDimension(R.dimen.selection_stroke_width)
|
||||||
|
checkIcon?.setTint(strokeColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemId(parent: RecyclerView, child: View): Long {
|
||||||
|
val holder = parent.getChildViewHolder(child) ?: return NO_ID
|
||||||
|
val item = holder.getItem(DownloadItemModel::class.java) ?: return NO_ID
|
||||||
|
return item.id.mostSignificantBits
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDrawForeground(
|
||||||
|
canvas: Canvas,
|
||||||
|
parent: RecyclerView,
|
||||||
|
child: View,
|
||||||
|
bounds: RectF,
|
||||||
|
state: RecyclerView.State,
|
||||||
|
) {
|
||||||
|
val isCard = child is CardView
|
||||||
|
val radius = (child as? CardView)?.radius ?: defaultRadius
|
||||||
|
paint.color = fillColor
|
||||||
|
paint.style = Paint.Style.FILL
|
||||||
|
canvas.drawRoundRect(bounds, radius, radius, paint)
|
||||||
|
paint.color = strokeColor
|
||||||
|
paint.style = Paint.Style.STROKE
|
||||||
|
canvas.drawRoundRect(bounds, radius, radius, paint)
|
||||||
|
if (isCard) {
|
||||||
|
checkIcon?.run {
|
||||||
|
setBounds(
|
||||||
|
(bounds.right - iconSize - iconOffset).toInt(),
|
||||||
|
(bounds.top + iconOffset).toInt(),
|
||||||
|
(bounds.right - iconOffset).toInt(),
|
||||||
|
(bounds.top + iconOffset + iconSize).toInt(),
|
||||||
|
)
|
||||||
|
draw(canvas)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,226 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.list
|
||||||
|
|
||||||
|
import androidx.collection.LongSparseArray
|
||||||
|
import androidx.collection.getOrElse
|
||||||
|
import androidx.collection.set
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import androidx.work.Data
|
||||||
|
import androidx.work.WorkInfo
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.mapLatest
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import kotlinx.coroutines.plus
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||||
|
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
||||||
|
import org.koitharu.kotatsu.core.ui.DateTimeAgo
|
||||||
|
import org.koitharu.kotatsu.download.domain.DownloadState
|
||||||
|
import org.koitharu.kotatsu.download.ui.worker.DownloadWorker
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.EmptyState
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.LoadingState
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||||
|
import org.koitharu.kotatsu.utils.asFlowLiveData
|
||||||
|
import org.koitharu.kotatsu.utils.ext.daysDiff
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.UUID
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class DownloadsViewModel @Inject constructor(
|
||||||
|
private val workScheduler: DownloadWorker.Scheduler,
|
||||||
|
private val mangaDataRepository: MangaDataRepository,
|
||||||
|
) : BaseViewModel() {
|
||||||
|
|
||||||
|
private val mangaCache = LongSparseArray<Manga>()
|
||||||
|
private val cacheMutex = Mutex()
|
||||||
|
private val works = workScheduler.observeWorks()
|
||||||
|
.mapLatest { it.toDownloadsList() }
|
||||||
|
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, null)
|
||||||
|
|
||||||
|
val items = works.map {
|
||||||
|
it?.toUiList() ?: listOf(LoadingState)
|
||||||
|
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, listOf(LoadingState))
|
||||||
|
|
||||||
|
val hasPausedWorks = works.map {
|
||||||
|
it?.any { x -> x.canResume } == true
|
||||||
|
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, false)
|
||||||
|
|
||||||
|
val hasActiveWorks = works.map {
|
||||||
|
it?.any { x -> x.canPause } == true
|
||||||
|
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, false)
|
||||||
|
|
||||||
|
val hasCancellableWorks = works.map {
|
||||||
|
it?.any { x -> !x.workState.isFinished } == true
|
||||||
|
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, false)
|
||||||
|
|
||||||
|
fun cancel(id: UUID) {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
workScheduler.cancel(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cancel(ids: Set<Long>) {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
val snapshot = works.value ?: return@launchJob
|
||||||
|
for (work in snapshot) {
|
||||||
|
if (work.id.mostSignificantBits in ids) {
|
||||||
|
workScheduler.cancel(work.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cancelAll() {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
workScheduler.cancelAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pause(ids: Set<Long>) {
|
||||||
|
val snapshot = works.value ?: return
|
||||||
|
for (work in snapshot) {
|
||||||
|
if (work.id.mostSignificantBits in ids) {
|
||||||
|
workScheduler.pause(work.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pauseAll() {
|
||||||
|
val snapshot = works.value ?: return
|
||||||
|
for (work in snapshot) {
|
||||||
|
if (work.canPause) {
|
||||||
|
workScheduler.pause(work.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resumeAll() {
|
||||||
|
val snapshot = works.value ?: return
|
||||||
|
for (work in snapshot) {
|
||||||
|
if (work.workState == WorkInfo.State.RUNNING && work.isPaused) {
|
||||||
|
workScheduler.resume(work.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resume(ids: Set<Long>) {
|
||||||
|
val snapshot = works.value ?: return
|
||||||
|
for (work in snapshot) {
|
||||||
|
if (work.id.mostSignificantBits in ids) {
|
||||||
|
workScheduler.resume(work.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun remove(ids: Set<Long>) {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
val snapshot = works.value ?: return@launchJob
|
||||||
|
for (work in snapshot) {
|
||||||
|
if (work.id.mostSignificantBits in ids) {
|
||||||
|
workScheduler.delete(work.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeCompleted() {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
workScheduler.removeCompleted()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun snapshot(ids: Set<Long>): Collection<DownloadItemModel> {
|
||||||
|
return works.value?.filterTo(ArrayList(ids.size)) { x -> x.id.mostSignificantBits in ids }.orEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun allIds(): Set<Long> = works.value?.mapToSet {
|
||||||
|
it.id.mostSignificantBits
|
||||||
|
} ?: emptySet()
|
||||||
|
|
||||||
|
private suspend fun List<WorkInfo>.toDownloadsList(): List<DownloadItemModel> {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return emptyList()
|
||||||
|
}
|
||||||
|
val list = mapNotNullTo(ArrayList(size)) { it.toUiModel() }
|
||||||
|
list.sortByDescending { it.timestamp }
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun List<DownloadItemModel>.toUiList(): List<ListModel> {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return emptyStateList()
|
||||||
|
}
|
||||||
|
val destination = ArrayList<ListModel>((size * 1.4).toInt())
|
||||||
|
var prevDate: DateTimeAgo? = null
|
||||||
|
for (item in this) {
|
||||||
|
val date = timeAgo(item.timestamp)
|
||||||
|
if (prevDate != date) {
|
||||||
|
destination += date
|
||||||
|
}
|
||||||
|
prevDate = date
|
||||||
|
destination += item
|
||||||
|
}
|
||||||
|
return destination
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun WorkInfo.toUiModel(): DownloadItemModel? {
|
||||||
|
val workData = if (outputData == Data.EMPTY) progress else outputData
|
||||||
|
val mangaId = DownloadState.getMangaId(workData)
|
||||||
|
if (mangaId == 0L) return null
|
||||||
|
val manga = getManga(mangaId) ?: return null
|
||||||
|
return DownloadItemModel(
|
||||||
|
id = id,
|
||||||
|
workState = state,
|
||||||
|
manga = manga,
|
||||||
|
error = DownloadState.getError(workData),
|
||||||
|
isIndeterminate = DownloadState.isIndeterminate(workData),
|
||||||
|
isPaused = DownloadState.isPaused(workData),
|
||||||
|
max = DownloadState.getMax(workData),
|
||||||
|
progress = DownloadState.getProgress(workData),
|
||||||
|
eta = DownloadState.getEta(workData),
|
||||||
|
timestamp = DownloadState.getTimestamp(workData),
|
||||||
|
totalChapters = DownloadState.getDownloadedChapters(workData).size,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun timeAgo(date: Date): DateTimeAgo {
|
||||||
|
val diff = (System.currentTimeMillis() - date.time).coerceAtLeast(0L)
|
||||||
|
val diffMinutes = TimeUnit.MILLISECONDS.toMinutes(diff).toInt()
|
||||||
|
val diffDays = -date.daysDiff(System.currentTimeMillis())
|
||||||
|
return when {
|
||||||
|
diffMinutes < 3 -> DateTimeAgo.JustNow
|
||||||
|
diffDays < 1 -> DateTimeAgo.Today
|
||||||
|
diffDays == 1 -> DateTimeAgo.Yesterday
|
||||||
|
diffDays < 6 -> DateTimeAgo.DaysAgo(diffDays)
|
||||||
|
else -> DateTimeAgo.Absolute(date)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun emptyStateList() = listOf(
|
||||||
|
EmptyState(
|
||||||
|
icon = R.drawable.ic_empty_common,
|
||||||
|
textPrimary = R.string.text_downloads_holder,
|
||||||
|
textSecondary = 0,
|
||||||
|
actionStringRes = 0,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
private suspend fun getManga(mangaId: Long): Manga? {
|
||||||
|
mangaCache[mangaId]?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
return cacheMutex.withLock {
|
||||||
|
mangaCache.getOrElse(mangaId) {
|
||||||
|
mangaDataRepository.findMangaById(mangaId)?.also { mangaCache[mangaId] = it } ?: return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,356 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui.service
|
|
||||||
|
|
||||||
import android.app.Notification
|
|
||||||
import android.app.NotificationChannel
|
|
||||||
import android.app.NotificationManager
|
|
||||||
import android.app.PendingIntent
|
|
||||||
import android.content.Context
|
|
||||||
import android.os.Build
|
|
||||||
import android.text.format.DateUtils
|
|
||||||
import android.util.SparseArray
|
|
||||||
import androidx.core.app.NotificationCompat
|
|
||||||
import androidx.core.app.NotificationManagerCompat
|
|
||||||
import androidx.core.app.PendingIntentCompat
|
|
||||||
import androidx.core.content.ContextCompat
|
|
||||||
import androidx.core.graphics.drawable.toBitmap
|
|
||||||
import androidx.core.text.HtmlCompat
|
|
||||||
import androidx.core.text.htmlEncode
|
|
||||||
import androidx.core.text.parseAsHtml
|
|
||||||
import androidx.core.util.forEach
|
|
||||||
import androidx.core.util.isNotEmpty
|
|
||||||
import androidx.core.util.size
|
|
||||||
import com.google.android.material.R as materialR
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.details.ui.DetailsActivity
|
|
||||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
|
||||||
import org.koitharu.kotatsu.download.ui.DownloadsActivity
|
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
||||||
import org.koitharu.kotatsu.parsers.util.ellipsize
|
|
||||||
import org.koitharu.kotatsu.parsers.util.format
|
|
||||||
import org.koitharu.kotatsu.search.ui.MangaListActivity
|
|
||||||
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
|
||||||
|
|
||||||
class DownloadNotification(private val context: Context) {
|
|
||||||
|
|
||||||
private val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
private val states = SparseArray<DownloadState>()
|
|
||||||
private val groupBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
|
|
||||||
|
|
||||||
private val queueIntent = PendingIntentCompat.getActivity(
|
|
||||||
context,
|
|
||||||
REQUEST_QUEUE,
|
|
||||||
DownloadsActivity.newIntent(context),
|
|
||||||
0,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
|
|
||||||
private val localListIntent = PendingIntentCompat.getActivity(
|
|
||||||
context,
|
|
||||||
REQUEST_LIST_LOCAL,
|
|
||||||
MangaListActivity.newIntent(context, MangaSource.LOCAL),
|
|
||||||
0,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
|
|
||||||
init {
|
|
||||||
groupBuilder.setOnlyAlertOnce(true)
|
|
||||||
groupBuilder.setDefaults(0)
|
|
||||||
groupBuilder.color = ContextCompat.getColor(context, R.color.blue_primary)
|
|
||||||
groupBuilder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
|
||||||
groupBuilder.setSilent(true)
|
|
||||||
groupBuilder.setGroup(GROUP_ID)
|
|
||||||
groupBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
|
|
||||||
groupBuilder.setGroupSummary(true)
|
|
||||||
groupBuilder.setContentTitle(context.getString(R.string.downloading_manga))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun buildGroupNotification(): Notification {
|
|
||||||
val style = NotificationCompat.InboxStyle(groupBuilder)
|
|
||||||
var progress = 0f
|
|
||||||
var isAllDone = true
|
|
||||||
var isInProgress = false
|
|
||||||
groupBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
||||||
states.forEach { _, state ->
|
|
||||||
if (state.manga.isNsfw) {
|
|
||||||
groupBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
|
||||||
}
|
|
||||||
val summary = when (state) {
|
|
||||||
is DownloadState.Cancelled -> {
|
|
||||||
progress++
|
|
||||||
context.getString(R.string.cancelling_)
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Done -> {
|
|
||||||
progress++
|
|
||||||
context.getString(R.string.download_complete)
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Error -> {
|
|
||||||
isAllDone = false
|
|
||||||
context.getString(R.string.error)
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.PostProcessing -> {
|
|
||||||
progress++
|
|
||||||
isInProgress = true
|
|
||||||
isAllDone = false
|
|
||||||
context.getString(R.string.processing_)
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Preparing -> {
|
|
||||||
isAllDone = false
|
|
||||||
isInProgress = true
|
|
||||||
context.getString(R.string.preparing_)
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Progress -> {
|
|
||||||
isAllDone = false
|
|
||||||
isInProgress = true
|
|
||||||
progress += state.percent
|
|
||||||
context.getString(R.string.percent_string_pattern, (state.percent * 100).format())
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Queued -> {
|
|
||||||
isAllDone = false
|
|
||||||
isInProgress = true
|
|
||||||
context.getString(R.string.queued)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
style.addLine(
|
|
||||||
context.getString(
|
|
||||||
R.string.download_summary_pattern,
|
|
||||||
state.manga.title.ellipsize(16).htmlEncode(),
|
|
||||||
summary.htmlEncode(),
|
|
||||||
).parseAsHtml(HtmlCompat.FROM_HTML_MODE_LEGACY),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
progress = if (isInProgress) {
|
|
||||||
progress / states.size.toFloat()
|
|
||||||
} else {
|
|
||||||
1f
|
|
||||||
}
|
|
||||||
style.setBigContentTitle(
|
|
||||||
context.getString(if (isAllDone) R.string.download_complete else R.string.downloading_manga),
|
|
||||||
)
|
|
||||||
groupBuilder.setContentText(context.resources.getQuantityString(R.plurals.items, states.size, states.size()))
|
|
||||||
groupBuilder.setNumber(states.size)
|
|
||||||
groupBuilder.setSmallIcon(
|
|
||||||
if (isInProgress) android.R.drawable.stat_sys_download else android.R.drawable.stat_sys_download_done,
|
|
||||||
)
|
|
||||||
groupBuilder.setContentIntent(if (isAllDone) localListIntent else queueIntent)
|
|
||||||
groupBuilder.setAutoCancel(isAllDone)
|
|
||||||
when (progress) {
|
|
||||||
1f -> groupBuilder.setProgress(0, 0, false)
|
|
||||||
0f -> groupBuilder.setProgress(1, 0, true)
|
|
||||||
else -> groupBuilder.setProgress(100, (progress * 100f).toInt(), false)
|
|
||||||
}
|
|
||||||
return groupBuilder.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun detach() {
|
|
||||||
if (states.isNotEmpty()) {
|
|
||||||
val notification = buildGroupNotification()
|
|
||||||
manager.notify(ID_GROUP_DETACHED, notification)
|
|
||||||
}
|
|
||||||
manager.cancel(ID_GROUP)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun newItem(startId: Int) = Item(startId)
|
|
||||||
|
|
||||||
inner class Item(
|
|
||||||
private val startId: Int,
|
|
||||||
) {
|
|
||||||
|
|
||||||
private val builder = NotificationCompat.Builder(context, CHANNEL_ID)
|
|
||||||
private val cancelAction = NotificationCompat.Action(
|
|
||||||
materialR.drawable.material_ic_clear_black_24dp,
|
|
||||||
context.getString(android.R.string.cancel),
|
|
||||||
PendingIntentCompat.getBroadcast(
|
|
||||||
context,
|
|
||||||
startId * 2,
|
|
||||||
DownloadService.getCancelIntent(startId),
|
|
||||||
PendingIntent.FLAG_CANCEL_CURRENT,
|
|
||||||
false,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
private val retryAction = NotificationCompat.Action(
|
|
||||||
R.drawable.ic_restart_black,
|
|
||||||
context.getString(R.string.try_again),
|
|
||||||
PendingIntentCompat.getBroadcast(
|
|
||||||
context,
|
|
||||||
startId * 2 + 1,
|
|
||||||
DownloadService.getResumeIntent(startId),
|
|
||||||
PendingIntent.FLAG_CANCEL_CURRENT,
|
|
||||||
false,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
init {
|
|
||||||
builder.setOnlyAlertOnce(true)
|
|
||||||
builder.setDefaults(0)
|
|
||||||
builder.color = ContextCompat.getColor(context, R.color.blue_primary)
|
|
||||||
builder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
|
||||||
builder.setSilent(true)
|
|
||||||
builder.setGroup(GROUP_ID)
|
|
||||||
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun notify(state: DownloadState, timeLeft: Long) {
|
|
||||||
builder.setContentTitle(state.manga.title)
|
|
||||||
builder.setContentText(context.getString(R.string.manga_downloading_))
|
|
||||||
builder.setProgress(1, 0, true)
|
|
||||||
builder.setSmallIcon(android.R.drawable.stat_sys_download)
|
|
||||||
builder.setContentIntent(queueIntent)
|
|
||||||
builder.setStyle(null)
|
|
||||||
builder.setLargeIcon(state.cover?.toBitmap())
|
|
||||||
builder.clearActions()
|
|
||||||
builder.setSubText(null)
|
|
||||||
builder.setShowWhen(false)
|
|
||||||
builder.setVisibility(
|
|
||||||
if (state.manga.isNsfw) {
|
|
||||||
NotificationCompat.VISIBILITY_PRIVATE
|
|
||||||
} else {
|
|
||||||
NotificationCompat.VISIBILITY_PUBLIC
|
|
||||||
},
|
|
||||||
)
|
|
||||||
when (state) {
|
|
||||||
is DownloadState.Cancelled -> {
|
|
||||||
builder.setProgress(1, 0, true)
|
|
||||||
builder.setContentText(context.getString(R.string.cancelling_))
|
|
||||||
builder.setContentIntent(null)
|
|
||||||
builder.setStyle(null)
|
|
||||||
builder.setOngoing(true)
|
|
||||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Done -> {
|
|
||||||
builder.setProgress(0, 0, false)
|
|
||||||
builder.setContentText(context.getString(R.string.download_complete))
|
|
||||||
builder.setContentIntent(createMangaIntent(context, state.localManga))
|
|
||||||
builder.setAutoCancel(true)
|
|
||||||
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
|
||||||
builder.setCategory(null)
|
|
||||||
builder.setStyle(null)
|
|
||||||
builder.setOngoing(false)
|
|
||||||
builder.setShowWhen(true)
|
|
||||||
builder.setWhen(System.currentTimeMillis())
|
|
||||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Error -> {
|
|
||||||
val message = state.error.getDisplayMessage(context.resources)
|
|
||||||
builder.setProgress(0, 0, false)
|
|
||||||
builder.setSmallIcon(android.R.drawable.stat_notify_error)
|
|
||||||
builder.setSubText(context.getString(R.string.error))
|
|
||||||
builder.setContentText(message)
|
|
||||||
builder.setAutoCancel(!state.canRetry)
|
|
||||||
builder.setOngoing(state.canRetry)
|
|
||||||
builder.setCategory(NotificationCompat.CATEGORY_ERROR)
|
|
||||||
builder.setShowWhen(true)
|
|
||||||
builder.setWhen(System.currentTimeMillis())
|
|
||||||
builder.setStyle(NotificationCompat.BigTextStyle().bigText(message))
|
|
||||||
if (state.canRetry) {
|
|
||||||
builder.addAction(cancelAction)
|
|
||||||
builder.addAction(retryAction)
|
|
||||||
}
|
|
||||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.PostProcessing -> {
|
|
||||||
builder.setProgress(1, 0, true)
|
|
||||||
builder.setContentText(context.getString(R.string.processing_))
|
|
||||||
builder.setStyle(null)
|
|
||||||
builder.setOngoing(true)
|
|
||||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Queued -> {
|
|
||||||
builder.setProgress(0, 0, false)
|
|
||||||
builder.setContentText(context.getString(R.string.queued))
|
|
||||||
builder.setStyle(null)
|
|
||||||
builder.setOngoing(true)
|
|
||||||
builder.addAction(cancelAction)
|
|
||||||
builder.priority = NotificationCompat.PRIORITY_LOW
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Preparing -> {
|
|
||||||
builder.setProgress(1, 0, true)
|
|
||||||
builder.setContentText(context.getString(R.string.preparing_))
|
|
||||||
builder.setStyle(null)
|
|
||||||
builder.setOngoing(true)
|
|
||||||
builder.addAction(cancelAction)
|
|
||||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
|
||||||
}
|
|
||||||
|
|
||||||
is DownloadState.Progress -> {
|
|
||||||
builder.setProgress(state.max, state.progress, false)
|
|
||||||
val percent = context.getString(R.string.percent_string_pattern, (state.percent * 100).format())
|
|
||||||
if (timeLeft > 0L) {
|
|
||||||
val eta = DateUtils.getRelativeTimeSpanString(timeLeft, 0L, DateUtils.SECOND_IN_MILLIS)
|
|
||||||
builder.setContentText(eta)
|
|
||||||
builder.setSubText(percent)
|
|
||||||
} else {
|
|
||||||
builder.setContentText(percent)
|
|
||||||
}
|
|
||||||
builder.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
|
||||||
builder.setStyle(null)
|
|
||||||
builder.setOngoing(true)
|
|
||||||
builder.addAction(cancelAction)
|
|
||||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val notification = builder.build()
|
|
||||||
states.append(startId, state)
|
|
||||||
updateGroupNotification()
|
|
||||||
manager.notify(TAG, startId, notification)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun dismiss() {
|
|
||||||
manager.cancel(TAG, startId)
|
|
||||||
states.remove(startId)
|
|
||||||
updateGroupNotification()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateGroupNotification() {
|
|
||||||
val notification = buildGroupNotification()
|
|
||||||
manager.notify(ID_GROUP, notification)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createMangaIntent(context: Context, manga: Manga) = PendingIntentCompat.getActivity(
|
|
||||||
context,
|
|
||||||
manga.hashCode(),
|
|
||||||
DetailsActivity.newIntent(context, manga),
|
|
||||||
PendingIntent.FLAG_CANCEL_CURRENT,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
private const val TAG = "download"
|
|
||||||
private const val CHANNEL_ID = "download"
|
|
||||||
private const val GROUP_ID = "downloads"
|
|
||||||
private const val REQUEST_QUEUE = 6
|
|
||||||
private const val REQUEST_LIST_LOCAL = 7
|
|
||||||
const val ID_GROUP = 9999
|
|
||||||
private const val ID_GROUP_DETACHED = 9998
|
|
||||||
|
|
||||||
fun createChannel(context: Context) {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
val manager = NotificationManagerCompat.from(context)
|
|
||||||
if (manager.getNotificationChannel(CHANNEL_ID) == null) {
|
|
||||||
val channel = NotificationChannel(
|
|
||||||
CHANNEL_ID,
|
|
||||||
context.getString(R.string.downloads),
|
|
||||||
NotificationManager.IMPORTANCE_LOW,
|
|
||||||
)
|
|
||||||
channel.enableVibration(false)
|
|
||||||
channel.enableLights(false)
|
|
||||||
channel.setSound(null, null)
|
|
||||||
manager.createNotificationChannel(channel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,262 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.download.ui.service
|
|
||||||
|
|
||||||
import android.app.DownloadManager.ACTION_DOWNLOAD_COMPLETE
|
|
||||||
import android.content.BroadcastReceiver
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.content.IntentFilter
|
|
||||||
import android.os.Binder
|
|
||||||
import android.os.IBinder
|
|
||||||
import android.os.PowerManager
|
|
||||||
import android.view.View
|
|
||||||
import androidx.annotation.MainThread
|
|
||||||
import androidx.core.app.ServiceCompat
|
|
||||||
import androidx.core.content.ContextCompat
|
|
||||||
import androidx.lifecycle.DefaultLifecycleObserver
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
||||||
import com.google.android.material.snackbar.Snackbar
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
import kotlinx.coroutines.flow.transformWhile
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import org.koitharu.kotatsu.BuildConfig
|
|
||||||
import org.koitharu.kotatsu.R
|
|
||||||
import org.koitharu.kotatsu.base.ui.BaseService
|
|
||||||
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
|
|
||||||
import org.koitharu.kotatsu.download.domain.DownloadManager
|
|
||||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
|
||||||
import org.koitharu.kotatsu.download.ui.DownloadsActivity
|
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
|
||||||
import org.koitharu.kotatsu.utils.ext.getParcelableExtraCompat
|
|
||||||
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
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import javax.inject.Inject
|
|
||||||
import kotlin.collections.set
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class DownloadService : BaseService() {
|
|
||||||
|
|
||||||
private lateinit var downloadNotification: DownloadNotification
|
|
||||||
private lateinit var wakeLock: PowerManager.WakeLock
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var downloadManager: DownloadManager
|
|
||||||
|
|
||||||
private val jobs = LinkedHashMap<Int, PausingProgressJob<DownloadState>>()
|
|
||||||
private val jobCount = MutableStateFlow(0)
|
|
||||||
private val controlReceiver = ControlReceiver()
|
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
downloadNotification = DownloadNotification(this)
|
|
||||||
wakeLock = (applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager)
|
|
||||||
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "kotatsu:downloading")
|
|
||||||
wakeLock.acquire(TimeUnit.HOURS.toMillis(8))
|
|
||||||
DownloadNotification.createChannel(this)
|
|
||||||
startForeground(DownloadNotification.ID_GROUP, downloadNotification.buildGroupNotification())
|
|
||||||
val intentFilter = IntentFilter()
|
|
||||||
intentFilter.addAction(ACTION_DOWNLOAD_CANCEL)
|
|
||||||
intentFilter.addAction(ACTION_DOWNLOAD_RESUME)
|
|
||||||
ContextCompat.registerReceiver(this, controlReceiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
||||||
super.onStartCommand(intent, flags, startId)
|
|
||||||
val manga = intent?.getParcelableExtraCompat<ParcelableManga>(EXTRA_MANGA)?.manga
|
|
||||||
val chapters = intent?.getLongArrayExtra(EXTRA_CHAPTERS_IDS)
|
|
||||||
return if (manga != null) {
|
|
||||||
jobs[startId] = downloadManga(startId, manga, chapters)
|
|
||||||
jobCount.value = jobs.size
|
|
||||||
START_REDELIVER_INTENT
|
|
||||||
} else {
|
|
||||||
stopSelfIfIdle()
|
|
||||||
START_NOT_STICKY
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder {
|
|
||||||
super.onBind(intent)
|
|
||||||
return DownloadBinder(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
unregisterReceiver(controlReceiver)
|
|
||||||
if (wakeLock.isHeld) {
|
|
||||||
wakeLock.release()
|
|
||||||
}
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun downloadManga(
|
|
||||||
startId: Int,
|
|
||||||
manga: Manga,
|
|
||||||
chaptersIds: LongArray?,
|
|
||||||
): PausingProgressJob<DownloadState> {
|
|
||||||
val job = downloadManager.downloadManga(manga, chaptersIds, startId)
|
|
||||||
listenJob(job)
|
|
||||||
return job
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun listenJob(job: ProgressJob<DownloadState>) {
|
|
||||||
lifecycleScope.launch {
|
|
||||||
val startId = job.progressValue.startId
|
|
||||||
val notificationItem = downloadNotification.newItem(startId)
|
|
||||||
try {
|
|
||||||
val timeLeftEstimator = TimeLeftEstimator()
|
|
||||||
notificationItem.notify(job.progressValue, -1L)
|
|
||||||
job.progressAsFlow()
|
|
||||||
.onEach { state ->
|
|
||||||
if (state is DownloadState.Progress) {
|
|
||||||
timeLeftEstimator.tick(value = state.progress, total = state.max)
|
|
||||||
} else {
|
|
||||||
timeLeftEstimator.emptyTick()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.throttle { state -> if (state is DownloadState.Progress) 400L else 0L }
|
|
||||||
.whileActive()
|
|
||||||
.collect { state ->
|
|
||||||
val timeLeft = timeLeftEstimator.getEstimatedTimeLeft()
|
|
||||||
notificationItem.notify(state, timeLeft)
|
|
||||||
}
|
|
||||||
job.join()
|
|
||||||
} finally {
|
|
||||||
(job.progressValue as? DownloadState.Done)?.let {
|
|
||||||
sendBroadcast(
|
|
||||||
Intent(ACTION_DOWNLOAD_COMPLETE)
|
|
||||||
.putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (job.isCancelled) {
|
|
||||||
notificationItem.dismiss()
|
|
||||||
if (jobs.remove(startId) != null) {
|
|
||||||
jobCount.value = jobs.size
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
notificationItem.notify(job.progressValue, -1L)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.invokeOnCompletion {
|
|
||||||
stopSelfIfIdle()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Flow<DownloadState>.whileActive(): Flow<DownloadState> = transformWhile { state ->
|
|
||||||
emit(state)
|
|
||||||
!state.isTerminal
|
|
||||||
}
|
|
||||||
|
|
||||||
@MainThread
|
|
||||||
private fun stopSelfIfIdle() {
|
|
||||||
if (jobs.any { (_, job) -> job.isActive }) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
downloadNotification.detach()
|
|
||||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
|
||||||
stopSelf()
|
|
||||||
}
|
|
||||||
|
|
||||||
inner class ControlReceiver : BroadcastReceiver() {
|
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent?) {
|
|
||||||
when (intent?.action) {
|
|
||||||
ACTION_DOWNLOAD_CANCEL -> {
|
|
||||||
val cancelId = intent.getIntExtra(EXTRA_CANCEL_ID, 0)
|
|
||||||
jobs[cancelId]?.cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
ACTION_DOWNLOAD_RESUME -> {
|
|
||||||
val cancelId = intent.getIntExtra(EXTRA_CANCEL_ID, 0)
|
|
||||||
jobs[cancelId]?.resume()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DownloadBinder(service: DownloadService) : Binder(), DefaultLifecycleObserver {
|
|
||||||
|
|
||||||
private var downloadsStateFlow = MutableStateFlow<List<PausingProgressJob<DownloadState>>>(emptyList())
|
|
||||||
|
|
||||||
init {
|
|
||||||
service.lifecycle.addObserver(this)
|
|
||||||
service.jobCount.onEach {
|
|
||||||
downloadsStateFlow.value = service.jobs.values.toList()
|
|
||||||
}.launchIn(service.lifecycleScope)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy(owner: LifecycleOwner) {
|
|
||||||
owner.lifecycle.removeObserver(this)
|
|
||||||
downloadsStateFlow.value = emptyList()
|
|
||||||
super.onDestroy(owner)
|
|
||||||
}
|
|
||||||
|
|
||||||
val downloads
|
|
||||||
get() = downloadsStateFlow.asStateFlow()
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
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"
|
|
||||||
|
|
||||||
const val EXTRA_MANGA = "manga"
|
|
||||||
private const val EXTRA_CHAPTERS_IDS = "chapters_ids"
|
|
||||||
private const val EXTRA_CANCEL_ID = "cancel_id"
|
|
||||||
|
|
||||||
fun start(view: View, manga: Manga, chaptersIds: Collection<Long>? = null) {
|
|
||||||
if (chaptersIds?.isEmpty() == true) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val intent = Intent(view.context, DownloadService::class.java)
|
|
||||||
intent.putExtra(EXTRA_MANGA, ParcelableManga(manga, withChapters = false))
|
|
||||||
if (chaptersIds != null) {
|
|
||||||
intent.putExtra(EXTRA_CHAPTERS_IDS, chaptersIds.toLongArray())
|
|
||||||
}
|
|
||||||
ContextCompat.startForegroundService(view.context, intent)
|
|
||||||
showStartedSnackbar(view)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun start(view: View, manga: Collection<Manga>) {
|
|
||||||
if (manga.isEmpty()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for (item in manga) {
|
|
||||||
val intent = Intent(view.context, DownloadService::class.java)
|
|
||||||
intent.putExtra(EXTRA_MANGA, ParcelableManga(item, withChapters = false))
|
|
||||||
ContextCompat.startForegroundService(view.context, intent)
|
|
||||||
}
|
|
||||||
showStartedSnackbar(view)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun confirmAndStart(view: View, items: Set<Manga>) {
|
|
||||||
MaterialAlertDialogBuilder(view.context)
|
|
||||||
.setTitle(R.string.save_manga)
|
|
||||||
.setMessage(R.string.batch_manga_save_confirm)
|
|
||||||
.setNegativeButton(android.R.string.cancel, null)
|
|
||||||
.setPositiveButton(R.string.save) { _, _ ->
|
|
||||||
start(view, items)
|
|
||||||
}.show()
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
private fun showStartedSnackbar(view: View) {
|
|
||||||
Snackbar.make(view, R.string.download_started, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction(R.string.details) {
|
|
||||||
it.context.startActivity(DownloadsActivity.newIntent(it.context))
|
|
||||||
}.show()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,260 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.worker
|
||||||
|
|
||||||
|
import android.app.Notification
|
||||||
|
import android.app.NotificationChannel
|
||||||
|
import android.app.NotificationManager
|
||||||
|
import android.app.PendingIntent
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
|
import android.os.Build
|
||||||
|
import android.text.format.DateUtils
|
||||||
|
import androidx.core.app.NotificationCompat
|
||||||
|
import androidx.core.app.NotificationManagerCompat
|
||||||
|
import androidx.core.app.PendingIntentCompat
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.graphics.drawable.toBitmap
|
||||||
|
import androidx.work.WorkManager
|
||||||
|
import coil.ImageLoader
|
||||||
|
import coil.request.ImageRequest
|
||||||
|
import coil.size.Scale
|
||||||
|
import dagger.assisted.Assisted
|
||||||
|
import dagger.assisted.AssistedFactory
|
||||||
|
import dagger.assisted.AssistedInject
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.details.ui.DetailsActivity
|
||||||
|
import org.koitharu.kotatsu.download.domain.DownloadState
|
||||||
|
import org.koitharu.kotatsu.download.ui.list.DownloadsActivity
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
|
import org.koitharu.kotatsu.parsers.util.format
|
||||||
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||||
|
import org.koitharu.kotatsu.search.ui.MangaListActivity
|
||||||
|
import org.koitharu.kotatsu.utils.ext.getDrawableOrThrow
|
||||||
|
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||||
|
import java.util.UUID
|
||||||
|
import com.google.android.material.R as materialR
|
||||||
|
|
||||||
|
private const val CHANNEL_ID = "download"
|
||||||
|
private const val GROUP_ID = "downloads"
|
||||||
|
|
||||||
|
class DownloadNotificationFactory @AssistedInject constructor(
|
||||||
|
@ApplicationContext private val context: Context,
|
||||||
|
private val coil: ImageLoader,
|
||||||
|
@Assisted private val uuid: UUID,
|
||||||
|
) {
|
||||||
|
|
||||||
|
private val covers = HashMap<Manga, Drawable>()
|
||||||
|
private val builder = NotificationCompat.Builder(context, CHANNEL_ID)
|
||||||
|
private val mutex = Mutex()
|
||||||
|
|
||||||
|
private val coverWidth = context.resources.getDimensionPixelSize(
|
||||||
|
androidx.core.R.dimen.compat_notification_large_icon_max_width,
|
||||||
|
)
|
||||||
|
private val coverHeight = context.resources.getDimensionPixelSize(
|
||||||
|
androidx.core.R.dimen.compat_notification_large_icon_max_height,
|
||||||
|
)
|
||||||
|
private val queueIntent = PendingIntentCompat.getActivity(
|
||||||
|
context,
|
||||||
|
0,
|
||||||
|
DownloadsActivity.newIntent(context),
|
||||||
|
0,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
|
||||||
|
private val actionCancel by lazy {
|
||||||
|
NotificationCompat.Action(
|
||||||
|
materialR.drawable.material_ic_clear_black_24dp,
|
||||||
|
context.getString(android.R.string.cancel),
|
||||||
|
WorkManager.getInstance(context).createCancelPendingIntent(uuid),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val actionPause by lazy {
|
||||||
|
NotificationCompat.Action(
|
||||||
|
R.drawable.ic_action_pause,
|
||||||
|
context.getString(R.string.pause),
|
||||||
|
PausingReceiver.createPausePendingIntent(context, uuid),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val actionResume by lazy {
|
||||||
|
NotificationCompat.Action(
|
||||||
|
R.drawable.ic_action_resume,
|
||||||
|
context.getString(R.string.resume),
|
||||||
|
PausingReceiver.createResumePendingIntent(context, uuid),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
createChannel()
|
||||||
|
builder.setOnlyAlertOnce(true)
|
||||||
|
builder.setDefaults(0)
|
||||||
|
builder.color = ContextCompat.getColor(context, R.color.blue_primary)
|
||||||
|
builder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
||||||
|
builder.setSilent(true)
|
||||||
|
builder.setGroup(GROUP_ID)
|
||||||
|
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
|
||||||
|
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun create(state: DownloadState?): Notification = mutex.withLock {
|
||||||
|
builder.setContentTitle(state?.manga?.title ?: context.getString(R.string.preparing_))
|
||||||
|
builder.setContentText(context.getString(R.string.manga_downloading_))
|
||||||
|
builder.setProgress(1, 0, true)
|
||||||
|
builder.setSmallIcon(android.R.drawable.stat_sys_download)
|
||||||
|
builder.setContentIntent(queueIntent)
|
||||||
|
builder.setStyle(null)
|
||||||
|
builder.setLargeIcon(if (state != null) getCover(state.manga)?.toBitmap() else null)
|
||||||
|
builder.clearActions()
|
||||||
|
builder.setSubText(null)
|
||||||
|
builder.setShowWhen(false)
|
||||||
|
builder.setVisibility(
|
||||||
|
if (state != null && state.manga.isNsfw) {
|
||||||
|
NotificationCompat.VISIBILITY_PRIVATE
|
||||||
|
} else {
|
||||||
|
NotificationCompat.VISIBILITY_PUBLIC
|
||||||
|
},
|
||||||
|
)
|
||||||
|
when {
|
||||||
|
state == null -> Unit
|
||||||
|
state.localManga != null -> { // downloaded, final state
|
||||||
|
builder.setProgress(0, 0, false)
|
||||||
|
builder.setContentText(context.getString(R.string.download_complete))
|
||||||
|
builder.setContentIntent(createMangaIntent(context, state.localManga.manga))
|
||||||
|
builder.setAutoCancel(true)
|
||||||
|
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
||||||
|
builder.setCategory(null)
|
||||||
|
builder.setStyle(null)
|
||||||
|
builder.setOngoing(false)
|
||||||
|
builder.setShowWhen(true)
|
||||||
|
builder.setWhen(System.currentTimeMillis())
|
||||||
|
}
|
||||||
|
|
||||||
|
state.isStopped -> {
|
||||||
|
builder.setProgress(0, 0, false)
|
||||||
|
builder.setContentText(context.getString(R.string.queued))
|
||||||
|
builder.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||||
|
builder.setStyle(null)
|
||||||
|
builder.setOngoing(true)
|
||||||
|
builder.setSmallIcon(R.drawable.ic_stat_paused)
|
||||||
|
builder.addAction(actionCancel)
|
||||||
|
}
|
||||||
|
|
||||||
|
state.isPaused -> { // paused (with error or manually)
|
||||||
|
builder.setProgress(state.max, state.progress, false)
|
||||||
|
val percent = if (state.percent >= 0) {
|
||||||
|
context.getString(R.string.percent_string_pattern, (state.percent * 100).format())
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
if (state.error != null) {
|
||||||
|
builder.setContentText(state.error)
|
||||||
|
builder.setSubText(percent)
|
||||||
|
} else {
|
||||||
|
builder.setContentText(percent)
|
||||||
|
}
|
||||||
|
builder.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||||
|
builder.setStyle(null)
|
||||||
|
builder.setOngoing(true)
|
||||||
|
builder.setSmallIcon(R.drawable.ic_stat_paused)
|
||||||
|
builder.addAction(actionCancel)
|
||||||
|
builder.addAction(actionResume)
|
||||||
|
}
|
||||||
|
|
||||||
|
state.error != null -> { // error, final state
|
||||||
|
builder.setProgress(0, 0, false)
|
||||||
|
builder.setSmallIcon(android.R.drawable.stat_notify_error)
|
||||||
|
builder.setSubText(context.getString(R.string.error))
|
||||||
|
builder.setContentText(state.error)
|
||||||
|
builder.setAutoCancel(true)
|
||||||
|
builder.setOngoing(false)
|
||||||
|
builder.setCategory(NotificationCompat.CATEGORY_ERROR)
|
||||||
|
builder.setShowWhen(true)
|
||||||
|
builder.setWhen(System.currentTimeMillis())
|
||||||
|
builder.setStyle(NotificationCompat.BigTextStyle().bigText(state.error))
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
builder.setProgress(state.max, state.progress, false)
|
||||||
|
val percent = if (state.percent >= 0f) {
|
||||||
|
context.getString(R.string.percent_string_pattern, (state.percent * 100).format())
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
if (state.eta > 0L) {
|
||||||
|
val eta = DateUtils.getRelativeTimeSpanString(
|
||||||
|
state.eta,
|
||||||
|
System.currentTimeMillis(),
|
||||||
|
DateUtils.SECOND_IN_MILLIS,
|
||||||
|
)
|
||||||
|
builder.setContentText(eta)
|
||||||
|
builder.setSubText(percent)
|
||||||
|
} else {
|
||||||
|
builder.setContentText(percent)
|
||||||
|
}
|
||||||
|
builder.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||||
|
builder.setStyle(null)
|
||||||
|
builder.setOngoing(true)
|
||||||
|
builder.addAction(actionCancel)
|
||||||
|
builder.addAction(actionPause)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createMangaIntent(context: Context, manga: Manga?) = PendingIntentCompat.getActivity(
|
||||||
|
context,
|
||||||
|
manga.hashCode(),
|
||||||
|
if (manga != null) {
|
||||||
|
DetailsActivity.newIntent(context, manga)
|
||||||
|
} else {
|
||||||
|
MangaListActivity.newIntent(context, MangaSource.LOCAL)
|
||||||
|
},
|
||||||
|
PendingIntent.FLAG_CANCEL_CURRENT,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
|
||||||
|
private suspend fun getCover(manga: Manga) = covers[manga] ?: run {
|
||||||
|
runCatchingCancellable {
|
||||||
|
coil.execute(
|
||||||
|
ImageRequest.Builder(context)
|
||||||
|
.data(manga.coverUrl)
|
||||||
|
.allowHardware(false)
|
||||||
|
.tag(manga.source)
|
||||||
|
.size(coverWidth, coverHeight)
|
||||||
|
.scale(Scale.FILL)
|
||||||
|
.build(),
|
||||||
|
).getDrawableOrThrow()
|
||||||
|
}.onSuccess {
|
||||||
|
covers[manga] = it
|
||||||
|
}.onFailure {
|
||||||
|
it.printStackTraceDebug()
|
||||||
|
}.getOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createChannel() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
val manager = NotificationManagerCompat.from(context)
|
||||||
|
if (manager.getNotificationChannel(CHANNEL_ID) == null) {
|
||||||
|
val channel = NotificationChannel(
|
||||||
|
CHANNEL_ID,
|
||||||
|
context.getString(R.string.downloads),
|
||||||
|
NotificationManager.IMPORTANCE_LOW,
|
||||||
|
)
|
||||||
|
channel.enableVibration(false)
|
||||||
|
channel.enableLights(false)
|
||||||
|
channel.setSound(null, null)
|
||||||
|
manager.createNotificationChannel(channel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@AssistedFactory
|
||||||
|
interface Factory {
|
||||||
|
|
||||||
|
fun create(uuid: UUID): DownloadNotificationFactory
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.worker
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import androidx.lifecycle.Observer
|
||||||
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.download.ui.list.DownloadsActivity
|
||||||
|
import org.koitharu.kotatsu.main.ui.owners.BottomNavOwner
|
||||||
|
import org.koitharu.kotatsu.utils.ext.findActivity
|
||||||
|
|
||||||
|
class DownloadStartedObserver(
|
||||||
|
private val snackbarHost: View,
|
||||||
|
) : Observer<Unit> {
|
||||||
|
|
||||||
|
override fun onChanged(value: Unit) {
|
||||||
|
val snackbar = Snackbar.make(snackbarHost, R.string.download_started, Snackbar.LENGTH_LONG)
|
||||||
|
(snackbarHost.context.findActivity() as? BottomNavOwner)?.let {
|
||||||
|
snackbar.anchorView = it.bottomNav
|
||||||
|
}
|
||||||
|
snackbar.setAction(R.string.details) {
|
||||||
|
it.context.startActivity(DownloadsActivity.newIntent(it.context))
|
||||||
|
}
|
||||||
|
snackbar.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,454 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.worker
|
||||||
|
|
||||||
|
import android.app.NotificationManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.webkit.MimeTypeMap
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.hilt.work.HiltWorker
|
||||||
|
import androidx.lifecycle.asFlow
|
||||||
|
import androidx.work.BackoffPolicy
|
||||||
|
import androidx.work.Constraints
|
||||||
|
import androidx.work.CoroutineWorker
|
||||||
|
import androidx.work.Data
|
||||||
|
import androidx.work.ForegroundInfo
|
||||||
|
import androidx.work.NetworkType
|
||||||
|
import androidx.work.OneTimeWorkRequestBuilder
|
||||||
|
import androidx.work.OutOfQuotaPolicy
|
||||||
|
import androidx.work.WorkInfo
|
||||||
|
import androidx.work.WorkManager
|
||||||
|
import androidx.work.WorkerParameters
|
||||||
|
import androidx.work.await
|
||||||
|
import dagger.Reusable
|
||||||
|
import dagger.assisted.Assisted
|
||||||
|
import dagger.assisted.AssistedInject
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
|
import kotlinx.coroutines.NonCancellable
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.internal.closeQuietly
|
||||||
|
import okio.IOException
|
||||||
|
import okio.buffer
|
||||||
|
import okio.sink
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||||
|
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.domain.DownloadState
|
||||||
|
import org.koitharu.kotatsu.local.data.LocalManga
|
||||||
|
import org.koitharu.kotatsu.local.data.LocalStorageChanges
|
||||||
|
import org.koitharu.kotatsu.local.data.PagesCache
|
||||||
|
import org.koitharu.kotatsu.local.data.input.LocalMangaInput
|
||||||
|
import org.koitharu.kotatsu.local.data.output.LocalMangaOutput
|
||||||
|
import org.koitharu.kotatsu.local.domain.LocalMangaRepository
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
|
import org.koitharu.kotatsu.parsers.util.await
|
||||||
|
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||||
|
import org.koitharu.kotatsu.utils.Throttler
|
||||||
|
import org.koitharu.kotatsu.utils.WorkManagerHelper
|
||||||
|
import org.koitharu.kotatsu.utils.ext.deleteAwait
|
||||||
|
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||||
|
import org.koitharu.kotatsu.utils.ext.ifNullOrEmpty
|
||||||
|
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||||
|
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||||
|
import org.koitharu.kotatsu.utils.ext.writeAllCancellable
|
||||||
|
import org.koitharu.kotatsu.utils.progress.TimeLeftEstimator
|
||||||
|
import java.io.File
|
||||||
|
import java.util.UUID
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltWorker
|
||||||
|
class DownloadWorker @AssistedInject constructor(
|
||||||
|
@Assisted appContext: Context,
|
||||||
|
@Assisted params: WorkerParameters,
|
||||||
|
private val okHttp: OkHttpClient,
|
||||||
|
private val cache: PagesCache,
|
||||||
|
private val localMangaRepository: LocalMangaRepository,
|
||||||
|
private val mangaDataRepository: MangaDataRepository,
|
||||||
|
private val settings: AppSettings,
|
||||||
|
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||||
|
@LocalStorageChanges private val localStorageChanges: MutableSharedFlow<LocalManga?>,
|
||||||
|
notificationFactoryFactory: DownloadNotificationFactory.Factory,
|
||||||
|
) : CoroutineWorker(appContext, params) {
|
||||||
|
|
||||||
|
private val notificationFactory = notificationFactoryFactory.create(params.id)
|
||||||
|
private val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var lastPublishedState: DownloadState? = null
|
||||||
|
private val currentState: DownloadState
|
||||||
|
get() = checkNotNull(lastPublishedState)
|
||||||
|
|
||||||
|
private val pausingHandle = PausingHandle()
|
||||||
|
private val timeLeftEstimator = TimeLeftEstimator()
|
||||||
|
private val notificationThrottler = Throttler(400)
|
||||||
|
private val pausingReceiver = PausingReceiver(params.id, pausingHandle)
|
||||||
|
|
||||||
|
override suspend fun doWork(): Result {
|
||||||
|
setForeground(getForegroundInfo())
|
||||||
|
val mangaId = inputData.getLong(MANGA_ID, 0L)
|
||||||
|
val manga = mangaDataRepository.findMangaById(mangaId) ?: return Result.failure()
|
||||||
|
val chaptersIds = inputData.getLongArray(CHAPTERS_IDS)?.takeUnless { it.isEmpty() }
|
||||||
|
val downloadedIds = getDoneChapters()
|
||||||
|
lastPublishedState = DownloadState(manga, isIndeterminate = true)
|
||||||
|
return try {
|
||||||
|
downloadMangaImpl(chaptersIds, downloadedIds)
|
||||||
|
Result.success(currentState.toWorkData())
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
withContext(NonCancellable) {
|
||||||
|
val notification = notificationFactory.create(currentState.copy(isStopped = true))
|
||||||
|
notificationManager.notify(id.hashCode(), notification)
|
||||||
|
}
|
||||||
|
throw e
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTraceDebug()
|
||||||
|
Result.retry()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTraceDebug()
|
||||||
|
Result.failure(
|
||||||
|
currentState.copy(
|
||||||
|
error = e.getDisplayMessage(applicationContext.resources),
|
||||||
|
eta = -1L,
|
||||||
|
).toWorkData(),
|
||||||
|
)
|
||||||
|
} finally {
|
||||||
|
notificationManager.cancel(id.hashCode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getForegroundInfo() = ForegroundInfo(
|
||||||
|
id.hashCode(),
|
||||||
|
notificationFactory.create(lastPublishedState),
|
||||||
|
)
|
||||||
|
|
||||||
|
private suspend fun downloadMangaImpl(
|
||||||
|
includedIds: LongArray?,
|
||||||
|
excludedIds: LongArray,
|
||||||
|
) {
|
||||||
|
var manga = currentState.manga
|
||||||
|
val chaptersToSkip = excludedIds.toMutableSet()
|
||||||
|
withMangaLock(manga) {
|
||||||
|
ContextCompat.registerReceiver(
|
||||||
|
applicationContext,
|
||||||
|
pausingReceiver,
|
||||||
|
PausingReceiver.createIntentFilter(id),
|
||||||
|
ContextCompat.RECEIVER_NOT_EXPORTED,
|
||||||
|
)
|
||||||
|
val destination = localMangaRepository.getOutputDir(manga)
|
||||||
|
checkNotNull(destination) { applicationContext.getString(R.string.cannot_find_available_storage) }
|
||||||
|
val tempFileName = "${manga.id}_$id.tmp"
|
||||||
|
var output: LocalMangaOutput? = null
|
||||||
|
try {
|
||||||
|
if (manga.source == MangaSource.LOCAL) {
|
||||||
|
manga = localMangaRepository.getRemoteManga(manga)
|
||||||
|
?: error("Cannot obtain remote manga instance")
|
||||||
|
}
|
||||||
|
val repo = mangaRepositoryFactory.create(manga.source)
|
||||||
|
val mangaDetails = if (manga.chapters.isNullOrEmpty()) repo.getDetails(manga) else manga
|
||||||
|
output = LocalMangaOutput.getOrCreate(destination, mangaDetails)
|
||||||
|
val coverUrl = mangaDetails.largeCoverUrl.ifNullOrEmpty { mangaDetails.coverUrl }
|
||||||
|
if (coverUrl.isNotEmpty()) {
|
||||||
|
downloadFile(coverUrl, destination, tempFileName, repo.source).let { file ->
|
||||||
|
output.addCover(file, MimeTypeMap.getFileExtensionFromUrl(coverUrl))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val chapters = getChapters(mangaDetails, includedIds)
|
||||||
|
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
||||||
|
if (chaptersToSkip.remove(chapter.id)) {
|
||||||
|
publishState(
|
||||||
|
currentState.copy(
|
||||||
|
downloadedChapters = currentState.downloadedChapters + chapter.id,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val pages = runFailsafe(pausingHandle) {
|
||||||
|
repo.getPages(chapter)
|
||||||
|
}
|
||||||
|
for ((pageIndex, page) in pages.withIndex()) {
|
||||||
|
runFailsafe(pausingHandle) {
|
||||||
|
val url = repo.getPageUrl(page)
|
||||||
|
val file = cache.get(url)
|
||||||
|
?: downloadFile(url, destination, tempFileName, repo.source)
|
||||||
|
output.addPage(
|
||||||
|
chapter = chapter,
|
||||||
|
file = file,
|
||||||
|
pageNumber = pageIndex,
|
||||||
|
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
publishState(
|
||||||
|
currentState.copy(
|
||||||
|
totalChapters = chapters.size,
|
||||||
|
currentChapter = chapterIndex,
|
||||||
|
totalPages = pages.size,
|
||||||
|
currentPage = pageIndex,
|
||||||
|
isIndeterminate = false,
|
||||||
|
eta = timeLeftEstimator.getEta(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
if (settings.isDownloadsSlowdownEnabled) {
|
||||||
|
delay(SLOWDOWN_DELAY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (output.flushChapter(chapter)) {
|
||||||
|
runCatchingCancellable {
|
||||||
|
localStorageChanges.emit(LocalMangaInput.of(output.rootFile).getManga())
|
||||||
|
}.onFailure(Throwable::printStackTraceDebug)
|
||||||
|
}
|
||||||
|
publishState(
|
||||||
|
currentState.copy(
|
||||||
|
downloadedChapters = currentState.downloadedChapters + chapter.id,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
publishState(currentState.copy(isIndeterminate = true, eta = -1L))
|
||||||
|
output.mergeWithExisting()
|
||||||
|
output.finish()
|
||||||
|
val localManga = LocalMangaInput.of(output.rootFile).getManga()
|
||||||
|
localStorageChanges.emit(localManga)
|
||||||
|
publishState(currentState.copy(localManga = localManga, eta = -1L))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
if (e !is CancellationException) {
|
||||||
|
publishState(currentState.copy(error = e.getDisplayMessage(applicationContext.resources)))
|
||||||
|
}
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
|
withContext(NonCancellable) {
|
||||||
|
applicationContext.unregisterReceiver(pausingReceiver)
|
||||||
|
output?.closeQuietly()
|
||||||
|
output?.cleanup()
|
||||||
|
File(destination, tempFileName).deleteAwait()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun <R> runFailsafe(
|
||||||
|
pausingHandle: PausingHandle,
|
||||||
|
block: suspend () -> R,
|
||||||
|
): R {
|
||||||
|
if (pausingHandle.isPaused) {
|
||||||
|
publishState(currentState.copy(isPaused = true, eta = -1L))
|
||||||
|
pausingHandle.awaitResumed()
|
||||||
|
publishState(currentState.copy(isPaused = false))
|
||||||
|
}
|
||||||
|
var countDown = MAX_FAILSAFE_ATTEMPTS
|
||||||
|
failsafe@ while (true) {
|
||||||
|
try {
|
||||||
|
return block()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
if (countDown <= 0) {
|
||||||
|
publishState(
|
||||||
|
currentState.copy(
|
||||||
|
isPaused = true,
|
||||||
|
error = e.getDisplayMessage(applicationContext.resources),
|
||||||
|
eta = -1L,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
countDown = MAX_FAILSAFE_ATTEMPTS
|
||||||
|
pausingHandle.pause()
|
||||||
|
pausingHandle.awaitResumed()
|
||||||
|
publishState(currentState.copy(isPaused = false, error = null))
|
||||||
|
} else {
|
||||||
|
countDown--
|
||||||
|
delay(DOWNLOAD_ERROR_DELAY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun downloadFile(
|
||||||
|
url: String,
|
||||||
|
destination: File,
|
||||||
|
tempFileName: String,
|
||||||
|
source: MangaSource,
|
||||||
|
): File {
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.tag(MangaSource::class.java, source)
|
||||||
|
.header(CommonHeaders.ACCEPT, "image/webp,image/png;q=0.9,image/jpeg,*/*;q=0.8")
|
||||||
|
.cacheControl(CommonHeaders.CACHE_CONTROL_NO_STORE)
|
||||||
|
.get()
|
||||||
|
.build()
|
||||||
|
val call = okHttp.newCall(request)
|
||||||
|
val file = File(destination, tempFileName)
|
||||||
|
val response = call.clone().await()
|
||||||
|
checkNotNull(response.body).use { body ->
|
||||||
|
file.sink(append = false).buffer().use {
|
||||||
|
it.writeAllCancellable(body.source())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun publishState(state: DownloadState) {
|
||||||
|
val previousState = currentState
|
||||||
|
lastPublishedState = state
|
||||||
|
if (previousState.isParticularProgress && state.isParticularProgress) {
|
||||||
|
timeLeftEstimator.tick(state.progress, state.max)
|
||||||
|
} else {
|
||||||
|
timeLeftEstimator.emptyTick()
|
||||||
|
notificationThrottler.reset()
|
||||||
|
}
|
||||||
|
val notification = notificationFactory.create(state)
|
||||||
|
if (state.isFinalState) {
|
||||||
|
notificationManager.notify(id.toString(), id.hashCode(), notification)
|
||||||
|
} else if (notificationThrottler.throttle()) {
|
||||||
|
notificationManager.notify(id.hashCode(), notification)
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setProgress(state.toWorkData())
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getDoneChapters(): LongArray {
|
||||||
|
val work = WorkManagerHelper(WorkManager.getInstance(applicationContext)).getWorkInfoById(id)
|
||||||
|
?: return LongArray(0)
|
||||||
|
return DownloadState.getDownloadedChapters(work.progress)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getChapters(
|
||||||
|
manga: Manga,
|
||||||
|
includedIds: LongArray?,
|
||||||
|
): List<MangaChapter> {
|
||||||
|
val chapters = checkNotNull(manga.chapters?.toMutableList()) {
|
||||||
|
"Chapters list must not be null"
|
||||||
|
}
|
||||||
|
if (includedIds != null) {
|
||||||
|
val chaptersIdsSet = includedIds.toMutableSet()
|
||||||
|
chapters.retainAll { x -> chaptersIdsSet.remove(x.id) }
|
||||||
|
check(chaptersIdsSet.isEmpty()) {
|
||||||
|
"${chaptersIdsSet.size} of ${includedIds.size} requested chapters not found in manga"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check(chapters.isNotEmpty()) { "Chapters list must not be empty" }
|
||||||
|
return chapters
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend inline fun <T> withMangaLock(manga: Manga, block: () -> T) = try {
|
||||||
|
localMangaRepository.lockManga(manga.id)
|
||||||
|
block()
|
||||||
|
} finally {
|
||||||
|
localMangaRepository.unlockManga(manga.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Reusable
|
||||||
|
class Scheduler @Inject constructor(
|
||||||
|
@ApplicationContext private val context: Context,
|
||||||
|
private val dataRepository: MangaDataRepository,
|
||||||
|
private val settings: AppSettings,
|
||||||
|
) {
|
||||||
|
|
||||||
|
private val workManager: WorkManager
|
||||||
|
inline get() = WorkManager.getInstance(context)
|
||||||
|
|
||||||
|
suspend fun schedule(manga: Manga, chaptersIds: Collection<Long>?) {
|
||||||
|
dataRepository.storeManga(manga)
|
||||||
|
val data = Data.Builder()
|
||||||
|
.putLong(MANGA_ID, manga.id)
|
||||||
|
if (!chaptersIds.isNullOrEmpty()) {
|
||||||
|
data.putLongArray(CHAPTERS_IDS, chaptersIds.toLongArray())
|
||||||
|
}
|
||||||
|
scheduleImpl(listOf(data.build()))
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun schedule(manga: Collection<Manga>) {
|
||||||
|
val data = manga.map {
|
||||||
|
dataRepository.storeManga(it)
|
||||||
|
Data.Builder()
|
||||||
|
.putLong(MANGA_ID, it.id)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
scheduleImpl(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun observeWorks(): Flow<List<WorkInfo>> = workManager
|
||||||
|
.getWorkInfosByTagLiveData(TAG)
|
||||||
|
.asFlow()
|
||||||
|
|
||||||
|
suspend fun cancel(id: UUID) {
|
||||||
|
workManager.cancelWorkById(id).await()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun cancelAll() {
|
||||||
|
workManager.cancelAllWorkByTag(TAG).await()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pause(id: UUID) {
|
||||||
|
val intent = PausingReceiver.getPauseIntent(id)
|
||||||
|
context.sendBroadcast(intent)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resume(id: UUID) {
|
||||||
|
val intent = PausingReceiver.getResumeIntent(id)
|
||||||
|
context.sendBroadcast(intent)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun delete(id: UUID) {
|
||||||
|
WorkManagerHelper(workManager).deleteWork(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun removeCompleted() {
|
||||||
|
val helper = WorkManagerHelper(workManager)
|
||||||
|
val finishedWorks = helper.getFinishedWorkInfosByTag(TAG)
|
||||||
|
helper.deleteWorks(finishedWorks.mapToSet { it.id })
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun updateConstraints() {
|
||||||
|
val constraints = Constraints.Builder()
|
||||||
|
.setRequiresStorageNotLow(true)
|
||||||
|
.setRequiredNetworkType(if (settings.isDownloadsWiFiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
|
||||||
|
.build()
|
||||||
|
val helper = WorkManagerHelper(workManager)
|
||||||
|
val works = helper.getWorkInfosByTag(TAG)
|
||||||
|
for (work in works) {
|
||||||
|
if (work.state.isFinished) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val request = OneTimeWorkRequestBuilder<DownloadWorker>()
|
||||||
|
.setConstraints(constraints)
|
||||||
|
.setId(work.id)
|
||||||
|
.build()
|
||||||
|
helper.updateWork(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun scheduleImpl(data: Collection<Data>) {
|
||||||
|
val constraints = Constraints.Builder()
|
||||||
|
.setRequiresStorageNotLow(true)
|
||||||
|
.setRequiredNetworkType(if (settings.isDownloadsWiFiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
|
||||||
|
.build()
|
||||||
|
val requests = data.map { inputData ->
|
||||||
|
OneTimeWorkRequestBuilder<DownloadWorker>()
|
||||||
|
.setConstraints(constraints)
|
||||||
|
.addTag(TAG)
|
||||||
|
.keepResultsForAtLeast(7, TimeUnit.DAYS)
|
||||||
|
.setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.SECONDS)
|
||||||
|
.setInputData(inputData)
|
||||||
|
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
workManager.enqueue(requests).await()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
|
||||||
|
const val MAX_FAILSAFE_ATTEMPTS = 2
|
||||||
|
const val DOWNLOAD_ERROR_DELAY = 500L
|
||||||
|
const val SLOWDOWN_DELAY = 100L
|
||||||
|
const val MANGA_ID = "manga_id"
|
||||||
|
const val CHAPTERS_IDS = "chapters"
|
||||||
|
const val TAG = "download"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.worker
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.IntentFilter
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.PatternMatcher
|
||||||
|
import androidx.core.app.PendingIntentCompat
|
||||||
|
import org.koitharu.kotatsu.utils.ext.toUUIDOrNull
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class PausingReceiver(
|
||||||
|
private val id: UUID,
|
||||||
|
private val pausingHandle: PausingHandle,
|
||||||
|
) : BroadcastReceiver() {
|
||||||
|
|
||||||
|
override fun onReceive(context: Context, intent: Intent?) {
|
||||||
|
val uuid = intent?.getStringExtra(EXTRA_UUID)?.toUUIDOrNull()
|
||||||
|
assert(uuid == id)
|
||||||
|
when (intent?.action) {
|
||||||
|
ACTION_RESUME -> pausingHandle.resume()
|
||||||
|
ACTION_PAUSE -> pausingHandle.pause()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val ACTION_PAUSE = "org.koitharu.kotatsu.download.PAUSE"
|
||||||
|
private const val ACTION_RESUME = "org.koitharu.kotatsu.download.RESUME"
|
||||||
|
private const val EXTRA_UUID = "uuid"
|
||||||
|
private const val SCHEME = "workuid"
|
||||||
|
|
||||||
|
fun createIntentFilter(id: UUID) = IntentFilter().apply {
|
||||||
|
addAction(ACTION_PAUSE)
|
||||||
|
addAction(ACTION_RESUME)
|
||||||
|
addDataScheme(SCHEME)
|
||||||
|
addDataPath(id.toString(), PatternMatcher.PATTERN_SIMPLE_GLOB)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getPauseIntent(id: UUID) = Intent(ACTION_PAUSE)
|
||||||
|
.setData(Uri.parse("$SCHEME://$id"))
|
||||||
|
.putExtra(EXTRA_UUID, id.toString())
|
||||||
|
|
||||||
|
fun getResumeIntent(id: UUID) = Intent(ACTION_RESUME)
|
||||||
|
.setData(Uri.parse("$SCHEME://$id"))
|
||||||
|
.putExtra(EXTRA_UUID, id.toString())
|
||||||
|
|
||||||
|
fun createPausePendingIntent(context: Context, id: UUID) = PendingIntentCompat.getBroadcast(
|
||||||
|
context,
|
||||||
|
0,
|
||||||
|
getPauseIntent(id),
|
||||||
|
0,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun createResumePendingIntent(context: Context, id: UUID) = PendingIntentCompat.getBroadcast(
|
||||||
|
context,
|
||||||
|
0,
|
||||||
|
getResumeIntent(id),
|
||||||
|
0,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package org.koitharu.kotatsu.utils
|
||||||
|
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.ensureActive
|
||||||
|
import okio.Buffer
|
||||||
|
import okio.ForwardingSource
|
||||||
|
import okio.Source
|
||||||
|
|
||||||
|
class CancellableSource(
|
||||||
|
private val job: Job?,
|
||||||
|
delegate: Source,
|
||||||
|
) : ForwardingSource(delegate) {
|
||||||
|
|
||||||
|
override fun read(sink: Buffer, byteCount: Long): Long {
|
||||||
|
job?.ensureActive()
|
||||||
|
return super.read(sink, byteCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,14 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.utils
|
|
||||||
|
|
||||||
import okhttp3.Interceptor
|
|
||||||
import okhttp3.Response
|
|
||||||
import org.koitharu.kotatsu.core.network.CommonHeaders.CONTENT_ENCODING
|
|
||||||
|
|
||||||
class GZipInterceptor : Interceptor {
|
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
|
||||||
val newRequest = chain.request().newBuilder()
|
|
||||||
newRequest.addHeader(CONTENT_ENCODING, "gzip")
|
|
||||||
return chain.proceed(newRequest.build())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package org.koitharu.kotatsu.utils
|
||||||
|
|
||||||
|
import android.os.SystemClock
|
||||||
|
|
||||||
|
class Throttler(
|
||||||
|
private val timeoutMs: Long,
|
||||||
|
) {
|
||||||
|
|
||||||
|
private var lastTick = 0L
|
||||||
|
|
||||||
|
fun throttle(): Boolean {
|
||||||
|
val now = SystemClock.elapsedRealtime()
|
||||||
|
return if (lastTick + timeoutMs <= now) {
|
||||||
|
lastTick = now
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun reset() {
|
||||||
|
lastTick = 0L
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
package org.koitharu.kotatsu.utils
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import androidx.work.WorkInfo
|
||||||
|
import androidx.work.WorkManager
|
||||||
|
import androidx.work.WorkQuery
|
||||||
|
import androidx.work.WorkRequest
|
||||||
|
import androidx.work.await
|
||||||
|
import androidx.work.impl.WorkManagerImpl
|
||||||
|
import java.util.UUID
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
import kotlin.coroutines.resumeWithException
|
||||||
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
|
@SuppressLint("RestrictedApi")
|
||||||
|
class WorkManagerHelper(
|
||||||
|
workManager: WorkManager,
|
||||||
|
) {
|
||||||
|
|
||||||
|
private val workManagerImpl = workManager as WorkManagerImpl
|
||||||
|
|
||||||
|
suspend fun deleteWork(id: UUID) = suspendCoroutine { cont ->
|
||||||
|
workManagerImpl.workTaskExecutor.executeOnTaskThread {
|
||||||
|
try {
|
||||||
|
workManagerImpl.workDatabase.workSpecDao().delete(id.toString())
|
||||||
|
cont.resume(Unit)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
cont.resumeWithException(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun deleteWorks(ids: Collection<UUID>) = suspendCoroutine { cont ->
|
||||||
|
workManagerImpl.workTaskExecutor.executeOnTaskThread {
|
||||||
|
try {
|
||||||
|
val db = workManagerImpl.workDatabase
|
||||||
|
db.runInTransaction {
|
||||||
|
for (id in ids) {
|
||||||
|
db.workSpecDao().delete(id.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cont.resume(Unit)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
cont.resumeWithException(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getWorkInfosByTag(tag: String): List<WorkInfo> {
|
||||||
|
return workManagerImpl.getWorkInfosByTag(tag).await()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getFinishedWorkInfosByTag(tag: String): List<WorkInfo> {
|
||||||
|
val query = WorkQuery.Builder.fromTags(listOf(tag))
|
||||||
|
.addStates(listOf(WorkInfo.State.SUCCEEDED, WorkInfo.State.CANCELLED, WorkInfo.State.FAILED))
|
||||||
|
.build()
|
||||||
|
return workManagerImpl.getWorkInfos(query).await()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getWorkInfoById(id: UUID): WorkInfo? {
|
||||||
|
return workManagerImpl.getWorkInfoById(id).await()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun updateWork(request: WorkRequest): WorkManager.UpdateResult {
|
||||||
|
return workManagerImpl.updateWork(request).await()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24"
|
||||||
|
android:tint="#FFFFFF">
|
||||||
|
<group android:scaleX="1.3320464"
|
||||||
|
android:scaleY="1.3320464"
|
||||||
|
android:translateX="-3.984556"
|
||||||
|
android:translateY="-3.984556">
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/white"
|
||||||
|
android:pathData="M6,19h4L10,5L6,5v14zM14,5v14h4L18,5h-4z"/>
|
||||||
|
</group>
|
||||||
|
</vector>
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 138 B |
Binary file not shown.
|
After Width: | Height: | Size: 144 B |
Binary file not shown.
|
After Width: | Height: | Size: 218 B |
Binary file not shown.
|
After Width: | Height: | Size: 534 B |
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="#000000"
|
||||||
|
android:pathData="M14,19H18V5H14M6,19H10V5H6V19Z" />
|
||||||
|
</vector>
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="#000000"
|
||||||
|
android:pathData="M8,5.14V19.14L19,12.14L8,5.14Z" />
|
||||||
|
</vector>
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_resume"
|
||||||
|
android:icon="@drawable/ic_action_resume"
|
||||||
|
android:title="@string/resume"
|
||||||
|
app:showAsAction="ifRoom|withText" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_pause"
|
||||||
|
android:icon="@drawable/ic_action_pause"
|
||||||
|
android:title="@string/pause"
|
||||||
|
app:showAsAction="ifRoom|withText" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_cancel"
|
||||||
|
android:icon="@drawable/abc_ic_clear_material"
|
||||||
|
android:title="@android:string/cancel"
|
||||||
|
app:showAsAction="ifRoom|withText" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_remove"
|
||||||
|
android:icon="@drawable/ic_delete"
|
||||||
|
android:title="@string/remove"
|
||||||
|
app:showAsAction="ifRoom|withText" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_select_all"
|
||||||
|
android:icon="?actionModeSelectAllDrawable"
|
||||||
|
android:title="@android:string/selectAll"
|
||||||
|
app:showAsAction="ifRoom|withText" />
|
||||||
|
|
||||||
|
</menu>
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_pause"
|
||||||
|
android:icon="@drawable/ic_action_pause"
|
||||||
|
android:title="@string/pause"
|
||||||
|
android:visible="false"
|
||||||
|
app:showAsAction="ifRoom|withText" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_resume"
|
||||||
|
android:icon="@drawable/ic_action_resume"
|
||||||
|
android:title="@string/resume"
|
||||||
|
android:visible="false"
|
||||||
|
app:showAsAction="ifRoom|withText" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_cancel_all"
|
||||||
|
android:title="@string/cancel_all"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_remove_completed"
|
||||||
|
android:title="@string/remove_completed"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
</menu>
|
||||||
Loading…
Reference in New Issue