|
|
@ -7,6 +7,7 @@ import android.content.Intent |
|
|
|
import android.content.IntentFilter |
|
|
|
import android.content.IntentFilter |
|
|
|
import android.net.Uri |
|
|
|
import android.net.Uri |
|
|
|
import android.os.Build |
|
|
|
import android.os.Build |
|
|
|
|
|
|
|
import android.os.Environment |
|
|
|
import androidx.core.app.NotificationCompat |
|
|
|
import androidx.core.app.NotificationCompat |
|
|
|
import androidx.core.content.FileProvider |
|
|
|
import androidx.core.content.FileProvider |
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.R |
|
|
@ -29,7 +30,8 @@ import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
class DownloadService : BaseService() { |
|
|
|
class DownloadService : BaseService() { |
|
|
|
private val groupKey = "${appCtx.packageName}.download" |
|
|
|
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 val completeDownloads = hashSetOf<Long>() |
|
|
|
private var upStateJob: Job? = null |
|
|
|
private var upStateJob: Job? = null |
|
|
|
private val downloadReceiver = object : BroadcastReceiver() { |
|
|
|
private val downloadReceiver = object : BroadcastReceiver() { |
|
|
@ -52,13 +54,13 @@ class DownloadService : BaseService() { |
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
|
|
|
when (intent?.action) { |
|
|
|
when (intent?.action) { |
|
|
|
IntentAction.start -> startDownload( |
|
|
|
IntentAction.start -> startDownload( |
|
|
|
intent.getLongExtra("downloadId", 0), |
|
|
|
intent.getStringExtra("url"), |
|
|
|
intent.getStringExtra("fileName") ?: "未知文件" |
|
|
|
intent.getStringExtra("fileName") |
|
|
|
) |
|
|
|
) |
|
|
|
IntentAction.play -> { |
|
|
|
IntentAction.play -> { |
|
|
|
val id = intent.getLongExtra("downloadId", 0) |
|
|
|
val id = intent.getLongExtra("downloadId", 0) |
|
|
|
if (completeDownloads.contains(id) |
|
|
|
if (completeDownloads.contains(id) |
|
|
|
&& downloads[id]?.endsWith(".apk") == true |
|
|
|
&& downloadNames[id]?.endsWith(".apk") == true |
|
|
|
) { |
|
|
|
) { |
|
|
|
installApk(id) |
|
|
|
installApk(id) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -74,13 +76,30 @@ class DownloadService : BaseService() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
private fun startDownload(downloadId: Long, fileName: String) { |
|
|
|
private fun startDownload(url: String?, fileName: String?) { |
|
|
|
if (downloadId > 0) { |
|
|
|
if (url == null || fileName == null) { |
|
|
|
downloads[downloadId] = fileName |
|
|
|
if (downloadNames.isEmpty()) { |
|
|
|
queryState() |
|
|
|
stopSelf() |
|
|
|
if (upStateJob == null) { |
|
|
|
|
|
|
|
checkDownloadState() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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)) { |
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
downloadManager.remove(downloadId) |
|
|
|
downloadManager.remove(downloadId) |
|
|
|
} |
|
|
|
} |
|
|
|
downloads.remove(downloadId) |
|
|
|
downloadUrls.remove(downloadId) |
|
|
|
|
|
|
|
downloadNames.remove(downloadId) |
|
|
|
notificationManager.cancel(downloadId.toInt()) |
|
|
|
notificationManager.cancel(downloadId.toInt()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -97,10 +117,10 @@ class DownloadService : BaseService() { |
|
|
|
private fun successDownload(downloadId: Long) { |
|
|
|
private fun successDownload(downloadId: Long) { |
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
completeDownloads.add(downloadId) |
|
|
|
completeDownloads.add(downloadId) |
|
|
|
if (downloads[downloadId]?.endsWith(".apk") == true) { |
|
|
|
if (downloadNames[downloadId]?.endsWith(".apk") == true) { |
|
|
|
installApk(downloadId) |
|
|
|
installApk(downloadId) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
toastOnUi("${downloads[downloadId]} 下载完成") |
|
|
|
toastOnUi("${downloadNames[downloadId]} 下载完成") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -118,11 +138,11 @@ class DownloadService : BaseService() { |
|
|
|
//查询下载进度 |
|
|
|
//查询下载进度 |
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
private fun queryState() { |
|
|
|
private fun queryState() { |
|
|
|
if (downloads.isEmpty()) { |
|
|
|
if (downloadNames.isEmpty()) { |
|
|
|
stopSelf() |
|
|
|
stopSelf() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
val ids = downloads.keys |
|
|
|
val ids = downloadNames.keys |
|
|
|
val query = DownloadManager.Query() |
|
|
|
val query = DownloadManager.Query() |
|
|
|
query.setFilterById(*ids.toLongArray()) |
|
|
|
query.setFilterById(*ids.toLongArray()) |
|
|
|
downloadManager.query(query).use { cursor -> |
|
|
|
downloadManager.query(query).use { cursor -> |
|
|
@ -144,7 +164,7 @@ class DownloadService : BaseService() { |
|
|
|
DownloadManager.STATUS_FAILED -> "下载失败" |
|
|
|
DownloadManager.STATUS_FAILED -> "下载失败" |
|
|
|
else -> "未知状态" |
|
|
|
else -> "未知状态" |
|
|
|
} |
|
|
|
} |
|
|
|
upDownloadNotification(id, "${downloads[id]} $status", max, progress) |
|
|
|
upDownloadNotification(id, "${downloadNames[id]} $status", max, progress) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|