pull/1395/head
gedoor 3 years ago
parent 205895dfd6
commit 3c8ae2b6da
  1. 30
      app/src/main/java/io/legado/app/service/DownloadService.kt
  2. 4
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  3. 4
      app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt
  4. 30
      app/src/main/java/io/legado/app/utils/IntentType.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<Long, Pair<String, String>>()
@ -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 {

@ -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<ActivityRssReadBinding, ReadRssViewModel>
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)
}

@ -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<ActivityWebViewBinding, WebViewModel>() {
@ -88,7 +89,8 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
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)
}

@ -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 -> "*/*"
}
}
}
Loading…
Cancel
Save