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.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.IBinder
|
|
|
|
import android.util.Log
|
|
|
|
import androidx.core.app.NotificationCompat
|
2022-03-05 00:22:30 +05:30
|
|
|
import androidx.core.app.NotificationManagerCompat
|
2022-09-08 22:12:52 +05:30
|
|
|
import com.github.libretube.Globals
|
|
|
|
import com.github.libretube.R
|
2022-09-08 22:11:57 +05:30
|
|
|
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
|
|
|
|
import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID
|
|
|
|
import com.github.libretube.constants.DOWNLOAD_SUCCESS_NOTIFICATION_ID
|
2022-08-14 13:25:28 +05:30
|
|
|
import com.github.libretube.extensions.TAG
|
2022-07-12 01:30:56 +05:30
|
|
|
import com.github.libretube.obj.DownloadType
|
2022-03-03 12:08:36 +05:30
|
|
|
import java.io.File
|
2022-03-04 23:30:50 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
class DownloadService : Service() {
|
2022-07-12 01:30:56 +05:30
|
|
|
|
|
|
|
private lateinit var notification: NotificationCompat.Builder
|
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private var downloadId: Long = -1
|
2022-08-03 12:35:34 +05:30
|
|
|
private lateinit var videoName: String
|
2022-03-03 12:08:36 +05:30
|
|
|
private lateinit var videoUrl: String
|
|
|
|
private lateinit var audioUrl: String
|
2022-07-12 01:30:56 +05:30
|
|
|
private var downloadType: Int = 3
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-09-09 16:41:54 +05:30
|
|
|
private lateinit var downloadDir: File
|
|
|
|
|
2022-03-04 21:27:10 +05:30
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
2022-07-18 22:59:56 +05:30
|
|
|
Globals.IS_DOWNLOAD_RUNNING = true
|
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-08-03 12:35:34 +05:30
|
|
|
videoName = intent?.getStringExtra("videoName")!!
|
2022-03-03 12:08:36 +05:30
|
|
|
videoUrl = intent.getStringExtra("videoUrl")!!
|
|
|
|
audioUrl = intent.getStringExtra("audioUrl")!!
|
2022-08-03 12:35:34 +05:30
|
|
|
|
2022-08-24 21:26:57 +05:30
|
|
|
downloadType = if (audioUrl != "") {
|
|
|
|
DownloadType.AUDIO
|
|
|
|
} else if (videoUrl != "") {
|
|
|
|
DownloadType.VIDEO
|
|
|
|
} else {
|
|
|
|
DownloadType.NONE
|
|
|
|
}
|
2022-07-12 01:30:56 +05:30
|
|
|
if (downloadType != DownloadType.NONE) {
|
2022-06-05 23:10:16 +05:30
|
|
|
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-09-09 16:41:54 +05:30
|
|
|
downloadDir = File(
|
|
|
|
this.getExternalFilesDir(null),
|
|
|
|
"video"
|
2022-06-05 23:10:16 +05:30
|
|
|
)
|
2022-06-09 17:52:07 +05:30
|
|
|
|
2022-09-09 16:41:54 +05:30
|
|
|
if (!downloadDir.exists()) downloadDir.mkdirs()
|
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) {
|
2022-07-12 01:30:56 +05:30
|
|
|
DownloadType.VIDEO -> {
|
2022-06-17 20:20:48 +05:30
|
|
|
downloadId = downloadManagerRequest(
|
|
|
|
getString(R.string.video),
|
|
|
|
getString(R.string.downloading),
|
|
|
|
videoUrl,
|
2022-09-09 16:41:54 +05:30
|
|
|
Uri.fromFile(
|
|
|
|
File(downloadDir, videoName)
|
|
|
|
)
|
2022-06-17 20:20:48 +05:30
|
|
|
)
|
2022-06-05 23:10:16 +05:30
|
|
|
}
|
2022-07-12 01:30:56 +05:30
|
|
|
DownloadType.AUDIO -> {
|
2022-06-17 20:20:48 +05:30
|
|
|
downloadId = downloadManagerRequest(
|
|
|
|
getString(R.string.audio),
|
|
|
|
getString(R.string.downloading),
|
|
|
|
audioUrl,
|
2022-09-09 16:41:54 +05:30
|
|
|
Uri.fromFile(
|
|
|
|
File(downloadDir, videoName)
|
|
|
|
)
|
2022-06-17 20:20:48 +05:30
|
|
|
)
|
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) {
|
2022-08-14 13:25:28 +05:30
|
|
|
Log.e(TAG(), "download error $e")
|
2022-08-03 12:35:34 +05:30
|
|
|
downloadFailedNotification()
|
2022-03-03 12:08:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-09 16:41:54 +05:30
|
|
|
if (downloadId != id) return
|
|
|
|
|
|
|
|
downloadSucceededNotification()
|
|
|
|
onDestroy()
|
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,
|
2022-09-09 16:41:54 +05:30
|
|
|
destination: Uri
|
2022-06-05 22:02:32 +05:30
|
|
|
): 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
|
2022-09-09 16:41:54 +05:30
|
|
|
.setDestinationUri(destination)
|
2022-06-05 21:38:47 +05:30
|
|
|
.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 downloadFailedNotification() {
|
2022-07-30 14:51:18 +05:30
|
|
|
val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
|
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-07-30 14:51:18 +05:30
|
|
|
notify(DOWNLOAD_FAILURE_NOTIFICATION_ID, builder.build())
|
2022-06-05 21:38:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:10:16 +05:30
|
|
|
private fun downloadSucceededNotification() {
|
2022-08-14 13:25:28 +05:30
|
|
|
Log.i(TAG(), "Download succeeded")
|
2022-07-30 14:51:18 +05:30
|
|
|
val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
|
2022-06-05 23:10:16 +05:30
|
|
|
.setSmallIcon(R.drawable.ic_download)
|
|
|
|
.setContentTitle(resources.getString(R.string.success))
|
2022-08-03 12:35:34 +05:30
|
|
|
.setContentText(getString(R.string.downloadsucceeded))
|
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-07-30 14:51:18 +05:30
|
|
|
notify(DOWNLOAD_SUCCESS_NOTIFICATION_ID, builder.build())
|
2022-06-05 21:38:47 +05:30
|
|
|
}
|
2022-06-05 23:10:16 +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-07-18 22:59:56 +05:30
|
|
|
Globals.IS_DOWNLOAD_RUNNING = false
|
2022-08-14 13:25:28 +05:30
|
|
|
Log.d(TAG(), "dl finished!")
|
2022-09-08 22:11:57 +05:30
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
|
|
} else {
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
stopForeground(true)
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:10:16 +05:30
|
|
|
stopService(Intent(this@DownloadService, DownloadService::class.java))
|
2022-03-03 12:08:36 +05:30
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
}
|