Notification.java revision 3c5f92432734e1e3b9bdc515628a4c09d7759cd4
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.graphics.Bitmap; 24import android.media.AudioManager; 25import android.net.Uri; 26import android.os.Bundle; 27import android.os.IBinder; 28import android.os.Parcel; 29import android.os.Parcelable; 30import android.os.SystemClock; 31import android.text.TextUtils; 32import android.util.IntProperty; 33import android.util.Log; 34import android.view.View; 35import android.widget.ProgressBar; 36import android.widget.RemoteViews; 37 38import java.text.NumberFormat; 39import java.util.ArrayList; 40 41/** 42 * A class that represents how a persistent notification is to be presented to 43 * the user using the {@link android.app.NotificationManager}. 44 * 45 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it 46 * easier to construct Notifications.</p> 47 * 48 * <div class="special reference"> 49 * <h3>Developer Guides</h3> 50 * <p>For a guide to creating notifications, read the 51 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> 52 * developer guide.</p> 53 * </div> 54 */ 55public class Notification implements Parcelable 56{ 57 /** 58 * Use all default values (where applicable). 59 */ 60 public static final int DEFAULT_ALL = ~0; 61 62 /** 63 * Use the default notification sound. This will ignore any given 64 * {@link #sound}. 65 * 66 67 * @see #defaults 68 */ 69 70 public static final int DEFAULT_SOUND = 1; 71 72 /** 73 * Use the default notification vibrate. This will ignore any given 74 * {@link #vibrate}. Using phone vibration requires the 75 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission. 76 * 77 * @see #defaults 78 */ 79 80 public static final int DEFAULT_VIBRATE = 2; 81 82 /** 83 * Use the default notification lights. This will ignore the 84 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or 85 * {@link #ledOnMS}. 86 * 87 * @see #defaults 88 */ 89 90 public static final int DEFAULT_LIGHTS = 4; 91 92 /** 93 * A timestamp related to this notification, in milliseconds since the epoch. 94 * 95 * Default value: {@link System#currentTimeMillis() Now}. 96 * 97 * Choose a timestamp that will be most relevant to the user. For most finite events, this 98 * corresponds to the time the event happened (or will happen, in the case of events that have 99 * yet to occur but about which the user is being informed). Indefinite events should be 100 * timestamped according to when the activity began. 101 * 102 * Some examples: 103 * 104 * <ul> 105 * <li>Notification of a new chat message should be stamped when the message was received.</li> 106 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li> 107 * <li>Notification of a completed file download should be stamped when the download finished.</li> 108 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li> 109 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time. 110 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time. 111 * </ul> 112 * 113 */ 114 public long when; 115 116 /** 117 * The resource id of a drawable to use as the icon in the status bar. 118 * This is required; notifications with an invalid icon resource will not be shown. 119 */ 120 public int icon; 121 122 /** 123 * If the icon in the status bar is to have more than one level, you can set this. Otherwise, 124 * leave it at its default value of 0. 125 * 126 * @see android.widget.ImageView#setImageLevel 127 * @see android.graphics.drawable#setLevel 128 */ 129 public int iconLevel; 130 131 /** 132 * The number of events that this notification represents. For example, in a new mail 133 * notification, this could be the number of unread messages. 134 * 135 * The system may or may not use this field to modify the appearance of the notification. For 136 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was 137 * superimposed over the icon in the status bar. Starting with 138 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by 139 * {@link Notification.Builder} has displayed the number in the expanded notification view. 140 * 141 * If the number is 0 or negative, it is never shown. 142 */ 143 public int number; 144 145 /** 146 * The intent to execute when the expanded status entry is clicked. If 147 * this is an activity, it must include the 148 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires 149 * that you take care of task management as described in the 150 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back 151 * Stack</a> document. In particular, make sure to read the notification section 152 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling 153 * Notifications</a> for the correct ways to launch an application from a 154 * notification. 155 */ 156 public PendingIntent contentIntent; 157 158 /** 159 * The intent to execute when the notification is explicitly dismissed by the user, either with 160 * the "Clear All" button or by swiping it away individually. 161 * 162 * This probably shouldn't be launching an activity since several of those will be sent 163 * at the same time. 164 */ 165 public PendingIntent deleteIntent; 166 167 /** 168 * An intent to launch instead of posting the notification to the status bar. 169 * 170 * @see Notification.Builder#setFullScreenIntent 171 */ 172 public PendingIntent fullScreenIntent; 173 174 /** 175 * Text to scroll across the screen when this item is added to 176 * the status bar on large and smaller devices. 177 * 178 * @see #tickerView 179 */ 180 public CharSequence tickerText; 181 182 /** 183 * The view to show as the ticker in the status bar when the notification 184 * is posted. 185 */ 186 public RemoteViews tickerView; 187 188 /** 189 * The view that will represent this notification in the expanded status bar. 190 */ 191 public RemoteViews contentView; 192 193 /** 194 * A large-format version of {@link #contentView}, giving the Notification an 195 * opportunity to show more detail. The system UI may choose to show this 196 * instead of the normal content view at its discretion. 197 */ 198 public RemoteViews bigContentView; 199 200 /** 201 * The bitmap that may escape the bounds of the panel and bar. 202 */ 203 public Bitmap largeIcon; 204 205 /** 206 * The sound to play. 207 * 208 * <p> 209 * To play the default notification sound, see {@link #defaults}. 210 * </p> 211 */ 212 public Uri sound; 213 214 /** 215 * Use this constant as the value for audioStreamType to request that 216 * the default stream type for notifications be used. Currently the 217 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}. 218 */ 219 public static final int STREAM_DEFAULT = -1; 220 221 /** 222 * The audio stream type to use when playing the sound. 223 * Should be one of the STREAM_ constants from 224 * {@link android.media.AudioManager}. 225 */ 226 public int audioStreamType = STREAM_DEFAULT; 227 228 /** 229 * The pattern with which to vibrate. 230 * 231 * <p> 232 * To vibrate the default pattern, see {@link #defaults}. 233 * </p> 234 * 235 * @see android.os.Vibrator#vibrate(long[],int) 236 */ 237 public long[] vibrate; 238 239 /** 240 * The color of the led. The hardware will do its best approximation. 241 * 242 * @see #FLAG_SHOW_LIGHTS 243 * @see #flags 244 */ 245 public int ledARGB; 246 247 /** 248 * The number of milliseconds for the LED to be on while it's flashing. 249 * The hardware will do its best approximation. 250 * 251 * @see #FLAG_SHOW_LIGHTS 252 * @see #flags 253 */ 254 public int ledOnMS; 255 256 /** 257 * The number of milliseconds for the LED to be off while it's flashing. 258 * The hardware will do its best approximation. 259 * 260 * @see #FLAG_SHOW_LIGHTS 261 * @see #flags 262 */ 263 public int ledOffMS; 264 265 /** 266 * Specifies which values should be taken from the defaults. 267 * <p> 268 * To set, OR the desired from {@link #DEFAULT_SOUND}, 269 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default 270 * values, use {@link #DEFAULT_ALL}. 271 * </p> 272 */ 273 public int defaults; 274 275 /** 276 * Bit to be bitwise-ored into the {@link #flags} field that should be 277 * set if you want the LED on for this notification. 278 * <ul> 279 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB 280 * or 0 for both ledOnMS and ledOffMS.</li> 281 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li> 282 * <li>To flash the LED, pass the number of milliseconds that it should 283 * be on and off to ledOnMS and ledOffMS.</li> 284 * </ul> 285 * <p> 286 * Since hardware varies, you are not guaranteed that any of the values 287 * you pass are honored exactly. Use the system defaults (TODO) if possible 288 * because they will be set to values that work on any given hardware. 289 * <p> 290 * The alpha channel must be set for forward compatibility. 291 * 292 */ 293 public static final int FLAG_SHOW_LIGHTS = 0x00000001; 294 295 /** 296 * Bit to be bitwise-ored into the {@link #flags} field that should be 297 * set if this notification is in reference to something that is ongoing, 298 * like a phone call. It should not be set if this notification is in 299 * reference to something that happened at a particular point in time, 300 * like a missed phone call. 301 */ 302 public static final int FLAG_ONGOING_EVENT = 0x00000002; 303 304 /** 305 * Bit to be bitwise-ored into the {@link #flags} field that if set, 306 * the audio will be repeated until the notification is 307 * cancelled or the notification window is opened. 308 */ 309 public static final int FLAG_INSISTENT = 0x00000004; 310 311 /** 312 * Bit to be bitwise-ored into the {@link #flags} field that should be 313 * set if you want the sound and/or vibration play each time the 314 * notification is sent, even if it has not been canceled before that. 315 */ 316 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008; 317 318 /** 319 * Bit to be bitwise-ored into the {@link #flags} field that should be 320 * set if the notification should be canceled when it is clicked by the 321 * user. On tablets, the 322 323 */ 324 public static final int FLAG_AUTO_CANCEL = 0x00000010; 325 326 /** 327 * Bit to be bitwise-ored into the {@link #flags} field that should be 328 * set if the notification should not be canceled when the user clicks 329 * the Clear all button. 330 */ 331 public static final int FLAG_NO_CLEAR = 0x00000020; 332 333 /** 334 * Bit to be bitwise-ored into the {@link #flags} field that should be 335 * set if this notification represents a currently running service. This 336 * will normally be set for you by {@link Service#startForeground}. 337 */ 338 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040; 339 340 /** 341 * Obsolete flag indicating high-priority notifications; use the priority field instead. 342 * 343 * @deprecated Use {@link #priority} with a positive value. 344 */ 345 public static final int FLAG_HIGH_PRIORITY = 0x00000080; 346 347 public int flags; 348 349 /** 350 * Default notification {@link #priority}. If your application does not prioritize its own 351 * notifications, use this value for all notifications. 352 */ 353 public static final int PRIORITY_DEFAULT = 0; 354 355 /** 356 * Lower {@link #priority}, for items that are less important. The UI may choose to show these 357 * items smaller, or at a different position in the list, compared with your app's 358 * {@link #PRIORITY_DEFAULT} items. 359 */ 360 public static final int PRIORITY_LOW = -1; 361 362 /** 363 * Lowest {@link #priority}; these items might not be shown to the user except under special 364 * circumstances, such as detailed notification logs. 365 */ 366 public static final int PRIORITY_MIN = -2; 367 368 /** 369 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to 370 * show these items larger, or at a different position in notification lists, compared with 371 * your app's {@link #PRIORITY_DEFAULT} items. 372 */ 373 public static final int PRIORITY_HIGH = 1; 374 375 /** 376 * Highest {@link #priority}, for your application's most important items that require the 377 * user's prompt attention or input. 378 */ 379 public static final int PRIORITY_MAX = 2; 380 381 /** 382 * Relative priority for this notification. 383 * 384 * Priority is an indication of how much of the user's valuable attention should be consumed by 385 * this notification. Low-priority notifications may be hidden from the user in certain 386 * situations, while the user might be interrupted for a higher-priority notification. The 387 * system will make a determination about how to interpret notification priority as described in 388 * MUMBLE MUMBLE. 389 */ 390 public int priority; 391 392 /** 393 * Notification type: incoming call (voice or video) or similar synchronous communication request. 394 */ 395 public static final String KIND_CALL = "android.call"; 396 397 /** 398 * Notification type: incoming direct message (SMS, instant message, etc.). 399 */ 400 public static final String KIND_MESSAGE = "android.message"; 401 402 /** 403 * Notification type: asynchronous bulk message (email). 404 */ 405 public static final String KIND_EMAIL = "android.email"; 406 407 /** 408 * Notification type: calendar event. 409 */ 410 public static final String KIND_EVENT = "android.event"; 411 412 /** 413 * Notification type: promotion or advertisement. 414 */ 415 public static final String KIND_PROMO = "android.promo"; 416 417 /** 418 * If this notification matches of one or more special types (see the <code>KIND_*</code> 419 * constants), add them here, best match first. 420 */ 421 public String[] kind; 422 423 /** 424 * Extra key for people values (type TBD). 425 * 426 * @hide 427 */ 428 public static final String EXTRA_PEOPLE = "android.people"; 429 430 private Bundle extras; 431 432 /** 433 * Structure to encapsulate an "action", including title and icon, that can be attached to a Notification. 434 * @hide 435 */ 436 private static class Action implements Parcelable { 437 public int icon; 438 public CharSequence title; 439 public PendingIntent actionIntent; 440 @SuppressWarnings("unused") 441 public Action() { } 442 private Action(Parcel in) { 443 icon = in.readInt(); 444 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 445 if (in.readInt() == 1) { 446 actionIntent = PendingIntent.CREATOR.createFromParcel(in); 447 } 448 } 449 public Action(int icon_, CharSequence title_, PendingIntent intent_) { 450 this.icon = icon_; 451 this.title = title_; 452 this.actionIntent = intent_; 453 } 454 @Override 455 public Action clone() { 456 return new Action( 457 this.icon, 458 this.title.toString(), 459 this.actionIntent // safe to alias 460 ); 461 } 462 @Override 463 public int describeContents() { 464 return 0; 465 } 466 @Override 467 public void writeToParcel(Parcel out, int flags) { 468 out.writeInt(icon); 469 TextUtils.writeToParcel(title, out, flags); 470 if (actionIntent != null) { 471 out.writeInt(1); 472 actionIntent.writeToParcel(out, flags); 473 } else { 474 out.writeInt(0); 475 } 476 } 477 public static final Parcelable.Creator<Action> CREATOR 478 = new Parcelable.Creator<Action>() { 479 public Action createFromParcel(Parcel in) { 480 return new Action(in); 481 } 482 public Action[] newArray(int size) { 483 return new Action[size]; 484 } 485 }; 486 } 487 488 private Action[] actions; 489 490 /** 491 * Constructs a Notification object with default values. 492 * You might want to consider using {@link Builder} instead. 493 */ 494 public Notification() 495 { 496 this.when = System.currentTimeMillis(); 497 this.priority = PRIORITY_DEFAULT; 498 } 499 500 /** 501 * @hide 502 */ 503 public Notification(Context context, int icon, CharSequence tickerText, long when, 504 CharSequence contentTitle, CharSequence contentText, Intent contentIntent) 505 { 506 this.when = when; 507 this.icon = icon; 508 this.tickerText = tickerText; 509 setLatestEventInfo(context, contentTitle, contentText, 510 PendingIntent.getActivity(context, 0, contentIntent, 0)); 511 } 512 513 /** 514 * Constructs a Notification object with the information needed to 515 * have a status bar icon without the standard expanded view. 516 * 517 * @param icon The resource id of the icon to put in the status bar. 518 * @param tickerText The text that flows by in the status bar when the notification first 519 * activates. 520 * @param when The time to show in the time field. In the System.currentTimeMillis 521 * timebase. 522 * 523 * @deprecated Use {@link Builder} instead. 524 */ 525 @Deprecated 526 public Notification(int icon, CharSequence tickerText, long when) 527 { 528 this.icon = icon; 529 this.tickerText = tickerText; 530 this.when = when; 531 } 532 533 /** 534 * Unflatten the notification from a parcel. 535 */ 536 public Notification(Parcel parcel) 537 { 538 int version = parcel.readInt(); 539 540 when = parcel.readLong(); 541 icon = parcel.readInt(); 542 number = parcel.readInt(); 543 if (parcel.readInt() != 0) { 544 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel); 545 } 546 if (parcel.readInt() != 0) { 547 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel); 548 } 549 if (parcel.readInt() != 0) { 550 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); 551 } 552 if (parcel.readInt() != 0) { 553 tickerView = RemoteViews.CREATOR.createFromParcel(parcel); 554 } 555 if (parcel.readInt() != 0) { 556 contentView = RemoteViews.CREATOR.createFromParcel(parcel); 557 } 558 if (parcel.readInt() != 0) { 559 largeIcon = Bitmap.CREATOR.createFromParcel(parcel); 560 } 561 defaults = parcel.readInt(); 562 flags = parcel.readInt(); 563 if (parcel.readInt() != 0) { 564 sound = Uri.CREATOR.createFromParcel(parcel); 565 } 566 567 audioStreamType = parcel.readInt(); 568 vibrate = parcel.createLongArray(); 569 ledARGB = parcel.readInt(); 570 ledOnMS = parcel.readInt(); 571 ledOffMS = parcel.readInt(); 572 iconLevel = parcel.readInt(); 573 574 if (parcel.readInt() != 0) { 575 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel); 576 } 577 578 priority = parcel.readInt(); 579 580 kind = parcel.createStringArray(); // may set kind to null 581 582 if (parcel.readInt() != 0) { 583 extras = parcel.readBundle(); 584 } 585 586 actions = parcel.createTypedArray(Action.CREATOR); 587 if (parcel.readInt() != 0) { 588 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel); 589 } 590 } 591 592 @Override 593 public Notification clone() { 594 Notification that = new Notification(); 595 596 that.when = this.when; 597 that.icon = this.icon; 598 that.number = this.number; 599 600 // PendingIntents are global, so there's no reason (or way) to clone them. 601 that.contentIntent = this.contentIntent; 602 that.deleteIntent = this.deleteIntent; 603 that.fullScreenIntent = this.fullScreenIntent; 604 605 if (this.tickerText != null) { 606 that.tickerText = this.tickerText.toString(); 607 } 608 if (this.tickerView != null) { 609 that.tickerView = this.tickerView.clone(); 610 } 611 if (this.contentView != null) { 612 that.contentView = this.contentView.clone(); 613 } 614 if (this.largeIcon != null) { 615 that.largeIcon = Bitmap.createBitmap(this.largeIcon); 616 } 617 that.iconLevel = this.iconLevel; 618 that.sound = this.sound; // android.net.Uri is immutable 619 that.audioStreamType = this.audioStreamType; 620 621 final long[] vibrate = this.vibrate; 622 if (vibrate != null) { 623 final int N = vibrate.length; 624 final long[] vib = that.vibrate = new long[N]; 625 System.arraycopy(vibrate, 0, vib, 0, N); 626 } 627 628 that.ledARGB = this.ledARGB; 629 that.ledOnMS = this.ledOnMS; 630 that.ledOffMS = this.ledOffMS; 631 that.defaults = this.defaults; 632 633 that.flags = this.flags; 634 635 that.priority = this.priority; 636 637 final String[] thiskind = this.kind; 638 if (thiskind != null) { 639 final int N = thiskind.length; 640 final String[] thatkind = that.kind = new String[N]; 641 System.arraycopy(thiskind, 0, thatkind, 0, N); 642 } 643 644 if (this.extras != null) { 645 that.extras = new Bundle(this.extras); 646 647 } 648 649 that.actions = new Action[this.actions.length]; 650 for(int i=0; i<this.actions.length; i++) { 651 that.actions[i] = this.actions[i].clone(); 652 } 653 if (this.bigContentView != null) { 654 that.bigContentView = this.bigContentView.clone(); 655 } 656 657 return that; 658 } 659 660 public int describeContents() { 661 return 0; 662 } 663 664 /** 665 * Flatten this notification from a parcel. 666 */ 667 public void writeToParcel(Parcel parcel, int flags) 668 { 669 parcel.writeInt(1); 670 671 parcel.writeLong(when); 672 parcel.writeInt(icon); 673 parcel.writeInt(number); 674 if (contentIntent != null) { 675 parcel.writeInt(1); 676 contentIntent.writeToParcel(parcel, 0); 677 } else { 678 parcel.writeInt(0); 679 } 680 if (deleteIntent != null) { 681 parcel.writeInt(1); 682 deleteIntent.writeToParcel(parcel, 0); 683 } else { 684 parcel.writeInt(0); 685 } 686 if (tickerText != null) { 687 parcel.writeInt(1); 688 TextUtils.writeToParcel(tickerText, parcel, flags); 689 } else { 690 parcel.writeInt(0); 691 } 692 if (tickerView != null) { 693 parcel.writeInt(1); 694 tickerView.writeToParcel(parcel, 0); 695 } else { 696 parcel.writeInt(0); 697 } 698 if (contentView != null) { 699 parcel.writeInt(1); 700 contentView.writeToParcel(parcel, 0); 701 } else { 702 parcel.writeInt(0); 703 } 704 if (largeIcon != null) { 705 parcel.writeInt(1); 706 largeIcon.writeToParcel(parcel, 0); 707 } else { 708 parcel.writeInt(0); 709 } 710 711 parcel.writeInt(defaults); 712 parcel.writeInt(this.flags); 713 714 if (sound != null) { 715 parcel.writeInt(1); 716 sound.writeToParcel(parcel, 0); 717 } else { 718 parcel.writeInt(0); 719 } 720 parcel.writeInt(audioStreamType); 721 parcel.writeLongArray(vibrate); 722 parcel.writeInt(ledARGB); 723 parcel.writeInt(ledOnMS); 724 parcel.writeInt(ledOffMS); 725 parcel.writeInt(iconLevel); 726 727 if (fullScreenIntent != null) { 728 parcel.writeInt(1); 729 fullScreenIntent.writeToParcel(parcel, 0); 730 } else { 731 parcel.writeInt(0); 732 } 733 734 parcel.writeInt(priority); 735 736 parcel.writeStringArray(kind); // ok for null 737 738 if (extras != null) { 739 parcel.writeInt(1); 740 extras.writeToParcel(parcel, 0); 741 } else { 742 parcel.writeInt(0); 743 } 744 745 parcel.writeTypedArray(actions, 0); 746 747 if (bigContentView != null) { 748 parcel.writeInt(1); 749 bigContentView.writeToParcel(parcel, 0); 750 } else { 751 parcel.writeInt(0); 752 } 753 } 754 755 /** 756 * Parcelable.Creator that instantiates Notification objects 757 */ 758 public static final Parcelable.Creator<Notification> CREATOR 759 = new Parcelable.Creator<Notification>() 760 { 761 public Notification createFromParcel(Parcel parcel) 762 { 763 return new Notification(parcel); 764 } 765 766 public Notification[] newArray(int size) 767 { 768 return new Notification[size]; 769 } 770 }; 771 772 /** 773 * Sets the {@link #contentView} field to be a view with the standard "Latest Event" 774 * layout. 775 * 776 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields 777 * in the view.</p> 778 * @param context The context for your application / activity. 779 * @param contentTitle The title that goes in the expanded entry. 780 * @param contentText The text that goes in the expanded entry. 781 * @param contentIntent The intent to launch when the user clicks the expanded notification. 782 * If this is an activity, it must include the 783 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires 784 * that you take care of task management as described in the 785 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back 786 * Stack</a> document. 787 * 788 * @deprecated Use {@link Builder} instead. 789 */ 790 @Deprecated 791 public void setLatestEventInfo(Context context, 792 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) { 793 // TODO: rewrite this to use Builder 794 RemoteViews contentView = new RemoteViews(context.getPackageName(), 795 R.layout.notification_template_base); 796 if (this.icon != 0) { 797 contentView.setImageViewResource(R.id.icon, this.icon); 798 } 799 if (contentTitle != null) { 800 contentView.setTextViewText(R.id.title, contentTitle); 801 } 802 if (contentText != null) { 803 contentView.setTextViewText(R.id.text, contentText); 804 } 805 if (this.when != 0) { 806 contentView.setViewVisibility(R.id.time, View.VISIBLE); 807 contentView.setLong(R.id.time, "setTime", when); 808 } 809 810 this.contentView = contentView; 811 this.contentIntent = contentIntent; 812 } 813 814 @Override 815 public String toString() { 816 StringBuilder sb = new StringBuilder(); 817 sb.append("Notification(pri="); 818 sb.append(priority); 819 sb.append(" contentView="); 820 if (contentView != null) { 821 sb.append(contentView.getPackage()); 822 sb.append("/0x"); 823 sb.append(Integer.toHexString(contentView.getLayoutId())); 824 } else { 825 sb.append("null"); 826 } 827 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below 828 sb.append(" vibrate="); 829 if (this.vibrate != null) { 830 int N = this.vibrate.length-1; 831 sb.append("["); 832 for (int i=0; i<N; i++) { 833 sb.append(this.vibrate[i]); 834 sb.append(','); 835 } 836 if (N != -1) { 837 sb.append(this.vibrate[N]); 838 } 839 sb.append("]"); 840 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) { 841 sb.append("default"); 842 } else { 843 sb.append("null"); 844 } 845 sb.append(" sound="); 846 if (this.sound != null) { 847 sb.append(this.sound.toString()); 848 } else if ((this.defaults & DEFAULT_SOUND) != 0) { 849 sb.append("default"); 850 } else { 851 sb.append("null"); 852 } 853 sb.append(" defaults=0x"); 854 sb.append(Integer.toHexString(this.defaults)); 855 sb.append(" flags=0x"); 856 sb.append(Integer.toHexString(this.flags)); 857 sb.append(" kind=["); 858 if (this.kind == null) { 859 sb.append("null"); 860 } else { 861 for (int i=0; i<this.kind.length; i++) { 862 if (i>0) sb.append(","); 863 sb.append(this.kind[i]); 864 } 865 } 866 sb.append("]"); 867 if (actions != null) { 868 sb.append(" "); 869 sb.append(actions.length); 870 sb.append(" action"); 871 if (actions.length > 1) sb.append("s"); 872 } 873 sb.append(")"); 874 return sb.toString(); 875 } 876 877 /** 878 * Builder class for {@link Notification} objects. 879 * 880 * Provides a convenient way to set the various fields of a {@link Notification} and generate 881 * content views using the platform's notification layout template. 882 * 883 * Example: 884 * 885 * <pre class="prettyprint"> 886 * Notification noti = new Notification.Builder() 887 * .setContentTitle("New mail from " + sender.toString()) 888 * .setContentText(subject) 889 * .setSmallIcon(R.drawable.new_mail) 890 * .setLargeIcon(aBitmap) 891 * .build(); 892 * </pre> 893 */ 894 public static class Builder { 895 private Context mContext; 896 897 private long mWhen; 898 private int mSmallIcon; 899 private int mSmallIconLevel; 900 private int mNumber; 901 private CharSequence mContentTitle; 902 private CharSequence mContentText; 903 private CharSequence mContentInfo; 904 private CharSequence mSubText; 905 private PendingIntent mContentIntent; 906 private RemoteViews mContentView; 907 private PendingIntent mDeleteIntent; 908 private PendingIntent mFullScreenIntent; 909 private CharSequence mTickerText; 910 private RemoteViews mTickerView; 911 private Bitmap mLargeIcon; 912 private Uri mSound; 913 private int mAudioStreamType; 914 private long[] mVibrate; 915 private int mLedArgb; 916 private int mLedOnMs; 917 private int mLedOffMs; 918 private int mDefaults; 919 private int mFlags; 920 private int mProgressMax; 921 private int mProgress; 922 private boolean mProgressIndeterminate; 923 private ArrayList<String> mKindList = new ArrayList<String>(1); 924 private Bundle mExtras; 925 private int mPriority; 926 private ArrayList<Action> mActions = new ArrayList<Action>(3); 927 private boolean mUseChronometer; 928 private Style mStyle; 929 930 /** 931 * Constructs a new Builder with the defaults: 932 * 933 934 * <table> 935 * <tr><th align=right>priority</th> 936 * <td>{@link #PRIORITY_DEFAULT}</td></tr> 937 * <tr><th align=right>when</th> 938 * <td>now ({@link System#currentTimeMillis()})</td></tr> 939 * <tr><th align=right>audio stream</th> 940 * <td>{@link #STREAM_DEFAULT}</td></tr> 941 * </table> 942 * 943 944 * @param context 945 * A {@link Context} that will be used by the Builder to construct the 946 * RemoteViews. The Context will not be held past the lifetime of this Builder 947 * object. 948 */ 949 public Builder(Context context) { 950 mContext = context; 951 952 // Set defaults to match the defaults of a Notification 953 mWhen = System.currentTimeMillis(); 954 mAudioStreamType = STREAM_DEFAULT; 955 mPriority = PRIORITY_DEFAULT; 956 } 957 958 /** 959 * Add a timestamp pertaining to the notification (usually the time the event occurred). 960 * 961 962 * @see Notification#when 963 */ 964 public Builder setWhen(long when) { 965 mWhen = when; 966 return this; 967 } 968 969 /** 970 * Show the {@link Notification#when} field as a countdown (or count-up) timer instead of a timestamp. 971 * 972 * @see Notification#when 973 */ 974 public Builder setUsesChronometer(boolean b) { 975 mUseChronometer = b; 976 return this; 977 } 978 979 /** 980 * Set the small icon resource, which will be used to represent the notification in the 981 * status bar. 982 * 983 984 * The platform template for the expanded view will draw this icon in the left, unless a 985 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small 986 * icon will be moved to the right-hand side. 987 * 988 989 * @param icon 990 * A resource ID in the application's package of the drawable to use. 991 * @see Notification#icon 992 */ 993 public Builder setSmallIcon(int icon) { 994 mSmallIcon = icon; 995 return this; 996 } 997 998 /** 999 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional 1000 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable 1001 * LevelListDrawable}. 1002 * 1003 * @param icon A resource ID in the application's package of the drawable to use. 1004 * @param level The level to use for the icon. 1005 * 1006 * @see Notification#icon 1007 * @see Notification#iconLevel 1008 */ 1009 public Builder setSmallIcon(int icon, int level) { 1010 mSmallIcon = icon; 1011 mSmallIconLevel = level; 1012 return this; 1013 } 1014 1015 /** 1016 * Set the first line of text in the platform notification template. 1017 */ 1018 public Builder setContentTitle(CharSequence title) { 1019 mContentTitle = title; 1020 return this; 1021 } 1022 1023 /** 1024 * Set the second line of text in the platform notification template. 1025 */ 1026 public Builder setContentText(CharSequence text) { 1027 mContentText = text; 1028 return this; 1029 } 1030 1031 /** 1032 * Set the third line of text in the platform notification template. 1033 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the same location in the standard template. 1034 */ 1035 public Builder setSubText(CharSequence text) { 1036 mSubText = text; 1037 return this; 1038 } 1039 1040 /** 1041 * Set the large number at the right-hand side of the notification. This is 1042 * equivalent to setContentInfo, although it might show the number in a different 1043 * font size for readability. 1044 */ 1045 public Builder setNumber(int number) { 1046 mNumber = number; 1047 return this; 1048 } 1049 1050 /** 1051 * A small piece of additional information pertaining to this notification. 1052 * 1053 * The platform template will draw this on the last line of the notification, at the far 1054 * right (to the right of a smallIcon if it has been placed there). 1055 */ 1056 public Builder setContentInfo(CharSequence info) { 1057 mContentInfo = info; 1058 return this; 1059 } 1060 1061 /** 1062 * Set the progress this notification represents. 1063 * 1064 * The platform template will represent this using a {@link ProgressBar}. 1065 */ 1066 public Builder setProgress(int max, int progress, boolean indeterminate) { 1067 mProgressMax = max; 1068 mProgress = progress; 1069 mProgressIndeterminate = indeterminate; 1070 return this; 1071 } 1072 1073 /** 1074 * Supply a custom RemoteViews to use instead of the platform template. 1075 * 1076 * @see Notification#contentView 1077 */ 1078 public Builder setContent(RemoteViews views) { 1079 mContentView = views; 1080 return this; 1081 } 1082 1083 /** 1084 * Supply a {@link PendingIntent} to be sent when the notification is clicked. 1085 * 1086 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you 1087 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use 1088 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)} 1089 * to assign PendingIntents to individual views in that custom layout (i.e., to create 1090 * clickable buttons inside the notification view). 1091 * 1092 * @see Notification#contentIntent Notification.contentIntent 1093 */ 1094 public Builder setContentIntent(PendingIntent intent) { 1095 mContentIntent = intent; 1096 return this; 1097 } 1098 1099 /** 1100 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user. 1101 * 1102 * @see Notification#deleteIntent 1103 */ 1104 public Builder setDeleteIntent(PendingIntent intent) { 1105 mDeleteIntent = intent; 1106 return this; 1107 } 1108 1109 /** 1110 * An intent to launch instead of posting the notification to the status bar. 1111 * Only for use with extremely high-priority notifications demanding the user's 1112 * <strong>immediate</strong> attention, such as an incoming phone call or 1113 * alarm clock that the user has explicitly set to a particular time. 1114 * If this facility is used for something else, please give the user an option 1115 * to turn it off and use a normal notification, as this can be extremely 1116 * disruptive. 1117 * 1118 * @param intent The pending intent to launch. 1119 * @param highPriority Passing true will cause this notification to be sent 1120 * even if other notifications are suppressed. 1121 * 1122 * @see Notification#fullScreenIntent 1123 */ 1124 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { 1125 mFullScreenIntent = intent; 1126 setFlag(FLAG_HIGH_PRIORITY, highPriority); 1127 return this; 1128 } 1129 1130 /** 1131 * Set the "ticker" text which is displayed in the status bar when the notification first 1132 * arrives. 1133 * 1134 * @see Notification#tickerText 1135 */ 1136 public Builder setTicker(CharSequence tickerText) { 1137 mTickerText = tickerText; 1138 return this; 1139 } 1140 1141 /** 1142 * Set the text that is displayed in the status bar when the notification first 1143 * arrives, and also a RemoteViews object that may be displayed instead on some 1144 * devices. 1145 * 1146 * @see Notification#tickerText 1147 * @see Notification#tickerView 1148 */ 1149 public Builder setTicker(CharSequence tickerText, RemoteViews views) { 1150 mTickerText = tickerText; 1151 mTickerView = views; 1152 return this; 1153 } 1154 1155 /** 1156 * Add a large icon to the notification (and the ticker on some devices). 1157 * 1158 * In the platform template, this image will be shown on the left of the notification view 1159 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side). 1160 * 1161 * @see Notification#largeIcon 1162 */ 1163 public Builder setLargeIcon(Bitmap icon) { 1164 mLargeIcon = icon; 1165 return this; 1166 } 1167 1168 /** 1169 * Set the sound to play. 1170 * 1171 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications. 1172 * 1173 * @see Notification#sound 1174 */ 1175 public Builder setSound(Uri sound) { 1176 mSound = sound; 1177 mAudioStreamType = STREAM_DEFAULT; 1178 return this; 1179 } 1180 1181 /** 1182 * Set the sound to play, along with a specific stream on which to play it. 1183 * 1184 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants. 1185 * 1186 * @see Notification#sound 1187 */ 1188 public Builder setSound(Uri sound, int streamType) { 1189 mSound = sound; 1190 mAudioStreamType = streamType; 1191 return this; 1192 } 1193 1194 /** 1195 * Set the vibration pattern to use. 1196 * 1197 1198 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the 1199 * <code>pattern</code> parameter. 1200 * 1201 1202 * @see Notification#vibrate 1203 */ 1204 public Builder setVibrate(long[] pattern) { 1205 mVibrate = pattern; 1206 return this; 1207 } 1208 1209 /** 1210 * Set the desired color for the indicator LED on the device, as well as the 1211 * blink duty cycle (specified in milliseconds). 1212 * 1213 1214 * Not all devices will honor all (or even any) of these values. 1215 * 1216 1217 * @see Notification#ledARGB 1218 * @see Notification#ledOnMS 1219 * @see Notification#ledOffMS 1220 */ 1221 public Builder setLights(int argb, int onMs, int offMs) { 1222 mLedArgb = argb; 1223 mLedOnMs = onMs; 1224 mLedOffMs = offMs; 1225 return this; 1226 } 1227 1228 /** 1229 * Set whether this is an "ongoing" notification. 1230 * 1231 1232 * Ongoing notifications cannot be dismissed by the user, so your application or service 1233 * must take care of canceling them. 1234 * 1235 1236 * They are typically used to indicate a background task that the user is actively engaged 1237 * with (e.g., playing music) or is pending in some way and therefore occupying the device 1238 * (e.g., a file download, sync operation, active network connection). 1239 * 1240 1241 * @see Notification#FLAG_ONGOING_EVENT 1242 * @see Service#setForeground(boolean) 1243 */ 1244 public Builder setOngoing(boolean ongoing) { 1245 setFlag(FLAG_ONGOING_EVENT, ongoing); 1246 return this; 1247 } 1248 1249 /** 1250 * Set this flag if you would only like the sound, vibrate 1251 * and ticker to be played if the notification is not already showing. 1252 * 1253 * @see Notification#FLAG_ONLY_ALERT_ONCE 1254 */ 1255 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { 1256 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); 1257 return this; 1258 } 1259 1260 /** 1261 * Make this notification automatically dismissed when the user touches it. The 1262 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens. 1263 * 1264 * @see Notification#FLAG_AUTO_CANCEL 1265 */ 1266 public Builder setAutoCancel(boolean autoCancel) { 1267 setFlag(FLAG_AUTO_CANCEL, autoCancel); 1268 return this; 1269 } 1270 1271 /** 1272 * Set which notification properties will be inherited from system defaults. 1273 * <p> 1274 * The value should be one or more of the following fields combined with 1275 * bitwise-or: 1276 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. 1277 * <p> 1278 * For all default values, use {@link #DEFAULT_ALL}. 1279 */ 1280 public Builder setDefaults(int defaults) { 1281 mDefaults = defaults; 1282 return this; 1283 } 1284 1285 /** 1286 * Set the priority of this notification. 1287 * 1288 * @see Notification#priority 1289 */ 1290 public Builder setPriority(int pri) { 1291 mPriority = pri; 1292 return this; 1293 } 1294 1295 /** 1296 * Add a kind (category) to this notification. Optional. 1297 * 1298 * @see Notification#kind 1299 */ 1300 public Builder addKind(String k) { 1301 mKindList.add(k); 1302 return this; 1303 } 1304 1305 /** 1306 * Add metadata to this notification. 1307 * 1308 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's 1309 * current contents are copied into the Notification each time {@link #build()} is 1310 * called. 1311 * 1312 * @see Notification#extras 1313 * @hide 1314 */ 1315 public Builder setExtras(Bundle bag) { 1316 mExtras = bag; 1317 return this; 1318 } 1319 1320 /** 1321 * Add an action to this notification. Actions are typically displayed by 1322 * the system as a button adjacent to the notification content. 1323 * 1324 * @param icon Resource ID of a drawable that represents the action. 1325 * @param title Text describing the action. 1326 * @param intent PendingIntent to be fired when the action is invoked. 1327 */ 1328 public Builder addAction(int icon, CharSequence title, PendingIntent intent) { 1329 mActions.add(new Action(icon, title, intent)); 1330 return this; 1331 } 1332 1333 /** 1334 * Add a rich notification style to be applied at build time. 1335 * 1336 * @param style Object responsible for modifying the notification style. 1337 */ 1338 public Builder setStyle(Style style) { 1339 if (mStyle != style) { 1340 mStyle = style; 1341 mStyle.setBuilder(this); 1342 } 1343 return this; 1344 } 1345 1346 private void setFlag(int mask, boolean value) { 1347 if (value) { 1348 mFlags |= mask; 1349 } else { 1350 mFlags &= ~mask; 1351 } 1352 } 1353 1354 private RemoteViews applyStandardTemplate(int resId) { 1355 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId); 1356 boolean hasLine3 = false; 1357 boolean hasLine2 = false; 1358 int smallIconImageViewId = R.id.icon; 1359 if (mLargeIcon != null) { 1360 contentView.setImageViewBitmap(R.id.icon, mLargeIcon); 1361 smallIconImageViewId = R.id.right_icon; 1362 } 1363 if (mSmallIcon != 0) { 1364 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon); 1365 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE); 1366 } else { 1367 contentView.setViewVisibility(smallIconImageViewId, View.GONE); 1368 } 1369 if (mContentTitle != null) { 1370 contentView.setTextViewText(R.id.title, mContentTitle); 1371 } 1372 if (mContentText != null) { 1373 contentView.setTextViewText( 1374 (mSubText != null) ? R.id.text2 : R.id.text, 1375 mContentText); 1376 hasLine3 = true; 1377 } 1378 if (mContentInfo != null) { 1379 contentView.setTextViewText(R.id.info, mContentInfo); 1380 contentView.setViewVisibility(R.id.info, View.VISIBLE); 1381 hasLine3 = true; 1382 } else if (mNumber > 0) { 1383 final int tooBig = mContext.getResources().getInteger( 1384 R.integer.status_bar_notification_info_maxnum); 1385 if (mNumber > tooBig) { 1386 contentView.setTextViewText(R.id.info, mContext.getResources().getString( 1387 R.string.status_bar_notification_info_overflow)); 1388 } else { 1389 NumberFormat f = NumberFormat.getIntegerInstance(); 1390 contentView.setTextViewText(R.id.info, f.format(mNumber)); 1391 } 1392 contentView.setViewVisibility(R.id.info, View.VISIBLE); 1393 hasLine3 = true; 1394 } else { 1395 contentView.setViewVisibility(R.id.info, View.GONE); 1396 } 1397 1398 if (mSubText != null) { 1399 contentView.setTextViewText(R.id.text, mSubText); 1400 contentView.setViewVisibility(R.id.text2, 1401 mContentText != null ? View.VISIBLE : View.GONE); 1402 } else { 1403 contentView.setViewVisibility(R.id.text2, View.GONE); 1404 if (mProgressMax != 0 || mProgressIndeterminate) { 1405 contentView.setProgressBar( 1406 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate); 1407 contentView.setViewVisibility(R.id.progress, View.VISIBLE); 1408 } else { 1409 contentView.setViewVisibility(R.id.progress, View.GONE); 1410 } 1411 } 1412 if (mWhen != 0) { 1413 if (mUseChronometer) { 1414 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE); 1415 contentView.setLong(R.id.chronometer, "setBase", 1416 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis())); 1417 contentView.setBoolean(R.id.chronometer, "setStarted", true); 1418 } else { 1419 contentView.setViewVisibility(R.id.time, View.VISIBLE); 1420 contentView.setLong(R.id.time, "setTime", mWhen); 1421 } 1422 } 1423 contentView.setViewVisibility(R.id.line3, hasLine3 ? View.VISIBLE : View.GONE); 1424 return contentView; 1425 } 1426 1427 private RemoteViews applyStandardTemplateWithActions(int layoutId) { 1428 RemoteViews big = applyStandardTemplate(layoutId); 1429 1430 int N = mActions.size(); 1431 if (N > 0) { 1432 // Log.d("Notification", "has actions: " + mContentText); 1433 big.setViewVisibility(R.id.actions, View.VISIBLE); 1434 if (N>3) N=3; 1435 for (int i=0; i<N; i++) { 1436 final RemoteViews button = generateActionButton(mActions.get(i)); 1437 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title); 1438 big.addView(R.id.actions, button); 1439 } 1440 } 1441 return big; 1442 } 1443 1444 private RemoteViews makeContentView() { 1445 if (mContentView != null) { 1446 return mContentView; 1447 } else { 1448 return applyStandardTemplate(R.layout.notification_template_base); // no more special large_icon flavor 1449 } 1450 } 1451 1452 private RemoteViews makeTickerView() { 1453 if (mTickerView != null) { 1454 return mTickerView; 1455 } else { 1456 if (mContentView == null) { 1457 return applyStandardTemplate(mLargeIcon == null 1458 ? R.layout.status_bar_latest_event_ticker 1459 : R.layout.status_bar_latest_event_ticker_large_icon); 1460 } else { 1461 return null; 1462 } 1463 } 1464 } 1465 1466 private RemoteViews makeBigContentView() { 1467 if (mActions.size() == 0) return null; 1468 1469 return applyStandardTemplateWithActions(R.layout.notification_template_big_base); 1470 } 1471 1472 private RemoteViews generateActionButton(Action action) { 1473 RemoteViews button = new RemoteViews(mContext.getPackageName(), R.layout.notification_action); 1474 button.setTextViewCompoundDrawables(R.id.action0, action.icon, 0, 0, 0); 1475 button.setTextViewText(R.id.action0, action.title); 1476 button.setOnClickPendingIntent(R.id.action0, action.actionIntent); 1477 button.setContentDescription(R.id.action0, action.title); 1478 return button; 1479 } 1480 1481 /** 1482 * Apply the unstyled operations and return a new {@link Notification} object. 1483 */ 1484 private Notification buildUnstyled() { 1485 Notification n = new Notification(); 1486 n.when = mWhen; 1487 n.icon = mSmallIcon; 1488 n.iconLevel = mSmallIconLevel; 1489 n.number = mNumber; 1490 n.contentView = makeContentView(); 1491 n.contentIntent = mContentIntent; 1492 n.deleteIntent = mDeleteIntent; 1493 n.fullScreenIntent = mFullScreenIntent; 1494 n.tickerText = mTickerText; 1495 n.tickerView = makeTickerView(); 1496 n.largeIcon = mLargeIcon; 1497 n.sound = mSound; 1498 n.audioStreamType = mAudioStreamType; 1499 n.vibrate = mVibrate; 1500 n.ledARGB = mLedArgb; 1501 n.ledOnMS = mLedOnMs; 1502 n.ledOffMS = mLedOffMs; 1503 n.defaults = mDefaults; 1504 n.flags = mFlags; 1505 n.bigContentView = makeBigContentView(); 1506 if (mLedOnMs != 0 && mLedOffMs != 0) { 1507 n.flags |= FLAG_SHOW_LIGHTS; 1508 } 1509 if ((mDefaults & DEFAULT_LIGHTS) != 0) { 1510 n.flags |= FLAG_SHOW_LIGHTS; 1511 } 1512 if (mKindList.size() > 0) { 1513 n.kind = new String[mKindList.size()]; 1514 mKindList.toArray(n.kind); 1515 } else { 1516 n.kind = null; 1517 } 1518 n.priority = mPriority; 1519 n.extras = mExtras != null ? new Bundle(mExtras) : null; 1520 if (mActions.size() > 0) { 1521 n.actions = new Action[mActions.size()]; 1522 mActions.toArray(n.actions); 1523 } 1524 return n; 1525 } 1526 1527 /** 1528 * @deprecated Use {@link #build()} instead. 1529 */ 1530 @Deprecated 1531 public Notification getNotification() { 1532 return build(); 1533 } 1534 1535 /** 1536 * Combine all of the options that have been set and return a new {@link Notification} 1537 * object. 1538 */ 1539 public Notification build() { 1540 if (mStyle != null) { 1541 return mStyle.build(); 1542 } else { 1543 return buildUnstyled(); 1544 } 1545 } 1546 } 1547 1548 1549 /** 1550 * An object that can apply a rich notification style to a {@link Notification.Builder} 1551 * object. 1552 */ 1553 public static abstract class Style 1554 { 1555 private CharSequence mBigContentTitle; 1556 private CharSequence mSummaryText = null; 1557 1558 protected Builder mBuilder; 1559 1560 /** 1561 * Overrides ContentTitle in the big form of the template. 1562 * This defaults to the value passed to setContentTitle(). 1563 */ 1564 protected void internalSetBigContentTitle(CharSequence title) { 1565 mBigContentTitle = title; 1566 } 1567 1568 /** 1569 * Set the first line of text after the detail section in the big form of the template. 1570 */ 1571 protected void internalSetSummaryText(CharSequence cs) { 1572 mSummaryText = cs; 1573 } 1574 1575 public void setBuilder(Builder builder) { 1576 if (mBuilder != builder) { 1577 mBuilder = builder; 1578 mBuilder.setStyle(this); 1579 } 1580 } 1581 1582 protected void checkBuilder() { 1583 if (mBuilder == null) { 1584 throw new IllegalArgumentException("Style requires a valid Builder object"); 1585 } 1586 } 1587 1588 protected RemoteViews getStandardView(int layoutId) { 1589 checkBuilder(); 1590 1591 if (mBigContentTitle != null) { 1592 mBuilder.setContentTitle(mBigContentTitle); 1593 } 1594 1595 if (mBuilder.mSubText == null) { 1596 mBuilder.setContentText(null); 1597 } 1598 1599 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId); 1600 1601 if (mBuilder.mSubText == null) { 1602 contentView.setViewVisibility(R.id.line3, View.GONE); 1603 } 1604 1605 if (mBigContentTitle != null && mBigContentTitle.equals("")) { 1606 contentView.setViewVisibility(R.id.line1, View.GONE); 1607 } 1608 1609 if (mSummaryText != null && !mSummaryText.equals("")) { 1610 contentView.setViewVisibility(R.id.overflow_title, View.VISIBLE); 1611 contentView.setTextViewText(R.id.overflow_title, mSummaryText); 1612 } 1613 1614 return contentView; 1615 } 1616 1617 public abstract Notification build(); 1618 } 1619 1620 /** 1621 * Helper class for generating large-format notifications that include a large image attachment. 1622 * 1623 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so: 1624 * <pre class="prettyprint"> 1625 * Notification noti = new Notification.BigPictureStyle( 1626 * new Notification.Builder() 1627 * .setContentTitle("New photo from " + sender.toString()) 1628 * .setContentText(subject) 1629 * .setSmallIcon(R.drawable.new_post) 1630 * .setLargeIcon(aBitmap)) 1631 * .bigPicture(aBigBitmap) 1632 * .build(); 1633 * </pre> 1634 * 1635 * @see Notification#bigContentView 1636 */ 1637 public static class BigPictureStyle extends Style { 1638 private Bitmap mPicture; 1639 1640 public BigPictureStyle() { 1641 } 1642 1643 public BigPictureStyle(Builder builder) { 1644 setBuilder(builder); 1645 } 1646 1647 /** 1648 * Overrides ContentTitle in the big form of the template. 1649 * This defaults to the value passed to setContentTitle(). 1650 */ 1651 public BigPictureStyle setBigContentTitle(CharSequence title) { 1652 internalSetBigContentTitle(title); 1653 return this; 1654 } 1655 1656 /** 1657 * Set the first line of text after the detail section in the big form of the template. 1658 */ 1659 public BigPictureStyle setSummaryText(CharSequence cs) { 1660 internalSetSummaryText(cs); 1661 return this; 1662 } 1663 1664 public BigPictureStyle bigPicture(Bitmap b) { 1665 mPicture = b; 1666 return this; 1667 } 1668 1669 private RemoteViews makeBigContentView() { 1670 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture); 1671 1672 contentView.setImageViewBitmap(R.id.big_picture, mPicture); 1673 1674 return contentView; 1675 } 1676 1677 @Override 1678 public Notification build() { 1679 checkBuilder(); 1680 Notification wip = mBuilder.buildUnstyled(); 1681 wip.bigContentView = makeBigContentView(); 1682 return wip; 1683 } 1684 } 1685 1686 /** 1687 * Helper class for generating large-format notifications that include a lot of text. 1688 * 1689 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so: 1690 * <pre class="prettyprint"> 1691 * Notification noti = new Notification.BigPictureStyle( 1692 * new Notification.Builder() 1693 * .setContentTitle("New mail from " + sender.toString()) 1694 * .setContentText(subject) 1695 * .setSmallIcon(R.drawable.new_mail) 1696 * .setLargeIcon(aBitmap)) 1697 * .bigText(aVeryLongString) 1698 * .build(); 1699 * </pre> 1700 * 1701 * @see Notification#bigContentView 1702 */ 1703 public static class BigTextStyle extends Style { 1704 private CharSequence mBigText; 1705 1706 public BigTextStyle() { 1707 } 1708 1709 public BigTextStyle(Builder builder) { 1710 setBuilder(builder); 1711 } 1712 1713 /** 1714 * Overrides ContentTitle in the big form of the template. 1715 * This defaults to the value passed to setContentTitle(). 1716 */ 1717 public BigTextStyle setBigContentTitle(CharSequence title) { 1718 internalSetBigContentTitle(title); 1719 return this; 1720 } 1721 1722 /** 1723 * Set the first line of text after the detail section in the big form of the template. 1724 */ 1725 public BigTextStyle setSummaryText(CharSequence cs) { 1726 internalSetSummaryText(cs); 1727 return this; 1728 } 1729 1730 public BigTextStyle bigText(CharSequence cs) { 1731 mBigText = cs; 1732 return this; 1733 } 1734 1735 private RemoteViews makeBigContentView() { 1736 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text); 1737 contentView.setTextViewText(R.id.big_text, mBigText); 1738 contentView.setViewVisibility(R.id.big_text, View.VISIBLE); 1739 contentView.setViewVisibility(R.id.text2, View.GONE); 1740 1741 return contentView; 1742 } 1743 1744 @Override 1745 public Notification build() { 1746 checkBuilder(); 1747 Notification wip = mBuilder.buildUnstyled(); 1748 wip.bigContentView = makeBigContentView(); 1749 return wip; 1750 } 1751 } 1752 1753 /** 1754 * Helper class for generating large-format notifications that include a list of (up to 5) strings. 1755 * 1756 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so: 1757 * <pre class="prettyprint"> 1758 * Notification noti = new Notification.InboxStyle( 1759 * new Notification.Builder() 1760 * .setContentTitle("5 New mails from " + sender.toString()) 1761 * .setContentText(subject) 1762 * .setSmallIcon(R.drawable.new_mail) 1763 * .setLargeIcon(aBitmap)) 1764 * .addLine(str1) 1765 * .addLine(str2) 1766 * .setContentTitle("") 1767 * .setSummaryText("+3 more") 1768 * .build(); 1769 * </pre> 1770 * 1771 * @see Notification#bigContentView 1772 */ 1773 public static class InboxStyle extends Style { 1774 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5); 1775 1776 public InboxStyle() { 1777 } 1778 1779 public InboxStyle(Builder builder) { 1780 setBuilder(builder); 1781 } 1782 1783 /** 1784 * Overrides ContentTitle in the big form of the template. 1785 * This defaults to the value passed to setContentTitle(). 1786 */ 1787 public InboxStyle setBigContentTitle(CharSequence title) { 1788 internalSetBigContentTitle(title); 1789 return this; 1790 } 1791 1792 /** 1793 * Set the first line of text after the detail section in the big form of the template. 1794 */ 1795 public InboxStyle setSummaryText(CharSequence cs) { 1796 internalSetSummaryText(cs); 1797 return this; 1798 } 1799 1800 public InboxStyle addLine(CharSequence cs) { 1801 mTexts.add(cs); 1802 return this; 1803 } 1804 1805 private RemoteViews makeBigContentView() { 1806 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox); 1807 contentView.setViewVisibility(R.id.text2, View.GONE); 1808 1809 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3, 1810 R.id.inbox_text4}; 1811 1812 int i=0; 1813 while (i < mTexts.size() && i < rowIds.length) { 1814 CharSequence str = mTexts.get(i); 1815 if (str != null && !str.equals("")) { 1816 contentView.setViewVisibility(rowIds[i], View.VISIBLE); 1817 contentView.setTextViewText(rowIds[i], str); 1818 } 1819 i++; 1820 } 1821 1822 return contentView; 1823 } 1824 1825 @Override 1826 public Notification build() { 1827 checkBuilder(); 1828 Notification wip = mBuilder.buildUnstyled(); 1829 wip.bigContentView = makeBigContentView(); 1830 return wip; 1831 } 1832 } 1833} 1834