mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
change download destination
This commit is contained in:
parent
c38820a110
commit
193c9b0e46
@ -1,7 +1,6 @@
|
|||||||
package com.github.libretube.services
|
package com.github.libretube.services
|
||||||
|
|
||||||
import android.app.DownloadManager
|
import android.app.DownloadManager
|
||||||
import android.app.PendingIntent
|
|
||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
@ -9,10 +8,6 @@ import android.content.Intent
|
|||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Environment
|
|
||||||
import android.os.Environment.DIRECTORY_DOWNLOADS
|
|
||||||
import android.os.Environment.DIRECTORY_MOVIES
|
|
||||||
import android.os.Environment.DIRECTORY_MUSIC
|
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
@ -21,12 +16,9 @@ import com.github.libretube.Globals
|
|||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
|
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
|
||||||
import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID
|
import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID
|
||||||
import com.github.libretube.constants.DOWNLOAD_PENDING_NOTIFICATION_ID
|
|
||||||
import com.github.libretube.constants.DOWNLOAD_SUCCESS_NOTIFICATION_ID
|
import com.github.libretube.constants.DOWNLOAD_SUCCESS_NOTIFICATION_ID
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.obj.DownloadType
|
import com.github.libretube.obj.DownloadType
|
||||||
import com.github.libretube.util.PreferenceHelper
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class DownloadService : Service() {
|
class DownloadService : Service() {
|
||||||
@ -39,10 +31,8 @@ class DownloadService : Service() {
|
|||||||
private lateinit var audioUrl: String
|
private lateinit var audioUrl: String
|
||||||
private var downloadType: Int = 3
|
private var downloadType: Int = 3
|
||||||
|
|
||||||
private lateinit var audioDir: File
|
private lateinit var downloadDir: File
|
||||||
private lateinit var videoDir: File
|
|
||||||
private lateinit var libretubeDir: File
|
|
||||||
private lateinit var tempDir: File
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
Globals.IS_DOWNLOAD_RUNNING = true
|
Globals.IS_DOWNLOAD_RUNNING = true
|
||||||
@ -61,7 +51,6 @@ class DownloadService : Service() {
|
|||||||
DownloadType.NONE
|
DownloadType.NONE
|
||||||
}
|
}
|
||||||
if (downloadType != DownloadType.NONE) {
|
if (downloadType != DownloadType.NONE) {
|
||||||
downloadNotification(intent)
|
|
||||||
downloadManager()
|
downloadManager()
|
||||||
} else {
|
} else {
|
||||||
onDestroy()
|
onDestroy()
|
||||||
@ -75,37 +64,12 @@ class DownloadService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun downloadManager() {
|
private fun downloadManager() {
|
||||||
// create folder for temporary files
|
downloadDir = File(
|
||||||
tempDir = File(
|
this.getExternalFilesDir(null),
|
||||||
applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS),
|
"video"
|
||||||
".tmp"
|
|
||||||
)
|
)
|
||||||
if (!tempDir.exists()) {
|
|
||||||
tempDir.mkdirs()
|
|
||||||
Log.e(TAG(), "Directory make")
|
|
||||||
} else {
|
|
||||||
tempDir.deleteRecursively()
|
|
||||||
tempDir.mkdirs()
|
|
||||||
Log.e(TAG(), "Directory already have")
|
|
||||||
}
|
|
||||||
|
|
||||||
val downloadLocationPref = PreferenceHelper.getString(PreferenceKeys.DOWNLOAD_LOCATION, "")
|
if (!downloadDir.exists()) downloadDir.mkdirs()
|
||||||
val folderName = PreferenceHelper.getString(PreferenceKeys.DOWNLOAD_FOLDER, "LibreTube")
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
libretubeDir = File(
|
|
||||||
location,
|
|
||||||
folderName
|
|
||||||
)
|
|
||||||
if (!libretubeDir.exists()) libretubeDir.mkdirs()
|
|
||||||
Log.i(TAG(), libretubeDir.toString())
|
|
||||||
|
|
||||||
// start download
|
// start download
|
||||||
try {
|
try {
|
||||||
@ -115,21 +79,23 @@ class DownloadService : Service() {
|
|||||||
)
|
)
|
||||||
when (downloadType) {
|
when (downloadType) {
|
||||||
DownloadType.VIDEO -> {
|
DownloadType.VIDEO -> {
|
||||||
videoDir = File(libretubeDir, videoName)
|
|
||||||
downloadId = downloadManagerRequest(
|
downloadId = downloadManagerRequest(
|
||||||
getString(R.string.video),
|
getString(R.string.video),
|
||||||
getString(R.string.downloading),
|
getString(R.string.downloading),
|
||||||
videoUrl,
|
videoUrl,
|
||||||
videoDir
|
Uri.fromFile(
|
||||||
|
File(downloadDir, videoName)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
DownloadType.AUDIO -> {
|
DownloadType.AUDIO -> {
|
||||||
audioDir = File(libretubeDir, videoName)
|
|
||||||
downloadId = downloadManagerRequest(
|
downloadId = downloadManagerRequest(
|
||||||
getString(R.string.audio),
|
getString(R.string.audio),
|
||||||
getString(R.string.downloading),
|
getString(R.string.downloading),
|
||||||
audioUrl,
|
audioUrl,
|
||||||
audioDir
|
Uri.fromFile(
|
||||||
|
File(downloadDir, videoName)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,34 +110,24 @@ class DownloadService : Service() {
|
|||||||
// Fetching the download id received with the broadcast
|
// Fetching the download id received with the broadcast
|
||||||
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
||||||
// Checking if the received broadcast is for our enqueued download by matching download id
|
// Checking if the received broadcast is for our enqueued download by matching download id
|
||||||
if (downloadId == id) {
|
if (downloadId != id) return
|
||||||
if (downloadType == DownloadType.MUX) {
|
|
||||||
downloadManagerRequest(
|
|
||||||
getString(R.string.audio),
|
|
||||||
getString(R.string.downloading),
|
|
||||||
audioUrl,
|
|
||||||
audioDir
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
downloadSucceededNotification()
|
downloadSucceededNotification()
|
||||||
onDestroy()
|
onDestroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun downloadManagerRequest(
|
private fun downloadManagerRequest(
|
||||||
title: String,
|
title: String,
|
||||||
descriptionText: String,
|
descriptionText: String,
|
||||||
url: String,
|
url: String,
|
||||||
fileDir: File
|
destination: Uri
|
||||||
): Long {
|
): Long {
|
||||||
val request: DownloadManager.Request =
|
val request: DownloadManager.Request =
|
||||||
DownloadManager.Request(Uri.parse(url))
|
DownloadManager.Request(Uri.parse(url))
|
||||||
.setTitle(title) // Title of the Download Notification
|
.setTitle(title) // Title of the Download Notification
|
||||||
.setDescription(descriptionText) // Description of the Download Notification
|
.setDescription(descriptionText) // Description of the Download Notification
|
||||||
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // Visibility of the download Notification
|
.setDestinationUri(destination)
|
||||||
.setDestinationUri(Uri.fromFile(fileDir))
|
|
||||||
.setAllowedOverMetered(true) // Set if download is allowed on Mobile network
|
.setAllowedOverMetered(true) // Set if download is allowed on Mobile network
|
||||||
.setAllowedOverRoaming(true) //
|
.setAllowedOverRoaming(true) //
|
||||||
val downloadManager: DownloadManager =
|
val downloadManager: DownloadManager =
|
||||||
@ -179,27 +135,6 @@ class DownloadService : Service() {
|
|||||||
return downloadManager.enqueue(request)
|
return downloadManager.enqueue(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun downloadNotification(intent: Intent) {
|
|
||||||
val 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, DOWNLOAD_CHANNEL_ID)
|
|
||||||
.setSmallIcon(R.drawable.ic_download)
|
|
||||||
.setContentTitle("LibreTube")
|
|
||||||
.setContentText(getString(R.string.downloading))
|
|
||||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
||||||
.setOngoing(true)
|
|
||||||
.setOnlyAlertOnce(true)
|
|
||||||
.setProgress(100, 0, true)
|
|
||||||
.setContentIntent(pendingIntent)
|
|
||||||
.setAutoCancel(true)
|
|
||||||
startForeground(DOWNLOAD_PENDING_NOTIFICATION_ID, notification.build())
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun downloadFailedNotification() {
|
private fun downloadFailedNotification() {
|
||||||
val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
|
val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
|
||||||
.setSmallIcon(R.drawable.ic_download)
|
.setSmallIcon(R.drawable.ic_download)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<!--
|
||||||
<PreferenceCategory app:title="@string/downloads">
|
<PreferenceCategory app:title="@string/downloads">
|
||||||
|
|
||||||
<ListPreference
|
<ListPreference
|
||||||
@ -31,6 +32,7 @@
|
|||||||
app:title="@string/share_with_time" />
|
app:title="@string/share_with_time" />
|
||||||
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
-->
|
||||||
|
|
||||||
<PreferenceCategory app:title="@string/advanced">
|
<PreferenceCategory app:title="@string/advanced">
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user