OLD | NEW |
---|---|
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; |
(...skipping 17 matching lines...) Expand all Loading... | |
28 public static final String CHANNEL_ID_SITES = "sites"; | 28 public static final String CHANNEL_ID_SITES = "sites"; |
29 | 29 |
30 @StringDef({CHANNEL_GROUP_ID_GENERAL}) | 30 @StringDef({CHANNEL_GROUP_ID_GENERAL}) |
31 @Retention(RetentionPolicy.SOURCE) | 31 @Retention(RetentionPolicy.SOURCE) |
32 private @interface ChannelGroupId {} | 32 private @interface ChannelGroupId {} |
33 static final String CHANNEL_GROUP_ID_GENERAL = "general"; | 33 static final String CHANNEL_GROUP_ID_GENERAL = "general"; |
34 | 34 |
35 // Map defined in static inner class so it's only initialized lazily. | 35 // Map defined in static inner class so it's only initialized lazily. |
36 @TargetApi(Build.VERSION_CODES.N) // for NotificationManager.IMPORTANCE_* co nstants | 36 @TargetApi(Build.VERSION_CODES.N) // for NotificationManager.IMPORTANCE_* co nstants |
37 private static class PredefinedChannels { | 37 private static class PredefinedChannels { |
38 /** | |
39 * The set of predefined channels to be initialized on startup. | |
40 * {@link ChannelsUpdater#CHANNELS_VERSION} must be incremented every ti me an entry is | |
Peter Beverloo
2017/04/11 19:08:29
Maybe consider making this a static member of Chan
awdf
2017/04/12 13:58:15
Done.
| |
41 * modified, removed or added to this map. | |
42 */ | |
38 private static final Map<String, Channel> MAP; | 43 private static final Map<String, Channel> MAP; |
39 static { | 44 static { |
40 Map<String, Channel> map = new HashMap<>(); | 45 Map<String, Channel> map = new HashMap<>(); |
41 map.put(CHANNEL_ID_BROWSER, | 46 map.put(CHANNEL_ID_BROWSER, |
42 new Channel(CHANNEL_ID_BROWSER, | 47 new Channel(CHANNEL_ID_BROWSER, |
43 org.chromium.chrome.R.string.notification_category_b rowser, | 48 org.chromium.chrome.R.string.notification_category_b rowser, |
44 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL)); | 49 NotificationManager.IMPORTANCE_LOW, CHANNEL_GROUP_ID _GENERAL)); |
45 map.put(CHANNEL_ID_SITES, | 50 map.put(CHANNEL_ID_SITES, |
46 new Channel(CHANNEL_ID_SITES, | 51 new Channel(CHANNEL_ID_SITES, |
47 org.chromium.chrome.R.string.notification_category_s ites, | 52 org.chromium.chrome.R.string.notification_category_s ites, |
(...skipping 13 matching lines...) Expand all Loading... | |
61 MAP = Collections.unmodifiableMap(map); | 66 MAP = Collections.unmodifiableMap(map); |
62 } | 67 } |
63 } | 68 } |
64 | 69 |
65 private final NotificationManagerProxy mNotificationManager; | 70 private final NotificationManagerProxy mNotificationManager; |
66 | 71 |
67 public ChannelsInitializer(NotificationManagerProxy notificationManagerProxy ) { | 72 public ChannelsInitializer(NotificationManagerProxy notificationManagerProxy ) { |
68 mNotificationManager = notificationManagerProxy; | 73 mNotificationManager = notificationManagerProxy; |
69 } | 74 } |
70 | 75 |
76 public void initializeStartupChannels() { | |
77 for (@ChannelId String channelId : PredefinedChannels.MAP.keySet()) { | |
78 ensureInitialized(channelId); | |
79 } | |
80 } | |
81 | |
82 void deleteAllChannels() { | |
83 for (String channelId : mNotificationManager.getNotificationChannelIds() ) { | |
84 mNotificationManager.deleteNotificationChannel(channelId); | |
85 } | |
86 } | |
87 | |
71 /** | 88 /** |
72 * Ensures the given channel has been created on the notification manager so a notification | 89 * Ensures the given channel has been created on the notification manager so a notification |
73 * can be safely posted to it. This should only be used for channels that ar e predefined in | 90 * can be safely posted to it. This should only be used for channels that ar e predefined in |
74 * ChannelsInitializer. | 91 * ChannelsInitializer. |
75 * | 92 * |
76 * Calling this is a (potentially lengthy) no-op if the channel has already been created. | 93 * Calling this is a (potentially lengthy) no-op if the channel has already been created. |
77 * | 94 * |
78 * @param channelId The ID of the channel to be initialized. | 95 * @param channelId The ID of the channel to be initialized. |
79 */ | 96 */ |
80 public void ensureInitialized(@ChannelId String channelId) { | 97 public void ensureInitialized(@ChannelId String channelId) { |
81 if (!PredefinedChannels.MAP.containsKey(channelId)) { | 98 if (!PredefinedChannels.MAP.containsKey(channelId)) { |
82 throw new IllegalStateException("Could not initialize channel: " + c hannelId); | 99 throw new IllegalStateException("Could not initialize channel: " + c hannelId); |
83 } | 100 } |
84 Channel channel = PredefinedChannels.MAP.get(channelId); | 101 Channel channel = PredefinedChannels.MAP.get(channelId); |
85 // Channel group must be created before the channel. | 102 // Channel group must be created before the channel. |
86 mNotificationManager.createNotificationChannelGroup( | 103 mNotificationManager.createNotificationChannelGroup( |
87 PredefinedChannelGroups.MAP.get(channel.mGroupId)); | 104 PredefinedChannelGroups.MAP.get(channel.mGroupId)); |
88 mNotificationManager.createNotificationChannel(channel); | 105 mNotificationManager.createNotificationChannel(channel); |
89 } | 106 } |
90 | 107 |
91 /** | 108 /** |
92 * Helper class containing notification channel properties. | 109 * Helper class containing notification channel properties. |
93 */ | 110 */ |
94 public static class Channel { | 111 public static class Channel { |
95 @ChannelId | 112 @ChannelId |
96 final String mId; | 113 public final String mId; |
97 final int mNameResId; | 114 final int mNameResId; |
98 final int mImportance; | 115 final int mImportance; |
99 @ChannelGroupId | 116 @ChannelGroupId |
100 final String mGroupId; | 117 final String mGroupId; |
101 | 118 |
102 Channel(@ChannelId String id, int nameResId, int importance, | 119 Channel(@ChannelId String id, int nameResId, int importance, |
103 @ChannelGroupId String groupId) { | 120 @ChannelGroupId String groupId) { |
104 this.mId = id; | 121 this.mId = id; |
105 this.mNameResId = nameResId; | 122 this.mNameResId = nameResId; |
106 this.mImportance = importance; | 123 this.mImportance = importance; |
107 this.mGroupId = groupId; | 124 this.mGroupId = groupId; |
108 } | 125 } |
109 } | 126 } |
110 | 127 |
111 /** | 128 /** |
112 * Helper class containing notification channel group properties. | 129 * Helper class containing notification channel group properties. |
113 */ | 130 */ |
114 public static class ChannelGroup { | 131 public static class ChannelGroup { |
115 @ChannelGroupId | 132 @ChannelGroupId |
116 final String mId; | 133 final String mId; |
117 final int mNameResId; | 134 final int mNameResId; |
118 | 135 |
119 ChannelGroup(@ChannelGroupId String id, int nameResId) { | 136 ChannelGroup(@ChannelGroupId String id, int nameResId) { |
120 this.mId = id; | 137 this.mId = id; |
121 this.mNameResId = nameResId; | 138 this.mNameResId = nameResId; |
122 } | 139 } |
123 } | 140 } |
124 } | 141 } |
OLD | NEW |