blob: 38ab15f34f56d9775ec018a125b441b561a41994 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.Context;
20import android.content.Intent;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040021import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040022import android.graphics.Bitmap;
Jeff Sharkey098d5802012-04-26 17:30:34 -070023import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040025import android.os.BadParcelableException;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050026import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Parcel;
28import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040029import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070030import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040032import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040033import android.util.TypedValue;
Griff Hazenc3104152014-05-22 14:38:36 -070034import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080035import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070036import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.widget.RemoteViews;
38
Griff Hazen959591e2014-05-15 22:26:18 -070039import com.android.internal.R;
40
Andy Stadler110988c2010-12-03 14:29:16 -080041import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050042import java.util.ArrayList;
Griff Hazenc3104152014-05-22 14:38:36 -070043import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070044import java.util.Collections;
Griff Hazenc3104152014-05-22 14:38:36 -070045import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047/**
48 * A class that represents how a persistent notification is to be presented to
49 * the user using the {@link android.app.NotificationManager}.
50 *
Joe Onoratocb109a02011-01-18 17:57:41 -080051 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
52 * easier to construct Notifications.</p>
53 *
Joe Fernandez558459f2011-10-13 16:47:36 -070054 * <div class="special reference">
55 * <h3>Developer Guides</h3>
56 * <p>For a guide to creating notifications, read the
57 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
58 * developer guide.</p>
59 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 */
61public class Notification implements Parcelable
62{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040063 private static final String TAG = "Notification";
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 /**
66 * Use all default values (where applicable).
67 */
68 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 /**
71 * Use the default notification sound. This will ignore any given
72 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050073 *
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050076 */
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 public static final int DEFAULT_SOUND = 1;
79
80 /**
81 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050082 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070083 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050084 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050086 */
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
91 * Use the default notification lights. This will ignore the
92 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
93 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050096 */
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500101 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800102 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500103 * Default value: {@link System#currentTimeMillis() Now}.
104 *
105 * Choose a timestamp that will be most relevant to the user. For most finite events, this
106 * corresponds to the time the event happened (or will happen, in the case of events that have
107 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800108 * timestamped according to when the activity began.
109 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800111 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 * <ul>
113 * <li>Notification of a new chat message should be stamped when the message was received.</li>
114 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
115 * <li>Notification of a completed file download should be stamped when the download finished.</li>
116 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
117 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
118 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800119 * </ul>
120 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 */
122 public long when;
123
124 /**
125 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500126 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 */
128 public int icon;
129
130 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800131 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
132 * leave it at its default value of 0.
133 *
134 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700135 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800136 */
137 public int iconLevel;
138
139 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140 * The number of events that this notification represents. For example, in a new mail
141 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500143 * The system may or may not use this field to modify the appearance of the notification. For
144 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
145 * superimposed over the icon in the status bar. Starting with
146 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
147 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800148 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500149 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
151 public int number;
152
153 /**
154 * The intent to execute when the expanded status entry is clicked. If
155 * this is an activity, it must include the
156 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800157 * that you take care of task management as described in the
158 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800159 * Stack</a> document. In particular, make sure to read the notification section
160 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
161 * Notifications</a> for the correct ways to launch an application from a
162 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 */
164 public PendingIntent contentIntent;
165
166 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * The intent to execute when the notification is explicitly dismissed by the user, either with
168 * the "Clear All" button or by swiping it away individually.
169 *
170 * This probably shouldn't be launching an activity since several of those will be sent
171 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 */
173 public PendingIntent deleteIntent;
174
175 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700176 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800177 *
178 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400179 */
180 public PendingIntent fullScreenIntent;
181
182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400184 * the status bar on large and smaller devices.
185 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800186 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 */
188 public CharSequence tickerText;
189
190 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800191 * The view to show as the ticker in the status bar when the notification
192 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400193 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800194 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400195
196 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400197 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 */
199 public RemoteViews contentView;
200
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400201 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400202 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400203 * opportunity to show more detail. The system UI may choose to show this
204 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400205 */
206 public RemoteViews bigContentView;
207
208 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800209 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800211 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
213 /**
214 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500217 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * </p>
219 */
220 public Uri sound;
221
222 /**
223 * Use this constant as the value for audioStreamType to request that
224 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700225 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
227 public static final int STREAM_DEFAULT = -1;
228
229 /**
230 * The audio stream type to use when playing the sound.
231 * Should be one of the STREAM_ constants from
232 * {@link android.media.AudioManager}.
233 */
234 public int audioStreamType = STREAM_DEFAULT;
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500237 * The pattern with which to vibrate.
238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 * <p>
240 * To vibrate the default pattern, see {@link #defaults}.
241 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 * @see android.os.Vibrator#vibrate(long[],int)
244 */
245 public long[] vibrate;
246
247 /**
248 * The color of the led. The hardware will do its best approximation.
249 *
250 * @see #FLAG_SHOW_LIGHTS
251 * @see #flags
252 */
253 public int ledARGB;
254
255 /**
256 * The number of milliseconds for the LED to be on while it's flashing.
257 * The hardware will do its best approximation.
258 *
259 * @see #FLAG_SHOW_LIGHTS
260 * @see #flags
261 */
262 public int ledOnMS;
263
264 /**
265 * The number of milliseconds for the LED to be off while it's flashing.
266 * The hardware will do its best approximation.
267 *
268 * @see #FLAG_SHOW_LIGHTS
269 * @see #flags
270 */
271 public int ledOffMS;
272
273 /**
274 * Specifies which values should be taken from the defaults.
275 * <p>
276 * To set, OR the desired from {@link #DEFAULT_SOUND},
277 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
278 * values, use {@link #DEFAULT_ALL}.
279 * </p>
280 */
281 public int defaults;
282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Bit to be bitwise-ored into the {@link #flags} field that should be
285 * set if you want the LED on for this notification.
286 * <ul>
287 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
288 * or 0 for both ledOnMS and ledOffMS.</li>
289 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
290 * <li>To flash the LED, pass the number of milliseconds that it should
291 * be on and off to ledOnMS and ledOffMS.</li>
292 * </ul>
293 * <p>
294 * Since hardware varies, you are not guaranteed that any of the values
295 * you pass are honored exactly. Use the system defaults (TODO) if possible
296 * because they will be set to values that work on any given hardware.
297 * <p>
298 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
301 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
302
303 /**
304 * Bit to be bitwise-ored into the {@link #flags} field that should be
305 * set if this notification is in reference to something that is ongoing,
306 * like a phone call. It should not be set if this notification is in
307 * reference to something that happened at a particular point in time,
308 * like a missed phone call.
309 */
310 public static final int FLAG_ONGOING_EVENT = 0x00000002;
311
312 /**
313 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700314 * the audio will be repeated until the notification is
315 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 */
317 public static final int FLAG_INSISTENT = 0x00000004;
318
319 /**
320 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700321 * set if you would only like the sound, vibrate and ticker to be played
322 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
324 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
325
326 /**
327 * Bit to be bitwise-ored into the {@link #flags} field that should be
328 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500329 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 */
332 public static final int FLAG_AUTO_CANCEL = 0x00000010;
333
334 /**
335 * Bit to be bitwise-ored into the {@link #flags} field that should be
336 * set if the notification should not be canceled when the user clicks
337 * the Clear all button.
338 */
339 public static final int FLAG_NO_CLEAR = 0x00000020;
340
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700341 /**
342 * Bit to be bitwise-ored into the {@link #flags} field that should be
343 * set if this notification represents a currently running service. This
344 * will normally be set for you by {@link Service#startForeground}.
345 */
346 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
347
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400348 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500349 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800350 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500351 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400352 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500353 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400354
Griff Hazendfcb0802014-02-11 12:00:00 -0800355 /**
356 * Bit to be bitswise-ored into the {@link #flags} field that should be
357 * set if this notification is relevant to the current device only
358 * and it is not recommended that it bridge to other devices.
359 */
360 public static final int FLAG_LOCAL_ONLY = 0x00000100;
361
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700362 /**
363 * Bit to be bitswise-ored into the {@link #flags} field that should be
364 * set if this notification is the group summary for a group of notifications.
365 * Grouped notifications may display in a cluster or stack on devices which
366 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
367 */
368 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 public int flags;
371
372 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500373 * Default notification {@link #priority}. If your application does not prioritize its own
374 * notifications, use this value for all notifications.
375 */
376 public static final int PRIORITY_DEFAULT = 0;
377
378 /**
379 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
380 * items smaller, or at a different position in the list, compared with your app's
381 * {@link #PRIORITY_DEFAULT} items.
382 */
383 public static final int PRIORITY_LOW = -1;
384
385 /**
386 * Lowest {@link #priority}; these items might not be shown to the user except under special
387 * circumstances, such as detailed notification logs.
388 */
389 public static final int PRIORITY_MIN = -2;
390
391 /**
392 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
393 * show these items larger, or at a different position in notification lists, compared with
394 * your app's {@link #PRIORITY_DEFAULT} items.
395 */
396 public static final int PRIORITY_HIGH = 1;
397
398 /**
399 * Highest {@link #priority}, for your application's most important items that require the
400 * user's prompt attention or input.
401 */
402 public static final int PRIORITY_MAX = 2;
403
404 /**
405 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800406 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500407 * Priority is an indication of how much of the user's valuable attention should be consumed by
408 * this notification. Low-priority notifications may be hidden from the user in certain
409 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500410 * system will make a determination about how to interpret this priority when presenting
411 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500412 */
413 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800414
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500415 /**
John Spurlockcf44a122014-03-24 11:02:36 -0400416 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Griff Hazened0c87e2014-05-05 15:15:12 -0700417 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500418 */
John Spurlockcf44a122014-03-24 11:02:36 -0400419 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500420
421 /**
John Spurlockcf44a122014-03-24 11:02:36 -0400422 * Notification category: incoming direct message (SMS, instant message, etc.).
Griff Hazened0c87e2014-05-05 15:15:12 -0700423 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500424 */
John Spurlockcf44a122014-03-24 11:02:36 -0400425 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500426
427 /**
John Spurlockcf44a122014-03-24 11:02:36 -0400428 * Notification category: asynchronous bulk message (email).
Griff Hazened0c87e2014-05-05 15:15:12 -0700429 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500430 */
John Spurlockcf44a122014-03-24 11:02:36 -0400431 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500432
433 /**
John Spurlockcf44a122014-03-24 11:02:36 -0400434 * Notification category: calendar event.
Griff Hazened0c87e2014-05-05 15:15:12 -0700435 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500436 */
John Spurlockcf44a122014-03-24 11:02:36 -0400437 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500438
439 /**
John Spurlockcf44a122014-03-24 11:02:36 -0400440 * Notification category: promotion or advertisement.
Griff Hazened0c87e2014-05-05 15:15:12 -0700441 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500442 */
John Spurlockcf44a122014-03-24 11:02:36 -0400443 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500444
445 /**
John Spurlockcf44a122014-03-24 11:02:36 -0400446 * Notification category: alarm or timer.
Griff Hazened0c87e2014-05-05 15:15:12 -0700447 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 */
John Spurlockcf44a122014-03-24 11:02:36 -0400449 public static final String CATEGORY_ALARM = "alarm";
450
451 /**
452 * Notification category: progress of a long-running background operation.
Griff Hazened0c87e2014-05-05 15:15:12 -0700453 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400454 */
455 public static final String CATEGORY_PROGRESS = "progress";
456
457 /**
458 * Notification category: social network or sharing update.
Griff Hazened0c87e2014-05-05 15:15:12 -0700459 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400460 */
461 public static final String CATEGORY_SOCIAL = "social";
462
463 /**
464 * Notification category: error in background operation or authentication status.
Griff Hazened0c87e2014-05-05 15:15:12 -0700465 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400466 */
467 public static final String CATEGORY_ERROR = "err";
468
469 /**
470 * Notification category: media transport control for playback.
Griff Hazened0c87e2014-05-05 15:15:12 -0700471 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400472 */
473 public static final String CATEGORY_TRANSPORT = "transport";
474
475 /**
476 * Notification category: system or device status update. Reserved for system use.
Griff Hazened0c87e2014-05-05 15:15:12 -0700477 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400478 */
479 public static final String CATEGORY_SYSTEM = "sys";
480
481 /**
482 * Notification category: indication of running background service.
Griff Hazened0c87e2014-05-05 15:15:12 -0700483 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400484 */
485 public static final String CATEGORY_SERVICE = "service";
486
487 /**
488 * Notification category: a specific, timely recommendation for a single thing.
489 * For example, a news app might want to recommend a news story it believes the user will
490 * want to read next.
Griff Hazened0c87e2014-05-05 15:15:12 -0700491 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400492 */
493 public static final String CATEGORY_RECOMMENDATION = "recommendation";
494
495 /**
496 * Notification category: ongoing information about device or contextual status.
Griff Hazened0c87e2014-05-05 15:15:12 -0700497 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400498 */
499 public static final String CATEGORY_STATUS = "status";
500
501 /**
502 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
503 * that best describes this Notification. May be used by the system for ranking and filtering.
Griff Hazened0c87e2014-05-05 15:15:12 -0700504 * @hide
John Spurlockcf44a122014-03-24 11:02:36 -0400505 */
506 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500507
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700508 private String mGroupKey;
509
510 /**
511 * Get the key used to group this notification into a cluster or stack
512 * with other notifications on devices which support such rendering.
513 */
514 public String getGroup() {
515 return mGroupKey;
516 }
517
518 private String mSortKey;
519
520 /**
521 * Get a sort key that orders this notification among other notifications from the
522 * same package. This can be useful if an external sort was already applied and an app
523 * would like to preserve this. Notifications will be sorted lexicographically using this
524 * value, although providing different priorities in addition to providing sort key may
525 * cause this value to be ignored.
526 *
527 * <p>This sort key can also be used to order members of a notification group. See
528 * {@link Builder#setGroup}.
529 *
530 * @see String#compareTo(String)
531 */
532 public String getSortKey() {
533 return mSortKey;
534 }
535
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400537 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400538 * <p>
539 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
540 * APIs, and are intended to be used by
541 * {@link android.service.notification.NotificationListenerService} implementations to extract
542 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500543 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400544 public Bundle extras = new Bundle();
545
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400546 /**
547 * {@link #extras} key: this is the title of the notification,
548 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
549 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500550 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400551
552 /**
553 * {@link #extras} key: this is the title of the notification when shown in expanded form,
554 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
555 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400556 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400557
558 /**
559 * {@link #extras} key: this is the main text payload, as supplied to
560 * {@link Builder#setContentText(CharSequence)}.
561 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500562 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400563
564 /**
565 * {@link #extras} key: this is a third line of text, as supplied to
566 * {@link Builder#setSubText(CharSequence)}.
567 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400568 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400569
570 /**
571 * {@link #extras} key: this is a small piece of additional text as supplied to
572 * {@link Builder#setContentInfo(CharSequence)}.
573 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400574 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400575
576 /**
577 * {@link #extras} key: this is a line of summary information intended to be shown
578 * alongside expanded notifications, as supplied to (e.g.)
579 * {@link BigTextStyle#setSummaryText(CharSequence)}.
580 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400581 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400582
583 /**
584 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
585 * supplied to {@link Builder#setSmallIcon(int)}.
586 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500587 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400588
589 /**
590 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
591 * notification payload, as
592 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
593 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400594 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400595
596 /**
597 * {@link #extras} key: this is a bitmap to be used instead of the one from
598 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
599 * shown in its expanded form, as supplied to
600 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
601 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400602 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400603
604 /**
605 * {@link #extras} key: this is the progress value supplied to
606 * {@link Builder#setProgress(int, int, boolean)}.
607 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400608 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400609
610 /**
611 * {@link #extras} key: this is the maximum value supplied to
612 * {@link Builder#setProgress(int, int, boolean)}.
613 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400614 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400615
616 /**
617 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
618 * {@link Builder#setProgress(int, int, boolean)}.
619 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400620 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400621
622 /**
623 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
624 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
625 * {@link Builder#setUsesChronometer(boolean)}.
626 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400627 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400628
629 /**
630 * {@link #extras} key: whether {@link #when} should be shown,
631 * as supplied to {@link Builder#setShowWhen(boolean)}.
632 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400633 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400634
635 /**
636 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
637 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
638 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400639 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400640
641 /**
642 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
643 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
644 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400645 public static final String EXTRA_TEXT_LINES = "android.textLines";
646
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400647 /**
648 * {@link #extras} key: An array of people that this notification relates to, specified
649 * by contacts provider contact URI.
650 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400651 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652
653 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400654 * @hide
655 * Extra added by NotificationManagerService to indicate whether a NotificationScorer
656 * modified the Notifications's score.
657 */
658 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
659
660 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400661 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400662 * @hide
663 */
664 public static final String EXTRA_AS_HEADS_UP = "headsup";
665
666 /**
John Spurlockcf44a122014-03-24 11:02:36 -0400667 * Allow certain system-generated notifications to appear before the device is provisioned.
668 * Only available to notifications coming from the android package.
669 * @hide
670 */
671 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
672
673 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400674 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400675 * @hide
676 */
677 public static final int HEADS_UP_NEVER = 0;
678
679 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400680 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400681 * @hide
682 */
683 public static final int HEADS_UP_ALLOWED = 1;
684
685 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400686 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400687 * @hide
688 */
689 public static final int HEADS_UP_REQUESTED = 2;
690
691 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400692 * Structure to encapsulate a named action that can be shown as part of this notification.
693 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
694 * selected by the user.
695 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700696 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
697 * or {@link Notification.Builder#addAction(Notification.Action)}
698 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400699 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500700 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700701 private final Bundle mExtras;
Griff Hazenc3104152014-05-22 14:38:36 -0700702 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700703
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400704 /**
705 * Small icon representing the action.
706 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400707 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700708
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400709 /**
710 * Title of the action.
711 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400712 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700713
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400714 /**
715 * Intent to send when the user invokes this action. May be null, in which case the action
716 * may be rendered in a disabled presentation by the system UI.
717 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400718 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700719
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400720 private Action(Parcel in) {
721 icon = in.readInt();
722 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
723 if (in.readInt() == 1) {
724 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
725 }
Griff Hazen959591e2014-05-15 22:26:18 -0700726 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700727 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400728 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700729
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400730 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700731 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400732 */
733 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700734 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700735 }
736
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700737 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
738 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400739 this.icon = icon;
740 this.title = title;
741 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700742 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700743 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700744 }
745
746 /**
747 * Get additional metadata carried around with this Action.
748 */
749 public Bundle getExtras() {
750 return mExtras;
751 }
752
753 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700754 * Get the list of inputs to be collected from the user when this action is sent.
755 * May return null if no remote inputs were added.
756 */
757 public RemoteInput[] getRemoteInputs() {
758 return mRemoteInputs;
759 }
760
761 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700762 * Builder class for {@link Action} objects.
763 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700764 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700765 private final int mIcon;
766 private final CharSequence mTitle;
767 private final PendingIntent mIntent;
768 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700769 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700770
771 /**
772 * Construct a new builder for {@link Action} object.
773 * @param icon icon to show for this action
774 * @param title the title of the action
775 * @param intent the {@link PendingIntent} to fire when users trigger this action
776 */
777 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700778 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700779 }
780
781 /**
782 * Construct a new builder for {@link Action} object using the fields from an
783 * {@link Action}.
784 * @param action the action to read fields from.
785 */
786 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700787 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
788 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700789 }
790
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700791 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
792 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700793 mIcon = icon;
794 mTitle = title;
795 mIntent = intent;
796 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700797 if (remoteInputs != null) {
798 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
799 Collections.addAll(mRemoteInputs, remoteInputs);
800 }
Griff Hazen959591e2014-05-15 22:26:18 -0700801 }
802
803 /**
804 * Merge additional metadata into this builder.
805 *
806 * <p>Values within the Bundle will replace existing extras values in this Builder.
807 *
808 * @see Notification.Action#extras
809 */
810 public Builder addExtras(Bundle extras) {
811 if (extras != null) {
812 mExtras.putAll(extras);
813 }
814 return this;
815 }
816
817 /**
818 * Get the metadata Bundle used by this Builder.
819 *
820 * <p>The returned Bundle is shared with this Builder.
821 */
822 public Bundle getExtras() {
823 return mExtras;
824 }
825
826 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700827 * Add an input to be collected from the user when this action is sent.
828 * Response values can be retrieved from the fired intent by using the
829 * {@link RemoteInput#getResultsFromIntent} function.
830 * @param remoteInput a {@link RemoteInput} to add to the action
831 * @return this object for method chaining
832 */
833 public Builder addRemoteInput(RemoteInput remoteInput) {
834 if (mRemoteInputs == null) {
835 mRemoteInputs = new ArrayList<RemoteInput>();
836 }
837 mRemoteInputs.add(remoteInput);
838 return this;
839 }
840
841 /**
842 * Apply an extender to this action builder. Extenders may be used to add
843 * metadata or change options on this builder.
844 */
Griff Hazenc3104152014-05-22 14:38:36 -0700845 public Builder extend(Extender extender) {
846 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700847 return this;
848 }
849
850 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700851 * Combine all of the options that have been set and return a new {@link Action}
852 * object.
853 * @return the built action
854 */
855 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700856 RemoteInput[] remoteInputs = mRemoteInputs != null
857 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
858 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -0700859 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400860 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400861
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400862 @Override
863 public Action clone() {
864 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700865 icon,
866 title,
867 actionIntent, // safe to alias
868 new Bundle(mExtras),
869 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400870 }
871 @Override
872 public int describeContents() {
873 return 0;
874 }
875 @Override
876 public void writeToParcel(Parcel out, int flags) {
877 out.writeInt(icon);
878 TextUtils.writeToParcel(title, out, flags);
879 if (actionIntent != null) {
880 out.writeInt(1);
881 actionIntent.writeToParcel(out, flags);
882 } else {
883 out.writeInt(0);
884 }
Griff Hazen959591e2014-05-15 22:26:18 -0700885 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700886 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400887 }
Griff Hazen959591e2014-05-15 22:26:18 -0700888 public static final Parcelable.Creator<Action> CREATOR =
889 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400890 public Action createFromParcel(Parcel in) {
891 return new Action(in);
892 }
893 public Action[] newArray(int size) {
894 return new Action[size];
895 }
896 };
Griff Hazenc3104152014-05-22 14:38:36 -0700897
898 /**
899 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
900 * metadata or change options on an action builder.
901 */
902 public interface Extender {
903 /**
904 * Apply this extender to a notification action builder.
905 * @param builder the builder to be modified.
906 * @return the build object for chaining.
907 */
908 public Builder extend(Builder builder);
909 }
910
911 /**
912 * Wearable extender for notification actions. To add extensions to an action,
913 * create a new {@link android.app.Notification.Action.WearableExtender} object using
914 * the {@code WearableExtender()} constructor and apply it to a
915 * {@link android.app.Notification.Action.Builder} using
916 * {@link android.app.Notification.Action.Builder#extend}.
917 *
918 * <pre class="prettyprint">
919 * Notification.Action action = new Notification.Action.Builder(
920 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen6f72ac52014-05-26 09:07:14 -0700921 * .extend(new Notification.Action.WearableExtender()
Griff Hazenc3104152014-05-22 14:38:36 -0700922 * .setAvailableOffline(false))
Griff Hazen6f72ac52014-05-26 09:07:14 -0700923 * .build();</pre>
Griff Hazenc3104152014-05-22 14:38:36 -0700924 */
925 public static final class WearableExtender implements Extender {
926 /** Notification action extra which contains wearable extensions */
927 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
928
929 private static final String KEY_FLAGS = "flags";
930
931 // Flags bitwise-ored to mFlags
932 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
933
934 // Default value for flags integer
935 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
936
937 private int mFlags = DEFAULT_FLAGS;
938
939 /**
940 * Create a {@link android.app.Notification.Action.WearableExtender} with default
941 * options.
942 */
943 public WearableExtender() {
944 }
945
946 /**
947 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
948 * wearable options present in an existing notification action.
949 * @param action the notification action to inspect.
950 */
951 public WearableExtender(Action action) {
952 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
953 if (wearableBundle != null) {
954 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
955 }
956 }
957
958 /**
959 * Apply wearable extensions to a notification action that is being built. This is
960 * typically called by the {@link android.app.Notification.Action.Builder#extend}
961 * method of {@link android.app.Notification.Action.Builder}.
962 */
963 @Override
964 public Action.Builder extend(Action.Builder builder) {
965 Bundle wearableBundle = new Bundle();
966
967 if (mFlags != DEFAULT_FLAGS) {
968 wearableBundle.putInt(KEY_FLAGS, mFlags);
969 }
970
971 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
972 return builder;
973 }
974
975 @Override
976 public WearableExtender clone() {
977 WearableExtender that = new WearableExtender();
978 that.mFlags = this.mFlags;
979 return that;
980 }
981
982 /**
983 * Set whether this action is available when the wearable device is not connected to
984 * a companion device. The user can still trigger this action when the wearable device is
985 * offline, but a visual hint will indicate that the action may not be available.
986 * Defaults to true.
987 */
988 public WearableExtender setAvailableOffline(boolean availableOffline) {
989 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
990 return this;
991 }
992
993 /**
994 * Get whether this action is available when the wearable device is not connected to
995 * a companion device. The user can still trigger this action when the wearable device is
996 * offline, but a visual hint will indicate that the action may not be available.
997 * Defaults to true.
998 */
999 public boolean isAvailableOffline() {
1000 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1001 }
1002
1003 private void setFlag(int mask, boolean value) {
1004 if (value) {
1005 mFlags |= mask;
1006 } else {
1007 mFlags &= ~mask;
1008 }
1009 }
1010 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001011 }
1012
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013 /**
1014 * Array of all {@link Action} structures attached to this notification by
1015 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1016 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1017 * interface for invoking actions.
1018 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001019 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020
1021 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001022 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001023 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public Notification()
1026 {
1027 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001028 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
1030
1031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 * @hide
1033 */
1034 public Notification(Context context, int icon, CharSequence tickerText, long when,
1035 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1036 {
1037 this.when = when;
1038 this.icon = icon;
1039 this.tickerText = tickerText;
1040 setLatestEventInfo(context, contentTitle, contentText,
1041 PendingIntent.getActivity(context, 0, contentIntent, 0));
1042 }
1043
1044 /**
1045 * Constructs a Notification object with the information needed to
1046 * have a status bar icon without the standard expanded view.
1047 *
1048 * @param icon The resource id of the icon to put in the status bar.
1049 * @param tickerText The text that flows by in the status bar when the notification first
1050 * activates.
1051 * @param when The time to show in the time field. In the System.currentTimeMillis
1052 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001053 *
1054 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001056 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 public Notification(int icon, CharSequence tickerText, long when)
1058 {
1059 this.icon = icon;
1060 this.tickerText = tickerText;
1061 this.when = when;
1062 }
1063
1064 /**
1065 * Unflatten the notification from a parcel.
1066 */
1067 public Notification(Parcel parcel)
1068 {
1069 int version = parcel.readInt();
1070
1071 when = parcel.readLong();
1072 icon = parcel.readInt();
1073 number = parcel.readInt();
1074 if (parcel.readInt() != 0) {
1075 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1076 }
1077 if (parcel.readInt() != 0) {
1078 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1079 }
1080 if (parcel.readInt() != 0) {
1081 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1082 }
1083 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001084 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001085 }
1086 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1088 }
Joe Onorato561d3852010-11-20 18:09:34 -08001089 if (parcel.readInt() != 0) {
1090 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 defaults = parcel.readInt();
1093 flags = parcel.readInt();
1094 if (parcel.readInt() != 0) {
1095 sound = Uri.CREATOR.createFromParcel(parcel);
1096 }
1097
1098 audioStreamType = parcel.readInt();
1099 vibrate = parcel.createLongArray();
1100 ledARGB = parcel.readInt();
1101 ledOnMS = parcel.readInt();
1102 ledOffMS = parcel.readInt();
1103 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001104
1105 if (parcel.readInt() != 0) {
1106 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1107 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001108
1109 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001110
John Spurlockcf44a122014-03-24 11:02:36 -04001111 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001112
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001113 mGroupKey = parcel.readString();
1114
1115 mSortKey = parcel.readString();
1116
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001117 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001118
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001119 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1120
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001121 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001122 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125
Andy Stadler110988c2010-12-03 14:29:16 -08001126 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001127 public Notification clone() {
1128 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001129 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001130 return that;
1131 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001132
Daniel Sandler1a497d32013-04-18 14:52:45 -04001133 /**
1134 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1135 * of this into that.
1136 * @hide
1137 */
1138 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001139 that.when = this.when;
1140 that.icon = this.icon;
1141 that.number = this.number;
1142
1143 // PendingIntents are global, so there's no reason (or way) to clone them.
1144 that.contentIntent = this.contentIntent;
1145 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001146 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001147
1148 if (this.tickerText != null) {
1149 that.tickerText = this.tickerText.toString();
1150 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001151 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001152 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001153 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001154 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001155 that.contentView = this.contentView.clone();
1156 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001157 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001158 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1159 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001160 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001161 that.sound = this.sound; // android.net.Uri is immutable
1162 that.audioStreamType = this.audioStreamType;
1163
1164 final long[] vibrate = this.vibrate;
1165 if (vibrate != null) {
1166 final int N = vibrate.length;
1167 final long[] vib = that.vibrate = new long[N];
1168 System.arraycopy(vibrate, 0, vib, 0, N);
1169 }
1170
1171 that.ledARGB = this.ledARGB;
1172 that.ledOnMS = this.ledOnMS;
1173 that.ledOffMS = this.ledOffMS;
1174 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001175
Joe Onorato18e69df2010-05-17 22:26:12 -07001176 that.flags = this.flags;
1177
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001178 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001179
John Spurlockcf44a122014-03-24 11:02:36 -04001180 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001181
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001182 that.mGroupKey = this.mGroupKey;
1183
1184 that.mSortKey = this.mSortKey;
1185
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001186 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001187 try {
1188 that.extras = new Bundle(this.extras);
1189 // will unparcel
1190 that.extras.size();
1191 } catch (BadParcelableException e) {
1192 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1193 that.extras = null;
1194 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001195 }
1196
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001197 if (this.actions != null) {
1198 that.actions = new Action[this.actions.length];
1199 for(int i=0; i<this.actions.length; i++) {
1200 that.actions[i] = this.actions[i].clone();
1201 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001202 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001203
Daniel Sandler1a497d32013-04-18 14:52:45 -04001204 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001205 that.bigContentView = this.bigContentView.clone();
1206 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001207
1208 if (!heavy) {
1209 that.lightenPayload(); // will clean out extras
1210 }
1211 }
1212
1213 /**
1214 * Removes heavyweight parts of the Notification object for archival or for sending to
1215 * listeners when the full contents are not necessary.
1216 * @hide
1217 */
1218 public final void lightenPayload() {
1219 tickerView = null;
1220 contentView = null;
1221 bigContentView = null;
1222 largeIcon = null;
1223 if (extras != null) {
1224 extras.remove(Notification.EXTRA_LARGE_ICON);
1225 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1226 extras.remove(Notification.EXTRA_PICTURE);
1227 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001228 }
1229
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001230 /**
1231 * Make sure this CharSequence is safe to put into a bundle, which basically
1232 * means it had better not be some custom Parcelable implementation.
1233 * @hide
1234 */
1235 public static CharSequence safeCharSequence(CharSequence cs) {
1236 if (cs instanceof Parcelable) {
1237 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1238 + " instance is a custom Parcelable and not allowed in Notification");
1239 return cs.toString();
1240 }
1241
1242 return cs;
1243 }
1244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 public int describeContents() {
1246 return 0;
1247 }
1248
1249 /**
1250 * Flatten this notification from a parcel.
1251 */
1252 public void writeToParcel(Parcel parcel, int flags)
1253 {
1254 parcel.writeInt(1);
1255
1256 parcel.writeLong(when);
1257 parcel.writeInt(icon);
1258 parcel.writeInt(number);
1259 if (contentIntent != null) {
1260 parcel.writeInt(1);
1261 contentIntent.writeToParcel(parcel, 0);
1262 } else {
1263 parcel.writeInt(0);
1264 }
1265 if (deleteIntent != null) {
1266 parcel.writeInt(1);
1267 deleteIntent.writeToParcel(parcel, 0);
1268 } else {
1269 parcel.writeInt(0);
1270 }
1271 if (tickerText != null) {
1272 parcel.writeInt(1);
1273 TextUtils.writeToParcel(tickerText, parcel, flags);
1274 } else {
1275 parcel.writeInt(0);
1276 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001277 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001278 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001279 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001280 } else {
1281 parcel.writeInt(0);
1282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 if (contentView != null) {
1284 parcel.writeInt(1);
1285 contentView.writeToParcel(parcel, 0);
1286 } else {
1287 parcel.writeInt(0);
1288 }
Joe Onorato561d3852010-11-20 18:09:34 -08001289 if (largeIcon != null) {
1290 parcel.writeInt(1);
1291 largeIcon.writeToParcel(parcel, 0);
1292 } else {
1293 parcel.writeInt(0);
1294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295
1296 parcel.writeInt(defaults);
1297 parcel.writeInt(this.flags);
1298
1299 if (sound != null) {
1300 parcel.writeInt(1);
1301 sound.writeToParcel(parcel, 0);
1302 } else {
1303 parcel.writeInt(0);
1304 }
1305 parcel.writeInt(audioStreamType);
1306 parcel.writeLongArray(vibrate);
1307 parcel.writeInt(ledARGB);
1308 parcel.writeInt(ledOnMS);
1309 parcel.writeInt(ledOffMS);
1310 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001311
1312 if (fullScreenIntent != null) {
1313 parcel.writeInt(1);
1314 fullScreenIntent.writeToParcel(parcel, 0);
1315 } else {
1316 parcel.writeInt(0);
1317 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001318
1319 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001320
John Spurlockcf44a122014-03-24 11:02:36 -04001321 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001322
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001323 parcel.writeString(mGroupKey);
1324
1325 parcel.writeString(mSortKey);
1326
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001327 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001328
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001329 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001330
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001331 if (bigContentView != null) {
1332 parcel.writeInt(1);
1333 bigContentView.writeToParcel(parcel, 0);
1334 } else {
1335 parcel.writeInt(0);
1336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338
1339 /**
1340 * Parcelable.Creator that instantiates Notification objects
1341 */
1342 public static final Parcelable.Creator<Notification> CREATOR
1343 = new Parcelable.Creator<Notification>()
1344 {
1345 public Notification createFromParcel(Parcel parcel)
1346 {
1347 return new Notification(parcel);
1348 }
1349
1350 public Notification[] newArray(int size)
1351 {
1352 return new Notification[size];
1353 }
1354 };
1355
1356 /**
1357 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1358 * layout.
1359 *
1360 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1361 * in the view.</p>
1362 * @param context The context for your application / activity.
1363 * @param contentTitle The title that goes in the expanded entry.
1364 * @param contentText The text that goes in the expanded entry.
1365 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1366 * If this is an activity, it must include the
1367 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001368 * that you take care of task management as described in the
1369 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1370 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001371 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001372 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001374 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 public void setLatestEventInfo(Context context,
1376 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001377 Notification.Builder builder = new Notification.Builder(context);
1378
1379 // First, ensure that key pieces of information that may have been set directly
1380 // are preserved
1381 builder.setWhen(this.when);
1382 builder.setSmallIcon(this.icon);
1383 builder.setPriority(this.priority);
1384 builder.setTicker(this.tickerText);
1385 builder.setNumber(this.number);
1386 builder.mFlags = this.flags;
1387 builder.setSound(this.sound, this.audioStreamType);
1388 builder.setDefaults(this.defaults);
1389 builder.setVibrate(this.vibrate);
1390
1391 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001393 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001396 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001398 builder.setContentIntent(contentIntent);
1399 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401
1402 @Override
1403 public String toString() {
1404 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001405 sb.append("Notification(pri=");
1406 sb.append(priority);
1407 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001408 if (contentView != null) {
1409 sb.append(contentView.getPackage());
1410 sb.append("/0x");
1411 sb.append(Integer.toHexString(contentView.getLayoutId()));
1412 } else {
1413 sb.append("null");
1414 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001415 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001416 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001417 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1418 sb.append("default");
1419 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 int N = this.vibrate.length-1;
1421 sb.append("[");
1422 for (int i=0; i<N; i++) {
1423 sb.append(this.vibrate[i]);
1424 sb.append(',');
1425 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001426 if (N != -1) {
1427 sb.append(this.vibrate[N]);
1428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 } else {
1431 sb.append("null");
1432 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001433 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001434 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001436 } else if (this.sound != null) {
1437 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 } else {
1439 sb.append("null");
1440 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001441 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001443 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001444 sb.append(Integer.toHexString(this.flags));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001445 if (this.category != null) {
1446 sb.append(" category=");
1447 sb.append(this.category);
1448 }
1449 if (this.mGroupKey != null) {
1450 sb.append(" groupKey=");
1451 sb.append(this.mGroupKey);
1452 }
1453 if (this.mSortKey != null) {
1454 sb.append(" sortKey=");
1455 sb.append(this.mSortKey);
1456 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001457 if (actions != null) {
1458 sb.append(" ");
1459 sb.append(actions.length);
1460 sb.append(" action");
1461 if (actions.length > 1) sb.append("s");
1462 }
1463 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 return sb.toString();
1465 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001466
Jeff Sharkey6d515712012-09-20 16:06:08 -07001467 /** {@hide} */
1468 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001469 if (user.getIdentifier() == UserHandle.USER_ALL) {
1470 user = UserHandle.OWNER;
1471 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001472 if (tickerView != null) {
1473 tickerView.setUser(user);
1474 }
1475 if (contentView != null) {
1476 contentView.setUser(user);
1477 }
1478 if (bigContentView != null) {
1479 bigContentView.setUser(user);
1480 }
1481 }
1482
Joe Onoratocb109a02011-01-18 17:57:41 -08001483 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001484 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001485 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001486 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001487 * content views using the platform's notification layout template. If your app supports
1488 * versions of Android as old as API level 4, you can instead use
1489 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1490 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1491 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001492 *
Scott Main183bf112012-08-13 19:12:13 -07001493 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001494 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001495 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001496 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001497 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1498 * .setContentText(subject)
1499 * .setSmallIcon(R.drawable.new_mail)
1500 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001501 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001502 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001503 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001504 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001505 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001506
Joe Onorato46439ce2010-11-19 13:56:21 -08001507 private Context mContext;
1508
1509 private long mWhen;
1510 private int mSmallIcon;
1511 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001512 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001513 private CharSequence mContentTitle;
1514 private CharSequence mContentText;
1515 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001516 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001517 private PendingIntent mContentIntent;
1518 private RemoteViews mContentView;
1519 private PendingIntent mDeleteIntent;
1520 private PendingIntent mFullScreenIntent;
1521 private CharSequence mTickerText;
1522 private RemoteViews mTickerView;
1523 private Bitmap mLargeIcon;
1524 private Uri mSound;
1525 private int mAudioStreamType;
1526 private long[] mVibrate;
1527 private int mLedArgb;
1528 private int mLedOnMs;
1529 private int mLedOffMs;
1530 private int mDefaults;
1531 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001532 private int mProgressMax;
1533 private int mProgress;
1534 private boolean mProgressIndeterminate;
John Spurlockcf44a122014-03-24 11:02:36 -04001535 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001536 private String mGroupKey;
1537 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001538 private Bundle mExtras;
1539 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001540 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001541 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001542 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001543 private boolean mShowWhen = true;
Joe Onorato46439ce2010-11-19 13:56:21 -08001544
Joe Onoratocb109a02011-01-18 17:57:41 -08001545 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001546 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001547 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001548
1549 * <table>
1550 * <tr><th align=right>priority</th>
1551 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1552 * <tr><th align=right>when</th>
1553 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1554 * <tr><th align=right>audio stream</th>
1555 * <td>{@link #STREAM_DEFAULT}</td></tr>
1556 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001557 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001558
1559 * @param context
1560 * A {@link Context} that will be used by the Builder to construct the
1561 * RemoteViews. The Context will not be held past the lifetime of this Builder
1562 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001563 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001564 public Builder(Context context) {
1565 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001566
1567 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001568 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001569 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001570 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001571 }
1572
Joe Onoratocb109a02011-01-18 17:57:41 -08001573 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001574 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001575 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001576 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001577 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001578 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001579 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001580 public Builder setWhen(long when) {
1581 mWhen = when;
1582 return this;
1583 }
1584
Joe Onoratocb109a02011-01-18 17:57:41 -08001585 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001586 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001587 * in the content view.
1588 */
1589 public Builder setShowWhen(boolean show) {
1590 mShowWhen = show;
1591 return this;
1592 }
1593
1594 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001595 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001596 *
1597 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001598 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001599 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001600 * Useful when showing an elapsed time (like an ongoing phone call).
1601 *
1602 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001603 * @see Notification#when
1604 */
1605 public Builder setUsesChronometer(boolean b) {
1606 mUseChronometer = b;
1607 return this;
1608 }
1609
1610 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001611 * Set the small icon resource, which will be used to represent the notification in the
1612 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001613 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001614
1615 * The platform template for the expanded view will draw this icon in the left, unless a
1616 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1617 * icon will be moved to the right-hand side.
1618 *
1619
1620 * @param icon
1621 * A resource ID in the application's package of the drawable to use.
1622 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001623 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001624 public Builder setSmallIcon(int icon) {
1625 mSmallIcon = icon;
1626 return this;
1627 }
1628
Joe Onoratocb109a02011-01-18 17:57:41 -08001629 /**
1630 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1631 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1632 * LevelListDrawable}.
1633 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001634 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001635 * @param level The level to use for the icon.
1636 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001637 * @see Notification#icon
1638 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001639 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001640 public Builder setSmallIcon(int icon, int level) {
1641 mSmallIcon = icon;
1642 mSmallIconLevel = level;
1643 return this;
1644 }
1645
Joe Onoratocb109a02011-01-18 17:57:41 -08001646 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001647 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001648 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001649 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001650 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001651 return this;
1652 }
1653
Joe Onoratocb109a02011-01-18 17:57:41 -08001654 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001655 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001656 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001657 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001658 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001659 return this;
1660 }
1661
Joe Onoratocb109a02011-01-18 17:57:41 -08001662 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001663 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001664 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1665 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001666 */
1667 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001668 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001669 return this;
1670 }
1671
1672 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001673 * Set the large number at the right-hand side of the notification. This is
1674 * equivalent to setContentInfo, although it might show the number in a different
1675 * font size for readability.
1676 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001677 public Builder setNumber(int number) {
1678 mNumber = number;
1679 return this;
1680 }
1681
Joe Onoratocb109a02011-01-18 17:57:41 -08001682 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001683 * A small piece of additional information pertaining to this notification.
1684 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001685 * The platform template will draw this on the last line of the notification, at the far
1686 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001687 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001688 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001689 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001690 return this;
1691 }
1692
Joe Onoratocb109a02011-01-18 17:57:41 -08001693 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001694 * Set the progress this notification represents.
1695 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001696 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001697 */
1698 public Builder setProgress(int max, int progress, boolean indeterminate) {
1699 mProgressMax = max;
1700 mProgress = progress;
1701 mProgressIndeterminate = indeterminate;
1702 return this;
1703 }
1704
1705 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001706 * Supply a custom RemoteViews to use instead of the platform template.
1707 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001708 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001709 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001710 public Builder setContent(RemoteViews views) {
1711 mContentView = views;
1712 return this;
1713 }
1714
Joe Onoratocb109a02011-01-18 17:57:41 -08001715 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001716 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1717 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001718 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1719 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1720 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001721 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001722 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001723 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001724 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001725 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001726 public Builder setContentIntent(PendingIntent intent) {
1727 mContentIntent = intent;
1728 return this;
1729 }
1730
Joe Onoratocb109a02011-01-18 17:57:41 -08001731 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001732 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1733 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001734 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001735 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001736 public Builder setDeleteIntent(PendingIntent intent) {
1737 mDeleteIntent = intent;
1738 return this;
1739 }
1740
Joe Onoratocb109a02011-01-18 17:57:41 -08001741 /**
1742 * An intent to launch instead of posting the notification to the status bar.
1743 * Only for use with extremely high-priority notifications demanding the user's
1744 * <strong>immediate</strong> attention, such as an incoming phone call or
1745 * alarm clock that the user has explicitly set to a particular time.
1746 * If this facility is used for something else, please give the user an option
1747 * to turn it off and use a normal notification, as this can be extremely
1748 * disruptive.
1749 *
1750 * @param intent The pending intent to launch.
1751 * @param highPriority Passing true will cause this notification to be sent
1752 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001753 *
1754 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001755 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001756 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1757 mFullScreenIntent = intent;
1758 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1759 return this;
1760 }
1761
Joe Onoratocb109a02011-01-18 17:57:41 -08001762 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001763 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001764 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001765 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001766 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001767 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001768 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001769 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001770 return this;
1771 }
1772
Joe Onoratocb109a02011-01-18 17:57:41 -08001773 /**
1774 * Set the text that is displayed in the status bar when the notification first
1775 * arrives, and also a RemoteViews object that may be displayed instead on some
1776 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001777 *
1778 * @see Notification#tickerText
1779 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001780 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001781 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001782 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001783 mTickerView = views;
1784 return this;
1785 }
1786
Joe Onoratocb109a02011-01-18 17:57:41 -08001787 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001788 * Add a large icon to the notification (and the ticker on some devices).
1789 *
1790 * In the platform template, this image will be shown on the left of the notification view
1791 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1792 *
1793 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001794 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001795 public Builder setLargeIcon(Bitmap icon) {
1796 mLargeIcon = icon;
1797 return this;
1798 }
1799
Joe Onoratocb109a02011-01-18 17:57:41 -08001800 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001801 * Set the sound to play.
1802 *
1803 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1804 *
1805 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001806 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001807 public Builder setSound(Uri sound) {
1808 mSound = sound;
1809 mAudioStreamType = STREAM_DEFAULT;
1810 return this;
1811 }
1812
Joe Onoratocb109a02011-01-18 17:57:41 -08001813 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001814 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001815 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001816 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1817 *
1818 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001819 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001820 public Builder setSound(Uri sound, int streamType) {
1821 mSound = sound;
1822 mAudioStreamType = streamType;
1823 return this;
1824 }
1825
Joe Onoratocb109a02011-01-18 17:57:41 -08001826 /**
1827 * Set the vibration pattern to use.
1828 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001829
1830 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1831 * <code>pattern</code> parameter.
1832 *
1833
1834 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001835 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001836 public Builder setVibrate(long[] pattern) {
1837 mVibrate = pattern;
1838 return this;
1839 }
1840
Joe Onoratocb109a02011-01-18 17:57:41 -08001841 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001842 * Set the desired color for the indicator LED on the device, as well as the
1843 * blink duty cycle (specified in milliseconds).
1844 *
1845
1846 * Not all devices will honor all (or even any) of these values.
1847 *
1848
1849 * @see Notification#ledARGB
1850 * @see Notification#ledOnMS
1851 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001852 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001853 public Builder setLights(int argb, int onMs, int offMs) {
1854 mLedArgb = argb;
1855 mLedOnMs = onMs;
1856 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001857 return this;
1858 }
1859
Joe Onoratocb109a02011-01-18 17:57:41 -08001860 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001861 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001862 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001863
1864 * Ongoing notifications cannot be dismissed by the user, so your application or service
1865 * must take care of canceling them.
1866 *
1867
1868 * They are typically used to indicate a background task that the user is actively engaged
1869 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1870 * (e.g., a file download, sync operation, active network connection).
1871 *
1872
1873 * @see Notification#FLAG_ONGOING_EVENT
1874 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001875 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001876 public Builder setOngoing(boolean ongoing) {
1877 setFlag(FLAG_ONGOING_EVENT, ongoing);
1878 return this;
1879 }
1880
Joe Onoratocb109a02011-01-18 17:57:41 -08001881 /**
1882 * Set this flag if you would only like the sound, vibrate
1883 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001884 *
1885 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001886 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001887 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1888 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1889 return this;
1890 }
1891
Joe Onoratocb109a02011-01-18 17:57:41 -08001892 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001893 * Make this notification automatically dismissed when the user touches it. The
1894 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1895 *
1896 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001897 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001898 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001899 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001900 return this;
1901 }
1902
Joe Onoratocb109a02011-01-18 17:57:41 -08001903 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08001904 * Set whether or not this notification should not bridge to other devices.
1905 *
1906 * <p>Some notifications can be bridged to other devices for remote display.
1907 * This hint can be set to recommend this notification not be bridged.
1908 */
1909 public Builder setLocalOnly(boolean localOnly) {
1910 setFlag(FLAG_LOCAL_ONLY, localOnly);
1911 return this;
1912 }
1913
1914 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001915 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001916 * <p>
1917 * The value should be one or more of the following fields combined with
1918 * bitwise-or:
1919 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1920 * <p>
1921 * For all default values, use {@link #DEFAULT_ALL}.
1922 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001923 public Builder setDefaults(int defaults) {
1924 mDefaults = defaults;
1925 return this;
1926 }
1927
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001928 /**
1929 * Set the priority of this notification.
1930 *
1931 * @see Notification#priority
1932 */
1933 public Builder setPriority(int pri) {
1934 mPriority = pri;
1935 return this;
1936 }
Joe Malin8d40d042012-11-05 11:36:40 -08001937
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001938 /**
John Spurlockcf44a122014-03-24 11:02:36 -04001939 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08001940 *
John Spurlockcf44a122014-03-24 11:02:36 -04001941 * @see Notification#category
Griff Hazened0c87e2014-05-05 15:15:12 -07001942 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001943 */
John Spurlockcf44a122014-03-24 11:02:36 -04001944 public Builder setCategory(String category) {
1945 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001946 return this;
1947 }
1948
1949 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001950 * Set this notification to be part of a group of notifications sharing the same key.
1951 * Grouped notifications may display in a cluster or stack on devices which
1952 * support such rendering.
1953 *
1954 * <p>To make this notification the summary for its group, also call
1955 * {@link #setGroupSummary}. A sort order can be specified for group members by using
1956 * {@link #setSortKey}.
1957 * @param groupKey The group key of the group.
1958 * @return this object for method chaining
1959 */
1960 public Builder setGroup(String groupKey) {
1961 mGroupKey = groupKey;
1962 return this;
1963 }
1964
1965 /**
1966 * Set this notification to be the group summary for a group of notifications.
1967 * Grouped notifications may display in a cluster or stack on devices which
1968 * support such rendering. Requires a group key also be set using {@link #setGroup}.
1969 * @param isGroupSummary Whether this notification should be a group summary.
1970 * @return this object for method chaining
1971 */
1972 public Builder setGroupSummary(boolean isGroupSummary) {
1973 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
1974 return this;
1975 }
1976
1977 /**
1978 * Set a sort key that orders this notification among other notifications from the
1979 * same package. This can be useful if an external sort was already applied and an app
1980 * would like to preserve this. Notifications will be sorted lexicographically using this
1981 * value, although providing different priorities in addition to providing sort key may
1982 * cause this value to be ignored.
1983 *
1984 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07001985 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001986 *
1987 * @see String#compareTo(String)
1988 */
1989 public Builder setSortKey(String sortKey) {
1990 mSortKey = sortKey;
1991 return this;
1992 }
1993
1994 /**
Griff Hazen720042b2014-02-24 15:46:56 -08001995 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001996 *
Griff Hazen720042b2014-02-24 15:46:56 -08001997 * <p>Values within the Bundle will replace existing extras values in this Builder.
1998 *
1999 * @see Notification#extras
2000 */
Griff Hazen959591e2014-05-15 22:26:18 -07002001 public Builder addExtras(Bundle extras) {
2002 if (extras != null) {
2003 if (mExtras == null) {
2004 mExtras = new Bundle(extras);
2005 } else {
2006 mExtras.putAll(extras);
2007 }
Griff Hazen720042b2014-02-24 15:46:56 -08002008 }
2009 return this;
2010 }
2011
2012 /**
2013 * Set metadata for this notification.
2014 *
2015 * <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 -04002016 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002017 * called.
2018 *
Griff Hazen720042b2014-02-24 15:46:56 -08002019 * <p>Replaces any existing extras values with those from the provided Bundle.
2020 * Use {@link #addExtras} to merge in metadata instead.
2021 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002022 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002023 */
Griff Hazen959591e2014-05-15 22:26:18 -07002024 public Builder setExtras(Bundle extras) {
2025 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002026 return this;
2027 }
2028
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002029 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002030 * Get the current metadata Bundle used by this notification Builder.
2031 *
2032 * <p>The returned Bundle is shared with this Builder.
2033 *
2034 * <p>The current contents of this Bundle are copied into the Notification each time
2035 * {@link #build()} is called.
2036 *
2037 * @see Notification#extras
2038 */
2039 public Bundle getExtras() {
2040 if (mExtras == null) {
2041 mExtras = new Bundle();
2042 }
2043 return mExtras;
2044 }
2045
2046 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002047 * Add an action to this notification. Actions are typically displayed by
2048 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002049 * <p>
2050 * Every action must have an icon (32dp square and matching the
2051 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2052 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2053 * <p>
2054 * A notification in its expanded form can display up to 3 actions, from left to right in
2055 * the order they were added. Actions will not be displayed when the notification is
2056 * collapsed, however, so be sure that any essential functions may be accessed by the user
2057 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002058 *
2059 * @param icon Resource ID of a drawable that represents the action.
2060 * @param title Text describing the action.
2061 * @param intent PendingIntent to be fired when the action is invoked.
2062 */
2063 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002064 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002065 return this;
2066 }
2067
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002068 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002069 * Add an action to this notification. Actions are typically displayed by
2070 * the system as a button adjacent to the notification content.
2071 * <p>
2072 * Every action must have an icon (32dp square and matching the
2073 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2074 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2075 * <p>
2076 * A notification in its expanded form can display up to 3 actions, from left to right in
2077 * the order they were added. Actions will not be displayed when the notification is
2078 * collapsed, however, so be sure that any essential functions may be accessed by the user
2079 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2080 *
2081 * @param action The action to add.
2082 */
2083 public Builder addAction(Action action) {
2084 mActions.add(action);
2085 return this;
2086 }
2087
2088 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002089 * Add a rich notification style to be applied at build time.
2090 *
2091 * @param style Object responsible for modifying the notification style.
2092 */
2093 public Builder setStyle(Style style) {
2094 if (mStyle != style) {
2095 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002096 if (mStyle != null) {
2097 mStyle.setBuilder(this);
2098 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002099 }
2100 return this;
2101 }
2102
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002103 /**
2104 * Apply an extender to this notification builder. Extenders may be used to add
2105 * metadata or change options on this builder.
2106 */
Griff Hazenc3104152014-05-22 14:38:36 -07002107 public Builder extend(Extender extender) {
2108 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002109 return this;
2110 }
2111
Joe Onorato46439ce2010-11-19 13:56:21 -08002112 private void setFlag(int mask, boolean value) {
2113 if (value) {
2114 mFlags |= mask;
2115 } else {
2116 mFlags &= ~mask;
2117 }
2118 }
2119
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002120 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08002121 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002122 boolean showLine3 = false;
2123 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002124 int smallIconImageViewId = R.id.icon;
2125 if (mLargeIcon != null) {
2126 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
2127 smallIconImageViewId = R.id.right_icon;
2128 }
Daniel Sandlere95658c2012-05-10 00:33:54 -04002129 if (mPriority < PRIORITY_LOW) {
2130 contentView.setInt(R.id.icon,
2131 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
2132 contentView.setInt(R.id.status_bar_latest_event_content,
2133 "setBackgroundResource", R.drawable.notification_bg_low);
2134 }
Joe Onorato561d3852010-11-20 18:09:34 -08002135 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002136 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
2137 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002138 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002139 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002140 }
2141 if (mContentTitle != null) {
2142 contentView.setTextViewText(R.id.title, mContentTitle);
2143 }
2144 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002145 contentView.setTextViewText(R.id.text, mContentText);
2146 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002147 }
2148 if (mContentInfo != null) {
2149 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002150 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002151 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002152 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002153 final int tooBig = mContext.getResources().getInteger(
2154 R.integer.status_bar_notification_info_maxnum);
2155 if (mNumber > tooBig) {
2156 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
2157 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08002158 } else {
2159 NumberFormat f = NumberFormat.getIntegerInstance();
2160 contentView.setTextViewText(R.id.info, f.format(mNumber));
2161 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002162 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002163 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002164 } else {
2165 contentView.setViewVisibility(R.id.info, View.GONE);
2166 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002167
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002168 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002169 if (mSubText != null) {
2170 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002171 if (mContentText != null) {
2172 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002173 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2174 showLine2 = true;
2175 } else {
2176 contentView.setViewVisibility(R.id.text2, View.GONE);
2177 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002178 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002179 contentView.setViewVisibility(R.id.text2, View.GONE);
2180 if (mProgressMax != 0 || mProgressIndeterminate) {
2181 contentView.setProgressBar(
2182 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2183 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002184 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002185 } else {
2186 contentView.setViewVisibility(R.id.progress, View.GONE);
2187 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002188 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002189 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002190 if (fitIn1U) {
2191 // need to shrink all the type to make sure everything fits
2192 final Resources res = mContext.getResources();
2193 final float subTextSize = res.getDimensionPixelSize(
2194 R.dimen.notification_subtext_size);
2195 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2196 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002197 // vertical centering
2198 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2199 }
2200
Daniel Sandler0c890492012-09-12 17:23:10 -07002201 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002202 if (mUseChronometer) {
2203 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2204 contentView.setLong(R.id.chronometer, "setBase",
2205 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2206 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2207 } else {
2208 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2209 contentView.setLong(R.id.time, "setTime", mWhen);
2210 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002211 } else {
2212 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002213 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002214
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002215 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002216 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002217 return contentView;
2218 }
2219
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002220 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002221 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002222
2223 int N = mActions.size();
2224 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04002225 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002226 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002227 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002228 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002229 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002230 for (int i=0; i<N; i++) {
2231 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04002232 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002233 big.addView(R.id.actions, button);
2234 }
2235 }
2236 return big;
2237 }
2238
Joe Onorato46439ce2010-11-19 13:56:21 -08002239 private RemoteViews makeContentView() {
2240 if (mContentView != null) {
2241 return mContentView;
2242 } else {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002243 return applyStandardTemplate(R.layout.notification_template_base, true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002244 }
2245 }
2246
2247 private RemoteViews makeTickerView() {
2248 if (mTickerView != null) {
2249 return mTickerView;
2250 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08002251 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002252 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08002253 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002254 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08002255 } else {
2256 return null;
2257 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002258 }
2259 }
2260
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002261 private RemoteViews makeBigContentView() {
2262 if (mActions.size() == 0) return null;
2263
Chris Wrenb023bf82012-04-23 16:05:42 -04002264 return applyStandardTemplateWithActions(R.layout.notification_template_big_base);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002265 }
2266
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002267 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002268 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002269 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Daniel Sandler8680bf82012-05-15 16:52:52 -04002270 tombstone ? R.layout.notification_action_tombstone
2271 : R.layout.notification_action);
Chris Wrenbe63a952013-12-03 14:31:01 -05002272 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002273 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002274 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002275 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002276 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002277 button.setContentDescription(R.id.action0, action.title);
2278 return button;
2279 }
2280
Joe Onoratocb109a02011-01-18 17:57:41 -08002281 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002282 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002283 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002284 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002285 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002286 Notification n = new Notification();
2287 n.when = mWhen;
2288 n.icon = mSmallIcon;
2289 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002290 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08002291 n.contentView = makeContentView();
2292 n.contentIntent = mContentIntent;
2293 n.deleteIntent = mDeleteIntent;
2294 n.fullScreenIntent = mFullScreenIntent;
2295 n.tickerText = mTickerText;
2296 n.tickerView = makeTickerView();
2297 n.largeIcon = mLargeIcon;
2298 n.sound = mSound;
2299 n.audioStreamType = mAudioStreamType;
2300 n.vibrate = mVibrate;
2301 n.ledARGB = mLedArgb;
2302 n.ledOnMS = mLedOnMs;
2303 n.ledOffMS = mLedOffMs;
2304 n.defaults = mDefaults;
2305 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002306 n.bigContentView = makeBigContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002307 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002308 n.flags |= FLAG_SHOW_LIGHTS;
2309 }
2310 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2311 n.flags |= FLAG_SHOW_LIGHTS;
2312 }
John Spurlockcf44a122014-03-24 11:02:36 -04002313 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002314 n.mGroupKey = mGroupKey;
2315 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002316 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002317 if (mActions.size() > 0) {
2318 n.actions = new Action[mActions.size()];
2319 mActions.toArray(n.actions);
2320 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002321 return n;
2322 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002323
2324 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002325 * Capture, in the provided bundle, semantic information used in the construction of
2326 * this Notification object.
2327 * @hide
2328 */
Griff Hazen720042b2014-02-24 15:46:56 -08002329 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002330 // Store original information used in the construction of this object
2331 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2332 extras.putCharSequence(EXTRA_TEXT, mContentText);
2333 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2334 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2335 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2336 extras.putInt(EXTRA_PROGRESS, mProgress);
2337 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2338 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2339 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2340 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002341 if (mLargeIcon != null) {
2342 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2343 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002344 }
2345
2346 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002347 * @deprecated Use {@link #build()} instead.
2348 */
2349 @Deprecated
2350 public Notification getNotification() {
2351 return build();
2352 }
2353
2354 /**
2355 * Combine all of the options that have been set and return a new {@link Notification}
2356 * object.
2357 */
2358 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002359 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002360
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002361 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002362 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002363 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002364
2365 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2366
Griff Hazen720042b2014-02-24 15:46:56 -08002367 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002368 if (mStyle != null) {
2369 mStyle.addExtras(n.extras);
2370 }
2371
2372 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002373 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002374
2375 /**
2376 * Apply this Builder to an existing {@link Notification} object.
2377 *
2378 * @hide
2379 */
2380 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002381 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002382 return n;
2383 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002384 }
2385
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002386 /**
2387 * An object that can apply a rich notification style to a {@link Notification.Builder}
2388 * object.
2389 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002390 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002391 private CharSequence mBigContentTitle;
2392 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002393 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002394
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002395 protected Builder mBuilder;
2396
Chris Wrend6297db2012-05-03 16:20:13 -04002397 /**
2398 * Overrides ContentTitle in the big form of the template.
2399 * This defaults to the value passed to setContentTitle().
2400 */
2401 protected void internalSetBigContentTitle(CharSequence title) {
2402 mBigContentTitle = title;
2403 }
2404
2405 /**
2406 * Set the first line of text after the detail section in the big form of the template.
2407 */
2408 protected void internalSetSummaryText(CharSequence cs) {
2409 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002410 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002411 }
2412
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002413 public void setBuilder(Builder builder) {
2414 if (mBuilder != builder) {
2415 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002416 if (mBuilder != null) {
2417 mBuilder.setStyle(this);
2418 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002419 }
2420 }
2421
Chris Wrend6297db2012-05-03 16:20:13 -04002422 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002423 if (mBuilder == null) {
2424 throw new IllegalArgumentException("Style requires a valid Builder object");
2425 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002426 }
Chris Wrend6297db2012-05-03 16:20:13 -04002427
2428 protected RemoteViews getStandardView(int layoutId) {
2429 checkBuilder();
2430
2431 if (mBigContentTitle != null) {
2432 mBuilder.setContentTitle(mBigContentTitle);
2433 }
2434
Chris Wrend6297db2012-05-03 16:20:13 -04002435 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2436
Chris Wrend6297db2012-05-03 16:20:13 -04002437 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2438 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002439 } else {
2440 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002441 }
2442
Daniel Sandler619738c2012-06-07 16:33:08 -04002443 // The last line defaults to the subtext, but can be replaced by mSummaryText
2444 final CharSequence overflowText =
2445 mSummaryTextSet ? mSummaryText
2446 : mBuilder.mSubText;
2447 if (overflowText != null) {
2448 contentView.setTextViewText(R.id.text, overflowText);
2449 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002450 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002451 } else {
2452 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2453 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002454 }
2455
2456 return contentView;
2457 }
2458
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002459 /**
2460 * @hide
2461 */
2462 public void addExtras(Bundle extras) {
2463 if (mSummaryTextSet) {
2464 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2465 }
2466 if (mBigContentTitle != null) {
2467 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2468 }
2469 }
2470
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002471 /**
2472 * @hide
2473 */
2474 public abstract Notification buildStyled(Notification wip);
2475
2476 /**
2477 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2478 * attached to.
2479 *
2480 * @return the fully constructed Notification.
2481 */
2482 public Notification build() {
2483 checkBuilder();
2484 return mBuilder.build();
2485 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002486 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002487
2488 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002489 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002490 *
Robert Ly91c5ce32014-06-08 15:37:00 -07002491 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002492 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07002493 * Notification notif = new Notification.Builder(mContext)
2494 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
2495 * .setContentText(subject)
2496 * .setSmallIcon(R.drawable.new_post)
2497 * .setLargeIcon(aBitmap)
2498 * .setStyle(new Notification.BigPictureStyle()
2499 * .bigPicture(aBigBitmap))
2500 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002501 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002502 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002503 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002504 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002505 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002506 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002507 private Bitmap mBigLargeIcon;
2508 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002509
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002510 public BigPictureStyle() {
2511 }
2512
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002513 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002514 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002515 }
2516
Chris Wrend6297db2012-05-03 16:20:13 -04002517 /**
2518 * Overrides ContentTitle in the big form of the template.
2519 * This defaults to the value passed to setContentTitle().
2520 */
2521 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002522 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002523 return this;
2524 }
2525
2526 /**
2527 * Set the first line of text after the detail section in the big form of the template.
2528 */
2529 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002530 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002531 return this;
2532 }
2533
Chris Wren0bd664d2012-08-01 13:56:56 -04002534 /**
2535 * Provide the bitmap to be used as the payload for the BigPicture notification.
2536 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002537 public BigPictureStyle bigPicture(Bitmap b) {
2538 mPicture = b;
2539 return this;
2540 }
2541
Chris Wren3745a3d2012-05-22 15:11:52 -04002542 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002543 * Override the large icon when the big notification is shown.
2544 */
2545 public BigPictureStyle bigLargeIcon(Bitmap b) {
2546 mBigLargeIconSet = true;
2547 mBigLargeIcon = b;
2548 return this;
2549 }
2550
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002551 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04002552 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002553
2554 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2555
2556 return contentView;
2557 }
2558
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002559 /**
2560 * @hide
2561 */
2562 public void addExtras(Bundle extras) {
2563 super.addExtras(extras);
2564
2565 if (mBigLargeIconSet) {
2566 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2567 }
2568 extras.putParcelable(EXTRA_PICTURE, mPicture);
2569 }
2570
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002571 /**
2572 * @hide
2573 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002574 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002575 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002576 if (mBigLargeIconSet ) {
2577 mBuilder.mLargeIcon = mBigLargeIcon;
2578 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002579 wip.bigContentView = makeBigContentView();
2580 return wip;
2581 }
2582 }
2583
2584 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002585 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002586 *
Robert Ly91c5ce32014-06-08 15:37:00 -07002587 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002588 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07002589 * Notification notif = new Notification.Builder(mContext)
2590 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2591 * .setContentText(subject)
2592 * .setSmallIcon(R.drawable.new_mail)
2593 * .setLargeIcon(aBitmap)
2594 * .setStyle(new Notification.BigTextStyle()
2595 * .bigText(aVeryLongString))
2596 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002597 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002598 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002599 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002600 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002601 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002602 private CharSequence mBigText;
2603
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002604 public BigTextStyle() {
2605 }
2606
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002607 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002608 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002609 }
2610
Chris Wrend6297db2012-05-03 16:20:13 -04002611 /**
2612 * Overrides ContentTitle in the big form of the template.
2613 * This defaults to the value passed to setContentTitle().
2614 */
2615 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002616 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002617 return this;
2618 }
2619
2620 /**
2621 * Set the first line of text after the detail section in the big form of the template.
2622 */
2623 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002624 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002625 return this;
2626 }
2627
Chris Wren0bd664d2012-08-01 13:56:56 -04002628 /**
2629 * Provide the longer text to be displayed in the big form of the
2630 * template in place of the content text.
2631 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002632 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002633 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002634 return this;
2635 }
2636
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002637 /**
2638 * @hide
2639 */
2640 public void addExtras(Bundle extras) {
2641 super.addExtras(extras);
2642
2643 extras.putCharSequence(EXTRA_TEXT, mBigText);
2644 }
2645
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002646 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002647 // Remove the content text so line3 only shows if you have a summary
2648 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002649 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04002650
Chris Wrend6297db2012-05-03 16:20:13 -04002651 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text);
Joe Malin8d40d042012-11-05 11:36:40 -08002652
Daniel Sandler619738c2012-06-07 16:33:08 -04002653 if (hadThreeLines) {
2654 // vertical centering
2655 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2656 }
2657
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002658 contentView.setTextViewText(R.id.big_text, mBigText);
2659 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04002660 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002661
2662 return contentView;
2663 }
2664
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002665 /**
2666 * @hide
2667 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002668 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002669 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002670 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002671
2672 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
2673
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002674 return wip;
2675 }
2676 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04002677
2678 /**
2679 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08002680 *
Robert Ly91c5ce32014-06-08 15:37:00 -07002681 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04002682 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07002683 * Notification notif = new Notification.Builder(mContext)
2684 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
2685 * .setContentText(subject)
2686 * .setSmallIcon(R.drawable.new_mail)
2687 * .setLargeIcon(aBitmap)
2688 * .setStyle(new Notification.InboxStyle()
2689 * .addLine(str1)
2690 * .addLine(str2)
2691 * .setContentTitle(&quot;&quot;)
2692 * .setSummaryText(&quot;+3 more&quot;))
2693 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04002694 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002695 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002696 * @see Notification#bigContentView
2697 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002698 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002699 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
2700
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002701 public InboxStyle() {
2702 }
2703
Daniel Sandler879c5e02012-04-17 16:46:51 -04002704 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002705 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002706 }
2707
Chris Wrend6297db2012-05-03 16:20:13 -04002708 /**
2709 * Overrides ContentTitle in the big form of the template.
2710 * This defaults to the value passed to setContentTitle().
2711 */
2712 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002713 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002714 return this;
2715 }
2716
2717 /**
2718 * Set the first line of text after the detail section in the big form of the template.
2719 */
2720 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002721 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002722 return this;
2723 }
2724
Chris Wren0bd664d2012-08-01 13:56:56 -04002725 /**
2726 * Append a line to the digest section of the Inbox notification.
2727 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04002728 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002729 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04002730 return this;
2731 }
2732
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002733 /**
2734 * @hide
2735 */
2736 public void addExtras(Bundle extras) {
2737 super.addExtras(extras);
2738 CharSequence[] a = new CharSequence[mTexts.size()];
2739 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
2740 }
2741
Daniel Sandler879c5e02012-04-17 16:46:51 -04002742 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002743 // Remove the content text so line3 disappears unless you have a summary
2744 mBuilder.mContentText = null;
Chris Wrend6297db2012-05-03 16:20:13 -04002745 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox);
Daniel Sandler619738c2012-06-07 16:33:08 -04002746
Chris Wrend6297db2012-05-03 16:20:13 -04002747 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002748
Chris Wrend6297db2012-05-03 16:20:13 -04002749 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 -04002750 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04002751
Chris Wren4ed80d52012-05-17 09:30:03 -04002752 // Make sure all rows are gone in case we reuse a view.
2753 for (int rowId : rowIds) {
2754 contentView.setViewVisibility(rowId, View.GONE);
2755 }
2756
Chris Wren683ab002012-09-20 10:35:54 -04002757
Daniel Sandler879c5e02012-04-17 16:46:51 -04002758 int i=0;
2759 while (i < mTexts.size() && i < rowIds.length) {
2760 CharSequence str = mTexts.get(i);
2761 if (str != null && !str.equals("")) {
2762 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
2763 contentView.setTextViewText(rowIds[i], str);
2764 }
2765 i++;
2766 }
2767
Chris Wren683ab002012-09-20 10:35:54 -04002768 contentView.setViewVisibility(R.id.inbox_end_pad,
2769 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
2770
2771 contentView.setViewVisibility(R.id.inbox_more,
2772 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04002773
Daniel Sandler879c5e02012-04-17 16:46:51 -04002774 return contentView;
2775 }
2776
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002777 /**
2778 * @hide
2779 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002780 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002781 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002782 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002783
Daniel Sandler879c5e02012-04-17 16:46:51 -04002784 return wip;
2785 }
2786 }
Griff Hazenc3104152014-05-22 14:38:36 -07002787
2788 /**
2789 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
2790 * metadata or change options on a notification builder.
2791 */
2792 public interface Extender {
2793 /**
2794 * Apply this extender to a notification builder.
2795 * @param builder the builder to be modified.
2796 * @return the build object for chaining.
2797 */
2798 public Builder extend(Builder builder);
2799 }
2800
2801 /**
2802 * Helper class to add wearable extensions to notifications.
2803 * <p class="note"> See
2804 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
2805 * for Android Wear</a> for more information on how to use this class.
2806 * <p>
2807 * To create a notification with wearable extensions:
2808 * <ol>
2809 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
2810 * properties.
2811 * <li>Create a {@link android.app.Notification.WearableExtender}.
2812 * <li>Set wearable-specific properties using the
2813 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
2814 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
2815 * notification.
2816 * <li>Post the notification to the notification system with the
2817 * {@code NotificationManager.notify(...)} methods.
2818 * </ol>
2819 *
2820 * <pre class="prettyprint">
2821 * Notification notif = new Notification.Builder(mContext)
2822 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2823 * .setContentText(subject)
2824 * .setSmallIcon(R.drawable.new_mail)
2825 * .extend(new Notification.WearableExtender()
2826 * .setContentIcon(R.drawable.new_mail))
2827 * .build();
2828 * NotificationManager notificationManger =
2829 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2830 * notificationManger.notify(0, notif);</pre>
2831 *
2832 * <p>Wearable extensions can be accessed on an existing notification by using the
2833 * {@code WearableExtender(Notification)} constructor,
2834 * and then using the {@code get} methods to access values.
2835 *
2836 * <pre class="prettyprint">
2837 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
2838 * notification);
Griff Hazen6f72ac52014-05-26 09:07:14 -07002839 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazenc3104152014-05-22 14:38:36 -07002840 */
2841 public static final class WearableExtender implements Extender {
2842 /**
2843 * Sentinel value for an action index that is unset.
2844 */
2845 public static final int UNSET_ACTION_INDEX = -1;
2846
2847 /**
2848 * Size value for use with {@link #setCustomSizePreset} to show this notification with
2849 * default sizing.
2850 * <p>For custom display notifications created using {@link #setDisplayIntent},
2851 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
2852 * on their content.
2853 */
2854 public static final int SIZE_DEFAULT = 0;
2855
2856 /**
2857 * Size value for use with {@link #setCustomSizePreset} to show this notification
2858 * with an extra small size.
2859 * <p>This value is only applicable for custom display notifications created using
2860 * {@link #setDisplayIntent}.
2861 */
2862 public static final int SIZE_XSMALL = 1;
2863
2864 /**
2865 * Size value for use with {@link #setCustomSizePreset} to show this notification
2866 * with a small size.
2867 * <p>This value is only applicable for custom display notifications created using
2868 * {@link #setDisplayIntent}.
2869 */
2870 public static final int SIZE_SMALL = 2;
2871
2872 /**
2873 * Size value for use with {@link #setCustomSizePreset} to show this notification
2874 * with a medium size.
2875 * <p>This value is only applicable for custom display notifications created using
2876 * {@link #setDisplayIntent}.
2877 */
2878 public static final int SIZE_MEDIUM = 3;
2879
2880 /**
2881 * Size value for use with {@link #setCustomSizePreset} to show this notification
2882 * with a large size.
2883 * <p>This value is only applicable for custom display notifications created using
2884 * {@link #setDisplayIntent}.
2885 */
2886 public static final int SIZE_LARGE = 4;
2887
Griff Hazena9cb1e62014-05-27 15:40:09 -07002888 /**
2889 * Size value for use with {@link #setCustomSizePreset} to show this notification
2890 * full screen.
2891 * <p>This value is only applicable for custom display notifications created using
2892 * {@link #setDisplayIntent}.
2893 */
2894 public static final int SIZE_FULL_SCREEN = 5;
2895
Griff Hazenc3104152014-05-22 14:38:36 -07002896 /** Notification extra which contains wearable extensions */
2897 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
2898
2899 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
2900 private static final String KEY_ACTIONS = "actions";
2901 private static final String KEY_FLAGS = "flags";
2902 private static final String KEY_DISPLAY_INTENT = "displayIntent";
2903 private static final String KEY_PAGES = "pages";
2904 private static final String KEY_BACKGROUND = "background";
2905 private static final String KEY_CONTENT_ICON = "contentIcon";
2906 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
2907 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
2908 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
2909 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
2910 private static final String KEY_GRAVITY = "gravity";
2911
2912 // Flags bitwise-ored to mFlags
2913 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
2914 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
2915 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
2916 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
2917
2918 // Default value for flags integer
2919 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
2920
2921 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
2922 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
2923
2924 private ArrayList<Action> mActions = new ArrayList<Action>();
2925 private int mFlags = DEFAULT_FLAGS;
2926 private PendingIntent mDisplayIntent;
2927 private ArrayList<Notification> mPages = new ArrayList<Notification>();
2928 private Bitmap mBackground;
2929 private int mContentIcon;
2930 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
2931 private int mContentActionIndex = UNSET_ACTION_INDEX;
2932 private int mCustomSizePreset = SIZE_DEFAULT;
2933 private int mCustomContentHeight;
2934 private int mGravity = DEFAULT_GRAVITY;
2935
2936 /**
2937 * Create a {@link android.app.Notification.WearableExtender} with default
2938 * options.
2939 */
2940 public WearableExtender() {
2941 }
2942
2943 public WearableExtender(Notification notif) {
2944 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
2945 if (wearableBundle != null) {
2946 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
2947 if (actions != null) {
2948 mActions.addAll(actions);
2949 }
2950
2951 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
2952 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
2953
2954 Notification[] pages = getNotificationArrayFromBundle(
2955 wearableBundle, KEY_PAGES);
2956 if (pages != null) {
2957 Collections.addAll(mPages, pages);
2958 }
2959
2960 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
2961 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
2962 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
2963 DEFAULT_CONTENT_ICON_GRAVITY);
2964 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
2965 UNSET_ACTION_INDEX);
2966 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
2967 SIZE_DEFAULT);
2968 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
2969 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
2970 }
2971 }
2972
2973 /**
2974 * Apply wearable extensions to a notification that is being built. This is typically
2975 * called by the {@link android.app.Notification.Builder#extend} method of
2976 * {@link android.app.Notification.Builder}.
2977 */
2978 @Override
2979 public Notification.Builder extend(Notification.Builder builder) {
2980 Bundle wearableBundle = new Bundle();
2981
2982 if (!mActions.isEmpty()) {
2983 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
2984 }
2985 if (mFlags != DEFAULT_FLAGS) {
2986 wearableBundle.putInt(KEY_FLAGS, mFlags);
2987 }
2988 if (mDisplayIntent != null) {
2989 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
2990 }
2991 if (!mPages.isEmpty()) {
2992 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
2993 new Notification[mPages.size()]));
2994 }
2995 if (mBackground != null) {
2996 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
2997 }
2998 if (mContentIcon != 0) {
2999 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
3000 }
3001 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
3002 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
3003 }
3004 if (mContentActionIndex != UNSET_ACTION_INDEX) {
3005 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
3006 mContentActionIndex);
3007 }
3008 if (mCustomSizePreset != SIZE_DEFAULT) {
3009 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
3010 }
3011 if (mCustomContentHeight != 0) {
3012 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
3013 }
3014 if (mGravity != DEFAULT_GRAVITY) {
3015 wearableBundle.putInt(KEY_GRAVITY, mGravity);
3016 }
3017
3018 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
3019 return builder;
3020 }
3021
3022 @Override
3023 public WearableExtender clone() {
3024 WearableExtender that = new WearableExtender();
3025 that.mActions = new ArrayList<Action>(this.mActions);
3026 that.mFlags = this.mFlags;
3027 that.mDisplayIntent = this.mDisplayIntent;
3028 that.mPages = new ArrayList<Notification>(this.mPages);
3029 that.mBackground = this.mBackground;
3030 that.mContentIcon = this.mContentIcon;
3031 that.mContentIconGravity = this.mContentIconGravity;
3032 that.mContentActionIndex = this.mContentActionIndex;
3033 that.mCustomSizePreset = this.mCustomSizePreset;
3034 that.mCustomContentHeight = this.mCustomContentHeight;
3035 that.mGravity = this.mGravity;
3036 return that;
3037 }
3038
3039 /**
3040 * Add a wearable action to this notification.
3041 *
3042 * <p>When wearable actions are added using this method, the set of actions that
3043 * show on a wearable device splits from devices that only show actions added
3044 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3045 * of which actions display on different devices.
3046 *
3047 * @param action the action to add to this notification
3048 * @return this object for method chaining
3049 * @see android.app.Notification.Action
3050 */
3051 public WearableExtender addAction(Action action) {
3052 mActions.add(action);
3053 return this;
3054 }
3055
3056 /**
3057 * Adds wearable actions to this notification.
3058 *
3059 * <p>When wearable actions are added using this method, the set of actions that
3060 * show on a wearable device splits from devices that only show actions added
3061 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3062 * of which actions display on different devices.
3063 *
3064 * @param actions the actions to add to this notification
3065 * @return this object for method chaining
3066 * @see android.app.Notification.Action
3067 */
3068 public WearableExtender addActions(List<Action> actions) {
3069 mActions.addAll(actions);
3070 return this;
3071 }
3072
3073 /**
3074 * Clear all wearable actions present on this builder.
3075 * @return this object for method chaining.
3076 * @see #addAction
3077 */
3078 public WearableExtender clearActions() {
3079 mActions.clear();
3080 return this;
3081 }
3082
3083 /**
3084 * Get the wearable actions present on this notification.
3085 */
3086 public List<Action> getActions() {
3087 return mActions;
3088 }
3089
3090 /**
3091 * Set an intent to launch inside of an activity view when displaying
Griff Hazen6f72ac52014-05-26 09:07:14 -07003092 * this notification. The {@link PendingIntent} provided should be for an activity.
3093 *
3094 * <pre class="prettyprint">
3095 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
3096 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
3097 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3098 * Notification notif = new Notification.Builder(context)
3099 * .extend(new Notification.WearableExtender()
3100 * .setDisplayIntent(displayPendingIntent)
3101 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
3102 * .build();</pre>
3103 *
3104 * <p>The activity to launch needs to allow embedding, must be exported, and
3105 * should have an empty task affinity.
3106 *
3107 * <p>Example AndroidManifest.xml entry:
3108 * <pre class="prettyprint">
3109 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
3110 * android:exported=&quot;true&quot;
3111 * android:allowEmbedded=&quot;true&quot;
3112 * android:taskAffinity=&quot;&quot; /&gt;</pre>
Griff Hazenc3104152014-05-22 14:38:36 -07003113 *
3114 * @param intent the {@link PendingIntent} for an activity
3115 * @return this object for method chaining
3116 * @see android.app.Notification.WearableExtender#getDisplayIntent
3117 */
3118 public WearableExtender setDisplayIntent(PendingIntent intent) {
3119 mDisplayIntent = intent;
3120 return this;
3121 }
3122
3123 /**
3124 * Get the intent to launch inside of an activity view when displaying this
3125 * notification. This {@code PendingIntent} should be for an activity.
3126 */
3127 public PendingIntent getDisplayIntent() {
3128 return mDisplayIntent;
3129 }
3130
3131 /**
3132 * Add an additional page of content to display with this notification. The current
3133 * notification forms the first page, and pages added using this function form
3134 * subsequent pages. This field can be used to separate a notification into multiple
3135 * sections.
3136 *
3137 * @param page the notification to add as another page
3138 * @return this object for method chaining
3139 * @see android.app.Notification.WearableExtender#getPages
3140 */
3141 public WearableExtender addPage(Notification page) {
3142 mPages.add(page);
3143 return this;
3144 }
3145
3146 /**
3147 * Add additional pages of content to display with this notification. The current
3148 * notification forms the first page, and pages added using this function form
3149 * subsequent pages. This field can be used to separate a notification into multiple
3150 * sections.
3151 *
3152 * @param pages a list of notifications
3153 * @return this object for method chaining
3154 * @see android.app.Notification.WearableExtender#getPages
3155 */
3156 public WearableExtender addPages(List<Notification> pages) {
3157 mPages.addAll(pages);
3158 return this;
3159 }
3160
3161 /**
3162 * Clear all additional pages present on this builder.
3163 * @return this object for method chaining.
3164 * @see #addPage
3165 */
3166 public WearableExtender clearPages() {
3167 mPages.clear();
3168 return this;
3169 }
3170
3171 /**
3172 * Get the array of additional pages of content for displaying this notification. The
3173 * current notification forms the first page, and elements within this array form
3174 * subsequent pages. This field can be used to separate a notification into multiple
3175 * sections.
3176 * @return the pages for this notification
3177 */
3178 public List<Notification> getPages() {
3179 return mPages;
3180 }
3181
3182 /**
3183 * Set a background image to be displayed behind the notification content.
3184 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3185 * will work with any notification style.
3186 *
3187 * @param background the background bitmap
3188 * @return this object for method chaining
3189 * @see android.app.Notification.WearableExtender#getBackground
3190 */
3191 public WearableExtender setBackground(Bitmap background) {
3192 mBackground = background;
3193 return this;
3194 }
3195
3196 /**
3197 * Get a background image to be displayed behind the notification content.
3198 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3199 * will work with any notification style.
3200 *
3201 * @return the background image
3202 * @see android.app.Notification.WearableExtender#setBackground
3203 */
3204 public Bitmap getBackground() {
3205 return mBackground;
3206 }
3207
3208 /**
3209 * Set an icon that goes with the content of this notification.
3210 */
3211 public WearableExtender setContentIcon(int icon) {
3212 mContentIcon = icon;
3213 return this;
3214 }
3215
3216 /**
3217 * Get an icon that goes with the content of this notification.
3218 */
3219 public int getContentIcon() {
3220 return mContentIcon;
3221 }
3222
3223 /**
3224 * Set the gravity that the content icon should have within the notification display.
3225 * Supported values include {@link android.view.Gravity#START} and
3226 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3227 * @see #setContentIcon
3228 */
3229 public WearableExtender setContentIconGravity(int contentIconGravity) {
3230 mContentIconGravity = contentIconGravity;
3231 return this;
3232 }
3233
3234 /**
3235 * Get the gravity that the content icon should have within the notification display.
3236 * Supported values include {@link android.view.Gravity#START} and
3237 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3238 * @see #getContentIcon
3239 */
3240 public int getContentIconGravity() {
3241 return mContentIconGravity;
3242 }
3243
3244 /**
3245 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen6f72ac52014-05-26 09:07:14 -07003246 * this notification. This action will no longer display separately from the
3247 * notification's content.
3248 *
Griff Hazen339f4372014-05-28 09:13:05 -07003249 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen6f72ac52014-05-26 09:07:14 -07003250 * set, although the list of available actions comes from the main notification and not
3251 * from the child page's notification.
3252 *
3253 * @param actionIndex The index of the action to hoist onto the current notification page.
3254 * If wearable actions were added to the main notification, this index
3255 * will apply to that list, otherwise it will apply to the regular
3256 * actions list.
Griff Hazenc3104152014-05-22 14:38:36 -07003257 */
3258 public WearableExtender setContentAction(int actionIndex) {
3259 mContentActionIndex = actionIndex;
3260 return this;
3261 }
3262
3263 /**
Griff Hazen339f4372014-05-28 09:13:05 -07003264 * Get the index of the notification action, if any, that was specified as being clickable
3265 * with the content of this notification. This action will no longer display separately
Griff Hazen6f72ac52014-05-26 09:07:14 -07003266 * from the notification's content.
Griff Hazenc3104152014-05-22 14:38:36 -07003267 *
Griff Hazen339f4372014-05-28 09:13:05 -07003268 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen6f72ac52014-05-26 09:07:14 -07003269 * set, although the list of available actions comes from the main notification and not
3270 * from the child page's notification.
3271 *
3272 * <p>If wearable specific actions were added to the main notification, this index will
3273 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazen339f4372014-05-28 09:13:05 -07003274 *
3275 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazenc3104152014-05-22 14:38:36 -07003276 */
3277 public int getContentAction() {
3278 return mContentActionIndex;
3279 }
3280
3281 /**
3282 * Set the gravity that this notification should have within the available viewport space.
3283 * Supported values include {@link android.view.Gravity#TOP},
3284 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3285 * The default value is {@link android.view.Gravity#BOTTOM}.
3286 */
3287 public WearableExtender setGravity(int gravity) {
3288 mGravity = gravity;
3289 return this;
3290 }
3291
3292 /**
3293 * Get the gravity that this notification should have within the available viewport space.
3294 * Supported values include {@link android.view.Gravity#TOP},
3295 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3296 * The default value is {@link android.view.Gravity#BOTTOM}.
3297 */
3298 public int getGravity() {
3299 return mGravity;
3300 }
3301
3302 /**
3303 * Set the custom size preset for the display of this notification out of the available
3304 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3305 * {@link #SIZE_LARGE}.
3306 * <p>Some custom size presets are only applicable for custom display notifications created
3307 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
3308 * documentation for the preset in question. See also
3309 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
3310 */
3311 public WearableExtender setCustomSizePreset(int sizePreset) {
3312 mCustomSizePreset = sizePreset;
3313 return this;
3314 }
3315
3316 /**
3317 * Get the custom size preset for the display of this notification out of the available
3318 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3319 * {@link #SIZE_LARGE}.
3320 * <p>Some custom size presets are only applicable for custom display notifications created
3321 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
3322 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
3323 */
3324 public int getCustomSizePreset() {
3325 return mCustomSizePreset;
3326 }
3327
3328 /**
3329 * Set the custom height in pixels for the display of this notification's content.
3330 * <p>This option is only available for custom display notifications created
3331 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
3332 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
3333 * {@link #getCustomContentHeight}.
3334 */
3335 public WearableExtender setCustomContentHeight(int height) {
3336 mCustomContentHeight = height;
3337 return this;
3338 }
3339
3340 /**
3341 * Get the custom height in pixels for the display of this notification's content.
3342 * <p>This option is only available for custom display notifications created
3343 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
3344 * {@link #setCustomContentHeight}.
3345 */
3346 public int getCustomContentHeight() {
3347 return mCustomContentHeight;
3348 }
3349
3350 /**
3351 * Set whether the scrolling position for the contents of this notification should start
3352 * at the bottom of the contents instead of the top when the contents are too long to
3353 * display within the screen. Default is false (start scroll at the top).
3354 */
3355 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
3356 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
3357 return this;
3358 }
3359
3360 /**
3361 * Get whether the scrolling position for the contents of this notification should start
3362 * at the bottom of the contents instead of the top when the contents are too long to
3363 * display within the screen. Default is false (start scroll at the top).
3364 */
3365 public boolean getStartScrollBottom() {
3366 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
3367 }
3368
3369 /**
3370 * Set whether the content intent is available when the wearable device is not connected
3371 * to a companion device. The user can still trigger this intent when the wearable device
3372 * is offline, but a visual hint will indicate that the content intent may not be available.
3373 * Defaults to true.
3374 */
3375 public WearableExtender setContentIntentAvailableOffline(
3376 boolean contentIntentAvailableOffline) {
3377 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
3378 return this;
3379 }
3380
3381 /**
3382 * Get whether the content intent is available when the wearable device is not connected
3383 * to a companion device. The user can still trigger this intent when the wearable device
3384 * is offline, but a visual hint will indicate that the content intent may not be available.
3385 * Defaults to true.
3386 */
3387 public boolean getContentIntentAvailableOffline() {
3388 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
3389 }
3390
3391 /**
3392 * Set a hint that this notification's icon should not be displayed.
3393 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
3394 * @return this object for method chaining
3395 */
3396 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
3397 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
3398 return this;
3399 }
3400
3401 /**
3402 * Get a hint that this notification's icon should not be displayed.
3403 * @return {@code true} if this icon should not be displayed, false otherwise.
3404 * The default value is {@code false} if this was never set.
3405 */
3406 public boolean getHintHideIcon() {
3407 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
3408 }
3409
3410 /**
3411 * Set a visual hint that only the background image of this notification should be
3412 * displayed, and other semantic content should be hidden. This hint is only applicable
3413 * to sub-pages added using {@link #addPage}.
3414 */
3415 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
3416 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
3417 return this;
3418 }
3419
3420 /**
3421 * Get a visual hint that only the background image of this notification should be
3422 * displayed, and other semantic content should be hidden. This hint is only applicable
3423 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
3424 */
3425 public boolean getHintShowBackgroundOnly() {
3426 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
3427 }
3428
3429 private void setFlag(int mask, boolean value) {
3430 if (value) {
3431 mFlags |= mask;
3432 } else {
3433 mFlags &= ~mask;
3434 }
3435 }
3436 }
3437
3438 /**
3439 * Get an array of Notification objects from a parcelable array bundle field.
3440 * Update the bundle to have a typed array so fetches in the future don't need
3441 * to do an array copy.
3442 */
3443 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
3444 Parcelable[] array = bundle.getParcelableArray(key);
3445 if (array instanceof Notification[] || array == null) {
3446 return (Notification[]) array;
3447 }
3448 Notification[] typedArray = Arrays.copyOf(array, array.length,
3449 Notification[].class);
3450 bundle.putParcelableArray(key, typedArray);
3451 return typedArray;
3452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453}