From cf07145d4b2f24a0e0a62619eba0eae691606050 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Sun, 5 Sep 2021 15:21:26 -0400 Subject: Use NotificationChannelCompat utilities --- .../core/util/gui/NotificationUtils.java | 168 ++++++++++----------- 1 file changed, 84 insertions(+), 84 deletions(-) (limited to 'core/src/main/java/de/danoeh/antennapod') diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/gui/NotificationUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/gui/NotificationUtils.java index 5895c5933..2d5c7d01f 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/gui/NotificationUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/gui/NotificationUtils.java @@ -1,12 +1,13 @@ package de.danoeh.antennapod.core.util.gui; - -import android.app.NotificationChannel; -import android.app.NotificationChannelGroup; -import android.app.NotificationManager; import android.content.Context; -import android.os.Build; -import androidx.annotation.RequiresApi; + +import androidx.core.app.NotificationChannelCompat; +import androidx.core.app.NotificationChannelGroupCompat; +import androidx.core.app.NotificationManagerCompat; + +import java.util.Arrays; +import java.util.List; import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -23,113 +24,112 @@ public class NotificationUtils { public static final String GROUP_ID_ERRORS = "group_errors"; public static final String GROUP_ID_NEWS = "group_news"; - public static void createChannels(Context context) { - if (Build.VERSION.SDK_INT < 26) { - return; - } - NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - - if (mNotificationManager != null) { - mNotificationManager.createNotificationChannelGroup(createGroupErrors(context)); - mNotificationManager.createNotificationChannelGroup(createGroupNews(context)); - - mNotificationManager.createNotificationChannel(createChannelUserAction(context)); - mNotificationManager.createNotificationChannel(createChannelDownloading(context)); - mNotificationManager.createNotificationChannel(createChannelPlaying(context)); - mNotificationManager.createNotificationChannel(createChannelError(context)); - mNotificationManager.createNotificationChannel(createChannelSyncError(context)); - mNotificationManager.createNotificationChannel(createChannelAutoDownload(context)); - mNotificationManager.createNotificationChannel(createChannelEpisodeNotification(context)); - } + public static void createChannels(final Context context) { + final NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context); + + final List channelGroups = Arrays.asList( + createGroupErrors(context), + createGroupNews(context)); + mNotificationManager.createNotificationChannelGroupsCompat(channelGroups); + + final List channels = Arrays.asList( + createChannelUserAction(context), + createChannelDownloading(context), + createChannelPlaying(context), + createChannelError(context), + createChannelSyncError(context), + createChannelAutoDownload(context), + createChannelEpisodeNotification(context)); + mNotificationManager.createNotificationChannelsCompat(channels); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannel createChannelUserAction(Context c) { - NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_USER_ACTION, - c.getString(R.string.notification_channel_user_action), NotificationManager.IMPORTANCE_HIGH); - notificationChannel.setDescription(c.getString(R.string.notification_channel_user_action_description)); - notificationChannel.setGroup(GROUP_ID_ERRORS); - return notificationChannel; + private static NotificationChannelCompat createChannelUserAction(final Context c) { + return new NotificationChannelCompat.Builder( + CHANNEL_ID_USER_ACTION, NotificationManagerCompat.IMPORTANCE_HIGH) + .setName(c.getString(R.string.notification_channel_user_action)) + .setDescription(c.getString(R.string.notification_channel_user_action_description)) + .setGroup(GROUP_ID_ERRORS) + .build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannel createChannelDownloading(Context c) { - NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_DOWNLOADING, - c.getString(R.string.notification_channel_downloading), NotificationManager.IMPORTANCE_LOW); - notificationChannel.setDescription(c.getString(R.string.notification_channel_downloading_description)); - notificationChannel.setShowBadge(false); - return notificationChannel; + private static NotificationChannelCompat createChannelDownloading(final Context c) { + return new NotificationChannelCompat.Builder( + CHANNEL_ID_DOWNLOADING, NotificationManagerCompat.IMPORTANCE_LOW) + .setName(c.getString(R.string.notification_channel_downloading)) + .setDescription(c.getString(R.string.notification_channel_downloading_description)) + .setShowBadge(false) + .build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannel createChannelPlaying(Context c) { - NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_PLAYING, - c.getString(R.string.notification_channel_playing), NotificationManager.IMPORTANCE_LOW); - notificationChannel.setDescription(c.getString(R.string.notification_channel_playing_description)); - notificationChannel.setShowBadge(false); - return notificationChannel; + private static NotificationChannelCompat createChannelPlaying(final Context c) { + return new NotificationChannelCompat.Builder( + CHANNEL_ID_PLAYING, NotificationManagerCompat.IMPORTANCE_LOW) + .setName(c.getString(R.string.notification_channel_playing)) + .setDescription(c.getString(R.string.notification_channel_playing_description)) + .setShowBadge(false) + .build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannel createChannelError(Context c) { - NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_DOWNLOAD_ERROR, - c.getString(R.string.notification_channel_download_error), NotificationManager.IMPORTANCE_HIGH); - notificationChannel.setDescription(c.getString(R.string.notification_channel_download_error_description)); - notificationChannel.setGroup(GROUP_ID_ERRORS); + private static NotificationChannelCompat createChannelError(final Context c) { + final NotificationChannelCompat.Builder notificationChannel = new NotificationChannelCompat.Builder( + CHANNEL_ID_DOWNLOAD_ERROR, NotificationManagerCompat.IMPORTANCE_HIGH) + .setName(c.getString(R.string.notification_channel_download_error)) + .setDescription(c.getString(R.string.notification_channel_download_error_description)) + .setGroup(GROUP_ID_ERRORS); if (!UserPreferences.getShowDownloadReportRaw()) { // Migration from app managed setting: disable notification - notificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE); + notificationChannel.setImportance(NotificationManagerCompat.IMPORTANCE_NONE); } - return notificationChannel; + return notificationChannel.build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannel createChannelSyncError(Context c) { - NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_SYNC_ERROR, - c.getString(R.string.notification_channel_sync_error), NotificationManager.IMPORTANCE_HIGH); - notificationChannel.setDescription(c.getString(R.string.notification_channel_sync_error_description)); - notificationChannel.setGroup(GROUP_ID_ERRORS); + private static NotificationChannelCompat createChannelSyncError(final Context c) { + final NotificationChannelCompat.Builder notificationChannel = new NotificationChannelCompat.Builder( + CHANNEL_ID_SYNC_ERROR, NotificationManagerCompat.IMPORTANCE_HIGH) + .setName(c.getString(R.string.notification_channel_sync_error)) + .setDescription(c.getString(R.string.notification_channel_sync_error_description)) + .setGroup(GROUP_ID_ERRORS); if (!UserPreferences.getGpodnetNotificationsEnabledRaw()) { // Migration from app managed setting: disable notification - notificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE); + notificationChannel.setImportance(NotificationManagerCompat.IMPORTANCE_NONE); } - return notificationChannel; + return notificationChannel.build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannel createChannelAutoDownload(Context c) { - NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_AUTO_DOWNLOAD, - c.getString(R.string.notification_channel_auto_download), NotificationManager.IMPORTANCE_NONE); - notificationChannel.setDescription(c.getString(R.string.notification_channel_episode_auto_download)); - notificationChannel.setGroup(GROUP_ID_NEWS); + private static NotificationChannelCompat createChannelAutoDownload(final Context c) { + final NotificationChannelCompat.Builder notificationChannel = new NotificationChannelCompat.Builder( + CHANNEL_ID_AUTO_DOWNLOAD, NotificationManagerCompat.IMPORTANCE_NONE) + .setName(c.getString(R.string.notification_channel_auto_download)) + .setDescription(c.getString(R.string.notification_channel_episode_auto_download)) + .setGroup(GROUP_ID_NEWS); if (UserPreferences.getShowAutoDownloadReportRaw()) { // Migration from app managed setting: enable notification - notificationChannel.setImportance(NotificationManager.IMPORTANCE_DEFAULT); + notificationChannel.setImportance(NotificationManagerCompat.IMPORTANCE_DEFAULT); } - return notificationChannel; + return notificationChannel.build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannel createChannelEpisodeNotification(Context c) { - NotificationChannel channel = new NotificationChannel(CHANNEL_ID_EPISODE_NOTIFICATIONS, - c.getString(R.string.notification_channel_new_episode), NotificationManager.IMPORTANCE_DEFAULT); - channel.setDescription(c.getString(R.string.notification_channel_new_episode_description)); - channel.setGroup(GROUP_ID_NEWS); - return channel; + private static NotificationChannelCompat createChannelEpisodeNotification(final Context c) { + return new NotificationChannelCompat.Builder( + CHANNEL_ID_EPISODE_NOTIFICATIONS, NotificationManagerCompat.IMPORTANCE_DEFAULT) + .setName(c.getString(R.string.notification_channel_new_episode)) + .setDescription(c.getString(R.string.notification_channel_new_episode_description)) + .setGroup(GROUP_ID_NEWS) + .build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannelGroup createGroupErrors(Context c) { - return new NotificationChannelGroup(GROUP_ID_ERRORS, - c.getString(R.string.notification_group_errors)); + private static NotificationChannelGroupCompat createGroupErrors(final Context c) { + return new NotificationChannelGroupCompat.Builder(GROUP_ID_ERRORS) + .setName(c.getString(R.string.notification_group_errors)) + .build(); } - @RequiresApi(api = Build.VERSION_CODES.O) - private static NotificationChannelGroup createGroupNews(Context c) { - return new NotificationChannelGroup(GROUP_ID_NEWS, - c.getString(R.string.notification_group_news)); + private static NotificationChannelGroupCompat createGroupNews(final Context c) { + return new NotificationChannelGroupCompat.Builder(GROUP_ID_NEWS) + .setName(c.getString(R.string.notification_group_news)) + .build(); } } -- cgit v1.2.3