Use WorkManager for downloads
parent
78f417ebe1
commit
f05bb20428
@ -0,0 +1,48 @@
|
|||||||
|
package org.koitharu.kotatsu.download.domain
|
||||||
|
|
||||||
|
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 java.util.UUID
|
||||||
|
|
||||||
|
data class DownloadState2(
|
||||||
|
val id: UUID,
|
||||||
|
val manga: Manga,
|
||||||
|
val state: State,
|
||||||
|
val error: Throwable? = null,
|
||||||
|
val totalChapters: Int = 0,
|
||||||
|
val currentChapter: Int = 0,
|
||||||
|
val totalPages: Int = 0,
|
||||||
|
val currentPage: Int = 0,
|
||||||
|
val timeLeft: Long = -1L,
|
||||||
|
val localManga: LocalManga? = null,
|
||||||
|
) {
|
||||||
|
|
||||||
|
val isTerminal: Boolean
|
||||||
|
get() = state == State.FAILED || state == State.CANCELLED || state == State.DONE
|
||||||
|
|
||||||
|
val max: Int = totalChapters * totalPages
|
||||||
|
|
||||||
|
val progress: Int = totalPages * currentChapter + currentPage + 1
|
||||||
|
|
||||||
|
val percent: Float = if (max > 0) progress.toFloat() / max else PROGRESS_NONE
|
||||||
|
|
||||||
|
fun toWorkData() = Data.Builder()
|
||||||
|
.putString(DATA_UUID, id.toString())
|
||||||
|
.putLong(DATA_MANGA_ID, manga.id)
|
||||||
|
.putString(DATA_STATE, state.name)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
enum class State {
|
||||||
|
|
||||||
|
PREPARING, PROGRESS, PAUSED, FAILED, CANCELLED, DONE
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val DATA_UUID = "uuid"
|
||||||
|
private const val DATA_MANGA_ID = "manga_id"
|
||||||
|
private const val DATA_STATE = "state"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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,220 @@
|
|||||||
|
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.Reusable
|
||||||
|
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.DownloadState2
|
||||||
|
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.format
|
||||||
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||||
|
import org.koitharu.kotatsu.search.ui.MangaListActivity
|
||||||
|
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||||
|
import org.koitharu.kotatsu.utils.ext.getDrawableOrThrow
|
||||||
|
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||||
|
import java.util.UUID
|
||||||
|
import javax.inject.Inject
|
||||||
|
import com.google.android.material.R as materialR
|
||||||
|
|
||||||
|
private const val CHANNEL_ID = "download"
|
||||||
|
private const val GROUP_ID = "downloads"
|
||||||
|
|
||||||
|
@Reusable
|
||||||
|
class DownloadNotificationFactory @Inject constructor(
|
||||||
|
@ApplicationContext private val context: Context,
|
||||||
|
private val coil: ImageLoader,
|
||||||
|
) {
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun create(state: DownloadState2?): 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?.state) {
|
||||||
|
null -> Unit
|
||||||
|
DownloadState2.State.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
|
||||||
|
}
|
||||||
|
|
||||||
|
DownloadState2.State.DONE -> {
|
||||||
|
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())
|
||||||
|
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||||
|
}
|
||||||
|
|
||||||
|
DownloadState2.State.FAILED -> {
|
||||||
|
val message = state.error?.getDisplayMessage(context.resources)
|
||||||
|
?: context.getString(R.string.error_occurred)
|
||||||
|
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(true)
|
||||||
|
builder.setOngoing(false)
|
||||||
|
builder.setCategory(NotificationCompat.CATEGORY_ERROR)
|
||||||
|
builder.setShowWhen(true)
|
||||||
|
builder.setWhen(System.currentTimeMillis())
|
||||||
|
builder.setStyle(NotificationCompat.BigTextStyle().bigText(message))
|
||||||
|
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||||
|
}
|
||||||
|
|
||||||
|
DownloadState2.State.PREPARING -> {
|
||||||
|
builder.setProgress(1, 0, true)
|
||||||
|
builder.setContentText(context.getString(R.string.preparing_))
|
||||||
|
builder.setStyle(null)
|
||||||
|
builder.setOngoing(true)
|
||||||
|
builder.addAction(createCancelAction(state.id))
|
||||||
|
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||||
|
}
|
||||||
|
|
||||||
|
DownloadState2.State.PROGRESS -> {
|
||||||
|
builder.setProgress(state.max, state.progress, false)
|
||||||
|
val percent = context.getString(R.string.percent_string_pattern, (state.percent * 100).format())
|
||||||
|
if (state.timeLeft > 0L) {
|
||||||
|
val eta = DateUtils.getRelativeTimeSpanString(state.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(createCancelAction(state.id))
|
||||||
|
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||||
|
}
|
||||||
|
|
||||||
|
DownloadState2.State.PAUSED -> TODO()
|
||||||
|
}
|
||||||
|
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 fun createCancelAction(uuid: UUID) = NotificationCompat.Action(
|
||||||
|
materialR.drawable.material_ic_clear_black_24dp,
|
||||||
|
context.getString(android.R.string.cancel),
|
||||||
|
WorkManager.getInstance(context).createCancelPendingIntent(uuid),
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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.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,313 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.worker
|
||||||
|
|
||||||
|
import android.app.NotificationManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.webkit.MimeTypeMap
|
||||||
|
import androidx.hilt.work.HiltWorker
|
||||||
|
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.Operation
|
||||||
|
import androidx.work.OutOfQuotaPolicy
|
||||||
|
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.MutableSharedFlow
|
||||||
|
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.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.DownloadState2
|
||||||
|
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.printStackTraceDebug
|
||||||
|
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||||
|
import org.koitharu.kotatsu.utils.progress.TimeLeftEstimator
|
||||||
|
import java.io.File
|
||||||
|
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?>,
|
||||||
|
private val notificationFactory: DownloadNotificationFactory,
|
||||||
|
) : CoroutineWorker(appContext, params) {
|
||||||
|
|
||||||
|
private val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private lateinit var currentState: DownloadState2
|
||||||
|
|
||||||
|
private val timeLeftEstimator = TimeLeftEstimator()
|
||||||
|
private val notificationThrottler = Throttler(400)
|
||||||
|
|
||||||
|
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() }
|
||||||
|
currentState = DownloadState2(id, manga, DownloadState2.State.PREPARING)
|
||||||
|
val pausingHandle = PausingHandle()
|
||||||
|
downloadMangaImpl(chaptersIds, pausingHandle)
|
||||||
|
val outputData = currentState.toWorkData()
|
||||||
|
return when (currentState.state) {
|
||||||
|
DownloadState2.State.CANCELLED,
|
||||||
|
DownloadState2.State.DONE -> Result.success(outputData)
|
||||||
|
|
||||||
|
DownloadState2.State.FAILED -> Result.failure(outputData)
|
||||||
|
else -> Result.retry()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getForegroundInfo() = ForegroundInfo(
|
||||||
|
id.hashCode(),
|
||||||
|
notificationFactory.create(null),
|
||||||
|
)
|
||||||
|
|
||||||
|
private suspend fun downloadMangaImpl(
|
||||||
|
chaptersIds: LongArray?,
|
||||||
|
pausingHandle: PausingHandle,
|
||||||
|
) {
|
||||||
|
var manga = currentState.manga
|
||||||
|
val chaptersIdsSet = chaptersIds?.toMutableSet()
|
||||||
|
withMangaLock(manga) {
|
||||||
|
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 data = if (manga.chapters.isNullOrEmpty()) repo.getDetails(manga) else manga
|
||||||
|
output = LocalMangaOutput.getOrCreate(destination, data)
|
||||||
|
val coverUrl = data.largeCoverUrl ?: data.coverUrl
|
||||||
|
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(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(
|
||||||
|
state = DownloadState2.State.PROGRESS,
|
||||||
|
totalChapters = chapters.size,
|
||||||
|
currentChapter = chapterIndex,
|
||||||
|
totalPages = pages.size,
|
||||||
|
currentPage = pageIndex,
|
||||||
|
timeLeft = timeLeftEstimator.getEstimatedTimeLeft(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
if (settings.isDownloadsSlowdownEnabled) {
|
||||||
|
delay(SLOWDOWN_DELAY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (output.flushChapter(chapter)) {
|
||||||
|
runCatchingCancellable {
|
||||||
|
localStorageChanges.emit(LocalMangaInput.of(output.rootFile).getManga())
|
||||||
|
}.onFailure(Throwable::printStackTraceDebug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
publishState(currentState.copy(state = DownloadState2.State.PROGRESS))
|
||||||
|
output.mergeWithExisting()
|
||||||
|
output.finish()
|
||||||
|
val localManga = LocalMangaInput.of(output.rootFile).getManga()
|
||||||
|
localStorageChanges.emit(localManga)
|
||||||
|
publishState(currentState.copy(state = DownloadState2.State.DONE, localManga = localManga))
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
publishState(currentState.copy(state = DownloadState2.State.CANCELLED))
|
||||||
|
throw e
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
e.printStackTraceDebug()
|
||||||
|
publishState(currentState.copy(state = DownloadState2.State.FAILED, error = e))
|
||||||
|
} finally {
|
||||||
|
withContext(NonCancellable) {
|
||||||
|
output?.closeQuietly()
|
||||||
|
output?.cleanup()
|
||||||
|
File(destination, tempFileName).deleteAwait()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun <R> runFailsafe(
|
||||||
|
pausingHandle: PausingHandle,
|
||||||
|
block: suspend () -> R,
|
||||||
|
): R {
|
||||||
|
var countDown = MAX_FAILSAFE_ATTEMPTS
|
||||||
|
failsafe@ while (true) {
|
||||||
|
try {
|
||||||
|
return block()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
if (countDown <= 0) {
|
||||||
|
publishState(currentState.copy(state = DownloadState2.State.PAUSED, error = e))
|
||||||
|
countDown = MAX_FAILSAFE_ATTEMPTS
|
||||||
|
pausingHandle.pause()
|
||||||
|
pausingHandle.awaitResumed()
|
||||||
|
publishState(currentState.copy(state = DownloadState2.State.PROGRESS, 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)
|
||||||
|
.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 suspend fun publishState(state: DownloadState2) {
|
||||||
|
currentState = state
|
||||||
|
if (state.state == DownloadState2.State.PROGRESS && state.max > 0) {
|
||||||
|
timeLeftEstimator.tick(state.progress, state.max)
|
||||||
|
} else {
|
||||||
|
timeLeftEstimator.emptyTick()
|
||||||
|
notificationThrottler.reset()
|
||||||
|
}
|
||||||
|
val notification = notificationFactory.create(state)
|
||||||
|
if (state.isTerminal) {
|
||||||
|
notificationManager.notify(state.id.toString(), id.hashCode(), notification)
|
||||||
|
} else if (notificationThrottler.throttle()) {
|
||||||
|
notificationManager.notify(id.hashCode(), notification)
|
||||||
|
}
|
||||||
|
setProgress(state.toWorkData())
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
) {
|
||||||
|
|
||||||
|
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())).await()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun schedule(manga: Collection<Manga>) {
|
||||||
|
val data = manga.map {
|
||||||
|
dataRepository.storeManga(it)
|
||||||
|
Data.Builder()
|
||||||
|
.putLong(MANGA_ID, it.id)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
scheduleImpl(data).await()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun scheduleImpl(data: Collection<Data>): Operation {
|
||||||
|
val constraints = Constraints.Builder()
|
||||||
|
.setRequiresStorageNotLow(true)
|
||||||
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
|
.build()
|
||||||
|
val requests = data.map { inputData ->
|
||||||
|
OneTimeWorkRequestBuilder<DownloadWorker>()
|
||||||
|
.setConstraints(constraints)
|
||||||
|
.addTag(TAG)
|
||||||
|
.setInputData(inputData)
|
||||||
|
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
return WorkManager.getInstance(context).enqueue(requests)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,20 @@
|
|||||||
|
package org.koitharu.kotatsu.download.ui.worker
|
||||||
|
|
||||||
|
import android.os.SystemClock
|
||||||
|
|
||||||
|
class Throttler(
|
||||||
|
private val timeoutMs: Long,
|
||||||
|
) {
|
||||||
|
|
||||||
|
private var lastTick = 0L
|
||||||
|
|
||||||
|
fun throttle(): Boolean {
|
||||||
|
val prevValue = lastTick
|
||||||
|
lastTick = SystemClock.elapsedRealtime()
|
||||||
|
return lastTick > prevValue + timeoutMs
|
||||||
|
}
|
||||||
|
|
||||||
|
fun reset() {
|
||||||
|
lastTick = 0L
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue