Skip to content

Instantly share code, notes, and snippets.

@aartikov
Created January 25, 2021 09:38
Show Gist options
  • Save aartikov/9e2c383fbdaaddc71602fe48a3f92fde to your computer and use it in GitHub Desktop.
Save aartikov/9e2c383fbdaaddc71602fe48a3f92fde to your computer and use it in GitHub Desktop.
DownloadNotificationManager
abstract class DownloadNotificationManager(context: Context) : DefaultFetchNotificationManager(context) {
// Copied from DefaultFetchNotificationManager to hide Pause button
override fun updateNotification(notificationBuilder: NotificationCompat.Builder,
downloadNotification: DownloadNotification,
context: Context) {
val smallIcon = if (downloadNotification.isDownloading) {
android.R.drawable.stat_sys_download
} else {
android.R.drawable.stat_sys_download_done
}
notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(smallIcon)
.setContentTitle(downloadNotification.title)
.setContentText(getSubtitleText(context, downloadNotification))
.setOngoing(downloadNotification.isOnGoingNotification)
.setGroup(downloadNotification.groupId.toString())
.setGroupSummary(false)
if (downloadNotification.isFailed || downloadNotification.isCompleted) {
notificationBuilder.setProgress(0, 0, false)
} else {
val progressIndeterminate = downloadNotification.progressIndeterminate
val maxProgress = if (downloadNotification.progressIndeterminate) 0 else 100
val progress = if (downloadNotification.progress < 0) 0 else downloadNotification.progress
notificationBuilder.setProgress(maxProgress, progress, progressIndeterminate)
}
when {
downloadNotification.isDownloading -> {
notificationBuilder.setTimeoutAfter(getNotificationTimeOutMillis())
// Pause button hidden
/*
.addAction(R.drawable.fetch_notification_pause,
context.getString(R.string.fetch_notification_download_pause),
getActionPendingIntent(downloadNotification, DownloadNotification.ActionType.PAUSE))
*/
.addAction(R.drawable.fetch_notification_cancel,
context.getString(R.string.fetch_notification_download_cancel),
getActionPendingIntent(downloadNotification, DownloadNotification.ActionType.CANCEL))
}
downloadNotification.isPaused -> {
notificationBuilder.setTimeoutAfter(getNotificationTimeOutMillis())
.addAction(R.drawable.fetch_notification_resume,
context.getString(R.string.fetch_notification_download_resume),
getActionPendingIntent(downloadNotification, DownloadNotification.ActionType.RESUME))
.addAction(R.drawable.fetch_notification_cancel,
context.getString(R.string.fetch_notification_download_cancel),
getActionPendingIntent(downloadNotification, DownloadNotification.ActionType.CANCEL))
}
downloadNotification.isQueued -> {
notificationBuilder.setTimeoutAfter(getNotificationTimeOutMillis())
}
else -> {
notificationBuilder.setTimeoutAfter(DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment