NotificationCompat.java revision bd066b7315dd0e86bd59f1b8fc892899a15ec5ee
1/*
2 * Copyright (C) 2012 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.support.v4.app;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.content.Context;
23import android.graphics.Bitmap;
24import android.media.AudioManager;
25import android.net.Uri;
26import android.os.Build;
27import android.os.Bundle;
28import android.widget.RemoteViews;
29import java.util.ArrayList;
30
31/**
32 * Helper for accessing features in {@link android.app.Notification}
33 * introduced after API level 4 in a backwards compatible fashion.
34 */
35public class NotificationCompat {
36
37    /**
38     * Use all default values (where applicable).
39     */
40    public static final int DEFAULT_ALL = ~0;
41
42    /**
43     * Use the default notification sound. This will ignore any sound set using
44     * {@link Builder#setSound}
45     *
46     * @see Builder#setDefaults
47     */
48    public static final int DEFAULT_SOUND = 1;
49
50    /**
51     * Use the default notification vibrate. This will ignore any vibrate set using
52     * {@link Builder#setVibrate}. Using phone vibration requires the
53     * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
54     *
55     * @see Builder#setDefaults
56     */
57    public static final int DEFAULT_VIBRATE = 2;
58
59    /**
60     * Use the default notification lights. This will ignore the
61     * {@link #FLAG_SHOW_LIGHTS} bit, and values set with {@link Builder#setLights}.
62     *
63     * @see Builder#setDefaults
64     */
65    public static final int DEFAULT_LIGHTS = 4;
66
67    /**
68     * Use this constant as the value for audioStreamType to request that
69     * the default stream type for notifications be used.  Currently the
70     * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
71     */
72    public static final int STREAM_DEFAULT = -1;
73
74    /**
75     * Bit set in the Notification flags field when LEDs should be turned on
76     * for this notification.
77     */
78    public static final int FLAG_SHOW_LIGHTS        = 0x00000001;
79
80    /**
81     * Bit set in the Notification flags field if this notification is in
82     * reference to something that is ongoing, like a phone call.  It should
83     * not be set if this notification is in reference to something that
84     * happened at a particular point in time, like a missed phone call.
85     */
86    public static final int FLAG_ONGOING_EVENT      = 0x00000002;
87
88    /**
89     * Bit set in the Notification flags field if
90     * the audio will be repeated until the notification is
91     * cancelled or the notification window is opened.
92     */
93    public static final int FLAG_INSISTENT          = 0x00000004;
94
95    /**
96     * Bit set in the Notification flags field if the notification's sound,
97     * vibrate and ticker should only be played if the notification is not already showing.
98     */
99    public static final int FLAG_ONLY_ALERT_ONCE    = 0x00000008;
100
101    /**
102     * Bit set in the Notification flags field if the notification should be canceled when
103     * it is clicked by the user.
104     */
105    public static final int FLAG_AUTO_CANCEL        = 0x00000010;
106
107    /**
108     * Bit set in the Notification flags field if the notification should not be canceled
109     * when the user clicks the Clear all button.
110     */
111    public static final int FLAG_NO_CLEAR           = 0x00000020;
112
113    /**
114     * Bit set in the Notification flags field if this notification represents a currently
115     * running service.  This will normally be set for you by
116     * {@link android.app.Service#startForeground}.
117     */
118    public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
119
120    /**
121     * Obsolete flag indicating high-priority notifications; use the priority field instead.
122     *
123     * @deprecated Use {@link NotificationCompat.Builder#setPriority(int)} with a positive value.
124     */
125    public static final int FLAG_HIGH_PRIORITY = 0x00000080;
126
127    /**
128     * Default notification priority for {@link NotificationCompat.Builder#setPriority(int)}.
129     * If your application does not prioritize its own notifications,
130     * use this value for all notifications.
131     */
132    public static final int PRIORITY_DEFAULT = 0;
133
134    /**
135     * Lower notification priority for {@link NotificationCompat.Builder#setPriority(int)},
136     * for items that are less important. The UI may choose to show
137     * these items smaller, or at a different position in the list,
138     * compared with your app's {@link #PRIORITY_DEFAULT} items.
139     */
140    public static final int PRIORITY_LOW = -1;
141
142    /**
143     * Lowest notification priority for {@link NotificationCompat.Builder#setPriority(int)};
144     * these items might not be shown to the user except under
145     * special circumstances, such as detailed notification logs.
146     */
147    public static final int PRIORITY_MIN = -2;
148
149    /**
150     * Higher notification priority for {@link NotificationCompat.Builder#setPriority(int)},
151     * for more important notifications or alerts. The UI may choose
152     * to show these items larger, or at a different position in
153     * notification lists, compared with your app's {@link #PRIORITY_DEFAULT} items.
154     */
155    public static final int PRIORITY_HIGH = 1;
156
157    /**
158     * Highest notification priority for {@link NotificationCompat.Builder#setPriority(int)},
159     * for your application's most important items that require the user's
160     * prompt attention or input.
161     */
162    public static final int PRIORITY_MAX = 2;
163
164    /**
165     * Notification extras key: this is the title of the notification,
166     * as supplied to {@link Builder#setContentTitle(CharSequence)}.
167     */
168    public static final String EXTRA_TITLE = "android.title";
169
170    /**
171     * Notification extras key: this is the title of the notification when shown in expanded form,
172     * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
173     */
174    public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
175
176    /**
177     * Notification extras key: this is the main text payload, as supplied to
178     * {@link Builder#setContentText(CharSequence)}.
179     */
180    public static final String EXTRA_TEXT = "android.text";
181
182    /**
183     * Notification extras key: this is a third line of text, as supplied to
184     * {@link Builder#setSubText(CharSequence)}.
185     */
186    public static final String EXTRA_SUB_TEXT = "android.subText";
187
188    /**
189     * Notification extras key: this is a small piece of additional text as supplied to
190     * {@link Builder#setContentInfo(CharSequence)}.
191     */
192    public static final String EXTRA_INFO_TEXT = "android.infoText";
193
194    /**
195     * Notification extras key: this is a line of summary information intended to be shown
196     * alongside expanded notifications, as supplied to (e.g.)
197     * {@link BigTextStyle#setSummaryText(CharSequence)}.
198     */
199    public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
200
201    /**
202     * Notification extras key: this is the resource ID of the notification's main small icon, as
203     * supplied to {@link Builder#setSmallIcon(int)}.
204     */
205    public static final String EXTRA_SMALL_ICON = "android.icon";
206
207    /**
208     * Notification extras key: this is a bitmap to be used instead of the small icon when showing the
209     * notification payload, as
210     * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
211     */
212    public static final String EXTRA_LARGE_ICON = "android.largeIcon";
213
214    /**
215     * Notification extras key: this is a bitmap to be used instead of the one from
216     * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
217     * shown in its expanded form, as supplied to
218     * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
219     */
220    public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
221
222    /**
223     * Notification extras key: this is the progress value supplied to
224     * {@link Builder#setProgress(int, int, boolean)}.
225     */
226    public static final String EXTRA_PROGRESS = "android.progress";
227
228    /**
229     * Notification extras key: this is the maximum value supplied to
230     * {@link Builder#setProgress(int, int, boolean)}.
231     */
232    public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
233
234    /**
235     * Notification extras key: whether the progress bar is indeterminate, supplied to
236     * {@link Builder#setProgress(int, int, boolean)}.
237     */
238    public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
239
240    /**
241     * Notification extras key: whether the when field set using {@link Builder#setWhen} should
242     * be shown as a count-up timer (specifically a {@link android.widget.Chronometer}) instead
243     * of a timestamp, as supplied to {@link Builder#setUsesChronometer(boolean)}.
244     */
245    public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
246
247    /**
248     * Notification extras key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
249     * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
250     */
251    public static final String EXTRA_PICTURE = "android.picture";
252
253    /**
254     * Notification extras key: An array of CharSequences to show in {@link InboxStyle} expanded
255     * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
256     */
257    public static final String EXTRA_TEXT_LINES = "android.textLines";
258
259    /**
260     * Notification extras key: An array of people that this notification relates to, specified
261     * by contacts provider contact URI.
262     */
263    public static final String EXTRA_PEOPLE = "android.people";
264
265    private static final NotificationCompatImpl IMPL;
266
267    interface NotificationCompatImpl {
268        public Notification build(Builder b);
269        public Bundle getExtras(Notification n);
270    }
271
272    static class NotificationCompatImplBase implements NotificationCompatImpl {
273        @Override
274        public Notification build(Builder b) {
275            Notification result = b.mNotification;
276            result.setLatestEventInfo(b.mContext, b.mContentTitle,
277                    b.mContentText, b.mContentIntent);
278            // translate high priority requests into legacy flag
279            if (b.mPriority > PRIORITY_DEFAULT) {
280                result.flags |= FLAG_HIGH_PRIORITY;
281            }
282            return result;
283        }
284
285        @Override
286        public Bundle getExtras(Notification n) {
287            return null;
288        }
289    }
290
291    static class NotificationCompatImplGingerbread extends NotificationCompatImplBase {
292        @Override
293        public Notification build(Builder b) {
294            Notification result = b.mNotification;
295            result.setLatestEventInfo(b.mContext, b.mContentTitle,
296                    b.mContentText, b.mContentIntent);
297            result = NotificationCompatGingerbread.add(result, b.mContext,
298                    b.mContentTitle, b.mContentText, b.mContentIntent, b.mFullScreenIntent);
299            // translate high priority requests into legacy flag
300            if (b.mPriority > PRIORITY_DEFAULT) {
301                result.flags |= FLAG_HIGH_PRIORITY;
302            }
303            return result;
304        }
305    }
306
307    static class NotificationCompatImplHoneycomb extends NotificationCompatImplBase {
308        @Override
309        public Notification build(Builder b) {
310            return NotificationCompatHoneycomb.add(b.mContext, b.mNotification,
311                    b.mContentTitle, b.mContentText, b.mContentInfo, b.mTickerView,
312                    b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon);
313        }
314    }
315
316    static class NotificationCompatImplIceCreamSandwich extends NotificationCompatImplBase {
317        @Override
318        public Notification build(Builder b) {
319            return NotificationCompatIceCreamSandwich.add(b.mContext, b.mNotification,
320                    b.mContentTitle, b.mContentText, b.mContentInfo, b.mTickerView,
321                    b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon,
322                    b.mProgressMax, b.mProgress, b.mProgressIndeterminate);
323        }
324    }
325
326    static class NotificationCompatImplJellybean extends NotificationCompatImplBase {
327        @Override
328        public Notification build(Builder b) {
329            NotificationCompatJellybean.Builder builder = new NotificationCompatJellybean.Builder(
330                    b.mContext, b.mNotification, b.mContentTitle, b.mContentText, b.mContentInfo,
331                    b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon,
332                    b.mProgressMax, b.mProgress, b.mProgressIndeterminate,
333                    b.mUseChronometer, b.mPriority, b.mSubText, b.mExtras);
334            addActionsToBuilder(builder, b.mActions);
335            addStyleToBuilderJellybean(builder, b.mStyle);
336            return builder.build();
337        }
338
339        @Override
340        public Bundle getExtras(Notification n) {
341            return NotificationCompatJellybean.getExtras(n);
342        }
343    }
344
345    static class NotificationCompatImplKitKat extends NotificationCompatImplBase {
346        @Override
347        public Notification build(Builder b) {
348            NotificationCompatKitKat.Builder builder = new NotificationCompatKitKat.Builder(
349                    b.mContext, b.mNotification, b.mContentTitle, b.mContentText, b.mContentInfo,
350                    b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon,
351                    b.mProgressMax, b.mProgress, b.mProgressIndeterminate,
352                    b.mUseChronometer, b.mPriority, b.mSubText, b.mExtras);
353            addActionsToBuilder(builder, b.mActions);
354            addStyleToBuilderJellybean(builder, b.mStyle);
355            return builder.build();
356        }
357
358        @Override
359        public Bundle getExtras(Notification n) {
360            return NotificationCompatKitKat.getExtras(n);
361        }
362    }
363
364    private static void addActionsToBuilder(NotificationBuilderWithActions builder,
365            ArrayList<Action> actions) {
366        for (Action action : actions) {
367            builder.addAction(action.icon, action.title, action.actionIntent);
368        }
369    }
370
371    private static void addStyleToBuilderJellybean(NotificationBuilderWithBuilderAccessor builder,
372            Style style) {
373        if (style != null) {
374            if (style instanceof BigTextStyle) {
375                BigTextStyle bigTextStyle = (BigTextStyle) style;
376                NotificationCompatJellybean.addBigTextStyle(builder,
377                        bigTextStyle.mBigContentTitle,
378                        bigTextStyle.mSummaryTextSet,
379                        bigTextStyle.mSummaryText,
380                        bigTextStyle.mBigText);
381            } else if (style instanceof InboxStyle) {
382                InboxStyle inboxStyle = (InboxStyle) style;
383                NotificationCompatJellybean.addInboxStyle(builder,
384                        inboxStyle.mBigContentTitle,
385                        inboxStyle.mSummaryTextSet,
386                        inboxStyle.mSummaryText,
387                        inboxStyle.mTexts);
388            } else if (style instanceof BigPictureStyle) {
389                BigPictureStyle bigPictureStyle = (BigPictureStyle) style;
390                NotificationCompatJellybean.addBigPictureStyle(builder,
391                        bigPictureStyle.mBigContentTitle,
392                        bigPictureStyle.mSummaryTextSet,
393                        bigPictureStyle.mSummaryText,
394                        bigPictureStyle.mPicture,
395                        bigPictureStyle.mBigLargeIcon,
396                        bigPictureStyle.mBigLargeIconSet);
397            }
398        }
399    }
400
401    static {
402        if (Build.VERSION.SDK_INT >= 19) {
403            IMPL = new NotificationCompatImplKitKat();
404        } else if (Build.VERSION.SDK_INT >= 16) {
405            IMPL = new NotificationCompatImplJellybean();
406        } else if (Build.VERSION.SDK_INT >= 14) {
407            IMPL = new NotificationCompatImplIceCreamSandwich();
408        } else if (Build.VERSION.SDK_INT >= 11) {
409            IMPL = new NotificationCompatImplHoneycomb();
410        } else if (Build.VERSION.SDK_INT >= 9) {
411            IMPL = new NotificationCompatImplGingerbread();
412        } else {
413            IMPL = new NotificationCompatImplBase();
414        }
415    }
416
417    /**
418     * Builder class for {@link NotificationCompat} objects.  Allows easier control over
419     * all the flags, as well as help constructing the typical notification layouts.
420     * <p>
421     * On platform versions that don't offer expanded notifications, methods that depend on
422     * expanded notifications have no effect.
423     * </p>
424     * <p>
425     * For example, action buttons won't appear on platforms prior to Android 4.1. Action
426     * buttons depend on expanded notifications, which are only available in Android 4.1
427     * and later.
428     * <p>
429     * For this reason, you should always ensure that UI controls in a notification are also
430     * available in an {@link android.app.Activity} in your app, and you should always start that
431     * {@link android.app.Activity} when users click the notification. To do this, use the
432     * {@link NotificationCompat.Builder#setContentIntent setContentIntent()}
433     * method.
434     * </p>
435     *
436     */
437    public static class Builder {
438        Context mContext;
439
440        CharSequence mContentTitle;
441        CharSequence mContentText;
442        PendingIntent mContentIntent;
443        PendingIntent mFullScreenIntent;
444        RemoteViews mTickerView;
445        Bitmap mLargeIcon;
446        CharSequence mContentInfo;
447        int mNumber;
448        int mPriority;
449        boolean mUseChronometer;
450        Style mStyle;
451        CharSequence mSubText;
452        int mProgressMax;
453        int mProgress;
454        boolean mProgressIndeterminate;
455        ArrayList<Action> mActions = new ArrayList<Action>();
456        Bundle mExtras;
457
458        Notification mNotification = new Notification();
459
460        /**
461         * Constructor.
462         *
463         * Automatically sets the when field to {@link System#currentTimeMillis()
464         * System.currentTimeMillis()} and the audio stream to the
465         * {@link Notification#STREAM_DEFAULT}.
466         *
467         * @param context A {@link Context} that will be used to construct the
468         *      RemoteViews. The Context will not be held past the lifetime of this
469         *      Builder object.
470         */
471        public Builder(Context context) {
472            mContext = context;
473
474            // Set defaults to match the defaults of a Notification
475            mNotification.when = System.currentTimeMillis();
476            mNotification.audioStreamType = Notification.STREAM_DEFAULT;
477            mPriority = PRIORITY_DEFAULT;
478        }
479
480        /**
481         * Set the time that the event occurred.  Notifications in the panel are
482         * sorted by this time.
483         */
484        public Builder setWhen(long when) {
485            mNotification.when = when;
486            return this;
487        }
488
489        /**
490         * Show the {@link Notification#when} field as a stopwatch.
491         *
492         * Instead of presenting <code>when</code> as a timestamp, the notification will show an
493         * automatically updating display of the minutes and seconds since <code>when</code>.
494         *
495         * Useful when showing an elapsed time (like an ongoing phone call).
496         *
497         * @see android.widget.Chronometer
498         * @see Notification#when
499         */
500        public Builder setUsesChronometer(boolean b) {
501            mUseChronometer = b;
502            return this;
503        }
504
505        /**
506         * Set the small icon to use in the notification layouts.  Different classes of devices
507         * may return different sizes.  See the UX guidelines for more information on how to
508         * design these icons.
509         *
510         * @param icon A resource ID in the application's package of the drawble to use.
511         */
512        public Builder setSmallIcon(int icon) {
513            mNotification.icon = icon;
514            return this;
515        }
516
517        /**
518         * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
519         * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
520         * LevelListDrawable}.
521         *
522         * @param icon A resource ID in the application's package of the drawble to use.
523         * @param level The level to use for the icon.
524         *
525         * @see android.graphics.drawable.LevelListDrawable
526         */
527        public Builder setSmallIcon(int icon, int level) {
528            mNotification.icon = icon;
529            mNotification.iconLevel = level;
530            return this;
531        }
532
533        /**
534         * Set the title (first row) of the notification, in a standard notification.
535         */
536        public Builder setContentTitle(CharSequence title) {
537            mContentTitle = title;
538            return this;
539        }
540
541        /**
542         * Set the text (second row) of the notification, in a standard notification.
543         */
544        public Builder setContentText(CharSequence text) {
545            mContentText = text;
546            return this;
547        }
548
549        /**
550         * Set the third line of text in the platform notification template.
551         * Don't use if you're also using {@link #setProgress(int, int, boolean)};
552         * they occupy the same location in the standard template.
553         * <br>
554         * If the platform does not provide large-format notifications, this method has no effect.
555         * The third line of text only appears in expanded view.
556         * <br>
557         */
558        public Builder setSubText(CharSequence text) {
559            mSubText = text;
560            return this;
561        }
562
563        /**
564         * Set the large number at the right-hand side of the notification.  This is
565         * equivalent to setContentInfo, although it might show the number in a different
566         * font size for readability.
567         */
568        public Builder setNumber(int number) {
569            mNumber = number;
570            return this;
571        }
572
573        /**
574         * Set the large text at the right-hand side of the notification.
575         */
576        public Builder setContentInfo(CharSequence info) {
577            mContentInfo = info;
578            return this;
579        }
580
581        /**
582         * Set the progress this notification represents, which may be
583         * represented as a {@link android.widget.ProgressBar}.
584         */
585        public Builder setProgress(int max, int progress, boolean indeterminate) {
586            mProgressMax = max;
587            mProgress = progress;
588            mProgressIndeterminate = indeterminate;
589            return this;
590        }
591
592        /**
593         * Supply a custom RemoteViews to use instead of the standard one.
594         */
595        public Builder setContent(RemoteViews views) {
596            mNotification.contentView = views;
597            return this;
598        }
599
600        /**
601         * Supply a {@link PendingIntent} to send when the notification is clicked.
602         * If you do not supply an intent, you can now add PendingIntents to individual
603         * views to be launched when clicked by calling {@link RemoteViews#setOnClickPendingIntent
604         * RemoteViews.setOnClickPendingIntent(int,PendingIntent)}.  Be sure to
605         * read {@link Notification#contentIntent Notification.contentIntent} for
606         * how to correctly use this.
607         */
608        public Builder setContentIntent(PendingIntent intent) {
609            mContentIntent = intent;
610            return this;
611        }
612
613        /**
614         * Supply a {@link PendingIntent} to send when the notification is cleared by the user
615         * directly from the notification panel.  For example, this intent is sent when the user
616         * clicks the "Clear all" button, or the individual "X" buttons on notifications.  This
617         * intent is not sent when the application calls {@link NotificationManager#cancel
618         * NotificationManager.cancel(int)}.
619         */
620        public Builder setDeleteIntent(PendingIntent intent) {
621            mNotification.deleteIntent = intent;
622            return this;
623        }
624
625        /**
626         * An intent to launch instead of posting the notification to the status bar.
627         * Only for use with extremely high-priority notifications demanding the user's
628         * <strong>immediate</strong> attention, such as an incoming phone call or
629         * alarm clock that the user has explicitly set to a particular time.
630         * If this facility is used for something else, please give the user an option
631         * to turn it off and use a normal notification, as this can be extremely
632         * disruptive.
633         *
634         * @param intent The pending intent to launch.
635         * @param highPriority Passing true will cause this notification to be sent
636         *          even if other notifications are suppressed.
637         */
638        public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
639            mFullScreenIntent = intent;
640            setFlag(FLAG_HIGH_PRIORITY, highPriority);
641            return this;
642        }
643
644        /**
645         * Set the text that is displayed in the status bar when the notification first
646         * arrives.
647         */
648        public Builder setTicker(CharSequence tickerText) {
649            mNotification.tickerText = tickerText;
650            return this;
651        }
652
653        /**
654         * Set the text that is displayed in the status bar when the notification first
655         * arrives, and also a RemoteViews object that may be displayed instead on some
656         * devices.
657         */
658        public Builder setTicker(CharSequence tickerText, RemoteViews views) {
659            mNotification.tickerText = tickerText;
660            mTickerView = views;
661            return this;
662        }
663
664        /**
665         * Set the large icon that is shown in the ticker and notification.
666         */
667        public Builder setLargeIcon(Bitmap icon) {
668            mLargeIcon = icon;
669            return this;
670        }
671
672        /**
673         * Set the sound to play.  It will play on the default stream.
674         */
675        public Builder setSound(Uri sound) {
676            mNotification.sound = sound;
677            mNotification.audioStreamType = Notification.STREAM_DEFAULT;
678            return this;
679        }
680
681        /**
682         * Set the sound to play.  It will play on the stream you supply.
683         *
684         * @see Notification#STREAM_DEFAULT
685         * @see AudioManager for the <code>STREAM_</code> constants.
686         */
687        public Builder setSound(Uri sound, int streamType) {
688            mNotification.sound = sound;
689            mNotification.audioStreamType = streamType;
690            return this;
691        }
692
693        /**
694         * Set the vibration pattern to use.
695         *
696         * @see android.os.Vibrator for a discussion of the <code>pattern</code>
697         * parameter.
698         */
699        public Builder setVibrate(long[] pattern) {
700            mNotification.vibrate = pattern;
701            return this;
702        }
703
704        /**
705         * Set the argb value that you would like the LED on the device to blnk, as well as the
706         * rate.  The rate is specified in terms of the number of milliseconds to be on
707         * and then the number of milliseconds to be off.
708         */
709        public Builder setLights(int argb, int onMs, int offMs) {
710            mNotification.ledARGB = argb;
711            mNotification.ledOnMS = onMs;
712            mNotification.ledOffMS = offMs;
713            boolean showLights = mNotification.ledOnMS != 0 && mNotification.ledOffMS != 0;
714            mNotification.flags = (mNotification.flags & ~Notification.FLAG_SHOW_LIGHTS) |
715                    (showLights ? Notification.FLAG_SHOW_LIGHTS : 0);
716            return this;
717        }
718
719        /**
720         * Set whether this is an ongoing notification.
721         *
722         * <p>Ongoing notifications differ from regular notifications in the following ways:
723         * <ul>
724         *   <li>Ongoing notifications are sorted above the regular notifications in the
725         *   notification panel.</li>
726         *   <li>Ongoing notifications do not have an 'X' close button, and are not affected
727         *   by the "Clear all" button.
728         * </ul>
729         */
730        public Builder setOngoing(boolean ongoing) {
731            setFlag(Notification.FLAG_ONGOING_EVENT, ongoing);
732            return this;
733        }
734
735        /**
736         * Set this flag if you would only like the sound, vibrate
737         * and ticker to be played if the notification is not already showing.
738         */
739        public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
740            setFlag(Notification.FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
741            return this;
742        }
743
744        /**
745         * Setting this flag will make it so the notification is automatically
746         * canceled when the user clicks it in the panel.  The PendingIntent
747         * set with {@link #setDeleteIntent} will be broadcast when the notification
748         * is canceled.
749         */
750        public Builder setAutoCancel(boolean autoCancel) {
751            setFlag(Notification.FLAG_AUTO_CANCEL, autoCancel);
752            return this;
753        }
754
755        /**
756         * Set the default notification options that will be used.
757         * <p>
758         * The value should be one or more of the following fields combined with
759         * bitwise-or:
760         * {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE},
761         * {@link Notification#DEFAULT_LIGHTS}.
762         * <p>
763         * For all default values, use {@link Notification#DEFAULT_ALL}.
764         */
765        public Builder setDefaults(int defaults) {
766            mNotification.defaults = defaults;
767            if ((defaults & Notification.DEFAULT_LIGHTS) != 0) {
768                mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
769            }
770            return this;
771        }
772
773        private void setFlag(int mask, boolean value) {
774            if (value) {
775                mNotification.flags |= mask;
776            } else {
777                mNotification.flags &= ~mask;
778            }
779        }
780
781        /**
782         * Set the relative priority for this notification.
783         *
784         * Priority is an indication of how much of the user's
785         * valuable attention should be consumed by this
786         * notification. Low-priority notifications may be hidden from
787         * the user in certain situations, while the user might be
788         * interrupted for a higher-priority notification.
789         * The system sets a notification's priority based on various factors including the
790         * setPriority value. The effect may differ slightly on different platforms.
791         */
792        public Builder setPriority(int pri) {
793            mPriority = pri;
794            return this;
795        }
796
797        /**
798         * Merge additional metadata into this notification.
799         *
800         * <p>Values within the Bundle will replace existing extras values in this Builder.
801         *
802         * @see Notification#extras
803         */
804        public Builder addExtras(Bundle bag) {
805            if (mExtras == null) {
806                mExtras = new Bundle(bag);
807            } else {
808                mExtras.putAll(bag);
809            }
810            return this;
811        }
812
813        /**
814         * Set metadata for this notification.
815         *
816         * <p>A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
817         * current contents are copied into the Notification each time {@link #build()} is
818         * called.
819         *
820         * <p>Replaces any existing extras values with those from the provided Bundle.
821         * Use {@link #addExtras} to merge in metadata instead.
822         *
823         * @see Notification#extras
824         */
825        public Builder setExtras(Bundle bag) {
826            mExtras = bag;
827            return this;
828        }
829
830        /**
831         * Get the current metadata Bundle used by this notification Builder.
832         *
833         * <p>The returned Bundle is shared with this Builder.
834         *
835         * <p>The current contents of this Bundle are copied into the Notification each time
836         * {@link #build()} is called.
837         *
838         * @see Notification#extras
839         */
840        public Bundle getExtras() {
841            if (mExtras == null) {
842                mExtras = new Bundle();
843            }
844            return mExtras;
845        }
846
847        /**
848         * Add an action to this notification. Actions are typically displayed by
849         * the system as a button adjacent to the notification content.
850         * <br>
851         * Action buttons won't appear on platforms prior to Android 4.1. Action
852         * buttons depend on expanded notifications, which are only available in Android 4.1
853         * and later. To ensure that an action button's functionality is always available, first
854         * implement the functionality in the {@link android.app.Activity} that starts when a user
855         * clicks the  notification (see {@link #setContentIntent setContentIntent()}), and then
856         * enhance the notification by implementing the same functionality with
857         * {@link #addAction addAction()}.
858         *
859         * @param icon Resource ID of a drawable that represents the action.
860         * @param title Text describing the action.
861         * @param intent {@link android.app.PendingIntent} to be fired when the action is invoked.
862         */
863        public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
864            mActions.add(new Action(icon, title, intent));
865            return this;
866        }
867
868        /**
869         * Add a rich notification style to be applied at build time.
870         * <br>
871         * If the platform does not provide rich notification styles, this method has no effect. The
872         * user will always see the normal notification style.
873         *
874         * @param style Object responsible for modifying the notification style.
875         */
876        public Builder setStyle(Style style) {
877            if (mStyle != style) {
878                mStyle = style;
879                if (mStyle != null) {
880                    mStyle.setBuilder(this);
881                }
882            }
883            return this;
884        }
885
886        /**
887         * @deprecated Use {@link #build()} instead.
888         */
889        @Deprecated
890        public Notification getNotification() {
891            return IMPL.build(this);
892        }
893
894        /**
895         * Combine all of the options that have been set and return a new {@link Notification}
896         * object.
897         */
898        public Notification build() {
899            return IMPL.build(this);
900        }
901    }
902
903    /**
904     * An object that can apply a rich notification style to a {@link Notification.Builder}
905     * object.
906     * <br>
907     * If the platform does not provide rich notification styles, methods in this class have no
908     * effect.
909     */
910    public static abstract class Style {
911        Builder mBuilder;
912        CharSequence mBigContentTitle;
913        CharSequence mSummaryText;
914        boolean mSummaryTextSet = false;
915
916        public void setBuilder(Builder builder) {
917            if (mBuilder != builder) {
918                mBuilder = builder;
919                if (mBuilder != null) {
920                    mBuilder.setStyle(this);
921                }
922            }
923        }
924
925        public Notification build() {
926            Notification notification = null;
927            if (mBuilder != null) {
928                notification = mBuilder.build();
929            }
930            return notification;
931        }
932    }
933
934    /**
935     * Helper class for generating large-format notifications that include a large image attachment.
936     * <br>
937     * If the platform does not provide large-format notifications, this method has no effect. The
938     * user will always see the normal notification view.
939     * <br>
940     * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so:
941     * <pre class="prettyprint">
942     * Notification noti = new Notification.Builder()
943     *     .setContentTitle(&quot;New photo from &quot; + sender.toString())
944     *     .setContentText(subject)
945     *     .setSmallIcon(R.drawable.new_post)
946     *     .setLargeIcon(aBitmap)
947     *     .setStyle(new Notification.BigPictureStyle()
948     *         .bigPicture(aBigBitmap))
949     *     .build();
950     * </pre>
951     *
952     * @see Notification#bigContentView
953     */
954    public static class BigPictureStyle extends Style {
955        Bitmap mPicture;
956        Bitmap mBigLargeIcon;
957        boolean mBigLargeIconSet;
958
959        public BigPictureStyle() {
960        }
961
962        public BigPictureStyle(Builder builder) {
963            setBuilder(builder);
964        }
965
966        /**
967         * Overrides ContentTitle in the big form of the template.
968         * This defaults to the value passed to setContentTitle().
969         */
970        public BigPictureStyle setBigContentTitle(CharSequence title) {
971            mBigContentTitle = title;
972            return this;
973        }
974
975        /**
976         * Set the first line of text after the detail section in the big form of the template.
977         */
978        public BigPictureStyle setSummaryText(CharSequence cs) {
979            mSummaryText = cs;
980            mSummaryTextSet = true;
981            return this;
982        }
983
984        /**
985         * Provide the bitmap to be used as the payload for the BigPicture notification.
986         */
987        public BigPictureStyle bigPicture(Bitmap b) {
988            mPicture = b;
989            return this;
990        }
991
992        /**
993         * Override the large icon when the big notification is shown.
994         */
995        public BigPictureStyle bigLargeIcon(Bitmap b) {
996            mBigLargeIcon = b;
997            mBigLargeIconSet = true;
998            return this;
999        }
1000    }
1001
1002    /**
1003     * Helper class for generating large-format notifications that include a lot of text.
1004     *
1005     * <br>
1006     * If the platform does not provide large-format notifications, this method has no effect. The
1007     * user will always see the normal notification view.
1008     * <br>
1009     * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so:
1010     * <pre class="prettyprint">
1011     * Notification noti = new Notification.Builder()
1012     *     .setContentTitle(&quot;New mail from &quot; + sender.toString())
1013     *     .setContentText(subject)
1014     *     .setSmallIcon(R.drawable.new_mail)
1015     *     .setLargeIcon(aBitmap)
1016     *     .setStyle(new Notification.BigTextStyle()
1017     *         .bigText(aVeryLongString))
1018     *     .build();
1019     * </pre>
1020     *
1021     * @see Notification#bigContentView
1022     */
1023    public static class BigTextStyle extends Style {
1024        CharSequence mBigText;
1025
1026        public BigTextStyle() {
1027        }
1028
1029        public BigTextStyle(Builder builder) {
1030            setBuilder(builder);
1031        }
1032
1033        /**
1034         * Overrides ContentTitle in the big form of the template.
1035         * This defaults to the value passed to setContentTitle().
1036         */
1037        public BigTextStyle setBigContentTitle(CharSequence title) {
1038            mBigContentTitle = title;
1039            return this;
1040        }
1041
1042        /**
1043         * Set the first line of text after the detail section in the big form of the template.
1044         */
1045        public BigTextStyle setSummaryText(CharSequence cs) {
1046            mSummaryText = cs;
1047            mSummaryTextSet = true;
1048            return this;
1049        }
1050
1051        /**
1052         * Provide the longer text to be displayed in the big form of the
1053         * template in place of the content text.
1054         */
1055        public BigTextStyle bigText(CharSequence cs) {
1056            mBigText = cs;
1057            return this;
1058        }
1059    }
1060
1061    /**
1062     * Helper class for generating large-format notifications that include a list of (up to 5) strings.
1063     *
1064     * <br>
1065     * If the platform does not provide large-format notifications, this method has no effect. The
1066     * user will always see the normal notification view.
1067     * <br>
1068     * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so:
1069     * <pre class="prettyprint">
1070     * Notification noti = new Notification.Builder()
1071     *     .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
1072     *     .setContentText(subject)
1073     *     .setSmallIcon(R.drawable.new_mail)
1074     *     .setLargeIcon(aBitmap)
1075     *     .setStyle(new Notification.InboxStyle()
1076     *         .addLine(str1)
1077     *         .addLine(str2)
1078     *         .setContentTitle(&quot;&quot;)
1079     *         .setSummaryText(&quot;+3 more&quot;))
1080     *     .build();
1081     * </pre>
1082     *
1083     * @see Notification#bigContentView
1084     */
1085    public static class InboxStyle extends Style {
1086        ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>();
1087
1088        public InboxStyle() {
1089        }
1090
1091        public InboxStyle(Builder builder) {
1092            setBuilder(builder);
1093        }
1094
1095        /**
1096         * Overrides ContentTitle in the big form of the template.
1097         * This defaults to the value passed to setContentTitle().
1098         */
1099        public InboxStyle setBigContentTitle(CharSequence title) {
1100            mBigContentTitle = title;
1101            return this;
1102        }
1103
1104        /**
1105         * Set the first line of text after the detail section in the big form of the template.
1106         */
1107        public InboxStyle setSummaryText(CharSequence cs) {
1108            mSummaryText = cs;
1109            mSummaryTextSet = true;
1110            return this;
1111        }
1112
1113        /**
1114         * Append a line to the digest section of the Inbox notification.
1115         */
1116        public InboxStyle addLine(CharSequence cs) {
1117            mTexts.add(cs);
1118            return this;
1119        }
1120    }
1121
1122    public static class Action {
1123        public int icon;
1124        public CharSequence title;
1125        public PendingIntent actionIntent;
1126
1127        public Action(int icon_, CharSequence title_, PendingIntent intent_) {
1128            this.icon = icon_;
1129            this.title = title_;
1130            this.actionIntent = intent_;
1131        }
1132    }
1133
1134    /**
1135     * Gets the {@link Notification#extras} field from a notification in a backwards
1136     * compatible manner. Extras field was supported from JellyBean (Api level 16)
1137     * forwards. This function will return null on older api levels.
1138     */
1139    public static Bundle getExtras(Notification notif) {
1140        return IMPL.getExtras(notif);
1141    }
1142}
1143