Use NotificationChannelCompat utilities (#5781)

This commit is contained in:
Taco 2021-08-26 22:08:27 -04:00 committed by GitHub
parent 57aefcd917
commit 24fd82d773
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 73 deletions

View file

@ -1,12 +1,13 @@
package eu.kanade.tachiyomi.data.notification package eu.kanade.tachiyomi.data.notification
import android.app.NotificationChannel
import android.app.NotificationChannelGroup
import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.os.Build import androidx.core.app.NotificationManagerCompat
import androidx.core.app.NotificationManagerCompat.IMPORTANCE_DEFAULT
import androidx.core.app.NotificationManagerCompat.IMPORTANCE_HIGH
import androidx.core.app.NotificationManagerCompat.IMPORTANCE_LOW
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.buildNotificationChannel
import eu.kanade.tachiyomi.util.system.buildNotificationChannelGroup
/** /**
* Class to manage the basic information of all the notifications used in the app. * Class to manage the basic information of all the notifications used in the app.
@ -81,94 +82,73 @@ object Notifications {
/** /**
* Creates the notification channels introduced in Android Oreo. * Creates the notification channels introduced in Android Oreo.
* This won't do anything on Android versions that don't support notification channels.
* *
* @param context The application context. * @param context The application context.
*/ */
fun createChannels(context: Context) { fun createChannels(context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return val notificationService = NotificationManagerCompat.from(context)
listOf( val channelGroupList = listOf(
NotificationChannelGroup(GROUP_BACKUP_RESTORE, context.getString(R.string.group_backup_restore)), buildNotificationChannelGroup(GROUP_BACKUP_RESTORE) {
NotificationChannelGroup(GROUP_DOWNLOADER, context.getString(R.string.group_downloader)) setName(context.getString(R.string.group_backup_restore))
).forEach(context.notificationManager::createNotificationChannelGroup) },
buildNotificationChannelGroup(GROUP_DOWNLOADER) {
setName(context.getString(R.string.group_downloader))
}
)
notificationService.createNotificationChannelGroupsCompat(channelGroupList)
listOf( val channelList = listOf(
NotificationChannel( buildNotificationChannel(CHANNEL_COMMON, IMPORTANCE_LOW) {
CHANNEL_COMMON, setName(context.getString(R.string.channel_common))
context.getString(R.string.channel_common), },
NotificationManager.IMPORTANCE_LOW buildNotificationChannel(CHANNEL_LIBRARY, IMPORTANCE_LOW) {
), setName(context.getString(R.string.channel_library))
NotificationChannel(
CHANNEL_LIBRARY,
context.getString(R.string.channel_library),
NotificationManager.IMPORTANCE_LOW
).apply {
setShowBadge(false) setShowBadge(false)
}, },
NotificationChannel( buildNotificationChannel(CHANNEL_DOWNLOADER_PROGRESS, IMPORTANCE_LOW) {
CHANNEL_DOWNLOADER_PROGRESS, setName(context.getString(R.string.channel_progress))
context.getString(R.string.channel_progress), setGroup(GROUP_DOWNLOADER)
NotificationManager.IMPORTANCE_LOW
).apply {
group = GROUP_DOWNLOADER
setShowBadge(false) setShowBadge(false)
}, },
NotificationChannel( buildNotificationChannel(CHANNEL_DOWNLOADER_COMPLETE, IMPORTANCE_LOW) {
CHANNEL_DOWNLOADER_COMPLETE, setName(context.getString(R.string.channel_complete))
context.getString(R.string.channel_complete), setGroup(GROUP_DOWNLOADER)
NotificationManager.IMPORTANCE_LOW
).apply {
group = GROUP_DOWNLOADER
setShowBadge(false) setShowBadge(false)
}, },
NotificationChannel( buildNotificationChannel(CHANNEL_DOWNLOADER_ERROR, IMPORTANCE_LOW) {
CHANNEL_DOWNLOADER_ERROR, setName(context.getString(R.string.channel_errors))
context.getString(R.string.channel_errors), setGroup(GROUP_DOWNLOADER)
NotificationManager.IMPORTANCE_LOW
).apply {
group = GROUP_DOWNLOADER
setShowBadge(false) setShowBadge(false)
}, },
NotificationChannel( buildNotificationChannel(CHANNEL_NEW_CHAPTERS, IMPORTANCE_DEFAULT) {
CHANNEL_NEW_CHAPTERS, setName(context.getString(R.string.channel_new_chapters))
context.getString(R.string.channel_new_chapters), },
NotificationManager.IMPORTANCE_DEFAULT buildNotificationChannel(CHANNEL_UPDATES_TO_EXTS, IMPORTANCE_DEFAULT) {
), setName(context.getString(R.string.channel_ext_updates))
NotificationChannel( },
CHANNEL_UPDATES_TO_EXTS, buildNotificationChannel(CHANNEL_BACKUP_RESTORE_PROGRESS, IMPORTANCE_LOW) {
context.getString(R.string.channel_ext_updates), setName(context.getString(R.string.channel_progress))
NotificationManager.IMPORTANCE_DEFAULT setGroup(GROUP_BACKUP_RESTORE)
),
NotificationChannel(
CHANNEL_BACKUP_RESTORE_PROGRESS,
context.getString(R.string.channel_progress),
NotificationManager.IMPORTANCE_LOW
).apply {
group = GROUP_BACKUP_RESTORE
setShowBadge(false) setShowBadge(false)
}, },
NotificationChannel( buildNotificationChannel(CHANNEL_BACKUP_RESTORE_COMPLETE, IMPORTANCE_HIGH) {
CHANNEL_BACKUP_RESTORE_COMPLETE, setName(context.getString(R.string.channel_complete))
context.getString(R.string.channel_complete), setGroup(GROUP_BACKUP_RESTORE)
NotificationManager.IMPORTANCE_HIGH
).apply {
group = GROUP_BACKUP_RESTORE
setShowBadge(false) setShowBadge(false)
setSound(null, null) setSound(null, null)
}, },
NotificationChannel( buildNotificationChannel(CHANNEL_CRASH_LOGS, IMPORTANCE_HIGH) {
CHANNEL_CRASH_LOGS, setName(context.getString(R.string.channel_crash_logs))
context.getString(R.string.channel_crash_logs), },
NotificationManager.IMPORTANCE_HIGH buildNotificationChannel(CHANNEL_INCOGNITO_MODE, IMPORTANCE_LOW) {
), setName(context.getString(R.string.pref_incognito_mode))
NotificationChannel( },
CHANNEL_INCOGNITO_MODE, )
context.getString(R.string.pref_incognito_mode), notificationService.createNotificationChannelsCompat(channelList)
NotificationManager.IMPORTANCE_LOW
)
).forEach(context.notificationManager::createNotificationChannel)
// Delete old notification channels // Delete old notification channels
deprecatedChannels.forEach(context.notificationManager::deleteNotificationChannel) deprecatedChannels.forEach(notificationService::deleteNotificationChannel)
} }
} }

View file

@ -0,0 +1,38 @@
package eu.kanade.tachiyomi.util.system
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationChannelGroupCompat
/**
* Helper method to build a notification channel group.
*
* @param channelId the channel id.
* @param block the function that will execute inside the builder.
* @return a notification channel group to be displayed or updated.
*/
fun buildNotificationChannelGroup(
channelId: String,
block: (NotificationChannelGroupCompat.Builder.() -> Unit)
): NotificationChannelGroupCompat {
val builder = NotificationChannelGroupCompat.Builder(channelId)
builder.block()
return builder.build()
}
/**
* Helper method to build a notification channel.
*
* @param channelId the channel id.
* @param channelImportance the channel importance.
* @param block the function that will execute inside the builder.
* @return a notification channel to be displayed or updated.
*/
fun buildNotificationChannel(
channelId: String,
channelImportance: Int,
block: (NotificationChannelCompat.Builder.() -> Unit)
): NotificationChannelCompat {
val builder = NotificationChannelCompat.Builder(channelId, channelImportance)
builder.block()
return builder.build()
}