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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.notifications; 5 package org.chromium.chrome.browser.notifications;
6 6
7 import android.annotation.TargetApi; 7 import android.annotation.TargetApi;
8 import android.app.NotificationManager; 8 import android.app.NotificationManager;
9 import android.os.Build; 9 import android.os.Build;
10 import android.support.annotation.StringDef; 10 import android.support.annotation.StringDef;
11 11
12 import java.lang.annotation.Retention; 12 import java.lang.annotation.Retention;
13 import java.lang.annotation.RetentionPolicy; 13 import java.lang.annotation.RetentionPolicy;
14 import java.util.Collections; 14 import java.util.Collections;
15 import java.util.HashMap; 15 import java.util.HashMap;
16 import java.util.Map; 16 import java.util.Map;
17 import java.util.Set;
17 18
18 /** 19 /**
19 * Initializes our notification channels. 20 * Defines the properties of all notification channels we post notifications to in Android O+.
21 *
22 * PLEASE NOTE, notification channels appear in system UI and are persisted fore ver by Android,
23 * so should not be added or removed lightly, and the proper deprecation and ver sioning steps must
24 * be taken when doing so. Please read the comments and speak to one of this fil e's OWNERs when
25 * adding/removing a channel.
20 */ 26 */
21 public class ChannelsInitializer { 27 public class ChannelDefinitions {
28 public static final String CHANNEL_ID_BROWSER = "browser";
29 public static final String CHANNEL_ID_DOWNLOADS = "downloads";
30 public static final String CHANNEL_ID_INCOGNITO = "incognito";
31 public static final String CHANNEL_ID_MEDIA = "media";
32 public static final String CHANNEL_ID_SITES = "sites";
33 static final String CHANNEL_GROUP_ID_GENERAL = "general";
22 /** 34 /**
23 * Version number identifying the current set of channels. This must be incr emented whenever 35 * Version number identifying the current set of channels. This must be incr emented whenever
24 * any channels are added or removed from the set of channels created in 36 * any channels are added or removed from the set of channels created in
25 * {@link ChannelsInitializer#initializeStartupChannels()} 37 * {@link ChannelsInitializer#initializeStartupChannels()}.
26 */ 38 */
27 static final int CHANNELS_VERSION = 0; 39 static final int CHANNELS_VERSION = 0;
28 40
29 // To define a new channel, add the channel ID to this StringDef and add a n ew entry to 41 // To define a new channel, add the channel ID to this StringDef and add a n ew entry to
30 // PredefinedChannels.MAP below with the appropriate channel parameters. 42 // PredefinedChannels.MAP below with the appropriate channel parameters.
31 @StringDef({CHANNEL_ID_BROWSER, CHANNEL_ID_DOWNLOADS, CHANNEL_ID_INCOGNITO, CHANNEL_ID_MEDIA, 43 @StringDef({CHANNEL_ID_BROWSER, CHANNEL_ID_DOWNLOADS, CHANNEL_ID_INCOGNITO, CHANNEL_ID_MEDIA,
32 CHANNEL_ID_SITES}) 44 CHANNEL_ID_SITES})
33 @Retention(RetentionPolicy.SOURCE) 45 @Retention(RetentionPolicy.SOURCE)
34 public @interface ChannelId {} 46 public @interface ChannelId {}
35 public static final String CHANNEL_ID_BROWSER = "browser";
36 public static final String CHANNEL_ID_DOWNLOADS = "downloads";
37 public static final String CHANNEL_ID_INCOGNITO = "incognito";
38 public static final String CHANNEL_ID_MEDIA = "media";
39 public static final String CHANNEL_ID_SITES = "sites";
40 47
41 @StringDef({CHANNEL_GROUP_ID_GENERAL}) 48 @StringDef({CHANNEL_GROUP_ID_GENERAL})
42 @Retention(RetentionPolicy.SOURCE) 49 @Retention(RetentionPolicy.SOURCE)
43 private @interface ChannelGroupId {} 50 @interface ChannelGroupId {}
44 static final String CHANNEL_GROUP_ID_GENERAL = "general";
45 51
46 // Map defined in static inner class so it's only initialized lazily. 52 // Map defined in static inner class so it's only initialized lazily.
47 @TargetApi(Build.VERSION_CODES.N) // for NotificationManager.IMPORTANCE_* co nstants 53 @TargetApi(Build.VERSION_CODES.N) // for NotificationManager.IMPORTANCE_* co nstants
48 private static class PredefinedChannels { 54 private static class PredefinedChannels {
49 /** 55 /**
50 * The set of predefined channels to be initialized on startup. CHANNELS _VERSION must be 56 * The set of predefined channels to be initialized on startup. CHANNELS _VERSION must be
51 * incremented every time an entry is modified, removed or added to this map. 57 * incremented every time an entry is modified, removed or added to this map.
52 */ 58 */
53 private static final Map<String, Channel> MAP; 59 static final Map<String, Channel> MAP;
54 static { 60 static {
55 Map<String, Channel> map = new HashMap<>(); 61 Map<String, Channel> map = new HashMap<>();
56 map.put(CHANNEL_ID_BROWSER, 62 map.put(CHANNEL_ID_BROWSER,
57 new Channel(CHANNEL_ID_BROWSER, 63 new Channel(CHANNEL_ID_BROWSER,
58 org.chromium.chrome.R.string.notification_category_b rowser, 64 org.chromium.chrome.R.string.notification_category_b rowser,
59 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL)); 65 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL));
60 map.put(CHANNEL_ID_DOWNLOADS, 66 map.put(CHANNEL_ID_DOWNLOADS,
61 new Channel(CHANNEL_ID_DOWNLOADS, 67 new Channel(CHANNEL_ID_DOWNLOADS,
62 org.chromium.chrome.R.string.notification_category_d ownloads, 68 org.chromium.chrome.R.string.notification_category_d ownloads,
63 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL)); 69 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL));
64 map.put(CHANNEL_ID_INCOGNITO, 70 map.put(CHANNEL_ID_INCOGNITO,
65 new Channel(CHANNEL_ID_INCOGNITO, 71 new Channel(CHANNEL_ID_INCOGNITO,
66 org.chromium.chrome.R.string.notification_category_i ncognito, 72 org.chromium.chrome.R.string.notification_category_i ncognito,
67 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL)); 73 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL));
68 map.put(CHANNEL_ID_MEDIA, 74 map.put(CHANNEL_ID_MEDIA,
69 new Channel(CHANNEL_ID_MEDIA, 75 new Channel(CHANNEL_ID_MEDIA,
70 org.chromium.chrome.R.string.notification_category_m edia, 76 org.chromium.chrome.R.string.notification_category_m edia,
71 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL)); 77 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL));
72 map.put(CHANNEL_ID_SITES, 78 map.put(CHANNEL_ID_SITES,
73 new Channel(CHANNEL_ID_SITES, 79 new Channel(CHANNEL_ID_SITES,
74 org.chromium.chrome.R.string.notification_category_s ites, 80 org.chromium.chrome.R.string.notification_category_s ites,
75 NotificationManager.IMPORTANCE_DEFAULT, CHANNEL_GROU P_ID_GENERAL)); 81 NotificationManager.IMPORTANCE_DEFAULT, CHANNEL_GROU P_ID_GENERAL));
76 MAP = Collections.unmodifiableMap(map); 82 MAP = Collections.unmodifiableMap(map);
77 } 83 }
78 } 84 }
79 85
80 // Map defined in static inner class so it's only initialized lazily. 86 // Map defined in static inner class so it's only initialized lazily.
81 private static class PredefinedChannelGroups { 87 private static class PredefinedChannelGroups {
82 private static final Map<String, ChannelGroup> MAP; 88 static final Map<String, ChannelGroup> MAP;
83 static { 89 static {
84 Map<String, ChannelGroup> map = new HashMap<>(); 90 Map<String, ChannelGroup> map = new HashMap<>();
85 map.put(CHANNEL_GROUP_ID_GENERAL, 91 map.put(CHANNEL_GROUP_ID_GENERAL,
86 new ChannelGroup(CHANNEL_GROUP_ID_GENERAL, 92 new ChannelGroup(CHANNEL_GROUP_ID_GENERAL,
87 org.chromium.chrome.R.string.notification_category_g roup_general)); 93 org.chromium.chrome.R.string.notification_category_g roup_general));
88 MAP = Collections.unmodifiableMap(map); 94 MAP = Collections.unmodifiableMap(map);
89 } 95 }
90 } 96 }
91 97
92 private final NotificationManagerProxy mNotificationManager; 98 Set<String> getStartupChannelIds() {
93 99 // CHANNELS_VERSION must be incremented if the set of channels returned here changes.
94 public ChannelsInitializer(NotificationManagerProxy notificationManagerProxy ) { 100 return PredefinedChannels.MAP.keySet();
95 mNotificationManager = notificationManagerProxy;
96 } 101 }
97 102
98 public void initializeStartupChannels() { 103 ChannelGroup getChannelGroupFromId(Channel channel) {
99 // CHANNELS_VERSION must be incremented if the set of channels initializ ed here changes. 104 return PredefinedChannelGroups.MAP.get(channel.mGroupId);
100 for (@ChannelId String channelId : PredefinedChannels.MAP.keySet()) {
101 ensureInitialized(channelId);
102 }
103 } 105 }
104 106
105 void deleteAllChannels() { 107 Channel getChannelFromId(@ChannelId String channelId) {
106 for (String channelId : mNotificationManager.getNotificationChannelIds() ) { 108 return PredefinedChannels.MAP.get(channelId);
107 mNotificationManager.deleteNotificationChannel(channelId);
108 }
109 } 109 }
110 110
111 /** 111 /**
112 * Ensures the given channel has been created on the notification manager so a notification
113 * can be safely posted to it. This should only be used for channels that ar e predefined in
114 * ChannelsInitializer.
115 *
116 * Calling this is a (potentially lengthy) no-op if the channel has already been created.
117 *
118 * @param channelId The ID of the channel to be initialized.
119 */
120 public void ensureInitialized(@ChannelId String channelId) {
121 if (!PredefinedChannels.MAP.containsKey(channelId)) {
122 throw new IllegalStateException("Could not initialize channel: " + c hannelId);
123 }
124 Channel channel = PredefinedChannels.MAP.get(channelId);
125 // Channel group must be created before the channel.
126 mNotificationManager.createNotificationChannelGroup(
127 PredefinedChannelGroups.MAP.get(channel.mGroupId));
128 mNotificationManager.createNotificationChannel(channel);
129 }
130
131 /**
132 * Helper class containing notification channel properties. 112 * Helper class containing notification channel properties.
133 */ 113 */
134 public static class Channel { 114 public static class Channel {
135 @ChannelId 115 @ChannelId
136 public final String mId; 116 public final String mId;
137 final int mNameResId; 117 final int mNameResId;
138 final int mImportance; 118 final int mImportance;
139 @ChannelGroupId 119 @ChannelGroupId
140 final String mGroupId; 120 final String mGroupId;
141 121
(...skipping 13 matching lines...) Expand all
155 @ChannelGroupId 135 @ChannelGroupId
156 final String mId; 136 final String mId;
157 final int mNameResId; 137 final int mNameResId;
158 138
159 ChannelGroup(@ChannelGroupId String id, int nameResId) { 139 ChannelGroup(@ChannelGroupId String id, int nameResId) {
160 this.mId = id; 140 this.mId = id;
161 this.mNameResId = nameResId; 141 this.mNameResId = nameResId;
162 } 142 }
163 } 143 }
164 } 144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698