blob: 4a0ee480ce03822bed490019fd265bf85a7ae899 [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;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040023import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040024import android.graphics.Bitmap;
Jeff Sharkey098d5802012-04-26 17:30:34 -070025import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.net.Uri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050027import android.os.Bundle;
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;
Jeff Sharkey6d515712012-09-20 16:06:08 -070031import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.text.TextUtils;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040033import android.util.TypedValue;
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.
Joe Malin8d40d042012-11-05 11:36:40 -080094 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095 * 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
Joe Malin8d40d042012-11-05 11:36:40 -0800100 * timestamped according to when the activity began.
101 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500102 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800103 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500104 * <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.
Joe Malin8d40d042012-11-05 11:36:40 -0800111 * </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.
Joe Malin8d40d042012-11-05 11:36:40 -0800134 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500135 * 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.
Joe Malin8d40d042012-11-05 11:36:40 -0800140 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 * 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 Sandler8aa9ae62012-12-04 23:31:47 -0500321 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500322
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.
Joe Malin8d40d042012-11-05 11:36:40 -0800342 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500343 * @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.
Joe Malin8d40d042012-11-05 11:36:40 -0800383 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500384 * 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
Daniel Sandler6738eee2012-11-16 12:03:32 -0500387 * system will make a determination about how to interpret this priority when presenting
388 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500389 */
390 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800391
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500392 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400393 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500394 * Notification type: incoming call (voice or video) or similar synchronous communication request.
395 */
396 public static final String KIND_CALL = "android.call";
397
398 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400399 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500400 * Notification type: incoming direct message (SMS, instant message, etc.).
401 */
402 public static final String KIND_MESSAGE = "android.message";
403
404 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400405 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500406 * Notification type: asynchronous bulk message (email).
407 */
408 public static final String KIND_EMAIL = "android.email";
409
410 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400411 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500412 * Notification type: calendar event.
413 */
414 public static final String KIND_EVENT = "android.event";
415
416 /**
Daniel Sandlera90513d2012-06-04 02:11:17 -0400417 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500418 * Notification type: promotion or advertisement.
419 */
420 public static final String KIND_PROMO = "android.promo";
421
422 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400423 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500424 * If this notification matches of one or more special types (see the <code>KIND_*</code>
425 * constants), add them here, best match first.
426 */
427 public String[] kind;
428
429 /**
430 * Extra key for people values (type TBD).
431 *
432 * @hide
433 */
434 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500435 /** @hide */
436 public static final String EXTRA_TITLE = "android.title";
437 /** @hide */
438 public static final String EXTRA_TEXT = "android.text";
439 /** @hide */
440 public static final String EXTRA_SUBTEXT = "android.subtext";
441 /** @hide */
442 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500443
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500444 /** @hide */
445 public Bundle extras = new Bundle();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500446
447 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400448 * Structure to encapsulate an "action", including title and icon, that can be attached to a Notification.
449 * @hide
450 */
451 private static class Action implements Parcelable {
452 public int icon;
453 public CharSequence title;
454 public PendingIntent actionIntent;
455 @SuppressWarnings("unused")
456 public Action() { }
457 private Action(Parcel in) {
458 icon = in.readInt();
459 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
460 if (in.readInt() == 1) {
461 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
462 }
463 }
464 public Action(int icon_, CharSequence title_, PendingIntent intent_) {
465 this.icon = icon_;
466 this.title = title_;
467 this.actionIntent = intent_;
468 }
469 @Override
470 public Action clone() {
471 return new Action(
472 this.icon,
473 this.title.toString(),
474 this.actionIntent // safe to alias
475 );
476 }
477 @Override
478 public int describeContents() {
479 return 0;
480 }
481 @Override
482 public void writeToParcel(Parcel out, int flags) {
483 out.writeInt(icon);
484 TextUtils.writeToParcel(title, out, flags);
485 if (actionIntent != null) {
486 out.writeInt(1);
487 actionIntent.writeToParcel(out, flags);
488 } else {
489 out.writeInt(0);
490 }
491 }
492 public static final Parcelable.Creator<Action> CREATOR
493 = new Parcelable.Creator<Action>() {
494 public Action createFromParcel(Parcel in) {
495 return new Action(in);
496 }
497 public Action[] newArray(int size) {
498 return new Action[size];
499 }
500 };
501 }
502
503 private Action[] actions;
504
505 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500506 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800507 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
509 public Notification()
510 {
511 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500512 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 * @hide
517 */
518 public Notification(Context context, int icon, CharSequence tickerText, long when,
519 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
520 {
521 this.when = when;
522 this.icon = icon;
523 this.tickerText = tickerText;
524 setLatestEventInfo(context, contentTitle, contentText,
525 PendingIntent.getActivity(context, 0, contentIntent, 0));
526 }
527
528 /**
529 * Constructs a Notification object with the information needed to
530 * have a status bar icon without the standard expanded view.
531 *
532 * @param icon The resource id of the icon to put in the status bar.
533 * @param tickerText The text that flows by in the status bar when the notification first
534 * activates.
535 * @param when The time to show in the time field. In the System.currentTimeMillis
536 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800537 *
538 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800540 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 public Notification(int icon, CharSequence tickerText, long when)
542 {
543 this.icon = icon;
544 this.tickerText = tickerText;
545 this.when = when;
546 }
547
548 /**
549 * Unflatten the notification from a parcel.
550 */
551 public Notification(Parcel parcel)
552 {
553 int version = parcel.readInt();
554
555 when = parcel.readLong();
556 icon = parcel.readInt();
557 number = parcel.readInt();
558 if (parcel.readInt() != 0) {
559 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
560 }
561 if (parcel.readInt() != 0) {
562 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
563 }
564 if (parcel.readInt() != 0) {
565 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
566 }
567 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800568 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400569 }
570 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
572 }
Joe Onorato561d3852010-11-20 18:09:34 -0800573 if (parcel.readInt() != 0) {
574 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 defaults = parcel.readInt();
577 flags = parcel.readInt();
578 if (parcel.readInt() != 0) {
579 sound = Uri.CREATOR.createFromParcel(parcel);
580 }
581
582 audioStreamType = parcel.readInt();
583 vibrate = parcel.createLongArray();
584 ledARGB = parcel.readInt();
585 ledOnMS = parcel.readInt();
586 ledOffMS = parcel.readInt();
587 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400588
589 if (parcel.readInt() != 0) {
590 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
591 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500592
593 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400594
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595 kind = parcel.createStringArray(); // may set kind to null
596
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500597 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400598
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500599 actions = parcel.createTypedArray(Action.CREATOR); // may be null
600
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400601 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400602 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605
Andy Stadler110988c2010-12-03 14:29:16 -0800606 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700607 public Notification clone() {
608 Notification that = new Notification();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500609 cloneInto(that);
610 return that;
611 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700612
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500613 private void cloneInto(Notification that) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700614 that.when = this.when;
615 that.icon = this.icon;
616 that.number = this.number;
617
618 // PendingIntents are global, so there's no reason (or way) to clone them.
619 that.contentIntent = this.contentIntent;
620 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400621 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700622
623 if (this.tickerText != null) {
624 that.tickerText = this.tickerText.toString();
625 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800626 if (this.tickerView != null) {
627 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400628 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700629 if (this.contentView != null) {
630 that.contentView = this.contentView.clone();
631 }
Joe Onorato561d3852010-11-20 18:09:34 -0800632 if (this.largeIcon != null) {
633 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
634 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100635 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700636 that.sound = this.sound; // android.net.Uri is immutable
637 that.audioStreamType = this.audioStreamType;
638
639 final long[] vibrate = this.vibrate;
640 if (vibrate != null) {
641 final int N = vibrate.length;
642 final long[] vib = that.vibrate = new long[N];
643 System.arraycopy(vibrate, 0, vib, 0, N);
644 }
645
646 that.ledARGB = this.ledARGB;
647 that.ledOnMS = this.ledOnMS;
648 that.ledOffMS = this.ledOffMS;
649 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650
Joe Onorato18e69df2010-05-17 22:26:12 -0700651 that.flags = this.flags;
652
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800654
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 final String[] thiskind = this.kind;
656 if (thiskind != null) {
657 final int N = thiskind.length;
658 final String[] thatkind = that.kind = new String[N];
659 System.arraycopy(thiskind, 0, thatkind, 0, N);
660 }
661
662 if (this.extras != null) {
663 that.extras = new Bundle(this.extras);
664
665 }
666
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500667 if (this.actions != null) {
668 that.actions = new Action[this.actions.length];
669 for(int i=0; i<this.actions.length; i++) {
670 that.actions[i] = this.actions[i].clone();
671 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400672 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500673
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400674 if (this.bigContentView != null) {
675 that.bigContentView = this.bigContentView.clone();
676 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700677 }
678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 public int describeContents() {
680 return 0;
681 }
682
683 /**
684 * Flatten this notification from a parcel.
685 */
686 public void writeToParcel(Parcel parcel, int flags)
687 {
688 parcel.writeInt(1);
689
690 parcel.writeLong(when);
691 parcel.writeInt(icon);
692 parcel.writeInt(number);
693 if (contentIntent != null) {
694 parcel.writeInt(1);
695 contentIntent.writeToParcel(parcel, 0);
696 } else {
697 parcel.writeInt(0);
698 }
699 if (deleteIntent != null) {
700 parcel.writeInt(1);
701 deleteIntent.writeToParcel(parcel, 0);
702 } else {
703 parcel.writeInt(0);
704 }
705 if (tickerText != null) {
706 parcel.writeInt(1);
707 TextUtils.writeToParcel(tickerText, parcel, flags);
708 } else {
709 parcel.writeInt(0);
710 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800711 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400712 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800713 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400714 } else {
715 parcel.writeInt(0);
716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 if (contentView != null) {
718 parcel.writeInt(1);
719 contentView.writeToParcel(parcel, 0);
720 } else {
721 parcel.writeInt(0);
722 }
Joe Onorato561d3852010-11-20 18:09:34 -0800723 if (largeIcon != null) {
724 parcel.writeInt(1);
725 largeIcon.writeToParcel(parcel, 0);
726 } else {
727 parcel.writeInt(0);
728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729
730 parcel.writeInt(defaults);
731 parcel.writeInt(this.flags);
732
733 if (sound != null) {
734 parcel.writeInt(1);
735 sound.writeToParcel(parcel, 0);
736 } else {
737 parcel.writeInt(0);
738 }
739 parcel.writeInt(audioStreamType);
740 parcel.writeLongArray(vibrate);
741 parcel.writeInt(ledARGB);
742 parcel.writeInt(ledOnMS);
743 parcel.writeInt(ledOffMS);
744 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400745
746 if (fullScreenIntent != null) {
747 parcel.writeInt(1);
748 fullScreenIntent.writeToParcel(parcel, 0);
749 } else {
750 parcel.writeInt(0);
751 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500752
753 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -0800754
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 parcel.writeStringArray(kind); // ok for null
Joe Malin8d40d042012-11-05 11:36:40 -0800756
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500757 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400758
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500759 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400760
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400761 if (bigContentView != null) {
762 parcel.writeInt(1);
763 bigContentView.writeToParcel(parcel, 0);
764 } else {
765 parcel.writeInt(0);
766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
768
769 /**
770 * Parcelable.Creator that instantiates Notification objects
771 */
772 public static final Parcelable.Creator<Notification> CREATOR
773 = new Parcelable.Creator<Notification>()
774 {
775 public Notification createFromParcel(Parcel parcel)
776 {
777 return new Notification(parcel);
778 }
779
780 public Notification[] newArray(int size)
781 {
782 return new Notification[size];
783 }
784 };
785
786 /**
787 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
788 * layout.
789 *
790 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
791 * in the view.</p>
792 * @param context The context for your application / activity.
793 * @param contentTitle The title that goes in the expanded entry.
794 * @param contentText The text that goes in the expanded entry.
795 * @param contentIntent The intent to launch when the user clicks the expanded notification.
796 * If this is an activity, it must include the
797 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800798 * that you take care of task management as described in the
799 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
800 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800802 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800804 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 public void setLatestEventInfo(Context context,
806 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500807 Notification.Builder builder = new Notification.Builder(context);
808
809 // First, ensure that key pieces of information that may have been set directly
810 // are preserved
811 builder.setWhen(this.when);
812 builder.setSmallIcon(this.icon);
813 builder.setPriority(this.priority);
814 builder.setTicker(this.tickerText);
815 builder.setNumber(this.number);
816 builder.mFlags = this.flags;
817 builder.setSound(this.sound, this.audioStreamType);
818 builder.setDefaults(this.defaults);
819 builder.setVibrate(this.vibrate);
820
821 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500823 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500826 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500828 builder.setContentIntent(contentIntent);
829 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
831
832 @Override
833 public String toString() {
834 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500835 sb.append("Notification(pri=");
836 sb.append(priority);
837 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -0800838 if (contentView != null) {
839 sb.append(contentView.getPackage());
840 sb.append("/0x");
841 sb.append(Integer.toHexString(contentView.getLayoutId()));
842 } else {
843 sb.append("null");
844 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500845 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -0800846 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -0500847 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
848 sb.append("default");
849 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 int N = this.vibrate.length-1;
851 sb.append("[");
852 for (int i=0; i<N; i++) {
853 sb.append(this.vibrate[i]);
854 sb.append(',');
855 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200856 if (N != -1) {
857 sb.append(this.vibrate[N]);
858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 } else {
861 sb.append("null");
862 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500863 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -0500864 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -0500866 } else if (this.sound != null) {
867 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 } else {
869 sb.append("null");
870 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500871 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500873 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400874 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500875 sb.append(" kind=[");
876 if (this.kind == null) {
877 sb.append("null");
878 } else {
879 for (int i=0; i<this.kind.length; i++) {
880 if (i>0) sb.append(",");
881 sb.append(this.kind[i]);
882 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400883 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400884 sb.append("]");
885 if (actions != null) {
886 sb.append(" ");
887 sb.append(actions.length);
888 sb.append(" action");
889 if (actions.length > 1) sb.append("s");
890 }
891 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 return sb.toString();
893 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800894
Jeff Sharkey6d515712012-09-20 16:06:08 -0700895 /** {@hide} */
896 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -0700897 if (user.getIdentifier() == UserHandle.USER_ALL) {
898 user = UserHandle.OWNER;
899 }
Jeff Sharkey6d515712012-09-20 16:06:08 -0700900 if (tickerView != null) {
901 tickerView.setUser(user);
902 }
903 if (contentView != null) {
904 contentView.setUser(user);
905 }
906 if (bigContentView != null) {
907 bigContentView.setUser(user);
908 }
909 }
910
Joe Onoratocb109a02011-01-18 17:57:41 -0800911 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500912 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -0800913 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500914 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -0700915 * content views using the platform's notification layout template. If your app supports
916 * versions of Android as old as API level 4, you can instead use
917 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
918 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
919 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -0800920 *
Scott Main183bf112012-08-13 19:12:13 -0700921 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -0800922 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500923 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -0700924 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500925 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
926 * .setContentText(subject)
927 * .setSmallIcon(R.drawable.new_mail)
928 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400929 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500930 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -0800931 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800932 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -0400933 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -0400934
Joe Onorato46439ce2010-11-19 13:56:21 -0800935 private Context mContext;
936
937 private long mWhen;
938 private int mSmallIcon;
939 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800940 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800941 private CharSequence mContentTitle;
942 private CharSequence mContentText;
943 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400944 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -0800945 private PendingIntent mContentIntent;
946 private RemoteViews mContentView;
947 private PendingIntent mDeleteIntent;
948 private PendingIntent mFullScreenIntent;
949 private CharSequence mTickerText;
950 private RemoteViews mTickerView;
951 private Bitmap mLargeIcon;
952 private Uri mSound;
953 private int mAudioStreamType;
954 private long[] mVibrate;
955 private int mLedArgb;
956 private int mLedOnMs;
957 private int mLedOffMs;
958 private int mDefaults;
959 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -0700960 private int mProgressMax;
961 private int mProgress;
962 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500963 private ArrayList<String> mKindList = new ArrayList<String>(1);
964 private Bundle mExtras;
965 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -0400966 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -0400967 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400968 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -0700969 private boolean mShowWhen = true;
Joe Onorato46439ce2010-11-19 13:56:21 -0800970
Joe Onoratocb109a02011-01-18 17:57:41 -0800971 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500972 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -0800973 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500974
975 * <table>
976 * <tr><th align=right>priority</th>
977 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
978 * <tr><th align=right>when</th>
979 * <td>now ({@link System#currentTimeMillis()})</td></tr>
980 * <tr><th align=right>audio stream</th>
981 * <td>{@link #STREAM_DEFAULT}</td></tr>
982 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -0800983 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500984
985 * @param context
986 * A {@link Context} that will be used by the Builder to construct the
987 * RemoteViews. The Context will not be held past the lifetime of this Builder
988 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -0800989 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800990 public Builder(Context context) {
991 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -0800992
993 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -0800994 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -0800995 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500996 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -0800997 }
998
Joe Onoratocb109a02011-01-18 17:57:41 -0800999 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001000 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001001 * It will be shown in the notification content view by default; use
1002 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001003 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001004 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001005 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001006 public Builder setWhen(long when) {
1007 mWhen = when;
1008 return this;
1009 }
1010
Joe Onoratocb109a02011-01-18 17:57:41 -08001011 /**
Daniel Sandler0c890492012-09-12 17:23:10 -07001012 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown
1013 * in the content view.
1014 */
1015 public Builder setShowWhen(boolean show) {
1016 mShowWhen = show;
1017 return this;
1018 }
1019
1020 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001021 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001022 *
1023 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001024 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001025 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001026 * Useful when showing an elapsed time (like an ongoing phone call).
1027 *
1028 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001029 * @see Notification#when
1030 */
1031 public Builder setUsesChronometer(boolean b) {
1032 mUseChronometer = b;
1033 return this;
1034 }
1035
1036 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001037 * Set the small icon resource, which will be used to represent the notification in the
1038 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001039 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001040
1041 * The platform template for the expanded view will draw this icon in the left, unless a
1042 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1043 * icon will be moved to the right-hand side.
1044 *
1045
1046 * @param icon
1047 * A resource ID in the application's package of the drawable to use.
1048 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001049 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001050 public Builder setSmallIcon(int icon) {
1051 mSmallIcon = icon;
1052 return this;
1053 }
1054
Joe Onoratocb109a02011-01-18 17:57:41 -08001055 /**
1056 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1057 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1058 * LevelListDrawable}.
1059 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001060 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001061 * @param level The level to use for the icon.
1062 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001063 * @see Notification#icon
1064 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001065 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001066 public Builder setSmallIcon(int icon, int level) {
1067 mSmallIcon = icon;
1068 mSmallIconLevel = level;
1069 return this;
1070 }
1071
Joe Onoratocb109a02011-01-18 17:57:41 -08001072 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001073 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001074 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001075 public Builder setContentTitle(CharSequence title) {
1076 mContentTitle = title;
1077 return this;
1078 }
1079
Joe Onoratocb109a02011-01-18 17:57:41 -08001080 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001081 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001082 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001083 public Builder setContentText(CharSequence text) {
1084 mContentText = text;
1085 return this;
1086 }
1087
Joe Onoratocb109a02011-01-18 17:57:41 -08001088 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001089 * Set the third line of text in the platform notification template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001090 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the same location in the standard template.
1091 */
1092 public Builder setSubText(CharSequence text) {
1093 mSubText = text;
1094 return this;
1095 }
1096
1097 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001098 * Set the large number at the right-hand side of the notification. This is
1099 * equivalent to setContentInfo, although it might show the number in a different
1100 * font size for readability.
1101 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001102 public Builder setNumber(int number) {
1103 mNumber = number;
1104 return this;
1105 }
1106
Joe Onoratocb109a02011-01-18 17:57:41 -08001107 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001108 * A small piece of additional information pertaining to this notification.
1109 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001110 * The platform template will draw this on the last line of the notification, at the far
1111 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001112 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001113 public Builder setContentInfo(CharSequence info) {
1114 mContentInfo = info;
1115 return this;
1116 }
1117
Joe Onoratocb109a02011-01-18 17:57:41 -08001118 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001119 * Set the progress this notification represents.
1120 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001121 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001122 */
1123 public Builder setProgress(int max, int progress, boolean indeterminate) {
1124 mProgressMax = max;
1125 mProgress = progress;
1126 mProgressIndeterminate = indeterminate;
1127 return this;
1128 }
1129
1130 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001131 * Supply a custom RemoteViews to use instead of the platform template.
1132 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001133 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001134 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001135 public Builder setContent(RemoteViews views) {
1136 mContentView = views;
1137 return this;
1138 }
1139
Joe Onoratocb109a02011-01-18 17:57:41 -08001140 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001141 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001143 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1144 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1145 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001146 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001147 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001148 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001149 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001150 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001151 public Builder setContentIntent(PendingIntent intent) {
1152 mContentIntent = intent;
1153 return this;
1154 }
1155
Joe Onoratocb109a02011-01-18 17:57:41 -08001156 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001157 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1158 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001159 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001160 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001161 public Builder setDeleteIntent(PendingIntent intent) {
1162 mDeleteIntent = intent;
1163 return this;
1164 }
1165
Joe Onoratocb109a02011-01-18 17:57:41 -08001166 /**
1167 * An intent to launch instead of posting the notification to the status bar.
1168 * Only for use with extremely high-priority notifications demanding the user's
1169 * <strong>immediate</strong> attention, such as an incoming phone call or
1170 * alarm clock that the user has explicitly set to a particular time.
1171 * If this facility is used for something else, please give the user an option
1172 * to turn it off and use a normal notification, as this can be extremely
1173 * disruptive.
1174 *
1175 * @param intent The pending intent to launch.
1176 * @param highPriority Passing true will cause this notification to be sent
1177 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001178 *
1179 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001180 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001181 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1182 mFullScreenIntent = intent;
1183 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1184 return this;
1185 }
1186
Joe Onoratocb109a02011-01-18 17:57:41 -08001187 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001188 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001189 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001190 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001191 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001192 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001193 public Builder setTicker(CharSequence tickerText) {
1194 mTickerText = tickerText;
1195 return this;
1196 }
1197
Joe Onoratocb109a02011-01-18 17:57:41 -08001198 /**
1199 * Set the text that is displayed in the status bar when the notification first
1200 * arrives, and also a RemoteViews object that may be displayed instead on some
1201 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001202 *
1203 * @see Notification#tickerText
1204 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001205 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001206 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
1207 mTickerText = tickerText;
1208 mTickerView = views;
1209 return this;
1210 }
1211
Joe Onoratocb109a02011-01-18 17:57:41 -08001212 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001213 * Add a large icon to the notification (and the ticker on some devices).
1214 *
1215 * In the platform template, this image will be shown on the left of the notification view
1216 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1217 *
1218 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001219 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001220 public Builder setLargeIcon(Bitmap icon) {
1221 mLargeIcon = icon;
1222 return this;
1223 }
1224
Joe Onoratocb109a02011-01-18 17:57:41 -08001225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001226 * Set the sound to play.
1227 *
1228 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1229 *
1230 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001231 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001232 public Builder setSound(Uri sound) {
1233 mSound = sound;
1234 mAudioStreamType = STREAM_DEFAULT;
1235 return this;
1236 }
1237
Joe Onoratocb109a02011-01-18 17:57:41 -08001238 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001239 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001240 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001241 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1242 *
1243 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001244 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001245 public Builder setSound(Uri sound, int streamType) {
1246 mSound = sound;
1247 mAudioStreamType = streamType;
1248 return this;
1249 }
1250
Joe Onoratocb109a02011-01-18 17:57:41 -08001251 /**
1252 * Set the vibration pattern to use.
1253 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001254
1255 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1256 * <code>pattern</code> parameter.
1257 *
1258
1259 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001260 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001261 public Builder setVibrate(long[] pattern) {
1262 mVibrate = pattern;
1263 return this;
1264 }
1265
Joe Onoratocb109a02011-01-18 17:57:41 -08001266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001267 * Set the desired color for the indicator LED on the device, as well as the
1268 * blink duty cycle (specified in milliseconds).
1269 *
1270
1271 * Not all devices will honor all (or even any) of these values.
1272 *
1273
1274 * @see Notification#ledARGB
1275 * @see Notification#ledOnMS
1276 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001277 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001278 public Builder setLights(int argb, int onMs, int offMs) {
1279 mLedArgb = argb;
1280 mLedOnMs = onMs;
1281 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001282 return this;
1283 }
1284
Joe Onoratocb109a02011-01-18 17:57:41 -08001285 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001286 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001287 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001288
1289 * Ongoing notifications cannot be dismissed by the user, so your application or service
1290 * must take care of canceling them.
1291 *
1292
1293 * They are typically used to indicate a background task that the user is actively engaged
1294 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1295 * (e.g., a file download, sync operation, active network connection).
1296 *
1297
1298 * @see Notification#FLAG_ONGOING_EVENT
1299 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001300 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001301 public Builder setOngoing(boolean ongoing) {
1302 setFlag(FLAG_ONGOING_EVENT, ongoing);
1303 return this;
1304 }
1305
Joe Onoratocb109a02011-01-18 17:57:41 -08001306 /**
1307 * Set this flag if you would only like the sound, vibrate
1308 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001309 *
1310 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001311 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001312 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1313 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1314 return this;
1315 }
1316
Joe Onoratocb109a02011-01-18 17:57:41 -08001317 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001318 * Make this notification automatically dismissed when the user touches it. The
1319 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1320 *
1321 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001322 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001323 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001324 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001325 return this;
1326 }
1327
Joe Onoratocb109a02011-01-18 17:57:41 -08001328 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001329 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001330 * <p>
1331 * The value should be one or more of the following fields combined with
1332 * bitwise-or:
1333 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1334 * <p>
1335 * For all default values, use {@link #DEFAULT_ALL}.
1336 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001337 public Builder setDefaults(int defaults) {
1338 mDefaults = defaults;
1339 return this;
1340 }
1341
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001342 /**
1343 * Set the priority of this notification.
1344 *
1345 * @see Notification#priority
1346 */
1347 public Builder setPriority(int pri) {
1348 mPriority = pri;
1349 return this;
1350 }
Joe Malin8d40d042012-11-05 11:36:40 -08001351
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001352 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001353 * @hide
Joe Malin8d40d042012-11-05 11:36:40 -08001354 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001355 * Add a kind (category) to this notification. Optional.
Joe Malin8d40d042012-11-05 11:36:40 -08001356 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001357 * @see Notification#kind
1358 */
1359 public Builder addKind(String k) {
1360 mKindList.add(k);
1361 return this;
1362 }
1363
1364 /**
1365 * Add metadata to this notification.
1366 *
1367 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001368 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001369 * called.
1370 *
1371 * @see Notification#extras
1372 * @hide
1373 */
1374 public Builder setExtras(Bundle bag) {
1375 mExtras = bag;
1376 return this;
1377 }
1378
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001379 /**
1380 * Add an action to this notification. Actions are typically displayed by
1381 * the system as a button adjacent to the notification content.
Joe Malin8d40d042012-11-05 11:36:40 -08001382 * <br>
1383 * A notification displays up to 3 actions, from left to right in the order they were added.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001384 *
1385 * @param icon Resource ID of a drawable that represents the action.
1386 * @param title Text describing the action.
1387 * @param intent PendingIntent to be fired when the action is invoked.
1388 */
1389 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
1390 mActions.add(new Action(icon, title, intent));
1391 return this;
1392 }
1393
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001394 /**
1395 * Add a rich notification style to be applied at build time.
1396 *
1397 * @param style Object responsible for modifying the notification style.
1398 */
1399 public Builder setStyle(Style style) {
1400 if (mStyle != style) {
1401 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001402 if (mStyle != null) {
1403 mStyle.setBuilder(this);
1404 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001405 }
1406 return this;
1407 }
1408
Joe Onorato46439ce2010-11-19 13:56:21 -08001409 private void setFlag(int mask, boolean value) {
1410 if (value) {
1411 mFlags |= mask;
1412 } else {
1413 mFlags &= ~mask;
1414 }
1415 }
1416
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001417 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08001418 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001419 boolean showLine3 = false;
1420 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001421 int smallIconImageViewId = R.id.icon;
1422 if (mLargeIcon != null) {
1423 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1424 smallIconImageViewId = R.id.right_icon;
1425 }
Daniel Sandlere95658c2012-05-10 00:33:54 -04001426 if (mPriority < PRIORITY_LOW) {
1427 contentView.setInt(R.id.icon,
1428 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1429 contentView.setInt(R.id.status_bar_latest_event_content,
1430 "setBackgroundResource", R.drawable.notification_bg_low);
1431 }
Joe Onorato561d3852010-11-20 18:09:34 -08001432 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001433 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1434 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001435 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001436 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001437 }
1438 if (mContentTitle != null) {
1439 contentView.setTextViewText(R.id.title, mContentTitle);
1440 }
1441 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001442 contentView.setTextViewText(R.id.text, mContentText);
1443 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001444 }
1445 if (mContentInfo != null) {
1446 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001447 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001448 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001449 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001450 final int tooBig = mContext.getResources().getInteger(
1451 R.integer.status_bar_notification_info_maxnum);
1452 if (mNumber > tooBig) {
1453 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1454 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001455 } else {
1456 NumberFormat f = NumberFormat.getIntegerInstance();
1457 contentView.setTextViewText(R.id.info, f.format(mNumber));
1458 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001459 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001460 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001461 } else {
1462 contentView.setViewVisibility(R.id.info, View.GONE);
1463 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001464
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001465 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001466 if (mSubText != null) {
1467 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001468 if (mContentText != null) {
1469 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001470 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
1471 showLine2 = true;
1472 } else {
1473 contentView.setViewVisibility(R.id.text2, View.GONE);
1474 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001475 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001476 contentView.setViewVisibility(R.id.text2, View.GONE);
1477 if (mProgressMax != 0 || mProgressIndeterminate) {
1478 contentView.setProgressBar(
1479 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1480 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001481 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001482 } else {
1483 contentView.setViewVisibility(R.id.progress, View.GONE);
1484 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001485 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001486 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001487 if (fitIn1U) {
1488 // need to shrink all the type to make sure everything fits
1489 final Resources res = mContext.getResources();
1490 final float subTextSize = res.getDimensionPixelSize(
1491 R.dimen.notification_subtext_size);
1492 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
1493 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001494 // vertical centering
1495 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1496 }
1497
Daniel Sandler0c890492012-09-12 17:23:10 -07001498 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001499 if (mUseChronometer) {
1500 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1501 contentView.setLong(R.id.chronometer, "setBase",
1502 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1503 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1504 } else {
1505 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1506 contentView.setLong(R.id.time, "setTime", mWhen);
1507 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001508 } else {
1509 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001510 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001511
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001512 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001513 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001514 return contentView;
1515 }
1516
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001517 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001518 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001519
1520 int N = mActions.size();
1521 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001522 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001523 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001524 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001525 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04001526 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001527 for (int i=0; i<N; i++) {
1528 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001529 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001530 big.addView(R.id.actions, button);
1531 }
1532 }
1533 return big;
1534 }
1535
Joe Onorato46439ce2010-11-19 13:56:21 -08001536 private RemoteViews makeContentView() {
1537 if (mContentView != null) {
1538 return mContentView;
1539 } else {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001540 return applyStandardTemplate(R.layout.notification_template_base, true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001541 }
1542 }
1543
1544 private RemoteViews makeTickerView() {
1545 if (mTickerView != null) {
1546 return mTickerView;
1547 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001548 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001549 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001550 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001551 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08001552 } else {
1553 return null;
1554 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001555 }
1556 }
1557
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001558 private RemoteViews makeBigContentView() {
1559 if (mActions.size() == 0) return null;
1560
Chris Wrenb023bf82012-04-23 16:05:42 -04001561 return applyStandardTemplateWithActions(R.layout.notification_template_big_base);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001562 }
1563
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001564 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04001565 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08001566 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Daniel Sandler8680bf82012-05-15 16:52:52 -04001567 tombstone ? R.layout.notification_action_tombstone
1568 : R.layout.notification_action);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001569 button.setTextViewCompoundDrawables(R.id.action0, action.icon, 0, 0, 0);
1570 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001571 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04001572 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04001573 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001574 button.setContentDescription(R.id.action0, action.title);
1575 return button;
1576 }
1577
Joe Onoratocb109a02011-01-18 17:57:41 -08001578 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001579 * Apply the unstyled operations and return a new {@link Notification} object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001580 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001581 private Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08001582 Notification n = new Notification();
1583 n.when = mWhen;
1584 n.icon = mSmallIcon;
1585 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001586 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001587 n.contentView = makeContentView();
1588 n.contentIntent = mContentIntent;
1589 n.deleteIntent = mDeleteIntent;
1590 n.fullScreenIntent = mFullScreenIntent;
1591 n.tickerText = mTickerText;
1592 n.tickerView = makeTickerView();
1593 n.largeIcon = mLargeIcon;
1594 n.sound = mSound;
1595 n.audioStreamType = mAudioStreamType;
1596 n.vibrate = mVibrate;
1597 n.ledARGB = mLedArgb;
1598 n.ledOnMS = mLedOnMs;
1599 n.ledOffMS = mLedOffMs;
1600 n.defaults = mDefaults;
1601 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001602 n.bigContentView = makeBigContentView();
Joe Onorato8d0b6552010-11-22 16:09:29 -08001603 if (mLedOnMs != 0 && mLedOffMs != 0) {
1604 n.flags |= FLAG_SHOW_LIGHTS;
1605 }
1606 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1607 n.flags |= FLAG_SHOW_LIGHTS;
1608 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001609 if (mKindList.size() > 0) {
1610 n.kind = new String[mKindList.size()];
1611 mKindList.toArray(n.kind);
1612 } else {
1613 n.kind = null;
1614 }
1615 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001616 if (mActions.size() > 0) {
1617 n.actions = new Action[mActions.size()];
1618 mActions.toArray(n.actions);
1619 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001620
1621 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
1622
1623 // Store original information used in the construction of this object
1624 n.extras.putCharSequence(EXTRA_TITLE, mContentTitle);
1625 n.extras.putCharSequence(EXTRA_TEXT, mContentText);
1626 n.extras.putCharSequence(EXTRA_SUBTEXT, mSubText);
1627 n.extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
1628 //n.extras.putByteArray(EXTRA_LARGE_ICON, ...
1629
Joe Onorato46439ce2010-11-19 13:56:21 -08001630 return n;
1631 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001632
1633 /**
1634 * @deprecated Use {@link #build()} instead.
1635 */
1636 @Deprecated
1637 public Notification getNotification() {
1638 return build();
1639 }
1640
1641 /**
1642 * Combine all of the options that have been set and return a new {@link Notification}
1643 * object.
1644 */
1645 public Notification build() {
1646 if (mStyle != null) {
1647 return mStyle.build();
1648 } else {
1649 return buildUnstyled();
1650 }
1651 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001652
1653 /**
1654 * Apply this Builder to an existing {@link Notification} object.
1655 *
1656 * @hide
1657 */
1658 public Notification buildInto(Notification n) {
1659 build().cloneInto(n);
1660 return n;
1661 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001662 }
1663
1664
1665 /**
1666 * An object that can apply a rich notification style to a {@link Notification.Builder}
1667 * object.
1668 */
Chris Wrend6297db2012-05-03 16:20:13 -04001669 public static abstract class Style
1670 {
1671 private CharSequence mBigContentTitle;
1672 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04001673 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04001674
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001675 protected Builder mBuilder;
1676
Chris Wrend6297db2012-05-03 16:20:13 -04001677 /**
1678 * Overrides ContentTitle in the big form of the template.
1679 * This defaults to the value passed to setContentTitle().
1680 */
1681 protected void internalSetBigContentTitle(CharSequence title) {
1682 mBigContentTitle = title;
1683 }
1684
1685 /**
1686 * Set the first line of text after the detail section in the big form of the template.
1687 */
1688 protected void internalSetSummaryText(CharSequence cs) {
1689 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04001690 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04001691 }
1692
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001693 public void setBuilder(Builder builder) {
1694 if (mBuilder != builder) {
1695 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001696 if (mBuilder != null) {
1697 mBuilder.setStyle(this);
1698 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001699 }
1700 }
1701
Chris Wrend6297db2012-05-03 16:20:13 -04001702 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001703 if (mBuilder == null) {
1704 throw new IllegalArgumentException("Style requires a valid Builder object");
1705 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001706 }
Chris Wrend6297db2012-05-03 16:20:13 -04001707
1708 protected RemoteViews getStandardView(int layoutId) {
1709 checkBuilder();
1710
1711 if (mBigContentTitle != null) {
1712 mBuilder.setContentTitle(mBigContentTitle);
1713 }
1714
Chris Wrend6297db2012-05-03 16:20:13 -04001715 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
1716
Chris Wrend6297db2012-05-03 16:20:13 -04001717 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
1718 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04001719 } else {
1720 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04001721 }
1722
Daniel Sandler619738c2012-06-07 16:33:08 -04001723 // The last line defaults to the subtext, but can be replaced by mSummaryText
1724 final CharSequence overflowText =
1725 mSummaryTextSet ? mSummaryText
1726 : mBuilder.mSubText;
1727 if (overflowText != null) {
1728 contentView.setTextViewText(R.id.text, overflowText);
1729 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001730 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04001731 } else {
1732 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
1733 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04001734 }
1735
1736 return contentView;
1737 }
1738
1739 public abstract Notification build();
Joe Onorato46439ce2010-11-19 13:56:21 -08001740 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001741
1742 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001743 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08001744 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001745 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001746 * <pre class="prettyprint">
1747 * Notification noti = new Notification.BigPictureStyle(
1748 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001749 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001750 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001751 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001752 * .setLargeIcon(aBitmap))
1753 * .bigPicture(aBigBitmap)
1754 * .build();
1755 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001756 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001757 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001758 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001759 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001760 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04001761 private Bitmap mBigLargeIcon;
1762 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001763
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001764 public BigPictureStyle() {
1765 }
1766
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001767 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001768 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001769 }
1770
Chris Wrend6297db2012-05-03 16:20:13 -04001771 /**
1772 * Overrides ContentTitle in the big form of the template.
1773 * This defaults to the value passed to setContentTitle().
1774 */
1775 public BigPictureStyle setBigContentTitle(CharSequence title) {
1776 internalSetBigContentTitle(title);
1777 return this;
1778 }
1779
1780 /**
1781 * Set the first line of text after the detail section in the big form of the template.
1782 */
1783 public BigPictureStyle setSummaryText(CharSequence cs) {
1784 internalSetSummaryText(cs);
1785 return this;
1786 }
1787
Chris Wren0bd664d2012-08-01 13:56:56 -04001788 /**
1789 * Provide the bitmap to be used as the payload for the BigPicture notification.
1790 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001791 public BigPictureStyle bigPicture(Bitmap b) {
1792 mPicture = b;
1793 return this;
1794 }
1795
Chris Wren3745a3d2012-05-22 15:11:52 -04001796 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04001797 * Override the large icon when the big notification is shown.
1798 */
1799 public BigPictureStyle bigLargeIcon(Bitmap b) {
1800 mBigLargeIconSet = true;
1801 mBigLargeIcon = b;
1802 return this;
1803 }
1804
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001805 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04001806 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001807
1808 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
1809
1810 return contentView;
1811 }
1812
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001813 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001814 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001815 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001816 Notification wip = mBuilder.buildUnstyled();
Chris Wren3745a3d2012-05-22 15:11:52 -04001817 if (mBigLargeIconSet ) {
1818 mBuilder.mLargeIcon = mBigLargeIcon;
1819 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001820 wip.bigContentView = makeBigContentView();
1821 return wip;
1822 }
1823 }
1824
1825 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001826 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08001827 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001828 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001829 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05001830 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001831 * new Notification.Builder()
1832 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1833 * .setContentText(subject)
1834 * .setSmallIcon(R.drawable.new_mail)
1835 * .setLargeIcon(aBitmap))
1836 * .bigText(aVeryLongString)
1837 * .build();
1838 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001839 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001840 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001841 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001842 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001843 private CharSequence mBigText;
1844
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001845 public BigTextStyle() {
1846 }
1847
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001848 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001849 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001850 }
1851
Chris Wrend6297db2012-05-03 16:20:13 -04001852 /**
1853 * Overrides ContentTitle in the big form of the template.
1854 * This defaults to the value passed to setContentTitle().
1855 */
1856 public BigTextStyle setBigContentTitle(CharSequence title) {
1857 internalSetBigContentTitle(title);
1858 return this;
1859 }
1860
1861 /**
1862 * Set the first line of text after the detail section in the big form of the template.
1863 */
1864 public BigTextStyle setSummaryText(CharSequence cs) {
1865 internalSetSummaryText(cs);
1866 return this;
1867 }
1868
Chris Wren0bd664d2012-08-01 13:56:56 -04001869 /**
1870 * Provide the longer text to be displayed in the big form of the
1871 * template in place of the content text.
1872 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001873 public BigTextStyle bigText(CharSequence cs) {
1874 mBigText = cs;
1875 return this;
1876 }
1877
1878 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04001879 // Remove the content text so line3 only shows if you have a summary
1880 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001881 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04001882
Chris Wrend6297db2012-05-03 16:20:13 -04001883 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text);
Joe Malin8d40d042012-11-05 11:36:40 -08001884
Daniel Sandler619738c2012-06-07 16:33:08 -04001885 if (hadThreeLines) {
1886 // vertical centering
1887 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1888 }
1889
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001890 contentView.setTextViewText(R.id.big_text, mBigText);
1891 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04001892 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001893
1894 return contentView;
1895 }
1896
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001897 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001898 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001899 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001900 Notification wip = mBuilder.buildUnstyled();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001901 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001902
1903 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
1904
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001905 return wip;
1906 }
1907 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04001908
1909 /**
1910 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08001911 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04001912 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
1913 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001914 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04001915 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04001916 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04001917 * .setContentText(subject)
1918 * .setSmallIcon(R.drawable.new_mail)
1919 * .setLargeIcon(aBitmap))
1920 * .addLine(str1)
1921 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04001922 * .setContentTitle("")
1923 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04001924 * .build();
1925 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001926 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04001927 * @see Notification#bigContentView
1928 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001929 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04001930 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
1931
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001932 public InboxStyle() {
1933 }
1934
Daniel Sandler879c5e02012-04-17 16:46:51 -04001935 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001936 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04001937 }
1938
Chris Wrend6297db2012-05-03 16:20:13 -04001939 /**
1940 * Overrides ContentTitle in the big form of the template.
1941 * This defaults to the value passed to setContentTitle().
1942 */
1943 public InboxStyle setBigContentTitle(CharSequence title) {
1944 internalSetBigContentTitle(title);
1945 return this;
1946 }
1947
1948 /**
1949 * Set the first line of text after the detail section in the big form of the template.
1950 */
1951 public InboxStyle setSummaryText(CharSequence cs) {
1952 internalSetSummaryText(cs);
1953 return this;
1954 }
1955
Chris Wren0bd664d2012-08-01 13:56:56 -04001956 /**
1957 * Append a line to the digest section of the Inbox notification.
1958 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04001959 public InboxStyle addLine(CharSequence cs) {
1960 mTexts.add(cs);
1961 return this;
1962 }
1963
1964 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04001965 // Remove the content text so line3 disappears unless you have a summary
1966 mBuilder.mContentText = null;
Chris Wrend6297db2012-05-03 16:20:13 -04001967 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox);
Daniel Sandler619738c2012-06-07 16:33:08 -04001968
Chris Wrend6297db2012-05-03 16:20:13 -04001969 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04001970
Chris Wrend6297db2012-05-03 16:20:13 -04001971 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
Chris Wren29bb6d92012-05-17 18:09:42 -04001972 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04001973
Chris Wren4ed80d52012-05-17 09:30:03 -04001974 // Make sure all rows are gone in case we reuse a view.
1975 for (int rowId : rowIds) {
1976 contentView.setViewVisibility(rowId, View.GONE);
1977 }
1978
Chris Wren683ab002012-09-20 10:35:54 -04001979
Daniel Sandler879c5e02012-04-17 16:46:51 -04001980 int i=0;
1981 while (i < mTexts.size() && i < rowIds.length) {
1982 CharSequence str = mTexts.get(i);
1983 if (str != null && !str.equals("")) {
1984 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
1985 contentView.setTextViewText(rowIds[i], str);
1986 }
1987 i++;
1988 }
1989
Chris Wren683ab002012-09-20 10:35:54 -04001990 contentView.setViewVisibility(R.id.inbox_end_pad,
1991 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
1992
1993 contentView.setViewVisibility(R.id.inbox_more,
1994 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04001995
Daniel Sandler879c5e02012-04-17 16:46:51 -04001996 return contentView;
1997 }
1998
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001999 @Override
Daniel Sandler879c5e02012-04-17 16:46:51 -04002000 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04002001 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002002 Notification wip = mBuilder.buildUnstyled();
Daniel Sandler879c5e02012-04-17 16:46:51 -04002003 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002004
2005 StringBuilder builder = new StringBuilder();
2006 for (CharSequence str : mTexts) {
2007 builder.append(str);
2008 builder.append("\n");
2009 }
2010 wip.extras.putCharSequence(EXTRA_TEXT, builder);
2011
Daniel Sandler879c5e02012-04-17 16:46:51 -04002012 return wip;
2013 }
2014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015}