From 194d772b956529cff6f0c75fd186bc4ab9d6c78f Mon Sep 17 00:00:00 2001 From: Zion Huang Date: Sun, 1 Sep 2024 15:39:37 +0800 Subject: [PATCH] fix(download): cancel button --- .../zionhuang/music/playback/ExoDownloadService.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/zionhuang/music/playback/ExoDownloadService.kt b/app/src/main/java/com/zionhuang/music/playback/ExoDownloadService.kt index 76839b1f6..d6f53ca52 100644 --- a/app/src/main/java/com/zionhuang/music/playback/ExoDownloadService.kt +++ b/app/src/main/java/com/zionhuang/music/playback/ExoDownloadService.kt @@ -29,6 +29,15 @@ class ExoDownloadService : DownloadService( @Inject lateinit var downloadUtil: DownloadUtil + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (intent?.action == REMOVE_ALL_PENDING_DOWNLOADS) { + downloadManager.currentDownloads.forEach { download -> + downloadManager.removeDownload(download.request.id) + } + } + return super.onStartCommand(intent, flags, startId) + } + override fun getDownloadManager() = downloadUtil.downloadManager override fun getScheduler(): Scheduler = PlatformScheduler(this, JOB_ID) @@ -51,7 +60,7 @@ class ExoDownloadService : DownloadService( PendingIntent.getService( this, 0, - Intent(this, ExoDownloadService::class.java).setAction(ACTION_REMOVE_ALL_DOWNLOADS), + Intent(this, ExoDownloadService::class.java).setAction(REMOVE_ALL_PENDING_DOWNLOADS), PendingIntent.FLAG_IMMUTABLE ) ).build() @@ -87,5 +96,6 @@ class ExoDownloadService : DownloadService( const val CHANNEL_ID = "download" const val NOTIFICATION_ID = 1 const val JOB_ID = 1 + const val REMOVE_ALL_PENDING_DOWNLOADS = "REMOVE_ALL_PENDING_DOWNLOADS" } } \ No newline at end of file