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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java

Issue 1681123002: Plumb Notification action icons through to the UI layer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ActionIconResourceFetching
Patch Set: Rebase. Appease FindBugs some more. Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java
index 08ef5fb47bd1fc05f5ea60dc671ced168090cdcb..13c01bbbbffdbf6bb60d0968f04c24fa756062d7 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java
@@ -4,10 +4,12 @@
package org.chromium.chrome.browser.notifications;
+import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.graphics.Bitmap;
-import android.support.v4.app.NotificationCompat.Action;
+import android.graphics.drawable.Icon;
+import android.os.Build;
import org.chromium.base.VisibleForTesting;
@@ -21,6 +23,25 @@
* Abstract base class for building a notification. Stores all given arguments for later use.
*/
public abstract class NotificationBuilderBase {
+ protected static class Action {
+ public int iconId;
+ public Bitmap iconBitmap;
+ public CharSequence title;
+ public PendingIntent intent;
+
+ Action(int iconId, CharSequence title, PendingIntent intent) {
+ this.iconId = iconId;
+ this.title = title;
+ this.intent = intent;
+ }
+
+ Action(Bitmap iconBitmap, CharSequence title, PendingIntent intent) {
+ this.iconBitmap = iconBitmap;
+ this.title = title;
+ this.intent = intent;
+ }
+ }
+
/**
* 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
@@ -33,7 +54,7 @@
* 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;
+ private static final int MAX_ACTION_BUTTONS = 2;
Peter Beverloo 2016/02/10 12:29:37 This is because addSettingsAction() will be called
Michael van Ouwerkerk 2016/02/10 17:18:12 Done. This slipped when I moved the settings butto
protected CharSequence mTitle;
protected CharSequence mBody;
@@ -134,6 +155,20 @@ public NotificationBuilderBase addAction(
}
/**
+ * Adds an action to the notification. Actions are typically displayed as a button adjacent to
+ * the notification content.
+ */
+ public NotificationBuilderBase addAction(@Nullable Bitmap iconBitmap,
+ @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(iconBitmap, limitLength(title), intent));
+ return this;
+ }
+
+ /**
* Adds an action to the notification for opening the settings screen.
*/
public NotificationBuilderBase addSettingsAction(
@@ -183,4 +218,20 @@ private static CharSequence limitLength(@Nullable CharSequence input) {
}
return input;
}
+
+ /**
+ * Adds an action to {@code builder} using a {@code Bitmap} if possible, and otherwise a
+ * resource id if it is set.
+ */
+ @SuppressWarnings("deprecation") // For addAction(int, CharSequence, PendingIntent)
+ @TargetApi(Build.VERSION_CODES.M) // For the Icon class.
+ protected static void addAction(Notification.Builder builder, Action action) {
Peter Beverloo 2016/02/10 12:29:37 Could we give this a different name instead of ove
Michael van Ouwerkerk 2016/02/10 17:18:12 Sure, we do have many methods called addAction. Do
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && action.iconBitmap != null) {
+ Icon icon = Icon.createWithBitmap(action.iconBitmap);
+ builder.addAction(
+ new Notification.Action.Builder(icon, action.title, action.intent).build());
+ } else {
+ builder.addAction(action.iconId, action.title, action.intent);
+ }
+ }
}

Powered by Google App Engine
This is Rietveld 408576698