blob: a1cdf59ec2fbe3aebca1b308a1432a175e962a10 [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;
Daniel Sandler01df1c62014-06-09 10:54:01 -040020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.Intent;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040024import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040025import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010026import android.graphics.BitmapFactory;
27import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010028import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010029import android.graphics.drawable.Drawable;
Jeff Sharkey098d5802012-04-26 17:30:34 -070030import android.media.AudioManager;
Dan Sandler842dd772014-05-15 09:36:47 -040031import android.media.session.MediaSessionToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040033import android.os.BadParcelableException;
Dan Sandler26e81cf2014-05-06 10:01:27 -040034import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050035import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Parcel;
37import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040038import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070039import android.os.UserHandle;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040042import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040043import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070044import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080045import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070046import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.widget.RemoteViews;
48
Griff Hazen959591e2014-05-15 22:26:18 -070049import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070050import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070051
Tor Norbyed9273d62013-05-30 15:59:53 -070052import java.lang.annotation.Retention;
53import java.lang.annotation.RetentionPolicy;
Andy Stadler110988c2010-12-03 14:29:16 -080054import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050055import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070056import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070057import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070058import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060/**
61 * A class that represents how a persistent notification is to be presented to
62 * the user using the {@link android.app.NotificationManager}.
63 *
Joe Onoratocb109a02011-01-18 17:57:41 -080064 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
65 * easier to construct Notifications.</p>
66 *
Joe Fernandez558459f2011-10-13 16:47:36 -070067 * <div class="special reference">
68 * <h3>Developer Guides</h3>
69 * <p>For a guide to creating notifications, read the
70 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
71 * developer guide.</p>
72 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 */
74public class Notification implements Parcelable
75{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040076 private static final String TAG = "Notification";
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040079 * An activity that provides a user interface for adjusting notification preferences for its
80 * containing application. Optional but recommended for apps that post
81 * {@link android.app.Notification Notifications}.
82 */
83 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
84 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
85 = "android.intent.category.NOTIFICATION_PREFERENCES";
86
87 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * Use all default values (where applicable).
89 */
90 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
93 * Use the default notification sound. This will ignore any given
94 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095 *
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050098 */
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final int DEFAULT_SOUND = 1;
101
102 /**
103 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500104 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700105 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500108 */
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 /**
113 * Use the default notification lights. This will ignore the
114 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
115 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118 */
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800124 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500125 * Default value: {@link System#currentTimeMillis() Now}.
126 *
127 * Choose a timestamp that will be most relevant to the user. For most finite events, this
128 * corresponds to the time the event happened (or will happen, in the case of events that have
129 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800130 * timestamped according to when the activity began.
131 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800133 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134 * <ul>
135 * <li>Notification of a new chat message should be stamped when the message was received.</li>
136 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
137 * <li>Notification of a completed file download should be stamped when the download finished.</li>
138 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
139 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
140 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800141 * </ul>
142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 */
144 public long when;
145
146 /**
147 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500148 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
150 public int icon;
151
152 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800153 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
154 * leave it at its default value of 0.
155 *
156 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700157 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800158 */
159 public int iconLevel;
160
161 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 * The number of events that this notification represents. For example, in a new mail
163 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800164 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500165 * The system may or may not use this field to modify the appearance of the notification. For
166 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
167 * superimposed over the icon in the status bar. Starting with
168 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
169 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800170 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 */
173 public int number;
174
175 /**
176 * The intent to execute when the expanded status entry is clicked. If
177 * this is an activity, it must include the
178 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800179 * that you take care of task management as described in the
180 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800181 * Stack</a> document. In particular, make sure to read the notification section
182 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
183 * Notifications</a> for the correct ways to launch an application from a
184 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 */
186 public PendingIntent contentIntent;
187
188 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 * The intent to execute when the notification is explicitly dismissed by the user, either with
190 * the "Clear All" button or by swiping it away individually.
191 *
192 * This probably shouldn't be launching an activity since several of those will be sent
193 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
195 public PendingIntent deleteIntent;
196
197 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700198 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800199 *
200 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400201 */
202 public PendingIntent fullScreenIntent;
203
204 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400206 * the status bar on large and smaller devices.
207 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800208 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 public CharSequence tickerText;
211
212 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800213 * The view to show as the ticker in the status bar when the notification
214 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400215 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800216 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400217
218 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400219 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 */
221 public RemoteViews contentView;
222
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400223 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400224 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400225 * opportunity to show more detail. The system UI may choose to show this
226 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400227 */
228 public RemoteViews bigContentView;
229
Chris Wren8fd39ec2014-02-27 17:43:26 -0500230
231 /**
232 * @hide
233 * A medium-format version of {@link #contentView}, giving the Notification an
234 * opportunity to add action buttons to contentView. The system UI may
235 * choose to show this as a popup notification at its discretion.
236 */
237 public RemoteViews headsUpContentView;
238
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400239 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800240 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800242 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
244 /**
245 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500248 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * </p>
250 */
251 public Uri sound;
252
253 /**
254 * Use this constant as the value for audioStreamType to request that
255 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700256 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 */
258 public static final int STREAM_DEFAULT = -1;
259
260 /**
261 * The audio stream type to use when playing the sound.
262 * Should be one of the STREAM_ constants from
263 * {@link android.media.AudioManager}.
264 */
265 public int audioStreamType = STREAM_DEFAULT;
266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500268 * The pattern with which to vibrate.
269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * <p>
271 * To vibrate the default pattern, see {@link #defaults}.
272 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * @see android.os.Vibrator#vibrate(long[],int)
275 */
276 public long[] vibrate;
277
278 /**
279 * The color of the led. The hardware will do its best approximation.
280 *
281 * @see #FLAG_SHOW_LIGHTS
282 * @see #flags
283 */
284 public int ledARGB;
285
286 /**
287 * The number of milliseconds for the LED to be on while it's flashing.
288 * The hardware will do its best approximation.
289 *
290 * @see #FLAG_SHOW_LIGHTS
291 * @see #flags
292 */
293 public int ledOnMS;
294
295 /**
296 * The number of milliseconds for the LED to be off while it's flashing.
297 * The hardware will do its best approximation.
298 *
299 * @see #FLAG_SHOW_LIGHTS
300 * @see #flags
301 */
302 public int ledOffMS;
303
304 /**
305 * Specifies which values should be taken from the defaults.
306 * <p>
307 * To set, OR the desired from {@link #DEFAULT_SOUND},
308 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
309 * values, use {@link #DEFAULT_ALL}.
310 * </p>
311 */
312 public int defaults;
313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * Bit to be bitwise-ored into the {@link #flags} field that should be
316 * set if you want the LED on for this notification.
317 * <ul>
318 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
319 * or 0 for both ledOnMS and ledOffMS.</li>
320 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
321 * <li>To flash the LED, pass the number of milliseconds that it should
322 * be on and off to ledOnMS and ledOffMS.</li>
323 * </ul>
324 * <p>
325 * Since hardware varies, you are not guaranteed that any of the values
326 * you pass are honored exactly. Use the system defaults (TODO) if possible
327 * because they will be set to values that work on any given hardware.
328 * <p>
329 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 */
332 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
333
334 /**
335 * Bit to be bitwise-ored into the {@link #flags} field that should be
336 * set if this notification is in reference to something that is ongoing,
337 * like a phone call. It should not be set if this notification is in
338 * reference to something that happened at a particular point in time,
339 * like a missed phone call.
340 */
341 public static final int FLAG_ONGOING_EVENT = 0x00000002;
342
343 /**
344 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700345 * the audio will be repeated until the notification is
346 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
348 public static final int FLAG_INSISTENT = 0x00000004;
349
350 /**
351 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700352 * set if you would only like the sound, vibrate and ticker to be played
353 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 */
355 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
356
357 /**
358 * Bit to be bitwise-ored into the {@link #flags} field that should be
359 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500360 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 */
363 public static final int FLAG_AUTO_CANCEL = 0x00000010;
364
365 /**
366 * Bit to be bitwise-ored into the {@link #flags} field that should be
367 * set if the notification should not be canceled when the user clicks
368 * the Clear all button.
369 */
370 public static final int FLAG_NO_CLEAR = 0x00000020;
371
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700372 /**
373 * Bit to be bitwise-ored into the {@link #flags} field that should be
374 * set if this notification represents a currently running service. This
375 * will normally be set for you by {@link Service#startForeground}.
376 */
377 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
378
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400379 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500380 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800381 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500382 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400383 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500384 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400385
Griff Hazendfcb0802014-02-11 12:00:00 -0800386 /**
387 * Bit to be bitswise-ored into the {@link #flags} field that should be
388 * set if this notification is relevant to the current device only
389 * and it is not recommended that it bridge to other devices.
390 */
391 public static final int FLAG_LOCAL_ONLY = 0x00000100;
392
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700393 /**
394 * Bit to be bitswise-ored into the {@link #flags} field that should be
395 * set if this notification is the group summary for a group of notifications.
396 * Grouped notifications may display in a cluster or stack on devices which
397 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
398 */
399 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 public int flags;
402
Tor Norbyed9273d62013-05-30 15:59:53 -0700403 /** @hide */
404 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
405 @Retention(RetentionPolicy.SOURCE)
406 public @interface Priority {}
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500409 * Default notification {@link #priority}. If your application does not prioritize its own
410 * notifications, use this value for all notifications.
411 */
412 public static final int PRIORITY_DEFAULT = 0;
413
414 /**
415 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
416 * items smaller, or at a different position in the list, compared with your app's
417 * {@link #PRIORITY_DEFAULT} items.
418 */
419 public static final int PRIORITY_LOW = -1;
420
421 /**
422 * Lowest {@link #priority}; these items might not be shown to the user except under special
423 * circumstances, such as detailed notification logs.
424 */
425 public static final int PRIORITY_MIN = -2;
426
427 /**
428 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
429 * show these items larger, or at a different position in notification lists, compared with
430 * your app's {@link #PRIORITY_DEFAULT} items.
431 */
432 public static final int PRIORITY_HIGH = 1;
433
434 /**
435 * Highest {@link #priority}, for your application's most important items that require the
436 * user's prompt attention or input.
437 */
438 public static final int PRIORITY_MAX = 2;
439
440 /**
441 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800442 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500443 * Priority is an indication of how much of the user's valuable attention should be consumed by
444 * this notification. Low-priority notifications may be hidden from the user in certain
445 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500446 * system will make a determination about how to interpret this priority when presenting
447 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700449 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500450 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800451
Dan Sandler26e81cf2014-05-06 10:01:27 -0400452 /**
453 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
454 * to be applied by the standard Style templates when presenting this notification.
455 *
456 * The current template design constructs a colorful header image by overlaying the
457 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
458 * ignored.
459 */
460 public int color = COLOR_DEFAULT;
461
462 /**
463 * Special value of {@link #color} telling the system not to decorate this notification with
464 * any special color but instead use default colors when presenting this notification.
465 */
466 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600467
468 /**
469 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
470 * the notification's presence and contents in untrusted situations (namely, on the secure
471 * lockscreen).
472 *
473 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
474 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
475 * shown in all situations, but the contents are only available if the device is unlocked for
476 * the appropriate user.
477 *
478 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
479 * can be read even in an "insecure" context (that is, above a secure lockscreen).
480 * To modify the public version of this notification—for example, to redact some portions—see
481 * {@link Builder#setPublicVersion(Notification)}.
482 *
483 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
484 * and ticker until the user has bypassed the lockscreen.
485 */
486 public int visibility;
487
488 public static final int VISIBILITY_PUBLIC = 1;
489 public static final int VISIBILITY_PRIVATE = 0;
490 public static final int VISIBILITY_SECRET = -1;
491
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500492 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400493 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500494 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400495 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500496
497 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400498 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500499 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400500 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500501
502 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400503 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500504 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400505 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500506
507 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400508 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500509 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400510 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500511
512 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400513 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500514 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400515 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500516
517 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400518 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500519 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400520 public static final String CATEGORY_ALARM = "alarm";
521
522 /**
523 * Notification category: progress of a long-running background operation.
524 */
525 public static final String CATEGORY_PROGRESS = "progress";
526
527 /**
528 * Notification category: social network or sharing update.
529 */
530 public static final String CATEGORY_SOCIAL = "social";
531
532 /**
533 * Notification category: error in background operation or authentication status.
534 */
535 public static final String CATEGORY_ERROR = "err";
536
537 /**
538 * Notification category: media transport control for playback.
539 */
540 public static final String CATEGORY_TRANSPORT = "transport";
541
542 /**
543 * Notification category: system or device status update. Reserved for system use.
544 */
545 public static final String CATEGORY_SYSTEM = "sys";
546
547 /**
548 * Notification category: indication of running background service.
549 */
550 public static final String CATEGORY_SERVICE = "service";
551
552 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400553 * Notification category: a specific, timely recommendation for a single thing.
554 * For example, a news app might want to recommend a news story it believes the user will
555 * want to read next.
556 */
557 public static final String CATEGORY_RECOMMENDATION = "recommendation";
558
559 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400560 * Notification category: ongoing information about device or contextual status.
561 */
562 public static final String CATEGORY_STATUS = "status";
563
564 /**
565 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
566 * that best describes this Notification. May be used by the system for ranking and filtering.
567 */
568 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500569
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700570 private String mGroupKey;
571
572 /**
573 * Get the key used to group this notification into a cluster or stack
574 * with other notifications on devices which support such rendering.
575 */
576 public String getGroup() {
577 return mGroupKey;
578 }
579
580 private String mSortKey;
581
582 /**
583 * Get a sort key that orders this notification among other notifications from the
584 * same package. This can be useful if an external sort was already applied and an app
585 * would like to preserve this. Notifications will be sorted lexicographically using this
586 * value, although providing different priorities in addition to providing sort key may
587 * cause this value to be ignored.
588 *
589 * <p>This sort key can also be used to order members of a notification group. See
590 * {@link Builder#setGroup}.
591 *
592 * @see String#compareTo(String)
593 */
594 public String getSortKey() {
595 return mSortKey;
596 }
597
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500598 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400599 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400600 * <p>
601 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
602 * APIs, and are intended to be used by
603 * {@link android.service.notification.NotificationListenerService} implementations to extract
604 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400606 public Bundle extras = new Bundle();
607
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400608 /**
609 * {@link #extras} key: this is the title of the notification,
610 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
611 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500612 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400613
614 /**
615 * {@link #extras} key: this is the title of the notification when shown in expanded form,
616 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
617 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400618 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400619
620 /**
621 * {@link #extras} key: this is the main text payload, as supplied to
622 * {@link Builder#setContentText(CharSequence)}.
623 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500624 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400625
626 /**
627 * {@link #extras} key: this is a third line of text, as supplied to
628 * {@link Builder#setSubText(CharSequence)}.
629 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400630 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400631
632 /**
633 * {@link #extras} key: this is a small piece of additional text as supplied to
634 * {@link Builder#setContentInfo(CharSequence)}.
635 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400636 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400637
638 /**
639 * {@link #extras} key: this is a line of summary information intended to be shown
640 * alongside expanded notifications, as supplied to (e.g.)
641 * {@link BigTextStyle#setSummaryText(CharSequence)}.
642 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400643 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400644
645 /**
646 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
647 * supplied to {@link Builder#setSmallIcon(int)}.
648 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500649 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400650
651 /**
652 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
653 * notification payload, as
654 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
655 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400656 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400657
658 /**
659 * {@link #extras} key: this is a bitmap to be used instead of the one from
660 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
661 * shown in its expanded form, as supplied to
662 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
663 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400664 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400665
666 /**
667 * {@link #extras} key: this is the progress value supplied to
668 * {@link Builder#setProgress(int, int, boolean)}.
669 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400670 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400671
672 /**
673 * {@link #extras} key: this is the maximum value supplied to
674 * {@link Builder#setProgress(int, int, boolean)}.
675 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400676 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400677
678 /**
679 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
680 * {@link Builder#setProgress(int, int, boolean)}.
681 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400682 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400683
684 /**
685 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
686 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
687 * {@link Builder#setUsesChronometer(boolean)}.
688 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400689 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400690
691 /**
692 * {@link #extras} key: whether {@link #when} should be shown,
693 * as supplied to {@link Builder#setShowWhen(boolean)}.
694 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400695 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400696
697 /**
698 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
699 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
700 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400701 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400702
703 /**
704 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
705 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
706 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400707 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400708
709 /**
710 * {@link #extras} key: A string representing the name of the specific
711 * {@link android.app.Notification.Style} used to create this notification.
712 */
Chris Wren91ad5632013-06-05 15:05:57 -0400713 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400714
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400715 /**
716 * {@link #extras} key: An array of people that this notification relates to, specified
717 * by contacts provider contact URI.
718 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400719 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500720
721 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400722 * @hide
Chris Wrenf9536642014-04-17 10:01:54 -0400723 * Extra added by NotificationManagerService to indicate whether
724 * the Notifications's score has been modified.
Scott Greenwald9a05b312013-06-28 00:37:54 -0400725 */
726 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
727
728 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400729 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400730 * @hide
731 */
732 public static final String EXTRA_AS_HEADS_UP = "headsup";
733
734 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 * Allow certain system-generated notifications to appear before the device is provisioned.
736 * Only available to notifications coming from the android package.
737 * @hide
738 */
739 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
740
741 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700742 * {@link #extras} key: A
743 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
744 * in the background when the notification is selected. The URI must point to an image stream
745 * suitable for passing into
746 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
747 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
748 * URI used for this purpose must require no permissions to read the image data.
749 */
750 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
751
752 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400753 * {@link #extras} key: A
754 * {@link android.media.session.MediaSessionToken} associated with a
755 * {@link android.app.Notification.MediaStyle} notification.
756 */
757 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
758
759 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400760 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400761 * @hide
762 */
763 public static final int HEADS_UP_NEVER = 0;
764
765 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400766 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400767 * @hide
768 */
769 public static final int HEADS_UP_ALLOWED = 1;
770
771 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400772 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400773 * @hide
774 */
775 public static final int HEADS_UP_REQUESTED = 2;
776
777 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400778 * Structure to encapsulate a named action that can be shown as part of this notification.
779 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
780 * selected by the user.
781 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700782 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
783 * or {@link Notification.Builder#addAction(Notification.Action)}
784 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400785 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500786 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700787 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700788 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700789
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400790 /**
791 * Small icon representing the action.
792 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400793 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700794
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400795 /**
796 * Title of the action.
797 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400798 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700799
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400800 /**
801 * Intent to send when the user invokes this action. May be null, in which case the action
802 * may be rendered in a disabled presentation by the system UI.
803 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400804 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700805
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400806 private Action(Parcel in) {
807 icon = in.readInt();
808 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
809 if (in.readInt() == 1) {
810 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
811 }
Griff Hazen959591e2014-05-15 22:26:18 -0700812 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700813 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400814 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700815
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400816 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700817 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400818 */
819 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700820 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700821 }
822
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700823 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
824 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825 this.icon = icon;
826 this.title = title;
827 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700828 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700829 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700830 }
831
832 /**
833 * Get additional metadata carried around with this Action.
834 */
835 public Bundle getExtras() {
836 return mExtras;
837 }
838
839 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700840 * Get the list of inputs to be collected from the user when this action is sent.
841 * May return null if no remote inputs were added.
842 */
843 public RemoteInput[] getRemoteInputs() {
844 return mRemoteInputs;
845 }
846
847 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700848 * Builder class for {@link Action} objects.
849 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700850 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700851 private final int mIcon;
852 private final CharSequence mTitle;
853 private final PendingIntent mIntent;
854 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700855 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700856
857 /**
858 * Construct a new builder for {@link Action} object.
859 * @param icon icon to show for this action
860 * @param title the title of the action
861 * @param intent the {@link PendingIntent} to fire when users trigger this action
862 */
863 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700864 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700865 }
866
867 /**
868 * Construct a new builder for {@link Action} object using the fields from an
869 * {@link Action}.
870 * @param action the action to read fields from.
871 */
872 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700873 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
874 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700875 }
876
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700877 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
878 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700879 mIcon = icon;
880 mTitle = title;
881 mIntent = intent;
882 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700883 if (remoteInputs != null) {
884 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
885 Collections.addAll(mRemoteInputs, remoteInputs);
886 }
Griff Hazen959591e2014-05-15 22:26:18 -0700887 }
888
889 /**
890 * Merge additional metadata into this builder.
891 *
892 * <p>Values within the Bundle will replace existing extras values in this Builder.
893 *
894 * @see Notification.Action#extras
895 */
896 public Builder addExtras(Bundle extras) {
897 if (extras != null) {
898 mExtras.putAll(extras);
899 }
900 return this;
901 }
902
903 /**
904 * Get the metadata Bundle used by this Builder.
905 *
906 * <p>The returned Bundle is shared with this Builder.
907 */
908 public Bundle getExtras() {
909 return mExtras;
910 }
911
912 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700913 * Add an input to be collected from the user when this action is sent.
914 * Response values can be retrieved from the fired intent by using the
915 * {@link RemoteInput#getResultsFromIntent} function.
916 * @param remoteInput a {@link RemoteInput} to add to the action
917 * @return this object for method chaining
918 */
919 public Builder addRemoteInput(RemoteInput remoteInput) {
920 if (mRemoteInputs == null) {
921 mRemoteInputs = new ArrayList<RemoteInput>();
922 }
923 mRemoteInputs.add(remoteInput);
924 return this;
925 }
926
927 /**
928 * Apply an extender to this action builder. Extenders may be used to add
929 * metadata or change options on this builder.
930 */
Griff Hazen61a9e862014-05-22 16:05:19 -0700931 public Builder extend(Extender extender) {
932 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700933 return this;
934 }
935
936 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700937 * Combine all of the options that have been set and return a new {@link Action}
938 * object.
939 * @return the built action
940 */
941 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700942 RemoteInput[] remoteInputs = mRemoteInputs != null
943 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
944 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -0700945 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400946 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400948 @Override
949 public Action clone() {
950 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700951 icon,
952 title,
953 actionIntent, // safe to alias
954 new Bundle(mExtras),
955 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400956 }
957 @Override
958 public int describeContents() {
959 return 0;
960 }
961 @Override
962 public void writeToParcel(Parcel out, int flags) {
963 out.writeInt(icon);
964 TextUtils.writeToParcel(title, out, flags);
965 if (actionIntent != null) {
966 out.writeInt(1);
967 actionIntent.writeToParcel(out, flags);
968 } else {
969 out.writeInt(0);
970 }
Griff Hazen959591e2014-05-15 22:26:18 -0700971 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700972 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400973 }
Griff Hazen959591e2014-05-15 22:26:18 -0700974 public static final Parcelable.Creator<Action> CREATOR =
975 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400976 public Action createFromParcel(Parcel in) {
977 return new Action(in);
978 }
979 public Action[] newArray(int size) {
980 return new Action[size];
981 }
982 };
Griff Hazen61a9e862014-05-22 16:05:19 -0700983
984 /**
985 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
986 * metadata or change options on an action builder.
987 */
988 public interface Extender {
989 /**
990 * Apply this extender to a notification action builder.
991 * @param builder the builder to be modified.
992 * @return the build object for chaining.
993 */
994 public Builder extend(Builder builder);
995 }
996
997 /**
998 * Wearable extender for notification actions. To add extensions to an action,
999 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1000 * the {@code WearableExtender()} constructor and apply it to a
1001 * {@link android.app.Notification.Action.Builder} using
1002 * {@link android.app.Notification.Action.Builder#extend}.
1003 *
1004 * <pre class="prettyprint">
1005 * Notification.Action action = new Notification.Action.Builder(
1006 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001007 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001008 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001009 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001010 */
1011 public static final class WearableExtender implements Extender {
1012 /** Notification action extra which contains wearable extensions */
1013 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1014
1015 private static final String KEY_FLAGS = "flags";
1016
1017 // Flags bitwise-ored to mFlags
1018 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1019
1020 // Default value for flags integer
1021 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1022
1023 private int mFlags = DEFAULT_FLAGS;
1024
1025 /**
1026 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1027 * options.
1028 */
1029 public WearableExtender() {
1030 }
1031
1032 /**
1033 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1034 * wearable options present in an existing notification action.
1035 * @param action the notification action to inspect.
1036 */
1037 public WearableExtender(Action action) {
1038 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1039 if (wearableBundle != null) {
1040 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1041 }
1042 }
1043
1044 /**
1045 * Apply wearable extensions to a notification action that is being built. This is
1046 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1047 * method of {@link android.app.Notification.Action.Builder}.
1048 */
1049 @Override
1050 public Action.Builder extend(Action.Builder builder) {
1051 Bundle wearableBundle = new Bundle();
1052
1053 if (mFlags != DEFAULT_FLAGS) {
1054 wearableBundle.putInt(KEY_FLAGS, mFlags);
1055 }
1056
1057 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1058 return builder;
1059 }
1060
1061 @Override
1062 public WearableExtender clone() {
1063 WearableExtender that = new WearableExtender();
1064 that.mFlags = this.mFlags;
1065 return that;
1066 }
1067
1068 /**
1069 * Set 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 WearableExtender setAvailableOffline(boolean availableOffline) {
1075 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1076 return this;
1077 }
1078
1079 /**
1080 * Get whether this action is available when the wearable device is not connected to
1081 * a companion device. The user can still trigger this action when the wearable device is
1082 * offline, but a visual hint will indicate that the action may not be available.
1083 * Defaults to true.
1084 */
1085 public boolean isAvailableOffline() {
1086 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1087 }
1088
1089 private void setFlag(int mask, boolean value) {
1090 if (value) {
1091 mFlags |= mask;
1092 } else {
1093 mFlags &= ~mask;
1094 }
1095 }
1096 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001097 }
1098
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001099 /**
1100 * Array of all {@link Action} structures attached to this notification by
1101 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1102 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1103 * interface for invoking actions.
1104 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001105 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001106
1107 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001108 * Replacement version of this notification whose content will be shown
1109 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1110 * and {@link #VISIBILITY_PUBLIC}.
1111 */
1112 public Notification publicVersion;
1113
1114 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001115 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001116 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 */
1118 public Notification()
1119 {
1120 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001121 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123
1124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 * @hide
1126 */
1127 public Notification(Context context, int icon, CharSequence tickerText, long when,
1128 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1129 {
1130 this.when = when;
1131 this.icon = icon;
1132 this.tickerText = tickerText;
1133 setLatestEventInfo(context, contentTitle, contentText,
1134 PendingIntent.getActivity(context, 0, contentIntent, 0));
1135 }
1136
1137 /**
1138 * Constructs a Notification object with the information needed to
1139 * have a status bar icon without the standard expanded view.
1140 *
1141 * @param icon The resource id of the icon to put in the status bar.
1142 * @param tickerText The text that flows by in the status bar when the notification first
1143 * activates.
1144 * @param when The time to show in the time field. In the System.currentTimeMillis
1145 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001146 *
1147 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001149 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 public Notification(int icon, CharSequence tickerText, long when)
1151 {
1152 this.icon = icon;
1153 this.tickerText = tickerText;
1154 this.when = when;
1155 }
1156
1157 /**
1158 * Unflatten the notification from a parcel.
1159 */
1160 public Notification(Parcel parcel)
1161 {
1162 int version = parcel.readInt();
1163
1164 when = parcel.readLong();
1165 icon = parcel.readInt();
1166 number = parcel.readInt();
1167 if (parcel.readInt() != 0) {
1168 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1169 }
1170 if (parcel.readInt() != 0) {
1171 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1172 }
1173 if (parcel.readInt() != 0) {
1174 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1175 }
1176 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001177 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001178 }
1179 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1181 }
Joe Onorato561d3852010-11-20 18:09:34 -08001182 if (parcel.readInt() != 0) {
1183 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 defaults = parcel.readInt();
1186 flags = parcel.readInt();
1187 if (parcel.readInt() != 0) {
1188 sound = Uri.CREATOR.createFromParcel(parcel);
1189 }
1190
1191 audioStreamType = parcel.readInt();
1192 vibrate = parcel.createLongArray();
1193 ledARGB = parcel.readInt();
1194 ledOnMS = parcel.readInt();
1195 ledOffMS = parcel.readInt();
1196 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001197
1198 if (parcel.readInt() != 0) {
1199 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1200 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001201
1202 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001203
John Spurlockfd7f1e02014-03-18 16:41:57 -04001204 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001205
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001206 mGroupKey = parcel.readString();
1207
1208 mSortKey = parcel.readString();
1209
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001210 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001211
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001212 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1213
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001214 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001215 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1216 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001217
Chris Wren8fd39ec2014-02-27 17:43:26 -05001218 if (parcel.readInt() != 0) {
1219 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1220 }
1221
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001222 visibility = parcel.readInt();
1223
1224 if (parcel.readInt() != 0) {
1225 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1226 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001227
1228 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 }
1230
Andy Stadler110988c2010-12-03 14:29:16 -08001231 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001232 public Notification clone() {
1233 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001234 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001235 return that;
1236 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001237
Daniel Sandler1a497d32013-04-18 14:52:45 -04001238 /**
1239 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1240 * of this into that.
1241 * @hide
1242 */
1243 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001244 that.when = this.when;
1245 that.icon = this.icon;
1246 that.number = this.number;
1247
1248 // PendingIntents are global, so there's no reason (or way) to clone them.
1249 that.contentIntent = this.contentIntent;
1250 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001251 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001252
1253 if (this.tickerText != null) {
1254 that.tickerText = this.tickerText.toString();
1255 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001256 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001257 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001258 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001259 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001260 that.contentView = this.contentView.clone();
1261 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001262 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001263 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1264 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001265 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001266 that.sound = this.sound; // android.net.Uri is immutable
1267 that.audioStreamType = this.audioStreamType;
1268
1269 final long[] vibrate = this.vibrate;
1270 if (vibrate != null) {
1271 final int N = vibrate.length;
1272 final long[] vib = that.vibrate = new long[N];
1273 System.arraycopy(vibrate, 0, vib, 0, N);
1274 }
1275
1276 that.ledARGB = this.ledARGB;
1277 that.ledOnMS = this.ledOnMS;
1278 that.ledOffMS = this.ledOffMS;
1279 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001280
Joe Onorato18e69df2010-05-17 22:26:12 -07001281 that.flags = this.flags;
1282
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001283 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001284
John Spurlockfd7f1e02014-03-18 16:41:57 -04001285 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001286
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001287 that.mGroupKey = this.mGroupKey;
1288
1289 that.mSortKey = this.mSortKey;
1290
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001291 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001292 try {
1293 that.extras = new Bundle(this.extras);
1294 // will unparcel
1295 that.extras.size();
1296 } catch (BadParcelableException e) {
1297 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1298 that.extras = null;
1299 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001300 }
1301
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001302 if (this.actions != null) {
1303 that.actions = new Action[this.actions.length];
1304 for(int i=0; i<this.actions.length; i++) {
1305 that.actions[i] = this.actions[i].clone();
1306 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001307 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001308
Daniel Sandler1a497d32013-04-18 14:52:45 -04001309 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001310 that.bigContentView = this.bigContentView.clone();
1311 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001312
Chris Wren8fd39ec2014-02-27 17:43:26 -05001313 if (heavy && this.headsUpContentView != null) {
1314 that.headsUpContentView = this.headsUpContentView.clone();
1315 }
1316
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001317 that.visibility = this.visibility;
1318
1319 if (this.publicVersion != null) {
1320 that.publicVersion = new Notification();
1321 this.publicVersion.cloneInto(that.publicVersion, heavy);
1322 }
1323
Dan Sandler26e81cf2014-05-06 10:01:27 -04001324 that.color = this.color;
1325
Daniel Sandler1a497d32013-04-18 14:52:45 -04001326 if (!heavy) {
1327 that.lightenPayload(); // will clean out extras
1328 }
1329 }
1330
1331 /**
1332 * Removes heavyweight parts of the Notification object for archival or for sending to
1333 * listeners when the full contents are not necessary.
1334 * @hide
1335 */
1336 public final void lightenPayload() {
1337 tickerView = null;
1338 contentView = null;
1339 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001340 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001341 largeIcon = null;
1342 if (extras != null) {
1343 extras.remove(Notification.EXTRA_LARGE_ICON);
1344 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1345 extras.remove(Notification.EXTRA_PICTURE);
1346 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001347 }
1348
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001349 /**
1350 * Make sure this CharSequence is safe to put into a bundle, which basically
1351 * means it had better not be some custom Parcelable implementation.
1352 * @hide
1353 */
1354 public static CharSequence safeCharSequence(CharSequence cs) {
1355 if (cs instanceof Parcelable) {
1356 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1357 + " instance is a custom Parcelable and not allowed in Notification");
1358 return cs.toString();
1359 }
1360
1361 return cs;
1362 }
1363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 public int describeContents() {
1365 return 0;
1366 }
1367
1368 /**
1369 * Flatten this notification from a parcel.
1370 */
1371 public void writeToParcel(Parcel parcel, int flags)
1372 {
1373 parcel.writeInt(1);
1374
1375 parcel.writeLong(when);
1376 parcel.writeInt(icon);
1377 parcel.writeInt(number);
1378 if (contentIntent != null) {
1379 parcel.writeInt(1);
1380 contentIntent.writeToParcel(parcel, 0);
1381 } else {
1382 parcel.writeInt(0);
1383 }
1384 if (deleteIntent != null) {
1385 parcel.writeInt(1);
1386 deleteIntent.writeToParcel(parcel, 0);
1387 } else {
1388 parcel.writeInt(0);
1389 }
1390 if (tickerText != null) {
1391 parcel.writeInt(1);
1392 TextUtils.writeToParcel(tickerText, parcel, flags);
1393 } else {
1394 parcel.writeInt(0);
1395 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001396 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001397 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001398 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001399 } else {
1400 parcel.writeInt(0);
1401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 if (contentView != null) {
1403 parcel.writeInt(1);
1404 contentView.writeToParcel(parcel, 0);
1405 } else {
1406 parcel.writeInt(0);
1407 }
Joe Onorato561d3852010-11-20 18:09:34 -08001408 if (largeIcon != null) {
1409 parcel.writeInt(1);
1410 largeIcon.writeToParcel(parcel, 0);
1411 } else {
1412 parcel.writeInt(0);
1413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414
1415 parcel.writeInt(defaults);
1416 parcel.writeInt(this.flags);
1417
1418 if (sound != null) {
1419 parcel.writeInt(1);
1420 sound.writeToParcel(parcel, 0);
1421 } else {
1422 parcel.writeInt(0);
1423 }
1424 parcel.writeInt(audioStreamType);
1425 parcel.writeLongArray(vibrate);
1426 parcel.writeInt(ledARGB);
1427 parcel.writeInt(ledOnMS);
1428 parcel.writeInt(ledOffMS);
1429 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001430
1431 if (fullScreenIntent != null) {
1432 parcel.writeInt(1);
1433 fullScreenIntent.writeToParcel(parcel, 0);
1434 } else {
1435 parcel.writeInt(0);
1436 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001437
1438 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001439
John Spurlockfd7f1e02014-03-18 16:41:57 -04001440 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001441
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001442 parcel.writeString(mGroupKey);
1443
1444 parcel.writeString(mSortKey);
1445
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001446 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001447
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001448 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001449
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001450 if (bigContentView != null) {
1451 parcel.writeInt(1);
1452 bigContentView.writeToParcel(parcel, 0);
1453 } else {
1454 parcel.writeInt(0);
1455 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001456
Chris Wren8fd39ec2014-02-27 17:43:26 -05001457 if (headsUpContentView != null) {
1458 parcel.writeInt(1);
1459 headsUpContentView.writeToParcel(parcel, 0);
1460 } else {
1461 parcel.writeInt(0);
1462 }
1463
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001464 parcel.writeInt(visibility);
1465
1466 if (publicVersion != null) {
1467 parcel.writeInt(1);
1468 publicVersion.writeToParcel(parcel, 0);
1469 } else {
1470 parcel.writeInt(0);
1471 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001472
1473 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 }
1475
1476 /**
1477 * Parcelable.Creator that instantiates Notification objects
1478 */
1479 public static final Parcelable.Creator<Notification> CREATOR
1480 = new Parcelable.Creator<Notification>()
1481 {
1482 public Notification createFromParcel(Parcel parcel)
1483 {
1484 return new Notification(parcel);
1485 }
1486
1487 public Notification[] newArray(int size)
1488 {
1489 return new Notification[size];
1490 }
1491 };
1492
1493 /**
1494 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1495 * layout.
1496 *
1497 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1498 * in the view.</p>
1499 * @param context The context for your application / activity.
1500 * @param contentTitle The title that goes in the expanded entry.
1501 * @param contentText The text that goes in the expanded entry.
1502 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1503 * If this is an activity, it must include the
1504 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001505 * that you take care of task management as described in the
1506 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1507 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001508 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001509 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001511 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 public void setLatestEventInfo(Context context,
1513 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001514 Notification.Builder builder = new Notification.Builder(context);
1515
1516 // First, ensure that key pieces of information that may have been set directly
1517 // are preserved
1518 builder.setWhen(this.when);
1519 builder.setSmallIcon(this.icon);
1520 builder.setPriority(this.priority);
1521 builder.setTicker(this.tickerText);
1522 builder.setNumber(this.number);
1523 builder.mFlags = this.flags;
1524 builder.setSound(this.sound, this.audioStreamType);
1525 builder.setDefaults(this.defaults);
1526 builder.setVibrate(this.vibrate);
1527
1528 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001530 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
1532 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001533 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001535 builder.setContentIntent(contentIntent);
1536 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 }
1538
1539 @Override
1540 public String toString() {
1541 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001542 sb.append("Notification(pri=");
1543 sb.append(priority);
1544 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001545 if (contentView != null) {
1546 sb.append(contentView.getPackage());
1547 sb.append("/0x");
1548 sb.append(Integer.toHexString(contentView.getLayoutId()));
1549 } else {
1550 sb.append("null");
1551 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001552 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001553 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001554 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1555 sb.append("default");
1556 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 int N = this.vibrate.length-1;
1558 sb.append("[");
1559 for (int i=0; i<N; i++) {
1560 sb.append(this.vibrate[i]);
1561 sb.append(',');
1562 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001563 if (N != -1) {
1564 sb.append(this.vibrate[N]);
1565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 } else {
1568 sb.append("null");
1569 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001570 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001571 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001573 } else if (this.sound != null) {
1574 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 } else {
1576 sb.append("null");
1577 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001578 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001580 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001581 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001582 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001583 if (this.category != null) {
1584 sb.append(" category=");
1585 sb.append(this.category);
1586 }
1587 if (this.mGroupKey != null) {
1588 sb.append(" groupKey=");
1589 sb.append(this.mGroupKey);
1590 }
1591 if (this.mSortKey != null) {
1592 sb.append(" sortKey=");
1593 sb.append(this.mSortKey);
1594 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001595 if (actions != null) {
1596 sb.append(" ");
1597 sb.append(actions.length);
1598 sb.append(" action");
1599 if (actions.length > 1) sb.append("s");
1600 }
1601 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 return sb.toString();
1603 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001604
Jeff Sharkey6d515712012-09-20 16:06:08 -07001605 /** {@hide} */
1606 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001607 if (user.getIdentifier() == UserHandle.USER_ALL) {
1608 user = UserHandle.OWNER;
1609 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001610 if (tickerView != null) {
1611 tickerView.setUser(user);
1612 }
1613 if (contentView != null) {
1614 contentView.setUser(user);
1615 }
1616 if (bigContentView != null) {
1617 bigContentView.setUser(user);
1618 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001619 if (headsUpContentView != null) {
1620 headsUpContentView.setUser(user);
1621 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001622 }
1623
Joe Onoratocb109a02011-01-18 17:57:41 -08001624 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001625 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001626 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001627 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001628 * content views using the platform's notification layout template. If your app supports
1629 * versions of Android as old as API level 4, you can instead use
1630 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1631 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1632 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001633 *
Scott Main183bf112012-08-13 19:12:13 -07001634 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001635 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001636 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001637 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001638 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1639 * .setContentText(subject)
1640 * .setSmallIcon(R.drawable.new_mail)
1641 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001642 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001643 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001644 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001645 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001646 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001647
Joe Onorato46439ce2010-11-19 13:56:21 -08001648 private Context mContext;
1649
1650 private long mWhen;
1651 private int mSmallIcon;
1652 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001653 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001654 private CharSequence mContentTitle;
1655 private CharSequence mContentText;
1656 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001657 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001658 private PendingIntent mContentIntent;
1659 private RemoteViews mContentView;
1660 private PendingIntent mDeleteIntent;
1661 private PendingIntent mFullScreenIntent;
1662 private CharSequence mTickerText;
1663 private RemoteViews mTickerView;
1664 private Bitmap mLargeIcon;
1665 private Uri mSound;
1666 private int mAudioStreamType;
1667 private long[] mVibrate;
1668 private int mLedArgb;
1669 private int mLedOnMs;
1670 private int mLedOffMs;
1671 private int mDefaults;
1672 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001673 private int mProgressMax;
1674 private int mProgress;
1675 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001676 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001677 private String mGroupKey;
1678 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001679 private Bundle mExtras;
1680 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001681 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001682 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001683 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001684 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001685 private int mVisibility = VISIBILITY_PRIVATE;
1686 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001687 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001688 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001689 private int mColor = COLOR_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001690
Joe Onoratocb109a02011-01-18 17:57:41 -08001691 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001692 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001693 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001694
1695 * <table>
1696 * <tr><th align=right>priority</th>
1697 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1698 * <tr><th align=right>when</th>
1699 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1700 * <tr><th align=right>audio stream</th>
1701 * <td>{@link #STREAM_DEFAULT}</td></tr>
1702 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001703 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001704
1705 * @param context
1706 * A {@link Context} that will be used by the Builder to construct the
1707 * RemoteViews. The Context will not be held past the lifetime of this Builder
1708 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001709 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001710 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001711 /*
1712 * Important compatibility note!
1713 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1714 * constructor for later use. At this point Activities - themselves subclasses of
1715 * ContextWrapper - do not have their inner Context populated yet. This means that
1716 * any calls to Context methods from within this constructor can cause NPEs in existing
1717 * apps. Any data populated from mContext should therefore be populated lazily to
1718 * preserve compatibility.
1719 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001720 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001721
1722 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001723 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001724 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001725 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001726 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001727
Dan Sandler26e81cf2014-05-06 10:01:27 -04001728 mColorUtil = NotificationColorUtil.getInstance();
Joe Onorato46439ce2010-11-19 13:56:21 -08001729 }
1730
Joe Onoratocb109a02011-01-18 17:57:41 -08001731 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001732 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001733 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001734 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001735 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001736 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001737 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001738 public Builder setWhen(long when) {
1739 mWhen = when;
1740 return this;
1741 }
1742
Joe Onoratocb109a02011-01-18 17:57:41 -08001743 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001744 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001745 * in the content view.
1746 */
1747 public Builder setShowWhen(boolean show) {
1748 mShowWhen = show;
1749 return this;
1750 }
1751
1752 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001753 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001754 *
1755 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001756 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001757 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001758 * Useful when showing an elapsed time (like an ongoing phone call).
1759 *
1760 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001761 * @see Notification#when
1762 */
1763 public Builder setUsesChronometer(boolean b) {
1764 mUseChronometer = b;
1765 return this;
1766 }
1767
1768 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001769 * Set the small icon resource, which will be used to represent the notification in the
1770 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001771 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001772
1773 * The platform template for the expanded view will draw this icon in the left, unless a
1774 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1775 * icon will be moved to the right-hand side.
1776 *
1777
1778 * @param icon
1779 * A resource ID in the application's package of the drawable to use.
1780 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001781 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001782 public Builder setSmallIcon(int icon) {
1783 mSmallIcon = icon;
1784 return this;
1785 }
1786
Joe Onoratocb109a02011-01-18 17:57:41 -08001787 /**
1788 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1789 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1790 * LevelListDrawable}.
1791 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001792 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001793 * @param level The level to use for the icon.
1794 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001795 * @see Notification#icon
1796 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001797 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001798 public Builder setSmallIcon(int icon, int level) {
1799 mSmallIcon = icon;
1800 mSmallIconLevel = level;
1801 return this;
1802 }
1803
Joe Onoratocb109a02011-01-18 17:57:41 -08001804 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001805 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001806 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001807 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001808 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001809 return this;
1810 }
1811
Joe Onoratocb109a02011-01-18 17:57:41 -08001812 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001813 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001814 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001815 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001816 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001817 return this;
1818 }
1819
Joe Onoratocb109a02011-01-18 17:57:41 -08001820 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001821 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001822 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1823 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001824 */
1825 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001826 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001827 return this;
1828 }
1829
1830 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001831 * Set the large number at the right-hand side of the notification. This is
1832 * equivalent to setContentInfo, although it might show the number in a different
1833 * font size for readability.
1834 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001835 public Builder setNumber(int number) {
1836 mNumber = number;
1837 return this;
1838 }
1839
Joe Onoratocb109a02011-01-18 17:57:41 -08001840 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001841 * A small piece of additional information pertaining to this notification.
1842 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001843 * The platform template will draw this on the last line of the notification, at the far
1844 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001845 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001846 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001847 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001848 return this;
1849 }
1850
Joe Onoratocb109a02011-01-18 17:57:41 -08001851 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001852 * Set the progress this notification represents.
1853 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001854 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001855 */
1856 public Builder setProgress(int max, int progress, boolean indeterminate) {
1857 mProgressMax = max;
1858 mProgress = progress;
1859 mProgressIndeterminate = indeterminate;
1860 return this;
1861 }
1862
1863 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001864 * Supply a custom RemoteViews to use instead of the platform template.
1865 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001866 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001867 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001868 public Builder setContent(RemoteViews views) {
1869 mContentView = views;
1870 return this;
1871 }
1872
Joe Onoratocb109a02011-01-18 17:57:41 -08001873 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001874 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1875 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001876 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1877 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1878 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001879 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001880 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001881 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001882 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001883 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001884 public Builder setContentIntent(PendingIntent intent) {
1885 mContentIntent = intent;
1886 return this;
1887 }
1888
Joe Onoratocb109a02011-01-18 17:57:41 -08001889 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001890 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1891 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001892 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001893 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001894 public Builder setDeleteIntent(PendingIntent intent) {
1895 mDeleteIntent = intent;
1896 return this;
1897 }
1898
Joe Onoratocb109a02011-01-18 17:57:41 -08001899 /**
1900 * An intent to launch instead of posting the notification to the status bar.
1901 * Only for use with extremely high-priority notifications demanding the user's
1902 * <strong>immediate</strong> attention, such as an incoming phone call or
1903 * alarm clock that the user has explicitly set to a particular time.
1904 * If this facility is used for something else, please give the user an option
1905 * to turn it off and use a normal notification, as this can be extremely
1906 * disruptive.
1907 *
1908 * @param intent The pending intent to launch.
1909 * @param highPriority Passing true will cause this notification to be sent
1910 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001911 *
1912 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001913 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001914 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1915 mFullScreenIntent = intent;
1916 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1917 return this;
1918 }
1919
Joe Onoratocb109a02011-01-18 17:57:41 -08001920 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001921 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001922 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001923 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001924 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001925 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001926 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001927 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001928 return this;
1929 }
1930
Joe Onoratocb109a02011-01-18 17:57:41 -08001931 /**
1932 * Set the text that is displayed in the status bar when the notification first
1933 * arrives, and also a RemoteViews object that may be displayed instead on some
1934 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001935 *
1936 * @see Notification#tickerText
1937 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001938 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001939 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001940 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001941 mTickerView = views;
1942 return this;
1943 }
1944
Joe Onoratocb109a02011-01-18 17:57:41 -08001945 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001946 * Add a large icon to the notification (and the ticker on some devices).
1947 *
1948 * In the platform template, this image will be shown on the left of the notification view
1949 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1950 *
1951 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001952 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001953 public Builder setLargeIcon(Bitmap icon) {
1954 mLargeIcon = icon;
1955 return this;
1956 }
1957
Joe Onoratocb109a02011-01-18 17:57:41 -08001958 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001959 * Set the sound to play.
1960 *
1961 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1962 *
1963 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001964 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001965 public Builder setSound(Uri sound) {
1966 mSound = sound;
1967 mAudioStreamType = STREAM_DEFAULT;
1968 return this;
1969 }
1970
Joe Onoratocb109a02011-01-18 17:57:41 -08001971 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001972 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001973 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001974 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1975 *
1976 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001977 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001978 public Builder setSound(Uri sound, int streamType) {
1979 mSound = sound;
1980 mAudioStreamType = streamType;
1981 return this;
1982 }
1983
Joe Onoratocb109a02011-01-18 17:57:41 -08001984 /**
1985 * Set the vibration pattern to use.
1986 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001987
1988 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1989 * <code>pattern</code> parameter.
1990 *
1991
1992 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001993 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001994 public Builder setVibrate(long[] pattern) {
1995 mVibrate = pattern;
1996 return this;
1997 }
1998
Joe Onoratocb109a02011-01-18 17:57:41 -08001999 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002000 * Set the desired color for the indicator LED on the device, as well as the
2001 * blink duty cycle (specified in milliseconds).
2002 *
2003
2004 * Not all devices will honor all (or even any) of these values.
2005 *
2006
2007 * @see Notification#ledARGB
2008 * @see Notification#ledOnMS
2009 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002010 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002011 public Builder setLights(int argb, int onMs, int offMs) {
2012 mLedArgb = argb;
2013 mLedOnMs = onMs;
2014 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002015 return this;
2016 }
2017
Joe Onoratocb109a02011-01-18 17:57:41 -08002018 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002019 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002020 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002021
2022 * Ongoing notifications cannot be dismissed by the user, so your application or service
2023 * must take care of canceling them.
2024 *
2025
2026 * They are typically used to indicate a background task that the user is actively engaged
2027 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2028 * (e.g., a file download, sync operation, active network connection).
2029 *
2030
2031 * @see Notification#FLAG_ONGOING_EVENT
2032 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002033 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002034 public Builder setOngoing(boolean ongoing) {
2035 setFlag(FLAG_ONGOING_EVENT, ongoing);
2036 return this;
2037 }
2038
Joe Onoratocb109a02011-01-18 17:57:41 -08002039 /**
2040 * Set this flag if you would only like the sound, vibrate
2041 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002042 *
2043 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002044 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002045 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2046 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2047 return this;
2048 }
2049
Joe Onoratocb109a02011-01-18 17:57:41 -08002050 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002051 * Make this notification automatically dismissed when the user touches it. The
2052 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2053 *
2054 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002055 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002056 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002057 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002058 return this;
2059 }
2060
Joe Onoratocb109a02011-01-18 17:57:41 -08002061 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002062 * Set whether or not this notification should not bridge to other devices.
2063 *
2064 * <p>Some notifications can be bridged to other devices for remote display.
2065 * This hint can be set to recommend this notification not be bridged.
2066 */
2067 public Builder setLocalOnly(boolean localOnly) {
2068 setFlag(FLAG_LOCAL_ONLY, localOnly);
2069 return this;
2070 }
2071
2072 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002073 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002074 * <p>
2075 * The value should be one or more of the following fields combined with
2076 * bitwise-or:
2077 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2078 * <p>
2079 * For all default values, use {@link #DEFAULT_ALL}.
2080 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002081 public Builder setDefaults(int defaults) {
2082 mDefaults = defaults;
2083 return this;
2084 }
2085
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002086 /**
2087 * Set the priority of this notification.
2088 *
2089 * @see Notification#priority
2090 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002091 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092 mPriority = pri;
2093 return this;
2094 }
Joe Malin8d40d042012-11-05 11:36:40 -08002095
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002096 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002097 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002098 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002099 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002100 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002101 public Builder setCategory(String category) {
2102 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002103 return this;
2104 }
2105
2106 /**
Chris Wrendde75302014-03-26 17:24:15 -04002107 * Add a person that is relevant to this notification.
2108 *
2109 * @see Notification#EXTRA_PEOPLE
2110 */
2111 public Builder addPerson(String handle) {
2112 mPeople.add(handle);
2113 return this;
2114 }
2115
2116 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002117 * Set this notification to be part of a group of notifications sharing the same key.
2118 * Grouped notifications may display in a cluster or stack on devices which
2119 * support such rendering.
2120 *
2121 * <p>To make this notification the summary for its group, also call
2122 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2123 * {@link #setSortKey}.
2124 * @param groupKey The group key of the group.
2125 * @return this object for method chaining
2126 */
2127 public Builder setGroup(String groupKey) {
2128 mGroupKey = groupKey;
2129 return this;
2130 }
2131
2132 /**
2133 * Set this notification to be the group summary for a group of notifications.
2134 * Grouped notifications may display in a cluster or stack on devices which
2135 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2136 * @param isGroupSummary Whether this notification should be a group summary.
2137 * @return this object for method chaining
2138 */
2139 public Builder setGroupSummary(boolean isGroupSummary) {
2140 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2141 return this;
2142 }
2143
2144 /**
2145 * Set a sort key that orders this notification among other notifications from the
2146 * same package. This can be useful if an external sort was already applied and an app
2147 * would like to preserve this. Notifications will be sorted lexicographically using this
2148 * value, although providing different priorities in addition to providing sort key may
2149 * cause this value to be ignored.
2150 *
2151 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002152 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002153 *
2154 * @see String#compareTo(String)
2155 */
2156 public Builder setSortKey(String sortKey) {
2157 mSortKey = sortKey;
2158 return this;
2159 }
2160
2161 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002162 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002163 *
Griff Hazen720042b2014-02-24 15:46:56 -08002164 * <p>Values within the Bundle will replace existing extras values in this Builder.
2165 *
2166 * @see Notification#extras
2167 */
Griff Hazen959591e2014-05-15 22:26:18 -07002168 public Builder addExtras(Bundle extras) {
2169 if (extras != null) {
2170 if (mExtras == null) {
2171 mExtras = new Bundle(extras);
2172 } else {
2173 mExtras.putAll(extras);
2174 }
Griff Hazen720042b2014-02-24 15:46:56 -08002175 }
2176 return this;
2177 }
2178
2179 /**
2180 * Set metadata for this notification.
2181 *
2182 * <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 -04002183 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002184 * called.
2185 *
Griff Hazen720042b2014-02-24 15:46:56 -08002186 * <p>Replaces any existing extras values with those from the provided Bundle.
2187 * Use {@link #addExtras} to merge in metadata instead.
2188 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002189 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002190 */
Griff Hazen959591e2014-05-15 22:26:18 -07002191 public Builder setExtras(Bundle extras) {
2192 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002193 return this;
2194 }
2195
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002196 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002197 * Get the current metadata Bundle used by this notification Builder.
2198 *
2199 * <p>The returned Bundle is shared with this Builder.
2200 *
2201 * <p>The current contents of this Bundle are copied into the Notification each time
2202 * {@link #build()} is called.
2203 *
2204 * @see Notification#extras
2205 */
2206 public Bundle getExtras() {
2207 if (mExtras == null) {
2208 mExtras = new Bundle();
2209 }
2210 return mExtras;
2211 }
2212
2213 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002214 * Add an action to this notification. Actions are typically displayed by
2215 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002216 * <p>
2217 * Every action must have an icon (32dp square and matching the
2218 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2219 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2220 * <p>
2221 * A notification in its expanded form can display up to 3 actions, from left to right in
2222 * the order they were added. Actions will not be displayed when the notification is
2223 * collapsed, however, so be sure that any essential functions may be accessed by the user
2224 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002225 *
2226 * @param icon Resource ID of a drawable that represents the action.
2227 * @param title Text describing the action.
2228 * @param intent PendingIntent to be fired when the action is invoked.
2229 */
2230 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002231 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002232 return this;
2233 }
2234
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002235 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002236 * Add an action to this notification. Actions are typically displayed by
2237 * the system as a button adjacent to the notification content.
2238 * <p>
2239 * Every action must have an icon (32dp square and matching the
2240 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2241 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2242 * <p>
2243 * A notification in its expanded form can display up to 3 actions, from left to right in
2244 * the order they were added. Actions will not be displayed when the notification is
2245 * collapsed, however, so be sure that any essential functions may be accessed by the user
2246 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2247 *
2248 * @param action The action to add.
2249 */
2250 public Builder addAction(Action action) {
2251 mActions.add(action);
2252 return this;
2253 }
2254
2255 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002256 * Add a rich notification style to be applied at build time.
2257 *
2258 * @param style Object responsible for modifying the notification style.
2259 */
2260 public Builder setStyle(Style style) {
2261 if (mStyle != style) {
2262 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002263 if (mStyle != null) {
2264 mStyle.setBuilder(this);
2265 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002266 }
2267 return this;
2268 }
2269
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002270 /**
2271 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002272 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002273 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2274 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2275 *
2276 * @return The same Builder.
2277 */
2278 public Builder setVisibility(int visibility) {
2279 mVisibility = visibility;
2280 return this;
2281 }
2282
2283 /**
2284 * Supply a replacement Notification whose contents should be shown in insecure contexts
2285 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2286 * @param n A replacement notification, presumably with some or all info redacted.
2287 * @return The same Builder.
2288 */
2289 public Builder setPublicVersion(Notification n) {
2290 mPublicVersion = n;
2291 return this;
2292 }
2293
Griff Hazenb720abe2014-05-20 13:15:30 -07002294 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002295 * Apply an extender to this notification builder. Extenders may be used to add
2296 * metadata or change options on this builder.
2297 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002298 public Builder extend(Extender extender) {
2299 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002300 return this;
2301 }
2302
Joe Onorato46439ce2010-11-19 13:56:21 -08002303 private void setFlag(int mask, boolean value) {
2304 if (value) {
2305 mFlags |= mask;
2306 } else {
2307 mFlags &= ~mask;
2308 }
2309 }
2310
Dan Sandler26e81cf2014-05-06 10:01:27 -04002311 /**
2312 * Sets {@link Notification#color}.
2313 *
2314 * @param argb The accent color to use
2315 *
2316 * @return The same Builder.
2317 */
2318 public Builder setColor(int argb) {
2319 mColor = argb;
2320 return this;
2321 }
2322
Kenny Guy8a0101b2014-05-08 23:34:12 +01002323 private Bitmap getProfileBadge() {
2324 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
2325 Drawable badge = userManager.getBadgeForUser(android.os.Process.myUserHandle());
2326 if (badge == null) {
2327 return null;
2328 }
2329 final int width = badge.getIntrinsicWidth();
2330 final int height = badge.getIntrinsicHeight();
2331 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2332 Canvas canvas = new Canvas(bitmap);
2333 badge.setBounds(0, 0, width, height);
2334 badge.draw(canvas);
2335 return bitmap;
2336 }
2337
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002338 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Kenny Guy8a0101b2014-05-08 23:34:12 +01002339 Bitmap profileIcon = getProfileBadge();
Joe Onorato561d3852010-11-20 18:09:34 -08002340 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002341 boolean showLine3 = false;
2342 boolean showLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002343
Dan Sandler26e81cf2014-05-06 10:01:27 -04002344 if (mPriority < PRIORITY_LOW) {
2345 // TODO: Low priority presentation
Daniel Sandlere95658c2012-05-10 00:33:54 -04002346 }
Kenny Guy8a0101b2014-05-08 23:34:12 +01002347 if (profileIcon != null) {
2348 contentView.setImageViewBitmap(R.id.profile_icon, profileIcon);
2349 contentView.setViewVisibility(R.id.profile_icon, View.VISIBLE);
2350 } else {
2351 contentView.setViewVisibility(R.id.profile_icon, View.GONE);
2352 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002353 if (mLargeIcon != null) {
2354 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002355 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002356 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2357 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2358 processSmallRightIcon(mSmallIcon, contentView);
2359 } else { // small icon at left
2360 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2361 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2362 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002363 }
2364 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002365 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002366 }
2367 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002368 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002369 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002370 }
2371 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002372 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002373 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002374 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002375 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002376 final int tooBig = mContext.getResources().getInteger(
2377 R.integer.status_bar_notification_info_maxnum);
2378 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002379 contentView.setTextViewText(R.id.info, processLegacyText(
2380 mContext.getResources().getString(
2381 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002382 } else {
2383 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002384 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002385 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002386 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002387 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002388 } else {
2389 contentView.setViewVisibility(R.id.info, View.GONE);
2390 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002391
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002392 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002393 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002394 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002395 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002396 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002397 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2398 showLine2 = true;
2399 } else {
2400 contentView.setViewVisibility(R.id.text2, View.GONE);
2401 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002402 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002403 contentView.setViewVisibility(R.id.text2, View.GONE);
2404 if (mProgressMax != 0 || mProgressIndeterminate) {
2405 contentView.setProgressBar(
2406 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2407 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002408 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002409 } else {
2410 contentView.setViewVisibility(R.id.progress, View.GONE);
2411 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002412 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002413 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002414 if (fitIn1U) {
2415 // need to shrink all the type to make sure everything fits
2416 final Resources res = mContext.getResources();
2417 final float subTextSize = res.getDimensionPixelSize(
2418 R.dimen.notification_subtext_size);
2419 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2420 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002421 // vertical centering
2422 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2423 }
2424
Daniel Sandler0c890492012-09-12 17:23:10 -07002425 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002426 if (mUseChronometer) {
2427 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2428 contentView.setLong(R.id.chronometer, "setBase",
2429 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2430 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2431 } else {
2432 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2433 contentView.setLong(R.id.time, "setTime", mWhen);
2434 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002435 } else {
2436 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002437 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002438
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002439 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002440 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002441 return contentView;
2442 }
2443
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002444 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002445 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002446
2447 int N = mActions.size();
2448 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002449 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002450 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002451 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002452 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002453 for (int i=0; i<N; i++) {
2454 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002455 big.addView(R.id.actions, button);
2456 }
2457 }
2458 return big;
2459 }
2460
Joe Onorato46439ce2010-11-19 13:56:21 -08002461 private RemoteViews makeContentView() {
2462 if (mContentView != null) {
2463 return mContentView;
2464 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002465 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002466 }
2467 }
2468
2469 private RemoteViews makeTickerView() {
2470 if (mTickerView != null) {
2471 return mTickerView;
2472 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08002473 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002474 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08002475 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002476 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08002477 } else {
2478 return null;
2479 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002480 }
2481 }
2482
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002483 private RemoteViews makeBigContentView() {
2484 if (mActions.size() == 0) return null;
2485
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002486 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002487 }
2488
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002489 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002490 if (mActions.size() == 0) return null;
2491
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002492 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002493 }
2494
2495
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002496 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002497 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002498 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002499 tombstone ? getActionTombstoneLayoutResource()
2500 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002501 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002502 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002503 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002504 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002505 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002506 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002507 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002508 return button;
2509 }
2510
Joe Onoratocb109a02011-01-18 17:57:41 -08002511 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002512 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002513 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002514 */
2515 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002516 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002517 }
2518
2519 private void processLegacyAction(Action action, RemoteViews button) {
2520 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002521 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002522 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2523 mContext.getResources().getColor(
2524 R.color.notification_action_legacy_color_filter),
2525 PorterDuff.Mode.MULTIPLY);
2526 }
2527 }
2528 }
2529
2530 private CharSequence processLegacyText(CharSequence charSequence) {
2531 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002532 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002533 } else {
2534 return charSequence;
2535 }
2536 }
2537
Dan Sandler26e81cf2014-05-06 10:01:27 -04002538 /**
2539 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2540 */
2541 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2542 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2543 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002544 }
2545 }
2546
Dan Sandler26e81cf2014-05-06 10:01:27 -04002547 /**
2548 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2549 * if it's grayscale).
2550 */
2551 // TODO: also check bounds, transparency, that sort of thing.
2552 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2553 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2554 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002555 } else {
2556 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002557 }
2558 }
2559
Dan Sandler26e81cf2014-05-06 10:01:27 -04002560 /**
2561 * Add a colored circle behind the largeIcon slot.
2562 */
2563 private void applyLargeIconBackground(RemoteViews contentView) {
2564 contentView.setInt(R.id.icon, "setBackgroundResource",
2565 R.drawable.notification_icon_legacy_bg_inset);
2566
2567 contentView.setDrawableParameters(
2568 R.id.icon,
2569 true,
2570 -1,
2571 mColor,
2572 PorterDuff.Mode.SRC_ATOP,
2573 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002574 }
2575
Dan Sandler190d58d2014-05-15 09:33:39 -04002576 private void removeLargeIconBackground(RemoteViews contentView) {
2577 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2578 }
2579
Dan Sandler26e81cf2014-05-06 10:01:27 -04002580 /**
2581 * Recolor small icons when used in the R.id.right_icon slot.
2582 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002583 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002584 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002585 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002586 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2587 0xFFFFFFFF,
2588 PorterDuff.Mode.SRC_ATOP, -1);
2589
2590 contentView.setInt(R.id.right_icon,
2591 "setBackgroundResource",
2592 R.drawable.notification_icon_legacy_bg);
2593
2594 contentView.setDrawableParameters(
2595 R.id.right_icon,
2596 true,
2597 -1,
2598 mColor,
2599 PorterDuff.Mode.SRC_ATOP,
2600 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002601 }
2602 }
2603
Dan Sandler26e81cf2014-05-06 10:01:27 -04002604 private int resolveColor() {
2605 if (mColor == COLOR_DEFAULT) {
2606 mColor = mContext.getResources().getColor(R.color.notification_icon_bg_color);
2607 } else {
2608 mColor |= 0xFF000000; // no alpha for custom colors
2609 }
2610 return mColor;
2611 }
2612
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002613 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002614 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002615 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002616 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002617 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002618 Notification n = new Notification();
2619 n.when = mWhen;
2620 n.icon = mSmallIcon;
2621 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002622 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002623
2624 n.color = resolveColor();
2625
Joe Onorato46439ce2010-11-19 13:56:21 -08002626 n.contentView = makeContentView();
2627 n.contentIntent = mContentIntent;
2628 n.deleteIntent = mDeleteIntent;
2629 n.fullScreenIntent = mFullScreenIntent;
2630 n.tickerText = mTickerText;
2631 n.tickerView = makeTickerView();
2632 n.largeIcon = mLargeIcon;
2633 n.sound = mSound;
2634 n.audioStreamType = mAudioStreamType;
2635 n.vibrate = mVibrate;
2636 n.ledARGB = mLedArgb;
2637 n.ledOnMS = mLedOnMs;
2638 n.ledOffMS = mLedOffMs;
2639 n.defaults = mDefaults;
2640 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002641 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002642 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002643 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002644 n.flags |= FLAG_SHOW_LIGHTS;
2645 }
2646 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2647 n.flags |= FLAG_SHOW_LIGHTS;
2648 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002649 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002650 n.mGroupKey = mGroupKey;
2651 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002652 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002653 if (mActions.size() > 0) {
2654 n.actions = new Action[mActions.size()];
2655 mActions.toArray(n.actions);
2656 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002657 n.visibility = mVisibility;
2658
2659 if (mPublicVersion != null) {
2660 n.publicVersion = new Notification();
2661 mPublicVersion.cloneInto(n.publicVersion, true);
2662 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002663
Joe Onorato46439ce2010-11-19 13:56:21 -08002664 return n;
2665 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002666
2667 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002668 * Capture, in the provided bundle, semantic information used in the construction of
2669 * this Notification object.
2670 * @hide
2671 */
Griff Hazen720042b2014-02-24 15:46:56 -08002672 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002673 // Store original information used in the construction of this object
2674 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2675 extras.putCharSequence(EXTRA_TEXT, mContentText);
2676 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2677 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2678 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2679 extras.putInt(EXTRA_PROGRESS, mProgress);
2680 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2681 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2682 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2683 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002684 if (mLargeIcon != null) {
2685 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2686 }
Chris Wrendde75302014-03-26 17:24:15 -04002687 if (!mPeople.isEmpty()) {
2688 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2689 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002690 }
2691
2692 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002693 * @deprecated Use {@link #build()} instead.
2694 */
2695 @Deprecated
2696 public Notification getNotification() {
2697 return build();
2698 }
2699
2700 /**
2701 * Combine all of the options that have been set and return a new {@link Notification}
2702 * object.
2703 */
2704 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002705 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002706
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002707 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002708 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002709 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002710
2711 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2712
Griff Hazen720042b2014-02-24 15:46:56 -08002713 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002714 if (mStyle != null) {
2715 mStyle.addExtras(n.extras);
2716 }
2717
2718 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002719 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002720
2721 /**
2722 * Apply this Builder to an existing {@link Notification} object.
2723 *
2724 * @hide
2725 */
2726 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002727 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002728 return n;
2729 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002730
2731
2732 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002733 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002734 }
2735
2736 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002737 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002738 }
2739
2740 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002741 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002742 }
2743
2744 private int getBigTextLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002745 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002746 }
2747
2748 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002749 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002750 }
2751
2752 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002753 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002754 }
2755
2756 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002757 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002758 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002759 }
2760
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002761 /**
2762 * An object that can apply a rich notification style to a {@link Notification.Builder}
2763 * object.
2764 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002765 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002766 private CharSequence mBigContentTitle;
2767 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002768 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002769
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002770 protected Builder mBuilder;
2771
Chris Wrend6297db2012-05-03 16:20:13 -04002772 /**
2773 * Overrides ContentTitle in the big form of the template.
2774 * This defaults to the value passed to setContentTitle().
2775 */
2776 protected void internalSetBigContentTitle(CharSequence title) {
2777 mBigContentTitle = title;
2778 }
2779
2780 /**
2781 * Set the first line of text after the detail section in the big form of the template.
2782 */
2783 protected void internalSetSummaryText(CharSequence cs) {
2784 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002785 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002786 }
2787
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002788 public void setBuilder(Builder builder) {
2789 if (mBuilder != builder) {
2790 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002791 if (mBuilder != null) {
2792 mBuilder.setStyle(this);
2793 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002794 }
2795 }
2796
Chris Wrend6297db2012-05-03 16:20:13 -04002797 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002798 if (mBuilder == null) {
2799 throw new IllegalArgumentException("Style requires a valid Builder object");
2800 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002801 }
Chris Wrend6297db2012-05-03 16:20:13 -04002802
2803 protected RemoteViews getStandardView(int layoutId) {
2804 checkBuilder();
2805
2806 if (mBigContentTitle != null) {
2807 mBuilder.setContentTitle(mBigContentTitle);
2808 }
2809
Chris Wrend6297db2012-05-03 16:20:13 -04002810 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2811
Chris Wrend6297db2012-05-03 16:20:13 -04002812 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2813 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002814 } else {
2815 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002816 }
2817
Daniel Sandler619738c2012-06-07 16:33:08 -04002818 // The last line defaults to the subtext, but can be replaced by mSummaryText
2819 final CharSequence overflowText =
2820 mSummaryTextSet ? mSummaryText
2821 : mBuilder.mSubText;
2822 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002823 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04002824 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002825 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002826 } else {
2827 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2828 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002829 }
2830
2831 return contentView;
2832 }
2833
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002834 /**
2835 * @hide
2836 */
2837 public void addExtras(Bundle extras) {
2838 if (mSummaryTextSet) {
2839 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2840 }
2841 if (mBigContentTitle != null) {
2842 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2843 }
Chris Wren91ad5632013-06-05 15:05:57 -04002844 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002845 }
2846
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002847 /**
2848 * @hide
2849 */
2850 public abstract Notification buildStyled(Notification wip);
2851
2852 /**
2853 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2854 * attached to.
2855 *
2856 * @return the fully constructed Notification.
2857 */
2858 public Notification build() {
2859 checkBuilder();
2860 return mBuilder.build();
2861 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002862 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002863
2864 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002865 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002866 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002867 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002868 * <pre class="prettyprint">
2869 * Notification noti = new Notification.BigPictureStyle(
2870 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002871 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002872 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002873 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002874 * .setLargeIcon(aBitmap))
2875 * .bigPicture(aBigBitmap)
2876 * .build();
2877 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002878 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002879 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002880 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002881 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002882 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002883 private Bitmap mBigLargeIcon;
2884 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002885
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002886 public BigPictureStyle() {
2887 }
2888
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002889 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002890 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002891 }
2892
Chris Wrend6297db2012-05-03 16:20:13 -04002893 /**
2894 * Overrides ContentTitle in the big form of the template.
2895 * This defaults to the value passed to setContentTitle().
2896 */
2897 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002898 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002899 return this;
2900 }
2901
2902 /**
2903 * Set the first line of text after the detail section in the big form of the template.
2904 */
2905 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002906 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002907 return this;
2908 }
2909
Chris Wren0bd664d2012-08-01 13:56:56 -04002910 /**
2911 * Provide the bitmap to be used as the payload for the BigPicture notification.
2912 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002913 public BigPictureStyle bigPicture(Bitmap b) {
2914 mPicture = b;
2915 return this;
2916 }
2917
Chris Wren3745a3d2012-05-22 15:11:52 -04002918 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002919 * Override the large icon when the big notification is shown.
2920 */
2921 public BigPictureStyle bigLargeIcon(Bitmap b) {
2922 mBigLargeIconSet = true;
2923 mBigLargeIcon = b;
2924 return this;
2925 }
2926
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002927 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002928 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002929
2930 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2931
2932 return contentView;
2933 }
2934
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002935 /**
2936 * @hide
2937 */
2938 public void addExtras(Bundle extras) {
2939 super.addExtras(extras);
2940
2941 if (mBigLargeIconSet) {
2942 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2943 }
2944 extras.putParcelable(EXTRA_PICTURE, mPicture);
2945 }
2946
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002947 /**
2948 * @hide
2949 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002950 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002951 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002952 if (mBigLargeIconSet ) {
2953 mBuilder.mLargeIcon = mBigLargeIcon;
2954 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002955 wip.bigContentView = makeBigContentView();
2956 return wip;
2957 }
2958 }
2959
2960 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002961 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002962 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002963 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002964 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05002965 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002966 * new Notification.Builder()
2967 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2968 * .setContentText(subject)
2969 * .setSmallIcon(R.drawable.new_mail)
2970 * .setLargeIcon(aBitmap))
2971 * .bigText(aVeryLongString)
2972 * .build();
2973 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002974 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002975 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002976 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002977 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002978 private CharSequence mBigText;
2979
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002980 public BigTextStyle() {
2981 }
2982
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002983 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002984 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002985 }
2986
Chris Wrend6297db2012-05-03 16:20:13 -04002987 /**
2988 * Overrides ContentTitle in the big form of the template.
2989 * This defaults to the value passed to setContentTitle().
2990 */
2991 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002992 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002993 return this;
2994 }
2995
2996 /**
2997 * Set the first line of text after the detail section in the big form of the template.
2998 */
2999 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003000 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003001 return this;
3002 }
3003
Chris Wren0bd664d2012-08-01 13:56:56 -04003004 /**
3005 * Provide the longer text to be displayed in the big form of the
3006 * template in place of the content text.
3007 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003008 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003009 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003010 return this;
3011 }
3012
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003013 /**
3014 * @hide
3015 */
3016 public void addExtras(Bundle extras) {
3017 super.addExtras(extras);
3018
3019 extras.putCharSequence(EXTRA_TEXT, mBigText);
3020 }
3021
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003022 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003023 // Remove the content text so line3 only shows if you have a summary
3024 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003025 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003026
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003027 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003028
Daniel Sandler619738c2012-06-07 16:33:08 -04003029 if (hadThreeLines) {
3030 // vertical centering
3031 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
3032 }
3033
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003034 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003035 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003036 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003037
3038 return contentView;
3039 }
3040
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003041 /**
3042 * @hide
3043 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003044 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003045 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003046 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003047
3048 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
3049
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003050 return wip;
3051 }
3052 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003053
3054 /**
3055 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003056 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003057 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
3058 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003059 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04003060 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04003061 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04003062 * .setContentText(subject)
3063 * .setSmallIcon(R.drawable.new_mail)
3064 * .setLargeIcon(aBitmap))
3065 * .addLine(str1)
3066 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04003067 * .setContentTitle("")
3068 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04003069 * .build();
3070 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003071 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003072 * @see Notification#bigContentView
3073 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003074 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003075 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3076
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003077 public InboxStyle() {
3078 }
3079
Daniel Sandler879c5e02012-04-17 16:46:51 -04003080 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003081 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003082 }
3083
Chris Wrend6297db2012-05-03 16:20:13 -04003084 /**
3085 * Overrides ContentTitle in the big form of the template.
3086 * This defaults to the value passed to setContentTitle().
3087 */
3088 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003089 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003090 return this;
3091 }
3092
3093 /**
3094 * Set the first line of text after the detail section in the big form of the template.
3095 */
3096 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003097 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003098 return this;
3099 }
3100
Chris Wren0bd664d2012-08-01 13:56:56 -04003101 /**
3102 * Append a line to the digest section of the Inbox notification.
3103 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003104 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003105 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003106 return this;
3107 }
3108
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003109 /**
3110 * @hide
3111 */
3112 public void addExtras(Bundle extras) {
3113 super.addExtras(extras);
3114 CharSequence[] a = new CharSequence[mTexts.size()];
3115 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3116 }
3117
Daniel Sandler879c5e02012-04-17 16:46:51 -04003118 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003119 // Remove the content text so line3 disappears unless you have a summary
3120 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003121 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003122
Chris Wrend6297db2012-05-03 16:20:13 -04003123 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003124
Chris Wrend6297db2012-05-03 16:20:13 -04003125 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 -04003126 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003127
Chris Wren4ed80d52012-05-17 09:30:03 -04003128 // Make sure all rows are gone in case we reuse a view.
3129 for (int rowId : rowIds) {
3130 contentView.setViewVisibility(rowId, View.GONE);
3131 }
3132
Chris Wren683ab002012-09-20 10:35:54 -04003133
Daniel Sandler879c5e02012-04-17 16:46:51 -04003134 int i=0;
3135 while (i < mTexts.size() && i < rowIds.length) {
3136 CharSequence str = mTexts.get(i);
3137 if (str != null && !str.equals("")) {
3138 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003139 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003140 }
3141 i++;
3142 }
3143
Chris Wren683ab002012-09-20 10:35:54 -04003144 contentView.setViewVisibility(R.id.inbox_end_pad,
3145 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3146
3147 contentView.setViewVisibility(R.id.inbox_more,
3148 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003149
Daniel Sandler879c5e02012-04-17 16:46:51 -04003150 return contentView;
3151 }
3152
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003153 /**
3154 * @hide
3155 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003156 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003157 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003158 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003159
Daniel Sandler879c5e02012-04-17 16:46:51 -04003160 return wip;
3161 }
3162 }
Dan Sandler842dd772014-05-15 09:36:47 -04003163
3164 /**
3165 * Notification style for media playback notifications.
3166 *
3167 * In the expanded form, {@link Notification#bigContentView}, up to 5
3168 * {@link Notification.Action}s specified with
3169 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3170 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3171 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3172 * treated as album artwork.
3173 *
3174 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3175 * {@link Notification#contentView}; by providing action indices to
3176 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3177 * in the standard view alongside the usual content.
3178 *
3179 * Finally, if you attach a {@link android.media.session.MediaSessionToken} using
3180 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSessionToken)},
3181 * the System UI can identify this as a notification representing an active media session
3182 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3183 *
3184 * To use this style with your Notification, feed it to
3185 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3186 * <pre class="prettyprint">
3187 * Notification noti = new Notification.Builder()
3188 * .setSmallIcon(R.drawable.ic_stat_player)
3189 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3190 * .setContentText(&quot;Artist - Album&quot;) // if you use
3191 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token, true)
3192 * .setMediaSession(mySession, true)
3193 * .setStyle(<b>new Notification.MediaStyle()</b>)
3194 * .build();
3195 * </pre>
3196 *
3197 * @see Notification#bigContentView
3198 */
3199 public static class MediaStyle extends Style {
3200 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3201 static final int MAX_MEDIA_BUTTONS = 5;
3202
3203 private int[] mActionsToShowInCompact = null;
3204 private MediaSessionToken mToken;
3205
3206 public MediaStyle() {
3207 }
3208
3209 public MediaStyle(Builder builder) {
3210 setBuilder(builder);
3211 }
3212
3213 /**
3214 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3215 * notification view.
3216 */
3217 public MediaStyle setShowActionsInCompactView(int...actions) {
3218 mActionsToShowInCompact = actions;
3219 return this;
3220 }
3221
3222 /**
3223 * Attach a {@link android.media.session.MediaSessionToken} to this Notification to provide
3224 * additional playback information and control to the SystemUI.
3225 */
3226 public MediaStyle setMediaSession(MediaSessionToken token) {
3227 mToken = token;
3228 return this;
3229 }
3230
3231 @Override
3232 public Notification buildStyled(Notification wip) {
3233 wip.contentView = makeMediaContentView();
3234 wip.bigContentView = makeMediaBigContentView();
3235
3236 return wip;
3237 }
3238
3239 /** @hide */
3240 @Override
3241 public void addExtras(Bundle extras) {
3242 super.addExtras(extras);
3243
3244 if (mToken != null) {
3245 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3246 }
3247 }
3248
3249 private RemoteViews generateMediaActionButton(Action action) {
3250 final boolean tombstone = (action.actionIntent == null);
3251 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07003252 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04003253 button.setImageViewResource(R.id.action0, action.icon);
3254 if (!tombstone) {
3255 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3256 }
3257 button.setContentDescription(R.id.action0, action.title);
3258 return button;
3259 }
3260
3261 private RemoteViews makeMediaContentView() {
3262 RemoteViews view = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003263 R.layout.notification_template_material_media, true /* 1U */);
Dan Sandler842dd772014-05-15 09:36:47 -04003264
3265 final int numActions = mBuilder.mActions.size();
3266 final int N = mActionsToShowInCompact == null
3267 ? 0
3268 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3269 if (N > 0) {
3270 view.removeAllViews(R.id.actions);
3271 for (int i = 0; i < N; i++) {
3272 if (i >= numActions) {
3273 throw new IllegalArgumentException(String.format(
3274 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3275 i, numActions - 1));
3276 }
3277
3278 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3279 final RemoteViews button = generateMediaActionButton(action);
3280 view.addView(R.id.actions, button);
3281 }
3282 }
3283 return view;
3284 }
3285
3286 private RemoteViews makeMediaBigContentView() {
3287 RemoteViews big = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003288 R.layout.notification_template_material_big_media, false);
Dan Sandler842dd772014-05-15 09:36:47 -04003289
3290 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3291 if (N > 0) {
3292 big.removeAllViews(R.id.actions);
3293 for (int i=0; i<N; i++) {
3294 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3295 big.addView(R.id.actions, button);
3296 }
3297 }
3298 return big;
3299 }
3300 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003301
3302 /**
3303 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
3304 * metadata or change options on a notification builder.
3305 */
3306 public interface Extender {
3307 /**
3308 * Apply this extender to a notification builder.
3309 * @param builder the builder to be modified.
3310 * @return the build object for chaining.
3311 */
3312 public Builder extend(Builder builder);
3313 }
3314
3315 /**
3316 * Helper class to add wearable extensions to notifications.
3317 * <p class="note"> See
3318 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
3319 * for Android Wear</a> for more information on how to use this class.
3320 * <p>
3321 * To create a notification with wearable extensions:
3322 * <ol>
3323 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
3324 * properties.
3325 * <li>Create a {@link android.app.Notification.WearableExtender}.
3326 * <li>Set wearable-specific properties using the
3327 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
3328 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
3329 * notification.
3330 * <li>Post the notification to the notification system with the
3331 * {@code NotificationManager.notify(...)} methods.
3332 * </ol>
3333 *
3334 * <pre class="prettyprint">
3335 * Notification notif = new Notification.Builder(mContext)
3336 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3337 * .setContentText(subject)
3338 * .setSmallIcon(R.drawable.new_mail)
3339 * .extend(new Notification.WearableExtender()
3340 * .setContentIcon(R.drawable.new_mail))
3341 * .build();
3342 * NotificationManager notificationManger =
3343 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3344 * notificationManger.notify(0, notif);</pre>
3345 *
3346 * <p>Wearable extensions can be accessed on an existing notification by using the
3347 * {@code WearableExtender(Notification)} constructor,
3348 * and then using the {@code get} methods to access values.
3349 *
3350 * <pre class="prettyprint">
3351 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
3352 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07003353 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003354 */
3355 public static final class WearableExtender implements Extender {
3356 /**
3357 * Sentinel value for an action index that is unset.
3358 */
3359 public static final int UNSET_ACTION_INDEX = -1;
3360
3361 /**
3362 * Size value for use with {@link #setCustomSizePreset} to show this notification with
3363 * default sizing.
3364 * <p>For custom display notifications created using {@link #setDisplayIntent},
3365 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
3366 * on their content.
3367 */
3368 public static final int SIZE_DEFAULT = 0;
3369
3370 /**
3371 * Size value for use with {@link #setCustomSizePreset} to show this notification
3372 * with an extra small size.
3373 * <p>This value is only applicable for custom display notifications created using
3374 * {@link #setDisplayIntent}.
3375 */
3376 public static final int SIZE_XSMALL = 1;
3377
3378 /**
3379 * Size value for use with {@link #setCustomSizePreset} to show this notification
3380 * with a small size.
3381 * <p>This value is only applicable for custom display notifications created using
3382 * {@link #setDisplayIntent}.
3383 */
3384 public static final int SIZE_SMALL = 2;
3385
3386 /**
3387 * Size value for use with {@link #setCustomSizePreset} to show this notification
3388 * with a medium size.
3389 * <p>This value is only applicable for custom display notifications created using
3390 * {@link #setDisplayIntent}.
3391 */
3392 public static final int SIZE_MEDIUM = 3;
3393
3394 /**
3395 * Size value for use with {@link #setCustomSizePreset} to show this notification
3396 * with a large size.
3397 * <p>This value is only applicable for custom display notifications created using
3398 * {@link #setDisplayIntent}.
3399 */
3400 public static final int SIZE_LARGE = 4;
3401
Griff Hazend5f11f92014-05-27 15:40:09 -07003402 /**
3403 * Size value for use with {@link #setCustomSizePreset} to show this notification
3404 * full screen.
3405 * <p>This value is only applicable for custom display notifications created using
3406 * {@link #setDisplayIntent}.
3407 */
3408 public static final int SIZE_FULL_SCREEN = 5;
3409
Griff Hazen61a9e862014-05-22 16:05:19 -07003410 /** Notification extra which contains wearable extensions */
3411 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
3412
3413 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
3414 private static final String KEY_ACTIONS = "actions";
3415 private static final String KEY_FLAGS = "flags";
3416 private static final String KEY_DISPLAY_INTENT = "displayIntent";
3417 private static final String KEY_PAGES = "pages";
3418 private static final String KEY_BACKGROUND = "background";
3419 private static final String KEY_CONTENT_ICON = "contentIcon";
3420 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
3421 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
3422 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
3423 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
3424 private static final String KEY_GRAVITY = "gravity";
3425
3426 // Flags bitwise-ored to mFlags
3427 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
3428 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
3429 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
3430 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
3431
3432 // Default value for flags integer
3433 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
3434
3435 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
3436 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
3437
3438 private ArrayList<Action> mActions = new ArrayList<Action>();
3439 private int mFlags = DEFAULT_FLAGS;
3440 private PendingIntent mDisplayIntent;
3441 private ArrayList<Notification> mPages = new ArrayList<Notification>();
3442 private Bitmap mBackground;
3443 private int mContentIcon;
3444 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
3445 private int mContentActionIndex = UNSET_ACTION_INDEX;
3446 private int mCustomSizePreset = SIZE_DEFAULT;
3447 private int mCustomContentHeight;
3448 private int mGravity = DEFAULT_GRAVITY;
3449
3450 /**
3451 * Create a {@link android.app.Notification.WearableExtender} with default
3452 * options.
3453 */
3454 public WearableExtender() {
3455 }
3456
3457 public WearableExtender(Notification notif) {
3458 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
3459 if (wearableBundle != null) {
3460 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
3461 if (actions != null) {
3462 mActions.addAll(actions);
3463 }
3464
3465 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
3466 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
3467
3468 Notification[] pages = getNotificationArrayFromBundle(
3469 wearableBundle, KEY_PAGES);
3470 if (pages != null) {
3471 Collections.addAll(mPages, pages);
3472 }
3473
3474 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
3475 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
3476 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
3477 DEFAULT_CONTENT_ICON_GRAVITY);
3478 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
3479 UNSET_ACTION_INDEX);
3480 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
3481 SIZE_DEFAULT);
3482 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
3483 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
3484 }
3485 }
3486
3487 /**
3488 * Apply wearable extensions to a notification that is being built. This is typically
3489 * called by the {@link android.app.Notification.Builder#extend} method of
3490 * {@link android.app.Notification.Builder}.
3491 */
3492 @Override
3493 public Notification.Builder extend(Notification.Builder builder) {
3494 Bundle wearableBundle = new Bundle();
3495
3496 if (!mActions.isEmpty()) {
3497 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
3498 }
3499 if (mFlags != DEFAULT_FLAGS) {
3500 wearableBundle.putInt(KEY_FLAGS, mFlags);
3501 }
3502 if (mDisplayIntent != null) {
3503 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
3504 }
3505 if (!mPages.isEmpty()) {
3506 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
3507 new Notification[mPages.size()]));
3508 }
3509 if (mBackground != null) {
3510 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
3511 }
3512 if (mContentIcon != 0) {
3513 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
3514 }
3515 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
3516 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
3517 }
3518 if (mContentActionIndex != UNSET_ACTION_INDEX) {
3519 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
3520 mContentActionIndex);
3521 }
3522 if (mCustomSizePreset != SIZE_DEFAULT) {
3523 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
3524 }
3525 if (mCustomContentHeight != 0) {
3526 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
3527 }
3528 if (mGravity != DEFAULT_GRAVITY) {
3529 wearableBundle.putInt(KEY_GRAVITY, mGravity);
3530 }
3531
3532 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
3533 return builder;
3534 }
3535
3536 @Override
3537 public WearableExtender clone() {
3538 WearableExtender that = new WearableExtender();
3539 that.mActions = new ArrayList<Action>(this.mActions);
3540 that.mFlags = this.mFlags;
3541 that.mDisplayIntent = this.mDisplayIntent;
3542 that.mPages = new ArrayList<Notification>(this.mPages);
3543 that.mBackground = this.mBackground;
3544 that.mContentIcon = this.mContentIcon;
3545 that.mContentIconGravity = this.mContentIconGravity;
3546 that.mContentActionIndex = this.mContentActionIndex;
3547 that.mCustomSizePreset = this.mCustomSizePreset;
3548 that.mCustomContentHeight = this.mCustomContentHeight;
3549 that.mGravity = this.mGravity;
3550 return that;
3551 }
3552
3553 /**
3554 * Add a wearable action to this notification.
3555 *
3556 * <p>When wearable actions are added using this method, the set of actions that
3557 * show on a wearable device splits from devices that only show actions added
3558 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3559 * of which actions display on different devices.
3560 *
3561 * @param action the action to add to this notification
3562 * @return this object for method chaining
3563 * @see android.app.Notification.Action
3564 */
3565 public WearableExtender addAction(Action action) {
3566 mActions.add(action);
3567 return this;
3568 }
3569
3570 /**
3571 * Adds wearable actions to this notification.
3572 *
3573 * <p>When wearable actions are added using this method, the set of actions that
3574 * show on a wearable device splits from devices that only show actions added
3575 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3576 * of which actions display on different devices.
3577 *
3578 * @param actions the actions to add to this notification
3579 * @return this object for method chaining
3580 * @see android.app.Notification.Action
3581 */
3582 public WearableExtender addActions(List<Action> actions) {
3583 mActions.addAll(actions);
3584 return this;
3585 }
3586
3587 /**
3588 * Clear all wearable actions present on this builder.
3589 * @return this object for method chaining.
3590 * @see #addAction
3591 */
3592 public WearableExtender clearActions() {
3593 mActions.clear();
3594 return this;
3595 }
3596
3597 /**
3598 * Get the wearable actions present on this notification.
3599 */
3600 public List<Action> getActions() {
3601 return mActions;
3602 }
3603
3604 /**
3605 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07003606 * this notification. The {@link PendingIntent} provided should be for an activity.
3607 *
3608 * <pre class="prettyprint">
3609 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
3610 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
3611 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3612 * Notification notif = new Notification.Builder(context)
3613 * .extend(new Notification.WearableExtender()
3614 * .setDisplayIntent(displayPendingIntent)
3615 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
3616 * .build();</pre>
3617 *
3618 * <p>The activity to launch needs to allow embedding, must be exported, and
3619 * should have an empty task affinity.
3620 *
3621 * <p>Example AndroidManifest.xml entry:
3622 * <pre class="prettyprint">
3623 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
3624 * android:exported=&quot;true&quot;
3625 * android:allowEmbedded=&quot;true&quot;
3626 * android:taskAffinity=&quot;&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003627 *
3628 * @param intent the {@link PendingIntent} for an activity
3629 * @return this object for method chaining
3630 * @see android.app.Notification.WearableExtender#getDisplayIntent
3631 */
3632 public WearableExtender setDisplayIntent(PendingIntent intent) {
3633 mDisplayIntent = intent;
3634 return this;
3635 }
3636
3637 /**
3638 * Get the intent to launch inside of an activity view when displaying this
3639 * notification. This {@code PendingIntent} should be for an activity.
3640 */
3641 public PendingIntent getDisplayIntent() {
3642 return mDisplayIntent;
3643 }
3644
3645 /**
3646 * Add an additional page of content to display with this notification. The current
3647 * notification forms the first page, and pages added using this function form
3648 * subsequent pages. This field can be used to separate a notification into multiple
3649 * sections.
3650 *
3651 * @param page the notification to add as another page
3652 * @return this object for method chaining
3653 * @see android.app.Notification.WearableExtender#getPages
3654 */
3655 public WearableExtender addPage(Notification page) {
3656 mPages.add(page);
3657 return this;
3658 }
3659
3660 /**
3661 * Add additional pages of content to display with this notification. The current
3662 * notification forms the first page, and pages added using this function form
3663 * subsequent pages. This field can be used to separate a notification into multiple
3664 * sections.
3665 *
3666 * @param pages a list of notifications
3667 * @return this object for method chaining
3668 * @see android.app.Notification.WearableExtender#getPages
3669 */
3670 public WearableExtender addPages(List<Notification> pages) {
3671 mPages.addAll(pages);
3672 return this;
3673 }
3674
3675 /**
3676 * Clear all additional pages present on this builder.
3677 * @return this object for method chaining.
3678 * @see #addPage
3679 */
3680 public WearableExtender clearPages() {
3681 mPages.clear();
3682 return this;
3683 }
3684
3685 /**
3686 * Get the array of additional pages of content for displaying this notification. The
3687 * current notification forms the first page, and elements within this array form
3688 * subsequent pages. This field can be used to separate a notification into multiple
3689 * sections.
3690 * @return the pages for this notification
3691 */
3692 public List<Notification> getPages() {
3693 return mPages;
3694 }
3695
3696 /**
3697 * Set a background image to be displayed behind the notification content.
3698 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3699 * will work with any notification style.
3700 *
3701 * @param background the background bitmap
3702 * @return this object for method chaining
3703 * @see android.app.Notification.WearableExtender#getBackground
3704 */
3705 public WearableExtender setBackground(Bitmap background) {
3706 mBackground = background;
3707 return this;
3708 }
3709
3710 /**
3711 * Get a background image to be displayed behind the notification content.
3712 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3713 * will work with any notification style.
3714 *
3715 * @return the background image
3716 * @see android.app.Notification.WearableExtender#setBackground
3717 */
3718 public Bitmap getBackground() {
3719 return mBackground;
3720 }
3721
3722 /**
3723 * Set an icon that goes with the content of this notification.
3724 */
3725 public WearableExtender setContentIcon(int icon) {
3726 mContentIcon = icon;
3727 return this;
3728 }
3729
3730 /**
3731 * Get an icon that goes with the content of this notification.
3732 */
3733 public int getContentIcon() {
3734 return mContentIcon;
3735 }
3736
3737 /**
3738 * Set 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 #setContentIcon
3742 */
3743 public WearableExtender setContentIconGravity(int contentIconGravity) {
3744 mContentIconGravity = contentIconGravity;
3745 return this;
3746 }
3747
3748 /**
3749 * Get the gravity that the content icon should have within the notification display.
3750 * Supported values include {@link android.view.Gravity#START} and
3751 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3752 * @see #getContentIcon
3753 */
3754 public int getContentIconGravity() {
3755 return mContentIconGravity;
3756 }
3757
3758 /**
3759 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07003760 * this notification. This action will no longer display separately from the
3761 * notification's content.
3762 *
Griff Hazenca48d352014-05-28 22:37:13 -07003763 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003764 * set, although the list of available actions comes from the main notification and not
3765 * from the child page's notification.
3766 *
3767 * @param actionIndex The index of the action to hoist onto the current notification page.
3768 * If wearable actions were added to the main notification, this index
3769 * will apply to that list, otherwise it will apply to the regular
3770 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07003771 */
3772 public WearableExtender setContentAction(int actionIndex) {
3773 mContentActionIndex = actionIndex;
3774 return this;
3775 }
3776
3777 /**
Griff Hazenca48d352014-05-28 22:37:13 -07003778 * Get the index of the notification action, if any, that was specified as being clickable
3779 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07003780 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07003781 *
Griff Hazenca48d352014-05-28 22:37:13 -07003782 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003783 * set, although the list of available actions comes from the main notification and not
3784 * from the child page's notification.
3785 *
3786 * <p>If wearable specific actions were added to the main notification, this index will
3787 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07003788 *
3789 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07003790 */
3791 public int getContentAction() {
3792 return mContentActionIndex;
3793 }
3794
3795 /**
3796 * Set 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 WearableExtender setGravity(int gravity) {
3802 mGravity = gravity;
3803 return this;
3804 }
3805
3806 /**
3807 * Get the gravity that this notification should have within the available viewport space.
3808 * Supported values include {@link android.view.Gravity#TOP},
3809 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3810 * The default value is {@link android.view.Gravity#BOTTOM}.
3811 */
3812 public int getGravity() {
3813 return mGravity;
3814 }
3815
3816 /**
3817 * Set the custom size preset for the display of this notification out of the available
3818 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3819 * {@link #SIZE_LARGE}.
3820 * <p>Some custom size presets are only applicable for custom display notifications created
3821 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
3822 * documentation for the preset in question. See also
3823 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
3824 */
3825 public WearableExtender setCustomSizePreset(int sizePreset) {
3826 mCustomSizePreset = sizePreset;
3827 return this;
3828 }
3829
3830 /**
3831 * Get the custom size preset for the display of this notification out of the available
3832 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3833 * {@link #SIZE_LARGE}.
3834 * <p>Some custom size presets are only applicable for custom display notifications created
3835 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
3836 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
3837 */
3838 public int getCustomSizePreset() {
3839 return mCustomSizePreset;
3840 }
3841
3842 /**
3843 * Set the custom height in pixels for the display of this notification's content.
3844 * <p>This option is only available for custom display notifications created
3845 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
3846 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
3847 * {@link #getCustomContentHeight}.
3848 */
3849 public WearableExtender setCustomContentHeight(int height) {
3850 mCustomContentHeight = height;
3851 return this;
3852 }
3853
3854 /**
3855 * Get the custom height in pixels for the display of this notification's content.
3856 * <p>This option is only available for custom display notifications created
3857 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
3858 * {@link #setCustomContentHeight}.
3859 */
3860 public int getCustomContentHeight() {
3861 return mCustomContentHeight;
3862 }
3863
3864 /**
3865 * Set whether the scrolling position for the contents of this notification should start
3866 * at the bottom of the contents instead of the top when the contents are too long to
3867 * display within the screen. Default is false (start scroll at the top).
3868 */
3869 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
3870 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
3871 return this;
3872 }
3873
3874 /**
3875 * Get whether the scrolling position for the contents of this notification should start
3876 * at the bottom of the contents instead of the top when the contents are too long to
3877 * display within the screen. Default is false (start scroll at the top).
3878 */
3879 public boolean getStartScrollBottom() {
3880 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
3881 }
3882
3883 /**
3884 * Set whether the content intent is available when the wearable device is not connected
3885 * to a companion device. The user can still trigger this intent when the wearable device
3886 * is offline, but a visual hint will indicate that the content intent may not be available.
3887 * Defaults to true.
3888 */
3889 public WearableExtender setContentIntentAvailableOffline(
3890 boolean contentIntentAvailableOffline) {
3891 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
3892 return this;
3893 }
3894
3895 /**
3896 * Get whether the content intent is available when the wearable device is not connected
3897 * to a companion device. The user can still trigger this intent when the wearable device
3898 * is offline, but a visual hint will indicate that the content intent may not be available.
3899 * Defaults to true.
3900 */
3901 public boolean getContentIntentAvailableOffline() {
3902 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
3903 }
3904
3905 /**
3906 * Set a hint that this notification's icon should not be displayed.
3907 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
3908 * @return this object for method chaining
3909 */
3910 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
3911 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
3912 return this;
3913 }
3914
3915 /**
3916 * Get a hint that this notification's icon should not be displayed.
3917 * @return {@code true} if this icon should not be displayed, false otherwise.
3918 * The default value is {@code false} if this was never set.
3919 */
3920 public boolean getHintHideIcon() {
3921 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
3922 }
3923
3924 /**
3925 * Set a visual hint that only the background image of this notification should be
3926 * displayed, and other semantic content should be hidden. This hint is only applicable
3927 * to sub-pages added using {@link #addPage}.
3928 */
3929 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
3930 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
3931 return this;
3932 }
3933
3934 /**
3935 * Get a visual hint that only the background image of this notification should be
3936 * displayed, and other semantic content should be hidden. This hint is only applicable
3937 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
3938 */
3939 public boolean getHintShowBackgroundOnly() {
3940 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
3941 }
3942
3943 private void setFlag(int mask, boolean value) {
3944 if (value) {
3945 mFlags |= mask;
3946 } else {
3947 mFlags &= ~mask;
3948 }
3949 }
3950 }
3951
3952 /**
3953 * Get an array of Notification objects from a parcelable array bundle field.
3954 * Update the bundle to have a typed array so fetches in the future don't need
3955 * to do an array copy.
3956 */
3957 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
3958 Parcelable[] array = bundle.getParcelableArray(key);
3959 if (array instanceof Notification[] || array == null) {
3960 return (Notification[]) array;
3961 }
3962 Notification[] typedArray = Arrays.copyOf(array, array.length,
3963 Notification[].class);
3964 bundle.putParcelableArray(key, typedArray);
3965 return typedArray;
3966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967}