From 671219b8fb07b081dffbfe42c0d401b276404615 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 24 Sep 2021 09:45:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/model/Download.kt | 21 ++++++++++++------- .../legado/app/ui/rss/read/ReadRssActivity.kt | 13 +----------- 2 files changed, 15 insertions(+), 19 deletions(-) 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) } }