blob: 09d24e38f452c568141a7309bed33a378eb61eac [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 }
Daniel Sandlere95658c2012-05-10 00:33:54 -0400799 if (priority < PRIORITY_LOW) {
800 contentView.setInt(R.id.icon,
801 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
802 contentView.setInt(R.id.status_bar_latest_event_content,
803 "setBackgroundResource", R.drawable.notification_bg_low);
804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 if (contentTitle != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800806 contentView.setTextViewText(R.id.title, contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 }
808 if (contentText != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800809 contentView.setTextViewText(R.id.text, contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811 if (this.when != 0) {
Daniel Sandlercde8aae2012-04-04 23:40:22 -0400812 contentView.setViewVisibility(R.id.time, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -0800813 contentView.setLong(R.id.time, "setTime", when);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
Daniel Sandlerb4b22232012-04-30 10:38:31 -0400815 if (this.number != 0) {
816 NumberFormat f = NumberFormat.getIntegerInstance();
817 contentView.setTextViewText(R.id.info, f.format(this.number));
818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
820 this.contentView = contentView;
821 this.contentIntent = contentIntent;
822 }
823
824 @Override
825 public String toString() {
826 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500827 sb.append("Notification(pri=");
828 sb.append(priority);
829 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -0800830 if (contentView != null) {
831 sb.append(contentView.getPackage());
832 sb.append("/0x");
833 sb.append(Integer.toHexString(contentView.getLayoutId()));
834 } else {
835 sb.append("null");
836 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500837 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -0800838 sb.append(" vibrate=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 if (this.vibrate != null) {
840 int N = this.vibrate.length-1;
841 sb.append("[");
842 for (int i=0; i<N; i++) {
843 sb.append(this.vibrate[i]);
844 sb.append(',');
845 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200846 if (N != -1) {
847 sb.append(this.vibrate[N]);
848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 sb.append("]");
850 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) {
851 sb.append("default");
852 } else {
853 sb.append("null");
854 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500855 sb.append(" sound=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 if (this.sound != null) {
857 sb.append(this.sound.toString());
858 } else if ((this.defaults & DEFAULT_SOUND) != 0) {
859 sb.append("default");
860 } else {
861 sb.append("null");
862 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500863 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500865 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400866 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500867 sb.append(" kind=[");
868 if (this.kind == null) {
869 sb.append("null");
870 } else {
871 for (int i=0; i<this.kind.length; i++) {
872 if (i>0) sb.append(",");
873 sb.append(this.kind[i]);
874 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400875 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400876 sb.append("]");
877 if (actions != null) {
878 sb.append(" ");
879 sb.append(actions.length);
880 sb.append(" action");
881 if (actions.length > 1) sb.append("s");
882 }
883 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 return sb.toString();
885 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800886
Joe Onoratocb109a02011-01-18 17:57:41 -0800887 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500888 * Builder class for {@link Notification} objects.
889 *
890 * Provides a convenient way to set the various fields of a {@link Notification} and generate
891 * content views using the platform's notification layout template.
892 *
893 * Example:
894 *
895 * <pre class="prettyprint">
896 * Notification noti = new Notification.Builder()
897 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
898 * .setContentText(subject)
899 * .setSmallIcon(R.drawable.new_mail)
900 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400901 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500902 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -0800903 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800904 public static class Builder {
905 private Context mContext;
906
907 private long mWhen;
908 private int mSmallIcon;
909 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800910 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800911 private CharSequence mContentTitle;
912 private CharSequence mContentText;
913 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400914 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -0800915 private PendingIntent mContentIntent;
916 private RemoteViews mContentView;
917 private PendingIntent mDeleteIntent;
918 private PendingIntent mFullScreenIntent;
919 private CharSequence mTickerText;
920 private RemoteViews mTickerView;
921 private Bitmap mLargeIcon;
922 private Uri mSound;
923 private int mAudioStreamType;
924 private long[] mVibrate;
925 private int mLedArgb;
926 private int mLedOnMs;
927 private int mLedOffMs;
928 private int mDefaults;
929 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -0700930 private int mProgressMax;
931 private int mProgress;
932 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500933 private ArrayList<String> mKindList = new ArrayList<String>(1);
934 private Bundle mExtras;
935 private int mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400936 private ArrayList<Action> mActions = new ArrayList<Action>(3);
Daniel Sandlera2985ed2012-04-03 16:42:00 -0400937 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400938 private Style mStyle;
Joe Onorato46439ce2010-11-19 13:56:21 -0800939
Joe Onoratocb109a02011-01-18 17:57:41 -0800940 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500941 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -0800942 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500943
944 * <table>
945 * <tr><th align=right>priority</th>
946 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
947 * <tr><th align=right>when</th>
948 * <td>now ({@link System#currentTimeMillis()})</td></tr>
949 * <tr><th align=right>audio stream</th>
950 * <td>{@link #STREAM_DEFAULT}</td></tr>
951 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -0800952 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500953
954 * @param context
955 * A {@link Context} that will be used by the Builder to construct the
956 * RemoteViews. The Context will not be held past the lifetime of this Builder
957 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -0800958 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800959 public Builder(Context context) {
960 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -0800961
962 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -0800963 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -0800964 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500965 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -0800966 }
967
Joe Onoratocb109a02011-01-18 17:57:41 -0800968 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500969 * Add a timestamp pertaining to the notification (usually the time the event occurred).
970 *
971
972 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -0800973 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800974 public Builder setWhen(long when) {
975 mWhen = when;
976 return this;
977 }
978
Joe Onoratocb109a02011-01-18 17:57:41 -0800979 /**
Daniel Sandlera2985ed2012-04-03 16:42:00 -0400980 * Show the {@link Notification#when} field as a countdown (or count-up) timer instead of a timestamp.
981 *
982 * @see Notification#when
983 */
984 public Builder setUsesChronometer(boolean b) {
985 mUseChronometer = b;
986 return this;
987 }
988
989 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500990 * Set the small icon resource, which will be used to represent the notification in the
991 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800992 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500993
994 * The platform template for the expanded view will draw this icon in the left, unless a
995 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
996 * icon will be moved to the right-hand side.
997 *
998
999 * @param icon
1000 * A resource ID in the application's package of the drawable to use.
1001 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001002 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001003 public Builder setSmallIcon(int icon) {
1004 mSmallIcon = icon;
1005 return this;
1006 }
1007
Joe Onoratocb109a02011-01-18 17:57:41 -08001008 /**
1009 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1010 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1011 * LevelListDrawable}.
1012 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001013 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001014 * @param level The level to use for the icon.
1015 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001016 * @see Notification#icon
1017 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001018 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001019 public Builder setSmallIcon(int icon, int level) {
1020 mSmallIcon = icon;
1021 mSmallIconLevel = level;
1022 return this;
1023 }
1024
Joe Onoratocb109a02011-01-18 17:57:41 -08001025 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001026 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001027 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001028 public Builder setContentTitle(CharSequence title) {
1029 mContentTitle = title;
1030 return this;
1031 }
1032
Joe Onoratocb109a02011-01-18 17:57:41 -08001033 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001034 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001035 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001036 public Builder setContentText(CharSequence text) {
1037 mContentText = text;
1038 return this;
1039 }
1040
Joe Onoratocb109a02011-01-18 17:57:41 -08001041 /**
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001042 * Set the third line of text in the platform notification template.
1043 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the same location in the standard template.
1044 */
1045 public Builder setSubText(CharSequence text) {
1046 mSubText = text;
1047 return this;
1048 }
1049
1050 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001051 * Set the large number at the right-hand side of the notification. This is
1052 * equivalent to setContentInfo, although it might show the number in a different
1053 * font size for readability.
1054 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001055 public Builder setNumber(int number) {
1056 mNumber = number;
1057 return this;
1058 }
1059
Joe Onoratocb109a02011-01-18 17:57:41 -08001060 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001061 * A small piece of additional information pertaining to this notification.
1062 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001063 * The platform template will draw this on the last line of the notification, at the far
1064 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001065 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001066 public Builder setContentInfo(CharSequence info) {
1067 mContentInfo = info;
1068 return this;
1069 }
1070
Joe Onoratocb109a02011-01-18 17:57:41 -08001071 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001072 * Set the progress this notification represents.
1073 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001074 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001075 */
1076 public Builder setProgress(int max, int progress, boolean indeterminate) {
1077 mProgressMax = max;
1078 mProgress = progress;
1079 mProgressIndeterminate = indeterminate;
1080 return this;
1081 }
1082
1083 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001084 * Supply a custom RemoteViews to use instead of the platform template.
1085 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001086 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001087 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001088 public Builder setContent(RemoteViews views) {
1089 mContentView = views;
1090 return this;
1091 }
1092
Joe Onoratocb109a02011-01-18 17:57:41 -08001093 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001094 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1095 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001096 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1097 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1098 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001099 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001100 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001101 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001102 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001103 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001104 public Builder setContentIntent(PendingIntent intent) {
1105 mContentIntent = intent;
1106 return this;
1107 }
1108
Joe Onoratocb109a02011-01-18 17:57:41 -08001109 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001110 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1111 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001112 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001113 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001114 public Builder setDeleteIntent(PendingIntent intent) {
1115 mDeleteIntent = intent;
1116 return this;
1117 }
1118
Joe Onoratocb109a02011-01-18 17:57:41 -08001119 /**
1120 * An intent to launch instead of posting the notification to the status bar.
1121 * Only for use with extremely high-priority notifications demanding the user's
1122 * <strong>immediate</strong> attention, such as an incoming phone call or
1123 * alarm clock that the user has explicitly set to a particular time.
1124 * If this facility is used for something else, please give the user an option
1125 * to turn it off and use a normal notification, as this can be extremely
1126 * disruptive.
1127 *
1128 * @param intent The pending intent to launch.
1129 * @param highPriority Passing true will cause this notification to be sent
1130 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001131 *
1132 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001133 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001134 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1135 mFullScreenIntent = intent;
1136 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1137 return this;
1138 }
1139
Joe Onoratocb109a02011-01-18 17:57:41 -08001140 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001141 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001142 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001143 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001144 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001145 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001146 public Builder setTicker(CharSequence tickerText) {
1147 mTickerText = tickerText;
1148 return this;
1149 }
1150
Joe Onoratocb109a02011-01-18 17:57:41 -08001151 /**
1152 * Set the text that is displayed in the status bar when the notification first
1153 * arrives, and also a RemoteViews object that may be displayed instead on some
1154 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001155 *
1156 * @see Notification#tickerText
1157 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001158 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001159 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
1160 mTickerText = tickerText;
1161 mTickerView = views;
1162 return this;
1163 }
1164
Joe Onoratocb109a02011-01-18 17:57:41 -08001165 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001166 * Add a large icon to the notification (and the ticker on some devices).
1167 *
1168 * In the platform template, this image will be shown on the left of the notification view
1169 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1170 *
1171 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001172 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001173 public Builder setLargeIcon(Bitmap icon) {
1174 mLargeIcon = icon;
1175 return this;
1176 }
1177
Joe Onoratocb109a02011-01-18 17:57:41 -08001178 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001179 * Set the sound to play.
1180 *
1181 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1182 *
1183 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001184 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001185 public Builder setSound(Uri sound) {
1186 mSound = sound;
1187 mAudioStreamType = STREAM_DEFAULT;
1188 return this;
1189 }
1190
Joe Onoratocb109a02011-01-18 17:57:41 -08001191 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001192 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001193 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001194 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1195 *
1196 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001197 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001198 public Builder setSound(Uri sound, int streamType) {
1199 mSound = sound;
1200 mAudioStreamType = streamType;
1201 return this;
1202 }
1203
Joe Onoratocb109a02011-01-18 17:57:41 -08001204 /**
1205 * Set the vibration pattern to use.
1206 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001207
1208 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1209 * <code>pattern</code> parameter.
1210 *
1211
1212 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001213 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001214 public Builder setVibrate(long[] pattern) {
1215 mVibrate = pattern;
1216 return this;
1217 }
1218
Joe Onoratocb109a02011-01-18 17:57:41 -08001219 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001220 * Set the desired color for the indicator LED on the device, as well as the
1221 * blink duty cycle (specified in milliseconds).
1222 *
1223
1224 * Not all devices will honor all (or even any) of these values.
1225 *
1226
1227 * @see Notification#ledARGB
1228 * @see Notification#ledOnMS
1229 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001230 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001231 public Builder setLights(int argb, int onMs, int offMs) {
1232 mLedArgb = argb;
1233 mLedOnMs = onMs;
1234 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001235 return this;
1236 }
1237
Joe Onoratocb109a02011-01-18 17:57:41 -08001238 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001239 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001240 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001241
1242 * Ongoing notifications cannot be dismissed by the user, so your application or service
1243 * must take care of canceling them.
1244 *
1245
1246 * They are typically used to indicate a background task that the user is actively engaged
1247 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1248 * (e.g., a file download, sync operation, active network connection).
1249 *
1250
1251 * @see Notification#FLAG_ONGOING_EVENT
1252 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001253 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001254 public Builder setOngoing(boolean ongoing) {
1255 setFlag(FLAG_ONGOING_EVENT, ongoing);
1256 return this;
1257 }
1258
Joe Onoratocb109a02011-01-18 17:57:41 -08001259 /**
1260 * Set this flag if you would only like the sound, vibrate
1261 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001262 *
1263 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001264 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001265 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1266 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1267 return this;
1268 }
1269
Joe Onoratocb109a02011-01-18 17:57:41 -08001270 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001271 * Make this notification automatically dismissed when the user touches it. The
1272 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1273 *
1274 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001275 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001276 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001277 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001278 return this;
1279 }
1280
Joe Onoratocb109a02011-01-18 17:57:41 -08001281 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001282 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001283 * <p>
1284 * The value should be one or more of the following fields combined with
1285 * bitwise-or:
1286 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1287 * <p>
1288 * For all default values, use {@link #DEFAULT_ALL}.
1289 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001290 public Builder setDefaults(int defaults) {
1291 mDefaults = defaults;
1292 return this;
1293 }
1294
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001295 /**
1296 * Set the priority of this notification.
1297 *
1298 * @see Notification#priority
1299 */
1300 public Builder setPriority(int pri) {
1301 mPriority = pri;
1302 return this;
1303 }
1304
1305 /**
1306 * Add a kind (category) to this notification. Optional.
1307 *
1308 * @see Notification#kind
1309 */
1310 public Builder addKind(String k) {
1311 mKindList.add(k);
1312 return this;
1313 }
1314
1315 /**
1316 * Add metadata to this notification.
1317 *
1318 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001319 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001320 * called.
1321 *
1322 * @see Notification#extras
1323 * @hide
1324 */
1325 public Builder setExtras(Bundle bag) {
1326 mExtras = bag;
1327 return this;
1328 }
1329
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001330 /**
1331 * Add an action to this notification. Actions are typically displayed by
1332 * the system as a button adjacent to the notification content.
1333 *
1334 * @param icon Resource ID of a drawable that represents the action.
1335 * @param title Text describing the action.
1336 * @param intent PendingIntent to be fired when the action is invoked.
1337 */
1338 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
1339 mActions.add(new Action(icon, title, intent));
1340 return this;
1341 }
1342
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001343 /**
1344 * Add a rich notification style to be applied at build time.
1345 *
1346 * @param style Object responsible for modifying the notification style.
1347 */
1348 public Builder setStyle(Style style) {
1349 if (mStyle != style) {
1350 mStyle = style;
1351 mStyle.setBuilder(this);
1352 }
1353 return this;
1354 }
1355
Joe Onorato46439ce2010-11-19 13:56:21 -08001356 private void setFlag(int mask, boolean value) {
1357 if (value) {
1358 mFlags |= mask;
1359 } else {
1360 mFlags &= ~mask;
1361 }
1362 }
1363
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001364 private RemoteViews applyStandardTemplate(int resId) {
Joe Onorato561d3852010-11-20 18:09:34 -08001365 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001366 boolean hasLine3 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001367 boolean hasLine2 = false;
1368 int smallIconImageViewId = R.id.icon;
1369 if (mLargeIcon != null) {
1370 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1371 smallIconImageViewId = R.id.right_icon;
1372 }
Daniel Sandlere95658c2012-05-10 00:33:54 -04001373 if (mPriority < PRIORITY_LOW) {
1374 contentView.setInt(R.id.icon,
1375 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1376 contentView.setInt(R.id.status_bar_latest_event_content,
1377 "setBackgroundResource", R.drawable.notification_bg_low);
1378 }
Joe Onorato561d3852010-11-20 18:09:34 -08001379 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001380 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1381 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001382 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001383 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001384 }
1385 if (mContentTitle != null) {
1386 contentView.setTextViewText(R.id.title, mContentTitle);
1387 }
1388 if (mContentText != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001389 contentView.setTextViewText(
1390 (mSubText != null) ? R.id.text2 : R.id.text,
1391 mContentText);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001392 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001393 }
1394 if (mContentInfo != null) {
1395 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001396 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1397 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001398 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001399 final int tooBig = mContext.getResources().getInteger(
1400 R.integer.status_bar_notification_info_maxnum);
1401 if (mNumber > tooBig) {
1402 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1403 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001404 } else {
1405 NumberFormat f = NumberFormat.getIntegerInstance();
1406 contentView.setTextViewText(R.id.info, f.format(mNumber));
1407 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001408 contentView.setViewVisibility(R.id.info, View.VISIBLE);
1409 hasLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001410 } else {
1411 contentView.setViewVisibility(R.id.info, View.GONE);
1412 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001413
1414 if (mSubText != null) {
1415 contentView.setTextViewText(R.id.text, mSubText);
Chris Wrend6297db2012-05-03 16:20:13 -04001416 contentView.setViewVisibility(R.id.text2,
1417 mContentText != null ? View.VISIBLE : View.GONE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001418 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001419 contentView.setViewVisibility(R.id.text2, View.GONE);
1420 if (mProgressMax != 0 || mProgressIndeterminate) {
1421 contentView.setProgressBar(
1422 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1423 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
1424 } else {
1425 contentView.setViewVisibility(R.id.progress, View.GONE);
1426 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001427 }
Joe Onorato561d3852010-11-20 18:09:34 -08001428 if (mWhen != 0) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001429 if (mUseChronometer) {
1430 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1431 contentView.setLong(R.id.chronometer, "setBase",
1432 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1433 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1434 } else {
1435 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1436 contentView.setLong(R.id.time, "setTime", mWhen);
1437 }
Joe Onorato561d3852010-11-20 18:09:34 -08001438 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001439 contentView.setViewVisibility(R.id.line3, hasLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001440 return contentView;
1441 }
1442
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001443 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
1444 RemoteViews big = applyStandardTemplate(layoutId);
1445
1446 int N = mActions.size();
1447 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001448 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001449 big.setViewVisibility(R.id.actions, View.VISIBLE);
1450 if (N>3) N=3;
Chris Wren2c22eb02012-05-08 09:49:13 -04001451 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001452 for (int i=0; i<N; i++) {
1453 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001454 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001455 big.addView(R.id.actions, button);
1456 }
1457 }
1458 return big;
1459 }
1460
Joe Onorato46439ce2010-11-19 13:56:21 -08001461 private RemoteViews makeContentView() {
1462 if (mContentView != null) {
1463 return mContentView;
1464 } else {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001465 return applyStandardTemplate(R.layout.notification_template_base); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001466 }
1467 }
1468
1469 private RemoteViews makeTickerView() {
1470 if (mTickerView != null) {
1471 return mTickerView;
1472 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001473 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001474 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001475 ? R.layout.status_bar_latest_event_ticker
1476 : R.layout.status_bar_latest_event_ticker_large_icon);
1477 } else {
1478 return null;
1479 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001480 }
1481 }
1482
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001483 private RemoteViews makeBigContentView() {
1484 if (mActions.size() == 0) return null;
1485
Chris Wrenb023bf82012-04-23 16:05:42 -04001486 return applyStandardTemplateWithActions(R.layout.notification_template_big_base);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001487 }
1488
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001489 private RemoteViews generateActionButton(Action action) {
1490 RemoteViews button = new RemoteViews(mContext.getPackageName(), R.layout.notification_action);
1491 button.setTextViewCompoundDrawables(R.id.action0, action.icon, 0, 0, 0);
1492 button.setTextViewText(R.id.action0, action.title);
Daniel Sandlere5518842012-05-10 16:20:40 -04001493 if (action.actionIntent != null) {
1494 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
1495 //button.setBoolean(R.id.action0, "setEnabled", true);
1496 button.setFloat(R.id.button0, "setAlpha", 1.0f);
1497 button.setBoolean(R.id.button0, "setClickable", true);
1498 } else {
1499 //button.setBoolean(R.id.action0, "setEnabled", false);
1500 button.setFloat(R.id.button0, "setAlpha", 0.5f);
1501 button.setBoolean(R.id.button0, "setClickable", false);
1502 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001503 button.setContentDescription(R.id.action0, action.title);
1504 return button;
1505 }
1506
Joe Onoratocb109a02011-01-18 17:57:41 -08001507 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001508 * Apply the unstyled operations and return a new {@link Notification} object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001509 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001510 private Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08001511 Notification n = new Notification();
1512 n.when = mWhen;
1513 n.icon = mSmallIcon;
1514 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001515 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001516 n.contentView = makeContentView();
1517 n.contentIntent = mContentIntent;
1518 n.deleteIntent = mDeleteIntent;
1519 n.fullScreenIntent = mFullScreenIntent;
1520 n.tickerText = mTickerText;
1521 n.tickerView = makeTickerView();
1522 n.largeIcon = mLargeIcon;
1523 n.sound = mSound;
1524 n.audioStreamType = mAudioStreamType;
1525 n.vibrate = mVibrate;
1526 n.ledARGB = mLedArgb;
1527 n.ledOnMS = mLedOnMs;
1528 n.ledOffMS = mLedOffMs;
1529 n.defaults = mDefaults;
1530 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001531 n.bigContentView = makeBigContentView();
Joe Onorato8d0b6552010-11-22 16:09:29 -08001532 if (mLedOnMs != 0 && mLedOffMs != 0) {
1533 n.flags |= FLAG_SHOW_LIGHTS;
1534 }
1535 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1536 n.flags |= FLAG_SHOW_LIGHTS;
1537 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001538 if (mKindList.size() > 0) {
1539 n.kind = new String[mKindList.size()];
1540 mKindList.toArray(n.kind);
1541 } else {
1542 n.kind = null;
1543 }
1544 n.priority = mPriority;
1545 n.extras = mExtras != null ? new Bundle(mExtras) : null;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001546 if (mActions.size() > 0) {
1547 n.actions = new Action[mActions.size()];
1548 mActions.toArray(n.actions);
1549 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001550 return n;
1551 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001552
1553 /**
1554 * @deprecated Use {@link #build()} instead.
1555 */
1556 @Deprecated
1557 public Notification getNotification() {
1558 return build();
1559 }
1560
1561 /**
1562 * Combine all of the options that have been set and return a new {@link Notification}
1563 * object.
1564 */
1565 public Notification build() {
1566 if (mStyle != null) {
1567 return mStyle.build();
1568 } else {
1569 return buildUnstyled();
1570 }
1571 }
1572 }
1573
1574
1575 /**
1576 * An object that can apply a rich notification style to a {@link Notification.Builder}
1577 * object.
1578 */
Chris Wrend6297db2012-05-03 16:20:13 -04001579 public static abstract class Style
1580 {
1581 private CharSequence mBigContentTitle;
1582 private CharSequence mSummaryText = null;
1583
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001584 protected Builder mBuilder;
1585
Chris Wrend6297db2012-05-03 16:20:13 -04001586 /**
1587 * Overrides ContentTitle in the big form of the template.
1588 * This defaults to the value passed to setContentTitle().
1589 */
1590 protected void internalSetBigContentTitle(CharSequence title) {
1591 mBigContentTitle = title;
1592 }
1593
1594 /**
1595 * Set the first line of text after the detail section in the big form of the template.
1596 */
1597 protected void internalSetSummaryText(CharSequence cs) {
1598 mSummaryText = cs;
1599 }
1600
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001601 public void setBuilder(Builder builder) {
1602 if (mBuilder != builder) {
1603 mBuilder = builder;
1604 mBuilder.setStyle(this);
1605 }
1606 }
1607
Chris Wrend6297db2012-05-03 16:20:13 -04001608 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001609 if (mBuilder == null) {
1610 throw new IllegalArgumentException("Style requires a valid Builder object");
1611 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001612 }
Chris Wrend6297db2012-05-03 16:20:13 -04001613
1614 protected RemoteViews getStandardView(int layoutId) {
1615 checkBuilder();
1616
1617 if (mBigContentTitle != null) {
1618 mBuilder.setContentTitle(mBigContentTitle);
1619 }
1620
1621 if (mBuilder.mSubText == null) {
1622 mBuilder.setContentText(null);
1623 }
1624
1625 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
1626
1627 if (mBuilder.mSubText == null) {
1628 contentView.setViewVisibility(R.id.line3, View.GONE);
1629 }
1630
1631 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
1632 contentView.setViewVisibility(R.id.line1, View.GONE);
1633 }
1634
1635 if (mSummaryText != null && !mSummaryText.equals("")) {
1636 contentView.setViewVisibility(R.id.overflow_title, View.VISIBLE);
1637 contentView.setTextViewText(R.id.overflow_title, mSummaryText);
1638 }
1639
1640 return contentView;
1641 }
1642
1643 public abstract Notification build();
Joe Onorato46439ce2010-11-19 13:56:21 -08001644 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001645
1646 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001647 * Helper class for generating large-format notifications that include a large image attachment.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001648 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001649 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001650 * <pre class="prettyprint">
1651 * Notification noti = new Notification.BigPictureStyle(
1652 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001653 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001654 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001655 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001656 * .setLargeIcon(aBitmap))
1657 * .bigPicture(aBigBitmap)
1658 * .build();
1659 * </pre>
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001660 *
1661 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001662 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001663 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001664 private Bitmap mPicture;
1665
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001666 public BigPictureStyle() {
1667 }
1668
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001669 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001670 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001671 }
1672
Chris Wrend6297db2012-05-03 16:20:13 -04001673 /**
1674 * Overrides ContentTitle in the big form of the template.
1675 * This defaults to the value passed to setContentTitle().
1676 */
1677 public BigPictureStyle setBigContentTitle(CharSequence title) {
1678 internalSetBigContentTitle(title);
1679 return this;
1680 }
1681
1682 /**
1683 * Set the first line of text after the detail section in the big form of the template.
1684 */
1685 public BigPictureStyle setSummaryText(CharSequence cs) {
1686 internalSetSummaryText(cs);
1687 return this;
1688 }
1689
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001690 public BigPictureStyle bigPicture(Bitmap b) {
1691 mPicture = b;
1692 return this;
1693 }
1694
1695 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04001696 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001697
1698 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
1699
1700 return contentView;
1701 }
1702
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001703 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001704 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001705 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001706 Notification wip = mBuilder.buildUnstyled();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001707 wip.bigContentView = makeBigContentView();
1708 return wip;
1709 }
1710 }
1711
1712 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001713 * Helper class for generating large-format notifications that include a lot of text.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001714 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001715 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001716 * <pre class="prettyprint">
1717 * Notification noti = new Notification.BigPictureStyle(
1718 * new Notification.Builder()
1719 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1720 * .setContentText(subject)
1721 * .setSmallIcon(R.drawable.new_mail)
1722 * .setLargeIcon(aBitmap))
1723 * .bigText(aVeryLongString)
1724 * .build();
1725 * </pre>
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001726 *
1727 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001728 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001729 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001730 private CharSequence mBigText;
1731
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001732 public BigTextStyle() {
1733 }
1734
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001735 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001736 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001737 }
1738
Chris Wrend6297db2012-05-03 16:20:13 -04001739 /**
1740 * Overrides ContentTitle in the big form of the template.
1741 * This defaults to the value passed to setContentTitle().
1742 */
1743 public BigTextStyle setBigContentTitle(CharSequence title) {
1744 internalSetBigContentTitle(title);
1745 return this;
1746 }
1747
1748 /**
1749 * Set the first line of text after the detail section in the big form of the template.
1750 */
1751 public BigTextStyle setSummaryText(CharSequence cs) {
1752 internalSetSummaryText(cs);
1753 return this;
1754 }
1755
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001756 public BigTextStyle bigText(CharSequence cs) {
1757 mBigText = cs;
1758 return this;
1759 }
1760
1761 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04001762 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001763 contentView.setTextViewText(R.id.big_text, mBigText);
1764 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04001765 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001766
1767 return contentView;
1768 }
1769
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001770 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001771 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001772 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001773 Notification wip = mBuilder.buildUnstyled();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001774 wip.bigContentView = makeBigContentView();
1775 return wip;
1776 }
1777 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04001778
1779 /**
1780 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
1781 *
1782 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
1783 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001784 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04001785 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04001786 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04001787 * .setContentText(subject)
1788 * .setSmallIcon(R.drawable.new_mail)
1789 * .setLargeIcon(aBitmap))
1790 * .addLine(str1)
1791 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04001792 * .setContentTitle("")
1793 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04001794 * .build();
1795 * </pre>
1796 *
1797 * @see Notification#bigContentView
1798 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001799 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04001800 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
1801
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001802 public InboxStyle() {
1803 }
1804
Daniel Sandler879c5e02012-04-17 16:46:51 -04001805 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001806 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04001807 }
1808
Chris Wrend6297db2012-05-03 16:20:13 -04001809 /**
1810 * Overrides ContentTitle in the big form of the template.
1811 * This defaults to the value passed to setContentTitle().
1812 */
1813 public InboxStyle setBigContentTitle(CharSequence title) {
1814 internalSetBigContentTitle(title);
1815 return this;
1816 }
1817
1818 /**
1819 * Set the first line of text after the detail section in the big form of the template.
1820 */
1821 public InboxStyle setSummaryText(CharSequence cs) {
1822 internalSetSummaryText(cs);
1823 return this;
1824 }
1825
Daniel Sandler879c5e02012-04-17 16:46:51 -04001826 public InboxStyle addLine(CharSequence cs) {
1827 mTexts.add(cs);
1828 return this;
1829 }
1830
1831 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04001832 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox);
1833 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04001834
Chris Wrend6297db2012-05-03 16:20:13 -04001835 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
1836 R.id.inbox_text4};
1837
Daniel Sandler879c5e02012-04-17 16:46:51 -04001838 int i=0;
1839 while (i < mTexts.size() && i < rowIds.length) {
1840 CharSequence str = mTexts.get(i);
1841 if (str != null && !str.equals("")) {
1842 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
1843 contentView.setTextViewText(rowIds[i], str);
1844 }
1845 i++;
1846 }
1847
1848 return contentView;
1849 }
1850
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001851 @Override
Daniel Sandler879c5e02012-04-17 16:46:51 -04001852 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001853 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001854 Notification wip = mBuilder.buildUnstyled();
Daniel Sandler879c5e02012-04-17 16:46:51 -04001855 wip.bigContentView = makeBigContentView();
1856 return wip;
1857 }
1858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859}