mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Merge pull request #2698 from Bnyro/master
Download Manager: Apply review from #2469
This commit is contained in:
commit
fec8965a4a
@ -0,0 +1,20 @@
|
|||||||
|
package com.github.libretube.compat
|
||||||
|
|
||||||
|
import android.app.PendingIntent
|
||||||
|
import android.os.Build
|
||||||
|
|
||||||
|
object PendingIntentCompat {
|
||||||
|
val updateCurrentFlags: Int
|
||||||
|
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
(PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
||||||
|
} else {
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
|
}
|
||||||
|
|
||||||
|
val cancelCurrentFlags: Int
|
||||||
|
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
|
||||||
|
} else {
|
||||||
|
PendingIntent.FLAG_CANCEL_CURRENT
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.github.libretube.compat
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
|
|
||||||
|
class ServiceCompat(private val context: Context) {
|
||||||
|
fun startForeground(intent: Intent) {
|
||||||
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
|
||||||
|
context.startForegroundService(intent)
|
||||||
|
} else {
|
||||||
|
context.startService(intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,9 +10,9 @@ object IntentData {
|
|||||||
const val fileName = "fileName"
|
const val fileName = "fileName"
|
||||||
const val keepQueue = "keepQueue"
|
const val keepQueue = "keepQueue"
|
||||||
const val playlistType = "playlistType"
|
const val playlistType = "playlistType"
|
||||||
const val videoFormat = "videoFormate"
|
const val videoFormat = "videoFormat"
|
||||||
const val videoQuality = "videoQuality"
|
const val videoQuality = "videoQuality"
|
||||||
const val audioFormat = "audioFormate"
|
const val audioFormat = "audioFormat"
|
||||||
const val audioQuality = "audioQuality"
|
const val audioQuality = "audioQuality"
|
||||||
const val subtitleCode = "subtitleCode"
|
const val subtitleCode = "subtitleCode"
|
||||||
const val downloading = "downloading"
|
const val downloading = "downloading"
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
package com.github.libretube.enums
|
|
||||||
|
|
||||||
/**
|
|
||||||
* object for saving the download type
|
|
||||||
*/
|
|
||||||
enum class DownloadType {
|
|
||||||
AUDIO,
|
|
||||||
VIDEO,
|
|
||||||
AUDIO_VIDEO,
|
|
||||||
NONE
|
|
||||||
}
|
|
@ -3,7 +3,7 @@ package com.github.libretube.receivers
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import com.github.libretube.compat.ServiceCompat
|
||||||
import com.github.libretube.services.DownloadService
|
import com.github.libretube.services.DownloadService
|
||||||
|
|
||||||
class NotificationReceiver : BroadcastReceiver() {
|
class NotificationReceiver : BroadcastReceiver() {
|
||||||
@ -17,10 +17,8 @@ class NotificationReceiver : BroadcastReceiver() {
|
|||||||
if (id == -1) return
|
if (id == -1) return
|
||||||
serviceIntent.putExtra("id", id)
|
serviceIntent.putExtra("id", id)
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
|
context?.let {
|
||||||
context?.startForegroundService(serviceIntent)
|
ServiceCompat(it).startForeground(serviceIntent)
|
||||||
} else {
|
|
||||||
context?.startService(serviceIntent)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import androidx.core.app.NotificationCompat
|
|||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.CronetHelper
|
import com.github.libretube.api.CronetHelper
|
||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.compat.PendingIntentCompat
|
||||||
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
|
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
|
||||||
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
|
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
@ -95,18 +96,14 @@ class DownloadService : Service() {
|
|||||||
try {
|
try {
|
||||||
val streams = RetrofitInstance.api.getStreams(videoId)
|
val streams = RetrofitInstance.api.getStreams(videoId)
|
||||||
|
|
||||||
|
val thumbnailTargetFile = getDownloadFile(DownloadHelper.THUMBNAIL_DIR, fileName)
|
||||||
|
|
||||||
awaitQuery {
|
awaitQuery {
|
||||||
Database.downloadDao().insertDownload(
|
Database.downloadDao().insertDownload(
|
||||||
Download(
|
Download(
|
||||||
videoId = videoId,
|
videoId = videoId,
|
||||||
title = streams.title ?: "",
|
title = streams.title ?: "",
|
||||||
thumbnailPath = File(
|
thumbnailPath = thumbnailTargetFile.absolutePath,
|
||||||
DownloadHelper.getDownloadDir(
|
|
||||||
this@DownloadService,
|
|
||||||
DownloadHelper.THUMBNAIL_DIR
|
|
||||||
),
|
|
||||||
fileName
|
|
||||||
).absolutePath,
|
|
||||||
description = streams.description ?: "",
|
description = streams.description ?: "",
|
||||||
uploadDate = streams.uploadDate,
|
uploadDate = streams.uploadDate,
|
||||||
uploader = streams.uploader ?: ""
|
uploader = streams.uploader ?: ""
|
||||||
@ -117,13 +114,7 @@ class DownloadService : Service() {
|
|||||||
ImageHelper.downloadImage(
|
ImageHelper.downloadImage(
|
||||||
this@DownloadService,
|
this@DownloadService,
|
||||||
url,
|
url,
|
||||||
File(
|
thumbnailTargetFile.absolutePath
|
||||||
DownloadHelper.getDownloadDir(
|
|
||||||
this@DownloadService,
|
|
||||||
DownloadHelper.THUMBNAIL_DIR
|
|
||||||
),
|
|
||||||
fileName
|
|
||||||
).absolutePath
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,27 +142,9 @@ class DownloadService : Service() {
|
|||||||
*/
|
*/
|
||||||
private fun start(item: DownloadItem) {
|
private fun start(item: DownloadItem) {
|
||||||
val file: File = when (item.type) {
|
val file: File = when (item.type) {
|
||||||
FileType.AUDIO -> {
|
FileType.AUDIO -> getDownloadFile(DownloadHelper.AUDIO_DIR, item.fileName)
|
||||||
val audioDownloadDir = DownloadHelper.getDownloadDir(
|
FileType.VIDEO -> getDownloadFile(DownloadHelper.VIDEO_DIR, item.fileName)
|
||||||
this,
|
FileType.SUBTITLE -> getDownloadFile(DownloadHelper.SUBTITLE_DIR, item.fileName)
|
||||||
DownloadHelper.AUDIO_DIR
|
|
||||||
)
|
|
||||||
File(audioDownloadDir, item.fileName)
|
|
||||||
}
|
|
||||||
FileType.VIDEO -> {
|
|
||||||
val videoDownloadDir = DownloadHelper.getDownloadDir(
|
|
||||||
this,
|
|
||||||
DownloadHelper.VIDEO_DIR
|
|
||||||
)
|
|
||||||
File(videoDownloadDir, item.fileName)
|
|
||||||
}
|
|
||||||
FileType.SUBTITLE -> {
|
|
||||||
val subtitleDownloadDir = DownloadHelper.getDownloadDir(
|
|
||||||
this,
|
|
||||||
DownloadHelper.SUBTITLE_DIR
|
|
||||||
)
|
|
||||||
File(subtitleDownloadDir, item.fileName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
file.createNewFile()
|
file.createNewFile()
|
||||||
item.path = file.absolutePath
|
item.path = file.absolutePath
|
||||||
@ -382,7 +355,7 @@ class DownloadService : Service() {
|
|||||||
|
|
||||||
summaryNotificationBuilder = NotificationCompat
|
summaryNotificationBuilder = NotificationCompat
|
||||||
.Builder(this, DOWNLOAD_CHANNEL_ID)
|
.Builder(this, DOWNLOAD_CHANNEL_ID)
|
||||||
.setSmallIcon(R.mipmap.ic_launcher)
|
.setSmallIcon(R.drawable.ic_launcher_lockscreen)
|
||||||
.setContentTitle(getString(R.string.downloading))
|
.setContentTitle(getString(R.string.downloading))
|
||||||
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
|
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
|
||||||
.setGroup(DOWNLOAD_NOTIFICATION_GROUP)
|
.setGroup(DOWNLOAD_NOTIFICATION_GROUP)
|
||||||
@ -394,12 +367,6 @@ class DownloadService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder {
|
private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder {
|
||||||
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
|
|
||||||
} else {
|
|
||||||
PendingIntent.FLAG_CANCEL_CURRENT
|
|
||||||
}
|
|
||||||
|
|
||||||
val activityIntent =
|
val activityIntent =
|
||||||
PendingIntent.getActivity(
|
PendingIntent.getActivity(
|
||||||
this@DownloadService,
|
this@DownloadService,
|
||||||
@ -407,7 +374,7 @@ class DownloadService : Service() {
|
|||||||
Intent(this@DownloadService, MainActivity::class.java).apply {
|
Intent(this@DownloadService, MainActivity::class.java).apply {
|
||||||
putExtra("fragmentToOpen", "downloads")
|
putExtra("fragmentToOpen", "downloads")
|
||||||
},
|
},
|
||||||
flags
|
PendingIntentCompat.cancelCurrentFlags
|
||||||
)
|
)
|
||||||
|
|
||||||
return NotificationCompat
|
return NotificationCompat
|
||||||
@ -459,43 +426,44 @@ class DownloadService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getResumeAction(id: Int): NotificationCompat.Action {
|
private fun getResumeAction(id: Int): NotificationCompat.Action {
|
||||||
val intent = Intent(this, NotificationReceiver::class.java)
|
val intent = Intent(this, NotificationReceiver::class.java).apply {
|
||||||
|
action = ACTION_DOWNLOAD_RESUME
|
||||||
intent.action = ACTION_DOWNLOAD_RESUME
|
putExtra("id", id)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NotificationCompat.Action.Builder(
|
return NotificationCompat.Action.Builder(
|
||||||
R.drawable.ic_play,
|
R.drawable.ic_play,
|
||||||
getString(R.string.resume),
|
getString(R.string.resume),
|
||||||
PendingIntent.getBroadcast(this, id, intent, flags)
|
PendingIntent.getBroadcast(this, id, intent, PendingIntentCompat.updateCurrentFlags)
|
||||||
).build()
|
).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getPauseAction(id: Int): NotificationCompat.Action {
|
private fun getPauseAction(id: Int): NotificationCompat.Action {
|
||||||
val intent = Intent(this, NotificationReceiver::class.java)
|
val intent = Intent(this, NotificationReceiver::class.java).apply {
|
||||||
|
action = ACTION_DOWNLOAD_PAUSE
|
||||||
intent.action = ACTION_DOWNLOAD_PAUSE
|
putExtra("id", id)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NotificationCompat.Action.Builder(
|
return NotificationCompat.Action.Builder(
|
||||||
R.drawable.ic_pause,
|
R.drawable.ic_pause,
|
||||||
getString(R.string.pause),
|
getString(R.string.pause),
|
||||||
PendingIntent.getBroadcast(this, id, intent, flags)
|
PendingIntent.getBroadcast(this, id, intent, PendingIntentCompat.updateCurrentFlags)
|
||||||
).build()
|
).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a [File] from the corresponding download directory and the file name
|
||||||
|
*/
|
||||||
|
private fun getDownloadFile(directory: String, fileName: String): File {
|
||||||
|
return File(
|
||||||
|
DownloadHelper.getDownloadDir(
|
||||||
|
this@DownloadService,
|
||||||
|
directory
|
||||||
|
),
|
||||||
|
fileName
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
downloadQueue.clear()
|
downloadQueue.clear()
|
||||||
IS_DOWNLOAD_RUNNING = false
|
IS_DOWNLOAD_RUNNING = false
|
||||||
|
@ -3,7 +3,7 @@ package com.github.libretube.util
|
|||||||
import android.app.ActivityManager
|
import android.app.ActivityManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import com.github.libretube.compat.ServiceCompat
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
import com.github.libretube.services.BackgroundMode
|
import com.github.libretube.services.BackgroundMode
|
||||||
|
|
||||||
@ -33,11 +33,7 @@ object BackgroundHelper {
|
|||||||
intent.putExtra(IntentData.keepQueue, keepQueue)
|
intent.putExtra(IntentData.keepQueue, keepQueue)
|
||||||
|
|
||||||
// start the background mode as foreground service
|
// start the background mode as foreground service
|
||||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
|
ServiceCompat(context).startForeground(intent)
|
||||||
context.startForegroundService(intent)
|
|
||||||
} else {
|
|
||||||
context.startService(intent)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ package com.github.libretube.util
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import com.github.libretube.compat.ServiceCompat
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.db.obj.DownloadItem
|
import com.github.libretube.db.obj.DownloadItem
|
||||||
@ -65,11 +66,7 @@ object DownloadHelper {
|
|||||||
intent.putExtra(IntentData.audioQuality, audioQuality)
|
intent.putExtra(IntentData.audioQuality, audioQuality)
|
||||||
intent.putExtra(IntentData.subtitleCode, subtitleCode)
|
intent.putExtra(IntentData.subtitleCode, subtitleCode)
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
|
ServiceCompat(context).startForeground(intent)
|
||||||
context.startForegroundService(intent)
|
|
||||||
} else {
|
|
||||||
context.startService(intent)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun DownloadItem.getNotificationId(): Int {
|
fun DownloadItem.getNotificationId(): Int {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user