Skip to content

Commit 7196464

Browse files
cwrenAndroid (Google) Code Review
authored andcommitted
Merge "Allow the Notification.Builder to carry around a Style to apply at build" into jb-dev
2 parents 115f48a + fbd96ba commit 7196464

File tree

2 files changed

+108
-24
lines changed

2 files changed

+108
-24
lines changed

api/current.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,23 +3748,24 @@ package android.app {
37483748
field public long when;
37493749
}
37503750

3751-
public static class Notification.BigPictureStyle {
3751+
public static class Notification.BigPictureStyle extends android.app.Notification.Style {
3752+
ctor public Notification.BigPictureStyle();
37523753
ctor public Notification.BigPictureStyle(android.app.Notification.Builder);
37533754
method public android.app.Notification.BigPictureStyle bigPicture(android.graphics.Bitmap);
3754-
method public android.app.Notification build();
37553755
}
37563756

3757-
public static class Notification.BigTextStyle {
3757+
public static class Notification.BigTextStyle extends android.app.Notification.Style {
3758+
ctor public Notification.BigTextStyle();
37583759
ctor public Notification.BigTextStyle(android.app.Notification.Builder);
37593760
method public android.app.Notification.BigTextStyle bigText(java.lang.CharSequence);
3760-
method public android.app.Notification build();
37613761
}
37623762

37633763
public static class Notification.Builder {
37643764
ctor public Notification.Builder(android.content.Context);
37653765
method public android.app.Notification.Builder addAction(int, java.lang.CharSequence, android.app.PendingIntent);
37663766
method public android.app.Notification.Builder addKind(java.lang.String);
3767-
method public android.app.Notification getNotification();
3767+
method public android.app.Notification build();
3768+
method public deprecated android.app.Notification getNotification();
37683769
method public android.app.Notification.Builder setAutoCancel(boolean);
37693770
method public android.app.Notification.Builder setContent(android.widget.RemoteViews);
37703771
method public android.app.Notification.Builder setContentInfo(java.lang.CharSequence);
@@ -3785,6 +3786,7 @@ package android.app {
37853786
method public android.app.Notification.Builder setSmallIcon(int, int);
37863787
method public android.app.Notification.Builder setSound(android.net.Uri);
37873788
method public android.app.Notification.Builder setSound(android.net.Uri, int);
3789+
method public android.app.Notification.Builder setStyle(android.app.Notification.Style);
37883790
method public android.app.Notification.Builder setSubText(java.lang.CharSequence);
37893791
method public android.app.Notification.Builder setTicker(java.lang.CharSequence);
37903792
method public android.app.Notification.Builder setTicker(java.lang.CharSequence, android.widget.RemoteViews);
@@ -3793,10 +3795,17 @@ package android.app {
37933795
method public android.app.Notification.Builder setWhen(long);
37943796
}
37953797

3796-
public static class Notification.InboxStyle {
3798+
public static class Notification.InboxStyle extends android.app.Notification.Style {
3799+
ctor public Notification.InboxStyle();
37973800
ctor public Notification.InboxStyle(android.app.Notification.Builder);
37983801
method public android.app.Notification.InboxStyle addLine(java.lang.CharSequence);
3802+
}
3803+
3804+
public static class Notification.Style {
3805+
ctor public Notification.Style();
37993806
method public android.app.Notification build();
3807+
method public void setBuilder(android.app.Notification.Builder);
3808+
field protected android.app.Notification.Builder mBuilder;
38003809
}
38013810

38023811
public class NotificationManager {

core/java/android/app/Notification.java

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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(&quot;New mail from &quot; + 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

Comments
 (0)