暴力预防下载通知状态改变时无法刷新问题

This commit is contained in:
kehaoyuan
2019-12-28 11:00:11 +08:00
parent 1ecd83b6af
commit 5f0bfd1184
2 changed files with 38 additions and 18 deletions

View File

@ -7,6 +7,7 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Handler
import androidx.core.app.NotificationCompat
import com.gh.gamecenter.R
import com.halo.assistant.HaloApp
@ -24,6 +25,9 @@ object DownloadNotificationHelper {
const val ACTION_INSTALL = "com.gh.gamecenter.INSTALL"
const val ACTION_DOWNLOAD = "com.gh.gamecenter.DOWNLOAD"
private var mCreatedNotificationGroup = false
private val mHandler = Handler()
private fun getNotificationManager(): NotificationManager {
return HaloApp.getInstance().application.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
@ -51,15 +55,6 @@ object DownloadNotificationHelper {
notificationManager.createNotificationChannel(channel)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val groupBuilder = NotificationCompat.Builder(HaloApp.getInstance().application, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.mipmap.logo)
.setGroup(DOWNLOAD_GROUP_KEY)
.setStyle(NotificationCompat.BigTextStyle().bigText("下载任务"))
.setGroupSummary(true)
notificationManager.notify(DOWNLOAD_NOTIFICATION_FOLD_ID, groupBuilder.build())
}
val builder = NotificationCompat.Builder(HaloApp.getInstance().application, DOWNLOAD_CHANNEL_ID)
.setContentTitle(entity.name)
.setSmallIcon(R.mipmap.logo)
@ -73,7 +68,9 @@ object DownloadNotificationHelper {
SpeedUtils.getRemainTime(entity.size, entity.progress, entity.speed * 1024)))
DownloadStatus.done -> builder.setContentText("下载完成,点击立即安装")
DownloadStatus.waiting -> builder.setContentText("等待中")
DownloadStatus.subscribe -> builder.setContentText("已暂停连接WiFi自动下载")
DownloadStatus.subscribe,
DownloadStatus.timeout,
DownloadStatus.neterror -> builder.setContentText("已暂停连接WiFi自动下载")
else -> builder.setContentText("暂停中")
}
@ -86,15 +83,37 @@ object DownloadNotificationHelper {
val notification = builder.build()
notification.flags = notification.flags or Notification.FLAG_NO_CLEAR
// fuck,使用tag会影响折叠的最小值
if (entity.status == DownloadStatus.delete ||
entity.status == DownloadStatus.cancel ||
entity.status == DownloadStatus.hijack ||
entity.status == DownloadStatus.notfound) {
entity.status == DownloadStatus.notfound ||
entity.status == DownloadStatus.overflow) {
notificationManager.cancel(downloadNotificationId)
} else {
// todo 个别手机会丢失部分刷新数据(redmi k20 pro)
notificationManager.notify(downloadNotificationId, notification)
if (entity.status != DownloadStatus.downloading) {
// 刷新过于频繁,部分通知会丢失,对一个特殊状态做延迟处理
mHandler.postDelayed({
notificationManager.notify(downloadNotificationId, notification)
}, 1000)
} else {
notificationManager.notify(downloadNotificationId, notification)
}
}
if (!mCreatedNotificationGroup) createNotificationGroup()
}
private fun createNotificationGroup() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mCreatedNotificationGroup = true
val groupBuilder = NotificationCompat.Builder(HaloApp.getInstance().application, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.mipmap.logo)
.setGroup(DOWNLOAD_GROUP_KEY)
.setGroupSummary(true)
.setStyle(NotificationCompat.BigTextStyle().bigText("下载任务"))
val groupNotification = groupBuilder.build()
groupNotification.flags = groupNotification.flags or Notification.FLAG_NO_CLEAR
getNotificationManager().notify(DOWNLOAD_NOTIFICATION_FOLD_ID, groupNotification)
}
}