From 58d9a55cd2f760a243aa3babcf779f87a2245446 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 25 Jan 2023 18:05:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/DownloadService.kt | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index f3e5aba3b..e130dbaed 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -11,6 +11,7 @@ import androidx.core.app.NotificationCompat import io.legado.app.R import io.legado.app.base.BaseService import io.legado.app.constant.AppConst +import io.legado.app.constant.AppLog import io.legado.app.constant.IntentAction import io.legado.app.help.IntentType import io.legado.app.utils.openFileUri @@ -71,6 +72,9 @@ class DownloadService : BaseService() { return super.onStartCommand(intent, flags, startId) } + /** + * 开始下载 + */ @Synchronized private fun startDownload(url: String?, fileName: String?) { if (url == null || fileName == null) { @@ -105,6 +109,9 @@ class DownloadService : BaseService() { } } + /** + * 取消下载 + */ @Synchronized private fun removeDownload(downloadId: Long) { if (!completeDownloads.contains(downloadId)) { @@ -115,14 +122,15 @@ class DownloadService : BaseService() { notificationManager.cancel(downloadId.toInt()) } + /** + * 下载成功 + */ @Synchronized private fun successDownload(downloadId: Long) { if (!completeDownloads.contains(downloadId)) { completeDownloads.add(downloadId) val fileName = downloads[downloadId]?.second - kotlin.runCatching { - openDownload(downloadId, fileName) - } + openDownload(downloadId, fileName) } } @@ -136,7 +144,9 @@ class DownloadService : BaseService() { } } - //查询下载进度 + /** + * 查询下载进度 + */ @Synchronized private fun queryState() { if (downloads.isEmpty()) { @@ -174,13 +184,23 @@ class DownloadService : BaseService() { } } + /** + * 打开下载文件 + */ private fun openDownload(downloadId: Long, fileName: String?) { - downloadManager.getUriForDownloadedFile(downloadId)?.let { uri -> - val type = IntentType.from(fileName) - openFileUri(uri, type) + kotlin.runCatching { + downloadManager.getUriForDownloadedFile(downloadId)?.let { uri -> + val type = IntentType.from(fileName) + openFileUri(uri, type) + } + }.onFailure { + AppLog.put("打开下载文件${fileName}出错", it) } } + /** + * 更新下载进度条 + */ private fun upSummaryNotification() { val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload) .setSmallIcon(R.drawable.ic_download)