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 254ab0d9f..7307f1cc0 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -9,12 +9,11 @@ import android.net.Uri import android.os.Build import android.os.Environment import androidx.core.app.NotificationCompat -import androidx.core.content.FileProvider 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.RealPathUtil +import io.legado.app.utils.IntentType import io.legado.app.utils.msg import io.legado.app.utils.servicePendingIntent import io.legado.app.utils.toastOnUi @@ -25,9 +24,10 @@ import kotlinx.coroutines.launch import splitties.init.appCtx import splitties.systemservices.downloadManager import splitties.systemservices.notificationManager -import java.io.File - +/** + * 下载文件 + */ class DownloadService : BaseService() { private val groupKey = "${appCtx.packageName}.download" private val downloads = hashMapOf>() @@ -58,12 +58,10 @@ class DownloadService : BaseService() { ) IntentAction.play -> { val id = intent.getLongExtra("downloadId", 0) - if (completeDownloads.contains(id) - && downloads[id]?.second?.endsWith(".apk") == true - ) { - installApk(id) + if (completeDownloads.contains(id)) { + openDownload(id) } else { - toastOnUi("下载的文件在Download文件夹") + toastOnUi("未完成,下载的文件夹Download") } } IntentAction.stop -> { @@ -117,7 +115,7 @@ class DownloadService : BaseService() { completeDownloads.add(downloadId) val fileName = downloads[downloadId]?.second if (fileName?.endsWith(".apk") == true) { - installApk(downloadId) + openDownload(downloadId) } else { toastOnUi("$fileName ${getString(R.string.download_success)}") } @@ -172,22 +170,18 @@ class DownloadService : BaseService() { } } - private fun installApk(downloadId: Long) { - downloadManager.getUriForDownloadedFile(downloadId)?.let { - val filePath = RealPathUtil.getPath(this, it) ?: return - val file = File(filePath) + private fun openDownload(downloadId: Long) { + downloadManager.getUriForDownloadedFile(downloadId)?.let { uri -> //调用系统安装apk val intent = Intent() intent.action = Intent.ACTION_VIEW intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0版本以上 - val contentUrl = FileProvider.getUriForFile(this, AppConst.authority, file) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - intent.setDataAndType(contentUrl, "application/vnd.android.package-archive") + intent.setDataAndType(uri, IntentType.from(uri)) } else { - val uri: Uri = Uri.fromFile(file) - intent.setDataAndType(uri, "application/vnd.android.package-archive") + intent.setDataAndType(uri, IntentType.from(uri)) } try { diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 803e2aa03..c1ecf1b85 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -27,6 +27,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.launch import org.apache.commons.text.StringEscapeUtils import org.jsoup.Jsoup +import java.net.URLDecoder /** * rss阅读界面 @@ -132,7 +133,8 @@ class ReadRssActivity : VMBaseActivity return@setOnLongClickListener false } binding.webView.setDownloadListener { url, _, contentDisposition, _, _ -> - val fileName = URLUtil.guessFileName(url, contentDisposition, null) + var fileName = URLUtil.guessFileName(url, contentDisposition, null) + fileName = URLDecoder.decode(fileName, "UTF-8") binding.llView.longSnackbar(fileName, getString(R.string.action_download)) { Download.start(this, url, fileName) } diff --git a/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt b/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt index 5d6f79782..07c2fea8f 100644 --- a/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt +++ b/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt @@ -23,6 +23,7 @@ import io.legado.app.ui.association.OnLineImportActivity import io.legado.app.ui.document.HandleFileContract import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import java.net.URLDecoder class WebViewActivity : VMBaseActivity() { @@ -88,7 +89,8 @@ class WebViewActivity : VMBaseActivity() { return@setOnLongClickListener false } binding.webView.setDownloadListener { url, _, contentDisposition, _, _ -> - val fileName = URLUtil.guessFileName(url, contentDisposition, null) + var fileName = URLUtil.guessFileName(url, contentDisposition, null) + fileName = URLDecoder.decode(fileName, "UTF-8") binding.llView.longSnackbar(fileName, getString(R.string.action_download)) { Download.start(this, url, fileName) } diff --git a/app/src/main/java/io/legado/app/utils/IntentType.kt b/app/src/main/java/io/legado/app/utils/IntentType.kt new file mode 100644 index 000000000..5b05586b6 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/IntentType.kt @@ -0,0 +1,30 @@ +package io.legado.app.utils + +import android.net.Uri +import java.io.File + +object IntentType { + + @JvmStatic + fun from(uri: Uri): String { + return from(uri.toString()) + } + + @JvmStatic + fun from(file: File): String { + return from(file.absolutePath) + } + + @JvmStatic + 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 -> "*/*" + } + } + +} \ No newline at end of file