Notification.java revision aca4b25f479065088a798eff3b87ec46325632e3
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.app; 18 19import android.annotation.IntDef; 20import android.annotation.SdkConstant; 21import android.annotation.SdkConstant.SdkConstantType; 22import android.content.Context; 23import android.content.Intent; 24import android.content.pm.ApplicationInfo; 25import android.content.pm.PackageManager.NameNotFoundException; 26import android.graphics.Bitmap; 27import android.graphics.Canvas; 28import android.graphics.PorterDuff; 29import android.graphics.drawable.Drawable; 30import android.media.AudioAttributes; 31import android.media.AudioManager; 32import android.media.session.MediaSession; 33import android.net.Uri; 34import android.os.BadParcelableException; 35import android.os.Build; 36import android.os.Bundle; 37import android.os.Parcel; 38import android.os.Parcelable; 39import android.os.SystemClock; 40import android.os.UserHandle; 41import android.text.TextUtils; 42import android.util.Log; 43import android.util.MathUtils; 44import android.util.TypedValue; 45import android.view.Gravity; 46import android.view.View; 47import android.widget.ProgressBar; 48import android.widget.RemoteViews; 49 50import com.android.internal.R; 51import com.android.internal.util.NotificationColorUtil; 52 53import java.lang.annotation.Retention; 54import java.lang.annotation.RetentionPolicy; 55import java.lang.reflect.Constructor; 56import java.text.NumberFormat; 57import java.util.ArrayList; 58import java.util.Arrays; 59import java.util.Collections; 60import java.util.List; 61 62/** 63 * A class that represents how a persistent notification is to be presented to 64 * the user using the {@link android.app.NotificationManager}. 65 * 66 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it 67 * easier to construct Notifications.</p> 68 * 69 * <div class="special reference"> 70 * <h3>Developer Guides</h3> 71 * <p>For a guide to creating notifications, read the 72 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> 73 * developer guide.</p> 74 * </div> 75 */ 76public class Notification implements Parcelable 77{ 78 private static final String TAG = "Notification"; 79 80 /** 81 * An activity that provides a user interface for adjusting notification preferences for its 82 * containing application. Optional but recommended for apps that post 83 * {@link android.app.Notification Notifications}. 84 */ 85 @SdkConstant(SdkConstantType.INTENT_CATEGORY) 86 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES 87 = "android.intent.category.NOTIFICATION_PREFERENCES"; 88 89 /** 90 * Use all default values (where applicable). 91 */ 92 public static final int DEFAULT_ALL = ~0; 93 94 /** 95 * Use the default notification sound. This will ignore any given 96 * {@link #sound}. 97 * 98 * <p> 99 * A notification that is noisy is more likely to be presented as a heads-up notification. 100 * </p> 101 * 102 * @see #defaults 103 */ 104 105 public static final int DEFAULT_SOUND = 1; 106 107 /** 108 * Use the default notification vibrate. This will ignore any given 109 * {@link #vibrate}. Using phone vibration requires the 110 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission. 111 * 112 * <p> 113 * A notification that vibrates is more likely to be presented as a heads-up notification. 114 * </p> 115 * 116 * @see #defaults 117 */ 118 119 public static final int DEFAULT_VIBRATE = 2; 120 121 /** 122 * Use the default notification lights. This will ignore the 123 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or 124 * {@link #ledOnMS}. 125 * 126 * @see #defaults 127 */ 128 129 public static final int DEFAULT_LIGHTS = 4; 130 131 /** 132 * Maximum length of CharSequences accepted by Builder and friends. 133 * 134 * <p> 135 * Avoids spamming the system with overly large strings such as full e-mails. 136 */ 137 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024; 138 139 /** 140 * A timestamp related to this notification, in milliseconds since the epoch. 141 * 142 * Default value: {@link System#currentTimeMillis() Now}. 143 * 144 * Choose a timestamp that will be most relevant to the user. For most finite events, this 145 * corresponds to the time the event happened (or will happen, in the case of events that have 146 * yet to occur but about which the user is being informed). Indefinite events should be 147 * timestamped according to when the activity began. 148 * 149 * Some examples: 150 * 151 * <ul> 152 * <li>Notification of a new chat message should be stamped when the message was received.</li> 153 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li> 154 * <li>Notification of a completed file download should be stamped when the download finished.</li> 155 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li> 156 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time. 157 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time. 158 * </ul> 159 * 160 */ 161 public long when; 162 163 /** 164 * The resource id of a drawable to use as the icon in the status bar. 165 * This is required; notifications with an invalid icon resource will not be shown. 166 */ 167 public int icon; 168 169 /** 170 * If the icon in the status bar is to have more than one level, you can set this. Otherwise, 171 * leave it at its default value of 0. 172 * 173 * @see android.widget.ImageView#setImageLevel 174 * @see android.graphics.drawable.Drawable#setLevel 175 */ 176 public int iconLevel; 177 178 /** 179 * The number of events that this notification represents. For example, in a new mail 180 * notification, this could be the number of unread messages. 181 * 182 * The system may or may not use this field to modify the appearance of the notification. For 183 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was 184 * superimposed over the icon in the status bar. Starting with 185 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by 186 * {@link Notification.Builder} has displayed the number in the expanded notification view. 187 * 188 * If the number is 0 or negative, it is never shown. 189 */ 190 public int number; 191 192 /** 193 * The intent to execute when the expanded status entry is clicked. If 194 * this is an activity, it must include the 195 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires 196 * that you take care of task management as described in the 197 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back 198 * Stack</a> document. In particular, make sure to read the notification section 199 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling 200 * Notifications</a> for the correct ways to launch an application from a 201 * notification. 202 */ 203 public PendingIntent contentIntent; 204 205 /** 206 * The intent to execute when the notification is explicitly dismissed by the user, either with 207 * the "Clear All" button or by swiping it away individually. 208 * 209 * This probably shouldn't be launching an activity since several of those will be sent 210 * at the same time. 211 */ 212 public PendingIntent deleteIntent; 213 214 /** 215 * An intent to launch instead of posting the notification to the status bar. 216 * 217 * <p> 218 * The system UI may choose to display a heads-up notification, instead of 219 * launching this intent, while the user is using the device. 220 * </p> 221 * 222 * @see Notification.Builder#setFullScreenIntent 223 */ 224 public PendingIntent fullScreenIntent; 225 226 /** 227 * Text that summarizes this notification for accessibility services. 228 * 229 * As of the L release, this text is no longer shown on screen, but it is still useful to 230 * accessibility services (where it serves as an audible announcement of the notification's 231 * appearance). 232 * 233 * @see #tickerView 234 */ 235 public CharSequence tickerText; 236 237 /** 238 * Formerly, a view showing the {@link #tickerText}. 239 * 240 * No longer displayed in the status bar as of API 21. 241 */ 242 @Deprecated 243 public RemoteViews tickerView; 244 245 /** 246 * The view that will represent this notification in the expanded status bar. 247 */ 248 public RemoteViews contentView; 249 250 /** 251 * A large-format version of {@link #contentView}, giving the Notification an 252 * opportunity to show more detail. The system UI may choose to show this 253 * instead of the normal content view at its discretion. 254 */ 255 public RemoteViews bigContentView; 256 257 258 /** 259 * A medium-format version of {@link #contentView}, providing the Notification an 260 * opportunity to add action buttons to contentView. At its discretion, the system UI may 261 * choose to show this as a heads-up notification, which will pop up so the user can see 262 * it without leaving their current activity. 263 */ 264 public RemoteViews headsUpContentView; 265 266 /** 267 * The bitmap that may escape the bounds of the panel and bar. 268 */ 269 public Bitmap largeIcon; 270 271 /** 272 * The sound to play. 273 * 274 * <p> 275 * A notification that is noisy is more likely to be presented as a heads-up notification. 276 * </p> 277 * 278 * <p> 279 * To play the default notification sound, see {@link #defaults}. 280 * </p> 281 */ 282 public Uri sound; 283 284 /** 285 * Use this constant as the value for audioStreamType to request that 286 * the default stream type for notifications be used. Currently the 287 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}. 288 * 289 * @deprecated Use {@link #audioAttributes} instead. 290 */ 291 @Deprecated 292 public static final int STREAM_DEFAULT = -1; 293 294 /** 295 * The audio stream type to use when playing the sound. 296 * Should be one of the STREAM_ constants from 297 * {@link android.media.AudioManager}. 298 * 299 * @deprecated Use {@link #audioAttributes} instead. 300 */ 301 @Deprecated 302 public int audioStreamType = STREAM_DEFAULT; 303 304 /** 305 * The default value of {@link #audioAttributes}. 306 */ 307 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder() 308 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 309 .setUsage(AudioAttributes.USAGE_NOTIFICATION) 310 .build(); 311 312 /** 313 * The {@link AudioAttributes audio attributes} to use when playing the sound. 314 */ 315 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT; 316 317 /** 318 * The pattern with which to vibrate. 319 * 320 * <p> 321 * To vibrate the default pattern, see {@link #defaults}. 322 * </p> 323 * 324 * <p> 325 * A notification that vibrates is more likely to be presented as a heads-up notification. 326 * </p> 327 * 328 * @see android.os.Vibrator#vibrate(long[],int) 329 */ 330 public long[] vibrate; 331 332 /** 333 * The color of the led. The hardware will do its best approximation. 334 * 335 * @see #FLAG_SHOW_LIGHTS 336 * @see #flags 337 */ 338 public int ledARGB; 339 340 /** 341 * The number of milliseconds for the LED to be on while it's flashing. 342 * The hardware will do its best approximation. 343 * 344 * @see #FLAG_SHOW_LIGHTS 345 * @see #flags 346 */ 347 public int ledOnMS; 348 349 /** 350 * The number of milliseconds for the LED to be off while it's flashing. 351 * The hardware will do its best approximation. 352 * 353 * @see #FLAG_SHOW_LIGHTS 354 * @see #flags 355 */ 356 public int ledOffMS; 357 358 /** 359 * Specifies which values should be taken from the defaults. 360 * <p> 361 * To set, OR the desired from {@link #DEFAULT_SOUND}, 362 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default 363 * values, use {@link #DEFAULT_ALL}. 364 * </p> 365 */ 366 public int defaults; 367 368 /** 369 * Bit to be bitwise-ored into the {@link #flags} field that should be 370 * set if you want the LED on for this notification. 371 * <ul> 372 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB 373 * or 0 for both ledOnMS and ledOffMS.</li> 374 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li> 375 * <li>To flash the LED, pass the number of milliseconds that it should 376 * be on and off to ledOnMS and ledOffMS.</li> 377 * </ul> 378 * <p> 379 * Since hardware varies, you are not guaranteed that any of the values 380 * you pass are honored exactly. Use the system defaults (TODO) if possible 381 * because they will be set to values that work on any given hardware. 382 * <p> 383 * The alpha channel must be set for forward compatibility. 384 * 385 */ 386 public static final int FLAG_SHOW_LIGHTS = 0x00000001; 387 388 /** 389 * Bit to be bitwise-ored into the {@link #flags} field that should be 390 * set if this notification is in reference to something that is ongoing, 391 * like a phone call. It should not be set if this notification is in 392 * reference to something that happened at a particular point in time, 393 * like a missed phone call. 394 */ 395 public static final int FLAG_ONGOING_EVENT = 0x00000002; 396 397 /** 398 * Bit to be bitwise-ored into the {@link #flags} field that if set, 399 * the audio will be repeated until the notification is 400 * cancelled or the notification window is opened. 401 */ 402 public static final int FLAG_INSISTENT = 0x00000004; 403 404 /** 405 * Bit to be bitwise-ored into the {@link #flags} field that should be 406 * set if you would only like the sound, vibrate and ticker to be played 407 * if the notification was not already showing. 408 */ 409 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008; 410 411 /** 412 * Bit to be bitwise-ored into the {@link #flags} field that should be 413 * set if the notification should be canceled when it is clicked by the 414 * user. 415 416 */ 417 public static final int FLAG_AUTO_CANCEL = 0x00000010; 418 419 /** 420 * Bit to be bitwise-ored into the {@link #flags} field that should be 421 * set if the notification should not be canceled when the user clicks 422 * the Clear all button. 423 */ 424 public static final int FLAG_NO_CLEAR = 0x00000020; 425 426 /** 427 * Bit to be bitwise-ored into the {@link #flags} field that should be 428 * set if this notification represents a currently running service. This 429 * will normally be set for you by {@link Service#startForeground}. 430 */ 431 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040; 432 433 /** 434 * Obsolete flag indicating high-priority notifications; use the priority field instead. 435 * 436 * @deprecated Use {@link #priority} with a positive value. 437 */ 438 public static final int FLAG_HIGH_PRIORITY = 0x00000080; 439 440 /** 441 * Bit to be bitswise-ored into the {@link #flags} field that should be 442 * set if this notification is relevant to the current device only 443 * and it is not recommended that it bridge to other devices. 444 */ 445 public static final int FLAG_LOCAL_ONLY = 0x00000100; 446 447 /** 448 * Bit to be bitswise-ored into the {@link #flags} field that should be 449 * set if this notification is the group summary for a group of notifications. 450 * Grouped notifications may display in a cluster or stack on devices which 451 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}. 452 */ 453 public static final int FLAG_GROUP_SUMMARY = 0x00000200; 454 455 public int flags; 456 457 /** @hide */ 458 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX}) 459 @Retention(RetentionPolicy.SOURCE) 460 public @interface Priority {} 461 462 /** 463 * Default notification {@link #priority}. If your application does not prioritize its own 464 * notifications, use this value for all notifications. 465 */ 466 public static final int PRIORITY_DEFAULT = 0; 467 468 /** 469 * Lower {@link #priority}, for items that are less important. The UI may choose to show these 470 * items smaller, or at a different position in the list, compared with your app's 471 * {@link #PRIORITY_DEFAULT} items. 472 */ 473 public static final int PRIORITY_LOW = -1; 474 475 /** 476 * Lowest {@link #priority}; these items might not be shown to the user except under special 477 * circumstances, such as detailed notification logs. 478 */ 479 public static final int PRIORITY_MIN = -2; 480 481 /** 482 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to 483 * show these items larger, or at a different position in notification lists, compared with 484 * your app's {@link #PRIORITY_DEFAULT} items. 485 */ 486 public static final int PRIORITY_HIGH = 1; 487 488 /** 489 * Highest {@link #priority}, for your application's most important items that require the 490 * user's prompt attention or input. 491 */ 492 public static final int PRIORITY_MAX = 2; 493 494 /** 495 * Relative priority for this notification. 496 * 497 * Priority is an indication of how much of the user's valuable attention should be consumed by 498 * this notification. Low-priority notifications may be hidden from the user in certain 499 * situations, while the user might be interrupted for a higher-priority notification. The 500 * system will make a determination about how to interpret this priority when presenting 501 * the notification. 502 * 503 * <p> 504 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented 505 * as a heads-up notification. 506 * </p> 507 * 508 */ 509 @Priority 510 public int priority; 511 512 /** 513 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color}) 514 * to be applied by the standard Style templates when presenting this notification. 515 * 516 * The current template design constructs a colorful header image by overlaying the 517 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are 518 * ignored. 519 */ 520 public int color = COLOR_DEFAULT; 521 522 /** 523 * Special value of {@link #color} telling the system not to decorate this notification with 524 * any special color but instead use default colors when presenting this notification. 525 */ 526 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT 527 528 /** 529 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals 530 * the notification's presence and contents in untrusted situations (namely, on the secure 531 * lockscreen). 532 * 533 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always 534 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are 535 * shown in all situations, but the contents are only available if the device is unlocked for 536 * the appropriate user. 537 * 538 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification 539 * can be read even in an "insecure" context (that is, above a secure lockscreen). 540 * To modify the public version of this notification—for example, to redact some portions—see 541 * {@link Builder#setPublicVersion(Notification)}. 542 * 543 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon 544 * and ticker until the user has bypassed the lockscreen. 545 */ 546 public int visibility; 547 548 public static final int VISIBILITY_PUBLIC = 1; 549 public static final int VISIBILITY_PRIVATE = 0; 550 public static final int VISIBILITY_SECRET = -1; 551 552 /** 553 * Notification category: incoming call (voice or video) or similar synchronous communication request. 554 */ 555 public static final String CATEGORY_CALL = "call"; 556 557 /** 558 * Notification category: incoming direct message (SMS, instant message, etc.). 559 */ 560 public static final String CATEGORY_MESSAGE = "msg"; 561 562 /** 563 * Notification category: asynchronous bulk message (email). 564 */ 565 public static final String CATEGORY_EMAIL = "email"; 566 567 /** 568 * Notification category: calendar event. 569 */ 570 public static final String CATEGORY_EVENT = "event"; 571 572 /** 573 * Notification category: promotion or advertisement. 574 */ 575 public static final String CATEGORY_PROMO = "promo"; 576 577 /** 578 * Notification category: alarm or timer. 579 */ 580 public static final String CATEGORY_ALARM = "alarm"; 581 582 /** 583 * Notification category: progress of a long-running background operation. 584 */ 585 public static final String CATEGORY_PROGRESS = "progress"; 586 587 /** 588 * Notification category: social network or sharing update. 589 */ 590 public static final String CATEGORY_SOCIAL = "social"; 591 592 /** 593 * Notification category: error in background operation or authentication status. 594 */ 595 public static final String CATEGORY_ERROR = "err"; 596 597 /** 598 * Notification category: media transport control for playback. 599 */ 600 public static final String CATEGORY_TRANSPORT = "transport"; 601 602 /** 603 * Notification category: system or device status update. Reserved for system use. 604 */ 605 public static final String CATEGORY_SYSTEM = "sys"; 606 607 /** 608 * Notification category: indication of running background service. 609 */ 610 public static final String CATEGORY_SERVICE = "service"; 611 612 /** 613 * Notification category: a specific, timely recommendation for a single thing. 614 * For example, a news app might want to recommend a news story it believes the user will 615 * want to read next. 616 */ 617 public static final String CATEGORY_RECOMMENDATION = "recommendation"; 618 619 /** 620 * Notification category: ongoing information about device or contextual status. 621 */ 622 public static final String CATEGORY_STATUS = "status"; 623 624 /** 625 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants) 626 * that best describes this Notification. May be used by the system for ranking and filtering. 627 */ 628 public String category; 629 630 private String mGroupKey; 631 632 /** 633 * Get the key used to group this notification into a cluster or stack 634 * with other notifications on devices which support such rendering. 635 */ 636 public String getGroup() { 637 return mGroupKey; 638 } 639 640 private String mSortKey; 641 642 /** 643 * Get a sort key that orders this notification among other notifications from the 644 * same package. This can be useful if an external sort was already applied and an app 645 * would like to preserve this. Notifications will be sorted lexicographically using this 646 * value, although providing different priorities in addition to providing sort key may 647 * cause this value to be ignored. 648 * 649 * <p>This sort key can also be used to order members of a notification group. See 650 * {@link Builder#setGroup}. 651 * 652 * @see String#compareTo(String) 653 */ 654 public String getSortKey() { 655 return mSortKey; 656 } 657 658 /** 659 * Additional semantic data to be carried around with this Notification. 660 * <p> 661 * The extras keys defined here are intended to capture the original inputs to {@link Builder} 662 * APIs, and are intended to be used by 663 * {@link android.service.notification.NotificationListenerService} implementations to extract 664 * detailed information from notification objects. 665 */ 666 public Bundle extras = new Bundle(); 667 668 /** 669 * {@link #extras} key: this is the title of the notification, 670 * as supplied to {@link Builder#setContentTitle(CharSequence)}. 671 */ 672 public static final String EXTRA_TITLE = "android.title"; 673 674 /** 675 * {@link #extras} key: this is the title of the notification when shown in expanded form, 676 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}. 677 */ 678 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big"; 679 680 /** 681 * {@link #extras} key: this is the main text payload, as supplied to 682 * {@link Builder#setContentText(CharSequence)}. 683 */ 684 public static final String EXTRA_TEXT = "android.text"; 685 686 /** 687 * {@link #extras} key: this is a third line of text, as supplied to 688 * {@link Builder#setSubText(CharSequence)}. 689 */ 690 public static final String EXTRA_SUB_TEXT = "android.subText"; 691 692 /** 693 * {@link #extras} key: this is a small piece of additional text as supplied to 694 * {@link Builder#setContentInfo(CharSequence)}. 695 */ 696 public static final String EXTRA_INFO_TEXT = "android.infoText"; 697 698 /** 699 * {@link #extras} key: this is a line of summary information intended to be shown 700 * alongside expanded notifications, as supplied to (e.g.) 701 * {@link BigTextStyle#setSummaryText(CharSequence)}. 702 */ 703 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText"; 704 705 /** 706 * {@link #extras} key: this is the longer text shown in the big form of a 707 * {@link BigTextStyle} notification, as supplied to 708 * {@link BigTextStyle#bigText(CharSequence)}. 709 */ 710 public static final String EXTRA_BIG_TEXT = "android.bigText"; 711 712 /** 713 * {@link #extras} key: this is the resource ID of the notification's main small icon, as 714 * supplied to {@link Builder#setSmallIcon(int)}. 715 */ 716 public static final String EXTRA_SMALL_ICON = "android.icon"; 717 718 /** 719 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the 720 * notification payload, as 721 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}. 722 */ 723 public static final String EXTRA_LARGE_ICON = "android.largeIcon"; 724 725 /** 726 * {@link #extras} key: this is a bitmap to be used instead of the one from 727 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is 728 * shown in its expanded form, as supplied to 729 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}. 730 */ 731 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big"; 732 733 /** 734 * {@link #extras} key: this is the progress value supplied to 735 * {@link Builder#setProgress(int, int, boolean)}. 736 */ 737 public static final String EXTRA_PROGRESS = "android.progress"; 738 739 /** 740 * {@link #extras} key: this is the maximum value supplied to 741 * {@link Builder#setProgress(int, int, boolean)}. 742 */ 743 public static final String EXTRA_PROGRESS_MAX = "android.progressMax"; 744 745 /** 746 * {@link #extras} key: whether the progress bar is indeterminate, supplied to 747 * {@link Builder#setProgress(int, int, boolean)}. 748 */ 749 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate"; 750 751 /** 752 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically 753 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to 754 * {@link Builder#setUsesChronometer(boolean)}. 755 */ 756 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer"; 757 758 /** 759 * {@link #extras} key: whether {@link #when} should be shown, 760 * as supplied to {@link Builder#setShowWhen(boolean)}. 761 */ 762 public static final String EXTRA_SHOW_WHEN = "android.showWhen"; 763 764 /** 765 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded 766 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}. 767 */ 768 public static final String EXTRA_PICTURE = "android.picture"; 769 770 /** 771 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded 772 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}. 773 */ 774 public static final String EXTRA_TEXT_LINES = "android.textLines"; 775 776 /** 777 * {@link #extras} key: A string representing the name of the specific 778 * {@link android.app.Notification.Style} used to create this notification. 779 */ 780 public static final String EXTRA_TEMPLATE = "android.template"; 781 782 /** 783 * {@link #extras} key: An array of people that this notification relates to, specified 784 * by contacts provider contact URI. 785 */ 786 public static final String EXTRA_PEOPLE = "android.people"; 787 788 /** 789 * {@link #extras} key: used to provide hints about the appropriateness of 790 * displaying this notification as a heads-up notification. 791 * @hide 792 */ 793 public static final String EXTRA_AS_HEADS_UP = "headsup"; 794 795 /** 796 * Allow certain system-generated notifications to appear before the device is provisioned. 797 * Only available to notifications coming from the android package. 798 * @hide 799 */ 800 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup"; 801 802 /** 803 * {@link #extras} key: A 804 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed 805 * in the background when the notification is selected. The URI must point to an image stream 806 * suitable for passing into 807 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream) 808 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider 809 * URI used for this purpose must require no permissions to read the image data. 810 */ 811 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri"; 812 813 /** 814 * {@link #extras} key: A 815 * {@link android.media.session.MediaSession.Token} associated with a 816 * {@link android.app.Notification.MediaStyle} notification. 817 */ 818 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession"; 819 820 /** 821 * {@link #extras} key: the indices of actions to be shown in the compact view, 822 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}. 823 */ 824 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions"; 825 826 /** 827 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be 828 * displayed in the heads up space. 829 * 830 * <p> 831 * If this notification has a {@link #fullScreenIntent}, then it will always launch the 832 * full-screen intent when posted. 833 * </p> 834 * @hide 835 */ 836 public static final int HEADS_UP_NEVER = 0; 837 838 /** 839 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be 840 * displayed as a heads up. 841 * @hide 842 */ 843 public static final int HEADS_UP_ALLOWED = 1; 844 845 /** 846 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a 847 * good candidate for display as a heads up. 848 * @hide 849 */ 850 public static final int HEADS_UP_REQUESTED = 2; 851 852 /** 853 * Structure to encapsulate a named action that can be shown as part of this notification. 854 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is 855 * selected by the user. 856 * <p> 857 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)} 858 * or {@link Notification.Builder#addAction(Notification.Action)} 859 * to attach actions. 860 */ 861 public static class Action implements Parcelable { 862 private final Bundle mExtras; 863 private final RemoteInput[] mRemoteInputs; 864 865 /** 866 * Small icon representing the action. 867 */ 868 public int icon; 869 870 /** 871 * Title of the action. 872 */ 873 public CharSequence title; 874 875 /** 876 * Intent to send when the user invokes this action. May be null, in which case the action 877 * may be rendered in a disabled presentation by the system UI. 878 */ 879 public PendingIntent actionIntent; 880 881 private Action(Parcel in) { 882 icon = in.readInt(); 883 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 884 if (in.readInt() == 1) { 885 actionIntent = PendingIntent.CREATOR.createFromParcel(in); 886 } 887 mExtras = in.readBundle(); 888 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR); 889 } 890 891 /** 892 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}. 893 */ 894 public Action(int icon, CharSequence title, PendingIntent intent) { 895 this(icon, title, intent, new Bundle(), null); 896 } 897 898 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras, 899 RemoteInput[] remoteInputs) { 900 this.icon = icon; 901 this.title = title; 902 this.actionIntent = intent; 903 this.mExtras = extras != null ? extras : new Bundle(); 904 this.mRemoteInputs = remoteInputs; 905 } 906 907 /** 908 * Get additional metadata carried around with this Action. 909 */ 910 public Bundle getExtras() { 911 return mExtras; 912 } 913 914 /** 915 * Get the list of inputs to be collected from the user when this action is sent. 916 * May return null if no remote inputs were added. 917 */ 918 public RemoteInput[] getRemoteInputs() { 919 return mRemoteInputs; 920 } 921 922 /** 923 * Builder class for {@link Action} objects. 924 */ 925 public static final class Builder { 926 private final int mIcon; 927 private final CharSequence mTitle; 928 private final PendingIntent mIntent; 929 private final Bundle mExtras; 930 private ArrayList<RemoteInput> mRemoteInputs; 931 932 /** 933 * Construct a new builder for {@link Action} object. 934 * @param icon icon to show for this action 935 * @param title the title of the action 936 * @param intent the {@link PendingIntent} to fire when users trigger this action 937 */ 938 public Builder(int icon, CharSequence title, PendingIntent intent) { 939 this(icon, title, intent, new Bundle(), null); 940 } 941 942 /** 943 * Construct a new builder for {@link Action} object using the fields from an 944 * {@link Action}. 945 * @param action the action to read fields from. 946 */ 947 public Builder(Action action) { 948 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras), 949 action.getRemoteInputs()); 950 } 951 952 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras, 953 RemoteInput[] remoteInputs) { 954 mIcon = icon; 955 mTitle = title; 956 mIntent = intent; 957 mExtras = extras; 958 if (remoteInputs != null) { 959 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length); 960 Collections.addAll(mRemoteInputs, remoteInputs); 961 } 962 } 963 964 /** 965 * Merge additional metadata into this builder. 966 * 967 * <p>Values within the Bundle will replace existing extras values in this Builder. 968 * 969 * @see Notification.Action#extras 970 */ 971 public Builder addExtras(Bundle extras) { 972 if (extras != null) { 973 mExtras.putAll(extras); 974 } 975 return this; 976 } 977 978 /** 979 * Get the metadata Bundle used by this Builder. 980 * 981 * <p>The returned Bundle is shared with this Builder. 982 */ 983 public Bundle getExtras() { 984 return mExtras; 985 } 986 987 /** 988 * Add an input to be collected from the user when this action is sent. 989 * Response values can be retrieved from the fired intent by using the 990 * {@link RemoteInput#getResultsFromIntent} function. 991 * @param remoteInput a {@link RemoteInput} to add to the action 992 * @return this object for method chaining 993 */ 994 public Builder addRemoteInput(RemoteInput remoteInput) { 995 if (mRemoteInputs == null) { 996 mRemoteInputs = new ArrayList<RemoteInput>(); 997 } 998 mRemoteInputs.add(remoteInput); 999 return this; 1000 } 1001 1002 /** 1003 * Apply an extender to this action builder. Extenders may be used to add 1004 * metadata or change options on this builder. 1005 */ 1006 public Builder extend(Extender extender) { 1007 extender.extend(this); 1008 return this; 1009 } 1010 1011 /** 1012 * Combine all of the options that have been set and return a new {@link Action} 1013 * object. 1014 * @return the built action 1015 */ 1016 public Action build() { 1017 RemoteInput[] remoteInputs = mRemoteInputs != null 1018 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null; 1019 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs); 1020 } 1021 } 1022 1023 @Override 1024 public Action clone() { 1025 return new Action( 1026 icon, 1027 title, 1028 actionIntent, // safe to alias 1029 new Bundle(mExtras), 1030 getRemoteInputs()); 1031 } 1032 @Override 1033 public int describeContents() { 1034 return 0; 1035 } 1036 @Override 1037 public void writeToParcel(Parcel out, int flags) { 1038 out.writeInt(icon); 1039 TextUtils.writeToParcel(title, out, flags); 1040 if (actionIntent != null) { 1041 out.writeInt(1); 1042 actionIntent.writeToParcel(out, flags); 1043 } else { 1044 out.writeInt(0); 1045 } 1046 out.writeBundle(mExtras); 1047 out.writeTypedArray(mRemoteInputs, flags); 1048 } 1049 public static final Parcelable.Creator<Action> CREATOR = 1050 new Parcelable.Creator<Action>() { 1051 public Action createFromParcel(Parcel in) { 1052 return new Action(in); 1053 } 1054 public Action[] newArray(int size) { 1055 return new Action[size]; 1056 } 1057 }; 1058 1059 /** 1060 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add 1061 * metadata or change options on an action builder. 1062 */ 1063 public interface Extender { 1064 /** 1065 * Apply this extender to a notification action builder. 1066 * @param builder the builder to be modified. 1067 * @return the build object for chaining. 1068 */ 1069 public Builder extend(Builder builder); 1070 } 1071 1072 /** 1073 * Wearable extender for notification actions. To add extensions to an action, 1074 * create a new {@link android.app.Notification.Action.WearableExtender} object using 1075 * the {@code WearableExtender()} constructor and apply it to a 1076 * {@link android.app.Notification.Action.Builder} using 1077 * {@link android.app.Notification.Action.Builder#extend}. 1078 * 1079 * <pre class="prettyprint"> 1080 * Notification.Action action = new Notification.Action.Builder( 1081 * R.drawable.archive_all, "Archive all", actionIntent) 1082 * .extend(new Notification.Action.WearableExtender() 1083 * .setAvailableOffline(false)) 1084 * .build();</pre> 1085 */ 1086 public static final class WearableExtender implements Extender { 1087 /** Notification action extra which contains wearable extensions */ 1088 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS"; 1089 1090 private static final String KEY_FLAGS = "flags"; 1091 1092 // Flags bitwise-ored to mFlags 1093 private static final int FLAG_AVAILABLE_OFFLINE = 0x1; 1094 1095 // Default value for flags integer 1096 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE; 1097 1098 private int mFlags = DEFAULT_FLAGS; 1099 1100 /** 1101 * Create a {@link android.app.Notification.Action.WearableExtender} with default 1102 * options. 1103 */ 1104 public WearableExtender() { 1105 } 1106 1107 /** 1108 * Create a {@link android.app.Notification.Action.WearableExtender} by reading 1109 * wearable options present in an existing notification action. 1110 * @param action the notification action to inspect. 1111 */ 1112 public WearableExtender(Action action) { 1113 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS); 1114 if (wearableBundle != null) { 1115 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS); 1116 } 1117 } 1118 1119 /** 1120 * Apply wearable extensions to a notification action that is being built. This is 1121 * typically called by the {@link android.app.Notification.Action.Builder#extend} 1122 * method of {@link android.app.Notification.Action.Builder}. 1123 */ 1124 @Override 1125 public Action.Builder extend(Action.Builder builder) { 1126 Bundle wearableBundle = new Bundle(); 1127 1128 if (mFlags != DEFAULT_FLAGS) { 1129 wearableBundle.putInt(KEY_FLAGS, mFlags); 1130 } 1131 1132 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle); 1133 return builder; 1134 } 1135 1136 @Override 1137 public WearableExtender clone() { 1138 WearableExtender that = new WearableExtender(); 1139 that.mFlags = this.mFlags; 1140 return that; 1141 } 1142 1143 /** 1144 * Set whether this action is available when the wearable device is not connected to 1145 * a companion device. The user can still trigger this action when the wearable device is 1146 * offline, but a visual hint will indicate that the action may not be available. 1147 * Defaults to true. 1148 */ 1149 public WearableExtender setAvailableOffline(boolean availableOffline) { 1150 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline); 1151 return this; 1152 } 1153 1154 /** 1155 * Get whether this action is available when the wearable device is not connected to 1156 * a companion device. The user can still trigger this action when the wearable device is 1157 * offline, but a visual hint will indicate that the action may not be available. 1158 * Defaults to true. 1159 */ 1160 public boolean isAvailableOffline() { 1161 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0; 1162 } 1163 1164 private void setFlag(int mask, boolean value) { 1165 if (value) { 1166 mFlags |= mask; 1167 } else { 1168 mFlags &= ~mask; 1169 } 1170 } 1171 } 1172 } 1173 1174 /** 1175 * Array of all {@link Action} structures attached to this notification by 1176 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of 1177 * {@link android.service.notification.NotificationListenerService} that provide an alternative 1178 * interface for invoking actions. 1179 */ 1180 public Action[] actions; 1181 1182 /** 1183 * Replacement version of this notification whose content will be shown 1184 * in an insecure context such as atop a secure keyguard. See {@link #visibility} 1185 * and {@link #VISIBILITY_PUBLIC}. 1186 */ 1187 public Notification publicVersion; 1188 1189 /** 1190 * Constructs a Notification object with default values. 1191 * You might want to consider using {@link Builder} instead. 1192 */ 1193 public Notification() 1194 { 1195 this.when = System.currentTimeMillis(); 1196 this.priority = PRIORITY_DEFAULT; 1197 } 1198 1199 /** 1200 * @hide 1201 */ 1202 public Notification(Context context, int icon, CharSequence tickerText, long when, 1203 CharSequence contentTitle, CharSequence contentText, Intent contentIntent) 1204 { 1205 this.when = when; 1206 this.icon = icon; 1207 this.tickerText = tickerText; 1208 setLatestEventInfo(context, contentTitle, contentText, 1209 PendingIntent.getActivity(context, 0, contentIntent, 0)); 1210 } 1211 1212 /** 1213 * Constructs a Notification object with the information needed to 1214 * have a status bar icon without the standard expanded view. 1215 * 1216 * @param icon The resource id of the icon to put in the status bar. 1217 * @param tickerText The text that flows by in the status bar when the notification first 1218 * activates. 1219 * @param when The time to show in the time field. In the System.currentTimeMillis 1220 * timebase. 1221 * 1222 * @deprecated Use {@link Builder} instead. 1223 */ 1224 @Deprecated 1225 public Notification(int icon, CharSequence tickerText, long when) 1226 { 1227 this.icon = icon; 1228 this.tickerText = tickerText; 1229 this.when = when; 1230 } 1231 1232 /** 1233 * Unflatten the notification from a parcel. 1234 */ 1235 public Notification(Parcel parcel) 1236 { 1237 int version = parcel.readInt(); 1238 1239 when = parcel.readLong(); 1240 icon = parcel.readInt(); 1241 number = parcel.readInt(); 1242 if (parcel.readInt() != 0) { 1243 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel); 1244 } 1245 if (parcel.readInt() != 0) { 1246 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel); 1247 } 1248 if (parcel.readInt() != 0) { 1249 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); 1250 } 1251 if (parcel.readInt() != 0) { 1252 tickerView = RemoteViews.CREATOR.createFromParcel(parcel); 1253 } 1254 if (parcel.readInt() != 0) { 1255 contentView = RemoteViews.CREATOR.createFromParcel(parcel); 1256 } 1257 if (parcel.readInt() != 0) { 1258 largeIcon = Bitmap.CREATOR.createFromParcel(parcel); 1259 } 1260 defaults = parcel.readInt(); 1261 flags = parcel.readInt(); 1262 if (parcel.readInt() != 0) { 1263 sound = Uri.CREATOR.createFromParcel(parcel); 1264 } 1265 1266 audioStreamType = parcel.readInt(); 1267 if (parcel.readInt() != 0) { 1268 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel); 1269 } 1270 vibrate = parcel.createLongArray(); 1271 ledARGB = parcel.readInt(); 1272 ledOnMS = parcel.readInt(); 1273 ledOffMS = parcel.readInt(); 1274 iconLevel = parcel.readInt(); 1275 1276 if (parcel.readInt() != 0) { 1277 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel); 1278 } 1279 1280 priority = parcel.readInt(); 1281 1282 category = parcel.readString(); 1283 1284 mGroupKey = parcel.readString(); 1285 1286 mSortKey = parcel.readString(); 1287 1288 extras = parcel.readBundle(); // may be null 1289 1290 actions = parcel.createTypedArray(Action.CREATOR); // may be null 1291 1292 if (parcel.readInt() != 0) { 1293 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel); 1294 } 1295 1296 if (parcel.readInt() != 0) { 1297 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel); 1298 } 1299 1300 visibility = parcel.readInt(); 1301 1302 if (parcel.readInt() != 0) { 1303 publicVersion = Notification.CREATOR.createFromParcel(parcel); 1304 } 1305 1306 color = parcel.readInt(); 1307 } 1308 1309 @Override 1310 public Notification clone() { 1311 Notification that = new Notification(); 1312 cloneInto(that, true); 1313 return that; 1314 } 1315 1316 /** 1317 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members 1318 * of this into that. 1319 * @hide 1320 */ 1321 public void cloneInto(Notification that, boolean heavy) { 1322 that.when = this.when; 1323 that.icon = this.icon; 1324 that.number = this.number; 1325 1326 // PendingIntents are global, so there's no reason (or way) to clone them. 1327 that.contentIntent = this.contentIntent; 1328 that.deleteIntent = this.deleteIntent; 1329 that.fullScreenIntent = this.fullScreenIntent; 1330 1331 if (this.tickerText != null) { 1332 that.tickerText = this.tickerText.toString(); 1333 } 1334 if (heavy && this.tickerView != null) { 1335 that.tickerView = this.tickerView.clone(); 1336 } 1337 if (heavy && this.contentView != null) { 1338 that.contentView = this.contentView.clone(); 1339 } 1340 if (heavy && this.largeIcon != null) { 1341 that.largeIcon = Bitmap.createBitmap(this.largeIcon); 1342 } 1343 that.iconLevel = this.iconLevel; 1344 that.sound = this.sound; // android.net.Uri is immutable 1345 that.audioStreamType = this.audioStreamType; 1346 if (this.audioAttributes != null) { 1347 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build(); 1348 } 1349 1350 final long[] vibrate = this.vibrate; 1351 if (vibrate != null) { 1352 final int N = vibrate.length; 1353 final long[] vib = that.vibrate = new long[N]; 1354 System.arraycopy(vibrate, 0, vib, 0, N); 1355 } 1356 1357 that.ledARGB = this.ledARGB; 1358 that.ledOnMS = this.ledOnMS; 1359 that.ledOffMS = this.ledOffMS; 1360 that.defaults = this.defaults; 1361 1362 that.flags = this.flags; 1363 1364 that.priority = this.priority; 1365 1366 that.category = this.category; 1367 1368 that.mGroupKey = this.mGroupKey; 1369 1370 that.mSortKey = this.mSortKey; 1371 1372 if (this.extras != null) { 1373 try { 1374 that.extras = new Bundle(this.extras); 1375 // will unparcel 1376 that.extras.size(); 1377 } catch (BadParcelableException e) { 1378 Log.e(TAG, "could not unparcel extras from notification: " + this, e); 1379 that.extras = null; 1380 } 1381 } 1382 1383 if (this.actions != null) { 1384 that.actions = new Action[this.actions.length]; 1385 for(int i=0; i<this.actions.length; i++) { 1386 that.actions[i] = this.actions[i].clone(); 1387 } 1388 } 1389 1390 if (heavy && this.bigContentView != null) { 1391 that.bigContentView = this.bigContentView.clone(); 1392 } 1393 1394 if (heavy && this.headsUpContentView != null) { 1395 that.headsUpContentView = this.headsUpContentView.clone(); 1396 } 1397 1398 that.visibility = this.visibility; 1399 1400 if (this.publicVersion != null) { 1401 that.publicVersion = new Notification(); 1402 this.publicVersion.cloneInto(that.publicVersion, heavy); 1403 } 1404 1405 that.color = this.color; 1406 1407 if (!heavy) { 1408 that.lightenPayload(); // will clean out extras 1409 } 1410 } 1411 1412 /** 1413 * Removes heavyweight parts of the Notification object for archival or for sending to 1414 * listeners when the full contents are not necessary. 1415 * @hide 1416 */ 1417 public final void lightenPayload() { 1418 tickerView = null; 1419 contentView = null; 1420 bigContentView = null; 1421 headsUpContentView = null; 1422 largeIcon = null; 1423 if (extras != null) { 1424 extras.remove(Notification.EXTRA_LARGE_ICON); 1425 extras.remove(Notification.EXTRA_LARGE_ICON_BIG); 1426 extras.remove(Notification.EXTRA_PICTURE); 1427 extras.remove(Notification.EXTRA_BIG_TEXT); 1428 // Prevent light notifications from being rebuilt. 1429 extras.remove(Builder.EXTRA_NEEDS_REBUILD); 1430 } 1431 } 1432 1433 /** 1434 * Make sure this CharSequence is safe to put into a bundle, which basically 1435 * means it had better not be some custom Parcelable implementation. 1436 * @hide 1437 */ 1438 public static CharSequence safeCharSequence(CharSequence cs) { 1439 if (cs == null) return cs; 1440 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) { 1441 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH); 1442 } 1443 if (cs instanceof Parcelable) { 1444 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName() 1445 + " instance is a custom Parcelable and not allowed in Notification"); 1446 return cs.toString(); 1447 } 1448 1449 return cs; 1450 } 1451 1452 public int describeContents() { 1453 return 0; 1454 } 1455 1456 /** 1457 * Flatten this notification from a parcel. 1458 */ 1459 public void writeToParcel(Parcel parcel, int flags) 1460 { 1461 parcel.writeInt(1); 1462 1463 parcel.writeLong(when); 1464 parcel.writeInt(icon); 1465 parcel.writeInt(number); 1466 if (contentIntent != null) { 1467 parcel.writeInt(1); 1468 contentIntent.writeToParcel(parcel, 0); 1469 } else { 1470 parcel.writeInt(0); 1471 } 1472 if (deleteIntent != null) { 1473 parcel.writeInt(1); 1474 deleteIntent.writeToParcel(parcel, 0); 1475 } else { 1476 parcel.writeInt(0); 1477 } 1478 if (tickerText != null) { 1479 parcel.writeInt(1); 1480 TextUtils.writeToParcel(tickerText, parcel, flags); 1481 } else { 1482 parcel.writeInt(0); 1483 } 1484 if (tickerView != null) { 1485 parcel.writeInt(1); 1486 tickerView.writeToParcel(parcel, 0); 1487 } else { 1488 parcel.writeInt(0); 1489 } 1490 if (contentView != null) { 1491 parcel.writeInt(1); 1492 contentView.writeToParcel(parcel, 0); 1493 } else { 1494 parcel.writeInt(0); 1495 } 1496 if (largeIcon != null) { 1497 parcel.writeInt(1); 1498 largeIcon.writeToParcel(parcel, 0); 1499 } else { 1500 parcel.writeInt(0); 1501 } 1502 1503 parcel.writeInt(defaults); 1504 parcel.writeInt(this.flags); 1505 1506 if (sound != null) { 1507 parcel.writeInt(1); 1508 sound.writeToParcel(parcel, 0); 1509 } else { 1510 parcel.writeInt(0); 1511 } 1512 parcel.writeInt(audioStreamType); 1513 1514 if (audioAttributes != null) { 1515 parcel.writeInt(1); 1516 audioAttributes.writeToParcel(parcel, 0); 1517 } else { 1518 parcel.writeInt(0); 1519 } 1520 1521 parcel.writeLongArray(vibrate); 1522 parcel.writeInt(ledARGB); 1523 parcel.writeInt(ledOnMS); 1524 parcel.writeInt(ledOffMS); 1525 parcel.writeInt(iconLevel); 1526 1527 if (fullScreenIntent != null) { 1528 parcel.writeInt(1); 1529 fullScreenIntent.writeToParcel(parcel, 0); 1530 } else { 1531 parcel.writeInt(0); 1532 } 1533 1534 parcel.writeInt(priority); 1535 1536 parcel.writeString(category); 1537 1538 parcel.writeString(mGroupKey); 1539 1540 parcel.writeString(mSortKey); 1541 1542 parcel.writeBundle(extras); // null ok 1543 1544 parcel.writeTypedArray(actions, 0); // null ok 1545 1546 if (bigContentView != null) { 1547 parcel.writeInt(1); 1548 bigContentView.writeToParcel(parcel, 0); 1549 } else { 1550 parcel.writeInt(0); 1551 } 1552 1553 if (headsUpContentView != null) { 1554 parcel.writeInt(1); 1555 headsUpContentView.writeToParcel(parcel, 0); 1556 } else { 1557 parcel.writeInt(0); 1558 } 1559 1560 parcel.writeInt(visibility); 1561 1562 if (publicVersion != null) { 1563 parcel.writeInt(1); 1564 publicVersion.writeToParcel(parcel, 0); 1565 } else { 1566 parcel.writeInt(0); 1567 } 1568 1569 parcel.writeInt(color); 1570 } 1571 1572 /** 1573 * Parcelable.Creator that instantiates Notification objects 1574 */ 1575 public static final Parcelable.Creator<Notification> CREATOR 1576 = new Parcelable.Creator<Notification>() 1577 { 1578 public Notification createFromParcel(Parcel parcel) 1579 { 1580 return new Notification(parcel); 1581 } 1582 1583 public Notification[] newArray(int size) 1584 { 1585 return new Notification[size]; 1586 } 1587 }; 1588 1589 /** 1590 * Sets the {@link #contentView} field to be a view with the standard "Latest Event" 1591 * layout. 1592 * 1593 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields 1594 * in the view.</p> 1595 * @param context The context for your application / activity. 1596 * @param contentTitle The title that goes in the expanded entry. 1597 * @param contentText The text that goes in the expanded entry. 1598 * @param contentIntent The intent to launch when the user clicks the expanded notification. 1599 * If this is an activity, it must include the 1600 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires 1601 * that you take care of task management as described in the 1602 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back 1603 * Stack</a> document. 1604 * 1605 * @deprecated Use {@link Builder} instead. 1606 */ 1607 @Deprecated 1608 public void setLatestEventInfo(Context context, 1609 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) { 1610 Notification.Builder builder = new Notification.Builder(context); 1611 1612 // First, ensure that key pieces of information that may have been set directly 1613 // are preserved 1614 builder.setWhen(this.when); 1615 builder.setSmallIcon(this.icon); 1616 builder.setPriority(this.priority); 1617 builder.setTicker(this.tickerText); 1618 builder.setNumber(this.number); 1619 builder.setColor(this.color); 1620 builder.mFlags = this.flags; 1621 builder.setSound(this.sound, this.audioStreamType); 1622 builder.setDefaults(this.defaults); 1623 builder.setVibrate(this.vibrate); 1624 1625 // now apply the latestEventInfo fields 1626 if (contentTitle != null) { 1627 builder.setContentTitle(contentTitle); 1628 } 1629 if (contentText != null) { 1630 builder.setContentText(contentText); 1631 } 1632 builder.setContentIntent(contentIntent); 1633 builder.buildInto(this); 1634 } 1635 1636 @Override 1637 public String toString() { 1638 StringBuilder sb = new StringBuilder(); 1639 sb.append("Notification(pri="); 1640 sb.append(priority); 1641 sb.append(" contentView="); 1642 if (contentView != null) { 1643 sb.append(contentView.getPackage()); 1644 sb.append("/0x"); 1645 sb.append(Integer.toHexString(contentView.getLayoutId())); 1646 } else { 1647 sb.append("null"); 1648 } 1649 sb.append(" vibrate="); 1650 if ((this.defaults & DEFAULT_VIBRATE) != 0) { 1651 sb.append("default"); 1652 } else if (this.vibrate != null) { 1653 int N = this.vibrate.length-1; 1654 sb.append("["); 1655 for (int i=0; i<N; i++) { 1656 sb.append(this.vibrate[i]); 1657 sb.append(','); 1658 } 1659 if (N != -1) { 1660 sb.append(this.vibrate[N]); 1661 } 1662 sb.append("]"); 1663 } else { 1664 sb.append("null"); 1665 } 1666 sb.append(" sound="); 1667 if ((this.defaults & DEFAULT_SOUND) != 0) { 1668 sb.append("default"); 1669 } else if (this.sound != null) { 1670 sb.append(this.sound.toString()); 1671 } else { 1672 sb.append("null"); 1673 } 1674 sb.append(" defaults=0x"); 1675 sb.append(Integer.toHexString(this.defaults)); 1676 sb.append(" flags=0x"); 1677 sb.append(Integer.toHexString(this.flags)); 1678 sb.append(String.format(" color=0x%08x", this.color)); 1679 if (this.category != null) { 1680 sb.append(" category="); 1681 sb.append(this.category); 1682 } 1683 if (this.mGroupKey != null) { 1684 sb.append(" groupKey="); 1685 sb.append(this.mGroupKey); 1686 } 1687 if (this.mSortKey != null) { 1688 sb.append(" sortKey="); 1689 sb.append(this.mSortKey); 1690 } 1691 if (actions != null) { 1692 sb.append(" actions="); 1693 sb.append(actions.length); 1694 } 1695 sb.append(" vis="); 1696 sb.append(visibilityToString(this.visibility)); 1697 if (this.publicVersion != null) { 1698 sb.append(" publicVersion="); 1699 sb.append(publicVersion.toString()); 1700 } 1701 sb.append(")"); 1702 return sb.toString(); 1703 } 1704 1705 /** 1706 * {@hide} 1707 */ 1708 public static String visibilityToString(int vis) { 1709 switch (vis) { 1710 case VISIBILITY_PRIVATE: 1711 return "PRIVATE"; 1712 case VISIBILITY_PUBLIC: 1713 return "PUBLIC"; 1714 case VISIBILITY_SECRET: 1715 return "SECRET"; 1716 default: 1717 return "UNKNOWN(" + String.valueOf(vis) + ")"; 1718 } 1719 } 1720 1721 /** 1722 * @hide 1723 */ 1724 public boolean isValid() { 1725 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that 1726 // for legacy reasons. 1727 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW); 1728 } 1729 1730 /** 1731 * @hide 1732 */ 1733 public boolean isGroupSummary() { 1734 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0; 1735 } 1736 1737 /** 1738 * @hide 1739 */ 1740 public boolean isGroupChild() { 1741 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0; 1742 } 1743 1744 /** 1745 * Builder class for {@link Notification} objects. 1746 * 1747 * Provides a convenient way to set the various fields of a {@link Notification} and generate 1748 * content views using the platform's notification layout template. If your app supports 1749 * versions of Android as old as API level 4, you can instead use 1750 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder}, 1751 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support 1752 * library</a>. 1753 * 1754 * <p>Example: 1755 * 1756 * <pre class="prettyprint"> 1757 * Notification noti = new Notification.Builder(mContext) 1758 * .setContentTitle("New mail from " + sender.toString()) 1759 * .setContentText(subject) 1760 * .setSmallIcon(R.drawable.new_mail) 1761 * .setLargeIcon(aBitmap) 1762 * .build(); 1763 * </pre> 1764 */ 1765 public static class Builder { 1766 private static final int MAX_ACTION_BUTTONS = 3; 1767 private static final float LARGE_TEXT_SCALE = 1.3f; 1768 1769 /** 1770 * @hide 1771 */ 1772 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild"; 1773 1774 /** 1775 * @hide 1776 */ 1777 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon"; 1778 /** 1779 * @hide 1780 */ 1781 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView"; 1782 /** 1783 * @hide 1784 */ 1785 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT = 1786 "android.rebuild.contentViewActionCount"; 1787 /** 1788 * @hide 1789 */ 1790 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW 1791 = "android.rebuild.bigView"; 1792 /** 1793 * @hide 1794 */ 1795 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT 1796 = "android.rebuild.bigViewActionCount"; 1797 /** 1798 * @hide 1799 */ 1800 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW 1801 = "android.rebuild.hudView"; 1802 /** 1803 * @hide 1804 */ 1805 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT 1806 = "android.rebuild.hudViewActionCount"; 1807 1808 /** 1809 * The ApplicationInfo of the package that created the notification, used to create 1810 * a context to rebuild the notification via a Builder. 1811 * @hide 1812 */ 1813 private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO = 1814 "android.rebuild.applicationInfo"; 1815 1816 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of 1817 // memory intensive resources. 1818 private static final boolean STRIP_AND_REBUILD = true; 1819 1820 private Context mContext; 1821 1822 private long mWhen; 1823 private int mSmallIcon; 1824 private int mSmallIconLevel; 1825 private int mNumber; 1826 private CharSequence mContentTitle; 1827 private CharSequence mContentText; 1828 private CharSequence mContentInfo; 1829 private CharSequence mSubText; 1830 private PendingIntent mContentIntent; 1831 private RemoteViews mContentView; 1832 private PendingIntent mDeleteIntent; 1833 private PendingIntent mFullScreenIntent; 1834 private CharSequence mTickerText; 1835 private RemoteViews mTickerView; 1836 private Bitmap mLargeIcon; 1837 private Uri mSound; 1838 private int mAudioStreamType; 1839 private AudioAttributes mAudioAttributes; 1840 private long[] mVibrate; 1841 private int mLedArgb; 1842 private int mLedOnMs; 1843 private int mLedOffMs; 1844 private int mDefaults; 1845 private int mFlags; 1846 private int mProgressMax; 1847 private int mProgress; 1848 private boolean mProgressIndeterminate; 1849 private String mCategory; 1850 private String mGroupKey; 1851 private String mSortKey; 1852 private Bundle mExtras; 1853 private int mPriority; 1854 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS); 1855 private boolean mUseChronometer; 1856 private Style mStyle; 1857 private boolean mShowWhen = true; 1858 private int mVisibility = VISIBILITY_PRIVATE; 1859 private Notification mPublicVersion = null; 1860 private final NotificationColorUtil mColorUtil; 1861 private ArrayList<String> mPeople; 1862 private int mColor = COLOR_DEFAULT; 1863 1864 /** 1865 * Contains extras related to rebuilding during the build phase. 1866 */ 1867 private Bundle mRebuildBundle = new Bundle(); 1868 /** 1869 * Contains the notification to rebuild when this Builder is in "rebuild" mode. 1870 * Null otherwise. 1871 */ 1872 private Notification mRebuildNotification = null; 1873 1874 /** 1875 * Whether the build notification has three lines. This is used to make the top padding for 1876 * both the contracted and expanded layout consistent. 1877 * 1878 * <p> 1879 * This field is only valid during the build phase. 1880 */ 1881 private boolean mHasThreeLines; 1882 1883 /** 1884 * Constructs a new Builder with the defaults: 1885 * 1886 1887 * <table> 1888 * <tr><th align=right>priority</th> 1889 * <td>{@link #PRIORITY_DEFAULT}</td></tr> 1890 * <tr><th align=right>when</th> 1891 * <td>now ({@link System#currentTimeMillis()})</td></tr> 1892 * <tr><th align=right>audio stream</th> 1893 * <td>{@link #STREAM_DEFAULT}</td></tr> 1894 * </table> 1895 * 1896 1897 * @param context 1898 * A {@link Context} that will be used by the Builder to construct the 1899 * RemoteViews. The Context will not be held past the lifetime of this Builder 1900 * object. 1901 */ 1902 public Builder(Context context) { 1903 /* 1904 * Important compatibility note! 1905 * Some apps out in the wild create a Notification.Builder in their Activity subclass 1906 * constructor for later use. At this point Activities - themselves subclasses of 1907 * ContextWrapper - do not have their inner Context populated yet. This means that 1908 * any calls to Context methods from within this constructor can cause NPEs in existing 1909 * apps. Any data populated from mContext should therefore be populated lazily to 1910 * preserve compatibility. 1911 */ 1912 mContext = context; 1913 1914 // Set defaults to match the defaults of a Notification 1915 mWhen = System.currentTimeMillis(); 1916 mAudioStreamType = STREAM_DEFAULT; 1917 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT; 1918 mPriority = PRIORITY_DEFAULT; 1919 mPeople = new ArrayList<String>(); 1920 1921 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.L ? 1922 NotificationColorUtil.getInstance(mContext) : null; 1923 } 1924 1925 /** 1926 * Creates a Builder for rebuilding the given Notification. 1927 * <p> 1928 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'. 1929 */ 1930 private Builder(Context context, Notification n) { 1931 this(context); 1932 mRebuildNotification = n; 1933 restoreFromNotification(n); 1934 1935 Style style = null; 1936 Bundle extras = n.extras; 1937 String templateClass = extras.getString(EXTRA_TEMPLATE); 1938 if (!TextUtils.isEmpty(templateClass)) { 1939 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass); 1940 if (styleClass == null) { 1941 Log.d(TAG, "Unknown style class: " + styleClass); 1942 return; 1943 } 1944 1945 try { 1946 Constructor<? extends Style> constructor = styleClass.getConstructor(); 1947 style = constructor.newInstance(); 1948 style.restoreFromExtras(extras); 1949 } catch (Throwable t) { 1950 Log.e(TAG, "Could not create Style", t); 1951 return; 1952 } 1953 } 1954 if (style != null) { 1955 setStyle(style); 1956 } 1957 } 1958 1959 /** 1960 * Add a timestamp pertaining to the notification (usually the time the event occurred). 1961 * It will be shown in the notification content view by default; use 1962 * {@link #setShowWhen(boolean) setShowWhen} to control this. 1963 * 1964 * @see Notification#when 1965 */ 1966 public Builder setWhen(long when) { 1967 mWhen = when; 1968 return this; 1969 } 1970 1971 /** 1972 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown 1973 * in the content view. 1974 */ 1975 public Builder setShowWhen(boolean show) { 1976 mShowWhen = show; 1977 return this; 1978 } 1979 1980 /** 1981 * Show the {@link Notification#when} field as a stopwatch. 1982 * 1983 * Instead of presenting <code>when</code> as a timestamp, the notification will show an 1984 * automatically updating display of the minutes and seconds since <code>when</code>. 1985 * 1986 * Useful when showing an elapsed time (like an ongoing phone call). 1987 * 1988 * @see android.widget.Chronometer 1989 * @see Notification#when 1990 */ 1991 public Builder setUsesChronometer(boolean b) { 1992 mUseChronometer = b; 1993 return this; 1994 } 1995 1996 /** 1997 * Set the small icon resource, which will be used to represent the notification in the 1998 * status bar. 1999 * 2000 2001 * The platform template for the expanded view will draw this icon in the left, unless a 2002 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small 2003 * icon will be moved to the right-hand side. 2004 * 2005 2006 * @param icon 2007 * A resource ID in the application's package of the drawable to use. 2008 * @see Notification#icon 2009 */ 2010 public Builder setSmallIcon(int icon) { 2011 mSmallIcon = icon; 2012 return this; 2013 } 2014 2015 /** 2016 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional 2017 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable 2018 * LevelListDrawable}. 2019 * 2020 * @param icon A resource ID in the application's package of the drawable to use. 2021 * @param level The level to use for the icon. 2022 * 2023 * @see Notification#icon 2024 * @see Notification#iconLevel 2025 */ 2026 public Builder setSmallIcon(int icon, int level) { 2027 mSmallIcon = icon; 2028 mSmallIconLevel = level; 2029 return this; 2030 } 2031 2032 /** 2033 * Set the first line of text in the platform notification template. 2034 */ 2035 public Builder setContentTitle(CharSequence title) { 2036 mContentTitle = safeCharSequence(title); 2037 return this; 2038 } 2039 2040 /** 2041 * Set the second line of text in the platform notification template. 2042 */ 2043 public Builder setContentText(CharSequence text) { 2044 mContentText = safeCharSequence(text); 2045 return this; 2046 } 2047 2048 /** 2049 * Set the third line of text in the platform notification template. 2050 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the 2051 * same location in the standard template. 2052 */ 2053 public Builder setSubText(CharSequence text) { 2054 mSubText = safeCharSequence(text); 2055 return this; 2056 } 2057 2058 /** 2059 * Set the large number at the right-hand side of the notification. This is 2060 * equivalent to setContentInfo, although it might show the number in a different 2061 * font size for readability. 2062 */ 2063 public Builder setNumber(int number) { 2064 mNumber = number; 2065 return this; 2066 } 2067 2068 /** 2069 * A small piece of additional information pertaining to this notification. 2070 * 2071 * The platform template will draw this on the last line of the notification, at the far 2072 * right (to the right of a smallIcon if it has been placed there). 2073 */ 2074 public Builder setContentInfo(CharSequence info) { 2075 mContentInfo = safeCharSequence(info); 2076 return this; 2077 } 2078 2079 /** 2080 * Set the progress this notification represents. 2081 * 2082 * The platform template will represent this using a {@link ProgressBar}. 2083 */ 2084 public Builder setProgress(int max, int progress, boolean indeterminate) { 2085 mProgressMax = max; 2086 mProgress = progress; 2087 mProgressIndeterminate = indeterminate; 2088 return this; 2089 } 2090 2091 /** 2092 * Supply a custom RemoteViews to use instead of the platform template. 2093 * 2094 * @see Notification#contentView 2095 */ 2096 public Builder setContent(RemoteViews views) { 2097 mContentView = views; 2098 return this; 2099 } 2100 2101 /** 2102 * Supply a {@link PendingIntent} to be sent when the notification is clicked. 2103 * 2104 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you 2105 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use 2106 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)} 2107 * to assign PendingIntents to individual views in that custom layout (i.e., to create 2108 * clickable buttons inside the notification view). 2109 * 2110 * @see Notification#contentIntent Notification.contentIntent 2111 */ 2112 public Builder setContentIntent(PendingIntent intent) { 2113 mContentIntent = intent; 2114 return this; 2115 } 2116 2117 /** 2118 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user. 2119 * 2120 * @see Notification#deleteIntent 2121 */ 2122 public Builder setDeleteIntent(PendingIntent intent) { 2123 mDeleteIntent = intent; 2124 return this; 2125 } 2126 2127 /** 2128 * An intent to launch instead of posting the notification to the status bar. 2129 * Only for use with extremely high-priority notifications demanding the user's 2130 * <strong>immediate</strong> attention, such as an incoming phone call or 2131 * alarm clock that the user has explicitly set to a particular time. 2132 * If this facility is used for something else, please give the user an option 2133 * to turn it off and use a normal notification, as this can be extremely 2134 * disruptive. 2135 * 2136 * <p> 2137 * The system UI may choose to display a heads-up notification, instead of 2138 * launching this intent, while the user is using the device. 2139 * </p> 2140 * 2141 * @param intent The pending intent to launch. 2142 * @param highPriority Passing true will cause this notification to be sent 2143 * even if other notifications are suppressed. 2144 * 2145 * @see Notification#fullScreenIntent 2146 */ 2147 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { 2148 mFullScreenIntent = intent; 2149 setFlag(FLAG_HIGH_PRIORITY, highPriority); 2150 return this; 2151 } 2152 2153 /** 2154 * Set the "ticker" text which is sent to accessibility services. 2155 * 2156 * @see Notification#tickerText 2157 */ 2158 public Builder setTicker(CharSequence tickerText) { 2159 mTickerText = safeCharSequence(tickerText); 2160 return this; 2161 } 2162 2163 /** 2164 * Obsolete version of {@link #setTicker(CharSequence)}. 2165 * 2166 */ 2167 @Deprecated 2168 public Builder setTicker(CharSequence tickerText, RemoteViews views) { 2169 mTickerText = safeCharSequence(tickerText); 2170 mTickerView = views; // we'll save it for you anyway 2171 return this; 2172 } 2173 2174 /** 2175 * Add a large icon to the notification (and the ticker on some devices). 2176 * 2177 * In the platform template, this image will be shown on the left of the notification view 2178 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side). 2179 * 2180 * @see Notification#largeIcon 2181 */ 2182 public Builder setLargeIcon(Bitmap icon) { 2183 mLargeIcon = icon; 2184 return this; 2185 } 2186 2187 /** 2188 * Set the sound to play. 2189 * 2190 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes} 2191 * for notifications. 2192 * 2193 * <p> 2194 * A notification that is noisy is more likely to be presented as a heads-up notification. 2195 * </p> 2196 * 2197 * @see Notification#sound 2198 */ 2199 public Builder setSound(Uri sound) { 2200 mSound = sound; 2201 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT; 2202 return this; 2203 } 2204 2205 /** 2206 * Set the sound to play, along with a specific stream on which to play it. 2207 * 2208 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants. 2209 * 2210 * <p> 2211 * A notification that is noisy is more likely to be presented as a heads-up notification. 2212 * </p> 2213 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead. 2214 * @see Notification#sound 2215 */ 2216 @Deprecated 2217 public Builder setSound(Uri sound, int streamType) { 2218 mSound = sound; 2219 mAudioStreamType = streamType; 2220 return this; 2221 } 2222 2223 /** 2224 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to 2225 * use during playback. 2226 * 2227 * <p> 2228 * A notification that is noisy is more likely to be presented as a heads-up notification. 2229 * </p> 2230 * 2231 * @see Notification#sound 2232 */ 2233 public Builder setSound(Uri sound, AudioAttributes audioAttributes) { 2234 mSound = sound; 2235 mAudioAttributes = audioAttributes; 2236 return this; 2237 } 2238 2239 /** 2240 * Set the vibration pattern to use. 2241 * 2242 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the 2243 * <code>pattern</code> parameter. 2244 * 2245 * <p> 2246 * A notification that vibrates is more likely to be presented as a heads-up notification. 2247 * </p> 2248 * 2249 * @see Notification#vibrate 2250 */ 2251 public Builder setVibrate(long[] pattern) { 2252 mVibrate = pattern; 2253 return this; 2254 } 2255 2256 /** 2257 * Set the desired color for the indicator LED on the device, as well as the 2258 * blink duty cycle (specified in milliseconds). 2259 * 2260 2261 * Not all devices will honor all (or even any) of these values. 2262 * 2263 2264 * @see Notification#ledARGB 2265 * @see Notification#ledOnMS 2266 * @see Notification#ledOffMS 2267 */ 2268 public Builder setLights(int argb, int onMs, int offMs) { 2269 mLedArgb = argb; 2270 mLedOnMs = onMs; 2271 mLedOffMs = offMs; 2272 return this; 2273 } 2274 2275 /** 2276 * Set whether this is an "ongoing" notification. 2277 * 2278 2279 * Ongoing notifications cannot be dismissed by the user, so your application or service 2280 * must take care of canceling them. 2281 * 2282 2283 * They are typically used to indicate a background task that the user is actively engaged 2284 * with (e.g., playing music) or is pending in some way and therefore occupying the device 2285 * (e.g., a file download, sync operation, active network connection). 2286 * 2287 2288 * @see Notification#FLAG_ONGOING_EVENT 2289 * @see Service#setForeground(boolean) 2290 */ 2291 public Builder setOngoing(boolean ongoing) { 2292 setFlag(FLAG_ONGOING_EVENT, ongoing); 2293 return this; 2294 } 2295 2296 /** 2297 * Set this flag if you would only like the sound, vibrate 2298 * and ticker to be played if the notification is not already showing. 2299 * 2300 * @see Notification#FLAG_ONLY_ALERT_ONCE 2301 */ 2302 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { 2303 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); 2304 return this; 2305 } 2306 2307 /** 2308 * Make this notification automatically dismissed when the user touches it. The 2309 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens. 2310 * 2311 * @see Notification#FLAG_AUTO_CANCEL 2312 */ 2313 public Builder setAutoCancel(boolean autoCancel) { 2314 setFlag(FLAG_AUTO_CANCEL, autoCancel); 2315 return this; 2316 } 2317 2318 /** 2319 * Set whether or not this notification should not bridge to other devices. 2320 * 2321 * <p>Some notifications can be bridged to other devices for remote display. 2322 * This hint can be set to recommend this notification not be bridged. 2323 */ 2324 public Builder setLocalOnly(boolean localOnly) { 2325 setFlag(FLAG_LOCAL_ONLY, localOnly); 2326 return this; 2327 } 2328 2329 /** 2330 * Set which notification properties will be inherited from system defaults. 2331 * <p> 2332 * The value should be one or more of the following fields combined with 2333 * bitwise-or: 2334 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. 2335 * <p> 2336 * For all default values, use {@link #DEFAULT_ALL}. 2337 */ 2338 public Builder setDefaults(int defaults) { 2339 mDefaults = defaults; 2340 return this; 2341 } 2342 2343 /** 2344 * Set the priority of this notification. 2345 * 2346 * @see Notification#priority 2347 */ 2348 public Builder setPriority(@Priority int pri) { 2349 mPriority = pri; 2350 return this; 2351 } 2352 2353 /** 2354 * Set the notification category. 2355 * 2356 * @see Notification#category 2357 */ 2358 public Builder setCategory(String category) { 2359 mCategory = category; 2360 return this; 2361 } 2362 2363 /** 2364 * Add a person that is relevant to this notification. 2365 * 2366 * @see Notification#EXTRA_PEOPLE 2367 */ 2368 public Builder addPerson(String handle) { 2369 mPeople.add(handle); 2370 return this; 2371 } 2372 2373 /** 2374 * Set this notification to be part of a group of notifications sharing the same key. 2375 * Grouped notifications may display in a cluster or stack on devices which 2376 * support such rendering. 2377 * 2378 * <p>To make this notification the summary for its group, also call 2379 * {@link #setGroupSummary}. A sort order can be specified for group members by using 2380 * {@link #setSortKey}. 2381 * @param groupKey The group key of the group. 2382 * @return this object for method chaining 2383 */ 2384 public Builder setGroup(String groupKey) { 2385 mGroupKey = groupKey; 2386 return this; 2387 } 2388 2389 /** 2390 * Set this notification to be the group summary for a group of notifications. 2391 * Grouped notifications may display in a cluster or stack on devices which 2392 * support such rendering. Requires a group key also be set using {@link #setGroup}. 2393 * @param isGroupSummary Whether this notification should be a group summary. 2394 * @return this object for method chaining 2395 */ 2396 public Builder setGroupSummary(boolean isGroupSummary) { 2397 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary); 2398 return this; 2399 } 2400 2401 /** 2402 * Set a sort key that orders this notification among other notifications from the 2403 * same package. This can be useful if an external sort was already applied and an app 2404 * would like to preserve this. Notifications will be sorted lexicographically using this 2405 * value, although providing different priorities in addition to providing sort key may 2406 * cause this value to be ignored. 2407 * 2408 * <p>This sort key can also be used to order members of a notification group. See 2409 * {@link #setGroup}. 2410 * 2411 * @see String#compareTo(String) 2412 */ 2413 public Builder setSortKey(String sortKey) { 2414 mSortKey = sortKey; 2415 return this; 2416 } 2417 2418 /** 2419 * Merge additional metadata into this notification. 2420 * 2421 * <p>Values within the Bundle will replace existing extras values in this Builder. 2422 * 2423 * @see Notification#extras 2424 */ 2425 public Builder addExtras(Bundle extras) { 2426 if (extras != null) { 2427 if (mExtras == null) { 2428 mExtras = new Bundle(extras); 2429 } else { 2430 mExtras.putAll(extras); 2431 } 2432 } 2433 return this; 2434 } 2435 2436 /** 2437 * Set metadata for this notification. 2438 * 2439 * <p>A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's 2440 * current contents are copied into the Notification each time {@link #build()} is 2441 * called. 2442 * 2443 * <p>Replaces any existing extras values with those from the provided Bundle. 2444 * Use {@link #addExtras} to merge in metadata instead. 2445 * 2446 * @see Notification#extras 2447 */ 2448 public Builder setExtras(Bundle extras) { 2449 mExtras = extras; 2450 return this; 2451 } 2452 2453 /** 2454 * Get the current metadata Bundle used by this notification Builder. 2455 * 2456 * <p>The returned Bundle is shared with this Builder. 2457 * 2458 * <p>The current contents of this Bundle are copied into the Notification each time 2459 * {@link #build()} is called. 2460 * 2461 * @see Notification#extras 2462 */ 2463 public Bundle getExtras() { 2464 if (mExtras == null) { 2465 mExtras = new Bundle(); 2466 } 2467 return mExtras; 2468 } 2469 2470 /** 2471 * Add an action to this notification. Actions are typically displayed by 2472 * the system as a button adjacent to the notification content. 2473 * <p> 2474 * Every action must have an icon (32dp square and matching the 2475 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo 2476 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}. 2477 * <p> 2478 * A notification in its expanded form can display up to 3 actions, from left to right in 2479 * the order they were added. Actions will not be displayed when the notification is 2480 * collapsed, however, so be sure that any essential functions may be accessed by the user 2481 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}). 2482 * 2483 * @param icon Resource ID of a drawable that represents the action. 2484 * @param title Text describing the action. 2485 * @param intent PendingIntent to be fired when the action is invoked. 2486 */ 2487 public Builder addAction(int icon, CharSequence title, PendingIntent intent) { 2488 mActions.add(new Action(icon, safeCharSequence(title), intent)); 2489 return this; 2490 } 2491 2492 /** 2493 * Add an action to this notification. Actions are typically displayed by 2494 * the system as a button adjacent to the notification content. 2495 * <p> 2496 * Every action must have an icon (32dp square and matching the 2497 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo 2498 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}. 2499 * <p> 2500 * A notification in its expanded form can display up to 3 actions, from left to right in 2501 * the order they were added. Actions will not be displayed when the notification is 2502 * collapsed, however, so be sure that any essential functions may be accessed by the user 2503 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}). 2504 * 2505 * @param action The action to add. 2506 */ 2507 public Builder addAction(Action action) { 2508 mActions.add(action); 2509 return this; 2510 } 2511 2512 /** 2513 * Add a rich notification style to be applied at build time. 2514 * 2515 * @param style Object responsible for modifying the notification style. 2516 */ 2517 public Builder setStyle(Style style) { 2518 if (mStyle != style) { 2519 mStyle = style; 2520 if (mStyle != null) { 2521 mStyle.setBuilder(this); 2522 } 2523 } 2524 return this; 2525 } 2526 2527 /** 2528 * Specify the value of {@link #visibility}. 2529 * 2530 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default), 2531 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}. 2532 * 2533 * @return The same Builder. 2534 */ 2535 public Builder setVisibility(int visibility) { 2536 mVisibility = visibility; 2537 return this; 2538 } 2539 2540 /** 2541 * Supply a replacement Notification whose contents should be shown in insecure contexts 2542 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}. 2543 * @param n A replacement notification, presumably with some or all info redacted. 2544 * @return The same Builder. 2545 */ 2546 public Builder setPublicVersion(Notification n) { 2547 mPublicVersion = n; 2548 return this; 2549 } 2550 2551 /** 2552 * Apply an extender to this notification builder. Extenders may be used to add 2553 * metadata or change options on this builder. 2554 */ 2555 public Builder extend(Extender extender) { 2556 extender.extend(this); 2557 return this; 2558 } 2559 2560 private void setFlag(int mask, boolean value) { 2561 if (value) { 2562 mFlags |= mask; 2563 } else { 2564 mFlags &= ~mask; 2565 } 2566 } 2567 2568 /** 2569 * Sets {@link Notification#color}. 2570 * 2571 * @param argb The accent color to use 2572 * 2573 * @return The same Builder. 2574 */ 2575 public Builder setColor(int argb) { 2576 mColor = argb; 2577 return this; 2578 } 2579 2580 private Drawable getProfileBadgeDrawable() { 2581 // Note: This assumes that the current user can read the profile badge of the 2582 // originating user. 2583 return mContext.getPackageManager().getUserBadgeForDensity( 2584 new UserHandle(mContext.getUserId()), 0); 2585 } 2586 2587 private Bitmap getProfileBadge() { 2588 Drawable badge = getProfileBadgeDrawable(); 2589 if (badge == null) { 2590 return null; 2591 } 2592 final int size = mContext.getResources().getDimensionPixelSize( 2593 R.dimen.notification_badge_size); 2594 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 2595 Canvas canvas = new Canvas(bitmap); 2596 badge.setBounds(0, 0, size, size); 2597 badge.draw(canvas); 2598 return bitmap; 2599 } 2600 2601 private boolean addProfileBadge(RemoteViews contentView, int resId) { 2602 Bitmap profileBadge = getProfileBadge(); 2603 2604 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE); 2605 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE); 2606 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE); 2607 2608 if (profileBadge != null) { 2609 contentView.setImageViewBitmap(resId, profileBadge); 2610 contentView.setViewVisibility(resId, View.VISIBLE); 2611 2612 // Make sure Line 3 is visible. As badge will be here if there 2613 // is no text to display. 2614 if (resId == R.id.profile_badge_line3) { 2615 contentView.setViewVisibility(R.id.line3, View.VISIBLE); 2616 } 2617 return true; 2618 } 2619 return false; 2620 } 2621 2622 private void shrinkLine3Text(RemoteViews contentView) { 2623 float subTextSize = mContext.getResources().getDimensionPixelSize( 2624 R.dimen.notification_subtext_size); 2625 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize); 2626 } 2627 2628 private void unshrinkLine3Text(RemoteViews contentView) { 2629 float regularTextSize = mContext.getResources().getDimensionPixelSize( 2630 com.android.internal.R.dimen.notification_text_size); 2631 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize); 2632 } 2633 2634 private void resetStandardTemplate(RemoteViews contentView) { 2635 removeLargeIconBackground(contentView); 2636 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0); 2637 contentView.setImageViewResource(R.id.icon, 0); 2638 contentView.setInt(R.id.icon, "setBackgroundResource", 0); 2639 contentView.setViewVisibility(R.id.right_icon, View.GONE); 2640 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0); 2641 contentView.setImageViewResource(R.id.right_icon, 0); 2642 contentView.setImageViewResource(R.id.icon, 0); 2643 contentView.setTextViewText(R.id.title, null); 2644 contentView.setTextViewText(R.id.text, null); 2645 unshrinkLine3Text(contentView); 2646 contentView.setTextViewText(R.id.text2, null); 2647 contentView.setViewVisibility(R.id.text2, View.GONE); 2648 contentView.setViewVisibility(R.id.info, View.GONE); 2649 contentView.setViewVisibility(R.id.time, View.GONE); 2650 contentView.setViewVisibility(R.id.line3, View.GONE); 2651 contentView.setViewVisibility(R.id.overflow_divider, View.GONE); 2652 contentView.setViewVisibility(R.id.progress, View.GONE); 2653 } 2654 2655 private RemoteViews applyStandardTemplate(int resId) { 2656 return applyStandardTemplate(resId, true /* hasProgress */); 2657 } 2658 2659 /** 2660 * @param hasProgress whether the progress bar should be shown and set 2661 */ 2662 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) { 2663 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId); 2664 2665 resetStandardTemplate(contentView); 2666 2667 boolean showLine3 = false; 2668 boolean showLine2 = false; 2669 boolean contentTextInLine2 = false; 2670 2671 if (mLargeIcon != null) { 2672 contentView.setImageViewBitmap(R.id.icon, mLargeIcon); 2673 processLargeLegacyIcon(mLargeIcon, contentView); 2674 contentView.setImageViewResource(R.id.right_icon, mSmallIcon); 2675 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE); 2676 processSmallRightIcon(mSmallIcon, contentView); 2677 } else { // small icon at left 2678 contentView.setImageViewResource(R.id.icon, mSmallIcon); 2679 contentView.setViewVisibility(R.id.icon, View.VISIBLE); 2680 processSmallIconAsLarge(mSmallIcon, contentView); 2681 } 2682 if (mContentTitle != null) { 2683 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle)); 2684 } 2685 if (mContentText != null) { 2686 contentView.setTextViewText(R.id.text, processLegacyText(mContentText)); 2687 showLine3 = true; 2688 } 2689 if (mContentInfo != null) { 2690 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo)); 2691 contentView.setViewVisibility(R.id.info, View.VISIBLE); 2692 showLine3 = true; 2693 } else if (mNumber > 0) { 2694 final int tooBig = mContext.getResources().getInteger( 2695 R.integer.status_bar_notification_info_maxnum); 2696 if (mNumber > tooBig) { 2697 contentView.setTextViewText(R.id.info, processLegacyText( 2698 mContext.getResources().getString( 2699 R.string.status_bar_notification_info_overflow))); 2700 } else { 2701 NumberFormat f = NumberFormat.getIntegerInstance(); 2702 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber))); 2703 } 2704 contentView.setViewVisibility(R.id.info, View.VISIBLE); 2705 showLine3 = true; 2706 } else { 2707 contentView.setViewVisibility(R.id.info, View.GONE); 2708 } 2709 2710 // Need to show three lines? 2711 if (mSubText != null) { 2712 contentView.setTextViewText(R.id.text, processLegacyText(mSubText)); 2713 if (mContentText != null) { 2714 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText)); 2715 contentView.setViewVisibility(R.id.text2, View.VISIBLE); 2716 showLine2 = true; 2717 contentTextInLine2 = true; 2718 } else { 2719 contentView.setViewVisibility(R.id.text2, View.GONE); 2720 } 2721 } else { 2722 contentView.setViewVisibility(R.id.text2, View.GONE); 2723 if (hasProgress && (mProgressMax != 0 || mProgressIndeterminate)) { 2724 contentView.setViewVisibility(R.id.progress, View.VISIBLE); 2725 contentView.setProgressBar( 2726 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate); 2727 showLine2 = true; 2728 } else { 2729 contentView.setViewVisibility(R.id.progress, View.GONE); 2730 } 2731 } 2732 if (showLine2) { 2733 2734 // need to shrink all the type to make sure everything fits 2735 shrinkLine3Text(contentView); 2736 } 2737 2738 if (showsTimeOrChronometer()) { 2739 if (mUseChronometer) { 2740 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE); 2741 contentView.setLong(R.id.chronometer, "setBase", 2742 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis())); 2743 contentView.setBoolean(R.id.chronometer, "setStarted", true); 2744 } else { 2745 contentView.setViewVisibility(R.id.time, View.VISIBLE); 2746 contentView.setLong(R.id.time, "setTime", mWhen); 2747 } 2748 } else { 2749 contentView.setViewVisibility(R.id.time, View.GONE); 2750 } 2751 2752 // Adjust padding depending on line count and font size. 2753 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext, 2754 mHasThreeLines, mContext.getResources().getConfiguration().fontScale), 2755 0, 0); 2756 2757 // We want to add badge to first line of text. 2758 boolean addedBadge = addProfileBadge(contentView, 2759 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3); 2760 // If we added the badge to line 3 then we should show line 3. 2761 if (addedBadge && !contentTextInLine2) { 2762 showLine3 = true; 2763 } 2764 2765 // Note getStandardView may hide line 3 again. 2766 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE); 2767 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE); 2768 return contentView; 2769 } 2770 2771 /** 2772 * @return true if the built notification will show the time or the chronometer; false 2773 * otherwise 2774 */ 2775 private boolean showsTimeOrChronometer() { 2776 return mWhen != 0 && mShowWhen; 2777 } 2778 2779 /** 2780 * Logic to find out whether the notification is going to have three lines in the contracted 2781 * layout. This is used to adjust the top padding. 2782 * 2783 * @return true if the notification is going to have three lines; false if the notification 2784 * is going to have one or two lines 2785 */ 2786 private boolean hasThreeLines() { 2787 boolean contentTextInLine2 = mSubText != null && mContentText != null; 2788 boolean hasProgress = mStyle == null || mStyle.hasProgress(); 2789 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise 2790 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2; 2791 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0 2792 || badgeInLine3; 2793 boolean hasLine2 = (mSubText != null && mContentText != null) || 2794 (hasProgress && mSubText == null 2795 && (mProgressMax != 0 || mProgressIndeterminate)); 2796 return hasLine2 && hasLine3; 2797 } 2798 2799 /** 2800 * @hide 2801 */ 2802 public static int calculateTopPadding(Context ctx, boolean hasThreeLines, 2803 float fontScale) { 2804 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines 2805 ? R.dimen.notification_top_pad_narrow 2806 : R.dimen.notification_top_pad); 2807 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines 2808 ? R.dimen.notification_top_pad_large_text_narrow 2809 : R.dimen.notification_top_pad_large_text); 2810 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f) 2811 / (LARGE_TEXT_SCALE - 1f); 2812 2813 // Linearly interpolate the padding between large and normal with the font scale ranging 2814 // from 1f to LARGE_TEXT_SCALE 2815 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding); 2816 } 2817 2818 private void resetStandardTemplateWithActions(RemoteViews big) { 2819 big.setViewVisibility(R.id.actions, View.GONE); 2820 big.setViewVisibility(R.id.action_divider, View.GONE); 2821 big.removeAllViews(R.id.actions); 2822 } 2823 2824 private RemoteViews applyStandardTemplateWithActions(int layoutId) { 2825 RemoteViews big = applyStandardTemplate(layoutId); 2826 2827 resetStandardTemplateWithActions(big); 2828 2829 int N = mActions.size(); 2830 if (N > 0) { 2831 big.setViewVisibility(R.id.actions, View.VISIBLE); 2832 big.setViewVisibility(R.id.action_divider, View.VISIBLE); 2833 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS; 2834 for (int i=0; i<N; i++) { 2835 final RemoteViews button = generateActionButton(mActions.get(i)); 2836 big.addView(R.id.actions, button); 2837 } 2838 } 2839 return big; 2840 } 2841 2842 private RemoteViews makeContentView() { 2843 if (mContentView != null) { 2844 return mContentView; 2845 } else { 2846 return applyStandardTemplate(getBaseLayoutResource()); 2847 } 2848 } 2849 2850 private RemoteViews makeTickerView() { 2851 if (mTickerView != null) { 2852 return mTickerView; 2853 } 2854 return null; // tickers are not created by default anymore 2855 } 2856 2857 private RemoteViews makeBigContentView() { 2858 if (mActions.size() == 0) return null; 2859 2860 return applyStandardTemplateWithActions(getBigBaseLayoutResource()); 2861 } 2862 2863 private RemoteViews makeHeadsUpContentView() { 2864 if (mActions.size() == 0) return null; 2865 2866 return applyStandardTemplateWithActions(getBigBaseLayoutResource()); 2867 } 2868 2869 2870 private RemoteViews generateActionButton(Action action) { 2871 final boolean tombstone = (action.actionIntent == null); 2872 RemoteViews button = new RemoteViews(mContext.getPackageName(), 2873 tombstone ? getActionTombstoneLayoutResource() 2874 : getActionLayoutResource()); 2875 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0); 2876 button.setTextViewText(R.id.action0, processLegacyText(action.title)); 2877 if (!tombstone) { 2878 button.setOnClickPendingIntent(R.id.action0, action.actionIntent); 2879 } 2880 button.setContentDescription(R.id.action0, action.title); 2881 processLegacyAction(action, button); 2882 return button; 2883 } 2884 2885 /** 2886 * @return Whether we are currently building a notification from a legacy (an app that 2887 * doesn't create material notifications by itself) app. 2888 */ 2889 private boolean isLegacy() { 2890 return mColorUtil != null; 2891 } 2892 2893 private void processLegacyAction(Action action, RemoteViews button) { 2894 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) { 2895 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0, 2896 mContext.getResources().getColor(R.color.notification_action_color_filter), 2897 PorterDuff.Mode.MULTIPLY); 2898 } 2899 } 2900 2901 private CharSequence processLegacyText(CharSequence charSequence) { 2902 if (isLegacy()) { 2903 return mColorUtil.invertCharSequenceColors(charSequence); 2904 } else { 2905 return charSequence; 2906 } 2907 } 2908 2909 /** 2910 * Apply any necessary background to smallIcons being used in the largeIcon spot. 2911 */ 2912 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) { 2913 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, largeIconId)) { 2914 applyLargeIconBackground(contentView); 2915 } 2916 } 2917 2918 /** 2919 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is, 2920 * if it's grayscale). 2921 */ 2922 // TODO: also check bounds, transparency, that sort of thing. 2923 private void processLargeLegacyIcon(Bitmap largeIcon, RemoteViews contentView) { 2924 if (isLegacy() && mColorUtil.isGrayscaleIcon(largeIcon)) { 2925 applyLargeIconBackground(contentView); 2926 } else { 2927 removeLargeIconBackground(contentView); 2928 } 2929 } 2930 2931 /** 2932 * Add a colored circle behind the largeIcon slot. 2933 */ 2934 private void applyLargeIconBackground(RemoteViews contentView) { 2935 contentView.setInt(R.id.icon, "setBackgroundResource", 2936 R.drawable.notification_icon_legacy_bg); 2937 2938 contentView.setDrawableParameters( 2939 R.id.icon, 2940 true, 2941 -1, 2942 resolveColor(), 2943 PorterDuff.Mode.SRC_ATOP, 2944 -1); 2945 2946 int padding = mContext.getResources().getDimensionPixelSize( 2947 R.dimen.notification_large_icon_circle_padding); 2948 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding); 2949 } 2950 2951 private void removeLargeIconBackground(RemoteViews contentView) { 2952 contentView.setInt(R.id.icon, "setBackgroundResource", 0); 2953 } 2954 2955 /** 2956 * Recolor small icons when used in the R.id.right_icon slot. 2957 */ 2958 private void processSmallRightIcon(int smallIconDrawableId, 2959 RemoteViews contentView) { 2960 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, smallIconDrawableId)) { 2961 contentView.setDrawableParameters(R.id.right_icon, false, -1, 2962 0xFFFFFFFF, 2963 PorterDuff.Mode.SRC_ATOP, -1); 2964 2965 contentView.setInt(R.id.right_icon, 2966 "setBackgroundResource", 2967 R.drawable.notification_icon_legacy_bg); 2968 2969 contentView.setDrawableParameters( 2970 R.id.right_icon, 2971 true, 2972 -1, 2973 resolveColor(), 2974 PorterDuff.Mode.SRC_ATOP, 2975 -1); 2976 } 2977 } 2978 2979 private int sanitizeColor() { 2980 if (mColor != COLOR_DEFAULT) { 2981 mColor |= 0xFF000000; // no alpha for custom colors 2982 } 2983 return mColor; 2984 } 2985 2986 private int resolveColor() { 2987 if (mColor == COLOR_DEFAULT) { 2988 return mContext.getResources().getColor(R.color.notification_icon_bg_color); 2989 } 2990 return mColor; 2991 } 2992 2993 /** 2994 * Apply the unstyled operations and return a new {@link Notification} object. 2995 * @hide 2996 */ 2997 public Notification buildUnstyled() { 2998 Notification n = new Notification(); 2999 n.when = mWhen; 3000 n.icon = mSmallIcon; 3001 n.iconLevel = mSmallIconLevel; 3002 n.number = mNumber; 3003 3004 n.color = sanitizeColor(); 3005 3006 setBuilderContentView(n, makeContentView()); 3007 n.contentIntent = mContentIntent; 3008 n.deleteIntent = mDeleteIntent; 3009 n.fullScreenIntent = mFullScreenIntent; 3010 n.tickerText = mTickerText; 3011 n.tickerView = makeTickerView(); 3012 n.largeIcon = mLargeIcon; 3013 n.sound = mSound; 3014 n.audioStreamType = mAudioStreamType; 3015 n.audioAttributes = mAudioAttributes; 3016 n.vibrate = mVibrate; 3017 n.ledARGB = mLedArgb; 3018 n.ledOnMS = mLedOnMs; 3019 n.ledOffMS = mLedOffMs; 3020 n.defaults = mDefaults; 3021 n.flags = mFlags; 3022 setBuilderBigContentView(n, makeBigContentView()); 3023 setBuilderHeadsUpContentView(n, makeHeadsUpContentView()); 3024 if (mLedOnMs != 0 || mLedOffMs != 0) { 3025 n.flags |= FLAG_SHOW_LIGHTS; 3026 } 3027 if ((mDefaults & DEFAULT_LIGHTS) != 0) { 3028 n.flags |= FLAG_SHOW_LIGHTS; 3029 } 3030 n.category = mCategory; 3031 n.mGroupKey = mGroupKey; 3032 n.mSortKey = mSortKey; 3033 n.priority = mPriority; 3034 if (mActions.size() > 0) { 3035 n.actions = new Action[mActions.size()]; 3036 mActions.toArray(n.actions); 3037 } 3038 n.visibility = mVisibility; 3039 3040 if (mPublicVersion != null) { 3041 n.publicVersion = new Notification(); 3042 mPublicVersion.cloneInto(n.publicVersion, true); 3043 } 3044 // Note: If you're adding new fields, also update restoreFromNotitification(). 3045 return n; 3046 } 3047 3048 /** 3049 * Capture, in the provided bundle, semantic information used in the construction of 3050 * this Notification object. 3051 * @hide 3052 */ 3053 public void populateExtras(Bundle extras) { 3054 // Store original information used in the construction of this object 3055 extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO, 3056 mContext.getApplicationInfo()); 3057 extras.putCharSequence(EXTRA_TITLE, mContentTitle); 3058 extras.putCharSequence(EXTRA_TEXT, mContentText); 3059 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText); 3060 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo); 3061 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon); 3062 extras.putInt(EXTRA_PROGRESS, mProgress); 3063 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax); 3064 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate); 3065 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer); 3066 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen); 3067 if (mLargeIcon != null) { 3068 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon); 3069 } 3070 if (!mPeople.isEmpty()) { 3071 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()])); 3072 } 3073 // NOTE: If you're adding new extras also update restoreFromNotification(). 3074 } 3075 3076 3077 /** 3078 * @hide 3079 */ 3080 public static void stripForDelivery(Notification n) { 3081 if (!STRIP_AND_REBUILD) { 3082 return; 3083 } 3084 3085 String templateClass = n.extras.getString(EXTRA_TEMPLATE); 3086 // Only strip views for known Styles because we won't know how to 3087 // re-create them otherwise. 3088 boolean stripViews = TextUtils.isEmpty(templateClass) || 3089 getNotificationStyleClass(templateClass) != null; 3090 3091 boolean isStripped = false; 3092 3093 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) { 3094 // TODO: Would like to check for equality here, but if the notification 3095 // has been cloned, we can't. 3096 n.largeIcon = null; 3097 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true); 3098 isStripped = true; 3099 } 3100 // Get rid of unmodified BuilderRemoteViews. 3101 3102 if (stripViews && 3103 n.contentView instanceof BuilderRemoteViews && 3104 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) == 3105 n.contentView.getSequenceNumber()) { 3106 n.contentView = null; 3107 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true); 3108 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT); 3109 isStripped = true; 3110 } 3111 if (stripViews && 3112 n.bigContentView instanceof BuilderRemoteViews && 3113 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) == 3114 n.bigContentView.getSequenceNumber()) { 3115 n.bigContentView = null; 3116 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true); 3117 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT); 3118 isStripped = true; 3119 } 3120 if (stripViews && 3121 n.headsUpContentView instanceof BuilderRemoteViews && 3122 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) == 3123 n.headsUpContentView.getSequenceNumber()) { 3124 n.headsUpContentView = null; 3125 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true); 3126 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT); 3127 isStripped = true; 3128 } 3129 3130 if (isStripped) { 3131 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true); 3132 } 3133 } 3134 3135 /** 3136 * @hide 3137 */ 3138 public static Notification rebuild(Context context, Notification n) { 3139 Bundle extras = n.extras; 3140 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n; 3141 extras.remove(EXTRA_NEEDS_REBUILD); 3142 3143 // Re-create notification context so we can access app resources. 3144 ApplicationInfo applicationInfo = extras.getParcelable( 3145 EXTRA_REBUILD_CONTEXT_APPLICATION_INFO); 3146 Context builderContext; 3147 try { 3148 builderContext = context.createApplicationContext(applicationInfo, 3149 Context.CONTEXT_RESTRICTED); 3150 } catch (NameNotFoundException e) { 3151 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found"); 3152 builderContext = context; // try with our context 3153 } 3154 3155 Builder b = new Builder(builderContext, n); 3156 return b.rebuild(); 3157 } 3158 3159 /** 3160 * Rebuilds the notification passed in to the rebuild-constructor 3161 * {@link #Builder(Context, Notification)}. 3162 * 3163 * <p> 3164 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode. 3165 * 3166 * @hide 3167 */ 3168 private Notification rebuild() { 3169 if (mRebuildNotification == null) { 3170 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode."); 3171 } 3172 mHasThreeLines = hasThreeLines(); 3173 3174 Bundle extras = mRebuildNotification.extras; 3175 3176 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) { 3177 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON); 3178 } 3179 extras.remove(EXTRA_REBUILD_LARGE_ICON); 3180 3181 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) { 3182 setBuilderContentView(mRebuildNotification, makeContentView()); 3183 if (mStyle != null) { 3184 mStyle.populateContentView(mRebuildNotification); 3185 } 3186 } 3187 extras.remove(EXTRA_REBUILD_CONTENT_VIEW); 3188 3189 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) { 3190 setBuilderBigContentView(mRebuildNotification, makeBigContentView()); 3191 if (mStyle != null) { 3192 mStyle.populateBigContentView(mRebuildNotification); 3193 } 3194 } 3195 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW); 3196 3197 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) { 3198 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView()); 3199 if (mStyle != null) { 3200 mStyle.populateHeadsUpContentView(mRebuildNotification); 3201 } 3202 } 3203 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW); 3204 3205 mHasThreeLines = false; 3206 return mRebuildNotification; 3207 } 3208 3209 private static Class<? extends Style> getNotificationStyleClass(String templateClass) { 3210 Class<? extends Style>[] classes = new Class[]{ 3211 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class}; 3212 for (Class<? extends Style> innerClass : classes) { 3213 if (templateClass.equals(innerClass.getName())) { 3214 return innerClass; 3215 } 3216 } 3217 return null; 3218 } 3219 3220 private void setBuilderContentView(Notification n, RemoteViews contentView) { 3221 n.contentView = contentView; 3222 if (contentView instanceof BuilderRemoteViews) { 3223 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, 3224 contentView.getSequenceNumber()); 3225 } 3226 } 3227 3228 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) { 3229 n.bigContentView = bigContentView; 3230 if (bigContentView instanceof BuilderRemoteViews) { 3231 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, 3232 bigContentView.getSequenceNumber()); 3233 } 3234 } 3235 3236 private void setBuilderHeadsUpContentView(Notification n, 3237 RemoteViews headsUpContentView) { 3238 n.headsUpContentView = headsUpContentView; 3239 if (headsUpContentView instanceof BuilderRemoteViews) { 3240 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, 3241 headsUpContentView.getSequenceNumber()); 3242 } 3243 } 3244 3245 private void restoreFromNotification(Notification n) { 3246 3247 // Notification fields. 3248 mWhen = n.when; 3249 mSmallIcon = n.icon; 3250 mSmallIconLevel = n.iconLevel; 3251 mNumber = n.number; 3252 3253 mColor = n.color; 3254 3255 mContentView = n.contentView; 3256 mDeleteIntent = n.deleteIntent; 3257 mFullScreenIntent = n.fullScreenIntent; 3258 mTickerText = n.tickerText; 3259 mTickerView = n.tickerView; 3260 mLargeIcon = n.largeIcon; 3261 mSound = n.sound; 3262 mAudioStreamType = n.audioStreamType; 3263 mAudioAttributes = n.audioAttributes; 3264 3265 mVibrate = n.vibrate; 3266 mLedArgb = n.ledARGB; 3267 mLedOnMs = n.ledOnMS; 3268 mLedOffMs = n.ledOffMS; 3269 mDefaults = n.defaults; 3270 mFlags = n.flags; 3271 3272 mCategory = n.category; 3273 mGroupKey = n.mGroupKey; 3274 mSortKey = n.mSortKey; 3275 mPriority = n.priority; 3276 mActions.clear(); 3277 if (n.actions != null) { 3278 Collections.addAll(mActions, n.actions); 3279 } 3280 mVisibility = n.visibility; 3281 3282 mPublicVersion = n.publicVersion; 3283 3284 // Extras. 3285 Bundle extras = n.extras; 3286 mContentTitle = extras.getCharSequence(EXTRA_TITLE); 3287 mContentText = extras.getCharSequence(EXTRA_TEXT); 3288 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT); 3289 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT); 3290 mSmallIcon = extras.getInt(EXTRA_SMALL_ICON); 3291 mProgress = extras.getInt(EXTRA_PROGRESS); 3292 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX); 3293 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE); 3294 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER); 3295 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN); 3296 if (extras.containsKey(EXTRA_LARGE_ICON)) { 3297 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON); 3298 } 3299 if (extras.containsKey(EXTRA_PEOPLE)) { 3300 mPeople.clear(); 3301 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE)); 3302 } 3303 } 3304 3305 /** 3306 * @deprecated Use {@link #build()} instead. 3307 */ 3308 @Deprecated 3309 public Notification getNotification() { 3310 return build(); 3311 } 3312 3313 /** 3314 * Combine all of the options that have been set and return a new {@link Notification} 3315 * object. 3316 */ 3317 public Notification build() { 3318 mHasThreeLines = hasThreeLines(); 3319 3320 Notification n = buildUnstyled(); 3321 3322 if (mStyle != null) { 3323 n = mStyle.buildStyled(n); 3324 } 3325 3326 if (mExtras != null) { 3327 n.extras.putAll(mExtras); 3328 } 3329 3330 if (mRebuildBundle.size() > 0) { 3331 n.extras.putAll(mRebuildBundle); 3332 mRebuildBundle.clear(); 3333 } 3334 3335 populateExtras(n.extras); 3336 if (mStyle != null) { 3337 mStyle.addExtras(n.extras); 3338 } 3339 3340 mHasThreeLines = false; 3341 return n; 3342 } 3343 3344 /** 3345 * Apply this Builder to an existing {@link Notification} object. 3346 * 3347 * @hide 3348 */ 3349 public Notification buildInto(Notification n) { 3350 build().cloneInto(n, true); 3351 return n; 3352 } 3353 3354 private int getBaseLayoutResource() { 3355 return R.layout.notification_template_material_base; 3356 } 3357 3358 private int getBigBaseLayoutResource() { 3359 return R.layout.notification_template_material_big_base; 3360 } 3361 3362 private int getBigPictureLayoutResource() { 3363 return R.layout.notification_template_material_big_picture; 3364 } 3365 3366 private int getBigTextLayoutResource() { 3367 return R.layout.notification_template_material_big_text; 3368 } 3369 3370 private int getInboxLayoutResource() { 3371 return R.layout.notification_template_material_inbox; 3372 } 3373 3374 private int getActionLayoutResource() { 3375 return R.layout.notification_material_action; 3376 } 3377 3378 private int getActionTombstoneLayoutResource() { 3379 return R.layout.notification_material_action_tombstone; 3380 } 3381 } 3382 3383 /** 3384 * An object that can apply a rich notification style to a {@link Notification.Builder} 3385 * object. 3386 */ 3387 public static abstract class Style { 3388 private CharSequence mBigContentTitle; 3389 private CharSequence mSummaryText = null; 3390 private boolean mSummaryTextSet = false; 3391 3392 protected Builder mBuilder; 3393 3394 /** 3395 * Overrides ContentTitle in the big form of the template. 3396 * This defaults to the value passed to setContentTitle(). 3397 */ 3398 protected void internalSetBigContentTitle(CharSequence title) { 3399 mBigContentTitle = title; 3400 } 3401 3402 /** 3403 * Set the first line of text after the detail section in the big form of the template. 3404 */ 3405 protected void internalSetSummaryText(CharSequence cs) { 3406 mSummaryText = cs; 3407 mSummaryTextSet = true; 3408 } 3409 3410 public void setBuilder(Builder builder) { 3411 if (mBuilder != builder) { 3412 mBuilder = builder; 3413 if (mBuilder != null) { 3414 mBuilder.setStyle(this); 3415 } 3416 } 3417 } 3418 3419 protected void checkBuilder() { 3420 if (mBuilder == null) { 3421 throw new IllegalArgumentException("Style requires a valid Builder object"); 3422 } 3423 } 3424 3425 protected RemoteViews getStandardView(int layoutId) { 3426 checkBuilder(); 3427 3428 // Nasty. 3429 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle; 3430 if (mBigContentTitle != null) { 3431 mBuilder.setContentTitle(mBigContentTitle); 3432 } 3433 3434 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId); 3435 3436 mBuilder.mContentTitle = oldBuilderContentTitle; 3437 3438 if (mBigContentTitle != null && mBigContentTitle.equals("")) { 3439 contentView.setViewVisibility(R.id.line1, View.GONE); 3440 } else { 3441 contentView.setViewVisibility(R.id.line1, View.VISIBLE); 3442 } 3443 3444 // The last line defaults to the subtext, but can be replaced by mSummaryText 3445 final CharSequence overflowText = 3446 mSummaryTextSet ? mSummaryText 3447 : mBuilder.mSubText; 3448 if (overflowText != null) { 3449 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText)); 3450 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE); 3451 contentView.setViewVisibility(R.id.line3, View.VISIBLE); 3452 } else { 3453 // Clear text in case we use the line to show the profile badge. 3454 contentView.setTextViewText(R.id.text, ""); 3455 contentView.setViewVisibility(R.id.overflow_divider, View.GONE); 3456 contentView.setViewVisibility(R.id.line3, View.GONE); 3457 } 3458 3459 return contentView; 3460 } 3461 3462 /** 3463 * Changes the padding of the first line such that the big and small content view have the 3464 * same top padding. 3465 * 3466 * @hide 3467 */ 3468 protected void applyTopPadding(RemoteViews contentView) { 3469 int topPadding = Builder.calculateTopPadding(mBuilder.mContext, 3470 mBuilder.mHasThreeLines, 3471 mBuilder.mContext.getResources().getConfiguration().fontScale); 3472 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0); 3473 } 3474 3475 /** 3476 * @hide 3477 */ 3478 public void addExtras(Bundle extras) { 3479 if (mSummaryTextSet) { 3480 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText); 3481 } 3482 if (mBigContentTitle != null) { 3483 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle); 3484 } 3485 extras.putString(EXTRA_TEMPLATE, this.getClass().getName()); 3486 } 3487 3488 /** 3489 * @hide 3490 */ 3491 protected void restoreFromExtras(Bundle extras) { 3492 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) { 3493 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT); 3494 mSummaryTextSet = true; 3495 } 3496 if (extras.containsKey(EXTRA_TITLE_BIG)) { 3497 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG); 3498 } 3499 } 3500 3501 3502 /** 3503 * @hide 3504 */ 3505 public Notification buildStyled(Notification wip) { 3506 populateTickerView(wip); 3507 populateContentView(wip); 3508 populateBigContentView(wip); 3509 populateHeadsUpContentView(wip); 3510 return wip; 3511 } 3512 3513 // The following methods are split out so we can re-create notification partially. 3514 /** 3515 * @hide 3516 */ 3517 protected void populateTickerView(Notification wip) {} 3518 /** 3519 * @hide 3520 */ 3521 protected void populateContentView(Notification wip) {} 3522 3523 /** 3524 * @hide 3525 */ 3526 protected void populateBigContentView(Notification wip) {} 3527 3528 /** 3529 * @hide 3530 */ 3531 protected void populateHeadsUpContentView(Notification wip) {} 3532 3533 /** 3534 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is 3535 * attached to. 3536 * 3537 * @return the fully constructed Notification. 3538 */ 3539 public Notification build() { 3540 checkBuilder(); 3541 return mBuilder.build(); 3542 } 3543 3544 /** 3545 * @hide 3546 * @return true if the style positions the progress bar on the second line; false if the 3547 * style hides the progress bar 3548 */ 3549 protected boolean hasProgress() { 3550 return true; 3551 } 3552 } 3553 3554 /** 3555 * Helper class for generating large-format notifications that include a large image attachment. 3556 * 3557 * Here's how you'd set the <code>BigPictureStyle</code> on a notification: 3558 * <pre class="prettyprint"> 3559 * Notification notif = new Notification.Builder(mContext) 3560 * .setContentTitle("New photo from " + sender.toString()) 3561 * .setContentText(subject) 3562 * .setSmallIcon(R.drawable.new_post) 3563 * .setLargeIcon(aBitmap) 3564 * .setStyle(new Notification.BigPictureStyle() 3565 * .bigPicture(aBigBitmap)) 3566 * .build(); 3567 * </pre> 3568 * 3569 * @see Notification#bigContentView 3570 */ 3571 public static class BigPictureStyle extends Style { 3572 private Bitmap mPicture; 3573 private Bitmap mBigLargeIcon; 3574 private boolean mBigLargeIconSet = false; 3575 3576 public BigPictureStyle() { 3577 } 3578 3579 public BigPictureStyle(Builder builder) { 3580 setBuilder(builder); 3581 } 3582 3583 /** 3584 * Overrides ContentTitle in the big form of the template. 3585 * This defaults to the value passed to setContentTitle(). 3586 */ 3587 public BigPictureStyle setBigContentTitle(CharSequence title) { 3588 internalSetBigContentTitle(safeCharSequence(title)); 3589 return this; 3590 } 3591 3592 /** 3593 * Set the first line of text after the detail section in the big form of the template. 3594 */ 3595 public BigPictureStyle setSummaryText(CharSequence cs) { 3596 internalSetSummaryText(safeCharSequence(cs)); 3597 return this; 3598 } 3599 3600 /** 3601 * Provide the bitmap to be used as the payload for the BigPicture notification. 3602 */ 3603 public BigPictureStyle bigPicture(Bitmap b) { 3604 mPicture = b; 3605 return this; 3606 } 3607 3608 /** 3609 * Override the large icon when the big notification is shown. 3610 */ 3611 public BigPictureStyle bigLargeIcon(Bitmap b) { 3612 mBigLargeIconSet = true; 3613 mBigLargeIcon = b; 3614 return this; 3615 } 3616 3617 private RemoteViews makeBigContentView() { 3618 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource()); 3619 3620 contentView.setImageViewBitmap(R.id.big_picture, mPicture); 3621 3622 applyTopPadding(contentView); 3623 3624 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null; 3625 mBuilder.addProfileBadge(contentView, 3626 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3); 3627 return contentView; 3628 } 3629 3630 /** 3631 * @hide 3632 */ 3633 public void addExtras(Bundle extras) { 3634 super.addExtras(extras); 3635 3636 if (mBigLargeIconSet) { 3637 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon); 3638 } 3639 extras.putParcelable(EXTRA_PICTURE, mPicture); 3640 } 3641 3642 /** 3643 * @hide 3644 */ 3645 @Override 3646 protected void restoreFromExtras(Bundle extras) { 3647 super.restoreFromExtras(extras); 3648 3649 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) { 3650 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG); 3651 } 3652 mPicture = extras.getParcelable(EXTRA_PICTURE); 3653 } 3654 3655 /** 3656 * @hide 3657 */ 3658 @Override 3659 public void populateBigContentView(Notification wip) { 3660 mBuilder.setBuilderBigContentView(wip, makeBigContentView()); 3661 } 3662 } 3663 3664 /** 3665 * Helper class for generating large-format notifications that include a lot of text. 3666 * 3667 * Here's how you'd set the <code>BigTextStyle</code> on a notification: 3668 * <pre class="prettyprint"> 3669 * Notification notif = new Notification.Builder(mContext) 3670 * .setContentTitle("New mail from " + sender.toString()) 3671 * .setContentText(subject) 3672 * .setSmallIcon(R.drawable.new_mail) 3673 * .setLargeIcon(aBitmap) 3674 * .setStyle(new Notification.BigTextStyle() 3675 * .bigText(aVeryLongString)) 3676 * .build(); 3677 * </pre> 3678 * 3679 * @see Notification#bigContentView 3680 */ 3681 public static class BigTextStyle extends Style { 3682 private CharSequence mBigText; 3683 3684 public BigTextStyle() { 3685 } 3686 3687 public BigTextStyle(Builder builder) { 3688 setBuilder(builder); 3689 } 3690 3691 /** 3692 * Overrides ContentTitle in the big form of the template. 3693 * This defaults to the value passed to setContentTitle(). 3694 */ 3695 public BigTextStyle setBigContentTitle(CharSequence title) { 3696 internalSetBigContentTitle(safeCharSequence(title)); 3697 return this; 3698 } 3699 3700 /** 3701 * Set the first line of text after the detail section in the big form of the template. 3702 */ 3703 public BigTextStyle setSummaryText(CharSequence cs) { 3704 internalSetSummaryText(safeCharSequence(cs)); 3705 return this; 3706 } 3707 3708 /** 3709 * Provide the longer text to be displayed in the big form of the 3710 * template in place of the content text. 3711 */ 3712 public BigTextStyle bigText(CharSequence cs) { 3713 mBigText = safeCharSequence(cs); 3714 return this; 3715 } 3716 3717 /** 3718 * @hide 3719 */ 3720 public void addExtras(Bundle extras) { 3721 super.addExtras(extras); 3722 3723 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText); 3724 } 3725 3726 /** 3727 * @hide 3728 */ 3729 @Override 3730 protected void restoreFromExtras(Bundle extras) { 3731 super.restoreFromExtras(extras); 3732 3733 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT); 3734 } 3735 3736 private RemoteViews makeBigContentView() { 3737 3738 // Nasty 3739 CharSequence oldBuilderContentText = mBuilder.mContentText; 3740 mBuilder.mContentText = null; 3741 3742 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource()); 3743 3744 mBuilder.mContentText = oldBuilderContentText; 3745 3746 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText)); 3747 contentView.setViewVisibility(R.id.big_text, View.VISIBLE); 3748 contentView.setViewVisibility(R.id.text2, View.GONE); 3749 3750 applyTopPadding(contentView); 3751 3752 mBuilder.shrinkLine3Text(contentView); 3753 3754 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template); 3755 3756 return contentView; 3757 } 3758 3759 /** 3760 * @hide 3761 */ 3762 @Override 3763 public void populateBigContentView(Notification wip) { 3764 mBuilder.setBuilderBigContentView(wip, makeBigContentView()); 3765 } 3766 } 3767 3768 /** 3769 * Helper class for generating large-format notifications that include a list of (up to 5) strings. 3770 * 3771 * Here's how you'd set the <code>InboxStyle</code> on a notification: 3772 * <pre class="prettyprint"> 3773 * Notification notif = new Notification.Builder(mContext) 3774 * .setContentTitle("5 New mails from " + sender.toString()) 3775 * .setContentText(subject) 3776 * .setSmallIcon(R.drawable.new_mail) 3777 * .setLargeIcon(aBitmap) 3778 * .setStyle(new Notification.InboxStyle() 3779 * .addLine(str1) 3780 * .addLine(str2) 3781 * .setContentTitle("") 3782 * .setSummaryText("+3 more")) 3783 * .build(); 3784 * </pre> 3785 * 3786 * @see Notification#bigContentView 3787 */ 3788 public static class InboxStyle extends Style { 3789 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5); 3790 3791 public InboxStyle() { 3792 } 3793 3794 public InboxStyle(Builder builder) { 3795 setBuilder(builder); 3796 } 3797 3798 /** 3799 * Overrides ContentTitle in the big form of the template. 3800 * This defaults to the value passed to setContentTitle(). 3801 */ 3802 public InboxStyle setBigContentTitle(CharSequence title) { 3803 internalSetBigContentTitle(safeCharSequence(title)); 3804 return this; 3805 } 3806 3807 /** 3808 * Set the first line of text after the detail section in the big form of the template. 3809 */ 3810 public InboxStyle setSummaryText(CharSequence cs) { 3811 internalSetSummaryText(safeCharSequence(cs)); 3812 return this; 3813 } 3814 3815 /** 3816 * Append a line to the digest section of the Inbox notification. 3817 */ 3818 public InboxStyle addLine(CharSequence cs) { 3819 mTexts.add(safeCharSequence(cs)); 3820 return this; 3821 } 3822 3823 /** 3824 * @hide 3825 */ 3826 public void addExtras(Bundle extras) { 3827 super.addExtras(extras); 3828 3829 CharSequence[] a = new CharSequence[mTexts.size()]; 3830 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a)); 3831 } 3832 3833 /** 3834 * @hide 3835 */ 3836 @Override 3837 protected void restoreFromExtras(Bundle extras) { 3838 super.restoreFromExtras(extras); 3839 3840 mTexts.clear(); 3841 if (extras.containsKey(EXTRA_TEXT_LINES)) { 3842 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES)); 3843 } 3844 } 3845 3846 private RemoteViews makeBigContentView() { 3847 // Remove the content text so line3 disappears unless you have a summary 3848 3849 // Nasty 3850 CharSequence oldBuilderContentText = mBuilder.mContentText; 3851 mBuilder.mContentText = null; 3852 3853 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource()); 3854 3855 mBuilder.mContentText = oldBuilderContentText; 3856 3857 contentView.setViewVisibility(R.id.text2, View.GONE); 3858 3859 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3, 3860 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6}; 3861 3862 // Make sure all rows are gone in case we reuse a view. 3863 for (int rowId : rowIds) { 3864 contentView.setViewVisibility(rowId, View.GONE); 3865 } 3866 3867 final boolean largeText = 3868 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f; 3869 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize( 3870 R.dimen.notification_subtext_size); 3871 int i=0; 3872 while (i < mTexts.size() && i < rowIds.length) { 3873 CharSequence str = mTexts.get(i); 3874 if (str != null && !str.equals("")) { 3875 contentView.setViewVisibility(rowIds[i], View.VISIBLE); 3876 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str)); 3877 if (largeText) { 3878 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX, 3879 subTextSize); 3880 } 3881 } 3882 i++; 3883 } 3884 3885 contentView.setViewVisibility(R.id.inbox_end_pad, 3886 mTexts.size() > 0 ? View.VISIBLE : View.GONE); 3887 3888 contentView.setViewVisibility(R.id.inbox_more, 3889 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE); 3890 3891 applyTopPadding(contentView); 3892 3893 mBuilder.shrinkLine3Text(contentView); 3894 3895 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template); 3896 3897 return contentView; 3898 } 3899 3900 /** 3901 * @hide 3902 */ 3903 @Override 3904 public void populateBigContentView(Notification wip) { 3905 mBuilder.setBuilderBigContentView(wip, makeBigContentView()); 3906 } 3907 } 3908 3909 /** 3910 * Notification style for media playback notifications. 3911 * 3912 * In the expanded form, {@link Notification#bigContentView}, up to 5 3913 * {@link Notification.Action}s specified with 3914 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be 3915 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to 3916 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be 3917 * treated as album artwork. 3918 * 3919 * Unlike the other styles provided here, MediaStyle can also modify the standard-size 3920 * {@link Notification#contentView}; by providing action indices to 3921 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed 3922 * in the standard view alongside the usual content. 3923 * 3924 * Notifications created with MediaStyle will have their category set to 3925 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different 3926 * category using {@link Notification.Builder#setCategory(String) setCategory()}. 3927 * 3928 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using 3929 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)}, 3930 * the System UI can identify this as a notification representing an active media session 3931 * and respond accordingly (by showing album artwork in the lockscreen, for example). 3932 * 3933 * To use this style with your Notification, feed it to 3934 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so: 3935 * <pre class="prettyprint"> 3936 * Notification noti = new Notification.Builder() 3937 * .setSmallIcon(R.drawable.ic_stat_player) 3938 * .setContentTitle("Track title") // these three lines are optional 3939 * .setContentText("Artist - Album") // if you use 3940 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token) 3941 * .setStyle(<b>new Notification.MediaStyle()</b> 3942 * .setMediaSession(mySession)) 3943 * .build(); 3944 * </pre> 3945 * 3946 * @see Notification#bigContentView 3947 */ 3948 public static class MediaStyle extends Style { 3949 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3; 3950 static final int MAX_MEDIA_BUTTONS = 5; 3951 3952 private int[] mActionsToShowInCompact = null; 3953 private MediaSession.Token mToken; 3954 3955 public MediaStyle() { 3956 } 3957 3958 public MediaStyle(Builder builder) { 3959 setBuilder(builder); 3960 } 3961 3962 /** 3963 * Request up to 3 actions (by index in the order of addition) to be shown in the compact 3964 * notification view. 3965 * 3966 * @param actions the indices of the actions to show in the compact notification view 3967 */ 3968 public MediaStyle setShowActionsInCompactView(int...actions) { 3969 mActionsToShowInCompact = actions; 3970 return this; 3971 } 3972 3973 /** 3974 * Attach a {@link android.media.session.MediaSession.Token} to this Notification 3975 * to provide additional playback information and control to the SystemUI. 3976 */ 3977 public MediaStyle setMediaSession(MediaSession.Token token) { 3978 mToken = token; 3979 return this; 3980 } 3981 3982 /** 3983 * @hide 3984 */ 3985 @Override 3986 public Notification buildStyled(Notification wip) { 3987 super.buildStyled(wip); 3988 if (wip.category == null) { 3989 wip.category = Notification.CATEGORY_TRANSPORT; 3990 } 3991 return wip; 3992 } 3993 3994 /** 3995 * @hide 3996 */ 3997 @Override 3998 public void populateContentView(Notification wip) { 3999 mBuilder.setBuilderContentView(wip, makeMediaContentView()); 4000 } 4001 4002 /** 4003 * @hide 4004 */ 4005 @Override 4006 public void populateBigContentView(Notification wip) { 4007 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView()); 4008 } 4009 4010 /** @hide */ 4011 @Override 4012 public void addExtras(Bundle extras) { 4013 super.addExtras(extras); 4014 4015 if (mToken != null) { 4016 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken); 4017 } 4018 if (mActionsToShowInCompact != null) { 4019 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact); 4020 } 4021 } 4022 4023 /** 4024 * @hide 4025 */ 4026 @Override 4027 protected void restoreFromExtras(Bundle extras) { 4028 super.restoreFromExtras(extras); 4029 4030 if (extras.containsKey(EXTRA_MEDIA_SESSION)) { 4031 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION); 4032 } 4033 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) { 4034 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS); 4035 } 4036 } 4037 4038 private RemoteViews generateMediaActionButton(Action action) { 4039 final boolean tombstone = (action.actionIntent == null); 4040 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(), 4041 R.layout.notification_material_media_action); 4042 button.setImageViewResource(R.id.action0, action.icon); 4043 button.setDrawableParameters(R.id.action0, false, -1, 4044 0xFFFFFFFF, 4045 PorterDuff.Mode.SRC_ATOP, -1); 4046 if (!tombstone) { 4047 button.setOnClickPendingIntent(R.id.action0, action.actionIntent); 4048 } 4049 button.setContentDescription(R.id.action0, action.title); 4050 return button; 4051 } 4052 4053 private RemoteViews makeMediaContentView() { 4054 RemoteViews view = mBuilder.applyStandardTemplate( 4055 R.layout.notification_template_material_media, false /* hasProgress */); 4056 4057 final int numActions = mBuilder.mActions.size(); 4058 final int N = mActionsToShowInCompact == null 4059 ? 0 4060 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT); 4061 if (N > 0) { 4062 view.removeAllViews(com.android.internal.R.id.media_actions); 4063 for (int i = 0; i < N; i++) { 4064 if (i >= numActions) { 4065 throw new IllegalArgumentException(String.format( 4066 "setShowActionsInCompactView: action %d out of bounds (max %d)", 4067 i, numActions - 1)); 4068 } 4069 4070 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]); 4071 final RemoteViews button = generateMediaActionButton(action); 4072 view.addView(com.android.internal.R.id.media_actions, button); 4073 } 4074 } 4075 styleText(view); 4076 hideRightIcon(view); 4077 return view; 4078 } 4079 4080 private RemoteViews makeMediaBigContentView() { 4081 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS); 4082 RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount), 4083 false /* hasProgress */); 4084 4085 if (actionCount > 0) { 4086 big.removeAllViews(com.android.internal.R.id.media_actions); 4087 for (int i = 0; i < actionCount; i++) { 4088 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i)); 4089 big.addView(com.android.internal.R.id.media_actions, button); 4090 } 4091 } 4092 styleText(big); 4093 hideRightIcon(big); 4094 applyTopPadding(big); 4095 big.setViewVisibility(android.R.id.progress, View.GONE); 4096 return big; 4097 } 4098 4099 private int getBigLayoutResource(int actionCount) { 4100 if (actionCount <= 3) { 4101 return R.layout.notification_template_material_big_media_narrow; 4102 } else { 4103 return R.layout.notification_template_material_big_media; 4104 } 4105 } 4106 4107 private void hideRightIcon(RemoteViews contentView) { 4108 contentView.setViewVisibility(R.id.right_icon, View.GONE); 4109 } 4110 4111 /** 4112 * Applies the special text colors for media notifications to all text views. 4113 */ 4114 private void styleText(RemoteViews contentView) { 4115 int primaryColor = mBuilder.mContext.getResources().getColor( 4116 R.color.notification_media_primary_color); 4117 int secondaryColor = mBuilder.mContext.getResources().getColor( 4118 R.color.notification_media_secondary_color); 4119 contentView.setTextColor(R.id.title, primaryColor); 4120 if (mBuilder.showsTimeOrChronometer()) { 4121 if (mBuilder.mUseChronometer) { 4122 contentView.setTextColor(R.id.chronometer, secondaryColor); 4123 } else { 4124 contentView.setTextColor(R.id.time, secondaryColor); 4125 } 4126 } 4127 contentView.setTextColor(R.id.text2, secondaryColor); 4128 contentView.setTextColor(R.id.text, secondaryColor); 4129 contentView.setTextColor(R.id.info, secondaryColor); 4130 } 4131 4132 /** 4133 * @hide 4134 */ 4135 @Override 4136 protected boolean hasProgress() { 4137 return false; 4138 } 4139 } 4140 4141 // When adding a new Style subclass here, don't forget to update 4142 // Builder.getNotificationStyleClass. 4143 4144 /** 4145 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add 4146 * metadata or change options on a notification builder. 4147 */ 4148 public interface Extender { 4149 /** 4150 * Apply this extender to a notification builder. 4151 * @param builder the builder to be modified. 4152 * @return the build object for chaining. 4153 */ 4154 public Builder extend(Builder builder); 4155 } 4156 4157 /** 4158 * Helper class to add wearable extensions to notifications. 4159 * <p class="note"> See 4160 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications 4161 * for Android Wear</a> for more information on how to use this class. 4162 * <p> 4163 * To create a notification with wearable extensions: 4164 * <ol> 4165 * <li>Create a {@link android.app.Notification.Builder}, setting any desired 4166 * properties. 4167 * <li>Create a {@link android.app.Notification.WearableExtender}. 4168 * <li>Set wearable-specific properties using the 4169 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}. 4170 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a 4171 * notification. 4172 * <li>Post the notification to the notification system with the 4173 * {@code NotificationManager.notify(...)} methods. 4174 * </ol> 4175 * 4176 * <pre class="prettyprint"> 4177 * Notification notif = new Notification.Builder(mContext) 4178 * .setContentTitle("New mail from " + sender.toString()) 4179 * .setContentText(subject) 4180 * .setSmallIcon(R.drawable.new_mail) 4181 * .extend(new Notification.WearableExtender() 4182 * .setContentIcon(R.drawable.new_mail)) 4183 * .build(); 4184 * NotificationManager notificationManger = 4185 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 4186 * notificationManger.notify(0, notif);</pre> 4187 * 4188 * <p>Wearable extensions can be accessed on an existing notification by using the 4189 * {@code WearableExtender(Notification)} constructor, 4190 * and then using the {@code get} methods to access values. 4191 * 4192 * <pre class="prettyprint"> 4193 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender( 4194 * notification); 4195 * List<Notification> pages = wearableExtender.getPages();</pre> 4196 */ 4197 public static final class WearableExtender implements Extender { 4198 /** 4199 * Sentinel value for an action index that is unset. 4200 */ 4201 public static final int UNSET_ACTION_INDEX = -1; 4202 4203 /** 4204 * Size value for use with {@link #setCustomSizePreset} to show this notification with 4205 * default sizing. 4206 * <p>For custom display notifications created using {@link #setDisplayIntent}, 4207 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based 4208 * on their content. 4209 */ 4210 public static final int SIZE_DEFAULT = 0; 4211 4212 /** 4213 * Size value for use with {@link #setCustomSizePreset} to show this notification 4214 * with an extra small size. 4215 * <p>This value is only applicable for custom display notifications created using 4216 * {@link #setDisplayIntent}. 4217 */ 4218 public static final int SIZE_XSMALL = 1; 4219 4220 /** 4221 * Size value for use with {@link #setCustomSizePreset} to show this notification 4222 * with a small size. 4223 * <p>This value is only applicable for custom display notifications created using 4224 * {@link #setDisplayIntent}. 4225 */ 4226 public static final int SIZE_SMALL = 2; 4227 4228 /** 4229 * Size value for use with {@link #setCustomSizePreset} to show this notification 4230 * with a medium size. 4231 * <p>This value is only applicable for custom display notifications created using 4232 * {@link #setDisplayIntent}. 4233 */ 4234 public static final int SIZE_MEDIUM = 3; 4235 4236 /** 4237 * Size value for use with {@link #setCustomSizePreset} to show this notification 4238 * with a large size. 4239 * <p>This value is only applicable for custom display notifications created using 4240 * {@link #setDisplayIntent}. 4241 */ 4242 public static final int SIZE_LARGE = 4; 4243 4244 /** 4245 * Size value for use with {@link #setCustomSizePreset} to show this notification 4246 * full screen. 4247 * <p>This value is only applicable for custom display notifications created using 4248 * {@link #setDisplayIntent}. 4249 */ 4250 public static final int SIZE_FULL_SCREEN = 5; 4251 4252 /** Notification extra which contains wearable extensions */ 4253 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS"; 4254 4255 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options. 4256 private static final String KEY_ACTIONS = "actions"; 4257 private static final String KEY_FLAGS = "flags"; 4258 private static final String KEY_DISPLAY_INTENT = "displayIntent"; 4259 private static final String KEY_PAGES = "pages"; 4260 private static final String KEY_BACKGROUND = "background"; 4261 private static final String KEY_CONTENT_ICON = "contentIcon"; 4262 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity"; 4263 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex"; 4264 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset"; 4265 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight"; 4266 private static final String KEY_GRAVITY = "gravity"; 4267 4268 // Flags bitwise-ored to mFlags 4269 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1; 4270 private static final int FLAG_HINT_HIDE_ICON = 1 << 1; 4271 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2; 4272 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3; 4273 4274 // Default value for flags integer 4275 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE; 4276 4277 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END; 4278 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM; 4279 4280 private ArrayList<Action> mActions = new ArrayList<Action>(); 4281 private int mFlags = DEFAULT_FLAGS; 4282 private PendingIntent mDisplayIntent; 4283 private ArrayList<Notification> mPages = new ArrayList<Notification>(); 4284 private Bitmap mBackground; 4285 private int mContentIcon; 4286 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY; 4287 private int mContentActionIndex = UNSET_ACTION_INDEX; 4288 private int mCustomSizePreset = SIZE_DEFAULT; 4289 private int mCustomContentHeight; 4290 private int mGravity = DEFAULT_GRAVITY; 4291 4292 /** 4293 * Create a {@link android.app.Notification.WearableExtender} with default 4294 * options. 4295 */ 4296 public WearableExtender() { 4297 } 4298 4299 public WearableExtender(Notification notif) { 4300 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS); 4301 if (wearableBundle != null) { 4302 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS); 4303 if (actions != null) { 4304 mActions.addAll(actions); 4305 } 4306 4307 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS); 4308 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT); 4309 4310 Notification[] pages = getNotificationArrayFromBundle( 4311 wearableBundle, KEY_PAGES); 4312 if (pages != null) { 4313 Collections.addAll(mPages, pages); 4314 } 4315 4316 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND); 4317 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON); 4318 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY, 4319 DEFAULT_CONTENT_ICON_GRAVITY); 4320 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX, 4321 UNSET_ACTION_INDEX); 4322 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET, 4323 SIZE_DEFAULT); 4324 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT); 4325 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY); 4326 } 4327 } 4328 4329 /** 4330 * Apply wearable extensions to a notification that is being built. This is typically 4331 * called by the {@link android.app.Notification.Builder#extend} method of 4332 * {@link android.app.Notification.Builder}. 4333 */ 4334 @Override 4335 public Notification.Builder extend(Notification.Builder builder) { 4336 Bundle wearableBundle = new Bundle(); 4337 4338 if (!mActions.isEmpty()) { 4339 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions); 4340 } 4341 if (mFlags != DEFAULT_FLAGS) { 4342 wearableBundle.putInt(KEY_FLAGS, mFlags); 4343 } 4344 if (mDisplayIntent != null) { 4345 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent); 4346 } 4347 if (!mPages.isEmpty()) { 4348 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray( 4349 new Notification[mPages.size()])); 4350 } 4351 if (mBackground != null) { 4352 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground); 4353 } 4354 if (mContentIcon != 0) { 4355 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon); 4356 } 4357 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) { 4358 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity); 4359 } 4360 if (mContentActionIndex != UNSET_ACTION_INDEX) { 4361 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX, 4362 mContentActionIndex); 4363 } 4364 if (mCustomSizePreset != SIZE_DEFAULT) { 4365 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset); 4366 } 4367 if (mCustomContentHeight != 0) { 4368 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight); 4369 } 4370 if (mGravity != DEFAULT_GRAVITY) { 4371 wearableBundle.putInt(KEY_GRAVITY, mGravity); 4372 } 4373 4374 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle); 4375 return builder; 4376 } 4377 4378 @Override 4379 public WearableExtender clone() { 4380 WearableExtender that = new WearableExtender(); 4381 that.mActions = new ArrayList<Action>(this.mActions); 4382 that.mFlags = this.mFlags; 4383 that.mDisplayIntent = this.mDisplayIntent; 4384 that.mPages = new ArrayList<Notification>(this.mPages); 4385 that.mBackground = this.mBackground; 4386 that.mContentIcon = this.mContentIcon; 4387 that.mContentIconGravity = this.mContentIconGravity; 4388 that.mContentActionIndex = this.mContentActionIndex; 4389 that.mCustomSizePreset = this.mCustomSizePreset; 4390 that.mCustomContentHeight = this.mCustomContentHeight; 4391 that.mGravity = this.mGravity; 4392 return that; 4393 } 4394 4395 /** 4396 * Add a wearable action to this notification. 4397 * 4398 * <p>When wearable actions are added using this method, the set of actions that 4399 * show on a wearable device splits from devices that only show actions added 4400 * using {@link android.app.Notification.Builder#addAction}. This allows for customization 4401 * of which actions display on different devices. 4402 * 4403 * @param action the action to add to this notification 4404 * @return this object for method chaining 4405 * @see android.app.Notification.Action 4406 */ 4407 public WearableExtender addAction(Action action) { 4408 mActions.add(action); 4409 return this; 4410 } 4411 4412 /** 4413 * Adds wearable actions to this notification. 4414 * 4415 * <p>When wearable actions are added using this method, the set of actions that 4416 * show on a wearable device splits from devices that only show actions added 4417 * using {@link android.app.Notification.Builder#addAction}. This allows for customization 4418 * of which actions display on different devices. 4419 * 4420 * @param actions the actions to add to this notification 4421 * @return this object for method chaining 4422 * @see android.app.Notification.Action 4423 */ 4424 public WearableExtender addActions(List<Action> actions) { 4425 mActions.addAll(actions); 4426 return this; 4427 } 4428 4429 /** 4430 * Clear all wearable actions present on this builder. 4431 * @return this object for method chaining. 4432 * @see #addAction 4433 */ 4434 public WearableExtender clearActions() { 4435 mActions.clear(); 4436 return this; 4437 } 4438 4439 /** 4440 * Get the wearable actions present on this notification. 4441 */ 4442 public List<Action> getActions() { 4443 return mActions; 4444 } 4445 4446 /** 4447 * Set an intent to launch inside of an activity view when displaying 4448 * this notification. The {@link PendingIntent} provided should be for an activity. 4449 * 4450 * <pre class="prettyprint"> 4451 * Intent displayIntent = new Intent(context, MyDisplayActivity.class); 4452 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context, 4453 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT); 4454 * Notification notif = new Notification.Builder(context) 4455 * .extend(new Notification.WearableExtender() 4456 * .setDisplayIntent(displayPendingIntent) 4457 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM)) 4458 * .build();</pre> 4459 * 4460 * <p>The activity to launch needs to allow embedding, must be exported, and 4461 * should have an empty task affinity. It is also recommended to use the device 4462 * default light theme. 4463 * 4464 * <p>Example AndroidManifest.xml entry: 4465 * <pre class="prettyprint"> 4466 * <activity android:name="com.example.MyDisplayActivity" 4467 * android:exported="true" 4468 * android:allowEmbedded="true" 4469 * android:taskAffinity="" 4470 * android:theme="@android:style/Theme.DeviceDefault.Light" /></pre> 4471 * 4472 * @param intent the {@link PendingIntent} for an activity 4473 * @return this object for method chaining 4474 * @see android.app.Notification.WearableExtender#getDisplayIntent 4475 */ 4476 public WearableExtender setDisplayIntent(PendingIntent intent) { 4477 mDisplayIntent = intent; 4478 return this; 4479 } 4480 4481 /** 4482 * Get the intent to launch inside of an activity view when displaying this 4483 * notification. This {@code PendingIntent} should be for an activity. 4484 */ 4485 public PendingIntent getDisplayIntent() { 4486 return mDisplayIntent; 4487 } 4488 4489 /** 4490 * Add an additional page of content to display with this notification. The current 4491 * notification forms the first page, and pages added using this function form 4492 * subsequent pages. This field can be used to separate a notification into multiple 4493 * sections. 4494 * 4495 * @param page the notification to add as another page 4496 * @return this object for method chaining 4497 * @see android.app.Notification.WearableExtender#getPages 4498 */ 4499 public WearableExtender addPage(Notification page) { 4500 mPages.add(page); 4501 return this; 4502 } 4503 4504 /** 4505 * Add additional pages of content to display with this notification. The current 4506 * notification forms the first page, and pages added using this function form 4507 * subsequent pages. This field can be used to separate a notification into multiple 4508 * sections. 4509 * 4510 * @param pages a list of notifications 4511 * @return this object for method chaining 4512 * @see android.app.Notification.WearableExtender#getPages 4513 */ 4514 public WearableExtender addPages(List<Notification> pages) { 4515 mPages.addAll(pages); 4516 return this; 4517 } 4518 4519 /** 4520 * Clear all additional pages present on this builder. 4521 * @return this object for method chaining. 4522 * @see #addPage 4523 */ 4524 public WearableExtender clearPages() { 4525 mPages.clear(); 4526 return this; 4527 } 4528 4529 /** 4530 * Get the array of additional pages of content for displaying this notification. The 4531 * current notification forms the first page, and elements within this array form 4532 * subsequent pages. This field can be used to separate a notification into multiple 4533 * sections. 4534 * @return the pages for this notification 4535 */ 4536 public List<Notification> getPages() { 4537 return mPages; 4538 } 4539 4540 /** 4541 * Set a background image to be displayed behind the notification content. 4542 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background 4543 * will work with any notification style. 4544 * 4545 * @param background the background bitmap 4546 * @return this object for method chaining 4547 * @see android.app.Notification.WearableExtender#getBackground 4548 */ 4549 public WearableExtender setBackground(Bitmap background) { 4550 mBackground = background; 4551 return this; 4552 } 4553 4554 /** 4555 * Get a background image to be displayed behind the notification content. 4556 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background 4557 * will work with any notification style. 4558 * 4559 * @return the background image 4560 * @see android.app.Notification.WearableExtender#setBackground 4561 */ 4562 public Bitmap getBackground() { 4563 return mBackground; 4564 } 4565 4566 /** 4567 * Set an icon that goes with the content of this notification. 4568 */ 4569 public WearableExtender setContentIcon(int icon) { 4570 mContentIcon = icon; 4571 return this; 4572 } 4573 4574 /** 4575 * Get an icon that goes with the content of this notification. 4576 */ 4577 public int getContentIcon() { 4578 return mContentIcon; 4579 } 4580 4581 /** 4582 * Set the gravity that the content icon should have within the notification display. 4583 * Supported values include {@link android.view.Gravity#START} and 4584 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}. 4585 * @see #setContentIcon 4586 */ 4587 public WearableExtender setContentIconGravity(int contentIconGravity) { 4588 mContentIconGravity = contentIconGravity; 4589 return this; 4590 } 4591 4592 /** 4593 * Get the gravity that the content icon should have within the notification display. 4594 * Supported values include {@link android.view.Gravity#START} and 4595 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}. 4596 * @see #getContentIcon 4597 */ 4598 public int getContentIconGravity() { 4599 return mContentIconGravity; 4600 } 4601 4602 /** 4603 * Set an action from this notification's actions to be clickable with the content of 4604 * this notification. This action will no longer display separately from the 4605 * notification's content. 4606 * 4607 * <p>For notifications with multiple pages, child pages can also have content actions 4608 * set, although the list of available actions comes from the main notification and not 4609 * from the child page's notification. 4610 * 4611 * @param actionIndex The index of the action to hoist onto the current notification page. 4612 * If wearable actions were added to the main notification, this index 4613 * will apply to that list, otherwise it will apply to the regular 4614 * actions list. 4615 */ 4616 public WearableExtender setContentAction(int actionIndex) { 4617 mContentActionIndex = actionIndex; 4618 return this; 4619 } 4620 4621 /** 4622 * Get the index of the notification action, if any, that was specified as being clickable 4623 * with the content of this notification. This action will no longer display separately 4624 * from the notification's content. 4625 * 4626 * <p>For notifications with multiple pages, child pages can also have content actions 4627 * set, although the list of available actions comes from the main notification and not 4628 * from the child page's notification. 4629 * 4630 * <p>If wearable specific actions were added to the main notification, this index will 4631 * apply to that list, otherwise it will apply to the regular actions list. 4632 * 4633 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected. 4634 */ 4635 public int getContentAction() { 4636 return mContentActionIndex; 4637 } 4638 4639 /** 4640 * Set the gravity that this notification should have within the available viewport space. 4641 * Supported values include {@link android.view.Gravity#TOP}, 4642 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}. 4643 * The default value is {@link android.view.Gravity#BOTTOM}. 4644 */ 4645 public WearableExtender setGravity(int gravity) { 4646 mGravity = gravity; 4647 return this; 4648 } 4649 4650 /** 4651 * Get the gravity that this notification should have within the available viewport space. 4652 * Supported values include {@link android.view.Gravity#TOP}, 4653 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}. 4654 * The default value is {@link android.view.Gravity#BOTTOM}. 4655 */ 4656 public int getGravity() { 4657 return mGravity; 4658 } 4659 4660 /** 4661 * Set the custom size preset for the display of this notification out of the available 4662 * presets found in {@link android.app.Notification.WearableExtender}, e.g. 4663 * {@link #SIZE_LARGE}. 4664 * <p>Some custom size presets are only applicable for custom display notifications created 4665 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the 4666 * documentation for the preset in question. See also 4667 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}. 4668 */ 4669 public WearableExtender setCustomSizePreset(int sizePreset) { 4670 mCustomSizePreset = sizePreset; 4671 return this; 4672 } 4673 4674 /** 4675 * Get the custom size preset for the display of this notification out of the available 4676 * presets found in {@link android.app.Notification.WearableExtender}, e.g. 4677 * {@link #SIZE_LARGE}. 4678 * <p>Some custom size presets are only applicable for custom display notifications created 4679 * using {@link #setDisplayIntent}. Check the documentation for the preset in question. 4680 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}. 4681 */ 4682 public int getCustomSizePreset() { 4683 return mCustomSizePreset; 4684 } 4685 4686 /** 4687 * Set the custom height in pixels for the display of this notification's content. 4688 * <p>This option is only available for custom display notifications created 4689 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also 4690 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and 4691 * {@link #getCustomContentHeight}. 4692 */ 4693 public WearableExtender setCustomContentHeight(int height) { 4694 mCustomContentHeight = height; 4695 return this; 4696 } 4697 4698 /** 4699 * Get the custom height in pixels for the display of this notification's content. 4700 * <p>This option is only available for custom display notifications created 4701 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and 4702 * {@link #setCustomContentHeight}. 4703 */ 4704 public int getCustomContentHeight() { 4705 return mCustomContentHeight; 4706 } 4707 4708 /** 4709 * Set whether the scrolling position for the contents of this notification should start 4710 * at the bottom of the contents instead of the top when the contents are too long to 4711 * display within the screen. Default is false (start scroll at the top). 4712 */ 4713 public WearableExtender setStartScrollBottom(boolean startScrollBottom) { 4714 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom); 4715 return this; 4716 } 4717 4718 /** 4719 * Get whether the scrolling position for the contents of this notification should start 4720 * at the bottom of the contents instead of the top when the contents are too long to 4721 * display within the screen. Default is false (start scroll at the top). 4722 */ 4723 public boolean getStartScrollBottom() { 4724 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0; 4725 } 4726 4727 /** 4728 * Set whether the content intent is available when the wearable device is not connected 4729 * to a companion device. The user can still trigger this intent when the wearable device 4730 * is offline, but a visual hint will indicate that the content intent may not be available. 4731 * Defaults to true. 4732 */ 4733 public WearableExtender setContentIntentAvailableOffline( 4734 boolean contentIntentAvailableOffline) { 4735 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline); 4736 return this; 4737 } 4738 4739 /** 4740 * Get whether the content intent is available when the wearable device is not connected 4741 * to a companion device. The user can still trigger this intent when the wearable device 4742 * is offline, but a visual hint will indicate that the content intent may not be available. 4743 * Defaults to true. 4744 */ 4745 public boolean getContentIntentAvailableOffline() { 4746 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0; 4747 } 4748 4749 /** 4750 * Set a hint that this notification's icon should not be displayed. 4751 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise. 4752 * @return this object for method chaining 4753 */ 4754 public WearableExtender setHintHideIcon(boolean hintHideIcon) { 4755 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon); 4756 return this; 4757 } 4758 4759 /** 4760 * Get a hint that this notification's icon should not be displayed. 4761 * @return {@code true} if this icon should not be displayed, false otherwise. 4762 * The default value is {@code false} if this was never set. 4763 */ 4764 public boolean getHintHideIcon() { 4765 return (mFlags & FLAG_HINT_HIDE_ICON) != 0; 4766 } 4767 4768 /** 4769 * Set a visual hint that only the background image of this notification should be 4770 * displayed, and other semantic content should be hidden. This hint is only applicable 4771 * to sub-pages added using {@link #addPage}. 4772 */ 4773 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) { 4774 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly); 4775 return this; 4776 } 4777 4778 /** 4779 * Get a visual hint that only the background image of this notification should be 4780 * displayed, and other semantic content should be hidden. This hint is only applicable 4781 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}. 4782 */ 4783 public boolean getHintShowBackgroundOnly() { 4784 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0; 4785 } 4786 4787 private void setFlag(int mask, boolean value) { 4788 if (value) { 4789 mFlags |= mask; 4790 } else { 4791 mFlags &= ~mask; 4792 } 4793 } 4794 } 4795 4796 /** 4797 * Get an array of Notification objects from a parcelable array bundle field. 4798 * Update the bundle to have a typed array so fetches in the future don't need 4799 * to do an array copy. 4800 */ 4801 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) { 4802 Parcelable[] array = bundle.getParcelableArray(key); 4803 if (array instanceof Notification[] || array == null) { 4804 return (Notification[]) array; 4805 } 4806 Notification[] typedArray = Arrays.copyOf(array, array.length, 4807 Notification[].class); 4808 bundle.putParcelableArray(key, typedArray); 4809 return typedArray; 4810 } 4811 4812 private static class BuilderRemoteViews extends RemoteViews { 4813 public BuilderRemoteViews(Parcel parcel) { 4814 super(parcel); 4815 } 4816 4817 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) { 4818 super(appInfo, layoutId); 4819 } 4820 4821 @Override 4822 public BuilderRemoteViews clone() { 4823 Parcel p = Parcel.obtain(); 4824 writeToParcel(p, 0); 4825 p.setDataPosition(0); 4826 BuilderRemoteViews brv = new BuilderRemoteViews(p); 4827 p.recycle(); 4828 return brv; 4829 } 4830 } 4831} 4832