From 8943402e3b9f3d84b73cedccd60f0895540edf90 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 1 Oct 2021 09:30:36 +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 --- .../main/java/io/legado/app/service/DownloadService.kt | 10 ++++++---- .../main/java/io/legado/app/utils/ContextExtensions.kt | 4 ++-- app/src/main/java/io/legado/app/utils/IntentType.kt | 10 +++++----- 3 files changed, 13 insertions(+), 11 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 013e6c630..ebe68c7fa 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -12,6 +12,7 @@ import io.legado.app.R import io.legado.app.base.BaseService import io.legado.app.constant.AppConst import io.legado.app.constant.IntentAction +import io.legado.app.utils.IntentType import io.legado.app.utils.openFileUri import io.legado.app.utils.servicePendingIntent import io.legado.app.utils.toastOnUi @@ -57,7 +58,7 @@ class DownloadService : BaseService() { IntentAction.play -> { val id = intent.getLongExtra("downloadId", 0) if (completeDownloads.contains(id)) { - openDownload(id) + openDownload(id, downloads[id]?.second) } else { toastOnUi("未完成,下载的文件夹Download") } @@ -113,7 +114,7 @@ class DownloadService : BaseService() { completeDownloads.add(downloadId) val fileName = downloads[downloadId]?.second if (fileName?.endsWith(".apk") == true) { - openDownload(downloadId) + openDownload(downloadId, fileName) } else { toastOnUi("$fileName ${getString(R.string.download_success)}") } @@ -168,9 +169,10 @@ class DownloadService : BaseService() { } } - private fun openDownload(downloadId: Long) { + private fun openDownload(downloadId: Long, fileName: String?) { downloadManager.getUriForDownloadedFile(downloadId)?.let { uri -> - openFileUri(uri) + val type = IntentType.from(fileName) + openFileUri(uri, type) } } diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 97cff9d38..fea483582 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -293,7 +293,7 @@ fun Context.openUrl(uri: Uri) { } } -fun Context.openFileUri(uri: Uri) { +fun Context.openFileUri(uri: Uri, type: String?) { val intent = Intent() intent.action = Intent.ACTION_VIEW intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) @@ -301,7 +301,7 @@ fun Context.openFileUri(uri: Uri) { //7.0版本以上 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) } - intent.setDataAndType(uri, IntentType.from(uri)) + intent.setDataAndType(uri, type) try { startActivity(intent) } catch (e: Exception) { diff --git a/app/src/main/java/io/legado/app/utils/IntentType.kt b/app/src/main/java/io/legado/app/utils/IntentType.kt index 5b05586b6..e1b88f6c2 100644 --- a/app/src/main/java/io/legado/app/utils/IntentType.kt +++ b/app/src/main/java/io/legado/app/utils/IntentType.kt @@ -6,24 +6,24 @@ import java.io.File object IntentType { @JvmStatic - fun from(uri: Uri): String { + fun from(uri: Uri): String? { return from(uri.toString()) } @JvmStatic - fun from(file: File): String { + fun from(file: File): String? { return from(file.absolutePath) } @JvmStatic - fun from(path: String): String { - return when (path.substringAfterLast(".").lowercase()) { + fun from(path: String?): String? { + return when (path?.substringAfterLast(".")?.lowercase()) { "apk" -> "application/vnd.android.package-archive" "m4a", "mp3", "mid", "xmf", "ogg", "wav" -> "video/*" "3gp", "mp4" -> "audio/*" "jpg", "gif", "png", "jpeg", "bmp" -> "image/*" "txt", "json" -> "text/plain" - else -> "*/*" + else -> null } }