|
|
@ -30,8 +30,7 @@ import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
class DownloadService : BaseService() { |
|
|
|
class DownloadService : BaseService() { |
|
|
|
private val groupKey = "${appCtx.packageName}.download" |
|
|
|
private val groupKey = "${appCtx.packageName}.download" |
|
|
|
private val downloadUrls = hashMapOf<Long, String>() |
|
|
|
private val downloads = hashMapOf<Long, Pair<String, 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() { |
|
|
@ -60,7 +59,7 @@ class DownloadService : BaseService() { |
|
|
|
IntentAction.play -> { |
|
|
|
IntentAction.play -> { |
|
|
|
val id = intent.getLongExtra("downloadId", 0) |
|
|
|
val id = intent.getLongExtra("downloadId", 0) |
|
|
|
if (completeDownloads.contains(id) |
|
|
|
if (completeDownloads.contains(id) |
|
|
|
&& downloadNames[id]?.endsWith(".apk") == true |
|
|
|
&& downloads[id]?.second?.endsWith(".apk") == true |
|
|
|
) { |
|
|
|
) { |
|
|
|
installApk(id) |
|
|
|
installApk(id) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -78,12 +77,12 @@ class DownloadService : BaseService() { |
|
|
|
@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) { |
|
|
|
if (downloadNames.isEmpty()) { |
|
|
|
if (downloads.isEmpty()) { |
|
|
|
stopSelf() |
|
|
|
stopSelf() |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if (downloadUrls.values.contains(url)) { |
|
|
|
if (downloads.values.any { it.first == url }) { |
|
|
|
toastOnUi("已在下载列表") |
|
|
|
toastOnUi("已在下载列表") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -95,8 +94,7 @@ class DownloadService : BaseService() { |
|
|
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) |
|
|
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) |
|
|
|
// 添加一个下载任务 |
|
|
|
// 添加一个下载任务 |
|
|
|
val downloadId = downloadManager.enqueue(request) |
|
|
|
val downloadId = downloadManager.enqueue(request) |
|
|
|
downloadUrls[downloadId] = url |
|
|
|
downloads[downloadId] = Pair(url, fileName) |
|
|
|
downloadNames[downloadId] = fileName |
|
|
|
|
|
|
|
queryState() |
|
|
|
queryState() |
|
|
|
if (upStateJob == null) { |
|
|
|
if (upStateJob == null) { |
|
|
|
checkDownloadState() |
|
|
|
checkDownloadState() |
|
|
@ -108,8 +106,8 @@ class DownloadService : BaseService() { |
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
downloadManager.remove(downloadId) |
|
|
|
downloadManager.remove(downloadId) |
|
|
|
} |
|
|
|
} |
|
|
|
downloadUrls.remove(downloadId) |
|
|
|
downloads.remove(downloadId) |
|
|
|
downloadNames.remove(downloadId) |
|
|
|
completeDownloads.remove(downloadId) |
|
|
|
notificationManager.cancel(downloadId.toInt()) |
|
|
|
notificationManager.cancel(downloadId.toInt()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -117,10 +115,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 (downloadNames[downloadId]?.endsWith(".apk") == true) { |
|
|
|
if (downloads[downloadId]?.second?.endsWith(".apk") == true) { |
|
|
|
installApk(downloadId) |
|
|
|
installApk(downloadId) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
toastOnUi("${downloadNames[downloadId]} ${getString(R.string.download_success)}") |
|
|
|
toastOnUi("${downloads[downloadId]?.second} ${getString(R.string.download_success)}") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -138,11 +136,11 @@ class DownloadService : BaseService() { |
|
|
|
//查询下载进度 |
|
|
|
//查询下载进度 |
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
private fun queryState() { |
|
|
|
private fun queryState() { |
|
|
|
if (downloadNames.isEmpty()) { |
|
|
|
if (downloads.isEmpty()) { |
|
|
|
stopSelf() |
|
|
|
stopSelf() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
val ids = downloadNames.keys |
|
|
|
val ids = downloads.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 -> |
|
|
@ -165,7 +163,7 @@ class DownloadService : BaseService() { |
|
|
|
DownloadManager.STATUS_FAILED -> getString(R.string.download_error) |
|
|
|
DownloadManager.STATUS_FAILED -> getString(R.string.download_error) |
|
|
|
else -> getString(R.string.unknown_state) |
|
|
|
else -> getString(R.string.unknown_state) |
|
|
|
} |
|
|
|
} |
|
|
|
upDownloadNotification(id, "${downloadNames[id]} $status", max, progress) |
|
|
|
upDownloadNotification(id, "${downloads[id]?.second} $status", max, progress) |
|
|
|
} while (cursor.moveToNext()) |
|
|
|
} while (cursor.moveToNext()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|