This commit is contained in:
Bnyro 2022-06-05 18:08:47 +02:00
parent cbf05d292b
commit c94e3b4868

View File

@ -1,7 +1,6 @@
package com.github.libretube package com.github.libretube
import android.app.DownloadManager import android.app.DownloadManager
import android.app.Notification
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
@ -10,7 +9,6 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Environment import android.os.Environment
@ -33,11 +31,9 @@ class DownloadService : Service() {
private lateinit var extension: String private lateinit var extension: String
private var duration: Int = 0 private var duration: Int = 0
// private lateinit var command: String
private lateinit var audioDir: File private lateinit var audioDir: File
private lateinit var videoDir: File private lateinit var videoDir: File
lateinit var service: NotificationManager private lateinit var notification: NotificationCompat.Builder
lateinit var notification: NotificationCompat.Builder
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
IS_DOWNLOAD_RUNNING = true IS_DOWNLOAD_RUNNING = true
@ -48,43 +44,8 @@ class DownloadService : Service() {
videoUrl = intent.getStringExtra("videoUrl")!! videoUrl = intent.getStringExtra("videoUrl")!!
audioUrl = intent.getStringExtra("audioUrl")!! audioUrl = intent.getStringExtra("audioUrl")!!
extension = intent.getStringExtra("extension")!! extension = intent.getStringExtra("extension")!!
// command = intent.getStringExtra("command")!!
duration = intent.getIntExtra("duration", 1) duration = intent.getIntExtra("duration", 1)
service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager downloadNotification(intent)
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val chan = NotificationChannel(
"service",
"DownloadService", NotificationManager.IMPORTANCE_NONE
)
chan.lightColor = Color.BLUE
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
service.createNotificationChannel(chan)
"service"
} else {
// If earlier version channel ID is not used
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
""
}
var pendingIntent: PendingIntent? = null
pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
} else {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
}
// Creating a notification and setting its various attributes
notification =
NotificationCompat.Builder(this@DownloadService, channelId)
.setSmallIcon(R.drawable.ic_download)
.setContentTitle("LibreTube")
.setContentText("Downloading")
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setProgress(100, 0, true)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
startForeground(1, notification.build())
downloadManager() downloadManager()
return super.onStartCommand(intent, flags, startId) return super.onStartCommand(intent, flags, startId)
@ -95,7 +56,7 @@ class DownloadService : Service() {
} }
private fun downloadManager() { private fun downloadManager() {
val path = applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) val path = applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS)
val folder_main = ".tmp" val folder_main = ".tmp"
val f = File(path, folder_main) val f = File(path, folder_main)
if (!f.exists()) { if (!f.exists()) {
@ -114,35 +75,15 @@ class DownloadService : Service() {
onDownloadComplete, onDownloadComplete,
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE) IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
) )
val request: DownloadManager.Request = downloadId = downloadManagerRequest("Video", "Downloading", videoUrl, videoDir)
DownloadManager.Request(Uri.parse(videoUrl)) if (videoUrl != "") {
.setTitle("Video") // Title of the Download Notification
.setDescription("Downloading") // Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // Visibility of the download Notification
.setDestinationUri(Uri.fromFile(videoDir))
.setAllowedOverMetered(true) // Set if download is allowed on Mobile network
.setAllowedOverRoaming(true) //
val downloadManager: DownloadManager =
applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadId = downloadManager.enqueue(request)
if (audioUrl == "") {
downloadId = 0L downloadId = 0L
} }
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Log.e(TAG, "download error $e") Log.e(TAG, "download error $e")
try { try {
downloadId = 0L downloadId = 0L
val request: DownloadManager.Request = downloadManagerRequest("Audio", "Downloading", audioUrl, audioDir)
DownloadManager.Request(Uri.parse(audioUrl))
.setTitle("Audio") // Title of the Download Notification
.setDescription("Downloading") // Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // Visibility of the download Notification
.setDestinationUri(Uri.fromFile(audioDir))
.setAllowedOverMetered(true) // Set if download is allowed on Mobile network
.setAllowedOverRoaming(true) //
val downloadManager: DownloadManager =
applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "audio download error $e") Log.e(TAG, "audio download error $e")
stopService(Intent(this, DownloadService::class.java)) stopService(Intent(this, DownloadService::class.java))
@ -158,17 +99,7 @@ class DownloadService : Service() {
if (downloadId == id) { if (downloadId == id) {
downloadId = 0L downloadId = 0L
try { try {
val request: DownloadManager.Request = downloadManagerRequest("Audio", "Downloading", audioUrl, audioDir)
DownloadManager.Request(Uri.parse(audioUrl))
.setTitle("Audio") // Title of the Download Notification
.setDescription("Downloading") // Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // Visibility of the download Notification
.setDestinationUri(Uri.fromFile(audioDir))
.setAllowedOverMetered(true) // Set if download is allowed on Mobile network
.setAllowedOverRoaming(true) //
val downloadManager: DownloadManager =
applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
} catch (e: Exception) { } catch (e: Exception) {
} }
} else if (downloadId == 0L) { } else if (downloadId == 0L) {
@ -181,15 +112,91 @@ class DownloadService : Service() {
} else { } else {
Log.e(TAG, "libreTube Directory already have") Log.e(TAG, "libreTube Directory already have")
} }
var command: String = when { muxDownloadedMedia(libreTube)
}
}
}
private fun downloadManagerRequest(title: String, descriptionText: String, url: String, fileDir: File): Long {
val request: DownloadManager.Request =
DownloadManager.Request(Uri.parse(url))
.setTitle(title) // Title of the Download Notification
.setDescription(descriptionText) // Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // Visibility of the download Notification
.setDestinationUri(Uri.fromFile(fileDir))
.setAllowedOverMetered(true) // Set if download is allowed on Mobile network
.setAllowedOverRoaming(true) //
val downloadManager: DownloadManager =
applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
return downloadManager.enqueue(request)
}
private fun createNotificationChannel(id: String, name: String, descriptionText: String, importance: Int): String {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(id, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
id
} else ""
}
private fun downloadNotification(intent: Intent) {
// Creating the notification channel
val channelId = createNotificationChannel(
"service", "service", "DownloadService",
NotificationManager.IMPORTANCE_NONE
)
var pendingIntent: PendingIntent? = null
pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
} else {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
}
// Creating a notification and setting its various attributes
notification =
NotificationCompat.Builder(this@DownloadService, channelId)
.setSmallIcon(R.drawable.ic_download)
.setContentTitle("LibreTube")
.setContentText("Downloading")
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setProgress(100, 0, true)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
startForeground(2, notification.build())
}
private fun downloadFailedNotification() {
val builder = NotificationCompat.Builder(this@DownloadService, "failed")
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.downloadfailed))
.setContentText("failure")
.setPriority(NotificationCompat.PRIORITY_HIGH)
createNotificationChannel("failed", "failed", "Download Failed", NotificationManager.IMPORTANCE_DEFAULT)
with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define
notify(69, builder.build())
}
}
private fun muxDownloadedMedia(targetDir: File) {
val command: String = when {
videoUrl == "" -> { videoUrl == "" -> {
"-y -i $audioDir -c copy $libreTube/$videoId-audio$extension" "-y -i $audioDir -c copy $targetDir/$videoId-audio$extension"
} }
audioUrl == "" -> { audioUrl == "" -> {
"-y -i $videoDir -c copy $libreTube/$videoId-video$extension" "-y -i $videoDir -c copy $targetDir/$videoId-video$extension"
} }
else -> { else -> {
"-y -i $videoDir -i $audioDir -c copy $libreTube/${videoId}$extension" "-y -i $videoDir -i $audioDir -c copy $targetDir/${videoId}$extension"
} }
} }
notification.setContentTitle("Muxing") notification.setContentTitle("Muxing")
@ -213,18 +220,7 @@ class DownloadService : Service() {
val folder_main = ".tmp" val folder_main = ".tmp"
val f = File(path, folder_main) val f = File(path, folder_main)
f.deleteRecursively() f.deleteRecursively()
if (returnCode.toString() != "0") { if (returnCode.toString() != "0") downloadFailedNotification()
var builder = NotificationCompat.Builder(this@DownloadService, "failed")
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.downloadfailed))
.setContentText("failure")
.setPriority(NotificationCompat.PRIORITY_HIGH)
createNotificationChannel()
with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define
notify(69, builder.build())
}
}
stopForeground(true) stopForeground(true)
stopService(Intent(this@DownloadService, DownloadService::class.java)) stopService(Intent(this@DownloadService, DownloadService::class.java))
}, { }, {
@ -242,25 +238,6 @@ class DownloadService : Service() {
}*/ }*/
} }
} }
}
}
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "failed"
val descriptionText = "Download Failed"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("failed", name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
override fun onDestroy() { override fun onDestroy() {
try { try {