blob: 97a99203153da3b57a9819fb755800d12ab7258c [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 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400747 * {@link #extras} key: used to provide hints about the appropriateness of
748 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400749 * @hide
750 */
751 public static final String EXTRA_AS_HEADS_UP = "headsup";
752
753 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400754 * Allow certain system-generated notifications to appear before the device is provisioned.
755 * Only available to notifications coming from the android package.
756 * @hide
757 */
758 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
759
760 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700761 * {@link #extras} key: A
762 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
763 * in the background when the notification is selected. The URI must point to an image stream
764 * suitable for passing into
765 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
766 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
767 * URI used for this purpose must require no permissions to read the image data.
768 */
769 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
770
771 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400772 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700773 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400774 * {@link android.app.Notification.MediaStyle} notification.
775 */
776 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
777
778 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400779 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
780 * displayed in the heads up space.
781 *
782 * <p>
783 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
784 * full-screen intent when posted.
785 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400786 * @hide
787 */
788 public static final int HEADS_UP_NEVER = 0;
789
790 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400791 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
792 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400793 * @hide
794 */
795 public static final int HEADS_UP_ALLOWED = 1;
796
797 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400798 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
799 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400800 * @hide
801 */
802 public static final int HEADS_UP_REQUESTED = 2;
803
804 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400805 * Structure to encapsulate a named action that can be shown as part of this notification.
806 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
807 * selected by the user.
808 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700809 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
810 * or {@link Notification.Builder#addAction(Notification.Action)}
811 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400812 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500813 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700814 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700815 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700816
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400817 /**
818 * Small icon representing the action.
819 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400820 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700821
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400822 /**
823 * Title of the action.
824 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400825 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700826
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400827 /**
828 * Intent to send when the user invokes this action. May be null, in which case the action
829 * may be rendered in a disabled presentation by the system UI.
830 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400831 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700832
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400833 private Action(Parcel in) {
834 icon = in.readInt();
835 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
836 if (in.readInt() == 1) {
837 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
838 }
Griff Hazen959591e2014-05-15 22:26:18 -0700839 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700840 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400841 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700842
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400843 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700844 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845 */
846 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700847 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700848 }
849
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700850 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
851 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400852 this.icon = icon;
853 this.title = title;
854 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700855 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700856 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700857 }
858
859 /**
860 * Get additional metadata carried around with this Action.
861 */
862 public Bundle getExtras() {
863 return mExtras;
864 }
865
866 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700867 * Get the list of inputs to be collected from the user when this action is sent.
868 * May return null if no remote inputs were added.
869 */
870 public RemoteInput[] getRemoteInputs() {
871 return mRemoteInputs;
872 }
873
874 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700875 * Builder class for {@link Action} objects.
876 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700877 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700878 private final int mIcon;
879 private final CharSequence mTitle;
880 private final PendingIntent mIntent;
881 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700882 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700883
884 /**
885 * Construct a new builder for {@link Action} object.
886 * @param icon icon to show for this action
887 * @param title the title of the action
888 * @param intent the {@link PendingIntent} to fire when users trigger this action
889 */
890 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700891 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700892 }
893
894 /**
895 * Construct a new builder for {@link Action} object using the fields from an
896 * {@link Action}.
897 * @param action the action to read fields from.
898 */
899 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700900 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
901 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700902 }
903
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700904 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
905 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700906 mIcon = icon;
907 mTitle = title;
908 mIntent = intent;
909 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700910 if (remoteInputs != null) {
911 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
912 Collections.addAll(mRemoteInputs, remoteInputs);
913 }
Griff Hazen959591e2014-05-15 22:26:18 -0700914 }
915
916 /**
917 * Merge additional metadata into this builder.
918 *
919 * <p>Values within the Bundle will replace existing extras values in this Builder.
920 *
921 * @see Notification.Action#extras
922 */
923 public Builder addExtras(Bundle extras) {
924 if (extras != null) {
925 mExtras.putAll(extras);
926 }
927 return this;
928 }
929
930 /**
931 * Get the metadata Bundle used by this Builder.
932 *
933 * <p>The returned Bundle is shared with this Builder.
934 */
935 public Bundle getExtras() {
936 return mExtras;
937 }
938
939 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700940 * Add an input to be collected from the user when this action is sent.
941 * Response values can be retrieved from the fired intent by using the
942 * {@link RemoteInput#getResultsFromIntent} function.
943 * @param remoteInput a {@link RemoteInput} to add to the action
944 * @return this object for method chaining
945 */
946 public Builder addRemoteInput(RemoteInput remoteInput) {
947 if (mRemoteInputs == null) {
948 mRemoteInputs = new ArrayList<RemoteInput>();
949 }
950 mRemoteInputs.add(remoteInput);
951 return this;
952 }
953
954 /**
955 * Apply an extender to this action builder. Extenders may be used to add
956 * metadata or change options on this builder.
957 */
Griff Hazen61a9e862014-05-22 16:05:19 -0700958 public Builder extend(Extender extender) {
959 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700960 return this;
961 }
962
963 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700964 * Combine all of the options that have been set and return a new {@link Action}
965 * object.
966 * @return the built action
967 */
968 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700969 RemoteInput[] remoteInputs = mRemoteInputs != null
970 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
971 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -0700972 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400973 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400974
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400975 @Override
976 public Action clone() {
977 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700978 icon,
979 title,
980 actionIntent, // safe to alias
981 new Bundle(mExtras),
982 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400983 }
984 @Override
985 public int describeContents() {
986 return 0;
987 }
988 @Override
989 public void writeToParcel(Parcel out, int flags) {
990 out.writeInt(icon);
991 TextUtils.writeToParcel(title, out, flags);
992 if (actionIntent != null) {
993 out.writeInt(1);
994 actionIntent.writeToParcel(out, flags);
995 } else {
996 out.writeInt(0);
997 }
Griff Hazen959591e2014-05-15 22:26:18 -0700998 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700999 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001000 }
Griff Hazen959591e2014-05-15 22:26:18 -07001001 public static final Parcelable.Creator<Action> CREATOR =
1002 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001003 public Action createFromParcel(Parcel in) {
1004 return new Action(in);
1005 }
1006 public Action[] newArray(int size) {
1007 return new Action[size];
1008 }
1009 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001010
1011 /**
1012 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1013 * metadata or change options on an action builder.
1014 */
1015 public interface Extender {
1016 /**
1017 * Apply this extender to a notification action builder.
1018 * @param builder the builder to be modified.
1019 * @return the build object for chaining.
1020 */
1021 public Builder extend(Builder builder);
1022 }
1023
1024 /**
1025 * Wearable extender for notification actions. To add extensions to an action,
1026 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1027 * the {@code WearableExtender()} constructor and apply it to a
1028 * {@link android.app.Notification.Action.Builder} using
1029 * {@link android.app.Notification.Action.Builder#extend}.
1030 *
1031 * <pre class="prettyprint">
1032 * Notification.Action action = new Notification.Action.Builder(
1033 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001034 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001035 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001036 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001037 */
1038 public static final class WearableExtender implements Extender {
1039 /** Notification action extra which contains wearable extensions */
1040 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1041
1042 private static final String KEY_FLAGS = "flags";
1043
1044 // Flags bitwise-ored to mFlags
1045 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1046
1047 // Default value for flags integer
1048 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1049
1050 private int mFlags = DEFAULT_FLAGS;
1051
1052 /**
1053 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1054 * options.
1055 */
1056 public WearableExtender() {
1057 }
1058
1059 /**
1060 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1061 * wearable options present in an existing notification action.
1062 * @param action the notification action to inspect.
1063 */
1064 public WearableExtender(Action action) {
1065 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1066 if (wearableBundle != null) {
1067 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1068 }
1069 }
1070
1071 /**
1072 * Apply wearable extensions to a notification action that is being built. This is
1073 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1074 * method of {@link android.app.Notification.Action.Builder}.
1075 */
1076 @Override
1077 public Action.Builder extend(Action.Builder builder) {
1078 Bundle wearableBundle = new Bundle();
1079
1080 if (mFlags != DEFAULT_FLAGS) {
1081 wearableBundle.putInt(KEY_FLAGS, mFlags);
1082 }
1083
1084 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1085 return builder;
1086 }
1087
1088 @Override
1089 public WearableExtender clone() {
1090 WearableExtender that = new WearableExtender();
1091 that.mFlags = this.mFlags;
1092 return that;
1093 }
1094
1095 /**
1096 * Set whether this action is available when the wearable device is not connected to
1097 * a companion device. The user can still trigger this action when the wearable device is
1098 * offline, but a visual hint will indicate that the action may not be available.
1099 * Defaults to true.
1100 */
1101 public WearableExtender setAvailableOffline(boolean availableOffline) {
1102 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1103 return this;
1104 }
1105
1106 /**
1107 * Get whether this action is available when the wearable device is not connected to
1108 * a companion device. The user can still trigger this action when the wearable device is
1109 * offline, but a visual hint will indicate that the action may not be available.
1110 * Defaults to true.
1111 */
1112 public boolean isAvailableOffline() {
1113 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1114 }
1115
1116 private void setFlag(int mask, boolean value) {
1117 if (value) {
1118 mFlags |= mask;
1119 } else {
1120 mFlags &= ~mask;
1121 }
1122 }
1123 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001124 }
1125
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001126 /**
1127 * Array of all {@link Action} structures attached to this notification by
1128 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1129 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1130 * interface for invoking actions.
1131 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001132 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001133
1134 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001135 * Replacement version of this notification whose content will be shown
1136 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1137 * and {@link #VISIBILITY_PUBLIC}.
1138 */
1139 public Notification publicVersion;
1140
1141 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001142 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001143 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 */
1145 public Notification()
1146 {
1147 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001148 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
1150
1151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 * @hide
1153 */
1154 public Notification(Context context, int icon, CharSequence tickerText, long when,
1155 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1156 {
1157 this.when = when;
1158 this.icon = icon;
1159 this.tickerText = tickerText;
1160 setLatestEventInfo(context, contentTitle, contentText,
1161 PendingIntent.getActivity(context, 0, contentIntent, 0));
1162 }
1163
1164 /**
1165 * Constructs a Notification object with the information needed to
1166 * have a status bar icon without the standard expanded view.
1167 *
1168 * @param icon The resource id of the icon to put in the status bar.
1169 * @param tickerText The text that flows by in the status bar when the notification first
1170 * activates.
1171 * @param when The time to show in the time field. In the System.currentTimeMillis
1172 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001173 *
1174 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001176 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 public Notification(int icon, CharSequence tickerText, long when)
1178 {
1179 this.icon = icon;
1180 this.tickerText = tickerText;
1181 this.when = when;
1182 }
1183
1184 /**
1185 * Unflatten the notification from a parcel.
1186 */
1187 public Notification(Parcel parcel)
1188 {
1189 int version = parcel.readInt();
1190
1191 when = parcel.readLong();
1192 icon = parcel.readInt();
1193 number = parcel.readInt();
1194 if (parcel.readInt() != 0) {
1195 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1196 }
1197 if (parcel.readInt() != 0) {
1198 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1199 }
1200 if (parcel.readInt() != 0) {
1201 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1202 }
1203 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001204 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001205 }
1206 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1208 }
Joe Onorato561d3852010-11-20 18:09:34 -08001209 if (parcel.readInt() != 0) {
1210 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 defaults = parcel.readInt();
1213 flags = parcel.readInt();
1214 if (parcel.readInt() != 0) {
1215 sound = Uri.CREATOR.createFromParcel(parcel);
1216 }
1217
1218 audioStreamType = parcel.readInt();
1219 vibrate = parcel.createLongArray();
1220 ledARGB = parcel.readInt();
1221 ledOnMS = parcel.readInt();
1222 ledOffMS = parcel.readInt();
1223 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001224
1225 if (parcel.readInt() != 0) {
1226 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1227 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001228
1229 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001230
John Spurlockfd7f1e02014-03-18 16:41:57 -04001231 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001232
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001233 mGroupKey = parcel.readString();
1234
1235 mSortKey = parcel.readString();
1236
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001237 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001238
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001239 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1240
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001241 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001242 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1243 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001244
Chris Wren8fd39ec2014-02-27 17:43:26 -05001245 if (parcel.readInt() != 0) {
1246 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1247 }
1248
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001249 visibility = parcel.readInt();
1250
1251 if (parcel.readInt() != 0) {
1252 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1253 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001254
1255 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 }
1257
Andy Stadler110988c2010-12-03 14:29:16 -08001258 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001259 public Notification clone() {
1260 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001261 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001262 return that;
1263 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001264
Daniel Sandler1a497d32013-04-18 14:52:45 -04001265 /**
1266 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1267 * of this into that.
1268 * @hide
1269 */
1270 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001271 that.when = this.when;
1272 that.icon = this.icon;
1273 that.number = this.number;
1274
1275 // PendingIntents are global, so there's no reason (or way) to clone them.
1276 that.contentIntent = this.contentIntent;
1277 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001278 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001279
1280 if (this.tickerText != null) {
1281 that.tickerText = this.tickerText.toString();
1282 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001283 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001284 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001285 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001286 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001287 that.contentView = this.contentView.clone();
1288 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001289 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001290 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1291 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001292 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001293 that.sound = this.sound; // android.net.Uri is immutable
1294 that.audioStreamType = this.audioStreamType;
1295
1296 final long[] vibrate = this.vibrate;
1297 if (vibrate != null) {
1298 final int N = vibrate.length;
1299 final long[] vib = that.vibrate = new long[N];
1300 System.arraycopy(vibrate, 0, vib, 0, N);
1301 }
1302
1303 that.ledARGB = this.ledARGB;
1304 that.ledOnMS = this.ledOnMS;
1305 that.ledOffMS = this.ledOffMS;
1306 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001307
Joe Onorato18e69df2010-05-17 22:26:12 -07001308 that.flags = this.flags;
1309
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001310 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001311
John Spurlockfd7f1e02014-03-18 16:41:57 -04001312 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001313
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001314 that.mGroupKey = this.mGroupKey;
1315
1316 that.mSortKey = this.mSortKey;
1317
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001318 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001319 try {
1320 that.extras = new Bundle(this.extras);
1321 // will unparcel
1322 that.extras.size();
1323 } catch (BadParcelableException e) {
1324 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1325 that.extras = null;
1326 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001327 }
1328
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001329 if (this.actions != null) {
1330 that.actions = new Action[this.actions.length];
1331 for(int i=0; i<this.actions.length; i++) {
1332 that.actions[i] = this.actions[i].clone();
1333 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001334 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001335
Daniel Sandler1a497d32013-04-18 14:52:45 -04001336 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001337 that.bigContentView = this.bigContentView.clone();
1338 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001339
Chris Wren8fd39ec2014-02-27 17:43:26 -05001340 if (heavy && this.headsUpContentView != null) {
1341 that.headsUpContentView = this.headsUpContentView.clone();
1342 }
1343
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001344 that.visibility = this.visibility;
1345
1346 if (this.publicVersion != null) {
1347 that.publicVersion = new Notification();
1348 this.publicVersion.cloneInto(that.publicVersion, heavy);
1349 }
1350
Dan Sandler26e81cf2014-05-06 10:01:27 -04001351 that.color = this.color;
1352
Daniel Sandler1a497d32013-04-18 14:52:45 -04001353 if (!heavy) {
1354 that.lightenPayload(); // will clean out extras
1355 }
1356 }
1357
1358 /**
1359 * Removes heavyweight parts of the Notification object for archival or for sending to
1360 * listeners when the full contents are not necessary.
1361 * @hide
1362 */
1363 public final void lightenPayload() {
1364 tickerView = null;
1365 contentView = null;
1366 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001367 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001368 largeIcon = null;
1369 if (extras != null) {
1370 extras.remove(Notification.EXTRA_LARGE_ICON);
1371 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1372 extras.remove(Notification.EXTRA_PICTURE);
1373 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001374 }
1375
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001376 /**
1377 * Make sure this CharSequence is safe to put into a bundle, which basically
1378 * means it had better not be some custom Parcelable implementation.
1379 * @hide
1380 */
1381 public static CharSequence safeCharSequence(CharSequence cs) {
1382 if (cs instanceof Parcelable) {
1383 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1384 + " instance is a custom Parcelable and not allowed in Notification");
1385 return cs.toString();
1386 }
1387
1388 return cs;
1389 }
1390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 public int describeContents() {
1392 return 0;
1393 }
1394
1395 /**
1396 * Flatten this notification from a parcel.
1397 */
1398 public void writeToParcel(Parcel parcel, int flags)
1399 {
1400 parcel.writeInt(1);
1401
1402 parcel.writeLong(when);
1403 parcel.writeInt(icon);
1404 parcel.writeInt(number);
1405 if (contentIntent != null) {
1406 parcel.writeInt(1);
1407 contentIntent.writeToParcel(parcel, 0);
1408 } else {
1409 parcel.writeInt(0);
1410 }
1411 if (deleteIntent != null) {
1412 parcel.writeInt(1);
1413 deleteIntent.writeToParcel(parcel, 0);
1414 } else {
1415 parcel.writeInt(0);
1416 }
1417 if (tickerText != null) {
1418 parcel.writeInt(1);
1419 TextUtils.writeToParcel(tickerText, parcel, flags);
1420 } else {
1421 parcel.writeInt(0);
1422 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001423 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001424 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001425 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001426 } else {
1427 parcel.writeInt(0);
1428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 if (contentView != null) {
1430 parcel.writeInt(1);
1431 contentView.writeToParcel(parcel, 0);
1432 } else {
1433 parcel.writeInt(0);
1434 }
Joe Onorato561d3852010-11-20 18:09:34 -08001435 if (largeIcon != null) {
1436 parcel.writeInt(1);
1437 largeIcon.writeToParcel(parcel, 0);
1438 } else {
1439 parcel.writeInt(0);
1440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441
1442 parcel.writeInt(defaults);
1443 parcel.writeInt(this.flags);
1444
1445 if (sound != null) {
1446 parcel.writeInt(1);
1447 sound.writeToParcel(parcel, 0);
1448 } else {
1449 parcel.writeInt(0);
1450 }
1451 parcel.writeInt(audioStreamType);
1452 parcel.writeLongArray(vibrate);
1453 parcel.writeInt(ledARGB);
1454 parcel.writeInt(ledOnMS);
1455 parcel.writeInt(ledOffMS);
1456 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001457
1458 if (fullScreenIntent != null) {
1459 parcel.writeInt(1);
1460 fullScreenIntent.writeToParcel(parcel, 0);
1461 } else {
1462 parcel.writeInt(0);
1463 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001464
1465 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001466
John Spurlockfd7f1e02014-03-18 16:41:57 -04001467 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001468
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001469 parcel.writeString(mGroupKey);
1470
1471 parcel.writeString(mSortKey);
1472
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001473 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001474
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001475 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001476
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001477 if (bigContentView != null) {
1478 parcel.writeInt(1);
1479 bigContentView.writeToParcel(parcel, 0);
1480 } else {
1481 parcel.writeInt(0);
1482 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001483
Chris Wren8fd39ec2014-02-27 17:43:26 -05001484 if (headsUpContentView != null) {
1485 parcel.writeInt(1);
1486 headsUpContentView.writeToParcel(parcel, 0);
1487 } else {
1488 parcel.writeInt(0);
1489 }
1490
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001491 parcel.writeInt(visibility);
1492
1493 if (publicVersion != null) {
1494 parcel.writeInt(1);
1495 publicVersion.writeToParcel(parcel, 0);
1496 } else {
1497 parcel.writeInt(0);
1498 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001499
1500 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 }
1502
1503 /**
1504 * Parcelable.Creator that instantiates Notification objects
1505 */
1506 public static final Parcelable.Creator<Notification> CREATOR
1507 = new Parcelable.Creator<Notification>()
1508 {
1509 public Notification createFromParcel(Parcel parcel)
1510 {
1511 return new Notification(parcel);
1512 }
1513
1514 public Notification[] newArray(int size)
1515 {
1516 return new Notification[size];
1517 }
1518 };
1519
1520 /**
1521 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1522 * layout.
1523 *
1524 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1525 * in the view.</p>
1526 * @param context The context for your application / activity.
1527 * @param contentTitle The title that goes in the expanded entry.
1528 * @param contentText The text that goes in the expanded entry.
1529 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1530 * If this is an activity, it must include the
1531 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001532 * that you take care of task management as described in the
1533 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1534 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001535 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001536 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001538 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 public void setLatestEventInfo(Context context,
1540 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001541 Notification.Builder builder = new Notification.Builder(context);
1542
1543 // First, ensure that key pieces of information that may have been set directly
1544 // are preserved
1545 builder.setWhen(this.when);
1546 builder.setSmallIcon(this.icon);
1547 builder.setPriority(this.priority);
1548 builder.setTicker(this.tickerText);
1549 builder.setNumber(this.number);
1550 builder.mFlags = this.flags;
1551 builder.setSound(this.sound, this.audioStreamType);
1552 builder.setDefaults(this.defaults);
1553 builder.setVibrate(this.vibrate);
1554
1555 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001557 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001560 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001562 builder.setContentIntent(contentIntent);
1563 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565
1566 @Override
1567 public String toString() {
1568 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001569 sb.append("Notification(pri=");
1570 sb.append(priority);
1571 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001572 if (contentView != null) {
1573 sb.append(contentView.getPackage());
1574 sb.append("/0x");
1575 sb.append(Integer.toHexString(contentView.getLayoutId()));
1576 } else {
1577 sb.append("null");
1578 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001579 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001580 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001581 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1582 sb.append("default");
1583 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 int N = this.vibrate.length-1;
1585 sb.append("[");
1586 for (int i=0; i<N; i++) {
1587 sb.append(this.vibrate[i]);
1588 sb.append(',');
1589 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001590 if (N != -1) {
1591 sb.append(this.vibrate[N]);
1592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 } else {
1595 sb.append("null");
1596 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001597 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001598 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001600 } else if (this.sound != null) {
1601 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 } else {
1603 sb.append("null");
1604 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001605 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001607 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001608 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001609 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001610 if (this.category != null) {
1611 sb.append(" category=");
1612 sb.append(this.category);
1613 }
1614 if (this.mGroupKey != null) {
1615 sb.append(" groupKey=");
1616 sb.append(this.mGroupKey);
1617 }
1618 if (this.mSortKey != null) {
1619 sb.append(" sortKey=");
1620 sb.append(this.mSortKey);
1621 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001622 if (actions != null) {
1623 sb.append(" ");
1624 sb.append(actions.length);
1625 sb.append(" action");
1626 if (actions.length > 1) sb.append("s");
1627 }
1628 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 return sb.toString();
1630 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001631
Jeff Sharkey6d515712012-09-20 16:06:08 -07001632 /** {@hide} */
1633 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001634 if (user.getIdentifier() == UserHandle.USER_ALL) {
1635 user = UserHandle.OWNER;
1636 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001637 if (tickerView != null) {
1638 tickerView.setUser(user);
1639 }
1640 if (contentView != null) {
1641 contentView.setUser(user);
1642 }
1643 if (bigContentView != null) {
1644 bigContentView.setUser(user);
1645 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001646 if (headsUpContentView != null) {
1647 headsUpContentView.setUser(user);
1648 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001649 }
1650
Joe Onoratocb109a02011-01-18 17:57:41 -08001651 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001652 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001653 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001654 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001655 * content views using the platform's notification layout template. If your app supports
1656 * versions of Android as old as API level 4, you can instead use
1657 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1658 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1659 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001660 *
Scott Main183bf112012-08-13 19:12:13 -07001661 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001662 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001663 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001664 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001665 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1666 * .setContentText(subject)
1667 * .setSmallIcon(R.drawable.new_mail)
1668 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001669 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001670 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001671 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001672 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001673 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001674
Joe Onorato46439ce2010-11-19 13:56:21 -08001675 private Context mContext;
1676
1677 private long mWhen;
1678 private int mSmallIcon;
1679 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001680 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001681 private CharSequence mContentTitle;
1682 private CharSequence mContentText;
1683 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001684 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001685 private PendingIntent mContentIntent;
1686 private RemoteViews mContentView;
1687 private PendingIntent mDeleteIntent;
1688 private PendingIntent mFullScreenIntent;
1689 private CharSequence mTickerText;
1690 private RemoteViews mTickerView;
1691 private Bitmap mLargeIcon;
1692 private Uri mSound;
1693 private int mAudioStreamType;
1694 private long[] mVibrate;
1695 private int mLedArgb;
1696 private int mLedOnMs;
1697 private int mLedOffMs;
1698 private int mDefaults;
1699 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001700 private int mProgressMax;
1701 private int mProgress;
1702 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001703 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001704 private String mGroupKey;
1705 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001706 private Bundle mExtras;
1707 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001708 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001709 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001710 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001711 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001712 private int mVisibility = VISIBILITY_PRIVATE;
1713 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001714 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001715 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001716 private int mColor = COLOR_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001717
Joe Onoratocb109a02011-01-18 17:57:41 -08001718 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001719 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001720 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001721
1722 * <table>
1723 * <tr><th align=right>priority</th>
1724 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1725 * <tr><th align=right>when</th>
1726 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1727 * <tr><th align=right>audio stream</th>
1728 * <td>{@link #STREAM_DEFAULT}</td></tr>
1729 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001730 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001731
1732 * @param context
1733 * A {@link Context} that will be used by the Builder to construct the
1734 * RemoteViews. The Context will not be held past the lifetime of this Builder
1735 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001736 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001737 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001738 /*
1739 * Important compatibility note!
1740 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1741 * constructor for later use. At this point Activities - themselves subclasses of
1742 * ContextWrapper - do not have their inner Context populated yet. This means that
1743 * any calls to Context methods from within this constructor can cause NPEs in existing
1744 * apps. Any data populated from mContext should therefore be populated lazily to
1745 * preserve compatibility.
1746 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001747 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001748
1749 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001750 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001751 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001752 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001753 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001754
Dan Sandler26e81cf2014-05-06 10:01:27 -04001755 mColorUtil = NotificationColorUtil.getInstance();
Joe Onorato46439ce2010-11-19 13:56:21 -08001756 }
1757
Joe Onoratocb109a02011-01-18 17:57:41 -08001758 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001759 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001760 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001761 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001762 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001763 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001764 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001765 public Builder setWhen(long when) {
1766 mWhen = when;
1767 return this;
1768 }
1769
Joe Onoratocb109a02011-01-18 17:57:41 -08001770 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001771 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001772 * in the content view.
1773 */
1774 public Builder setShowWhen(boolean show) {
1775 mShowWhen = show;
1776 return this;
1777 }
1778
1779 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001780 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001781 *
1782 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001783 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001784 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001785 * Useful when showing an elapsed time (like an ongoing phone call).
1786 *
1787 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001788 * @see Notification#when
1789 */
1790 public Builder setUsesChronometer(boolean b) {
1791 mUseChronometer = b;
1792 return this;
1793 }
1794
1795 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001796 * Set the small icon resource, which will be used to represent the notification in the
1797 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001798 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001799
1800 * The platform template for the expanded view will draw this icon in the left, unless a
1801 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1802 * icon will be moved to the right-hand side.
1803 *
1804
1805 * @param icon
1806 * A resource ID in the application's package of the drawable to use.
1807 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001808 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001809 public Builder setSmallIcon(int icon) {
1810 mSmallIcon = icon;
1811 return this;
1812 }
1813
Joe Onoratocb109a02011-01-18 17:57:41 -08001814 /**
1815 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1816 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1817 * LevelListDrawable}.
1818 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001819 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001820 * @param level The level to use for the icon.
1821 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001822 * @see Notification#icon
1823 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001824 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001825 public Builder setSmallIcon(int icon, int level) {
1826 mSmallIcon = icon;
1827 mSmallIconLevel = level;
1828 return this;
1829 }
1830
Joe Onoratocb109a02011-01-18 17:57:41 -08001831 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001832 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001833 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001834 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001835 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001836 return this;
1837 }
1838
Joe Onoratocb109a02011-01-18 17:57:41 -08001839 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001840 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001841 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001842 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001843 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001844 return this;
1845 }
1846
Joe Onoratocb109a02011-01-18 17:57:41 -08001847 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001848 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001849 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1850 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001851 */
1852 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001853 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001854 return this;
1855 }
1856
1857 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001858 * Set the large number at the right-hand side of the notification. This is
1859 * equivalent to setContentInfo, although it might show the number in a different
1860 * font size for readability.
1861 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001862 public Builder setNumber(int number) {
1863 mNumber = number;
1864 return this;
1865 }
1866
Joe Onoratocb109a02011-01-18 17:57:41 -08001867 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001868 * A small piece of additional information pertaining to this notification.
1869 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001870 * The platform template will draw this on the last line of the notification, at the far
1871 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001872 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001873 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001874 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001875 return this;
1876 }
1877
Joe Onoratocb109a02011-01-18 17:57:41 -08001878 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001879 * Set the progress this notification represents.
1880 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001881 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001882 */
1883 public Builder setProgress(int max, int progress, boolean indeterminate) {
1884 mProgressMax = max;
1885 mProgress = progress;
1886 mProgressIndeterminate = indeterminate;
1887 return this;
1888 }
1889
1890 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001891 * Supply a custom RemoteViews to use instead of the platform template.
1892 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001893 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001894 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001895 public Builder setContent(RemoteViews views) {
1896 mContentView = views;
1897 return this;
1898 }
1899
Joe Onoratocb109a02011-01-18 17:57:41 -08001900 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001901 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1902 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001903 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1904 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1905 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001906 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001907 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001908 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001909 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001910 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001911 public Builder setContentIntent(PendingIntent intent) {
1912 mContentIntent = intent;
1913 return this;
1914 }
1915
Joe Onoratocb109a02011-01-18 17:57:41 -08001916 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001917 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1918 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001919 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001920 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001921 public Builder setDeleteIntent(PendingIntent intent) {
1922 mDeleteIntent = intent;
1923 return this;
1924 }
1925
Joe Onoratocb109a02011-01-18 17:57:41 -08001926 /**
1927 * An intent to launch instead of posting the notification to the status bar.
1928 * Only for use with extremely high-priority notifications demanding the user's
1929 * <strong>immediate</strong> attention, such as an incoming phone call or
1930 * alarm clock that the user has explicitly set to a particular time.
1931 * If this facility is used for something else, please give the user an option
1932 * to turn it off and use a normal notification, as this can be extremely
1933 * disruptive.
1934 *
Chris Wren47c20a12014-06-18 17:27:29 -04001935 * <p>
1936 * The system UI may choose to display a heads-up notification, instead of
1937 * launching this intent, while the user is using the device.
1938 * </p>
1939 *
Joe Onoratocb109a02011-01-18 17:57:41 -08001940 * @param intent The pending intent to launch.
1941 * @param highPriority Passing true will cause this notification to be sent
1942 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001943 *
1944 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001945 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001946 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1947 mFullScreenIntent = intent;
1948 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1949 return this;
1950 }
1951
Joe Onoratocb109a02011-01-18 17:57:41 -08001952 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001953 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001954 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001955 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001956 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001957 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001958 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001959 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001960 return this;
1961 }
1962
Joe Onoratocb109a02011-01-18 17:57:41 -08001963 /**
1964 * Set the text that is displayed in the status bar when the notification first
1965 * arrives, and also a RemoteViews object that may be displayed instead on some
1966 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001967 *
1968 * @see Notification#tickerText
1969 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001970 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001971 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001972 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001973 mTickerView = views;
1974 return this;
1975 }
1976
Joe Onoratocb109a02011-01-18 17:57:41 -08001977 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001978 * Add a large icon to the notification (and the ticker on some devices).
1979 *
1980 * In the platform template, this image will be shown on the left of the notification view
1981 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1982 *
1983 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001984 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001985 public Builder setLargeIcon(Bitmap icon) {
1986 mLargeIcon = icon;
1987 return this;
1988 }
1989
Joe Onoratocb109a02011-01-18 17:57:41 -08001990 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001991 * Set the sound to play.
1992 *
1993 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1994 *
Chris Wren47c20a12014-06-18 17:27:29 -04001995 * <p>
1996 * A notification that is noisy is more likely to be presented as a heads-up notification.
1997 * </p>
1998 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001999 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002000 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002001 public Builder setSound(Uri sound) {
2002 mSound = sound;
2003 mAudioStreamType = STREAM_DEFAULT;
2004 return this;
2005 }
2006
Joe Onoratocb109a02011-01-18 17:57:41 -08002007 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002008 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002009 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002010 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2011 *
Chris Wren47c20a12014-06-18 17:27:29 -04002012 * <p>
2013 * A notification that is noisy is more likely to be presented as a heads-up notification.
2014 * </p>
2015 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002016 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002017 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002018 public Builder setSound(Uri sound, int streamType) {
2019 mSound = sound;
2020 mAudioStreamType = streamType;
2021 return this;
2022 }
2023
Joe Onoratocb109a02011-01-18 17:57:41 -08002024 /**
2025 * Set the vibration pattern to use.
2026 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002027 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2028 * <code>pattern</code> parameter.
2029 *
Chris Wren47c20a12014-06-18 17:27:29 -04002030 * <p>
2031 * A notification that vibrates is more likely to be presented as a heads-up notification.
2032 * </p>
2033 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002034 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002035 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002036 public Builder setVibrate(long[] pattern) {
2037 mVibrate = pattern;
2038 return this;
2039 }
2040
Joe Onoratocb109a02011-01-18 17:57:41 -08002041 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002042 * Set the desired color for the indicator LED on the device, as well as the
2043 * blink duty cycle (specified in milliseconds).
2044 *
2045
2046 * Not all devices will honor all (or even any) of these values.
2047 *
2048
2049 * @see Notification#ledARGB
2050 * @see Notification#ledOnMS
2051 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002052 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002053 public Builder setLights(int argb, int onMs, int offMs) {
2054 mLedArgb = argb;
2055 mLedOnMs = onMs;
2056 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002057 return this;
2058 }
2059
Joe Onoratocb109a02011-01-18 17:57:41 -08002060 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002061 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002062 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002063
2064 * Ongoing notifications cannot be dismissed by the user, so your application or service
2065 * must take care of canceling them.
2066 *
2067
2068 * They are typically used to indicate a background task that the user is actively engaged
2069 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2070 * (e.g., a file download, sync operation, active network connection).
2071 *
2072
2073 * @see Notification#FLAG_ONGOING_EVENT
2074 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002075 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002076 public Builder setOngoing(boolean ongoing) {
2077 setFlag(FLAG_ONGOING_EVENT, ongoing);
2078 return this;
2079 }
2080
Joe Onoratocb109a02011-01-18 17:57:41 -08002081 /**
2082 * Set this flag if you would only like the sound, vibrate
2083 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002084 *
2085 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002086 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002087 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2088 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2089 return this;
2090 }
2091
Joe Onoratocb109a02011-01-18 17:57:41 -08002092 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002093 * Make this notification automatically dismissed when the user touches it. The
2094 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2095 *
2096 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002097 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002098 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002099 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002100 return this;
2101 }
2102
Joe Onoratocb109a02011-01-18 17:57:41 -08002103 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002104 * Set whether or not this notification should not bridge to other devices.
2105 *
2106 * <p>Some notifications can be bridged to other devices for remote display.
2107 * This hint can be set to recommend this notification not be bridged.
2108 */
2109 public Builder setLocalOnly(boolean localOnly) {
2110 setFlag(FLAG_LOCAL_ONLY, localOnly);
2111 return this;
2112 }
2113
2114 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002115 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002116 * <p>
2117 * The value should be one or more of the following fields combined with
2118 * bitwise-or:
2119 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2120 * <p>
2121 * For all default values, use {@link #DEFAULT_ALL}.
2122 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002123 public Builder setDefaults(int defaults) {
2124 mDefaults = defaults;
2125 return this;
2126 }
2127
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002128 /**
2129 * Set the priority of this notification.
2130 *
2131 * @see Notification#priority
2132 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002133 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002134 mPriority = pri;
2135 return this;
2136 }
Joe Malin8d40d042012-11-05 11:36:40 -08002137
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002138 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002139 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002140 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002141 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002143 public Builder setCategory(String category) {
2144 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002145 return this;
2146 }
2147
2148 /**
Chris Wrendde75302014-03-26 17:24:15 -04002149 * Add a person that is relevant to this notification.
2150 *
2151 * @see Notification#EXTRA_PEOPLE
2152 */
2153 public Builder addPerson(String handle) {
2154 mPeople.add(handle);
2155 return this;
2156 }
2157
2158 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002159 * Set this notification to be part of a group of notifications sharing the same key.
2160 * Grouped notifications may display in a cluster or stack on devices which
2161 * support such rendering.
2162 *
2163 * <p>To make this notification the summary for its group, also call
2164 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2165 * {@link #setSortKey}.
2166 * @param groupKey The group key of the group.
2167 * @return this object for method chaining
2168 */
2169 public Builder setGroup(String groupKey) {
2170 mGroupKey = groupKey;
2171 return this;
2172 }
2173
2174 /**
2175 * Set this notification to be the group summary for a group of notifications.
2176 * Grouped notifications may display in a cluster or stack on devices which
2177 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2178 * @param isGroupSummary Whether this notification should be a group summary.
2179 * @return this object for method chaining
2180 */
2181 public Builder setGroupSummary(boolean isGroupSummary) {
2182 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2183 return this;
2184 }
2185
2186 /**
2187 * Set a sort key that orders this notification among other notifications from the
2188 * same package. This can be useful if an external sort was already applied and an app
2189 * would like to preserve this. Notifications will be sorted lexicographically using this
2190 * value, although providing different priorities in addition to providing sort key may
2191 * cause this value to be ignored.
2192 *
2193 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002194 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002195 *
2196 * @see String#compareTo(String)
2197 */
2198 public Builder setSortKey(String sortKey) {
2199 mSortKey = sortKey;
2200 return this;
2201 }
2202
2203 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002204 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002205 *
Griff Hazen720042b2014-02-24 15:46:56 -08002206 * <p>Values within the Bundle will replace existing extras values in this Builder.
2207 *
2208 * @see Notification#extras
2209 */
Griff Hazen959591e2014-05-15 22:26:18 -07002210 public Builder addExtras(Bundle extras) {
2211 if (extras != null) {
2212 if (mExtras == null) {
2213 mExtras = new Bundle(extras);
2214 } else {
2215 mExtras.putAll(extras);
2216 }
Griff Hazen720042b2014-02-24 15:46:56 -08002217 }
2218 return this;
2219 }
2220
2221 /**
2222 * Set metadata for this notification.
2223 *
2224 * <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 -04002225 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002226 * called.
2227 *
Griff Hazen720042b2014-02-24 15:46:56 -08002228 * <p>Replaces any existing extras values with those from the provided Bundle.
2229 * Use {@link #addExtras} to merge in metadata instead.
2230 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002231 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002232 */
Griff Hazen959591e2014-05-15 22:26:18 -07002233 public Builder setExtras(Bundle extras) {
2234 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002235 return this;
2236 }
2237
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002238 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002239 * Get the current metadata Bundle used by this notification Builder.
2240 *
2241 * <p>The returned Bundle is shared with this Builder.
2242 *
2243 * <p>The current contents of this Bundle are copied into the Notification each time
2244 * {@link #build()} is called.
2245 *
2246 * @see Notification#extras
2247 */
2248 public Bundle getExtras() {
2249 if (mExtras == null) {
2250 mExtras = new Bundle();
2251 }
2252 return mExtras;
2253 }
2254
2255 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002256 * Add an action to this notification. Actions are typically displayed by
2257 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002258 * <p>
2259 * Every action must have an icon (32dp square and matching the
2260 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2261 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2262 * <p>
2263 * A notification in its expanded form can display up to 3 actions, from left to right in
2264 * the order they were added. Actions will not be displayed when the notification is
2265 * collapsed, however, so be sure that any essential functions may be accessed by the user
2266 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002267 *
2268 * @param icon Resource ID of a drawable that represents the action.
2269 * @param title Text describing the action.
2270 * @param intent PendingIntent to be fired when the action is invoked.
2271 */
2272 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002273 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002274 return this;
2275 }
2276
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002277 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002278 * Add an action to this notification. Actions are typically displayed by
2279 * the system as a button adjacent to the notification content.
2280 * <p>
2281 * Every action must have an icon (32dp square and matching the
2282 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2283 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2284 * <p>
2285 * A notification in its expanded form can display up to 3 actions, from left to right in
2286 * the order they were added. Actions will not be displayed when the notification is
2287 * collapsed, however, so be sure that any essential functions may be accessed by the user
2288 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2289 *
2290 * @param action The action to add.
2291 */
2292 public Builder addAction(Action action) {
2293 mActions.add(action);
2294 return this;
2295 }
2296
2297 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002298 * Add a rich notification style to be applied at build time.
2299 *
2300 * @param style Object responsible for modifying the notification style.
2301 */
2302 public Builder setStyle(Style style) {
2303 if (mStyle != style) {
2304 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002305 if (mStyle != null) {
2306 mStyle.setBuilder(this);
2307 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002308 }
2309 return this;
2310 }
2311
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002312 /**
2313 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002314 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002315 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2316 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2317 *
2318 * @return The same Builder.
2319 */
2320 public Builder setVisibility(int visibility) {
2321 mVisibility = visibility;
2322 return this;
2323 }
2324
2325 /**
2326 * Supply a replacement Notification whose contents should be shown in insecure contexts
2327 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2328 * @param n A replacement notification, presumably with some or all info redacted.
2329 * @return The same Builder.
2330 */
2331 public Builder setPublicVersion(Notification n) {
2332 mPublicVersion = n;
2333 return this;
2334 }
2335
Griff Hazenb720abe2014-05-20 13:15:30 -07002336 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002337 * Apply an extender to this notification builder. Extenders may be used to add
2338 * metadata or change options on this builder.
2339 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002340 public Builder extend(Extender extender) {
2341 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002342 return this;
2343 }
2344
Joe Onorato46439ce2010-11-19 13:56:21 -08002345 private void setFlag(int mask, boolean value) {
2346 if (value) {
2347 mFlags |= mask;
2348 } else {
2349 mFlags &= ~mask;
2350 }
2351 }
2352
Dan Sandler26e81cf2014-05-06 10:01:27 -04002353 /**
2354 * Sets {@link Notification#color}.
2355 *
2356 * @param argb The accent color to use
2357 *
2358 * @return The same Builder.
2359 */
2360 public Builder setColor(int argb) {
2361 mColor = argb;
2362 return this;
2363 }
2364
Kenny Guy8a0101b2014-05-08 23:34:12 +01002365 private Bitmap getProfileBadge() {
2366 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
2367 Drawable badge = userManager.getBadgeForUser(android.os.Process.myUserHandle());
2368 if (badge == null) {
2369 return null;
2370 }
2371 final int width = badge.getIntrinsicWidth();
2372 final int height = badge.getIntrinsicHeight();
2373 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2374 Canvas canvas = new Canvas(bitmap);
2375 badge.setBounds(0, 0, width, height);
2376 badge.draw(canvas);
2377 return bitmap;
2378 }
2379
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002380 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Kenny Guy8a0101b2014-05-08 23:34:12 +01002381 Bitmap profileIcon = getProfileBadge();
Joe Onorato561d3852010-11-20 18:09:34 -08002382 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002383 boolean showLine3 = false;
2384 boolean showLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002385
Dan Sandler26e81cf2014-05-06 10:01:27 -04002386 if (mPriority < PRIORITY_LOW) {
2387 // TODO: Low priority presentation
Daniel Sandlere95658c2012-05-10 00:33:54 -04002388 }
Kenny Guy8a0101b2014-05-08 23:34:12 +01002389 if (profileIcon != null) {
2390 contentView.setImageViewBitmap(R.id.profile_icon, profileIcon);
2391 contentView.setViewVisibility(R.id.profile_icon, View.VISIBLE);
2392 } else {
2393 contentView.setViewVisibility(R.id.profile_icon, View.GONE);
2394 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002395 if (mLargeIcon != null) {
2396 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002397 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002398 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2399 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2400 processSmallRightIcon(mSmallIcon, contentView);
2401 } else { // small icon at left
2402 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2403 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2404 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002405 }
2406 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002407 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002408 }
2409 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002410 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002411 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002412 }
2413 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002414 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002415 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002416 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002417 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002418 final int tooBig = mContext.getResources().getInteger(
2419 R.integer.status_bar_notification_info_maxnum);
2420 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002421 contentView.setTextViewText(R.id.info, processLegacyText(
2422 mContext.getResources().getString(
2423 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002424 } else {
2425 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002426 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002427 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002428 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002429 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002430 } else {
2431 contentView.setViewVisibility(R.id.info, View.GONE);
2432 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002433
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002434 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002435 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002436 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002437 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002438 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002439 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2440 showLine2 = true;
2441 } else {
2442 contentView.setViewVisibility(R.id.text2, View.GONE);
2443 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002444 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002445 contentView.setViewVisibility(R.id.text2, View.GONE);
2446 if (mProgressMax != 0 || mProgressIndeterminate) {
2447 contentView.setProgressBar(
2448 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2449 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002450 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002451 } else {
2452 contentView.setViewVisibility(R.id.progress, View.GONE);
2453 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002454 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002455 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002456 if (fitIn1U) {
2457 // need to shrink all the type to make sure everything fits
2458 final Resources res = mContext.getResources();
2459 final float subTextSize = res.getDimensionPixelSize(
2460 R.dimen.notification_subtext_size);
2461 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2462 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002463 // vertical centering
2464 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2465 }
2466
Daniel Sandler0c890492012-09-12 17:23:10 -07002467 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002468 if (mUseChronometer) {
2469 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2470 contentView.setLong(R.id.chronometer, "setBase",
2471 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2472 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2473 } else {
2474 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2475 contentView.setLong(R.id.time, "setTime", mWhen);
2476 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002477 } else {
2478 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002479 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002480
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002481 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002482 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002483 return contentView;
2484 }
2485
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002486 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002487 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002488
2489 int N = mActions.size();
2490 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002491 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002492 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002493 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002494 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002495 for (int i=0; i<N; i++) {
2496 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002497 big.addView(R.id.actions, button);
2498 }
2499 }
2500 return big;
2501 }
2502
Joe Onorato46439ce2010-11-19 13:56:21 -08002503 private RemoteViews makeContentView() {
2504 if (mContentView != null) {
2505 return mContentView;
2506 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002507 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002508 }
2509 }
2510
2511 private RemoteViews makeTickerView() {
2512 if (mTickerView != null) {
2513 return mTickerView;
2514 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08002515 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002516 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08002517 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002518 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08002519 } else {
2520 return null;
2521 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002522 }
2523 }
2524
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002525 private RemoteViews makeBigContentView() {
2526 if (mActions.size() == 0) return null;
2527
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002528 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002529 }
2530
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002531 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002532 if (mActions.size() == 0) return null;
2533
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002534 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002535 }
2536
2537
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002538 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002539 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002540 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002541 tombstone ? getActionTombstoneLayoutResource()
2542 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002543 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002544 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002545 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002546 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002547 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002548 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002549 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002550 return button;
2551 }
2552
Joe Onoratocb109a02011-01-18 17:57:41 -08002553 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002554 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002555 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002556 */
2557 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002558 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002559 }
2560
2561 private void processLegacyAction(Action action, RemoteViews button) {
2562 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002563 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002564 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2565 mContext.getResources().getColor(
2566 R.color.notification_action_legacy_color_filter),
2567 PorterDuff.Mode.MULTIPLY);
2568 }
2569 }
2570 }
2571
2572 private CharSequence processLegacyText(CharSequence charSequence) {
2573 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002574 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002575 } else {
2576 return charSequence;
2577 }
2578 }
2579
Dan Sandler26e81cf2014-05-06 10:01:27 -04002580 /**
2581 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2582 */
2583 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2584 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2585 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002586 }
2587 }
2588
Dan Sandler26e81cf2014-05-06 10:01:27 -04002589 /**
2590 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2591 * if it's grayscale).
2592 */
2593 // TODO: also check bounds, transparency, that sort of thing.
2594 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2595 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2596 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002597 } else {
2598 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002599 }
2600 }
2601
Dan Sandler26e81cf2014-05-06 10:01:27 -04002602 /**
2603 * Add a colored circle behind the largeIcon slot.
2604 */
2605 private void applyLargeIconBackground(RemoteViews contentView) {
2606 contentView.setInt(R.id.icon, "setBackgroundResource",
2607 R.drawable.notification_icon_legacy_bg_inset);
2608
2609 contentView.setDrawableParameters(
2610 R.id.icon,
2611 true,
2612 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002613 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002614 PorterDuff.Mode.SRC_ATOP,
2615 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002616 }
2617
Dan Sandler190d58d2014-05-15 09:33:39 -04002618 private void removeLargeIconBackground(RemoteViews contentView) {
2619 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2620 }
2621
Dan Sandler26e81cf2014-05-06 10:01:27 -04002622 /**
2623 * Recolor small icons when used in the R.id.right_icon slot.
2624 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002625 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002626 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002627 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002628 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2629 0xFFFFFFFF,
2630 PorterDuff.Mode.SRC_ATOP, -1);
2631
2632 contentView.setInt(R.id.right_icon,
2633 "setBackgroundResource",
2634 R.drawable.notification_icon_legacy_bg);
2635
2636 contentView.setDrawableParameters(
2637 R.id.right_icon,
2638 true,
2639 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002640 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04002641 PorterDuff.Mode.SRC_ATOP,
2642 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002643 }
2644 }
2645
Jorim Jaggi74419312014-06-10 20:57:21 +02002646 private int sanitizeColor() {
2647 if (mColor != COLOR_DEFAULT) {
2648 mColor |= 0xFF000000; // no alpha for custom colors
2649 }
2650 return mColor;
2651 }
2652
Dan Sandler26e81cf2014-05-06 10:01:27 -04002653 private int resolveColor() {
2654 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02002655 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002656 }
2657 return mColor;
2658 }
2659
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002660 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002661 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002662 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002663 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002664 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002665 Notification n = new Notification();
2666 n.when = mWhen;
2667 n.icon = mSmallIcon;
2668 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002669 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002670
Jorim Jaggi74419312014-06-10 20:57:21 +02002671 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002672
Joe Onorato46439ce2010-11-19 13:56:21 -08002673 n.contentView = makeContentView();
2674 n.contentIntent = mContentIntent;
2675 n.deleteIntent = mDeleteIntent;
2676 n.fullScreenIntent = mFullScreenIntent;
2677 n.tickerText = mTickerText;
2678 n.tickerView = makeTickerView();
2679 n.largeIcon = mLargeIcon;
2680 n.sound = mSound;
2681 n.audioStreamType = mAudioStreamType;
2682 n.vibrate = mVibrate;
2683 n.ledARGB = mLedArgb;
2684 n.ledOnMS = mLedOnMs;
2685 n.ledOffMS = mLedOffMs;
2686 n.defaults = mDefaults;
2687 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002688 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002689 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002690 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002691 n.flags |= FLAG_SHOW_LIGHTS;
2692 }
2693 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2694 n.flags |= FLAG_SHOW_LIGHTS;
2695 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002696 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002697 n.mGroupKey = mGroupKey;
2698 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002699 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002700 if (mActions.size() > 0) {
2701 n.actions = new Action[mActions.size()];
2702 mActions.toArray(n.actions);
2703 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002704 n.visibility = mVisibility;
2705
2706 if (mPublicVersion != null) {
2707 n.publicVersion = new Notification();
2708 mPublicVersion.cloneInto(n.publicVersion, true);
2709 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002710
Joe Onorato46439ce2010-11-19 13:56:21 -08002711 return n;
2712 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002713
2714 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002715 * Capture, in the provided bundle, semantic information used in the construction of
2716 * this Notification object.
2717 * @hide
2718 */
Griff Hazen720042b2014-02-24 15:46:56 -08002719 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002720 // Store original information used in the construction of this object
2721 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2722 extras.putCharSequence(EXTRA_TEXT, mContentText);
2723 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2724 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2725 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2726 extras.putInt(EXTRA_PROGRESS, mProgress);
2727 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2728 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2729 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2730 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002731 if (mLargeIcon != null) {
2732 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2733 }
Chris Wrendde75302014-03-26 17:24:15 -04002734 if (!mPeople.isEmpty()) {
2735 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2736 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002737 }
2738
2739 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002740 * @deprecated Use {@link #build()} instead.
2741 */
2742 @Deprecated
2743 public Notification getNotification() {
2744 return build();
2745 }
2746
2747 /**
2748 * Combine all of the options that have been set and return a new {@link Notification}
2749 * object.
2750 */
2751 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002752 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002753
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002754 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002755 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002756 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002757
2758 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2759
Griff Hazen720042b2014-02-24 15:46:56 -08002760 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002761 if (mStyle != null) {
2762 mStyle.addExtras(n.extras);
2763 }
2764
2765 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002766 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002767
2768 /**
2769 * Apply this Builder to an existing {@link Notification} object.
2770 *
2771 * @hide
2772 */
2773 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002774 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002775 return n;
2776 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002777
2778
2779 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002780 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002781 }
2782
2783 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002784 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002785 }
2786
2787 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002788 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002789 }
2790
2791 private int getBigTextLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002792 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002793 }
2794
2795 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002796 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002797 }
2798
2799 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002800 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002801 }
2802
2803 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002804 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002805 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002806 }
2807
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002808 /**
2809 * An object that can apply a rich notification style to a {@link Notification.Builder}
2810 * object.
2811 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002812 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002813 private CharSequence mBigContentTitle;
2814 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002815 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002816
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002817 protected Builder mBuilder;
2818
Chris Wrend6297db2012-05-03 16:20:13 -04002819 /**
2820 * Overrides ContentTitle in the big form of the template.
2821 * This defaults to the value passed to setContentTitle().
2822 */
2823 protected void internalSetBigContentTitle(CharSequence title) {
2824 mBigContentTitle = title;
2825 }
2826
2827 /**
2828 * Set the first line of text after the detail section in the big form of the template.
2829 */
2830 protected void internalSetSummaryText(CharSequence cs) {
2831 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002832 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002833 }
2834
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002835 public void setBuilder(Builder builder) {
2836 if (mBuilder != builder) {
2837 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002838 if (mBuilder != null) {
2839 mBuilder.setStyle(this);
2840 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002841 }
2842 }
2843
Chris Wrend6297db2012-05-03 16:20:13 -04002844 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002845 if (mBuilder == null) {
2846 throw new IllegalArgumentException("Style requires a valid Builder object");
2847 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002848 }
Chris Wrend6297db2012-05-03 16:20:13 -04002849
2850 protected RemoteViews getStandardView(int layoutId) {
2851 checkBuilder();
2852
2853 if (mBigContentTitle != null) {
2854 mBuilder.setContentTitle(mBigContentTitle);
2855 }
2856
Chris Wrend6297db2012-05-03 16:20:13 -04002857 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2858
Chris Wrend6297db2012-05-03 16:20:13 -04002859 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2860 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002861 } else {
2862 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002863 }
2864
Daniel Sandler619738c2012-06-07 16:33:08 -04002865 // The last line defaults to the subtext, but can be replaced by mSummaryText
2866 final CharSequence overflowText =
2867 mSummaryTextSet ? mSummaryText
2868 : mBuilder.mSubText;
2869 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002870 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04002871 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002872 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002873 } else {
2874 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2875 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002876 }
2877
2878 return contentView;
2879 }
2880
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002881 /**
2882 * @hide
2883 */
2884 public void addExtras(Bundle extras) {
2885 if (mSummaryTextSet) {
2886 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2887 }
2888 if (mBigContentTitle != null) {
2889 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2890 }
Chris Wren91ad5632013-06-05 15:05:57 -04002891 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002892 }
2893
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002894 /**
2895 * @hide
2896 */
2897 public abstract Notification buildStyled(Notification wip);
2898
2899 /**
2900 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2901 * attached to.
2902 *
2903 * @return the fully constructed Notification.
2904 */
2905 public Notification build() {
2906 checkBuilder();
2907 return mBuilder.build();
2908 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002909 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002910
2911 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002912 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002913 *
Robert Ly91c5ce32014-06-08 15:37:00 -07002914 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002915 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07002916 * Notification notif = new Notification.Builder(mContext)
2917 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
2918 * .setContentText(subject)
2919 * .setSmallIcon(R.drawable.new_post)
2920 * .setLargeIcon(aBitmap)
2921 * .setStyle(new Notification.BigPictureStyle()
2922 * .bigPicture(aBigBitmap))
2923 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002924 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002925 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002926 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002927 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002928 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002929 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002930 private Bitmap mBigLargeIcon;
2931 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002932
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002933 public BigPictureStyle() {
2934 }
2935
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002936 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002937 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002938 }
2939
Chris Wrend6297db2012-05-03 16:20:13 -04002940 /**
2941 * Overrides ContentTitle in the big form of the template.
2942 * This defaults to the value passed to setContentTitle().
2943 */
2944 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002945 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002946 return this;
2947 }
2948
2949 /**
2950 * Set the first line of text after the detail section in the big form of the template.
2951 */
2952 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002953 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002954 return this;
2955 }
2956
Chris Wren0bd664d2012-08-01 13:56:56 -04002957 /**
2958 * Provide the bitmap to be used as the payload for the BigPicture notification.
2959 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002960 public BigPictureStyle bigPicture(Bitmap b) {
2961 mPicture = b;
2962 return this;
2963 }
2964
Chris Wren3745a3d2012-05-22 15:11:52 -04002965 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002966 * Override the large icon when the big notification is shown.
2967 */
2968 public BigPictureStyle bigLargeIcon(Bitmap b) {
2969 mBigLargeIconSet = true;
2970 mBigLargeIcon = b;
2971 return this;
2972 }
2973
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002974 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002975 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002976
2977 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2978
2979 return contentView;
2980 }
2981
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002982 /**
2983 * @hide
2984 */
2985 public void addExtras(Bundle extras) {
2986 super.addExtras(extras);
2987
2988 if (mBigLargeIconSet) {
2989 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2990 }
2991 extras.putParcelable(EXTRA_PICTURE, mPicture);
2992 }
2993
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002994 /**
2995 * @hide
2996 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002997 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002998 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002999 if (mBigLargeIconSet ) {
3000 mBuilder.mLargeIcon = mBigLargeIcon;
3001 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003002 wip.bigContentView = makeBigContentView();
3003 return wip;
3004 }
3005 }
3006
3007 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003008 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003009 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003010 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003011 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003012 * Notification notif = new Notification.Builder(mContext)
3013 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3014 * .setContentText(subject)
3015 * .setSmallIcon(R.drawable.new_mail)
3016 * .setLargeIcon(aBitmap)
3017 * .setStyle(new Notification.BigTextStyle()
3018 * .bigText(aVeryLongString))
3019 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003020 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003021 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003022 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003023 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003024 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003025 private CharSequence mBigText;
3026
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003027 public BigTextStyle() {
3028 }
3029
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003030 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003031 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003032 }
3033
Chris Wrend6297db2012-05-03 16:20:13 -04003034 /**
3035 * Overrides ContentTitle in the big form of the template.
3036 * This defaults to the value passed to setContentTitle().
3037 */
3038 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003039 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003040 return this;
3041 }
3042
3043 /**
3044 * Set the first line of text after the detail section in the big form of the template.
3045 */
3046 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003047 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003048 return this;
3049 }
3050
Chris Wren0bd664d2012-08-01 13:56:56 -04003051 /**
3052 * Provide the longer text to be displayed in the big form of the
3053 * template in place of the content text.
3054 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003055 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003056 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003057 return this;
3058 }
3059
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003060 /**
3061 * @hide
3062 */
3063 public void addExtras(Bundle extras) {
3064 super.addExtras(extras);
3065
3066 extras.putCharSequence(EXTRA_TEXT, mBigText);
3067 }
3068
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003069 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003070 // Remove the content text so line3 only shows if you have a summary
3071 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003072 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003073
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003074 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003075
Daniel Sandler619738c2012-06-07 16:33:08 -04003076 if (hadThreeLines) {
3077 // vertical centering
3078 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
3079 }
3080
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003081 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003082 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003083 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003084
3085 return contentView;
3086 }
3087
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003088 /**
3089 * @hide
3090 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003091 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003092 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003093 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003094
3095 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
3096
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003097 return wip;
3098 }
3099 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003100
3101 /**
3102 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003103 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003104 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003105 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003106 * Notification notif = new Notification.Builder(mContext)
3107 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3108 * .setContentText(subject)
3109 * .setSmallIcon(R.drawable.new_mail)
3110 * .setLargeIcon(aBitmap)
3111 * .setStyle(new Notification.InboxStyle()
3112 * .addLine(str1)
3113 * .addLine(str2)
3114 * .setContentTitle(&quot;&quot;)
3115 * .setSummaryText(&quot;+3 more&quot;))
3116 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003117 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003118 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003119 * @see Notification#bigContentView
3120 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003121 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003122 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3123
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003124 public InboxStyle() {
3125 }
3126
Daniel Sandler879c5e02012-04-17 16:46:51 -04003127 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003128 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003129 }
3130
Chris Wrend6297db2012-05-03 16:20:13 -04003131 /**
3132 * Overrides ContentTitle in the big form of the template.
3133 * This defaults to the value passed to setContentTitle().
3134 */
3135 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003136 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003137 return this;
3138 }
3139
3140 /**
3141 * Set the first line of text after the detail section in the big form of the template.
3142 */
3143 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003144 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003145 return this;
3146 }
3147
Chris Wren0bd664d2012-08-01 13:56:56 -04003148 /**
3149 * Append a line to the digest section of the Inbox notification.
3150 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003151 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003152 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003153 return this;
3154 }
3155
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003156 /**
3157 * @hide
3158 */
3159 public void addExtras(Bundle extras) {
3160 super.addExtras(extras);
3161 CharSequence[] a = new CharSequence[mTexts.size()];
3162 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3163 }
3164
Daniel Sandler879c5e02012-04-17 16:46:51 -04003165 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003166 // Remove the content text so line3 disappears unless you have a summary
3167 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003168 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003169
Chris Wrend6297db2012-05-03 16:20:13 -04003170 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003171
Chris Wrend6297db2012-05-03 16:20:13 -04003172 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 -04003173 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003174
Chris Wren4ed80d52012-05-17 09:30:03 -04003175 // Make sure all rows are gone in case we reuse a view.
3176 for (int rowId : rowIds) {
3177 contentView.setViewVisibility(rowId, View.GONE);
3178 }
3179
Chris Wren683ab002012-09-20 10:35:54 -04003180
Daniel Sandler879c5e02012-04-17 16:46:51 -04003181 int i=0;
3182 while (i < mTexts.size() && i < rowIds.length) {
3183 CharSequence str = mTexts.get(i);
3184 if (str != null && !str.equals("")) {
3185 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003186 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003187 }
3188 i++;
3189 }
3190
Chris Wren683ab002012-09-20 10:35:54 -04003191 contentView.setViewVisibility(R.id.inbox_end_pad,
3192 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3193
3194 contentView.setViewVisibility(R.id.inbox_more,
3195 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003196
Daniel Sandler879c5e02012-04-17 16:46:51 -04003197 return contentView;
3198 }
3199
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003200 /**
3201 * @hide
3202 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003203 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003204 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003205 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003206
Daniel Sandler879c5e02012-04-17 16:46:51 -04003207 return wip;
3208 }
3209 }
Dan Sandler842dd772014-05-15 09:36:47 -04003210
3211 /**
3212 * Notification style for media playback notifications.
3213 *
3214 * In the expanded form, {@link Notification#bigContentView}, up to 5
3215 * {@link Notification.Action}s specified with
3216 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3217 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3218 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3219 * treated as album artwork.
3220 *
3221 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3222 * {@link Notification#contentView}; by providing action indices to
3223 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3224 * in the standard view alongside the usual content.
3225 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003226 * Notifications created with MediaStyle will have their category set to
3227 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
3228 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
3229 *
Jeff Browndba34ba2014-06-24 20:46:03 -07003230 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
3231 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04003232 * the System UI can identify this as a notification representing an active media session
3233 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3234 *
3235 * To use this style with your Notification, feed it to
3236 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3237 * <pre class="prettyprint">
3238 * Notification noti = new Notification.Builder()
3239 * .setSmallIcon(R.drawable.ic_stat_player)
3240 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3241 * .setContentText(&quot;Artist - Album&quot;) // if you use
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003242 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token)
3243 * .setStyle(<b>new Notification.MediaStyle()</b>
3244 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04003245 * .build();
3246 * </pre>
3247 *
3248 * @see Notification#bigContentView
3249 */
3250 public static class MediaStyle extends Style {
3251 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3252 static final int MAX_MEDIA_BUTTONS = 5;
3253
3254 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07003255 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04003256
3257 public MediaStyle() {
3258 }
3259
3260 public MediaStyle(Builder builder) {
3261 setBuilder(builder);
3262 }
3263
3264 /**
3265 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3266 * notification view.
3267 */
3268 public MediaStyle setShowActionsInCompactView(int...actions) {
3269 mActionsToShowInCompact = actions;
3270 return this;
3271 }
3272
3273 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07003274 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
3275 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04003276 */
Jeff Browndba34ba2014-06-24 20:46:03 -07003277 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04003278 mToken = token;
3279 return this;
3280 }
3281
3282 @Override
3283 public Notification buildStyled(Notification wip) {
3284 wip.contentView = makeMediaContentView();
3285 wip.bigContentView = makeMediaBigContentView();
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003286 if (wip.category == null) {
3287 wip.category = Notification.CATEGORY_TRANSPORT;
3288 }
Dan Sandler842dd772014-05-15 09:36:47 -04003289 return wip;
3290 }
3291
3292 /** @hide */
3293 @Override
3294 public void addExtras(Bundle extras) {
3295 super.addExtras(extras);
3296
3297 if (mToken != null) {
3298 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3299 }
3300 }
3301
3302 private RemoteViews generateMediaActionButton(Action action) {
3303 final boolean tombstone = (action.actionIntent == null);
3304 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07003305 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04003306 button.setImageViewResource(R.id.action0, action.icon);
3307 if (!tombstone) {
3308 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3309 }
3310 button.setContentDescription(R.id.action0, action.title);
3311 return button;
3312 }
3313
3314 private RemoteViews makeMediaContentView() {
3315 RemoteViews view = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003316 R.layout.notification_template_material_media, true /* 1U */);
Dan Sandler842dd772014-05-15 09:36:47 -04003317
3318 final int numActions = mBuilder.mActions.size();
3319 final int N = mActionsToShowInCompact == null
3320 ? 0
3321 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3322 if (N > 0) {
3323 view.removeAllViews(R.id.actions);
3324 for (int i = 0; i < N; i++) {
3325 if (i >= numActions) {
3326 throw new IllegalArgumentException(String.format(
3327 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3328 i, numActions - 1));
3329 }
3330
3331 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3332 final RemoteViews button = generateMediaActionButton(action);
3333 view.addView(R.id.actions, button);
3334 }
3335 }
3336 return view;
3337 }
3338
3339 private RemoteViews makeMediaBigContentView() {
3340 RemoteViews big = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003341 R.layout.notification_template_material_big_media, false);
Dan Sandler842dd772014-05-15 09:36:47 -04003342
3343 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3344 if (N > 0) {
3345 big.removeAllViews(R.id.actions);
3346 for (int i=0; i<N; i++) {
3347 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3348 big.addView(R.id.actions, button);
3349 }
3350 }
3351 return big;
3352 }
3353 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003354
3355 /**
3356 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
3357 * metadata or change options on a notification builder.
3358 */
3359 public interface Extender {
3360 /**
3361 * Apply this extender to a notification builder.
3362 * @param builder the builder to be modified.
3363 * @return the build object for chaining.
3364 */
3365 public Builder extend(Builder builder);
3366 }
3367
3368 /**
3369 * Helper class to add wearable extensions to notifications.
3370 * <p class="note"> See
3371 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
3372 * for Android Wear</a> for more information on how to use this class.
3373 * <p>
3374 * To create a notification with wearable extensions:
3375 * <ol>
3376 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
3377 * properties.
3378 * <li>Create a {@link android.app.Notification.WearableExtender}.
3379 * <li>Set wearable-specific properties using the
3380 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
3381 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
3382 * notification.
3383 * <li>Post the notification to the notification system with the
3384 * {@code NotificationManager.notify(...)} methods.
3385 * </ol>
3386 *
3387 * <pre class="prettyprint">
3388 * Notification notif = new Notification.Builder(mContext)
3389 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3390 * .setContentText(subject)
3391 * .setSmallIcon(R.drawable.new_mail)
3392 * .extend(new Notification.WearableExtender()
3393 * .setContentIcon(R.drawable.new_mail))
3394 * .build();
3395 * NotificationManager notificationManger =
3396 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3397 * notificationManger.notify(0, notif);</pre>
3398 *
3399 * <p>Wearable extensions can be accessed on an existing notification by using the
3400 * {@code WearableExtender(Notification)} constructor,
3401 * and then using the {@code get} methods to access values.
3402 *
3403 * <pre class="prettyprint">
3404 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
3405 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07003406 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003407 */
3408 public static final class WearableExtender implements Extender {
3409 /**
3410 * Sentinel value for an action index that is unset.
3411 */
3412 public static final int UNSET_ACTION_INDEX = -1;
3413
3414 /**
3415 * Size value for use with {@link #setCustomSizePreset} to show this notification with
3416 * default sizing.
3417 * <p>For custom display notifications created using {@link #setDisplayIntent},
3418 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
3419 * on their content.
3420 */
3421 public static final int SIZE_DEFAULT = 0;
3422
3423 /**
3424 * Size value for use with {@link #setCustomSizePreset} to show this notification
3425 * with an extra small size.
3426 * <p>This value is only applicable for custom display notifications created using
3427 * {@link #setDisplayIntent}.
3428 */
3429 public static final int SIZE_XSMALL = 1;
3430
3431 /**
3432 * Size value for use with {@link #setCustomSizePreset} to show this notification
3433 * with a small size.
3434 * <p>This value is only applicable for custom display notifications created using
3435 * {@link #setDisplayIntent}.
3436 */
3437 public static final int SIZE_SMALL = 2;
3438
3439 /**
3440 * Size value for use with {@link #setCustomSizePreset} to show this notification
3441 * with a medium size.
3442 * <p>This value is only applicable for custom display notifications created using
3443 * {@link #setDisplayIntent}.
3444 */
3445 public static final int SIZE_MEDIUM = 3;
3446
3447 /**
3448 * Size value for use with {@link #setCustomSizePreset} to show this notification
3449 * with a large size.
3450 * <p>This value is only applicable for custom display notifications created using
3451 * {@link #setDisplayIntent}.
3452 */
3453 public static final int SIZE_LARGE = 4;
3454
Griff Hazend5f11f92014-05-27 15:40:09 -07003455 /**
3456 * Size value for use with {@link #setCustomSizePreset} to show this notification
3457 * full screen.
3458 * <p>This value is only applicable for custom display notifications created using
3459 * {@link #setDisplayIntent}.
3460 */
3461 public static final int SIZE_FULL_SCREEN = 5;
3462
Griff Hazen61a9e862014-05-22 16:05:19 -07003463 /** Notification extra which contains wearable extensions */
3464 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
3465
3466 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
3467 private static final String KEY_ACTIONS = "actions";
3468 private static final String KEY_FLAGS = "flags";
3469 private static final String KEY_DISPLAY_INTENT = "displayIntent";
3470 private static final String KEY_PAGES = "pages";
3471 private static final String KEY_BACKGROUND = "background";
3472 private static final String KEY_CONTENT_ICON = "contentIcon";
3473 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
3474 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
3475 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
3476 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
3477 private static final String KEY_GRAVITY = "gravity";
3478
3479 // Flags bitwise-ored to mFlags
3480 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
3481 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
3482 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
3483 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
3484
3485 // Default value for flags integer
3486 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
3487
3488 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
3489 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
3490
3491 private ArrayList<Action> mActions = new ArrayList<Action>();
3492 private int mFlags = DEFAULT_FLAGS;
3493 private PendingIntent mDisplayIntent;
3494 private ArrayList<Notification> mPages = new ArrayList<Notification>();
3495 private Bitmap mBackground;
3496 private int mContentIcon;
3497 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
3498 private int mContentActionIndex = UNSET_ACTION_INDEX;
3499 private int mCustomSizePreset = SIZE_DEFAULT;
3500 private int mCustomContentHeight;
3501 private int mGravity = DEFAULT_GRAVITY;
3502
3503 /**
3504 * Create a {@link android.app.Notification.WearableExtender} with default
3505 * options.
3506 */
3507 public WearableExtender() {
3508 }
3509
3510 public WearableExtender(Notification notif) {
3511 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
3512 if (wearableBundle != null) {
3513 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
3514 if (actions != null) {
3515 mActions.addAll(actions);
3516 }
3517
3518 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
3519 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
3520
3521 Notification[] pages = getNotificationArrayFromBundle(
3522 wearableBundle, KEY_PAGES);
3523 if (pages != null) {
3524 Collections.addAll(mPages, pages);
3525 }
3526
3527 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
3528 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
3529 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
3530 DEFAULT_CONTENT_ICON_GRAVITY);
3531 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
3532 UNSET_ACTION_INDEX);
3533 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
3534 SIZE_DEFAULT);
3535 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
3536 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
3537 }
3538 }
3539
3540 /**
3541 * Apply wearable extensions to a notification that is being built. This is typically
3542 * called by the {@link android.app.Notification.Builder#extend} method of
3543 * {@link android.app.Notification.Builder}.
3544 */
3545 @Override
3546 public Notification.Builder extend(Notification.Builder builder) {
3547 Bundle wearableBundle = new Bundle();
3548
3549 if (!mActions.isEmpty()) {
3550 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
3551 }
3552 if (mFlags != DEFAULT_FLAGS) {
3553 wearableBundle.putInt(KEY_FLAGS, mFlags);
3554 }
3555 if (mDisplayIntent != null) {
3556 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
3557 }
3558 if (!mPages.isEmpty()) {
3559 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
3560 new Notification[mPages.size()]));
3561 }
3562 if (mBackground != null) {
3563 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
3564 }
3565 if (mContentIcon != 0) {
3566 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
3567 }
3568 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
3569 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
3570 }
3571 if (mContentActionIndex != UNSET_ACTION_INDEX) {
3572 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
3573 mContentActionIndex);
3574 }
3575 if (mCustomSizePreset != SIZE_DEFAULT) {
3576 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
3577 }
3578 if (mCustomContentHeight != 0) {
3579 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
3580 }
3581 if (mGravity != DEFAULT_GRAVITY) {
3582 wearableBundle.putInt(KEY_GRAVITY, mGravity);
3583 }
3584
3585 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
3586 return builder;
3587 }
3588
3589 @Override
3590 public WearableExtender clone() {
3591 WearableExtender that = new WearableExtender();
3592 that.mActions = new ArrayList<Action>(this.mActions);
3593 that.mFlags = this.mFlags;
3594 that.mDisplayIntent = this.mDisplayIntent;
3595 that.mPages = new ArrayList<Notification>(this.mPages);
3596 that.mBackground = this.mBackground;
3597 that.mContentIcon = this.mContentIcon;
3598 that.mContentIconGravity = this.mContentIconGravity;
3599 that.mContentActionIndex = this.mContentActionIndex;
3600 that.mCustomSizePreset = this.mCustomSizePreset;
3601 that.mCustomContentHeight = this.mCustomContentHeight;
3602 that.mGravity = this.mGravity;
3603 return that;
3604 }
3605
3606 /**
3607 * Add a wearable action to this notification.
3608 *
3609 * <p>When wearable actions are added using this method, the set of actions that
3610 * show on a wearable device splits from devices that only show actions added
3611 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3612 * of which actions display on different devices.
3613 *
3614 * @param action the action to add to this notification
3615 * @return this object for method chaining
3616 * @see android.app.Notification.Action
3617 */
3618 public WearableExtender addAction(Action action) {
3619 mActions.add(action);
3620 return this;
3621 }
3622
3623 /**
3624 * Adds wearable actions to this notification.
3625 *
3626 * <p>When wearable actions are added using this method, the set of actions that
3627 * show on a wearable device splits from devices that only show actions added
3628 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3629 * of which actions display on different devices.
3630 *
3631 * @param actions the actions to add to this notification
3632 * @return this object for method chaining
3633 * @see android.app.Notification.Action
3634 */
3635 public WearableExtender addActions(List<Action> actions) {
3636 mActions.addAll(actions);
3637 return this;
3638 }
3639
3640 /**
3641 * Clear all wearable actions present on this builder.
3642 * @return this object for method chaining.
3643 * @see #addAction
3644 */
3645 public WearableExtender clearActions() {
3646 mActions.clear();
3647 return this;
3648 }
3649
3650 /**
3651 * Get the wearable actions present on this notification.
3652 */
3653 public List<Action> getActions() {
3654 return mActions;
3655 }
3656
3657 /**
3658 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07003659 * this notification. The {@link PendingIntent} provided should be for an activity.
3660 *
3661 * <pre class="prettyprint">
3662 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
3663 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
3664 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3665 * Notification notif = new Notification.Builder(context)
3666 * .extend(new Notification.WearableExtender()
3667 * .setDisplayIntent(displayPendingIntent)
3668 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
3669 * .build();</pre>
3670 *
3671 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07003672 * should have an empty task affinity. It is also recommended to use the device
3673 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07003674 *
3675 * <p>Example AndroidManifest.xml entry:
3676 * <pre class="prettyprint">
3677 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
3678 * android:exported=&quot;true&quot;
3679 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07003680 * android:taskAffinity=&quot;&quot;
3681 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003682 *
3683 * @param intent the {@link PendingIntent} for an activity
3684 * @return this object for method chaining
3685 * @see android.app.Notification.WearableExtender#getDisplayIntent
3686 */
3687 public WearableExtender setDisplayIntent(PendingIntent intent) {
3688 mDisplayIntent = intent;
3689 return this;
3690 }
3691
3692 /**
3693 * Get the intent to launch inside of an activity view when displaying this
3694 * notification. This {@code PendingIntent} should be for an activity.
3695 */
3696 public PendingIntent getDisplayIntent() {
3697 return mDisplayIntent;
3698 }
3699
3700 /**
3701 * Add an additional page of content to display with this notification. The current
3702 * notification forms the first page, and pages added using this function form
3703 * subsequent pages. This field can be used to separate a notification into multiple
3704 * sections.
3705 *
3706 * @param page the notification to add as another page
3707 * @return this object for method chaining
3708 * @see android.app.Notification.WearableExtender#getPages
3709 */
3710 public WearableExtender addPage(Notification page) {
3711 mPages.add(page);
3712 return this;
3713 }
3714
3715 /**
3716 * Add additional pages of content to display with this notification. The current
3717 * notification forms the first page, and pages added using this function form
3718 * subsequent pages. This field can be used to separate a notification into multiple
3719 * sections.
3720 *
3721 * @param pages a list of notifications
3722 * @return this object for method chaining
3723 * @see android.app.Notification.WearableExtender#getPages
3724 */
3725 public WearableExtender addPages(List<Notification> pages) {
3726 mPages.addAll(pages);
3727 return this;
3728 }
3729
3730 /**
3731 * Clear all additional pages present on this builder.
3732 * @return this object for method chaining.
3733 * @see #addPage
3734 */
3735 public WearableExtender clearPages() {
3736 mPages.clear();
3737 return this;
3738 }
3739
3740 /**
3741 * Get the array of additional pages of content for displaying this notification. The
3742 * current notification forms the first page, and elements within this array form
3743 * subsequent pages. This field can be used to separate a notification into multiple
3744 * sections.
3745 * @return the pages for this notification
3746 */
3747 public List<Notification> getPages() {
3748 return mPages;
3749 }
3750
3751 /**
3752 * Set a background image to be displayed behind the notification content.
3753 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3754 * will work with any notification style.
3755 *
3756 * @param background the background bitmap
3757 * @return this object for method chaining
3758 * @see android.app.Notification.WearableExtender#getBackground
3759 */
3760 public WearableExtender setBackground(Bitmap background) {
3761 mBackground = background;
3762 return this;
3763 }
3764
3765 /**
3766 * Get a background image to be displayed behind the notification content.
3767 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3768 * will work with any notification style.
3769 *
3770 * @return the background image
3771 * @see android.app.Notification.WearableExtender#setBackground
3772 */
3773 public Bitmap getBackground() {
3774 return mBackground;
3775 }
3776
3777 /**
3778 * Set an icon that goes with the content of this notification.
3779 */
3780 public WearableExtender setContentIcon(int icon) {
3781 mContentIcon = icon;
3782 return this;
3783 }
3784
3785 /**
3786 * Get an icon that goes with the content of this notification.
3787 */
3788 public int getContentIcon() {
3789 return mContentIcon;
3790 }
3791
3792 /**
3793 * Set the gravity that the content icon should have within the notification display.
3794 * Supported values include {@link android.view.Gravity#START} and
3795 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3796 * @see #setContentIcon
3797 */
3798 public WearableExtender setContentIconGravity(int contentIconGravity) {
3799 mContentIconGravity = contentIconGravity;
3800 return this;
3801 }
3802
3803 /**
3804 * Get the gravity that the content icon should have within the notification display.
3805 * Supported values include {@link android.view.Gravity#START} and
3806 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3807 * @see #getContentIcon
3808 */
3809 public int getContentIconGravity() {
3810 return mContentIconGravity;
3811 }
3812
3813 /**
3814 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07003815 * this notification. This action will no longer display separately from the
3816 * notification's content.
3817 *
Griff Hazenca48d352014-05-28 22:37:13 -07003818 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003819 * set, although the list of available actions comes from the main notification and not
3820 * from the child page's notification.
3821 *
3822 * @param actionIndex The index of the action to hoist onto the current notification page.
3823 * If wearable actions were added to the main notification, this index
3824 * will apply to that list, otherwise it will apply to the regular
3825 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07003826 */
3827 public WearableExtender setContentAction(int actionIndex) {
3828 mContentActionIndex = actionIndex;
3829 return this;
3830 }
3831
3832 /**
Griff Hazenca48d352014-05-28 22:37:13 -07003833 * Get the index of the notification action, if any, that was specified as being clickable
3834 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07003835 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07003836 *
Griff Hazenca48d352014-05-28 22:37:13 -07003837 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003838 * set, although the list of available actions comes from the main notification and not
3839 * from the child page's notification.
3840 *
3841 * <p>If wearable specific actions were added to the main notification, this index will
3842 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07003843 *
3844 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07003845 */
3846 public int getContentAction() {
3847 return mContentActionIndex;
3848 }
3849
3850 /**
3851 * Set the gravity that this notification should have within the available viewport space.
3852 * Supported values include {@link android.view.Gravity#TOP},
3853 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3854 * The default value is {@link android.view.Gravity#BOTTOM}.
3855 */
3856 public WearableExtender setGravity(int gravity) {
3857 mGravity = gravity;
3858 return this;
3859 }
3860
3861 /**
3862 * Get the gravity that this notification should have within the available viewport space.
3863 * Supported values include {@link android.view.Gravity#TOP},
3864 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3865 * The default value is {@link android.view.Gravity#BOTTOM}.
3866 */
3867 public int getGravity() {
3868 return mGravity;
3869 }
3870
3871 /**
3872 * Set the custom size preset for the display of this notification out of the available
3873 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3874 * {@link #SIZE_LARGE}.
3875 * <p>Some custom size presets are only applicable for custom display notifications created
3876 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
3877 * documentation for the preset in question. See also
3878 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
3879 */
3880 public WearableExtender setCustomSizePreset(int sizePreset) {
3881 mCustomSizePreset = sizePreset;
3882 return this;
3883 }
3884
3885 /**
3886 * Get the custom size preset for the display of this notification out of the available
3887 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3888 * {@link #SIZE_LARGE}.
3889 * <p>Some custom size presets are only applicable for custom display notifications created
3890 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
3891 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
3892 */
3893 public int getCustomSizePreset() {
3894 return mCustomSizePreset;
3895 }
3896
3897 /**
3898 * Set the custom height in pixels for the display of this notification's content.
3899 * <p>This option is only available for custom display notifications created
3900 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
3901 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
3902 * {@link #getCustomContentHeight}.
3903 */
3904 public WearableExtender setCustomContentHeight(int height) {
3905 mCustomContentHeight = height;
3906 return this;
3907 }
3908
3909 /**
3910 * Get the custom height in pixels for the display of this notification's content.
3911 * <p>This option is only available for custom display notifications created
3912 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
3913 * {@link #setCustomContentHeight}.
3914 */
3915 public int getCustomContentHeight() {
3916 return mCustomContentHeight;
3917 }
3918
3919 /**
3920 * Set whether the scrolling position for the contents of this notification should start
3921 * at the bottom of the contents instead of the top when the contents are too long to
3922 * display within the screen. Default is false (start scroll at the top).
3923 */
3924 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
3925 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
3926 return this;
3927 }
3928
3929 /**
3930 * Get whether the scrolling position for the contents of this notification should start
3931 * at the bottom of the contents instead of the top when the contents are too long to
3932 * display within the screen. Default is false (start scroll at the top).
3933 */
3934 public boolean getStartScrollBottom() {
3935 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
3936 }
3937
3938 /**
3939 * Set whether the content intent is available when the wearable device is not connected
3940 * to a companion device. The user can still trigger this intent when the wearable device
3941 * is offline, but a visual hint will indicate that the content intent may not be available.
3942 * Defaults to true.
3943 */
3944 public WearableExtender setContentIntentAvailableOffline(
3945 boolean contentIntentAvailableOffline) {
3946 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
3947 return this;
3948 }
3949
3950 /**
3951 * Get whether the content intent is available when the wearable device is not connected
3952 * to a companion device. The user can still trigger this intent when the wearable device
3953 * is offline, but a visual hint will indicate that the content intent may not be available.
3954 * Defaults to true.
3955 */
3956 public boolean getContentIntentAvailableOffline() {
3957 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
3958 }
3959
3960 /**
3961 * Set a hint that this notification's icon should not be displayed.
3962 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
3963 * @return this object for method chaining
3964 */
3965 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
3966 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
3967 return this;
3968 }
3969
3970 /**
3971 * Get a hint that this notification's icon should not be displayed.
3972 * @return {@code true} if this icon should not be displayed, false otherwise.
3973 * The default value is {@code false} if this was never set.
3974 */
3975 public boolean getHintHideIcon() {
3976 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
3977 }
3978
3979 /**
3980 * Set a visual hint that only the background image of this notification should be
3981 * displayed, and other semantic content should be hidden. This hint is only applicable
3982 * to sub-pages added using {@link #addPage}.
3983 */
3984 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
3985 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
3986 return this;
3987 }
3988
3989 /**
3990 * Get a visual hint that only the background image of this notification should be
3991 * displayed, and other semantic content should be hidden. This hint is only applicable
3992 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
3993 */
3994 public boolean getHintShowBackgroundOnly() {
3995 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
3996 }
3997
3998 private void setFlag(int mask, boolean value) {
3999 if (value) {
4000 mFlags |= mask;
4001 } else {
4002 mFlags &= ~mask;
4003 }
4004 }
4005 }
4006
4007 /**
4008 * Get an array of Notification objects from a parcelable array bundle field.
4009 * Update the bundle to have a typed array so fetches in the future don't need
4010 * to do an array copy.
4011 */
4012 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4013 Parcelable[] array = bundle.getParcelableArray(key);
4014 if (array instanceof Notification[] || array == null) {
4015 return (Notification[]) array;
4016 }
4017 Notification[] typedArray = Arrays.copyOf(array, array.length,
4018 Notification[].class);
4019 bundle.putParcelableArray(key, typedArray);
4020 return typedArray;
4021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022}