change download destination

This commit is contained in:
Bnyro 2022-09-09 13:11:54 +02:00
parent c38820a110
commit 193c9b0e46
2 changed files with 20 additions and 83 deletions

View File

@ -1,7 +1,6 @@
package com.github.libretube.services
import android.app.DownloadManager
import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
@ -9,10 +8,6 @@ import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
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.util.Log
import androidx.core.app.NotificationCompat
@ -21,12 +16,9 @@ import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.constants.DOWNLOAD_CHANNEL_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.PreferenceKeys
import com.github.libretube.extensions.TAG
import com.github.libretube.obj.DownloadType
import com.github.libretube.util.PreferenceHelper
import java.io.File
class DownloadService : Service() {
@ -39,10 +31,8 @@ class DownloadService : Service() {
private lateinit var audioUrl: String
private var downloadType: Int = 3
private lateinit var audioDir: File
private lateinit var videoDir: File
private lateinit var libretubeDir: File
private lateinit var tempDir: File
private lateinit var downloadDir: File
override fun onCreate() {
super.onCreate()
Globals.IS_DOWNLOAD_RUNNING = true
@ -61,7 +51,6 @@ class DownloadService : Service() {
DownloadType.NONE
}
if (downloadType != DownloadType.NONE) {
downloadNotification(intent)
downloadManager()
} else {
onDestroy()
@ -75,37 +64,12 @@ class DownloadService : Service() {
}
private fun downloadManager() {
// create folder for temporary files
tempDir = File(
applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS),
".tmp"
downloadDir = File(
this.getExternalFilesDir(null),
"video"
)
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, "")
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())
if (!downloadDir.exists()) downloadDir.mkdirs()
// start download
try {
@ -115,21 +79,23 @@ class DownloadService : Service() {
)
when (downloadType) {
DownloadType.VIDEO -> {
videoDir = File(libretubeDir, videoName)
downloadId = downloadManagerRequest(
getString(R.string.video),
getString(R.string.downloading),
videoUrl,
videoDir
Uri.fromFile(
File(downloadDir, videoName)
)
)
}
DownloadType.AUDIO -> {
audioDir = File(libretubeDir, videoName)
downloadId = downloadManagerRequest(
getString(R.string.audio),
getString(R.string.downloading),
audioUrl,
audioDir
Uri.fromFile(
File(downloadDir, videoName)
)
)
}
}
@ -144,34 +110,24 @@ class DownloadService : Service() {
// Fetching the download id received with the broadcast
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
// Checking if the received broadcast is for our enqueued download by matching download id
if (downloadId == id) {
if (downloadType == DownloadType.MUX) {
downloadManagerRequest(
getString(R.string.audio),
getString(R.string.downloading),
audioUrl,
audioDir
)
} else {
if (downloadId != id) return
downloadSucceededNotification()
onDestroy()
}
}
}
}
private fun downloadManagerRequest(
title: String,
descriptionText: String,
url: String,
fileDir: File
destination: Uri
): 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))
.setDestinationUri(destination)
.setAllowedOverMetered(true) // Set if download is allowed on Mobile network
.setAllowedOverRoaming(true) //
val downloadManager: DownloadManager =
@ -179,27 +135,6 @@ class DownloadService : Service() {
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() {
val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_download)

View File

@ -2,6 +2,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--
<PreferenceCategory app:title="@string/downloads">
<ListPreference
@ -31,6 +32,7 @@
app:title="@string/share_with_time" />
</PreferenceCategory>
-->
<PreferenceCategory app:title="@string/advanced">