blob: ecaaefc15015fc37349a2e322270b73c9bfb97e7 [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;
Jeff Sharkey098d5802012-04-26 17:30:34 -070024import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.net.Uri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050026import android.os.Bundle;
Daniel Sandlera0a938c2012-03-15 08:42:37 -040027import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.Parcel;
29import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040030import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.text.TextUtils;
Daniel Sandlera0a938c2012-03-15 08:42:37 -040032import android.util.IntProperty;
Daniel Sandler96fd7c12012-03-30 16:37:36 -040033import android.util.Log;
Joe Onorato8595a3d2010-11-19 18:12:07 -080034import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070035import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.widget.RemoteViews;
37
Andy Stadler110988c2010-12-03 14:29:16 -080038import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050039import java.util.ArrayList;
Joe Onorato561d3852010-11-20 18:09:34 -080040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
42 * A class that represents how a persistent notification is to be presented to
43 * the user using the {@link android.app.NotificationManager}.
44 *
Joe Onoratocb109a02011-01-18 17:57:41 -080045 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
46 * easier to construct Notifications.</p>
47 *
Joe Fernandez558459f2011-10-13 16:47:36 -070048 * <div class="special reference">
49 * <h3>Developer Guides</h3>
50 * <p>For a guide to creating notifications, read the
51 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
52 * developer guide.</p>
53 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
55public class Notification implements Parcelable
56{
57 /**
58 * Use all default values (where applicable).
59 */
60 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 /**
63 * Use the default notification sound. This will ignore any given
64 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050065 *
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050068 */
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 public static final int DEFAULT_SOUND = 1;
71
72 /**
73 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050074 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070075 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050076 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050078 */
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
83 * Use the default notification lights. This will ignore the
84 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
85 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088 */
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093 * A timestamp related to this notification, in milliseconds since the epoch.
94 *
95 * Default value: {@link System#currentTimeMillis() Now}.
96 *
97 * Choose a timestamp that will be most relevant to the user. For most finite events, this
98 * corresponds to the time the event happened (or will happen, in the case of events that have
99 * yet to occur but about which the user is being informed). Indefinite events should be
100 * timestamped according to when the activity began.
101 *
102 * Some examples:
103 *
104 * <ul>
105 * <li>Notification of a new chat message should be stamped when the message was received.</li>
106 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
107 * <li>Notification of a completed file download should be stamped when the download finished.</li>
108 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
109 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
110 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
111 * </ul>
112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114 public long when;
115
116 /**
117 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500118 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 */
120 public int icon;
121
122 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800123 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
124 * leave it at its default value of 0.
125 *
126 * @see android.widget.ImageView#setImageLevel
127 * @see android.graphics.drawable#setLevel
128 */
129 public int iconLevel;
130
131 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132 * The number of events that this notification represents. For example, in a new mail
133 * notification, this could be the number of unread messages.
134 *
135 * The system may or may not use this field to modify the appearance of the notification. For
136 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
137 * superimposed over the icon in the status bar. Starting with
138 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
139 * {@link Notification.Builder} has displayed the number in the expanded notification view.
140 *
141 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 */
143 public int number;
144
145 /**
146 * The intent to execute when the expanded status entry is clicked. If
147 * this is an activity, it must include the
148 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800149 * that you take care of task management as described in the
150 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800151 * Stack</a> document. In particular, make sure to read the notification section
152 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
153 * Notifications</a> for the correct ways to launch an application from a
154 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 */
156 public PendingIntent contentIntent;
157
158 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500159 * The intent to execute when the notification is explicitly dismissed by the user, either with
160 * the "Clear All" button or by swiping it away individually.
161 *
162 * This probably shouldn't be launching an activity since several of those will be sent
163 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 */
165 public PendingIntent deleteIntent;
166
167 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700168 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800169 *
170 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400171 */
172 public PendingIntent fullScreenIntent;
173
174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400176 * the status bar on large and smaller devices.
177 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800178 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 */
180 public CharSequence tickerText;
181
182 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800183 * The view to show as the ticker in the status bar when the notification
184 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400185 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800186 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400187
188 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400189 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 */
191 public RemoteViews contentView;
192
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400193 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400194 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400195 * opportunity to show more detail. The system UI may choose to show this
196 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400197 */
198 public RemoteViews bigContentView;
199
200 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800201 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800203 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
205 /**
206 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500209 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * </p>
211 */
212 public Uri sound;
213
214 /**
215 * Use this constant as the value for audioStreamType to request that
216 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700217 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 public static final int STREAM_DEFAULT = -1;
220
221 /**
222 * The audio stream type to use when playing the sound.
223 * Should be one of the STREAM_ constants from
224 * {@link android.media.AudioManager}.
225 */
226 public int audioStreamType = STREAM_DEFAULT;
227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500229 * The pattern with which to vibrate.
230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <p>
232 * To vibrate the default pattern, see {@link #defaults}.
233 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * @see android.os.Vibrator#vibrate(long[],int)
236 */
237 public long[] vibrate;
238
239 /**
240 * The color of the led. The hardware will do its best approximation.
241 *
242 * @see #FLAG_SHOW_LIGHTS
243 * @see #flags
244 */
245 public int ledARGB;
246
247 /**
248 * The number of milliseconds for the LED to be on while it's flashing.
249 * The hardware will do its best approximation.
250 *
251 * @see #FLAG_SHOW_LIGHTS
252 * @see #flags
253 */
254 public int ledOnMS;
255
256 /**
257 * The number of milliseconds for the LED to be off while it's flashing.
258 * The hardware will do its best approximation.
259 *
260 * @see #FLAG_SHOW_LIGHTS
261 * @see #flags
262 */
263 public int ledOffMS;
264
265 /**
266 * Specifies which values should be taken from the defaults.
267 * <p>
268 * To set, OR the desired from {@link #DEFAULT_SOUND},
269 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
270 * values, use {@link #DEFAULT_ALL}.
271 * </p>
272 */
273 public int defaults;
274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * Bit to be bitwise-ored into the {@link #flags} field that should be
277 * set if you want the LED on for this notification.
278 * <ul>
279 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
280 * or 0 for both ledOnMS and ledOffMS.</li>
281 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
282 * <li>To flash the LED, pass the number of milliseconds that it should
283 * be on and off to ledOnMS and ledOffMS.</li>
284 * </ul>
285 * <p>
286 * Since hardware varies, you are not guaranteed that any of the values
287 * you pass are honored exactly. Use the system defaults (TODO) if possible
288 * because they will be set to values that work on any given hardware.
289 * <p>
290 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500291 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 */
293 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
294
295 /**
296 * Bit to be bitwise-ored into the {@link #flags} field that should be
297 * set if this notification is in reference to something that is ongoing,
298 * like a phone call. It should not be set if this notification is in
299 * reference to something that happened at a particular point in time,
300 * like a missed phone call.
301 */
302 public static final int FLAG_ONGOING_EVENT = 0x00000002;
303
304 /**
305 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700306 * the audio will be repeated until the notification is
307 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 */
309 public static final int FLAG_INSISTENT = 0x00000004;
310
311 /**
312 * Bit to be bitwise-ored into the {@link #flags} field that should be
313 * set if you want the sound and/or vibration play each time the
314 * notification is sent, even if it has not been canceled before that.
315 */
316 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
317
318 /**
319 * Bit to be bitwise-ored into the {@link #flags} field that should be
320 * set if the notification should be canceled when it is clicked by the
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500321 * user. On tablets, the
322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
324 public static final int FLAG_AUTO_CANCEL = 0x00000010;
325
326 /**
327 * Bit to be bitwise-ored into the {@link #flags} field that should be
328 * set if the notification should not be canceled when the user clicks
329 * the Clear all button.
330 */
331 public static final int FLAG_NO_CLEAR = 0x00000020;
332
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700333 /**
334 * Bit to be bitwise-ored into the {@link #flags} field that should be
335 * set if this notification represents a currently running service. This
336 * will normally be set for you by {@link Service#startForeground}.
337 */
338 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
339
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400340 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500341 * Obsolete flag indicating high-priority notifications; use the priority field instead.
342 *
343 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400344 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500345 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 public int flags;
348
349 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500350 * Default notification {@link #priority}. If your application does not prioritize its own
351 * notifications, use this value for all notifications.
352 */
353 public static final int PRIORITY_DEFAULT = 0;
354
355 /**
356 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
357 * items smaller, or at a different position in the list, compared with your app's
358 * {@link #PRIORITY_DEFAULT} items.
359 */
360 public static final int PRIORITY_LOW = -1;
361
362 /**
363 * Lowest {@link #priority}; these items might not be shown to the user except under special
364 * circumstances, such as detailed notification logs.
365 */
366 public static final int PRIORITY_MIN = -2;
367
368 /**
369 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
370 * show these items larger, or at a different position in notification lists, compared with
371 * your app's {@link #PRIORITY_DEFAULT} items.
372 */
373 public static final int PRIORITY_HIGH = 1;
374
375 /**
376 * Highest {@link #priority}, for your application's most important items that require the
377 * user's prompt attention or input.
378 */
379 public static final int PRIORITY_MAX = 2;
380
381 /**
382 * Relative priority for this notification.
383 *
384 * Priority is an indication of how much of the user's valuable attention should be consumed by
385 * this notification. Low-priority notifications may be hidden from the user in certain
386 * situations, while the user might be interrupted for a higher-priority notification. The
387 * system will make a determination about how to interpret notification priority as described in
388 * MUMBLE MUMBLE.
389 */
390 public int priority;
391
392 /**
393 * Notification type: incoming call (voice or video) or similar synchronous communication request.
394 */
395 public static final String KIND_CALL = "android.call";
396
397 /**
398 * Notification type: incoming direct message (SMS, instant message, etc.).
399 */
400 public static final String KIND_MESSAGE = "android.message";
401
402 /**
403 * Notification type: asynchronous bulk message (email).
404 */
405 public static final String KIND_EMAIL = "android.email";
406
407 /**
408 * Notification type: calendar event.
409 */
410 public static final String KIND_EVENT = "android.event";
411
412 /**
413 * Notification type: promotion or advertisement.
414 */
415 public static final String KIND_PROMO = "android.promo";
416
417 /**
418 * If this notification matches of one or more special types (see the <code>KIND_*</code>
419 * constants), add them here, best match first.
420 */
421 public String[] kind;
422
423 /**
424 * Extra key for people values (type TBD).
425 *
426 * @hide
427 */
428 public static final String EXTRA_PEOPLE = "android.people";
429
430 private Bundle extras;
431
432 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400433 * Structure to encapsulate an "action", including title and icon, that can be attached to a Notification.
434 * @hide
435 */
436 private static class Action implements Parcelable {
437 public int icon;
438 public CharSequence title;
439 public PendingIntent actionIntent;
440 @SuppressWarnings("unused")
441 public Action() { }
442 private Action(Parcel in) {
443 icon = in.readInt();
444 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
445 if (in.readInt() == 1) {
446 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
447 }
448 }
449 public Action(int icon_, CharSequence title_, PendingIntent intent_) {
450 this.icon = icon_;
451 this.title = title_;
452 this.actionIntent = intent_;
453 }
454 @Override
455 public Action clone() {
456 return new Action(
457 this.icon,
458 this.title.toString(),
459 this.actionIntent // safe to alias
460 );
461 }
462 @Override
463 public int describeContents() {
464 return 0;
465 }
466 @Override
467 public void writeToParcel(Parcel out, int flags) {
468 out.writeInt(icon);
469 TextUtils.writeToParcel(title, out, flags);
470 if (actionIntent != null) {
471 out.writeInt(1);
472 actionIntent.writeToParcel(out, flags);
473 } else {
474 out.writeInt(0);
475 }
476 }
477 public static final Parcelable.Creator<Action> CREATOR
478 = new Parcelable.Creator<Action>() {
479 public Action createFromParcel(Parcel in) {
480 return new Action(in);
481 }
482 public Action[] newArray(int size) {
483 return new Action[size];
484 }
485 };
486 }
487
488 private Action[] actions;
489
490 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500491 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800492 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
494 public Notification()
495 {
496 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500497 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499
500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 * @hide
502 */
503 public Notification(Context context, int icon, CharSequence tickerText, long when,
504 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
505 {
506 this.when = when;
507 this.icon = icon;
508 this.tickerText = tickerText;
509 setLatestEventInfo(context, contentTitle, contentText,
510 PendingIntent.getActivity(context, 0, contentIntent, 0));
511 }
512
513 /**
514 * Constructs a Notification object with the information needed to
515 * have a status bar icon without the standard expanded view.
516 *
517 * @param icon The resource id of the icon to put in the status bar.
518 * @param tickerText The text that flows by in the status bar when the notification first
519 * activates.
520 * @param when The time to show in the time field. In the System.currentTimeMillis
521 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800522 *
523 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800525 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 public Notification(int icon, CharSequence tickerText, long when)
527 {
528 this.icon = icon;
529 this.tickerText = tickerText;
530 this.when = when;
531 }
532
533 /**
534 * Unflatten the notification from a parcel.
535 */
536 public Notification(Parcel parcel)
537 {
538 int version = parcel.readInt();
539
540 when = parcel.readLong();
541 icon = parcel.readInt();
542 number = parcel.readInt();
543 if (parcel.readInt() != 0) {
544 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
545 }
546 if (parcel.readInt() != 0) {
547 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
548 }
549 if (parcel.readInt() != 0) {
550 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
551 }
552 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800553 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400554 }
555 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
557 }
Joe Onorato561d3852010-11-20 18:09:34 -0800558 if (parcel.readInt() != 0) {
559 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 defaults = parcel.readInt();
562 flags = parcel.readInt();
563 if (parcel.readInt() != 0) {
564 sound = Uri.CREATOR.createFromParcel(parcel);
565 }
566
567 audioStreamType = parcel.readInt();
568 vibrate = parcel.createLongArray();
569 ledARGB = parcel.readInt();
570 ledOnMS = parcel.readInt();
571 ledOffMS = parcel.readInt();
572 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400573
574 if (parcel.readInt() != 0) {
575 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
576 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577
578 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400579
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 kind = parcel.createStringArray(); // may set kind to null
581
582 if (parcel.readInt() != 0) {
583 extras = parcel.readBundle();
584 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400585
586 actions = parcel.createTypedArray(Action.CREATOR);
587 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400588 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
591
Andy Stadler110988c2010-12-03 14:29:16 -0800592 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700593 public Notification clone() {
594 Notification that = new Notification();
595
596 that.when = this.when;
597 that.icon = this.icon;
598 that.number = this.number;
599
600 // PendingIntents are global, so there's no reason (or way) to clone them.
601 that.contentIntent = this.contentIntent;
602 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400603 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700604
605 if (this.tickerText != null) {
606 that.tickerText = this.tickerText.toString();
607 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800608 if (this.tickerView != null) {
609 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400610 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700611 if (this.contentView != null) {
612 that.contentView = this.contentView.clone();
613 }
Joe Onorato561d3852010-11-20 18:09:34 -0800614 if (this.largeIcon != null) {
615 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
616 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100617 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700618 that.sound = this.sound; // android.net.Uri is immutable
619 that.audioStreamType = this.audioStreamType;
620
621 final long[] vibrate = this.vibrate;
622 if (vibrate != null) {
623 final int N = vibrate.length;
624 final long[] vib = that.vibrate = new long[N];
625 System.arraycopy(vibrate, 0, vib, 0, N);
626 }
627
628 that.ledARGB = this.ledARGB;
629 that.ledOnMS = this.ledOnMS;
630 that.ledOffMS = this.ledOffMS;
631 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632
Joe Onorato18e69df2010-05-17 22:26:12 -0700633 that.flags = this.flags;
634
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 that.priority = this.priority;
636
637 final String[] thiskind = this.kind;
638 if (thiskind != null) {
639 final int N = thiskind.length;
640 final String[] thatkind = that.kind = new String[N];
641 System.arraycopy(thiskind, 0, thatkind, 0, N);
642 }
643
644 if (this.extras != null) {
645 that.extras = new Bundle(this.extras);
646
647 }
648
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400649 that.actions = new Action[this.actions.length];
650 for(int i=0; i<this.actions.length; i++) {
651 that.actions[i] = this.actions[i].clone();
652 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400653 if (this.bigContentView != null) {
654 that.bigContentView = this.bigContentView.clone();
655 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400656
Joe Onorato18e69df2010-05-17 22:26:12 -0700657 return that;
658 }
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 public int describeContents() {
661 return 0;
662 }
663
664 /**
665 * Flatten this notification from a parcel.
666 */
667 public void writeToParcel(Parcel parcel, int flags)
668 {
669 parcel.writeInt(1);
670
671 parcel.writeLong(when);
672 parcel.writeInt(icon);
673 parcel.writeInt(number);
674 if (contentIntent != null) {
675 parcel.writeInt(1);
676 contentIntent.writeToParcel(parcel, 0);
677 } else {
678 parcel.writeInt(0);
679 }
680 if (deleteIntent != null) {
681 parcel.writeInt(1);
682 deleteIntent.writeToParcel(parcel, 0);
683 } else {
684 parcel.writeInt(0);
685 }
686 if (tickerText != null) {
687 parcel.writeInt(1);
688 TextUtils.writeToParcel(tickerText, parcel, flags);
689 } else {
690 parcel.writeInt(0);
691 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800692 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400693 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800694 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400695 } else {
696 parcel.writeInt(0);
697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (contentView != null) {
699 parcel.writeInt(1);
700 contentView.writeToParcel(parcel, 0);
701 } else {
702 parcel.writeInt(0);
703 }
Joe Onorato561d3852010-11-20 18:09:34 -0800704 if (largeIcon != null) {
705 parcel.writeInt(1);
706 largeIcon.writeToParcel(parcel, 0);
707 } else {
708 parcel.writeInt(0);
709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710
711 parcel.writeInt(defaults);
712 parcel.writeInt(this.flags);
713
714 if (sound != null) {
715 parcel.writeInt(1);
716 sound.writeToParcel(parcel, 0);
717 } else {
718 parcel.writeInt(0);
719 }
720 parcel.writeInt(audioStreamType);
721 parcel.writeLongArray(vibrate);
722 parcel.writeInt(ledARGB);
723 parcel.writeInt(ledOnMS);
724 parcel.writeInt(ledOffMS);
725 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400726
727 if (fullScreenIntent != null) {
728 parcel.writeInt(1);
729 fullScreenIntent.writeToParcel(parcel, 0);
730 } else {
731 parcel.writeInt(0);
732 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733
734 parcel.writeInt(priority);
735
736 parcel.writeStringArray(kind); // ok for null
737
738 if (extras != null) {
739 parcel.writeInt(1);
740 extras.writeToParcel(parcel, 0);
741 } else {
742 parcel.writeInt(0);
743 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400744
745 parcel.writeTypedArray(actions, 0);
746
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400747 if (bigContentView != null) {
748 parcel.writeInt(1);
749 bigContentView.writeToParcel(parcel, 0);
750 } else {
751 parcel.writeInt(0);
752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
754
755 /**
756 * Parcelable.Creator that instantiates Notification objects
757 */
758 public static final Parcelable.Creator<Notification> CREATOR
759 = new Parcelable.Creator<Notification>()
760 {
761 public Notification createFromParcel(Parcel parcel)
762 {
763 return new Notification(parcel);
764 }
765
766 public Notification[] newArray(int size)
767 {
768 return new Notification[size];
769 }
770 };
771
772 /**
773 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
774 * layout.
775 *
776 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
777 * in the view.</p>
778 * @param context The context for your application / activity.
779 * @param contentTitle The title that goes in the expanded entry.
780 * @param contentText The text that goes in the expanded entry.
781 * @param contentIntent The intent to launch when the user clicks the expanded notification.
782 * If this is an activity, it must include the
783 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800784 * that you take care of task management as described in the
785 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
786 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800788 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800790 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 public void setLatestEventInfo(Context context,
792 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlercde8aae2012-04-04 23:40:22 -0400793 // TODO: rewrite this to use Builder
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 RemoteViews contentView = new RemoteViews(context.getPackageName(),
Daniel Sandler96fd7c12012-03-30 16:37:36 -0400795 R.layout.notification_template_base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 if (this.icon != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800797 contentView.setImageViewResource(R.id.icon, this.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799 if (contentTitle != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800800 contentView.setTextViewText(R.id.title, contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802 if (contentText != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800803 contentView.setTextViewText(R.id.text, contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805 if (this.when != 0) {
Daniel Sandlercde8aae2012-04-04 23:40:22 -0400806 contentView.setViewVisibility(R.id.time, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -0800807 contentView.setLong(R.id.time, "setTime", when);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810 this.contentView = contentView;
811 this.contentIntent = contentIntent;
812 }
813
814 @Override
815 public String toString() {
816 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500817 sb.append("Notification(pri=");
818 sb.append(priority);
819 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -0800820 if (contentView != null) {
821 sb.append(contentView.getPackage());
822 sb.append("/0x");
823 sb.append(Integer.toHexString(contentView.getLayoutId()));
824 } else {
825 sb.append("null");
826 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500827 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -0800828 sb.append(" vibrate=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 if (this.vibrate != null) {
830 int N = this.vibrate.length-1;
831 sb.append("[");
832 for (int i=0; i<N; i++) {
833 sb.append(this.vibrate[i]);
834 sb.append(',');
835 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200836 if (N != -1) {
837 sb.append(this.vibrate[N]);
838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 sb.append("]");
840 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) {
841 sb.append("default");
842 } else {
843 sb.append("null");
844 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500845 sb.append(" sound=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 if (this.sound != null) {
847 sb.append(this.sound.toString());
848 } else if ((this.defaults & DEFAULT_SOUND) != 0) {
849 sb.append("default");
850 } else {
851 sb.append("null");
852 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500853 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500855 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400856 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500857 sb.append(" kind=[");
858 if (this.kind == null) {
859 sb.append("null");
860 } else {
861 for (int i=0; i<this.kind.length; i++) {
862 if (i>0) sb.append(",");
863 sb.append(this.kind[i]);
864 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400865 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400866 sb.append("]");
867 if (actions != null) {
868 sb.append(" ");
869 sb.append(actions.length);
870 sb.append(" action");
871 if (actions.length > 1) sb.append("s");
872 }
873 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 return sb.toString();
875 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800876
Joe Onoratocb109a02011-01-18 17:57:41 -0800877 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500878 * Builder class for {@link Notification} objects.
879 *
880 * Provides a convenient way to set the various fields of a {@link Notification} and generate
881 * content views using the platform's notification layout template.
882 *
883 * Example:
884 *
885 * <pre class="prettyprint">
886 * Notification noti = new Notification.Builder()
887 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
888 * .setContentText(subject)
889 * .setSmallIcon(R.drawable.new_mail)
890 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400891 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500892 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -0800893 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800894 public static class Builder {
895 private Context mContext;
896
897 private long mWhen;
898 private int mSmallIcon;
899 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800900 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800901 private CharSequence mContentTitle;
902 private CharSequence mContentText;
903 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400904 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -0800905 private PendingIntent mContentIntent;
906 private RemoteViews mContentView;
907 private PendingIntent mDeleteIntent;
908 private PendingIntent mFullScreenIntent;
909 private CharSequence mTickerText;
910 private RemoteViews mTickerView;
911 private Bitmap mLargeIcon;
912 private Uri mSound;
913 private int mAudioStreamType;
914 private long[] mVibrate;
915 private int mLedArgb;
916 private int mLedOnMs;
917 private int mLedOffMs;
918 private int mDefaults;
919 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -0700920 private int mProgressMax;
921 private int mProgress;
922 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500923 private ArrayList<String> mKindList = new ArrayList<String>(1);
924 private Bundle mExtras;
925 private int mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400926 private ArrayList<Action> mActions = new ArrayList<Action>(3);
Daniel Sandlera2985ed2012-04-03 16:42:00 -0400927 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400928 private Style mStyle;
Joe Onorato46439ce2010-11-19 13:56:21 -0800929
Joe Onoratocb109a02011-01-18 17:57:41 -0800930 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500931 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -0800932 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500933
934 * <table>
935 * <tr><th align=right>priority</th>
936 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
937 * <tr><th align=right>when</th>
938 * <td>now ({@link System#currentTimeMillis()})</td></tr>
939 * <tr><th align=right>audio stream</th>
940 * <td>{@link #STREAM_DEFAULT}</td></tr>
941 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -0800942 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500943
944 * @param context
945 * A {@link Context} that will be used by the Builder to construct the
946 * RemoteViews. The Context will not be held past the lifetime of this Builder
947 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -0800948 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800949 public Builder(Context context) {
950 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -0800951
952 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -0800953 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -0800954 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500955 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -0800956 }
957
Joe Onoratocb109a02011-01-18 17:57:41 -0800958 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500959 * Add a timestamp pertaining to the notification (usually the time the event occurred).
960 *
961
962 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -0800963 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800964 public Builder setWhen(long when) {
965 mWhen = when;
966 return this;
967 }
968
Joe Onoratocb109a02011-01-18 17:57:41 -0800969 /**
Daniel Sandlera2985ed2012-04-03 16:42:00 -0400970 * Show the {@link Notification#when} field as a countdown (or count-up) timer instead of a timestamp.
971 *
972 * @see Notification#when
973 */
974 public Builder setUsesChronometer(boolean b) {
975 mUseChronometer = b;
976 return this;
977 }
978
979 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500980 * Set the small icon resource, which will be used to represent the notification in the
981 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800982 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500983
984 * The platform template for the expanded view will draw this icon in the left, unless a
985 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
986 * icon will be moved to the right-hand side.
987 *
988
989 * @param icon
990 * A resource ID in the application's package of the drawable to use.
991 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -0800992 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800993 public Builder setSmallIcon(int icon) {
994 mSmallIcon = icon;
995 return this;
996 }
997
Joe Onoratocb109a02011-01-18 17:57:41 -0800998 /**
999 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1000 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1001 * LevelListDrawable}.
1002 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001003 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001004 * @param level The level to use for the icon.
1005 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001006 * @see Notification#icon
1007 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001008 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001009 public Builder setSmallIcon(int icon, int level) {
1010 mSmallIcon = icon;
1011 mSmallIconLevel = level;
1012 return this;
1013 }
1014
Joe Onoratocb109a02011-01-18 17:57:41 -08001015 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001016 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001017 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001018 public Builder setContentTitle(CharSequence title) {
1019 mContentTitle = title;
1020 return this;
1021 }
1022
Joe Onoratocb109a02011-01-18 17:57:41 -08001023 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001024 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001025 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001026 public Builder setContentText(CharSequence text) {
1027 mContentText = text;
1028 return this;
1029 }
1030
Joe Onoratocb109a02011-01-18 17:57:41 -08001031 /**
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001032 * Set the third line of text in the platform notification template.
1033 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the same location in the standard template.
1034 */
1035 public Builder setSubText(CharSequence text) {
1036 mSubText = text;
1037 return this;
1038 }
1039
1040 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001041 * Set the large number at the right-hand side of the notification. This is
1042 * equivalent to setContentInfo, although it might show the number in a different
1043 * font size for readability.
1044 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001045 public Builder setNumber(int number) {
1046 mNumber = number;
1047 return this;
1048 }
1049
Joe Onoratocb109a02011-01-18 17:57:41 -08001050 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001051 * A small piece of additional information pertaining to this notification.
1052 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001053 * The platform template will draw this on the last line of the notification, at the far
1054 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001055 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001056 public Builder setContentInfo(CharSequence info) {
1057 mContentInfo = info;
1058 return this;
1059 }
1060
Joe Onoratocb109a02011-01-18 17:57:41 -08001061 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001062 * Set the progress this notification represents.
1063 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001064 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001065 */
1066 public Builder setProgress(int max, int progress, boolean indeterminate) {
1067 mProgressMax = max;
1068 mProgress = progress;
1069 mProgressIndeterminate = indeterminate;
1070 return this;
1071 }
1072
1073 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001074 * Supply a custom RemoteViews to use instead of the platform template.
1075 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001076 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001077 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001078 public Builder setContent(RemoteViews views) {
1079 mContentView = views;
1080 return this;
1081 }
1082
Joe Onoratocb109a02011-01-18 17:57:41 -08001083 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001084 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1085 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001086 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1087 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1088 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001089 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001090 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001091 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001092 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001093 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001094 public Builder setContentIntent(PendingIntent intent) {
1095 mContentIntent = intent;
1096 return this;
1097 }
1098
Joe Onoratocb109a02011-01-18 17:57:41 -08001099 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001100 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1101 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001102 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001103 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001104 public Builder setDeleteIntent(PendingIntent intent) {
1105 mDeleteIntent = intent;
1106 return this;
1107 }
1108
Joe Onoratocb109a02011-01-18 17:57:41 -08001109 /**
1110 * An intent to launch instead of posting the notification to the status bar.
1111 * Only for use with extremely high-priority notifications demanding the user's
1112 * <strong>immediate</strong> attention, such as an incoming phone call or
1113 * alarm clock that the user has explicitly set to a particular time.
1114 * If this facility is used for something else, please give the user an option
1115 * to turn it off and use a normal notification, as this can be extremely
1116 * disruptive.
1117 *
1118 * @param intent The pending intent to launch.
1119 * @param highPriority Passing true will cause this notification to be sent
1120 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001121 *
1122 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001123 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001124 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1125 mFullScreenIntent = intent;
1126 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1127 return this;
1128 }
1129
Joe Onoratocb109a02011-01-18 17:57:41 -08001130 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001131 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001132 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001133 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001134 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001135 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001136 public Builder setTicker(CharSequence tickerText) {
1137 mTickerText = tickerText;
1138 return this;
1139 }
1140
Joe Onoratocb109a02011-01-18 17:57:41 -08001141 /**
1142 * Set the text that is displayed in the status bar when the notification first
1143 * arrives, and also a RemoteViews object that may be displayed instead on some
1144 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001145 *
1146 * @see Notification#tickerText
1147 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001148 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001149 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
1150 mTickerText = tickerText;
1151 mTickerView = views;
1152 return this;
1153 }
1154
Joe Onoratocb109a02011-01-18 17:57:41 -08001155 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001156 * Add a large icon to the notification (and the ticker on some devices).
1157 *
1158 * In the platform template, this image will be shown on the left of the notification view
1159 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1160 *
1161 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001162 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001163 public Builder setLargeIcon(Bitmap icon) {
1164 mLargeIcon = icon;
1165 return this;
1166 }
1167
Joe Onoratocb109a02011-01-18 17:57:41 -08001168 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001169 * Set the sound to play.
1170 *
1171 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1172 *
1173 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001174 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001175 public Builder setSound(Uri sound) {
1176 mSound = sound;
1177 mAudioStreamType = STREAM_DEFAULT;
1178 return this;
1179 }
1180
Joe Onoratocb109a02011-01-18 17:57:41 -08001181 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001182 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001183 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001184 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1185 *
1186 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001187 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001188 public Builder setSound(Uri sound, int streamType) {
1189 mSound = sound;
1190 mAudioStreamType = streamType;
1191 return this;
1192 }
1193
Joe Onoratocb109a02011-01-18 17:57:41 -08001194 /**
1195 * Set the vibration pattern to use.
1196 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001197
1198 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1199 * <code>pattern</code> parameter.
1200 *
1201
1202 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001203 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001204 public Builder setVibrate(long[] pattern) {
1205 mVibrate = pattern;
1206 return this;
1207 }
1208
Joe Onoratocb109a02011-01-18 17:57:41 -08001209 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001210 * Set the desired color for the indicator LED on the device, as well as the
1211 * blink duty cycle (specified in milliseconds).
1212 *
1213
1214 * Not all devices will honor all (or even any) of these values.
1215 *
1216
1217 * @see Notification#ledARGB
1218 * @see Notification#ledOnMS
1219 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001220 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001221 public Builder setLights(int argb, int onMs, int offMs) {
1222 mLedArgb = argb;
1223 mLedOnMs = onMs;
1224 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001225 return this;
1226 }
1227
Joe Onoratocb109a02011-01-18 17:57:41 -08001228 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001229 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001230 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001231
1232 * Ongoing notifications cannot be dismissed by the user, so your application or service
1233 * must take care of canceling them.
1234 *
1235
1236 * They are typically used to indicate a background task that the user is actively engaged
1237 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1238 * (e.g., a file download, sync operation, active network connection).
1239 *
1240
1241 * @see Notification#FLAG_ONGOING_EVENT
1242 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001243 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001244 public Builder setOngoing(boolean ongoing) {
1245 setFlag(FLAG_ONGOING_EVENT, ongoing);
1246 return this;
1247 }
1248
Joe Onoratocb109a02011-01-18 17:57:41 -08001249 /**
1250 * Set this flag if you would only like the sound, vibrate
1251 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001252 *
1253 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001254 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001255 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1256 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1257 return this;
1258 }
1259
Joe Onoratocb109a02011-01-18 17:57:41 -08001260 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001261 * Make this notification automatically dismissed when the user touches it. The
1262 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1263 *
1264 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001265 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001266 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001267 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001268 return this;
1269 }
1270
Joe Onoratocb109a02011-01-18 17:57:41 -08001271 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001272 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001273 * <p>
1274 * The value should be one or more of the following fields combined with
1275 * bitwise-or:
1276 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1277 * <p>
1278 * For all default values, use {@link #DEFAULT_ALL}.
1279 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001280 public Builder setDefaults(int defaults) {
1281 mDefaults = defaults;
1282 return this;
1283 }
1284
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001285 /**
1286 * Set the priority of this notification.
1287 *
1288 * @see Notification#priority
1289 */
1290 public Builder setPriority(int pri) {
1291 mPriority = pri;
1292 return this;
1293 }
1294
1295 /**
1296 * Add a kind (category) to this notification. Optional.
1297 *
1298 * @see Notification#kind
1299 */
1300 public Builder addKind(String k) {
1301 mKindList.add(k);
1302 return this;
1303 }
1304
1305 /**
1306 * Add metadata to this notification.
1307 *
1308 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001309 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001310 * called.
1311 *
1312 * @see Notification#extras
1313 * @hide
1314 */
1315 public Builder setExtras(Bundle bag) {
1316 mExtras = bag;
1317 return this;
1318 }
1319
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001320 /**
1321 * Add an action to this notification. Actions are typically displayed by
1322 * the system as a button adjacent to the notification content.
1323 *
1324 * @param icon Resource ID of a drawable that represents the action.
1325 * @param title Text describing the action.
1326 * @param intent PendingIntent to be fired when the action is invoked.
1327 */
1328 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
1329 mActions.add(new Action(icon, title, intent));
1330 return this;
1331 }
1332
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001333 /**
1334 * Add a rich notification style to be applied at build time.
1335 *
1336 * @param style Object responsible for modifying the notification style.
1337 */
1338 public Builder setStyle(Style style) {
1339 if (mStyle != style) {
1340 mStyle = style;
1341 mStyle.setBuilder(this);
1342 }
1343 return this;
1344 }
1345
Joe Onorato46439ce2010-11-19 13:56:21 -08001346 private void setFlag(int mask, boolean value) {
1347 if (value) {
1348 mFlags |= mask;
1349 } else {
1350 mFlags &= ~mask;
1351 }
1352 }
1353
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001354 private RemoteViews applyStandardTemplate(int resId) {
Joe Onorato561d3852010-11-20 18:09:34 -08001355 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001356 boolean hasLine3 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001357 boolean hasLine2 = false;
1358 int smallIconImageViewId = R.id.icon;
1359 if (mLargeIcon != null) {
1360 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1361 smallIconImageViewId = R.id.right_icon;
1362 }
Joe Onorato561d3852010-11-20 18:09:34 -08001363 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001364 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1365 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001366 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001367 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001368 }
1369 if (mContentTitle != null) {
1370 contentView.setTextViewText(R.id.title, mContentTitle);
1371 }
1372 if (mContentText != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001373 contentView.setTextViewText(
1374 (mSubText != null) ? R.id.text2 : R.id.text,
1375 mContentText);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001376 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001377 }
1378 if (mContentInfo != null) {
1379 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001380 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1381 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001382 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001383 final int tooBig = mContext.getResources().getInteger(
1384 R.integer.status_bar_notification_info_maxnum);
1385 if (mNumber > tooBig) {
1386 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1387 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001388 } else {
1389 NumberFormat f = NumberFormat.getIntegerInstance();
1390 contentView.setTextViewText(R.id.info, f.format(mNumber));
1391 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001392 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1393 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001394 } else {
1395 contentView.setViewVisibility(R.id.info, View.GONE);
1396 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001397
1398 if (mSubText != null) {
1399 contentView.setTextViewText(R.id.text, mSubText);
1400 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001401 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001402 contentView.setViewVisibility(R.id.text2, View.GONE);
1403 if (mProgressMax != 0 || mProgressIndeterminate) {
1404 contentView.setProgressBar(
1405 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1406 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
1407 } else {
1408 contentView.setViewVisibility(R.id.progress, View.GONE);
1409 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001410 }
Joe Onorato561d3852010-11-20 18:09:34 -08001411 if (mWhen != 0) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001412 if (mUseChronometer) {
1413 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1414 contentView.setLong(R.id.chronometer, "setBase",
1415 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1416 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1417 } else {
1418 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1419 contentView.setLong(R.id.time, "setTime", mWhen);
1420 }
Joe Onorato561d3852010-11-20 18:09:34 -08001421 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001422 contentView.setViewVisibility(R.id.line3, hasLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001423 return contentView;
1424 }
1425
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001426 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
1427 RemoteViews big = applyStandardTemplate(layoutId);
1428
1429 int N = mActions.size();
1430 if (N > 0) {
1431 Log.d("Notification", "has actions: " + mContentText);
1432 big.setViewVisibility(R.id.actions, View.VISIBLE);
1433 if (N>3) N=3;
1434 for (int i=0; i<N; i++) {
1435 final RemoteViews button = generateActionButton(mActions.get(i));
1436 Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
1437 big.addView(R.id.actions, button);
1438 }
1439 }
1440 return big;
1441 }
1442
Joe Onorato46439ce2010-11-19 13:56:21 -08001443 private RemoteViews makeContentView() {
1444 if (mContentView != null) {
1445 return mContentView;
1446 } else {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001447 return applyStandardTemplate(R.layout.notification_template_base); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001448 }
1449 }
1450
1451 private RemoteViews makeTickerView() {
1452 if (mTickerView != null) {
1453 return mTickerView;
1454 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001455 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001456 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001457 ? R.layout.status_bar_latest_event_ticker
1458 : R.layout.status_bar_latest_event_ticker_large_icon);
1459 } else {
1460 return null;
1461 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001462 }
1463 }
1464
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001465 private RemoteViews makeBigContentView() {
1466 if (mActions.size() == 0) return null;
1467
Chris Wrenb023bf82012-04-23 16:05:42 -04001468 return applyStandardTemplateWithActions(R.layout.notification_template_big_base);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001469 }
1470
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001471 private RemoteViews generateActionButton(Action action) {
1472 RemoteViews button = new RemoteViews(mContext.getPackageName(), R.layout.notification_action);
1473 button.setTextViewCompoundDrawables(R.id.action0, action.icon, 0, 0, 0);
1474 button.setTextViewText(R.id.action0, action.title);
1475 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
1476 button.setContentDescription(R.id.action0, action.title);
1477 return button;
1478 }
1479
Joe Onoratocb109a02011-01-18 17:57:41 -08001480 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001481 * Apply the unstyled operations and return a new {@link Notification} object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001482 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001483 private Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08001484 Notification n = new Notification();
1485 n.when = mWhen;
1486 n.icon = mSmallIcon;
1487 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001488 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001489 n.contentView = makeContentView();
1490 n.contentIntent = mContentIntent;
1491 n.deleteIntent = mDeleteIntent;
1492 n.fullScreenIntent = mFullScreenIntent;
1493 n.tickerText = mTickerText;
1494 n.tickerView = makeTickerView();
1495 n.largeIcon = mLargeIcon;
1496 n.sound = mSound;
1497 n.audioStreamType = mAudioStreamType;
1498 n.vibrate = mVibrate;
1499 n.ledARGB = mLedArgb;
1500 n.ledOnMS = mLedOnMs;
1501 n.ledOffMS = mLedOffMs;
1502 n.defaults = mDefaults;
1503 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001504 n.bigContentView = makeBigContentView();
Joe Onorato8d0b6552010-11-22 16:09:29 -08001505 if (mLedOnMs != 0 && mLedOffMs != 0) {
1506 n.flags |= FLAG_SHOW_LIGHTS;
1507 }
1508 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1509 n.flags |= FLAG_SHOW_LIGHTS;
1510 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001511 if (mKindList.size() > 0) {
1512 n.kind = new String[mKindList.size()];
1513 mKindList.toArray(n.kind);
1514 } else {
1515 n.kind = null;
1516 }
1517 n.priority = mPriority;
1518 n.extras = mExtras != null ? new Bundle(mExtras) : null;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001519 if (mActions.size() > 0) {
1520 n.actions = new Action[mActions.size()];
1521 mActions.toArray(n.actions);
1522 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001523 return n;
1524 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001525
1526 /**
1527 * @deprecated Use {@link #build()} instead.
1528 */
1529 @Deprecated
1530 public Notification getNotification() {
1531 return build();
1532 }
1533
1534 /**
1535 * Combine all of the options that have been set and return a new {@link Notification}
1536 * object.
1537 */
1538 public Notification build() {
1539 if (mStyle != null) {
1540 return mStyle.build();
1541 } else {
1542 return buildUnstyled();
1543 }
1544 }
1545 }
1546
1547
1548 /**
1549 * An object that can apply a rich notification style to a {@link Notification.Builder}
1550 * object.
1551 */
1552 public static class Style {
1553 protected Builder mBuilder;
1554
1555 public void setBuilder(Builder builder) {
1556 if (mBuilder != builder) {
1557 mBuilder = builder;
1558 mBuilder.setStyle(this);
1559 }
1560 }
1561
1562 public Notification build() {
1563 if (mBuilder == null) {
1564 throw new IllegalArgumentException("Style requires a valid Builder object");
1565 }
1566 return mBuilder.buildUnstyled();
1567 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001568 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001569
1570 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001571 * Helper class for generating large-format notifications that include a large image attachment.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001572 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001573 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001574 * <pre class="prettyprint">
1575 * Notification noti = new Notification.BigPictureStyle(
1576 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001577 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001578 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001579 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001580 * .setLargeIcon(aBitmap))
1581 * .bigPicture(aBigBitmap)
1582 * .build();
1583 * </pre>
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001584 *
1585 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001586 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001587 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001588 private Bitmap mPicture;
1589
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001590 public BigPictureStyle() {
1591 }
1592
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001593 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001594 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001595 }
1596
1597 public BigPictureStyle bigPicture(Bitmap b) {
1598 mPicture = b;
1599 return this;
1600 }
1601
1602 private RemoteViews makeBigContentView() {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001603 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(R.layout.notification_template_big_picture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001604
1605 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
1606
1607 return contentView;
1608 }
1609
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001610 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001611 public Notification build() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001612 if (mBuilder == null) {
1613 throw new IllegalArgumentException("Style requires a valid Builder object");
1614 }
1615 Notification wip = mBuilder.buildUnstyled();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001616 wip.bigContentView = makeBigContentView();
1617 return wip;
1618 }
1619 }
1620
1621 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001622 * Helper class for generating large-format notifications that include a lot of text.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001623 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001624 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001625 * <pre class="prettyprint">
1626 * Notification noti = new Notification.BigPictureStyle(
1627 * new Notification.Builder()
1628 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1629 * .setContentText(subject)
1630 * .setSmallIcon(R.drawable.new_mail)
1631 * .setLargeIcon(aBitmap))
1632 * .bigText(aVeryLongString)
1633 * .build();
1634 * </pre>
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001635 *
1636 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001637 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001638 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001639 private CharSequence mBigText;
1640
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001641 public BigTextStyle() {
1642 }
1643
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001644 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001645 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001646 }
1647
1648 public BigTextStyle bigText(CharSequence cs) {
1649 mBigText = cs;
1650 return this;
1651 }
1652
1653 private RemoteViews makeBigContentView() {
Daniel Sandler6a858c32012-03-12 14:38:58 -04001654 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(R.layout.notification_template_big_text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001655
1656 contentView.setTextViewText(R.id.big_text, mBigText);
1657 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
1658 contentView.setTextViewText(R.id.text, ""); // XXX: what do do with this spot?
1659
1660 return contentView;
1661 }
1662
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001663 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001664 public Notification build() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001665 if (mBuilder == null) {
1666 throw new IllegalArgumentException("Style requires a valid Builder object");
1667 }
1668 mBuilder.mSubText = null;
1669 Notification wip = mBuilder.buildUnstyled();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001670 wip.bigContentView = makeBigContentView();
1671 return wip;
1672 }
1673 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04001674
1675 /**
1676 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
1677 *
1678 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
1679 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001680 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04001681 * new Notification.Builder()
1682 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1683 * .setContentText(subject)
1684 * .setSmallIcon(R.drawable.new_mail)
1685 * .setLargeIcon(aBitmap))
1686 * .addLine(str1)
1687 * .addLine(str2)
1688 * .build();
1689 * </pre>
1690 *
1691 * @see Notification#bigContentView
1692 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001693 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04001694 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
1695
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001696 public InboxStyle() {
1697 }
1698
Daniel Sandler879c5e02012-04-17 16:46:51 -04001699 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001700 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04001701 }
1702
1703 public InboxStyle addLine(CharSequence cs) {
1704 mTexts.add(cs);
1705 return this;
1706 }
1707
1708 private RemoteViews makeBigContentView() {
1709 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(R.layout.notification_template_inbox);
1710
1711 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3, R.id.inbox_text4};
1712
1713 int i=0;
1714 while (i < mTexts.size() && i < rowIds.length) {
1715 CharSequence str = mTexts.get(i);
1716 if (str != null && !str.equals("")) {
1717 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
1718 contentView.setTextViewText(rowIds[i], str);
1719 }
1720 i++;
1721 }
1722
1723 return contentView;
1724 }
1725
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001726 @Override
Daniel Sandler879c5e02012-04-17 16:46:51 -04001727 public Notification build() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001728 if (mBuilder == null) {
1729 throw new IllegalArgumentException("Style requires a valid Builder object");
1730 }
1731 Notification wip = mBuilder.buildUnstyled();
Daniel Sandler879c5e02012-04-17 16:46:51 -04001732 wip.bigContentView = makeBigContentView();
1733 return wip;
1734 }
1735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736}