OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.app.Notification; | 7 import android.app.Notification; |
8 import android.app.PendingIntent; | 8 import android.app.PendingIntent; |
9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
| 10 import android.support.v4.app.NotificationCompat.Action; |
| 11 |
| 12 import org.chromium.base.VisibleForTesting; |
| 13 |
| 14 import java.util.ArrayList; |
| 15 import java.util.Arrays; |
| 16 import java.util.List; |
10 | 17 |
11 import javax.annotation.Nullable; | 18 import javax.annotation.Nullable; |
12 | 19 |
13 /** | 20 /** |
14 * Builds a notification using the given inputs. | 21 * Abstract base class for building a notification. Stores all given arguments f
or later use. |
15 */ | 22 */ |
16 public interface NotificationBuilder { | 23 public abstract class NotificationBuilderBase { |
| 24 /** |
| 25 * Maximum length of CharSequence inputs to prevent excessive memory consump
tion. At current |
| 26 * screen sizes we display about 500 characters at most, so this is a pretty
generous limit, and |
| 27 * it matches what the Notification class does. |
| 28 */ |
| 29 @VisibleForTesting |
| 30 static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024; |
| 31 |
| 32 /** |
| 33 * The maximum number of action buttons. One is for the settings button, and
two more slots are |
| 34 * for developer provided buttons. |
| 35 */ |
| 36 private static final int MAX_ACTION_BUTTONS = 3; |
| 37 |
| 38 protected CharSequence mTitle; |
| 39 protected CharSequence mBody; |
| 40 protected CharSequence mOrigin; |
| 41 protected CharSequence mTickerText; |
| 42 protected Bitmap mLargeIcon; |
| 43 protected int mSmallIconId; |
| 44 protected PendingIntent mContentIntent; |
| 45 protected PendingIntent mDeleteIntent; |
| 46 protected List<Action> mActions = new ArrayList<>(MAX_ACTION_BUTTONS); |
| 47 protected Action mSettingsAction; |
| 48 protected int mDefaults = Notification.DEFAULT_ALL; |
| 49 protected long[] mVibratePattern; |
17 | 50 |
18 /** | 51 /** |
19 * Combines all of the options that have been set and returns a new Notifica
tion object. | 52 * Combines all of the options that have been set and returns a new Notifica
tion object. |
20 */ | 53 */ |
21 Notification build(); | 54 public abstract Notification build(); |
22 | 55 |
23 /** | 56 /** |
24 * Sets the title text (first row) of the notification. | 57 * Sets the title text of the notification. |
25 */ | 58 */ |
26 NotificationBuilder setTitle(@Nullable CharSequence title); | 59 public NotificationBuilderBase setTitle(@Nullable CharSequence title) { |
| 60 mTitle = limitLength(title); |
| 61 return this; |
| 62 } |
27 | 63 |
28 /** | 64 /** |
29 * Sets the body text (second row) of the notification. | 65 * Sets the body text of the notification. |
30 */ | 66 */ |
31 NotificationBuilder setBody(@Nullable CharSequence body); | 67 public NotificationBuilderBase setBody(@Nullable CharSequence body) { |
| 68 mBody = limitLength(body); |
| 69 return this; |
| 70 } |
32 | 71 |
33 /** | 72 /** |
34 * Sets the origin text (bottom row) of the notification. | 73 * Sets the origin text of the notification. |
35 */ | 74 */ |
36 NotificationBuilder setOrigin(@Nullable CharSequence origin); | 75 public NotificationBuilderBase setOrigin(@Nullable CharSequence origin) { |
| 76 mOrigin = limitLength(origin); |
| 77 return this; |
| 78 } |
37 | 79 |
38 /** | 80 /** |
39 * Sets the text that is displayed in the status bar when the notification f
irst arrives. | 81 * Sets the text that is displayed in the status bar when the notification f
irst arrives. |
40 */ | 82 */ |
41 NotificationBuilder setTicker(@Nullable CharSequence tickerText); | 83 public NotificationBuilderBase setTicker(@Nullable CharSequence tickerText)
{ |
| 84 mTickerText = limitLength(tickerText); |
| 85 return this; |
| 86 } |
42 | 87 |
43 /** | 88 /** |
44 * Sets the large icon that is shown in the notification. | 89 * Sets the large icon that is shown in the notification. |
45 */ | 90 */ |
46 NotificationBuilder setLargeIcon(@Nullable Bitmap icon); | 91 public NotificationBuilderBase setLargeIcon(@Nullable Bitmap icon) { |
| 92 mLargeIcon = icon; |
| 93 return this; |
| 94 } |
47 | 95 |
48 /** | 96 /** |
49 * Sets the the small icon that is shown in the notification and in the stat
us bar. | 97 * Sets the the small icon that is shown in the notification and in the stat
us bar. |
50 */ | 98 */ |
51 NotificationBuilder setSmallIcon(int iconId); | 99 public NotificationBuilderBase setSmallIcon(int iconId) { |
| 100 mSmallIconId = iconId; |
| 101 return this; |
| 102 } |
52 | 103 |
53 /** | 104 /** |
54 * Sets the PendingIntent to send when the notification is clicked. | 105 * Sets the PendingIntent to send when the notification is clicked. |
55 */ | 106 */ |
56 NotificationBuilder setContentIntent(@Nullable PendingIntent intent); | 107 public NotificationBuilderBase setContentIntent(@Nullable PendingIntent inte
nt) { |
| 108 mContentIntent = intent; |
| 109 return this; |
| 110 } |
57 | 111 |
58 /** | 112 /** |
59 * Sets the PendingIntent to send when the notification is cleared by the us
er directly from the | 113 * Sets the PendingIntent to send when the notification is cleared by the us
er directly from the |
60 * notification panel. | 114 * notification panel. |
61 */ | 115 */ |
62 NotificationBuilder setDeleteIntent(@Nullable PendingIntent intent); | 116 public NotificationBuilderBase setDeleteIntent(@Nullable PendingIntent inten
t) { |
| 117 mDeleteIntent = intent; |
| 118 return this; |
| 119 } |
63 | 120 |
64 /** | 121 /** |
65 * Adds an action to the notification. Actions are typically displayed as a
button adjacent to | 122 * Adds an action to the notification. Actions are typically displayed as a
button adjacent to |
66 * the notification content. | 123 * the notification content. |
67 */ | 124 */ |
68 NotificationBuilder addAction( | 125 public NotificationBuilderBase addAction( |
69 int iconId, @Nullable CharSequence title, @Nullable PendingIntent in
tent); | 126 int iconId, @Nullable CharSequence title, @Nullable PendingIntent in
tent) { |
| 127 if (mActions.size() == MAX_ACTION_BUTTONS) { |
| 128 throw new IllegalStateException( |
| 129 "Cannot add more than " + MAX_ACTION_BUTTONS + " actions."); |
| 130 } |
| 131 mActions.add(new Action(iconId, limitLength(title), intent)); |
| 132 return this; |
| 133 } |
70 | 134 |
71 /** | 135 /** |
72 * Adds an action to the notification for opening the settings screen. | 136 * Adds an action to the notification for opening the settings screen. |
73 */ | 137 */ |
74 NotificationBuilder addSettingsAction(int iconId, CharSequence title, Pendin
gIntent intent); | 138 public NotificationBuilderBase addSettingsAction( |
| 139 int iconId, @Nullable CharSequence title, @Nullable PendingIntent in
tent) { |
| 140 mSettingsAction = new Action(iconId, limitLength(title), intent); |
| 141 return this; |
| 142 } |
75 | 143 |
76 /** | 144 /** |
77 * Sets the default notification options that will be used. | 145 * Sets the default notification options that will be used. |
78 * <p> | 146 * <p> |
79 * The value should be one or more of the following fields combined with | 147 * The value should be one or more of the following fields combined with |
80 * bitwise-or: | 148 * bitwise-or: |
81 * {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE}, | 149 * {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE}, |
82 * {@link Notification#DEFAULT_LIGHTS}. | 150 * {@link Notification#DEFAULT_LIGHTS}. |
83 * <p> | 151 * <p> |
84 * For all default values, use {@link Notification#DEFAULT_ALL}. | 152 * For all default values, use {@link Notification#DEFAULT_ALL}. |
85 */ | 153 */ |
86 NotificationBuilder setDefaults(int defaults); | 154 public NotificationBuilderBase setDefaults(int defaults) { |
| 155 mDefaults = defaults; |
| 156 return this; |
| 157 } |
87 | 158 |
88 /** | 159 /** |
89 * Sets the vibration pattern to use. | 160 * Sets the vibration pattern to use. |
90 */ | 161 */ |
91 NotificationBuilder setVibrate(@Nullable long[] pattern); | 162 public NotificationBuilderBase setVibrate(long[] pattern) { |
| 163 mVibratePattern = Arrays.copyOf(pattern, pattern.length); |
| 164 return this; |
| 165 } |
| 166 |
| 167 @Nullable |
| 168 private static CharSequence limitLength(@Nullable CharSequence input) { |
| 169 if (input == null) { |
| 170 return input; |
| 171 } |
| 172 if (input.length() > MAX_CHARSEQUENCE_LENGTH) { |
| 173 return input.subSequence(0, MAX_CHARSEQUENCE_LENGTH); |
| 174 } |
| 175 return input; |
| 176 } |
92 } | 177 } |
OLD | NEW |