Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilder.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java |
similarity index 30% |
rename from chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilder.java |
rename to chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java |
index 112df41bd988d3508d07f481a68997c12dc7cf1e..8afc53a5a1334514ae2265f166bda778d6e2c4d7 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilder.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java |
@@ -1,4 +1,4 @@ |
-// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
@@ -7,71 +7,139 @@ |
import android.app.Notification; |
import android.app.PendingIntent; |
import android.graphics.Bitmap; |
+import android.support.v4.app.NotificationCompat.Action; |
+ |
+import org.chromium.base.VisibleForTesting; |
+ |
+import java.util.ArrayList; |
+import java.util.Arrays; |
+import java.util.List; |
import javax.annotation.Nullable; |
/** |
- * Builds a notification using the given inputs. |
+ * Abstract base class for building a notification. Stores all given arguments for later use. |
*/ |
-public interface NotificationBuilder { |
+public abstract class NotificationBuilderBase { |
+ /** |
+ * Maximum length of CharSequence inputs to prevent excessive memory consumption. At current |
+ * screen sizes we display about 500 characters at most, so this is a pretty generous limit, and |
+ * it matches what the Notification class does. |
+ */ |
+ @VisibleForTesting |
+ static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024; |
+ |
+ /** |
+ * The maximum number of action buttons. One is for the settings button, and two more slots are |
+ * for developer provided buttons. |
+ */ |
+ private static final int MAX_ACTION_BUTTONS = 3; |
+ |
+ protected CharSequence mTitle; |
+ protected CharSequence mBody; |
+ protected CharSequence mOrigin; |
+ protected CharSequence mTickerText; |
+ protected Bitmap mLargeIcon; |
+ protected int mSmallIconId; |
+ protected PendingIntent mContentIntent; |
+ protected PendingIntent mDeleteIntent; |
+ protected List<Action> mActions = new ArrayList<>(MAX_ACTION_BUTTONS); |
+ protected Action mSettingsAction; |
+ protected int mDefaults = Notification.DEFAULT_ALL; |
+ protected long[] mVibratePattern; |
/** |
* Combines all of the options that have been set and returns a new Notification object. |
*/ |
- Notification build(); |
+ public abstract Notification build(); |
/** |
- * Sets the title text (first row) of the notification. |
+ * Sets the title text of the notification. |
*/ |
- NotificationBuilder setTitle(@Nullable CharSequence title); |
+ public NotificationBuilderBase setTitle(@Nullable CharSequence title) { |
+ mTitle = limitLength(title); |
+ return this; |
+ } |
/** |
- * Sets the body text (second row) of the notification. |
+ * Sets the body text of the notification. |
*/ |
- NotificationBuilder setBody(@Nullable CharSequence body); |
+ public NotificationBuilderBase setBody(@Nullable CharSequence body) { |
+ mBody = limitLength(body); |
+ return this; |
+ } |
/** |
- * Sets the origin text (bottom row) of the notification. |
+ * Sets the origin text of the notification. |
*/ |
- NotificationBuilder setOrigin(@Nullable CharSequence origin); |
+ public NotificationBuilderBase setOrigin(@Nullable CharSequence origin) { |
+ mOrigin = limitLength(origin); |
+ return this; |
+ } |
/** |
* Sets the text that is displayed in the status bar when the notification first arrives. |
*/ |
- NotificationBuilder setTicker(@Nullable CharSequence tickerText); |
+ public NotificationBuilderBase setTicker(@Nullable CharSequence tickerText) { |
+ mTickerText = limitLength(tickerText); |
+ return this; |
+ } |
/** |
* Sets the large icon that is shown in the notification. |
*/ |
- NotificationBuilder setLargeIcon(@Nullable Bitmap icon); |
+ public NotificationBuilderBase setLargeIcon(@Nullable Bitmap icon) { |
+ mLargeIcon = icon; |
+ return this; |
+ } |
/** |
* Sets the the small icon that is shown in the notification and in the status bar. |
*/ |
- NotificationBuilder setSmallIcon(int iconId); |
+ public NotificationBuilderBase setSmallIcon(int iconId) { |
+ mSmallIconId = iconId; |
+ return this; |
+ } |
/** |
* Sets the PendingIntent to send when the notification is clicked. |
*/ |
- NotificationBuilder setContentIntent(@Nullable PendingIntent intent); |
+ public NotificationBuilderBase setContentIntent(@Nullable PendingIntent intent) { |
+ mContentIntent = intent; |
+ return this; |
+ } |
/** |
* Sets the PendingIntent to send when the notification is cleared by the user directly from the |
* notification panel. |
*/ |
- NotificationBuilder setDeleteIntent(@Nullable PendingIntent intent); |
+ public NotificationBuilderBase setDeleteIntent(@Nullable PendingIntent intent) { |
+ mDeleteIntent = intent; |
+ return this; |
+ } |
/** |
* Adds an action to the notification. Actions are typically displayed as a button adjacent to |
* the notification content. |
*/ |
- NotificationBuilder addAction( |
- int iconId, @Nullable CharSequence title, @Nullable PendingIntent intent); |
+ public NotificationBuilderBase addAction( |
+ int iconId, @Nullable CharSequence title, @Nullable PendingIntent intent) { |
+ if (mActions.size() == MAX_ACTION_BUTTONS) { |
+ throw new IllegalStateException( |
+ "Cannot add more than " + MAX_ACTION_BUTTONS + " actions."); |
+ } |
+ mActions.add(new Action(iconId, limitLength(title), intent)); |
+ return this; |
+ } |
/** |
* Adds an action to the notification for opening the settings screen. |
*/ |
- NotificationBuilder addSettingsAction(int iconId, CharSequence title, PendingIntent intent); |
+ public NotificationBuilderBase addSettingsAction( |
+ int iconId, @Nullable CharSequence title, @Nullable PendingIntent intent) { |
+ mSettingsAction = new Action(iconId, limitLength(title), intent); |
+ return this; |
+ } |
/** |
* Sets the default notification options that will be used. |
@@ -83,10 +151,27 @@ NotificationBuilder addAction( |
* <p> |
* For all default values, use {@link Notification#DEFAULT_ALL}. |
*/ |
- NotificationBuilder setDefaults(int defaults); |
+ public NotificationBuilderBase setDefaults(int defaults) { |
+ mDefaults = defaults; |
+ return this; |
+ } |
/** |
* Sets the vibration pattern to use. |
*/ |
- NotificationBuilder setVibrate(@Nullable long[] pattern); |
+ public NotificationBuilderBase setVibrate(long[] pattern) { |
+ mVibratePattern = Arrays.copyOf(pattern, pattern.length); |
+ return this; |
+ } |
+ |
+ @Nullable |
+ private static CharSequence limitLength(@Nullable CharSequence input) { |
+ if (input == null) { |
+ return input; |
+ } |
+ if (input.length() > MAX_CHARSEQUENCE_LENGTH) { |
+ return input.subSequence(0, MAX_CHARSEQUENCE_LENGTH); |
+ } |
+ return input; |
+ } |
} |