优化
This commit is contained in:
@@ -11,6 +11,7 @@ import androidx.core.app.NotificationCompat
|
|||||||
import io.legado.app.R
|
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.AppLog
|
||||||
import io.legado.app.constant.IntentAction
|
import io.legado.app.constant.IntentAction
|
||||||
import io.legado.app.help.IntentType
|
import io.legado.app.help.IntentType
|
||||||
import io.legado.app.utils.openFileUri
|
import io.legado.app.utils.openFileUri
|
||||||
@@ -71,6 +72,9 @@ class DownloadService : BaseService() {
|
|||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始下载
|
||||||
|
*/
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun startDownload(url: String?, fileName: String?) {
|
private fun startDownload(url: String?, fileName: String?) {
|
||||||
if (url == null || fileName == null) {
|
if (url == null || fileName == null) {
|
||||||
@@ -105,6 +109,9 @@ class DownloadService : BaseService() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消下载
|
||||||
|
*/
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun removeDownload(downloadId: Long) {
|
private fun removeDownload(downloadId: Long) {
|
||||||
if (!completeDownloads.contains(downloadId)) {
|
if (!completeDownloads.contains(downloadId)) {
|
||||||
@@ -115,14 +122,15 @@ class DownloadService : BaseService() {
|
|||||||
notificationManager.cancel(downloadId.toInt())
|
notificationManager.cancel(downloadId.toInt())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载成功
|
||||||
|
*/
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun successDownload(downloadId: Long) {
|
private fun successDownload(downloadId: Long) {
|
||||||
if (!completeDownloads.contains(downloadId)) {
|
if (!completeDownloads.contains(downloadId)) {
|
||||||
completeDownloads.add(downloadId)
|
completeDownloads.add(downloadId)
|
||||||
val fileName = downloads[downloadId]?.second
|
val fileName = downloads[downloadId]?.second
|
||||||
kotlin.runCatching {
|
openDownload(downloadId, fileName)
|
||||||
openDownload(downloadId, fileName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +144,9 @@ class DownloadService : BaseService() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//查询下载进度
|
/**
|
||||||
|
* 查询下载进度
|
||||||
|
*/
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun queryState() {
|
private fun queryState() {
|
||||||
if (downloads.isEmpty()) {
|
if (downloads.isEmpty()) {
|
||||||
@@ -174,13 +184,23 @@ class DownloadService : BaseService() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开下载文件
|
||||||
|
*/
|
||||||
private fun openDownload(downloadId: Long, fileName: String?) {
|
private fun openDownload(downloadId: Long, fileName: String?) {
|
||||||
downloadManager.getUriForDownloadedFile(downloadId)?.let { uri ->
|
kotlin.runCatching {
|
||||||
val type = IntentType.from(fileName)
|
downloadManager.getUriForDownloadedFile(downloadId)?.let { uri ->
|
||||||
openFileUri(uri, type)
|
val type = IntentType.from(fileName)
|
||||||
|
openFileUri(uri, type)
|
||||||
|
}
|
||||||
|
}.onFailure {
|
||||||
|
AppLog.put("打开下载文件${fileName}出错", it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新下载进度条
|
||||||
|
*/
|
||||||
private fun upSummaryNotification() {
|
private fun upSummaryNotification() {
|
||||||
val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload)
|
val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload)
|
||||||
.setSmallIcon(R.drawable.ic_download)
|
.setSmallIcon(R.drawable.ic_download)
|
||||||
|
|||||||
Reference in New Issue
Block a user