blob: 2c70803042d1a20e6a670b03866807537578495a [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
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.Intent;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040024import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040025import android.graphics.Bitmap;
Jeff Sharkey098d5802012-04-26 17:30:34 -070026import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040028import android.os.BadParcelableException;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050029import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Parcel;
31import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040032import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070033import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040035import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040036import android.util.TypedValue;
Joe Onorato8595a3d2010-11-19 18:12:07 -080037import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070038import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.widget.RemoteViews;
40
Tor Norbyed9273d62013-05-30 15:59:53 -070041import java.lang.annotation.Retention;
42import java.lang.annotation.RetentionPolicy;
Andy Stadler110988c2010-12-03 14:29:16 -080043import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050044import java.util.ArrayList;
Joe Onorato561d3852010-11-20 18:09:34 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/**
47 * A class that represents how a persistent notification is to be presented to
48 * the user using the {@link android.app.NotificationManager}.
49 *
Joe Onoratocb109a02011-01-18 17:57:41 -080050 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
51 * easier to construct Notifications.</p>
52 *
Joe Fernandez558459f2011-10-13 16:47:36 -070053 * <div class="special reference">
54 * <h3>Developer Guides</h3>
55 * <p>For a guide to creating notifications, read the
56 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
57 * developer guide.</p>
58 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 */
60public class Notification implements Parcelable
61{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040062 private static final String TAG = "Notification";
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 /**
65 * Use all default values (where applicable).
66 */
67 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
70 * Use the default notification sound. This will ignore any given
71 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050072 *
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050075 */
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public static final int DEFAULT_SOUND = 1;
78
79 /**
80 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050081 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070082 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050085 */
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
90 * Use the default notification lights. This will ignore the
91 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
92 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095 */
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500100 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800101 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500102 * Default value: {@link System#currentTimeMillis() Now}.
103 *
104 * Choose a timestamp that will be most relevant to the user. For most finite events, this
105 * corresponds to the time the event happened (or will happen, in the case of events that have
106 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800107 * timestamped according to when the activity began.
108 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800110 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111 * <ul>
112 * <li>Notification of a new chat message should be stamped when the message was received.</li>
113 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
114 * <li>Notification of a completed file download should be stamped when the download finished.</li>
115 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
116 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
117 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800118 * </ul>
119 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 */
121 public long when;
122
123 /**
124 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500125 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 */
127 public int icon;
128
129 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800130 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
131 * leave it at its default value of 0.
132 *
133 * @see android.widget.ImageView#setImageLevel
134 * @see android.graphics.drawable#setLevel
135 */
136 public int iconLevel;
137
138 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 * The number of events that this notification represents. For example, in a new mail
140 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800141 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142 * The system may or may not use this field to modify the appearance of the notification. For
143 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
144 * superimposed over the icon in the status bar. Starting with
145 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
146 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800147 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500148 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
150 public int number;
151
152 /**
153 * The intent to execute when the expanded status entry is clicked. If
154 * this is an activity, it must include the
155 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800156 * that you take care of task management as described in the
157 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800158 * Stack</a> document. In particular, make sure to read the notification section
159 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
160 * Notifications</a> for the correct ways to launch an application from a
161 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
163 public PendingIntent contentIntent;
164
165 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * The intent to execute when the notification is explicitly dismissed by the user, either with
167 * the "Clear All" button or by swiping it away individually.
168 *
169 * This probably shouldn't be launching an activity since several of those will be sent
170 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
172 public PendingIntent deleteIntent;
173
174 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700175 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800176 *
177 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400178 */
179 public PendingIntent fullScreenIntent;
180
181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400183 * the status bar on large and smaller devices.
184 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800185 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 */
187 public CharSequence tickerText;
188
189 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800190 * The view to show as the ticker in the status bar when the notification
191 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400192 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800193 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400194
195 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400196 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 */
198 public RemoteViews contentView;
199
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400200 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400201 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400202 * opportunity to show more detail. The system UI may choose to show this
203 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400204 */
205 public RemoteViews bigContentView;
206
207 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800208 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800210 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
212 /**
213 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500214 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500216 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * </p>
218 */
219 public Uri sound;
220
221 /**
222 * Use this constant as the value for audioStreamType to request that
223 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700224 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 */
226 public static final int STREAM_DEFAULT = -1;
227
228 /**
229 * The audio stream type to use when playing the sound.
230 * Should be one of the STREAM_ constants from
231 * {@link android.media.AudioManager}.
232 */
233 public int audioStreamType = STREAM_DEFAULT;
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500236 * The pattern with which to vibrate.
237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 * <p>
239 * To vibrate the default pattern, see {@link #defaults}.
240 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * @see android.os.Vibrator#vibrate(long[],int)
243 */
244 public long[] vibrate;
245
246 /**
247 * The color of the led. The hardware will do its best approximation.
248 *
249 * @see #FLAG_SHOW_LIGHTS
250 * @see #flags
251 */
252 public int ledARGB;
253
254 /**
255 * The number of milliseconds for the LED to be on while it's flashing.
256 * The hardware will do its best approximation.
257 *
258 * @see #FLAG_SHOW_LIGHTS
259 * @see #flags
260 */
261 public int ledOnMS;
262
263 /**
264 * The number of milliseconds for the LED to be off while it's flashing.
265 * The hardware will do its best approximation.
266 *
267 * @see #FLAG_SHOW_LIGHTS
268 * @see #flags
269 */
270 public int ledOffMS;
271
272 /**
273 * Specifies which values should be taken from the defaults.
274 * <p>
275 * To set, OR the desired from {@link #DEFAULT_SOUND},
276 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
277 * values, use {@link #DEFAULT_ALL}.
278 * </p>
279 */
280 public int defaults;
281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Bit to be bitwise-ored into the {@link #flags} field that should be
284 * set if you want the LED on for this notification.
285 * <ul>
286 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
287 * or 0 for both ledOnMS and ledOffMS.</li>
288 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
289 * <li>To flash the LED, pass the number of milliseconds that it should
290 * be on and off to ledOnMS and ledOffMS.</li>
291 * </ul>
292 * <p>
293 * Since hardware varies, you are not guaranteed that any of the values
294 * you pass are honored exactly. Use the system defaults (TODO) if possible
295 * because they will be set to values that work on any given hardware.
296 * <p>
297 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 */
300 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
301
302 /**
303 * Bit to be bitwise-ored into the {@link #flags} field that should be
304 * set if this notification is in reference to something that is ongoing,
305 * like a phone call. It should not be set if this notification is in
306 * reference to something that happened at a particular point in time,
307 * like a missed phone call.
308 */
309 public static final int FLAG_ONGOING_EVENT = 0x00000002;
310
311 /**
312 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700313 * the audio will be repeated until the notification is
314 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 */
316 public static final int FLAG_INSISTENT = 0x00000004;
317
318 /**
319 * Bit to be bitwise-ored into the {@link #flags} field that should be
320 * set if you want the sound and/or vibration play each time the
321 * notification is sent, even if it has not been canceled before that.
322 */
323 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
324
325 /**
326 * Bit to be bitwise-ored into the {@link #flags} field that should be
327 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500328 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 */
331 public static final int FLAG_AUTO_CANCEL = 0x00000010;
332
333 /**
334 * Bit to be bitwise-ored into the {@link #flags} field that should be
335 * set if the notification should not be canceled when the user clicks
336 * the Clear all button.
337 */
338 public static final int FLAG_NO_CLEAR = 0x00000020;
339
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700340 /**
341 * Bit to be bitwise-ored into the {@link #flags} field that should be
342 * set if this notification represents a currently running service. This
343 * will normally be set for you by {@link Service#startForeground}.
344 */
345 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
346
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400347 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500348 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800349 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500350 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400351 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500352 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 public int flags;
355
Tor Norbyed9273d62013-05-30 15:59:53 -0700356 /** @hide */
357 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
358 @Retention(RetentionPolicy.SOURCE)
359 public @interface Priority {}
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500362 * Default notification {@link #priority}. If your application does not prioritize its own
363 * notifications, use this value for all notifications.
364 */
365 public static final int PRIORITY_DEFAULT = 0;
366
367 /**
368 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
369 * items smaller, or at a different position in the list, compared with your app's
370 * {@link #PRIORITY_DEFAULT} items.
371 */
372 public static final int PRIORITY_LOW = -1;
373
374 /**
375 * Lowest {@link #priority}; these items might not be shown to the user except under special
376 * circumstances, such as detailed notification logs.
377 */
378 public static final int PRIORITY_MIN = -2;
379
380 /**
381 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
382 * show these items larger, or at a different position in notification lists, compared with
383 * your app's {@link #PRIORITY_DEFAULT} items.
384 */
385 public static final int PRIORITY_HIGH = 1;
386
387 /**
388 * Highest {@link #priority}, for your application's most important items that require the
389 * user's prompt attention or input.
390 */
391 public static final int PRIORITY_MAX = 2;
392
393 /**
394 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800395 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500396 * Priority is an indication of how much of the user's valuable attention should be consumed by
397 * this notification. Low-priority notifications may be hidden from the user in certain
398 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500399 * system will make a determination about how to interpret this priority when presenting
400 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500401 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700402 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800404
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600405
406 /**
407 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
408 * the notification's presence and contents in untrusted situations (namely, on the secure
409 * lockscreen).
410 *
411 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
412 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
413 * shown in all situations, but the contents are only available if the device is unlocked for
414 * the appropriate user.
415 *
416 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
417 * can be read even in an "insecure" context (that is, above a secure lockscreen).
418 * To modify the public version of this notification—for example, to redact some portions—see
419 * {@link Builder#setPublicVersion(Notification)}.
420 *
421 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
422 * and ticker until the user has bypassed the lockscreen.
423 */
424 public int visibility;
425
426 public static final int VISIBILITY_PUBLIC = 1;
427 public static final int VISIBILITY_PRIVATE = 0;
428 public static final int VISIBILITY_SECRET = -1;
429
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500430 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400431 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500432 * Notification type: incoming call (voice or video) or similar synchronous communication request.
433 */
434 public static final String KIND_CALL = "android.call";
435
436 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400437 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500438 * Notification type: incoming direct message (SMS, instant message, etc.).
439 */
440 public static final String KIND_MESSAGE = "android.message";
441
442 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400443 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500444 * Notification type: asynchronous bulk message (email).
445 */
446 public static final String KIND_EMAIL = "android.email";
447
448 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400449 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500450 * Notification type: calendar event.
451 */
452 public static final String KIND_EVENT = "android.event";
453
454 /**
Daniel Sandlera90513d2012-06-04 02:11:17 -0400455 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 * Notification type: promotion or advertisement.
457 */
458 public static final String KIND_PROMO = "android.promo";
459
460 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400461 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500462 * If this notification matches of one or more special types (see the <code>KIND_*</code>
463 * constants), add them here, best match first.
464 */
465 public String[] kind;
466
467 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400468 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400469 * <p>
470 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
471 * APIs, and are intended to be used by
472 * {@link android.service.notification.NotificationListenerService} implementations to extract
473 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500474 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400475 public Bundle extras = new Bundle();
476
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400477 /**
478 * {@link #extras} key: this is the title of the notification,
479 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
480 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500481 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400482
483 /**
484 * {@link #extras} key: this is the title of the notification when shown in expanded form,
485 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
486 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400487 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400488
489 /**
490 * {@link #extras} key: this is the main text payload, as supplied to
491 * {@link Builder#setContentText(CharSequence)}.
492 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500493 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400494
495 /**
496 * {@link #extras} key: this is a third line of text, as supplied to
497 * {@link Builder#setSubText(CharSequence)}.
498 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400499 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400500
501 /**
502 * {@link #extras} key: this is a small piece of additional text as supplied to
503 * {@link Builder#setContentInfo(CharSequence)}.
504 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400505 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400506
507 /**
508 * {@link #extras} key: this is a line of summary information intended to be shown
509 * alongside expanded notifications, as supplied to (e.g.)
510 * {@link BigTextStyle#setSummaryText(CharSequence)}.
511 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400512 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400513
514 /**
515 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
516 * supplied to {@link Builder#setSmallIcon(int)}.
517 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500518 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400519
520 /**
521 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
522 * notification payload, as
523 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
524 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400525 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400526
527 /**
528 * {@link #extras} key: this is a bitmap to be used instead of the one from
529 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
530 * shown in its expanded form, as supplied to
531 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
532 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400533 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400534
535 /**
536 * {@link #extras} key: this is the progress value supplied to
537 * {@link Builder#setProgress(int, int, boolean)}.
538 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400539 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400540
541 /**
542 * {@link #extras} key: this is the maximum value supplied to
543 * {@link Builder#setProgress(int, int, boolean)}.
544 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400545 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400546
547 /**
548 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
549 * {@link Builder#setProgress(int, int, boolean)}.
550 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400551 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400552
553 /**
554 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
555 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
556 * {@link Builder#setUsesChronometer(boolean)}.
557 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400558 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400559
560 /**
561 * {@link #extras} key: whether {@link #when} should be shown,
562 * as supplied to {@link Builder#setShowWhen(boolean)}.
563 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400564 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400565
566 /**
567 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
568 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
569 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400570 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400571
572 /**
573 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
574 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
575 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400576 public static final String EXTRA_TEXT_LINES = "android.textLines";
577
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400578 /**
579 * {@link #extras} key: An array of people that this notification relates to, specified
580 * by contacts provider contact URI.
581 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400582 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583
584 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400585 * @hide
586 * Extra added by NotificationManagerService to indicate whether a NotificationScorer
587 * modified the Notifications's score.
588 */
589 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
590
591 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400592 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400593 * @hide
594 */
595 public static final String EXTRA_AS_HEADS_UP = "headsup";
596
597 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400598 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400599 * @hide
600 */
601 public static final int HEADS_UP_NEVER = 0;
602
603 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400604 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400605 * @hide
606 */
607 public static final int HEADS_UP_ALLOWED = 1;
608
609 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400610 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400611 * @hide
612 */
613 public static final int HEADS_UP_REQUESTED = 2;
614
615 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400616 * Structure to encapsulate a named action that can be shown as part of this notification.
617 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
618 * selected by the user.
619 * <p>
620 * Apps should use {@link Builder#addAction(int, CharSequence, PendingIntent)} to create and
621 * attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400622 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500623 public static class Action implements Parcelable {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400624 /**
625 * Small icon representing the action.
626 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400627 public int icon;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400628 /**
629 * Title of the action.
630 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400631 public CharSequence title;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400632 /**
633 * Intent to send when the user invokes this action. May be null, in which case the action
634 * may be rendered in a disabled presentation by the system UI.
635 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400636 public PendingIntent actionIntent;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400637
638 private Action() { }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400639 private Action(Parcel in) {
640 icon = in.readInt();
641 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
642 if (in.readInt() == 1) {
643 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
644 }
645 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400646 /**
647 * Use {@link Builder#addAction(int, CharSequence, PendingIntent)}.
648 */
649 public Action(int icon, CharSequence title, PendingIntent intent) {
650 this.icon = icon;
651 this.title = title;
652 this.actionIntent = intent;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400653 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400654
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400655 @Override
656 public Action clone() {
657 return new Action(
658 this.icon,
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400659 this.title,
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400660 this.actionIntent // safe to alias
661 );
662 }
663 @Override
664 public int describeContents() {
665 return 0;
666 }
667 @Override
668 public void writeToParcel(Parcel out, int flags) {
669 out.writeInt(icon);
670 TextUtils.writeToParcel(title, out, flags);
671 if (actionIntent != null) {
672 out.writeInt(1);
673 actionIntent.writeToParcel(out, flags);
674 } else {
675 out.writeInt(0);
676 }
677 }
678 public static final Parcelable.Creator<Action> CREATOR
679 = new Parcelable.Creator<Action>() {
680 public Action createFromParcel(Parcel in) {
681 return new Action(in);
682 }
683 public Action[] newArray(int size) {
684 return new Action[size];
685 }
686 };
687 }
688
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400689 /**
690 * Array of all {@link Action} structures attached to this notification by
691 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
692 * {@link android.service.notification.NotificationListenerService} that provide an alternative
693 * interface for invoking actions.
694 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500695 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400696
697 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600698 * Replacement version of this notification whose content will be shown
699 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
700 * and {@link #VISIBILITY_PUBLIC}.
701 */
702 public Notification publicVersion;
703
704 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500705 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800706 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 */
708 public Notification()
709 {
710 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500711 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713
714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 * @hide
716 */
717 public Notification(Context context, int icon, CharSequence tickerText, long when,
718 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
719 {
720 this.when = when;
721 this.icon = icon;
722 this.tickerText = tickerText;
723 setLatestEventInfo(context, contentTitle, contentText,
724 PendingIntent.getActivity(context, 0, contentIntent, 0));
725 }
726
727 /**
728 * Constructs a Notification object with the information needed to
729 * have a status bar icon without the standard expanded view.
730 *
731 * @param icon The resource id of the icon to put in the status bar.
732 * @param tickerText The text that flows by in the status bar when the notification first
733 * activates.
734 * @param when The time to show in the time field. In the System.currentTimeMillis
735 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800736 *
737 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800739 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 public Notification(int icon, CharSequence tickerText, long when)
741 {
742 this.icon = icon;
743 this.tickerText = tickerText;
744 this.when = when;
745 }
746
747 /**
748 * Unflatten the notification from a parcel.
749 */
750 public Notification(Parcel parcel)
751 {
752 int version = parcel.readInt();
753
754 when = parcel.readLong();
755 icon = parcel.readInt();
756 number = parcel.readInt();
757 if (parcel.readInt() != 0) {
758 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
759 }
760 if (parcel.readInt() != 0) {
761 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
762 }
763 if (parcel.readInt() != 0) {
764 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
765 }
766 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800767 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400768 }
769 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
771 }
Joe Onorato561d3852010-11-20 18:09:34 -0800772 if (parcel.readInt() != 0) {
773 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 defaults = parcel.readInt();
776 flags = parcel.readInt();
777 if (parcel.readInt() != 0) {
778 sound = Uri.CREATOR.createFromParcel(parcel);
779 }
780
781 audioStreamType = parcel.readInt();
782 vibrate = parcel.createLongArray();
783 ledARGB = parcel.readInt();
784 ledOnMS = parcel.readInt();
785 ledOffMS = parcel.readInt();
786 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400787
788 if (parcel.readInt() != 0) {
789 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
790 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400793
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 kind = parcel.createStringArray(); // may set kind to null
795
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500796 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400797
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500798 actions = parcel.createTypedArray(Action.CREATOR); // may be null
799
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400800 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400801 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
802 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600803
804 visibility = parcel.readInt();
805
806 if (parcel.readInt() != 0) {
807 publicVersion = Notification.CREATOR.createFromParcel(parcel);
808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 }
810
Andy Stadler110988c2010-12-03 14:29:16 -0800811 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700812 public Notification clone() {
813 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -0400814 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500815 return that;
816 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700817
Daniel Sandler1a497d32013-04-18 14:52:45 -0400818 /**
819 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
820 * of this into that.
821 * @hide
822 */
823 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700824 that.when = this.when;
825 that.icon = this.icon;
826 that.number = this.number;
827
828 // PendingIntents are global, so there's no reason (or way) to clone them.
829 that.contentIntent = this.contentIntent;
830 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400831 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700832
833 if (this.tickerText != null) {
834 that.tickerText = this.tickerText.toString();
835 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400836 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800837 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400838 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400839 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700840 that.contentView = this.contentView.clone();
841 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400842 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800843 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
844 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100845 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700846 that.sound = this.sound; // android.net.Uri is immutable
847 that.audioStreamType = this.audioStreamType;
848
849 final long[] vibrate = this.vibrate;
850 if (vibrate != null) {
851 final int N = vibrate.length;
852 final long[] vib = that.vibrate = new long[N];
853 System.arraycopy(vibrate, 0, vib, 0, N);
854 }
855
856 that.ledARGB = this.ledARGB;
857 that.ledOnMS = this.ledOnMS;
858 that.ledOffMS = this.ledOffMS;
859 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500860
Joe Onorato18e69df2010-05-17 22:26:12 -0700861 that.flags = this.flags;
862
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500863 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800864
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500865 final String[] thiskind = this.kind;
866 if (thiskind != null) {
867 final int N = thiskind.length;
868 final String[] thatkind = that.kind = new String[N];
869 System.arraycopy(thiskind, 0, thatkind, 0, N);
870 }
871
872 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400873 try {
874 that.extras = new Bundle(this.extras);
875 // will unparcel
876 that.extras.size();
877 } catch (BadParcelableException e) {
878 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
879 that.extras = null;
880 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500881 }
882
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500883 if (this.actions != null) {
884 that.actions = new Action[this.actions.length];
885 for(int i=0; i<this.actions.length; i++) {
886 that.actions[i] = this.actions[i].clone();
887 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400888 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500889
Daniel Sandler1a497d32013-04-18 14:52:45 -0400890 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400891 that.bigContentView = this.bigContentView.clone();
892 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400893
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600894 that.visibility = this.visibility;
895
896 if (this.publicVersion != null) {
897 that.publicVersion = new Notification();
898 this.publicVersion.cloneInto(that.publicVersion, heavy);
899 }
900
Daniel Sandler1a497d32013-04-18 14:52:45 -0400901 if (!heavy) {
902 that.lightenPayload(); // will clean out extras
903 }
904 }
905
906 /**
907 * Removes heavyweight parts of the Notification object for archival or for sending to
908 * listeners when the full contents are not necessary.
909 * @hide
910 */
911 public final void lightenPayload() {
912 tickerView = null;
913 contentView = null;
914 bigContentView = null;
915 largeIcon = null;
916 if (extras != null) {
917 extras.remove(Notification.EXTRA_LARGE_ICON);
918 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
919 extras.remove(Notification.EXTRA_PICTURE);
920 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700921 }
922
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400923 /**
924 * Make sure this CharSequence is safe to put into a bundle, which basically
925 * means it had better not be some custom Parcelable implementation.
926 * @hide
927 */
928 public static CharSequence safeCharSequence(CharSequence cs) {
929 if (cs instanceof Parcelable) {
930 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
931 + " instance is a custom Parcelable and not allowed in Notification");
932 return cs.toString();
933 }
934
935 return cs;
936 }
937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public int describeContents() {
939 return 0;
940 }
941
942 /**
943 * Flatten this notification from a parcel.
944 */
945 public void writeToParcel(Parcel parcel, int flags)
946 {
947 parcel.writeInt(1);
948
949 parcel.writeLong(when);
950 parcel.writeInt(icon);
951 parcel.writeInt(number);
952 if (contentIntent != null) {
953 parcel.writeInt(1);
954 contentIntent.writeToParcel(parcel, 0);
955 } else {
956 parcel.writeInt(0);
957 }
958 if (deleteIntent != null) {
959 parcel.writeInt(1);
960 deleteIntent.writeToParcel(parcel, 0);
961 } else {
962 parcel.writeInt(0);
963 }
964 if (tickerText != null) {
965 parcel.writeInt(1);
966 TextUtils.writeToParcel(tickerText, parcel, flags);
967 } else {
968 parcel.writeInt(0);
969 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800970 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400971 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800972 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400973 } else {
974 parcel.writeInt(0);
975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 if (contentView != null) {
977 parcel.writeInt(1);
978 contentView.writeToParcel(parcel, 0);
979 } else {
980 parcel.writeInt(0);
981 }
Joe Onorato561d3852010-11-20 18:09:34 -0800982 if (largeIcon != null) {
983 parcel.writeInt(1);
984 largeIcon.writeToParcel(parcel, 0);
985 } else {
986 parcel.writeInt(0);
987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988
989 parcel.writeInt(defaults);
990 parcel.writeInt(this.flags);
991
992 if (sound != null) {
993 parcel.writeInt(1);
994 sound.writeToParcel(parcel, 0);
995 } else {
996 parcel.writeInt(0);
997 }
998 parcel.writeInt(audioStreamType);
999 parcel.writeLongArray(vibrate);
1000 parcel.writeInt(ledARGB);
1001 parcel.writeInt(ledOnMS);
1002 parcel.writeInt(ledOffMS);
1003 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001004
1005 if (fullScreenIntent != null) {
1006 parcel.writeInt(1);
1007 fullScreenIntent.writeToParcel(parcel, 0);
1008 } else {
1009 parcel.writeInt(0);
1010 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001011
1012 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001013
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001014 parcel.writeStringArray(kind); // ok for null
Joe Malin8d40d042012-11-05 11:36:40 -08001015
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001016 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001017
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001018 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001019
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001020 if (bigContentView != null) {
1021 parcel.writeInt(1);
1022 bigContentView.writeToParcel(parcel, 0);
1023 } else {
1024 parcel.writeInt(0);
1025 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001026
1027 parcel.writeInt(visibility);
1028
1029 if (publicVersion != null) {
1030 parcel.writeInt(1);
1031 publicVersion.writeToParcel(parcel, 0);
1032 } else {
1033 parcel.writeInt(0);
1034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 }
1036
1037 /**
1038 * Parcelable.Creator that instantiates Notification objects
1039 */
1040 public static final Parcelable.Creator<Notification> CREATOR
1041 = new Parcelable.Creator<Notification>()
1042 {
1043 public Notification createFromParcel(Parcel parcel)
1044 {
1045 return new Notification(parcel);
1046 }
1047
1048 public Notification[] newArray(int size)
1049 {
1050 return new Notification[size];
1051 }
1052 };
1053
1054 /**
1055 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1056 * layout.
1057 *
1058 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1059 * in the view.</p>
1060 * @param context The context for your application / activity.
1061 * @param contentTitle The title that goes in the expanded entry.
1062 * @param contentText The text that goes in the expanded entry.
1063 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1064 * If this is an activity, it must include the
1065 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001066 * that you take care of task management as described in the
1067 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1068 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001069 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001070 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001072 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 public void setLatestEventInfo(Context context,
1074 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001075 Notification.Builder builder = new Notification.Builder(context);
1076
1077 // First, ensure that key pieces of information that may have been set directly
1078 // are preserved
1079 builder.setWhen(this.when);
1080 builder.setSmallIcon(this.icon);
1081 builder.setPriority(this.priority);
1082 builder.setTicker(this.tickerText);
1083 builder.setNumber(this.number);
1084 builder.mFlags = this.flags;
1085 builder.setSound(this.sound, this.audioStreamType);
1086 builder.setDefaults(this.defaults);
1087 builder.setVibrate(this.vibrate);
1088
1089 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001091 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001094 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001096 builder.setContentIntent(contentIntent);
1097 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099
1100 @Override
1101 public String toString() {
1102 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001103 sb.append("Notification(pri=");
1104 sb.append(priority);
1105 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001106 if (contentView != null) {
1107 sb.append(contentView.getPackage());
1108 sb.append("/0x");
1109 sb.append(Integer.toHexString(contentView.getLayoutId()));
1110 } else {
1111 sb.append("null");
1112 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001113 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001114 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001115 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1116 sb.append("default");
1117 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 int N = this.vibrate.length-1;
1119 sb.append("[");
1120 for (int i=0; i<N; i++) {
1121 sb.append(this.vibrate[i]);
1122 sb.append(',');
1123 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001124 if (N != -1) {
1125 sb.append(this.vibrate[N]);
1126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 } else {
1129 sb.append("null");
1130 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001131 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001132 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001134 } else if (this.sound != null) {
1135 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 } else {
1137 sb.append("null");
1138 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001139 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001141 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001142 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001143 sb.append(" kind=[");
1144 if (this.kind == null) {
1145 sb.append("null");
1146 } else {
1147 for (int i=0; i<this.kind.length; i++) {
1148 if (i>0) sb.append(",");
1149 sb.append(this.kind[i]);
1150 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001151 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001152 sb.append("]");
1153 if (actions != null) {
1154 sb.append(" ");
1155 sb.append(actions.length);
1156 sb.append(" action");
1157 if (actions.length > 1) sb.append("s");
1158 }
1159 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 return sb.toString();
1161 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001162
Jeff Sharkey6d515712012-09-20 16:06:08 -07001163 /** {@hide} */
1164 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001165 if (user.getIdentifier() == UserHandle.USER_ALL) {
1166 user = UserHandle.OWNER;
1167 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001168 if (tickerView != null) {
1169 tickerView.setUser(user);
1170 }
1171 if (contentView != null) {
1172 contentView.setUser(user);
1173 }
1174 if (bigContentView != null) {
1175 bigContentView.setUser(user);
1176 }
1177 }
1178
Joe Onoratocb109a02011-01-18 17:57:41 -08001179 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001180 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001181 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001182 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001183 * content views using the platform's notification layout template. If your app supports
1184 * versions of Android as old as API level 4, you can instead use
1185 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1186 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1187 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001188 *
Scott Main183bf112012-08-13 19:12:13 -07001189 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001190 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001191 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001192 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001193 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1194 * .setContentText(subject)
1195 * .setSmallIcon(R.drawable.new_mail)
1196 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001197 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001198 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001199 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001200 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001201 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001202
Joe Onorato46439ce2010-11-19 13:56:21 -08001203 private Context mContext;
1204
1205 private long mWhen;
1206 private int mSmallIcon;
1207 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001208 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001209 private CharSequence mContentTitle;
1210 private CharSequence mContentText;
1211 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001212 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001213 private PendingIntent mContentIntent;
1214 private RemoteViews mContentView;
1215 private PendingIntent mDeleteIntent;
1216 private PendingIntent mFullScreenIntent;
1217 private CharSequence mTickerText;
1218 private RemoteViews mTickerView;
1219 private Bitmap mLargeIcon;
1220 private Uri mSound;
1221 private int mAudioStreamType;
1222 private long[] mVibrate;
1223 private int mLedArgb;
1224 private int mLedOnMs;
1225 private int mLedOffMs;
1226 private int mDefaults;
1227 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001228 private int mProgressMax;
1229 private int mProgress;
1230 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001231 private ArrayList<String> mKindList = new ArrayList<String>(1);
1232 private Bundle mExtras;
1233 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001234 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001235 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001236 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001237 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001238 private int mVisibility = VISIBILITY_PRIVATE;
1239 private Notification mPublicVersion = null;
Joe Onorato46439ce2010-11-19 13:56:21 -08001240
Joe Onoratocb109a02011-01-18 17:57:41 -08001241 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001242 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001243 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001244
1245 * <table>
1246 * <tr><th align=right>priority</th>
1247 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1248 * <tr><th align=right>when</th>
1249 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1250 * <tr><th align=right>audio stream</th>
1251 * <td>{@link #STREAM_DEFAULT}</td></tr>
1252 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001253 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001254
1255 * @param context
1256 * A {@link Context} that will be used by the Builder to construct the
1257 * RemoteViews. The Context will not be held past the lifetime of this Builder
1258 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001259 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001260 public Builder(Context context) {
1261 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001262
1263 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001264 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001265 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001266 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001267 }
1268
Joe Onoratocb109a02011-01-18 17:57:41 -08001269 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001270 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001271 * It will be shown in the notification content view by default; use
1272 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001273 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001274 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001275 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001276 public Builder setWhen(long when) {
1277 mWhen = when;
1278 return this;
1279 }
1280
Joe Onoratocb109a02011-01-18 17:57:41 -08001281 /**
Daniel Sandler0c890492012-09-12 17:23:10 -07001282 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown
1283 * in the content view.
1284 */
1285 public Builder setShowWhen(boolean show) {
1286 mShowWhen = show;
1287 return this;
1288 }
1289
1290 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001291 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001292 *
1293 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001294 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001295 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001296 * Useful when showing an elapsed time (like an ongoing phone call).
1297 *
1298 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001299 * @see Notification#when
1300 */
1301 public Builder setUsesChronometer(boolean b) {
1302 mUseChronometer = b;
1303 return this;
1304 }
1305
1306 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001307 * Set the small icon resource, which will be used to represent the notification in the
1308 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001309 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001310
1311 * The platform template for the expanded view will draw this icon in the left, unless a
1312 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1313 * icon will be moved to the right-hand side.
1314 *
1315
1316 * @param icon
1317 * A resource ID in the application's package of the drawable to use.
1318 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001319 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001320 public Builder setSmallIcon(int icon) {
1321 mSmallIcon = icon;
1322 return this;
1323 }
1324
Joe Onoratocb109a02011-01-18 17:57:41 -08001325 /**
1326 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1327 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1328 * LevelListDrawable}.
1329 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001330 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001331 * @param level The level to use for the icon.
1332 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001333 * @see Notification#icon
1334 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001335 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001336 public Builder setSmallIcon(int icon, int level) {
1337 mSmallIcon = icon;
1338 mSmallIconLevel = level;
1339 return this;
1340 }
1341
Joe Onoratocb109a02011-01-18 17:57:41 -08001342 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001343 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001344 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001345 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001346 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001347 return this;
1348 }
1349
Joe Onoratocb109a02011-01-18 17:57:41 -08001350 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001351 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001352 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001353 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001354 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001355 return this;
1356 }
1357
Joe Onoratocb109a02011-01-18 17:57:41 -08001358 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001359 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001360 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1361 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001362 */
1363 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001364 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001365 return this;
1366 }
1367
1368 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001369 * Set the large number at the right-hand side of the notification. This is
1370 * equivalent to setContentInfo, although it might show the number in a different
1371 * font size for readability.
1372 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001373 public Builder setNumber(int number) {
1374 mNumber = number;
1375 return this;
1376 }
1377
Joe Onoratocb109a02011-01-18 17:57:41 -08001378 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001379 * A small piece of additional information pertaining to this notification.
1380 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001381 * The platform template will draw this on the last line of the notification, at the far
1382 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001383 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001384 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001385 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001386 return this;
1387 }
1388
Joe Onoratocb109a02011-01-18 17:57:41 -08001389 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001390 * Set the progress this notification represents.
1391 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001392 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001393 */
1394 public Builder setProgress(int max, int progress, boolean indeterminate) {
1395 mProgressMax = max;
1396 mProgress = progress;
1397 mProgressIndeterminate = indeterminate;
1398 return this;
1399 }
1400
1401 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001402 * Supply a custom RemoteViews to use instead of the platform template.
1403 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001404 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001405 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001406 public Builder setContent(RemoteViews views) {
1407 mContentView = views;
1408 return this;
1409 }
1410
Joe Onoratocb109a02011-01-18 17:57:41 -08001411 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001412 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1413 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001414 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1415 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1416 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001417 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001418 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001419 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001420 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001421 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001422 public Builder setContentIntent(PendingIntent intent) {
1423 mContentIntent = intent;
1424 return this;
1425 }
1426
Joe Onoratocb109a02011-01-18 17:57:41 -08001427 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001428 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1429 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001430 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001431 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001432 public Builder setDeleteIntent(PendingIntent intent) {
1433 mDeleteIntent = intent;
1434 return this;
1435 }
1436
Joe Onoratocb109a02011-01-18 17:57:41 -08001437 /**
1438 * An intent to launch instead of posting the notification to the status bar.
1439 * Only for use with extremely high-priority notifications demanding the user's
1440 * <strong>immediate</strong> attention, such as an incoming phone call or
1441 * alarm clock that the user has explicitly set to a particular time.
1442 * If this facility is used for something else, please give the user an option
1443 * to turn it off and use a normal notification, as this can be extremely
1444 * disruptive.
1445 *
1446 * @param intent The pending intent to launch.
1447 * @param highPriority Passing true will cause this notification to be sent
1448 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001449 *
1450 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001451 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001452 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1453 mFullScreenIntent = intent;
1454 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1455 return this;
1456 }
1457
Joe Onoratocb109a02011-01-18 17:57:41 -08001458 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001459 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001460 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001461 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001462 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001463 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001464 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001465 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001466 return this;
1467 }
1468
Joe Onoratocb109a02011-01-18 17:57:41 -08001469 /**
1470 * Set the text that is displayed in the status bar when the notification first
1471 * arrives, and also a RemoteViews object that may be displayed instead on some
1472 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001473 *
1474 * @see Notification#tickerText
1475 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001476 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001477 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001478 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001479 mTickerView = views;
1480 return this;
1481 }
1482
Joe Onoratocb109a02011-01-18 17:57:41 -08001483 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001484 * Add a large icon to the notification (and the ticker on some devices).
1485 *
1486 * In the platform template, this image will be shown on the left of the notification view
1487 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1488 *
1489 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001490 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001491 public Builder setLargeIcon(Bitmap icon) {
1492 mLargeIcon = icon;
1493 return this;
1494 }
1495
Joe Onoratocb109a02011-01-18 17:57:41 -08001496 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001497 * Set the sound to play.
1498 *
1499 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1500 *
1501 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001502 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001503 public Builder setSound(Uri sound) {
1504 mSound = sound;
1505 mAudioStreamType = STREAM_DEFAULT;
1506 return this;
1507 }
1508
Joe Onoratocb109a02011-01-18 17:57:41 -08001509 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001510 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001511 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001512 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1513 *
1514 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001515 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001516 public Builder setSound(Uri sound, int streamType) {
1517 mSound = sound;
1518 mAudioStreamType = streamType;
1519 return this;
1520 }
1521
Joe Onoratocb109a02011-01-18 17:57:41 -08001522 /**
1523 * Set the vibration pattern to use.
1524 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001525
1526 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1527 * <code>pattern</code> parameter.
1528 *
1529
1530 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001531 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001532 public Builder setVibrate(long[] pattern) {
1533 mVibrate = pattern;
1534 return this;
1535 }
1536
Joe Onoratocb109a02011-01-18 17:57:41 -08001537 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001538 * Set the desired color for the indicator LED on the device, as well as the
1539 * blink duty cycle (specified in milliseconds).
1540 *
1541
1542 * Not all devices will honor all (or even any) of these values.
1543 *
1544
1545 * @see Notification#ledARGB
1546 * @see Notification#ledOnMS
1547 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001548 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001549 public Builder setLights(int argb, int onMs, int offMs) {
1550 mLedArgb = argb;
1551 mLedOnMs = onMs;
1552 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001553 return this;
1554 }
1555
Joe Onoratocb109a02011-01-18 17:57:41 -08001556 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001557 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001558 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001559
1560 * Ongoing notifications cannot be dismissed by the user, so your application or service
1561 * must take care of canceling them.
1562 *
1563
1564 * They are typically used to indicate a background task that the user is actively engaged
1565 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1566 * (e.g., a file download, sync operation, active network connection).
1567 *
1568
1569 * @see Notification#FLAG_ONGOING_EVENT
1570 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001571 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001572 public Builder setOngoing(boolean ongoing) {
1573 setFlag(FLAG_ONGOING_EVENT, ongoing);
1574 return this;
1575 }
1576
Joe Onoratocb109a02011-01-18 17:57:41 -08001577 /**
1578 * Set this flag if you would only like the sound, vibrate
1579 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001580 *
1581 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001582 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001583 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1584 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1585 return this;
1586 }
1587
Joe Onoratocb109a02011-01-18 17:57:41 -08001588 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001589 * Make this notification automatically dismissed when the user touches it. The
1590 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1591 *
1592 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001593 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001594 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001595 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001596 return this;
1597 }
1598
Joe Onoratocb109a02011-01-18 17:57:41 -08001599 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001600 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001601 * <p>
1602 * The value should be one or more of the following fields combined with
1603 * bitwise-or:
1604 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1605 * <p>
1606 * For all default values, use {@link #DEFAULT_ALL}.
1607 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001608 public Builder setDefaults(int defaults) {
1609 mDefaults = defaults;
1610 return this;
1611 }
1612
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001613 /**
1614 * Set the priority of this notification.
1615 *
1616 * @see Notification#priority
1617 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001618 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001619 mPriority = pri;
1620 return this;
1621 }
Joe Malin8d40d042012-11-05 11:36:40 -08001622
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001623 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001624 * @hide
Joe Malin8d40d042012-11-05 11:36:40 -08001625 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001626 * Add a kind (category) to this notification. Optional.
Joe Malin8d40d042012-11-05 11:36:40 -08001627 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001628 * @see Notification#kind
1629 */
1630 public Builder addKind(String k) {
1631 mKindList.add(k);
1632 return this;
1633 }
1634
1635 /**
1636 * Add metadata to this notification.
1637 *
1638 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001639 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001640 * called.
1641 *
1642 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001643 */
1644 public Builder setExtras(Bundle bag) {
1645 mExtras = bag;
1646 return this;
1647 }
1648
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001649 /**
1650 * Add an action to this notification. Actions are typically displayed by
1651 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001652 * <p>
1653 * Every action must have an icon (32dp square and matching the
1654 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
1655 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
1656 * <p>
1657 * A notification in its expanded form can display up to 3 actions, from left to right in
1658 * the order they were added. Actions will not be displayed when the notification is
1659 * collapsed, however, so be sure that any essential functions may be accessed by the user
1660 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001661 *
1662 * @param icon Resource ID of a drawable that represents the action.
1663 * @param title Text describing the action.
1664 * @param intent PendingIntent to be fired when the action is invoked.
1665 */
1666 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001667 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001668 return this;
1669 }
1670
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001671 /**
1672 * Add a rich notification style to be applied at build time.
1673 *
1674 * @param style Object responsible for modifying the notification style.
1675 */
1676 public Builder setStyle(Style style) {
1677 if (mStyle != style) {
1678 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001679 if (mStyle != null) {
1680 mStyle.setBuilder(this);
1681 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001682 }
1683 return this;
1684 }
1685
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001686 /**
1687 * Specify the value of {@link #visibility}.
1688
1689 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
1690 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
1691 *
1692 * @return The same Builder.
1693 */
1694 public Builder setVisibility(int visibility) {
1695 mVisibility = visibility;
1696 return this;
1697 }
1698
1699 /**
1700 * Supply a replacement Notification whose contents should be shown in insecure contexts
1701 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
1702 * @param n A replacement notification, presumably with some or all info redacted.
1703 * @return The same Builder.
1704 */
1705 public Builder setPublicVersion(Notification n) {
1706 mPublicVersion = n;
1707 return this;
1708 }
1709
Joe Onorato46439ce2010-11-19 13:56:21 -08001710 private void setFlag(int mask, boolean value) {
1711 if (value) {
1712 mFlags |= mask;
1713 } else {
1714 mFlags &= ~mask;
1715 }
1716 }
1717
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001718 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08001719 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001720 boolean showLine3 = false;
1721 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001722 int smallIconImageViewId = R.id.icon;
1723 if (mLargeIcon != null) {
1724 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1725 smallIconImageViewId = R.id.right_icon;
1726 }
Daniel Sandlere95658c2012-05-10 00:33:54 -04001727 if (mPriority < PRIORITY_LOW) {
1728 contentView.setInt(R.id.icon,
1729 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1730 contentView.setInt(R.id.status_bar_latest_event_content,
1731 "setBackgroundResource", R.drawable.notification_bg_low);
1732 }
Joe Onorato561d3852010-11-20 18:09:34 -08001733 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001734 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1735 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001736 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001737 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001738 }
1739 if (mContentTitle != null) {
1740 contentView.setTextViewText(R.id.title, mContentTitle);
1741 }
1742 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001743 contentView.setTextViewText(R.id.text, mContentText);
1744 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001745 }
1746 if (mContentInfo != null) {
1747 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001748 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001749 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001750 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001751 final int tooBig = mContext.getResources().getInteger(
1752 R.integer.status_bar_notification_info_maxnum);
1753 if (mNumber > tooBig) {
1754 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1755 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001756 } else {
1757 NumberFormat f = NumberFormat.getIntegerInstance();
1758 contentView.setTextViewText(R.id.info, f.format(mNumber));
1759 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001760 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001761 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001762 } else {
1763 contentView.setViewVisibility(R.id.info, View.GONE);
1764 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001765
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001766 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001767 if (mSubText != null) {
1768 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001769 if (mContentText != null) {
1770 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001771 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
1772 showLine2 = true;
1773 } else {
1774 contentView.setViewVisibility(R.id.text2, View.GONE);
1775 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001776 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001777 contentView.setViewVisibility(R.id.text2, View.GONE);
1778 if (mProgressMax != 0 || mProgressIndeterminate) {
1779 contentView.setProgressBar(
1780 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1781 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001782 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001783 } else {
1784 contentView.setViewVisibility(R.id.progress, View.GONE);
1785 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001786 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001787 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001788 if (fitIn1U) {
1789 // need to shrink all the type to make sure everything fits
1790 final Resources res = mContext.getResources();
1791 final float subTextSize = res.getDimensionPixelSize(
1792 R.dimen.notification_subtext_size);
1793 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
1794 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001795 // vertical centering
1796 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1797 }
1798
Daniel Sandler0c890492012-09-12 17:23:10 -07001799 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001800 if (mUseChronometer) {
1801 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1802 contentView.setLong(R.id.chronometer, "setBase",
1803 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1804 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1805 } else {
1806 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1807 contentView.setLong(R.id.time, "setTime", mWhen);
1808 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001809 } else {
1810 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001811 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001812
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001813 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001814 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001815 return contentView;
1816 }
1817
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001818 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001819 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001820
1821 int N = mActions.size();
1822 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001823 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001824 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001825 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001826 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04001827 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001828 for (int i=0; i<N; i++) {
1829 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001830 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001831 big.addView(R.id.actions, button);
1832 }
1833 }
1834 return big;
1835 }
1836
Joe Onorato46439ce2010-11-19 13:56:21 -08001837 private RemoteViews makeContentView() {
1838 if (mContentView != null) {
1839 return mContentView;
1840 } else {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001841 return applyStandardTemplate(R.layout.notification_template_base, true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001842 }
1843 }
1844
1845 private RemoteViews makeTickerView() {
1846 if (mTickerView != null) {
1847 return mTickerView;
1848 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001849 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001850 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001851 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001852 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08001853 } else {
1854 return null;
1855 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001856 }
1857 }
1858
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001859 private RemoteViews makeBigContentView() {
1860 if (mActions.size() == 0) return null;
1861
Chris Wrenb023bf82012-04-23 16:05:42 -04001862 return applyStandardTemplateWithActions(R.layout.notification_template_big_base);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001863 }
1864
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001865 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04001866 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08001867 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Daniel Sandler8680bf82012-05-15 16:52:52 -04001868 tombstone ? R.layout.notification_action_tombstone
1869 : R.layout.notification_action);
Chris Wren8749ac8a2013-12-03 14:31:01 -05001870 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001871 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001872 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04001873 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04001874 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001875 button.setContentDescription(R.id.action0, action.title);
1876 return button;
1877 }
1878
Joe Onoratocb109a02011-01-18 17:57:41 -08001879 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001880 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001881 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08001882 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001883 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08001884 Notification n = new Notification();
1885 n.when = mWhen;
1886 n.icon = mSmallIcon;
1887 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001888 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001889 n.contentView = makeContentView();
1890 n.contentIntent = mContentIntent;
1891 n.deleteIntent = mDeleteIntent;
1892 n.fullScreenIntent = mFullScreenIntent;
1893 n.tickerText = mTickerText;
1894 n.tickerView = makeTickerView();
1895 n.largeIcon = mLargeIcon;
1896 n.sound = mSound;
1897 n.audioStreamType = mAudioStreamType;
1898 n.vibrate = mVibrate;
1899 n.ledARGB = mLedArgb;
1900 n.ledOnMS = mLedOnMs;
1901 n.ledOffMS = mLedOffMs;
1902 n.defaults = mDefaults;
1903 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001904 n.bigContentView = makeBigContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04001905 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08001906 n.flags |= FLAG_SHOW_LIGHTS;
1907 }
1908 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1909 n.flags |= FLAG_SHOW_LIGHTS;
1910 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001911 if (mKindList.size() > 0) {
1912 n.kind = new String[mKindList.size()];
1913 mKindList.toArray(n.kind);
1914 } else {
1915 n.kind = null;
1916 }
1917 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001918 if (mActions.size() > 0) {
1919 n.actions = new Action[mActions.size()];
1920 mActions.toArray(n.actions);
1921 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001922 n.visibility = mVisibility;
1923
1924 if (mPublicVersion != null) {
1925 n.publicVersion = new Notification();
1926 mPublicVersion.cloneInto(n.publicVersion, true);
1927 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001928
Joe Onorato46439ce2010-11-19 13:56:21 -08001929 return n;
1930 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001931
1932 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001933 * Capture, in the provided bundle, semantic information used in the construction of
1934 * this Notification object.
1935 * @hide
1936 */
1937 public void addExtras(Bundle extras) {
1938 // Store original information used in the construction of this object
1939 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
1940 extras.putCharSequence(EXTRA_TEXT, mContentText);
1941 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
1942 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
1943 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
1944 extras.putInt(EXTRA_PROGRESS, mProgress);
1945 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
1946 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
1947 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
1948 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04001949 if (mLargeIcon != null) {
1950 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
1951 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001952 }
1953
1954 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001955 * @deprecated Use {@link #build()} instead.
1956 */
1957 @Deprecated
1958 public Notification getNotification() {
1959 return build();
1960 }
1961
1962 /**
1963 * Combine all of the options that have been set and return a new {@link Notification}
1964 * object.
1965 */
1966 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001967 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001968
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001969 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001970 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001971 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001972
1973 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
1974
1975 addExtras(n.extras);
1976 if (mStyle != null) {
1977 mStyle.addExtras(n.extras);
1978 }
1979
1980 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001981 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001982
1983 /**
1984 * Apply this Builder to an existing {@link Notification} object.
1985 *
1986 * @hide
1987 */
1988 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04001989 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001990 return n;
1991 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001992 }
1993
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001994 /**
1995 * An object that can apply a rich notification style to a {@link Notification.Builder}
1996 * object.
1997 */
Chris Wrend6297db2012-05-03 16:20:13 -04001998 public static abstract class Style
1999 {
2000 private CharSequence mBigContentTitle;
2001 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002002 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002003
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002004 protected Builder mBuilder;
2005
Chris Wrend6297db2012-05-03 16:20:13 -04002006 /**
2007 * Overrides ContentTitle in the big form of the template.
2008 * This defaults to the value passed to setContentTitle().
2009 */
2010 protected void internalSetBigContentTitle(CharSequence title) {
2011 mBigContentTitle = title;
2012 }
2013
2014 /**
2015 * Set the first line of text after the detail section in the big form of the template.
2016 */
2017 protected void internalSetSummaryText(CharSequence cs) {
2018 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002019 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002020 }
2021
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002022 public void setBuilder(Builder builder) {
2023 if (mBuilder != builder) {
2024 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002025 if (mBuilder != null) {
2026 mBuilder.setStyle(this);
2027 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002028 }
2029 }
2030
Chris Wrend6297db2012-05-03 16:20:13 -04002031 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002032 if (mBuilder == null) {
2033 throw new IllegalArgumentException("Style requires a valid Builder object");
2034 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002035 }
Chris Wrend6297db2012-05-03 16:20:13 -04002036
2037 protected RemoteViews getStandardView(int layoutId) {
2038 checkBuilder();
2039
2040 if (mBigContentTitle != null) {
2041 mBuilder.setContentTitle(mBigContentTitle);
2042 }
2043
Chris Wrend6297db2012-05-03 16:20:13 -04002044 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2045
Chris Wrend6297db2012-05-03 16:20:13 -04002046 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2047 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002048 } else {
2049 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002050 }
2051
Daniel Sandler619738c2012-06-07 16:33:08 -04002052 // The last line defaults to the subtext, but can be replaced by mSummaryText
2053 final CharSequence overflowText =
2054 mSummaryTextSet ? mSummaryText
2055 : mBuilder.mSubText;
2056 if (overflowText != null) {
2057 contentView.setTextViewText(R.id.text, overflowText);
2058 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002059 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002060 } else {
2061 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2062 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002063 }
2064
2065 return contentView;
2066 }
2067
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002068 /**
2069 * @hide
2070 */
2071 public void addExtras(Bundle extras) {
2072 if (mSummaryTextSet) {
2073 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2074 }
2075 if (mBigContentTitle != null) {
2076 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2077 }
2078 }
2079
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002080 /**
2081 * @hide
2082 */
2083 public abstract Notification buildStyled(Notification wip);
2084
2085 /**
2086 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2087 * attached to.
2088 *
2089 * @return the fully constructed Notification.
2090 */
2091 public Notification build() {
2092 checkBuilder();
2093 return mBuilder.build();
2094 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002095 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002096
2097 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002098 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002099 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002100 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002101 * <pre class="prettyprint">
2102 * Notification noti = new Notification.BigPictureStyle(
2103 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002104 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002105 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002106 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002107 * .setLargeIcon(aBitmap))
2108 * .bigPicture(aBigBitmap)
2109 * .build();
2110 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002111 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002112 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002113 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002114 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002115 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002116 private Bitmap mBigLargeIcon;
2117 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002118
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002119 public BigPictureStyle() {
2120 }
2121
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002122 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002123 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002124 }
2125
Chris Wrend6297db2012-05-03 16:20:13 -04002126 /**
2127 * Overrides ContentTitle in the big form of the template.
2128 * This defaults to the value passed to setContentTitle().
2129 */
2130 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002131 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002132 return this;
2133 }
2134
2135 /**
2136 * Set the first line of text after the detail section in the big form of the template.
2137 */
2138 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002139 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002140 return this;
2141 }
2142
Chris Wren0bd664d2012-08-01 13:56:56 -04002143 /**
2144 * Provide the bitmap to be used as the payload for the BigPicture notification.
2145 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002146 public BigPictureStyle bigPicture(Bitmap b) {
2147 mPicture = b;
2148 return this;
2149 }
2150
Chris Wren3745a3d2012-05-22 15:11:52 -04002151 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002152 * Override the large icon when the big notification is shown.
2153 */
2154 public BigPictureStyle bigLargeIcon(Bitmap b) {
2155 mBigLargeIconSet = true;
2156 mBigLargeIcon = b;
2157 return this;
2158 }
2159
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002160 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04002161 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002162
2163 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2164
2165 return contentView;
2166 }
2167
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002168 /**
2169 * @hide
2170 */
2171 public void addExtras(Bundle extras) {
2172 super.addExtras(extras);
2173
2174 if (mBigLargeIconSet) {
2175 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2176 }
2177 extras.putParcelable(EXTRA_PICTURE, mPicture);
2178 }
2179
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002180 /**
2181 * @hide
2182 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002183 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002184 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002185 if (mBigLargeIconSet ) {
2186 mBuilder.mLargeIcon = mBigLargeIcon;
2187 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002188 wip.bigContentView = makeBigContentView();
2189 return wip;
2190 }
2191 }
2192
2193 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002194 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002195 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002196 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002197 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05002198 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002199 * new Notification.Builder()
2200 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2201 * .setContentText(subject)
2202 * .setSmallIcon(R.drawable.new_mail)
2203 * .setLargeIcon(aBitmap))
2204 * .bigText(aVeryLongString)
2205 * .build();
2206 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002207 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002208 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002209 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002210 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002211 private CharSequence mBigText;
2212
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002213 public BigTextStyle() {
2214 }
2215
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002216 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002217 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002218 }
2219
Chris Wrend6297db2012-05-03 16:20:13 -04002220 /**
2221 * Overrides ContentTitle in the big form of the template.
2222 * This defaults to the value passed to setContentTitle().
2223 */
2224 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002225 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002226 return this;
2227 }
2228
2229 /**
2230 * Set the first line of text after the detail section in the big form of the template.
2231 */
2232 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002233 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002234 return this;
2235 }
2236
Chris Wren0bd664d2012-08-01 13:56:56 -04002237 /**
2238 * Provide the longer text to be displayed in the big form of the
2239 * template in place of the content text.
2240 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002241 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002242 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002243 return this;
2244 }
2245
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002246 /**
2247 * @hide
2248 */
2249 public void addExtras(Bundle extras) {
2250 super.addExtras(extras);
2251
2252 extras.putCharSequence(EXTRA_TEXT, mBigText);
2253 }
2254
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002255 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002256 // Remove the content text so line3 only shows if you have a summary
2257 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002258 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04002259
Chris Wrend6297db2012-05-03 16:20:13 -04002260 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text);
Joe Malin8d40d042012-11-05 11:36:40 -08002261
Daniel Sandler619738c2012-06-07 16:33:08 -04002262 if (hadThreeLines) {
2263 // vertical centering
2264 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2265 }
2266
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002267 contentView.setTextViewText(R.id.big_text, mBigText);
2268 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04002269 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002270
2271 return contentView;
2272 }
2273
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002274 /**
2275 * @hide
2276 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002277 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002278 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002279 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002280
2281 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
2282
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002283 return wip;
2284 }
2285 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04002286
2287 /**
2288 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08002289 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002290 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
2291 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002292 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04002293 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04002294 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04002295 * .setContentText(subject)
2296 * .setSmallIcon(R.drawable.new_mail)
2297 * .setLargeIcon(aBitmap))
2298 * .addLine(str1)
2299 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04002300 * .setContentTitle("")
2301 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04002302 * .build();
2303 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002304 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002305 * @see Notification#bigContentView
2306 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002307 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002308 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
2309
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002310 public InboxStyle() {
2311 }
2312
Daniel Sandler879c5e02012-04-17 16:46:51 -04002313 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002314 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002315 }
2316
Chris Wrend6297db2012-05-03 16:20:13 -04002317 /**
2318 * Overrides ContentTitle in the big form of the template.
2319 * This defaults to the value passed to setContentTitle().
2320 */
2321 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002322 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002323 return this;
2324 }
2325
2326 /**
2327 * Set the first line of text after the detail section in the big form of the template.
2328 */
2329 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002330 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002331 return this;
2332 }
2333
Chris Wren0bd664d2012-08-01 13:56:56 -04002334 /**
2335 * Append a line to the digest section of the Inbox notification.
2336 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04002337 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002338 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04002339 return this;
2340 }
2341
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002342 /**
2343 * @hide
2344 */
2345 public void addExtras(Bundle extras) {
2346 super.addExtras(extras);
2347 CharSequence[] a = new CharSequence[mTexts.size()];
2348 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
2349 }
2350
Daniel Sandler879c5e02012-04-17 16:46:51 -04002351 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002352 // Remove the content text so line3 disappears unless you have a summary
2353 mBuilder.mContentText = null;
Chris Wrend6297db2012-05-03 16:20:13 -04002354 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox);
Daniel Sandler619738c2012-06-07 16:33:08 -04002355
Chris Wrend6297db2012-05-03 16:20:13 -04002356 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002357
Chris Wrend6297db2012-05-03 16:20:13 -04002358 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 -04002359 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04002360
Chris Wren4ed80d52012-05-17 09:30:03 -04002361 // Make sure all rows are gone in case we reuse a view.
2362 for (int rowId : rowIds) {
2363 contentView.setViewVisibility(rowId, View.GONE);
2364 }
2365
Chris Wren683ab002012-09-20 10:35:54 -04002366
Daniel Sandler879c5e02012-04-17 16:46:51 -04002367 int i=0;
2368 while (i < mTexts.size() && i < rowIds.length) {
2369 CharSequence str = mTexts.get(i);
2370 if (str != null && !str.equals("")) {
2371 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
2372 contentView.setTextViewText(rowIds[i], str);
2373 }
2374 i++;
2375 }
2376
Chris Wren683ab002012-09-20 10:35:54 -04002377 contentView.setViewVisibility(R.id.inbox_end_pad,
2378 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
2379
2380 contentView.setViewVisibility(R.id.inbox_more,
2381 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04002382
Daniel Sandler879c5e02012-04-17 16:46:51 -04002383 return contentView;
2384 }
2385
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002386 /**
2387 * @hide
2388 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002389 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002390 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002391 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002392
Daniel Sandler879c5e02012-04-17 16:46:51 -04002393 return wip;
2394 }
2395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396}