download stopping

This commit is contained in:
rebelonion 2024-01-20 23:41:22 -06:00
parent 45a341397b
commit 87c2d82462

View file

@ -71,6 +71,7 @@ class AnimeDownloaderService : Service() {
private val downloadJobs = mutableMapOf<String, Job>() private val downloadJobs = mutableMapOf<String, Job>()
private val mutex = Mutex() private val mutex = Mutex()
private var isCurrentlyProcessing = false private var isCurrentlyProcessing = false
private var currentTasks: MutableList<AnimeDownloadTask> = mutableListOf()
override fun onBind(intent: Intent?): IBinder? { override fun onBind(intent: Intent?): IBinder? {
// This is only required for bound services. // This is only required for bound services.
@ -133,6 +134,7 @@ class AnimeDownloaderService : Service() {
val task = AnimeServiceDataSingleton.downloadQueue.poll() val task = AnimeServiceDataSingleton.downloadQueue.poll()
if (task != null) { if (task != null) {
val job = launch { download(task) } val job = launch { download(task) }
currentTasks.add(task)
mutex.withLock { mutex.withLock {
downloadJobs[task.getTaskName()] = job downloadJobs[task.getTaskName()] = job
} }
@ -153,25 +155,29 @@ class AnimeDownloaderService : Service() {
@UnstableApi @UnstableApi
fun cancelDownload(taskName: String) { fun cancelDownload(taskName: String) {
CoroutineScope(Dispatchers.Default).launch {
mutex.withLock {
val url = val url =
AnimeServiceDataSingleton.downloadQueue.find { it.getTaskName() == taskName }?.video?.file?.url AnimeServiceDataSingleton.downloadQueue.find { it.getTaskName() == taskName }?.video?.file?.url
?: "" ?: currentTasks.find { it.getTaskName() == taskName }?.video?.file?.url ?: ""
if (url.isEmpty()) {
snackString("Failed to cancel download")
return
}
currentTasks.removeAll { it.getTaskName() == taskName }
DownloadService.sendSetStopReason( DownloadService.sendSetStopReason(
this@AnimeDownloaderService, this@AnimeDownloaderService,
ExoplayerDownloadService::class.java, ExoplayerDownloadService::class.java,
url, url,
androidx.media3.exoplayer.offline.Download.STATE_REMOVING, androidx.media3.exoplayer.offline.Download.STATE_STOPPED,
false false
) )
DownloadService.sendRemoveDownload( DownloadService.sendRemoveDownload(
this@AnimeDownloaderService, this@AnimeDownloaderService,
ExoplayerDownloadService::class.java, ExoplayerDownloadService::class.java,
url, url,
false false
) )
CoroutineScope(Dispatchers.Default).launch {
mutex.withLock {
downloadJobs[taskName]?.cancel() downloadJobs[taskName]?.cancel()
downloadJobs.remove(taskName) downloadJobs.remove(taskName)
AnimeServiceDataSingleton.downloadQueue.removeAll { it.getTaskName() == taskName } AnimeServiceDataSingleton.downloadQueue.removeAll { it.getTaskName() == taskName }
@ -279,6 +285,7 @@ class AnimeDownloaderService : Service() {
" episode: ${task.episode}" " episode: ${task.episode}"
) )
) )
currentTasks.removeAll { it.getTaskName() == task.getTaskName() }
broadcastDownloadFailed(task.episode) broadcastDownloadFailed(task.episode)
break break
} }
@ -301,6 +308,7 @@ class AnimeDownloaderService : Service() {
DownloadedType.Type.ANIME, DownloadedType.Type.ANIME,
) )
) )
currentTasks.removeAll { it.getTaskName() == task.getTaskName() }
broadcastDownloadFinished(task.episode) broadcastDownloadFinished(task.episode)
break break
} }
@ -323,10 +331,12 @@ class AnimeDownloaderService : Service() {
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
if (e.message?.contains("Coroutine was cancelled") == false) { //wut
logger("Exception while downloading file: ${e.message}") logger("Exception while downloading file: ${e.message}")
snackString("Exception while downloading file: ${e.message}") snackString("Exception while downloading file: ${e.message}")
e.printStackTrace() e.printStackTrace()
FirebaseCrashlytics.getInstance().recordException(e) FirebaseCrashlytics.getInstance().recordException(e)
}
broadcastDownloadFailed(task.episode) broadcastDownloadFailed(task.episode)
} }
} }