Notification.java revision 6adfd86ca94f3eabb93fb1b4a0b9b0ea50895dcd
1/* 2 * Copyright (C) 2007 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.app; 18 19import com.android.internal.R; 20 21import android.annotation.IntDef; 22import android.content.Context; 23import android.content.Intent; 24import android.content.res.Resources; 25import android.graphics.Bitmap; 26import android.media.AudioManager; 27import android.net.Uri; 28import android.os.BadParcelableException; 29import android.os.Bundle; 30import android.os.Parcel; 31import android.os.Parcelable; 32import android.os.SystemClock; 33import android.os.UserHandle; 34import android.text.TextUtils; 35import android.util.Log; 36import android.util.TypedValue; 37import android.view.View; 38import android.widget.ProgressBar; 39import android.widget.RemoteViews; 40 41import java.lang.annotation.Retention; 42import java.lang.annotation.RetentionPolicy; 43import java.text.NumberFormat; 44import java.util.ArrayList; 45 46/** 47 * A class that represents how a persistent notification is to be presented to 48 * the user using the {@link android.app.NotificationManager}. 49 * 50 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it 51 * easier to construct Notifications.</p> 52 * 53 * <div class="special reference"> 54 * <h3>Developer Guides</h3> 55 * <p>For a guide to creating notifications, read the 56 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> 57 * developer guide.</p> 58 * </div> 59 */ 60public class Notification implements Parcelable 61{ 62 private static final String TAG = "Notification"; 63 64 /** 65 * Use all default values (where applicable). 66 */ 67 public static final int DEFAULT_ALL = ~0; 68 69 /** 70 * Use the default notification sound. This will ignore any given 71 * {@link #sound}. 72 * 73 74 * @see #defaults 75 */ 76 77 public static final int DEFAULT_SOUND = 1; 78 79 /** 80 * Use the default notification vibrate. This will ignore any given 81 * {@link #vibrate}. Using phone vibration requires the 82 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission. 83 * 84 * @see #defaults 85 */ 86 87 public static final int DEFAULT_VIBRATE = 2; 88 89 /** 90 * Use the default notification lights. This will ignore the 91 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or 92 * {@link #ledOnMS}. 93 * 94 * @see #defaults 95 */ 96 97 public static final int DEFAULT_LIGHTS = 4; 98 99 /** 100 * A timestamp related to this notification, in milliseconds since the epoch. 101 * 102 * Default value: {@link System#currentTimeMillis() Now}. 103 * 104 * Choose a timestamp that will be most relevant to the user. For most finite events, this 105 * corresponds to the time the event happened (or will happen, in the case of events that have 106 * yet to occur but about which the user is being informed). Indefinite events should be 107 * timestamped according to when the activity began. 108 * 109 * Some examples: 110 * 111 * <ul> 112 * <li>Notification of a new chat message should be stamped when the message was received.</li> 113 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li> 114 * <li>Notification of a completed file download should be stamped when the download finished.</li> 115 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li> 116 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time. 117 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time. 118 * </ul> 119 * 120 */ 121 public long when; 122 123 /** 124 * The resource id of a drawable to use as the icon in the status bar. 125 * This is required; notifications with an invalid icon resource will not be shown. 126 */ 127 public int icon; 128 129 /** 130 * If the icon in the status bar is to have more than one level, you can set this. Otherwise, 131 * leave it at its default value of 0. 132 * 133 * @see android.widget.ImageView#setImageLevel 134 * @see android.graphics.drawable#setLevel 135 */ 136 public int iconLevel; 137 138 /** 139 * The number of events that this notification represents. For example, in a new mail 140 * notification, this could be the number of unread messages. 141 * 142 * The system may or may not use this field to modify the appearance of the notification. For 143 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was 144 * superimposed over the icon in the status bar. Starting with 145 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by 146 * {@link Notification.Builder} has displayed the number in the expanded notification view. 147 * 148 * If the number is 0 or negative, it is never shown. 149 */ 150 public int number; 151 152 /** 153 * The intent to execute when the expanded status entry is clicked. If 154 * this is an activity, it must include the 155 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires 156 * that you take care of task management as described in the 157 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back 158 * Stack</a> document. In particular, make sure to read the notification section 159 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling 160 * Notifications</a> for the correct ways to launch an application from a 161 * notification. 162 */ 163 public PendingIntent contentIntent; 164 165 /** 166 * The intent to execute when the notification is explicitly dismissed by the user, either with 167 * the "Clear All" button or by swiping it away individually. 168 * 169 * This probably shouldn't be launching an activity since several of those will be sent 170 * at the same time. 171 */ 172 public PendingIntent deleteIntent; 173 174 /** 175 * An intent to launch instead of posting the notification to the status bar. 176 * 177 * @see Notification.Builder#setFullScreenIntent 178 */ 179 public PendingIntent fullScreenIntent; 180 181 /** 182 * Text to scroll across the screen when this item is added to 183 * the status bar on large and smaller devices. 184 * 185 * @see #tickerView 186 */ 187 public CharSequence tickerText; 188 189 /** 190 * The view to show as the ticker in the status bar when the notification 191 * is posted. 192 */ 193 public RemoteViews tickerView; 194 195 /** 196 * The view that will represent this notification in the expanded status bar. 197 */ 198 public RemoteViews contentView; 199 200 /** 201 * A large-format version of {@link #contentView}, giving the Notification an 202 * opportunity to show more detail. The system UI may choose to show this 203 * instead of the normal content view at its discretion. 204 */ 205 public RemoteViews bigContentView; 206 207 /** 208 * The bitmap that may escape the bounds of the panel and bar. 209 */ 210 public Bitmap largeIcon; 211 212 /** 213 * The sound to play. 214 * 215 * <p> 216 * To play the default notification sound, see {@link #defaults}. 217 * </p> 218 */ 219 public Uri sound; 220 221 /** 222 * Use this constant as the value for audioStreamType to request that 223 * the default stream type for notifications be used. Currently the 224 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}. 225 */ 226 public static final int STREAM_DEFAULT = -1; 227 228 /** 229 * The audio stream type to use when playing the sound. 230 * Should be one of the STREAM_ constants from 231 * {@link android.media.AudioManager}. 232 */ 233 public int audioStreamType = STREAM_DEFAULT; 234 235 /** 236 * The pattern with which to vibrate. 237 * 238 * <p> 239 * To vibrate the default pattern, see {@link #defaults}. 240 * </p> 241 * 242 * @see android.os.Vibrator#vibrate(long[],int) 243 */ 244 public long[] vibrate; 245 246 /** 247 * The color of the led. The hardware will do its best approximation. 248 * 249 * @see #FLAG_SHOW_LIGHTS 250 * @see #flags 251 */ 252 public int ledARGB; 253 254 /** 255 * The number of milliseconds for the LED to be on while it's flashing. 256 * The hardware will do its best approximation. 257 * 258 * @see #FLAG_SHOW_LIGHTS 259 * @see #flags 260 */ 261 public int ledOnMS; 262 263 /** 264 * The number of milliseconds for the LED to be off while it's flashing. 265 * The hardware will do its best approximation. 266 * 267 * @see #FLAG_SHOW_LIGHTS 268 * @see #flags 269 */ 270 public int ledOffMS; 271 272 /** 273 * Specifies which values should be taken from the defaults. 274 * <p> 275 * To set, OR the desired from {@link #DEFAULT_SOUND}, 276 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default 277 * values, use {@link #DEFAULT_ALL}. 278 * </p> 279 */ 280 public int defaults; 281 282 /** 283 * Bit to be bitwise-ored into the {@link #flags} field that should be 284 * set if you want the LED on for this notification. 285 * <ul> 286 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB 287 * or 0 for both ledOnMS and ledOffMS.</li> 288 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li> 289 * <li>To flash the LED, pass the number of milliseconds that it should 290 * be on and off to ledOnMS and ledOffMS.</li> 291 * </ul> 292 * <p> 293 * Since hardware varies, you are not guaranteed that any of the values 294 * you pass are honored exactly. Use the system defaults (TODO) if possible 295 * because they will be set to values that work on any given hardware. 296 * <p> 297 * The alpha channel must be set for forward compatibility. 298 * 299 */ 300 public static final int FLAG_SHOW_LIGHTS = 0x00000001; 301 302 /** 303 * Bit to be bitwise-ored into the {@link #flags} field that should be 304 * set if this notification is in reference to something that is ongoing, 305 * like a phone call. It should not be set if this notification is in 306 * reference to something that happened at a particular point in time, 307 * like a missed phone call. 308 */ 309 public static final int FLAG_ONGOING_EVENT = 0x00000002; 310 311 /** 312 * Bit to be bitwise-ored into the {@link #flags} field that if set, 313 * the audio will be repeated until the notification is 314 * cancelled or the notification window is opened. 315 */ 316 public static final int FLAG_INSISTENT = 0x00000004; 317 318 /** 319 * Bit to be bitwise-ored into the {@link #flags} field that should be 320 * set if you want the sound and/or vibration play each time the 321 * notification is sent, even if it has not been canceled before that. 322 */ 323 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008; 324 325 /** 326 * Bit to be bitwise-ored into the {@link #flags} field that should be 327 * set if the notification should be canceled when it is clicked by the 328 * user. 329 330 */ 331 public static final int FLAG_AUTO_CANCEL = 0x00000010; 332 333 /** 334 * Bit to be bitwise-ored into the {@link #flags} field that should be 335 * set if the notification should not be canceled when the user clicks 336 * the Clear all button. 337 */ 338 public static final int FLAG_NO_CLEAR = 0x00000020; 339 340 /** 341 * Bit to be bitwise-ored into the {@link #flags} field that should be 342 * set if this notification represents a currently running service. This 343 * will normally be set for you by {@link Service#startForeground}. 344 */ 345 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040; 346 347 /** 348 * Obsolete flag indicating high-priority notifications; use the priority field instead. 349 * 350 * @deprecated Use {@link #priority} with a positive value. 351 */ 352 public static final int FLAG_HIGH_PRIORITY = 0x00000080; 353 354 /** 355 * Bit to be bitswise-ored into the {@link #flags} field that should be 356 * set if this notification is relevant to the current device only 357 * and it is not recommended that it bridge to other devices. 358 */ 359 public static final int FLAG_LOCAL_ONLY = 0x00000100; 360 361 public int flags; 362 363 /** @hide */ 364 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX}) 365 @Retention(RetentionPolicy.SOURCE) 366 public @interface Priority {} 367 368 /** 369 * Default notification {@link #priority}. If your application does not prioritize its own 370 * notifications, use this value for all notifications. 371 */ 372 public static final int PRIORITY_DEFAULT = 0; 373 374 /** 375 * Lower {@link #priority}, for items that are less important. The UI may choose to show these 376 * items smaller, or at a different position in the list, compared with your app's 377 * {@link #PRIORITY_DEFAULT} items. 378 */ 379 public static final int PRIORITY_LOW = -1; 380 381 /** 382 * Lowest {@link #priority}; these items might not be shown to the user except under special 383 * circumstances, such as detailed notification logs. 384 */ 385 public static final int PRIORITY_MIN = -2; 386 387 /** 388 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to 389 * show these items larger, or at a different position in notification lists, compared with 390 * your app's {@link #PRIORITY_DEFAULT} items. 391 */ 392 public static final int PRIORITY_HIGH = 1; 393 394 /** 395 * Highest {@link #priority}, for your application's most important items that require the 396 * user's prompt attention or input. 397 */ 398 public static final int PRIORITY_MAX = 2; 399 400 /** 401 * Relative priority for this notification. 402 * 403 * Priority is an indication of how much of the user's valuable attention should be consumed by 404 * this notification. Low-priority notifications may be hidden from the user in certain 405 * situations, while the user might be interrupted for a higher-priority notification. The 406 * system will make a determination about how to interpret this priority when presenting 407 * the notification. 408 */ 409 @Priority 410 public int priority; 411 412 413 /** 414 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals 415 * the notification's presence and contents in untrusted situations (namely, on the secure 416 * lockscreen). 417 * 418 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always 419 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are 420 * shown in all situations, but the contents are only available if the device is unlocked for 421 * the appropriate user. 422 * 423 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification 424 * can be read even in an "insecure" context (that is, above a secure lockscreen). 425 * To modify the public version of this notification—for example, to redact some portions—see 426 * {@link Builder#setPublicVersion(Notification)}. 427 * 428 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon 429 * and ticker until the user has bypassed the lockscreen. 430 */ 431 public int visibility; 432 433 public static final int VISIBILITY_PUBLIC = 1; 434 public static final int VISIBILITY_PRIVATE = 0; 435 public static final int VISIBILITY_SECRET = -1; 436 437 /** 438 * @hide 439 * Notification type: incoming call (voice or video) or similar synchronous communication request. 440 */ 441 public static final String KIND_CALL = "android.call"; 442 443 /** 444 * @hide 445 * Notification type: incoming direct message (SMS, instant message, etc.). 446 */ 447 public static final String KIND_MESSAGE = "android.message"; 448 449 /** 450 * @hide 451 * Notification type: asynchronous bulk message (email). 452 */ 453 public static final String KIND_EMAIL = "android.email"; 454 455 /** 456 * @hide 457 * Notification type: calendar event. 458 */ 459 public static final String KIND_EVENT = "android.event"; 460 461 /** 462 * @hide 463 * Notification type: promotion or advertisement. 464 */ 465 public static final String KIND_PROMO = "android.promo"; 466 467 /** 468 * @hide 469 * If this notification matches of one or more special types (see the <code>KIND_*</code> 470 * constants), add them here, best match first. 471 */ 472 public String[] kind; 473 474 /** 475 * Additional semantic data to be carried around with this Notification. 476 * <p> 477 * The extras keys defined here are intended to capture the original inputs to {@link Builder} 478 * APIs, and are intended to be used by 479 * {@link android.service.notification.NotificationListenerService} implementations to extract 480 * detailed information from notification objects. 481 */ 482 public Bundle extras = new Bundle(); 483 484 /** 485 * {@link #extras} key: this is the title of the notification, 486 * as supplied to {@link Builder#setContentTitle(CharSequence)}. 487 */ 488 public static final String EXTRA_TITLE = "android.title"; 489 490 /** 491 * {@link #extras} key: this is the title of the notification when shown in expanded form, 492 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}. 493 */ 494 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big"; 495 496 /** 497 * {@link #extras} key: this is the main text payload, as supplied to 498 * {@link Builder#setContentText(CharSequence)}. 499 */ 500 public static final String EXTRA_TEXT = "android.text"; 501 502 /** 503 * {@link #extras} key: this is a third line of text, as supplied to 504 * {@link Builder#setSubText(CharSequence)}. 505 */ 506 public static final String EXTRA_SUB_TEXT = "android.subText"; 507 508 /** 509 * {@link #extras} key: this is a small piece of additional text as supplied to 510 * {@link Builder#setContentInfo(CharSequence)}. 511 */ 512 public static final String EXTRA_INFO_TEXT = "android.infoText"; 513 514 /** 515 * {@link #extras} key: this is a line of summary information intended to be shown 516 * alongside expanded notifications, as supplied to (e.g.) 517 * {@link BigTextStyle#setSummaryText(CharSequence)}. 518 */ 519 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText"; 520 521 /** 522 * {@link #extras} key: this is the resource ID of the notification's main small icon, as 523 * supplied to {@link Builder#setSmallIcon(int)}. 524 */ 525 public static final String EXTRA_SMALL_ICON = "android.icon"; 526 527 /** 528 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the 529 * notification payload, as 530 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}. 531 */ 532 public static final String EXTRA_LARGE_ICON = "android.largeIcon"; 533 534 /** 535 * {@link #extras} key: this is a bitmap to be used instead of the one from 536 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is 537 * shown in its expanded form, as supplied to 538 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}. 539 */ 540 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big"; 541 542 /** 543 * {@link #extras} key: this is the progress value supplied to 544 * {@link Builder#setProgress(int, int, boolean)}. 545 */ 546 public static final String EXTRA_PROGRESS = "android.progress"; 547 548 /** 549 * {@link #extras} key: this is the maximum value supplied to 550 * {@link Builder#setProgress(int, int, boolean)}. 551 */ 552 public static final String EXTRA_PROGRESS_MAX = "android.progressMax"; 553 554 /** 555 * {@link #extras} key: whether the progress bar is indeterminate, supplied to 556 * {@link Builder#setProgress(int, int, boolean)}. 557 */ 558 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate"; 559 560 /** 561 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically 562 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to 563 * {@link Builder#setUsesChronometer(boolean)}. 564 */ 565 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer"; 566 567 /** 568 * {@link #extras} key: whether {@link #when} should be shown, 569 * as supplied to {@link Builder#setShowWhen(boolean)}. 570 */ 571 public static final String EXTRA_SHOW_WHEN = "android.showWhen"; 572 573 /** 574 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded 575 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}. 576 */ 577 public static final String EXTRA_PICTURE = "android.picture"; 578 579 /** 580 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded 581 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}. 582 */ 583 public static final String EXTRA_TEXT_LINES = "android.textLines"; 584 public static final String EXTRA_TEMPLATE = "android.template"; 585 586 /** 587 * {@link #extras} key: An array of people that this notification relates to, specified 588 * by contacts provider contact URI. 589 */ 590 public static final String EXTRA_PEOPLE = "android.people"; 591 592 /** 593 * @hide 594 * Extra added by NotificationManagerService to indicate whether a NotificationScorer 595 * modified the Notifications's score. 596 */ 597 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified"; 598 599 /** 600 * Not used. 601 * @hide 602 */ 603 public static final String EXTRA_AS_HEADS_UP = "headsup"; 604 605 /** 606 * Value for {@link #EXTRA_AS_HEADS_UP}. 607 * @hide 608 */ 609 public static final int HEADS_UP_NEVER = 0; 610 611 /** 612 * Default value for {@link #EXTRA_AS_HEADS_UP}. 613 * @hide 614 */ 615 public static final int HEADS_UP_ALLOWED = 1; 616 617 /** 618 * Value for {@link #EXTRA_AS_HEADS_UP}. 619 * @hide 620 */ 621 public static final int HEADS_UP_REQUESTED = 2; 622 623 /** 624 * Structure to encapsulate a named action that can be shown as part of this notification. 625 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is 626 * selected by the user. 627 * <p> 628 * Apps should use {@link Builder#addAction(int, CharSequence, PendingIntent)} to create and 629 * attach actions. 630 */ 631 public static class Action implements Parcelable { 632 /** 633 * Small icon representing the action. 634 */ 635 public int icon; 636 /** 637 * Title of the action. 638 */ 639 public CharSequence title; 640 /** 641 * Intent to send when the user invokes this action. May be null, in which case the action 642 * may be rendered in a disabled presentation by the system UI. 643 */ 644 public PendingIntent actionIntent; 645 646 private Action() { } 647 private Action(Parcel in) { 648 icon = in.readInt(); 649 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 650 if (in.readInt() == 1) { 651 actionIntent = PendingIntent.CREATOR.createFromParcel(in); 652 } 653 } 654 /** 655 * Use {@link Builder#addAction(int, CharSequence, PendingIntent)}. 656 */ 657 public Action(int icon, CharSequence title, PendingIntent intent) { 658 this.icon = icon; 659 this.title = title; 660 this.actionIntent = intent; 661 } 662 663 @Override 664 public Action clone() { 665 return new Action( 666 this.icon, 667 this.title, 668 this.actionIntent // safe to alias 669 ); 670 } 671 @Override 672 public int describeContents() { 673 return 0; 674 } 675 @Override 676 public void writeToParcel(Parcel out, int flags) { 677 out.writeInt(icon); 678 TextUtils.writeToParcel(title, out, flags); 679 if (actionIntent != null) { 680 out.writeInt(1); 681 actionIntent.writeToParcel(out, flags); 682 } else { 683 out.writeInt(0); 684 } 685 } 686 public static final Parcelable.Creator<Action> CREATOR 687 = new Parcelable.Creator<Action>() { 688 public Action createFromParcel(Parcel in) { 689 return new Action(in); 690 } 691 public Action[] newArray(int size) { 692 return new Action[size]; 693 } 694 }; 695 } 696 697 /** 698 * Array of all {@link Action} structures attached to this notification by 699 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of 700 * {@link android.service.notification.NotificationListenerService} that provide an alternative 701 * interface for invoking actions. 702 */ 703 public Action[] actions; 704 705 /** 706 * Replacement version of this notification whose content will be shown 707 * in an insecure context such as atop a secure keyguard. See {@link #visibility} 708 * and {@link #VISIBILITY_PUBLIC}. 709 */ 710 public Notification publicVersion; 711 712 /** 713 * Constructs a Notification object with default values. 714 * You might want to consider using {@link Builder} instead. 715 */ 716 public Notification() 717 { 718 this.when = System.currentTimeMillis(); 719 this.priority = PRIORITY_DEFAULT; 720 } 721 722 /** 723 * @hide 724 */ 725 public Notification(Context context, int icon, CharSequence tickerText, long when, 726 CharSequence contentTitle, CharSequence contentText, Intent contentIntent) 727 { 728 this.when = when; 729 this.icon = icon; 730 this.tickerText = tickerText; 731 setLatestEventInfo(context, contentTitle, contentText, 732 PendingIntent.getActivity(context, 0, contentIntent, 0)); 733 } 734 735 /** 736 * Constructs a Notification object with the information needed to 737 * have a status bar icon without the standard expanded view. 738 * 739 * @param icon The resource id of the icon to put in the status bar. 740 * @param tickerText The text that flows by in the status bar when the notification first 741 * activates. 742 * @param when The time to show in the time field. In the System.currentTimeMillis 743 * timebase. 744 * 745 * @deprecated Use {@link Builder} instead. 746 */ 747 @Deprecated 748 public Notification(int icon, CharSequence tickerText, long when) 749 { 750 this.icon = icon; 751 this.tickerText = tickerText; 752 this.when = when; 753 } 754 755 /** 756 * Unflatten the notification from a parcel. 757 */ 758 public Notification(Parcel parcel) 759 { 760 int version = parcel.readInt(); 761 762 when = parcel.readLong(); 763 icon = parcel.readInt(); 764 number = parcel.readInt(); 765 if (parcel.readInt() != 0) { 766 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel); 767 } 768 if (parcel.readInt() != 0) { 769 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel); 770 } 771 if (parcel.readInt() != 0) { 772 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); 773 } 774 if (parcel.readInt() != 0) { 775 tickerView = RemoteViews.CREATOR.createFromParcel(parcel); 776 } 777 if (parcel.readInt() != 0) { 778 contentView = RemoteViews.CREATOR.createFromParcel(parcel); 779 } 780 if (parcel.readInt() != 0) { 781 largeIcon = Bitmap.CREATOR.createFromParcel(parcel); 782 } 783 defaults = parcel.readInt(); 784 flags = parcel.readInt(); 785 if (parcel.readInt() != 0) { 786 sound = Uri.CREATOR.createFromParcel(parcel); 787 } 788 789 audioStreamType = parcel.readInt(); 790 vibrate = parcel.createLongArray(); 791 ledARGB = parcel.readInt(); 792 ledOnMS = parcel.readInt(); 793 ledOffMS = parcel.readInt(); 794 iconLevel = parcel.readInt(); 795 796 if (parcel.readInt() != 0) { 797 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel); 798 } 799 800 priority = parcel.readInt(); 801 802 kind = parcel.createStringArray(); // may set kind to null 803 804 extras = parcel.readBundle(); // may be null 805 806 actions = parcel.createTypedArray(Action.CREATOR); // may be null 807 808 if (parcel.readInt() != 0) { 809 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel); 810 } 811 812 visibility = parcel.readInt(); 813 814 if (parcel.readInt() != 0) { 815 publicVersion = Notification.CREATOR.createFromParcel(parcel); 816 } 817 } 818 819 @Override 820 public Notification clone() { 821 Notification that = new Notification(); 822 cloneInto(that, true); 823 return that; 824 } 825 826 /** 827 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members 828 * of this into that. 829 * @hide 830 */ 831 public void cloneInto(Notification that, boolean heavy) { 832 that.when = this.when; 833 that.icon = this.icon; 834 that.number = this.number; 835 836 // PendingIntents are global, so there's no reason (or way) to clone them. 837 that.contentIntent = this.contentIntent; 838 that.deleteIntent = this.deleteIntent; 839 that.fullScreenIntent = this.fullScreenIntent; 840 841 if (this.tickerText != null) { 842 that.tickerText = this.tickerText.toString(); 843 } 844 if (heavy && this.tickerView != null) { 845 that.tickerView = this.tickerView.clone(); 846 } 847 if (heavy && this.contentView != null) { 848 that.contentView = this.contentView.clone(); 849 } 850 if (heavy && this.largeIcon != null) { 851 that.largeIcon = Bitmap.createBitmap(this.largeIcon); 852 } 853 that.iconLevel = this.iconLevel; 854 that.sound = this.sound; // android.net.Uri is immutable 855 that.audioStreamType = this.audioStreamType; 856 857 final long[] vibrate = this.vibrate; 858 if (vibrate != null) { 859 final int N = vibrate.length; 860 final long[] vib = that.vibrate = new long[N]; 861 System.arraycopy(vibrate, 0, vib, 0, N); 862 } 863 864 that.ledARGB = this.ledARGB; 865 that.ledOnMS = this.ledOnMS; 866 that.ledOffMS = this.ledOffMS; 867 that.defaults = this.defaults; 868 869 that.flags = this.flags; 870 871 that.priority = this.priority; 872 873 final String[] thiskind = this.kind; 874 if (thiskind != null) { 875 final int N = thiskind.length; 876 final String[] thatkind = that.kind = new String[N]; 877 System.arraycopy(thiskind, 0, thatkind, 0, N); 878 } 879 880 if (this.extras != null) { 881 try { 882 that.extras = new Bundle(this.extras); 883 // will unparcel 884 that.extras.size(); 885 } catch (BadParcelableException e) { 886 Log.e(TAG, "could not unparcel extras from notification: " + this, e); 887 that.extras = null; 888 } 889 } 890 891 if (this.actions != null) { 892 that.actions = new Action[this.actions.length]; 893 for(int i=0; i<this.actions.length; i++) { 894 that.actions[i] = this.actions[i].clone(); 895 } 896 } 897 898 if (heavy && this.bigContentView != null) { 899 that.bigContentView = this.bigContentView.clone(); 900 } 901 902 that.visibility = this.visibility; 903 904 if (this.publicVersion != null) { 905 that.publicVersion = new Notification(); 906 this.publicVersion.cloneInto(that.publicVersion, heavy); 907 } 908 909 if (!heavy) { 910 that.lightenPayload(); // will clean out extras 911 } 912 } 913 914 /** 915 * Removes heavyweight parts of the Notification object for archival or for sending to 916 * listeners when the full contents are not necessary. 917 * @hide 918 */ 919 public final void lightenPayload() { 920 tickerView = null; 921 contentView = null; 922 bigContentView = null; 923 largeIcon = null; 924 if (extras != null) { 925 extras.remove(Notification.EXTRA_LARGE_ICON); 926 extras.remove(Notification.EXTRA_LARGE_ICON_BIG); 927 extras.remove(Notification.EXTRA_PICTURE); 928 } 929 } 930 931 /** 932 * Make sure this CharSequence is safe to put into a bundle, which basically 933 * means it had better not be some custom Parcelable implementation. 934 * @hide 935 */ 936 public static CharSequence safeCharSequence(CharSequence cs) { 937 if (cs instanceof Parcelable) { 938 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName() 939 + " instance is a custom Parcelable and not allowed in Notification"); 940 return cs.toString(); 941 } 942 943 return cs; 944 } 945 946 public int describeContents() { 947 return 0; 948 } 949 950 /** 951 * Flatten this notification from a parcel. 952 */ 953 public void writeToParcel(Parcel parcel, int flags) 954 { 955 parcel.writeInt(1); 956 957 parcel.writeLong(when); 958 parcel.writeInt(icon); 959 parcel.writeInt(number); 960 if (contentIntent != null) { 961 parcel.writeInt(1); 962 contentIntent.writeToParcel(parcel, 0); 963 } else { 964 parcel.writeInt(0); 965 } 966 if (deleteIntent != null) { 967 parcel.writeInt(1); 968 deleteIntent.writeToParcel(parcel, 0); 969 } else { 970 parcel.writeInt(0); 971 } 972 if (tickerText != null) { 973 parcel.writeInt(1); 974 TextUtils.writeToParcel(tickerText, parcel, flags); 975 } else { 976 parcel.writeInt(0); 977 } 978 if (tickerView != null) { 979 parcel.writeInt(1); 980 tickerView.writeToParcel(parcel, 0); 981 } else { 982 parcel.writeInt(0); 983 } 984 if (contentView != null) { 985 parcel.writeInt(1); 986 contentView.writeToParcel(parcel, 0); 987 } else { 988 parcel.writeInt(0); 989 } 990 if (largeIcon != null) { 991 parcel.writeInt(1); 992 largeIcon.writeToParcel(parcel, 0); 993 } else { 994 parcel.writeInt(0); 995 } 996 997 parcel.writeInt(defaults); 998 parcel.writeInt(this.flags); 999 1000 if (sound != null) { 1001 parcel.writeInt(1); 1002 sound.writeToParcel(parcel, 0); 1003 } else { 1004 parcel.writeInt(0); 1005 } 1006 parcel.writeInt(audioStreamType); 1007 parcel.writeLongArray(vibrate); 1008 parcel.writeInt(ledARGB); 1009 parcel.writeInt(ledOnMS); 1010 parcel.writeInt(ledOffMS); 1011 parcel.writeInt(iconLevel); 1012 1013 if (fullScreenIntent != null) { 1014 parcel.writeInt(1); 1015 fullScreenIntent.writeToParcel(parcel, 0); 1016 } else { 1017 parcel.writeInt(0); 1018 } 1019 1020 parcel.writeInt(priority); 1021 1022 parcel.writeStringArray(kind); // ok for null 1023 1024 parcel.writeBundle(extras); // null ok 1025 1026 parcel.writeTypedArray(actions, 0); // null ok 1027 1028 if (bigContentView != null) { 1029 parcel.writeInt(1); 1030 bigContentView.writeToParcel(parcel, 0); 1031 } else { 1032 parcel.writeInt(0); 1033 } 1034 1035 parcel.writeInt(visibility); 1036 1037 if (publicVersion != null) { 1038 parcel.writeInt(1); 1039 publicVersion.writeToParcel(parcel, 0); 1040 } else { 1041 parcel.writeInt(0); 1042 } 1043 } 1044 1045 /** 1046 * Parcelable.Creator that instantiates Notification objects 1047 */ 1048 public static final Parcelable.Creator<Notification> CREATOR 1049 = new Parcelable.Creator<Notification>() 1050 { 1051 public Notification createFromParcel(Parcel parcel) 1052 { 1053 return new Notification(parcel); 1054 } 1055 1056 public Notification[] newArray(int size) 1057 { 1058 return new Notification[size]; 1059 } 1060 }; 1061 1062 /** 1063 * Sets the {@link #contentView} field to be a view with the standard "Latest Event" 1064 * layout. 1065 * 1066 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields 1067 * in the view.</p> 1068 * @param context The context for your application / activity. 1069 * @param contentTitle The title that goes in the expanded entry. 1070 * @param contentText The text that goes in the expanded entry. 1071 * @param contentIntent The intent to launch when the user clicks the expanded notification. 1072 * If this is an activity, it must include the 1073 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires 1074 * that you take care of task management as described in the 1075 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back 1076 * Stack</a> document. 1077 * 1078 * @deprecated Use {@link Builder} instead. 1079 */ 1080 @Deprecated 1081 public void setLatestEventInfo(Context context, 1082 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) { 1083 Notification.Builder builder = new Notification.Builder(context); 1084 1085 // First, ensure that key pieces of information that may have been set directly 1086 // are preserved 1087 builder.setWhen(this.when); 1088 builder.setSmallIcon(this.icon); 1089 builder.setPriority(this.priority); 1090 builder.setTicker(this.tickerText); 1091 builder.setNumber(this.number); 1092 builder.mFlags = this.flags; 1093 builder.setSound(this.sound, this.audioStreamType); 1094 builder.setDefaults(this.defaults); 1095 builder.setVibrate(this.vibrate); 1096 1097 // now apply the latestEventInfo fields 1098 if (contentTitle != null) { 1099 builder.setContentTitle(contentTitle); 1100 } 1101 if (contentText != null) { 1102 builder.setContentText(contentText); 1103 } 1104 builder.setContentIntent(contentIntent); 1105 builder.buildInto(this); 1106 } 1107 1108 @Override 1109 public String toString() { 1110 StringBuilder sb = new StringBuilder(); 1111 sb.append("Notification(pri="); 1112 sb.append(priority); 1113 sb.append(" contentView="); 1114 if (contentView != null) { 1115 sb.append(contentView.getPackage()); 1116 sb.append("/0x"); 1117 sb.append(Integer.toHexString(contentView.getLayoutId())); 1118 } else { 1119 sb.append("null"); 1120 } 1121 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below 1122 sb.append(" vibrate="); 1123 if ((this.defaults & DEFAULT_VIBRATE) != 0) { 1124 sb.append("default"); 1125 } else if (this.vibrate != null) { 1126 int N = this.vibrate.length-1; 1127 sb.append("["); 1128 for (int i=0; i<N; i++) { 1129 sb.append(this.vibrate[i]); 1130 sb.append(','); 1131 } 1132 if (N != -1) { 1133 sb.append(this.vibrate[N]); 1134 } 1135 sb.append("]"); 1136 } else { 1137 sb.append("null"); 1138 } 1139 sb.append(" sound="); 1140 if ((this.defaults & DEFAULT_SOUND) != 0) { 1141 sb.append("default"); 1142 } else if (this.sound != null) { 1143 sb.append(this.sound.toString()); 1144 } else { 1145 sb.append("null"); 1146 } 1147 sb.append(" defaults=0x"); 1148 sb.append(Integer.toHexString(this.defaults)); 1149 sb.append(" flags=0x"); 1150 sb.append(Integer.toHexString(this.flags)); 1151 sb.append(" kind=["); 1152 if (this.kind == null) { 1153 sb.append("null"); 1154 } else { 1155 for (int i=0; i<this.kind.length; i++) { 1156 if (i>0) sb.append(","); 1157 sb.append(this.kind[i]); 1158 } 1159 } 1160 sb.append("]"); 1161 if (actions != null) { 1162 sb.append(" "); 1163 sb.append(actions.length); 1164 sb.append(" action"); 1165 if (actions.length > 1) sb.append("s"); 1166 } 1167 sb.append(")"); 1168 return sb.toString(); 1169 } 1170 1171 /** {@hide} */ 1172 public void setUser(UserHandle user) { 1173 if (user.getIdentifier() == UserHandle.USER_ALL) { 1174 user = UserHandle.OWNER; 1175 } 1176 if (tickerView != null) { 1177 tickerView.setUser(user); 1178 } 1179 if (contentView != null) { 1180 contentView.setUser(user); 1181 } 1182 if (bigContentView != null) { 1183 bigContentView.setUser(user); 1184 } 1185 } 1186 1187 /** 1188 * Builder class for {@link Notification} objects. 1189 * 1190 * Provides a convenient way to set the various fields of a {@link Notification} and generate 1191 * content views using the platform's notification layout template. If your app supports 1192 * versions of Android as old as API level 4, you can instead use 1193 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder}, 1194 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support 1195 * library</a>. 1196 * 1197 * <p>Example: 1198 * 1199 * <pre class="prettyprint"> 1200 * Notification noti = new Notification.Builder(mContext) 1201 * .setContentTitle("New mail from " + sender.toString()) 1202 * .setContentText(subject) 1203 * .setSmallIcon(R.drawable.new_mail) 1204 * .setLargeIcon(aBitmap) 1205 * .build(); 1206 * </pre> 1207 */ 1208 public static class Builder { 1209 private static final int MAX_ACTION_BUTTONS = 3; 1210 1211 private Context mContext; 1212 1213 private long mWhen; 1214 private int mSmallIcon; 1215 private int mSmallIconLevel; 1216 private int mNumber; 1217 private CharSequence mContentTitle; 1218 private CharSequence mContentText; 1219 private CharSequence mContentInfo; 1220 private CharSequence mSubText; 1221 private PendingIntent mContentIntent; 1222 private RemoteViews mContentView; 1223 private PendingIntent mDeleteIntent; 1224 private PendingIntent mFullScreenIntent; 1225 private CharSequence mTickerText; 1226 private RemoteViews mTickerView; 1227 private Bitmap mLargeIcon; 1228 private Uri mSound; 1229 private int mAudioStreamType; 1230 private long[] mVibrate; 1231 private int mLedArgb; 1232 private int mLedOnMs; 1233 private int mLedOffMs; 1234 private int mDefaults; 1235 private int mFlags; 1236 private int mProgressMax; 1237 private int mProgress; 1238 private boolean mProgressIndeterminate; 1239 private ArrayList<String> mKindList = new ArrayList<String>(1); 1240 private Bundle mExtras; 1241 private int mPriority; 1242 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS); 1243 private boolean mUseChronometer; 1244 private Style mStyle; 1245 private boolean mShowWhen = true; 1246 private int mVisibility = VISIBILITY_PRIVATE; 1247 private Notification mPublicVersion = null; 1248 1249 /** 1250 * Constructs a new Builder with the defaults: 1251 * 1252 1253 * <table> 1254 * <tr><th align=right>priority</th> 1255 * <td>{@link #PRIORITY_DEFAULT}</td></tr> 1256 * <tr><th align=right>when</th> 1257 * <td>now ({@link System#currentTimeMillis()})</td></tr> 1258 * <tr><th align=right>audio stream</th> 1259 * <td>{@link #STREAM_DEFAULT}</td></tr> 1260 * </table> 1261 * 1262 1263 * @param context 1264 * A {@link Context} that will be used by the Builder to construct the 1265 * RemoteViews. The Context will not be held past the lifetime of this Builder 1266 * object. 1267 */ 1268 public Builder(Context context) { 1269 mContext = context; 1270 1271 // Set defaults to match the defaults of a Notification 1272 mWhen = System.currentTimeMillis(); 1273 mAudioStreamType = STREAM_DEFAULT; 1274 mPriority = PRIORITY_DEFAULT; 1275 } 1276 1277 /** 1278 * Add a timestamp pertaining to the notification (usually the time the event occurred). 1279 * It will be shown in the notification content view by default; use 1280 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this. 1281 * 1282 * @see Notification#when 1283 */ 1284 public Builder setWhen(long when) { 1285 mWhen = when; 1286 return this; 1287 } 1288 1289 /** 1290 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown 1291 * in the content view. 1292 */ 1293 public Builder setShowWhen(boolean show) { 1294 mShowWhen = show; 1295 return this; 1296 } 1297 1298 /** 1299 * Show the {@link Notification#when} field as a stopwatch. 1300 * 1301 * Instead of presenting <code>when</code> as a timestamp, the notification will show an 1302 * automatically updating display of the minutes and seconds since <code>when</code>. 1303 * 1304 * Useful when showing an elapsed time (like an ongoing phone call). 1305 * 1306 * @see android.widget.Chronometer 1307 * @see Notification#when 1308 */ 1309 public Builder setUsesChronometer(boolean b) { 1310 mUseChronometer = b; 1311 return this; 1312 } 1313 1314 /** 1315 * Set the small icon resource, which will be used to represent the notification in the 1316 * status bar. 1317 * 1318 1319 * The platform template for the expanded view will draw this icon in the left, unless a 1320 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small 1321 * icon will be moved to the right-hand side. 1322 * 1323 1324 * @param icon 1325 * A resource ID in the application's package of the drawable to use. 1326 * @see Notification#icon 1327 */ 1328 public Builder setSmallIcon(int icon) { 1329 mSmallIcon = icon; 1330 return this; 1331 } 1332 1333 /** 1334 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional 1335 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable 1336 * LevelListDrawable}. 1337 * 1338 * @param icon A resource ID in the application's package of the drawable to use. 1339 * @param level The level to use for the icon. 1340 * 1341 * @see Notification#icon 1342 * @see Notification#iconLevel 1343 */ 1344 public Builder setSmallIcon(int icon, int level) { 1345 mSmallIcon = icon; 1346 mSmallIconLevel = level; 1347 return this; 1348 } 1349 1350 /** 1351 * Set the first line of text in the platform notification template. 1352 */ 1353 public Builder setContentTitle(CharSequence title) { 1354 mContentTitle = safeCharSequence(title); 1355 return this; 1356 } 1357 1358 /** 1359 * Set the second line of text in the platform notification template. 1360 */ 1361 public Builder setContentText(CharSequence text) { 1362 mContentText = safeCharSequence(text); 1363 return this; 1364 } 1365 1366 /** 1367 * Set the third line of text in the platform notification template. 1368 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the 1369 * same location in the standard template. 1370 */ 1371 public Builder setSubText(CharSequence text) { 1372 mSubText = safeCharSequence(text); 1373 return this; 1374 } 1375 1376 /** 1377 * Set the large number at the right-hand side of the notification. This is 1378 * equivalent to setContentInfo, although it might show the number in a different 1379 * font size for readability. 1380 */ 1381 public Builder setNumber(int number) { 1382 mNumber = number; 1383 return this; 1384 } 1385 1386 /** 1387 * A small piece of additional information pertaining to this notification. 1388 * 1389 * The platform template will draw this on the last line of the notification, at the far 1390 * right (to the right of a smallIcon if it has been placed there). 1391 */ 1392 public Builder setContentInfo(CharSequence info) { 1393 mContentInfo = safeCharSequence(info); 1394 return this; 1395 } 1396 1397 /** 1398 * Set the progress this notification represents. 1399 * 1400 * The platform template will represent this using a {@link ProgressBar}. 1401 */ 1402 public Builder setProgress(int max, int progress, boolean indeterminate) { 1403 mProgressMax = max; 1404 mProgress = progress; 1405 mProgressIndeterminate = indeterminate; 1406 return this; 1407 } 1408 1409 /** 1410 * Supply a custom RemoteViews to use instead of the platform template. 1411 * 1412 * @see Notification#contentView 1413 */ 1414 public Builder setContent(RemoteViews views) { 1415 mContentView = views; 1416 return this; 1417 } 1418 1419 /** 1420 * Supply a {@link PendingIntent} to be sent when the notification is clicked. 1421 * 1422 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you 1423 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use 1424 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)} 1425 * to assign PendingIntents to individual views in that custom layout (i.e., to create 1426 * clickable buttons inside the notification view). 1427 * 1428 * @see Notification#contentIntent Notification.contentIntent 1429 */ 1430 public Builder setContentIntent(PendingIntent intent) { 1431 mContentIntent = intent; 1432 return this; 1433 } 1434 1435 /** 1436 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user. 1437 * 1438 * @see Notification#deleteIntent 1439 */ 1440 public Builder setDeleteIntent(PendingIntent intent) { 1441 mDeleteIntent = intent; 1442 return this; 1443 } 1444 1445 /** 1446 * An intent to launch instead of posting the notification to the status bar. 1447 * Only for use with extremely high-priority notifications demanding the user's 1448 * <strong>immediate</strong> attention, such as an incoming phone call or 1449 * alarm clock that the user has explicitly set to a particular time. 1450 * If this facility is used for something else, please give the user an option 1451 * to turn it off and use a normal notification, as this can be extremely 1452 * disruptive. 1453 * 1454 * @param intent The pending intent to launch. 1455 * @param highPriority Passing true will cause this notification to be sent 1456 * even if other notifications are suppressed. 1457 * 1458 * @see Notification#fullScreenIntent 1459 */ 1460 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { 1461 mFullScreenIntent = intent; 1462 setFlag(FLAG_HIGH_PRIORITY, highPriority); 1463 return this; 1464 } 1465 1466 /** 1467 * Set the "ticker" text which is displayed in the status bar when the notification first 1468 * arrives. 1469 * 1470 * @see Notification#tickerText 1471 */ 1472 public Builder setTicker(CharSequence tickerText) { 1473 mTickerText = safeCharSequence(tickerText); 1474 return this; 1475 } 1476 1477 /** 1478 * Set the text that is displayed in the status bar when the notification first 1479 * arrives, and also a RemoteViews object that may be displayed instead on some 1480 * devices. 1481 * 1482 * @see Notification#tickerText 1483 * @see Notification#tickerView 1484 */ 1485 public Builder setTicker(CharSequence tickerText, RemoteViews views) { 1486 mTickerText = safeCharSequence(tickerText); 1487 mTickerView = views; 1488 return this; 1489 } 1490 1491 /** 1492 * Add a large icon to the notification (and the ticker on some devices). 1493 * 1494 * In the platform template, this image will be shown on the left of the notification view 1495 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side). 1496 * 1497 * @see Notification#largeIcon 1498 */ 1499 public Builder setLargeIcon(Bitmap icon) { 1500 mLargeIcon = icon; 1501 return this; 1502 } 1503 1504 /** 1505 * Set the sound to play. 1506 * 1507 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications. 1508 * 1509 * @see Notification#sound 1510 */ 1511 public Builder setSound(Uri sound) { 1512 mSound = sound; 1513 mAudioStreamType = STREAM_DEFAULT; 1514 return this; 1515 } 1516 1517 /** 1518 * Set the sound to play, along with a specific stream on which to play it. 1519 * 1520 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants. 1521 * 1522 * @see Notification#sound 1523 */ 1524 public Builder setSound(Uri sound, int streamType) { 1525 mSound = sound; 1526 mAudioStreamType = streamType; 1527 return this; 1528 } 1529 1530 /** 1531 * Set the vibration pattern to use. 1532 * 1533 1534 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the 1535 * <code>pattern</code> parameter. 1536 * 1537 1538 * @see Notification#vibrate 1539 */ 1540 public Builder setVibrate(long[] pattern) { 1541 mVibrate = pattern; 1542 return this; 1543 } 1544 1545 /** 1546 * Set the desired color for the indicator LED on the device, as well as the 1547 * blink duty cycle (specified in milliseconds). 1548 * 1549 1550 * Not all devices will honor all (or even any) of these values. 1551 * 1552 1553 * @see Notification#ledARGB 1554 * @see Notification#ledOnMS 1555 * @see Notification#ledOffMS 1556 */ 1557 public Builder setLights(int argb, int onMs, int offMs) { 1558 mLedArgb = argb; 1559 mLedOnMs = onMs; 1560 mLedOffMs = offMs; 1561 return this; 1562 } 1563 1564 /** 1565 * Set whether this is an "ongoing" notification. 1566 * 1567 1568 * Ongoing notifications cannot be dismissed by the user, so your application or service 1569 * must take care of canceling them. 1570 * 1571 1572 * They are typically used to indicate a background task that the user is actively engaged 1573 * with (e.g., playing music) or is pending in some way and therefore occupying the device 1574 * (e.g., a file download, sync operation, active network connection). 1575 * 1576 1577 * @see Notification#FLAG_ONGOING_EVENT 1578 * @see Service#setForeground(boolean) 1579 */ 1580 public Builder setOngoing(boolean ongoing) { 1581 setFlag(FLAG_ONGOING_EVENT, ongoing); 1582 return this; 1583 } 1584 1585 /** 1586 * Set this flag if you would only like the sound, vibrate 1587 * and ticker to be played if the notification is not already showing. 1588 * 1589 * @see Notification#FLAG_ONLY_ALERT_ONCE 1590 */ 1591 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { 1592 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); 1593 return this; 1594 } 1595 1596 /** 1597 * Make this notification automatically dismissed when the user touches it. The 1598 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens. 1599 * 1600 * @see Notification#FLAG_AUTO_CANCEL 1601 */ 1602 public Builder setAutoCancel(boolean autoCancel) { 1603 setFlag(FLAG_AUTO_CANCEL, autoCancel); 1604 return this; 1605 } 1606 1607 /** 1608 * Set whether or not this notification should not bridge to other devices. 1609 * 1610 * <p>Some notifications can be bridged to other devices for remote display. 1611 * This hint can be set to recommend this notification not be bridged. 1612 */ 1613 public Builder setLocalOnly(boolean localOnly) { 1614 setFlag(FLAG_LOCAL_ONLY, localOnly); 1615 return this; 1616 } 1617 1618 /** 1619 * Set which notification properties will be inherited from system defaults. 1620 * <p> 1621 * The value should be one or more of the following fields combined with 1622 * bitwise-or: 1623 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. 1624 * <p> 1625 * For all default values, use {@link #DEFAULT_ALL}. 1626 */ 1627 public Builder setDefaults(int defaults) { 1628 mDefaults = defaults; 1629 return this; 1630 } 1631 1632 /** 1633 * Set the priority of this notification. 1634 * 1635 * @see Notification#priority 1636 */ 1637 public Builder setPriority(@Priority int pri) { 1638 mPriority = pri; 1639 return this; 1640 } 1641 1642 /** 1643 * @hide 1644 * 1645 * Add a kind (category) to this notification. Optional. 1646 * 1647 * @see Notification#kind 1648 */ 1649 public Builder addKind(String k) { 1650 mKindList.add(k); 1651 return this; 1652 } 1653 1654 /** 1655 * Add metadata to this notification. 1656 * 1657 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's 1658 * current contents are copied into the Notification each time {@link #build()} is 1659 * called. 1660 * 1661 * @see Notification#extras 1662 */ 1663 public Builder setExtras(Bundle bag) { 1664 mExtras = bag; 1665 return this; 1666 } 1667 1668 /** 1669 * Add an action to this notification. Actions are typically displayed by 1670 * the system as a button adjacent to the notification content. 1671 * <p> 1672 * Every action must have an icon (32dp square and matching the 1673 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo 1674 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}. 1675 * <p> 1676 * A notification in its expanded form can display up to 3 actions, from left to right in 1677 * the order they were added. Actions will not be displayed when the notification is 1678 * collapsed, however, so be sure that any essential functions may be accessed by the user 1679 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}). 1680 * 1681 * @param icon Resource ID of a drawable that represents the action. 1682 * @param title Text describing the action. 1683 * @param intent PendingIntent to be fired when the action is invoked. 1684 */ 1685 public Builder addAction(int icon, CharSequence title, PendingIntent intent) { 1686 mActions.add(new Action(icon, safeCharSequence(title), intent)); 1687 return this; 1688 } 1689 1690 /** 1691 * Add a rich notification style to be applied at build time. 1692 * 1693 * @param style Object responsible for modifying the notification style. 1694 */ 1695 public Builder setStyle(Style style) { 1696 if (mStyle != style) { 1697 mStyle = style; 1698 if (mStyle != null) { 1699 mStyle.setBuilder(this); 1700 } 1701 } 1702 return this; 1703 } 1704 1705 /** 1706 * Specify the value of {@link #visibility}. 1707 1708 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default), 1709 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}. 1710 * 1711 * @return The same Builder. 1712 */ 1713 public Builder setVisibility(int visibility) { 1714 mVisibility = visibility; 1715 return this; 1716 } 1717 1718 /** 1719 * Supply a replacement Notification whose contents should be shown in insecure contexts 1720 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}. 1721 * @param n A replacement notification, presumably with some or all info redacted. 1722 * @return The same Builder. 1723 */ 1724 public Builder setPublicVersion(Notification n) { 1725 mPublicVersion = n; 1726 return this; 1727 } 1728 1729 private void setFlag(int mask, boolean value) { 1730 if (value) { 1731 mFlags |= mask; 1732 } else { 1733 mFlags &= ~mask; 1734 } 1735 } 1736 1737 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) { 1738 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId); 1739 boolean showLine3 = false; 1740 boolean showLine2 = false; 1741 int smallIconImageViewId = R.id.icon; 1742 if (mLargeIcon != null) { 1743 contentView.setImageViewBitmap(R.id.icon, mLargeIcon); 1744 smallIconImageViewId = R.id.right_icon; 1745 } 1746 if (mPriority < PRIORITY_LOW) { 1747 contentView.setInt(R.id.icon, 1748 "setBackgroundResource", R.drawable.notification_template_icon_low_bg); 1749 contentView.setInt(R.id.status_bar_latest_event_content, 1750 "setBackgroundResource", R.drawable.notification_bg_low); 1751 } 1752 if (mSmallIcon != 0) { 1753 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon); 1754 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE); 1755 } else { 1756 contentView.setViewVisibility(smallIconImageViewId, View.GONE); 1757 } 1758 if (mContentTitle != null) { 1759 contentView.setTextViewText(R.id.title, mContentTitle); 1760 } 1761 if (mContentText != null) { 1762 contentView.setTextViewText(R.id.text, mContentText); 1763 showLine3 = true; 1764 } 1765 if (mContentInfo != null) { 1766 contentView.setTextViewText(R.id.info, mContentInfo); 1767 contentView.setViewVisibility(R.id.info, View.VISIBLE); 1768 showLine3 = true; 1769 } else if (mNumber > 0) { 1770 final int tooBig = mContext.getResources().getInteger( 1771 R.integer.status_bar_notification_info_maxnum); 1772 if (mNumber > tooBig) { 1773 contentView.setTextViewText(R.id.info, mContext.getResources().getString( 1774 R.string.status_bar_notification_info_overflow)); 1775 } else { 1776 NumberFormat f = NumberFormat.getIntegerInstance(); 1777 contentView.setTextViewText(R.id.info, f.format(mNumber)); 1778 } 1779 contentView.setViewVisibility(R.id.info, View.VISIBLE); 1780 showLine3 = true; 1781 } else { 1782 contentView.setViewVisibility(R.id.info, View.GONE); 1783 } 1784 1785 // Need to show three lines? 1786 if (mSubText != null) { 1787 contentView.setTextViewText(R.id.text, mSubText); 1788 if (mContentText != null) { 1789 contentView.setTextViewText(R.id.text2, mContentText); 1790 contentView.setViewVisibility(R.id.text2, View.VISIBLE); 1791 showLine2 = true; 1792 } else { 1793 contentView.setViewVisibility(R.id.text2, View.GONE); 1794 } 1795 } else { 1796 contentView.setViewVisibility(R.id.text2, View.GONE); 1797 if (mProgressMax != 0 || mProgressIndeterminate) { 1798 contentView.setProgressBar( 1799 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate); 1800 contentView.setViewVisibility(R.id.progress, View.VISIBLE); 1801 showLine2 = true; 1802 } else { 1803 contentView.setViewVisibility(R.id.progress, View.GONE); 1804 } 1805 } 1806 if (showLine2) { 1807 if (fitIn1U) { 1808 // need to shrink all the type to make sure everything fits 1809 final Resources res = mContext.getResources(); 1810 final float subTextSize = res.getDimensionPixelSize( 1811 R.dimen.notification_subtext_size); 1812 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize); 1813 } 1814 // vertical centering 1815 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0); 1816 } 1817 1818 if (mWhen != 0 && mShowWhen) { 1819 if (mUseChronometer) { 1820 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE); 1821 contentView.setLong(R.id.chronometer, "setBase", 1822 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis())); 1823 contentView.setBoolean(R.id.chronometer, "setStarted", true); 1824 } else { 1825 contentView.setViewVisibility(R.id.time, View.VISIBLE); 1826 contentView.setLong(R.id.time, "setTime", mWhen); 1827 } 1828 } else { 1829 contentView.setViewVisibility(R.id.time, View.GONE); 1830 } 1831 1832 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE); 1833 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE); 1834 return contentView; 1835 } 1836 1837 private RemoteViews applyStandardTemplateWithActions(int layoutId) { 1838 RemoteViews big = applyStandardTemplate(layoutId, false); 1839 1840 int N = mActions.size(); 1841 if (N > 0) { 1842 // Log.d("Notification", "has actions: " + mContentText); 1843 big.setViewVisibility(R.id.actions, View.VISIBLE); 1844 big.setViewVisibility(R.id.action_divider, View.VISIBLE); 1845 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS; 1846 big.removeAllViews(R.id.actions); 1847 for (int i=0; i<N; i++) { 1848 final RemoteViews button = generateActionButton(mActions.get(i)); 1849 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title); 1850 big.addView(R.id.actions, button); 1851 } 1852 } 1853 return big; 1854 } 1855 1856 private RemoteViews makeContentView() { 1857 if (mContentView != null) { 1858 return mContentView; 1859 } else { 1860 return applyStandardTemplate(R.layout.notification_template_base, true); // no more special large_icon flavor 1861 } 1862 } 1863 1864 private RemoteViews makeTickerView() { 1865 if (mTickerView != null) { 1866 return mTickerView; 1867 } else { 1868 if (mContentView == null) { 1869 return applyStandardTemplate(mLargeIcon == null 1870 ? R.layout.status_bar_latest_event_ticker 1871 : R.layout.status_bar_latest_event_ticker_large_icon, true); 1872 } else { 1873 return null; 1874 } 1875 } 1876 } 1877 1878 private RemoteViews makeBigContentView() { 1879 if (mActions.size() == 0) return null; 1880 1881 return applyStandardTemplateWithActions(R.layout.notification_template_big_base); 1882 } 1883 1884 private RemoteViews generateActionButton(Action action) { 1885 final boolean tombstone = (action.actionIntent == null); 1886 RemoteViews button = new RemoteViews(mContext.getPackageName(), 1887 tombstone ? R.layout.notification_action_tombstone 1888 : R.layout.notification_action); 1889 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0); 1890 button.setTextViewText(R.id.action0, action.title); 1891 if (!tombstone) { 1892 button.setOnClickPendingIntent(R.id.action0, action.actionIntent); 1893 } 1894 button.setContentDescription(R.id.action0, action.title); 1895 return button; 1896 } 1897 1898 /** 1899 * Apply the unstyled operations and return a new {@link Notification} object. 1900 * @hide 1901 */ 1902 public Notification buildUnstyled() { 1903 Notification n = new Notification(); 1904 n.when = mWhen; 1905 n.icon = mSmallIcon; 1906 n.iconLevel = mSmallIconLevel; 1907 n.number = mNumber; 1908 n.contentView = makeContentView(); 1909 n.contentIntent = mContentIntent; 1910 n.deleteIntent = mDeleteIntent; 1911 n.fullScreenIntent = mFullScreenIntent; 1912 n.tickerText = mTickerText; 1913 n.tickerView = makeTickerView(); 1914 n.largeIcon = mLargeIcon; 1915 n.sound = mSound; 1916 n.audioStreamType = mAudioStreamType; 1917 n.vibrate = mVibrate; 1918 n.ledARGB = mLedArgb; 1919 n.ledOnMS = mLedOnMs; 1920 n.ledOffMS = mLedOffMs; 1921 n.defaults = mDefaults; 1922 n.flags = mFlags; 1923 n.bigContentView = makeBigContentView(); 1924 if (mLedOnMs != 0 || mLedOffMs != 0) { 1925 n.flags |= FLAG_SHOW_LIGHTS; 1926 } 1927 if ((mDefaults & DEFAULT_LIGHTS) != 0) { 1928 n.flags |= FLAG_SHOW_LIGHTS; 1929 } 1930 if (mKindList.size() > 0) { 1931 n.kind = new String[mKindList.size()]; 1932 mKindList.toArray(n.kind); 1933 } else { 1934 n.kind = null; 1935 } 1936 n.priority = mPriority; 1937 if (mActions.size() > 0) { 1938 n.actions = new Action[mActions.size()]; 1939 mActions.toArray(n.actions); 1940 } 1941 n.visibility = mVisibility; 1942 1943 if (mPublicVersion != null) { 1944 n.publicVersion = new Notification(); 1945 mPublicVersion.cloneInto(n.publicVersion, true); 1946 } 1947 1948 return n; 1949 } 1950 1951 /** 1952 * Capture, in the provided bundle, semantic information used in the construction of 1953 * this Notification object. 1954 * @hide 1955 */ 1956 public void addExtras(Bundle extras) { 1957 // Store original information used in the construction of this object 1958 extras.putCharSequence(EXTRA_TITLE, mContentTitle); 1959 extras.putCharSequence(EXTRA_TEXT, mContentText); 1960 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText); 1961 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo); 1962 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon); 1963 extras.putInt(EXTRA_PROGRESS, mProgress); 1964 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax); 1965 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate); 1966 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer); 1967 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen); 1968 if (mLargeIcon != null) { 1969 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon); 1970 } 1971 } 1972 1973 /** 1974 * @deprecated Use {@link #build()} instead. 1975 */ 1976 @Deprecated 1977 public Notification getNotification() { 1978 return build(); 1979 } 1980 1981 /** 1982 * Combine all of the options that have been set and return a new {@link Notification} 1983 * object. 1984 */ 1985 public Notification build() { 1986 Notification n = buildUnstyled(); 1987 1988 if (mStyle != null) { 1989 n = mStyle.buildStyled(n); 1990 } 1991 1992 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle(); 1993 1994 addExtras(n.extras); 1995 if (mStyle != null) { 1996 mStyle.addExtras(n.extras); 1997 } 1998 1999 return n; 2000 } 2001 2002 /** 2003 * Apply this Builder to an existing {@link Notification} object. 2004 * 2005 * @hide 2006 */ 2007 public Notification buildInto(Notification n) { 2008 build().cloneInto(n, true); 2009 return n; 2010 } 2011 } 2012 2013 /** 2014 * An object that can apply a rich notification style to a {@link Notification.Builder} 2015 * object. 2016 */ 2017 public static abstract class Style { 2018 private CharSequence mBigContentTitle; 2019 private CharSequence mSummaryText = null; 2020 private boolean mSummaryTextSet = false; 2021 2022 protected Builder mBuilder; 2023 2024 /** 2025 * Overrides ContentTitle in the big form of the template. 2026 * This defaults to the value passed to setContentTitle(). 2027 */ 2028 protected void internalSetBigContentTitle(CharSequence title) { 2029 mBigContentTitle = title; 2030 } 2031 2032 /** 2033 * Set the first line of text after the detail section in the big form of the template. 2034 */ 2035 protected void internalSetSummaryText(CharSequence cs) { 2036 mSummaryText = cs; 2037 mSummaryTextSet = true; 2038 } 2039 2040 public void setBuilder(Builder builder) { 2041 if (mBuilder != builder) { 2042 mBuilder = builder; 2043 if (mBuilder != null) { 2044 mBuilder.setStyle(this); 2045 } 2046 } 2047 } 2048 2049 protected void checkBuilder() { 2050 if (mBuilder == null) { 2051 throw new IllegalArgumentException("Style requires a valid Builder object"); 2052 } 2053 } 2054 2055 protected RemoteViews getStandardView(int layoutId) { 2056 checkBuilder(); 2057 2058 if (mBigContentTitle != null) { 2059 mBuilder.setContentTitle(mBigContentTitle); 2060 } 2061 2062 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId); 2063 2064 if (mBigContentTitle != null && mBigContentTitle.equals("")) { 2065 contentView.setViewVisibility(R.id.line1, View.GONE); 2066 } else { 2067 contentView.setViewVisibility(R.id.line1, View.VISIBLE); 2068 } 2069 2070 // The last line defaults to the subtext, but can be replaced by mSummaryText 2071 final CharSequence overflowText = 2072 mSummaryTextSet ? mSummaryText 2073 : mBuilder.mSubText; 2074 if (overflowText != null) { 2075 contentView.setTextViewText(R.id.text, overflowText); 2076 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE); 2077 contentView.setViewVisibility(R.id.line3, View.VISIBLE); 2078 } else { 2079 contentView.setViewVisibility(R.id.overflow_divider, View.GONE); 2080 contentView.setViewVisibility(R.id.line3, View.GONE); 2081 } 2082 2083 return contentView; 2084 } 2085 2086 /** 2087 * @hide 2088 */ 2089 public void addExtras(Bundle extras) { 2090 if (mSummaryTextSet) { 2091 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText); 2092 } 2093 if (mBigContentTitle != null) { 2094 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle); 2095 } 2096 extras.putString(EXTRA_TEMPLATE, this.getClass().getName()); 2097 } 2098 2099 /** 2100 * @hide 2101 */ 2102 public abstract Notification buildStyled(Notification wip); 2103 2104 /** 2105 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is 2106 * attached to. 2107 * 2108 * @return the fully constructed Notification. 2109 */ 2110 public Notification build() { 2111 checkBuilder(); 2112 return mBuilder.build(); 2113 } 2114 } 2115 2116 /** 2117 * Helper class for generating large-format notifications that include a large image attachment. 2118 * 2119 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so: 2120 * <pre class="prettyprint"> 2121 * Notification noti = new Notification.BigPictureStyle( 2122 * new Notification.Builder() 2123 * .setContentTitle("New photo from " + sender.toString()) 2124 * .setContentText(subject) 2125 * .setSmallIcon(R.drawable.new_post) 2126 * .setLargeIcon(aBitmap)) 2127 * .bigPicture(aBigBitmap) 2128 * .build(); 2129 * </pre> 2130 * 2131 * @see Notification#bigContentView 2132 */ 2133 public static class BigPictureStyle extends Style { 2134 private Bitmap mPicture; 2135 private Bitmap mBigLargeIcon; 2136 private boolean mBigLargeIconSet = false; 2137 2138 public BigPictureStyle() { 2139 } 2140 2141 public BigPictureStyle(Builder builder) { 2142 setBuilder(builder); 2143 } 2144 2145 /** 2146 * Overrides ContentTitle in the big form of the template. 2147 * This defaults to the value passed to setContentTitle(). 2148 */ 2149 public BigPictureStyle setBigContentTitle(CharSequence title) { 2150 internalSetBigContentTitle(safeCharSequence(title)); 2151 return this; 2152 } 2153 2154 /** 2155 * Set the first line of text after the detail section in the big form of the template. 2156 */ 2157 public BigPictureStyle setSummaryText(CharSequence cs) { 2158 internalSetSummaryText(safeCharSequence(cs)); 2159 return this; 2160 } 2161 2162 /** 2163 * Provide the bitmap to be used as the payload for the BigPicture notification. 2164 */ 2165 public BigPictureStyle bigPicture(Bitmap b) { 2166 mPicture = b; 2167 return this; 2168 } 2169 2170 /** 2171 * Override the large icon when the big notification is shown. 2172 */ 2173 public BigPictureStyle bigLargeIcon(Bitmap b) { 2174 mBigLargeIconSet = true; 2175 mBigLargeIcon = b; 2176 return this; 2177 } 2178 2179 private RemoteViews makeBigContentView() { 2180 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture); 2181 2182 contentView.setImageViewBitmap(R.id.big_picture, mPicture); 2183 2184 return contentView; 2185 } 2186 2187 /** 2188 * @hide 2189 */ 2190 public void addExtras(Bundle extras) { 2191 super.addExtras(extras); 2192 2193 if (mBigLargeIconSet) { 2194 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon); 2195 } 2196 extras.putParcelable(EXTRA_PICTURE, mPicture); 2197 } 2198 2199 /** 2200 * @hide 2201 */ 2202 @Override 2203 public Notification buildStyled(Notification wip) { 2204 if (mBigLargeIconSet ) { 2205 mBuilder.mLargeIcon = mBigLargeIcon; 2206 } 2207 wip.bigContentView = makeBigContentView(); 2208 return wip; 2209 } 2210 } 2211 2212 /** 2213 * Helper class for generating large-format notifications that include a lot of text. 2214 * 2215 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so: 2216 * <pre class="prettyprint"> 2217 * Notification noti = new Notification.BigTextStyle( 2218 * new Notification.Builder() 2219 * .setContentTitle("New mail from " + sender.toString()) 2220 * .setContentText(subject) 2221 * .setSmallIcon(R.drawable.new_mail) 2222 * .setLargeIcon(aBitmap)) 2223 * .bigText(aVeryLongString) 2224 * .build(); 2225 * </pre> 2226 * 2227 * @see Notification#bigContentView 2228 */ 2229 public static class BigTextStyle extends Style { 2230 private CharSequence mBigText; 2231 2232 public BigTextStyle() { 2233 } 2234 2235 public BigTextStyle(Builder builder) { 2236 setBuilder(builder); 2237 } 2238 2239 /** 2240 * Overrides ContentTitle in the big form of the template. 2241 * This defaults to the value passed to setContentTitle(). 2242 */ 2243 public BigTextStyle setBigContentTitle(CharSequence title) { 2244 internalSetBigContentTitle(safeCharSequence(title)); 2245 return this; 2246 } 2247 2248 /** 2249 * Set the first line of text after the detail section in the big form of the template. 2250 */ 2251 public BigTextStyle setSummaryText(CharSequence cs) { 2252 internalSetSummaryText(safeCharSequence(cs)); 2253 return this; 2254 } 2255 2256 /** 2257 * Provide the longer text to be displayed in the big form of the 2258 * template in place of the content text. 2259 */ 2260 public BigTextStyle bigText(CharSequence cs) { 2261 mBigText = safeCharSequence(cs); 2262 return this; 2263 } 2264 2265 /** 2266 * @hide 2267 */ 2268 public void addExtras(Bundle extras) { 2269 super.addExtras(extras); 2270 2271 extras.putCharSequence(EXTRA_TEXT, mBigText); 2272 } 2273 2274 private RemoteViews makeBigContentView() { 2275 // Remove the content text so line3 only shows if you have a summary 2276 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null); 2277 mBuilder.mContentText = null; 2278 2279 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text); 2280 2281 if (hadThreeLines) { 2282 // vertical centering 2283 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0); 2284 } 2285 2286 contentView.setTextViewText(R.id.big_text, mBigText); 2287 contentView.setViewVisibility(R.id.big_text, View.VISIBLE); 2288 contentView.setViewVisibility(R.id.text2, View.GONE); 2289 2290 return contentView; 2291 } 2292 2293 /** 2294 * @hide 2295 */ 2296 @Override 2297 public Notification buildStyled(Notification wip) { 2298 wip.bigContentView = makeBigContentView(); 2299 2300 wip.extras.putCharSequence(EXTRA_TEXT, mBigText); 2301 2302 return wip; 2303 } 2304 } 2305 2306 /** 2307 * Helper class for generating large-format notifications that include a list of (up to 5) strings. 2308 * 2309 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so: 2310 * <pre class="prettyprint"> 2311 * Notification noti = new Notification.InboxStyle( 2312 * new Notification.Builder() 2313 * .setContentTitle("5 New mails from " + sender.toString()) 2314 * .setContentText(subject) 2315 * .setSmallIcon(R.drawable.new_mail) 2316 * .setLargeIcon(aBitmap)) 2317 * .addLine(str1) 2318 * .addLine(str2) 2319 * .setContentTitle("") 2320 * .setSummaryText("+3 more") 2321 * .build(); 2322 * </pre> 2323 * 2324 * @see Notification#bigContentView 2325 */ 2326 public static class InboxStyle extends Style { 2327 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5); 2328 2329 public InboxStyle() { 2330 } 2331 2332 public InboxStyle(Builder builder) { 2333 setBuilder(builder); 2334 } 2335 2336 /** 2337 * Overrides ContentTitle in the big form of the template. 2338 * This defaults to the value passed to setContentTitle(). 2339 */ 2340 public InboxStyle setBigContentTitle(CharSequence title) { 2341 internalSetBigContentTitle(safeCharSequence(title)); 2342 return this; 2343 } 2344 2345 /** 2346 * Set the first line of text after the detail section in the big form of the template. 2347 */ 2348 public InboxStyle setSummaryText(CharSequence cs) { 2349 internalSetSummaryText(safeCharSequence(cs)); 2350 return this; 2351 } 2352 2353 /** 2354 * Append a line to the digest section of the Inbox notification. 2355 */ 2356 public InboxStyle addLine(CharSequence cs) { 2357 mTexts.add(safeCharSequence(cs)); 2358 return this; 2359 } 2360 2361 /** 2362 * @hide 2363 */ 2364 public void addExtras(Bundle extras) { 2365 super.addExtras(extras); 2366 CharSequence[] a = new CharSequence[mTexts.size()]; 2367 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a)); 2368 } 2369 2370 private RemoteViews makeBigContentView() { 2371 // Remove the content text so line3 disappears unless you have a summary 2372 mBuilder.mContentText = null; 2373 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox); 2374 2375 contentView.setViewVisibility(R.id.text2, View.GONE); 2376 2377 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3, 2378 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6}; 2379 2380 // Make sure all rows are gone in case we reuse a view. 2381 for (int rowId : rowIds) { 2382 contentView.setViewVisibility(rowId, View.GONE); 2383 } 2384 2385 2386 int i=0; 2387 while (i < mTexts.size() && i < rowIds.length) { 2388 CharSequence str = mTexts.get(i); 2389 if (str != null && !str.equals("")) { 2390 contentView.setViewVisibility(rowIds[i], View.VISIBLE); 2391 contentView.setTextViewText(rowIds[i], str); 2392 } 2393 i++; 2394 } 2395 2396 contentView.setViewVisibility(R.id.inbox_end_pad, 2397 mTexts.size() > 0 ? View.VISIBLE : View.GONE); 2398 2399 contentView.setViewVisibility(R.id.inbox_more, 2400 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE); 2401 2402 return contentView; 2403 } 2404 2405 /** 2406 * @hide 2407 */ 2408 @Override 2409 public Notification buildStyled(Notification wip) { 2410 wip.bigContentView = makeBigContentView(); 2411 2412 return wip; 2413 } 2414 } 2415} 2416