blob: 13e74da569bfa870b84607099d97ec3d9781297c [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
Chris Wren8fd39ec2014-02-27 17:43:26 -0500207
208 /**
209 * @hide
210 * A medium-format version of {@link #contentView}, giving the Notification an
211 * opportunity to add action buttons to contentView. The system UI may
212 * choose to show this as a popup notification at its discretion.
213 */
214 public RemoteViews headsUpContentView;
215
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400216 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800217 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800219 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
221 /**
222 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * </p>
227 */
228 public Uri sound;
229
230 /**
231 * Use this constant as the value for audioStreamType to request that
232 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700233 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 */
235 public static final int STREAM_DEFAULT = -1;
236
237 /**
238 * The audio stream type to use when playing the sound.
239 * Should be one of the STREAM_ constants from
240 * {@link android.media.AudioManager}.
241 */
242 public int audioStreamType = STREAM_DEFAULT;
243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500245 * The pattern with which to vibrate.
246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <p>
248 * To vibrate the default pattern, see {@link #defaults}.
249 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * @see android.os.Vibrator#vibrate(long[],int)
252 */
253 public long[] vibrate;
254
255 /**
256 * The color of the led. The hardware will do its best approximation.
257 *
258 * @see #FLAG_SHOW_LIGHTS
259 * @see #flags
260 */
261 public int ledARGB;
262
263 /**
264 * The number of milliseconds for the LED to be on 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 ledOnMS;
271
272 /**
273 * The number of milliseconds for the LED to be off while it's flashing.
274 * The hardware will do its best approximation.
275 *
276 * @see #FLAG_SHOW_LIGHTS
277 * @see #flags
278 */
279 public int ledOffMS;
280
281 /**
282 * Specifies which values should be taken from the defaults.
283 * <p>
284 * To set, OR the desired from {@link #DEFAULT_SOUND},
285 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
286 * values, use {@link #DEFAULT_ALL}.
287 * </p>
288 */
289 public int defaults;
290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * Bit to be bitwise-ored into the {@link #flags} field that should be
293 * set if you want the LED on for this notification.
294 * <ul>
295 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
296 * or 0 for both ledOnMS and ledOffMS.</li>
297 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
298 * <li>To flash the LED, pass the number of milliseconds that it should
299 * be on and off to ledOnMS and ledOffMS.</li>
300 * </ul>
301 * <p>
302 * Since hardware varies, you are not guaranteed that any of the values
303 * you pass are honored exactly. Use the system defaults (TODO) if possible
304 * because they will be set to values that work on any given hardware.
305 * <p>
306 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 */
309 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
310
311 /**
312 * Bit to be bitwise-ored into the {@link #flags} field that should be
313 * set if this notification is in reference to something that is ongoing,
314 * like a phone call. It should not be set if this notification is in
315 * reference to something that happened at a particular point in time,
316 * like a missed phone call.
317 */
318 public static final int FLAG_ONGOING_EVENT = 0x00000002;
319
320 /**
321 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700322 * the audio will be repeated until the notification is
323 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 */
325 public static final int FLAG_INSISTENT = 0x00000004;
326
327 /**
328 * Bit to be bitwise-ored into the {@link #flags} field that should be
329 * set if you want the sound and/or vibration play each time the
330 * notification is sent, even if it has not been canceled before that.
331 */
332 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
333
334 /**
335 * Bit to be bitwise-ored into the {@link #flags} field that should be
336 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500337 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 */
340 public static final int FLAG_AUTO_CANCEL = 0x00000010;
341
342 /**
343 * Bit to be bitwise-ored into the {@link #flags} field that should be
344 * set if the notification should not be canceled when the user clicks
345 * the Clear all button.
346 */
347 public static final int FLAG_NO_CLEAR = 0x00000020;
348
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700349 /**
350 * Bit to be bitwise-ored into the {@link #flags} field that should be
351 * set if this notification represents a currently running service. This
352 * will normally be set for you by {@link Service#startForeground}.
353 */
354 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
355
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400356 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500357 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800358 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400360 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500361 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400362
Griff Hazendfcb0802014-02-11 12:00:00 -0800363 /**
364 * Bit to be bitswise-ored into the {@link #flags} field that should be
365 * set if this notification is relevant to the current device only
366 * and it is not recommended that it bridge to other devices.
367 */
368 public static final int FLAG_LOCAL_ONLY = 0x00000100;
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 public int flags;
371
Tor Norbyed9273d62013-05-30 15:59:53 -0700372 /** @hide */
373 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
374 @Retention(RetentionPolicy.SOURCE)
375 public @interface Priority {}
376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500378 * Default notification {@link #priority}. If your application does not prioritize its own
379 * notifications, use this value for all notifications.
380 */
381 public static final int PRIORITY_DEFAULT = 0;
382
383 /**
384 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
385 * items smaller, or at a different position in the list, compared with your app's
386 * {@link #PRIORITY_DEFAULT} items.
387 */
388 public static final int PRIORITY_LOW = -1;
389
390 /**
391 * Lowest {@link #priority}; these items might not be shown to the user except under special
392 * circumstances, such as detailed notification logs.
393 */
394 public static final int PRIORITY_MIN = -2;
395
396 /**
397 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
398 * show these items larger, or at a different position in notification lists, compared with
399 * your app's {@link #PRIORITY_DEFAULT} items.
400 */
401 public static final int PRIORITY_HIGH = 1;
402
403 /**
404 * Highest {@link #priority}, for your application's most important items that require the
405 * user's prompt attention or input.
406 */
407 public static final int PRIORITY_MAX = 2;
408
409 /**
410 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800411 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500412 * Priority is an indication of how much of the user's valuable attention should be consumed by
413 * this notification. Low-priority notifications may be hidden from the user in certain
414 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500415 * system will make a determination about how to interpret this priority when presenting
416 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500417 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700418 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500419 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800420
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600421
422 /**
423 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
424 * the notification's presence and contents in untrusted situations (namely, on the secure
425 * lockscreen).
426 *
427 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
428 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
429 * shown in all situations, but the contents are only available if the device is unlocked for
430 * the appropriate user.
431 *
432 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
433 * can be read even in an "insecure" context (that is, above a secure lockscreen).
434 * To modify the public version of this notification—for example, to redact some portions—see
435 * {@link Builder#setPublicVersion(Notification)}.
436 *
437 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
438 * and ticker until the user has bypassed the lockscreen.
439 */
440 public int visibility;
441
442 public static final int VISIBILITY_PUBLIC = 1;
443 public static final int VISIBILITY_PRIVATE = 0;
444 public static final int VISIBILITY_SECRET = -1;
445
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500446 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400447 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400449 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500450
451 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400452 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500453 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400454 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500455
456 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400457 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500458 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400459 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500460
461 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400462 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500463 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400464 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500465
466 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400467 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500468 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400469 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500470
471 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400472 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400474 public static final String CATEGORY_ALARM = "alarm";
475
476 /**
477 * Notification category: progress of a long-running background operation.
478 */
479 public static final String CATEGORY_PROGRESS = "progress";
480
481 /**
482 * Notification category: social network or sharing update.
483 */
484 public static final String CATEGORY_SOCIAL = "social";
485
486 /**
487 * Notification category: error in background operation or authentication status.
488 */
489 public static final String CATEGORY_ERROR = "err";
490
491 /**
492 * Notification category: media transport control for playback.
493 */
494 public static final String CATEGORY_TRANSPORT = "transport";
495
496 /**
497 * Notification category: system or device status update. Reserved for system use.
498 */
499 public static final String CATEGORY_SYSTEM = "sys";
500
501 /**
502 * Notification category: indication of running background service.
503 */
504 public static final String CATEGORY_SERVICE = "service";
505
506 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400507 * Notification category: a specific, timely recommendation for a single thing.
508 * For example, a news app might want to recommend a news story it believes the user will
509 * want to read next.
510 */
511 public static final String CATEGORY_RECOMMENDATION = "recommendation";
512
513 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400514 * Notification category: ongoing information about device or contextual status.
515 */
516 public static final String CATEGORY_STATUS = "status";
517
518 /**
519 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
520 * that best describes this Notification. May be used by the system for ranking and filtering.
521 */
522 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500523
524 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400525 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400526 * <p>
527 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
528 * APIs, and are intended to be used by
529 * {@link android.service.notification.NotificationListenerService} implementations to extract
530 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500531 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400532 public Bundle extras = new Bundle();
533
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400534 /**
535 * {@link #extras} key: this is the title of the notification,
536 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
537 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500538 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400539
540 /**
541 * {@link #extras} key: this is the title of the notification when shown in expanded form,
542 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
543 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400544 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400545
546 /**
547 * {@link #extras} key: this is the main text payload, as supplied to
548 * {@link Builder#setContentText(CharSequence)}.
549 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500550 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400551
552 /**
553 * {@link #extras} key: this is a third line of text, as supplied to
554 * {@link Builder#setSubText(CharSequence)}.
555 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400556 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400557
558 /**
559 * {@link #extras} key: this is a small piece of additional text as supplied to
560 * {@link Builder#setContentInfo(CharSequence)}.
561 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400562 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400563
564 /**
565 * {@link #extras} key: this is a line of summary information intended to be shown
566 * alongside expanded notifications, as supplied to (e.g.)
567 * {@link BigTextStyle#setSummaryText(CharSequence)}.
568 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400569 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400570
571 /**
572 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
573 * supplied to {@link Builder#setSmallIcon(int)}.
574 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500575 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400576
577 /**
578 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
579 * notification payload, as
580 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
581 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400582 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400583
584 /**
585 * {@link #extras} key: this is a bitmap to be used instead of the one from
586 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
587 * shown in its expanded form, as supplied to
588 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
589 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400590 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400591
592 /**
593 * {@link #extras} key: this is the progress value supplied to
594 * {@link Builder#setProgress(int, int, boolean)}.
595 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400596 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400597
598 /**
599 * {@link #extras} key: this is the maximum value supplied to
600 * {@link Builder#setProgress(int, int, boolean)}.
601 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400602 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400603
604 /**
605 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
606 * {@link Builder#setProgress(int, int, boolean)}.
607 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400608 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400609
610 /**
611 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
612 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
613 * {@link Builder#setUsesChronometer(boolean)}.
614 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400615 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400616
617 /**
618 * {@link #extras} key: whether {@link #when} should be shown,
619 * as supplied to {@link Builder#setShowWhen(boolean)}.
620 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400621 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400622
623 /**
624 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
625 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
626 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400627 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400628
629 /**
630 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
631 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
632 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400633 public static final String EXTRA_TEXT_LINES = "android.textLines";
Chris Wren91ad5632013-06-05 15:05:57 -0400634 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400635
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400636 /**
637 * {@link #extras} key: An array of people that this notification relates to, specified
638 * by contacts provider contact URI.
639 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400640 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641
642 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400643 * @hide
644 * Extra added by NotificationManagerService to indicate whether a NotificationScorer
645 * modified the Notifications's score.
646 */
647 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
648
649 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400650 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400651 * @hide
652 */
653 public static final String EXTRA_AS_HEADS_UP = "headsup";
654
655 /**
Jorim Jaggi39fa59f2014-02-25 15:38:45 +0100656 * Extra added from {@link Notification.Builder} to indicate that the remote views were inflated
657 * from the builder, as opposed to being created directly from the application.
658 * @hide
659 */
660 public static final String EXTRA_BUILDER_REMOTE_VIEWS = "android.builderRemoteViews";
661
662 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400663 * Allow certain system-generated notifications to appear before the device is provisioned.
664 * Only available to notifications coming from the android package.
665 * @hide
666 */
667 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
668
669 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400670 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400671 * @hide
672 */
673 public static final int HEADS_UP_NEVER = 0;
674
675 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400676 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400677 * @hide
678 */
679 public static final int HEADS_UP_ALLOWED = 1;
680
681 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400682 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400683 * @hide
684 */
685 public static final int HEADS_UP_REQUESTED = 2;
686
687 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400688 * Structure to encapsulate a named action that can be shown as part of this notification.
689 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
690 * selected by the user.
691 * <p>
692 * Apps should use {@link Builder#addAction(int, CharSequence, PendingIntent)} to create and
693 * attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400694 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500695 public static class Action implements Parcelable {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400696 /**
697 * Small icon representing the action.
698 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400699 public int icon;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400700 /**
701 * Title of the action.
702 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400703 public CharSequence title;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400704 /**
705 * Intent to send when the user invokes this action. May be null, in which case the action
706 * may be rendered in a disabled presentation by the system UI.
707 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400708 public PendingIntent actionIntent;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400709
710 private Action() { }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400711 private Action(Parcel in) {
712 icon = in.readInt();
713 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
714 if (in.readInt() == 1) {
715 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
716 }
717 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400718 /**
719 * Use {@link Builder#addAction(int, CharSequence, PendingIntent)}.
720 */
721 public Action(int icon, CharSequence title, PendingIntent intent) {
722 this.icon = icon;
723 this.title = title;
724 this.actionIntent = intent;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400725 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400726
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400727 @Override
728 public Action clone() {
729 return new Action(
730 this.icon,
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400731 this.title,
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400732 this.actionIntent // safe to alias
733 );
734 }
735 @Override
736 public int describeContents() {
737 return 0;
738 }
739 @Override
740 public void writeToParcel(Parcel out, int flags) {
741 out.writeInt(icon);
742 TextUtils.writeToParcel(title, out, flags);
743 if (actionIntent != null) {
744 out.writeInt(1);
745 actionIntent.writeToParcel(out, flags);
746 } else {
747 out.writeInt(0);
748 }
749 }
750 public static final Parcelable.Creator<Action> CREATOR
751 = new Parcelable.Creator<Action>() {
752 public Action createFromParcel(Parcel in) {
753 return new Action(in);
754 }
755 public Action[] newArray(int size) {
756 return new Action[size];
757 }
758 };
759 }
760
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400761 /**
762 * Array of all {@link Action} structures attached to this notification by
763 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
764 * {@link android.service.notification.NotificationListenerService} that provide an alternative
765 * interface for invoking actions.
766 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500767 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400768
769 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600770 * Replacement version of this notification whose content will be shown
771 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
772 * and {@link #VISIBILITY_PUBLIC}.
773 */
774 public Notification publicVersion;
775
776 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500777 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800778 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 */
780 public Notification()
781 {
782 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500783 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785
786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 * @hide
788 */
789 public Notification(Context context, int icon, CharSequence tickerText, long when,
790 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
791 {
792 this.when = when;
793 this.icon = icon;
794 this.tickerText = tickerText;
795 setLatestEventInfo(context, contentTitle, contentText,
796 PendingIntent.getActivity(context, 0, contentIntent, 0));
797 }
798
799 /**
800 * Constructs a Notification object with the information needed to
801 * have a status bar icon without the standard expanded view.
802 *
803 * @param icon The resource id of the icon to put in the status bar.
804 * @param tickerText The text that flows by in the status bar when the notification first
805 * activates.
806 * @param when The time to show in the time field. In the System.currentTimeMillis
807 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800808 *
809 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800811 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 public Notification(int icon, CharSequence tickerText, long when)
813 {
814 this.icon = icon;
815 this.tickerText = tickerText;
816 this.when = when;
817 }
818
819 /**
820 * Unflatten the notification from a parcel.
821 */
822 public Notification(Parcel parcel)
823 {
824 int version = parcel.readInt();
825
826 when = parcel.readLong();
827 icon = parcel.readInt();
828 number = parcel.readInt();
829 if (parcel.readInt() != 0) {
830 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
831 }
832 if (parcel.readInt() != 0) {
833 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
834 }
835 if (parcel.readInt() != 0) {
836 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
837 }
838 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800839 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400840 }
841 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
843 }
Joe Onorato561d3852010-11-20 18:09:34 -0800844 if (parcel.readInt() != 0) {
845 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 defaults = parcel.readInt();
848 flags = parcel.readInt();
849 if (parcel.readInt() != 0) {
850 sound = Uri.CREATOR.createFromParcel(parcel);
851 }
852
853 audioStreamType = parcel.readInt();
854 vibrate = parcel.createLongArray();
855 ledARGB = parcel.readInt();
856 ledOnMS = parcel.readInt();
857 ledOffMS = parcel.readInt();
858 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400859
860 if (parcel.readInt() != 0) {
861 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
862 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500863
864 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400865
John Spurlockfd7f1e02014-03-18 16:41:57 -0400866 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500867
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500868 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400869
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500870 actions = parcel.createTypedArray(Action.CREATOR); // may be null
871
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400872 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400873 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
874 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600875
Chris Wren8fd39ec2014-02-27 17:43:26 -0500876 if (parcel.readInt() != 0) {
877 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
878 }
879
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600880 visibility = parcel.readInt();
881
882 if (parcel.readInt() != 0) {
883 publicVersion = Notification.CREATOR.createFromParcel(parcel);
884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 }
886
Andy Stadler110988c2010-12-03 14:29:16 -0800887 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700888 public Notification clone() {
889 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -0400890 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500891 return that;
892 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700893
Daniel Sandler1a497d32013-04-18 14:52:45 -0400894 /**
895 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
896 * of this into that.
897 * @hide
898 */
899 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700900 that.when = this.when;
901 that.icon = this.icon;
902 that.number = this.number;
903
904 // PendingIntents are global, so there's no reason (or way) to clone them.
905 that.contentIntent = this.contentIntent;
906 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400907 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700908
909 if (this.tickerText != null) {
910 that.tickerText = this.tickerText.toString();
911 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400912 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800913 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400914 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400915 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700916 that.contentView = this.contentView.clone();
917 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400918 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800919 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
920 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100921 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700922 that.sound = this.sound; // android.net.Uri is immutable
923 that.audioStreamType = this.audioStreamType;
924
925 final long[] vibrate = this.vibrate;
926 if (vibrate != null) {
927 final int N = vibrate.length;
928 final long[] vib = that.vibrate = new long[N];
929 System.arraycopy(vibrate, 0, vib, 0, N);
930 }
931
932 that.ledARGB = this.ledARGB;
933 that.ledOnMS = this.ledOnMS;
934 that.ledOffMS = this.ledOffMS;
935 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500936
Joe Onorato18e69df2010-05-17 22:26:12 -0700937 that.flags = this.flags;
938
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500939 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800940
John Spurlockfd7f1e02014-03-18 16:41:57 -0400941 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500942
943 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400944 try {
945 that.extras = new Bundle(this.extras);
946 // will unparcel
947 that.extras.size();
948 } catch (BadParcelableException e) {
949 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
950 that.extras = null;
951 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500952 }
953
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500954 if (this.actions != null) {
955 that.actions = new Action[this.actions.length];
956 for(int i=0; i<this.actions.length; i++) {
957 that.actions[i] = this.actions[i].clone();
958 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400959 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500960
Daniel Sandler1a497d32013-04-18 14:52:45 -0400961 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400962 that.bigContentView = this.bigContentView.clone();
963 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400964
Chris Wren8fd39ec2014-02-27 17:43:26 -0500965 if (heavy && this.headsUpContentView != null) {
966 that.headsUpContentView = this.headsUpContentView.clone();
967 }
968
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600969 that.visibility = this.visibility;
970
971 if (this.publicVersion != null) {
972 that.publicVersion = new Notification();
973 this.publicVersion.cloneInto(that.publicVersion, heavy);
974 }
975
Daniel Sandler1a497d32013-04-18 14:52:45 -0400976 if (!heavy) {
977 that.lightenPayload(); // will clean out extras
978 }
979 }
980
981 /**
982 * Removes heavyweight parts of the Notification object for archival or for sending to
983 * listeners when the full contents are not necessary.
984 * @hide
985 */
986 public final void lightenPayload() {
987 tickerView = null;
988 contentView = null;
989 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -0500990 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -0400991 largeIcon = null;
992 if (extras != null) {
993 extras.remove(Notification.EXTRA_LARGE_ICON);
994 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
995 extras.remove(Notification.EXTRA_PICTURE);
996 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700997 }
998
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400999 /**
1000 * Make sure this CharSequence is safe to put into a bundle, which basically
1001 * means it had better not be some custom Parcelable implementation.
1002 * @hide
1003 */
1004 public static CharSequence safeCharSequence(CharSequence cs) {
1005 if (cs instanceof Parcelable) {
1006 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1007 + " instance is a custom Parcelable and not allowed in Notification");
1008 return cs.toString();
1009 }
1010
1011 return cs;
1012 }
1013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 public int describeContents() {
1015 return 0;
1016 }
1017
1018 /**
1019 * Flatten this notification from a parcel.
1020 */
1021 public void writeToParcel(Parcel parcel, int flags)
1022 {
1023 parcel.writeInt(1);
1024
1025 parcel.writeLong(when);
1026 parcel.writeInt(icon);
1027 parcel.writeInt(number);
1028 if (contentIntent != null) {
1029 parcel.writeInt(1);
1030 contentIntent.writeToParcel(parcel, 0);
1031 } else {
1032 parcel.writeInt(0);
1033 }
1034 if (deleteIntent != null) {
1035 parcel.writeInt(1);
1036 deleteIntent.writeToParcel(parcel, 0);
1037 } else {
1038 parcel.writeInt(0);
1039 }
1040 if (tickerText != null) {
1041 parcel.writeInt(1);
1042 TextUtils.writeToParcel(tickerText, parcel, flags);
1043 } else {
1044 parcel.writeInt(0);
1045 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001046 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001047 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001048 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001049 } else {
1050 parcel.writeInt(0);
1051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 if (contentView != null) {
1053 parcel.writeInt(1);
1054 contentView.writeToParcel(parcel, 0);
1055 } else {
1056 parcel.writeInt(0);
1057 }
Joe Onorato561d3852010-11-20 18:09:34 -08001058 if (largeIcon != null) {
1059 parcel.writeInt(1);
1060 largeIcon.writeToParcel(parcel, 0);
1061 } else {
1062 parcel.writeInt(0);
1063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064
1065 parcel.writeInt(defaults);
1066 parcel.writeInt(this.flags);
1067
1068 if (sound != null) {
1069 parcel.writeInt(1);
1070 sound.writeToParcel(parcel, 0);
1071 } else {
1072 parcel.writeInt(0);
1073 }
1074 parcel.writeInt(audioStreamType);
1075 parcel.writeLongArray(vibrate);
1076 parcel.writeInt(ledARGB);
1077 parcel.writeInt(ledOnMS);
1078 parcel.writeInt(ledOffMS);
1079 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001080
1081 if (fullScreenIntent != null) {
1082 parcel.writeInt(1);
1083 fullScreenIntent.writeToParcel(parcel, 0);
1084 } else {
1085 parcel.writeInt(0);
1086 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001087
1088 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001089
John Spurlockfd7f1e02014-03-18 16:41:57 -04001090 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001091
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001092 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001093
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001094 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001095
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001096 if (bigContentView != null) {
1097 parcel.writeInt(1);
1098 bigContentView.writeToParcel(parcel, 0);
1099 } else {
1100 parcel.writeInt(0);
1101 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001102
Chris Wren8fd39ec2014-02-27 17:43:26 -05001103 if (headsUpContentView != null) {
1104 parcel.writeInt(1);
1105 headsUpContentView.writeToParcel(parcel, 0);
1106 } else {
1107 parcel.writeInt(0);
1108 }
1109
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001110 parcel.writeInt(visibility);
1111
1112 if (publicVersion != null) {
1113 parcel.writeInt(1);
1114 publicVersion.writeToParcel(parcel, 0);
1115 } else {
1116 parcel.writeInt(0);
1117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
1119
1120 /**
1121 * Parcelable.Creator that instantiates Notification objects
1122 */
1123 public static final Parcelable.Creator<Notification> CREATOR
1124 = new Parcelable.Creator<Notification>()
1125 {
1126 public Notification createFromParcel(Parcel parcel)
1127 {
1128 return new Notification(parcel);
1129 }
1130
1131 public Notification[] newArray(int size)
1132 {
1133 return new Notification[size];
1134 }
1135 };
1136
1137 /**
1138 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1139 * layout.
1140 *
1141 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1142 * in the view.</p>
1143 * @param context The context for your application / activity.
1144 * @param contentTitle The title that goes in the expanded entry.
1145 * @param contentText The text that goes in the expanded entry.
1146 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1147 * If this is an activity, it must include the
1148 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001149 * that you take care of task management as described in the
1150 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1151 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001152 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001153 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001155 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 public void setLatestEventInfo(Context context,
1157 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001158 Notification.Builder builder = new Notification.Builder(context);
1159
1160 // First, ensure that key pieces of information that may have been set directly
1161 // are preserved
1162 builder.setWhen(this.when);
1163 builder.setSmallIcon(this.icon);
1164 builder.setPriority(this.priority);
1165 builder.setTicker(this.tickerText);
1166 builder.setNumber(this.number);
1167 builder.mFlags = this.flags;
1168 builder.setSound(this.sound, this.audioStreamType);
1169 builder.setDefaults(this.defaults);
1170 builder.setVibrate(this.vibrate);
1171
1172 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001174 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001177 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001179 builder.setContentIntent(contentIntent);
1180 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182
1183 @Override
1184 public String toString() {
1185 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001186 sb.append("Notification(pri=");
1187 sb.append(priority);
1188 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001189 if (contentView != null) {
1190 sb.append(contentView.getPackage());
1191 sb.append("/0x");
1192 sb.append(Integer.toHexString(contentView.getLayoutId()));
1193 } else {
1194 sb.append("null");
1195 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001196 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001197 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001198 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1199 sb.append("default");
1200 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 int N = this.vibrate.length-1;
1202 sb.append("[");
1203 for (int i=0; i<N; i++) {
1204 sb.append(this.vibrate[i]);
1205 sb.append(',');
1206 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001207 if (N != -1) {
1208 sb.append(this.vibrate[N]);
1209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 } else {
1212 sb.append("null");
1213 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001214 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001215 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001217 } else if (this.sound != null) {
1218 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 } else {
1220 sb.append("null");
1221 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001222 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001224 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001225 sb.append(Integer.toHexString(this.flags));
John Spurlockfd7f1e02014-03-18 16:41:57 -04001226 sb.append(" category="); sb.append(this.category);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001227 if (actions != null) {
1228 sb.append(" ");
1229 sb.append(actions.length);
1230 sb.append(" action");
1231 if (actions.length > 1) sb.append("s");
1232 }
1233 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 return sb.toString();
1235 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001236
Jeff Sharkey6d515712012-09-20 16:06:08 -07001237 /** {@hide} */
1238 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001239 if (user.getIdentifier() == UserHandle.USER_ALL) {
1240 user = UserHandle.OWNER;
1241 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001242 if (tickerView != null) {
1243 tickerView.setUser(user);
1244 }
1245 if (contentView != null) {
1246 contentView.setUser(user);
1247 }
1248 if (bigContentView != null) {
1249 bigContentView.setUser(user);
1250 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001251 if (headsUpContentView != null) {
1252 headsUpContentView.setUser(user);
1253 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001254 }
1255
Joe Onoratocb109a02011-01-18 17:57:41 -08001256 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001257 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001258 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001259 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001260 * content views using the platform's notification layout template. If your app supports
1261 * versions of Android as old as API level 4, you can instead use
1262 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1263 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1264 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001265 *
Scott Main183bf112012-08-13 19:12:13 -07001266 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001267 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001268 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001269 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001270 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1271 * .setContentText(subject)
1272 * .setSmallIcon(R.drawable.new_mail)
1273 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001274 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001275 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001276 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001277 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001278 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001279
Joe Onorato46439ce2010-11-19 13:56:21 -08001280 private Context mContext;
1281
1282 private long mWhen;
1283 private int mSmallIcon;
1284 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001285 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001286 private CharSequence mContentTitle;
1287 private CharSequence mContentText;
1288 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001289 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001290 private PendingIntent mContentIntent;
1291 private RemoteViews mContentView;
1292 private PendingIntent mDeleteIntent;
1293 private PendingIntent mFullScreenIntent;
1294 private CharSequence mTickerText;
1295 private RemoteViews mTickerView;
1296 private Bitmap mLargeIcon;
1297 private Uri mSound;
1298 private int mAudioStreamType;
1299 private long[] mVibrate;
1300 private int mLedArgb;
1301 private int mLedOnMs;
1302 private int mLedOffMs;
1303 private int mDefaults;
1304 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001305 private int mProgressMax;
1306 private int mProgress;
1307 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001308 private String mCategory;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001309 private Bundle mExtras;
1310 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001311 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001312 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001313 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001314 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001315 private int mVisibility = VISIBILITY_PRIVATE;
1316 private Notification mPublicVersion = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001317 private boolean mQuantumTheme;
Joe Onorato46439ce2010-11-19 13:56:21 -08001318
Joe Onoratocb109a02011-01-18 17:57:41 -08001319 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001320 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001321 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001322
1323 * <table>
1324 * <tr><th align=right>priority</th>
1325 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1326 * <tr><th align=right>when</th>
1327 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1328 * <tr><th align=right>audio stream</th>
1329 * <td>{@link #STREAM_DEFAULT}</td></tr>
1330 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001331 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001332
1333 * @param context
1334 * A {@link Context} that will be used by the Builder to construct the
1335 * RemoteViews. The Context will not be held past the lifetime of this Builder
1336 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001337 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001338 public Builder(Context context) {
1339 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001340
1341 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001342 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001343 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001344 mPriority = PRIORITY_DEFAULT;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001345
1346 // TODO: Decide on targetSdk from calling app whether to use quantum theme.
1347 mQuantumTheme = true;
Joe Onorato46439ce2010-11-19 13:56:21 -08001348 }
1349
Joe Onoratocb109a02011-01-18 17:57:41 -08001350 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001351 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001352 * It will be shown in the notification content view by default; use
1353 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001354 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001355 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001356 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001357 public Builder setWhen(long when) {
1358 mWhen = when;
1359 return this;
1360 }
1361
Joe Onoratocb109a02011-01-18 17:57:41 -08001362 /**
Daniel Sandler0c890492012-09-12 17:23:10 -07001363 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown
1364 * in the content view.
1365 */
1366 public Builder setShowWhen(boolean show) {
1367 mShowWhen = show;
1368 return this;
1369 }
1370
1371 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001372 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001373 *
1374 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001375 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001376 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001377 * Useful when showing an elapsed time (like an ongoing phone call).
1378 *
1379 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001380 * @see Notification#when
1381 */
1382 public Builder setUsesChronometer(boolean b) {
1383 mUseChronometer = b;
1384 return this;
1385 }
1386
1387 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001388 * Set the small icon resource, which will be used to represent the notification in the
1389 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001390 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001391
1392 * The platform template for the expanded view will draw this icon in the left, unless a
1393 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1394 * icon will be moved to the right-hand side.
1395 *
1396
1397 * @param icon
1398 * A resource ID in the application's package of the drawable to use.
1399 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001400 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001401 public Builder setSmallIcon(int icon) {
1402 mSmallIcon = icon;
1403 return this;
1404 }
1405
Joe Onoratocb109a02011-01-18 17:57:41 -08001406 /**
1407 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1408 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1409 * LevelListDrawable}.
1410 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001411 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001412 * @param level The level to use for the icon.
1413 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001414 * @see Notification#icon
1415 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001416 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001417 public Builder setSmallIcon(int icon, int level) {
1418 mSmallIcon = icon;
1419 mSmallIconLevel = level;
1420 return this;
1421 }
1422
Joe Onoratocb109a02011-01-18 17:57:41 -08001423 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001424 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001425 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001426 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001427 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001428 return this;
1429 }
1430
Joe Onoratocb109a02011-01-18 17:57:41 -08001431 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001432 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001433 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001434 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001435 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001436 return this;
1437 }
1438
Joe Onoratocb109a02011-01-18 17:57:41 -08001439 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001440 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001441 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1442 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001443 */
1444 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001445 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001446 return this;
1447 }
1448
1449 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001450 * Set the large number at the right-hand side of the notification. This is
1451 * equivalent to setContentInfo, although it might show the number in a different
1452 * font size for readability.
1453 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001454 public Builder setNumber(int number) {
1455 mNumber = number;
1456 return this;
1457 }
1458
Joe Onoratocb109a02011-01-18 17:57:41 -08001459 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001460 * A small piece of additional information pertaining to this notification.
1461 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001462 * The platform template will draw this on the last line of the notification, at the far
1463 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001464 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001465 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001466 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001467 return this;
1468 }
1469
Joe Onoratocb109a02011-01-18 17:57:41 -08001470 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001471 * Set the progress this notification represents.
1472 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001473 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001474 */
1475 public Builder setProgress(int max, int progress, boolean indeterminate) {
1476 mProgressMax = max;
1477 mProgress = progress;
1478 mProgressIndeterminate = indeterminate;
1479 return this;
1480 }
1481
1482 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001483 * Supply a custom RemoteViews to use instead of the platform template.
1484 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001485 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001486 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001487 public Builder setContent(RemoteViews views) {
1488 mContentView = views;
1489 return this;
1490 }
1491
Joe Onoratocb109a02011-01-18 17:57:41 -08001492 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001493 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1494 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001495 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1496 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1497 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001498 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001499 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001500 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001501 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001502 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001503 public Builder setContentIntent(PendingIntent intent) {
1504 mContentIntent = intent;
1505 return this;
1506 }
1507
Joe Onoratocb109a02011-01-18 17:57:41 -08001508 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001509 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1510 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001511 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001512 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001513 public Builder setDeleteIntent(PendingIntent intent) {
1514 mDeleteIntent = intent;
1515 return this;
1516 }
1517
Joe Onoratocb109a02011-01-18 17:57:41 -08001518 /**
1519 * An intent to launch instead of posting the notification to the status bar.
1520 * Only for use with extremely high-priority notifications demanding the user's
1521 * <strong>immediate</strong> attention, such as an incoming phone call or
1522 * alarm clock that the user has explicitly set to a particular time.
1523 * If this facility is used for something else, please give the user an option
1524 * to turn it off and use a normal notification, as this can be extremely
1525 * disruptive.
1526 *
1527 * @param intent The pending intent to launch.
1528 * @param highPriority Passing true will cause this notification to be sent
1529 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001530 *
1531 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001532 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001533 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1534 mFullScreenIntent = intent;
1535 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1536 return this;
1537 }
1538
Joe Onoratocb109a02011-01-18 17:57:41 -08001539 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001540 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001541 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001542 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001543 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001544 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001545 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001546 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001547 return this;
1548 }
1549
Joe Onoratocb109a02011-01-18 17:57:41 -08001550 /**
1551 * Set the text that is displayed in the status bar when the notification first
1552 * arrives, and also a RemoteViews object that may be displayed instead on some
1553 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001554 *
1555 * @see Notification#tickerText
1556 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001557 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001558 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001559 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001560 mTickerView = views;
1561 return this;
1562 }
1563
Joe Onoratocb109a02011-01-18 17:57:41 -08001564 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001565 * Add a large icon to the notification (and the ticker on some devices).
1566 *
1567 * In the platform template, this image will be shown on the left of the notification view
1568 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1569 *
1570 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001571 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001572 public Builder setLargeIcon(Bitmap icon) {
1573 mLargeIcon = icon;
1574 return this;
1575 }
1576
Joe Onoratocb109a02011-01-18 17:57:41 -08001577 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001578 * Set the sound to play.
1579 *
1580 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1581 *
1582 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001583 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001584 public Builder setSound(Uri sound) {
1585 mSound = sound;
1586 mAudioStreamType = STREAM_DEFAULT;
1587 return this;
1588 }
1589
Joe Onoratocb109a02011-01-18 17:57:41 -08001590 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001591 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001592 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001593 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1594 *
1595 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001596 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001597 public Builder setSound(Uri sound, int streamType) {
1598 mSound = sound;
1599 mAudioStreamType = streamType;
1600 return this;
1601 }
1602
Joe Onoratocb109a02011-01-18 17:57:41 -08001603 /**
1604 * Set the vibration pattern to use.
1605 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001606
1607 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1608 * <code>pattern</code> parameter.
1609 *
1610
1611 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001612 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001613 public Builder setVibrate(long[] pattern) {
1614 mVibrate = pattern;
1615 return this;
1616 }
1617
Joe Onoratocb109a02011-01-18 17:57:41 -08001618 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001619 * Set the desired color for the indicator LED on the device, as well as the
1620 * blink duty cycle (specified in milliseconds).
1621 *
1622
1623 * Not all devices will honor all (or even any) of these values.
1624 *
1625
1626 * @see Notification#ledARGB
1627 * @see Notification#ledOnMS
1628 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001629 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001630 public Builder setLights(int argb, int onMs, int offMs) {
1631 mLedArgb = argb;
1632 mLedOnMs = onMs;
1633 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001634 return this;
1635 }
1636
Joe Onoratocb109a02011-01-18 17:57:41 -08001637 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001638 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001639 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001640
1641 * Ongoing notifications cannot be dismissed by the user, so your application or service
1642 * must take care of canceling them.
1643 *
1644
1645 * They are typically used to indicate a background task that the user is actively engaged
1646 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1647 * (e.g., a file download, sync operation, active network connection).
1648 *
1649
1650 * @see Notification#FLAG_ONGOING_EVENT
1651 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001652 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001653 public Builder setOngoing(boolean ongoing) {
1654 setFlag(FLAG_ONGOING_EVENT, ongoing);
1655 return this;
1656 }
1657
Joe Onoratocb109a02011-01-18 17:57:41 -08001658 /**
1659 * Set this flag if you would only like the sound, vibrate
1660 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001661 *
1662 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001663 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001664 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1665 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1666 return this;
1667 }
1668
Joe Onoratocb109a02011-01-18 17:57:41 -08001669 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001670 * Make this notification automatically dismissed when the user touches it. The
1671 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1672 *
1673 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001674 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001675 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001676 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001677 return this;
1678 }
1679
Joe Onoratocb109a02011-01-18 17:57:41 -08001680 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08001681 * Set whether or not this notification should not bridge to other devices.
1682 *
1683 * <p>Some notifications can be bridged to other devices for remote display.
1684 * This hint can be set to recommend this notification not be bridged.
1685 */
1686 public Builder setLocalOnly(boolean localOnly) {
1687 setFlag(FLAG_LOCAL_ONLY, localOnly);
1688 return this;
1689 }
1690
1691 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001692 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001693 * <p>
1694 * The value should be one or more of the following fields combined with
1695 * bitwise-or:
1696 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1697 * <p>
1698 * For all default values, use {@link #DEFAULT_ALL}.
1699 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001700 public Builder setDefaults(int defaults) {
1701 mDefaults = defaults;
1702 return this;
1703 }
1704
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001705 /**
1706 * Set the priority of this notification.
1707 *
1708 * @see Notification#priority
1709 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001710 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001711 mPriority = pri;
1712 return this;
1713 }
Joe Malin8d40d042012-11-05 11:36:40 -08001714
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001715 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001716 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08001717 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04001718 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001719 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04001720 public Builder setCategory(String category) {
1721 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001722 return this;
1723 }
1724
1725 /**
Griff Hazen720042b2014-02-24 15:46:56 -08001726 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001727 *
Griff Hazen720042b2014-02-24 15:46:56 -08001728 * <p>Values within the Bundle will replace existing extras values in this Builder.
1729 *
1730 * @see Notification#extras
1731 */
1732 public Builder addExtras(Bundle bag) {
1733 if (mExtras == null) {
1734 mExtras = new Bundle(bag);
1735 } else {
1736 mExtras.putAll(bag);
1737 }
1738 return this;
1739 }
1740
1741 /**
1742 * Set metadata for this notification.
1743 *
1744 * <p>A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001745 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001746 * called.
1747 *
Griff Hazen720042b2014-02-24 15:46:56 -08001748 * <p>Replaces any existing extras values with those from the provided Bundle.
1749 * Use {@link #addExtras} to merge in metadata instead.
1750 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001751 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001752 */
1753 public Builder setExtras(Bundle bag) {
1754 mExtras = bag;
1755 return this;
1756 }
1757
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001758 /**
Griff Hazen720042b2014-02-24 15:46:56 -08001759 * Get the current metadata Bundle used by this notification Builder.
1760 *
1761 * <p>The returned Bundle is shared with this Builder.
1762 *
1763 * <p>The current contents of this Bundle are copied into the Notification each time
1764 * {@link #build()} is called.
1765 *
1766 * @see Notification#extras
1767 */
1768 public Bundle getExtras() {
1769 if (mExtras == null) {
1770 mExtras = new Bundle();
1771 }
1772 return mExtras;
1773 }
1774
1775 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001776 * Add an action to this notification. Actions are typically displayed by
1777 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001778 * <p>
1779 * Every action must have an icon (32dp square and matching the
1780 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
1781 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
1782 * <p>
1783 * A notification in its expanded form can display up to 3 actions, from left to right in
1784 * the order they were added. Actions will not be displayed when the notification is
1785 * collapsed, however, so be sure that any essential functions may be accessed by the user
1786 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001787 *
1788 * @param icon Resource ID of a drawable that represents the action.
1789 * @param title Text describing the action.
1790 * @param intent PendingIntent to be fired when the action is invoked.
1791 */
1792 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001793 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001794 return this;
1795 }
1796
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001797 /**
1798 * Add a rich notification style to be applied at build time.
1799 *
1800 * @param style Object responsible for modifying the notification style.
1801 */
1802 public Builder setStyle(Style style) {
1803 if (mStyle != style) {
1804 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001805 if (mStyle != null) {
1806 mStyle.setBuilder(this);
1807 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001808 }
1809 return this;
1810 }
1811
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001812 /**
1813 * Specify the value of {@link #visibility}.
1814
1815 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
1816 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
1817 *
1818 * @return The same Builder.
1819 */
1820 public Builder setVisibility(int visibility) {
1821 mVisibility = visibility;
1822 return this;
1823 }
1824
1825 /**
1826 * Supply a replacement Notification whose contents should be shown in insecure contexts
1827 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
1828 * @param n A replacement notification, presumably with some or all info redacted.
1829 * @return The same Builder.
1830 */
1831 public Builder setPublicVersion(Notification n) {
1832 mPublicVersion = n;
1833 return this;
1834 }
1835
Joe Onorato46439ce2010-11-19 13:56:21 -08001836 private void setFlag(int mask, boolean value) {
1837 if (value) {
1838 mFlags |= mask;
1839 } else {
1840 mFlags &= ~mask;
1841 }
1842 }
1843
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001844 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08001845 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001846 boolean showLine3 = false;
1847 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001848 int smallIconImageViewId = R.id.icon;
1849 if (mLargeIcon != null) {
1850 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1851 smallIconImageViewId = R.id.right_icon;
1852 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001853 if (!mQuantumTheme && mPriority < PRIORITY_LOW) {
Daniel Sandlere95658c2012-05-10 00:33:54 -04001854 contentView.setInt(R.id.icon,
1855 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1856 contentView.setInt(R.id.status_bar_latest_event_content,
1857 "setBackgroundResource", R.drawable.notification_bg_low);
1858 }
Joe Onorato561d3852010-11-20 18:09:34 -08001859 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001860 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1861 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001862 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001863 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001864 }
1865 if (mContentTitle != null) {
1866 contentView.setTextViewText(R.id.title, mContentTitle);
1867 }
1868 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001869 contentView.setTextViewText(R.id.text, mContentText);
1870 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001871 }
1872 if (mContentInfo != null) {
1873 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001874 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001875 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001876 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001877 final int tooBig = mContext.getResources().getInteger(
1878 R.integer.status_bar_notification_info_maxnum);
1879 if (mNumber > tooBig) {
1880 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1881 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001882 } else {
1883 NumberFormat f = NumberFormat.getIntegerInstance();
1884 contentView.setTextViewText(R.id.info, f.format(mNumber));
1885 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001886 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001887 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001888 } else {
1889 contentView.setViewVisibility(R.id.info, View.GONE);
1890 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001891
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001892 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001893 if (mSubText != null) {
1894 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001895 if (mContentText != null) {
1896 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001897 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
1898 showLine2 = true;
1899 } else {
1900 contentView.setViewVisibility(R.id.text2, View.GONE);
1901 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001902 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001903 contentView.setViewVisibility(R.id.text2, View.GONE);
1904 if (mProgressMax != 0 || mProgressIndeterminate) {
1905 contentView.setProgressBar(
1906 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1907 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001908 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001909 } else {
1910 contentView.setViewVisibility(R.id.progress, View.GONE);
1911 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001912 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001913 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001914 if (fitIn1U) {
1915 // need to shrink all the type to make sure everything fits
1916 final Resources res = mContext.getResources();
1917 final float subTextSize = res.getDimensionPixelSize(
1918 R.dimen.notification_subtext_size);
1919 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
1920 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001921 // vertical centering
1922 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1923 }
1924
Daniel Sandler0c890492012-09-12 17:23:10 -07001925 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001926 if (mUseChronometer) {
1927 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1928 contentView.setLong(R.id.chronometer, "setBase",
1929 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1930 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1931 } else {
1932 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1933 contentView.setLong(R.id.time, "setTime", mWhen);
1934 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001935 } else {
1936 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001937 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001938
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001939 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001940 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001941 return contentView;
1942 }
1943
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001944 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001945 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001946
1947 int N = mActions.size();
1948 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001949 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001950 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001951 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001952 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04001953 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001954 for (int i=0; i<N; i++) {
1955 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001956 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001957 big.addView(R.id.actions, button);
1958 }
1959 }
1960 return big;
1961 }
1962
Joe Onorato46439ce2010-11-19 13:56:21 -08001963 private RemoteViews makeContentView() {
1964 if (mContentView != null) {
1965 return mContentView;
1966 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001967 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001968 }
1969 }
1970
1971 private RemoteViews makeTickerView() {
1972 if (mTickerView != null) {
1973 return mTickerView;
1974 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001975 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001976 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001977 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001978 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08001979 } else {
1980 return null;
1981 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001982 }
1983 }
1984
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001985 private RemoteViews makeBigContentView() {
1986 if (mActions.size() == 0) return null;
1987
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001988 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001989 }
1990
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001991 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05001992 if (mActions.size() == 0) return null;
1993
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001994 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05001995 }
1996
1997
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001998 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04001999 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002000 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002001 tombstone ? getActionTombstoneLayoutResource()
2002 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002003 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002004 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002005 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002006 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002007 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002008 button.setContentDescription(R.id.action0, action.title);
2009 return button;
2010 }
2011
Joe Onoratocb109a02011-01-18 17:57:41 -08002012 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002013 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002014 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002015 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002016 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002017 Notification n = new Notification();
2018 n.when = mWhen;
2019 n.icon = mSmallIcon;
2020 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002021 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08002022 n.contentView = makeContentView();
2023 n.contentIntent = mContentIntent;
2024 n.deleteIntent = mDeleteIntent;
2025 n.fullScreenIntent = mFullScreenIntent;
2026 n.tickerText = mTickerText;
2027 n.tickerView = makeTickerView();
2028 n.largeIcon = mLargeIcon;
2029 n.sound = mSound;
2030 n.audioStreamType = mAudioStreamType;
2031 n.vibrate = mVibrate;
2032 n.ledARGB = mLedArgb;
2033 n.ledOnMS = mLedOnMs;
2034 n.ledOffMS = mLedOffMs;
2035 n.defaults = mDefaults;
2036 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002037 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002038 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002039 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002040 n.flags |= FLAG_SHOW_LIGHTS;
2041 }
2042 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2043 n.flags |= FLAG_SHOW_LIGHTS;
2044 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002045 n.category = mCategory;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002046 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002047 if (mActions.size() > 0) {
2048 n.actions = new Action[mActions.size()];
2049 mActions.toArray(n.actions);
2050 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002051 n.visibility = mVisibility;
2052
2053 if (mPublicVersion != null) {
2054 n.publicVersion = new Notification();
2055 mPublicVersion.cloneInto(n.publicVersion, true);
2056 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002057
Joe Onorato46439ce2010-11-19 13:56:21 -08002058 return n;
2059 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002060
2061 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002062 * Capture, in the provided bundle, semantic information used in the construction of
2063 * this Notification object.
2064 * @hide
2065 */
Griff Hazen720042b2014-02-24 15:46:56 -08002066 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002067 // Store original information used in the construction of this object
2068 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2069 extras.putCharSequence(EXTRA_TEXT, mContentText);
2070 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2071 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2072 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2073 extras.putInt(EXTRA_PROGRESS, mProgress);
2074 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2075 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2076 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2077 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002078 extras.putBoolean(EXTRA_BUILDER_REMOTE_VIEWS, mContentView == null);
John Spurlockac08a472013-06-10 11:37:08 -04002079 if (mLargeIcon != null) {
2080 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2081 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002082 }
2083
2084 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002085 * @deprecated Use {@link #build()} instead.
2086 */
2087 @Deprecated
2088 public Notification getNotification() {
2089 return build();
2090 }
2091
2092 /**
2093 * Combine all of the options that have been set and return a new {@link Notification}
2094 * object.
2095 */
2096 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002097 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002098
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002099 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002100 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002101 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002102
2103 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2104
Griff Hazen720042b2014-02-24 15:46:56 -08002105 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002106 if (mStyle != null) {
2107 mStyle.addExtras(n.extras);
2108 }
2109
2110 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002111 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002112
2113 /**
2114 * Apply this Builder to an existing {@link Notification} object.
2115 *
2116 * @hide
2117 */
2118 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002119 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002120 return n;
2121 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002122
2123
2124 private int getBaseLayoutResource() {
2125 return mQuantumTheme
2126 ? R.layout.notification_template_quantum_base
2127 : R.layout.notification_template_base;
2128 }
2129
2130 private int getBigBaseLayoutResource() {
2131 return mQuantumTheme
2132 ? R.layout.notification_template_quantum_big_base
2133 : R.layout.notification_template_big_base;
2134 }
2135
2136 private int getBigPictureLayoutResource() {
2137 return mQuantumTheme
2138 ? R.layout.notification_template_quantum_big_picture
2139 : R.layout.notification_template_big_picture;
2140 }
2141
2142 private int getBigTextLayoutResource() {
2143 return mQuantumTheme
2144 ? R.layout.notification_template_quantum_big_text
2145 : R.layout.notification_template_big_text;
2146 }
2147
2148 private int getInboxLayoutResource() {
2149 return mQuantumTheme
2150 ? R.layout.notification_template_quantum_inbox
2151 : R.layout.notification_template_inbox;
2152 }
2153
2154 private int getActionLayoutResource() {
2155 return mQuantumTheme
2156 ? R.layout.notification_quantum_action
2157 : R.layout.notification_action;
2158 }
2159
2160 private int getActionTombstoneLayoutResource() {
2161 return mQuantumTheme
2162 ? R.layout.notification_quantum_action_tombstone
2163 : R.layout.notification_action_tombstone;
2164 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002165 }
2166
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002167 /**
2168 * An object that can apply a rich notification style to a {@link Notification.Builder}
2169 * object.
2170 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002171 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002172 private CharSequence mBigContentTitle;
2173 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002174 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002175
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002176 protected Builder mBuilder;
2177
Chris Wrend6297db2012-05-03 16:20:13 -04002178 /**
2179 * Overrides ContentTitle in the big form of the template.
2180 * This defaults to the value passed to setContentTitle().
2181 */
2182 protected void internalSetBigContentTitle(CharSequence title) {
2183 mBigContentTitle = title;
2184 }
2185
2186 /**
2187 * Set the first line of text after the detail section in the big form of the template.
2188 */
2189 protected void internalSetSummaryText(CharSequence cs) {
2190 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002191 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002192 }
2193
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002194 public void setBuilder(Builder builder) {
2195 if (mBuilder != builder) {
2196 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002197 if (mBuilder != null) {
2198 mBuilder.setStyle(this);
2199 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002200 }
2201 }
2202
Chris Wrend6297db2012-05-03 16:20:13 -04002203 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002204 if (mBuilder == null) {
2205 throw new IllegalArgumentException("Style requires a valid Builder object");
2206 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002207 }
Chris Wrend6297db2012-05-03 16:20:13 -04002208
2209 protected RemoteViews getStandardView(int layoutId) {
2210 checkBuilder();
2211
2212 if (mBigContentTitle != null) {
2213 mBuilder.setContentTitle(mBigContentTitle);
2214 }
2215
Chris Wrend6297db2012-05-03 16:20:13 -04002216 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2217
Chris Wrend6297db2012-05-03 16:20:13 -04002218 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2219 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002220 } else {
2221 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002222 }
2223
Daniel Sandler619738c2012-06-07 16:33:08 -04002224 // The last line defaults to the subtext, but can be replaced by mSummaryText
2225 final CharSequence overflowText =
2226 mSummaryTextSet ? mSummaryText
2227 : mBuilder.mSubText;
2228 if (overflowText != null) {
2229 contentView.setTextViewText(R.id.text, overflowText);
2230 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002231 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002232 } else {
2233 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2234 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002235 }
2236
2237 return contentView;
2238 }
2239
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002240 /**
2241 * @hide
2242 */
2243 public void addExtras(Bundle extras) {
2244 if (mSummaryTextSet) {
2245 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2246 }
2247 if (mBigContentTitle != null) {
2248 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2249 }
Chris Wren91ad5632013-06-05 15:05:57 -04002250 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002251 }
2252
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002253 /**
2254 * @hide
2255 */
2256 public abstract Notification buildStyled(Notification wip);
2257
2258 /**
2259 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2260 * attached to.
2261 *
2262 * @return the fully constructed Notification.
2263 */
2264 public Notification build() {
2265 checkBuilder();
2266 return mBuilder.build();
2267 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002268 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002269
2270 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002271 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002272 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002273 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002274 * <pre class="prettyprint">
2275 * Notification noti = new Notification.BigPictureStyle(
2276 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002277 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002278 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002279 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002280 * .setLargeIcon(aBitmap))
2281 * .bigPicture(aBigBitmap)
2282 * .build();
2283 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002284 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002285 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002286 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002287 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002288 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002289 private Bitmap mBigLargeIcon;
2290 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002291
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002292 public BigPictureStyle() {
2293 }
2294
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002295 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002296 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002297 }
2298
Chris Wrend6297db2012-05-03 16:20:13 -04002299 /**
2300 * Overrides ContentTitle in the big form of the template.
2301 * This defaults to the value passed to setContentTitle().
2302 */
2303 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002304 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002305 return this;
2306 }
2307
2308 /**
2309 * Set the first line of text after the detail section in the big form of the template.
2310 */
2311 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002312 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002313 return this;
2314 }
2315
Chris Wren0bd664d2012-08-01 13:56:56 -04002316 /**
2317 * Provide the bitmap to be used as the payload for the BigPicture notification.
2318 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002319 public BigPictureStyle bigPicture(Bitmap b) {
2320 mPicture = b;
2321 return this;
2322 }
2323
Chris Wren3745a3d2012-05-22 15:11:52 -04002324 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002325 * Override the large icon when the big notification is shown.
2326 */
2327 public BigPictureStyle bigLargeIcon(Bitmap b) {
2328 mBigLargeIconSet = true;
2329 mBigLargeIcon = b;
2330 return this;
2331 }
2332
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002333 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002334 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002335
2336 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2337
2338 return contentView;
2339 }
2340
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002341 /**
2342 * @hide
2343 */
2344 public void addExtras(Bundle extras) {
2345 super.addExtras(extras);
2346
2347 if (mBigLargeIconSet) {
2348 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2349 }
2350 extras.putParcelable(EXTRA_PICTURE, mPicture);
2351 }
2352
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002353 /**
2354 * @hide
2355 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002356 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002357 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002358 if (mBigLargeIconSet ) {
2359 mBuilder.mLargeIcon = mBigLargeIcon;
2360 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002361 wip.bigContentView = makeBigContentView();
2362 return wip;
2363 }
2364 }
2365
2366 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002367 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002368 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002369 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002370 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05002371 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002372 * new Notification.Builder()
2373 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2374 * .setContentText(subject)
2375 * .setSmallIcon(R.drawable.new_mail)
2376 * .setLargeIcon(aBitmap))
2377 * .bigText(aVeryLongString)
2378 * .build();
2379 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002380 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002381 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002382 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002383 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002384 private CharSequence mBigText;
2385
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002386 public BigTextStyle() {
2387 }
2388
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002389 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002390 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002391 }
2392
Chris Wrend6297db2012-05-03 16:20:13 -04002393 /**
2394 * Overrides ContentTitle in the big form of the template.
2395 * This defaults to the value passed to setContentTitle().
2396 */
2397 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002398 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002399 return this;
2400 }
2401
2402 /**
2403 * Set the first line of text after the detail section in the big form of the template.
2404 */
2405 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002406 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002407 return this;
2408 }
2409
Chris Wren0bd664d2012-08-01 13:56:56 -04002410 /**
2411 * Provide the longer text to be displayed in the big form of the
2412 * template in place of the content text.
2413 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002414 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002415 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002416 return this;
2417 }
2418
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002419 /**
2420 * @hide
2421 */
2422 public void addExtras(Bundle extras) {
2423 super.addExtras(extras);
2424
2425 extras.putCharSequence(EXTRA_TEXT, mBigText);
2426 }
2427
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002428 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002429 // Remove the content text so line3 only shows if you have a summary
2430 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002431 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04002432
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002433 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08002434
Daniel Sandler619738c2012-06-07 16:33:08 -04002435 if (hadThreeLines) {
2436 // vertical centering
2437 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2438 }
2439
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002440 contentView.setTextViewText(R.id.big_text, mBigText);
2441 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04002442 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002443
2444 return contentView;
2445 }
2446
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002447 /**
2448 * @hide
2449 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002450 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002451 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002452 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002453
2454 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
2455
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002456 return wip;
2457 }
2458 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04002459
2460 /**
2461 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08002462 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002463 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
2464 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002465 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04002466 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04002467 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04002468 * .setContentText(subject)
2469 * .setSmallIcon(R.drawable.new_mail)
2470 * .setLargeIcon(aBitmap))
2471 * .addLine(str1)
2472 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04002473 * .setContentTitle("")
2474 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04002475 * .build();
2476 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002477 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002478 * @see Notification#bigContentView
2479 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002480 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002481 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
2482
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002483 public InboxStyle() {
2484 }
2485
Daniel Sandler879c5e02012-04-17 16:46:51 -04002486 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002487 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002488 }
2489
Chris Wrend6297db2012-05-03 16:20:13 -04002490 /**
2491 * Overrides ContentTitle in the big form of the template.
2492 * This defaults to the value passed to setContentTitle().
2493 */
2494 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002495 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002496 return this;
2497 }
2498
2499 /**
2500 * Set the first line of text after the detail section in the big form of the template.
2501 */
2502 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002503 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002504 return this;
2505 }
2506
Chris Wren0bd664d2012-08-01 13:56:56 -04002507 /**
2508 * Append a line to the digest section of the Inbox notification.
2509 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04002510 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002511 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04002512 return this;
2513 }
2514
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002515 /**
2516 * @hide
2517 */
2518 public void addExtras(Bundle extras) {
2519 super.addExtras(extras);
2520 CharSequence[] a = new CharSequence[mTexts.size()];
2521 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
2522 }
2523
Daniel Sandler879c5e02012-04-17 16:46:51 -04002524 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002525 // Remove the content text so line3 disappears unless you have a summary
2526 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002527 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04002528
Chris Wrend6297db2012-05-03 16:20:13 -04002529 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002530
Chris Wrend6297db2012-05-03 16:20:13 -04002531 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 -04002532 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04002533
Chris Wren4ed80d52012-05-17 09:30:03 -04002534 // Make sure all rows are gone in case we reuse a view.
2535 for (int rowId : rowIds) {
2536 contentView.setViewVisibility(rowId, View.GONE);
2537 }
2538
Chris Wren683ab002012-09-20 10:35:54 -04002539
Daniel Sandler879c5e02012-04-17 16:46:51 -04002540 int i=0;
2541 while (i < mTexts.size() && i < rowIds.length) {
2542 CharSequence str = mTexts.get(i);
2543 if (str != null && !str.equals("")) {
2544 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
2545 contentView.setTextViewText(rowIds[i], str);
2546 }
2547 i++;
2548 }
2549
Chris Wren683ab002012-09-20 10:35:54 -04002550 contentView.setViewVisibility(R.id.inbox_end_pad,
2551 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
2552
2553 contentView.setViewVisibility(R.id.inbox_more,
2554 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04002555
Daniel Sandler879c5e02012-04-17 16:46:51 -04002556 return contentView;
2557 }
2558
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002559 /**
2560 * @hide
2561 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002562 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002563 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002564 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002565
Daniel Sandler879c5e02012-04-17 16:46:51 -04002566 return wip;
2567 }
2568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569}