LibreTube/app/src/main/java/com/github/libretube/services/DownloadService.kt

286 lines
11 KiB
Kotlin
Raw Normal View History

2022-06-28 20:02:26 +05:30
package com.github.libretube.services
2022-03-03 12:08:36 +05:30
2022-05-21 13:32:04 +05:30
import android.app.DownloadManager
import android.app.PendingIntent
import android.app.Service
2022-03-03 12:08:36 +05:30
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Build
import android.os.Environment
2022-05-21 13:32:04 +05:30
import android.os.Environment.DIRECTORY_DOWNLOADS
2022-06-09 17:52:07 +05:30
import android.os.Environment.DIRECTORY_MOVIES
import android.os.Environment.DIRECTORY_MUSIC
2022-03-03 12:08:36 +05:30
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
2022-03-05 00:22:30 +05:30
import androidx.core.app.NotificationManagerCompat
2022-03-03 12:08:36 +05:30
import com.arthenica.ffmpegkit.FFmpegKit
2022-06-28 20:02:26 +05:30
import com.github.libretube.R
2022-06-26 01:51:22 +05:30
import com.github.libretube.util.PreferenceHelper
2022-03-03 12:08:36 +05:30
import java.io.File
2022-03-04 23:30:50 +05:30
2022-03-04 21:27:10 +05:30
var IS_DOWNLOAD_RUNNING = false
2022-05-21 13:32:04 +05:30
2022-05-20 03:52:10 +05:30
class DownloadService : Service() {
2022-03-03 12:08:36 +05:30
val TAG = "DownloadService"
2022-05-20 03:52:10 +05:30
private var downloadId: Long = -1
2022-03-03 12:08:36 +05:30
private lateinit var videoId: String
private lateinit var videoUrl: String
private lateinit var audioUrl: String
2022-03-04 21:27:10 +05:30
private lateinit var extension: String
2022-03-03 12:08:36 +05:30
private var duration: Int = 0
2022-05-21 13:32:04 +05:30
2022-03-03 12:08:36 +05:30
private lateinit var audioDir: File
private lateinit var videoDir: File
2022-06-05 21:38:47 +05:30
private lateinit var notification: NotificationCompat.Builder
2022-06-05 23:10:16 +05:30
private lateinit var downloadType: String
private lateinit var libretubeDir: File
private lateinit var tempDir: File
2022-03-04 21:27:10 +05:30
override fun onCreate() {
super.onCreate()
IS_DOWNLOAD_RUNNING = true
}
2022-03-03 12:08:36 +05:30
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
videoId = intent?.getStringExtra("videoId")!!
videoUrl = intent.getStringExtra("videoUrl")!!
audioUrl = intent.getStringExtra("audioUrl")!!
2022-05-20 03:52:10 +05:30
duration = intent.getIntExtra("duration", 1)
2022-06-26 01:51:22 +05:30
extension = PreferenceHelper.getString(this, "video_format", ".mp4")!!
2022-06-05 23:10:16 +05:30
downloadType = if (audioUrl != "" && videoUrl != "") "mux"
else if (audioUrl != "") "audio"
else if (videoUrl != "") "video"
else "none"
if (downloadType != "none") {
downloadNotification(intent)
downloadManager()
} else {
onDestroy()
}
2022-03-03 12:08:36 +05:30
return super.onStartCommand(intent, flags, startId)
}
2022-05-21 13:32:04 +05:30
2022-03-03 12:08:36 +05:30
override fun onBind(intent: Intent?): IBinder? {
TODO("Not yet implemented")
}
private fun downloadManager() {
2022-06-05 23:10:16 +05:30
// create folder for temporary files
tempDir = File(
applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS),
".tmp"
)
if (!tempDir.exists()) {
tempDir.mkdirs()
2022-03-03 12:08:36 +05:30
Log.e(TAG, "Directory make")
} else {
2022-06-05 23:10:16 +05:30
tempDir.deleteRecursively()
tempDir.mkdirs()
2022-03-03 12:08:36 +05:30
Log.e(TAG, "Directory already have")
}
2022-06-05 23:10:16 +05:30
2022-06-26 01:51:22 +05:30
val downloadLocationPref = PreferenceHelper.getString(this, "download_location", "")
val folderName = PreferenceHelper.getString(this, "download_folder", "LibreTube")
2022-06-09 17:52:07 +05:30
val location = when (downloadLocationPref) {
"downloads" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)
"music" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC)
"movies" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES)
"sdcard" -> Environment.getExternalStorageDirectory()
else -> Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)
}
2022-06-05 23:10:16 +05:30
libretubeDir = File(
2022-06-09 17:52:07 +05:30
location,
folderName
2022-06-05 23:10:16 +05:30
)
if (!libretubeDir.exists()) libretubeDir.mkdirs()
2022-06-07 16:12:59 +05:30
Log.i(TAG, libretubeDir.toString())
2022-06-05 23:10:16 +05:30
// start download
2022-03-03 12:08:36 +05:30
try {
2022-05-21 13:32:04 +05:30
registerReceiver(
onDownloadComplete,
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
)
2022-06-05 23:10:16 +05:30
when (downloadType) {
"mux" -> {
audioDir = File(tempDir, "$videoId-audio")
videoDir = File(tempDir, "$videoId-video")
2022-06-17 20:20:48 +05:30
downloadId = downloadManagerRequest(
getString(R.string.video),
getString(R.string.downloading),
videoUrl,
videoDir
)
2022-06-05 23:10:16 +05:30
}
"video" -> {
videoDir = File(libretubeDir, "$videoId-video")
2022-06-17 20:20:48 +05:30
downloadId = downloadManagerRequest(
getString(R.string.video),
getString(R.string.downloading),
videoUrl,
videoDir
)
2022-06-05 23:10:16 +05:30
}
"audio" -> {
audioDir = File(libretubeDir, "$videoId-audio")
2022-06-17 20:20:48 +05:30
downloadId = downloadManagerRequest(
getString(R.string.audio),
getString(R.string.downloading),
audioUrl,
audioDir
)
2022-06-05 23:10:16 +05:30
}
2022-05-21 13:32:04 +05:30
}
2022-03-03 12:08:36 +05:30
} catch (e: IllegalArgumentException) {
Log.e(TAG, "download error $e")
}
}
private val onDownloadComplete: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
2022-05-20 03:52:10 +05:30
// Fetching the download id received with the broadcast
2022-03-03 12:08:36 +05:30
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
2022-05-20 03:52:10 +05:30
// Checking if the received broadcast is for our enqueued download by matching download id
2022-06-05 23:10:16 +05:30
if (downloadId == id) {
if (downloadType == "mux") {
2022-06-17 20:20:48 +05:30
downloadManagerRequest(
2022-06-17 20:33:17 +05:30
getString(R.string.audio),
2022-06-17 20:20:48 +05:30
getString(R.string.downloading),
audioUrl,
audioDir
)
2022-03-04 21:27:10 +05:30
} else {
2022-06-05 23:10:16 +05:30
downloadSucceededNotification()
onDestroy()
2022-03-04 21:27:10 +05:30
}
2022-06-05 23:10:16 +05:30
} else {
muxDownloadedMedia()
2022-03-03 12:08:36 +05:30
}
}
}
2022-06-05 22:02:32 +05:30
private fun downloadManagerRequest(
title: String,
descriptionText: String,
url: String,
fileDir: File
): Long {
2022-06-05 21:38:47 +05:30
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 downloadNotification(intent: Intent) {
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 =
2022-06-06 20:12:46 +05:30
NotificationCompat.Builder(this@DownloadService, "download_service")
2022-06-05 21:38:47 +05:30
.setSmallIcon(R.drawable.ic_download)
.setContentTitle("LibreTube")
2022-06-17 20:09:05 +05:30
.setContentText(getString(R.string.downloading))
2022-06-05 21:38:47 +05:30
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setProgress(100, 0, true)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
startForeground(2, notification.build())
}
private fun downloadFailedNotification() {
2022-06-06 20:12:46 +05:30
val builder = NotificationCompat.Builder(this@DownloadService, "download_service")
2022-06-05 21:38:47 +05:30
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.downloadfailed))
2022-06-17 20:09:05 +05:30
.setContentText(getString(R.string.fail))
2022-06-05 21:38:47 +05:30
.setPriority(NotificationCompat.PRIORITY_HIGH)
with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define
2022-06-06 20:12:46 +05:30
notify(3, builder.build())
2022-06-05 21:38:47 +05:30
}
}
2022-06-05 23:10:16 +05:30
private fun downloadSucceededNotification() {
Log.i(TAG, "Download succeeded")
2022-06-06 20:12:46 +05:30
val builder = NotificationCompat.Builder(this@DownloadService, "download_service")
2022-06-05 23:10:16 +05:30
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.success))
2022-06-17 20:09:05 +05:30
.setContentText(getString(R.string.fail))
2022-06-05 23:10:16 +05:30
.setPriority(NotificationCompat.PRIORITY_HIGH)
with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define
2022-06-06 20:12:46 +05:30
notify(4, builder.build())
2022-06-05 21:38:47 +05:30
}
2022-06-05 23:10:16 +05:30
}
private fun muxDownloadedMedia() {
val command = "-y -i $videoDir -i $audioDir -c copy $libretubeDir/${videoId}$extension"
2022-06-05 21:38:47 +05:30
notification.setContentTitle("Muxing")
FFmpegKit.executeAsync(
command,
{ session ->
val state = session.state
val returnCode = session.returnCode
// CALLED WHEN SESSION IS EXECUTED
Log.d(
TAG,
String.format(
"FFmpeg process exited with state %s and rc %s.%s",
state,
returnCode,
session.failStackTrace
)
)
2022-06-05 23:10:16 +05:30
tempDir.deleteRecursively()
2022-06-05 21:38:47 +05:30
if (returnCode.toString() != "0") downloadFailedNotification()
2022-06-05 23:10:16 +05:30
else downloadSucceededNotification()
onDestroy()
2022-06-24 20:56:36 +05:30
},
{
// CALLED WHEN SESSION PRINTS LOGS
Log.e(TAG, it.message.toString())
}
2022-06-05 21:38:47 +05:30
) {
// CALLED WHEN SESSION GENERATES STATISTICS
Log.e(TAG + "stat", it.time.toString())
2022-06-07 12:22:11 +05:30
/*val progress = it.time/(10*duration!!)
if (progress<1){
notification
.setProgress(progressMax, progress.toInt(), false)
service.notify(1,notification.build())
}*/
2022-03-05 00:22:30 +05:30
}
}
2022-05-21 13:32:04 +05:30
2022-03-03 12:08:36 +05:30
override fun onDestroy() {
2022-03-04 21:27:10 +05:30
try {
unregisterReceiver(onDownloadComplete)
2022-06-07 12:22:11 +05:30
} catch (e: Exception) {
}
2022-06-05 23:10:16 +05:30
2022-03-04 21:27:10 +05:30
IS_DOWNLOAD_RUNNING = false
2022-05-20 03:52:10 +05:30
Log.d(TAG, "dl finished!")
2022-06-05 23:10:16 +05:30
stopForeground(true)
stopService(Intent(this@DownloadService, DownloadService::class.java))
2022-03-03 12:08:36 +05:30
super.onDestroy()
}
}