blob: 5ac2a33938556dee6dd4b82c3dff5552377c424e [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
Tor Norbyed9273d62013-05-30 15:59:53 -070019import android.annotation.IntDef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.Intent;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040022import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040023import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010024import android.graphics.BitmapFactory;
25import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010026import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010027import android.graphics.drawable.Drawable;
Jeff Sharkey098d5802012-04-26 17:30:34 -070028import android.media.AudioManager;
Dan Sandler842dd772014-05-15 09:36:47 -040029import android.media.session.MediaSessionToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040031import android.os.BadParcelableException;
Dan Sandler26e81cf2014-05-06 10:01:27 -040032import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050033import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Parcel;
35import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040036import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070037import android.os.UserHandle;
Kenny Guy8a0101b2014-05-08 23:34:12 +010038import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040040import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040041import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070042import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080043import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070044import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.widget.RemoteViews;
46
Griff Hazen959591e2014-05-15 22:26:18 -070047import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070048import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070049
Tor Norbyed9273d62013-05-30 15:59:53 -070050import java.lang.annotation.Retention;
51import java.lang.annotation.RetentionPolicy;
Andy Stadler110988c2010-12-03 14:29:16 -080052import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050053import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070054import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070055import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070056import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058/**
59 * A class that represents how a persistent notification is to be presented to
60 * the user using the {@link android.app.NotificationManager}.
61 *
Joe Onoratocb109a02011-01-18 17:57:41 -080062 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
63 * easier to construct Notifications.</p>
64 *
Joe Fernandez558459f2011-10-13 16:47:36 -070065 * <div class="special reference">
66 * <h3>Developer Guides</h3>
67 * <p>For a guide to creating notifications, read the
68 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
69 * developer guide.</p>
70 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
72public class Notification implements Parcelable
73{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040074 private static final String TAG = "Notification";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
77 * Use all default values (where applicable).
78 */
79 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 /**
82 * Use the default notification sound. This will ignore any given
83 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050084 *
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050087 */
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 public static final int DEFAULT_SOUND = 1;
90
91 /**
92 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070094 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050097 */
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 /**
102 * Use the default notification lights. This will ignore the
103 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
104 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500107 */
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800113 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500114 * Default value: {@link System#currentTimeMillis() Now}.
115 *
116 * Choose a timestamp that will be most relevant to the user. For most finite events, this
117 * corresponds to the time the event happened (or will happen, in the case of events that have
118 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800119 * timestamped according to when the activity began.
120 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800122 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 * <ul>
124 * <li>Notification of a new chat message should be stamped when the message was received.</li>
125 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
126 * <li>Notification of a completed file download should be stamped when the download finished.</li>
127 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
128 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
129 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800130 * </ul>
131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 */
133 public long when;
134
135 /**
136 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500137 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 */
139 public int icon;
140
141 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800142 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
143 * leave it at its default value of 0.
144 *
145 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700146 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800147 */
148 public int iconLevel;
149
150 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500151 * The number of events that this notification represents. For example, in a new mail
152 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800153 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154 * The system may or may not use this field to modify the appearance of the notification. For
155 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
156 * superimposed over the icon in the status bar. Starting with
157 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
158 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800159 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 */
162 public int number;
163
164 /**
165 * The intent to execute when the expanded status entry is clicked. If
166 * this is an activity, it must include the
167 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800168 * that you take care of task management as described in the
169 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800170 * Stack</a> document. In particular, make sure to read the notification section
171 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
172 * Notifications</a> for the correct ways to launch an application from a
173 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 */
175 public PendingIntent contentIntent;
176
177 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 * The intent to execute when the notification is explicitly dismissed by the user, either with
179 * the "Clear All" button or by swiping it away individually.
180 *
181 * This probably shouldn't be launching an activity since several of those will be sent
182 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 */
184 public PendingIntent deleteIntent;
185
186 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700187 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800188 *
189 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400190 */
191 public PendingIntent fullScreenIntent;
192
193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400195 * the status bar on large and smaller devices.
196 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800197 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 */
199 public CharSequence tickerText;
200
201 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800202 * The view to show as the ticker in the status bar when the notification
203 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400204 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800205 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400206
207 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400208 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 public RemoteViews contentView;
211
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400212 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400213 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400214 * opportunity to show more detail. The system UI may choose to show this
215 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400216 */
217 public RemoteViews bigContentView;
218
Chris Wren8fd39ec2014-02-27 17:43:26 -0500219
220 /**
221 * @hide
222 * A medium-format version of {@link #contentView}, giving the Notification an
223 * opportunity to add action buttons to contentView. The system UI may
224 * choose to show this as a popup notification at its discretion.
225 */
226 public RemoteViews headsUpContentView;
227
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400228 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800229 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800231 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
233 /**
234 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500237 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 * </p>
239 */
240 public Uri sound;
241
242 /**
243 * Use this constant as the value for audioStreamType to request that
244 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700245 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 */
247 public static final int STREAM_DEFAULT = -1;
248
249 /**
250 * The audio stream type to use when playing the sound.
251 * Should be one of the STREAM_ constants from
252 * {@link android.media.AudioManager}.
253 */
254 public int audioStreamType = STREAM_DEFAULT;
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500257 * The pattern with which to vibrate.
258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * <p>
260 * To vibrate the default pattern, see {@link #defaults}.
261 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * @see android.os.Vibrator#vibrate(long[],int)
264 */
265 public long[] vibrate;
266
267 /**
268 * The color of the led. The hardware will do its best approximation.
269 *
270 * @see #FLAG_SHOW_LIGHTS
271 * @see #flags
272 */
273 public int ledARGB;
274
275 /**
276 * The number of milliseconds for the LED to be on while it's flashing.
277 * The hardware will do its best approximation.
278 *
279 * @see #FLAG_SHOW_LIGHTS
280 * @see #flags
281 */
282 public int ledOnMS;
283
284 /**
285 * The number of milliseconds for the LED to be off while it's flashing.
286 * The hardware will do its best approximation.
287 *
288 * @see #FLAG_SHOW_LIGHTS
289 * @see #flags
290 */
291 public int ledOffMS;
292
293 /**
294 * Specifies which values should be taken from the defaults.
295 * <p>
296 * To set, OR the desired from {@link #DEFAULT_SOUND},
297 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
298 * values, use {@link #DEFAULT_ALL}.
299 * </p>
300 */
301 public int defaults;
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * Bit to be bitwise-ored into the {@link #flags} field that should be
305 * set if you want the LED on for this notification.
306 * <ul>
307 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
308 * or 0 for both ledOnMS and ledOffMS.</li>
309 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
310 * <li>To flash the LED, pass the number of milliseconds that it should
311 * be on and off to ledOnMS and ledOffMS.</li>
312 * </ul>
313 * <p>
314 * Since hardware varies, you are not guaranteed that any of the values
315 * you pass are honored exactly. Use the system defaults (TODO) if possible
316 * because they will be set to values that work on any given hardware.
317 * <p>
318 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500319 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 */
321 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
322
323 /**
324 * Bit to be bitwise-ored into the {@link #flags} field that should be
325 * set if this notification is in reference to something that is ongoing,
326 * like a phone call. It should not be set if this notification is in
327 * reference to something that happened at a particular point in time,
328 * like a missed phone call.
329 */
330 public static final int FLAG_ONGOING_EVENT = 0x00000002;
331
332 /**
333 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700334 * the audio will be repeated until the notification is
335 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 */
337 public static final int FLAG_INSISTENT = 0x00000004;
338
339 /**
340 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700341 * set if you would only like the sound, vibrate and ticker to be played
342 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 */
344 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
345
346 /**
347 * Bit to be bitwise-ored into the {@link #flags} field that should be
348 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500349 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 */
352 public static final int FLAG_AUTO_CANCEL = 0x00000010;
353
354 /**
355 * Bit to be bitwise-ored into the {@link #flags} field that should be
356 * set if the notification should not be canceled when the user clicks
357 * the Clear all button.
358 */
359 public static final int FLAG_NO_CLEAR = 0x00000020;
360
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700361 /**
362 * Bit to be bitwise-ored into the {@link #flags} field that should be
363 * set if this notification represents a currently running service. This
364 * will normally be set for you by {@link Service#startForeground}.
365 */
366 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
367
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400368 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500369 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800370 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500371 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400372 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500373 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400374
Griff Hazendfcb0802014-02-11 12:00:00 -0800375 /**
376 * Bit to be bitswise-ored into the {@link #flags} field that should be
377 * set if this notification is relevant to the current device only
378 * and it is not recommended that it bridge to other devices.
379 */
380 public static final int FLAG_LOCAL_ONLY = 0x00000100;
381
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700382 /**
383 * Bit to be bitswise-ored into the {@link #flags} field that should be
384 * set if this notification is the group summary for a group of notifications.
385 * Grouped notifications may display in a cluster or stack on devices which
386 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
387 */
388 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 public int flags;
391
Tor Norbyed9273d62013-05-30 15:59:53 -0700392 /** @hide */
393 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
394 @Retention(RetentionPolicy.SOURCE)
395 public @interface Priority {}
396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500398 * Default notification {@link #priority}. If your application does not prioritize its own
399 * notifications, use this value for all notifications.
400 */
401 public static final int PRIORITY_DEFAULT = 0;
402
403 /**
404 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
405 * items smaller, or at a different position in the list, compared with your app's
406 * {@link #PRIORITY_DEFAULT} items.
407 */
408 public static final int PRIORITY_LOW = -1;
409
410 /**
411 * Lowest {@link #priority}; these items might not be shown to the user except under special
412 * circumstances, such as detailed notification logs.
413 */
414 public static final int PRIORITY_MIN = -2;
415
416 /**
417 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
418 * show these items larger, or at a different position in notification lists, compared with
419 * your app's {@link #PRIORITY_DEFAULT} items.
420 */
421 public static final int PRIORITY_HIGH = 1;
422
423 /**
424 * Highest {@link #priority}, for your application's most important items that require the
425 * user's prompt attention or input.
426 */
427 public static final int PRIORITY_MAX = 2;
428
429 /**
430 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800431 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500432 * Priority is an indication of how much of the user's valuable attention should be consumed by
433 * this notification. Low-priority notifications may be hidden from the user in certain
434 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500435 * system will make a determination about how to interpret this priority when presenting
436 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500437 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700438 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500439 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800440
Dan Sandler26e81cf2014-05-06 10:01:27 -0400441 /**
442 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
443 * to be applied by the standard Style templates when presenting this notification.
444 *
445 * The current template design constructs a colorful header image by overlaying the
446 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
447 * ignored.
448 */
449 public int color = COLOR_DEFAULT;
450
451 /**
452 * Special value of {@link #color} telling the system not to decorate this notification with
453 * any special color but instead use default colors when presenting this notification.
454 */
455 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600456
457 /**
458 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
459 * the notification's presence and contents in untrusted situations (namely, on the secure
460 * lockscreen).
461 *
462 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
463 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
464 * shown in all situations, but the contents are only available if the device is unlocked for
465 * the appropriate user.
466 *
467 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
468 * can be read even in an "insecure" context (that is, above a secure lockscreen).
469 * To modify the public version of this notification—for example, to redact some portions—see
470 * {@link Builder#setPublicVersion(Notification)}.
471 *
472 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
473 * and ticker until the user has bypassed the lockscreen.
474 */
475 public int visibility;
476
477 public static final int VISIBILITY_PUBLIC = 1;
478 public static final int VISIBILITY_PRIVATE = 0;
479 public static final int VISIBILITY_SECRET = -1;
480
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500481 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400482 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500483 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400484 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500485
486 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400487 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500488 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400489 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490
491 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400492 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500493 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400494 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500495
496 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400497 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500498 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400499 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500500
501 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400502 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500503 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400504 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500505
506 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400507 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500508 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400509 public static final String CATEGORY_ALARM = "alarm";
510
511 /**
512 * Notification category: progress of a long-running background operation.
513 */
514 public static final String CATEGORY_PROGRESS = "progress";
515
516 /**
517 * Notification category: social network or sharing update.
518 */
519 public static final String CATEGORY_SOCIAL = "social";
520
521 /**
522 * Notification category: error in background operation or authentication status.
523 */
524 public static final String CATEGORY_ERROR = "err";
525
526 /**
527 * Notification category: media transport control for playback.
528 */
529 public static final String CATEGORY_TRANSPORT = "transport";
530
531 /**
532 * Notification category: system or device status update. Reserved for system use.
533 */
534 public static final String CATEGORY_SYSTEM = "sys";
535
536 /**
537 * Notification category: indication of running background service.
538 */
539 public static final String CATEGORY_SERVICE = "service";
540
541 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400542 * Notification category: a specific, timely recommendation for a single thing.
543 * For example, a news app might want to recommend a news story it believes the user will
544 * want to read next.
545 */
546 public static final String CATEGORY_RECOMMENDATION = "recommendation";
547
548 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400549 * Notification category: ongoing information about device or contextual status.
550 */
551 public static final String CATEGORY_STATUS = "status";
552
553 /**
554 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
555 * that best describes this Notification. May be used by the system for ranking and filtering.
556 */
557 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500558
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700559 private String mGroupKey;
560
561 /**
562 * Get the key used to group this notification into a cluster or stack
563 * with other notifications on devices which support such rendering.
564 */
565 public String getGroup() {
566 return mGroupKey;
567 }
568
569 private String mSortKey;
570
571 /**
572 * Get a sort key that orders this notification among other notifications from the
573 * same package. This can be useful if an external sort was already applied and an app
574 * would like to preserve this. Notifications will be sorted lexicographically using this
575 * value, although providing different priorities in addition to providing sort key may
576 * cause this value to be ignored.
577 *
578 * <p>This sort key can also be used to order members of a notification group. See
579 * {@link Builder#setGroup}.
580 *
581 * @see String#compareTo(String)
582 */
583 public String getSortKey() {
584 return mSortKey;
585 }
586
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500587 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400588 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400589 * <p>
590 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
591 * APIs, and are intended to be used by
592 * {@link android.service.notification.NotificationListenerService} implementations to extract
593 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500594 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400595 public Bundle extras = new Bundle();
596
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400597 /**
598 * {@link #extras} key: this is the title of the notification,
599 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
600 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500601 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400602
603 /**
604 * {@link #extras} key: this is the title of the notification when shown in expanded form,
605 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
606 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400607 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400608
609 /**
610 * {@link #extras} key: this is the main text payload, as supplied to
611 * {@link Builder#setContentText(CharSequence)}.
612 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500613 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400614
615 /**
616 * {@link #extras} key: this is a third line of text, as supplied to
617 * {@link Builder#setSubText(CharSequence)}.
618 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400619 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400620
621 /**
622 * {@link #extras} key: this is a small piece of additional text as supplied to
623 * {@link Builder#setContentInfo(CharSequence)}.
624 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400625 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400626
627 /**
628 * {@link #extras} key: this is a line of summary information intended to be shown
629 * alongside expanded notifications, as supplied to (e.g.)
630 * {@link BigTextStyle#setSummaryText(CharSequence)}.
631 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400632 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400633
634 /**
635 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
636 * supplied to {@link Builder#setSmallIcon(int)}.
637 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500638 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400639
640 /**
641 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
642 * notification payload, as
643 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
644 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400645 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400646
647 /**
648 * {@link #extras} key: this is a bitmap to be used instead of the one from
649 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
650 * shown in its expanded form, as supplied to
651 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
652 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400653 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400654
655 /**
656 * {@link #extras} key: this is the progress value supplied to
657 * {@link Builder#setProgress(int, int, boolean)}.
658 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400659 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400660
661 /**
662 * {@link #extras} key: this is the maximum value supplied to
663 * {@link Builder#setProgress(int, int, boolean)}.
664 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400665 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400666
667 /**
668 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
669 * {@link Builder#setProgress(int, int, boolean)}.
670 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400671 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400672
673 /**
674 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
675 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
676 * {@link Builder#setUsesChronometer(boolean)}.
677 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400678 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400679
680 /**
681 * {@link #extras} key: whether {@link #when} should be shown,
682 * as supplied to {@link Builder#setShowWhen(boolean)}.
683 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400684 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400685
686 /**
687 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
688 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
689 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400690 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400691
692 /**
693 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
694 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
695 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400696 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400697
698 /**
699 * {@link #extras} key: A string representing the name of the specific
700 * {@link android.app.Notification.Style} used to create this notification.
701 */
Chris Wren91ad5632013-06-05 15:05:57 -0400702 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400703
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400704 /**
705 * {@link #extras} key: An array of people that this notification relates to, specified
706 * by contacts provider contact URI.
707 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400708 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500709
710 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400711 * @hide
Chris Wrenf9536642014-04-17 10:01:54 -0400712 * Extra added by NotificationManagerService to indicate whether
713 * the Notifications's score has been modified.
Scott Greenwald9a05b312013-06-28 00:37:54 -0400714 */
715 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
716
717 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400718 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400719 * @hide
720 */
721 public static final String EXTRA_AS_HEADS_UP = "headsup";
722
723 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400724 * Allow certain system-generated notifications to appear before the device is provisioned.
725 * Only available to notifications coming from the android package.
726 * @hide
727 */
728 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
729
730 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700731 * {@link #extras} key: A
732 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
733 * in the background when the notification is selected. The URI must point to an image stream
734 * suitable for passing into
735 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
736 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
737 * URI used for this purpose must require no permissions to read the image data.
738 */
739 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
740
741 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400742 * {@link #extras} key: A
743 * {@link android.media.session.MediaSessionToken} associated with a
744 * {@link android.app.Notification.MediaStyle} notification.
745 */
746 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
747
748 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400749 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400750 * @hide
751 */
752 public static final int HEADS_UP_NEVER = 0;
753
754 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400755 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400756 * @hide
757 */
758 public static final int HEADS_UP_ALLOWED = 1;
759
760 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400761 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400762 * @hide
763 */
764 public static final int HEADS_UP_REQUESTED = 2;
765
766 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400767 * Structure to encapsulate a named action that can be shown as part of this notification.
768 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
769 * selected by the user.
770 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700771 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
772 * or {@link Notification.Builder#addAction(Notification.Action)}
773 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400774 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500775 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700776 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700777 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700778
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400779 /**
780 * Small icon representing the action.
781 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400782 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700783
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784 /**
785 * Title of the action.
786 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400787 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700788
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400789 /**
790 * Intent to send when the user invokes this action. May be null, in which case the action
791 * may be rendered in a disabled presentation by the system UI.
792 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400793 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700794
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400795 private Action(Parcel in) {
796 icon = in.readInt();
797 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
798 if (in.readInt() == 1) {
799 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
800 }
Griff Hazen959591e2014-05-15 22:26:18 -0700801 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700802 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400803 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700804
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400805 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700806 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400807 */
808 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700809 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700810 }
811
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700812 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
813 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814 this.icon = icon;
815 this.title = title;
816 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700817 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700818 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700819 }
820
821 /**
822 * Get additional metadata carried around with this Action.
823 */
824 public Bundle getExtras() {
825 return mExtras;
826 }
827
828 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700829 * Get the list of inputs to be collected from the user when this action is sent.
830 * May return null if no remote inputs were added.
831 */
832 public RemoteInput[] getRemoteInputs() {
833 return mRemoteInputs;
834 }
835
836 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700837 * Builder class for {@link Action} objects.
838 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700839 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700840 private final int mIcon;
841 private final CharSequence mTitle;
842 private final PendingIntent mIntent;
843 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700844 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700845
846 /**
847 * Construct a new builder for {@link Action} object.
848 * @param icon icon to show for this action
849 * @param title the title of the action
850 * @param intent the {@link PendingIntent} to fire when users trigger this action
851 */
852 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700853 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700854 }
855
856 /**
857 * Construct a new builder for {@link Action} object using the fields from an
858 * {@link Action}.
859 * @param action the action to read fields from.
860 */
861 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700862 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
863 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700864 }
865
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700866 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
867 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700868 mIcon = icon;
869 mTitle = title;
870 mIntent = intent;
871 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700872 if (remoteInputs != null) {
873 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
874 Collections.addAll(mRemoteInputs, remoteInputs);
875 }
Griff Hazen959591e2014-05-15 22:26:18 -0700876 }
877
878 /**
879 * Merge additional metadata into this builder.
880 *
881 * <p>Values within the Bundle will replace existing extras values in this Builder.
882 *
883 * @see Notification.Action#extras
884 */
885 public Builder addExtras(Bundle extras) {
886 if (extras != null) {
887 mExtras.putAll(extras);
888 }
889 return this;
890 }
891
892 /**
893 * Get the metadata Bundle used by this Builder.
894 *
895 * <p>The returned Bundle is shared with this Builder.
896 */
897 public Bundle getExtras() {
898 return mExtras;
899 }
900
901 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700902 * Add an input to be collected from the user when this action is sent.
903 * Response values can be retrieved from the fired intent by using the
904 * {@link RemoteInput#getResultsFromIntent} function.
905 * @param remoteInput a {@link RemoteInput} to add to the action
906 * @return this object for method chaining
907 */
908 public Builder addRemoteInput(RemoteInput remoteInput) {
909 if (mRemoteInputs == null) {
910 mRemoteInputs = new ArrayList<RemoteInput>();
911 }
912 mRemoteInputs.add(remoteInput);
913 return this;
914 }
915
916 /**
917 * Apply an extender to this action builder. Extenders may be used to add
918 * metadata or change options on this builder.
919 */
Griff Hazen61a9e862014-05-22 16:05:19 -0700920 public Builder extend(Extender extender) {
921 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700922 return this;
923 }
924
925 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700926 * Combine all of the options that have been set and return a new {@link Action}
927 * object.
928 * @return the built action
929 */
930 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700931 RemoteInput[] remoteInputs = mRemoteInputs != null
932 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
933 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -0700934 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400935 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400936
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400937 @Override
938 public Action clone() {
939 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700940 icon,
941 title,
942 actionIntent, // safe to alias
943 new Bundle(mExtras),
944 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400945 }
946 @Override
947 public int describeContents() {
948 return 0;
949 }
950 @Override
951 public void writeToParcel(Parcel out, int flags) {
952 out.writeInt(icon);
953 TextUtils.writeToParcel(title, out, flags);
954 if (actionIntent != null) {
955 out.writeInt(1);
956 actionIntent.writeToParcel(out, flags);
957 } else {
958 out.writeInt(0);
959 }
Griff Hazen959591e2014-05-15 22:26:18 -0700960 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700961 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400962 }
Griff Hazen959591e2014-05-15 22:26:18 -0700963 public static final Parcelable.Creator<Action> CREATOR =
964 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400965 public Action createFromParcel(Parcel in) {
966 return new Action(in);
967 }
968 public Action[] newArray(int size) {
969 return new Action[size];
970 }
971 };
Griff Hazen61a9e862014-05-22 16:05:19 -0700972
973 /**
974 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
975 * metadata or change options on an action builder.
976 */
977 public interface Extender {
978 /**
979 * Apply this extender to a notification action builder.
980 * @param builder the builder to be modified.
981 * @return the build object for chaining.
982 */
983 public Builder extend(Builder builder);
984 }
985
986 /**
987 * Wearable extender for notification actions. To add extensions to an action,
988 * create a new {@link android.app.Notification.Action.WearableExtender} object using
989 * the {@code WearableExtender()} constructor and apply it to a
990 * {@link android.app.Notification.Action.Builder} using
991 * {@link android.app.Notification.Action.Builder#extend}.
992 *
993 * <pre class="prettyprint">
994 * Notification.Action action = new Notification.Action.Builder(
995 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -0700996 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -0700997 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -0700998 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -0700999 */
1000 public static final class WearableExtender implements Extender {
1001 /** Notification action extra which contains wearable extensions */
1002 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1003
1004 private static final String KEY_FLAGS = "flags";
1005
1006 // Flags bitwise-ored to mFlags
1007 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1008
1009 // Default value for flags integer
1010 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1011
1012 private int mFlags = DEFAULT_FLAGS;
1013
1014 /**
1015 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1016 * options.
1017 */
1018 public WearableExtender() {
1019 }
1020
1021 /**
1022 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1023 * wearable options present in an existing notification action.
1024 * @param action the notification action to inspect.
1025 */
1026 public WearableExtender(Action action) {
1027 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1028 if (wearableBundle != null) {
1029 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1030 }
1031 }
1032
1033 /**
1034 * Apply wearable extensions to a notification action that is being built. This is
1035 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1036 * method of {@link android.app.Notification.Action.Builder}.
1037 */
1038 @Override
1039 public Action.Builder extend(Action.Builder builder) {
1040 Bundle wearableBundle = new Bundle();
1041
1042 if (mFlags != DEFAULT_FLAGS) {
1043 wearableBundle.putInt(KEY_FLAGS, mFlags);
1044 }
1045
1046 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1047 return builder;
1048 }
1049
1050 @Override
1051 public WearableExtender clone() {
1052 WearableExtender that = new WearableExtender();
1053 that.mFlags = this.mFlags;
1054 return that;
1055 }
1056
1057 /**
1058 * Set whether this action is available when the wearable device is not connected to
1059 * a companion device. The user can still trigger this action when the wearable device is
1060 * offline, but a visual hint will indicate that the action may not be available.
1061 * Defaults to true.
1062 */
1063 public WearableExtender setAvailableOffline(boolean availableOffline) {
1064 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1065 return this;
1066 }
1067
1068 /**
1069 * Get whether this action is available when the wearable device is not connected to
1070 * a companion device. The user can still trigger this action when the wearable device is
1071 * offline, but a visual hint will indicate that the action may not be available.
1072 * Defaults to true.
1073 */
1074 public boolean isAvailableOffline() {
1075 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1076 }
1077
1078 private void setFlag(int mask, boolean value) {
1079 if (value) {
1080 mFlags |= mask;
1081 } else {
1082 mFlags &= ~mask;
1083 }
1084 }
1085 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001086 }
1087
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001088 /**
1089 * Array of all {@link Action} structures attached to this notification by
1090 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1091 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1092 * interface for invoking actions.
1093 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001094 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001095
1096 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001097 * Replacement version of this notification whose content will be shown
1098 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1099 * and {@link #VISIBILITY_PUBLIC}.
1100 */
1101 public Notification publicVersion;
1102
1103 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001104 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001105 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 */
1107 public Notification()
1108 {
1109 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001110 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112
1113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 * @hide
1115 */
1116 public Notification(Context context, int icon, CharSequence tickerText, long when,
1117 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1118 {
1119 this.when = when;
1120 this.icon = icon;
1121 this.tickerText = tickerText;
1122 setLatestEventInfo(context, contentTitle, contentText,
1123 PendingIntent.getActivity(context, 0, contentIntent, 0));
1124 }
1125
1126 /**
1127 * Constructs a Notification object with the information needed to
1128 * have a status bar icon without the standard expanded view.
1129 *
1130 * @param icon The resource id of the icon to put in the status bar.
1131 * @param tickerText The text that flows by in the status bar when the notification first
1132 * activates.
1133 * @param when The time to show in the time field. In the System.currentTimeMillis
1134 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001135 *
1136 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001138 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 public Notification(int icon, CharSequence tickerText, long when)
1140 {
1141 this.icon = icon;
1142 this.tickerText = tickerText;
1143 this.when = when;
1144 }
1145
1146 /**
1147 * Unflatten the notification from a parcel.
1148 */
1149 public Notification(Parcel parcel)
1150 {
1151 int version = parcel.readInt();
1152
1153 when = parcel.readLong();
1154 icon = parcel.readInt();
1155 number = parcel.readInt();
1156 if (parcel.readInt() != 0) {
1157 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1158 }
1159 if (parcel.readInt() != 0) {
1160 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1161 }
1162 if (parcel.readInt() != 0) {
1163 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1164 }
1165 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001166 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001167 }
1168 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1170 }
Joe Onorato561d3852010-11-20 18:09:34 -08001171 if (parcel.readInt() != 0) {
1172 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 defaults = parcel.readInt();
1175 flags = parcel.readInt();
1176 if (parcel.readInt() != 0) {
1177 sound = Uri.CREATOR.createFromParcel(parcel);
1178 }
1179
1180 audioStreamType = parcel.readInt();
1181 vibrate = parcel.createLongArray();
1182 ledARGB = parcel.readInt();
1183 ledOnMS = parcel.readInt();
1184 ledOffMS = parcel.readInt();
1185 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001186
1187 if (parcel.readInt() != 0) {
1188 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1189 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001190
1191 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001192
John Spurlockfd7f1e02014-03-18 16:41:57 -04001193 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001194
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001195 mGroupKey = parcel.readString();
1196
1197 mSortKey = parcel.readString();
1198
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001199 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001200
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001201 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1202
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001203 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001204 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1205 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001206
Chris Wren8fd39ec2014-02-27 17:43:26 -05001207 if (parcel.readInt() != 0) {
1208 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1209 }
1210
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001211 visibility = parcel.readInt();
1212
1213 if (parcel.readInt() != 0) {
1214 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1215 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001216
1217 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
1219
Andy Stadler110988c2010-12-03 14:29:16 -08001220 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001221 public Notification clone() {
1222 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001223 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001224 return that;
1225 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001226
Daniel Sandler1a497d32013-04-18 14:52:45 -04001227 /**
1228 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1229 * of this into that.
1230 * @hide
1231 */
1232 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001233 that.when = this.when;
1234 that.icon = this.icon;
1235 that.number = this.number;
1236
1237 // PendingIntents are global, so there's no reason (or way) to clone them.
1238 that.contentIntent = this.contentIntent;
1239 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001240 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001241
1242 if (this.tickerText != null) {
1243 that.tickerText = this.tickerText.toString();
1244 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001245 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001246 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001247 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001248 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001249 that.contentView = this.contentView.clone();
1250 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001251 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001252 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1253 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001254 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001255 that.sound = this.sound; // android.net.Uri is immutable
1256 that.audioStreamType = this.audioStreamType;
1257
1258 final long[] vibrate = this.vibrate;
1259 if (vibrate != null) {
1260 final int N = vibrate.length;
1261 final long[] vib = that.vibrate = new long[N];
1262 System.arraycopy(vibrate, 0, vib, 0, N);
1263 }
1264
1265 that.ledARGB = this.ledARGB;
1266 that.ledOnMS = this.ledOnMS;
1267 that.ledOffMS = this.ledOffMS;
1268 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001269
Joe Onorato18e69df2010-05-17 22:26:12 -07001270 that.flags = this.flags;
1271
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001272 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001273
John Spurlockfd7f1e02014-03-18 16:41:57 -04001274 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001275
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001276 that.mGroupKey = this.mGroupKey;
1277
1278 that.mSortKey = this.mSortKey;
1279
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001280 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001281 try {
1282 that.extras = new Bundle(this.extras);
1283 // will unparcel
1284 that.extras.size();
1285 } catch (BadParcelableException e) {
1286 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1287 that.extras = null;
1288 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001289 }
1290
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001291 if (this.actions != null) {
1292 that.actions = new Action[this.actions.length];
1293 for(int i=0; i<this.actions.length; i++) {
1294 that.actions[i] = this.actions[i].clone();
1295 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001296 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001297
Daniel Sandler1a497d32013-04-18 14:52:45 -04001298 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001299 that.bigContentView = this.bigContentView.clone();
1300 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001301
Chris Wren8fd39ec2014-02-27 17:43:26 -05001302 if (heavy && this.headsUpContentView != null) {
1303 that.headsUpContentView = this.headsUpContentView.clone();
1304 }
1305
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001306 that.visibility = this.visibility;
1307
1308 if (this.publicVersion != null) {
1309 that.publicVersion = new Notification();
1310 this.publicVersion.cloneInto(that.publicVersion, heavy);
1311 }
1312
Dan Sandler26e81cf2014-05-06 10:01:27 -04001313 that.color = this.color;
1314
Daniel Sandler1a497d32013-04-18 14:52:45 -04001315 if (!heavy) {
1316 that.lightenPayload(); // will clean out extras
1317 }
1318 }
1319
1320 /**
1321 * Removes heavyweight parts of the Notification object for archival or for sending to
1322 * listeners when the full contents are not necessary.
1323 * @hide
1324 */
1325 public final void lightenPayload() {
1326 tickerView = null;
1327 contentView = null;
1328 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001329 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001330 largeIcon = null;
1331 if (extras != null) {
1332 extras.remove(Notification.EXTRA_LARGE_ICON);
1333 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1334 extras.remove(Notification.EXTRA_PICTURE);
1335 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001336 }
1337
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001338 /**
1339 * Make sure this CharSequence is safe to put into a bundle, which basically
1340 * means it had better not be some custom Parcelable implementation.
1341 * @hide
1342 */
1343 public static CharSequence safeCharSequence(CharSequence cs) {
1344 if (cs instanceof Parcelable) {
1345 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1346 + " instance is a custom Parcelable and not allowed in Notification");
1347 return cs.toString();
1348 }
1349
1350 return cs;
1351 }
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 public int describeContents() {
1354 return 0;
1355 }
1356
1357 /**
1358 * Flatten this notification from a parcel.
1359 */
1360 public void writeToParcel(Parcel parcel, int flags)
1361 {
1362 parcel.writeInt(1);
1363
1364 parcel.writeLong(when);
1365 parcel.writeInt(icon);
1366 parcel.writeInt(number);
1367 if (contentIntent != null) {
1368 parcel.writeInt(1);
1369 contentIntent.writeToParcel(parcel, 0);
1370 } else {
1371 parcel.writeInt(0);
1372 }
1373 if (deleteIntent != null) {
1374 parcel.writeInt(1);
1375 deleteIntent.writeToParcel(parcel, 0);
1376 } else {
1377 parcel.writeInt(0);
1378 }
1379 if (tickerText != null) {
1380 parcel.writeInt(1);
1381 TextUtils.writeToParcel(tickerText, parcel, flags);
1382 } else {
1383 parcel.writeInt(0);
1384 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001385 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001386 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001387 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001388 } else {
1389 parcel.writeInt(0);
1390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 if (contentView != null) {
1392 parcel.writeInt(1);
1393 contentView.writeToParcel(parcel, 0);
1394 } else {
1395 parcel.writeInt(0);
1396 }
Joe Onorato561d3852010-11-20 18:09:34 -08001397 if (largeIcon != null) {
1398 parcel.writeInt(1);
1399 largeIcon.writeToParcel(parcel, 0);
1400 } else {
1401 parcel.writeInt(0);
1402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403
1404 parcel.writeInt(defaults);
1405 parcel.writeInt(this.flags);
1406
1407 if (sound != null) {
1408 parcel.writeInt(1);
1409 sound.writeToParcel(parcel, 0);
1410 } else {
1411 parcel.writeInt(0);
1412 }
1413 parcel.writeInt(audioStreamType);
1414 parcel.writeLongArray(vibrate);
1415 parcel.writeInt(ledARGB);
1416 parcel.writeInt(ledOnMS);
1417 parcel.writeInt(ledOffMS);
1418 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001419
1420 if (fullScreenIntent != null) {
1421 parcel.writeInt(1);
1422 fullScreenIntent.writeToParcel(parcel, 0);
1423 } else {
1424 parcel.writeInt(0);
1425 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001426
1427 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001428
John Spurlockfd7f1e02014-03-18 16:41:57 -04001429 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001430
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001431 parcel.writeString(mGroupKey);
1432
1433 parcel.writeString(mSortKey);
1434
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001435 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001436
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001437 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001438
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001439 if (bigContentView != null) {
1440 parcel.writeInt(1);
1441 bigContentView.writeToParcel(parcel, 0);
1442 } else {
1443 parcel.writeInt(0);
1444 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001445
Chris Wren8fd39ec2014-02-27 17:43:26 -05001446 if (headsUpContentView != null) {
1447 parcel.writeInt(1);
1448 headsUpContentView.writeToParcel(parcel, 0);
1449 } else {
1450 parcel.writeInt(0);
1451 }
1452
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001453 parcel.writeInt(visibility);
1454
1455 if (publicVersion != null) {
1456 parcel.writeInt(1);
1457 publicVersion.writeToParcel(parcel, 0);
1458 } else {
1459 parcel.writeInt(0);
1460 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001461
1462 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 }
1464
1465 /**
1466 * Parcelable.Creator that instantiates Notification objects
1467 */
1468 public static final Parcelable.Creator<Notification> CREATOR
1469 = new Parcelable.Creator<Notification>()
1470 {
1471 public Notification createFromParcel(Parcel parcel)
1472 {
1473 return new Notification(parcel);
1474 }
1475
1476 public Notification[] newArray(int size)
1477 {
1478 return new Notification[size];
1479 }
1480 };
1481
1482 /**
1483 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1484 * layout.
1485 *
1486 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1487 * in the view.</p>
1488 * @param context The context for your application / activity.
1489 * @param contentTitle The title that goes in the expanded entry.
1490 * @param contentText The text that goes in the expanded entry.
1491 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1492 * If this is an activity, it must include the
1493 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001494 * that you take care of task management as described in the
1495 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1496 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001497 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001498 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001500 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 public void setLatestEventInfo(Context context,
1502 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001503 Notification.Builder builder = new Notification.Builder(context);
1504
1505 // First, ensure that key pieces of information that may have been set directly
1506 // are preserved
1507 builder.setWhen(this.when);
1508 builder.setSmallIcon(this.icon);
1509 builder.setPriority(this.priority);
1510 builder.setTicker(this.tickerText);
1511 builder.setNumber(this.number);
1512 builder.mFlags = this.flags;
1513 builder.setSound(this.sound, this.audioStreamType);
1514 builder.setDefaults(this.defaults);
1515 builder.setVibrate(this.vibrate);
1516
1517 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001519 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
1521 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001522 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001524 builder.setContentIntent(contentIntent);
1525 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 }
1527
1528 @Override
1529 public String toString() {
1530 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001531 sb.append("Notification(pri=");
1532 sb.append(priority);
1533 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001534 if (contentView != null) {
1535 sb.append(contentView.getPackage());
1536 sb.append("/0x");
1537 sb.append(Integer.toHexString(contentView.getLayoutId()));
1538 } else {
1539 sb.append("null");
1540 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001541 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001542 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001543 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1544 sb.append("default");
1545 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 int N = this.vibrate.length-1;
1547 sb.append("[");
1548 for (int i=0; i<N; i++) {
1549 sb.append(this.vibrate[i]);
1550 sb.append(',');
1551 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001552 if (N != -1) {
1553 sb.append(this.vibrate[N]);
1554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 } else {
1557 sb.append("null");
1558 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001559 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001560 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001562 } else if (this.sound != null) {
1563 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 } else {
1565 sb.append("null");
1566 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001567 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001569 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001570 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001571 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001572 if (this.category != null) {
1573 sb.append(" category=");
1574 sb.append(this.category);
1575 }
1576 if (this.mGroupKey != null) {
1577 sb.append(" groupKey=");
1578 sb.append(this.mGroupKey);
1579 }
1580 if (this.mSortKey != null) {
1581 sb.append(" sortKey=");
1582 sb.append(this.mSortKey);
1583 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001584 if (actions != null) {
1585 sb.append(" ");
1586 sb.append(actions.length);
1587 sb.append(" action");
1588 if (actions.length > 1) sb.append("s");
1589 }
1590 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 return sb.toString();
1592 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001593
Jeff Sharkey6d515712012-09-20 16:06:08 -07001594 /** {@hide} */
1595 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001596 if (user.getIdentifier() == UserHandle.USER_ALL) {
1597 user = UserHandle.OWNER;
1598 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001599 if (tickerView != null) {
1600 tickerView.setUser(user);
1601 }
1602 if (contentView != null) {
1603 contentView.setUser(user);
1604 }
1605 if (bigContentView != null) {
1606 bigContentView.setUser(user);
1607 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001608 if (headsUpContentView != null) {
1609 headsUpContentView.setUser(user);
1610 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001611 }
1612
Joe Onoratocb109a02011-01-18 17:57:41 -08001613 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001614 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001615 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001616 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001617 * content views using the platform's notification layout template. If your app supports
1618 * versions of Android as old as API level 4, you can instead use
1619 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1620 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1621 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001622 *
Scott Main183bf112012-08-13 19:12:13 -07001623 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001624 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001625 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001626 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001627 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1628 * .setContentText(subject)
1629 * .setSmallIcon(R.drawable.new_mail)
1630 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001631 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001632 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001633 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001634 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001635 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001636
Joe Onorato46439ce2010-11-19 13:56:21 -08001637 private Context mContext;
1638
1639 private long mWhen;
1640 private int mSmallIcon;
1641 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001642 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001643 private CharSequence mContentTitle;
1644 private CharSequence mContentText;
1645 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001646 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001647 private PendingIntent mContentIntent;
1648 private RemoteViews mContentView;
1649 private PendingIntent mDeleteIntent;
1650 private PendingIntent mFullScreenIntent;
1651 private CharSequence mTickerText;
1652 private RemoteViews mTickerView;
1653 private Bitmap mLargeIcon;
1654 private Uri mSound;
1655 private int mAudioStreamType;
1656 private long[] mVibrate;
1657 private int mLedArgb;
1658 private int mLedOnMs;
1659 private int mLedOffMs;
1660 private int mDefaults;
1661 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001662 private int mProgressMax;
1663 private int mProgress;
1664 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001665 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001666 private String mGroupKey;
1667 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001668 private Bundle mExtras;
1669 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001670 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001671 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001672 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001673 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001674 private int mVisibility = VISIBILITY_PRIVATE;
1675 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001676 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001677 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001678 private int mColor = COLOR_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001679
Joe Onoratocb109a02011-01-18 17:57:41 -08001680 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001681 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001682 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001683
1684 * <table>
1685 * <tr><th align=right>priority</th>
1686 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1687 * <tr><th align=right>when</th>
1688 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1689 * <tr><th align=right>audio stream</th>
1690 * <td>{@link #STREAM_DEFAULT}</td></tr>
1691 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001692 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001693
1694 * @param context
1695 * A {@link Context} that will be used by the Builder to construct the
1696 * RemoteViews. The Context will not be held past the lifetime of this Builder
1697 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001698 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001699 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001700 /*
1701 * Important compatibility note!
1702 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1703 * constructor for later use. At this point Activities - themselves subclasses of
1704 * ContextWrapper - do not have their inner Context populated yet. This means that
1705 * any calls to Context methods from within this constructor can cause NPEs in existing
1706 * apps. Any data populated from mContext should therefore be populated lazily to
1707 * preserve compatibility.
1708 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001709 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001710
1711 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001712 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001713 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001714 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001715 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001716
Dan Sandler26e81cf2014-05-06 10:01:27 -04001717 mColorUtil = NotificationColorUtil.getInstance();
Joe Onorato46439ce2010-11-19 13:56:21 -08001718 }
1719
Joe Onoratocb109a02011-01-18 17:57:41 -08001720 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001721 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001722 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001723 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001724 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001725 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001726 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001727 public Builder setWhen(long when) {
1728 mWhen = when;
1729 return this;
1730 }
1731
Joe Onoratocb109a02011-01-18 17:57:41 -08001732 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001733 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001734 * in the content view.
1735 */
1736 public Builder setShowWhen(boolean show) {
1737 mShowWhen = show;
1738 return this;
1739 }
1740
1741 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001742 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001743 *
1744 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001745 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001746 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001747 * Useful when showing an elapsed time (like an ongoing phone call).
1748 *
1749 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001750 * @see Notification#when
1751 */
1752 public Builder setUsesChronometer(boolean b) {
1753 mUseChronometer = b;
1754 return this;
1755 }
1756
1757 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001758 * Set the small icon resource, which will be used to represent the notification in the
1759 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001760 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001761
1762 * The platform template for the expanded view will draw this icon in the left, unless a
1763 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1764 * icon will be moved to the right-hand side.
1765 *
1766
1767 * @param icon
1768 * A resource ID in the application's package of the drawable to use.
1769 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001770 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001771 public Builder setSmallIcon(int icon) {
1772 mSmallIcon = icon;
1773 return this;
1774 }
1775
Joe Onoratocb109a02011-01-18 17:57:41 -08001776 /**
1777 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1778 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1779 * LevelListDrawable}.
1780 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001781 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001782 * @param level The level to use for the icon.
1783 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001784 * @see Notification#icon
1785 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001786 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001787 public Builder setSmallIcon(int icon, int level) {
1788 mSmallIcon = icon;
1789 mSmallIconLevel = level;
1790 return this;
1791 }
1792
Joe Onoratocb109a02011-01-18 17:57:41 -08001793 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001794 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001795 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001796 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001797 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001798 return this;
1799 }
1800
Joe Onoratocb109a02011-01-18 17:57:41 -08001801 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001802 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001803 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001804 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001805 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001806 return this;
1807 }
1808
Joe Onoratocb109a02011-01-18 17:57:41 -08001809 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001810 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001811 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1812 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001813 */
1814 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001815 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001816 return this;
1817 }
1818
1819 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001820 * Set the large number at the right-hand side of the notification. This is
1821 * equivalent to setContentInfo, although it might show the number in a different
1822 * font size for readability.
1823 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001824 public Builder setNumber(int number) {
1825 mNumber = number;
1826 return this;
1827 }
1828
Joe Onoratocb109a02011-01-18 17:57:41 -08001829 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001830 * A small piece of additional information pertaining to this notification.
1831 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001832 * The platform template will draw this on the last line of the notification, at the far
1833 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001834 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001835 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001836 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001837 return this;
1838 }
1839
Joe Onoratocb109a02011-01-18 17:57:41 -08001840 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001841 * Set the progress this notification represents.
1842 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001843 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001844 */
1845 public Builder setProgress(int max, int progress, boolean indeterminate) {
1846 mProgressMax = max;
1847 mProgress = progress;
1848 mProgressIndeterminate = indeterminate;
1849 return this;
1850 }
1851
1852 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001853 * Supply a custom RemoteViews to use instead of the platform template.
1854 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001855 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001856 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001857 public Builder setContent(RemoteViews views) {
1858 mContentView = views;
1859 return this;
1860 }
1861
Joe Onoratocb109a02011-01-18 17:57:41 -08001862 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001863 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1864 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001865 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1866 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1867 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001868 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001869 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001870 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001871 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001872 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001873 public Builder setContentIntent(PendingIntent intent) {
1874 mContentIntent = intent;
1875 return this;
1876 }
1877
Joe Onoratocb109a02011-01-18 17:57:41 -08001878 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001879 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1880 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001881 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001882 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001883 public Builder setDeleteIntent(PendingIntent intent) {
1884 mDeleteIntent = intent;
1885 return this;
1886 }
1887
Joe Onoratocb109a02011-01-18 17:57:41 -08001888 /**
1889 * An intent to launch instead of posting the notification to the status bar.
1890 * Only for use with extremely high-priority notifications demanding the user's
1891 * <strong>immediate</strong> attention, such as an incoming phone call or
1892 * alarm clock that the user has explicitly set to a particular time.
1893 * If this facility is used for something else, please give the user an option
1894 * to turn it off and use a normal notification, as this can be extremely
1895 * disruptive.
1896 *
1897 * @param intent The pending intent to launch.
1898 * @param highPriority Passing true will cause this notification to be sent
1899 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001900 *
1901 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001902 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001903 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1904 mFullScreenIntent = intent;
1905 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1906 return this;
1907 }
1908
Joe Onoratocb109a02011-01-18 17:57:41 -08001909 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001910 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001911 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001912 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001913 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001914 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001915 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001916 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001917 return this;
1918 }
1919
Joe Onoratocb109a02011-01-18 17:57:41 -08001920 /**
1921 * Set the text that is displayed in the status bar when the notification first
1922 * arrives, and also a RemoteViews object that may be displayed instead on some
1923 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001924 *
1925 * @see Notification#tickerText
1926 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001927 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001928 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001929 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001930 mTickerView = views;
1931 return this;
1932 }
1933
Joe Onoratocb109a02011-01-18 17:57:41 -08001934 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001935 * Add a large icon to the notification (and the ticker on some devices).
1936 *
1937 * In the platform template, this image will be shown on the left of the notification view
1938 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1939 *
1940 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001941 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001942 public Builder setLargeIcon(Bitmap icon) {
1943 mLargeIcon = icon;
1944 return this;
1945 }
1946
Joe Onoratocb109a02011-01-18 17:57:41 -08001947 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001948 * Set the sound to play.
1949 *
1950 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1951 *
1952 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001953 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001954 public Builder setSound(Uri sound) {
1955 mSound = sound;
1956 mAudioStreamType = STREAM_DEFAULT;
1957 return this;
1958 }
1959
Joe Onoratocb109a02011-01-18 17:57:41 -08001960 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001961 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001962 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001963 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1964 *
1965 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001966 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001967 public Builder setSound(Uri sound, int streamType) {
1968 mSound = sound;
1969 mAudioStreamType = streamType;
1970 return this;
1971 }
1972
Joe Onoratocb109a02011-01-18 17:57:41 -08001973 /**
1974 * Set the vibration pattern to use.
1975 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001976
1977 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1978 * <code>pattern</code> parameter.
1979 *
1980
1981 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001982 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001983 public Builder setVibrate(long[] pattern) {
1984 mVibrate = pattern;
1985 return this;
1986 }
1987
Joe Onoratocb109a02011-01-18 17:57:41 -08001988 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001989 * Set the desired color for the indicator LED on the device, as well as the
1990 * blink duty cycle (specified in milliseconds).
1991 *
1992
1993 * Not all devices will honor all (or even any) of these values.
1994 *
1995
1996 * @see Notification#ledARGB
1997 * @see Notification#ledOnMS
1998 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001999 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002000 public Builder setLights(int argb, int onMs, int offMs) {
2001 mLedArgb = argb;
2002 mLedOnMs = onMs;
2003 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002004 return this;
2005 }
2006
Joe Onoratocb109a02011-01-18 17:57:41 -08002007 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002008 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002009 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002010
2011 * Ongoing notifications cannot be dismissed by the user, so your application or service
2012 * must take care of canceling them.
2013 *
2014
2015 * They are typically used to indicate a background task that the user is actively engaged
2016 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2017 * (e.g., a file download, sync operation, active network connection).
2018 *
2019
2020 * @see Notification#FLAG_ONGOING_EVENT
2021 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002022 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002023 public Builder setOngoing(boolean ongoing) {
2024 setFlag(FLAG_ONGOING_EVENT, ongoing);
2025 return this;
2026 }
2027
Joe Onoratocb109a02011-01-18 17:57:41 -08002028 /**
2029 * Set this flag if you would only like the sound, vibrate
2030 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002031 *
2032 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002033 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002034 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2035 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2036 return this;
2037 }
2038
Joe Onoratocb109a02011-01-18 17:57:41 -08002039 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002040 * Make this notification automatically dismissed when the user touches it. The
2041 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2042 *
2043 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002044 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002045 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002046 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002047 return this;
2048 }
2049
Joe Onoratocb109a02011-01-18 17:57:41 -08002050 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002051 * Set whether or not this notification should not bridge to other devices.
2052 *
2053 * <p>Some notifications can be bridged to other devices for remote display.
2054 * This hint can be set to recommend this notification not be bridged.
2055 */
2056 public Builder setLocalOnly(boolean localOnly) {
2057 setFlag(FLAG_LOCAL_ONLY, localOnly);
2058 return this;
2059 }
2060
2061 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002062 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002063 * <p>
2064 * The value should be one or more of the following fields combined with
2065 * bitwise-or:
2066 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2067 * <p>
2068 * For all default values, use {@link #DEFAULT_ALL}.
2069 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002070 public Builder setDefaults(int defaults) {
2071 mDefaults = defaults;
2072 return this;
2073 }
2074
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002075 /**
2076 * Set the priority of this notification.
2077 *
2078 * @see Notification#priority
2079 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002080 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002081 mPriority = pri;
2082 return this;
2083 }
Joe Malin8d40d042012-11-05 11:36:40 -08002084
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002085 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002086 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002087 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002088 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002090 public Builder setCategory(String category) {
2091 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092 return this;
2093 }
2094
2095 /**
Chris Wrendde75302014-03-26 17:24:15 -04002096 * Add a person that is relevant to this notification.
2097 *
2098 * @see Notification#EXTRA_PEOPLE
2099 */
2100 public Builder addPerson(String handle) {
2101 mPeople.add(handle);
2102 return this;
2103 }
2104
2105 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002106 * Set this notification to be part of a group of notifications sharing the same key.
2107 * Grouped notifications may display in a cluster or stack on devices which
2108 * support such rendering.
2109 *
2110 * <p>To make this notification the summary for its group, also call
2111 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2112 * {@link #setSortKey}.
2113 * @param groupKey The group key of the group.
2114 * @return this object for method chaining
2115 */
2116 public Builder setGroup(String groupKey) {
2117 mGroupKey = groupKey;
2118 return this;
2119 }
2120
2121 /**
2122 * Set this notification to be the group summary for a group of notifications.
2123 * Grouped notifications may display in a cluster or stack on devices which
2124 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2125 * @param isGroupSummary Whether this notification should be a group summary.
2126 * @return this object for method chaining
2127 */
2128 public Builder setGroupSummary(boolean isGroupSummary) {
2129 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2130 return this;
2131 }
2132
2133 /**
2134 * Set a sort key that orders this notification among other notifications from the
2135 * same package. This can be useful if an external sort was already applied and an app
2136 * would like to preserve this. Notifications will be sorted lexicographically using this
2137 * value, although providing different priorities in addition to providing sort key may
2138 * cause this value to be ignored.
2139 *
2140 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002141 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002142 *
2143 * @see String#compareTo(String)
2144 */
2145 public Builder setSortKey(String sortKey) {
2146 mSortKey = sortKey;
2147 return this;
2148 }
2149
2150 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002151 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002152 *
Griff Hazen720042b2014-02-24 15:46:56 -08002153 * <p>Values within the Bundle will replace existing extras values in this Builder.
2154 *
2155 * @see Notification#extras
2156 */
Griff Hazen959591e2014-05-15 22:26:18 -07002157 public Builder addExtras(Bundle extras) {
2158 if (extras != null) {
2159 if (mExtras == null) {
2160 mExtras = new Bundle(extras);
2161 } else {
2162 mExtras.putAll(extras);
2163 }
Griff Hazen720042b2014-02-24 15:46:56 -08002164 }
2165 return this;
2166 }
2167
2168 /**
2169 * Set metadata for this notification.
2170 *
2171 * <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 -04002172 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002173 * called.
2174 *
Griff Hazen720042b2014-02-24 15:46:56 -08002175 * <p>Replaces any existing extras values with those from the provided Bundle.
2176 * Use {@link #addExtras} to merge in metadata instead.
2177 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002178 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002179 */
Griff Hazen959591e2014-05-15 22:26:18 -07002180 public Builder setExtras(Bundle extras) {
2181 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002182 return this;
2183 }
2184
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002185 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002186 * Get the current metadata Bundle used by this notification Builder.
2187 *
2188 * <p>The returned Bundle is shared with this Builder.
2189 *
2190 * <p>The current contents of this Bundle are copied into the Notification each time
2191 * {@link #build()} is called.
2192 *
2193 * @see Notification#extras
2194 */
2195 public Bundle getExtras() {
2196 if (mExtras == null) {
2197 mExtras = new Bundle();
2198 }
2199 return mExtras;
2200 }
2201
2202 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002203 * Add an action to this notification. Actions are typically displayed by
2204 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002205 * <p>
2206 * Every action must have an icon (32dp square and matching the
2207 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2208 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2209 * <p>
2210 * A notification in its expanded form can display up to 3 actions, from left to right in
2211 * the order they were added. Actions will not be displayed when the notification is
2212 * collapsed, however, so be sure that any essential functions may be accessed by the user
2213 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002214 *
2215 * @param icon Resource ID of a drawable that represents the action.
2216 * @param title Text describing the action.
2217 * @param intent PendingIntent to be fired when the action is invoked.
2218 */
2219 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002220 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002221 return this;
2222 }
2223
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002224 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002225 * Add an action to this notification. Actions are typically displayed by
2226 * the system as a button adjacent to the notification content.
2227 * <p>
2228 * Every action must have an icon (32dp square and matching the
2229 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2230 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2231 * <p>
2232 * A notification in its expanded form can display up to 3 actions, from left to right in
2233 * the order they were added. Actions will not be displayed when the notification is
2234 * collapsed, however, so be sure that any essential functions may be accessed by the user
2235 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2236 *
2237 * @param action The action to add.
2238 */
2239 public Builder addAction(Action action) {
2240 mActions.add(action);
2241 return this;
2242 }
2243
2244 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002245 * Add a rich notification style to be applied at build time.
2246 *
2247 * @param style Object responsible for modifying the notification style.
2248 */
2249 public Builder setStyle(Style style) {
2250 if (mStyle != style) {
2251 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002252 if (mStyle != null) {
2253 mStyle.setBuilder(this);
2254 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002255 }
2256 return this;
2257 }
2258
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002259 /**
2260 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002261 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002262 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2263 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2264 *
2265 * @return The same Builder.
2266 */
2267 public Builder setVisibility(int visibility) {
2268 mVisibility = visibility;
2269 return this;
2270 }
2271
2272 /**
2273 * Supply a replacement Notification whose contents should be shown in insecure contexts
2274 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2275 * @param n A replacement notification, presumably with some or all info redacted.
2276 * @return The same Builder.
2277 */
2278 public Builder setPublicVersion(Notification n) {
2279 mPublicVersion = n;
2280 return this;
2281 }
2282
Griff Hazenb720abe2014-05-20 13:15:30 -07002283 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002284 * Apply an extender to this notification builder. Extenders may be used to add
2285 * metadata or change options on this builder.
2286 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002287 public Builder extend(Extender extender) {
2288 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002289 return this;
2290 }
2291
Joe Onorato46439ce2010-11-19 13:56:21 -08002292 private void setFlag(int mask, boolean value) {
2293 if (value) {
2294 mFlags |= mask;
2295 } else {
2296 mFlags &= ~mask;
2297 }
2298 }
2299
Dan Sandler26e81cf2014-05-06 10:01:27 -04002300 /**
2301 * Sets {@link Notification#color}.
2302 *
2303 * @param argb The accent color to use
2304 *
2305 * @return The same Builder.
2306 */
2307 public Builder setColor(int argb) {
2308 mColor = argb;
2309 return this;
2310 }
2311
Kenny Guy8a0101b2014-05-08 23:34:12 +01002312 private Bitmap getProfileBadge() {
2313 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
2314 Drawable badge = userManager.getBadgeForUser(android.os.Process.myUserHandle());
2315 if (badge == null) {
2316 return null;
2317 }
2318 final int width = badge.getIntrinsicWidth();
2319 final int height = badge.getIntrinsicHeight();
2320 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2321 Canvas canvas = new Canvas(bitmap);
2322 badge.setBounds(0, 0, width, height);
2323 badge.draw(canvas);
2324 return bitmap;
2325 }
2326
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002327 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Kenny Guy8a0101b2014-05-08 23:34:12 +01002328 Bitmap profileIcon = getProfileBadge();
Joe Onorato561d3852010-11-20 18:09:34 -08002329 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002330 boolean showLine3 = false;
2331 boolean showLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002332
Dan Sandler26e81cf2014-05-06 10:01:27 -04002333 if (mPriority < PRIORITY_LOW) {
2334 // TODO: Low priority presentation
Daniel Sandlere95658c2012-05-10 00:33:54 -04002335 }
Kenny Guy8a0101b2014-05-08 23:34:12 +01002336 if (profileIcon != null) {
2337 contentView.setImageViewBitmap(R.id.profile_icon, profileIcon);
2338 contentView.setViewVisibility(R.id.profile_icon, View.VISIBLE);
2339 } else {
2340 contentView.setViewVisibility(R.id.profile_icon, View.GONE);
2341 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002342 if (mLargeIcon != null) {
2343 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002344 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002345 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2346 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2347 processSmallRightIcon(mSmallIcon, contentView);
2348 } else { // small icon at left
2349 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2350 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2351 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002352 }
2353 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002354 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002355 }
2356 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002357 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002358 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002359 }
2360 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002361 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002362 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002363 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002364 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002365 final int tooBig = mContext.getResources().getInteger(
2366 R.integer.status_bar_notification_info_maxnum);
2367 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002368 contentView.setTextViewText(R.id.info, processLegacyText(
2369 mContext.getResources().getString(
2370 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002371 } else {
2372 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002373 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002374 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002375 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002376 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002377 } else {
2378 contentView.setViewVisibility(R.id.info, View.GONE);
2379 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002380
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002381 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002382 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002383 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002384 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002385 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002386 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2387 showLine2 = true;
2388 } else {
2389 contentView.setViewVisibility(R.id.text2, View.GONE);
2390 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002391 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002392 contentView.setViewVisibility(R.id.text2, View.GONE);
2393 if (mProgressMax != 0 || mProgressIndeterminate) {
2394 contentView.setProgressBar(
2395 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2396 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002397 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002398 } else {
2399 contentView.setViewVisibility(R.id.progress, View.GONE);
2400 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002401 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002402 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002403 if (fitIn1U) {
2404 // need to shrink all the type to make sure everything fits
2405 final Resources res = mContext.getResources();
2406 final float subTextSize = res.getDimensionPixelSize(
2407 R.dimen.notification_subtext_size);
2408 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2409 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002410 // vertical centering
2411 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2412 }
2413
Daniel Sandler0c890492012-09-12 17:23:10 -07002414 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002415 if (mUseChronometer) {
2416 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2417 contentView.setLong(R.id.chronometer, "setBase",
2418 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2419 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2420 } else {
2421 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2422 contentView.setLong(R.id.time, "setTime", mWhen);
2423 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002424 } else {
2425 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002426 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002427
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002428 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002429 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002430 return contentView;
2431 }
2432
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002433 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002434 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002435
2436 int N = mActions.size();
2437 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002438 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002439 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002440 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002441 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002442 for (int i=0; i<N; i++) {
2443 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002444 big.addView(R.id.actions, button);
2445 }
2446 }
2447 return big;
2448 }
2449
Joe Onorato46439ce2010-11-19 13:56:21 -08002450 private RemoteViews makeContentView() {
2451 if (mContentView != null) {
2452 return mContentView;
2453 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002454 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002455 }
2456 }
2457
2458 private RemoteViews makeTickerView() {
2459 if (mTickerView != null) {
2460 return mTickerView;
2461 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08002462 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002463 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08002464 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002465 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08002466 } else {
2467 return null;
2468 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002469 }
2470 }
2471
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002472 private RemoteViews makeBigContentView() {
2473 if (mActions.size() == 0) return null;
2474
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002475 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002476 }
2477
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002478 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002479 if (mActions.size() == 0) return null;
2480
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002481 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002482 }
2483
2484
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002485 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002486 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002487 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002488 tombstone ? getActionTombstoneLayoutResource()
2489 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002490 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002491 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002492 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002493 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002494 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002495 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002496 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002497 return button;
2498 }
2499
Joe Onoratocb109a02011-01-18 17:57:41 -08002500 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002501 * @return Whether we are currently building a notification from a legacy (an app that
2502 * doesn't create quantum notifications by itself) app.
2503 */
2504 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002505 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002506 }
2507
2508 private void processLegacyAction(Action action, RemoteViews button) {
2509 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002510 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002511 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2512 mContext.getResources().getColor(
2513 R.color.notification_action_legacy_color_filter),
2514 PorterDuff.Mode.MULTIPLY);
2515 }
2516 }
2517 }
2518
2519 private CharSequence processLegacyText(CharSequence charSequence) {
2520 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002521 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002522 } else {
2523 return charSequence;
2524 }
2525 }
2526
Dan Sandler26e81cf2014-05-06 10:01:27 -04002527 /**
2528 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2529 */
2530 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2531 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2532 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002533 }
2534 }
2535
Dan Sandler26e81cf2014-05-06 10:01:27 -04002536 /**
2537 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2538 * if it's grayscale).
2539 */
2540 // TODO: also check bounds, transparency, that sort of thing.
2541 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2542 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2543 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002544 } else {
2545 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002546 }
2547 }
2548
Dan Sandler26e81cf2014-05-06 10:01:27 -04002549 /**
2550 * Add a colored circle behind the largeIcon slot.
2551 */
2552 private void applyLargeIconBackground(RemoteViews contentView) {
2553 contentView.setInt(R.id.icon, "setBackgroundResource",
2554 R.drawable.notification_icon_legacy_bg_inset);
2555
2556 contentView.setDrawableParameters(
2557 R.id.icon,
2558 true,
2559 -1,
2560 mColor,
2561 PorterDuff.Mode.SRC_ATOP,
2562 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002563 }
2564
Dan Sandler190d58d2014-05-15 09:33:39 -04002565 private void removeLargeIconBackground(RemoteViews contentView) {
2566 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2567 }
2568
Dan Sandler26e81cf2014-05-06 10:01:27 -04002569 /**
2570 * Recolor small icons when used in the R.id.right_icon slot.
2571 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002572 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002573 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002574 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002575 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2576 0xFFFFFFFF,
2577 PorterDuff.Mode.SRC_ATOP, -1);
2578
2579 contentView.setInt(R.id.right_icon,
2580 "setBackgroundResource",
2581 R.drawable.notification_icon_legacy_bg);
2582
2583 contentView.setDrawableParameters(
2584 R.id.right_icon,
2585 true,
2586 -1,
2587 mColor,
2588 PorterDuff.Mode.SRC_ATOP,
2589 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002590 }
2591 }
2592
Dan Sandler26e81cf2014-05-06 10:01:27 -04002593 private int resolveColor() {
2594 if (mColor == COLOR_DEFAULT) {
2595 mColor = mContext.getResources().getColor(R.color.notification_icon_bg_color);
2596 } else {
2597 mColor |= 0xFF000000; // no alpha for custom colors
2598 }
2599 return mColor;
2600 }
2601
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002602 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002603 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002604 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002606 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002607 Notification n = new Notification();
2608 n.when = mWhen;
2609 n.icon = mSmallIcon;
2610 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002611 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002612
2613 n.color = resolveColor();
2614
Joe Onorato46439ce2010-11-19 13:56:21 -08002615 n.contentView = makeContentView();
2616 n.contentIntent = mContentIntent;
2617 n.deleteIntent = mDeleteIntent;
2618 n.fullScreenIntent = mFullScreenIntent;
2619 n.tickerText = mTickerText;
2620 n.tickerView = makeTickerView();
2621 n.largeIcon = mLargeIcon;
2622 n.sound = mSound;
2623 n.audioStreamType = mAudioStreamType;
2624 n.vibrate = mVibrate;
2625 n.ledARGB = mLedArgb;
2626 n.ledOnMS = mLedOnMs;
2627 n.ledOffMS = mLedOffMs;
2628 n.defaults = mDefaults;
2629 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002630 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002631 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002632 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002633 n.flags |= FLAG_SHOW_LIGHTS;
2634 }
2635 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2636 n.flags |= FLAG_SHOW_LIGHTS;
2637 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002638 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002639 n.mGroupKey = mGroupKey;
2640 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002641 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002642 if (mActions.size() > 0) {
2643 n.actions = new Action[mActions.size()];
2644 mActions.toArray(n.actions);
2645 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002646 n.visibility = mVisibility;
2647
2648 if (mPublicVersion != null) {
2649 n.publicVersion = new Notification();
2650 mPublicVersion.cloneInto(n.publicVersion, true);
2651 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002652
Joe Onorato46439ce2010-11-19 13:56:21 -08002653 return n;
2654 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002655
2656 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002657 * Capture, in the provided bundle, semantic information used in the construction of
2658 * this Notification object.
2659 * @hide
2660 */
Griff Hazen720042b2014-02-24 15:46:56 -08002661 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002662 // Store original information used in the construction of this object
2663 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2664 extras.putCharSequence(EXTRA_TEXT, mContentText);
2665 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2666 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2667 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2668 extras.putInt(EXTRA_PROGRESS, mProgress);
2669 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2670 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2671 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2672 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002673 if (mLargeIcon != null) {
2674 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2675 }
Chris Wrendde75302014-03-26 17:24:15 -04002676 if (!mPeople.isEmpty()) {
2677 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2678 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002679 }
2680
2681 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002682 * @deprecated Use {@link #build()} instead.
2683 */
2684 @Deprecated
2685 public Notification getNotification() {
2686 return build();
2687 }
2688
2689 /**
2690 * Combine all of the options that have been set and return a new {@link Notification}
2691 * object.
2692 */
2693 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002694 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002695
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002696 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002697 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002698 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002699
2700 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2701
Griff Hazen720042b2014-02-24 15:46:56 -08002702 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002703 if (mStyle != null) {
2704 mStyle.addExtras(n.extras);
2705 }
2706
2707 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002708 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002709
2710 /**
2711 * Apply this Builder to an existing {@link Notification} object.
2712 *
2713 * @hide
2714 */
2715 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002716 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002717 return n;
2718 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002719
2720
2721 private int getBaseLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002722 return R.layout.notification_template_quantum_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002723 }
2724
2725 private int getBigBaseLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002726 return R.layout.notification_template_quantum_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002727 }
2728
2729 private int getBigPictureLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002730 return R.layout.notification_template_quantum_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002731 }
2732
2733 private int getBigTextLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002734 return R.layout.notification_template_quantum_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002735 }
2736
2737 private int getInboxLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002738 return R.layout.notification_template_quantum_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002739 }
2740
2741 private int getActionLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002742 return R.layout.notification_quantum_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002743 }
2744
2745 private int getActionTombstoneLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002746 return R.layout.notification_quantum_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002747 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002748 }
2749
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002750 /**
2751 * An object that can apply a rich notification style to a {@link Notification.Builder}
2752 * object.
2753 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002754 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002755 private CharSequence mBigContentTitle;
2756 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002757 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002758
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002759 protected Builder mBuilder;
2760
Chris Wrend6297db2012-05-03 16:20:13 -04002761 /**
2762 * Overrides ContentTitle in the big form of the template.
2763 * This defaults to the value passed to setContentTitle().
2764 */
2765 protected void internalSetBigContentTitle(CharSequence title) {
2766 mBigContentTitle = title;
2767 }
2768
2769 /**
2770 * Set the first line of text after the detail section in the big form of the template.
2771 */
2772 protected void internalSetSummaryText(CharSequence cs) {
2773 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002774 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002775 }
2776
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002777 public void setBuilder(Builder builder) {
2778 if (mBuilder != builder) {
2779 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002780 if (mBuilder != null) {
2781 mBuilder.setStyle(this);
2782 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002783 }
2784 }
2785
Chris Wrend6297db2012-05-03 16:20:13 -04002786 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002787 if (mBuilder == null) {
2788 throw new IllegalArgumentException("Style requires a valid Builder object");
2789 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002790 }
Chris Wrend6297db2012-05-03 16:20:13 -04002791
2792 protected RemoteViews getStandardView(int layoutId) {
2793 checkBuilder();
2794
2795 if (mBigContentTitle != null) {
2796 mBuilder.setContentTitle(mBigContentTitle);
2797 }
2798
Chris Wrend6297db2012-05-03 16:20:13 -04002799 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2800
Chris Wrend6297db2012-05-03 16:20:13 -04002801 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2802 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002803 } else {
2804 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002805 }
2806
Daniel Sandler619738c2012-06-07 16:33:08 -04002807 // The last line defaults to the subtext, but can be replaced by mSummaryText
2808 final CharSequence overflowText =
2809 mSummaryTextSet ? mSummaryText
2810 : mBuilder.mSubText;
2811 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002812 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04002813 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002814 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002815 } else {
2816 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2817 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002818 }
2819
2820 return contentView;
2821 }
2822
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002823 /**
2824 * @hide
2825 */
2826 public void addExtras(Bundle extras) {
2827 if (mSummaryTextSet) {
2828 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2829 }
2830 if (mBigContentTitle != null) {
2831 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2832 }
Chris Wren91ad5632013-06-05 15:05:57 -04002833 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002834 }
2835
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002836 /**
2837 * @hide
2838 */
2839 public abstract Notification buildStyled(Notification wip);
2840
2841 /**
2842 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2843 * attached to.
2844 *
2845 * @return the fully constructed Notification.
2846 */
2847 public Notification build() {
2848 checkBuilder();
2849 return mBuilder.build();
2850 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002851 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002852
2853 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002854 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002855 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002856 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002857 * <pre class="prettyprint">
2858 * Notification noti = new Notification.BigPictureStyle(
2859 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002860 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002861 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002862 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002863 * .setLargeIcon(aBitmap))
2864 * .bigPicture(aBigBitmap)
2865 * .build();
2866 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002867 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002868 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002869 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002870 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002871 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002872 private Bitmap mBigLargeIcon;
2873 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002874
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002875 public BigPictureStyle() {
2876 }
2877
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002878 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002879 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002880 }
2881
Chris Wrend6297db2012-05-03 16:20:13 -04002882 /**
2883 * Overrides ContentTitle in the big form of the template.
2884 * This defaults to the value passed to setContentTitle().
2885 */
2886 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002887 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002888 return this;
2889 }
2890
2891 /**
2892 * Set the first line of text after the detail section in the big form of the template.
2893 */
2894 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002895 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002896 return this;
2897 }
2898
Chris Wren0bd664d2012-08-01 13:56:56 -04002899 /**
2900 * Provide the bitmap to be used as the payload for the BigPicture notification.
2901 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002902 public BigPictureStyle bigPicture(Bitmap b) {
2903 mPicture = b;
2904 return this;
2905 }
2906
Chris Wren3745a3d2012-05-22 15:11:52 -04002907 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002908 * Override the large icon when the big notification is shown.
2909 */
2910 public BigPictureStyle bigLargeIcon(Bitmap b) {
2911 mBigLargeIconSet = true;
2912 mBigLargeIcon = b;
2913 return this;
2914 }
2915
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002916 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002917 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002918
2919 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2920
2921 return contentView;
2922 }
2923
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002924 /**
2925 * @hide
2926 */
2927 public void addExtras(Bundle extras) {
2928 super.addExtras(extras);
2929
2930 if (mBigLargeIconSet) {
2931 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2932 }
2933 extras.putParcelable(EXTRA_PICTURE, mPicture);
2934 }
2935
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002936 /**
2937 * @hide
2938 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002939 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002940 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002941 if (mBigLargeIconSet ) {
2942 mBuilder.mLargeIcon = mBigLargeIcon;
2943 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002944 wip.bigContentView = makeBigContentView();
2945 return wip;
2946 }
2947 }
2948
2949 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002950 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002951 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002952 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002953 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05002954 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002955 * new Notification.Builder()
2956 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2957 * .setContentText(subject)
2958 * .setSmallIcon(R.drawable.new_mail)
2959 * .setLargeIcon(aBitmap))
2960 * .bigText(aVeryLongString)
2961 * .build();
2962 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002963 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002964 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002965 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002966 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002967 private CharSequence mBigText;
2968
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002969 public BigTextStyle() {
2970 }
2971
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002972 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002973 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002974 }
2975
Chris Wrend6297db2012-05-03 16:20:13 -04002976 /**
2977 * Overrides ContentTitle in the big form of the template.
2978 * This defaults to the value passed to setContentTitle().
2979 */
2980 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002981 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002982 return this;
2983 }
2984
2985 /**
2986 * Set the first line of text after the detail section in the big form of the template.
2987 */
2988 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002989 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002990 return this;
2991 }
2992
Chris Wren0bd664d2012-08-01 13:56:56 -04002993 /**
2994 * Provide the longer text to be displayed in the big form of the
2995 * template in place of the content text.
2996 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002997 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002998 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002999 return this;
3000 }
3001
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003002 /**
3003 * @hide
3004 */
3005 public void addExtras(Bundle extras) {
3006 super.addExtras(extras);
3007
3008 extras.putCharSequence(EXTRA_TEXT, mBigText);
3009 }
3010
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003011 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003012 // Remove the content text so line3 only shows if you have a summary
3013 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003014 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003015
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003016 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003017
Daniel Sandler619738c2012-06-07 16:33:08 -04003018 if (hadThreeLines) {
3019 // vertical centering
3020 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
3021 }
3022
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003023 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003024 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003025 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003026
3027 return contentView;
3028 }
3029
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003030 /**
3031 * @hide
3032 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003033 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003034 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003035 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003036
3037 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
3038
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003039 return wip;
3040 }
3041 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003042
3043 /**
3044 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003045 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003046 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
3047 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003048 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04003049 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04003050 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04003051 * .setContentText(subject)
3052 * .setSmallIcon(R.drawable.new_mail)
3053 * .setLargeIcon(aBitmap))
3054 * .addLine(str1)
3055 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04003056 * .setContentTitle("")
3057 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04003058 * .build();
3059 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003060 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003061 * @see Notification#bigContentView
3062 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003063 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003064 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3065
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003066 public InboxStyle() {
3067 }
3068
Daniel Sandler879c5e02012-04-17 16:46:51 -04003069 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003070 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003071 }
3072
Chris Wrend6297db2012-05-03 16:20:13 -04003073 /**
3074 * Overrides ContentTitle in the big form of the template.
3075 * This defaults to the value passed to setContentTitle().
3076 */
3077 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003078 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003079 return this;
3080 }
3081
3082 /**
3083 * Set the first line of text after the detail section in the big form of the template.
3084 */
3085 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003086 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003087 return this;
3088 }
3089
Chris Wren0bd664d2012-08-01 13:56:56 -04003090 /**
3091 * Append a line to the digest section of the Inbox notification.
3092 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003093 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003094 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003095 return this;
3096 }
3097
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003098 /**
3099 * @hide
3100 */
3101 public void addExtras(Bundle extras) {
3102 super.addExtras(extras);
3103 CharSequence[] a = new CharSequence[mTexts.size()];
3104 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3105 }
3106
Daniel Sandler879c5e02012-04-17 16:46:51 -04003107 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003108 // Remove the content text so line3 disappears unless you have a summary
3109 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003110 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003111
Chris Wrend6297db2012-05-03 16:20:13 -04003112 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003113
Chris Wrend6297db2012-05-03 16:20:13 -04003114 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 -04003115 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003116
Chris Wren4ed80d52012-05-17 09:30:03 -04003117 // Make sure all rows are gone in case we reuse a view.
3118 for (int rowId : rowIds) {
3119 contentView.setViewVisibility(rowId, View.GONE);
3120 }
3121
Chris Wren683ab002012-09-20 10:35:54 -04003122
Daniel Sandler879c5e02012-04-17 16:46:51 -04003123 int i=0;
3124 while (i < mTexts.size() && i < rowIds.length) {
3125 CharSequence str = mTexts.get(i);
3126 if (str != null && !str.equals("")) {
3127 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003128 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003129 }
3130 i++;
3131 }
3132
Chris Wren683ab002012-09-20 10:35:54 -04003133 contentView.setViewVisibility(R.id.inbox_end_pad,
3134 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3135
3136 contentView.setViewVisibility(R.id.inbox_more,
3137 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003138
Daniel Sandler879c5e02012-04-17 16:46:51 -04003139 return contentView;
3140 }
3141
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003142 /**
3143 * @hide
3144 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003145 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003146 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003147 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003148
Daniel Sandler879c5e02012-04-17 16:46:51 -04003149 return wip;
3150 }
3151 }
Dan Sandler842dd772014-05-15 09:36:47 -04003152
3153 /**
3154 * Notification style for media playback notifications.
3155 *
3156 * In the expanded form, {@link Notification#bigContentView}, up to 5
3157 * {@link Notification.Action}s specified with
3158 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3159 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3160 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3161 * treated as album artwork.
3162 *
3163 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3164 * {@link Notification#contentView}; by providing action indices to
3165 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3166 * in the standard view alongside the usual content.
3167 *
3168 * Finally, if you attach a {@link android.media.session.MediaSessionToken} using
3169 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSessionToken)},
3170 * the System UI can identify this as a notification representing an active media session
3171 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3172 *
3173 * To use this style with your Notification, feed it to
3174 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3175 * <pre class="prettyprint">
3176 * Notification noti = new Notification.Builder()
3177 * .setSmallIcon(R.drawable.ic_stat_player)
3178 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3179 * .setContentText(&quot;Artist - Album&quot;) // if you use
3180 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token, true)
3181 * .setMediaSession(mySession, true)
3182 * .setStyle(<b>new Notification.MediaStyle()</b>)
3183 * .build();
3184 * </pre>
3185 *
3186 * @see Notification#bigContentView
3187 */
3188 public static class MediaStyle extends Style {
3189 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3190 static final int MAX_MEDIA_BUTTONS = 5;
3191
3192 private int[] mActionsToShowInCompact = null;
3193 private MediaSessionToken mToken;
3194
3195 public MediaStyle() {
3196 }
3197
3198 public MediaStyle(Builder builder) {
3199 setBuilder(builder);
3200 }
3201
3202 /**
3203 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3204 * notification view.
3205 */
3206 public MediaStyle setShowActionsInCompactView(int...actions) {
3207 mActionsToShowInCompact = actions;
3208 return this;
3209 }
3210
3211 /**
3212 * Attach a {@link android.media.session.MediaSessionToken} to this Notification to provide
3213 * additional playback information and control to the SystemUI.
3214 */
3215 public MediaStyle setMediaSession(MediaSessionToken token) {
3216 mToken = token;
3217 return this;
3218 }
3219
3220 @Override
3221 public Notification buildStyled(Notification wip) {
3222 wip.contentView = makeMediaContentView();
3223 wip.bigContentView = makeMediaBigContentView();
3224
3225 return wip;
3226 }
3227
3228 /** @hide */
3229 @Override
3230 public void addExtras(Bundle extras) {
3231 super.addExtras(extras);
3232
3233 if (mToken != null) {
3234 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3235 }
3236 }
3237
3238 private RemoteViews generateMediaActionButton(Action action) {
3239 final boolean tombstone = (action.actionIntent == null);
3240 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
3241 R.layout.notification_quantum_media_action);
3242 button.setImageViewResource(R.id.action0, action.icon);
3243 if (!tombstone) {
3244 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3245 }
3246 button.setContentDescription(R.id.action0, action.title);
3247 return button;
3248 }
3249
3250 private RemoteViews makeMediaContentView() {
3251 RemoteViews view = mBuilder.applyStandardTemplate(
3252 R.layout.notification_template_quantum_media, true /* 1U */);
3253
3254 final int numActions = mBuilder.mActions.size();
3255 final int N = mActionsToShowInCompact == null
3256 ? 0
3257 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3258 if (N > 0) {
3259 view.removeAllViews(R.id.actions);
3260 for (int i = 0; i < N; i++) {
3261 if (i >= numActions) {
3262 throw new IllegalArgumentException(String.format(
3263 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3264 i, numActions - 1));
3265 }
3266
3267 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3268 final RemoteViews button = generateMediaActionButton(action);
3269 view.addView(R.id.actions, button);
3270 }
3271 }
3272 return view;
3273 }
3274
3275 private RemoteViews makeMediaBigContentView() {
3276 RemoteViews big = mBuilder.applyStandardTemplate(
3277 R.layout.notification_template_quantum_big_media, false);
3278
3279 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3280 if (N > 0) {
3281 big.removeAllViews(R.id.actions);
3282 for (int i=0; i<N; i++) {
3283 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3284 big.addView(R.id.actions, button);
3285 }
3286 }
3287 return big;
3288 }
3289 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003290
3291 /**
3292 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
3293 * metadata or change options on a notification builder.
3294 */
3295 public interface Extender {
3296 /**
3297 * Apply this extender to a notification builder.
3298 * @param builder the builder to be modified.
3299 * @return the build object for chaining.
3300 */
3301 public Builder extend(Builder builder);
3302 }
3303
3304 /**
3305 * Helper class to add wearable extensions to notifications.
3306 * <p class="note"> See
3307 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
3308 * for Android Wear</a> for more information on how to use this class.
3309 * <p>
3310 * To create a notification with wearable extensions:
3311 * <ol>
3312 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
3313 * properties.
3314 * <li>Create a {@link android.app.Notification.WearableExtender}.
3315 * <li>Set wearable-specific properties using the
3316 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
3317 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
3318 * notification.
3319 * <li>Post the notification to the notification system with the
3320 * {@code NotificationManager.notify(...)} methods.
3321 * </ol>
3322 *
3323 * <pre class="prettyprint">
3324 * Notification notif = new Notification.Builder(mContext)
3325 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3326 * .setContentText(subject)
3327 * .setSmallIcon(R.drawable.new_mail)
3328 * .extend(new Notification.WearableExtender()
3329 * .setContentIcon(R.drawable.new_mail))
3330 * .build();
3331 * NotificationManager notificationManger =
3332 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3333 * notificationManger.notify(0, notif);</pre>
3334 *
3335 * <p>Wearable extensions can be accessed on an existing notification by using the
3336 * {@code WearableExtender(Notification)} constructor,
3337 * and then using the {@code get} methods to access values.
3338 *
3339 * <pre class="prettyprint">
3340 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
3341 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07003342 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003343 */
3344 public static final class WearableExtender implements Extender {
3345 /**
3346 * Sentinel value for an action index that is unset.
3347 */
3348 public static final int UNSET_ACTION_INDEX = -1;
3349
3350 /**
3351 * Size value for use with {@link #setCustomSizePreset} to show this notification with
3352 * default sizing.
3353 * <p>For custom display notifications created using {@link #setDisplayIntent},
3354 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
3355 * on their content.
3356 */
3357 public static final int SIZE_DEFAULT = 0;
3358
3359 /**
3360 * Size value for use with {@link #setCustomSizePreset} to show this notification
3361 * with an extra small size.
3362 * <p>This value is only applicable for custom display notifications created using
3363 * {@link #setDisplayIntent}.
3364 */
3365 public static final int SIZE_XSMALL = 1;
3366
3367 /**
3368 * Size value for use with {@link #setCustomSizePreset} to show this notification
3369 * with a small size.
3370 * <p>This value is only applicable for custom display notifications created using
3371 * {@link #setDisplayIntent}.
3372 */
3373 public static final int SIZE_SMALL = 2;
3374
3375 /**
3376 * Size value for use with {@link #setCustomSizePreset} to show this notification
3377 * with a medium size.
3378 * <p>This value is only applicable for custom display notifications created using
3379 * {@link #setDisplayIntent}.
3380 */
3381 public static final int SIZE_MEDIUM = 3;
3382
3383 /**
3384 * Size value for use with {@link #setCustomSizePreset} to show this notification
3385 * with a large size.
3386 * <p>This value is only applicable for custom display notifications created using
3387 * {@link #setDisplayIntent}.
3388 */
3389 public static final int SIZE_LARGE = 4;
3390
Griff Hazend5f11f92014-05-27 15:40:09 -07003391 /**
3392 * Size value for use with {@link #setCustomSizePreset} to show this notification
3393 * full screen.
3394 * <p>This value is only applicable for custom display notifications created using
3395 * {@link #setDisplayIntent}.
3396 */
3397 public static final int SIZE_FULL_SCREEN = 5;
3398
Griff Hazen61a9e862014-05-22 16:05:19 -07003399 /** Notification extra which contains wearable extensions */
3400 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
3401
3402 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
3403 private static final String KEY_ACTIONS = "actions";
3404 private static final String KEY_FLAGS = "flags";
3405 private static final String KEY_DISPLAY_INTENT = "displayIntent";
3406 private static final String KEY_PAGES = "pages";
3407 private static final String KEY_BACKGROUND = "background";
3408 private static final String KEY_CONTENT_ICON = "contentIcon";
3409 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
3410 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
3411 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
3412 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
3413 private static final String KEY_GRAVITY = "gravity";
3414
3415 // Flags bitwise-ored to mFlags
3416 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
3417 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
3418 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
3419 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
3420
3421 // Default value for flags integer
3422 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
3423
3424 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
3425 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
3426
3427 private ArrayList<Action> mActions = new ArrayList<Action>();
3428 private int mFlags = DEFAULT_FLAGS;
3429 private PendingIntent mDisplayIntent;
3430 private ArrayList<Notification> mPages = new ArrayList<Notification>();
3431 private Bitmap mBackground;
3432 private int mContentIcon;
3433 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
3434 private int mContentActionIndex = UNSET_ACTION_INDEX;
3435 private int mCustomSizePreset = SIZE_DEFAULT;
3436 private int mCustomContentHeight;
3437 private int mGravity = DEFAULT_GRAVITY;
3438
3439 /**
3440 * Create a {@link android.app.Notification.WearableExtender} with default
3441 * options.
3442 */
3443 public WearableExtender() {
3444 }
3445
3446 public WearableExtender(Notification notif) {
3447 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
3448 if (wearableBundle != null) {
3449 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
3450 if (actions != null) {
3451 mActions.addAll(actions);
3452 }
3453
3454 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
3455 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
3456
3457 Notification[] pages = getNotificationArrayFromBundle(
3458 wearableBundle, KEY_PAGES);
3459 if (pages != null) {
3460 Collections.addAll(mPages, pages);
3461 }
3462
3463 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
3464 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
3465 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
3466 DEFAULT_CONTENT_ICON_GRAVITY);
3467 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
3468 UNSET_ACTION_INDEX);
3469 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
3470 SIZE_DEFAULT);
3471 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
3472 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
3473 }
3474 }
3475
3476 /**
3477 * Apply wearable extensions to a notification that is being built. This is typically
3478 * called by the {@link android.app.Notification.Builder#extend} method of
3479 * {@link android.app.Notification.Builder}.
3480 */
3481 @Override
3482 public Notification.Builder extend(Notification.Builder builder) {
3483 Bundle wearableBundle = new Bundle();
3484
3485 if (!mActions.isEmpty()) {
3486 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
3487 }
3488 if (mFlags != DEFAULT_FLAGS) {
3489 wearableBundle.putInt(KEY_FLAGS, mFlags);
3490 }
3491 if (mDisplayIntent != null) {
3492 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
3493 }
3494 if (!mPages.isEmpty()) {
3495 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
3496 new Notification[mPages.size()]));
3497 }
3498 if (mBackground != null) {
3499 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
3500 }
3501 if (mContentIcon != 0) {
3502 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
3503 }
3504 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
3505 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
3506 }
3507 if (mContentActionIndex != UNSET_ACTION_INDEX) {
3508 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
3509 mContentActionIndex);
3510 }
3511 if (mCustomSizePreset != SIZE_DEFAULT) {
3512 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
3513 }
3514 if (mCustomContentHeight != 0) {
3515 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
3516 }
3517 if (mGravity != DEFAULT_GRAVITY) {
3518 wearableBundle.putInt(KEY_GRAVITY, mGravity);
3519 }
3520
3521 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
3522 return builder;
3523 }
3524
3525 @Override
3526 public WearableExtender clone() {
3527 WearableExtender that = new WearableExtender();
3528 that.mActions = new ArrayList<Action>(this.mActions);
3529 that.mFlags = this.mFlags;
3530 that.mDisplayIntent = this.mDisplayIntent;
3531 that.mPages = new ArrayList<Notification>(this.mPages);
3532 that.mBackground = this.mBackground;
3533 that.mContentIcon = this.mContentIcon;
3534 that.mContentIconGravity = this.mContentIconGravity;
3535 that.mContentActionIndex = this.mContentActionIndex;
3536 that.mCustomSizePreset = this.mCustomSizePreset;
3537 that.mCustomContentHeight = this.mCustomContentHeight;
3538 that.mGravity = this.mGravity;
3539 return that;
3540 }
3541
3542 /**
3543 * Add a wearable action to this notification.
3544 *
3545 * <p>When wearable actions are added using this method, the set of actions that
3546 * show on a wearable device splits from devices that only show actions added
3547 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3548 * of which actions display on different devices.
3549 *
3550 * @param action the action to add to this notification
3551 * @return this object for method chaining
3552 * @see android.app.Notification.Action
3553 */
3554 public WearableExtender addAction(Action action) {
3555 mActions.add(action);
3556 return this;
3557 }
3558
3559 /**
3560 * Adds wearable actions to this notification.
3561 *
3562 * <p>When wearable actions are added using this method, the set of actions that
3563 * show on a wearable device splits from devices that only show actions added
3564 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3565 * of which actions display on different devices.
3566 *
3567 * @param actions the actions to add to this notification
3568 * @return this object for method chaining
3569 * @see android.app.Notification.Action
3570 */
3571 public WearableExtender addActions(List<Action> actions) {
3572 mActions.addAll(actions);
3573 return this;
3574 }
3575
3576 /**
3577 * Clear all wearable actions present on this builder.
3578 * @return this object for method chaining.
3579 * @see #addAction
3580 */
3581 public WearableExtender clearActions() {
3582 mActions.clear();
3583 return this;
3584 }
3585
3586 /**
3587 * Get the wearable actions present on this notification.
3588 */
3589 public List<Action> getActions() {
3590 return mActions;
3591 }
3592
3593 /**
3594 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07003595 * this notification. The {@link PendingIntent} provided should be for an activity.
3596 *
3597 * <pre class="prettyprint">
3598 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
3599 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
3600 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3601 * Notification notif = new Notification.Builder(context)
3602 * .extend(new Notification.WearableExtender()
3603 * .setDisplayIntent(displayPendingIntent)
3604 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
3605 * .build();</pre>
3606 *
3607 * <p>The activity to launch needs to allow embedding, must be exported, and
3608 * should have an empty task affinity.
3609 *
3610 * <p>Example AndroidManifest.xml entry:
3611 * <pre class="prettyprint">
3612 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
3613 * android:exported=&quot;true&quot;
3614 * android:allowEmbedded=&quot;true&quot;
3615 * android:taskAffinity=&quot;&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003616 *
3617 * @param intent the {@link PendingIntent} for an activity
3618 * @return this object for method chaining
3619 * @see android.app.Notification.WearableExtender#getDisplayIntent
3620 */
3621 public WearableExtender setDisplayIntent(PendingIntent intent) {
3622 mDisplayIntent = intent;
3623 return this;
3624 }
3625
3626 /**
3627 * Get the intent to launch inside of an activity view when displaying this
3628 * notification. This {@code PendingIntent} should be for an activity.
3629 */
3630 public PendingIntent getDisplayIntent() {
3631 return mDisplayIntent;
3632 }
3633
3634 /**
3635 * Add an additional page of content to display with this notification. The current
3636 * notification forms the first page, and pages added using this function form
3637 * subsequent pages. This field can be used to separate a notification into multiple
3638 * sections.
3639 *
3640 * @param page the notification to add as another page
3641 * @return this object for method chaining
3642 * @see android.app.Notification.WearableExtender#getPages
3643 */
3644 public WearableExtender addPage(Notification page) {
3645 mPages.add(page);
3646 return this;
3647 }
3648
3649 /**
3650 * Add additional pages of content to display with this notification. The current
3651 * notification forms the first page, and pages added using this function form
3652 * subsequent pages. This field can be used to separate a notification into multiple
3653 * sections.
3654 *
3655 * @param pages a list of notifications
3656 * @return this object for method chaining
3657 * @see android.app.Notification.WearableExtender#getPages
3658 */
3659 public WearableExtender addPages(List<Notification> pages) {
3660 mPages.addAll(pages);
3661 return this;
3662 }
3663
3664 /**
3665 * Clear all additional pages present on this builder.
3666 * @return this object for method chaining.
3667 * @see #addPage
3668 */
3669 public WearableExtender clearPages() {
3670 mPages.clear();
3671 return this;
3672 }
3673
3674 /**
3675 * Get the array of additional pages of content for displaying this notification. The
3676 * current notification forms the first page, and elements within this array form
3677 * subsequent pages. This field can be used to separate a notification into multiple
3678 * sections.
3679 * @return the pages for this notification
3680 */
3681 public List<Notification> getPages() {
3682 return mPages;
3683 }
3684
3685 /**
3686 * Set a background image to be displayed behind the notification content.
3687 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3688 * will work with any notification style.
3689 *
3690 * @param background the background bitmap
3691 * @return this object for method chaining
3692 * @see android.app.Notification.WearableExtender#getBackground
3693 */
3694 public WearableExtender setBackground(Bitmap background) {
3695 mBackground = background;
3696 return this;
3697 }
3698
3699 /**
3700 * Get a background image to be displayed behind the notification content.
3701 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3702 * will work with any notification style.
3703 *
3704 * @return the background image
3705 * @see android.app.Notification.WearableExtender#setBackground
3706 */
3707 public Bitmap getBackground() {
3708 return mBackground;
3709 }
3710
3711 /**
3712 * Set an icon that goes with the content of this notification.
3713 */
3714 public WearableExtender setContentIcon(int icon) {
3715 mContentIcon = icon;
3716 return this;
3717 }
3718
3719 /**
3720 * Get an icon that goes with the content of this notification.
3721 */
3722 public int getContentIcon() {
3723 return mContentIcon;
3724 }
3725
3726 /**
3727 * Set the gravity that the content icon should have within the notification display.
3728 * Supported values include {@link android.view.Gravity#START} and
3729 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3730 * @see #setContentIcon
3731 */
3732 public WearableExtender setContentIconGravity(int contentIconGravity) {
3733 mContentIconGravity = contentIconGravity;
3734 return this;
3735 }
3736
3737 /**
3738 * Get the gravity that the content icon should have within the notification display.
3739 * Supported values include {@link android.view.Gravity#START} and
3740 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3741 * @see #getContentIcon
3742 */
3743 public int getContentIconGravity() {
3744 return mContentIconGravity;
3745 }
3746
3747 /**
3748 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07003749 * this notification. This action will no longer display separately from the
3750 * notification's content.
3751 *
Griff Hazenca48d352014-05-28 22:37:13 -07003752 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003753 * set, although the list of available actions comes from the main notification and not
3754 * from the child page's notification.
3755 *
3756 * @param actionIndex The index of the action to hoist onto the current notification page.
3757 * If wearable actions were added to the main notification, this index
3758 * will apply to that list, otherwise it will apply to the regular
3759 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07003760 */
3761 public WearableExtender setContentAction(int actionIndex) {
3762 mContentActionIndex = actionIndex;
3763 return this;
3764 }
3765
3766 /**
Griff Hazenca48d352014-05-28 22:37:13 -07003767 * Get the index of the notification action, if any, that was specified as being clickable
3768 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07003769 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07003770 *
Griff Hazenca48d352014-05-28 22:37:13 -07003771 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003772 * set, although the list of available actions comes from the main notification and not
3773 * from the child page's notification.
3774 *
3775 * <p>If wearable specific actions were added to the main notification, this index will
3776 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07003777 *
3778 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07003779 */
3780 public int getContentAction() {
3781 return mContentActionIndex;
3782 }
3783
3784 /**
3785 * Set the gravity that this notification should have within the available viewport space.
3786 * Supported values include {@link android.view.Gravity#TOP},
3787 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3788 * The default value is {@link android.view.Gravity#BOTTOM}.
3789 */
3790 public WearableExtender setGravity(int gravity) {
3791 mGravity = gravity;
3792 return this;
3793 }
3794
3795 /**
3796 * Get the gravity that this notification should have within the available viewport space.
3797 * Supported values include {@link android.view.Gravity#TOP},
3798 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3799 * The default value is {@link android.view.Gravity#BOTTOM}.
3800 */
3801 public int getGravity() {
3802 return mGravity;
3803 }
3804
3805 /**
3806 * Set the custom size preset for the display of this notification out of the available
3807 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3808 * {@link #SIZE_LARGE}.
3809 * <p>Some custom size presets are only applicable for custom display notifications created
3810 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
3811 * documentation for the preset in question. See also
3812 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
3813 */
3814 public WearableExtender setCustomSizePreset(int sizePreset) {
3815 mCustomSizePreset = sizePreset;
3816 return this;
3817 }
3818
3819 /**
3820 * Get the custom size preset for the display of this notification out of the available
3821 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3822 * {@link #SIZE_LARGE}.
3823 * <p>Some custom size presets are only applicable for custom display notifications created
3824 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
3825 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
3826 */
3827 public int getCustomSizePreset() {
3828 return mCustomSizePreset;
3829 }
3830
3831 /**
3832 * Set the custom height in pixels for the display of this notification's content.
3833 * <p>This option is only available for custom display notifications created
3834 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
3835 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
3836 * {@link #getCustomContentHeight}.
3837 */
3838 public WearableExtender setCustomContentHeight(int height) {
3839 mCustomContentHeight = height;
3840 return this;
3841 }
3842
3843 /**
3844 * Get the custom height in pixels for the display of this notification's content.
3845 * <p>This option is only available for custom display notifications created
3846 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
3847 * {@link #setCustomContentHeight}.
3848 */
3849 public int getCustomContentHeight() {
3850 return mCustomContentHeight;
3851 }
3852
3853 /**
3854 * Set whether the scrolling position for the contents of this notification should start
3855 * at the bottom of the contents instead of the top when the contents are too long to
3856 * display within the screen. Default is false (start scroll at the top).
3857 */
3858 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
3859 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
3860 return this;
3861 }
3862
3863 /**
3864 * Get whether the scrolling position for the contents of this notification should start
3865 * at the bottom of the contents instead of the top when the contents are too long to
3866 * display within the screen. Default is false (start scroll at the top).
3867 */
3868 public boolean getStartScrollBottom() {
3869 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
3870 }
3871
3872 /**
3873 * Set whether the content intent is available when the wearable device is not connected
3874 * to a companion device. The user can still trigger this intent when the wearable device
3875 * is offline, but a visual hint will indicate that the content intent may not be available.
3876 * Defaults to true.
3877 */
3878 public WearableExtender setContentIntentAvailableOffline(
3879 boolean contentIntentAvailableOffline) {
3880 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
3881 return this;
3882 }
3883
3884 /**
3885 * Get whether the content intent is available when the wearable device is not connected
3886 * to a companion device. The user can still trigger this intent when the wearable device
3887 * is offline, but a visual hint will indicate that the content intent may not be available.
3888 * Defaults to true.
3889 */
3890 public boolean getContentIntentAvailableOffline() {
3891 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
3892 }
3893
3894 /**
3895 * Set a hint that this notification's icon should not be displayed.
3896 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
3897 * @return this object for method chaining
3898 */
3899 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
3900 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
3901 return this;
3902 }
3903
3904 /**
3905 * Get a hint that this notification's icon should not be displayed.
3906 * @return {@code true} if this icon should not be displayed, false otherwise.
3907 * The default value is {@code false} if this was never set.
3908 */
3909 public boolean getHintHideIcon() {
3910 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
3911 }
3912
3913 /**
3914 * Set a visual hint that only the background image of this notification should be
3915 * displayed, and other semantic content should be hidden. This hint is only applicable
3916 * to sub-pages added using {@link #addPage}.
3917 */
3918 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
3919 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
3920 return this;
3921 }
3922
3923 /**
3924 * Get a visual hint that only the background image of this notification should be
3925 * displayed, and other semantic content should be hidden. This hint is only applicable
3926 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
3927 */
3928 public boolean getHintShowBackgroundOnly() {
3929 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
3930 }
3931
3932 private void setFlag(int mask, boolean value) {
3933 if (value) {
3934 mFlags |= mask;
3935 } else {
3936 mFlags &= ~mask;
3937 }
3938 }
3939 }
3940
3941 /**
3942 * Get an array of Notification objects from a parcelable array bundle field.
3943 * Update the bundle to have a typed array so fetches in the future don't need
3944 * to do an array copy.
3945 */
3946 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
3947 Parcelable[] array = bundle.getParcelableArray(key);
3948 if (array instanceof Notification[] || array == null) {
3949 return (Notification[]) array;
3950 }
3951 Notification[] typedArray = Arrays.copyOf(array, array.length,
3952 Notification[].class);
3953 bundle.putParcelableArray(key, typedArray);
3954 return typedArray;
3955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956}