pull/1395/head
gedoor 3 years ago
parent 9d3f7980e2
commit 8943402e3b
  1. 10
      app/src/main/java/io/legado/app/service/DownloadService.kt
  2. 4
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt
  3. 10
      app/src/main/java/io/legado/app/utils/IntentType.kt

@ -12,6 +12,7 @@ import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.utils.IntentType
import io.legado.app.utils.openFileUri import io.legado.app.utils.openFileUri
import io.legado.app.utils.servicePendingIntent import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
@ -57,7 +58,7 @@ class DownloadService : BaseService() {
IntentAction.play -> { IntentAction.play -> {
val id = intent.getLongExtra("downloadId", 0) val id = intent.getLongExtra("downloadId", 0)
if (completeDownloads.contains(id)) { if (completeDownloads.contains(id)) {
openDownload(id) openDownload(id, downloads[id]?.second)
} else { } else {
toastOnUi("未完成,下载的文件夹Download") toastOnUi("未完成,下载的文件夹Download")
} }
@ -113,7 +114,7 @@ class DownloadService : BaseService() {
completeDownloads.add(downloadId) completeDownloads.add(downloadId)
val fileName = downloads[downloadId]?.second val fileName = downloads[downloadId]?.second
if (fileName?.endsWith(".apk") == true) { if (fileName?.endsWith(".apk") == true) {
openDownload(downloadId) openDownload(downloadId, fileName)
} else { } else {
toastOnUi("$fileName ${getString(R.string.download_success)}") 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 -> downloadManager.getUriForDownloadedFile(downloadId)?.let { uri ->
openFileUri(uri) val type = IntentType.from(fileName)
openFileUri(uri, type)
} }
} }

@ -293,7 +293,7 @@ fun Context.openUrl(uri: Uri) {
} }
} }
fun Context.openFileUri(uri: Uri) { fun Context.openFileUri(uri: Uri, type: String?) {
val intent = Intent() val intent = Intent()
intent.action = Intent.ACTION_VIEW intent.action = Intent.ACTION_VIEW
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
@ -301,7 +301,7 @@ fun Context.openFileUri(uri: Uri) {
//7.0版本以上 //7.0版本以上
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
} }
intent.setDataAndType(uri, IntentType.from(uri)) intent.setDataAndType(uri, type)
try { try {
startActivity(intent) startActivity(intent)
} catch (e: Exception) { } catch (e: Exception) {

@ -6,24 +6,24 @@ import java.io.File
object IntentType { object IntentType {
@JvmStatic @JvmStatic
fun from(uri: Uri): String { fun from(uri: Uri): String? {
return from(uri.toString()) return from(uri.toString())
} }
@JvmStatic @JvmStatic
fun from(file: File): String { fun from(file: File): String? {
return from(file.absolutePath) return from(file.absolutePath)
} }
@JvmStatic @JvmStatic
fun from(path: String): String { fun from(path: String?): String? {
return when (path.substringAfterLast(".").lowercase()) { return when (path?.substringAfterLast(".")?.lowercase()) {
"apk" -> "application/vnd.android.package-archive" "apk" -> "application/vnd.android.package-archive"
"m4a", "mp3", "mid", "xmf", "ogg", "wav" -> "video/*" "m4a", "mp3", "mid", "xmf", "ogg", "wav" -> "video/*"
"3gp", "mp4" -> "audio/*" "3gp", "mp4" -> "audio/*"
"jpg", "gif", "png", "jpeg", "bmp" -> "image/*" "jpg", "gif", "png", "jpeg", "bmp" -> "image/*"
"txt", "json" -> "text/plain" "txt", "json" -> "text/plain"
else -> "*/*" else -> null
} }
} }

Loading…
Cancel
Save