|
|
@ -67,14 +67,13 @@ class DownloadService : BaseService() { |
|
|
|
} |
|
|
|
} |
|
|
|
IntentAction.stop -> { |
|
|
|
IntentAction.stop -> { |
|
|
|
val downloadId = intent.getLongExtra("downloadId", 0) |
|
|
|
val downloadId = intent.getLongExtra("downloadId", 0) |
|
|
|
downloadManager.remove(downloadId) |
|
|
|
removeDownload(downloadId) |
|
|
|
downloads.remove(downloadId) |
|
|
|
|
|
|
|
notificationManager.cancel(downloadId.toInt()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return super.onStartCommand(intent, flags, startId) |
|
|
|
return super.onStartCommand(intent, flags, startId) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
private fun startDownload(downloadId: Long, fileName: String) { |
|
|
|
private fun startDownload(downloadId: Long, fileName: String) { |
|
|
|
if (downloadId > 0) { |
|
|
|
if (downloadId > 0) { |
|
|
|
downloads[downloadId] = fileName |
|
|
|
downloads[downloadId] = fileName |
|
|
@ -85,6 +84,27 @@ class DownloadService : BaseService() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
private fun removeDownload(downloadId: Long) { |
|
|
|
|
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
|
|
|
|
downloadManager.remove(downloadId) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
downloads.remove(downloadId) |
|
|
|
|
|
|
|
notificationManager.cancel(downloadId.toInt()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
private fun successDownload(downloadId: Long) { |
|
|
|
|
|
|
|
if (!completeDownloads.contains(downloadId)) { |
|
|
|
|
|
|
|
completeDownloads.add(downloadId) |
|
|
|
|
|
|
|
if (downloads[downloadId]?.endsWith(".apk") == true) { |
|
|
|
|
|
|
|
installApk(downloadId) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
toastOnUi("${downloads[downloadId]} 下载完成") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun checkDownloadState() { |
|
|
|
private fun checkDownloadState() { |
|
|
|
upStateJob?.cancel() |
|
|
|
upStateJob?.cancel() |
|
|
|
upStateJob = launch { |
|
|
|
upStateJob = launch { |
|
|
@ -96,6 +116,7 @@ class DownloadService : BaseService() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//查询下载进度 |
|
|
|
//查询下载进度 |
|
|
|
|
|
|
|
@Synchronized |
|
|
|
private fun queryState() { |
|
|
|
private fun queryState() { |
|
|
|
if (downloads.isEmpty()) { |
|
|
|
if (downloads.isEmpty()) { |
|
|
|
stopSelf() |
|
|
|
stopSelf() |
|
|
@ -107,28 +128,23 @@ class DownloadService : BaseService() { |
|
|
|
downloadManager.query(query).use { cursor -> |
|
|
|
downloadManager.query(query).use { cursor -> |
|
|
|
if (!cursor.moveToFirst()) return |
|
|
|
if (!cursor.moveToFirst()) return |
|
|
|
val id = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)) |
|
|
|
val id = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)) |
|
|
|
val progress: Int = |
|
|
|
val progress: Int = cursor |
|
|
|
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)) |
|
|
|
.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)) |
|
|
|
val max: Int = |
|
|
|
val max: Int = cursor |
|
|
|
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)) |
|
|
|
.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)) |
|
|
|
val status = |
|
|
|
val status = |
|
|
|
when (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))) { |
|
|
|
when (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))) { |
|
|
|
DownloadManager.STATUS_PAUSED -> "暂停" |
|
|
|
DownloadManager.STATUS_PAUSED -> "暂停" |
|
|
|
DownloadManager.STATUS_PENDING -> "待下载" |
|
|
|
DownloadManager.STATUS_PENDING -> "待下载" |
|
|
|
DownloadManager.STATUS_RUNNING -> "下载中" |
|
|
|
DownloadManager.STATUS_RUNNING -> "下载中" |
|
|
|
DownloadManager.STATUS_SUCCESSFUL -> { |
|
|
|
DownloadManager.STATUS_SUCCESSFUL -> { |
|
|
|
if (!completeDownloads.contains(id)) { |
|
|
|
successDownload(id) |
|
|
|
completeDownloads.add(id) |
|
|
|
|
|
|
|
if (downloads[id]?.endsWith(".apk") == true) { |
|
|
|
|
|
|
|
installApk(id) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
"下载完成" |
|
|
|
"下载完成" |
|
|
|
} |
|
|
|
} |
|
|
|
DownloadManager.STATUS_FAILED -> "下载失败" |
|
|
|
DownloadManager.STATUS_FAILED -> "下载失败" |
|
|
|
else -> "未知状态" |
|
|
|
else -> "未知状态" |
|
|
|
} |
|
|
|
} |
|
|
|
updateNotification(id, "${downloads[id]} $status", max, progress) |
|
|
|
upDownloadNotification(id, "${downloads[id]} $status", max, progress) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -159,21 +175,21 @@ class DownloadService : BaseService() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun upSummaryNotification() { |
|
|
|
private fun upSummaryNotification() { |
|
|
|
val notificationBuilder = NotificationCompat.Builder(this, AppConst.channelIdDownload) |
|
|
|
val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload) |
|
|
|
.setSmallIcon(R.drawable.ic_download) |
|
|
|
.setSmallIcon(R.drawable.ic_download) |
|
|
|
.setOngoing(true) |
|
|
|
|
|
|
|
.setContentTitle(getString(R.string.action_download)) |
|
|
|
.setContentTitle(getString(R.string.action_download)) |
|
|
|
.setGroup(groupKey) |
|
|
|
.setGroup(groupKey) |
|
|
|
.setGroupSummary(true) |
|
|
|
.setGroupSummary(true) |
|
|
|
val notification = notificationBuilder.build() |
|
|
|
.setOngoing(true) |
|
|
|
|
|
|
|
.build() |
|
|
|
startForeground(AppConst.notificationIdDownload, notification) |
|
|
|
startForeground(AppConst.notificationIdDownload, notification) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 更新通知 |
|
|
|
* 更新通知 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun updateNotification(downloadId: Long, content: String, max: Int, progress: Int) { |
|
|
|
private fun upDownloadNotification(downloadId: Long, content: String, max: Int, progress: Int) { |
|
|
|
val notificationBuilder = NotificationCompat.Builder(this, AppConst.channelIdDownload) |
|
|
|
val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload) |
|
|
|
.setSmallIcon(R.drawable.ic_download) |
|
|
|
.setSmallIcon(R.drawable.ic_download) |
|
|
|
.setContentTitle(getString(R.string.action_download)) |
|
|
|
.setContentTitle(getString(R.string.action_download)) |
|
|
|
.setContentIntent( |
|
|
|
.setContentIntent( |
|
|
@ -181,13 +197,6 @@ class DownloadService : BaseService() { |
|
|
|
putExtra("downloadId", downloadId) |
|
|
|
putExtra("downloadId", downloadId) |
|
|
|
} |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
.addAction( |
|
|
|
|
|
|
|
R.drawable.ic_stop_black_24dp, |
|
|
|
|
|
|
|
getString(R.string.cancel), |
|
|
|
|
|
|
|
servicePendingIntent<DownloadService>(IntentAction.stop) { |
|
|
|
|
|
|
|
putExtra("downloadId", downloadId) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.setDeleteIntent( |
|
|
|
.setDeleteIntent( |
|
|
|
servicePendingIntent<DownloadService>(IntentAction.stop) { |
|
|
|
servicePendingIntent<DownloadService>(IntentAction.stop) { |
|
|
|
putExtra("downloadId", downloadId) |
|
|
|
putExtra("downloadId", downloadId) |
|
|
@ -197,7 +206,7 @@ class DownloadService : BaseService() { |
|
|
|
.setContentText(content) |
|
|
|
.setContentText(content) |
|
|
|
.setProgress(max, progress, false) |
|
|
|
.setProgress(max, progress, false) |
|
|
|
.setGroup(groupKey) |
|
|
|
.setGroup(groupKey) |
|
|
|
val notification = notificationBuilder.build() |
|
|
|
.build() |
|
|
|
notificationManager.notify(downloadId.toInt(), notification) |
|
|
|
notificationManager.notify(downloadId.toInt(), notification) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|