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