diff --git a/app/src/main/java/io/legado/app/model/Download.kt b/app/src/main/java/io/legado/app/model/Download.kt index 9a1cb907f..b4c7d3c17 100644 --- a/app/src/main/java/io/legado/app/model/Download.kt +++ b/app/src/main/java/io/legado/app/model/Download.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 { action = IntentAction.start putExtra("downloadId", downloadId) @@ -15,10 +28,4 @@ object Download { } } - fun stop(context: Context) { - context.startService { - action = IntentAction.stop - } - } - } \ No newline at end of file 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 2dddc80ad..20c77dbcb 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 @@ -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(false), @@ -137,15 +134,7 @@ class ReadRssActivity : VMBaseActivity 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) } }