Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelDefinitions.java

Issue 2832433002: [Android O] Refactor channel definitions into new class (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelDefinitions.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelsInitializer.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelDefinitions.java
similarity index 72%
copy from chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelsInitializer.java
copy to chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelDefinitions.java
index afd4d32337f028750832e7c60e551eb28b295299..c7842aaa479685a870f774881f251b770191d485 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelsInitializer.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelDefinitions.java
@@ -14,15 +14,27 @@ import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
/**
- * Initializes our notification channels.
+ * Defines the properties of all notification channels we post notifications to in Android O+.
+ *
+ * PLEASE NOTE, notification channels appear in system UI and are persisted forever by Android,
+ * so should not be added or removed lightly, and the proper deprecation and versioning steps must
+ * be taken when doing so. Please read the comments and speak to one of this file's OWNERs when
+ * adding/removing a channel.
*/
-public class ChannelsInitializer {
+public class ChannelDefinitions {
+ public static final String CHANNEL_ID_BROWSER = "browser";
+ public static final String CHANNEL_ID_DOWNLOADS = "downloads";
+ public static final String CHANNEL_ID_INCOGNITO = "incognito";
+ public static final String CHANNEL_ID_MEDIA = "media";
+ public static final String CHANNEL_ID_SITES = "sites";
+ static final String CHANNEL_GROUP_ID_GENERAL = "general";
/**
* Version number identifying the current set of channels. This must be incremented whenever
* any channels are added or removed from the set of channels created in
- * {@link ChannelsInitializer#initializeStartupChannels()}
+ * {@link ChannelsInitializer#initializeStartupChannels()}.
*/
static final int CHANNELS_VERSION = 0;
@@ -32,16 +44,10 @@ public class ChannelsInitializer {
CHANNEL_ID_SITES})
@Retention(RetentionPolicy.SOURCE)
public @interface ChannelId {}
- public static final String CHANNEL_ID_BROWSER = "browser";
- public static final String CHANNEL_ID_DOWNLOADS = "downloads";
- public static final String CHANNEL_ID_INCOGNITO = "incognito";
- public static final String CHANNEL_ID_MEDIA = "media";
- public static final String CHANNEL_ID_SITES = "sites";
@StringDef({CHANNEL_GROUP_ID_GENERAL})
@Retention(RetentionPolicy.SOURCE)
- private @interface ChannelGroupId {}
- static final String CHANNEL_GROUP_ID_GENERAL = "general";
+ @interface ChannelGroupId {}
// Map defined in static inner class so it's only initialized lazily.
@TargetApi(Build.VERSION_CODES.N) // for NotificationManager.IMPORTANCE_* constants
@@ -50,7 +56,7 @@ public class ChannelsInitializer {
* The set of predefined channels to be initialized on startup. CHANNELS_VERSION must be
* incremented every time an entry is modified, removed or added to this map.
*/
- private static final Map<String, Channel> MAP;
+ static final Map<String, Channel> MAP;
static {
Map<String, Channel> map = new HashMap<>();
map.put(CHANNEL_ID_BROWSER,
@@ -79,7 +85,7 @@ public class ChannelsInitializer {
// Map defined in static inner class so it's only initialized lazily.
private static class PredefinedChannelGroups {
- private static final Map<String, ChannelGroup> MAP;
+ static final Map<String, ChannelGroup> MAP;
static {
Map<String, ChannelGroup> map = new HashMap<>();
map.put(CHANNEL_GROUP_ID_GENERAL,
@@ -89,43 +95,17 @@ public class ChannelsInitializer {
}
}
- private final NotificationManagerProxy mNotificationManager;
-
- public ChannelsInitializer(NotificationManagerProxy notificationManagerProxy) {
- mNotificationManager = notificationManagerProxy;
- }
-
- public void initializeStartupChannels() {
- // CHANNELS_VERSION must be incremented if the set of channels initialized here changes.
- for (@ChannelId String channelId : PredefinedChannels.MAP.keySet()) {
- ensureInitialized(channelId);
- }
+ Set<String> getStartupChannelIds() {
+ // CHANNELS_VERSION must be incremented if the set of channels returned here changes.
+ return PredefinedChannels.MAP.keySet();
}
- void deleteAllChannels() {
- for (String channelId : mNotificationManager.getNotificationChannelIds()) {
- mNotificationManager.deleteNotificationChannel(channelId);
- }
+ ChannelGroup getChannelGroupFromId(Channel channel) {
+ return PredefinedChannelGroups.MAP.get(channel.mGroupId);
}
- /**
- * Ensures the given channel has been created on the notification manager so a notification
- * can be safely posted to it. This should only be used for channels that are predefined in
- * ChannelsInitializer.
- *
- * Calling this is a (potentially lengthy) no-op if the channel has already been created.
- *
- * @param channelId The ID of the channel to be initialized.
- */
- public void ensureInitialized(@ChannelId String channelId) {
- if (!PredefinedChannels.MAP.containsKey(channelId)) {
- throw new IllegalStateException("Could not initialize channel: " + channelId);
- }
- Channel channel = PredefinedChannels.MAP.get(channelId);
- // Channel group must be created before the channel.
- mNotificationManager.createNotificationChannelGroup(
- PredefinedChannelGroups.MAP.get(channel.mGroupId));
- mNotificationManager.createNotificationChannel(channel);
+ Channel getChannelFromId(@ChannelId String channelId) {
+ return PredefinedChannels.MAP.get(channelId);
}
/**

Powered by Google App Engine
This is Rietveld 408576698