NotificationCompat.java revision 62d32dda7a6dd510ba7bbf11bb3edaa314c4948e
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.app; 18 19import android.app.Notification; 20import android.app.NotificationManager; 21import android.app.PendingIntent; 22import android.content.Context; 23import android.graphics.Bitmap; 24import android.media.AudioManager; 25import android.net.Uri; 26import android.os.Build; 27import android.os.Bundle; 28import android.widget.RemoteViews; 29import java.util.ArrayList; 30 31/** 32 * Helper for accessing features in {@link android.app.Notification} 33 * introduced after API level 4 in a backwards compatible fashion. 34 */ 35public class NotificationCompat { 36 /** 37 * Obsolete flag indicating high-priority notifications; use the priority field instead. 38 * 39 * @deprecated Use {@link NotificationCompat.Builder#setPriority(int)} with a positive value. 40 */ 41 public static final int FLAG_HIGH_PRIORITY = 0x00000080; 42 43 /** 44 * Default notification priority for {@link NotificationCompat.Builder#setPriority(int)}. 45 * If your application does not prioritize its own notifications, 46 * use this value for all notifications. 47 */ 48 public static final int PRIORITY_DEFAULT = 0; 49 50 /** 51 * Lower notification priority for {@link NotificationCompat.Builder#setPriority(int)}, 52 * for items that are less important. The UI may choose to show 53 * these items smaller, or at a different position in the list, 54 * compared with your app's {@link #PRIORITY_DEFAULT} items. 55 */ 56 public static final int PRIORITY_LOW = -1; 57 58 /** 59 * Lowest notification priority for {@link NotificationCompat.Builder#setPriority(int)}; 60 * these items might not be shown to the user except under 61 * special circumstances, such as detailed notification logs. 62 */ 63 public static final int PRIORITY_MIN = -2; 64 65 /** 66 * Higher notification priority for {@link NotificationCompat.Builder#setPriority(int)}, 67 * for more important notifications or alerts. The UI may choose 68 * to show these items larger, or at a different position in 69 * notification lists, compared with your app's {@link #PRIORITY_DEFAULT} items. 70 */ 71 public static final int PRIORITY_HIGH = 1; 72 73 /** 74 * Highest notification priority for {@link NotificationCompat.Builder#setPriority(int)}, 75 * for your application's most important items that require the user's 76 * prompt attention or input. 77 */ 78 public static final int PRIORITY_MAX = 2; 79 80 private static final NotificationCompatImpl IMPL; 81 82 interface NotificationCompatImpl { 83 public Notification build(Builder b); 84 public Bundle getExtras(Notification n); 85 public boolean getLocalOnly(Notification n); 86 } 87 88 static class NotificationCompatImplBase implements NotificationCompatImpl { 89 @Override 90 public Notification build(Builder b) { 91 Notification result = b.mNotification; 92 result.setLatestEventInfo(b.mContext, b.mContentTitle, 93 b.mContentText, b.mContentIntent); 94 // translate high priority requests into legacy flag 95 if (b.mPriority > PRIORITY_DEFAULT) { 96 result.flags |= FLAG_HIGH_PRIORITY; 97 } 98 return result; 99 } 100 101 @Override 102 public Bundle getExtras(Notification n) { 103 return null; 104 } 105 106 @Override 107 public boolean getLocalOnly(Notification n) { 108 return false; 109 } 110 } 111 112 static class NotificationCompatImplGingerbread extends NotificationCompatImplBase { 113 @Override 114 public Notification build(Builder b) { 115 Notification result = b.mNotification; 116 result.setLatestEventInfo(b.mContext, b.mContentTitle, 117 b.mContentText, b.mContentIntent); 118 result = NotificationCompatGingerbread.add(result, b.mContext, 119 b.mContentTitle, b.mContentText, b.mContentIntent, b.mFullScreenIntent); 120 // translate high priority requests into legacy flag 121 if (b.mPriority > PRIORITY_DEFAULT) { 122 result.flags |= FLAG_HIGH_PRIORITY; 123 } 124 return result; 125 } 126 } 127 128 static class NotificationCompatImplHoneycomb extends NotificationCompatImplBase { 129 @Override 130 public Notification build(Builder b) { 131 return NotificationCompatHoneycomb.add(b.mContext, b.mNotification, 132 b.mContentTitle, b.mContentText, b.mContentInfo, b.mTickerView, 133 b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon); 134 } 135 } 136 137 static class NotificationCompatImplIceCreamSandwich extends NotificationCompatImplBase { 138 @Override 139 public Notification build(Builder b) { 140 return NotificationCompatIceCreamSandwich.add(b.mContext, b.mNotification, 141 b.mContentTitle, b.mContentText, b.mContentInfo, b.mTickerView, 142 b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon, 143 b.mProgressMax, b.mProgress, b.mProgressIndeterminate); 144 } 145 } 146 147 static class NotificationCompatImplJellybean extends NotificationCompatImplBase { 148 @Override 149 public Notification build(Builder b) { 150 NotificationCompatJellybean.Builder builder = new NotificationCompatJellybean.Builder( 151 b.mContext, b.mNotification, b.mContentTitle, b.mContentText, b.mContentInfo, 152 b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon, 153 b.mProgressMax, b.mProgress, b.mProgressIndeterminate, 154 b.mUseChronometer, b.mPriority, b.mSubText, b.mLocalOnly); 155 addActionsToBuilder(builder, b.mActions); 156 addStyleToBuilderJellybean(builder, b.mStyle); 157 return builder.build(); 158 } 159 160 @Override 161 public Bundle getExtras(Notification n) { 162 return NotificationCompatJellybean.getExtras(n); 163 } 164 165 @Override 166 public boolean getLocalOnly(Notification notif) { 167 return NotificationCompatJellybean.getLocalOnly(notif); 168 } 169 } 170 171 static class NotificationCompatImplCurrent extends NotificationCompatImplBase { 172 @Override 173 public Notification build(Builder b) { 174 NotificationCompatCurrent.Builder builder = new NotificationCompatCurrent.Builder( 175 b.mContext, b.mNotification, b.mContentTitle, b.mContentText, b.mContentInfo, 176 b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon, 177 b.mProgressMax, b.mProgress, b.mProgressIndeterminate, 178 b.mUseChronometer, b.mPriority, b.mSubText, b.mLocalOnly); 179 addActionsToBuilder(builder, b.mActions); 180 addStyleToBuilderJellybean(builder, b.mStyle); 181 return builder.build(); 182 } 183 184 @Override 185 public Bundle getExtras(Notification n) { 186 return NotificationCompatCurrent.getExtras(n); 187 } 188 189 @Override 190 public boolean getLocalOnly(Notification notif) { 191 return NotificationCompatCurrent.getLocalOnly(notif); 192 } 193 } 194 195 private static void addActionsToBuilder(NotificationBuilderWithActions builder, 196 ArrayList<Action> actions) { 197 for (Action action : actions) { 198 builder.addAction(action.icon, action.title, action.actionIntent); 199 } 200 } 201 202 private static void addStyleToBuilderJellybean(NotificationBuilderWithBuilderAccessor builder, 203 Style style) { 204 if (style != null) { 205 if (style instanceof BigTextStyle) { 206 BigTextStyle bigTextStyle = (BigTextStyle) style; 207 NotificationCompatJellybean.addBigTextStyle(builder, 208 bigTextStyle.mBigContentTitle, 209 bigTextStyle.mSummaryTextSet, 210 bigTextStyle.mSummaryText, 211 bigTextStyle.mBigText); 212 } else if (style instanceof InboxStyle) { 213 InboxStyle inboxStyle = (InboxStyle) style; 214 NotificationCompatJellybean.addInboxStyle(builder, 215 inboxStyle.mBigContentTitle, 216 inboxStyle.mSummaryTextSet, 217 inboxStyle.mSummaryText, 218 inboxStyle.mTexts); 219 } else if (style instanceof BigPictureStyle) { 220 BigPictureStyle bigPictureStyle = (BigPictureStyle) style; 221 NotificationCompatJellybean.addBigPictureStyle(builder, 222 bigPictureStyle.mBigContentTitle, 223 bigPictureStyle.mSummaryTextSet, 224 bigPictureStyle.mSummaryText, 225 bigPictureStyle.mPicture, 226 bigPictureStyle.mBigLargeIcon, 227 bigPictureStyle.mBigLargeIconSet); 228 } 229 } 230 } 231 232 static { 233 // TODO: Add NotificationCompatCurrent when SDK_INT is next incremented. 234 if (Build.VERSION.SDK_INT >= 16) { 235 IMPL = new NotificationCompatImplJellybean(); 236 } else if (Build.VERSION.SDK_INT >= 14) { 237 IMPL = new NotificationCompatImplIceCreamSandwich(); 238 } else if (Build.VERSION.SDK_INT >= 11) { 239 IMPL = new NotificationCompatImplHoneycomb(); 240 } else if (Build.VERSION.SDK_INT >= 9) { 241 IMPL = new NotificationCompatImplGingerbread(); 242 } else { 243 IMPL = new NotificationCompatImplBase(); 244 } 245 } 246 247 /** 248 * Builder class for {@link NotificationCompat} objects. Allows easier control over 249 * all the flags, as well as help constructing the typical notification layouts. 250 * <p> 251 * On platform versions that don't offer expanded notifications, methods that depend on 252 * expanded notifications have no effect. 253 * </p> 254 * <p> 255 * For example, action buttons won't appear on platforms prior to Android 4.1. Action 256 * buttons depend on expanded notifications, which are only available in Android 4.1 257 * and later. 258 * <p> 259 * For this reason, you should always ensure that UI controls in a notification are also 260 * available in an {@link android.app.Activity} in your app, and you should always start that 261 * {@link android.app.Activity} when users click the notification. To do this, use the 262 * {@link NotificationCompat.Builder#setContentIntent setContentIntent()} 263 * method. 264 * </p> 265 * 266 */ 267 public static class Builder { 268 Context mContext; 269 270 CharSequence mContentTitle; 271 CharSequence mContentText; 272 PendingIntent mContentIntent; 273 PendingIntent mFullScreenIntent; 274 RemoteViews mTickerView; 275 Bitmap mLargeIcon; 276 CharSequence mContentInfo; 277 int mNumber; 278 int mPriority; 279 boolean mUseChronometer; 280 Style mStyle; 281 CharSequence mSubText; 282 int mProgressMax; 283 int mProgress; 284 boolean mProgressIndeterminate; 285 ArrayList<Action> mActions = new ArrayList<Action>(); 286 boolean mLocalOnly = false; 287 288 Notification mNotification = new Notification(); 289 290 /** 291 * Constructor. 292 * 293 * Automatically sets the when field to {@link System#currentTimeMillis() 294 * System.currentTimeMillis()} and the audio stream to the 295 * {@link Notification#STREAM_DEFAULT}. 296 * 297 * @param context A {@link Context} that will be used to construct the 298 * RemoteViews. The Context will not be held past the lifetime of this 299 * Builder object. 300 */ 301 public Builder(Context context) { 302 mContext = context; 303 304 // Set defaults to match the defaults of a Notification 305 mNotification.when = System.currentTimeMillis(); 306 mNotification.audioStreamType = Notification.STREAM_DEFAULT; 307 mPriority = PRIORITY_DEFAULT; 308 } 309 310 /** 311 * Set the time that the event occurred. Notifications in the panel are 312 * sorted by this time. 313 */ 314 public Builder setWhen(long when) { 315 mNotification.when = when; 316 return this; 317 } 318 319 /** 320 * Show the {@link Notification#when} field as a stopwatch. 321 * 322 * Instead of presenting <code>when</code> as a timestamp, the notification will show an 323 * automatically updating display of the minutes and seconds since <code>when</code>. 324 * 325 * Useful when showing an elapsed time (like an ongoing phone call). 326 * 327 * @see android.widget.Chronometer 328 * @see Notification#when 329 */ 330 public Builder setUsesChronometer(boolean b) { 331 mUseChronometer = b; 332 return this; 333 } 334 335 /** 336 * Set the small icon to use in the notification layouts. Different classes of devices 337 * may return different sizes. See the UX guidelines for more information on how to 338 * design these icons. 339 * 340 * @param icon A resource ID in the application's package of the drawble to use. 341 */ 342 public Builder setSmallIcon(int icon) { 343 mNotification.icon = icon; 344 return this; 345 } 346 347 /** 348 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional 349 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable 350 * LevelListDrawable}. 351 * 352 * @param icon A resource ID in the application's package of the drawble to use. 353 * @param level The level to use for the icon. 354 * 355 * @see android.graphics.drawable.LevelListDrawable 356 */ 357 public Builder setSmallIcon(int icon, int level) { 358 mNotification.icon = icon; 359 mNotification.iconLevel = level; 360 return this; 361 } 362 363 /** 364 * Set the title (first row) of the notification, in a standard notification. 365 */ 366 public Builder setContentTitle(CharSequence title) { 367 mContentTitle = title; 368 return this; 369 } 370 371 /** 372 * Set the text (second row) of the notification, in a standard notification. 373 */ 374 public Builder setContentText(CharSequence text) { 375 mContentText = text; 376 return this; 377 } 378 379 /** 380 * Set the third line of text in the platform notification template. 381 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; 382 * they occupy the same location in the standard template. 383 * <br> 384 * If the platform does not provide large-format notifications, this method has no effect. 385 * The third line of text only appears in expanded view. 386 * <br> 387 */ 388 public Builder setSubText(CharSequence text) { 389 mSubText = text; 390 return this; 391 } 392 393 /** 394 * Set the large number at the right-hand side of the notification. This is 395 * equivalent to setContentInfo, although it might show the number in a different 396 * font size for readability. 397 */ 398 public Builder setNumber(int number) { 399 mNumber = number; 400 return this; 401 } 402 403 /** 404 * Set the large text at the right-hand side of the notification. 405 */ 406 public Builder setContentInfo(CharSequence info) { 407 mContentInfo = info; 408 return this; 409 } 410 411 /** 412 * Set the progress this notification represents, which may be 413 * represented as a {@link android.widget.ProgressBar}. 414 */ 415 public Builder setProgress(int max, int progress, boolean indeterminate) { 416 mProgressMax = max; 417 mProgress = progress; 418 mProgressIndeterminate = indeterminate; 419 return this; 420 } 421 422 /** 423 * Supply a custom RemoteViews to use instead of the standard one. 424 */ 425 public Builder setContent(RemoteViews views) { 426 mNotification.contentView = views; 427 return this; 428 } 429 430 /** 431 * Supply a {@link PendingIntent} to send when the notification is clicked. 432 * If you do not supply an intent, you can now add PendingIntents to individual 433 * views to be launched when clicked by calling {@link RemoteViews#setOnClickPendingIntent 434 * RemoteViews.setOnClickPendingIntent(int,PendingIntent)}. Be sure to 435 * read {@link Notification#contentIntent Notification.contentIntent} for 436 * how to correctly use this. 437 */ 438 public Builder setContentIntent(PendingIntent intent) { 439 mContentIntent = intent; 440 return this; 441 } 442 443 /** 444 * Supply a {@link PendingIntent} to send when the notification is cleared by the user 445 * directly from the notification panel. For example, this intent is sent when the user 446 * clicks the "Clear all" button, or the individual "X" buttons on notifications. This 447 * intent is not sent when the application calls {@link NotificationManager#cancel 448 * NotificationManager.cancel(int)}. 449 */ 450 public Builder setDeleteIntent(PendingIntent intent) { 451 mNotification.deleteIntent = intent; 452 return this; 453 } 454 455 /** 456 * An intent to launch instead of posting the notification to the status bar. 457 * Only for use with extremely high-priority notifications demanding the user's 458 * <strong>immediate</strong> attention, such as an incoming phone call or 459 * alarm clock that the user has explicitly set to a particular time. 460 * If this facility is used for something else, please give the user an option 461 * to turn it off and use a normal notification, as this can be extremely 462 * disruptive. 463 * 464 * @param intent The pending intent to launch. 465 * @param highPriority Passing true will cause this notification to be sent 466 * even if other notifications are suppressed. 467 */ 468 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { 469 mFullScreenIntent = intent; 470 setFlag(FLAG_HIGH_PRIORITY, highPriority); 471 return this; 472 } 473 474 /** 475 * Set the text that is displayed in the status bar when the notification first 476 * arrives. 477 */ 478 public Builder setTicker(CharSequence tickerText) { 479 mNotification.tickerText = tickerText; 480 return this; 481 } 482 483 /** 484 * Set the text that is displayed in the status bar when the notification first 485 * arrives, and also a RemoteViews object that may be displayed instead on some 486 * devices. 487 */ 488 public Builder setTicker(CharSequence tickerText, RemoteViews views) { 489 mNotification.tickerText = tickerText; 490 mTickerView = views; 491 return this; 492 } 493 494 /** 495 * Set the large icon that is shown in the ticker and notification. 496 */ 497 public Builder setLargeIcon(Bitmap icon) { 498 mLargeIcon = icon; 499 return this; 500 } 501 502 /** 503 * Set the sound to play. It will play on the default stream. 504 */ 505 public Builder setSound(Uri sound) { 506 mNotification.sound = sound; 507 mNotification.audioStreamType = Notification.STREAM_DEFAULT; 508 return this; 509 } 510 511 /** 512 * Set the sound to play. It will play on the stream you supply. 513 * 514 * @see Notification#STREAM_DEFAULT 515 * @see AudioManager for the <code>STREAM_</code> constants. 516 */ 517 public Builder setSound(Uri sound, int streamType) { 518 mNotification.sound = sound; 519 mNotification.audioStreamType = streamType; 520 return this; 521 } 522 523 /** 524 * Set the vibration pattern to use. 525 * 526 * @see android.os.Vibrator for a discussion of the <code>pattern</code> 527 * parameter. 528 */ 529 public Builder setVibrate(long[] pattern) { 530 mNotification.vibrate = pattern; 531 return this; 532 } 533 534 /** 535 * Set the argb value that you would like the LED on the device to blnk, as well as the 536 * rate. The rate is specified in terms of the number of milliseconds to be on 537 * and then the number of milliseconds to be off. 538 */ 539 public Builder setLights(int argb, int onMs, int offMs) { 540 mNotification.ledARGB = argb; 541 mNotification.ledOnMS = onMs; 542 mNotification.ledOffMS = offMs; 543 boolean showLights = mNotification.ledOnMS != 0 && mNotification.ledOffMS != 0; 544 mNotification.flags = (mNotification.flags & ~Notification.FLAG_SHOW_LIGHTS) | 545 (showLights ? Notification.FLAG_SHOW_LIGHTS : 0); 546 return this; 547 } 548 549 /** 550 * Set whether this is an ongoing notification. 551 * 552 * <p>Ongoing notifications differ from regular notifications in the following ways: 553 * <ul> 554 * <li>Ongoing notifications are sorted above the regular notifications in the 555 * notification panel.</li> 556 * <li>Ongoing notifications do not have an 'X' close button, and are not affected 557 * by the "Clear all" button. 558 * </ul> 559 */ 560 public Builder setOngoing(boolean ongoing) { 561 setFlag(Notification.FLAG_ONGOING_EVENT, ongoing); 562 return this; 563 } 564 565 /** 566 * Set this flag if you would only like the sound, vibrate 567 * and ticker to be played if the notification is not already showing. 568 */ 569 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { 570 setFlag(Notification.FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); 571 return this; 572 } 573 574 /** 575 * Setting this flag will make it so the notification is automatically 576 * canceled when the user clicks it in the panel. The PendingIntent 577 * set with {@link #setDeleteIntent} will be broadcast when the notification 578 * is canceled. 579 */ 580 public Builder setAutoCancel(boolean autoCancel) { 581 setFlag(Notification.FLAG_AUTO_CANCEL, autoCancel); 582 return this; 583 } 584 585 /** 586 * Set whether or not this notification is only relevant to the current device. 587 * 588 * <p>Some notifications can be bridged to other devices for remote display. 589 * This hint can be set to recommend this notification not be bridged. 590 */ 591 public Builder setLocalOnly(boolean b) { 592 mLocalOnly = b; 593 return this; 594 } 595 596 /** 597 * Set the default notification options that will be used. 598 * <p> 599 * The value should be one or more of the following fields combined with 600 * bitwise-or: 601 * {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE}, 602 * {@link Notification#DEFAULT_LIGHTS}. 603 * <p> 604 * For all default values, use {@link Notification#DEFAULT_ALL}. 605 */ 606 public Builder setDefaults(int defaults) { 607 mNotification.defaults = defaults; 608 if ((defaults & Notification.DEFAULT_LIGHTS) != 0) { 609 mNotification.flags |= Notification.FLAG_SHOW_LIGHTS; 610 } 611 return this; 612 } 613 614 private void setFlag(int mask, boolean value) { 615 if (value) { 616 mNotification.flags |= mask; 617 } else { 618 mNotification.flags &= ~mask; 619 } 620 } 621 622 /** 623 * Set the relative priority for this notification. 624 * 625 * Priority is an indication of how much of the user's 626 * valuable attention should be consumed by this 627 * notification. Low-priority notifications may be hidden from 628 * the user in certain situations, while the user might be 629 * interrupted for a higher-priority notification. 630 * The system sets a notification's priority based on various factors including the 631 * setPriority value. The effect may differ slightly on different platforms. 632 */ 633 public Builder setPriority(int pri) { 634 mPriority = pri; 635 return this; 636 } 637 638 /** 639 * Add an action to this notification. Actions are typically displayed by 640 * the system as a button adjacent to the notification content. 641 * <br> 642 * Action buttons won't appear on platforms prior to Android 4.1. Action 643 * buttons depend on expanded notifications, which are only available in Android 4.1 644 * and later. To ensure that an action button's functionality is always available, first 645 * implement the functionality in the {@link android.app.Activity} that starts when a user 646 * clicks the notification (see {@link #setContentIntent setContentIntent()}), and then 647 * enhance the notification by implementing the same functionality with 648 * {@link #addAction addAction()}. 649 * 650 * @param icon Resource ID of a drawable that represents the action. 651 * @param title Text describing the action. 652 * @param intent {@link android.app.PendingIntent} to be fired when the action is invoked. 653 */ 654 public Builder addAction(int icon, CharSequence title, PendingIntent intent) { 655 mActions.add(new Action(icon, title, intent)); 656 return this; 657 } 658 659 /** 660 * Add a rich notification style to be applied at build time. 661 * <br> 662 * If the platform does not provide rich notification styles, this method has no effect. The 663 * user will always see the normal notification style. 664 * 665 * @param style Object responsible for modifying the notification style. 666 */ 667 public Builder setStyle(Style style) { 668 if (mStyle != style) { 669 mStyle = style; 670 if (mStyle != null) { 671 mStyle.setBuilder(this); 672 } 673 } 674 return this; 675 } 676 677 /** 678 * @deprecated Use {@link #build()} instead. 679 */ 680 @Deprecated 681 public Notification getNotification() { 682 return IMPL.build(this); 683 } 684 685 /** 686 * Combine all of the options that have been set and return a new {@link Notification} 687 * object. 688 */ 689 public Notification build() { 690 return IMPL.build(this); 691 } 692 } 693 694 /** 695 * An object that can apply a rich notification style to a {@link Notification.Builder} 696 * object. 697 * <br> 698 * If the platform does not provide rich notification styles, methods in this class have no 699 * effect. 700 */ 701 public static abstract class Style { 702 Builder mBuilder; 703 CharSequence mBigContentTitle; 704 CharSequence mSummaryText; 705 boolean mSummaryTextSet = false; 706 707 public void setBuilder(Builder builder) { 708 if (mBuilder != builder) { 709 mBuilder = builder; 710 if (mBuilder != null) { 711 mBuilder.setStyle(this); 712 } 713 } 714 } 715 716 public Notification build() { 717 Notification notification = null; 718 if (mBuilder != null) { 719 notification = mBuilder.build(); 720 } 721 return notification; 722 } 723 } 724 725 /** 726 * Helper class for generating large-format notifications that include a large image attachment. 727 * <br> 728 * If the platform does not provide large-format notifications, this method has no effect. The 729 * user will always see the normal notification view. 730 * <br> 731 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so: 732 * <pre class="prettyprint"> 733 * Notification noti = new Notification.Builder() 734 * .setContentTitle("New photo from " + sender.toString()) 735 * .setContentText(subject) 736 * .setSmallIcon(R.drawable.new_post) 737 * .setLargeIcon(aBitmap) 738 * .setStyle(new Notification.BigPictureStyle() 739 * .bigPicture(aBigBitmap)) 740 * .build(); 741 * </pre> 742 * 743 * @see Notification#bigContentView 744 */ 745 public static class BigPictureStyle extends Style { 746 Bitmap mPicture; 747 Bitmap mBigLargeIcon; 748 boolean mBigLargeIconSet; 749 750 public BigPictureStyle() { 751 } 752 753 public BigPictureStyle(Builder builder) { 754 setBuilder(builder); 755 } 756 757 /** 758 * Overrides ContentTitle in the big form of the template. 759 * This defaults to the value passed to setContentTitle(). 760 */ 761 public BigPictureStyle setBigContentTitle(CharSequence title) { 762 mBigContentTitle = title; 763 return this; 764 } 765 766 /** 767 * Set the first line of text after the detail section in the big form of the template. 768 */ 769 public BigPictureStyle setSummaryText(CharSequence cs) { 770 mSummaryText = cs; 771 mSummaryTextSet = true; 772 return this; 773 } 774 775 /** 776 * Provide the bitmap to be used as the payload for the BigPicture notification. 777 */ 778 public BigPictureStyle bigPicture(Bitmap b) { 779 mPicture = b; 780 return this; 781 } 782 783 /** 784 * Override the large icon when the big notification is shown. 785 */ 786 public BigPictureStyle bigLargeIcon(Bitmap b) { 787 mBigLargeIcon = b; 788 mBigLargeIconSet = true; 789 return this; 790 } 791 } 792 793 /** 794 * Helper class for generating large-format notifications that include a lot of text. 795 * 796 * <br> 797 * If the platform does not provide large-format notifications, this method has no effect. The 798 * user will always see the normal notification view. 799 * <br> 800 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so: 801 * <pre class="prettyprint"> 802 * Notification noti = new Notification.Builder() 803 * .setContentTitle("New mail from " + sender.toString()) 804 * .setContentText(subject) 805 * .setSmallIcon(R.drawable.new_mail) 806 * .setLargeIcon(aBitmap) 807 * .setStyle(new Notification.BigTextStyle() 808 * .bigText(aVeryLongString)) 809 * .build(); 810 * </pre> 811 * 812 * @see Notification#bigContentView 813 */ 814 public static class BigTextStyle extends Style { 815 CharSequence mBigText; 816 817 public BigTextStyle() { 818 } 819 820 public BigTextStyle(Builder builder) { 821 setBuilder(builder); 822 } 823 824 /** 825 * Overrides ContentTitle in the big form of the template. 826 * This defaults to the value passed to setContentTitle(). 827 */ 828 public BigTextStyle setBigContentTitle(CharSequence title) { 829 mBigContentTitle = title; 830 return this; 831 } 832 833 /** 834 * Set the first line of text after the detail section in the big form of the template. 835 */ 836 public BigTextStyle setSummaryText(CharSequence cs) { 837 mSummaryText = cs; 838 mSummaryTextSet = true; 839 return this; 840 } 841 842 /** 843 * Provide the longer text to be displayed in the big form of the 844 * template in place of the content text. 845 */ 846 public BigTextStyle bigText(CharSequence cs) { 847 mBigText = cs; 848 return this; 849 } 850 } 851 852 /** 853 * Helper class for generating large-format notifications that include a list of (up to 5) strings. 854 * 855 * <br> 856 * If the platform does not provide large-format notifications, this method has no effect. The 857 * user will always see the normal notification view. 858 * <br> 859 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so: 860 * <pre class="prettyprint"> 861 * Notification noti = new Notification.Builder() 862 * .setContentTitle("5 New mails from " + sender.toString()) 863 * .setContentText(subject) 864 * .setSmallIcon(R.drawable.new_mail) 865 * .setLargeIcon(aBitmap) 866 * .setStyle(new Notification.InboxStyle() 867 * .addLine(str1) 868 * .addLine(str2) 869 * .setContentTitle("") 870 * .setSummaryText("+3 more")) 871 * .build(); 872 * </pre> 873 * 874 * @see Notification#bigContentView 875 */ 876 public static class InboxStyle extends Style { 877 ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(); 878 879 public InboxStyle() { 880 } 881 882 public InboxStyle(Builder builder) { 883 setBuilder(builder); 884 } 885 886 /** 887 * Overrides ContentTitle in the big form of the template. 888 * This defaults to the value passed to setContentTitle(). 889 */ 890 public InboxStyle setBigContentTitle(CharSequence title) { 891 mBigContentTitle = title; 892 return this; 893 } 894 895 /** 896 * Set the first line of text after the detail section in the big form of the template. 897 */ 898 public InboxStyle setSummaryText(CharSequence cs) { 899 mSummaryText = cs; 900 mSummaryTextSet = true; 901 return this; 902 } 903 904 /** 905 * Append a line to the digest section of the Inbox notification. 906 */ 907 public InboxStyle addLine(CharSequence cs) { 908 mTexts.add(cs); 909 return this; 910 } 911 } 912 913 public static class Action { 914 public int icon; 915 public CharSequence title; 916 public PendingIntent actionIntent; 917 918 public Action(int icon_, CharSequence title_, PendingIntent intent_) { 919 this.icon = icon_; 920 this.title = title_; 921 this.actionIntent = intent_; 922 } 923 } 924 925 /** 926 * Gets the {@link Notification#extras} field from a notification in a backwards 927 * compatible manner. Extras field was supported from JellyBean (Api level 16) 928 * forwards. This function will return null on older api levels. 929 */ 930 public static Bundle getExtras(Notification notif) { 931 return IMPL.getExtras(notif); 932 } 933 934 /** 935 * Get whether or not this notification is only relevant to the current device. 936 * 937 * <p>Some notifications can be bridged to other devices for remote display. 938 * If this hint is set, it is recommend that this notification not be bridged. 939 */ 940 public static boolean getLocalOnly(Notification notif) { 941 return IMPL.getLocalOnly(notif); 942 } 943} 944