small download stuff

This commit is contained in:
LagradOst 2021-07-06 02:18:56 +02:00
parent 5536f41ecb
commit 3ef14b93fb

View file

@ -14,7 +14,6 @@ import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.core.net.toUri import androidx.core.net.toUri
import com.anggrayudi.storage.extension.closeStream
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.lagradost.cloudstream3.MainActivity import com.lagradost.cloudstream3.MainActivity
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
@ -35,10 +34,11 @@ import java.net.URL
import java.net.URLConnection import java.net.URLConnection
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import kotlin.collections.HashMap
const val CHANNEL_ID = "cloudstream3.general" const val DOWNLOAD_CHANNEL_ID = "cloudstream3.general"
const val CHANNEL_NAME = "Downloads" const val DOWNLOAD_CHANNEL_NAME = "Downloads"
const val CHANNEL_DESCRIPT = "The download notification channel" const val DOWNLOAD_CHANNEL_DESCRIPT = "The download notification channel"
object VideoDownloadManager { object VideoDownloadManager {
var maxConcurrentDownloads = 3 var maxConcurrentDownloads = 3
@ -96,7 +96,7 @@ object VideoDownloadManager {
) )
data class DownloadItem( data class DownloadItem(
val source: String, val source: String?,
val folder: String?, val folder: String?,
val ep: DownloadEpisodeMetadata, val ep: DownloadEpisodeMetadata,
val links: List<ExtractorLink> val links: List<ExtractorLink>
@ -114,6 +114,7 @@ object VideoDownloadManager {
) )
data class DownloadedFileInfoResult( data class DownloadedFileInfoResult(
val fileLength: Long,
val totalBytes: Long, val totalBytes: Long,
val path: Uri, val path: Uri,
) )
@ -133,6 +134,8 @@ object VideoDownloadManager {
private const val KEY_RESUME_PACKAGES = "download_resume" private const val KEY_RESUME_PACKAGES = "download_resume"
private const val KEY_DOWNLOAD_INFO = "download_info" private const val KEY_DOWNLOAD_INFO = "download_info"
val downloadStatus = HashMap<Int, DownloadType>()
val downloadStatusEvent = Event<Pair<Int, DownloadType>>()
val downloadEvent = Event<Pair<Int, DownloadActionType>>() val downloadEvent = Event<Pair<Int, DownloadActionType>>()
val downloadProgressEvent = Event<Pair<Int, Long>>() val downloadProgressEvent = Event<Pair<Int, Long>>()
private val downloadQueue = LinkedList<DownloadResumePackage>() private val downloadQueue = LinkedList<DownloadResumePackage>()
@ -143,10 +146,10 @@ object VideoDownloadManager {
// Create the NotificationChannel, but only on API 26+ because // Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library // the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = CHANNEL_NAME //getString(R.string.channel_name) val name = DOWNLOAD_CHANNEL_NAME //getString(R.string.channel_name)
val descriptionText = CHANNEL_DESCRIPT//getString(R.string.channel_description) val descriptionText = DOWNLOAD_CHANNEL_DESCRIPT//getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { val channel = NotificationChannel(DOWNLOAD_CHANNEL_ID, name, importance).apply {
description = descriptionText description = descriptionText
} }
// Register the channel with the system // Register the channel with the system
@ -182,7 +185,7 @@ object VideoDownloadManager {
total: Long, total: Long,
) { ) {
main { // DON'T WANT TO SLOW IT DOWN main { // DON'T WANT TO SLOW IT DOWN
val builder = NotificationCompat.Builder(context, CHANNEL_ID) val builder = NotificationCompat.Builder(context, DOWNLOAD_CHANNEL_ID)
.setAutoCancel(true) .setAutoCancel(true)
.setColorized(true) .setColorized(true)
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
@ -518,6 +521,14 @@ object VideoDownloadManager {
isPaused -> DownloadType.IsPaused isPaused -> DownloadType.IsPaused
else -> DownloadType.IsDownloading else -> DownloadType.IsDownloading
} }
try {
downloadStatus[ep.id] = type
downloadStatusEvent.invoke(Pair(ep.id, type))
} catch (e: Exception) {
// IDK MIGHT ERROR
}
createNotification( createNotification(
context, context,
source, source,
@ -582,10 +593,16 @@ object VideoDownloadManager {
} }
// REMOVE AND EXIT ALL // REMOVE AND EXIT ALL
fileStream.closeStream() fileStream.close()
connectionInputStream.closeStream() connectionInputStream.close()
notificationCoroutine.cancel() notificationCoroutine.cancel()
try {
downloadStatus.remove(ep.id)
} catch (e: Exception) {
// IDK MIGHT ERROR
}
// RETURN MESSAGE // RETURN MESSAGE
return when { return when {
isFailed -> { isFailed -> {
@ -655,7 +672,7 @@ object VideoDownloadManager {
cr.getExistingDownloadUriOrNullQ(info.relativePath, info.displayName) ?: return null cr.getExistingDownloadUriOrNullQ(info.relativePath, info.displayName) ?: return null
val fileLength = cr.getFileLength(fileUri) val fileLength = cr.getFileLength(fileUri)
if (fileLength == 0L) return null if (fileLength == 0L) return null
return DownloadedFileInfoResult(fileLength, fileUri) return DownloadedFileInfoResult(fileLength, info.totalBytes, fileUri)
} else { } else {
val normalPath = val normalPath =
"${Environment.getExternalStorageDirectory()}${File.separatorChar}${info.relativePath}${info.displayName}".replace( "${Environment.getExternalStorageDirectory()}${File.separatorChar}${info.relativePath}${info.displayName}".replace(
@ -664,7 +681,7 @@ object VideoDownloadManager {
) )
val dFile = File(normalPath) val dFile = File(normalPath)
if (!dFile.exists()) return null if (!dFile.exists()) return null
return DownloadedFileInfoResult(dFile.length(), dFile.toUri()) return DownloadedFileInfoResult(dFile.length(), info.totalBytes, dFile.toUri())
} }
} }
@ -707,7 +724,7 @@ object VideoDownloadManager {
fun downloadEpisode( fun downloadEpisode(
context: Context, context: Context,
source: String, source: String?,
folder: String?, folder: String?,
ep: DownloadEpisodeMetadata, ep: DownloadEpisodeMetadata,
links: List<ExtractorLink> links: List<ExtractorLink>