blob: c5eb35642df77bbce8a42b7ba29749078bf4ac33 [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.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010027import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010028import android.graphics.drawable.Drawable;
Jeff Sharkey098d5802012-04-26 17:30:34 -070029import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070030import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040032import android.os.BadParcelableException;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050033import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Parcel;
35import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040036import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070037import android.os.UserHandle;
Kenny Guy8a0101b2014-05-08 23:34:12 +010038import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040040import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040041import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070042import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080043import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070044import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.widget.RemoteViews;
46
Griff Hazen959591e2014-05-15 22:26:18 -070047import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070048import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070049
Tor Norbyed9273d62013-05-30 15:59:53 -070050import java.lang.annotation.Retention;
51import java.lang.annotation.RetentionPolicy;
Andy Stadler110988c2010-12-03 14:29:16 -080052import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050053import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070054import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070055import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070056import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058/**
59 * A class that represents how a persistent notification is to be presented to
60 * the user using the {@link android.app.NotificationManager}.
61 *
Joe Onoratocb109a02011-01-18 17:57:41 -080062 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
63 * easier to construct Notifications.</p>
64 *
Joe Fernandez558459f2011-10-13 16:47:36 -070065 * <div class="special reference">
66 * <h3>Developer Guides</h3>
67 * <p>For a guide to creating notifications, read the
68 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
69 * developer guide.</p>
70 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
72public class Notification implements Parcelable
73{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040074 private static final String TAG = "Notification";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040077 * An activity that provides a user interface for adjusting notification preferences for its
78 * containing application. Optional but recommended for apps that post
79 * {@link android.app.Notification Notifications}.
80 */
81 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
82 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
83 = "android.intent.category.NOTIFICATION_PREFERENCES";
84
85 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * Use all default values (where applicable).
87 */
88 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
91 * Use the default notification sound. This will ignore any given
92 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093 *
Chris Wren47c20a12014-06-18 17:27:29 -040094 * <p>
95 * A notification that is noisy is more likely to be presented as a heads-up notification.
96 * </p>
97 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050099 */
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static final int DEFAULT_SOUND = 1;
102
103 /**
104 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500105 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700106 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500107 *
Chris Wren47c20a12014-06-18 17:27:29 -0400108 * <p>
109 * A notification that vibrates is more likely to be presented as a heads-up notification.
110 * </p>
111 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113 */
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
118 * Use the default notification lights. This will ignore the
119 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
120 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 */
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800129 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130 * Default value: {@link System#currentTimeMillis() Now}.
131 *
132 * Choose a timestamp that will be most relevant to the user. For most finite events, this
133 * corresponds to the time the event happened (or will happen, in the case of events that have
134 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800135 * timestamped according to when the activity began.
136 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800138 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 * <ul>
140 * <li>Notification of a new chat message should be stamped when the message was received.</li>
141 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
142 * <li>Notification of a completed file download should be stamped when the download finished.</li>
143 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
144 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
145 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800146 * </ul>
147 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 */
149 public long when;
150
151 /**
152 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500153 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 */
155 public int icon;
156
157 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800158 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
159 * leave it at its default value of 0.
160 *
161 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700162 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800163 */
164 public int iconLevel;
165
166 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * The number of events that this notification represents. For example, in a new mail
168 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800169 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 * The system may or may not use this field to modify the appearance of the notification. For
171 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
172 * superimposed over the icon in the status bar. Starting with
173 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
174 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800175 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 */
178 public int number;
179
180 /**
181 * The intent to execute when the expanded status entry is clicked. If
182 * this is an activity, it must include the
183 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800184 * that you take care of task management as described in the
185 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800186 * Stack</a> document. In particular, make sure to read the notification section
187 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
188 * Notifications</a> for the correct ways to launch an application from a
189 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 */
191 public PendingIntent contentIntent;
192
193 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500194 * The intent to execute when the notification is explicitly dismissed by the user, either with
195 * the "Clear All" button or by swiping it away individually.
196 *
197 * This probably shouldn't be launching an activity since several of those will be sent
198 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
200 public PendingIntent deleteIntent;
201
202 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700203 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800204 *
Chris Wren47c20a12014-06-18 17:27:29 -0400205 * <p>
206 * The system UI may choose to display a heads-up notification, instead of
207 * launching this intent, while the user is using the device.
208 * </p>
209 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800210 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400211 */
212 public PendingIntent fullScreenIntent;
213
214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400216 * the status bar on large and smaller devices.
217 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800218 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 */
220 public CharSequence tickerText;
221
222 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800223 * The view to show as the ticker in the status bar when the notification
224 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400225 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800226 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400227
228 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400229 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 */
231 public RemoteViews contentView;
232
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400233 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400234 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400235 * opportunity to show more detail. The system UI may choose to show this
236 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400237 */
238 public RemoteViews bigContentView;
239
Chris Wren8fd39ec2014-02-27 17:43:26 -0500240
241 /**
242 * @hide
Chris Wren47c20a12014-06-18 17:27:29 -0400243 * A medium-format version of {@link #contentView}, providing the Notification an
244 * opportunity to add action buttons to contentView. At its discretion, the system UI may
245 * choose to show this as a heads-up notification, which will pop up so the user can see
246 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500247 */
248 public RemoteViews headsUpContentView;
249
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400250 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800251 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800253 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
255 /**
256 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400259 * A notification that is noisy is more likely to be presented as a heads-up notification.
260 * </p>
261 *
262 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500263 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * </p>
265 */
266 public Uri sound;
267
268 /**
269 * Use this constant as the value for audioStreamType to request that
270 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700271 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public static final int STREAM_DEFAULT = -1;
274
275 /**
276 * The audio stream type to use when playing the sound.
277 * Should be one of the STREAM_ constants from
278 * {@link android.media.AudioManager}.
279 */
280 public int audioStreamType = STREAM_DEFAULT;
281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500283 * The pattern with which to vibrate.
284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * <p>
286 * To vibrate the default pattern, see {@link #defaults}.
287 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500288 *
Chris Wren47c20a12014-06-18 17:27:29 -0400289 * <p>
290 * A notification that vibrates is more likely to be presented as a heads-up notification.
291 * </p>
292 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 * @see android.os.Vibrator#vibrate(long[],int)
294 */
295 public long[] vibrate;
296
297 /**
298 * The color of the led. The hardware will do its best approximation.
299 *
300 * @see #FLAG_SHOW_LIGHTS
301 * @see #flags
302 */
303 public int ledARGB;
304
305 /**
306 * The number of milliseconds for the LED to be on while it's flashing.
307 * The hardware will do its best approximation.
308 *
309 * @see #FLAG_SHOW_LIGHTS
310 * @see #flags
311 */
312 public int ledOnMS;
313
314 /**
315 * The number of milliseconds for the LED to be off while it's flashing.
316 * The hardware will do its best approximation.
317 *
318 * @see #FLAG_SHOW_LIGHTS
319 * @see #flags
320 */
321 public int ledOffMS;
322
323 /**
324 * Specifies which values should be taken from the defaults.
325 * <p>
326 * To set, OR the desired from {@link #DEFAULT_SOUND},
327 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
328 * values, use {@link #DEFAULT_ALL}.
329 * </p>
330 */
331 public int defaults;
332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * Bit to be bitwise-ored into the {@link #flags} field that should be
335 * set if you want the LED on for this notification.
336 * <ul>
337 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
338 * or 0 for both ledOnMS and ledOffMS.</li>
339 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
340 * <li>To flash the LED, pass the number of milliseconds that it should
341 * be on and off to ledOnMS and ledOffMS.</li>
342 * </ul>
343 * <p>
344 * Since hardware varies, you are not guaranteed that any of the values
345 * you pass are honored exactly. Use the system defaults (TODO) if possible
346 * because they will be set to values that work on any given hardware.
347 * <p>
348 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500349 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 */
351 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
352
353 /**
354 * Bit to be bitwise-ored into the {@link #flags} field that should be
355 * set if this notification is in reference to something that is ongoing,
356 * like a phone call. It should not be set if this notification is in
357 * reference to something that happened at a particular point in time,
358 * like a missed phone call.
359 */
360 public static final int FLAG_ONGOING_EVENT = 0x00000002;
361
362 /**
363 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700364 * the audio will be repeated until the notification is
365 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 */
367 public static final int FLAG_INSISTENT = 0x00000004;
368
369 /**
370 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700371 * set if you would only like the sound, vibrate and ticker to be played
372 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 */
374 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
375
376 /**
377 * Bit to be bitwise-ored into the {@link #flags} field that should be
378 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500379 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 */
382 public static final int FLAG_AUTO_CANCEL = 0x00000010;
383
384 /**
385 * Bit to be bitwise-ored into the {@link #flags} field that should be
386 * set if the notification should not be canceled when the user clicks
387 * the Clear all button.
388 */
389 public static final int FLAG_NO_CLEAR = 0x00000020;
390
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700391 /**
392 * Bit to be bitwise-ored into the {@link #flags} field that should be
393 * set if this notification represents a currently running service. This
394 * will normally be set for you by {@link Service#startForeground}.
395 */
396 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
397
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400398 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500399 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800400 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500401 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400402 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400404
Griff Hazendfcb0802014-02-11 12:00:00 -0800405 /**
406 * Bit to be bitswise-ored into the {@link #flags} field that should be
407 * set if this notification is relevant to the current device only
408 * and it is not recommended that it bridge to other devices.
409 */
410 public static final int FLAG_LOCAL_ONLY = 0x00000100;
411
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700412 /**
413 * Bit to be bitswise-ored into the {@link #flags} field that should be
414 * set if this notification is the group summary for a group of notifications.
415 * Grouped notifications may display in a cluster or stack on devices which
416 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
417 */
418 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 public int flags;
421
Tor Norbyed9273d62013-05-30 15:59:53 -0700422 /** @hide */
423 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
424 @Retention(RetentionPolicy.SOURCE)
425 public @interface Priority {}
426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500428 * Default notification {@link #priority}. If your application does not prioritize its own
429 * notifications, use this value for all notifications.
430 */
431 public static final int PRIORITY_DEFAULT = 0;
432
433 /**
434 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
435 * items smaller, or at a different position in the list, compared with your app's
436 * {@link #PRIORITY_DEFAULT} items.
437 */
438 public static final int PRIORITY_LOW = -1;
439
440 /**
441 * Lowest {@link #priority}; these items might not be shown to the user except under special
442 * circumstances, such as detailed notification logs.
443 */
444 public static final int PRIORITY_MIN = -2;
445
446 /**
447 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
448 * show these items larger, or at a different position in notification lists, compared with
449 * your app's {@link #PRIORITY_DEFAULT} items.
450 */
451 public static final int PRIORITY_HIGH = 1;
452
453 /**
454 * Highest {@link #priority}, for your application's most important items that require the
455 * user's prompt attention or input.
456 */
457 public static final int PRIORITY_MAX = 2;
458
459 /**
460 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800461 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500462 * Priority is an indication of how much of the user's valuable attention should be consumed by
463 * this notification. Low-priority notifications may be hidden from the user in certain
464 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500465 * system will make a determination about how to interpret this priority when presenting
466 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400467 *
468 * <p>
469 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
470 * as a heads-up notification.
471 * </p>
472 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700474 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500475 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800476
Dan Sandler26e81cf2014-05-06 10:01:27 -0400477 /**
478 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
479 * to be applied by the standard Style templates when presenting this notification.
480 *
481 * The current template design constructs a colorful header image by overlaying the
482 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
483 * ignored.
484 */
485 public int color = COLOR_DEFAULT;
486
487 /**
488 * Special value of {@link #color} telling the system not to decorate this notification with
489 * any special color but instead use default colors when presenting this notification.
490 */
491 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600492
493 /**
494 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
495 * the notification's presence and contents in untrusted situations (namely, on the secure
496 * lockscreen).
497 *
498 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
499 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
500 * shown in all situations, but the contents are only available if the device is unlocked for
501 * the appropriate user.
502 *
503 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
504 * can be read even in an "insecure" context (that is, above a secure lockscreen).
505 * To modify the public version of this notification—for example, to redact some portions—see
506 * {@link Builder#setPublicVersion(Notification)}.
507 *
508 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
509 * and ticker until the user has bypassed the lockscreen.
510 */
511 public int visibility;
512
513 public static final int VISIBILITY_PUBLIC = 1;
514 public static final int VISIBILITY_PRIVATE = 0;
515 public static final int VISIBILITY_SECRET = -1;
516
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500517 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400518 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500519 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400520 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500521
522 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400523 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500524 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400525 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500526
527 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400528 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500529 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400530 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500531
532 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400533 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500534 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400535 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536
537 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400538 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500539 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400540 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500541
542 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400543 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500544 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400545 public static final String CATEGORY_ALARM = "alarm";
546
547 /**
548 * Notification category: progress of a long-running background operation.
549 */
550 public static final String CATEGORY_PROGRESS = "progress";
551
552 /**
553 * Notification category: social network or sharing update.
554 */
555 public static final String CATEGORY_SOCIAL = "social";
556
557 /**
558 * Notification category: error in background operation or authentication status.
559 */
560 public static final String CATEGORY_ERROR = "err";
561
562 /**
563 * Notification category: media transport control for playback.
564 */
565 public static final String CATEGORY_TRANSPORT = "transport";
566
567 /**
568 * Notification category: system or device status update. Reserved for system use.
569 */
570 public static final String CATEGORY_SYSTEM = "sys";
571
572 /**
573 * Notification category: indication of running background service.
574 */
575 public static final String CATEGORY_SERVICE = "service";
576
577 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400578 * Notification category: a specific, timely recommendation for a single thing.
579 * For example, a news app might want to recommend a news story it believes the user will
580 * want to read next.
581 */
582 public static final String CATEGORY_RECOMMENDATION = "recommendation";
583
584 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400585 * Notification category: ongoing information about device or contextual status.
586 */
587 public static final String CATEGORY_STATUS = "status";
588
589 /**
590 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
591 * that best describes this Notification. May be used by the system for ranking and filtering.
592 */
593 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500594
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700595 private String mGroupKey;
596
597 /**
598 * Get the key used to group this notification into a cluster or stack
599 * with other notifications on devices which support such rendering.
600 */
601 public String getGroup() {
602 return mGroupKey;
603 }
604
605 private String mSortKey;
606
607 /**
608 * Get a sort key that orders this notification among other notifications from the
609 * same package. This can be useful if an external sort was already applied and an app
610 * would like to preserve this. Notifications will be sorted lexicographically using this
611 * value, although providing different priorities in addition to providing sort key may
612 * cause this value to be ignored.
613 *
614 * <p>This sort key can also be used to order members of a notification group. See
615 * {@link Builder#setGroup}.
616 *
617 * @see String#compareTo(String)
618 */
619 public String getSortKey() {
620 return mSortKey;
621 }
622
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500623 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400624 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400625 * <p>
626 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
627 * APIs, and are intended to be used by
628 * {@link android.service.notification.NotificationListenerService} implementations to extract
629 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400631 public Bundle extras = new Bundle();
632
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400633 /**
634 * {@link #extras} key: this is the title of the notification,
635 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
636 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500637 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400638
639 /**
640 * {@link #extras} key: this is the title of the notification when shown in expanded form,
641 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
642 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400643 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400644
645 /**
646 * {@link #extras} key: this is the main text payload, as supplied to
647 * {@link Builder#setContentText(CharSequence)}.
648 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500649 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400650
651 /**
652 * {@link #extras} key: this is a third line of text, as supplied to
653 * {@link Builder#setSubText(CharSequence)}.
654 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400655 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400656
657 /**
658 * {@link #extras} key: this is a small piece of additional text as supplied to
659 * {@link Builder#setContentInfo(CharSequence)}.
660 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400661 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400662
663 /**
664 * {@link #extras} key: this is a line of summary information intended to be shown
665 * alongside expanded notifications, as supplied to (e.g.)
666 * {@link BigTextStyle#setSummaryText(CharSequence)}.
667 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400668 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400669
670 /**
671 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
672 * supplied to {@link Builder#setSmallIcon(int)}.
673 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500674 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400675
676 /**
677 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
678 * notification payload, as
679 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
680 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400681 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400682
683 /**
684 * {@link #extras} key: this is a bitmap to be used instead of the one from
685 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
686 * shown in its expanded form, as supplied to
687 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
688 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400689 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400690
691 /**
692 * {@link #extras} key: this is the progress value supplied to
693 * {@link Builder#setProgress(int, int, boolean)}.
694 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400695 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400696
697 /**
698 * {@link #extras} key: this is the maximum value supplied to
699 * {@link Builder#setProgress(int, int, boolean)}.
700 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400701 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400702
703 /**
704 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
705 * {@link Builder#setProgress(int, int, boolean)}.
706 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400707 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400708
709 /**
710 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
711 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
712 * {@link Builder#setUsesChronometer(boolean)}.
713 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400714 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400715
716 /**
717 * {@link #extras} key: whether {@link #when} should be shown,
718 * as supplied to {@link Builder#setShowWhen(boolean)}.
719 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400720 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400721
722 /**
723 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
724 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
725 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400726 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400727
728 /**
729 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
730 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
731 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400732 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400733
734 /**
735 * {@link #extras} key: A string representing the name of the specific
736 * {@link android.app.Notification.Style} used to create this notification.
737 */
Chris Wren91ad5632013-06-05 15:05:57 -0400738 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400739
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400740 /**
741 * {@link #extras} key: An array of people that this notification relates to, specified
742 * by contacts provider contact URI.
743 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400744 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500745
746 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400747 * @hide
Chris Wrenf9536642014-04-17 10:01:54 -0400748 * Extra added by NotificationManagerService to indicate whether
749 * the Notifications's score has been modified.
Scott Greenwald9a05b312013-06-28 00:37:54 -0400750 */
751 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
752
753 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400754 * {@link #extras} key: used to provide hints about the appropriateness of
755 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400756 * @hide
757 */
758 public static final String EXTRA_AS_HEADS_UP = "headsup";
759
760 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 * Allow certain system-generated notifications to appear before the device is provisioned.
762 * Only available to notifications coming from the android package.
763 * @hide
764 */
765 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
766
767 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700768 * {@link #extras} key: A
769 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
770 * in the background when the notification is selected. The URI must point to an image stream
771 * suitable for passing into
772 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
773 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
774 * URI used for this purpose must require no permissions to read the image data.
775 */
776 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
777
778 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400779 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700780 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400781 * {@link android.app.Notification.MediaStyle} notification.
782 */
783 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
784
785 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400786 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
787 * displayed in the heads up space.
788 *
789 * <p>
790 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
791 * full-screen intent when posted.
792 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400793 * @hide
794 */
795 public static final int HEADS_UP_NEVER = 0;
796
797 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400798 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
799 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400800 * @hide
801 */
802 public static final int HEADS_UP_ALLOWED = 1;
803
804 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400805 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
806 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400807 * @hide
808 */
809 public static final int HEADS_UP_REQUESTED = 2;
810
811 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400812 * Structure to encapsulate a named action that can be shown as part of this notification.
813 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
814 * selected by the user.
815 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700816 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
817 * or {@link Notification.Builder#addAction(Notification.Action)}
818 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400819 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500820 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700821 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700822 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700823
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400824 /**
825 * Small icon representing the action.
826 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400827 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700828
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400829 /**
830 * Title of the action.
831 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400832 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700833
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400834 /**
835 * Intent to send when the user invokes this action. May be null, in which case the action
836 * may be rendered in a disabled presentation by the system UI.
837 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400838 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700839
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400840 private Action(Parcel in) {
841 icon = in.readInt();
842 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
843 if (in.readInt() == 1) {
844 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
845 }
Griff Hazen959591e2014-05-15 22:26:18 -0700846 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700847 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400848 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700849
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400850 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700851 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400852 */
853 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700854 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700855 }
856
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700857 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
858 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400859 this.icon = icon;
860 this.title = title;
861 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700862 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700863 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700864 }
865
866 /**
867 * Get additional metadata carried around with this Action.
868 */
869 public Bundle getExtras() {
870 return mExtras;
871 }
872
873 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700874 * Get the list of inputs to be collected from the user when this action is sent.
875 * May return null if no remote inputs were added.
876 */
877 public RemoteInput[] getRemoteInputs() {
878 return mRemoteInputs;
879 }
880
881 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700882 * Builder class for {@link Action} objects.
883 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700884 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700885 private final int mIcon;
886 private final CharSequence mTitle;
887 private final PendingIntent mIntent;
888 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700889 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700890
891 /**
892 * Construct a new builder for {@link Action} object.
893 * @param icon icon to show for this action
894 * @param title the title of the action
895 * @param intent the {@link PendingIntent} to fire when users trigger this action
896 */
897 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700898 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700899 }
900
901 /**
902 * Construct a new builder for {@link Action} object using the fields from an
903 * {@link Action}.
904 * @param action the action to read fields from.
905 */
906 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700907 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
908 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700909 }
910
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700911 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
912 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700913 mIcon = icon;
914 mTitle = title;
915 mIntent = intent;
916 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700917 if (remoteInputs != null) {
918 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
919 Collections.addAll(mRemoteInputs, remoteInputs);
920 }
Griff Hazen959591e2014-05-15 22:26:18 -0700921 }
922
923 /**
924 * Merge additional metadata into this builder.
925 *
926 * <p>Values within the Bundle will replace existing extras values in this Builder.
927 *
928 * @see Notification.Action#extras
929 */
930 public Builder addExtras(Bundle extras) {
931 if (extras != null) {
932 mExtras.putAll(extras);
933 }
934 return this;
935 }
936
937 /**
938 * Get the metadata Bundle used by this Builder.
939 *
940 * <p>The returned Bundle is shared with this Builder.
941 */
942 public Bundle getExtras() {
943 return mExtras;
944 }
945
946 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700947 * Add an input to be collected from the user when this action is sent.
948 * Response values can be retrieved from the fired intent by using the
949 * {@link RemoteInput#getResultsFromIntent} function.
950 * @param remoteInput a {@link RemoteInput} to add to the action
951 * @return this object for method chaining
952 */
953 public Builder addRemoteInput(RemoteInput remoteInput) {
954 if (mRemoteInputs == null) {
955 mRemoteInputs = new ArrayList<RemoteInput>();
956 }
957 mRemoteInputs.add(remoteInput);
958 return this;
959 }
960
961 /**
962 * Apply an extender to this action builder. Extenders may be used to add
963 * metadata or change options on this builder.
964 */
Griff Hazen61a9e862014-05-22 16:05:19 -0700965 public Builder extend(Extender extender) {
966 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700967 return this;
968 }
969
970 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700971 * Combine all of the options that have been set and return a new {@link Action}
972 * object.
973 * @return the built action
974 */
975 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700976 RemoteInput[] remoteInputs = mRemoteInputs != null
977 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
978 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -0700979 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400980 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400981
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400982 @Override
983 public Action clone() {
984 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700985 icon,
986 title,
987 actionIntent, // safe to alias
988 new Bundle(mExtras),
989 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400990 }
991 @Override
992 public int describeContents() {
993 return 0;
994 }
995 @Override
996 public void writeToParcel(Parcel out, int flags) {
997 out.writeInt(icon);
998 TextUtils.writeToParcel(title, out, flags);
999 if (actionIntent != null) {
1000 out.writeInt(1);
1001 actionIntent.writeToParcel(out, flags);
1002 } else {
1003 out.writeInt(0);
1004 }
Griff Hazen959591e2014-05-15 22:26:18 -07001005 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001006 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001007 }
Griff Hazen959591e2014-05-15 22:26:18 -07001008 public static final Parcelable.Creator<Action> CREATOR =
1009 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001010 public Action createFromParcel(Parcel in) {
1011 return new Action(in);
1012 }
1013 public Action[] newArray(int size) {
1014 return new Action[size];
1015 }
1016 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001017
1018 /**
1019 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1020 * metadata or change options on an action builder.
1021 */
1022 public interface Extender {
1023 /**
1024 * Apply this extender to a notification action builder.
1025 * @param builder the builder to be modified.
1026 * @return the build object for chaining.
1027 */
1028 public Builder extend(Builder builder);
1029 }
1030
1031 /**
1032 * Wearable extender for notification actions. To add extensions to an action,
1033 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1034 * the {@code WearableExtender()} constructor and apply it to a
1035 * {@link android.app.Notification.Action.Builder} using
1036 * {@link android.app.Notification.Action.Builder#extend}.
1037 *
1038 * <pre class="prettyprint">
1039 * Notification.Action action = new Notification.Action.Builder(
1040 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001041 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001042 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001043 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001044 */
1045 public static final class WearableExtender implements Extender {
1046 /** Notification action extra which contains wearable extensions */
1047 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1048
1049 private static final String KEY_FLAGS = "flags";
1050
1051 // Flags bitwise-ored to mFlags
1052 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1053
1054 // Default value for flags integer
1055 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1056
1057 private int mFlags = DEFAULT_FLAGS;
1058
1059 /**
1060 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1061 * options.
1062 */
1063 public WearableExtender() {
1064 }
1065
1066 /**
1067 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1068 * wearable options present in an existing notification action.
1069 * @param action the notification action to inspect.
1070 */
1071 public WearableExtender(Action action) {
1072 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1073 if (wearableBundle != null) {
1074 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1075 }
1076 }
1077
1078 /**
1079 * Apply wearable extensions to a notification action that is being built. This is
1080 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1081 * method of {@link android.app.Notification.Action.Builder}.
1082 */
1083 @Override
1084 public Action.Builder extend(Action.Builder builder) {
1085 Bundle wearableBundle = new Bundle();
1086
1087 if (mFlags != DEFAULT_FLAGS) {
1088 wearableBundle.putInt(KEY_FLAGS, mFlags);
1089 }
1090
1091 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1092 return builder;
1093 }
1094
1095 @Override
1096 public WearableExtender clone() {
1097 WearableExtender that = new WearableExtender();
1098 that.mFlags = this.mFlags;
1099 return that;
1100 }
1101
1102 /**
1103 * Set whether this action is available when the wearable device is not connected to
1104 * a companion device. The user can still trigger this action when the wearable device is
1105 * offline, but a visual hint will indicate that the action may not be available.
1106 * Defaults to true.
1107 */
1108 public WearableExtender setAvailableOffline(boolean availableOffline) {
1109 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1110 return this;
1111 }
1112
1113 /**
1114 * Get whether this action is available when the wearable device is not connected to
1115 * a companion device. The user can still trigger this action when the wearable device is
1116 * offline, but a visual hint will indicate that the action may not be available.
1117 * Defaults to true.
1118 */
1119 public boolean isAvailableOffline() {
1120 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1121 }
1122
1123 private void setFlag(int mask, boolean value) {
1124 if (value) {
1125 mFlags |= mask;
1126 } else {
1127 mFlags &= ~mask;
1128 }
1129 }
1130 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001131 }
1132
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001133 /**
1134 * Array of all {@link Action} structures attached to this notification by
1135 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1136 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1137 * interface for invoking actions.
1138 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001139 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001140
1141 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001142 * Replacement version of this notification whose content will be shown
1143 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1144 * and {@link #VISIBILITY_PUBLIC}.
1145 */
1146 public Notification publicVersion;
1147
1148 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001149 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001150 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 */
1152 public Notification()
1153 {
1154 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001155 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157
1158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 * @hide
1160 */
1161 public Notification(Context context, int icon, CharSequence tickerText, long when,
1162 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1163 {
1164 this.when = when;
1165 this.icon = icon;
1166 this.tickerText = tickerText;
1167 setLatestEventInfo(context, contentTitle, contentText,
1168 PendingIntent.getActivity(context, 0, contentIntent, 0));
1169 }
1170
1171 /**
1172 * Constructs a Notification object with the information needed to
1173 * have a status bar icon without the standard expanded view.
1174 *
1175 * @param icon The resource id of the icon to put in the status bar.
1176 * @param tickerText The text that flows by in the status bar when the notification first
1177 * activates.
1178 * @param when The time to show in the time field. In the System.currentTimeMillis
1179 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001180 *
1181 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001183 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 public Notification(int icon, CharSequence tickerText, long when)
1185 {
1186 this.icon = icon;
1187 this.tickerText = tickerText;
1188 this.when = when;
1189 }
1190
1191 /**
1192 * Unflatten the notification from a parcel.
1193 */
1194 public Notification(Parcel parcel)
1195 {
1196 int version = parcel.readInt();
1197
1198 when = parcel.readLong();
1199 icon = parcel.readInt();
1200 number = parcel.readInt();
1201 if (parcel.readInt() != 0) {
1202 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1203 }
1204 if (parcel.readInt() != 0) {
1205 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1206 }
1207 if (parcel.readInt() != 0) {
1208 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1209 }
1210 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001211 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001212 }
1213 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1215 }
Joe Onorato561d3852010-11-20 18:09:34 -08001216 if (parcel.readInt() != 0) {
1217 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 defaults = parcel.readInt();
1220 flags = parcel.readInt();
1221 if (parcel.readInt() != 0) {
1222 sound = Uri.CREATOR.createFromParcel(parcel);
1223 }
1224
1225 audioStreamType = parcel.readInt();
1226 vibrate = parcel.createLongArray();
1227 ledARGB = parcel.readInt();
1228 ledOnMS = parcel.readInt();
1229 ledOffMS = parcel.readInt();
1230 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001231
1232 if (parcel.readInt() != 0) {
1233 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1234 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001235
1236 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001237
John Spurlockfd7f1e02014-03-18 16:41:57 -04001238 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001239
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001240 mGroupKey = parcel.readString();
1241
1242 mSortKey = parcel.readString();
1243
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001244 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001245
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001246 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1247
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001248 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001249 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1250 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001251
Chris Wren8fd39ec2014-02-27 17:43:26 -05001252 if (parcel.readInt() != 0) {
1253 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1254 }
1255
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001256 visibility = parcel.readInt();
1257
1258 if (parcel.readInt() != 0) {
1259 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1260 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001261
1262 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264
Andy Stadler110988c2010-12-03 14:29:16 -08001265 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001266 public Notification clone() {
1267 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001268 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001269 return that;
1270 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001271
Daniel Sandler1a497d32013-04-18 14:52:45 -04001272 /**
1273 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1274 * of this into that.
1275 * @hide
1276 */
1277 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001278 that.when = this.when;
1279 that.icon = this.icon;
1280 that.number = this.number;
1281
1282 // PendingIntents are global, so there's no reason (or way) to clone them.
1283 that.contentIntent = this.contentIntent;
1284 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001285 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001286
1287 if (this.tickerText != null) {
1288 that.tickerText = this.tickerText.toString();
1289 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001290 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001291 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001292 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001293 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001294 that.contentView = this.contentView.clone();
1295 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001296 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001297 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1298 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001299 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001300 that.sound = this.sound; // android.net.Uri is immutable
1301 that.audioStreamType = this.audioStreamType;
1302
1303 final long[] vibrate = this.vibrate;
1304 if (vibrate != null) {
1305 final int N = vibrate.length;
1306 final long[] vib = that.vibrate = new long[N];
1307 System.arraycopy(vibrate, 0, vib, 0, N);
1308 }
1309
1310 that.ledARGB = this.ledARGB;
1311 that.ledOnMS = this.ledOnMS;
1312 that.ledOffMS = this.ledOffMS;
1313 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001314
Joe Onorato18e69df2010-05-17 22:26:12 -07001315 that.flags = this.flags;
1316
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001317 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001318
John Spurlockfd7f1e02014-03-18 16:41:57 -04001319 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001320
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001321 that.mGroupKey = this.mGroupKey;
1322
1323 that.mSortKey = this.mSortKey;
1324
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001325 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001326 try {
1327 that.extras = new Bundle(this.extras);
1328 // will unparcel
1329 that.extras.size();
1330 } catch (BadParcelableException e) {
1331 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1332 that.extras = null;
1333 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001334 }
1335
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001336 if (this.actions != null) {
1337 that.actions = new Action[this.actions.length];
1338 for(int i=0; i<this.actions.length; i++) {
1339 that.actions[i] = this.actions[i].clone();
1340 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001341 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001342
Daniel Sandler1a497d32013-04-18 14:52:45 -04001343 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001344 that.bigContentView = this.bigContentView.clone();
1345 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001346
Chris Wren8fd39ec2014-02-27 17:43:26 -05001347 if (heavy && this.headsUpContentView != null) {
1348 that.headsUpContentView = this.headsUpContentView.clone();
1349 }
1350
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001351 that.visibility = this.visibility;
1352
1353 if (this.publicVersion != null) {
1354 that.publicVersion = new Notification();
1355 this.publicVersion.cloneInto(that.publicVersion, heavy);
1356 }
1357
Dan Sandler26e81cf2014-05-06 10:01:27 -04001358 that.color = this.color;
1359
Daniel Sandler1a497d32013-04-18 14:52:45 -04001360 if (!heavy) {
1361 that.lightenPayload(); // will clean out extras
1362 }
1363 }
1364
1365 /**
1366 * Removes heavyweight parts of the Notification object for archival or for sending to
1367 * listeners when the full contents are not necessary.
1368 * @hide
1369 */
1370 public final void lightenPayload() {
1371 tickerView = null;
1372 contentView = null;
1373 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001374 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001375 largeIcon = null;
1376 if (extras != null) {
1377 extras.remove(Notification.EXTRA_LARGE_ICON);
1378 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1379 extras.remove(Notification.EXTRA_PICTURE);
1380 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001381 }
1382
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001383 /**
1384 * Make sure this CharSequence is safe to put into a bundle, which basically
1385 * means it had better not be some custom Parcelable implementation.
1386 * @hide
1387 */
1388 public static CharSequence safeCharSequence(CharSequence cs) {
1389 if (cs instanceof Parcelable) {
1390 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1391 + " instance is a custom Parcelable and not allowed in Notification");
1392 return cs.toString();
1393 }
1394
1395 return cs;
1396 }
1397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 public int describeContents() {
1399 return 0;
1400 }
1401
1402 /**
1403 * Flatten this notification from a parcel.
1404 */
1405 public void writeToParcel(Parcel parcel, int flags)
1406 {
1407 parcel.writeInt(1);
1408
1409 parcel.writeLong(when);
1410 parcel.writeInt(icon);
1411 parcel.writeInt(number);
1412 if (contentIntent != null) {
1413 parcel.writeInt(1);
1414 contentIntent.writeToParcel(parcel, 0);
1415 } else {
1416 parcel.writeInt(0);
1417 }
1418 if (deleteIntent != null) {
1419 parcel.writeInt(1);
1420 deleteIntent.writeToParcel(parcel, 0);
1421 } else {
1422 parcel.writeInt(0);
1423 }
1424 if (tickerText != null) {
1425 parcel.writeInt(1);
1426 TextUtils.writeToParcel(tickerText, parcel, flags);
1427 } else {
1428 parcel.writeInt(0);
1429 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001430 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001431 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001432 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001433 } else {
1434 parcel.writeInt(0);
1435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 if (contentView != null) {
1437 parcel.writeInt(1);
1438 contentView.writeToParcel(parcel, 0);
1439 } else {
1440 parcel.writeInt(0);
1441 }
Joe Onorato561d3852010-11-20 18:09:34 -08001442 if (largeIcon != null) {
1443 parcel.writeInt(1);
1444 largeIcon.writeToParcel(parcel, 0);
1445 } else {
1446 parcel.writeInt(0);
1447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448
1449 parcel.writeInt(defaults);
1450 parcel.writeInt(this.flags);
1451
1452 if (sound != null) {
1453 parcel.writeInt(1);
1454 sound.writeToParcel(parcel, 0);
1455 } else {
1456 parcel.writeInt(0);
1457 }
1458 parcel.writeInt(audioStreamType);
1459 parcel.writeLongArray(vibrate);
1460 parcel.writeInt(ledARGB);
1461 parcel.writeInt(ledOnMS);
1462 parcel.writeInt(ledOffMS);
1463 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001464
1465 if (fullScreenIntent != null) {
1466 parcel.writeInt(1);
1467 fullScreenIntent.writeToParcel(parcel, 0);
1468 } else {
1469 parcel.writeInt(0);
1470 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001471
1472 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001473
John Spurlockfd7f1e02014-03-18 16:41:57 -04001474 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001475
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001476 parcel.writeString(mGroupKey);
1477
1478 parcel.writeString(mSortKey);
1479
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001480 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001481
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001482 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001483
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001484 if (bigContentView != null) {
1485 parcel.writeInt(1);
1486 bigContentView.writeToParcel(parcel, 0);
1487 } else {
1488 parcel.writeInt(0);
1489 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001490
Chris Wren8fd39ec2014-02-27 17:43:26 -05001491 if (headsUpContentView != null) {
1492 parcel.writeInt(1);
1493 headsUpContentView.writeToParcel(parcel, 0);
1494 } else {
1495 parcel.writeInt(0);
1496 }
1497
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001498 parcel.writeInt(visibility);
1499
1500 if (publicVersion != null) {
1501 parcel.writeInt(1);
1502 publicVersion.writeToParcel(parcel, 0);
1503 } else {
1504 parcel.writeInt(0);
1505 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001506
1507 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509
1510 /**
1511 * Parcelable.Creator that instantiates Notification objects
1512 */
1513 public static final Parcelable.Creator<Notification> CREATOR
1514 = new Parcelable.Creator<Notification>()
1515 {
1516 public Notification createFromParcel(Parcel parcel)
1517 {
1518 return new Notification(parcel);
1519 }
1520
1521 public Notification[] newArray(int size)
1522 {
1523 return new Notification[size];
1524 }
1525 };
1526
1527 /**
1528 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1529 * layout.
1530 *
1531 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1532 * in the view.</p>
1533 * @param context The context for your application / activity.
1534 * @param contentTitle The title that goes in the expanded entry.
1535 * @param contentText The text that goes in the expanded entry.
1536 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1537 * If this is an activity, it must include the
1538 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001539 * that you take care of task management as described in the
1540 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1541 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001542 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001543 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001545 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 public void setLatestEventInfo(Context context,
1547 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001548 Notification.Builder builder = new Notification.Builder(context);
1549
1550 // First, ensure that key pieces of information that may have been set directly
1551 // are preserved
1552 builder.setWhen(this.when);
1553 builder.setSmallIcon(this.icon);
1554 builder.setPriority(this.priority);
1555 builder.setTicker(this.tickerText);
1556 builder.setNumber(this.number);
1557 builder.mFlags = this.flags;
1558 builder.setSound(this.sound, this.audioStreamType);
1559 builder.setDefaults(this.defaults);
1560 builder.setVibrate(this.vibrate);
1561
1562 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001564 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001567 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001569 builder.setContentIntent(contentIntent);
1570 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
1572
1573 @Override
1574 public String toString() {
1575 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001576 sb.append("Notification(pri=");
1577 sb.append(priority);
1578 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001579 if (contentView != null) {
1580 sb.append(contentView.getPackage());
1581 sb.append("/0x");
1582 sb.append(Integer.toHexString(contentView.getLayoutId()));
1583 } else {
1584 sb.append("null");
1585 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001586 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001587 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001588 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1589 sb.append("default");
1590 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 int N = this.vibrate.length-1;
1592 sb.append("[");
1593 for (int i=0; i<N; i++) {
1594 sb.append(this.vibrate[i]);
1595 sb.append(',');
1596 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001597 if (N != -1) {
1598 sb.append(this.vibrate[N]);
1599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 } else {
1602 sb.append("null");
1603 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001604 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001605 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001607 } else if (this.sound != null) {
1608 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 } else {
1610 sb.append("null");
1611 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001612 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001614 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001615 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001616 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001617 if (this.category != null) {
1618 sb.append(" category=");
1619 sb.append(this.category);
1620 }
1621 if (this.mGroupKey != null) {
1622 sb.append(" groupKey=");
1623 sb.append(this.mGroupKey);
1624 }
1625 if (this.mSortKey != null) {
1626 sb.append(" sortKey=");
1627 sb.append(this.mSortKey);
1628 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001629 if (actions != null) {
1630 sb.append(" ");
1631 sb.append(actions.length);
1632 sb.append(" action");
1633 if (actions.length > 1) sb.append("s");
1634 }
1635 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 return sb.toString();
1637 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001638
Jeff Sharkey6d515712012-09-20 16:06:08 -07001639 /** {@hide} */
1640 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001641 if (user.getIdentifier() == UserHandle.USER_ALL) {
1642 user = UserHandle.OWNER;
1643 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001644 if (tickerView != null) {
1645 tickerView.setUser(user);
1646 }
1647 if (contentView != null) {
1648 contentView.setUser(user);
1649 }
1650 if (bigContentView != null) {
1651 bigContentView.setUser(user);
1652 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001653 if (headsUpContentView != null) {
1654 headsUpContentView.setUser(user);
1655 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001656 }
1657
Joe Onoratocb109a02011-01-18 17:57:41 -08001658 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001659 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001660 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001661 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001662 * content views using the platform's notification layout template. If your app supports
1663 * versions of Android as old as API level 4, you can instead use
1664 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1665 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1666 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001667 *
Scott Main183bf112012-08-13 19:12:13 -07001668 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001669 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001670 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001671 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001672 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1673 * .setContentText(subject)
1674 * .setSmallIcon(R.drawable.new_mail)
1675 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001676 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001677 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001678 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001679 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001680 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001681
Joe Onorato46439ce2010-11-19 13:56:21 -08001682 private Context mContext;
1683
1684 private long mWhen;
1685 private int mSmallIcon;
1686 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001687 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001688 private CharSequence mContentTitle;
1689 private CharSequence mContentText;
1690 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001691 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001692 private PendingIntent mContentIntent;
1693 private RemoteViews mContentView;
1694 private PendingIntent mDeleteIntent;
1695 private PendingIntent mFullScreenIntent;
1696 private CharSequence mTickerText;
1697 private RemoteViews mTickerView;
1698 private Bitmap mLargeIcon;
1699 private Uri mSound;
1700 private int mAudioStreamType;
1701 private long[] mVibrate;
1702 private int mLedArgb;
1703 private int mLedOnMs;
1704 private int mLedOffMs;
1705 private int mDefaults;
1706 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001707 private int mProgressMax;
1708 private int mProgress;
1709 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001710 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001711 private String mGroupKey;
1712 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001713 private Bundle mExtras;
1714 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001715 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001716 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001717 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001718 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001719 private int mVisibility = VISIBILITY_PRIVATE;
1720 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001721 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001722 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001723 private int mColor = COLOR_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001724
Joe Onoratocb109a02011-01-18 17:57:41 -08001725 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001726 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001727 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001728
1729 * <table>
1730 * <tr><th align=right>priority</th>
1731 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1732 * <tr><th align=right>when</th>
1733 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1734 * <tr><th align=right>audio stream</th>
1735 * <td>{@link #STREAM_DEFAULT}</td></tr>
1736 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001737 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001738
1739 * @param context
1740 * A {@link Context} that will be used by the Builder to construct the
1741 * RemoteViews. The Context will not be held past the lifetime of this Builder
1742 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001743 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001744 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001745 /*
1746 * Important compatibility note!
1747 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1748 * constructor for later use. At this point Activities - themselves subclasses of
1749 * ContextWrapper - do not have their inner Context populated yet. This means that
1750 * any calls to Context methods from within this constructor can cause NPEs in existing
1751 * apps. Any data populated from mContext should therefore be populated lazily to
1752 * preserve compatibility.
1753 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001754 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001755
1756 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001757 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001758 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001759 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001760 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001761
Dan Sandler26e81cf2014-05-06 10:01:27 -04001762 mColorUtil = NotificationColorUtil.getInstance();
Joe Onorato46439ce2010-11-19 13:56:21 -08001763 }
1764
Joe Onoratocb109a02011-01-18 17:57:41 -08001765 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001766 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001767 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001768 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001769 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001770 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001771 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001772 public Builder setWhen(long when) {
1773 mWhen = when;
1774 return this;
1775 }
1776
Joe Onoratocb109a02011-01-18 17:57:41 -08001777 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001778 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001779 * in the content view.
1780 */
1781 public Builder setShowWhen(boolean show) {
1782 mShowWhen = show;
1783 return this;
1784 }
1785
1786 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001787 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001788 *
1789 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001790 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001791 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001792 * Useful when showing an elapsed time (like an ongoing phone call).
1793 *
1794 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001795 * @see Notification#when
1796 */
1797 public Builder setUsesChronometer(boolean b) {
1798 mUseChronometer = b;
1799 return this;
1800 }
1801
1802 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001803 * Set the small icon resource, which will be used to represent the notification in the
1804 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001805 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001806
1807 * The platform template for the expanded view will draw this icon in the left, unless a
1808 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1809 * icon will be moved to the right-hand side.
1810 *
1811
1812 * @param icon
1813 * A resource ID in the application's package of the drawable to use.
1814 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001815 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001816 public Builder setSmallIcon(int icon) {
1817 mSmallIcon = icon;
1818 return this;
1819 }
1820
Joe Onoratocb109a02011-01-18 17:57:41 -08001821 /**
1822 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1823 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1824 * LevelListDrawable}.
1825 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001826 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001827 * @param level The level to use for the icon.
1828 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001829 * @see Notification#icon
1830 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001831 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001832 public Builder setSmallIcon(int icon, int level) {
1833 mSmallIcon = icon;
1834 mSmallIconLevel = level;
1835 return this;
1836 }
1837
Joe Onoratocb109a02011-01-18 17:57:41 -08001838 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001839 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001840 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001841 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001842 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001843 return this;
1844 }
1845
Joe Onoratocb109a02011-01-18 17:57:41 -08001846 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001847 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001848 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001849 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001850 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001851 return this;
1852 }
1853
Joe Onoratocb109a02011-01-18 17:57:41 -08001854 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001855 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001856 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1857 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001858 */
1859 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001860 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001861 return this;
1862 }
1863
1864 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001865 * Set the large number at the right-hand side of the notification. This is
1866 * equivalent to setContentInfo, although it might show the number in a different
1867 * font size for readability.
1868 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001869 public Builder setNumber(int number) {
1870 mNumber = number;
1871 return this;
1872 }
1873
Joe Onoratocb109a02011-01-18 17:57:41 -08001874 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001875 * A small piece of additional information pertaining to this notification.
1876 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001877 * The platform template will draw this on the last line of the notification, at the far
1878 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001879 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001880 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001881 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001882 return this;
1883 }
1884
Joe Onoratocb109a02011-01-18 17:57:41 -08001885 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001886 * Set the progress this notification represents.
1887 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001888 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001889 */
1890 public Builder setProgress(int max, int progress, boolean indeterminate) {
1891 mProgressMax = max;
1892 mProgress = progress;
1893 mProgressIndeterminate = indeterminate;
1894 return this;
1895 }
1896
1897 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001898 * Supply a custom RemoteViews to use instead of the platform template.
1899 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001900 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001901 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001902 public Builder setContent(RemoteViews views) {
1903 mContentView = views;
1904 return this;
1905 }
1906
Joe Onoratocb109a02011-01-18 17:57:41 -08001907 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001908 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1909 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001910 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1911 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1912 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001913 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001914 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001915 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001916 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001917 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001918 public Builder setContentIntent(PendingIntent intent) {
1919 mContentIntent = intent;
1920 return this;
1921 }
1922
Joe Onoratocb109a02011-01-18 17:57:41 -08001923 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001924 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1925 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001926 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001927 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001928 public Builder setDeleteIntent(PendingIntent intent) {
1929 mDeleteIntent = intent;
1930 return this;
1931 }
1932
Joe Onoratocb109a02011-01-18 17:57:41 -08001933 /**
1934 * An intent to launch instead of posting the notification to the status bar.
1935 * Only for use with extremely high-priority notifications demanding the user's
1936 * <strong>immediate</strong> attention, such as an incoming phone call or
1937 * alarm clock that the user has explicitly set to a particular time.
1938 * If this facility is used for something else, please give the user an option
1939 * to turn it off and use a normal notification, as this can be extremely
1940 * disruptive.
1941 *
Chris Wren47c20a12014-06-18 17:27:29 -04001942 * <p>
1943 * The system UI may choose to display a heads-up notification, instead of
1944 * launching this intent, while the user is using the device.
1945 * </p>
1946 *
Joe Onoratocb109a02011-01-18 17:57:41 -08001947 * @param intent The pending intent to launch.
1948 * @param highPriority Passing true will cause this notification to be sent
1949 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001950 *
1951 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001952 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001953 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1954 mFullScreenIntent = intent;
1955 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1956 return this;
1957 }
1958
Joe Onoratocb109a02011-01-18 17:57:41 -08001959 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001960 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001961 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001962 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001963 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001964 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001965 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001966 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001967 return this;
1968 }
1969
Joe Onoratocb109a02011-01-18 17:57:41 -08001970 /**
1971 * Set the text that is displayed in the status bar when the notification first
1972 * arrives, and also a RemoteViews object that may be displayed instead on some
1973 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001974 *
1975 * @see Notification#tickerText
1976 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001977 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001978 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001979 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001980 mTickerView = views;
1981 return this;
1982 }
1983
Joe Onoratocb109a02011-01-18 17:57:41 -08001984 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001985 * Add a large icon to the notification (and the ticker on some devices).
1986 *
1987 * In the platform template, this image will be shown on the left of the notification view
1988 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1989 *
1990 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001991 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001992 public Builder setLargeIcon(Bitmap icon) {
1993 mLargeIcon = icon;
1994 return this;
1995 }
1996
Joe Onoratocb109a02011-01-18 17:57:41 -08001997 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001998 * Set the sound to play.
1999 *
2000 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
2001 *
Chris Wren47c20a12014-06-18 17:27:29 -04002002 * <p>
2003 * A notification that is noisy is more likely to be presented as a heads-up notification.
2004 * </p>
2005 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002006 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002007 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002008 public Builder setSound(Uri sound) {
2009 mSound = sound;
2010 mAudioStreamType = STREAM_DEFAULT;
2011 return this;
2012 }
2013
Joe Onoratocb109a02011-01-18 17:57:41 -08002014 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002015 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002016 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002017 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2018 *
Chris Wren47c20a12014-06-18 17:27:29 -04002019 * <p>
2020 * A notification that is noisy is more likely to be presented as a heads-up notification.
2021 * </p>
2022 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002023 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002024 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002025 public Builder setSound(Uri sound, int streamType) {
2026 mSound = sound;
2027 mAudioStreamType = streamType;
2028 return this;
2029 }
2030
Joe Onoratocb109a02011-01-18 17:57:41 -08002031 /**
2032 * Set the vibration pattern to use.
2033 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002034 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2035 * <code>pattern</code> parameter.
2036 *
Chris Wren47c20a12014-06-18 17:27:29 -04002037 * <p>
2038 * A notification that vibrates is more likely to be presented as a heads-up notification.
2039 * </p>
2040 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002041 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002042 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002043 public Builder setVibrate(long[] pattern) {
2044 mVibrate = pattern;
2045 return this;
2046 }
2047
Joe Onoratocb109a02011-01-18 17:57:41 -08002048 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002049 * Set the desired color for the indicator LED on the device, as well as the
2050 * blink duty cycle (specified in milliseconds).
2051 *
2052
2053 * Not all devices will honor all (or even any) of these values.
2054 *
2055
2056 * @see Notification#ledARGB
2057 * @see Notification#ledOnMS
2058 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002059 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002060 public Builder setLights(int argb, int onMs, int offMs) {
2061 mLedArgb = argb;
2062 mLedOnMs = onMs;
2063 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002064 return this;
2065 }
2066
Joe Onoratocb109a02011-01-18 17:57:41 -08002067 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002068 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002069 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002070
2071 * Ongoing notifications cannot be dismissed by the user, so your application or service
2072 * must take care of canceling them.
2073 *
2074
2075 * They are typically used to indicate a background task that the user is actively engaged
2076 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2077 * (e.g., a file download, sync operation, active network connection).
2078 *
2079
2080 * @see Notification#FLAG_ONGOING_EVENT
2081 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002082 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002083 public Builder setOngoing(boolean ongoing) {
2084 setFlag(FLAG_ONGOING_EVENT, ongoing);
2085 return this;
2086 }
2087
Joe Onoratocb109a02011-01-18 17:57:41 -08002088 /**
2089 * Set this flag if you would only like the sound, vibrate
2090 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002091 *
2092 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002093 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002094 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2095 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2096 return this;
2097 }
2098
Joe Onoratocb109a02011-01-18 17:57:41 -08002099 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002100 * Make this notification automatically dismissed when the user touches it. The
2101 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2102 *
2103 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002104 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002105 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002106 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002107 return this;
2108 }
2109
Joe Onoratocb109a02011-01-18 17:57:41 -08002110 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002111 * Set whether or not this notification should not bridge to other devices.
2112 *
2113 * <p>Some notifications can be bridged to other devices for remote display.
2114 * This hint can be set to recommend this notification not be bridged.
2115 */
2116 public Builder setLocalOnly(boolean localOnly) {
2117 setFlag(FLAG_LOCAL_ONLY, localOnly);
2118 return this;
2119 }
2120
2121 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002122 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002123 * <p>
2124 * The value should be one or more of the following fields combined with
2125 * bitwise-or:
2126 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2127 * <p>
2128 * For all default values, use {@link #DEFAULT_ALL}.
2129 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002130 public Builder setDefaults(int defaults) {
2131 mDefaults = defaults;
2132 return this;
2133 }
2134
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002135 /**
2136 * Set the priority of this notification.
2137 *
2138 * @see Notification#priority
2139 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002140 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002141 mPriority = pri;
2142 return this;
2143 }
Joe Malin8d40d042012-11-05 11:36:40 -08002144
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002145 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002146 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002147 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002148 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002149 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002150 public Builder setCategory(String category) {
2151 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002152 return this;
2153 }
2154
2155 /**
Chris Wrendde75302014-03-26 17:24:15 -04002156 * Add a person that is relevant to this notification.
2157 *
2158 * @see Notification#EXTRA_PEOPLE
2159 */
2160 public Builder addPerson(String handle) {
2161 mPeople.add(handle);
2162 return this;
2163 }
2164
2165 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002166 * Set this notification to be part of a group of notifications sharing the same key.
2167 * Grouped notifications may display in a cluster or stack on devices which
2168 * support such rendering.
2169 *
2170 * <p>To make this notification the summary for its group, also call
2171 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2172 * {@link #setSortKey}.
2173 * @param groupKey The group key of the group.
2174 * @return this object for method chaining
2175 */
2176 public Builder setGroup(String groupKey) {
2177 mGroupKey = groupKey;
2178 return this;
2179 }
2180
2181 /**
2182 * Set this notification to be the group summary for a group of notifications.
2183 * Grouped notifications may display in a cluster or stack on devices which
2184 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2185 * @param isGroupSummary Whether this notification should be a group summary.
2186 * @return this object for method chaining
2187 */
2188 public Builder setGroupSummary(boolean isGroupSummary) {
2189 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2190 return this;
2191 }
2192
2193 /**
2194 * Set a sort key that orders this notification among other notifications from the
2195 * same package. This can be useful if an external sort was already applied and an app
2196 * would like to preserve this. Notifications will be sorted lexicographically using this
2197 * value, although providing different priorities in addition to providing sort key may
2198 * cause this value to be ignored.
2199 *
2200 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002201 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002202 *
2203 * @see String#compareTo(String)
2204 */
2205 public Builder setSortKey(String sortKey) {
2206 mSortKey = sortKey;
2207 return this;
2208 }
2209
2210 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002211 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002212 *
Griff Hazen720042b2014-02-24 15:46:56 -08002213 * <p>Values within the Bundle will replace existing extras values in this Builder.
2214 *
2215 * @see Notification#extras
2216 */
Griff Hazen959591e2014-05-15 22:26:18 -07002217 public Builder addExtras(Bundle extras) {
2218 if (extras != null) {
2219 if (mExtras == null) {
2220 mExtras = new Bundle(extras);
2221 } else {
2222 mExtras.putAll(extras);
2223 }
Griff Hazen720042b2014-02-24 15:46:56 -08002224 }
2225 return this;
2226 }
2227
2228 /**
2229 * Set metadata for this notification.
2230 *
2231 * <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 -04002232 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002233 * called.
2234 *
Griff Hazen720042b2014-02-24 15:46:56 -08002235 * <p>Replaces any existing extras values with those from the provided Bundle.
2236 * Use {@link #addExtras} to merge in metadata instead.
2237 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002238 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002239 */
Griff Hazen959591e2014-05-15 22:26:18 -07002240 public Builder setExtras(Bundle extras) {
2241 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002242 return this;
2243 }
2244
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002245 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002246 * Get the current metadata Bundle used by this notification Builder.
2247 *
2248 * <p>The returned Bundle is shared with this Builder.
2249 *
2250 * <p>The current contents of this Bundle are copied into the Notification each time
2251 * {@link #build()} is called.
2252 *
2253 * @see Notification#extras
2254 */
2255 public Bundle getExtras() {
2256 if (mExtras == null) {
2257 mExtras = new Bundle();
2258 }
2259 return mExtras;
2260 }
2261
2262 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002263 * Add an action to this notification. Actions are typically displayed by
2264 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002265 * <p>
2266 * Every action must have an icon (32dp square and matching the
2267 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2268 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2269 * <p>
2270 * A notification in its expanded form can display up to 3 actions, from left to right in
2271 * the order they were added. Actions will not be displayed when the notification is
2272 * collapsed, however, so be sure that any essential functions may be accessed by the user
2273 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002274 *
2275 * @param icon Resource ID of a drawable that represents the action.
2276 * @param title Text describing the action.
2277 * @param intent PendingIntent to be fired when the action is invoked.
2278 */
2279 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002280 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002281 return this;
2282 }
2283
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002284 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002285 * Add an action to this notification. Actions are typically displayed by
2286 * the system as a button adjacent to the notification content.
2287 * <p>
2288 * Every action must have an icon (32dp square and matching the
2289 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2290 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2291 * <p>
2292 * A notification in its expanded form can display up to 3 actions, from left to right in
2293 * the order they were added. Actions will not be displayed when the notification is
2294 * collapsed, however, so be sure that any essential functions may be accessed by the user
2295 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2296 *
2297 * @param action The action to add.
2298 */
2299 public Builder addAction(Action action) {
2300 mActions.add(action);
2301 return this;
2302 }
2303
2304 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002305 * Add a rich notification style to be applied at build time.
2306 *
2307 * @param style Object responsible for modifying the notification style.
2308 */
2309 public Builder setStyle(Style style) {
2310 if (mStyle != style) {
2311 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002312 if (mStyle != null) {
2313 mStyle.setBuilder(this);
2314 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002315 }
2316 return this;
2317 }
2318
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002319 /**
2320 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002321 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002322 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2323 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2324 *
2325 * @return The same Builder.
2326 */
2327 public Builder setVisibility(int visibility) {
2328 mVisibility = visibility;
2329 return this;
2330 }
2331
2332 /**
2333 * Supply a replacement Notification whose contents should be shown in insecure contexts
2334 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2335 * @param n A replacement notification, presumably with some or all info redacted.
2336 * @return The same Builder.
2337 */
2338 public Builder setPublicVersion(Notification n) {
2339 mPublicVersion = n;
2340 return this;
2341 }
2342
Griff Hazenb720abe2014-05-20 13:15:30 -07002343 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002344 * Apply an extender to this notification builder. Extenders may be used to add
2345 * metadata or change options on this builder.
2346 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002347 public Builder extend(Extender extender) {
2348 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002349 return this;
2350 }
2351
Joe Onorato46439ce2010-11-19 13:56:21 -08002352 private void setFlag(int mask, boolean value) {
2353 if (value) {
2354 mFlags |= mask;
2355 } else {
2356 mFlags &= ~mask;
2357 }
2358 }
2359
Dan Sandler26e81cf2014-05-06 10:01:27 -04002360 /**
2361 * Sets {@link Notification#color}.
2362 *
2363 * @param argb The accent color to use
2364 *
2365 * @return The same Builder.
2366 */
2367 public Builder setColor(int argb) {
2368 mColor = argb;
2369 return this;
2370 }
2371
Kenny Guy8a0101b2014-05-08 23:34:12 +01002372 private Bitmap getProfileBadge() {
2373 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
2374 Drawable badge = userManager.getBadgeForUser(android.os.Process.myUserHandle());
2375 if (badge == null) {
2376 return null;
2377 }
2378 final int width = badge.getIntrinsicWidth();
2379 final int height = badge.getIntrinsicHeight();
2380 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2381 Canvas canvas = new Canvas(bitmap);
2382 badge.setBounds(0, 0, width, height);
2383 badge.draw(canvas);
2384 return bitmap;
2385 }
2386
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002387 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Kenny Guy8a0101b2014-05-08 23:34:12 +01002388 Bitmap profileIcon = getProfileBadge();
Joe Onorato561d3852010-11-20 18:09:34 -08002389 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002390 boolean showLine3 = false;
2391 boolean showLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002392
Dan Sandler26e81cf2014-05-06 10:01:27 -04002393 if (mPriority < PRIORITY_LOW) {
2394 // TODO: Low priority presentation
Daniel Sandlere95658c2012-05-10 00:33:54 -04002395 }
Kenny Guy8a0101b2014-05-08 23:34:12 +01002396 if (profileIcon != null) {
2397 contentView.setImageViewBitmap(R.id.profile_icon, profileIcon);
2398 contentView.setViewVisibility(R.id.profile_icon, View.VISIBLE);
2399 } else {
2400 contentView.setViewVisibility(R.id.profile_icon, View.GONE);
2401 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002402 if (mLargeIcon != null) {
2403 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002404 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002405 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2406 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2407 processSmallRightIcon(mSmallIcon, contentView);
2408 } else { // small icon at left
2409 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2410 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2411 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002412 }
2413 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002414 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002415 }
2416 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002417 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002418 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002419 }
2420 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002421 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002422 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002423 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002424 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002425 final int tooBig = mContext.getResources().getInteger(
2426 R.integer.status_bar_notification_info_maxnum);
2427 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002428 contentView.setTextViewText(R.id.info, processLegacyText(
2429 mContext.getResources().getString(
2430 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002431 } else {
2432 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002433 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002434 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002435 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002436 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002437 } else {
2438 contentView.setViewVisibility(R.id.info, View.GONE);
2439 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002440
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002441 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002442 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002443 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002444 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002445 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002446 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2447 showLine2 = true;
2448 } else {
2449 contentView.setViewVisibility(R.id.text2, View.GONE);
2450 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002451 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002452 contentView.setViewVisibility(R.id.text2, View.GONE);
2453 if (mProgressMax != 0 || mProgressIndeterminate) {
2454 contentView.setProgressBar(
2455 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2456 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002457 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002458 } else {
2459 contentView.setViewVisibility(R.id.progress, View.GONE);
2460 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002461 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002462 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002463 if (fitIn1U) {
2464 // need to shrink all the type to make sure everything fits
2465 final Resources res = mContext.getResources();
2466 final float subTextSize = res.getDimensionPixelSize(
2467 R.dimen.notification_subtext_size);
2468 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2469 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002470 // vertical centering
2471 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2472 }
2473
Daniel Sandler0c890492012-09-12 17:23:10 -07002474 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002475 if (mUseChronometer) {
2476 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2477 contentView.setLong(R.id.chronometer, "setBase",
2478 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2479 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2480 } else {
2481 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2482 contentView.setLong(R.id.time, "setTime", mWhen);
2483 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002484 } else {
2485 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002486 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002487
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002488 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002489 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002490 return contentView;
2491 }
2492
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002493 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002494 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002495
2496 int N = mActions.size();
2497 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002498 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002499 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002500 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002501 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002502 for (int i=0; i<N; i++) {
2503 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002504 big.addView(R.id.actions, button);
2505 }
2506 }
2507 return big;
2508 }
2509
Joe Onorato46439ce2010-11-19 13:56:21 -08002510 private RemoteViews makeContentView() {
2511 if (mContentView != null) {
2512 return mContentView;
2513 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002514 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002515 }
2516 }
2517
2518 private RemoteViews makeTickerView() {
2519 if (mTickerView != null) {
2520 return mTickerView;
2521 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08002522 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002523 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08002524 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002525 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08002526 } else {
2527 return null;
2528 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002529 }
2530 }
2531
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002532 private RemoteViews makeBigContentView() {
2533 if (mActions.size() == 0) return null;
2534
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002535 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002536 }
2537
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002538 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002539 if (mActions.size() == 0) return null;
2540
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002541 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002542 }
2543
2544
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002545 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002546 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002547 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002548 tombstone ? getActionTombstoneLayoutResource()
2549 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002550 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002551 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002552 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002553 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002554 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002555 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002556 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002557 return button;
2558 }
2559
Joe Onoratocb109a02011-01-18 17:57:41 -08002560 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002561 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002562 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002563 */
2564 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002565 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002566 }
2567
2568 private void processLegacyAction(Action action, RemoteViews button) {
2569 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002570 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002571 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2572 mContext.getResources().getColor(
2573 R.color.notification_action_legacy_color_filter),
2574 PorterDuff.Mode.MULTIPLY);
2575 }
2576 }
2577 }
2578
2579 private CharSequence processLegacyText(CharSequence charSequence) {
2580 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002581 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002582 } else {
2583 return charSequence;
2584 }
2585 }
2586
Dan Sandler26e81cf2014-05-06 10:01:27 -04002587 /**
2588 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2589 */
2590 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2591 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2592 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002593 }
2594 }
2595
Dan Sandler26e81cf2014-05-06 10:01:27 -04002596 /**
2597 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2598 * if it's grayscale).
2599 */
2600 // TODO: also check bounds, transparency, that sort of thing.
2601 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2602 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2603 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002604 } else {
2605 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002606 }
2607 }
2608
Dan Sandler26e81cf2014-05-06 10:01:27 -04002609 /**
2610 * Add a colored circle behind the largeIcon slot.
2611 */
2612 private void applyLargeIconBackground(RemoteViews contentView) {
2613 contentView.setInt(R.id.icon, "setBackgroundResource",
2614 R.drawable.notification_icon_legacy_bg_inset);
2615
2616 contentView.setDrawableParameters(
2617 R.id.icon,
2618 true,
2619 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002620 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002621 PorterDuff.Mode.SRC_ATOP,
2622 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002623 }
2624
Dan Sandler190d58d2014-05-15 09:33:39 -04002625 private void removeLargeIconBackground(RemoteViews contentView) {
2626 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2627 }
2628
Dan Sandler26e81cf2014-05-06 10:01:27 -04002629 /**
2630 * Recolor small icons when used in the R.id.right_icon slot.
2631 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002632 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002633 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002634 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002635 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2636 0xFFFFFFFF,
2637 PorterDuff.Mode.SRC_ATOP, -1);
2638
2639 contentView.setInt(R.id.right_icon,
2640 "setBackgroundResource",
2641 R.drawable.notification_icon_legacy_bg);
2642
2643 contentView.setDrawableParameters(
2644 R.id.right_icon,
2645 true,
2646 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002647 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04002648 PorterDuff.Mode.SRC_ATOP,
2649 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002650 }
2651 }
2652
Jorim Jaggi74419312014-06-10 20:57:21 +02002653 private int sanitizeColor() {
2654 if (mColor != COLOR_DEFAULT) {
2655 mColor |= 0xFF000000; // no alpha for custom colors
2656 }
2657 return mColor;
2658 }
2659
Dan Sandler26e81cf2014-05-06 10:01:27 -04002660 private int resolveColor() {
2661 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02002662 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002663 }
2664 return mColor;
2665 }
2666
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002667 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002668 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002669 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002670 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002671 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002672 Notification n = new Notification();
2673 n.when = mWhen;
2674 n.icon = mSmallIcon;
2675 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002676 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002677
Jorim Jaggi74419312014-06-10 20:57:21 +02002678 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002679
Joe Onorato46439ce2010-11-19 13:56:21 -08002680 n.contentView = makeContentView();
2681 n.contentIntent = mContentIntent;
2682 n.deleteIntent = mDeleteIntent;
2683 n.fullScreenIntent = mFullScreenIntent;
2684 n.tickerText = mTickerText;
2685 n.tickerView = makeTickerView();
2686 n.largeIcon = mLargeIcon;
2687 n.sound = mSound;
2688 n.audioStreamType = mAudioStreamType;
2689 n.vibrate = mVibrate;
2690 n.ledARGB = mLedArgb;
2691 n.ledOnMS = mLedOnMs;
2692 n.ledOffMS = mLedOffMs;
2693 n.defaults = mDefaults;
2694 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002695 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002696 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002697 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002698 n.flags |= FLAG_SHOW_LIGHTS;
2699 }
2700 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2701 n.flags |= FLAG_SHOW_LIGHTS;
2702 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002703 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002704 n.mGroupKey = mGroupKey;
2705 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002706 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002707 if (mActions.size() > 0) {
2708 n.actions = new Action[mActions.size()];
2709 mActions.toArray(n.actions);
2710 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002711 n.visibility = mVisibility;
2712
2713 if (mPublicVersion != null) {
2714 n.publicVersion = new Notification();
2715 mPublicVersion.cloneInto(n.publicVersion, true);
2716 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002717
Joe Onorato46439ce2010-11-19 13:56:21 -08002718 return n;
2719 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002720
2721 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002722 * Capture, in the provided bundle, semantic information used in the construction of
2723 * this Notification object.
2724 * @hide
2725 */
Griff Hazen720042b2014-02-24 15:46:56 -08002726 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002727 // Store original information used in the construction of this object
2728 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2729 extras.putCharSequence(EXTRA_TEXT, mContentText);
2730 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2731 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2732 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2733 extras.putInt(EXTRA_PROGRESS, mProgress);
2734 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2735 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2736 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2737 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002738 if (mLargeIcon != null) {
2739 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2740 }
Chris Wrendde75302014-03-26 17:24:15 -04002741 if (!mPeople.isEmpty()) {
2742 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2743 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002744 }
2745
2746 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002747 * @deprecated Use {@link #build()} instead.
2748 */
2749 @Deprecated
2750 public Notification getNotification() {
2751 return build();
2752 }
2753
2754 /**
2755 * Combine all of the options that have been set and return a new {@link Notification}
2756 * object.
2757 */
2758 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002759 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002760
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002761 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002762 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002763 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002764
2765 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2766
Griff Hazen720042b2014-02-24 15:46:56 -08002767 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002768 if (mStyle != null) {
2769 mStyle.addExtras(n.extras);
2770 }
2771
2772 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002773 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002774
2775 /**
2776 * Apply this Builder to an existing {@link Notification} object.
2777 *
2778 * @hide
2779 */
2780 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002781 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002782 return n;
2783 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002784
2785
2786 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002787 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002788 }
2789
2790 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002791 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002792 }
2793
2794 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002795 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002796 }
2797
2798 private int getBigTextLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002799 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002800 }
2801
2802 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002803 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002804 }
2805
2806 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002807 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002808 }
2809
2810 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002811 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002812 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002813 }
2814
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002815 /**
2816 * An object that can apply a rich notification style to a {@link Notification.Builder}
2817 * object.
2818 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002819 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002820 private CharSequence mBigContentTitle;
2821 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002822 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002823
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002824 protected Builder mBuilder;
2825
Chris Wrend6297db2012-05-03 16:20:13 -04002826 /**
2827 * Overrides ContentTitle in the big form of the template.
2828 * This defaults to the value passed to setContentTitle().
2829 */
2830 protected void internalSetBigContentTitle(CharSequence title) {
2831 mBigContentTitle = title;
2832 }
2833
2834 /**
2835 * Set the first line of text after the detail section in the big form of the template.
2836 */
2837 protected void internalSetSummaryText(CharSequence cs) {
2838 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002839 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002840 }
2841
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002842 public void setBuilder(Builder builder) {
2843 if (mBuilder != builder) {
2844 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002845 if (mBuilder != null) {
2846 mBuilder.setStyle(this);
2847 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002848 }
2849 }
2850
Chris Wrend6297db2012-05-03 16:20:13 -04002851 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002852 if (mBuilder == null) {
2853 throw new IllegalArgumentException("Style requires a valid Builder object");
2854 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002855 }
Chris Wrend6297db2012-05-03 16:20:13 -04002856
2857 protected RemoteViews getStandardView(int layoutId) {
2858 checkBuilder();
2859
2860 if (mBigContentTitle != null) {
2861 mBuilder.setContentTitle(mBigContentTitle);
2862 }
2863
Chris Wrend6297db2012-05-03 16:20:13 -04002864 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2865
Chris Wrend6297db2012-05-03 16:20:13 -04002866 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2867 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002868 } else {
2869 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002870 }
2871
Daniel Sandler619738c2012-06-07 16:33:08 -04002872 // The last line defaults to the subtext, but can be replaced by mSummaryText
2873 final CharSequence overflowText =
2874 mSummaryTextSet ? mSummaryText
2875 : mBuilder.mSubText;
2876 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002877 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04002878 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002879 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002880 } else {
2881 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2882 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002883 }
2884
2885 return contentView;
2886 }
2887
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002888 /**
2889 * @hide
2890 */
2891 public void addExtras(Bundle extras) {
2892 if (mSummaryTextSet) {
2893 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2894 }
2895 if (mBigContentTitle != null) {
2896 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2897 }
Chris Wren91ad5632013-06-05 15:05:57 -04002898 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002899 }
2900
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002901 /**
2902 * @hide
2903 */
2904 public abstract Notification buildStyled(Notification wip);
2905
2906 /**
2907 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2908 * attached to.
2909 *
2910 * @return the fully constructed Notification.
2911 */
2912 public Notification build() {
2913 checkBuilder();
2914 return mBuilder.build();
2915 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002916 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002917
2918 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002919 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002920 *
Robert Ly91c5ce32014-06-08 15:37:00 -07002921 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002922 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07002923 * Notification notif = new Notification.Builder(mContext)
2924 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
2925 * .setContentText(subject)
2926 * .setSmallIcon(R.drawable.new_post)
2927 * .setLargeIcon(aBitmap)
2928 * .setStyle(new Notification.BigPictureStyle()
2929 * .bigPicture(aBigBitmap))
2930 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002931 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002932 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002933 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002934 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002935 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002936 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002937 private Bitmap mBigLargeIcon;
2938 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002939
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002940 public BigPictureStyle() {
2941 }
2942
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002943 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002944 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002945 }
2946
Chris Wrend6297db2012-05-03 16:20:13 -04002947 /**
2948 * Overrides ContentTitle in the big form of the template.
2949 * This defaults to the value passed to setContentTitle().
2950 */
2951 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002952 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002953 return this;
2954 }
2955
2956 /**
2957 * Set the first line of text after the detail section in the big form of the template.
2958 */
2959 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002960 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002961 return this;
2962 }
2963
Chris Wren0bd664d2012-08-01 13:56:56 -04002964 /**
2965 * Provide the bitmap to be used as the payload for the BigPicture notification.
2966 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002967 public BigPictureStyle bigPicture(Bitmap b) {
2968 mPicture = b;
2969 return this;
2970 }
2971
Chris Wren3745a3d2012-05-22 15:11:52 -04002972 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002973 * Override the large icon when the big notification is shown.
2974 */
2975 public BigPictureStyle bigLargeIcon(Bitmap b) {
2976 mBigLargeIconSet = true;
2977 mBigLargeIcon = b;
2978 return this;
2979 }
2980
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002981 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002982 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002983
2984 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2985
2986 return contentView;
2987 }
2988
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002989 /**
2990 * @hide
2991 */
2992 public void addExtras(Bundle extras) {
2993 super.addExtras(extras);
2994
2995 if (mBigLargeIconSet) {
2996 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2997 }
2998 extras.putParcelable(EXTRA_PICTURE, mPicture);
2999 }
3000
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003001 /**
3002 * @hide
3003 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003004 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003005 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003006 if (mBigLargeIconSet ) {
3007 mBuilder.mLargeIcon = mBigLargeIcon;
3008 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003009 wip.bigContentView = makeBigContentView();
3010 return wip;
3011 }
3012 }
3013
3014 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003015 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003016 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003017 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003018 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003019 * Notification notif = new Notification.Builder(mContext)
3020 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3021 * .setContentText(subject)
3022 * .setSmallIcon(R.drawable.new_mail)
3023 * .setLargeIcon(aBitmap)
3024 * .setStyle(new Notification.BigTextStyle()
3025 * .bigText(aVeryLongString))
3026 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003027 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003028 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003029 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003030 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003031 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003032 private CharSequence mBigText;
3033
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003034 public BigTextStyle() {
3035 }
3036
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003037 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003038 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003039 }
3040
Chris Wrend6297db2012-05-03 16:20:13 -04003041 /**
3042 * Overrides ContentTitle in the big form of the template.
3043 * This defaults to the value passed to setContentTitle().
3044 */
3045 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003046 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003047 return this;
3048 }
3049
3050 /**
3051 * Set the first line of text after the detail section in the big form of the template.
3052 */
3053 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003054 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003055 return this;
3056 }
3057
Chris Wren0bd664d2012-08-01 13:56:56 -04003058 /**
3059 * Provide the longer text to be displayed in the big form of the
3060 * template in place of the content text.
3061 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003062 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003063 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003064 return this;
3065 }
3066
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003067 /**
3068 * @hide
3069 */
3070 public void addExtras(Bundle extras) {
3071 super.addExtras(extras);
3072
3073 extras.putCharSequence(EXTRA_TEXT, mBigText);
3074 }
3075
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003076 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003077 // Remove the content text so line3 only shows if you have a summary
3078 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003079 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003080
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003081 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003082
Daniel Sandler619738c2012-06-07 16:33:08 -04003083 if (hadThreeLines) {
3084 // vertical centering
3085 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
3086 }
3087
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003088 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003089 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003090 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003091
3092 return contentView;
3093 }
3094
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003095 /**
3096 * @hide
3097 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003098 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003099 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003100 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003101
3102 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
3103
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003104 return wip;
3105 }
3106 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003107
3108 /**
3109 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003110 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003111 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003112 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003113 * Notification notif = new Notification.Builder(mContext)
3114 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3115 * .setContentText(subject)
3116 * .setSmallIcon(R.drawable.new_mail)
3117 * .setLargeIcon(aBitmap)
3118 * .setStyle(new Notification.InboxStyle()
3119 * .addLine(str1)
3120 * .addLine(str2)
3121 * .setContentTitle(&quot;&quot;)
3122 * .setSummaryText(&quot;+3 more&quot;))
3123 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003124 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003125 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003126 * @see Notification#bigContentView
3127 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003128 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003129 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3130
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003131 public InboxStyle() {
3132 }
3133
Daniel Sandler879c5e02012-04-17 16:46:51 -04003134 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003135 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003136 }
3137
Chris Wrend6297db2012-05-03 16:20:13 -04003138 /**
3139 * Overrides ContentTitle in the big form of the template.
3140 * This defaults to the value passed to setContentTitle().
3141 */
3142 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003143 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003144 return this;
3145 }
3146
3147 /**
3148 * Set the first line of text after the detail section in the big form of the template.
3149 */
3150 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003151 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003152 return this;
3153 }
3154
Chris Wren0bd664d2012-08-01 13:56:56 -04003155 /**
3156 * Append a line to the digest section of the Inbox notification.
3157 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003158 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003159 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003160 return this;
3161 }
3162
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003163 /**
3164 * @hide
3165 */
3166 public void addExtras(Bundle extras) {
3167 super.addExtras(extras);
3168 CharSequence[] a = new CharSequence[mTexts.size()];
3169 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3170 }
3171
Daniel Sandler879c5e02012-04-17 16:46:51 -04003172 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003173 // Remove the content text so line3 disappears unless you have a summary
3174 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003175 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003176
Chris Wrend6297db2012-05-03 16:20:13 -04003177 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003178
Chris Wrend6297db2012-05-03 16:20:13 -04003179 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 -04003180 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003181
Chris Wren4ed80d52012-05-17 09:30:03 -04003182 // Make sure all rows are gone in case we reuse a view.
3183 for (int rowId : rowIds) {
3184 contentView.setViewVisibility(rowId, View.GONE);
3185 }
3186
Chris Wren683ab002012-09-20 10:35:54 -04003187
Daniel Sandler879c5e02012-04-17 16:46:51 -04003188 int i=0;
3189 while (i < mTexts.size() && i < rowIds.length) {
3190 CharSequence str = mTexts.get(i);
3191 if (str != null && !str.equals("")) {
3192 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003193 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003194 }
3195 i++;
3196 }
3197
Chris Wren683ab002012-09-20 10:35:54 -04003198 contentView.setViewVisibility(R.id.inbox_end_pad,
3199 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3200
3201 contentView.setViewVisibility(R.id.inbox_more,
3202 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003203
Daniel Sandler879c5e02012-04-17 16:46:51 -04003204 return contentView;
3205 }
3206
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003207 /**
3208 * @hide
3209 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003210 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003211 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003212 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003213
Daniel Sandler879c5e02012-04-17 16:46:51 -04003214 return wip;
3215 }
3216 }
Dan Sandler842dd772014-05-15 09:36:47 -04003217
3218 /**
3219 * Notification style for media playback notifications.
3220 *
3221 * In the expanded form, {@link Notification#bigContentView}, up to 5
3222 * {@link Notification.Action}s specified with
3223 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3224 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3225 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3226 * treated as album artwork.
3227 *
3228 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3229 * {@link Notification#contentView}; by providing action indices to
3230 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3231 * in the standard view alongside the usual content.
3232 *
Jeff Browndba34ba2014-06-24 20:46:03 -07003233 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
3234 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04003235 * the System UI can identify this as a notification representing an active media session
3236 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3237 *
3238 * To use this style with your Notification, feed it to
3239 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3240 * <pre class="prettyprint">
3241 * Notification noti = new Notification.Builder()
3242 * .setSmallIcon(R.drawable.ic_stat_player)
3243 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3244 * .setContentText(&quot;Artist - Album&quot;) // if you use
3245 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token, true)
3246 * .setMediaSession(mySession, true)
3247 * .setStyle(<b>new Notification.MediaStyle()</b>)
3248 * .build();
3249 * </pre>
3250 *
3251 * @see Notification#bigContentView
3252 */
3253 public static class MediaStyle extends Style {
3254 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3255 static final int MAX_MEDIA_BUTTONS = 5;
3256
3257 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07003258 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04003259
3260 public MediaStyle() {
3261 }
3262
3263 public MediaStyle(Builder builder) {
3264 setBuilder(builder);
3265 }
3266
3267 /**
3268 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3269 * notification view.
3270 */
3271 public MediaStyle setShowActionsInCompactView(int...actions) {
3272 mActionsToShowInCompact = actions;
3273 return this;
3274 }
3275
3276 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07003277 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
3278 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04003279 */
Jeff Browndba34ba2014-06-24 20:46:03 -07003280 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04003281 mToken = token;
3282 return this;
3283 }
3284
3285 @Override
3286 public Notification buildStyled(Notification wip) {
3287 wip.contentView = makeMediaContentView();
3288 wip.bigContentView = makeMediaBigContentView();
3289
3290 return wip;
3291 }
3292
3293 /** @hide */
3294 @Override
3295 public void addExtras(Bundle extras) {
3296 super.addExtras(extras);
3297
3298 if (mToken != null) {
3299 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3300 }
3301 }
3302
3303 private RemoteViews generateMediaActionButton(Action action) {
3304 final boolean tombstone = (action.actionIntent == null);
3305 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07003306 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04003307 button.setImageViewResource(R.id.action0, action.icon);
3308 if (!tombstone) {
3309 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3310 }
3311 button.setContentDescription(R.id.action0, action.title);
3312 return button;
3313 }
3314
3315 private RemoteViews makeMediaContentView() {
3316 RemoteViews view = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003317 R.layout.notification_template_material_media, true /* 1U */);
Dan Sandler842dd772014-05-15 09:36:47 -04003318
3319 final int numActions = mBuilder.mActions.size();
3320 final int N = mActionsToShowInCompact == null
3321 ? 0
3322 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3323 if (N > 0) {
3324 view.removeAllViews(R.id.actions);
3325 for (int i = 0; i < N; i++) {
3326 if (i >= numActions) {
3327 throw new IllegalArgumentException(String.format(
3328 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3329 i, numActions - 1));
3330 }
3331
3332 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3333 final RemoteViews button = generateMediaActionButton(action);
3334 view.addView(R.id.actions, button);
3335 }
3336 }
3337 return view;
3338 }
3339
3340 private RemoteViews makeMediaBigContentView() {
3341 RemoteViews big = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003342 R.layout.notification_template_material_big_media, false);
Dan Sandler842dd772014-05-15 09:36:47 -04003343
3344 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3345 if (N > 0) {
3346 big.removeAllViews(R.id.actions);
3347 for (int i=0; i<N; i++) {
3348 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3349 big.addView(R.id.actions, button);
3350 }
3351 }
3352 return big;
3353 }
3354 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003355
3356 /**
3357 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
3358 * metadata or change options on a notification builder.
3359 */
3360 public interface Extender {
3361 /**
3362 * Apply this extender to a notification builder.
3363 * @param builder the builder to be modified.
3364 * @return the build object for chaining.
3365 */
3366 public Builder extend(Builder builder);
3367 }
3368
3369 /**
3370 * Helper class to add wearable extensions to notifications.
3371 * <p class="note"> See
3372 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
3373 * for Android Wear</a> for more information on how to use this class.
3374 * <p>
3375 * To create a notification with wearable extensions:
3376 * <ol>
3377 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
3378 * properties.
3379 * <li>Create a {@link android.app.Notification.WearableExtender}.
3380 * <li>Set wearable-specific properties using the
3381 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
3382 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
3383 * notification.
3384 * <li>Post the notification to the notification system with the
3385 * {@code NotificationManager.notify(...)} methods.
3386 * </ol>
3387 *
3388 * <pre class="prettyprint">
3389 * Notification notif = new Notification.Builder(mContext)
3390 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3391 * .setContentText(subject)
3392 * .setSmallIcon(R.drawable.new_mail)
3393 * .extend(new Notification.WearableExtender()
3394 * .setContentIcon(R.drawable.new_mail))
3395 * .build();
3396 * NotificationManager notificationManger =
3397 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3398 * notificationManger.notify(0, notif);</pre>
3399 *
3400 * <p>Wearable extensions can be accessed on an existing notification by using the
3401 * {@code WearableExtender(Notification)} constructor,
3402 * and then using the {@code get} methods to access values.
3403 *
3404 * <pre class="prettyprint">
3405 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
3406 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07003407 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003408 */
3409 public static final class WearableExtender implements Extender {
3410 /**
3411 * Sentinel value for an action index that is unset.
3412 */
3413 public static final int UNSET_ACTION_INDEX = -1;
3414
3415 /**
3416 * Size value for use with {@link #setCustomSizePreset} to show this notification with
3417 * default sizing.
3418 * <p>For custom display notifications created using {@link #setDisplayIntent},
3419 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
3420 * on their content.
3421 */
3422 public static final int SIZE_DEFAULT = 0;
3423
3424 /**
3425 * Size value for use with {@link #setCustomSizePreset} to show this notification
3426 * with an extra small size.
3427 * <p>This value is only applicable for custom display notifications created using
3428 * {@link #setDisplayIntent}.
3429 */
3430 public static final int SIZE_XSMALL = 1;
3431
3432 /**
3433 * Size value for use with {@link #setCustomSizePreset} to show this notification
3434 * with a small size.
3435 * <p>This value is only applicable for custom display notifications created using
3436 * {@link #setDisplayIntent}.
3437 */
3438 public static final int SIZE_SMALL = 2;
3439
3440 /**
3441 * Size value for use with {@link #setCustomSizePreset} to show this notification
3442 * with a medium size.
3443 * <p>This value is only applicable for custom display notifications created using
3444 * {@link #setDisplayIntent}.
3445 */
3446 public static final int SIZE_MEDIUM = 3;
3447
3448 /**
3449 * Size value for use with {@link #setCustomSizePreset} to show this notification
3450 * with a large size.
3451 * <p>This value is only applicable for custom display notifications created using
3452 * {@link #setDisplayIntent}.
3453 */
3454 public static final int SIZE_LARGE = 4;
3455
Griff Hazend5f11f92014-05-27 15:40:09 -07003456 /**
3457 * Size value for use with {@link #setCustomSizePreset} to show this notification
3458 * full screen.
3459 * <p>This value is only applicable for custom display notifications created using
3460 * {@link #setDisplayIntent}.
3461 */
3462 public static final int SIZE_FULL_SCREEN = 5;
3463
Griff Hazen61a9e862014-05-22 16:05:19 -07003464 /** Notification extra which contains wearable extensions */
3465 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
3466
3467 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
3468 private static final String KEY_ACTIONS = "actions";
3469 private static final String KEY_FLAGS = "flags";
3470 private static final String KEY_DISPLAY_INTENT = "displayIntent";
3471 private static final String KEY_PAGES = "pages";
3472 private static final String KEY_BACKGROUND = "background";
3473 private static final String KEY_CONTENT_ICON = "contentIcon";
3474 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
3475 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
3476 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
3477 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
3478 private static final String KEY_GRAVITY = "gravity";
3479
3480 // Flags bitwise-ored to mFlags
3481 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
3482 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
3483 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
3484 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
3485
3486 // Default value for flags integer
3487 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
3488
3489 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
3490 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
3491
3492 private ArrayList<Action> mActions = new ArrayList<Action>();
3493 private int mFlags = DEFAULT_FLAGS;
3494 private PendingIntent mDisplayIntent;
3495 private ArrayList<Notification> mPages = new ArrayList<Notification>();
3496 private Bitmap mBackground;
3497 private int mContentIcon;
3498 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
3499 private int mContentActionIndex = UNSET_ACTION_INDEX;
3500 private int mCustomSizePreset = SIZE_DEFAULT;
3501 private int mCustomContentHeight;
3502 private int mGravity = DEFAULT_GRAVITY;
3503
3504 /**
3505 * Create a {@link android.app.Notification.WearableExtender} with default
3506 * options.
3507 */
3508 public WearableExtender() {
3509 }
3510
3511 public WearableExtender(Notification notif) {
3512 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
3513 if (wearableBundle != null) {
3514 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
3515 if (actions != null) {
3516 mActions.addAll(actions);
3517 }
3518
3519 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
3520 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
3521
3522 Notification[] pages = getNotificationArrayFromBundle(
3523 wearableBundle, KEY_PAGES);
3524 if (pages != null) {
3525 Collections.addAll(mPages, pages);
3526 }
3527
3528 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
3529 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
3530 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
3531 DEFAULT_CONTENT_ICON_GRAVITY);
3532 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
3533 UNSET_ACTION_INDEX);
3534 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
3535 SIZE_DEFAULT);
3536 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
3537 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
3538 }
3539 }
3540
3541 /**
3542 * Apply wearable extensions to a notification that is being built. This is typically
3543 * called by the {@link android.app.Notification.Builder#extend} method of
3544 * {@link android.app.Notification.Builder}.
3545 */
3546 @Override
3547 public Notification.Builder extend(Notification.Builder builder) {
3548 Bundle wearableBundle = new Bundle();
3549
3550 if (!mActions.isEmpty()) {
3551 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
3552 }
3553 if (mFlags != DEFAULT_FLAGS) {
3554 wearableBundle.putInt(KEY_FLAGS, mFlags);
3555 }
3556 if (mDisplayIntent != null) {
3557 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
3558 }
3559 if (!mPages.isEmpty()) {
3560 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
3561 new Notification[mPages.size()]));
3562 }
3563 if (mBackground != null) {
3564 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
3565 }
3566 if (mContentIcon != 0) {
3567 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
3568 }
3569 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
3570 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
3571 }
3572 if (mContentActionIndex != UNSET_ACTION_INDEX) {
3573 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
3574 mContentActionIndex);
3575 }
3576 if (mCustomSizePreset != SIZE_DEFAULT) {
3577 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
3578 }
3579 if (mCustomContentHeight != 0) {
3580 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
3581 }
3582 if (mGravity != DEFAULT_GRAVITY) {
3583 wearableBundle.putInt(KEY_GRAVITY, mGravity);
3584 }
3585
3586 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
3587 return builder;
3588 }
3589
3590 @Override
3591 public WearableExtender clone() {
3592 WearableExtender that = new WearableExtender();
3593 that.mActions = new ArrayList<Action>(this.mActions);
3594 that.mFlags = this.mFlags;
3595 that.mDisplayIntent = this.mDisplayIntent;
3596 that.mPages = new ArrayList<Notification>(this.mPages);
3597 that.mBackground = this.mBackground;
3598 that.mContentIcon = this.mContentIcon;
3599 that.mContentIconGravity = this.mContentIconGravity;
3600 that.mContentActionIndex = this.mContentActionIndex;
3601 that.mCustomSizePreset = this.mCustomSizePreset;
3602 that.mCustomContentHeight = this.mCustomContentHeight;
3603 that.mGravity = this.mGravity;
3604 return that;
3605 }
3606
3607 /**
3608 * Add a wearable action to this notification.
3609 *
3610 * <p>When wearable actions are added using this method, the set of actions that
3611 * show on a wearable device splits from devices that only show actions added
3612 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3613 * of which actions display on different devices.
3614 *
3615 * @param action the action to add to this notification
3616 * @return this object for method chaining
3617 * @see android.app.Notification.Action
3618 */
3619 public WearableExtender addAction(Action action) {
3620 mActions.add(action);
3621 return this;
3622 }
3623
3624 /**
3625 * Adds wearable actions to this notification.
3626 *
3627 * <p>When wearable actions are added using this method, the set of actions that
3628 * show on a wearable device splits from devices that only show actions added
3629 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3630 * of which actions display on different devices.
3631 *
3632 * @param actions the actions to add to this notification
3633 * @return this object for method chaining
3634 * @see android.app.Notification.Action
3635 */
3636 public WearableExtender addActions(List<Action> actions) {
3637 mActions.addAll(actions);
3638 return this;
3639 }
3640
3641 /**
3642 * Clear all wearable actions present on this builder.
3643 * @return this object for method chaining.
3644 * @see #addAction
3645 */
3646 public WearableExtender clearActions() {
3647 mActions.clear();
3648 return this;
3649 }
3650
3651 /**
3652 * Get the wearable actions present on this notification.
3653 */
3654 public List<Action> getActions() {
3655 return mActions;
3656 }
3657
3658 /**
3659 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07003660 * this notification. The {@link PendingIntent} provided should be for an activity.
3661 *
3662 * <pre class="prettyprint">
3663 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
3664 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
3665 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3666 * Notification notif = new Notification.Builder(context)
3667 * .extend(new Notification.WearableExtender()
3668 * .setDisplayIntent(displayPendingIntent)
3669 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
3670 * .build();</pre>
3671 *
3672 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07003673 * should have an empty task affinity. It is also recommended to use the device
3674 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07003675 *
3676 * <p>Example AndroidManifest.xml entry:
3677 * <pre class="prettyprint">
3678 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
3679 * android:exported=&quot;true&quot;
3680 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07003681 * android:taskAffinity=&quot;&quot;
3682 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003683 *
3684 * @param intent the {@link PendingIntent} for an activity
3685 * @return this object for method chaining
3686 * @see android.app.Notification.WearableExtender#getDisplayIntent
3687 */
3688 public WearableExtender setDisplayIntent(PendingIntent intent) {
3689 mDisplayIntent = intent;
3690 return this;
3691 }
3692
3693 /**
3694 * Get the intent to launch inside of an activity view when displaying this
3695 * notification. This {@code PendingIntent} should be for an activity.
3696 */
3697 public PendingIntent getDisplayIntent() {
3698 return mDisplayIntent;
3699 }
3700
3701 /**
3702 * Add an additional page of content to display with this notification. The current
3703 * notification forms the first page, and pages added using this function form
3704 * subsequent pages. This field can be used to separate a notification into multiple
3705 * sections.
3706 *
3707 * @param page the notification to add as another page
3708 * @return this object for method chaining
3709 * @see android.app.Notification.WearableExtender#getPages
3710 */
3711 public WearableExtender addPage(Notification page) {
3712 mPages.add(page);
3713 return this;
3714 }
3715
3716 /**
3717 * Add additional pages of content to display with this notification. The current
3718 * notification forms the first page, and pages added using this function form
3719 * subsequent pages. This field can be used to separate a notification into multiple
3720 * sections.
3721 *
3722 * @param pages a list of notifications
3723 * @return this object for method chaining
3724 * @see android.app.Notification.WearableExtender#getPages
3725 */
3726 public WearableExtender addPages(List<Notification> pages) {
3727 mPages.addAll(pages);
3728 return this;
3729 }
3730
3731 /**
3732 * Clear all additional pages present on this builder.
3733 * @return this object for method chaining.
3734 * @see #addPage
3735 */
3736 public WearableExtender clearPages() {
3737 mPages.clear();
3738 return this;
3739 }
3740
3741 /**
3742 * Get the array of additional pages of content for displaying this notification. The
3743 * current notification forms the first page, and elements within this array form
3744 * subsequent pages. This field can be used to separate a notification into multiple
3745 * sections.
3746 * @return the pages for this notification
3747 */
3748 public List<Notification> getPages() {
3749 return mPages;
3750 }
3751
3752 /**
3753 * Set a background image to be displayed behind the notification content.
3754 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3755 * will work with any notification style.
3756 *
3757 * @param background the background bitmap
3758 * @return this object for method chaining
3759 * @see android.app.Notification.WearableExtender#getBackground
3760 */
3761 public WearableExtender setBackground(Bitmap background) {
3762 mBackground = background;
3763 return this;
3764 }
3765
3766 /**
3767 * Get a background image to be displayed behind the notification content.
3768 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3769 * will work with any notification style.
3770 *
3771 * @return the background image
3772 * @see android.app.Notification.WearableExtender#setBackground
3773 */
3774 public Bitmap getBackground() {
3775 return mBackground;
3776 }
3777
3778 /**
3779 * Set an icon that goes with the content of this notification.
3780 */
3781 public WearableExtender setContentIcon(int icon) {
3782 mContentIcon = icon;
3783 return this;
3784 }
3785
3786 /**
3787 * Get an icon that goes with the content of this notification.
3788 */
3789 public int getContentIcon() {
3790 return mContentIcon;
3791 }
3792
3793 /**
3794 * Set the gravity that the content icon should have within the notification display.
3795 * Supported values include {@link android.view.Gravity#START} and
3796 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3797 * @see #setContentIcon
3798 */
3799 public WearableExtender setContentIconGravity(int contentIconGravity) {
3800 mContentIconGravity = contentIconGravity;
3801 return this;
3802 }
3803
3804 /**
3805 * Get the gravity that the content icon should have within the notification display.
3806 * Supported values include {@link android.view.Gravity#START} and
3807 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3808 * @see #getContentIcon
3809 */
3810 public int getContentIconGravity() {
3811 return mContentIconGravity;
3812 }
3813
3814 /**
3815 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07003816 * this notification. This action will no longer display separately from the
3817 * notification's content.
3818 *
Griff Hazenca48d352014-05-28 22:37:13 -07003819 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003820 * set, although the list of available actions comes from the main notification and not
3821 * from the child page's notification.
3822 *
3823 * @param actionIndex The index of the action to hoist onto the current notification page.
3824 * If wearable actions were added to the main notification, this index
3825 * will apply to that list, otherwise it will apply to the regular
3826 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07003827 */
3828 public WearableExtender setContentAction(int actionIndex) {
3829 mContentActionIndex = actionIndex;
3830 return this;
3831 }
3832
3833 /**
Griff Hazenca48d352014-05-28 22:37:13 -07003834 * Get the index of the notification action, if any, that was specified as being clickable
3835 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07003836 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07003837 *
Griff Hazenca48d352014-05-28 22:37:13 -07003838 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003839 * set, although the list of available actions comes from the main notification and not
3840 * from the child page's notification.
3841 *
3842 * <p>If wearable specific actions were added to the main notification, this index will
3843 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07003844 *
3845 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07003846 */
3847 public int getContentAction() {
3848 return mContentActionIndex;
3849 }
3850
3851 /**
3852 * Set the gravity that this notification should have within the available viewport space.
3853 * Supported values include {@link android.view.Gravity#TOP},
3854 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3855 * The default value is {@link android.view.Gravity#BOTTOM}.
3856 */
3857 public WearableExtender setGravity(int gravity) {
3858 mGravity = gravity;
3859 return this;
3860 }
3861
3862 /**
3863 * Get the gravity that this notification should have within the available viewport space.
3864 * Supported values include {@link android.view.Gravity#TOP},
3865 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3866 * The default value is {@link android.view.Gravity#BOTTOM}.
3867 */
3868 public int getGravity() {
3869 return mGravity;
3870 }
3871
3872 /**
3873 * Set the custom size preset for the display of this notification out of the available
3874 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3875 * {@link #SIZE_LARGE}.
3876 * <p>Some custom size presets are only applicable for custom display notifications created
3877 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
3878 * documentation for the preset in question. See also
3879 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
3880 */
3881 public WearableExtender setCustomSizePreset(int sizePreset) {
3882 mCustomSizePreset = sizePreset;
3883 return this;
3884 }
3885
3886 /**
3887 * Get the custom size preset for the display of this notification out of the available
3888 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3889 * {@link #SIZE_LARGE}.
3890 * <p>Some custom size presets are only applicable for custom display notifications created
3891 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
3892 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
3893 */
3894 public int getCustomSizePreset() {
3895 return mCustomSizePreset;
3896 }
3897
3898 /**
3899 * Set the custom height in pixels for the display of this notification's content.
3900 * <p>This option is only available for custom display notifications created
3901 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
3902 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
3903 * {@link #getCustomContentHeight}.
3904 */
3905 public WearableExtender setCustomContentHeight(int height) {
3906 mCustomContentHeight = height;
3907 return this;
3908 }
3909
3910 /**
3911 * Get the custom height in pixels for the display of this notification's content.
3912 * <p>This option is only available for custom display notifications created
3913 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
3914 * {@link #setCustomContentHeight}.
3915 */
3916 public int getCustomContentHeight() {
3917 return mCustomContentHeight;
3918 }
3919
3920 /**
3921 * Set whether the scrolling position for the contents of this notification should start
3922 * at the bottom of the contents instead of the top when the contents are too long to
3923 * display within the screen. Default is false (start scroll at the top).
3924 */
3925 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
3926 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
3927 return this;
3928 }
3929
3930 /**
3931 * Get whether the scrolling position for the contents of this notification should start
3932 * at the bottom of the contents instead of the top when the contents are too long to
3933 * display within the screen. Default is false (start scroll at the top).
3934 */
3935 public boolean getStartScrollBottom() {
3936 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
3937 }
3938
3939 /**
3940 * Set whether the content intent is available when the wearable device is not connected
3941 * to a companion device. The user can still trigger this intent when the wearable device
3942 * is offline, but a visual hint will indicate that the content intent may not be available.
3943 * Defaults to true.
3944 */
3945 public WearableExtender setContentIntentAvailableOffline(
3946 boolean contentIntentAvailableOffline) {
3947 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
3948 return this;
3949 }
3950
3951 /**
3952 * Get whether the content intent is available when the wearable device is not connected
3953 * to a companion device. The user can still trigger this intent when the wearable device
3954 * is offline, but a visual hint will indicate that the content intent may not be available.
3955 * Defaults to true.
3956 */
3957 public boolean getContentIntentAvailableOffline() {
3958 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
3959 }
3960
3961 /**
3962 * Set a hint that this notification's icon should not be displayed.
3963 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
3964 * @return this object for method chaining
3965 */
3966 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
3967 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
3968 return this;
3969 }
3970
3971 /**
3972 * Get a hint that this notification's icon should not be displayed.
3973 * @return {@code true} if this icon should not be displayed, false otherwise.
3974 * The default value is {@code false} if this was never set.
3975 */
3976 public boolean getHintHideIcon() {
3977 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
3978 }
3979
3980 /**
3981 * Set a visual hint that only the background image of this notification should be
3982 * displayed, and other semantic content should be hidden. This hint is only applicable
3983 * to sub-pages added using {@link #addPage}.
3984 */
3985 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
3986 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
3987 return this;
3988 }
3989
3990 /**
3991 * Get a visual hint that only the background image of this notification should be
3992 * displayed, and other semantic content should be hidden. This hint is only applicable
3993 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
3994 */
3995 public boolean getHintShowBackgroundOnly() {
3996 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
3997 }
3998
3999 private void setFlag(int mask, boolean value) {
4000 if (value) {
4001 mFlags |= mask;
4002 } else {
4003 mFlags &= ~mask;
4004 }
4005 }
4006 }
4007
4008 /**
4009 * Get an array of Notification objects from a parcelable array bundle field.
4010 * Update the bundle to have a typed array so fetches in the future don't need
4011 * to do an array copy.
4012 */
4013 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4014 Parcelable[] array = bundle.getParcelableArray(key);
4015 if (array instanceof Notification[] || array == null) {
4016 return (Notification[]) array;
4017 }
4018 Notification[] typedArray = Arrays.copyOf(array, array.length,
4019 Notification[].class);
4020 bundle.putParcelableArray(key, typedArray);
4021 return typedArray;
4022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023}