|
|
|
@ -7,6 +7,7 @@ import android.content.Intent |
|
|
|
|
import android.content.IntentFilter |
|
|
|
|
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 |
|
|
|
@ -29,7 +30,8 @@ import java.io.File |
|
|
|
|
|
|
|
|
|
class DownloadService : BaseService() { |
|
|
|
|
private val groupKey = "${appCtx.packageName}.download" |
|
|
|
|
private val downloads = hashMapOf<Long, String>() |
|
|
|
|
private val downloadUrls = hashMapOf<Long, String>() |
|
|
|
|
private val downloadNames = hashMapOf<Long, String>() |
|
|
|
|
private val completeDownloads = hashSetOf<Long>() |
|
|
|
|
private var upStateJob: Job? = null |
|
|
|
|
private val downloadReceiver = object : BroadcastReceiver() { |
|
|
|
@ -52,13 +54,13 @@ class DownloadService : BaseService() { |
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
|
|
|
|
when (intent?.action) { |
|
|
|
|
IntentAction.start -> startDownload( |
|
|
|
|
intent.getLongExtra("downloadId", 0), |
|
|
|
|
intent.getStringExtra("fileName") ?: "未知文件" |
|
|
|
|
intent.getStringExtra("url"), |
|
|
|
|
intent.getStringExtra("fileName") |
|
|
|
|
) |
|
|
|
|
IntentAction.play -> { |
|
|
|
|
val id = intent.getLongExtra("downloadId", 0) |
|
|
|
|
if (completeDownloads.contains(id) |
|
|
|
|
&& downloads[id]?.endsWith(".apk") == true |
|
|
|
|
&& downloadNames[id]?.endsWith(".apk") == true |
|
|
|
|
) { |
|
|
|
|
installApk(id) |
|
|
|
|
} else { |
|
|
|
@ -74,13 +76,30 @@ class DownloadService : BaseService() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
private fun startDownload(downloadId: Long, fileName: String) { |
|
|
|
|
if (downloadId > 0) { |
|
|
|
|
downloads[downloadId] = fileName |
|
|
|
|
queryState() |
|
|
|
|
if (upStateJob == null) { |
|
|
|
|
checkDownloadState() |
|
|
|
|
private fun startDownload(url: String?, fileName: String?) { |
|
|
|
|
if (url == null || fileName == null) { |
|
|
|
|
if (downloadNames.isEmpty()) { |
|
|
|
|
stopSelf() |
|
|
|
|
} |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if (downloadUrls.values.contains(url)) { |
|
|
|
|
toastOnUi("已在下载列表") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
// 指定下载地址 |
|
|
|
|
val request = DownloadManager.Request(Uri.parse(url)) |
|
|
|
|
// 设置通知 |
|
|
|
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN) |
|
|
|
|
// 设置下载文件保存的路径和文件名 |
|
|
|
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) |
|
|
|
|
// 添加一个下载任务 |
|
|
|
|
val downloadId = downloadManager.enqueue(request) |
|
|
|
|
downloadUrls[downloadId] = url |
|
|
|
|
downloadNames[downloadId] = fileName |
|
|
|
|
queryState() |
|
|
|
|
if (upStateJob == null) { |
|
|
|
|
checkDownloadState() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -89,7 +108,8 @@ class DownloadService : BaseService() { |
|
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
|
downloadManager.remove(downloadId) |
|
|
|
|
} |
|
|
|
|
downloads.remove(downloadId) |
|
|
|
|
downloadUrls.remove(downloadId) |
|
|
|
|
downloadNames.remove(downloadId) |
|
|
|
|
notificationManager.cancel(downloadId.toInt()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -97,10 +117,10 @@ class DownloadService : BaseService() { |
|
|
|
|
private fun successDownload(downloadId: Long) { |
|
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
|
completeDownloads.add(downloadId) |
|
|
|
|
if (downloads[downloadId]?.endsWith(".apk") == true) { |
|
|
|
|
if (downloadNames[downloadId]?.endsWith(".apk") == true) { |
|
|
|
|
installApk(downloadId) |
|
|
|
|
} else { |
|
|
|
|
toastOnUi("${downloads[downloadId]} 下载完成") |
|
|
|
|
toastOnUi("${downloadNames[downloadId]} 下载完成") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -118,11 +138,11 @@ class DownloadService : BaseService() { |
|
|
|
|
//查询下载进度 |
|
|
|
|
@Synchronized |
|
|
|
|
private fun queryState() { |
|
|
|
|
if (downloads.isEmpty()) { |
|
|
|
|
if (downloadNames.isEmpty()) { |
|
|
|
|
stopSelf() |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
val ids = downloads.keys |
|
|
|
|
val ids = downloadNames.keys |
|
|
|
|
val query = DownloadManager.Query() |
|
|
|
|
query.setFilterById(*ids.toLongArray()) |
|
|
|
|
downloadManager.query(query).use { cursor -> |
|
|
|
@ -144,7 +164,7 @@ class DownloadService : BaseService() { |
|
|
|
|
DownloadManager.STATUS_FAILED -> "下载失败" |
|
|
|
|
else -> "未知状态" |
|
|
|
|
} |
|
|
|
|
upDownloadNotification(id, "${downloads[id]} $status", max, progress) |
|
|
|
|
upDownloadNotification(id, "${downloadNames[id]} $status", max, progress) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|