From 720042b72f10f5e67a173adc76ebf939bf6ecc4e Mon Sep 17 00:00:00 2001 From: Griff Hazen Date: Mon, 24 Feb 2014 15:46:56 -0800 Subject: [PATCH] Add addExtras and getExtras to Notification.Builder. addExtras allows merging with exiting set extras instead of replacing them. This matches the similar function putExtras in Intent. Both functions are useful for multi-stage notification building logic. Change-Id: Ice3e4a53ec05b7129ebdac14e2084163946273a4 --- api/current.txt | 2 ++ core/java/android/app/Notification.java | 44 ++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/api/current.txt b/api/current.txt index 0de7027d8af51..e0b49990e6705 100644 --- a/api/current.txt +++ b/api/current.txt @@ -4010,7 +4010,9 @@ package android.app { public static class Notification.Builder { ctor public Notification.Builder(android.content.Context); method public android.app.Notification.Builder addAction(int, java.lang.CharSequence, android.app.PendingIntent); + method public android.app.Notification.Builder addExtras(android.os.Bundle); method public android.app.Notification build(); + method public android.os.Bundle getExtras(); method public deprecated android.app.Notification getNotification(); method public android.app.Notification.Builder setAutoCancel(boolean); method public android.app.Notification.Builder setContent(android.widget.RemoteViews); diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index 1e96ebf3d1c50..dd2dc01215405 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -1586,12 +1586,31 @@ public class Notification implements Parcelable } /** - * Add metadata to this notification. + * Merge additional metadata into this notification. * - * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's + *

Values within the Bundle will replace existing extras values in this Builder. + * + * @see Notification#extras + */ + public Builder addExtras(Bundle bag) { + if (mExtras == null) { + mExtras = new Bundle(bag); + } else { + mExtras.putAll(bag); + } + return this; + } + + /** + * Set metadata for this notification. + * + *

A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's * current contents are copied into the Notification each time {@link #build()} is * called. * + *

Replaces any existing extras values with those from the provided Bundle. + * Use {@link #addExtras} to merge in metadata instead. + * * @see Notification#extras */ public Builder setExtras(Bundle bag) { @@ -1599,6 +1618,23 @@ public class Notification implements Parcelable return this; } + /** + * Get the current metadata Bundle used by this notification Builder. + * + *

The returned Bundle is shared with this Builder. + * + *

The current contents of this Bundle are copied into the Notification each time + * {@link #build()} is called. + * + * @see Notification#extras + */ + public Bundle getExtras() { + if (mExtras == null) { + mExtras = new Bundle(); + } + return mExtras; + } + /** * Add an action to this notification. Actions are typically displayed by * the system as a button adjacent to the notification content. @@ -1857,7 +1893,7 @@ public class Notification implements Parcelable * this Notification object. * @hide */ - public void addExtras(Bundle extras) { + public void populateExtras(Bundle extras) { // Store original information used in the construction of this object extras.putCharSequence(EXTRA_TITLE, mContentTitle); extras.putCharSequence(EXTRA_TEXT, mContentText); @@ -1895,7 +1931,7 @@ public class Notification implements Parcelable n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle(); - addExtras(n.extras); + populateExtras(n.extras); if (mStyle != null) { mStyle.addExtras(n.extras); }