blob: 2e8fbb6f8b1eeeb4424485c486de17fc6b533e39 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Andy Stadler110988c2010-12-03 14:29:16 -080019import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.Intent;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040024import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040025import android.graphics.Bitmap;
Jeff Sharkey098d5802012-04-26 17:30:34 -070026import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040028import android.os.BadParcelableException;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050029import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Parcel;
31import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040032import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070033import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040035import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040036import android.util.TypedValue;
Joe Onorato8595a3d2010-11-19 18:12:07 -080037import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070038import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.widget.RemoteViews;
40
Tor Norbyed9273d62013-05-30 15:59:53 -070041import java.lang.annotation.Retention;
42import java.lang.annotation.RetentionPolicy;
Andy Stadler110988c2010-12-03 14:29:16 -080043import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050044import java.util.ArrayList;
Joe Onorato561d3852010-11-20 18:09:34 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/**
47 * A class that represents how a persistent notification is to be presented to
48 * the user using the {@link android.app.NotificationManager}.
49 *
Joe Onoratocb109a02011-01-18 17:57:41 -080050 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
51 * easier to construct Notifications.</p>
52 *
Joe Fernandez558459f2011-10-13 16:47:36 -070053 * <div class="special reference">
54 * <h3>Developer Guides</h3>
55 * <p>For a guide to creating notifications, read the
56 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
57 * developer guide.</p>
58 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 */
60public class Notification implements Parcelable
61{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040062 private static final String TAG = "Notification";
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 /**
65 * Use all default values (where applicable).
66 */
67 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
70 * Use the default notification sound. This will ignore any given
71 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050072 *
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050075 */
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public static final int DEFAULT_SOUND = 1;
78
79 /**
80 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050081 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070082 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050085 */
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
90 * Use the default notification lights. This will ignore the
91 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
92 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095 */
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500100 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800101 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500102 * Default value: {@link System#currentTimeMillis() Now}.
103 *
104 * Choose a timestamp that will be most relevant to the user. For most finite events, this
105 * corresponds to the time the event happened (or will happen, in the case of events that have
106 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800107 * timestamped according to when the activity began.
108 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800110 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111 * <ul>
112 * <li>Notification of a new chat message should be stamped when the message was received.</li>
113 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
114 * <li>Notification of a completed file download should be stamped when the download finished.</li>
115 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
116 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
117 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800118 * </ul>
119 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 */
121 public long when;
122
123 /**
124 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500125 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 */
127 public int icon;
128
129 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800130 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
131 * leave it at its default value of 0.
132 *
133 * @see android.widget.ImageView#setImageLevel
134 * @see android.graphics.drawable#setLevel
135 */
136 public int iconLevel;
137
138 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 * The number of events that this notification represents. For example, in a new mail
140 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800141 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142 * The system may or may not use this field to modify the appearance of the notification. For
143 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
144 * superimposed over the icon in the status bar. Starting with
145 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
146 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800147 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500148 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
150 public int number;
151
152 /**
153 * The intent to execute when the expanded status entry is clicked. If
154 * this is an activity, it must include the
155 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800156 * that you take care of task management as described in the
157 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800158 * Stack</a> document. In particular, make sure to read the notification section
159 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
160 * Notifications</a> for the correct ways to launch an application from a
161 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
163 public PendingIntent contentIntent;
164
165 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * The intent to execute when the notification is explicitly dismissed by the user, either with
167 * the "Clear All" button or by swiping it away individually.
168 *
169 * This probably shouldn't be launching an activity since several of those will be sent
170 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
172 public PendingIntent deleteIntent;
173
174 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700175 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800176 *
177 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400178 */
179 public PendingIntent fullScreenIntent;
180
181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400183 * the status bar on large and smaller devices.
184 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800185 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 */
187 public CharSequence tickerText;
188
189 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800190 * The view to show as the ticker in the status bar when the notification
191 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400192 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800193 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400194
195 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400196 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 */
198 public RemoteViews contentView;
199
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400200 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400201 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400202 * opportunity to show more detail. The system UI may choose to show this
203 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400204 */
205 public RemoteViews bigContentView;
206
Chris Wren8fd39ec2014-02-27 17:43:26 -0500207
208 /**
209 * @hide
210 * A medium-format version of {@link #contentView}, giving the Notification an
211 * opportunity to add action buttons to contentView. The system UI may
212 * choose to show this as a popup notification at its discretion.
213 */
214 public RemoteViews headsUpContentView;
215
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400216 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800217 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800219 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
221 /**
222 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * </p>
227 */
228 public Uri sound;
229
230 /**
231 * Use this constant as the value for audioStreamType to request that
232 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700233 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 */
235 public static final int STREAM_DEFAULT = -1;
236
237 /**
238 * The audio stream type to use when playing the sound.
239 * Should be one of the STREAM_ constants from
240 * {@link android.media.AudioManager}.
241 */
242 public int audioStreamType = STREAM_DEFAULT;
243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500245 * The pattern with which to vibrate.
246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <p>
248 * To vibrate the default pattern, see {@link #defaults}.
249 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * @see android.os.Vibrator#vibrate(long[],int)
252 */
253 public long[] vibrate;
254
255 /**
256 * The color of the led. The hardware will do its best approximation.
257 *
258 * @see #FLAG_SHOW_LIGHTS
259 * @see #flags
260 */
261 public int ledARGB;
262
263 /**
264 * The number of milliseconds for the LED to be on while it's flashing.
265 * The hardware will do its best approximation.
266 *
267 * @see #FLAG_SHOW_LIGHTS
268 * @see #flags
269 */
270 public int ledOnMS;
271
272 /**
273 * The number of milliseconds for the LED to be off while it's flashing.
274 * The hardware will do its best approximation.
275 *
276 * @see #FLAG_SHOW_LIGHTS
277 * @see #flags
278 */
279 public int ledOffMS;
280
281 /**
282 * Specifies which values should be taken from the defaults.
283 * <p>
284 * To set, OR the desired from {@link #DEFAULT_SOUND},
285 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
286 * values, use {@link #DEFAULT_ALL}.
287 * </p>
288 */
289 public int defaults;
290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * Bit to be bitwise-ored into the {@link #flags} field that should be
293 * set if you want the LED on for this notification.
294 * <ul>
295 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
296 * or 0 for both ledOnMS and ledOffMS.</li>
297 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
298 * <li>To flash the LED, pass the number of milliseconds that it should
299 * be on and off to ledOnMS and ledOffMS.</li>
300 * </ul>
301 * <p>
302 * Since hardware varies, you are not guaranteed that any of the values
303 * you pass are honored exactly. Use the system defaults (TODO) if possible
304 * because they will be set to values that work on any given hardware.
305 * <p>
306 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 */
309 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
310
311 /**
312 * Bit to be bitwise-ored into the {@link #flags} field that should be
313 * set if this notification is in reference to something that is ongoing,
314 * like a phone call. It should not be set if this notification is in
315 * reference to something that happened at a particular point in time,
316 * like a missed phone call.
317 */
318 public static final int FLAG_ONGOING_EVENT = 0x00000002;
319
320 /**
321 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700322 * the audio will be repeated until the notification is
323 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 */
325 public static final int FLAG_INSISTENT = 0x00000004;
326
327 /**
328 * Bit to be bitwise-ored into the {@link #flags} field that should be
329 * set if you want the sound and/or vibration play each time the
330 * notification is sent, even if it has not been canceled before that.
331 */
332 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
333
334 /**
335 * Bit to be bitwise-ored into the {@link #flags} field that should be
336 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500337 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 */
340 public static final int FLAG_AUTO_CANCEL = 0x00000010;
341
342 /**
343 * Bit to be bitwise-ored into the {@link #flags} field that should be
344 * set if the notification should not be canceled when the user clicks
345 * the Clear all button.
346 */
347 public static final int FLAG_NO_CLEAR = 0x00000020;
348
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700349 /**
350 * Bit to be bitwise-ored into the {@link #flags} field that should be
351 * set if this notification represents a currently running service. This
352 * will normally be set for you by {@link Service#startForeground}.
353 */
354 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
355
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400356 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500357 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800358 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400360 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500361 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400362
Griff Hazendfcb0802014-02-11 12:00:00 -0800363 /**
364 * Bit to be bitswise-ored into the {@link #flags} field that should be
365 * set if this notification is relevant to the current device only
366 * and it is not recommended that it bridge to other devices.
367 */
368 public static final int FLAG_LOCAL_ONLY = 0x00000100;
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 public int flags;
371
Tor Norbyed9273d62013-05-30 15:59:53 -0700372 /** @hide */
373 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
374 @Retention(RetentionPolicy.SOURCE)
375 public @interface Priority {}
376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500378 * Default notification {@link #priority}. If your application does not prioritize its own
379 * notifications, use this value for all notifications.
380 */
381 public static final int PRIORITY_DEFAULT = 0;
382
383 /**
384 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
385 * items smaller, or at a different position in the list, compared with your app's
386 * {@link #PRIORITY_DEFAULT} items.
387 */
388 public static final int PRIORITY_LOW = -1;
389
390 /**
391 * Lowest {@link #priority}; these items might not be shown to the user except under special
392 * circumstances, such as detailed notification logs.
393 */
394 public static final int PRIORITY_MIN = -2;
395
396 /**
397 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
398 * show these items larger, or at a different position in notification lists, compared with
399 * your app's {@link #PRIORITY_DEFAULT} items.
400 */
401 public static final int PRIORITY_HIGH = 1;
402
403 /**
404 * Highest {@link #priority}, for your application's most important items that require the
405 * user's prompt attention or input.
406 */
407 public static final int PRIORITY_MAX = 2;
408
409 /**
410 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800411 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500412 * Priority is an indication of how much of the user's valuable attention should be consumed by
413 * this notification. Low-priority notifications may be hidden from the user in certain
414 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500415 * system will make a determination about how to interpret this priority when presenting
416 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500417 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700418 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500419 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800420
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600421
422 /**
423 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
424 * the notification's presence and contents in untrusted situations (namely, on the secure
425 * lockscreen).
426 *
427 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
428 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
429 * shown in all situations, but the contents are only available if the device is unlocked for
430 * the appropriate user.
431 *
432 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
433 * can be read even in an "insecure" context (that is, above a secure lockscreen).
434 * To modify the public version of this notification—for example, to redact some portions—see
435 * {@link Builder#setPublicVersion(Notification)}.
436 *
437 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
438 * and ticker until the user has bypassed the lockscreen.
439 */
440 public int visibility;
441
442 public static final int VISIBILITY_PUBLIC = 1;
443 public static final int VISIBILITY_PRIVATE = 0;
444 public static final int VISIBILITY_SECRET = -1;
445
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500446 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400447 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400449 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500450
451 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400452 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500453 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400454 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500455
456 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400457 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500458 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400459 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500460
461 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400462 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500463 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400464 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500465
466 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400467 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500468 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400469 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500470
471 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400472 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400474 public static final String CATEGORY_ALARM = "alarm";
475
476 /**
477 * Notification category: progress of a long-running background operation.
478 */
479 public static final String CATEGORY_PROGRESS = "progress";
480
481 /**
482 * Notification category: social network or sharing update.
483 */
484 public static final String CATEGORY_SOCIAL = "social";
485
486 /**
487 * Notification category: error in background operation or authentication status.
488 */
489 public static final String CATEGORY_ERROR = "err";
490
491 /**
492 * Notification category: media transport control for playback.
493 */
494 public static final String CATEGORY_TRANSPORT = "transport";
495
496 /**
497 * Notification category: system or device status update. Reserved for system use.
498 */
499 public static final String CATEGORY_SYSTEM = "sys";
500
501 /**
502 * Notification category: indication of running background service.
503 */
504 public static final String CATEGORY_SERVICE = "service";
505
506 /**
507 * Notification category: ongoing information about device or contextual status.
508 */
509 public static final String CATEGORY_STATUS = "status";
510
511 /**
512 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
513 * that best describes this Notification. May be used by the system for ranking and filtering.
514 */
515 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500516
517 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400518 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400519 * <p>
520 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
521 * APIs, and are intended to be used by
522 * {@link android.service.notification.NotificationListenerService} implementations to extract
523 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500524 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400525 public Bundle extras = new Bundle();
526
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400527 /**
528 * {@link #extras} key: this is the title of the notification,
529 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
530 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500531 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400532
533 /**
534 * {@link #extras} key: this is the title of the notification when shown in expanded form,
535 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
536 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400537 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400538
539 /**
540 * {@link #extras} key: this is the main text payload, as supplied to
541 * {@link Builder#setContentText(CharSequence)}.
542 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500543 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400544
545 /**
546 * {@link #extras} key: this is a third line of text, as supplied to
547 * {@link Builder#setSubText(CharSequence)}.
548 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400549 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400550
551 /**
552 * {@link #extras} key: this is a small piece of additional text as supplied to
553 * {@link Builder#setContentInfo(CharSequence)}.
554 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400555 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400556
557 /**
558 * {@link #extras} key: this is a line of summary information intended to be shown
559 * alongside expanded notifications, as supplied to (e.g.)
560 * {@link BigTextStyle#setSummaryText(CharSequence)}.
561 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400562 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400563
564 /**
565 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
566 * supplied to {@link Builder#setSmallIcon(int)}.
567 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500568 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400569
570 /**
571 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
572 * notification payload, as
573 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
574 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400575 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400576
577 /**
578 * {@link #extras} key: this is a bitmap to be used instead of the one from
579 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
580 * shown in its expanded form, as supplied to
581 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
582 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400583 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400584
585 /**
586 * {@link #extras} key: this is the progress value supplied to
587 * {@link Builder#setProgress(int, int, boolean)}.
588 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400589 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400590
591 /**
592 * {@link #extras} key: this is the maximum value supplied to
593 * {@link Builder#setProgress(int, int, boolean)}.
594 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400595 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400596
597 /**
598 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
599 * {@link Builder#setProgress(int, int, boolean)}.
600 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400601 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400602
603 /**
604 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
605 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
606 * {@link Builder#setUsesChronometer(boolean)}.
607 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400608 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400609
610 /**
611 * {@link #extras} key: whether {@link #when} should be shown,
612 * as supplied to {@link Builder#setShowWhen(boolean)}.
613 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400614 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400615
616 /**
617 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
618 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
619 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400620 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400621
622 /**
623 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
624 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
625 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400626 public static final String EXTRA_TEXT_LINES = "android.textLines";
Chris Wren91ad5632013-06-05 15:05:57 -0400627 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400628
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400629 /**
630 * {@link #extras} key: An array of people that this notification relates to, specified
631 * by contacts provider contact URI.
632 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400633 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634
635 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400636 * @hide
637 * Extra added by NotificationManagerService to indicate whether a NotificationScorer
638 * modified the Notifications's score.
639 */
640 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
641
642 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400643 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400644 * @hide
645 */
646 public static final String EXTRA_AS_HEADS_UP = "headsup";
647
648 /**
Jorim Jaggi39fa59f2014-02-25 15:38:45 +0100649 * Extra added from {@link Notification.Builder} to indicate that the remote views were inflated
650 * from the builder, as opposed to being created directly from the application.
651 * @hide
652 */
653 public static final String EXTRA_BUILDER_REMOTE_VIEWS = "android.builderRemoteViews";
654
655 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400656 * Allow certain system-generated notifications to appear before the device is provisioned.
657 * Only available to notifications coming from the android package.
658 * @hide
659 */
660 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
661
662 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400663 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400664 * @hide
665 */
666 public static final int HEADS_UP_NEVER = 0;
667
668 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400669 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400670 * @hide
671 */
672 public static final int HEADS_UP_ALLOWED = 1;
673
674 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400675 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400676 * @hide
677 */
678 public static final int HEADS_UP_REQUESTED = 2;
679
680 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400681 * Structure to encapsulate a named action that can be shown as part of this notification.
682 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
683 * selected by the user.
684 * <p>
685 * Apps should use {@link Builder#addAction(int, CharSequence, PendingIntent)} to create and
686 * attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400687 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500688 public static class Action implements Parcelable {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400689 /**
690 * Small icon representing the action.
691 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400692 public int icon;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400693 /**
694 * Title of the action.
695 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400696 public CharSequence title;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400697 /**
698 * Intent to send when the user invokes this action. May be null, in which case the action
699 * may be rendered in a disabled presentation by the system UI.
700 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400701 public PendingIntent actionIntent;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400702
703 private Action() { }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400704 private Action(Parcel in) {
705 icon = in.readInt();
706 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
707 if (in.readInt() == 1) {
708 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
709 }
710 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400711 /**
712 * Use {@link Builder#addAction(int, CharSequence, PendingIntent)}.
713 */
714 public Action(int icon, CharSequence title, PendingIntent intent) {
715 this.icon = icon;
716 this.title = title;
717 this.actionIntent = intent;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400718 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400719
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400720 @Override
721 public Action clone() {
722 return new Action(
723 this.icon,
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400724 this.title,
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400725 this.actionIntent // safe to alias
726 );
727 }
728 @Override
729 public int describeContents() {
730 return 0;
731 }
732 @Override
733 public void writeToParcel(Parcel out, int flags) {
734 out.writeInt(icon);
735 TextUtils.writeToParcel(title, out, flags);
736 if (actionIntent != null) {
737 out.writeInt(1);
738 actionIntent.writeToParcel(out, flags);
739 } else {
740 out.writeInt(0);
741 }
742 }
743 public static final Parcelable.Creator<Action> CREATOR
744 = new Parcelable.Creator<Action>() {
745 public Action createFromParcel(Parcel in) {
746 return new Action(in);
747 }
748 public Action[] newArray(int size) {
749 return new Action[size];
750 }
751 };
752 }
753
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400754 /**
755 * Array of all {@link Action} structures attached to this notification by
756 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
757 * {@link android.service.notification.NotificationListenerService} that provide an alternative
758 * interface for invoking actions.
759 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500760 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400761
762 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600763 * Replacement version of this notification whose content will be shown
764 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
765 * and {@link #VISIBILITY_PUBLIC}.
766 */
767 public Notification publicVersion;
768
769 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500770 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800771 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 */
773 public Notification()
774 {
775 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500776 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
778
779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 * @hide
781 */
782 public Notification(Context context, int icon, CharSequence tickerText, long when,
783 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
784 {
785 this.when = when;
786 this.icon = icon;
787 this.tickerText = tickerText;
788 setLatestEventInfo(context, contentTitle, contentText,
789 PendingIntent.getActivity(context, 0, contentIntent, 0));
790 }
791
792 /**
793 * Constructs a Notification object with the information needed to
794 * have a status bar icon without the standard expanded view.
795 *
796 * @param icon The resource id of the icon to put in the status bar.
797 * @param tickerText The text that flows by in the status bar when the notification first
798 * activates.
799 * @param when The time to show in the time field. In the System.currentTimeMillis
800 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800801 *
802 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800804 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 public Notification(int icon, CharSequence tickerText, long when)
806 {
807 this.icon = icon;
808 this.tickerText = tickerText;
809 this.when = when;
810 }
811
812 /**
813 * Unflatten the notification from a parcel.
814 */
815 public Notification(Parcel parcel)
816 {
817 int version = parcel.readInt();
818
819 when = parcel.readLong();
820 icon = parcel.readInt();
821 number = parcel.readInt();
822 if (parcel.readInt() != 0) {
823 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
824 }
825 if (parcel.readInt() != 0) {
826 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
827 }
828 if (parcel.readInt() != 0) {
829 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
830 }
831 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800832 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400833 }
834 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
836 }
Joe Onorato561d3852010-11-20 18:09:34 -0800837 if (parcel.readInt() != 0) {
838 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 defaults = parcel.readInt();
841 flags = parcel.readInt();
842 if (parcel.readInt() != 0) {
843 sound = Uri.CREATOR.createFromParcel(parcel);
844 }
845
846 audioStreamType = parcel.readInt();
847 vibrate = parcel.createLongArray();
848 ledARGB = parcel.readInt();
849 ledOnMS = parcel.readInt();
850 ledOffMS = parcel.readInt();
851 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400852
853 if (parcel.readInt() != 0) {
854 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
855 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500856
857 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400858
John Spurlockfd7f1e02014-03-18 16:41:57 -0400859 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500860
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500861 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400862
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500863 actions = parcel.createTypedArray(Action.CREATOR); // may be null
864
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400865 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400866 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
867 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600868
Chris Wren8fd39ec2014-02-27 17:43:26 -0500869 if (parcel.readInt() != 0) {
870 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
871 }
872
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600873 visibility = parcel.readInt();
874
875 if (parcel.readInt() != 0) {
876 publicVersion = Notification.CREATOR.createFromParcel(parcel);
877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
Andy Stadler110988c2010-12-03 14:29:16 -0800880 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700881 public Notification clone() {
882 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -0400883 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500884 return that;
885 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700886
Daniel Sandler1a497d32013-04-18 14:52:45 -0400887 /**
888 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
889 * of this into that.
890 * @hide
891 */
892 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700893 that.when = this.when;
894 that.icon = this.icon;
895 that.number = this.number;
896
897 // PendingIntents are global, so there's no reason (or way) to clone them.
898 that.contentIntent = this.contentIntent;
899 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400900 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700901
902 if (this.tickerText != null) {
903 that.tickerText = this.tickerText.toString();
904 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400905 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800906 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400907 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400908 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700909 that.contentView = this.contentView.clone();
910 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400911 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800912 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
913 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100914 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700915 that.sound = this.sound; // android.net.Uri is immutable
916 that.audioStreamType = this.audioStreamType;
917
918 final long[] vibrate = this.vibrate;
919 if (vibrate != null) {
920 final int N = vibrate.length;
921 final long[] vib = that.vibrate = new long[N];
922 System.arraycopy(vibrate, 0, vib, 0, N);
923 }
924
925 that.ledARGB = this.ledARGB;
926 that.ledOnMS = this.ledOnMS;
927 that.ledOffMS = this.ledOffMS;
928 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500929
Joe Onorato18e69df2010-05-17 22:26:12 -0700930 that.flags = this.flags;
931
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500932 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800933
John Spurlockfd7f1e02014-03-18 16:41:57 -0400934 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500935
936 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400937 try {
938 that.extras = new Bundle(this.extras);
939 // will unparcel
940 that.extras.size();
941 } catch (BadParcelableException e) {
942 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
943 that.extras = null;
944 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500945 }
946
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500947 if (this.actions != null) {
948 that.actions = new Action[this.actions.length];
949 for(int i=0; i<this.actions.length; i++) {
950 that.actions[i] = this.actions[i].clone();
951 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400952 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500953
Daniel Sandler1a497d32013-04-18 14:52:45 -0400954 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400955 that.bigContentView = this.bigContentView.clone();
956 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400957
Chris Wren8fd39ec2014-02-27 17:43:26 -0500958 if (heavy && this.headsUpContentView != null) {
959 that.headsUpContentView = this.headsUpContentView.clone();
960 }
961
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600962 that.visibility = this.visibility;
963
964 if (this.publicVersion != null) {
965 that.publicVersion = new Notification();
966 this.publicVersion.cloneInto(that.publicVersion, heavy);
967 }
968
Daniel Sandler1a497d32013-04-18 14:52:45 -0400969 if (!heavy) {
970 that.lightenPayload(); // will clean out extras
971 }
972 }
973
974 /**
975 * Removes heavyweight parts of the Notification object for archival or for sending to
976 * listeners when the full contents are not necessary.
977 * @hide
978 */
979 public final void lightenPayload() {
980 tickerView = null;
981 contentView = null;
982 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -0500983 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -0400984 largeIcon = null;
985 if (extras != null) {
986 extras.remove(Notification.EXTRA_LARGE_ICON);
987 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
988 extras.remove(Notification.EXTRA_PICTURE);
989 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700990 }
991
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400992 /**
993 * Make sure this CharSequence is safe to put into a bundle, which basically
994 * means it had better not be some custom Parcelable implementation.
995 * @hide
996 */
997 public static CharSequence safeCharSequence(CharSequence cs) {
998 if (cs instanceof Parcelable) {
999 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1000 + " instance is a custom Parcelable and not allowed in Notification");
1001 return cs.toString();
1002 }
1003
1004 return cs;
1005 }
1006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 public int describeContents() {
1008 return 0;
1009 }
1010
1011 /**
1012 * Flatten this notification from a parcel.
1013 */
1014 public void writeToParcel(Parcel parcel, int flags)
1015 {
1016 parcel.writeInt(1);
1017
1018 parcel.writeLong(when);
1019 parcel.writeInt(icon);
1020 parcel.writeInt(number);
1021 if (contentIntent != null) {
1022 parcel.writeInt(1);
1023 contentIntent.writeToParcel(parcel, 0);
1024 } else {
1025 parcel.writeInt(0);
1026 }
1027 if (deleteIntent != null) {
1028 parcel.writeInt(1);
1029 deleteIntent.writeToParcel(parcel, 0);
1030 } else {
1031 parcel.writeInt(0);
1032 }
1033 if (tickerText != null) {
1034 parcel.writeInt(1);
1035 TextUtils.writeToParcel(tickerText, parcel, flags);
1036 } else {
1037 parcel.writeInt(0);
1038 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001039 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001040 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001041 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001042 } else {
1043 parcel.writeInt(0);
1044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (contentView != null) {
1046 parcel.writeInt(1);
1047 contentView.writeToParcel(parcel, 0);
1048 } else {
1049 parcel.writeInt(0);
1050 }
Joe Onorato561d3852010-11-20 18:09:34 -08001051 if (largeIcon != null) {
1052 parcel.writeInt(1);
1053 largeIcon.writeToParcel(parcel, 0);
1054 } else {
1055 parcel.writeInt(0);
1056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057
1058 parcel.writeInt(defaults);
1059 parcel.writeInt(this.flags);
1060
1061 if (sound != null) {
1062 parcel.writeInt(1);
1063 sound.writeToParcel(parcel, 0);
1064 } else {
1065 parcel.writeInt(0);
1066 }
1067 parcel.writeInt(audioStreamType);
1068 parcel.writeLongArray(vibrate);
1069 parcel.writeInt(ledARGB);
1070 parcel.writeInt(ledOnMS);
1071 parcel.writeInt(ledOffMS);
1072 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001073
1074 if (fullScreenIntent != null) {
1075 parcel.writeInt(1);
1076 fullScreenIntent.writeToParcel(parcel, 0);
1077 } else {
1078 parcel.writeInt(0);
1079 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001080
1081 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001082
John Spurlockfd7f1e02014-03-18 16:41:57 -04001083 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001084
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001085 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001086
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001087 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001088
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001089 if (bigContentView != null) {
1090 parcel.writeInt(1);
1091 bigContentView.writeToParcel(parcel, 0);
1092 } else {
1093 parcel.writeInt(0);
1094 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001095
Chris Wren8fd39ec2014-02-27 17:43:26 -05001096 if (headsUpContentView != null) {
1097 parcel.writeInt(1);
1098 headsUpContentView.writeToParcel(parcel, 0);
1099 } else {
1100 parcel.writeInt(0);
1101 }
1102
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001103 parcel.writeInt(visibility);
1104
1105 if (publicVersion != null) {
1106 parcel.writeInt(1);
1107 publicVersion.writeToParcel(parcel, 0);
1108 } else {
1109 parcel.writeInt(0);
1110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112
1113 /**
1114 * Parcelable.Creator that instantiates Notification objects
1115 */
1116 public static final Parcelable.Creator<Notification> CREATOR
1117 = new Parcelable.Creator<Notification>()
1118 {
1119 public Notification createFromParcel(Parcel parcel)
1120 {
1121 return new Notification(parcel);
1122 }
1123
1124 public Notification[] newArray(int size)
1125 {
1126 return new Notification[size];
1127 }
1128 };
1129
1130 /**
1131 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1132 * layout.
1133 *
1134 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1135 * in the view.</p>
1136 * @param context The context for your application / activity.
1137 * @param contentTitle The title that goes in the expanded entry.
1138 * @param contentText The text that goes in the expanded entry.
1139 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1140 * If this is an activity, it must include the
1141 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001142 * that you take care of task management as described in the
1143 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1144 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001145 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001146 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001148 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 public void setLatestEventInfo(Context context,
1150 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001151 Notification.Builder builder = new Notification.Builder(context);
1152
1153 // First, ensure that key pieces of information that may have been set directly
1154 // are preserved
1155 builder.setWhen(this.when);
1156 builder.setSmallIcon(this.icon);
1157 builder.setPriority(this.priority);
1158 builder.setTicker(this.tickerText);
1159 builder.setNumber(this.number);
1160 builder.mFlags = this.flags;
1161 builder.setSound(this.sound, this.audioStreamType);
1162 builder.setDefaults(this.defaults);
1163 builder.setVibrate(this.vibrate);
1164
1165 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001167 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001170 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001172 builder.setContentIntent(contentIntent);
1173 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175
1176 @Override
1177 public String toString() {
1178 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001179 sb.append("Notification(pri=");
1180 sb.append(priority);
1181 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001182 if (contentView != null) {
1183 sb.append(contentView.getPackage());
1184 sb.append("/0x");
1185 sb.append(Integer.toHexString(contentView.getLayoutId()));
1186 } else {
1187 sb.append("null");
1188 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001189 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001190 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001191 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1192 sb.append("default");
1193 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 int N = this.vibrate.length-1;
1195 sb.append("[");
1196 for (int i=0; i<N; i++) {
1197 sb.append(this.vibrate[i]);
1198 sb.append(',');
1199 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001200 if (N != -1) {
1201 sb.append(this.vibrate[N]);
1202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 } else {
1205 sb.append("null");
1206 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001207 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001208 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001210 } else if (this.sound != null) {
1211 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } else {
1213 sb.append("null");
1214 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001215 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001217 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001218 sb.append(Integer.toHexString(this.flags));
John Spurlockfd7f1e02014-03-18 16:41:57 -04001219 sb.append(" category="); sb.append(this.category);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001220 if (actions != null) {
1221 sb.append(" ");
1222 sb.append(actions.length);
1223 sb.append(" action");
1224 if (actions.length > 1) sb.append("s");
1225 }
1226 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 return sb.toString();
1228 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001229
Jeff Sharkey6d515712012-09-20 16:06:08 -07001230 /** {@hide} */
1231 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001232 if (user.getIdentifier() == UserHandle.USER_ALL) {
1233 user = UserHandle.OWNER;
1234 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001235 if (tickerView != null) {
1236 tickerView.setUser(user);
1237 }
1238 if (contentView != null) {
1239 contentView.setUser(user);
1240 }
1241 if (bigContentView != null) {
1242 bigContentView.setUser(user);
1243 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001244 if (headsUpContentView != null) {
1245 headsUpContentView.setUser(user);
1246 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001247 }
1248
Joe Onoratocb109a02011-01-18 17:57:41 -08001249 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001250 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001251 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001252 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001253 * content views using the platform's notification layout template. If your app supports
1254 * versions of Android as old as API level 4, you can instead use
1255 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1256 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1257 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001258 *
Scott Main183bf112012-08-13 19:12:13 -07001259 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001260 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001261 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001262 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001263 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1264 * .setContentText(subject)
1265 * .setSmallIcon(R.drawable.new_mail)
1266 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001267 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001268 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001269 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001270 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001271 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001272
Joe Onorato46439ce2010-11-19 13:56:21 -08001273 private Context mContext;
1274
1275 private long mWhen;
1276 private int mSmallIcon;
1277 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001278 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001279 private CharSequence mContentTitle;
1280 private CharSequence mContentText;
1281 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001282 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001283 private PendingIntent mContentIntent;
1284 private RemoteViews mContentView;
1285 private PendingIntent mDeleteIntent;
1286 private PendingIntent mFullScreenIntent;
1287 private CharSequence mTickerText;
1288 private RemoteViews mTickerView;
1289 private Bitmap mLargeIcon;
1290 private Uri mSound;
1291 private int mAudioStreamType;
1292 private long[] mVibrate;
1293 private int mLedArgb;
1294 private int mLedOnMs;
1295 private int mLedOffMs;
1296 private int mDefaults;
1297 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001298 private int mProgressMax;
1299 private int mProgress;
1300 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001301 private String mCategory;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001302 private Bundle mExtras;
1303 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001304 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001305 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001306 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001307 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001308 private int mVisibility = VISIBILITY_PRIVATE;
1309 private Notification mPublicVersion = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001310 private boolean mQuantumTheme;
Joe Onorato46439ce2010-11-19 13:56:21 -08001311
Joe Onoratocb109a02011-01-18 17:57:41 -08001312 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001313 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001314 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001315
1316 * <table>
1317 * <tr><th align=right>priority</th>
1318 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1319 * <tr><th align=right>when</th>
1320 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1321 * <tr><th align=right>audio stream</th>
1322 * <td>{@link #STREAM_DEFAULT}</td></tr>
1323 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001324 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001325
1326 * @param context
1327 * A {@link Context} that will be used by the Builder to construct the
1328 * RemoteViews. The Context will not be held past the lifetime of this Builder
1329 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001330 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001331 public Builder(Context context) {
1332 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001333
1334 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001335 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001336 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001337 mPriority = PRIORITY_DEFAULT;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001338
1339 // TODO: Decide on targetSdk from calling app whether to use quantum theme.
1340 mQuantumTheme = true;
Joe Onorato46439ce2010-11-19 13:56:21 -08001341 }
1342
Joe Onoratocb109a02011-01-18 17:57:41 -08001343 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001344 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001345 * It will be shown in the notification content view by default; use
1346 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001347 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001348 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001349 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001350 public Builder setWhen(long when) {
1351 mWhen = when;
1352 return this;
1353 }
1354
Joe Onoratocb109a02011-01-18 17:57:41 -08001355 /**
Daniel Sandler0c890492012-09-12 17:23:10 -07001356 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown
1357 * in the content view.
1358 */
1359 public Builder setShowWhen(boolean show) {
1360 mShowWhen = show;
1361 return this;
1362 }
1363
1364 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001365 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001366 *
1367 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001368 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001369 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001370 * Useful when showing an elapsed time (like an ongoing phone call).
1371 *
1372 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001373 * @see Notification#when
1374 */
1375 public Builder setUsesChronometer(boolean b) {
1376 mUseChronometer = b;
1377 return this;
1378 }
1379
1380 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001381 * Set the small icon resource, which will be used to represent the notification in the
1382 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001383 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001384
1385 * The platform template for the expanded view will draw this icon in the left, unless a
1386 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1387 * icon will be moved to the right-hand side.
1388 *
1389
1390 * @param icon
1391 * A resource ID in the application's package of the drawable to use.
1392 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001393 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001394 public Builder setSmallIcon(int icon) {
1395 mSmallIcon = icon;
1396 return this;
1397 }
1398
Joe Onoratocb109a02011-01-18 17:57:41 -08001399 /**
1400 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1401 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1402 * LevelListDrawable}.
1403 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001404 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001405 * @param level The level to use for the icon.
1406 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001407 * @see Notification#icon
1408 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001409 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001410 public Builder setSmallIcon(int icon, int level) {
1411 mSmallIcon = icon;
1412 mSmallIconLevel = level;
1413 return this;
1414 }
1415
Joe Onoratocb109a02011-01-18 17:57:41 -08001416 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001417 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001418 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001419 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001420 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001421 return this;
1422 }
1423
Joe Onoratocb109a02011-01-18 17:57:41 -08001424 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001425 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001426 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001427 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001428 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001429 return this;
1430 }
1431
Joe Onoratocb109a02011-01-18 17:57:41 -08001432 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001433 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001434 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1435 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001436 */
1437 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001438 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001439 return this;
1440 }
1441
1442 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001443 * Set the large number at the right-hand side of the notification. This is
1444 * equivalent to setContentInfo, although it might show the number in a different
1445 * font size for readability.
1446 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001447 public Builder setNumber(int number) {
1448 mNumber = number;
1449 return this;
1450 }
1451
Joe Onoratocb109a02011-01-18 17:57:41 -08001452 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001453 * A small piece of additional information pertaining to this notification.
1454 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001455 * The platform template will draw this on the last line of the notification, at the far
1456 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001457 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001458 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001459 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001460 return this;
1461 }
1462
Joe Onoratocb109a02011-01-18 17:57:41 -08001463 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001464 * Set the progress this notification represents.
1465 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001466 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001467 */
1468 public Builder setProgress(int max, int progress, boolean indeterminate) {
1469 mProgressMax = max;
1470 mProgress = progress;
1471 mProgressIndeterminate = indeterminate;
1472 return this;
1473 }
1474
1475 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001476 * Supply a custom RemoteViews to use instead of the platform template.
1477 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001478 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001479 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001480 public Builder setContent(RemoteViews views) {
1481 mContentView = views;
1482 return this;
1483 }
1484
Joe Onoratocb109a02011-01-18 17:57:41 -08001485 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001486 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1487 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001488 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1489 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1490 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001491 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001492 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001493 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001494 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001495 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001496 public Builder setContentIntent(PendingIntent intent) {
1497 mContentIntent = intent;
1498 return this;
1499 }
1500
Joe Onoratocb109a02011-01-18 17:57:41 -08001501 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001502 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1503 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001504 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001505 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001506 public Builder setDeleteIntent(PendingIntent intent) {
1507 mDeleteIntent = intent;
1508 return this;
1509 }
1510
Joe Onoratocb109a02011-01-18 17:57:41 -08001511 /**
1512 * An intent to launch instead of posting the notification to the status bar.
1513 * Only for use with extremely high-priority notifications demanding the user's
1514 * <strong>immediate</strong> attention, such as an incoming phone call or
1515 * alarm clock that the user has explicitly set to a particular time.
1516 * If this facility is used for something else, please give the user an option
1517 * to turn it off and use a normal notification, as this can be extremely
1518 * disruptive.
1519 *
1520 * @param intent The pending intent to launch.
1521 * @param highPriority Passing true will cause this notification to be sent
1522 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001523 *
1524 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001525 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001526 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1527 mFullScreenIntent = intent;
1528 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1529 return this;
1530 }
1531
Joe Onoratocb109a02011-01-18 17:57:41 -08001532 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001533 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001534 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001535 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001536 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001537 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001538 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001539 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001540 return this;
1541 }
1542
Joe Onoratocb109a02011-01-18 17:57:41 -08001543 /**
1544 * Set the text that is displayed in the status bar when the notification first
1545 * arrives, and also a RemoteViews object that may be displayed instead on some
1546 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001547 *
1548 * @see Notification#tickerText
1549 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001550 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001551 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001552 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001553 mTickerView = views;
1554 return this;
1555 }
1556
Joe Onoratocb109a02011-01-18 17:57:41 -08001557 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001558 * Add a large icon to the notification (and the ticker on some devices).
1559 *
1560 * In the platform template, this image will be shown on the left of the notification view
1561 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1562 *
1563 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001564 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001565 public Builder setLargeIcon(Bitmap icon) {
1566 mLargeIcon = icon;
1567 return this;
1568 }
1569
Joe Onoratocb109a02011-01-18 17:57:41 -08001570 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001571 * Set the sound to play.
1572 *
1573 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1574 *
1575 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001576 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001577 public Builder setSound(Uri sound) {
1578 mSound = sound;
1579 mAudioStreamType = STREAM_DEFAULT;
1580 return this;
1581 }
1582
Joe Onoratocb109a02011-01-18 17:57:41 -08001583 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001584 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001585 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001586 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1587 *
1588 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001589 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001590 public Builder setSound(Uri sound, int streamType) {
1591 mSound = sound;
1592 mAudioStreamType = streamType;
1593 return this;
1594 }
1595
Joe Onoratocb109a02011-01-18 17:57:41 -08001596 /**
1597 * Set the vibration pattern to use.
1598 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001599
1600 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1601 * <code>pattern</code> parameter.
1602 *
1603
1604 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001605 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001606 public Builder setVibrate(long[] pattern) {
1607 mVibrate = pattern;
1608 return this;
1609 }
1610
Joe Onoratocb109a02011-01-18 17:57:41 -08001611 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001612 * Set the desired color for the indicator LED on the device, as well as the
1613 * blink duty cycle (specified in milliseconds).
1614 *
1615
1616 * Not all devices will honor all (or even any) of these values.
1617 *
1618
1619 * @see Notification#ledARGB
1620 * @see Notification#ledOnMS
1621 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001622 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001623 public Builder setLights(int argb, int onMs, int offMs) {
1624 mLedArgb = argb;
1625 mLedOnMs = onMs;
1626 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001627 return this;
1628 }
1629
Joe Onoratocb109a02011-01-18 17:57:41 -08001630 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001631 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001632 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001633
1634 * Ongoing notifications cannot be dismissed by the user, so your application or service
1635 * must take care of canceling them.
1636 *
1637
1638 * They are typically used to indicate a background task that the user is actively engaged
1639 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1640 * (e.g., a file download, sync operation, active network connection).
1641 *
1642
1643 * @see Notification#FLAG_ONGOING_EVENT
1644 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001645 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001646 public Builder setOngoing(boolean ongoing) {
1647 setFlag(FLAG_ONGOING_EVENT, ongoing);
1648 return this;
1649 }
1650
Joe Onoratocb109a02011-01-18 17:57:41 -08001651 /**
1652 * Set this flag if you would only like the sound, vibrate
1653 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001654 *
1655 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001656 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001657 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1658 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1659 return this;
1660 }
1661
Joe Onoratocb109a02011-01-18 17:57:41 -08001662 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001663 * Make this notification automatically dismissed when the user touches it. The
1664 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1665 *
1666 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001667 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001668 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001669 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001670 return this;
1671 }
1672
Joe Onoratocb109a02011-01-18 17:57:41 -08001673 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08001674 * Set whether or not this notification should not bridge to other devices.
1675 *
1676 * <p>Some notifications can be bridged to other devices for remote display.
1677 * This hint can be set to recommend this notification not be bridged.
1678 */
1679 public Builder setLocalOnly(boolean localOnly) {
1680 setFlag(FLAG_LOCAL_ONLY, localOnly);
1681 return this;
1682 }
1683
1684 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001685 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001686 * <p>
1687 * The value should be one or more of the following fields combined with
1688 * bitwise-or:
1689 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1690 * <p>
1691 * For all default values, use {@link #DEFAULT_ALL}.
1692 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001693 public Builder setDefaults(int defaults) {
1694 mDefaults = defaults;
1695 return this;
1696 }
1697
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001698 /**
1699 * Set the priority of this notification.
1700 *
1701 * @see Notification#priority
1702 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001703 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001704 mPriority = pri;
1705 return this;
1706 }
Joe Malin8d40d042012-11-05 11:36:40 -08001707
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001708 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001709 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08001710 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04001711 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001712 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04001713 public Builder setCategory(String category) {
1714 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001715 return this;
1716 }
1717
1718 /**
Griff Hazen720042b2014-02-24 15:46:56 -08001719 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001720 *
Griff Hazen720042b2014-02-24 15:46:56 -08001721 * <p>Values within the Bundle will replace existing extras values in this Builder.
1722 *
1723 * @see Notification#extras
1724 */
1725 public Builder addExtras(Bundle bag) {
1726 if (mExtras == null) {
1727 mExtras = new Bundle(bag);
1728 } else {
1729 mExtras.putAll(bag);
1730 }
1731 return this;
1732 }
1733
1734 /**
1735 * Set metadata for this notification.
1736 *
1737 * <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 -04001738 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001739 * called.
1740 *
Griff Hazen720042b2014-02-24 15:46:56 -08001741 * <p>Replaces any existing extras values with those from the provided Bundle.
1742 * Use {@link #addExtras} to merge in metadata instead.
1743 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001744 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001745 */
1746 public Builder setExtras(Bundle bag) {
1747 mExtras = bag;
1748 return this;
1749 }
1750
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001751 /**
Griff Hazen720042b2014-02-24 15:46:56 -08001752 * Get the current metadata Bundle used by this notification Builder.
1753 *
1754 * <p>The returned Bundle is shared with this Builder.
1755 *
1756 * <p>The current contents of this Bundle are copied into the Notification each time
1757 * {@link #build()} is called.
1758 *
1759 * @see Notification#extras
1760 */
1761 public Bundle getExtras() {
1762 if (mExtras == null) {
1763 mExtras = new Bundle();
1764 }
1765 return mExtras;
1766 }
1767
1768 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001769 * Add an action to this notification. Actions are typically displayed by
1770 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001771 * <p>
1772 * Every action must have an icon (32dp square and matching the
1773 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
1774 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
1775 * <p>
1776 * A notification in its expanded form can display up to 3 actions, from left to right in
1777 * the order they were added. Actions will not be displayed when the notification is
1778 * collapsed, however, so be sure that any essential functions may be accessed by the user
1779 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001780 *
1781 * @param icon Resource ID of a drawable that represents the action.
1782 * @param title Text describing the action.
1783 * @param intent PendingIntent to be fired when the action is invoked.
1784 */
1785 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001786 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001787 return this;
1788 }
1789
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001790 /**
1791 * Add a rich notification style to be applied at build time.
1792 *
1793 * @param style Object responsible for modifying the notification style.
1794 */
1795 public Builder setStyle(Style style) {
1796 if (mStyle != style) {
1797 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001798 if (mStyle != null) {
1799 mStyle.setBuilder(this);
1800 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001801 }
1802 return this;
1803 }
1804
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001805 /**
1806 * Specify the value of {@link #visibility}.
1807
1808 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
1809 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
1810 *
1811 * @return The same Builder.
1812 */
1813 public Builder setVisibility(int visibility) {
1814 mVisibility = visibility;
1815 return this;
1816 }
1817
1818 /**
1819 * Supply a replacement Notification whose contents should be shown in insecure contexts
1820 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
1821 * @param n A replacement notification, presumably with some or all info redacted.
1822 * @return The same Builder.
1823 */
1824 public Builder setPublicVersion(Notification n) {
1825 mPublicVersion = n;
1826 return this;
1827 }
1828
Joe Onorato46439ce2010-11-19 13:56:21 -08001829 private void setFlag(int mask, boolean value) {
1830 if (value) {
1831 mFlags |= mask;
1832 } else {
1833 mFlags &= ~mask;
1834 }
1835 }
1836
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001837 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08001838 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001839 boolean showLine3 = false;
1840 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001841 int smallIconImageViewId = R.id.icon;
1842 if (mLargeIcon != null) {
1843 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1844 smallIconImageViewId = R.id.right_icon;
1845 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001846 if (!mQuantumTheme && mPriority < PRIORITY_LOW) {
Daniel Sandlere95658c2012-05-10 00:33:54 -04001847 contentView.setInt(R.id.icon,
1848 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1849 contentView.setInt(R.id.status_bar_latest_event_content,
1850 "setBackgroundResource", R.drawable.notification_bg_low);
1851 }
Joe Onorato561d3852010-11-20 18:09:34 -08001852 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001853 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1854 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001855 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001856 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001857 }
1858 if (mContentTitle != null) {
1859 contentView.setTextViewText(R.id.title, mContentTitle);
1860 }
1861 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001862 contentView.setTextViewText(R.id.text, mContentText);
1863 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001864 }
1865 if (mContentInfo != null) {
1866 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001867 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001868 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001869 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001870 final int tooBig = mContext.getResources().getInteger(
1871 R.integer.status_bar_notification_info_maxnum);
1872 if (mNumber > tooBig) {
1873 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1874 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001875 } else {
1876 NumberFormat f = NumberFormat.getIntegerInstance();
1877 contentView.setTextViewText(R.id.info, f.format(mNumber));
1878 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001879 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001880 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001881 } else {
1882 contentView.setViewVisibility(R.id.info, View.GONE);
1883 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001884
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001885 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001886 if (mSubText != null) {
1887 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001888 if (mContentText != null) {
1889 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001890 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
1891 showLine2 = true;
1892 } else {
1893 contentView.setViewVisibility(R.id.text2, View.GONE);
1894 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001895 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001896 contentView.setViewVisibility(R.id.text2, View.GONE);
1897 if (mProgressMax != 0 || mProgressIndeterminate) {
1898 contentView.setProgressBar(
1899 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1900 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001901 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001902 } else {
1903 contentView.setViewVisibility(R.id.progress, View.GONE);
1904 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001905 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001906 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001907 if (fitIn1U) {
1908 // need to shrink all the type to make sure everything fits
1909 final Resources res = mContext.getResources();
1910 final float subTextSize = res.getDimensionPixelSize(
1911 R.dimen.notification_subtext_size);
1912 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
1913 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001914 // vertical centering
1915 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1916 }
1917
Daniel Sandler0c890492012-09-12 17:23:10 -07001918 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001919 if (mUseChronometer) {
1920 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1921 contentView.setLong(R.id.chronometer, "setBase",
1922 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1923 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1924 } else {
1925 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1926 contentView.setLong(R.id.time, "setTime", mWhen);
1927 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001928 } else {
1929 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001930 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001931
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001932 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001933 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001934 return contentView;
1935 }
1936
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001937 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001938 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001939
1940 int N = mActions.size();
1941 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001942 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001943 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001944 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001945 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04001946 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001947 for (int i=0; i<N; i++) {
1948 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001949 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001950 big.addView(R.id.actions, button);
1951 }
1952 }
1953 return big;
1954 }
1955
Joe Onorato46439ce2010-11-19 13:56:21 -08001956 private RemoteViews makeContentView() {
1957 if (mContentView != null) {
1958 return mContentView;
1959 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001960 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001961 }
1962 }
1963
1964 private RemoteViews makeTickerView() {
1965 if (mTickerView != null) {
1966 return mTickerView;
1967 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001968 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001969 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001970 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001971 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08001972 } else {
1973 return null;
1974 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001975 }
1976 }
1977
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001978 private RemoteViews makeBigContentView() {
1979 if (mActions.size() == 0) return null;
1980
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001981 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001982 }
1983
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001984 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05001985 if (mActions.size() == 0) return null;
1986
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001987 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05001988 }
1989
1990
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001991 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04001992 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08001993 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001994 tombstone ? getActionTombstoneLayoutResource()
1995 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05001996 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001997 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001998 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04001999 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002000 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002001 button.setContentDescription(R.id.action0, action.title);
2002 return button;
2003 }
2004
Joe Onoratocb109a02011-01-18 17:57:41 -08002005 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002006 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002007 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002008 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002009 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002010 Notification n = new Notification();
2011 n.when = mWhen;
2012 n.icon = mSmallIcon;
2013 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002014 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08002015 n.contentView = makeContentView();
2016 n.contentIntent = mContentIntent;
2017 n.deleteIntent = mDeleteIntent;
2018 n.fullScreenIntent = mFullScreenIntent;
2019 n.tickerText = mTickerText;
2020 n.tickerView = makeTickerView();
2021 n.largeIcon = mLargeIcon;
2022 n.sound = mSound;
2023 n.audioStreamType = mAudioStreamType;
2024 n.vibrate = mVibrate;
2025 n.ledARGB = mLedArgb;
2026 n.ledOnMS = mLedOnMs;
2027 n.ledOffMS = mLedOffMs;
2028 n.defaults = mDefaults;
2029 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002030 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002031 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002032 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002033 n.flags |= FLAG_SHOW_LIGHTS;
2034 }
2035 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2036 n.flags |= FLAG_SHOW_LIGHTS;
2037 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002038 n.category = mCategory;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002039 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002040 if (mActions.size() > 0) {
2041 n.actions = new Action[mActions.size()];
2042 mActions.toArray(n.actions);
2043 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002044 n.visibility = mVisibility;
2045
2046 if (mPublicVersion != null) {
2047 n.publicVersion = new Notification();
2048 mPublicVersion.cloneInto(n.publicVersion, true);
2049 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002050
Joe Onorato46439ce2010-11-19 13:56:21 -08002051 return n;
2052 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002053
2054 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002055 * Capture, in the provided bundle, semantic information used in the construction of
2056 * this Notification object.
2057 * @hide
2058 */
Griff Hazen720042b2014-02-24 15:46:56 -08002059 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002060 // Store original information used in the construction of this object
2061 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2062 extras.putCharSequence(EXTRA_TEXT, mContentText);
2063 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2064 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2065 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2066 extras.putInt(EXTRA_PROGRESS, mProgress);
2067 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2068 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2069 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2070 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002071 extras.putBoolean(EXTRA_BUILDER_REMOTE_VIEWS, mContentView == null);
John Spurlockac08a472013-06-10 11:37:08 -04002072 if (mLargeIcon != null) {
2073 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2074 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002075 }
2076
2077 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002078 * @deprecated Use {@link #build()} instead.
2079 */
2080 @Deprecated
2081 public Notification getNotification() {
2082 return build();
2083 }
2084
2085 /**
2086 * Combine all of the options that have been set and return a new {@link Notification}
2087 * object.
2088 */
2089 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002090 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002091
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002092 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002093 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002094 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002095
2096 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2097
Griff Hazen720042b2014-02-24 15:46:56 -08002098 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002099 if (mStyle != null) {
2100 mStyle.addExtras(n.extras);
2101 }
2102
2103 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002104 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002105
2106 /**
2107 * Apply this Builder to an existing {@link Notification} object.
2108 *
2109 * @hide
2110 */
2111 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002112 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002113 return n;
2114 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002115
2116
2117 private int getBaseLayoutResource() {
2118 return mQuantumTheme
2119 ? R.layout.notification_template_quantum_base
2120 : R.layout.notification_template_base;
2121 }
2122
2123 private int getBigBaseLayoutResource() {
2124 return mQuantumTheme
2125 ? R.layout.notification_template_quantum_big_base
2126 : R.layout.notification_template_big_base;
2127 }
2128
2129 private int getBigPictureLayoutResource() {
2130 return mQuantumTheme
2131 ? R.layout.notification_template_quantum_big_picture
2132 : R.layout.notification_template_big_picture;
2133 }
2134
2135 private int getBigTextLayoutResource() {
2136 return mQuantumTheme
2137 ? R.layout.notification_template_quantum_big_text
2138 : R.layout.notification_template_big_text;
2139 }
2140
2141 private int getInboxLayoutResource() {
2142 return mQuantumTheme
2143 ? R.layout.notification_template_quantum_inbox
2144 : R.layout.notification_template_inbox;
2145 }
2146
2147 private int getActionLayoutResource() {
2148 return mQuantumTheme
2149 ? R.layout.notification_quantum_action
2150 : R.layout.notification_action;
2151 }
2152
2153 private int getActionTombstoneLayoutResource() {
2154 return mQuantumTheme
2155 ? R.layout.notification_quantum_action_tombstone
2156 : R.layout.notification_action_tombstone;
2157 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002158 }
2159
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002160 /**
2161 * An object that can apply a rich notification style to a {@link Notification.Builder}
2162 * object.
2163 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002164 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002165 private CharSequence mBigContentTitle;
2166 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002167 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002168
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002169 protected Builder mBuilder;
2170
Chris Wrend6297db2012-05-03 16:20:13 -04002171 /**
2172 * Overrides ContentTitle in the big form of the template.
2173 * This defaults to the value passed to setContentTitle().
2174 */
2175 protected void internalSetBigContentTitle(CharSequence title) {
2176 mBigContentTitle = title;
2177 }
2178
2179 /**
2180 * Set the first line of text after the detail section in the big form of the template.
2181 */
2182 protected void internalSetSummaryText(CharSequence cs) {
2183 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002184 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002185 }
2186
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002187 public void setBuilder(Builder builder) {
2188 if (mBuilder != builder) {
2189 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002190 if (mBuilder != null) {
2191 mBuilder.setStyle(this);
2192 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002193 }
2194 }
2195
Chris Wrend6297db2012-05-03 16:20:13 -04002196 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002197 if (mBuilder == null) {
2198 throw new IllegalArgumentException("Style requires a valid Builder object");
2199 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002200 }
Chris Wrend6297db2012-05-03 16:20:13 -04002201
2202 protected RemoteViews getStandardView(int layoutId) {
2203 checkBuilder();
2204
2205 if (mBigContentTitle != null) {
2206 mBuilder.setContentTitle(mBigContentTitle);
2207 }
2208
Chris Wrend6297db2012-05-03 16:20:13 -04002209 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2210
Chris Wrend6297db2012-05-03 16:20:13 -04002211 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2212 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002213 } else {
2214 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002215 }
2216
Daniel Sandler619738c2012-06-07 16:33:08 -04002217 // The last line defaults to the subtext, but can be replaced by mSummaryText
2218 final CharSequence overflowText =
2219 mSummaryTextSet ? mSummaryText
2220 : mBuilder.mSubText;
2221 if (overflowText != null) {
2222 contentView.setTextViewText(R.id.text, overflowText);
2223 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002224 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002225 } else {
2226 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2227 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002228 }
2229
2230 return contentView;
2231 }
2232
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002233 /**
2234 * @hide
2235 */
2236 public void addExtras(Bundle extras) {
2237 if (mSummaryTextSet) {
2238 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2239 }
2240 if (mBigContentTitle != null) {
2241 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2242 }
Chris Wren91ad5632013-06-05 15:05:57 -04002243 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002244 }
2245
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002246 /**
2247 * @hide
2248 */
2249 public abstract Notification buildStyled(Notification wip);
2250
2251 /**
2252 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2253 * attached to.
2254 *
2255 * @return the fully constructed Notification.
2256 */
2257 public Notification build() {
2258 checkBuilder();
2259 return mBuilder.build();
2260 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002261 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002262
2263 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002264 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002265 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002266 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002267 * <pre class="prettyprint">
2268 * Notification noti = new Notification.BigPictureStyle(
2269 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002270 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002271 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002272 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002273 * .setLargeIcon(aBitmap))
2274 * .bigPicture(aBigBitmap)
2275 * .build();
2276 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002277 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002278 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002279 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002280 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002281 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002282 private Bitmap mBigLargeIcon;
2283 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002284
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002285 public BigPictureStyle() {
2286 }
2287
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002288 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002289 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002290 }
2291
Chris Wrend6297db2012-05-03 16:20:13 -04002292 /**
2293 * Overrides ContentTitle in the big form of the template.
2294 * This defaults to the value passed to setContentTitle().
2295 */
2296 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002297 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002298 return this;
2299 }
2300
2301 /**
2302 * Set the first line of text after the detail section in the big form of the template.
2303 */
2304 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002305 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002306 return this;
2307 }
2308
Chris Wren0bd664d2012-08-01 13:56:56 -04002309 /**
2310 * Provide the bitmap to be used as the payload for the BigPicture notification.
2311 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002312 public BigPictureStyle bigPicture(Bitmap b) {
2313 mPicture = b;
2314 return this;
2315 }
2316
Chris Wren3745a3d2012-05-22 15:11:52 -04002317 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002318 * Override the large icon when the big notification is shown.
2319 */
2320 public BigPictureStyle bigLargeIcon(Bitmap b) {
2321 mBigLargeIconSet = true;
2322 mBigLargeIcon = b;
2323 return this;
2324 }
2325
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002326 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002327 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002328
2329 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2330
2331 return contentView;
2332 }
2333
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002334 /**
2335 * @hide
2336 */
2337 public void addExtras(Bundle extras) {
2338 super.addExtras(extras);
2339
2340 if (mBigLargeIconSet) {
2341 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2342 }
2343 extras.putParcelable(EXTRA_PICTURE, mPicture);
2344 }
2345
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002346 /**
2347 * @hide
2348 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002349 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002350 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002351 if (mBigLargeIconSet ) {
2352 mBuilder.mLargeIcon = mBigLargeIcon;
2353 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002354 wip.bigContentView = makeBigContentView();
2355 return wip;
2356 }
2357 }
2358
2359 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002360 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002361 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002362 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002363 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05002364 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002365 * new Notification.Builder()
2366 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2367 * .setContentText(subject)
2368 * .setSmallIcon(R.drawable.new_mail)
2369 * .setLargeIcon(aBitmap))
2370 * .bigText(aVeryLongString)
2371 * .build();
2372 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002373 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002374 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002375 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002376 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002377 private CharSequence mBigText;
2378
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002379 public BigTextStyle() {
2380 }
2381
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002382 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002383 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002384 }
2385
Chris Wrend6297db2012-05-03 16:20:13 -04002386 /**
2387 * Overrides ContentTitle in the big form of the template.
2388 * This defaults to the value passed to setContentTitle().
2389 */
2390 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002391 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002392 return this;
2393 }
2394
2395 /**
2396 * Set the first line of text after the detail section in the big form of the template.
2397 */
2398 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002399 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002400 return this;
2401 }
2402
Chris Wren0bd664d2012-08-01 13:56:56 -04002403 /**
2404 * Provide the longer text to be displayed in the big form of the
2405 * template in place of the content text.
2406 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002407 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002408 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002409 return this;
2410 }
2411
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002412 /**
2413 * @hide
2414 */
2415 public void addExtras(Bundle extras) {
2416 super.addExtras(extras);
2417
2418 extras.putCharSequence(EXTRA_TEXT, mBigText);
2419 }
2420
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002421 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002422 // Remove the content text so line3 only shows if you have a summary
2423 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002424 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04002425
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002426 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08002427
Daniel Sandler619738c2012-06-07 16:33:08 -04002428 if (hadThreeLines) {
2429 // vertical centering
2430 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2431 }
2432
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002433 contentView.setTextViewText(R.id.big_text, mBigText);
2434 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04002435 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002436
2437 return contentView;
2438 }
2439
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002440 /**
2441 * @hide
2442 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002443 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002444 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002445 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002446
2447 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
2448
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002449 return wip;
2450 }
2451 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04002452
2453 /**
2454 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08002455 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002456 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
2457 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002458 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04002459 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04002460 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04002461 * .setContentText(subject)
2462 * .setSmallIcon(R.drawable.new_mail)
2463 * .setLargeIcon(aBitmap))
2464 * .addLine(str1)
2465 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04002466 * .setContentTitle("")
2467 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04002468 * .build();
2469 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002470 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002471 * @see Notification#bigContentView
2472 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002473 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002474 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
2475
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002476 public InboxStyle() {
2477 }
2478
Daniel Sandler879c5e02012-04-17 16:46:51 -04002479 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002480 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002481 }
2482
Chris Wrend6297db2012-05-03 16:20:13 -04002483 /**
2484 * Overrides ContentTitle in the big form of the template.
2485 * This defaults to the value passed to setContentTitle().
2486 */
2487 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002488 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002489 return this;
2490 }
2491
2492 /**
2493 * Set the first line of text after the detail section in the big form of the template.
2494 */
2495 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002496 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002497 return this;
2498 }
2499
Chris Wren0bd664d2012-08-01 13:56:56 -04002500 /**
2501 * Append a line to the digest section of the Inbox notification.
2502 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04002503 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002504 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04002505 return this;
2506 }
2507
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002508 /**
2509 * @hide
2510 */
2511 public void addExtras(Bundle extras) {
2512 super.addExtras(extras);
2513 CharSequence[] a = new CharSequence[mTexts.size()];
2514 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
2515 }
2516
Daniel Sandler879c5e02012-04-17 16:46:51 -04002517 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002518 // Remove the content text so line3 disappears unless you have a summary
2519 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002520 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04002521
Chris Wrend6297db2012-05-03 16:20:13 -04002522 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002523
Chris Wrend6297db2012-05-03 16:20:13 -04002524 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 -04002525 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04002526
Chris Wren4ed80d52012-05-17 09:30:03 -04002527 // Make sure all rows are gone in case we reuse a view.
2528 for (int rowId : rowIds) {
2529 contentView.setViewVisibility(rowId, View.GONE);
2530 }
2531
Chris Wren683ab002012-09-20 10:35:54 -04002532
Daniel Sandler879c5e02012-04-17 16:46:51 -04002533 int i=0;
2534 while (i < mTexts.size() && i < rowIds.length) {
2535 CharSequence str = mTexts.get(i);
2536 if (str != null && !str.equals("")) {
2537 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
2538 contentView.setTextViewText(rowIds[i], str);
2539 }
2540 i++;
2541 }
2542
Chris Wren683ab002012-09-20 10:35:54 -04002543 contentView.setViewVisibility(R.id.inbox_end_pad,
2544 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
2545
2546 contentView.setViewVisibility(R.id.inbox_more,
2547 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04002548
Daniel Sandler879c5e02012-04-17 16:46:51 -04002549 return contentView;
2550 }
2551
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002552 /**
2553 * @hide
2554 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002555 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002556 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002557 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002558
Daniel Sandler879c5e02012-04-17 16:46:51 -04002559 return wip;
2560 }
2561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562}