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