@@ -888,7 +888,7 @@ public String toString() {
888888 * .setContentText(subject)
889889 * .setSmallIcon(R.drawable.new_mail)
890890 * .setLargeIcon(aBitmap)
891- * .getNotification ();
891+ * .build ();
892892 * </pre>
893893 */
894894 public static class Builder {
@@ -925,6 +925,7 @@ public static class Builder {
925925 private int mPriority ;
926926 private ArrayList <Action > mActions = new ArrayList <Action >(3 );
927927 private boolean mUseChronometer ;
928+ private Style mStyle ;
928929
929930 /**
930931 * Constructs a new Builder with the defaults:
@@ -1305,7 +1306,7 @@ public Builder addKind(String k) {
13051306 * Add metadata to this notification.
13061307 *
13071308 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
1308- * current contents are copied into the Notification each time {@link #getNotification ()} is
1309+ * current contents are copied into the Notification each time {@link #build ()} is
13091310 * called.
13101311 *
13111312 * @see Notification#extras
@@ -1329,6 +1330,19 @@ public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
13291330 return this ;
13301331 }
13311332
1333+ /**
1334+ * Add a rich notification style to be applied at build time.
1335+ *
1336+ * @param style Object responsible for modifying the notification style.
1337+ */
1338+ public Builder setStyle (Style style ) {
1339+ if (mStyle != style ) {
1340+ mStyle = style ;
1341+ mStyle .setBuilder (this );
1342+ }
1343+ return this ;
1344+ }
1345+
13321346 private void setFlag (int mask , boolean value ) {
13331347 if (value ) {
13341348 mFlags |= mask ;
@@ -1464,10 +1478,9 @@ private RemoteViews generateActionButton(Action action) {
14641478 }
14651479
14661480 /**
1467- * Combine all of the options that have been set and return a new {@link Notification}
1468- * object.
1481+ * Apply the unstyled operations and return a new {@link Notification} object.
14691482 */
1470- public Notification getNotification () {
1483+ private Notification buildUnstyled () {
14711484 Notification n = new Notification ();
14721485 n .when = mWhen ;
14731486 n .icon = mSmallIcon ;
@@ -1509,6 +1522,49 @@ public Notification getNotification() {
15091522 }
15101523 return n ;
15111524 }
1525+
1526+ /**
1527+ * @deprecated Use {@link #build()} instead.
1528+ */
1529+ @ Deprecated
1530+ public Notification getNotification () {
1531+ return build ();
1532+ }
1533+
1534+ /**
1535+ * Combine all of the options that have been set and return a new {@link Notification}
1536+ * object.
1537+ */
1538+ public Notification build () {
1539+ if (mStyle != null ) {
1540+ return mStyle .build ();
1541+ } else {
1542+ return buildUnstyled ();
1543+ }
1544+ }
1545+ }
1546+
1547+
1548+ /**
1549+ * An object that can apply a rich notification style to a {@link Notification.Builder}
1550+ * object.
1551+ */
1552+ public static class Style {
1553+ protected Builder mBuilder ;
1554+
1555+ public void setBuilder (Builder builder ) {
1556+ if (mBuilder != builder ) {
1557+ mBuilder = builder ;
1558+ mBuilder .setStyle (this );
1559+ }
1560+ }
1561+
1562+ public Notification build () {
1563+ if (mBuilder == null ) {
1564+ throw new IllegalArgumentException ("Style requires a valid Builder object" );
1565+ }
1566+ return mBuilder .buildUnstyled ();
1567+ }
15121568 }
15131569
15141570 /**
@@ -1528,12 +1584,14 @@ public Notification getNotification() {
15281584 *
15291585 * @see Notification#bigContentView
15301586 */
1531- public static class BigPictureStyle {
1532- private Builder mBuilder ;
1587+ public static class BigPictureStyle extends Style {
15331588 private Bitmap mPicture ;
15341589
1590+ public BigPictureStyle () {
1591+ }
1592+
15351593 public BigPictureStyle (Builder builder ) {
1536- mBuilder = builder ;
1594+ setBuilder ( builder ) ;
15371595 }
15381596
15391597 public BigPictureStyle bigPicture (Bitmap b ) {
@@ -1549,8 +1607,12 @@ private RemoteViews makeBigContentView() {
15491607 return contentView ;
15501608 }
15511609
1610+ @ Override
15521611 public Notification build () {
1553- Notification wip = mBuilder .getNotification ();
1612+ if (mBuilder == null ) {
1613+ throw new IllegalArgumentException ("Style requires a valid Builder object" );
1614+ }
1615+ Notification wip = mBuilder .buildUnstyled ();
15541616 wip .bigContentView = makeBigContentView ();
15551617 return wip ;
15561618 }
@@ -1573,12 +1635,14 @@ public Notification build() {
15731635 *
15741636 * @see Notification#bigContentView
15751637 */
1576- public static class BigTextStyle {
1577- private Builder mBuilder ;
1638+ public static class BigTextStyle extends Style {
15781639 private CharSequence mBigText ;
15791640
1641+ public BigTextStyle () {
1642+ }
1643+
15801644 public BigTextStyle (Builder builder ) {
1581- mBuilder = builder ;
1645+ setBuilder ( builder ) ;
15821646 }
15831647
15841648 public BigTextStyle bigText (CharSequence cs ) {
@@ -1596,8 +1660,13 @@ private RemoteViews makeBigContentView() {
15961660 return contentView ;
15971661 }
15981662
1663+ @ Override
15991664 public Notification build () {
1600- Notification wip = mBuilder .getNotification ();
1665+ if (mBuilder == null ) {
1666+ throw new IllegalArgumentException ("Style requires a valid Builder object" );
1667+ }
1668+ mBuilder .mSubText = null ;
1669+ Notification wip = mBuilder .buildUnstyled ();
16011670 wip .bigContentView = makeBigContentView ();
16021671 return wip ;
16031672 }
@@ -1608,7 +1677,7 @@ public Notification build() {
16081677 *
16091678 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
16101679 * <pre class="prettyprint">
1611- * Notification noti = new Notification.DigestStyle (
1680+ * Notification noti = new Notification.InboxStyle (
16121681 * new Notification.Builder()
16131682 * .setContentTitle("New mail from " + sender.toString())
16141683 * .setContentText(subject)
@@ -1621,12 +1690,14 @@ public Notification build() {
16211690 *
16221691 * @see Notification#bigContentView
16231692 */
1624- public static class InboxStyle {
1625- private Builder mBuilder ;
1693+ public static class InboxStyle extends Style {
16261694 private ArrayList <CharSequence > mTexts = new ArrayList <CharSequence >(5 );
16271695
1696+ public InboxStyle () {
1697+ }
1698+
16281699 public InboxStyle (Builder builder ) {
1629- mBuilder = builder ;
1700+ setBuilder ( builder ) ;
16301701 }
16311702
16321703 public InboxStyle addLine (CharSequence cs ) {
@@ -1652,8 +1723,12 @@ private RemoteViews makeBigContentView() {
16521723 return contentView ;
16531724 }
16541725
1726+ @ Override
16551727 public Notification build () {
1656- Notification wip = mBuilder .getNotification ();
1728+ if (mBuilder == null ) {
1729+ throw new IllegalArgumentException ("Style requires a valid Builder object" );
1730+ }
1731+ Notification wip = mBuilder .buildUnstyled ();
16571732 wip .bigContentView = makeBigContentView ();
16581733 return wip ;
16591734 }
0 commit comments