blob: 5325af044083b497690431946560a956160ce038 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Andy Stadler110988c2010-12-03 14:29:16 -080019import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.Intent;
Joe Onoratoef1e7762010-09-17 18:38:38 -040023import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.net.Uri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050025import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.os.Parcel;
27import android.os.Parcelable;
28import android.text.TextUtils;
Joe Onorato8595a3d2010-11-19 18:12:07 -080029import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070030import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.widget.RemoteViews;
32
Andy Stadler110988c2010-12-03 14:29:16 -080033import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050034import java.util.ArrayList;
Joe Onorato561d3852010-11-20 18:09:34 -080035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036/**
37 * A class that represents how a persistent notification is to be presented to
38 * the user using the {@link android.app.NotificationManager}.
39 *
Joe Onoratocb109a02011-01-18 17:57:41 -080040 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
41 * easier to construct Notifications.</p>
42 *
Joe Fernandez558459f2011-10-13 16:47:36 -070043 * <div class="special reference">
44 * <h3>Developer Guides</h3>
45 * <p>For a guide to creating notifications, read the
46 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
47 * developer guide.</p>
48 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 */
50public class Notification implements Parcelable
51{
52 /**
53 * Use all default values (where applicable).
54 */
55 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 /**
58 * Use the default notification sound. This will ignore any given
59 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050060 *
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050063 */
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 public static final int DEFAULT_SOUND = 1;
66
67 /**
68 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050069 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070070 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050073 */
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 /**
78 * Use the default notification lights. This will ignore the
79 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
80 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050081 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050083 */
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088 * A timestamp related to this notification, in milliseconds since the epoch.
89 *
90 * Default value: {@link System#currentTimeMillis() Now}.
91 *
92 * Choose a timestamp that will be most relevant to the user. For most finite events, this
93 * corresponds to the time the event happened (or will happen, in the case of events that have
94 * yet to occur but about which the user is being informed). Indefinite events should be
95 * timestamped according to when the activity began.
96 *
97 * Some examples:
98 *
99 * <ul>
100 * <li>Notification of a new chat message should be stamped when the message was received.</li>
101 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
102 * <li>Notification of a completed file download should be stamped when the download finished.</li>
103 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
104 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
105 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
106 * </ul>
107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 */
109 public long when;
110
111 /**
112 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500113 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
115 public int icon;
116
117 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800118 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
119 * leave it at its default value of 0.
120 *
121 * @see android.widget.ImageView#setImageLevel
122 * @see android.graphics.drawable#setLevel
123 */
124 public int iconLevel;
125
126 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500127 * The number of events that this notification represents. For example, in a new mail
128 * notification, this could be the number of unread messages.
129 *
130 * The system may or may not use this field to modify the appearance of the notification. For
131 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
132 * superimposed over the icon in the status bar. Starting with
133 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
134 * {@link Notification.Builder} has displayed the number in the expanded notification view.
135 *
136 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 */
138 public int number;
139
140 /**
141 * The intent to execute when the expanded status entry is clicked. If
142 * this is an activity, it must include the
143 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800144 * that you take care of task management as described in the
145 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800146 * Stack</a> document. In particular, make sure to read the notification section
147 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
148 * Notifications</a> for the correct ways to launch an application from a
149 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
151 public PendingIntent contentIntent;
152
153 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154 * The intent to execute when the notification is explicitly dismissed by the user, either with
155 * the "Clear All" button or by swiping it away individually.
156 *
157 * This probably shouldn't be launching an activity since several of those will be sent
158 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 */
160 public PendingIntent deleteIntent;
161
162 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700163 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800164 *
165 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400166 */
167 public PendingIntent fullScreenIntent;
168
169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400171 * the status bar on large and smaller devices.
172 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800173 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 */
175 public CharSequence tickerText;
176
177 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800178 * The view to show as the ticker in the status bar when the notification
179 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400180 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800181 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400182
183 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400184 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 */
186 public RemoteViews contentView;
187
188 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800189 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800191 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
193 /**
194 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500197 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 * </p>
199 */
200 public Uri sound;
201
202 /**
203 * Use this constant as the value for audioStreamType to request that
204 * the default stream type for notifications be used. Currently the
205 * default stream type is STREAM_RING.
206 */
207 public static final int STREAM_DEFAULT = -1;
208
209 /**
210 * The audio stream type to use when playing the sound.
211 * Should be one of the STREAM_ constants from
212 * {@link android.media.AudioManager}.
213 */
214 public int audioStreamType = STREAM_DEFAULT;
215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500217 * The pattern with which to vibrate.
218 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <p>
220 * To vibrate the default pattern, see {@link #defaults}.
221 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500222 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * @see android.os.Vibrator#vibrate(long[],int)
224 */
225 public long[] vibrate;
226
227 /**
228 * The color of the led. The hardware will do its best approximation.
229 *
230 * @see #FLAG_SHOW_LIGHTS
231 * @see #flags
232 */
233 public int ledARGB;
234
235 /**
236 * The number of milliseconds for the LED to be on while it's flashing.
237 * The hardware will do its best approximation.
238 *
239 * @see #FLAG_SHOW_LIGHTS
240 * @see #flags
241 */
242 public int ledOnMS;
243
244 /**
245 * The number of milliseconds for the LED to be off while it's flashing.
246 * The hardware will do its best approximation.
247 *
248 * @see #FLAG_SHOW_LIGHTS
249 * @see #flags
250 */
251 public int ledOffMS;
252
253 /**
254 * Specifies which values should be taken from the defaults.
255 * <p>
256 * To set, OR the desired from {@link #DEFAULT_SOUND},
257 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
258 * values, use {@link #DEFAULT_ALL}.
259 * </p>
260 */
261 public int defaults;
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * Bit to be bitwise-ored into the {@link #flags} field that should be
265 * set if you want the LED on for this notification.
266 * <ul>
267 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
268 * or 0 for both ledOnMS and ledOffMS.</li>
269 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
270 * <li>To flash the LED, pass the number of milliseconds that it should
271 * be on and off to ledOnMS and ledOffMS.</li>
272 * </ul>
273 * <p>
274 * Since hardware varies, you are not guaranteed that any of the values
275 * you pass are honored exactly. Use the system defaults (TODO) if possible
276 * because they will be set to values that work on any given hardware.
277 * <p>
278 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 */
281 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
282
283 /**
284 * Bit to be bitwise-ored into the {@link #flags} field that should be
285 * set if this notification is in reference to something that is ongoing,
286 * like a phone call. It should not be set if this notification is in
287 * reference to something that happened at a particular point in time,
288 * like a missed phone call.
289 */
290 public static final int FLAG_ONGOING_EVENT = 0x00000002;
291
292 /**
293 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700294 * the audio will be repeated until the notification is
295 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 */
297 public static final int FLAG_INSISTENT = 0x00000004;
298
299 /**
300 * Bit to be bitwise-ored into the {@link #flags} field that should be
301 * set if you want the sound and/or vibration play each time the
302 * notification is sent, even if it has not been canceled before that.
303 */
304 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
305
306 /**
307 * Bit to be bitwise-ored into the {@link #flags} field that should be
308 * set if the notification should be canceled when it is clicked by the
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500309 * user. On tablets, the
310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 */
312 public static final int FLAG_AUTO_CANCEL = 0x00000010;
313
314 /**
315 * Bit to be bitwise-ored into the {@link #flags} field that should be
316 * set if the notification should not be canceled when the user clicks
317 * the Clear all button.
318 */
319 public static final int FLAG_NO_CLEAR = 0x00000020;
320
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700321 /**
322 * Bit to be bitwise-ored into the {@link #flags} field that should be
323 * set if this notification represents a currently running service. This
324 * will normally be set for you by {@link Service#startForeground}.
325 */
326 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
327
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400328 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500329 * Obsolete flag indicating high-priority notifications; use the priority field instead.
330 *
331 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400332 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500333 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 public int flags;
336
337 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500338 * Default notification {@link #priority}. If your application does not prioritize its own
339 * notifications, use this value for all notifications.
340 */
341 public static final int PRIORITY_DEFAULT = 0;
342
343 /**
344 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
345 * items smaller, or at a different position in the list, compared with your app's
346 * {@link #PRIORITY_DEFAULT} items.
347 */
348 public static final int PRIORITY_LOW = -1;
349
350 /**
351 * Lowest {@link #priority}; these items might not be shown to the user except under special
352 * circumstances, such as detailed notification logs.
353 */
354 public static final int PRIORITY_MIN = -2;
355
356 /**
357 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
358 * show these items larger, or at a different position in notification lists, compared with
359 * your app's {@link #PRIORITY_DEFAULT} items.
360 */
361 public static final int PRIORITY_HIGH = 1;
362
363 /**
364 * Highest {@link #priority}, for your application's most important items that require the
365 * user's prompt attention or input.
366 */
367 public static final int PRIORITY_MAX = 2;
368
369 /**
370 * Relative priority for this notification.
371 *
372 * Priority is an indication of how much of the user's valuable attention should be consumed by
373 * this notification. Low-priority notifications may be hidden from the user in certain
374 * situations, while the user might be interrupted for a higher-priority notification. The
375 * system will make a determination about how to interpret notification priority as described in
376 * MUMBLE MUMBLE.
377 */
378 public int priority;
379
380 /**
381 * Notification type: incoming call (voice or video) or similar synchronous communication request.
382 */
383 public static final String KIND_CALL = "android.call";
384
385 /**
386 * Notification type: incoming direct message (SMS, instant message, etc.).
387 */
388 public static final String KIND_MESSAGE = "android.message";
389
390 /**
391 * Notification type: asynchronous bulk message (email).
392 */
393 public static final String KIND_EMAIL = "android.email";
394
395 /**
396 * Notification type: calendar event.
397 */
398 public static final String KIND_EVENT = "android.event";
399
400 /**
401 * Notification type: promotion or advertisement.
402 */
403 public static final String KIND_PROMO = "android.promo";
404
405 /**
406 * If this notification matches of one or more special types (see the <code>KIND_*</code>
407 * constants), add them here, best match first.
408 */
409 public String[] kind;
410
411 /**
412 * Extra key for people values (type TBD).
413 *
414 * @hide
415 */
416 public static final String EXTRA_PEOPLE = "android.people";
417
418 private Bundle extras;
419
420 /**
421 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800422 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 */
424 public Notification()
425 {
426 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500427 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * @hide
432 */
433 public Notification(Context context, int icon, CharSequence tickerText, long when,
434 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
435 {
436 this.when = when;
437 this.icon = icon;
438 this.tickerText = tickerText;
439 setLatestEventInfo(context, contentTitle, contentText,
440 PendingIntent.getActivity(context, 0, contentIntent, 0));
441 }
442
443 /**
444 * Constructs a Notification object with the information needed to
445 * have a status bar icon without the standard expanded view.
446 *
447 * @param icon The resource id of the icon to put in the status bar.
448 * @param tickerText The text that flows by in the status bar when the notification first
449 * activates.
450 * @param when The time to show in the time field. In the System.currentTimeMillis
451 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800452 *
453 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800455 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 public Notification(int icon, CharSequence tickerText, long when)
457 {
458 this.icon = icon;
459 this.tickerText = tickerText;
460 this.when = when;
461 }
462
463 /**
464 * Unflatten the notification from a parcel.
465 */
466 public Notification(Parcel parcel)
467 {
468 int version = parcel.readInt();
469
470 when = parcel.readLong();
471 icon = parcel.readInt();
472 number = parcel.readInt();
473 if (parcel.readInt() != 0) {
474 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
475 }
476 if (parcel.readInt() != 0) {
477 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
478 }
479 if (parcel.readInt() != 0) {
480 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
481 }
482 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800483 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400484 }
485 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
487 }
Joe Onorato561d3852010-11-20 18:09:34 -0800488 if (parcel.readInt() != 0) {
489 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 defaults = parcel.readInt();
492 flags = parcel.readInt();
493 if (parcel.readInt() != 0) {
494 sound = Uri.CREATOR.createFromParcel(parcel);
495 }
496
497 audioStreamType = parcel.readInt();
498 vibrate = parcel.createLongArray();
499 ledARGB = parcel.readInt();
500 ledOnMS = parcel.readInt();
501 ledOffMS = parcel.readInt();
502 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400503
504 if (parcel.readInt() != 0) {
505 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
506 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500507
508 priority = parcel.readInt();
509
510 kind = parcel.createStringArray(); // may set kind to null
511
512 if (parcel.readInt() != 0) {
513 extras = parcel.readBundle();
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516
Andy Stadler110988c2010-12-03 14:29:16 -0800517 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700518 public Notification clone() {
519 Notification that = new Notification();
520
521 that.when = this.when;
522 that.icon = this.icon;
523 that.number = this.number;
524
525 // PendingIntents are global, so there's no reason (or way) to clone them.
526 that.contentIntent = this.contentIntent;
527 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400528 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700529
530 if (this.tickerText != null) {
531 that.tickerText = this.tickerText.toString();
532 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800533 if (this.tickerView != null) {
534 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400535 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700536 if (this.contentView != null) {
537 that.contentView = this.contentView.clone();
538 }
Joe Onorato561d3852010-11-20 18:09:34 -0800539 if (this.largeIcon != null) {
540 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
541 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100542 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700543 that.sound = this.sound; // android.net.Uri is immutable
544 that.audioStreamType = this.audioStreamType;
545
546 final long[] vibrate = this.vibrate;
547 if (vibrate != null) {
548 final int N = vibrate.length;
549 final long[] vib = that.vibrate = new long[N];
550 System.arraycopy(vibrate, 0, vib, 0, N);
551 }
552
553 that.ledARGB = this.ledARGB;
554 that.ledOnMS = this.ledOnMS;
555 that.ledOffMS = this.ledOffMS;
556 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500557
Joe Onorato18e69df2010-05-17 22:26:12 -0700558 that.flags = this.flags;
559
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500560 that.priority = this.priority;
561
562 final String[] thiskind = this.kind;
563 if (thiskind != null) {
564 final int N = thiskind.length;
565 final String[] thatkind = that.kind = new String[N];
566 System.arraycopy(thiskind, 0, thatkind, 0, N);
567 }
568
569 if (this.extras != null) {
570 that.extras = new Bundle(this.extras);
571
572 }
573
Joe Onorato18e69df2010-05-17 22:26:12 -0700574 return that;
575 }
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 public int describeContents() {
578 return 0;
579 }
580
581 /**
582 * Flatten this notification from a parcel.
583 */
584 public void writeToParcel(Parcel parcel, int flags)
585 {
586 parcel.writeInt(1);
587
588 parcel.writeLong(when);
589 parcel.writeInt(icon);
590 parcel.writeInt(number);
591 if (contentIntent != null) {
592 parcel.writeInt(1);
593 contentIntent.writeToParcel(parcel, 0);
594 } else {
595 parcel.writeInt(0);
596 }
597 if (deleteIntent != null) {
598 parcel.writeInt(1);
599 deleteIntent.writeToParcel(parcel, 0);
600 } else {
601 parcel.writeInt(0);
602 }
603 if (tickerText != null) {
604 parcel.writeInt(1);
605 TextUtils.writeToParcel(tickerText, parcel, flags);
606 } else {
607 parcel.writeInt(0);
608 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800609 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400610 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800611 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400612 } else {
613 parcel.writeInt(0);
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 if (contentView != null) {
616 parcel.writeInt(1);
617 contentView.writeToParcel(parcel, 0);
618 } else {
619 parcel.writeInt(0);
620 }
Joe Onorato561d3852010-11-20 18:09:34 -0800621 if (largeIcon != null) {
622 parcel.writeInt(1);
623 largeIcon.writeToParcel(parcel, 0);
624 } else {
625 parcel.writeInt(0);
626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 parcel.writeInt(defaults);
629 parcel.writeInt(this.flags);
630
631 if (sound != null) {
632 parcel.writeInt(1);
633 sound.writeToParcel(parcel, 0);
634 } else {
635 parcel.writeInt(0);
636 }
637 parcel.writeInt(audioStreamType);
638 parcel.writeLongArray(vibrate);
639 parcel.writeInt(ledARGB);
640 parcel.writeInt(ledOnMS);
641 parcel.writeInt(ledOffMS);
642 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400643
644 if (fullScreenIntent != null) {
645 parcel.writeInt(1);
646 fullScreenIntent.writeToParcel(parcel, 0);
647 } else {
648 parcel.writeInt(0);
649 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650
651 parcel.writeInt(priority);
652
653 parcel.writeStringArray(kind); // ok for null
654
655 if (extras != null) {
656 parcel.writeInt(1);
657 extras.writeToParcel(parcel, 0);
658 } else {
659 parcel.writeInt(0);
660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
662
663 /**
664 * Parcelable.Creator that instantiates Notification objects
665 */
666 public static final Parcelable.Creator<Notification> CREATOR
667 = new Parcelable.Creator<Notification>()
668 {
669 public Notification createFromParcel(Parcel parcel)
670 {
671 return new Notification(parcel);
672 }
673
674 public Notification[] newArray(int size)
675 {
676 return new Notification[size];
677 }
678 };
679
680 /**
681 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
682 * layout.
683 *
684 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
685 * in the view.</p>
686 * @param context The context for your application / activity.
687 * @param contentTitle The title that goes in the expanded entry.
688 * @param contentText The text that goes in the expanded entry.
689 * @param contentIntent The intent to launch when the user clicks the expanded notification.
690 * If this is an activity, it must include the
691 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800692 * that you take care of task management as described in the
693 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
694 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500695 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800696 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800698 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 public void setLatestEventInfo(Context context,
700 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
701 RemoteViews contentView = new RemoteViews(context.getPackageName(),
Joe Onorato561d3852010-11-20 18:09:34 -0800702 R.layout.status_bar_latest_event_content);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 if (this.icon != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800704 contentView.setImageViewResource(R.id.icon, this.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706 if (contentTitle != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800707 contentView.setTextViewText(R.id.title, contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709 if (contentText != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800710 contentView.setTextViewText(R.id.text, contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712 if (this.when != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800713 contentView.setLong(R.id.time, "setTime", when);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715
716 this.contentView = contentView;
717 this.contentIntent = contentIntent;
718 }
719
720 @Override
721 public String toString() {
722 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500723 sb.append("Notification(pri=");
724 sb.append(priority);
725 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -0800726 if (contentView != null) {
727 sb.append(contentView.getPackage());
728 sb.append("/0x");
729 sb.append(Integer.toHexString(contentView.getLayoutId()));
730 } else {
731 sb.append("null");
732 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -0800734 sb.append(" vibrate=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 if (this.vibrate != null) {
736 int N = this.vibrate.length-1;
737 sb.append("[");
738 for (int i=0; i<N; i++) {
739 sb.append(this.vibrate[i]);
740 sb.append(',');
741 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200742 if (N != -1) {
743 sb.append(this.vibrate[N]);
744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 sb.append("]");
746 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) {
747 sb.append("default");
748 } else {
749 sb.append("null");
750 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500751 sb.append(" sound=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 if (this.sound != null) {
753 sb.append(this.sound.toString());
754 } else if ((this.defaults & DEFAULT_SOUND) != 0) {
755 sb.append("default");
756 } else {
757 sb.append("null");
758 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500759 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500761 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400762 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500763 sb.append(" kind=[");
764 if (this.kind == null) {
765 sb.append("null");
766 } else {
767 for (int i=0; i<this.kind.length; i++) {
768 if (i>0) sb.append(",");
769 sb.append(this.kind[i]);
770 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400771 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500772 sb.append("])");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 return sb.toString();
774 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800775
Joe Onoratocb109a02011-01-18 17:57:41 -0800776 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500777 * Builder class for {@link Notification} objects.
778 *
779 * Provides a convenient way to set the various fields of a {@link Notification} and generate
780 * content views using the platform's notification layout template.
781 *
782 * Example:
783 *
784 * <pre class="prettyprint">
785 * Notification noti = new Notification.Builder()
786 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
787 * .setContentText(subject)
788 * .setSmallIcon(R.drawable.new_mail)
789 * .setLargeIcon(aBitmap)
790 * .getNotification();
791 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -0800792 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800793 public static class Builder {
794 private Context mContext;
795
796 private long mWhen;
797 private int mSmallIcon;
798 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800799 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800800 private CharSequence mContentTitle;
801 private CharSequence mContentText;
802 private CharSequence mContentInfo;
803 private PendingIntent mContentIntent;
804 private RemoteViews mContentView;
805 private PendingIntent mDeleteIntent;
806 private PendingIntent mFullScreenIntent;
807 private CharSequence mTickerText;
808 private RemoteViews mTickerView;
809 private Bitmap mLargeIcon;
810 private Uri mSound;
811 private int mAudioStreamType;
812 private long[] mVibrate;
813 private int mLedArgb;
814 private int mLedOnMs;
815 private int mLedOffMs;
816 private int mDefaults;
817 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -0700818 private int mProgressMax;
819 private int mProgress;
820 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500821 private ArrayList<String> mKindList = new ArrayList<String>(1);
822 private Bundle mExtras;
823 private int mPriority;
Joe Onorato46439ce2010-11-19 13:56:21 -0800824
Joe Onoratocb109a02011-01-18 17:57:41 -0800825 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500826 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -0800827 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500828
829 * <table>
830 * <tr><th align=right>priority</th>
831 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
832 * <tr><th align=right>when</th>
833 * <td>now ({@link System#currentTimeMillis()})</td></tr>
834 * <tr><th align=right>audio stream</th>
835 * <td>{@link #STREAM_DEFAULT}</td></tr>
836 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -0800837 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500838
839 * @param context
840 * A {@link Context} that will be used by the Builder to construct the
841 * RemoteViews. The Context will not be held past the lifetime of this Builder
842 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -0800843 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800844 public Builder(Context context) {
845 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -0800846
847 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -0800848 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -0800849 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500850 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -0800851 }
852
Joe Onoratocb109a02011-01-18 17:57:41 -0800853 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500854 * Add a timestamp pertaining to the notification (usually the time the event occurred).
855 *
856
857 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -0800858 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800859 public Builder setWhen(long when) {
860 mWhen = when;
861 return this;
862 }
863
Joe Onoratocb109a02011-01-18 17:57:41 -0800864 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500865 * Set the small icon resource, which will be used to represent the notification in the
866 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800867 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500868
869 * The platform template for the expanded view will draw this icon in the left, unless a
870 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
871 * icon will be moved to the right-hand side.
872 *
873
874 * @param icon
875 * A resource ID in the application's package of the drawable to use.
876 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -0800877 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800878 public Builder setSmallIcon(int icon) {
879 mSmallIcon = icon;
880 return this;
881 }
882
Joe Onoratocb109a02011-01-18 17:57:41 -0800883 /**
884 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
885 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
886 * LevelListDrawable}.
887 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500888 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -0800889 * @param level The level to use for the icon.
890 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500891 * @see Notification#icon
892 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -0800893 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800894 public Builder setSmallIcon(int icon, int level) {
895 mSmallIcon = icon;
896 mSmallIconLevel = level;
897 return this;
898 }
899
Joe Onoratocb109a02011-01-18 17:57:41 -0800900 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500901 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -0800902 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800903 public Builder setContentTitle(CharSequence title) {
904 mContentTitle = title;
905 return this;
906 }
907
Joe Onoratocb109a02011-01-18 17:57:41 -0800908 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500909 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -0800910 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800911 public Builder setContentText(CharSequence text) {
912 mContentText = text;
913 return this;
914 }
915
Joe Onoratocb109a02011-01-18 17:57:41 -0800916 /**
917 * Set the large number at the right-hand side of the notification. This is
918 * equivalent to setContentInfo, although it might show the number in a different
919 * font size for readability.
920 */
Joe Onorato8595a3d2010-11-19 18:12:07 -0800921 public Builder setNumber(int number) {
922 mNumber = number;
923 return this;
924 }
925
Joe Onoratocb109a02011-01-18 17:57:41 -0800926 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500927 * A small piece of additional information pertaining to this notification.
928 *
929
930 * The platform template will draw this on the last line of the notification, at the far
931 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -0800932 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800933 public Builder setContentInfo(CharSequence info) {
934 mContentInfo = info;
935 return this;
936 }
937
Joe Onoratocb109a02011-01-18 17:57:41 -0800938 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500939 * Set the progress this notification represents.
940 *
941
942 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -0700943 */
944 public Builder setProgress(int max, int progress, boolean indeterminate) {
945 mProgressMax = max;
946 mProgress = progress;
947 mProgressIndeterminate = indeterminate;
948 return this;
949 }
950
951 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500952 * Supply a custom RemoteViews to use instead of the platform template.
953 *
954
955 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -0800956 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800957 public Builder setContent(RemoteViews views) {
958 mContentView = views;
959 return this;
960 }
961
Joe Onoratocb109a02011-01-18 17:57:41 -0800962 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500963 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
964 *
965
966 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
967 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
968 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
969
970 * to assign PendingIntents to individual views in that custom layout (i.e., to create
971
972 * clickable buttons inside the
973 * notification view).
974 *
975
976 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -0800977 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800978 public Builder setContentIntent(PendingIntent intent) {
979 mContentIntent = intent;
980 return this;
981 }
982
Joe Onoratocb109a02011-01-18 17:57:41 -0800983 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500984 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
985 *
986
987 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -0800988 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800989 public Builder setDeleteIntent(PendingIntent intent) {
990 mDeleteIntent = intent;
991 return this;
992 }
993
Joe Onoratocb109a02011-01-18 17:57:41 -0800994 /**
995 * An intent to launch instead of posting the notification to the status bar.
996 * Only for use with extremely high-priority notifications demanding the user's
997 * <strong>immediate</strong> attention, such as an incoming phone call or
998 * alarm clock that the user has explicitly set to a particular time.
999 * If this facility is used for something else, please give the user an option
1000 * to turn it off and use a normal notification, as this can be extremely
1001 * disruptive.
1002 *
1003 * @param intent The pending intent to launch.
1004 * @param highPriority Passing true will cause this notification to be sent
1005 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001006 *
1007 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001008 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001009 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1010 mFullScreenIntent = intent;
1011 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1012 return this;
1013 }
1014
Joe Onoratocb109a02011-01-18 17:57:41 -08001015 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001016 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001017 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001018 *
1019
1020 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001021 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001022 public Builder setTicker(CharSequence tickerText) {
1023 mTickerText = tickerText;
1024 return this;
1025 }
1026
Joe Onoratocb109a02011-01-18 17:57:41 -08001027 /**
1028 * Set the text that is displayed in the status bar when the notification first
1029 * arrives, and also a RemoteViews object that may be displayed instead on some
1030 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001031 *
1032 * @see Notification#tickerText
1033 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001034 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001035 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
1036 mTickerText = tickerText;
1037 mTickerView = views;
1038 return this;
1039 }
1040
Joe Onoratocb109a02011-01-18 17:57:41 -08001041 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001042 * Add a large icon to the notification (and the ticker on some devices).
1043 *
1044 * In the platform template, this image will be shown on the left of the notification view
1045 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1046 *
1047 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001048 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001049 public Builder setLargeIcon(Bitmap icon) {
1050 mLargeIcon = icon;
1051 return this;
1052 }
1053
Joe Onoratocb109a02011-01-18 17:57:41 -08001054 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001055 * Set the sound to play.
1056 *
1057 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1058 *
1059 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001060 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001061 public Builder setSound(Uri sound) {
1062 mSound = sound;
1063 mAudioStreamType = STREAM_DEFAULT;
1064 return this;
1065 }
1066
Joe Onoratocb109a02011-01-18 17:57:41 -08001067 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001068 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001069 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001070 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1071 *
1072 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001073 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001074 public Builder setSound(Uri sound, int streamType) {
1075 mSound = sound;
1076 mAudioStreamType = streamType;
1077 return this;
1078 }
1079
Joe Onoratocb109a02011-01-18 17:57:41 -08001080 /**
1081 * Set the vibration pattern to use.
1082 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001083
1084 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1085 * <code>pattern</code> parameter.
1086 *
1087
1088 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001089 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001090 public Builder setVibrate(long[] pattern) {
1091 mVibrate = pattern;
1092 return this;
1093 }
1094
Joe Onoratocb109a02011-01-18 17:57:41 -08001095 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001096 * Set the desired color for the indicator LED on the device, as well as the
1097 * blink duty cycle (specified in milliseconds).
1098 *
1099
1100 * Not all devices will honor all (or even any) of these values.
1101 *
1102
1103 * @see Notification#ledARGB
1104 * @see Notification#ledOnMS
1105 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001106 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001107 public Builder setLights(int argb, int onMs, int offMs) {
1108 mLedArgb = argb;
1109 mLedOnMs = onMs;
1110 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001111 return this;
1112 }
1113
Joe Onoratocb109a02011-01-18 17:57:41 -08001114 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001115 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001116 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001117
1118 * Ongoing notifications cannot be dismissed by the user, so your application or service
1119 * must take care of canceling them.
1120 *
1121
1122 * They are typically used to indicate a background task that the user is actively engaged
1123 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1124 * (e.g., a file download, sync operation, active network connection).
1125 *
1126
1127 * @see Notification#FLAG_ONGOING_EVENT
1128 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001129 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001130 public Builder setOngoing(boolean ongoing) {
1131 setFlag(FLAG_ONGOING_EVENT, ongoing);
1132 return this;
1133 }
1134
Joe Onoratocb109a02011-01-18 17:57:41 -08001135 /**
1136 * Set this flag if you would only like the sound, vibrate
1137 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001138 *
1139 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001140 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001141 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1142 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1143 return this;
1144 }
1145
Joe Onoratocb109a02011-01-18 17:57:41 -08001146 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001147 * Make this notification automatically dismissed when the user touches it. The
1148 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1149 *
1150 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001151 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001152 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001153 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001154 return this;
1155 }
1156
Joe Onoratocb109a02011-01-18 17:57:41 -08001157 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001158 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001159 * <p>
1160 * The value should be one or more of the following fields combined with
1161 * bitwise-or:
1162 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1163 * <p>
1164 * For all default values, use {@link #DEFAULT_ALL}.
1165 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001166 public Builder setDefaults(int defaults) {
1167 mDefaults = defaults;
1168 return this;
1169 }
1170
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001171 /**
1172 * Set the priority of this notification.
1173 *
1174 * @see Notification#priority
1175 */
1176 public Builder setPriority(int pri) {
1177 mPriority = pri;
1178 return this;
1179 }
1180
1181 /**
1182 * Add a kind (category) to this notification. Optional.
1183 *
1184 * @see Notification#kind
1185 */
1186 public Builder addKind(String k) {
1187 mKindList.add(k);
1188 return this;
1189 }
1190
1191 /**
1192 * Add metadata to this notification.
1193 *
1194 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
1195 * current contents are copied into the Notification each time {@link #getNotification()} is
1196 * called.
1197 *
1198 * @see Notification#extras
1199 * @hide
1200 */
1201 public Builder setExtras(Bundle bag) {
1202 mExtras = bag;
1203 return this;
1204 }
1205
Joe Onorato46439ce2010-11-19 13:56:21 -08001206 private void setFlag(int mask, boolean value) {
1207 if (value) {
1208 mFlags |= mask;
1209 } else {
1210 mFlags &= ~mask;
1211 }
1212 }
1213
Joe Onorato561d3852010-11-20 18:09:34 -08001214 private RemoteViews makeRemoteViews(int resId) {
1215 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001216 boolean hasLine3 = false;
Joe Onorato561d3852010-11-20 18:09:34 -08001217 if (mSmallIcon != 0) {
1218 contentView.setImageViewResource(R.id.icon, mSmallIcon);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001219 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
1220 } else {
1221 contentView.setViewVisibility(R.id.icon, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001222 }
1223 if (mContentTitle != null) {
1224 contentView.setTextViewText(R.id.title, mContentTitle);
1225 }
1226 if (mContentText != null) {
1227 contentView.setTextViewText(R.id.text, mContentText);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001228 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001229 }
1230 if (mContentInfo != null) {
1231 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001232 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1233 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001234 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001235 final int tooBig = mContext.getResources().getInteger(
1236 R.integer.status_bar_notification_info_maxnum);
1237 if (mNumber > tooBig) {
1238 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1239 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001240 } else {
1241 NumberFormat f = NumberFormat.getIntegerInstance();
1242 contentView.setTextViewText(R.id.info, f.format(mNumber));
1243 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001244 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1245 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001246 } else {
1247 contentView.setViewVisibility(R.id.info, View.GONE);
1248 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001249 if (mProgressMax != 0 || mProgressIndeterminate) {
1250 contentView.setProgressBar(
1251 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1252 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
1253 } else {
1254 contentView.setViewVisibility(R.id.progress, View.GONE);
1255 }
Joe Onorato561d3852010-11-20 18:09:34 -08001256 if (mWhen != 0) {
1257 contentView.setLong(R.id.time, "setTime", mWhen);
1258 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001259 contentView.setViewVisibility(R.id.line3, hasLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001260 return contentView;
1261 }
1262
Joe Onorato46439ce2010-11-19 13:56:21 -08001263 private RemoteViews makeContentView() {
1264 if (mContentView != null) {
1265 return mContentView;
1266 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001267 return makeRemoteViews(mLargeIcon == null
1268 ? R.layout.status_bar_latest_event_content
1269 : R.layout.status_bar_latest_event_content_large_icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08001270 }
1271 }
1272
1273 private RemoteViews makeTickerView() {
1274 if (mTickerView != null) {
1275 return mTickerView;
1276 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001277 if (mContentView == null) {
1278 return makeRemoteViews(mLargeIcon == null
1279 ? R.layout.status_bar_latest_event_ticker
1280 : R.layout.status_bar_latest_event_ticker_large_icon);
1281 } else {
1282 return null;
1283 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001284 }
1285 }
1286
Joe Onoratocb109a02011-01-18 17:57:41 -08001287 /**
1288 * Combine all of the options that have been set and return a new {@link Notification}
1289 * object.
1290 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001291 public Notification getNotification() {
1292 Notification n = new Notification();
1293 n.when = mWhen;
1294 n.icon = mSmallIcon;
1295 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001296 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001297 n.contentView = makeContentView();
1298 n.contentIntent = mContentIntent;
1299 n.deleteIntent = mDeleteIntent;
1300 n.fullScreenIntent = mFullScreenIntent;
1301 n.tickerText = mTickerText;
1302 n.tickerView = makeTickerView();
1303 n.largeIcon = mLargeIcon;
1304 n.sound = mSound;
1305 n.audioStreamType = mAudioStreamType;
1306 n.vibrate = mVibrate;
1307 n.ledARGB = mLedArgb;
1308 n.ledOnMS = mLedOnMs;
1309 n.ledOffMS = mLedOffMs;
1310 n.defaults = mDefaults;
1311 n.flags = mFlags;
Joe Onorato8d0b6552010-11-22 16:09:29 -08001312 if (mLedOnMs != 0 && mLedOffMs != 0) {
1313 n.flags |= FLAG_SHOW_LIGHTS;
1314 }
1315 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1316 n.flags |= FLAG_SHOW_LIGHTS;
1317 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001318 if (mKindList.size() > 0) {
1319 n.kind = new String[mKindList.size()];
1320 mKindList.toArray(n.kind);
1321 } else {
1322 n.kind = null;
1323 }
1324 n.priority = mPriority;
1325 n.extras = mExtras != null ? new Bundle(mExtras) : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08001326 return n;
1327 }
1328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329}