2022-06-28 20:02:26 +05:30
|
|
|
package com.github.libretube.services
|
2022-03-03 12:08:36 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.app.PendingIntent
|
2022-05-21 13:32:04 +05:30
|
|
|
import android.app.Service
|
2022-03-03 12:08:36 +05:30
|
|
|
import android.content.Intent
|
2022-12-16 22:14:21 +05:30
|
|
|
import android.os.Binder
|
|
|
|
import android.os.Build
|
2022-03-03 12:08:36 +05:30
|
|
|
import android.os.IBinder
|
|
|
|
import androidx.core.app.NotificationCompat
|
2022-09-08 22:12:52 +05:30
|
|
|
import com.github.libretube.R
|
2022-12-16 22:14:21 +05:30
|
|
|
import com.github.libretube.api.RetrofitInstance
|
2022-09-08 22:11:57 +05:30
|
|
|
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
|
2022-12-16 22:14:21 +05:30
|
|
|
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
|
|
|
|
import com.github.libretube.constants.IntentData
|
|
|
|
import com.github.libretube.db.DatabaseHolder
|
|
|
|
import com.github.libretube.db.obj.Download
|
|
|
|
import com.github.libretube.db.obj.DownloadItem
|
|
|
|
import com.github.libretube.enums.FileType
|
|
|
|
import com.github.libretube.extensions.awaitQuery
|
2022-12-17 15:56:57 +05:30
|
|
|
import com.github.libretube.extensions.formatAsFileSize
|
2022-12-16 22:14:21 +05:30
|
|
|
import com.github.libretube.extensions.getContentLength
|
|
|
|
import com.github.libretube.extensions.query
|
|
|
|
import com.github.libretube.extensions.toDownloadItems
|
2022-12-17 15:56:57 +05:30
|
|
|
import com.github.libretube.extensions.toastFromMainThread
|
2022-12-16 22:14:21 +05:30
|
|
|
import com.github.libretube.obj.DownloadStatus
|
|
|
|
import com.github.libretube.receivers.NotificationReceiver
|
|
|
|
import com.github.libretube.ui.activities.MainActivity
|
2022-09-10 15:21:50 +05:30
|
|
|
import com.github.libretube.util.DownloadHelper
|
2022-12-16 22:14:21 +05:30
|
|
|
import com.github.libretube.util.ImageHelper
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
|
|
import kotlinx.coroutines.flow.SharedFlow
|
|
|
|
import kotlinx.coroutines.isActive
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import okhttp3.Request
|
|
|
|
import okio.BufferedSink
|
|
|
|
import okio.buffer
|
|
|
|
import okio.sink
|
2022-03-03 12:08:36 +05:30
|
|
|
import java.io.File
|
2022-12-16 22:14:21 +05:30
|
|
|
import java.net.URL
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
import kotlin.coroutines.coroutineContext
|
2022-03-04 23:30:50 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
/**
|
|
|
|
* Download service with custom implementation of downloading using [OkHttpClient].
|
|
|
|
*/
|
2022-05-20 03:52:10 +05:30
|
|
|
class DownloadService : Service() {
|
2022-07-12 01:30:56 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
private val binder = LocalBinder()
|
|
|
|
private val jobMain = SupervisorJob()
|
|
|
|
private val scope = CoroutineScope(Dispatchers.IO + jobMain)
|
2022-10-23 23:12:33 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
private lateinit var notificationManager: NotificationManager
|
|
|
|
private lateinit var summaryNotificationBuilder: NotificationCompat.Builder
|
|
|
|
|
|
|
|
private val jobs = mutableMapOf<Int, Job>()
|
|
|
|
private val _downloadFlow = MutableSharedFlow<Pair<Int, DownloadStatus>>()
|
|
|
|
val downloadFlow: SharedFlow<Pair<Int, DownloadStatus>> = _downloadFlow
|
2022-09-09 16:41:54 +05:30
|
|
|
|
2022-03-04 21:27:10 +05:30
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
2022-09-19 23:37:55 +05:30
|
|
|
IS_DOWNLOAD_RUNNING = true
|
2022-12-16 22:14:21 +05:30
|
|
|
notifyForeground()
|
2022-03-04 21:27:10 +05:30
|
|
|
}
|
|
|
|
|
2022-03-03 12:08:36 +05:30
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
2022-12-16 22:14:21 +05:30
|
|
|
when (intent?.action) {
|
|
|
|
ACTION_RESUME -> resume(intent.getIntExtra("id", -1))
|
|
|
|
ACTION_PAUSE -> pause(intent.getIntExtra("id", -1))
|
2022-08-24 21:26:57 +05:30
|
|
|
}
|
2022-09-09 21:09:49 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
val videoId = intent?.getStringExtra(IntentData.videoId) ?: return START_NOT_STICKY
|
|
|
|
val fileName = intent.getStringExtra(IntentData.fileName) ?: videoId
|
|
|
|
val videoFormat = intent.getStringExtra(IntentData.videoFormat)
|
|
|
|
val videoQuality = intent.getStringExtra(IntentData.videoQuality)
|
|
|
|
val audioFormat = intent.getStringExtra(IntentData.audioFormat)
|
|
|
|
val audioQuality = intent.getStringExtra(IntentData.audioQuality)
|
|
|
|
val subtitleCode = intent.getStringExtra(IntentData.subtitleCode)
|
|
|
|
|
|
|
|
scope.launch {
|
|
|
|
try {
|
|
|
|
val streams = RetrofitInstance.api.getStreams(videoId)
|
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
awaitQuery {
|
2022-12-16 22:14:21 +05:30
|
|
|
DatabaseHolder.Database.downloadDao().insertDownload(
|
|
|
|
Download(
|
|
|
|
videoId = videoId,
|
|
|
|
title = streams.title ?: "",
|
|
|
|
thumbnailPath = File(
|
|
|
|
DownloadHelper.getDownloadDir(this@DownloadService, DownloadHelper.THUMBNAIL_DIR),
|
|
|
|
fileName
|
|
|
|
).absolutePath,
|
|
|
|
description = streams.description ?: "",
|
|
|
|
uploadDate = streams.uploadDate
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
streams.thumbnailUrl?.let { url ->
|
|
|
|
ImageHelper.downloadImage(this@DownloadService, url, fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
val downloadItems = streams.toDownloadItems(
|
|
|
|
videoId,
|
|
|
|
fileName,
|
|
|
|
videoFormat,
|
|
|
|
videoQuality,
|
|
|
|
audioFormat,
|
|
|
|
audioQuality,
|
|
|
|
subtitleCode
|
|
|
|
)
|
|
|
|
downloadItems.forEach { start(it) }
|
|
|
|
} catch (e: Exception) {
|
|
|
|
return@launch
|
|
|
|
}
|
2022-06-05 23:10:16 +05:30
|
|
|
}
|
2022-03-03 12:08:36 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
return START_NOT_STICKY
|
2022-03-03 12:08:36 +05:30
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
/**
|
|
|
|
* Initiate download [Job] using [DownloadItem] by creating file according to [FileType]
|
|
|
|
* for the requested file.
|
|
|
|
*/
|
2022-12-16 22:14:21 +05:30
|
|
|
private fun start(item: DownloadItem) {
|
|
|
|
val file: File = when (item.type) {
|
|
|
|
FileType.AUDIO -> {
|
2022-12-17 15:56:57 +05:30
|
|
|
val audioDownloadDir = DownloadHelper.getDownloadDir(
|
|
|
|
this,
|
|
|
|
DownloadHelper.AUDIO_DIR
|
|
|
|
)
|
2022-12-16 22:14:21 +05:30
|
|
|
File(audioDownloadDir, item.fileName)
|
|
|
|
}
|
|
|
|
FileType.VIDEO -> {
|
2022-12-17 15:56:57 +05:30
|
|
|
val videoDownloadDir = DownloadHelper.getDownloadDir(
|
|
|
|
this,
|
|
|
|
DownloadHelper.VIDEO_DIR
|
|
|
|
)
|
2022-12-16 22:14:21 +05:30
|
|
|
File(videoDownloadDir, item.fileName)
|
|
|
|
}
|
|
|
|
FileType.SUBTITLE -> {
|
2022-12-17 15:56:57 +05:30
|
|
|
val subtitleDownloadDir = DownloadHelper.getDownloadDir(
|
|
|
|
this,
|
|
|
|
DownloadHelper.SUBTITLE_DIR
|
|
|
|
)
|
2022-12-16 22:14:21 +05:30
|
|
|
File(subtitleDownloadDir, item.fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.createNewFile()
|
|
|
|
item.path = file.absolutePath
|
|
|
|
|
|
|
|
item.id = awaitQuery {
|
|
|
|
DatabaseHolder.Database.downloadDao().insertDownloadItem(item)
|
|
|
|
}.toInt()
|
|
|
|
|
|
|
|
jobs[item.id] = scope.launch {
|
|
|
|
downloadFile(item)
|
|
|
|
}
|
2022-03-03 12:08:36 +05:30
|
|
|
}
|
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
/**
|
|
|
|
* Download file and emit [DownloadStatus] to the collectors of [downloadFlow]
|
|
|
|
* and notification.
|
|
|
|
*/
|
2022-12-16 22:14:21 +05:30
|
|
|
private suspend fun downloadFile(item: DownloadItem) {
|
2022-12-17 15:56:57 +05:30
|
|
|
val notificationBuilder = getNotificationBuilder(item)
|
|
|
|
setResumeNotification(notificationBuilder, item)
|
2022-06-09 17:52:07 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
val okHttpClient = OkHttpClient.Builder()
|
|
|
|
.connectTimeout(DownloadHelper.DEFAULT_TIMEOUT, TimeUnit.SECONDS)
|
|
|
|
.readTimeout(DownloadHelper.DEFAULT_TIMEOUT, TimeUnit.SECONDS)
|
|
|
|
.build()
|
2022-09-09 21:09:49 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
val file = File(item.path)
|
|
|
|
var totalRead = file.length()
|
|
|
|
val url = URL(item.url ?: return)
|
|
|
|
|
|
|
|
url.getContentLength().let { size ->
|
|
|
|
if (size > 0 && size != item.downloadSize) {
|
|
|
|
item.downloadSize = size
|
|
|
|
query {
|
|
|
|
DatabaseHolder.Database.downloadDao().updateDownloadItem(item)
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
}
|
2022-03-03 12:08:36 +05:30
|
|
|
}
|
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
// Set start range where last downloading was held.
|
2022-12-16 22:14:21 +05:30
|
|
|
val request = Request.Builder()
|
|
|
|
.url(url)
|
|
|
|
.addHeader("Range", "bytes=$totalRead-").build()
|
|
|
|
var lastTime = System.currentTimeMillis() / 1000
|
|
|
|
var lastRead: Long = 0
|
|
|
|
val sink: BufferedSink = file.sink(true).buffer()
|
|
|
|
|
|
|
|
try {
|
|
|
|
val response = okHttpClient.newCall(request).execute()
|
|
|
|
val sourceBytes = response.body!!.source()
|
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
// Check if job is still active and read next bytes.
|
2022-12-16 22:14:21 +05:30
|
|
|
while (coroutineContext.isActive &&
|
|
|
|
sourceBytes
|
|
|
|
.read(sink.buffer, DownloadHelper.DOWNLOAD_CHUNK_SIZE)
|
|
|
|
.also { lastRead = it } != -1L
|
|
|
|
) {
|
|
|
|
sink.emit()
|
|
|
|
totalRead += lastRead
|
|
|
|
_downloadFlow.emit(item.id to DownloadStatus.Progress(totalRead, item.downloadSize))
|
|
|
|
|
|
|
|
if (item.downloadSize != -1L &&
|
2022-12-17 15:56:57 +05:30
|
|
|
System.currentTimeMillis() / 1000 > lastTime
|
|
|
|
) {
|
|
|
|
notificationBuilder
|
|
|
|
.setContentText("${totalRead.formatAsFileSize()} / ${item.downloadSize.formatAsFileSize()}")
|
|
|
|
.setProgress(item.downloadSize.toInt(), totalRead.toInt(), false)
|
2022-12-16 22:14:21 +05:30
|
|
|
notificationManager.notify(item.id, notificationBuilder.build())
|
2022-12-17 15:56:57 +05:30
|
|
|
lastTime = System.currentTimeMillis() / 1000
|
2022-12-16 22:14:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
2022-12-17 15:56:57 +05:30
|
|
|
toastFromMainThread("${getString(R.string.download)}: ${e.message.toString()}")
|
2022-12-16 22:14:21 +05:30
|
|
|
_downloadFlow.emit(item.id to DownloadStatus.Error(e.message.toString(), e))
|
|
|
|
} finally {
|
|
|
|
sink.flush()
|
|
|
|
sink.close()
|
|
|
|
}
|
2022-09-09 21:09:49 +05:30
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
val completed = when (totalRead) {
|
|
|
|
item.downloadSize -> {
|
|
|
|
_downloadFlow.emit(item.id to DownloadStatus.Completed)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
_downloadFlow.emit(item.id to DownloadStatus.Paused)
|
|
|
|
false
|
|
|
|
}
|
2022-12-16 22:14:21 +05:30
|
|
|
}
|
2022-12-17 15:56:57 +05:30
|
|
|
pause(item.id)
|
|
|
|
setPauseNotification(notificationBuilder, item, completed)
|
2022-12-16 22:14:21 +05:30
|
|
|
}
|
2022-09-09 16:41:54 +05:30
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
/**
|
|
|
|
* Resume download which may have been paused.
|
|
|
|
*/
|
2022-12-16 22:14:21 +05:30
|
|
|
fun resume(id: Int) {
|
2022-12-17 15:56:57 +05:30
|
|
|
// Cancel last job if it is still active to avoid multiple
|
|
|
|
// jobs for same file.
|
2022-12-16 22:14:21 +05:30
|
|
|
jobs[id]?.cancel()
|
|
|
|
val downloadItem = awaitQuery {
|
|
|
|
DatabaseHolder.Database.downloadDao().findDownloadItemById(id)
|
2022-03-03 12:08:36 +05:30
|
|
|
}
|
2022-12-16 22:14:21 +05:30
|
|
|
jobs[id] = scope.launch {
|
|
|
|
downloadFile(downloadItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
/**
|
|
|
|
* Pause downloading job for given [id]. If no [jobs] are active, stop the service.
|
|
|
|
*/
|
2022-12-16 22:14:21 +05:30
|
|
|
fun pause(id: Int) {
|
|
|
|
jobs[id]?.cancel()
|
2022-12-17 15:56:57 +05:30
|
|
|
|
|
|
|
// Stop the service if no downloads are active.
|
|
|
|
if (jobs.values.none { it.isActive }) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
stopForeground(STOP_FOREGROUND_DETACH)
|
|
|
|
}
|
|
|
|
stopSelf()
|
|
|
|
}
|
2022-03-03 12:08:36 +05:30
|
|
|
}
|
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
/**
|
|
|
|
* Check whether the file downloading job is active or not.
|
|
|
|
*/
|
2022-12-16 22:14:21 +05:30
|
|
|
fun isDownloading(id: Int): Boolean {
|
|
|
|
return jobs[id]?.isActive ?: false
|
2022-06-05 21:38:47 +05:30
|
|
|
}
|
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
private fun notifyForeground() {
|
|
|
|
notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
2022-09-09 21:09:49 +05:30
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
summaryNotificationBuilder = NotificationCompat
|
|
|
|
.Builder(this, DOWNLOAD_CHANNEL_ID)
|
|
|
|
.setSmallIcon(R.mipmap.ic_launcher)
|
|
|
|
.setContentTitle(getString(R.string.downloading))
|
|
|
|
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
|
|
|
|
.setGroup(DOWNLOAD_NOTIFICATION_GROUP)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
|
|
.setOnlyAlertOnce(true)
|
|
|
|
.setGroupSummary(true)
|
|
|
|
|
|
|
|
startForeground(DOWNLOAD_PROGRESS_NOTIFICATION_ID, summaryNotificationBuilder.build())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder {
|
2022-12-16 22:14:21 +05:30
|
|
|
val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
PendingIntent.FLAG_IMMUTABLE
|
|
|
|
} else {
|
|
|
|
0
|
2022-06-05 21:38:47 +05:30
|
|
|
}
|
2022-12-16 22:14:21 +05:30
|
|
|
|
|
|
|
val activityIntent =
|
|
|
|
PendingIntent.getActivity(
|
|
|
|
this@DownloadService,
|
|
|
|
0,
|
|
|
|
Intent(this@DownloadService, MainActivity::class.java).apply {
|
|
|
|
putExtra("fragmentToOpen", "downloads")
|
|
|
|
},
|
|
|
|
flag
|
|
|
|
)
|
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
return NotificationCompat
|
2022-12-16 22:14:21 +05:30
|
|
|
.Builder(this, DOWNLOAD_CHANNEL_ID)
|
|
|
|
.setContentTitle(getString(R.string.downloading))
|
2022-12-17 15:56:57 +05:30
|
|
|
.setProgress(0, 0, true)
|
2022-12-16 22:14:21 +05:30
|
|
|
.setOngoing(true)
|
2022-12-17 15:56:57 +05:30
|
|
|
.setContentIntent(activityIntent)
|
2022-12-16 22:14:21 +05:30
|
|
|
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
|
2022-12-17 15:56:57 +05:30
|
|
|
.setGroup(DOWNLOAD_NOTIFICATION_GROUP)
|
|
|
|
}
|
2022-12-16 22:14:21 +05:30
|
|
|
|
2022-12-17 15:56:57 +05:30
|
|
|
private fun setResumeNotification(notificationBuilder: NotificationCompat.Builder, item: DownloadItem) {
|
|
|
|
notificationBuilder
|
|
|
|
.setContentTitle(item.fileName)
|
|
|
|
.setSmallIcon(android.R.drawable.stat_sys_download)
|
|
|
|
.setWhen(System.currentTimeMillis())
|
|
|
|
.setOngoing(true)
|
|
|
|
.clearActions()
|
|
|
|
.addAction(getPauseAction(item.id))
|
|
|
|
|
|
|
|
notificationManager.notify(item.id, notificationBuilder.build())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setPauseNotification(notificationBuilder: NotificationCompat.Builder, item: DownloadItem, isCompleted: Boolean = false) {
|
|
|
|
notificationBuilder
|
|
|
|
.setProgress(0, 0, false)
|
|
|
|
.setOngoing(false)
|
|
|
|
.clearActions()
|
|
|
|
|
|
|
|
if (isCompleted) {
|
|
|
|
notificationBuilder
|
|
|
|
.setSmallIcon(R.drawable.ic_done)
|
|
|
|
.setContentText(getString(R.string.download_completed))
|
|
|
|
} else {
|
|
|
|
notificationBuilder
|
|
|
|
.setSmallIcon(R.drawable.ic_pause)
|
|
|
|
.setContentText(getString(R.string.download_paused))
|
|
|
|
.addAction(getResumeAction(item.id))
|
|
|
|
}
|
|
|
|
notificationManager.notify(item.id, notificationBuilder.build())
|
2022-06-05 21:38:47 +05:30
|
|
|
}
|
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
private fun getResumeAction(id: Int): NotificationCompat.Action {
|
|
|
|
val intent = Intent(this, NotificationReceiver::class.java)
|
|
|
|
|
|
|
|
intent.action = ACTION_RESUME
|
|
|
|
intent.putExtra("id", id)
|
|
|
|
|
|
|
|
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
(PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
|
|
|
} else {
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT
|
2022-06-05 21:38:47 +05:30
|
|
|
}
|
2022-12-16 22:14:21 +05:30
|
|
|
|
|
|
|
return NotificationCompat.Action.Builder(
|
2022-12-17 15:56:57 +05:30
|
|
|
R.drawable.ic_play,
|
|
|
|
getString(R.string.resume),
|
|
|
|
PendingIntent.getBroadcast(this, id, intent, flags)
|
2022-12-16 22:14:21 +05:30
|
|
|
).build()
|
2022-06-05 23:10:16 +05:30
|
|
|
}
|
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
private fun getPauseAction(id: Int): NotificationCompat.Action {
|
|
|
|
val intent = Intent(this, NotificationReceiver::class.java)
|
|
|
|
|
|
|
|
intent.action = ACTION_PAUSE
|
|
|
|
intent.putExtra("id", id)
|
|
|
|
|
|
|
|
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
(PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
|
|
|
} else {
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT
|
2022-06-07 12:22:11 +05:30
|
|
|
}
|
2022-06-05 23:10:16 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
return NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.ic_pause,
|
2022-12-17 15:56:57 +05:30
|
|
|
getString(R.string.pause),
|
|
|
|
PendingIntent.getBroadcast(this, id, intent, flags)
|
2022-12-16 22:14:21 +05:30
|
|
|
).build()
|
|
|
|
}
|
2022-09-08 22:11:57 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
override fun onDestroy() {
|
|
|
|
jobMain.cancel()
|
|
|
|
IS_DOWNLOAD_RUNNING = false
|
2022-03-03 12:08:36 +05:30
|
|
|
super.onDestroy()
|
|
|
|
}
|
2022-09-19 23:37:55 +05:30
|
|
|
|
2022-12-16 22:14:21 +05:30
|
|
|
override fun onBind(intent: Intent?): IBinder {
|
|
|
|
return binder
|
|
|
|
}
|
|
|
|
|
|
|
|
inner class LocalBinder : Binder() {
|
|
|
|
fun getService(): DownloadService = this@DownloadService
|
|
|
|
}
|
|
|
|
|
2022-09-19 23:37:55 +05:30
|
|
|
companion object {
|
2022-12-17 15:56:57 +05:30
|
|
|
private const val DOWNLOAD_NOTIFICATION_GROUP = "download_notification_group"
|
2022-12-16 22:14:21 +05:30
|
|
|
const val ACTION_RESUME = "com.github.libretube.services.DownloadService.ACTION_RESUME"
|
|
|
|
const val ACTION_PAUSE = "com.github.libretube.services.DownloadService.ACTION_PAUSE"
|
2022-09-19 23:37:55 +05:30
|
|
|
var IS_DOWNLOAD_RUNNING = false
|
|
|
|
}
|
2022-03-03 12:08:36 +05:30
|
|
|
}
|