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