download resume bug fixed

This commit is contained in:
LagradOst 2021-07-05 20:43:28 +02:00
parent d4c0c17859
commit 18a727abe5

View file

@ -41,7 +41,7 @@ const val CHANNEL_DESCRIPT = "The download notification channel"
object VideoDownloadManager { object VideoDownloadManager {
var maxConcurrentDownloads = 3 var maxConcurrentDownloads = 3
var currentDownloads = 0 private var currentDownloads: Int = 0
private const val USER_AGENT = private const val USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
@ -280,7 +280,8 @@ object VideoDownloadManager {
actionResultIntent.putExtra("id", ep.id) actionResultIntent.putExtra("id", ep.id)
val pending: PendingIntent = PendingIntent.getService( val pending: PendingIntent = PendingIntent.getService(
context, 4337 + index + ep.id, // BECAUSE episodes lying near will have the same id +1, index will give the same requested as the previous episode, *100000 fixes this
context, (4337 + index*100000 + ep.id),
actionResultIntent, actionResultIntent,
PendingIntent.FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT
) )
@ -401,7 +402,8 @@ object VideoDownloadManager {
if (isScopedStorage()) { if (isScopedStorage()) {
val cr = context.contentResolver ?: return ERROR_CONTENT_RESOLVER_NOT_FOUND val cr = context.contentResolver ?: return ERROR_CONTENT_RESOLVER_NOT_FOUND
val currentExistingFile = cr.getExistingDownloadUriOrNullQ(relativePath, displayName) // CURRENT FILE WITH THE SAME PATH val currentExistingFile =
cr.getExistingDownloadUriOrNullQ(relativePath, displayName) // CURRENT FILE WITH THE SAME PATH
fileLength = fileLength =
if (currentExistingFile == null || !resume) 0 else cr.openFileDescriptor(currentExistingFile, "r") if (currentExistingFile == null || !resume) 0 else cr.openFileDescriptor(currentExistingFile, "r")
@ -515,7 +517,7 @@ object VideoDownloadManager {
) )
} }
fun onEvent(event: Pair<Int, DownloadActionType>) { events += { event ->
if (event.first == ep.id) { if (event.first == ep.id) {
when (event.second) { when (event.second) {
DownloadActionType.Pause -> { DownloadActionType.Pause -> {
@ -531,8 +533,6 @@ object VideoDownloadManager {
} }
} }
events += ::onEvent
// UPDATE DOWNLOAD NOTIFICATION // UPDATE DOWNLOAD NOTIFICATION
val notificationCoroutine = main { val notificationCoroutine = main {
while (true) { while (true) {
@ -568,7 +568,6 @@ object VideoDownloadManager {
} }
// REMOVE AND EXIT ALL // REMOVE AND EXIT ALL
events -= ::onEvent
fileStream.closeStream() fileStream.closeStream()
connectionInputStream.closeStream() connectionInputStream.closeStream()
notificationCoroutine.cancel() notificationCoroutine.cancel()
@ -591,11 +590,12 @@ object VideoDownloadManager {
private fun downloadCheck(context: Context) { private fun downloadCheck(context: Context) {
if (currentDownloads < maxConcurrentDownloads && downloadQueue.size > 0) { if (currentDownloads < maxConcurrentDownloads && downloadQueue.size > 0) {
currentDownloads++
val pkg = downloadQueue.removeFirst() val pkg = downloadQueue.removeFirst()
val item = pkg.item val item = pkg.item
currentDownloads++
try {
main { main {
try {
for (index in (pkg.linkIndex ?: 0) until item.links.size) { for (index in (pkg.linkIndex ?: 0) until item.links.size) {
val link = item.links[index] val link = item.links[index]
val resume = pkg.linkIndex == index val resume = pkg.linkIndex == index
@ -611,7 +611,6 @@ object VideoDownloadManager {
break break
} }
} }
}
} catch (e: Exception) { } catch (e: Exception) {
logError(e) logError(e)
} finally { } finally {
@ -620,6 +619,7 @@ object VideoDownloadManager {
} }
} }
} }
}
fun downloadFromResume(context: Context, pkg: DownloadResumePackage) { fun downloadFromResume(context: Context, pkg: DownloadResumePackage) {
downloadQueue.addLast(pkg) downloadQueue.addLast(pkg)