pull/1344/head
gedoor 3 years ago
parent c704535fbd
commit 671219b8fb
  1. 21
      app/src/main/java/io/legado/app/model/Download.kt
  2. 13
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt

@ -1,13 +1,26 @@
package io.legado.app.model
import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.os.Environment
import io.legado.app.constant.IntentAction
import io.legado.app.service.DownloadService
import io.legado.app.utils.startService
import splitties.systemservices.downloadManager
object Download {
fun start(context: Context, downloadId: Long, fileName: String) {
fun start(context: Context, url: String, fileName: String) {
// 指定下载地址
val request = DownloadManager.Request(Uri.parse(url))
// 设置通知
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
// 设置下载文件保存的路径和文件名
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
// 添加一个下载任务
val downloadId = downloadManager.enqueue(request)
context.startService<DownloadService> {
action = IntentAction.start
putExtra("downloadId", downloadId)
@ -15,10 +28,4 @@ object Download {
}
}
fun stop(context: Context) {
context.startService<DownloadService> {
action = IntentAction.stop
}
}
}

@ -1,12 +1,10 @@
package io.legado.app.ui.rss.read
import android.annotation.SuppressLint
import android.app.DownloadManager
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.*
import android.webkit.*
import androidx.activity.viewModels
@ -30,7 +28,6 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.launch
import org.apache.commons.text.StringEscapeUtils
import org.jsoup.Jsoup
import splitties.systemservices.downloadManager
class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>(false),
@ -137,15 +134,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
binding.webView.setDownloadListener { url, _, contentDisposition, _, _ ->
val fileName = URLUtil.guessFileName(url, contentDisposition, null)
binding.llView.longSnackbar(fileName, getString(R.string.action_download)) {
// 指定下载地址
val request = DownloadManager.Request(Uri.parse(url))
// 设置通知
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
// 设置下载文件保存的路径和文件名
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
// 添加一个下载任务
val downloadId = downloadManager.enqueue(request)
Download.start(this, downloadId, fileName)
Download.start(this, url, fileName)
}
}

Loading…
Cancel
Save