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