blob: 12a8ff6b9859017329f53e16685126e484471ada [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 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400447 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 * Notification type: incoming call (voice or video) or similar synchronous communication request.
449 */
450 public static final String KIND_CALL = "android.call";
451
452 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400453 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500454 * Notification type: incoming direct message (SMS, instant message, etc.).
455 */
456 public static final String KIND_MESSAGE = "android.message";
457
458 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400459 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500460 * Notification type: asynchronous bulk message (email).
461 */
462 public static final String KIND_EMAIL = "android.email";
463
464 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400465 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500466 * Notification type: calendar event.
467 */
468 public static final String KIND_EVENT = "android.event";
469
470 /**
Daniel Sandlera90513d2012-06-04 02:11:17 -0400471 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500472 * Notification type: promotion or advertisement.
473 */
474 public static final String KIND_PROMO = "android.promo";
475
476 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400477 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500478 * If this notification matches of one or more special types (see the <code>KIND_*</code>
479 * constants), add them here, best match first.
480 */
481 public String[] kind;
482
483 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400484 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400485 * <p>
486 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
487 * APIs, and are intended to be used by
488 * {@link android.service.notification.NotificationListenerService} implementations to extract
489 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400491 public Bundle extras = new Bundle();
492
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400493 /**
494 * {@link #extras} key: this is the title of the notification,
495 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
496 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500497 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400498
499 /**
500 * {@link #extras} key: this is the title of the notification when shown in expanded form,
501 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
502 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400503 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400504
505 /**
506 * {@link #extras} key: this is the main text payload, as supplied to
507 * {@link Builder#setContentText(CharSequence)}.
508 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500509 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400510
511 /**
512 * {@link #extras} key: this is a third line of text, as supplied to
513 * {@link Builder#setSubText(CharSequence)}.
514 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400515 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400516
517 /**
518 * {@link #extras} key: this is a small piece of additional text as supplied to
519 * {@link Builder#setContentInfo(CharSequence)}.
520 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400521 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400522
523 /**
524 * {@link #extras} key: this is a line of summary information intended to be shown
525 * alongside expanded notifications, as supplied to (e.g.)
526 * {@link BigTextStyle#setSummaryText(CharSequence)}.
527 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400528 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400529
530 /**
531 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
532 * supplied to {@link Builder#setSmallIcon(int)}.
533 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500534 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400535
536 /**
537 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
538 * notification payload, as
539 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
540 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400541 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400542
543 /**
544 * {@link #extras} key: this is a bitmap to be used instead of the one from
545 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
546 * shown in its expanded form, as supplied to
547 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
548 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400549 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400550
551 /**
552 * {@link #extras} key: this is the progress value supplied to
553 * {@link Builder#setProgress(int, int, boolean)}.
554 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400555 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400556
557 /**
558 * {@link #extras} key: this is the maximum value supplied to
559 * {@link Builder#setProgress(int, int, boolean)}.
560 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400561 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400562
563 /**
564 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
565 * {@link Builder#setProgress(int, int, boolean)}.
566 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400567 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400568
569 /**
570 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
571 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
572 * {@link Builder#setUsesChronometer(boolean)}.
573 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400574 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400575
576 /**
577 * {@link #extras} key: whether {@link #when} should be shown,
578 * as supplied to {@link Builder#setShowWhen(boolean)}.
579 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400580 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400581
582 /**
583 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
584 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
585 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400586 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400587
588 /**
589 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
590 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
591 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400592 public static final String EXTRA_TEXT_LINES = "android.textLines";
Chris Wren91ad5632013-06-05 15:05:57 -0400593 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400594
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400595 /**
596 * {@link #extras} key: An array of people that this notification relates to, specified
597 * by contacts provider contact URI.
598 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400599 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500600
601 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400602 * @hide
603 * Extra added by NotificationManagerService to indicate whether a NotificationScorer
604 * modified the Notifications's score.
605 */
606 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
607
608 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400609 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400610 * @hide
611 */
612 public static final String EXTRA_AS_HEADS_UP = "headsup";
613
614 /**
Jorim Jaggi39fa59f2014-02-25 15:38:45 +0100615 * Extra added from {@link Notification.Builder} to indicate that the remote views were inflated
616 * from the builder, as opposed to being created directly from the application.
617 * @hide
618 */
619 public static final String EXTRA_BUILDER_REMOTE_VIEWS = "android.builderRemoteViews";
620
621 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400622 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400623 * @hide
624 */
625 public static final int HEADS_UP_NEVER = 0;
626
627 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400628 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400629 * @hide
630 */
631 public static final int HEADS_UP_ALLOWED = 1;
632
633 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400634 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400635 * @hide
636 */
637 public static final int HEADS_UP_REQUESTED = 2;
638
639 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400640 * Structure to encapsulate a named action that can be shown as part of this notification.
641 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
642 * selected by the user.
643 * <p>
644 * Apps should use {@link Builder#addAction(int, CharSequence, PendingIntent)} to create and
645 * attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400646 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500647 public static class Action implements Parcelable {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400648 /**
649 * Small icon representing the action.
650 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400651 public int icon;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400652 /**
653 * Title of the action.
654 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400655 public CharSequence title;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400656 /**
657 * Intent to send when the user invokes this action. May be null, in which case the action
658 * may be rendered in a disabled presentation by the system UI.
659 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400660 public PendingIntent actionIntent;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400661
662 private Action() { }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400663 private Action(Parcel in) {
664 icon = in.readInt();
665 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
666 if (in.readInt() == 1) {
667 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
668 }
669 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400670 /**
671 * Use {@link Builder#addAction(int, CharSequence, PendingIntent)}.
672 */
673 public Action(int icon, CharSequence title, PendingIntent intent) {
674 this.icon = icon;
675 this.title = title;
676 this.actionIntent = intent;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400677 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400678
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400679 @Override
680 public Action clone() {
681 return new Action(
682 this.icon,
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400683 this.title,
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400684 this.actionIntent // safe to alias
685 );
686 }
687 @Override
688 public int describeContents() {
689 return 0;
690 }
691 @Override
692 public void writeToParcel(Parcel out, int flags) {
693 out.writeInt(icon);
694 TextUtils.writeToParcel(title, out, flags);
695 if (actionIntent != null) {
696 out.writeInt(1);
697 actionIntent.writeToParcel(out, flags);
698 } else {
699 out.writeInt(0);
700 }
701 }
702 public static final Parcelable.Creator<Action> CREATOR
703 = new Parcelable.Creator<Action>() {
704 public Action createFromParcel(Parcel in) {
705 return new Action(in);
706 }
707 public Action[] newArray(int size) {
708 return new Action[size];
709 }
710 };
711 }
712
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400713 /**
714 * Array of all {@link Action} structures attached to this notification by
715 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
716 * {@link android.service.notification.NotificationListenerService} that provide an alternative
717 * interface for invoking actions.
718 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500719 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400720
721 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600722 * Replacement version of this notification whose content will be shown
723 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
724 * and {@link #VISIBILITY_PUBLIC}.
725 */
726 public Notification publicVersion;
727
728 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800730 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 */
732 public Notification()
733 {
734 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500735 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
737
738 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 * @hide
740 */
741 public Notification(Context context, int icon, CharSequence tickerText, long when,
742 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
743 {
744 this.when = when;
745 this.icon = icon;
746 this.tickerText = tickerText;
747 setLatestEventInfo(context, contentTitle, contentText,
748 PendingIntent.getActivity(context, 0, contentIntent, 0));
749 }
750
751 /**
752 * Constructs a Notification object with the information needed to
753 * have a status bar icon without the standard expanded view.
754 *
755 * @param icon The resource id of the icon to put in the status bar.
756 * @param tickerText The text that flows by in the status bar when the notification first
757 * activates.
758 * @param when The time to show in the time field. In the System.currentTimeMillis
759 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800760 *
761 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800763 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 public Notification(int icon, CharSequence tickerText, long when)
765 {
766 this.icon = icon;
767 this.tickerText = tickerText;
768 this.when = when;
769 }
770
771 /**
772 * Unflatten the notification from a parcel.
773 */
774 public Notification(Parcel parcel)
775 {
776 int version = parcel.readInt();
777
778 when = parcel.readLong();
779 icon = parcel.readInt();
780 number = parcel.readInt();
781 if (parcel.readInt() != 0) {
782 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
783 }
784 if (parcel.readInt() != 0) {
785 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
786 }
787 if (parcel.readInt() != 0) {
788 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
789 }
790 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800791 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400792 }
793 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
795 }
Joe Onorato561d3852010-11-20 18:09:34 -0800796 if (parcel.readInt() != 0) {
797 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 defaults = parcel.readInt();
800 flags = parcel.readInt();
801 if (parcel.readInt() != 0) {
802 sound = Uri.CREATOR.createFromParcel(parcel);
803 }
804
805 audioStreamType = parcel.readInt();
806 vibrate = parcel.createLongArray();
807 ledARGB = parcel.readInt();
808 ledOnMS = parcel.readInt();
809 ledOffMS = parcel.readInt();
810 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400811
812 if (parcel.readInt() != 0) {
813 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
814 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500815
816 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400817
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500818 kind = parcel.createStringArray(); // may set kind to null
819
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500820 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400821
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500822 actions = parcel.createTypedArray(Action.CREATOR); // may be null
823
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400824 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400825 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
826 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600827
Chris Wren8fd39ec2014-02-27 17:43:26 -0500828 if (parcel.readInt() != 0) {
829 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
830 }
831
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600832 visibility = parcel.readInt();
833
834 if (parcel.readInt() != 0) {
835 publicVersion = Notification.CREATOR.createFromParcel(parcel);
836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
Andy Stadler110988c2010-12-03 14:29:16 -0800839 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700840 public Notification clone() {
841 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -0400842 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500843 return that;
844 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700845
Daniel Sandler1a497d32013-04-18 14:52:45 -0400846 /**
847 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
848 * of this into that.
849 * @hide
850 */
851 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700852 that.when = this.when;
853 that.icon = this.icon;
854 that.number = this.number;
855
856 // PendingIntents are global, so there's no reason (or way) to clone them.
857 that.contentIntent = this.contentIntent;
858 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400859 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700860
861 if (this.tickerText != null) {
862 that.tickerText = this.tickerText.toString();
863 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400864 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800865 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400866 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400867 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700868 that.contentView = this.contentView.clone();
869 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400870 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800871 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
872 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100873 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700874 that.sound = this.sound; // android.net.Uri is immutable
875 that.audioStreamType = this.audioStreamType;
876
877 final long[] vibrate = this.vibrate;
878 if (vibrate != null) {
879 final int N = vibrate.length;
880 final long[] vib = that.vibrate = new long[N];
881 System.arraycopy(vibrate, 0, vib, 0, N);
882 }
883
884 that.ledARGB = this.ledARGB;
885 that.ledOnMS = this.ledOnMS;
886 that.ledOffMS = this.ledOffMS;
887 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500888
Joe Onorato18e69df2010-05-17 22:26:12 -0700889 that.flags = this.flags;
890
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500891 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800892
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500893 final String[] thiskind = this.kind;
894 if (thiskind != null) {
895 final int N = thiskind.length;
896 final String[] thatkind = that.kind = new String[N];
897 System.arraycopy(thiskind, 0, thatkind, 0, N);
898 }
899
900 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400901 try {
902 that.extras = new Bundle(this.extras);
903 // will unparcel
904 that.extras.size();
905 } catch (BadParcelableException e) {
906 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
907 that.extras = null;
908 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500909 }
910
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500911 if (this.actions != null) {
912 that.actions = new Action[this.actions.length];
913 for(int i=0; i<this.actions.length; i++) {
914 that.actions[i] = this.actions[i].clone();
915 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400916 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500917
Daniel Sandler1a497d32013-04-18 14:52:45 -0400918 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400919 that.bigContentView = this.bigContentView.clone();
920 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400921
Chris Wren8fd39ec2014-02-27 17:43:26 -0500922 if (heavy && this.headsUpContentView != null) {
923 that.headsUpContentView = this.headsUpContentView.clone();
924 }
925
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600926 that.visibility = this.visibility;
927
928 if (this.publicVersion != null) {
929 that.publicVersion = new Notification();
930 this.publicVersion.cloneInto(that.publicVersion, heavy);
931 }
932
Daniel Sandler1a497d32013-04-18 14:52:45 -0400933 if (!heavy) {
934 that.lightenPayload(); // will clean out extras
935 }
936 }
937
938 /**
939 * Removes heavyweight parts of the Notification object for archival or for sending to
940 * listeners when the full contents are not necessary.
941 * @hide
942 */
943 public final void lightenPayload() {
944 tickerView = null;
945 contentView = null;
946 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -0500947 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -0400948 largeIcon = null;
949 if (extras != null) {
950 extras.remove(Notification.EXTRA_LARGE_ICON);
951 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
952 extras.remove(Notification.EXTRA_PICTURE);
953 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700954 }
955
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400956 /**
957 * Make sure this CharSequence is safe to put into a bundle, which basically
958 * means it had better not be some custom Parcelable implementation.
959 * @hide
960 */
961 public static CharSequence safeCharSequence(CharSequence cs) {
962 if (cs instanceof Parcelable) {
963 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
964 + " instance is a custom Parcelable and not allowed in Notification");
965 return cs.toString();
966 }
967
968 return cs;
969 }
970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 public int describeContents() {
972 return 0;
973 }
974
975 /**
976 * Flatten this notification from a parcel.
977 */
978 public void writeToParcel(Parcel parcel, int flags)
979 {
980 parcel.writeInt(1);
981
982 parcel.writeLong(when);
983 parcel.writeInt(icon);
984 parcel.writeInt(number);
985 if (contentIntent != null) {
986 parcel.writeInt(1);
987 contentIntent.writeToParcel(parcel, 0);
988 } else {
989 parcel.writeInt(0);
990 }
991 if (deleteIntent != null) {
992 parcel.writeInt(1);
993 deleteIntent.writeToParcel(parcel, 0);
994 } else {
995 parcel.writeInt(0);
996 }
997 if (tickerText != null) {
998 parcel.writeInt(1);
999 TextUtils.writeToParcel(tickerText, parcel, flags);
1000 } else {
1001 parcel.writeInt(0);
1002 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001003 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001004 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001005 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001006 } else {
1007 parcel.writeInt(0);
1008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 if (contentView != null) {
1010 parcel.writeInt(1);
1011 contentView.writeToParcel(parcel, 0);
1012 } else {
1013 parcel.writeInt(0);
1014 }
Joe Onorato561d3852010-11-20 18:09:34 -08001015 if (largeIcon != null) {
1016 parcel.writeInt(1);
1017 largeIcon.writeToParcel(parcel, 0);
1018 } else {
1019 parcel.writeInt(0);
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021
1022 parcel.writeInt(defaults);
1023 parcel.writeInt(this.flags);
1024
1025 if (sound != null) {
1026 parcel.writeInt(1);
1027 sound.writeToParcel(parcel, 0);
1028 } else {
1029 parcel.writeInt(0);
1030 }
1031 parcel.writeInt(audioStreamType);
1032 parcel.writeLongArray(vibrate);
1033 parcel.writeInt(ledARGB);
1034 parcel.writeInt(ledOnMS);
1035 parcel.writeInt(ledOffMS);
1036 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001037
1038 if (fullScreenIntent != null) {
1039 parcel.writeInt(1);
1040 fullScreenIntent.writeToParcel(parcel, 0);
1041 } else {
1042 parcel.writeInt(0);
1043 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001044
1045 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001046
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001047 parcel.writeStringArray(kind); // ok for null
Joe Malin8d40d042012-11-05 11:36:40 -08001048
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001049 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001050
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001051 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001052
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001053 if (bigContentView != null) {
1054 parcel.writeInt(1);
1055 bigContentView.writeToParcel(parcel, 0);
1056 } else {
1057 parcel.writeInt(0);
1058 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001059
Chris Wren8fd39ec2014-02-27 17:43:26 -05001060 if (headsUpContentView != null) {
1061 parcel.writeInt(1);
1062 headsUpContentView.writeToParcel(parcel, 0);
1063 } else {
1064 parcel.writeInt(0);
1065 }
1066
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001067 parcel.writeInt(visibility);
1068
1069 if (publicVersion != null) {
1070 parcel.writeInt(1);
1071 publicVersion.writeToParcel(parcel, 0);
1072 } else {
1073 parcel.writeInt(0);
1074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
1077 /**
1078 * Parcelable.Creator that instantiates Notification objects
1079 */
1080 public static final Parcelable.Creator<Notification> CREATOR
1081 = new Parcelable.Creator<Notification>()
1082 {
1083 public Notification createFromParcel(Parcel parcel)
1084 {
1085 return new Notification(parcel);
1086 }
1087
1088 public Notification[] newArray(int size)
1089 {
1090 return new Notification[size];
1091 }
1092 };
1093
1094 /**
1095 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1096 * layout.
1097 *
1098 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1099 * in the view.</p>
1100 * @param context The context for your application / activity.
1101 * @param contentTitle The title that goes in the expanded entry.
1102 * @param contentText The text that goes in the expanded entry.
1103 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1104 * If this is an activity, it must include the
1105 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001106 * that you take care of task management as described in the
1107 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1108 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001109 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001110 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001112 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 public void setLatestEventInfo(Context context,
1114 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001115 Notification.Builder builder = new Notification.Builder(context);
1116
1117 // First, ensure that key pieces of information that may have been set directly
1118 // are preserved
1119 builder.setWhen(this.when);
1120 builder.setSmallIcon(this.icon);
1121 builder.setPriority(this.priority);
1122 builder.setTicker(this.tickerText);
1123 builder.setNumber(this.number);
1124 builder.mFlags = this.flags;
1125 builder.setSound(this.sound, this.audioStreamType);
1126 builder.setDefaults(this.defaults);
1127 builder.setVibrate(this.vibrate);
1128
1129 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001131 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
1133 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001134 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001136 builder.setContentIntent(contentIntent);
1137 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139
1140 @Override
1141 public String toString() {
1142 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001143 sb.append("Notification(pri=");
1144 sb.append(priority);
1145 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001146 if (contentView != null) {
1147 sb.append(contentView.getPackage());
1148 sb.append("/0x");
1149 sb.append(Integer.toHexString(contentView.getLayoutId()));
1150 } else {
1151 sb.append("null");
1152 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001153 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001154 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001155 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1156 sb.append("default");
1157 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 int N = this.vibrate.length-1;
1159 sb.append("[");
1160 for (int i=0; i<N; i++) {
1161 sb.append(this.vibrate[i]);
1162 sb.append(',');
1163 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001164 if (N != -1) {
1165 sb.append(this.vibrate[N]);
1166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 } else {
1169 sb.append("null");
1170 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001171 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001172 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001174 } else if (this.sound != null) {
1175 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 } else {
1177 sb.append("null");
1178 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001179 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001181 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001182 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001183 sb.append(" kind=[");
1184 if (this.kind == null) {
1185 sb.append("null");
1186 } else {
1187 for (int i=0; i<this.kind.length; i++) {
1188 if (i>0) sb.append(",");
1189 sb.append(this.kind[i]);
1190 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001191 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001192 sb.append("]");
1193 if (actions != null) {
1194 sb.append(" ");
1195 sb.append(actions.length);
1196 sb.append(" action");
1197 if (actions.length > 1) sb.append("s");
1198 }
1199 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 return sb.toString();
1201 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001202
Jeff Sharkey6d515712012-09-20 16:06:08 -07001203 /** {@hide} */
1204 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001205 if (user.getIdentifier() == UserHandle.USER_ALL) {
1206 user = UserHandle.OWNER;
1207 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001208 if (tickerView != null) {
1209 tickerView.setUser(user);
1210 }
1211 if (contentView != null) {
1212 contentView.setUser(user);
1213 }
1214 if (bigContentView != null) {
1215 bigContentView.setUser(user);
1216 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001217 if (headsUpContentView != null) {
1218 headsUpContentView.setUser(user);
1219 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001220 }
1221
Joe Onoratocb109a02011-01-18 17:57:41 -08001222 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001223 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001225 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001226 * content views using the platform's notification layout template. If your app supports
1227 * versions of Android as old as API level 4, you can instead use
1228 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1229 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1230 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001231 *
Scott Main183bf112012-08-13 19:12:13 -07001232 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001234 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001235 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001236 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1237 * .setContentText(subject)
1238 * .setSmallIcon(R.drawable.new_mail)
1239 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001240 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001241 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001242 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001243 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001244 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001245
Joe Onorato46439ce2010-11-19 13:56:21 -08001246 private Context mContext;
1247
1248 private long mWhen;
1249 private int mSmallIcon;
1250 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001251 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001252 private CharSequence mContentTitle;
1253 private CharSequence mContentText;
1254 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001255 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001256 private PendingIntent mContentIntent;
1257 private RemoteViews mContentView;
1258 private PendingIntent mDeleteIntent;
1259 private PendingIntent mFullScreenIntent;
1260 private CharSequence mTickerText;
1261 private RemoteViews mTickerView;
1262 private Bitmap mLargeIcon;
1263 private Uri mSound;
1264 private int mAudioStreamType;
1265 private long[] mVibrate;
1266 private int mLedArgb;
1267 private int mLedOnMs;
1268 private int mLedOffMs;
1269 private int mDefaults;
1270 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001271 private int mProgressMax;
1272 private int mProgress;
1273 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001274 private ArrayList<String> mKindList = new ArrayList<String>(1);
1275 private Bundle mExtras;
1276 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001277 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001278 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001279 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001280 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001281 private int mVisibility = VISIBILITY_PRIVATE;
1282 private Notification mPublicVersion = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001283 private boolean mQuantumTheme;
Joe Onorato46439ce2010-11-19 13:56:21 -08001284
Joe Onoratocb109a02011-01-18 17:57:41 -08001285 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001286 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001287 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001288
1289 * <table>
1290 * <tr><th align=right>priority</th>
1291 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1292 * <tr><th align=right>when</th>
1293 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1294 * <tr><th align=right>audio stream</th>
1295 * <td>{@link #STREAM_DEFAULT}</td></tr>
1296 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001297 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001298
1299 * @param context
1300 * A {@link Context} that will be used by the Builder to construct the
1301 * RemoteViews. The Context will not be held past the lifetime of this Builder
1302 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001303 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001304 public Builder(Context context) {
1305 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001306
1307 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001308 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001309 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001310 mPriority = PRIORITY_DEFAULT;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001311
1312 // TODO: Decide on targetSdk from calling app whether to use quantum theme.
1313 mQuantumTheme = true;
Joe Onorato46439ce2010-11-19 13:56:21 -08001314 }
1315
Joe Onoratocb109a02011-01-18 17:57:41 -08001316 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001317 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001318 * It will be shown in the notification content view by default; use
1319 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001320 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001321 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001322 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001323 public Builder setWhen(long when) {
1324 mWhen = when;
1325 return this;
1326 }
1327
Joe Onoratocb109a02011-01-18 17:57:41 -08001328 /**
Daniel Sandler0c890492012-09-12 17:23:10 -07001329 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown
1330 * in the content view.
1331 */
1332 public Builder setShowWhen(boolean show) {
1333 mShowWhen = show;
1334 return this;
1335 }
1336
1337 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001338 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001339 *
1340 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001341 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001342 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001343 * Useful when showing an elapsed time (like an ongoing phone call).
1344 *
1345 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001346 * @see Notification#when
1347 */
1348 public Builder setUsesChronometer(boolean b) {
1349 mUseChronometer = b;
1350 return this;
1351 }
1352
1353 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001354 * Set the small icon resource, which will be used to represent the notification in the
1355 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001356 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001357
1358 * The platform template for the expanded view will draw this icon in the left, unless a
1359 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1360 * icon will be moved to the right-hand side.
1361 *
1362
1363 * @param icon
1364 * A resource ID in the application's package of the drawable to use.
1365 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001366 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001367 public Builder setSmallIcon(int icon) {
1368 mSmallIcon = icon;
1369 return this;
1370 }
1371
Joe Onoratocb109a02011-01-18 17:57:41 -08001372 /**
1373 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1374 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1375 * LevelListDrawable}.
1376 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001377 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001378 * @param level The level to use for the icon.
1379 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001380 * @see Notification#icon
1381 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001382 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001383 public Builder setSmallIcon(int icon, int level) {
1384 mSmallIcon = icon;
1385 mSmallIconLevel = level;
1386 return this;
1387 }
1388
Joe Onoratocb109a02011-01-18 17:57:41 -08001389 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001390 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001391 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001392 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001393 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001394 return this;
1395 }
1396
Joe Onoratocb109a02011-01-18 17:57:41 -08001397 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001398 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001399 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001400 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001401 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001402 return this;
1403 }
1404
Joe Onoratocb109a02011-01-18 17:57:41 -08001405 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001406 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001407 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1408 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001409 */
1410 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001411 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001412 return this;
1413 }
1414
1415 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001416 * Set the large number at the right-hand side of the notification. This is
1417 * equivalent to setContentInfo, although it might show the number in a different
1418 * font size for readability.
1419 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001420 public Builder setNumber(int number) {
1421 mNumber = number;
1422 return this;
1423 }
1424
Joe Onoratocb109a02011-01-18 17:57:41 -08001425 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001426 * A small piece of additional information pertaining to this notification.
1427 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001428 * The platform template will draw this on the last line of the notification, at the far
1429 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001430 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001431 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001432 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001433 return this;
1434 }
1435
Joe Onoratocb109a02011-01-18 17:57:41 -08001436 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001437 * Set the progress this notification represents.
1438 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001439 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001440 */
1441 public Builder setProgress(int max, int progress, boolean indeterminate) {
1442 mProgressMax = max;
1443 mProgress = progress;
1444 mProgressIndeterminate = indeterminate;
1445 return this;
1446 }
1447
1448 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001449 * Supply a custom RemoteViews to use instead of the platform template.
1450 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001451 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001452 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001453 public Builder setContent(RemoteViews views) {
1454 mContentView = views;
1455 return this;
1456 }
1457
Joe Onoratocb109a02011-01-18 17:57:41 -08001458 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001459 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1460 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001461 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1462 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1463 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001464 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001465 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001466 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001467 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001468 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001469 public Builder setContentIntent(PendingIntent intent) {
1470 mContentIntent = intent;
1471 return this;
1472 }
1473
Joe Onoratocb109a02011-01-18 17:57:41 -08001474 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001475 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1476 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001477 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001478 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001479 public Builder setDeleteIntent(PendingIntent intent) {
1480 mDeleteIntent = intent;
1481 return this;
1482 }
1483
Joe Onoratocb109a02011-01-18 17:57:41 -08001484 /**
1485 * An intent to launch instead of posting the notification to the status bar.
1486 * Only for use with extremely high-priority notifications demanding the user's
1487 * <strong>immediate</strong> attention, such as an incoming phone call or
1488 * alarm clock that the user has explicitly set to a particular time.
1489 * If this facility is used for something else, please give the user an option
1490 * to turn it off and use a normal notification, as this can be extremely
1491 * disruptive.
1492 *
1493 * @param intent The pending intent to launch.
1494 * @param highPriority Passing true will cause this notification to be sent
1495 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001496 *
1497 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001498 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001499 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1500 mFullScreenIntent = intent;
1501 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1502 return this;
1503 }
1504
Joe Onoratocb109a02011-01-18 17:57:41 -08001505 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001506 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001507 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001508 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001509 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001510 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001511 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001512 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001513 return this;
1514 }
1515
Joe Onoratocb109a02011-01-18 17:57:41 -08001516 /**
1517 * Set the text that is displayed in the status bar when the notification first
1518 * arrives, and also a RemoteViews object that may be displayed instead on some
1519 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001520 *
1521 * @see Notification#tickerText
1522 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001523 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001524 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001525 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001526 mTickerView = views;
1527 return this;
1528 }
1529
Joe Onoratocb109a02011-01-18 17:57:41 -08001530 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001531 * Add a large icon to the notification (and the ticker on some devices).
1532 *
1533 * In the platform template, this image will be shown on the left of the notification view
1534 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1535 *
1536 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001537 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001538 public Builder setLargeIcon(Bitmap icon) {
1539 mLargeIcon = icon;
1540 return this;
1541 }
1542
Joe Onoratocb109a02011-01-18 17:57:41 -08001543 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001544 * Set the sound to play.
1545 *
1546 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1547 *
1548 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001549 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001550 public Builder setSound(Uri sound) {
1551 mSound = sound;
1552 mAudioStreamType = STREAM_DEFAULT;
1553 return this;
1554 }
1555
Joe Onoratocb109a02011-01-18 17:57:41 -08001556 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001557 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001558 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001559 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1560 *
1561 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001562 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001563 public Builder setSound(Uri sound, int streamType) {
1564 mSound = sound;
1565 mAudioStreamType = streamType;
1566 return this;
1567 }
1568
Joe Onoratocb109a02011-01-18 17:57:41 -08001569 /**
1570 * Set the vibration pattern to use.
1571 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001572
1573 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1574 * <code>pattern</code> parameter.
1575 *
1576
1577 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001578 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001579 public Builder setVibrate(long[] pattern) {
1580 mVibrate = pattern;
1581 return this;
1582 }
1583
Joe Onoratocb109a02011-01-18 17:57:41 -08001584 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001585 * Set the desired color for the indicator LED on the device, as well as the
1586 * blink duty cycle (specified in milliseconds).
1587 *
1588
1589 * Not all devices will honor all (or even any) of these values.
1590 *
1591
1592 * @see Notification#ledARGB
1593 * @see Notification#ledOnMS
1594 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001595 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001596 public Builder setLights(int argb, int onMs, int offMs) {
1597 mLedArgb = argb;
1598 mLedOnMs = onMs;
1599 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001600 return this;
1601 }
1602
Joe Onoratocb109a02011-01-18 17:57:41 -08001603 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001604 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001605 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001606
1607 * Ongoing notifications cannot be dismissed by the user, so your application or service
1608 * must take care of canceling them.
1609 *
1610
1611 * They are typically used to indicate a background task that the user is actively engaged
1612 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1613 * (e.g., a file download, sync operation, active network connection).
1614 *
1615
1616 * @see Notification#FLAG_ONGOING_EVENT
1617 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001618 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001619 public Builder setOngoing(boolean ongoing) {
1620 setFlag(FLAG_ONGOING_EVENT, ongoing);
1621 return this;
1622 }
1623
Joe Onoratocb109a02011-01-18 17:57:41 -08001624 /**
1625 * Set this flag if you would only like the sound, vibrate
1626 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001627 *
1628 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001629 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001630 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1631 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1632 return this;
1633 }
1634
Joe Onoratocb109a02011-01-18 17:57:41 -08001635 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001636 * Make this notification automatically dismissed when the user touches it. The
1637 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1638 *
1639 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001640 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001641 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001642 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001643 return this;
1644 }
1645
Joe Onoratocb109a02011-01-18 17:57:41 -08001646 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08001647 * Set whether or not this notification should not bridge to other devices.
1648 *
1649 * <p>Some notifications can be bridged to other devices for remote display.
1650 * This hint can be set to recommend this notification not be bridged.
1651 */
1652 public Builder setLocalOnly(boolean localOnly) {
1653 setFlag(FLAG_LOCAL_ONLY, localOnly);
1654 return this;
1655 }
1656
1657 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001658 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001659 * <p>
1660 * The value should be one or more of the following fields combined with
1661 * bitwise-or:
1662 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1663 * <p>
1664 * For all default values, use {@link #DEFAULT_ALL}.
1665 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001666 public Builder setDefaults(int defaults) {
1667 mDefaults = defaults;
1668 return this;
1669 }
1670
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001671 /**
1672 * Set the priority of this notification.
1673 *
1674 * @see Notification#priority
1675 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001676 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001677 mPriority = pri;
1678 return this;
1679 }
Joe Malin8d40d042012-11-05 11:36:40 -08001680
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001681 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001682 * @hide
Joe Malin8d40d042012-11-05 11:36:40 -08001683 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001684 * Add a kind (category) to this notification. Optional.
Joe Malin8d40d042012-11-05 11:36:40 -08001685 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001686 * @see Notification#kind
1687 */
1688 public Builder addKind(String k) {
1689 mKindList.add(k);
1690 return this;
1691 }
1692
1693 /**
Griff Hazen720042b2014-02-24 15:46:56 -08001694 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001695 *
Griff Hazen720042b2014-02-24 15:46:56 -08001696 * <p>Values within the Bundle will replace existing extras values in this Builder.
1697 *
1698 * @see Notification#extras
1699 */
1700 public Builder addExtras(Bundle bag) {
1701 if (mExtras == null) {
1702 mExtras = new Bundle(bag);
1703 } else {
1704 mExtras.putAll(bag);
1705 }
1706 return this;
1707 }
1708
1709 /**
1710 * Set metadata for this notification.
1711 *
1712 * <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 -04001713 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001714 * called.
1715 *
Griff Hazen720042b2014-02-24 15:46:56 -08001716 * <p>Replaces any existing extras values with those from the provided Bundle.
1717 * Use {@link #addExtras} to merge in metadata instead.
1718 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001719 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001720 */
1721 public Builder setExtras(Bundle bag) {
1722 mExtras = bag;
1723 return this;
1724 }
1725
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001726 /**
Griff Hazen720042b2014-02-24 15:46:56 -08001727 * Get the current metadata Bundle used by this notification Builder.
1728 *
1729 * <p>The returned Bundle is shared with this Builder.
1730 *
1731 * <p>The current contents of this Bundle are copied into the Notification each time
1732 * {@link #build()} is called.
1733 *
1734 * @see Notification#extras
1735 */
1736 public Bundle getExtras() {
1737 if (mExtras == null) {
1738 mExtras = new Bundle();
1739 }
1740 return mExtras;
1741 }
1742
1743 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001744 * Add an action to this notification. Actions are typically displayed by
1745 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001746 * <p>
1747 * Every action must have an icon (32dp square and matching the
1748 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
1749 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
1750 * <p>
1751 * A notification in its expanded form can display up to 3 actions, from left to right in
1752 * the order they were added. Actions will not be displayed when the notification is
1753 * collapsed, however, so be sure that any essential functions may be accessed by the user
1754 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001755 *
1756 * @param icon Resource ID of a drawable that represents the action.
1757 * @param title Text describing the action.
1758 * @param intent PendingIntent to be fired when the action is invoked.
1759 */
1760 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001761 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001762 return this;
1763 }
1764
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001765 /**
1766 * Add a rich notification style to be applied at build time.
1767 *
1768 * @param style Object responsible for modifying the notification style.
1769 */
1770 public Builder setStyle(Style style) {
1771 if (mStyle != style) {
1772 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001773 if (mStyle != null) {
1774 mStyle.setBuilder(this);
1775 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001776 }
1777 return this;
1778 }
1779
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001780 /**
1781 * Specify the value of {@link #visibility}.
1782
1783 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
1784 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
1785 *
1786 * @return The same Builder.
1787 */
1788 public Builder setVisibility(int visibility) {
1789 mVisibility = visibility;
1790 return this;
1791 }
1792
1793 /**
1794 * Supply a replacement Notification whose contents should be shown in insecure contexts
1795 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
1796 * @param n A replacement notification, presumably with some or all info redacted.
1797 * @return The same Builder.
1798 */
1799 public Builder setPublicVersion(Notification n) {
1800 mPublicVersion = n;
1801 return this;
1802 }
1803
Joe Onorato46439ce2010-11-19 13:56:21 -08001804 private void setFlag(int mask, boolean value) {
1805 if (value) {
1806 mFlags |= mask;
1807 } else {
1808 mFlags &= ~mask;
1809 }
1810 }
1811
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001812 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08001813 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001814 boolean showLine3 = false;
1815 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001816 int smallIconImageViewId = R.id.icon;
1817 if (mLargeIcon != null) {
1818 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1819 smallIconImageViewId = R.id.right_icon;
1820 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001821 if (!mQuantumTheme && mPriority < PRIORITY_LOW) {
Daniel Sandlere95658c2012-05-10 00:33:54 -04001822 contentView.setInt(R.id.icon,
1823 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1824 contentView.setInt(R.id.status_bar_latest_event_content,
1825 "setBackgroundResource", R.drawable.notification_bg_low);
1826 }
Joe Onorato561d3852010-11-20 18:09:34 -08001827 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001828 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1829 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001830 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001831 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001832 }
1833 if (mContentTitle != null) {
1834 contentView.setTextViewText(R.id.title, mContentTitle);
1835 }
1836 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001837 contentView.setTextViewText(R.id.text, mContentText);
1838 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001839 }
1840 if (mContentInfo != null) {
1841 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001842 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001843 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001844 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001845 final int tooBig = mContext.getResources().getInteger(
1846 R.integer.status_bar_notification_info_maxnum);
1847 if (mNumber > tooBig) {
1848 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1849 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001850 } else {
1851 NumberFormat f = NumberFormat.getIntegerInstance();
1852 contentView.setTextViewText(R.id.info, f.format(mNumber));
1853 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001854 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001855 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001856 } else {
1857 contentView.setViewVisibility(R.id.info, View.GONE);
1858 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001859
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001860 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001861 if (mSubText != null) {
1862 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001863 if (mContentText != null) {
1864 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001865 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
1866 showLine2 = true;
1867 } else {
1868 contentView.setViewVisibility(R.id.text2, View.GONE);
1869 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001870 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001871 contentView.setViewVisibility(R.id.text2, View.GONE);
1872 if (mProgressMax != 0 || mProgressIndeterminate) {
1873 contentView.setProgressBar(
1874 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1875 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001876 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001877 } else {
1878 contentView.setViewVisibility(R.id.progress, View.GONE);
1879 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001880 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001881 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001882 if (fitIn1U) {
1883 // need to shrink all the type to make sure everything fits
1884 final Resources res = mContext.getResources();
1885 final float subTextSize = res.getDimensionPixelSize(
1886 R.dimen.notification_subtext_size);
1887 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
1888 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001889 // vertical centering
1890 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1891 }
1892
Daniel Sandler0c890492012-09-12 17:23:10 -07001893 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001894 if (mUseChronometer) {
1895 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1896 contentView.setLong(R.id.chronometer, "setBase",
1897 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1898 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1899 } else {
1900 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1901 contentView.setLong(R.id.time, "setTime", mWhen);
1902 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001903 } else {
1904 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001905 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001906
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001907 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001908 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001909 return contentView;
1910 }
1911
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001912 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001913 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001914
1915 int N = mActions.size();
1916 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001917 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001918 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001919 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001920 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04001921 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001922 for (int i=0; i<N; i++) {
1923 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001924 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001925 big.addView(R.id.actions, button);
1926 }
1927 }
1928 return big;
1929 }
1930
Joe Onorato46439ce2010-11-19 13:56:21 -08001931 private RemoteViews makeContentView() {
1932 if (mContentView != null) {
1933 return mContentView;
1934 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001935 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001936 }
1937 }
1938
1939 private RemoteViews makeTickerView() {
1940 if (mTickerView != null) {
1941 return mTickerView;
1942 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001943 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001944 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001945 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001946 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08001947 } else {
1948 return null;
1949 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001950 }
1951 }
1952
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001953 private RemoteViews makeBigContentView() {
1954 if (mActions.size() == 0) return null;
1955
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001956 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001957 }
1958
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001959 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05001960 if (mActions.size() == 0) return null;
1961
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001962 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05001963 }
1964
1965
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001966 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04001967 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08001968 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001969 tombstone ? getActionTombstoneLayoutResource()
1970 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05001971 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001972 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001973 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04001974 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04001975 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001976 button.setContentDescription(R.id.action0, action.title);
1977 return button;
1978 }
1979
Joe Onoratocb109a02011-01-18 17:57:41 -08001980 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001981 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001982 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08001983 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001984 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08001985 Notification n = new Notification();
1986 n.when = mWhen;
1987 n.icon = mSmallIcon;
1988 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001989 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001990 n.contentView = makeContentView();
1991 n.contentIntent = mContentIntent;
1992 n.deleteIntent = mDeleteIntent;
1993 n.fullScreenIntent = mFullScreenIntent;
1994 n.tickerText = mTickerText;
1995 n.tickerView = makeTickerView();
1996 n.largeIcon = mLargeIcon;
1997 n.sound = mSound;
1998 n.audioStreamType = mAudioStreamType;
1999 n.vibrate = mVibrate;
2000 n.ledARGB = mLedArgb;
2001 n.ledOnMS = mLedOnMs;
2002 n.ledOffMS = mLedOffMs;
2003 n.defaults = mDefaults;
2004 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002005 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002006 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002007 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002008 n.flags |= FLAG_SHOW_LIGHTS;
2009 }
2010 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2011 n.flags |= FLAG_SHOW_LIGHTS;
2012 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002013 if (mKindList.size() > 0) {
2014 n.kind = new String[mKindList.size()];
2015 mKindList.toArray(n.kind);
2016 } else {
2017 n.kind = null;
2018 }
2019 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002020 if (mActions.size() > 0) {
2021 n.actions = new Action[mActions.size()];
2022 mActions.toArray(n.actions);
2023 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002024 n.visibility = mVisibility;
2025
2026 if (mPublicVersion != null) {
2027 n.publicVersion = new Notification();
2028 mPublicVersion.cloneInto(n.publicVersion, true);
2029 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002030
Joe Onorato46439ce2010-11-19 13:56:21 -08002031 return n;
2032 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002033
2034 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002035 * Capture, in the provided bundle, semantic information used in the construction of
2036 * this Notification object.
2037 * @hide
2038 */
Griff Hazen720042b2014-02-24 15:46:56 -08002039 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002040 // Store original information used in the construction of this object
2041 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2042 extras.putCharSequence(EXTRA_TEXT, mContentText);
2043 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2044 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2045 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2046 extras.putInt(EXTRA_PROGRESS, mProgress);
2047 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2048 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2049 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2050 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002051 extras.putBoolean(EXTRA_BUILDER_REMOTE_VIEWS, mContentView == null);
John Spurlockac08a472013-06-10 11:37:08 -04002052 if (mLargeIcon != null) {
2053 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2054 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002055 }
2056
2057 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002058 * @deprecated Use {@link #build()} instead.
2059 */
2060 @Deprecated
2061 public Notification getNotification() {
2062 return build();
2063 }
2064
2065 /**
2066 * Combine all of the options that have been set and return a new {@link Notification}
2067 * object.
2068 */
2069 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002070 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002071
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002072 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002073 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002074 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002075
2076 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2077
Griff Hazen720042b2014-02-24 15:46:56 -08002078 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002079 if (mStyle != null) {
2080 mStyle.addExtras(n.extras);
2081 }
2082
2083 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002084 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002085
2086 /**
2087 * Apply this Builder to an existing {@link Notification} object.
2088 *
2089 * @hide
2090 */
2091 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002092 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002093 return n;
2094 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002095
2096
2097 private int getBaseLayoutResource() {
2098 return mQuantumTheme
2099 ? R.layout.notification_template_quantum_base
2100 : R.layout.notification_template_base;
2101 }
2102
2103 private int getBigBaseLayoutResource() {
2104 return mQuantumTheme
2105 ? R.layout.notification_template_quantum_big_base
2106 : R.layout.notification_template_big_base;
2107 }
2108
2109 private int getBigPictureLayoutResource() {
2110 return mQuantumTheme
2111 ? R.layout.notification_template_quantum_big_picture
2112 : R.layout.notification_template_big_picture;
2113 }
2114
2115 private int getBigTextLayoutResource() {
2116 return mQuantumTheme
2117 ? R.layout.notification_template_quantum_big_text
2118 : R.layout.notification_template_big_text;
2119 }
2120
2121 private int getInboxLayoutResource() {
2122 return mQuantumTheme
2123 ? R.layout.notification_template_quantum_inbox
2124 : R.layout.notification_template_inbox;
2125 }
2126
2127 private int getActionLayoutResource() {
2128 return mQuantumTheme
2129 ? R.layout.notification_quantum_action
2130 : R.layout.notification_action;
2131 }
2132
2133 private int getActionTombstoneLayoutResource() {
2134 return mQuantumTheme
2135 ? R.layout.notification_quantum_action_tombstone
2136 : R.layout.notification_action_tombstone;
2137 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002138 }
2139
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002140 /**
2141 * An object that can apply a rich notification style to a {@link Notification.Builder}
2142 * object.
2143 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002144 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002145 private CharSequence mBigContentTitle;
2146 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002147 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002148
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002149 protected Builder mBuilder;
2150
Chris Wrend6297db2012-05-03 16:20:13 -04002151 /**
2152 * Overrides ContentTitle in the big form of the template.
2153 * This defaults to the value passed to setContentTitle().
2154 */
2155 protected void internalSetBigContentTitle(CharSequence title) {
2156 mBigContentTitle = title;
2157 }
2158
2159 /**
2160 * Set the first line of text after the detail section in the big form of the template.
2161 */
2162 protected void internalSetSummaryText(CharSequence cs) {
2163 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002164 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002165 }
2166
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002167 public void setBuilder(Builder builder) {
2168 if (mBuilder != builder) {
2169 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002170 if (mBuilder != null) {
2171 mBuilder.setStyle(this);
2172 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002173 }
2174 }
2175
Chris Wrend6297db2012-05-03 16:20:13 -04002176 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002177 if (mBuilder == null) {
2178 throw new IllegalArgumentException("Style requires a valid Builder object");
2179 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002180 }
Chris Wrend6297db2012-05-03 16:20:13 -04002181
2182 protected RemoteViews getStandardView(int layoutId) {
2183 checkBuilder();
2184
2185 if (mBigContentTitle != null) {
2186 mBuilder.setContentTitle(mBigContentTitle);
2187 }
2188
Chris Wrend6297db2012-05-03 16:20:13 -04002189 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2190
Chris Wrend6297db2012-05-03 16:20:13 -04002191 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2192 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002193 } else {
2194 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002195 }
2196
Daniel Sandler619738c2012-06-07 16:33:08 -04002197 // The last line defaults to the subtext, but can be replaced by mSummaryText
2198 final CharSequence overflowText =
2199 mSummaryTextSet ? mSummaryText
2200 : mBuilder.mSubText;
2201 if (overflowText != null) {
2202 contentView.setTextViewText(R.id.text, overflowText);
2203 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002204 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002205 } else {
2206 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2207 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002208 }
2209
2210 return contentView;
2211 }
2212
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002213 /**
2214 * @hide
2215 */
2216 public void addExtras(Bundle extras) {
2217 if (mSummaryTextSet) {
2218 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2219 }
2220 if (mBigContentTitle != null) {
2221 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2222 }
Chris Wren91ad5632013-06-05 15:05:57 -04002223 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002224 }
2225
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002226 /**
2227 * @hide
2228 */
2229 public abstract Notification buildStyled(Notification wip);
2230
2231 /**
2232 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2233 * attached to.
2234 *
2235 * @return the fully constructed Notification.
2236 */
2237 public Notification build() {
2238 checkBuilder();
2239 return mBuilder.build();
2240 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002241 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002242
2243 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002244 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002245 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002246 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002247 * <pre class="prettyprint">
2248 * Notification noti = new Notification.BigPictureStyle(
2249 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002250 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002251 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002252 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002253 * .setLargeIcon(aBitmap))
2254 * .bigPicture(aBigBitmap)
2255 * .build();
2256 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002257 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002258 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002259 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002260 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002261 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002262 private Bitmap mBigLargeIcon;
2263 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002264
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002265 public BigPictureStyle() {
2266 }
2267
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002268 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002269 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002270 }
2271
Chris Wrend6297db2012-05-03 16:20:13 -04002272 /**
2273 * Overrides ContentTitle in the big form of the template.
2274 * This defaults to the value passed to setContentTitle().
2275 */
2276 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002277 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002278 return this;
2279 }
2280
2281 /**
2282 * Set the first line of text after the detail section in the big form of the template.
2283 */
2284 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002285 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002286 return this;
2287 }
2288
Chris Wren0bd664d2012-08-01 13:56:56 -04002289 /**
2290 * Provide the bitmap to be used as the payload for the BigPicture notification.
2291 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002292 public BigPictureStyle bigPicture(Bitmap b) {
2293 mPicture = b;
2294 return this;
2295 }
2296
Chris Wren3745a3d2012-05-22 15:11:52 -04002297 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002298 * Override the large icon when the big notification is shown.
2299 */
2300 public BigPictureStyle bigLargeIcon(Bitmap b) {
2301 mBigLargeIconSet = true;
2302 mBigLargeIcon = b;
2303 return this;
2304 }
2305
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002306 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002307 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002308
2309 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2310
2311 return contentView;
2312 }
2313
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002314 /**
2315 * @hide
2316 */
2317 public void addExtras(Bundle extras) {
2318 super.addExtras(extras);
2319
2320 if (mBigLargeIconSet) {
2321 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2322 }
2323 extras.putParcelable(EXTRA_PICTURE, mPicture);
2324 }
2325
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002326 /**
2327 * @hide
2328 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002329 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002330 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002331 if (mBigLargeIconSet ) {
2332 mBuilder.mLargeIcon = mBigLargeIcon;
2333 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002334 wip.bigContentView = makeBigContentView();
2335 return wip;
2336 }
2337 }
2338
2339 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002340 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002341 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002342 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002343 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05002344 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002345 * new Notification.Builder()
2346 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2347 * .setContentText(subject)
2348 * .setSmallIcon(R.drawable.new_mail)
2349 * .setLargeIcon(aBitmap))
2350 * .bigText(aVeryLongString)
2351 * .build();
2352 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002353 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002354 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002355 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002356 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002357 private CharSequence mBigText;
2358
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002359 public BigTextStyle() {
2360 }
2361
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002362 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002363 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002364 }
2365
Chris Wrend6297db2012-05-03 16:20:13 -04002366 /**
2367 * Overrides ContentTitle in the big form of the template.
2368 * This defaults to the value passed to setContentTitle().
2369 */
2370 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002371 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002372 return this;
2373 }
2374
2375 /**
2376 * Set the first line of text after the detail section in the big form of the template.
2377 */
2378 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002379 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002380 return this;
2381 }
2382
Chris Wren0bd664d2012-08-01 13:56:56 -04002383 /**
2384 * Provide the longer text to be displayed in the big form of the
2385 * template in place of the content text.
2386 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002387 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002388 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002389 return this;
2390 }
2391
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002392 /**
2393 * @hide
2394 */
2395 public void addExtras(Bundle extras) {
2396 super.addExtras(extras);
2397
2398 extras.putCharSequence(EXTRA_TEXT, mBigText);
2399 }
2400
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002401 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002402 // Remove the content text so line3 only shows if you have a summary
2403 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002404 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04002405
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002406 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08002407
Daniel Sandler619738c2012-06-07 16:33:08 -04002408 if (hadThreeLines) {
2409 // vertical centering
2410 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2411 }
2412
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002413 contentView.setTextViewText(R.id.big_text, mBigText);
2414 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04002415 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002416
2417 return contentView;
2418 }
2419
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002420 /**
2421 * @hide
2422 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002423 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002424 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002425 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002426
2427 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
2428
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002429 return wip;
2430 }
2431 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04002432
2433 /**
2434 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08002435 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002436 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
2437 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002438 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04002439 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04002440 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04002441 * .setContentText(subject)
2442 * .setSmallIcon(R.drawable.new_mail)
2443 * .setLargeIcon(aBitmap))
2444 * .addLine(str1)
2445 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04002446 * .setContentTitle("")
2447 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04002448 * .build();
2449 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002450 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002451 * @see Notification#bigContentView
2452 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002453 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002454 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
2455
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002456 public InboxStyle() {
2457 }
2458
Daniel Sandler879c5e02012-04-17 16:46:51 -04002459 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002460 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002461 }
2462
Chris Wrend6297db2012-05-03 16:20:13 -04002463 /**
2464 * Overrides ContentTitle in the big form of the template.
2465 * This defaults to the value passed to setContentTitle().
2466 */
2467 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002468 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002469 return this;
2470 }
2471
2472 /**
2473 * Set the first line of text after the detail section in the big form of the template.
2474 */
2475 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002476 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002477 return this;
2478 }
2479
Chris Wren0bd664d2012-08-01 13:56:56 -04002480 /**
2481 * Append a line to the digest section of the Inbox notification.
2482 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04002483 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002484 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04002485 return this;
2486 }
2487
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002488 /**
2489 * @hide
2490 */
2491 public void addExtras(Bundle extras) {
2492 super.addExtras(extras);
2493 CharSequence[] a = new CharSequence[mTexts.size()];
2494 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
2495 }
2496
Daniel Sandler879c5e02012-04-17 16:46:51 -04002497 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002498 // Remove the content text so line3 disappears unless you have a summary
2499 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002500 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04002501
Chris Wrend6297db2012-05-03 16:20:13 -04002502 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002503
Chris Wrend6297db2012-05-03 16:20:13 -04002504 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 -04002505 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04002506
Chris Wren4ed80d52012-05-17 09:30:03 -04002507 // Make sure all rows are gone in case we reuse a view.
2508 for (int rowId : rowIds) {
2509 contentView.setViewVisibility(rowId, View.GONE);
2510 }
2511
Chris Wren683ab002012-09-20 10:35:54 -04002512
Daniel Sandler879c5e02012-04-17 16:46:51 -04002513 int i=0;
2514 while (i < mTexts.size() && i < rowIds.length) {
2515 CharSequence str = mTexts.get(i);
2516 if (str != null && !str.equals("")) {
2517 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
2518 contentView.setTextViewText(rowIds[i], str);
2519 }
2520 i++;
2521 }
2522
Chris Wren683ab002012-09-20 10:35:54 -04002523 contentView.setViewVisibility(R.id.inbox_end_pad,
2524 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
2525
2526 contentView.setViewVisibility(R.id.inbox_more,
2527 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04002528
Daniel Sandler879c5e02012-04-17 16:46:51 -04002529 return contentView;
2530 }
2531
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002532 /**
2533 * @hide
2534 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002535 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002536 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002537 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002538
Daniel Sandler879c5e02012-04-17 16:46:51 -04002539 return wip;
2540 }
2541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542}