blob: 04ab4070f84ee38ddfd4f702cc4c882006ef953c [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;
Daniel Sandlera0a938c2012-03-15 08:42:37 -040026import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Parcel;
28import android.os.Parcelable;
29import android.text.TextUtils;
Daniel Sandlera0a938c2012-03-15 08:42:37 -040030import android.util.IntProperty;
Joe Onorato8595a3d2010-11-19 18:12:07 -080031import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070032import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.widget.RemoteViews;
34
Andy Stadler110988c2010-12-03 14:29:16 -080035import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050036import java.util.ArrayList;
Joe Onorato561d3852010-11-20 18:09:34 -080037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038/**
39 * A class that represents how a persistent notification is to be presented to
40 * the user using the {@link android.app.NotificationManager}.
41 *
Joe Onoratocb109a02011-01-18 17:57:41 -080042 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
43 * easier to construct Notifications.</p>
44 *
Joe Fernandez558459f2011-10-13 16:47:36 -070045 * <div class="special reference">
46 * <h3>Developer Guides</h3>
47 * <p>For a guide to creating notifications, read the
48 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
49 * developer guide.</p>
50 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 */
52public class Notification implements Parcelable
53{
54 /**
55 * Use all default values (where applicable).
56 */
57 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * Use the default notification sound. This will ignore any given
61 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050062 *
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050065 */
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 public static final int DEFAULT_SOUND = 1;
68
69 /**
70 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050071 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070072 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050075 */
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 /**
80 * Use the default notification lights. This will ignore the
81 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
82 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050085 */
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -050090 * A timestamp related to this notification, in milliseconds since the epoch.
91 *
92 * Default value: {@link System#currentTimeMillis() Now}.
93 *
94 * Choose a timestamp that will be most relevant to the user. For most finite events, this
95 * corresponds to the time the event happened (or will happen, in the case of events that have
96 * yet to occur but about which the user is being informed). Indefinite events should be
97 * timestamped according to when the activity began.
98 *
99 * Some examples:
100 *
101 * <ul>
102 * <li>Notification of a new chat message should be stamped when the message was received.</li>
103 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
104 * <li>Notification of a completed file download should be stamped when the download finished.</li>
105 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
106 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
107 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
108 * </ul>
109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 */
111 public long when;
112
113 /**
114 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500115 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 */
117 public int icon;
118
119 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800120 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
121 * leave it at its default value of 0.
122 *
123 * @see android.widget.ImageView#setImageLevel
124 * @see android.graphics.drawable#setLevel
125 */
126 public int iconLevel;
127
128 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 * The number of events that this notification represents. For example, in a new mail
130 * notification, this could be the number of unread messages.
131 *
132 * The system may or may not use this field to modify the appearance of the notification. For
133 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
134 * superimposed over the icon in the status bar. Starting with
135 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
136 * {@link Notification.Builder} has displayed the number in the expanded notification view.
137 *
138 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 */
140 public int number;
141
142 /**
143 * The intent to execute when the expanded status entry is clicked. If
144 * this is an activity, it must include the
145 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800146 * that you take care of task management as described in the
147 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800148 * Stack</a> document. In particular, make sure to read the notification section
149 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
150 * Notifications</a> for the correct ways to launch an application from a
151 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
153 public PendingIntent contentIntent;
154
155 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 * The intent to execute when the notification is explicitly dismissed by the user, either with
157 * the "Clear All" button or by swiping it away individually.
158 *
159 * This probably shouldn't be launching an activity since several of those will be sent
160 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 */
162 public PendingIntent deleteIntent;
163
164 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700165 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800166 *
167 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400168 */
169 public PendingIntent fullScreenIntent;
170
171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400173 * the status bar on large and smaller devices.
174 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800175 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 */
177 public CharSequence tickerText;
178
179 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800180 * The view to show as the ticker in the status bar when the notification
181 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400182 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800183 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400184
185 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400186 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 */
188 public RemoteViews contentView;
189
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400190
191 /**
192 * The view that will represent this notification in the pop-up "intruder alert" dialog.
193 * @hide
194 */
195 public RemoteViews intruderView;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800198 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800200 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
202 /**
203 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500204 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500206 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * </p>
208 */
209 public Uri sound;
210
211 /**
212 * Use this constant as the value for audioStreamType to request that
213 * the default stream type for notifications be used. Currently the
214 * default stream type is STREAM_RING.
215 */
216 public static final int STREAM_DEFAULT = -1;
217
218 /**
219 * The audio stream type to use when playing the sound.
220 * Should be one of the STREAM_ constants from
221 * {@link android.media.AudioManager}.
222 */
223 public int audioStreamType = STREAM_DEFAULT;
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * The pattern with which to vibrate.
227 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <p>
229 * To vibrate the default pattern, see {@link #defaults}.
230 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * @see android.os.Vibrator#vibrate(long[],int)
233 */
234 public long[] vibrate;
235
236 /**
237 * The color of the led. The hardware will do its best approximation.
238 *
239 * @see #FLAG_SHOW_LIGHTS
240 * @see #flags
241 */
242 public int ledARGB;
243
244 /**
245 * The number of milliseconds for the LED to be on 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 ledOnMS;
252
253 /**
254 * The number of milliseconds for the LED to be off while it's flashing.
255 * The hardware will do its best approximation.
256 *
257 * @see #FLAG_SHOW_LIGHTS
258 * @see #flags
259 */
260 public int ledOffMS;
261
262 /**
263 * Specifies which values should be taken from the defaults.
264 * <p>
265 * To set, OR the desired from {@link #DEFAULT_SOUND},
266 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
267 * values, use {@link #DEFAULT_ALL}.
268 * </p>
269 */
270 public int defaults;
271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * Bit to be bitwise-ored into the {@link #flags} field that should be
274 * set if you want the LED on for this notification.
275 * <ul>
276 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
277 * or 0 for both ledOnMS and ledOffMS.</li>
278 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
279 * <li>To flash the LED, pass the number of milliseconds that it should
280 * be on and off to ledOnMS and ledOffMS.</li>
281 * </ul>
282 * <p>
283 * Since hardware varies, you are not guaranteed that any of the values
284 * you pass are honored exactly. Use the system defaults (TODO) if possible
285 * because they will be set to values that work on any given hardware.
286 * <p>
287 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500288 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 */
290 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
291
292 /**
293 * Bit to be bitwise-ored into the {@link #flags} field that should be
294 * set if this notification is in reference to something that is ongoing,
295 * like a phone call. It should not be set if this notification is in
296 * reference to something that happened at a particular point in time,
297 * like a missed phone call.
298 */
299 public static final int FLAG_ONGOING_EVENT = 0x00000002;
300
301 /**
302 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700303 * the audio will be repeated until the notification is
304 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 */
306 public static final int FLAG_INSISTENT = 0x00000004;
307
308 /**
309 * Bit to be bitwise-ored into the {@link #flags} field that should be
310 * set if you want the sound and/or vibration play each time the
311 * notification is sent, even if it has not been canceled before that.
312 */
313 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
314
315 /**
316 * Bit to be bitwise-ored into the {@link #flags} field that should be
317 * set if the notification should be canceled when it is clicked by the
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500318 * user. On tablets, the
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 */
321 public static final int FLAG_AUTO_CANCEL = 0x00000010;
322
323 /**
324 * Bit to be bitwise-ored into the {@link #flags} field that should be
325 * set if the notification should not be canceled when the user clicks
326 * the Clear all button.
327 */
328 public static final int FLAG_NO_CLEAR = 0x00000020;
329
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700330 /**
331 * Bit to be bitwise-ored into the {@link #flags} field that should be
332 * set if this notification represents a currently running service. This
333 * will normally be set for you by {@link Service#startForeground}.
334 */
335 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
336
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400337 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500338 * Obsolete flag indicating high-priority notifications; use the priority field instead.
339 *
340 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400341 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500342 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 public int flags;
345
346 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500347 * Default notification {@link #priority}. If your application does not prioritize its own
348 * notifications, use this value for all notifications.
349 */
350 public static final int PRIORITY_DEFAULT = 0;
351
352 /**
353 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
354 * items smaller, or at a different position in the list, compared with your app's
355 * {@link #PRIORITY_DEFAULT} items.
356 */
357 public static final int PRIORITY_LOW = -1;
358
359 /**
360 * Lowest {@link #priority}; these items might not be shown to the user except under special
361 * circumstances, such as detailed notification logs.
362 */
363 public static final int PRIORITY_MIN = -2;
364
365 /**
366 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
367 * show these items larger, or at a different position in notification lists, compared with
368 * your app's {@link #PRIORITY_DEFAULT} items.
369 */
370 public static final int PRIORITY_HIGH = 1;
371
372 /**
373 * Highest {@link #priority}, for your application's most important items that require the
374 * user's prompt attention or input.
375 */
376 public static final int PRIORITY_MAX = 2;
377
378 /**
379 * Relative priority for this notification.
380 *
381 * Priority is an indication of how much of the user's valuable attention should be consumed by
382 * this notification. Low-priority notifications may be hidden from the user in certain
383 * situations, while the user might be interrupted for a higher-priority notification. The
384 * system will make a determination about how to interpret notification priority as described in
385 * MUMBLE MUMBLE.
386 */
387 public int priority;
388
389 /**
390 * Notification type: incoming call (voice or video) or similar synchronous communication request.
391 */
392 public static final String KIND_CALL = "android.call";
393
394 /**
395 * Notification type: incoming direct message (SMS, instant message, etc.).
396 */
397 public static final String KIND_MESSAGE = "android.message";
398
399 /**
400 * Notification type: asynchronous bulk message (email).
401 */
402 public static final String KIND_EMAIL = "android.email";
403
404 /**
405 * Notification type: calendar event.
406 */
407 public static final String KIND_EVENT = "android.event";
408
409 /**
410 * Notification type: promotion or advertisement.
411 */
412 public static final String KIND_PROMO = "android.promo";
413
414 /**
415 * If this notification matches of one or more special types (see the <code>KIND_*</code>
416 * constants), add them here, best match first.
417 */
418 public String[] kind;
419
420 /**
421 * Extra key for people values (type TBD).
422 *
423 * @hide
424 */
425 public static final String EXTRA_PEOPLE = "android.people";
426
427 private Bundle extras;
428
429 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400430 * Structure to encapsulate an "action", including title and icon, that can be attached to a Notification.
431 * @hide
432 */
433 private static class Action implements Parcelable {
434 public int icon;
435 public CharSequence title;
436 public PendingIntent actionIntent;
437 @SuppressWarnings("unused")
438 public Action() { }
439 private Action(Parcel in) {
440 icon = in.readInt();
441 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
442 if (in.readInt() == 1) {
443 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
444 }
445 }
446 public Action(int icon_, CharSequence title_, PendingIntent intent_) {
447 this.icon = icon_;
448 this.title = title_;
449 this.actionIntent = intent_;
450 }
451 @Override
452 public Action clone() {
453 return new Action(
454 this.icon,
455 this.title.toString(),
456 this.actionIntent // safe to alias
457 );
458 }
459 @Override
460 public int describeContents() {
461 return 0;
462 }
463 @Override
464 public void writeToParcel(Parcel out, int flags) {
465 out.writeInt(icon);
466 TextUtils.writeToParcel(title, out, flags);
467 if (actionIntent != null) {
468 out.writeInt(1);
469 actionIntent.writeToParcel(out, flags);
470 } else {
471 out.writeInt(0);
472 }
473 }
474 public static final Parcelable.Creator<Action> CREATOR
475 = new Parcelable.Creator<Action>() {
476 public Action createFromParcel(Parcel in) {
477 return new Action(in);
478 }
479 public Action[] newArray(int size) {
480 return new Action[size];
481 }
482 };
483 }
484
485 private Action[] actions;
486
487 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500488 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800489 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 */
491 public Notification()
492 {
493 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500494 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 * @hide
499 */
500 public Notification(Context context, int icon, CharSequence tickerText, long when,
501 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
502 {
503 this.when = when;
504 this.icon = icon;
505 this.tickerText = tickerText;
506 setLatestEventInfo(context, contentTitle, contentText,
507 PendingIntent.getActivity(context, 0, contentIntent, 0));
508 }
509
510 /**
511 * Constructs a Notification object with the information needed to
512 * have a status bar icon without the standard expanded view.
513 *
514 * @param icon The resource id of the icon to put in the status bar.
515 * @param tickerText The text that flows by in the status bar when the notification first
516 * activates.
517 * @param when The time to show in the time field. In the System.currentTimeMillis
518 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800519 *
520 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800522 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 public Notification(int icon, CharSequence tickerText, long when)
524 {
525 this.icon = icon;
526 this.tickerText = tickerText;
527 this.when = when;
528 }
529
530 /**
531 * Unflatten the notification from a parcel.
532 */
533 public Notification(Parcel parcel)
534 {
535 int version = parcel.readInt();
536
537 when = parcel.readLong();
538 icon = parcel.readInt();
539 number = parcel.readInt();
540 if (parcel.readInt() != 0) {
541 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
542 }
543 if (parcel.readInt() != 0) {
544 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
545 }
546 if (parcel.readInt() != 0) {
547 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
548 }
549 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800550 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400551 }
552 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
554 }
Joe Onorato561d3852010-11-20 18:09:34 -0800555 if (parcel.readInt() != 0) {
556 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 defaults = parcel.readInt();
559 flags = parcel.readInt();
560 if (parcel.readInt() != 0) {
561 sound = Uri.CREATOR.createFromParcel(parcel);
562 }
563
564 audioStreamType = parcel.readInt();
565 vibrate = parcel.createLongArray();
566 ledARGB = parcel.readInt();
567 ledOnMS = parcel.readInt();
568 ledOffMS = parcel.readInt();
569 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400570
571 if (parcel.readInt() != 0) {
572 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
573 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500574
575 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400576
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577 kind = parcel.createStringArray(); // may set kind to null
578
579 if (parcel.readInt() != 0) {
580 extras = parcel.readBundle();
581 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400582
583 actions = parcel.createTypedArray(Action.CREATOR);
584 if (parcel.readInt() != 0) {
585 intruderView = RemoteViews.CREATOR.createFromParcel(parcel);
586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
588
Andy Stadler110988c2010-12-03 14:29:16 -0800589 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700590 public Notification clone() {
591 Notification that = new Notification();
592
593 that.when = this.when;
594 that.icon = this.icon;
595 that.number = this.number;
596
597 // PendingIntents are global, so there's no reason (or way) to clone them.
598 that.contentIntent = this.contentIntent;
599 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400600 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700601
602 if (this.tickerText != null) {
603 that.tickerText = this.tickerText.toString();
604 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800605 if (this.tickerView != null) {
606 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400607 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700608 if (this.contentView != null) {
609 that.contentView = this.contentView.clone();
610 }
Joe Onorato561d3852010-11-20 18:09:34 -0800611 if (this.largeIcon != null) {
612 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
613 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100614 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700615 that.sound = this.sound; // android.net.Uri is immutable
616 that.audioStreamType = this.audioStreamType;
617
618 final long[] vibrate = this.vibrate;
619 if (vibrate != null) {
620 final int N = vibrate.length;
621 final long[] vib = that.vibrate = new long[N];
622 System.arraycopy(vibrate, 0, vib, 0, N);
623 }
624
625 that.ledARGB = this.ledARGB;
626 that.ledOnMS = this.ledOnMS;
627 that.ledOffMS = this.ledOffMS;
628 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629
Joe Onorato18e69df2010-05-17 22:26:12 -0700630 that.flags = this.flags;
631
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632 that.priority = this.priority;
633
634 final String[] thiskind = this.kind;
635 if (thiskind != null) {
636 final int N = thiskind.length;
637 final String[] thatkind = that.kind = new String[N];
638 System.arraycopy(thiskind, 0, thatkind, 0, N);
639 }
640
641 if (this.extras != null) {
642 that.extras = new Bundle(this.extras);
643
644 }
645
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400646 that.actions = new Action[this.actions.length];
647 for(int i=0; i<this.actions.length; i++) {
648 that.actions[i] = this.actions[i].clone();
649 }
650 if (this.intruderView != null) {
651 that.intruderView = this.intruderView.clone();
652 }
653
Joe Onorato18e69df2010-05-17 22:26:12 -0700654 return that;
655 }
656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 public int describeContents() {
658 return 0;
659 }
660
661 /**
662 * Flatten this notification from a parcel.
663 */
664 public void writeToParcel(Parcel parcel, int flags)
665 {
666 parcel.writeInt(1);
667
668 parcel.writeLong(when);
669 parcel.writeInt(icon);
670 parcel.writeInt(number);
671 if (contentIntent != null) {
672 parcel.writeInt(1);
673 contentIntent.writeToParcel(parcel, 0);
674 } else {
675 parcel.writeInt(0);
676 }
677 if (deleteIntent != null) {
678 parcel.writeInt(1);
679 deleteIntent.writeToParcel(parcel, 0);
680 } else {
681 parcel.writeInt(0);
682 }
683 if (tickerText != null) {
684 parcel.writeInt(1);
685 TextUtils.writeToParcel(tickerText, parcel, flags);
686 } else {
687 parcel.writeInt(0);
688 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800689 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400690 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800691 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400692 } else {
693 parcel.writeInt(0);
694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 if (contentView != null) {
696 parcel.writeInt(1);
697 contentView.writeToParcel(parcel, 0);
698 } else {
699 parcel.writeInt(0);
700 }
Joe Onorato561d3852010-11-20 18:09:34 -0800701 if (largeIcon != null) {
702 parcel.writeInt(1);
703 largeIcon.writeToParcel(parcel, 0);
704 } else {
705 parcel.writeInt(0);
706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707
708 parcel.writeInt(defaults);
709 parcel.writeInt(this.flags);
710
711 if (sound != null) {
712 parcel.writeInt(1);
713 sound.writeToParcel(parcel, 0);
714 } else {
715 parcel.writeInt(0);
716 }
717 parcel.writeInt(audioStreamType);
718 parcel.writeLongArray(vibrate);
719 parcel.writeInt(ledARGB);
720 parcel.writeInt(ledOnMS);
721 parcel.writeInt(ledOffMS);
722 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400723
724 if (fullScreenIntent != null) {
725 parcel.writeInt(1);
726 fullScreenIntent.writeToParcel(parcel, 0);
727 } else {
728 parcel.writeInt(0);
729 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500730
731 parcel.writeInt(priority);
732
733 parcel.writeStringArray(kind); // ok for null
734
735 if (extras != null) {
736 parcel.writeInt(1);
737 extras.writeToParcel(parcel, 0);
738 } else {
739 parcel.writeInt(0);
740 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400741
742 parcel.writeTypedArray(actions, 0);
743
744 if (intruderView != null) {
745 parcel.writeInt(1);
746 intruderView.writeToParcel(parcel, 0);
747 } else {
748 parcel.writeInt(0);
749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751
752 /**
753 * Parcelable.Creator that instantiates Notification objects
754 */
755 public static final Parcelable.Creator<Notification> CREATOR
756 = new Parcelable.Creator<Notification>()
757 {
758 public Notification createFromParcel(Parcel parcel)
759 {
760 return new Notification(parcel);
761 }
762
763 public Notification[] newArray(int size)
764 {
765 return new Notification[size];
766 }
767 };
768
769 /**
770 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
771 * layout.
772 *
773 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
774 * in the view.</p>
775 * @param context The context for your application / activity.
776 * @param contentTitle The title that goes in the expanded entry.
777 * @param contentText The text that goes in the expanded entry.
778 * @param contentIntent The intent to launch when the user clicks the expanded notification.
779 * If this is an activity, it must include the
780 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800781 * that you take care of task management as described in the
782 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
783 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800785 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800787 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 public void setLatestEventInfo(Context context,
789 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
790 RemoteViews contentView = new RemoteViews(context.getPackageName(),
Joe Onorato561d3852010-11-20 18:09:34 -0800791 R.layout.status_bar_latest_event_content);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 if (this.icon != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800793 contentView.setImageViewResource(R.id.icon, this.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795 if (contentTitle != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800796 contentView.setTextViewText(R.id.title, contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798 if (contentText != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800799 contentView.setTextViewText(R.id.text, contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 }
801 if (this.when != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800802 contentView.setLong(R.id.time, "setTime", when);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
805 this.contentView = contentView;
806 this.contentIntent = contentIntent;
807 }
808
809 @Override
810 public String toString() {
811 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500812 sb.append("Notification(pri=");
813 sb.append(priority);
814 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -0800815 if (contentView != null) {
816 sb.append(contentView.getPackage());
817 sb.append("/0x");
818 sb.append(Integer.toHexString(contentView.getLayoutId()));
819 } else {
820 sb.append("null");
821 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500822 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -0800823 sb.append(" vibrate=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 if (this.vibrate != null) {
825 int N = this.vibrate.length-1;
826 sb.append("[");
827 for (int i=0; i<N; i++) {
828 sb.append(this.vibrate[i]);
829 sb.append(',');
830 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200831 if (N != -1) {
832 sb.append(this.vibrate[N]);
833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 sb.append("]");
835 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) {
836 sb.append("default");
837 } else {
838 sb.append("null");
839 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500840 sb.append(" sound=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 if (this.sound != null) {
842 sb.append(this.sound.toString());
843 } else if ((this.defaults & DEFAULT_SOUND) != 0) {
844 sb.append("default");
845 } else {
846 sb.append("null");
847 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500848 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500850 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400851 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500852 sb.append(" kind=[");
853 if (this.kind == null) {
854 sb.append("null");
855 } else {
856 for (int i=0; i<this.kind.length; i++) {
857 if (i>0) sb.append(",");
858 sb.append(this.kind[i]);
859 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400860 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400861 sb.append("]");
862 if (actions != null) {
863 sb.append(" ");
864 sb.append(actions.length);
865 sb.append(" action");
866 if (actions.length > 1) sb.append("s");
867 }
868 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 return sb.toString();
870 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800871
Joe Onoratocb109a02011-01-18 17:57:41 -0800872 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500873 * Builder class for {@link Notification} objects.
874 *
875 * Provides a convenient way to set the various fields of a {@link Notification} and generate
876 * content views using the platform's notification layout template.
877 *
878 * Example:
879 *
880 * <pre class="prettyprint">
881 * Notification noti = new Notification.Builder()
882 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
883 * .setContentText(subject)
884 * .setSmallIcon(R.drawable.new_mail)
885 * .setLargeIcon(aBitmap)
886 * .getNotification();
887 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -0800888 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800889 public static class Builder {
890 private Context mContext;
891
892 private long mWhen;
893 private int mSmallIcon;
894 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800895 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800896 private CharSequence mContentTitle;
897 private CharSequence mContentText;
898 private CharSequence mContentInfo;
899 private PendingIntent mContentIntent;
900 private RemoteViews mContentView;
901 private PendingIntent mDeleteIntent;
902 private PendingIntent mFullScreenIntent;
903 private CharSequence mTickerText;
904 private RemoteViews mTickerView;
905 private Bitmap mLargeIcon;
906 private Uri mSound;
907 private int mAudioStreamType;
908 private long[] mVibrate;
909 private int mLedArgb;
910 private int mLedOnMs;
911 private int mLedOffMs;
912 private int mDefaults;
913 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -0700914 private int mProgressMax;
915 private int mProgress;
916 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500917 private ArrayList<String> mKindList = new ArrayList<String>(1);
918 private Bundle mExtras;
919 private int mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400920 private ArrayList<Action> mActions = new ArrayList<Action>(3);
Joe Onorato46439ce2010-11-19 13:56:21 -0800921
Joe Onoratocb109a02011-01-18 17:57:41 -0800922 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500923 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -0800924 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500925
926 * <table>
927 * <tr><th align=right>priority</th>
928 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
929 * <tr><th align=right>when</th>
930 * <td>now ({@link System#currentTimeMillis()})</td></tr>
931 * <tr><th align=right>audio stream</th>
932 * <td>{@link #STREAM_DEFAULT}</td></tr>
933 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -0800934 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500935
936 * @param context
937 * A {@link Context} that will be used by the Builder to construct the
938 * RemoteViews. The Context will not be held past the lifetime of this Builder
939 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -0800940 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800941 public Builder(Context context) {
942 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -0800943
944 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -0800945 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -0800946 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500947 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -0800948 }
949
Joe Onoratocb109a02011-01-18 17:57:41 -0800950 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500951 * Add a timestamp pertaining to the notification (usually the time the event occurred).
952 *
953
954 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -0800955 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800956 public Builder setWhen(long when) {
957 mWhen = when;
958 return this;
959 }
960
Joe Onoratocb109a02011-01-18 17:57:41 -0800961 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500962 * Set the small icon resource, which will be used to represent the notification in the
963 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800964 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500965
966 * The platform template for the expanded view will draw this icon in the left, unless a
967 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
968 * icon will be moved to the right-hand side.
969 *
970
971 * @param icon
972 * A resource ID in the application's package of the drawable to use.
973 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -0800974 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800975 public Builder setSmallIcon(int icon) {
976 mSmallIcon = icon;
977 return this;
978 }
979
Joe Onoratocb109a02011-01-18 17:57:41 -0800980 /**
981 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
982 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
983 * LevelListDrawable}.
984 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500985 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -0800986 * @param level The level to use for the icon.
987 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500988 * @see Notification#icon
989 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -0800990 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800991 public Builder setSmallIcon(int icon, int level) {
992 mSmallIcon = icon;
993 mSmallIconLevel = level;
994 return this;
995 }
996
Joe Onoratocb109a02011-01-18 17:57:41 -0800997 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500998 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -0800999 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001000 public Builder setContentTitle(CharSequence title) {
1001 mContentTitle = title;
1002 return this;
1003 }
1004
Joe Onoratocb109a02011-01-18 17:57:41 -08001005 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001006 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001007 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001008 public Builder setContentText(CharSequence text) {
1009 mContentText = text;
1010 return this;
1011 }
1012
Joe Onoratocb109a02011-01-18 17:57:41 -08001013 /**
1014 * Set the large number at the right-hand side of the notification. This is
1015 * equivalent to setContentInfo, although it might show the number in a different
1016 * font size for readability.
1017 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001018 public Builder setNumber(int number) {
1019 mNumber = number;
1020 return this;
1021 }
1022
Joe Onoratocb109a02011-01-18 17:57:41 -08001023 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001024 * A small piece of additional information pertaining to this notification.
1025 *
1026
1027 * The platform template will draw this on the last line of the notification, at the far
1028 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001029 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001030 public Builder setContentInfo(CharSequence info) {
1031 mContentInfo = info;
1032 return this;
1033 }
1034
Joe Onoratocb109a02011-01-18 17:57:41 -08001035 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001036 * Set the progress this notification represents.
1037 *
1038
1039 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001040 */
1041 public Builder setProgress(int max, int progress, boolean indeterminate) {
1042 mProgressMax = max;
1043 mProgress = progress;
1044 mProgressIndeterminate = indeterminate;
1045 return this;
1046 }
1047
1048 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001049 * Supply a custom RemoteViews to use instead of the platform template.
1050 *
1051
1052 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001053 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001054 public Builder setContent(RemoteViews views) {
1055 mContentView = views;
1056 return this;
1057 }
1058
Joe Onoratocb109a02011-01-18 17:57:41 -08001059 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001060 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1061 *
1062
1063 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1064 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1065 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
1066
1067 * to assign PendingIntents to individual views in that custom layout (i.e., to create
1068
1069 * clickable buttons inside the
1070 * notification view).
1071 *
1072
1073 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001074 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001075 public Builder setContentIntent(PendingIntent intent) {
1076 mContentIntent = intent;
1077 return this;
1078 }
1079
Joe Onoratocb109a02011-01-18 17:57:41 -08001080 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001081 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1082 *
1083
1084 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001085 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001086 public Builder setDeleteIntent(PendingIntent intent) {
1087 mDeleteIntent = intent;
1088 return this;
1089 }
1090
Joe Onoratocb109a02011-01-18 17:57:41 -08001091 /**
1092 * An intent to launch instead of posting the notification to the status bar.
1093 * Only for use with extremely high-priority notifications demanding the user's
1094 * <strong>immediate</strong> attention, such as an incoming phone call or
1095 * alarm clock that the user has explicitly set to a particular time.
1096 * If this facility is used for something else, please give the user an option
1097 * to turn it off and use a normal notification, as this can be extremely
1098 * disruptive.
1099 *
1100 * @param intent The pending intent to launch.
1101 * @param highPriority Passing true will cause this notification to be sent
1102 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001103 *
1104 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001105 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001106 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1107 mFullScreenIntent = intent;
1108 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1109 return this;
1110 }
1111
Joe Onoratocb109a02011-01-18 17:57:41 -08001112 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001113 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001114 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001115 *
1116
1117 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001118 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001119 public Builder setTicker(CharSequence tickerText) {
1120 mTickerText = tickerText;
1121 return this;
1122 }
1123
Joe Onoratocb109a02011-01-18 17:57:41 -08001124 /**
1125 * Set the text that is displayed in the status bar when the notification first
1126 * arrives, and also a RemoteViews object that may be displayed instead on some
1127 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001128 *
1129 * @see Notification#tickerText
1130 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001131 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001132 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
1133 mTickerText = tickerText;
1134 mTickerView = views;
1135 return this;
1136 }
1137
Joe Onoratocb109a02011-01-18 17:57:41 -08001138 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001139 * Add a large icon to the notification (and the ticker on some devices).
1140 *
1141 * In the platform template, this image will be shown on the left of the notification view
1142 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1143 *
1144 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001145 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001146 public Builder setLargeIcon(Bitmap icon) {
1147 mLargeIcon = icon;
1148 return this;
1149 }
1150
Joe Onoratocb109a02011-01-18 17:57:41 -08001151 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001152 * Set the sound to play.
1153 *
1154 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1155 *
1156 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001157 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001158 public Builder setSound(Uri sound) {
1159 mSound = sound;
1160 mAudioStreamType = STREAM_DEFAULT;
1161 return this;
1162 }
1163
Joe Onoratocb109a02011-01-18 17:57:41 -08001164 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001165 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001166 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001167 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1168 *
1169 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001170 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001171 public Builder setSound(Uri sound, int streamType) {
1172 mSound = sound;
1173 mAudioStreamType = streamType;
1174 return this;
1175 }
1176
Joe Onoratocb109a02011-01-18 17:57:41 -08001177 /**
1178 * Set the vibration pattern to use.
1179 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001180
1181 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1182 * <code>pattern</code> parameter.
1183 *
1184
1185 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001186 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001187 public Builder setVibrate(long[] pattern) {
1188 mVibrate = pattern;
1189 return this;
1190 }
1191
Joe Onoratocb109a02011-01-18 17:57:41 -08001192 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001193 * Set the desired color for the indicator LED on the device, as well as the
1194 * blink duty cycle (specified in milliseconds).
1195 *
1196
1197 * Not all devices will honor all (or even any) of these values.
1198 *
1199
1200 * @see Notification#ledARGB
1201 * @see Notification#ledOnMS
1202 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001203 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001204 public Builder setLights(int argb, int onMs, int offMs) {
1205 mLedArgb = argb;
1206 mLedOnMs = onMs;
1207 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001208 return this;
1209 }
1210
Joe Onoratocb109a02011-01-18 17:57:41 -08001211 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001212 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001213 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001214
1215 * Ongoing notifications cannot be dismissed by the user, so your application or service
1216 * must take care of canceling them.
1217 *
1218
1219 * They are typically used to indicate a background task that the user is actively engaged
1220 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1221 * (e.g., a file download, sync operation, active network connection).
1222 *
1223
1224 * @see Notification#FLAG_ONGOING_EVENT
1225 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001226 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001227 public Builder setOngoing(boolean ongoing) {
1228 setFlag(FLAG_ONGOING_EVENT, ongoing);
1229 return this;
1230 }
1231
Joe Onoratocb109a02011-01-18 17:57:41 -08001232 /**
1233 * Set this flag if you would only like the sound, vibrate
1234 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001235 *
1236 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001237 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001238 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1239 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1240 return this;
1241 }
1242
Joe Onoratocb109a02011-01-18 17:57:41 -08001243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001244 * Make this notification automatically dismissed when the user touches it. The
1245 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1246 *
1247 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001248 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001249 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001250 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001251 return this;
1252 }
1253
Joe Onoratocb109a02011-01-18 17:57:41 -08001254 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001255 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001256 * <p>
1257 * The value should be one or more of the following fields combined with
1258 * bitwise-or:
1259 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1260 * <p>
1261 * For all default values, use {@link #DEFAULT_ALL}.
1262 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001263 public Builder setDefaults(int defaults) {
1264 mDefaults = defaults;
1265 return this;
1266 }
1267
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001268 /**
1269 * Set the priority of this notification.
1270 *
1271 * @see Notification#priority
1272 */
1273 public Builder setPriority(int pri) {
1274 mPriority = pri;
1275 return this;
1276 }
1277
1278 /**
1279 * Add a kind (category) to this notification. Optional.
1280 *
1281 * @see Notification#kind
1282 */
1283 public Builder addKind(String k) {
1284 mKindList.add(k);
1285 return this;
1286 }
1287
1288 /**
1289 * Add metadata to this notification.
1290 *
1291 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
1292 * current contents are copied into the Notification each time {@link #getNotification()} is
1293 * called.
1294 *
1295 * @see Notification#extras
1296 * @hide
1297 */
1298 public Builder setExtras(Bundle bag) {
1299 mExtras = bag;
1300 return this;
1301 }
1302
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001303 /**
1304 * Add an action to this notification. Actions are typically displayed by
1305 * the system as a button adjacent to the notification content.
1306 *
1307 * @param icon Resource ID of a drawable that represents the action.
1308 * @param title Text describing the action.
1309 * @param intent PendingIntent to be fired when the action is invoked.
1310 */
1311 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
1312 mActions.add(new Action(icon, title, intent));
1313 return this;
1314 }
1315
Joe Onorato46439ce2010-11-19 13:56:21 -08001316 private void setFlag(int mask, boolean value) {
1317 if (value) {
1318 mFlags |= mask;
1319 } else {
1320 mFlags &= ~mask;
1321 }
1322 }
1323
Joe Onorato561d3852010-11-20 18:09:34 -08001324 private RemoteViews makeRemoteViews(int resId) {
1325 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001326 boolean hasLine3 = false;
Joe Onorato561d3852010-11-20 18:09:34 -08001327 if (mSmallIcon != 0) {
1328 contentView.setImageViewResource(R.id.icon, mSmallIcon);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001329 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
1330 } else {
1331 contentView.setViewVisibility(R.id.icon, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001332 }
1333 if (mContentTitle != null) {
1334 contentView.setTextViewText(R.id.title, mContentTitle);
1335 }
1336 if (mContentText != null) {
1337 contentView.setTextViewText(R.id.text, mContentText);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001338 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001339 }
1340 if (mContentInfo != null) {
1341 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001342 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1343 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001344 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001345 final int tooBig = mContext.getResources().getInteger(
1346 R.integer.status_bar_notification_info_maxnum);
1347 if (mNumber > tooBig) {
1348 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1349 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001350 } else {
1351 NumberFormat f = NumberFormat.getIntegerInstance();
1352 contentView.setTextViewText(R.id.info, f.format(mNumber));
1353 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001354 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1355 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001356 } else {
1357 contentView.setViewVisibility(R.id.info, View.GONE);
1358 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001359 if (mProgressMax != 0 || mProgressIndeterminate) {
1360 contentView.setProgressBar(
1361 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1362 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
1363 } else {
1364 contentView.setViewVisibility(R.id.progress, View.GONE);
1365 }
Joe Onorato561d3852010-11-20 18:09:34 -08001366 if (mWhen != 0) {
1367 contentView.setLong(R.id.time, "setTime", mWhen);
1368 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001369 contentView.setViewVisibility(R.id.line3, hasLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001370 return contentView;
1371 }
1372
Joe Onorato46439ce2010-11-19 13:56:21 -08001373 private RemoteViews makeContentView() {
1374 if (mContentView != null) {
1375 return mContentView;
1376 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001377 return makeRemoteViews(mLargeIcon == null
1378 ? R.layout.status_bar_latest_event_content
1379 : R.layout.status_bar_latest_event_content_large_icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08001380 }
1381 }
1382
1383 private RemoteViews makeTickerView() {
1384 if (mTickerView != null) {
1385 return mTickerView;
1386 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001387 if (mContentView == null) {
1388 return makeRemoteViews(mLargeIcon == null
1389 ? R.layout.status_bar_latest_event_ticker
1390 : R.layout.status_bar_latest_event_ticker_large_icon);
1391 } else {
1392 return null;
1393 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001394 }
1395 }
1396
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001397 private RemoteViews makeIntruderView() {
1398 RemoteViews intruderView = new RemoteViews(mContext.getPackageName(),
1399 R.layout.notification_intruder_content);
1400 if (mLargeIcon != null) {
1401 intruderView.setImageViewBitmap(R.id.icon, mLargeIcon);
1402 intruderView.setViewVisibility(R.id.icon, View.VISIBLE);
1403 } else if (mSmallIcon != 0) {
1404 intruderView.setImageViewResource(R.id.icon, mSmallIcon);
1405 intruderView.setViewVisibility(R.id.icon, View.VISIBLE);
1406 } else {
1407 intruderView.setViewVisibility(R.id.icon, View.GONE);
1408 }
1409 if (mContentTitle != null) {
1410 intruderView.setTextViewText(R.id.title, mContentTitle);
1411 }
1412 if (mContentText != null) {
1413 intruderView.setTextViewText(R.id.text, mContentText);
1414 }
1415 if (mActions.size() > 0) {
1416 intruderView.setViewVisibility(R.id.actions, View.VISIBLE);
1417 int N = mActions.size();
1418 if (N>3) N=3;
1419 final int[] BUTTONS = { R.id.action0, R.id.action1, R.id.action2 };
1420 for (int i=0; i<N; i++) {
1421 final Action action = mActions.get(i);
1422 final int buttonId = BUTTONS[i];
1423
1424 intruderView.setViewVisibility(buttonId, View.VISIBLE);
1425 intruderView.setImageViewResource(buttonId, action.icon);
1426 intruderView.setContentDescription(buttonId, action.title);
1427 intruderView.setOnClickPendingIntent(buttonId, action.actionIntent);
1428 }
1429 } else {
1430 intruderView.setViewVisibility(R.id.actions, View.GONE);
1431 }
1432 return intruderView;
1433 }
1434
Joe Onoratocb109a02011-01-18 17:57:41 -08001435 /**
1436 * Combine all of the options that have been set and return a new {@link Notification}
1437 * object.
1438 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001439 public Notification getNotification() {
1440 Notification n = new Notification();
1441 n.when = mWhen;
1442 n.icon = mSmallIcon;
1443 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001444 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001445 n.contentView = makeContentView();
1446 n.contentIntent = mContentIntent;
1447 n.deleteIntent = mDeleteIntent;
1448 n.fullScreenIntent = mFullScreenIntent;
1449 n.tickerText = mTickerText;
1450 n.tickerView = makeTickerView();
1451 n.largeIcon = mLargeIcon;
1452 n.sound = mSound;
1453 n.audioStreamType = mAudioStreamType;
1454 n.vibrate = mVibrate;
1455 n.ledARGB = mLedArgb;
1456 n.ledOnMS = mLedOnMs;
1457 n.ledOffMS = mLedOffMs;
1458 n.defaults = mDefaults;
1459 n.flags = mFlags;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001460 n.intruderView = makeIntruderView();
Joe Onorato8d0b6552010-11-22 16:09:29 -08001461 if (mLedOnMs != 0 && mLedOffMs != 0) {
1462 n.flags |= FLAG_SHOW_LIGHTS;
1463 }
1464 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1465 n.flags |= FLAG_SHOW_LIGHTS;
1466 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001467 if (mKindList.size() > 0) {
1468 n.kind = new String[mKindList.size()];
1469 mKindList.toArray(n.kind);
1470 } else {
1471 n.kind = null;
1472 }
1473 n.priority = mPriority;
1474 n.extras = mExtras != null ? new Bundle(mExtras) : null;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001475 if (mActions.size() > 0) {
1476 n.actions = new Action[mActions.size()];
1477 mActions.toArray(n.actions);
1478 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001479 return n;
1480 }
1481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482}