blob: da59a0338deb9411b8ab7e698202fa7ec65c1174 [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;
John Spurlockc0650f022014-07-19 13:22:39 -040029import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070030import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070031import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040033import android.os.BadParcelableException;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050034import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Parcel;
36import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040037import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070038import android.os.UserHandle;
Kenny Guy8a0101b2014-05-08 23:34:12 +010039import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040041import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040042import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070043import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080044import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070045import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.widget.RemoteViews;
47
Griff Hazen959591e2014-05-15 22:26:18 -070048import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070049import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070050
Tor Norbyed9273d62013-05-30 15:59:53 -070051import java.lang.annotation.Retention;
52import java.lang.annotation.RetentionPolicy;
Andy Stadler110988c2010-12-03 14:29:16 -080053import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050054import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070055import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070056import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070057import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059/**
60 * A class that represents how a persistent notification is to be presented to
61 * the user using the {@link android.app.NotificationManager}.
62 *
Joe Onoratocb109a02011-01-18 17:57:41 -080063 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
64 * easier to construct Notifications.</p>
65 *
Joe Fernandez558459f2011-10-13 16:47:36 -070066 * <div class="special reference">
67 * <h3>Developer Guides</h3>
68 * <p>For a guide to creating notifications, read the
69 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
70 * developer guide.</p>
71 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 */
73public class Notification implements Parcelable
74{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040075 private static final String TAG = "Notification";
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040078 * An activity that provides a user interface for adjusting notification preferences for its
79 * containing application. Optional but recommended for apps that post
80 * {@link android.app.Notification Notifications}.
81 */
82 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
83 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
84 = "android.intent.category.NOTIFICATION_PREFERENCES";
85
86 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * Use all default values (where applicable).
88 */
89 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 /**
92 * Use the default notification sound. This will ignore any given
93 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050094 *
Chris Wren47c20a12014-06-18 17:27:29 -040095 * <p>
96 * A notification that is noisy is more likely to be presented as a heads-up notification.
97 * </p>
98 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500100 */
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 public static final int DEFAULT_SOUND = 1;
103
104 /**
105 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700107 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500108 *
Chris Wren47c20a12014-06-18 17:27:29 -0400109 * <p>
110 * A notification that vibrates is more likely to be presented as a heads-up notification.
111 * </p>
112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500114 */
115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
119 * Use the default notification lights. This will ignore the
120 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
121 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500122 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124 */
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800130 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131 * Default value: {@link System#currentTimeMillis() Now}.
132 *
133 * Choose a timestamp that will be most relevant to the user. For most finite events, this
134 * corresponds to the time the event happened (or will happen, in the case of events that have
135 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800136 * timestamped according to when the activity began.
137 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500138 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800139 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140 * <ul>
141 * <li>Notification of a new chat message should be stamped when the message was received.</li>
142 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
143 * <li>Notification of a completed file download should be stamped when the download finished.</li>
144 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
145 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
146 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800147 * </ul>
148 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
150 public long when;
151
152 /**
153 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500154 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 */
156 public int icon;
157
158 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800159 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
160 * leave it at its default value of 0.
161 *
162 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700163 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800164 */
165 public int iconLevel;
166
167 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * The number of events that this notification represents. For example, in a new mail
169 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800170 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 * The system may or may not use this field to modify the appearance of the notification. For
172 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
173 * superimposed over the icon in the status bar. Starting with
174 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
175 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800176 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500177 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 */
179 public int number;
180
181 /**
182 * The intent to execute when the expanded status entry is clicked. If
183 * this is an activity, it must include the
184 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800185 * that you take care of task management as described in the
186 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800187 * Stack</a> document. In particular, make sure to read the notification section
188 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
189 * Notifications</a> for the correct ways to launch an application from a
190 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 */
192 public PendingIntent contentIntent;
193
194 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195 * The intent to execute when the notification is explicitly dismissed by the user, either with
196 * the "Clear All" button or by swiping it away individually.
197 *
198 * This probably shouldn't be launching an activity since several of those will be sent
199 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 */
201 public PendingIntent deleteIntent;
202
203 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700204 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800205 *
Chris Wren47c20a12014-06-18 17:27:29 -0400206 * <p>
207 * The system UI may choose to display a heads-up notification, instead of
208 * launching this intent, while the user is using the device.
209 * </p>
210 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800211 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400212 */
213 public PendingIntent fullScreenIntent;
214
215 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400216 * Text that summarizes this notification for accessibility services.
217 *
218 * As of the L release, this text is no longer shown on screen, but it is still useful to
219 * accessibility services (where it serves as an audible announcement of the notification's
220 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400221 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800222 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 */
224 public CharSequence tickerText;
225
226 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400227 * Formerly, a view showing the {@link #tickerText}.
228 *
229 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400230 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400231 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800232 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400233
234 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400235 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 */
237 public RemoteViews contentView;
238
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400239 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400240 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400241 * opportunity to show more detail. The system UI may choose to show this
242 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400243 */
244 public RemoteViews bigContentView;
245
Chris Wren8fd39ec2014-02-27 17:43:26 -0500246
247 /**
248 * @hide
Chris Wren47c20a12014-06-18 17:27:29 -0400249 * A medium-format version of {@link #contentView}, providing the Notification an
250 * opportunity to add action buttons to contentView. At its discretion, the system UI may
251 * choose to show this as a heads-up notification, which will pop up so the user can see
252 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500253 */
254 public RemoteViews headsUpContentView;
255
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400256 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800257 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800259 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 /**
262 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500263 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400265 * A notification that is noisy is more likely to be presented as a heads-up notification.
266 * </p>
267 *
268 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500269 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * </p>
271 */
272 public Uri sound;
273
274 /**
275 * Use this constant as the value for audioStreamType to request that
276 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700277 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400278 *
279 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 */
281 public static final int STREAM_DEFAULT = -1;
282
283 /**
284 * The audio stream type to use when playing the sound.
285 * Should be one of the STREAM_ constants from
286 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400287 *
288 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 */
290 public int audioStreamType = STREAM_DEFAULT;
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400293 * The default value of {@link #audioAttributes}.
294 */
295 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
296 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
297 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
298 .build();
299
300 /**
301 * The {@link AudioAttributes audio attributes} to use when playing the sound.
302 */
303 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
304
305 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500306 * The pattern with which to vibrate.
307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 * <p>
309 * To vibrate the default pattern, see {@link #defaults}.
310 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500311 *
Chris Wren47c20a12014-06-18 17:27:29 -0400312 * <p>
313 * A notification that vibrates is more likely to be presented as a heads-up notification.
314 * </p>
315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 * @see android.os.Vibrator#vibrate(long[],int)
317 */
318 public long[] vibrate;
319
320 /**
321 * The color of the led. The hardware will do its best approximation.
322 *
323 * @see #FLAG_SHOW_LIGHTS
324 * @see #flags
325 */
326 public int ledARGB;
327
328 /**
329 * The number of milliseconds for the LED to be on while it's flashing.
330 * The hardware will do its best approximation.
331 *
332 * @see #FLAG_SHOW_LIGHTS
333 * @see #flags
334 */
335 public int ledOnMS;
336
337 /**
338 * The number of milliseconds for the LED to be off while it's flashing.
339 * The hardware will do its best approximation.
340 *
341 * @see #FLAG_SHOW_LIGHTS
342 * @see #flags
343 */
344 public int ledOffMS;
345
346 /**
347 * Specifies which values should be taken from the defaults.
348 * <p>
349 * To set, OR the desired from {@link #DEFAULT_SOUND},
350 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
351 * values, use {@link #DEFAULT_ALL}.
352 * </p>
353 */
354 public int defaults;
355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
357 * Bit to be bitwise-ored into the {@link #flags} field that should be
358 * set if you want the LED on for this notification.
359 * <ul>
360 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
361 * or 0 for both ledOnMS and ledOffMS.</li>
362 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
363 * <li>To flash the LED, pass the number of milliseconds that it should
364 * be on and off to ledOnMS and ledOffMS.</li>
365 * </ul>
366 * <p>
367 * Since hardware varies, you are not guaranteed that any of the values
368 * you pass are honored exactly. Use the system defaults (TODO) if possible
369 * because they will be set to values that work on any given hardware.
370 * <p>
371 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500372 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 */
374 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
375
376 /**
377 * Bit to be bitwise-ored into the {@link #flags} field that should be
378 * set if this notification is in reference to something that is ongoing,
379 * like a phone call. It should not be set if this notification is in
380 * reference to something that happened at a particular point in time,
381 * like a missed phone call.
382 */
383 public static final int FLAG_ONGOING_EVENT = 0x00000002;
384
385 /**
386 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700387 * the audio will be repeated until the notification is
388 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 */
390 public static final int FLAG_INSISTENT = 0x00000004;
391
392 /**
393 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700394 * set if you would only like the sound, vibrate and ticker to be played
395 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
397 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
398
399 /**
400 * Bit to be bitwise-ored into the {@link #flags} field that should be
401 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500402 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 */
405 public static final int FLAG_AUTO_CANCEL = 0x00000010;
406
407 /**
408 * Bit to be bitwise-ored into the {@link #flags} field that should be
409 * set if the notification should not be canceled when the user clicks
410 * the Clear all button.
411 */
412 public static final int FLAG_NO_CLEAR = 0x00000020;
413
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700414 /**
415 * Bit to be bitwise-ored into the {@link #flags} field that should be
416 * set if this notification represents a currently running service. This
417 * will normally be set for you by {@link Service#startForeground}.
418 */
419 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
420
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400421 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500422 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800423 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500424 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400425 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500426 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400427
Griff Hazendfcb0802014-02-11 12:00:00 -0800428 /**
429 * Bit to be bitswise-ored into the {@link #flags} field that should be
430 * set if this notification is relevant to the current device only
431 * and it is not recommended that it bridge to other devices.
432 */
433 public static final int FLAG_LOCAL_ONLY = 0x00000100;
434
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700435 /**
436 * Bit to be bitswise-ored into the {@link #flags} field that should be
437 * set if this notification is the group summary for a group of notifications.
438 * Grouped notifications may display in a cluster or stack on devices which
439 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
440 */
441 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 public int flags;
444
Tor Norbyed9273d62013-05-30 15:59:53 -0700445 /** @hide */
446 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
447 @Retention(RetentionPolicy.SOURCE)
448 public @interface Priority {}
449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500451 * Default notification {@link #priority}. If your application does not prioritize its own
452 * notifications, use this value for all notifications.
453 */
454 public static final int PRIORITY_DEFAULT = 0;
455
456 /**
457 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
458 * items smaller, or at a different position in the list, compared with your app's
459 * {@link #PRIORITY_DEFAULT} items.
460 */
461 public static final int PRIORITY_LOW = -1;
462
463 /**
464 * Lowest {@link #priority}; these items might not be shown to the user except under special
465 * circumstances, such as detailed notification logs.
466 */
467 public static final int PRIORITY_MIN = -2;
468
469 /**
470 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
471 * show these items larger, or at a different position in notification lists, compared with
472 * your app's {@link #PRIORITY_DEFAULT} items.
473 */
474 public static final int PRIORITY_HIGH = 1;
475
476 /**
477 * Highest {@link #priority}, for your application's most important items that require the
478 * user's prompt attention or input.
479 */
480 public static final int PRIORITY_MAX = 2;
481
482 /**
483 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800484 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500485 * Priority is an indication of how much of the user's valuable attention should be consumed by
486 * this notification. Low-priority notifications may be hidden from the user in certain
487 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500488 * system will make a determination about how to interpret this priority when presenting
489 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400490 *
491 * <p>
492 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
493 * as a heads-up notification.
494 * </p>
495 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500496 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700497 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500498 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800499
Dan Sandler26e81cf2014-05-06 10:01:27 -0400500 /**
501 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
502 * to be applied by the standard Style templates when presenting this notification.
503 *
504 * The current template design constructs a colorful header image by overlaying the
505 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
506 * ignored.
507 */
508 public int color = COLOR_DEFAULT;
509
510 /**
511 * Special value of {@link #color} telling the system not to decorate this notification with
512 * any special color but instead use default colors when presenting this notification.
513 */
514 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600515
516 /**
517 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
518 * the notification's presence and contents in untrusted situations (namely, on the secure
519 * lockscreen).
520 *
521 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
522 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
523 * shown in all situations, but the contents are only available if the device is unlocked for
524 * the appropriate user.
525 *
526 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
527 * can be read even in an "insecure" context (that is, above a secure lockscreen).
528 * To modify the public version of this notification—for example, to redact some portions—see
529 * {@link Builder#setPublicVersion(Notification)}.
530 *
531 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
532 * and ticker until the user has bypassed the lockscreen.
533 */
534 public int visibility;
535
536 public static final int VISIBILITY_PUBLIC = 1;
537 public static final int VISIBILITY_PRIVATE = 0;
538 public static final int VISIBILITY_SECRET = -1;
539
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500540 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400541 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500542 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400543 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500544
545 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400546 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500547 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400548 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500549
550 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400551 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500552 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400553 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500554
555 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400556 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500557 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400558 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500559
560 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400561 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500562 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400563 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500564
565 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400566 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500567 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400568 public static final String CATEGORY_ALARM = "alarm";
569
570 /**
571 * Notification category: progress of a long-running background operation.
572 */
573 public static final String CATEGORY_PROGRESS = "progress";
574
575 /**
576 * Notification category: social network or sharing update.
577 */
578 public static final String CATEGORY_SOCIAL = "social";
579
580 /**
581 * Notification category: error in background operation or authentication status.
582 */
583 public static final String CATEGORY_ERROR = "err";
584
585 /**
586 * Notification category: media transport control for playback.
587 */
588 public static final String CATEGORY_TRANSPORT = "transport";
589
590 /**
591 * Notification category: system or device status update. Reserved for system use.
592 */
593 public static final String CATEGORY_SYSTEM = "sys";
594
595 /**
596 * Notification category: indication of running background service.
597 */
598 public static final String CATEGORY_SERVICE = "service";
599
600 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400601 * Notification category: a specific, timely recommendation for a single thing.
602 * For example, a news app might want to recommend a news story it believes the user will
603 * want to read next.
604 */
605 public static final String CATEGORY_RECOMMENDATION = "recommendation";
606
607 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400608 * Notification category: ongoing information about device or contextual status.
609 */
610 public static final String CATEGORY_STATUS = "status";
611
612 /**
613 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
614 * that best describes this Notification. May be used by the system for ranking and filtering.
615 */
616 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700618 private String mGroupKey;
619
620 /**
621 * Get the key used to group this notification into a cluster or stack
622 * with other notifications on devices which support such rendering.
623 */
624 public String getGroup() {
625 return mGroupKey;
626 }
627
628 private String mSortKey;
629
630 /**
631 * Get a sort key that orders this notification among other notifications from the
632 * same package. This can be useful if an external sort was already applied and an app
633 * would like to preserve this. Notifications will be sorted lexicographically using this
634 * value, although providing different priorities in addition to providing sort key may
635 * cause this value to be ignored.
636 *
637 * <p>This sort key can also be used to order members of a notification group. See
638 * {@link Builder#setGroup}.
639 *
640 * @see String#compareTo(String)
641 */
642 public String getSortKey() {
643 return mSortKey;
644 }
645
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400647 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400648 * <p>
649 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
650 * APIs, and are intended to be used by
651 * {@link android.service.notification.NotificationListenerService} implementations to extract
652 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400654 public Bundle extras = new Bundle();
655
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400656 /**
657 * {@link #extras} key: this is the title of the notification,
658 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
659 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500660 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400661
662 /**
663 * {@link #extras} key: this is the title of the notification when shown in expanded form,
664 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
665 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400666 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400667
668 /**
669 * {@link #extras} key: this is the main text payload, as supplied to
670 * {@link Builder#setContentText(CharSequence)}.
671 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500672 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400673
674 /**
675 * {@link #extras} key: this is a third line of text, as supplied to
676 * {@link Builder#setSubText(CharSequence)}.
677 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400678 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400679
680 /**
681 * {@link #extras} key: this is a small piece of additional text as supplied to
682 * {@link Builder#setContentInfo(CharSequence)}.
683 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400684 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400685
686 /**
687 * {@link #extras} key: this is a line of summary information intended to be shown
688 * alongside expanded notifications, as supplied to (e.g.)
689 * {@link BigTextStyle#setSummaryText(CharSequence)}.
690 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400691 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400692
693 /**
694 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
695 * supplied to {@link Builder#setSmallIcon(int)}.
696 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500697 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400698
699 /**
700 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
701 * notification payload, as
702 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
703 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400704 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400705
706 /**
707 * {@link #extras} key: this is a bitmap to be used instead of the one from
708 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
709 * shown in its expanded form, as supplied to
710 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
711 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400712 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400713
714 /**
715 * {@link #extras} key: this is the progress value supplied to
716 * {@link Builder#setProgress(int, int, boolean)}.
717 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400718 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400719
720 /**
721 * {@link #extras} key: this is the maximum value supplied to
722 * {@link Builder#setProgress(int, int, boolean)}.
723 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400724 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400725
726 /**
727 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
728 * {@link Builder#setProgress(int, int, boolean)}.
729 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400730 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400731
732 /**
733 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
734 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
735 * {@link Builder#setUsesChronometer(boolean)}.
736 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400737 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400738
739 /**
740 * {@link #extras} key: whether {@link #when} should be shown,
741 * as supplied to {@link Builder#setShowWhen(boolean)}.
742 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400743 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400744
745 /**
746 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
747 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
748 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400749 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400750
751 /**
752 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
753 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
754 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400755 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400756
757 /**
758 * {@link #extras} key: A string representing the name of the specific
759 * {@link android.app.Notification.Style} used to create this notification.
760 */
Chris Wren91ad5632013-06-05 15:05:57 -0400761 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400762
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400763 /**
764 * {@link #extras} key: An array of people that this notification relates to, specified
765 * by contacts provider contact URI.
766 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400767 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500768
769 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400770 * {@link #extras} key: used to provide hints about the appropriateness of
771 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400772 * @hide
773 */
774 public static final String EXTRA_AS_HEADS_UP = "headsup";
775
776 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400777 * Allow certain system-generated notifications to appear before the device is provisioned.
778 * Only available to notifications coming from the android package.
779 * @hide
780 */
781 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
782
783 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700784 * {@link #extras} key: A
785 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
786 * in the background when the notification is selected. The URI must point to an image stream
787 * suitable for passing into
788 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
789 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
790 * URI used for this purpose must require no permissions to read the image data.
791 */
792 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
793
794 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400795 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700796 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400797 * {@link android.app.Notification.MediaStyle} notification.
798 */
799 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
800
801 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400802 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
803 * displayed in the heads up space.
804 *
805 * <p>
806 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
807 * full-screen intent when posted.
808 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400809 * @hide
810 */
811 public static final int HEADS_UP_NEVER = 0;
812
813 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400814 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
815 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400816 * @hide
817 */
818 public static final int HEADS_UP_ALLOWED = 1;
819
820 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400821 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
822 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400823 * @hide
824 */
825 public static final int HEADS_UP_REQUESTED = 2;
826
827 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400828 * Structure to encapsulate a named action that can be shown as part of this notification.
829 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
830 * selected by the user.
831 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700832 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
833 * or {@link Notification.Builder#addAction(Notification.Action)}
834 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400835 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500836 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700837 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700838 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700839
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400840 /**
841 * Small icon representing the action.
842 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400843 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700844
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845 /**
846 * Title of the action.
847 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400848 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700849
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400850 /**
851 * Intent to send when the user invokes this action. May be null, in which case the action
852 * may be rendered in a disabled presentation by the system UI.
853 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400854 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700855
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400856 private Action(Parcel in) {
857 icon = in.readInt();
858 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
859 if (in.readInt() == 1) {
860 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
861 }
Griff Hazen959591e2014-05-15 22:26:18 -0700862 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700863 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400864 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700865
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400866 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700867 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400868 */
869 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700870 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700871 }
872
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700873 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
874 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400875 this.icon = icon;
876 this.title = title;
877 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700878 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700879 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700880 }
881
882 /**
883 * Get additional metadata carried around with this Action.
884 */
885 public Bundle getExtras() {
886 return mExtras;
887 }
888
889 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700890 * Get the list of inputs to be collected from the user when this action is sent.
891 * May return null if no remote inputs were added.
892 */
893 public RemoteInput[] getRemoteInputs() {
894 return mRemoteInputs;
895 }
896
897 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700898 * Builder class for {@link Action} objects.
899 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700900 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700901 private final int mIcon;
902 private final CharSequence mTitle;
903 private final PendingIntent mIntent;
904 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700905 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700906
907 /**
908 * Construct a new builder for {@link Action} object.
909 * @param icon icon to show for this action
910 * @param title the title of the action
911 * @param intent the {@link PendingIntent} to fire when users trigger this action
912 */
913 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700914 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700915 }
916
917 /**
918 * Construct a new builder for {@link Action} object using the fields from an
919 * {@link Action}.
920 * @param action the action to read fields from.
921 */
922 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700923 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
924 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700925 }
926
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700927 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
928 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700929 mIcon = icon;
930 mTitle = title;
931 mIntent = intent;
932 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700933 if (remoteInputs != null) {
934 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
935 Collections.addAll(mRemoteInputs, remoteInputs);
936 }
Griff Hazen959591e2014-05-15 22:26:18 -0700937 }
938
939 /**
940 * Merge additional metadata into this builder.
941 *
942 * <p>Values within the Bundle will replace existing extras values in this Builder.
943 *
944 * @see Notification.Action#extras
945 */
946 public Builder addExtras(Bundle extras) {
947 if (extras != null) {
948 mExtras.putAll(extras);
949 }
950 return this;
951 }
952
953 /**
954 * Get the metadata Bundle used by this Builder.
955 *
956 * <p>The returned Bundle is shared with this Builder.
957 */
958 public Bundle getExtras() {
959 return mExtras;
960 }
961
962 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700963 * Add an input to be collected from the user when this action is sent.
964 * Response values can be retrieved from the fired intent by using the
965 * {@link RemoteInput#getResultsFromIntent} function.
966 * @param remoteInput a {@link RemoteInput} to add to the action
967 * @return this object for method chaining
968 */
969 public Builder addRemoteInput(RemoteInput remoteInput) {
970 if (mRemoteInputs == null) {
971 mRemoteInputs = new ArrayList<RemoteInput>();
972 }
973 mRemoteInputs.add(remoteInput);
974 return this;
975 }
976
977 /**
978 * Apply an extender to this action builder. Extenders may be used to add
979 * metadata or change options on this builder.
980 */
Griff Hazen61a9e862014-05-22 16:05:19 -0700981 public Builder extend(Extender extender) {
982 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700983 return this;
984 }
985
986 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700987 * Combine all of the options that have been set and return a new {@link Action}
988 * object.
989 * @return the built action
990 */
991 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700992 RemoteInput[] remoteInputs = mRemoteInputs != null
993 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
994 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -0700995 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400996 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400997
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400998 @Override
999 public Action clone() {
1000 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001001 icon,
1002 title,
1003 actionIntent, // safe to alias
1004 new Bundle(mExtras),
1005 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001006 }
1007 @Override
1008 public int describeContents() {
1009 return 0;
1010 }
1011 @Override
1012 public void writeToParcel(Parcel out, int flags) {
1013 out.writeInt(icon);
1014 TextUtils.writeToParcel(title, out, flags);
1015 if (actionIntent != null) {
1016 out.writeInt(1);
1017 actionIntent.writeToParcel(out, flags);
1018 } else {
1019 out.writeInt(0);
1020 }
Griff Hazen959591e2014-05-15 22:26:18 -07001021 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001022 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001023 }
Griff Hazen959591e2014-05-15 22:26:18 -07001024 public static final Parcelable.Creator<Action> CREATOR =
1025 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001026 public Action createFromParcel(Parcel in) {
1027 return new Action(in);
1028 }
1029 public Action[] newArray(int size) {
1030 return new Action[size];
1031 }
1032 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001033
1034 /**
1035 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1036 * metadata or change options on an action builder.
1037 */
1038 public interface Extender {
1039 /**
1040 * Apply this extender to a notification action builder.
1041 * @param builder the builder to be modified.
1042 * @return the build object for chaining.
1043 */
1044 public Builder extend(Builder builder);
1045 }
1046
1047 /**
1048 * Wearable extender for notification actions. To add extensions to an action,
1049 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1050 * the {@code WearableExtender()} constructor and apply it to a
1051 * {@link android.app.Notification.Action.Builder} using
1052 * {@link android.app.Notification.Action.Builder#extend}.
1053 *
1054 * <pre class="prettyprint">
1055 * Notification.Action action = new Notification.Action.Builder(
1056 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001057 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001058 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001059 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001060 */
1061 public static final class WearableExtender implements Extender {
1062 /** Notification action extra which contains wearable extensions */
1063 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1064
1065 private static final String KEY_FLAGS = "flags";
1066
1067 // Flags bitwise-ored to mFlags
1068 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1069
1070 // Default value for flags integer
1071 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1072
1073 private int mFlags = DEFAULT_FLAGS;
1074
1075 /**
1076 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1077 * options.
1078 */
1079 public WearableExtender() {
1080 }
1081
1082 /**
1083 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1084 * wearable options present in an existing notification action.
1085 * @param action the notification action to inspect.
1086 */
1087 public WearableExtender(Action action) {
1088 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1089 if (wearableBundle != null) {
1090 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1091 }
1092 }
1093
1094 /**
1095 * Apply wearable extensions to a notification action that is being built. This is
1096 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1097 * method of {@link android.app.Notification.Action.Builder}.
1098 */
1099 @Override
1100 public Action.Builder extend(Action.Builder builder) {
1101 Bundle wearableBundle = new Bundle();
1102
1103 if (mFlags != DEFAULT_FLAGS) {
1104 wearableBundle.putInt(KEY_FLAGS, mFlags);
1105 }
1106
1107 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1108 return builder;
1109 }
1110
1111 @Override
1112 public WearableExtender clone() {
1113 WearableExtender that = new WearableExtender();
1114 that.mFlags = this.mFlags;
1115 return that;
1116 }
1117
1118 /**
1119 * Set whether this action is available when the wearable device is not connected to
1120 * a companion device. The user can still trigger this action when the wearable device is
1121 * offline, but a visual hint will indicate that the action may not be available.
1122 * Defaults to true.
1123 */
1124 public WearableExtender setAvailableOffline(boolean availableOffline) {
1125 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1126 return this;
1127 }
1128
1129 /**
1130 * Get whether this action is available when the wearable device is not connected to
1131 * a companion device. The user can still trigger this action when the wearable device is
1132 * offline, but a visual hint will indicate that the action may not be available.
1133 * Defaults to true.
1134 */
1135 public boolean isAvailableOffline() {
1136 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1137 }
1138
1139 private void setFlag(int mask, boolean value) {
1140 if (value) {
1141 mFlags |= mask;
1142 } else {
1143 mFlags &= ~mask;
1144 }
1145 }
1146 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001147 }
1148
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001149 /**
1150 * Array of all {@link Action} structures attached to this notification by
1151 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1152 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1153 * interface for invoking actions.
1154 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001155 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001156
1157 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001158 * Replacement version of this notification whose content will be shown
1159 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1160 * and {@link #VISIBILITY_PUBLIC}.
1161 */
1162 public Notification publicVersion;
1163
1164 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001165 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001166 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 */
1168 public Notification()
1169 {
1170 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001171 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173
1174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 * @hide
1176 */
1177 public Notification(Context context, int icon, CharSequence tickerText, long when,
1178 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1179 {
1180 this.when = when;
1181 this.icon = icon;
1182 this.tickerText = tickerText;
1183 setLatestEventInfo(context, contentTitle, contentText,
1184 PendingIntent.getActivity(context, 0, contentIntent, 0));
1185 }
1186
1187 /**
1188 * Constructs a Notification object with the information needed to
1189 * have a status bar icon without the standard expanded view.
1190 *
1191 * @param icon The resource id of the icon to put in the status bar.
1192 * @param tickerText The text that flows by in the status bar when the notification first
1193 * activates.
1194 * @param when The time to show in the time field. In the System.currentTimeMillis
1195 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001196 *
1197 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001199 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 public Notification(int icon, CharSequence tickerText, long when)
1201 {
1202 this.icon = icon;
1203 this.tickerText = tickerText;
1204 this.when = when;
1205 }
1206
1207 /**
1208 * Unflatten the notification from a parcel.
1209 */
1210 public Notification(Parcel parcel)
1211 {
1212 int version = parcel.readInt();
1213
1214 when = parcel.readLong();
1215 icon = parcel.readInt();
1216 number = parcel.readInt();
1217 if (parcel.readInt() != 0) {
1218 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1219 }
1220 if (parcel.readInt() != 0) {
1221 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1222 }
1223 if (parcel.readInt() != 0) {
1224 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1225 }
1226 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001227 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001228 }
1229 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1231 }
Joe Onorato561d3852010-11-20 18:09:34 -08001232 if (parcel.readInt() != 0) {
1233 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 defaults = parcel.readInt();
1236 flags = parcel.readInt();
1237 if (parcel.readInt() != 0) {
1238 sound = Uri.CREATOR.createFromParcel(parcel);
1239 }
1240
1241 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001242 if (parcel.readInt() != 0) {
1243 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 vibrate = parcel.createLongArray();
1246 ledARGB = parcel.readInt();
1247 ledOnMS = parcel.readInt();
1248 ledOffMS = parcel.readInt();
1249 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001250
1251 if (parcel.readInt() != 0) {
1252 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1253 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001254
1255 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001256
John Spurlockfd7f1e02014-03-18 16:41:57 -04001257 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001258
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001259 mGroupKey = parcel.readString();
1260
1261 mSortKey = parcel.readString();
1262
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001263 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001264
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001265 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1266
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001267 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001268 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1269 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001270
Chris Wren8fd39ec2014-02-27 17:43:26 -05001271 if (parcel.readInt() != 0) {
1272 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1273 }
1274
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001275 visibility = parcel.readInt();
1276
1277 if (parcel.readInt() != 0) {
1278 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1279 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001280
1281 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283
Andy Stadler110988c2010-12-03 14:29:16 -08001284 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001285 public Notification clone() {
1286 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001287 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001288 return that;
1289 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001290
Daniel Sandler1a497d32013-04-18 14:52:45 -04001291 /**
1292 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1293 * of this into that.
1294 * @hide
1295 */
1296 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001297 that.when = this.when;
1298 that.icon = this.icon;
1299 that.number = this.number;
1300
1301 // PendingIntents are global, so there's no reason (or way) to clone them.
1302 that.contentIntent = this.contentIntent;
1303 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001304 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001305
1306 if (this.tickerText != null) {
1307 that.tickerText = this.tickerText.toString();
1308 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001309 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001310 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001311 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001312 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001313 that.contentView = this.contentView.clone();
1314 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001315 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001316 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1317 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001318 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001319 that.sound = this.sound; // android.net.Uri is immutable
1320 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001321 if (this.audioAttributes != null) {
1322 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1323 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001324
1325 final long[] vibrate = this.vibrate;
1326 if (vibrate != null) {
1327 final int N = vibrate.length;
1328 final long[] vib = that.vibrate = new long[N];
1329 System.arraycopy(vibrate, 0, vib, 0, N);
1330 }
1331
1332 that.ledARGB = this.ledARGB;
1333 that.ledOnMS = this.ledOnMS;
1334 that.ledOffMS = this.ledOffMS;
1335 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001336
Joe Onorato18e69df2010-05-17 22:26:12 -07001337 that.flags = this.flags;
1338
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001339 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001340
John Spurlockfd7f1e02014-03-18 16:41:57 -04001341 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001342
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001343 that.mGroupKey = this.mGroupKey;
1344
1345 that.mSortKey = this.mSortKey;
1346
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001347 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001348 try {
1349 that.extras = new Bundle(this.extras);
1350 // will unparcel
1351 that.extras.size();
1352 } catch (BadParcelableException e) {
1353 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1354 that.extras = null;
1355 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001356 }
1357
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001358 if (this.actions != null) {
1359 that.actions = new Action[this.actions.length];
1360 for(int i=0; i<this.actions.length; i++) {
1361 that.actions[i] = this.actions[i].clone();
1362 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001363 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001364
Daniel Sandler1a497d32013-04-18 14:52:45 -04001365 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001366 that.bigContentView = this.bigContentView.clone();
1367 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001368
Chris Wren8fd39ec2014-02-27 17:43:26 -05001369 if (heavy && this.headsUpContentView != null) {
1370 that.headsUpContentView = this.headsUpContentView.clone();
1371 }
1372
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001373 that.visibility = this.visibility;
1374
1375 if (this.publicVersion != null) {
1376 that.publicVersion = new Notification();
1377 this.publicVersion.cloneInto(that.publicVersion, heavy);
1378 }
1379
Dan Sandler26e81cf2014-05-06 10:01:27 -04001380 that.color = this.color;
1381
Daniel Sandler1a497d32013-04-18 14:52:45 -04001382 if (!heavy) {
1383 that.lightenPayload(); // will clean out extras
1384 }
1385 }
1386
1387 /**
1388 * Removes heavyweight parts of the Notification object for archival or for sending to
1389 * listeners when the full contents are not necessary.
1390 * @hide
1391 */
1392 public final void lightenPayload() {
1393 tickerView = null;
1394 contentView = null;
1395 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001396 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001397 largeIcon = null;
1398 if (extras != null) {
1399 extras.remove(Notification.EXTRA_LARGE_ICON);
1400 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1401 extras.remove(Notification.EXTRA_PICTURE);
1402 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001403 }
1404
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001405 /**
1406 * Make sure this CharSequence is safe to put into a bundle, which basically
1407 * means it had better not be some custom Parcelable implementation.
1408 * @hide
1409 */
1410 public static CharSequence safeCharSequence(CharSequence cs) {
1411 if (cs instanceof Parcelable) {
1412 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1413 + " instance is a custom Parcelable and not allowed in Notification");
1414 return cs.toString();
1415 }
1416
1417 return cs;
1418 }
1419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 public int describeContents() {
1421 return 0;
1422 }
1423
1424 /**
1425 * Flatten this notification from a parcel.
1426 */
1427 public void writeToParcel(Parcel parcel, int flags)
1428 {
1429 parcel.writeInt(1);
1430
1431 parcel.writeLong(when);
1432 parcel.writeInt(icon);
1433 parcel.writeInt(number);
1434 if (contentIntent != null) {
1435 parcel.writeInt(1);
1436 contentIntent.writeToParcel(parcel, 0);
1437 } else {
1438 parcel.writeInt(0);
1439 }
1440 if (deleteIntent != null) {
1441 parcel.writeInt(1);
1442 deleteIntent.writeToParcel(parcel, 0);
1443 } else {
1444 parcel.writeInt(0);
1445 }
1446 if (tickerText != null) {
1447 parcel.writeInt(1);
1448 TextUtils.writeToParcel(tickerText, parcel, flags);
1449 } else {
1450 parcel.writeInt(0);
1451 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001452 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001453 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001454 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001455 } else {
1456 parcel.writeInt(0);
1457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 if (contentView != null) {
1459 parcel.writeInt(1);
1460 contentView.writeToParcel(parcel, 0);
1461 } else {
1462 parcel.writeInt(0);
1463 }
Joe Onorato561d3852010-11-20 18:09:34 -08001464 if (largeIcon != null) {
1465 parcel.writeInt(1);
1466 largeIcon.writeToParcel(parcel, 0);
1467 } else {
1468 parcel.writeInt(0);
1469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470
1471 parcel.writeInt(defaults);
1472 parcel.writeInt(this.flags);
1473
1474 if (sound != null) {
1475 parcel.writeInt(1);
1476 sound.writeToParcel(parcel, 0);
1477 } else {
1478 parcel.writeInt(0);
1479 }
1480 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001481
1482 if (audioAttributes != null) {
1483 parcel.writeInt(1);
1484 audioAttributes.writeToParcel(parcel, 0);
1485 } else {
1486 parcel.writeInt(0);
1487 }
1488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 parcel.writeLongArray(vibrate);
1490 parcel.writeInt(ledARGB);
1491 parcel.writeInt(ledOnMS);
1492 parcel.writeInt(ledOffMS);
1493 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001494
1495 if (fullScreenIntent != null) {
1496 parcel.writeInt(1);
1497 fullScreenIntent.writeToParcel(parcel, 0);
1498 } else {
1499 parcel.writeInt(0);
1500 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001501
1502 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001503
John Spurlockfd7f1e02014-03-18 16:41:57 -04001504 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001505
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001506 parcel.writeString(mGroupKey);
1507
1508 parcel.writeString(mSortKey);
1509
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001510 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001511
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001512 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001513
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001514 if (bigContentView != null) {
1515 parcel.writeInt(1);
1516 bigContentView.writeToParcel(parcel, 0);
1517 } else {
1518 parcel.writeInt(0);
1519 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001520
Chris Wren8fd39ec2014-02-27 17:43:26 -05001521 if (headsUpContentView != null) {
1522 parcel.writeInt(1);
1523 headsUpContentView.writeToParcel(parcel, 0);
1524 } else {
1525 parcel.writeInt(0);
1526 }
1527
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001528 parcel.writeInt(visibility);
1529
1530 if (publicVersion != null) {
1531 parcel.writeInt(1);
1532 publicVersion.writeToParcel(parcel, 0);
1533 } else {
1534 parcel.writeInt(0);
1535 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001536
1537 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539
1540 /**
1541 * Parcelable.Creator that instantiates Notification objects
1542 */
1543 public static final Parcelable.Creator<Notification> CREATOR
1544 = new Parcelable.Creator<Notification>()
1545 {
1546 public Notification createFromParcel(Parcel parcel)
1547 {
1548 return new Notification(parcel);
1549 }
1550
1551 public Notification[] newArray(int size)
1552 {
1553 return new Notification[size];
1554 }
1555 };
1556
1557 /**
1558 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1559 * layout.
1560 *
1561 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1562 * in the view.</p>
1563 * @param context The context for your application / activity.
1564 * @param contentTitle The title that goes in the expanded entry.
1565 * @param contentText The text that goes in the expanded entry.
1566 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1567 * If this is an activity, it must include the
1568 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001569 * that you take care of task management as described in the
1570 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1571 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001572 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001573 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001575 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 public void setLatestEventInfo(Context context,
1577 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001578 Notification.Builder builder = new Notification.Builder(context);
1579
1580 // First, ensure that key pieces of information that may have been set directly
1581 // are preserved
1582 builder.setWhen(this.when);
1583 builder.setSmallIcon(this.icon);
1584 builder.setPriority(this.priority);
1585 builder.setTicker(this.tickerText);
1586 builder.setNumber(this.number);
1587 builder.mFlags = this.flags;
1588 builder.setSound(this.sound, this.audioStreamType);
1589 builder.setDefaults(this.defaults);
1590 builder.setVibrate(this.vibrate);
1591
1592 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001594 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001597 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001599 builder.setContentIntent(contentIntent);
1600 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
1602
1603 @Override
1604 public String toString() {
1605 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001606 sb.append("Notification(pri=");
1607 sb.append(priority);
1608 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001609 if (contentView != null) {
1610 sb.append(contentView.getPackage());
1611 sb.append("/0x");
1612 sb.append(Integer.toHexString(contentView.getLayoutId()));
1613 } else {
1614 sb.append("null");
1615 }
1616 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001617 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1618 sb.append("default");
1619 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 int N = this.vibrate.length-1;
1621 sb.append("[");
1622 for (int i=0; i<N; i++) {
1623 sb.append(this.vibrate[i]);
1624 sb.append(',');
1625 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001626 if (N != -1) {
1627 sb.append(this.vibrate[N]);
1628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 } else {
1631 sb.append("null");
1632 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001633 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001634 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001636 } else if (this.sound != null) {
1637 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 } else {
1639 sb.append("null");
1640 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001641 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001643 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001644 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001645 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001646 if (this.category != null) {
1647 sb.append(" category=");
1648 sb.append(this.category);
1649 }
1650 if (this.mGroupKey != null) {
1651 sb.append(" groupKey=");
1652 sb.append(this.mGroupKey);
1653 }
1654 if (this.mSortKey != null) {
1655 sb.append(" sortKey=");
1656 sb.append(this.mSortKey);
1657 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001658 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001659 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001660 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001661 }
1662 sb.append(" vis=");
1663 sb.append(visibilityToString(this.visibility));
1664 if (this.publicVersion != null) {
1665 sb.append(" publicVersion=");
1666 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001667 }
1668 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 return sb.toString();
1670 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001671
Dan Sandler1b718782014-07-18 12:43:45 -04001672 /**
1673 * {@hide}
1674 */
1675 public static String visibilityToString(int vis) {
1676 switch (vis) {
1677 case VISIBILITY_PRIVATE:
1678 return "PRIVATE";
1679 case VISIBILITY_PUBLIC:
1680 return "PUBLIC";
1681 case VISIBILITY_SECRET:
1682 return "SECRET";
1683 default:
1684 return "UNKNOWN(" + String.valueOf(vis) + ")";
1685 }
1686 }
1687
Jeff Sharkey6d515712012-09-20 16:06:08 -07001688 /** {@hide} */
1689 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001690 if (user.getIdentifier() == UserHandle.USER_ALL) {
1691 user = UserHandle.OWNER;
1692 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001693 if (tickerView != null) {
1694 tickerView.setUser(user);
1695 }
1696 if (contentView != null) {
1697 contentView.setUser(user);
1698 }
1699 if (bigContentView != null) {
1700 bigContentView.setUser(user);
1701 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001702 if (headsUpContentView != null) {
1703 headsUpContentView.setUser(user);
1704 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001705 }
1706
Joe Onoratocb109a02011-01-18 17:57:41 -08001707 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001708 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001709 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001710 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001711 * content views using the platform's notification layout template. If your app supports
1712 * versions of Android as old as API level 4, you can instead use
1713 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1714 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1715 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001716 *
Scott Main183bf112012-08-13 19:12:13 -07001717 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001718 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001719 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001720 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001721 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1722 * .setContentText(subject)
1723 * .setSmallIcon(R.drawable.new_mail)
1724 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001725 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001726 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001727 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001728 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001729 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001730
Joe Onorato46439ce2010-11-19 13:56:21 -08001731 private Context mContext;
1732
1733 private long mWhen;
1734 private int mSmallIcon;
1735 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001736 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001737 private CharSequence mContentTitle;
1738 private CharSequence mContentText;
1739 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001740 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001741 private PendingIntent mContentIntent;
1742 private RemoteViews mContentView;
1743 private PendingIntent mDeleteIntent;
1744 private PendingIntent mFullScreenIntent;
1745 private CharSequence mTickerText;
1746 private RemoteViews mTickerView;
1747 private Bitmap mLargeIcon;
1748 private Uri mSound;
1749 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001750 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08001751 private long[] mVibrate;
1752 private int mLedArgb;
1753 private int mLedOnMs;
1754 private int mLedOffMs;
1755 private int mDefaults;
1756 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001757 private int mProgressMax;
1758 private int mProgress;
1759 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001760 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001761 private String mGroupKey;
1762 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001763 private Bundle mExtras;
1764 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001765 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001766 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001767 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001768 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001769 private int mVisibility = VISIBILITY_PRIVATE;
1770 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001771 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001772 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001773 private int mColor = COLOR_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001774
Joe Onoratocb109a02011-01-18 17:57:41 -08001775 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001776 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001777 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001778
1779 * <table>
1780 * <tr><th align=right>priority</th>
1781 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1782 * <tr><th align=right>when</th>
1783 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1784 * <tr><th align=right>audio stream</th>
1785 * <td>{@link #STREAM_DEFAULT}</td></tr>
1786 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001787 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001788
1789 * @param context
1790 * A {@link Context} that will be used by the Builder to construct the
1791 * RemoteViews. The Context will not be held past the lifetime of this Builder
1792 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001793 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001794 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001795 /*
1796 * Important compatibility note!
1797 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1798 * constructor for later use. At this point Activities - themselves subclasses of
1799 * ContextWrapper - do not have their inner Context populated yet. This means that
1800 * any calls to Context methods from within this constructor can cause NPEs in existing
1801 * apps. Any data populated from mContext should therefore be populated lazily to
1802 * preserve compatibility.
1803 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001804 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001805
1806 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001807 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001808 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04001809 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001810 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001811 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001812
Dan Sandler26e81cf2014-05-06 10:01:27 -04001813 mColorUtil = NotificationColorUtil.getInstance();
Joe Onorato46439ce2010-11-19 13:56:21 -08001814 }
1815
Joe Onoratocb109a02011-01-18 17:57:41 -08001816 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001817 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001818 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001819 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001820 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001821 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001822 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001823 public Builder setWhen(long when) {
1824 mWhen = when;
1825 return this;
1826 }
1827
Joe Onoratocb109a02011-01-18 17:57:41 -08001828 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001829 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001830 * in the content view.
1831 */
1832 public Builder setShowWhen(boolean show) {
1833 mShowWhen = show;
1834 return this;
1835 }
1836
1837 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001838 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001839 *
1840 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001841 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001842 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001843 * Useful when showing an elapsed time (like an ongoing phone call).
1844 *
1845 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001846 * @see Notification#when
1847 */
1848 public Builder setUsesChronometer(boolean b) {
1849 mUseChronometer = b;
1850 return this;
1851 }
1852
1853 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001854 * Set the small icon resource, which will be used to represent the notification in the
1855 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001856 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001857
1858 * The platform template for the expanded view will draw this icon in the left, unless a
1859 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1860 * icon will be moved to the right-hand side.
1861 *
1862
1863 * @param icon
1864 * A resource ID in the application's package of the drawable to use.
1865 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001866 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001867 public Builder setSmallIcon(int icon) {
1868 mSmallIcon = icon;
1869 return this;
1870 }
1871
Joe Onoratocb109a02011-01-18 17:57:41 -08001872 /**
1873 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1874 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1875 * LevelListDrawable}.
1876 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001877 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001878 * @param level The level to use for the icon.
1879 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001880 * @see Notification#icon
1881 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001882 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001883 public Builder setSmallIcon(int icon, int level) {
1884 mSmallIcon = icon;
1885 mSmallIconLevel = level;
1886 return this;
1887 }
1888
Joe Onoratocb109a02011-01-18 17:57:41 -08001889 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001890 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001891 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001892 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001893 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001894 return this;
1895 }
1896
Joe Onoratocb109a02011-01-18 17:57:41 -08001897 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001898 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001899 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001900 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001901 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001902 return this;
1903 }
1904
Joe Onoratocb109a02011-01-18 17:57:41 -08001905 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001906 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001907 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1908 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001909 */
1910 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001911 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001912 return this;
1913 }
1914
1915 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001916 * Set the large number at the right-hand side of the notification. This is
1917 * equivalent to setContentInfo, although it might show the number in a different
1918 * font size for readability.
1919 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001920 public Builder setNumber(int number) {
1921 mNumber = number;
1922 return this;
1923 }
1924
Joe Onoratocb109a02011-01-18 17:57:41 -08001925 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001926 * A small piece of additional information pertaining to this notification.
1927 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001928 * The platform template will draw this on the last line of the notification, at the far
1929 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001930 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001931 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001932 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001933 return this;
1934 }
1935
Joe Onoratocb109a02011-01-18 17:57:41 -08001936 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001937 * Set the progress this notification represents.
1938 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001939 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001940 */
1941 public Builder setProgress(int max, int progress, boolean indeterminate) {
1942 mProgressMax = max;
1943 mProgress = progress;
1944 mProgressIndeterminate = indeterminate;
1945 return this;
1946 }
1947
1948 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001949 * Supply a custom RemoteViews to use instead of the platform template.
1950 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001951 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001952 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001953 public Builder setContent(RemoteViews views) {
1954 mContentView = views;
1955 return this;
1956 }
1957
Joe Onoratocb109a02011-01-18 17:57:41 -08001958 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001959 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1960 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001961 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1962 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1963 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001964 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001965 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001966 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001967 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001968 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001969 public Builder setContentIntent(PendingIntent intent) {
1970 mContentIntent = intent;
1971 return this;
1972 }
1973
Joe Onoratocb109a02011-01-18 17:57:41 -08001974 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001975 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1976 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001977 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001978 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001979 public Builder setDeleteIntent(PendingIntent intent) {
1980 mDeleteIntent = intent;
1981 return this;
1982 }
1983
Joe Onoratocb109a02011-01-18 17:57:41 -08001984 /**
1985 * An intent to launch instead of posting the notification to the status bar.
1986 * Only for use with extremely high-priority notifications demanding the user's
1987 * <strong>immediate</strong> attention, such as an incoming phone call or
1988 * alarm clock that the user has explicitly set to a particular time.
1989 * If this facility is used for something else, please give the user an option
1990 * to turn it off and use a normal notification, as this can be extremely
1991 * disruptive.
1992 *
Chris Wren47c20a12014-06-18 17:27:29 -04001993 * <p>
1994 * The system UI may choose to display a heads-up notification, instead of
1995 * launching this intent, while the user is using the device.
1996 * </p>
1997 *
Joe Onoratocb109a02011-01-18 17:57:41 -08001998 * @param intent The pending intent to launch.
1999 * @param highPriority Passing true will cause this notification to be sent
2000 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002001 *
2002 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002003 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002004 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2005 mFullScreenIntent = intent;
2006 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2007 return this;
2008 }
2009
Joe Onoratocb109a02011-01-18 17:57:41 -08002010 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002011 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002012 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002013 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002014 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002015 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002016 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002017 return this;
2018 }
2019
Joe Onoratocb109a02011-01-18 17:57:41 -08002020 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002021 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002022 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002023 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002024 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002025 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002026 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002027 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002028 return this;
2029 }
2030
Joe Onoratocb109a02011-01-18 17:57:41 -08002031 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002032 * Add a large icon to the notification (and the ticker on some devices).
2033 *
2034 * In the platform template, this image will be shown on the left of the notification view
2035 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
2036 *
2037 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08002038 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002039 public Builder setLargeIcon(Bitmap icon) {
2040 mLargeIcon = icon;
2041 return this;
2042 }
2043
Joe Onoratocb109a02011-01-18 17:57:41 -08002044 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002045 * Set the sound to play.
2046 *
John Spurlockc0650f022014-07-19 13:22:39 -04002047 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2048 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002049 *
Chris Wren47c20a12014-06-18 17:27:29 -04002050 * <p>
2051 * A notification that is noisy is more likely to be presented as a heads-up notification.
2052 * </p>
2053 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002054 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002055 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002056 public Builder setSound(Uri sound) {
2057 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002058 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002059 return this;
2060 }
2061
Joe Onoratocb109a02011-01-18 17:57:41 -08002062 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002063 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002064 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002065 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2066 *
Chris Wren47c20a12014-06-18 17:27:29 -04002067 * <p>
2068 * A notification that is noisy is more likely to be presented as a heads-up notification.
2069 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002070 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002071 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002072 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002073 public Builder setSound(Uri sound, int streamType) {
2074 mSound = sound;
2075 mAudioStreamType = streamType;
2076 return this;
2077 }
2078
Joe Onoratocb109a02011-01-18 17:57:41 -08002079 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002080 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2081 * use during playback.
2082 *
2083 * <p>
2084 * A notification that is noisy is more likely to be presented as a heads-up notification.
2085 * </p>
2086 *
2087 * @see Notification#sound
2088 */
2089 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2090 mSound = sound;
2091 mAudioAttributes = audioAttributes;
2092 return this;
2093 }
2094
2095 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002096 * Set the vibration pattern to use.
2097 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002098 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2099 * <code>pattern</code> parameter.
2100 *
Chris Wren47c20a12014-06-18 17:27:29 -04002101 * <p>
2102 * A notification that vibrates is more likely to be presented as a heads-up notification.
2103 * </p>
2104 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002105 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002106 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002107 public Builder setVibrate(long[] pattern) {
2108 mVibrate = pattern;
2109 return this;
2110 }
2111
Joe Onoratocb109a02011-01-18 17:57:41 -08002112 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002113 * Set the desired color for the indicator LED on the device, as well as the
2114 * blink duty cycle (specified in milliseconds).
2115 *
2116
2117 * Not all devices will honor all (or even any) of these values.
2118 *
2119
2120 * @see Notification#ledARGB
2121 * @see Notification#ledOnMS
2122 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002123 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002124 public Builder setLights(int argb, int onMs, int offMs) {
2125 mLedArgb = argb;
2126 mLedOnMs = onMs;
2127 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002128 return this;
2129 }
2130
Joe Onoratocb109a02011-01-18 17:57:41 -08002131 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002132 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002133 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002134
2135 * Ongoing notifications cannot be dismissed by the user, so your application or service
2136 * must take care of canceling them.
2137 *
2138
2139 * They are typically used to indicate a background task that the user is actively engaged
2140 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2141 * (e.g., a file download, sync operation, active network connection).
2142 *
2143
2144 * @see Notification#FLAG_ONGOING_EVENT
2145 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002146 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002147 public Builder setOngoing(boolean ongoing) {
2148 setFlag(FLAG_ONGOING_EVENT, ongoing);
2149 return this;
2150 }
2151
Joe Onoratocb109a02011-01-18 17:57:41 -08002152 /**
2153 * Set this flag if you would only like the sound, vibrate
2154 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002155 *
2156 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002157 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002158 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2159 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2160 return this;
2161 }
2162
Joe Onoratocb109a02011-01-18 17:57:41 -08002163 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002164 * Make this notification automatically dismissed when the user touches it. The
2165 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2166 *
2167 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002168 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002169 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002170 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002171 return this;
2172 }
2173
Joe Onoratocb109a02011-01-18 17:57:41 -08002174 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002175 * Set whether or not this notification should not bridge to other devices.
2176 *
2177 * <p>Some notifications can be bridged to other devices for remote display.
2178 * This hint can be set to recommend this notification not be bridged.
2179 */
2180 public Builder setLocalOnly(boolean localOnly) {
2181 setFlag(FLAG_LOCAL_ONLY, localOnly);
2182 return this;
2183 }
2184
2185 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002186 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002187 * <p>
2188 * The value should be one or more of the following fields combined with
2189 * bitwise-or:
2190 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2191 * <p>
2192 * For all default values, use {@link #DEFAULT_ALL}.
2193 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002194 public Builder setDefaults(int defaults) {
2195 mDefaults = defaults;
2196 return this;
2197 }
2198
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002199 /**
2200 * Set the priority of this notification.
2201 *
2202 * @see Notification#priority
2203 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002204 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002205 mPriority = pri;
2206 return this;
2207 }
Joe Malin8d40d042012-11-05 11:36:40 -08002208
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002209 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002210 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002211 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002212 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002213 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002214 public Builder setCategory(String category) {
2215 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002216 return this;
2217 }
2218
2219 /**
Chris Wrendde75302014-03-26 17:24:15 -04002220 * Add a person that is relevant to this notification.
2221 *
2222 * @see Notification#EXTRA_PEOPLE
2223 */
2224 public Builder addPerson(String handle) {
2225 mPeople.add(handle);
2226 return this;
2227 }
2228
2229 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002230 * Set this notification to be part of a group of notifications sharing the same key.
2231 * Grouped notifications may display in a cluster or stack on devices which
2232 * support such rendering.
2233 *
2234 * <p>To make this notification the summary for its group, also call
2235 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2236 * {@link #setSortKey}.
2237 * @param groupKey The group key of the group.
2238 * @return this object for method chaining
2239 */
2240 public Builder setGroup(String groupKey) {
2241 mGroupKey = groupKey;
2242 return this;
2243 }
2244
2245 /**
2246 * Set this notification to be the group summary for a group of notifications.
2247 * Grouped notifications may display in a cluster or stack on devices which
2248 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2249 * @param isGroupSummary Whether this notification should be a group summary.
2250 * @return this object for method chaining
2251 */
2252 public Builder setGroupSummary(boolean isGroupSummary) {
2253 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2254 return this;
2255 }
2256
2257 /**
2258 * Set a sort key that orders this notification among other notifications from the
2259 * same package. This can be useful if an external sort was already applied and an app
2260 * would like to preserve this. Notifications will be sorted lexicographically using this
2261 * value, although providing different priorities in addition to providing sort key may
2262 * cause this value to be ignored.
2263 *
2264 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002265 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002266 *
2267 * @see String#compareTo(String)
2268 */
2269 public Builder setSortKey(String sortKey) {
2270 mSortKey = sortKey;
2271 return this;
2272 }
2273
2274 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002275 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002276 *
Griff Hazen720042b2014-02-24 15:46:56 -08002277 * <p>Values within the Bundle will replace existing extras values in this Builder.
2278 *
2279 * @see Notification#extras
2280 */
Griff Hazen959591e2014-05-15 22:26:18 -07002281 public Builder addExtras(Bundle extras) {
2282 if (extras != null) {
2283 if (mExtras == null) {
2284 mExtras = new Bundle(extras);
2285 } else {
2286 mExtras.putAll(extras);
2287 }
Griff Hazen720042b2014-02-24 15:46:56 -08002288 }
2289 return this;
2290 }
2291
2292 /**
2293 * Set metadata for this notification.
2294 *
2295 * <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 -04002296 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002297 * called.
2298 *
Griff Hazen720042b2014-02-24 15:46:56 -08002299 * <p>Replaces any existing extras values with those from the provided Bundle.
2300 * Use {@link #addExtras} to merge in metadata instead.
2301 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002302 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002303 */
Griff Hazen959591e2014-05-15 22:26:18 -07002304 public Builder setExtras(Bundle extras) {
2305 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002306 return this;
2307 }
2308
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002309 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002310 * Get the current metadata Bundle used by this notification Builder.
2311 *
2312 * <p>The returned Bundle is shared with this Builder.
2313 *
2314 * <p>The current contents of this Bundle are copied into the Notification each time
2315 * {@link #build()} is called.
2316 *
2317 * @see Notification#extras
2318 */
2319 public Bundle getExtras() {
2320 if (mExtras == null) {
2321 mExtras = new Bundle();
2322 }
2323 return mExtras;
2324 }
2325
2326 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002327 * Add an action to this notification. Actions are typically displayed by
2328 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002329 * <p>
2330 * Every action must have an icon (32dp square and matching the
2331 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2332 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2333 * <p>
2334 * A notification in its expanded form can display up to 3 actions, from left to right in
2335 * the order they were added. Actions will not be displayed when the notification is
2336 * collapsed, however, so be sure that any essential functions may be accessed by the user
2337 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002338 *
2339 * @param icon Resource ID of a drawable that represents the action.
2340 * @param title Text describing the action.
2341 * @param intent PendingIntent to be fired when the action is invoked.
2342 */
2343 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002344 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002345 return this;
2346 }
2347
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002348 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002349 * Add an action to this notification. Actions are typically displayed by
2350 * the system as a button adjacent to the notification content.
2351 * <p>
2352 * Every action must have an icon (32dp square and matching the
2353 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2354 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2355 * <p>
2356 * A notification in its expanded form can display up to 3 actions, from left to right in
2357 * the order they were added. Actions will not be displayed when the notification is
2358 * collapsed, however, so be sure that any essential functions may be accessed by the user
2359 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2360 *
2361 * @param action The action to add.
2362 */
2363 public Builder addAction(Action action) {
2364 mActions.add(action);
2365 return this;
2366 }
2367
2368 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002369 * Add a rich notification style to be applied at build time.
2370 *
2371 * @param style Object responsible for modifying the notification style.
2372 */
2373 public Builder setStyle(Style style) {
2374 if (mStyle != style) {
2375 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002376 if (mStyle != null) {
2377 mStyle.setBuilder(this);
2378 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002379 }
2380 return this;
2381 }
2382
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002383 /**
2384 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002385 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002386 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2387 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2388 *
2389 * @return The same Builder.
2390 */
2391 public Builder setVisibility(int visibility) {
2392 mVisibility = visibility;
2393 return this;
2394 }
2395
2396 /**
2397 * Supply a replacement Notification whose contents should be shown in insecure contexts
2398 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2399 * @param n A replacement notification, presumably with some or all info redacted.
2400 * @return The same Builder.
2401 */
2402 public Builder setPublicVersion(Notification n) {
2403 mPublicVersion = n;
2404 return this;
2405 }
2406
Griff Hazenb720abe2014-05-20 13:15:30 -07002407 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002408 * Apply an extender to this notification builder. Extenders may be used to add
2409 * metadata or change options on this builder.
2410 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002411 public Builder extend(Extender extender) {
2412 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002413 return this;
2414 }
2415
Joe Onorato46439ce2010-11-19 13:56:21 -08002416 private void setFlag(int mask, boolean value) {
2417 if (value) {
2418 mFlags |= mask;
2419 } else {
2420 mFlags &= ~mask;
2421 }
2422 }
2423
Dan Sandler26e81cf2014-05-06 10:01:27 -04002424 /**
2425 * Sets {@link Notification#color}.
2426 *
2427 * @param argb The accent color to use
2428 *
2429 * @return The same Builder.
2430 */
2431 public Builder setColor(int argb) {
2432 mColor = argb;
2433 return this;
2434 }
2435
Kenny Guy8a0101b2014-05-08 23:34:12 +01002436 private Bitmap getProfileBadge() {
2437 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
2438 Drawable badge = userManager.getBadgeForUser(android.os.Process.myUserHandle());
2439 if (badge == null) {
2440 return null;
2441 }
2442 final int width = badge.getIntrinsicWidth();
2443 final int height = badge.getIntrinsicHeight();
2444 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2445 Canvas canvas = new Canvas(bitmap);
2446 badge.setBounds(0, 0, width, height);
2447 badge.draw(canvas);
2448 return bitmap;
2449 }
2450
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002451 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Kenny Guy8a0101b2014-05-08 23:34:12 +01002452 Bitmap profileIcon = getProfileBadge();
Joe Onorato561d3852010-11-20 18:09:34 -08002453 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002454 boolean showLine3 = false;
2455 boolean showLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002456
Dan Sandler26e81cf2014-05-06 10:01:27 -04002457 if (mPriority < PRIORITY_LOW) {
2458 // TODO: Low priority presentation
Daniel Sandlere95658c2012-05-10 00:33:54 -04002459 }
Kenny Guy8a0101b2014-05-08 23:34:12 +01002460 if (profileIcon != null) {
2461 contentView.setImageViewBitmap(R.id.profile_icon, profileIcon);
2462 contentView.setViewVisibility(R.id.profile_icon, View.VISIBLE);
2463 } else {
2464 contentView.setViewVisibility(R.id.profile_icon, View.GONE);
2465 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002466 if (mLargeIcon != null) {
2467 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002468 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002469 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2470 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2471 processSmallRightIcon(mSmallIcon, contentView);
2472 } else { // small icon at left
2473 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2474 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2475 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002476 }
2477 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002478 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002479 }
2480 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002481 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002482 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002483 }
2484 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002485 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002486 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002487 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002488 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002489 final int tooBig = mContext.getResources().getInteger(
2490 R.integer.status_bar_notification_info_maxnum);
2491 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002492 contentView.setTextViewText(R.id.info, processLegacyText(
2493 mContext.getResources().getString(
2494 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002495 } else {
2496 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002497 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002498 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002499 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002500 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002501 } else {
2502 contentView.setViewVisibility(R.id.info, View.GONE);
2503 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002504
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002505 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002506 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002507 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002508 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002509 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002510 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2511 showLine2 = true;
2512 } else {
2513 contentView.setViewVisibility(R.id.text2, View.GONE);
2514 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002515 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002516 contentView.setViewVisibility(R.id.text2, View.GONE);
2517 if (mProgressMax != 0 || mProgressIndeterminate) {
2518 contentView.setProgressBar(
2519 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2520 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002521 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002522 } else {
2523 contentView.setViewVisibility(R.id.progress, View.GONE);
2524 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002525 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002526 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002527 if (fitIn1U) {
2528 // need to shrink all the type to make sure everything fits
2529 final Resources res = mContext.getResources();
2530 final float subTextSize = res.getDimensionPixelSize(
2531 R.dimen.notification_subtext_size);
2532 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2533 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002534 // vertical centering
2535 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2536 }
2537
Daniel Sandler0c890492012-09-12 17:23:10 -07002538 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002539 if (mUseChronometer) {
2540 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2541 contentView.setLong(R.id.chronometer, "setBase",
2542 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2543 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2544 } else {
2545 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2546 contentView.setLong(R.id.time, "setTime", mWhen);
2547 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002548 } else {
2549 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002550 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002551
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002552 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002553 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002554 return contentView;
2555 }
2556
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002557 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002558 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002559
2560 int N = mActions.size();
2561 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002562 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002563 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002564 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002565 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002566 for (int i=0; i<N; i++) {
2567 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002568 big.addView(R.id.actions, button);
2569 }
2570 }
2571 return big;
2572 }
2573
Joe Onorato46439ce2010-11-19 13:56:21 -08002574 private RemoteViews makeContentView() {
2575 if (mContentView != null) {
2576 return mContentView;
2577 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002578 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002579 }
2580 }
2581
2582 private RemoteViews makeTickerView() {
2583 if (mTickerView != null) {
2584 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002585 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002586 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08002587 }
2588
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002589 private RemoteViews makeBigContentView() {
2590 if (mActions.size() == 0) return null;
2591
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002592 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002593 }
2594
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002595 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002596 if (mActions.size() == 0) return null;
2597
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002598 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002599 }
2600
2601
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002602 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002603 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002604 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002605 tombstone ? getActionTombstoneLayoutResource()
2606 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002607 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002608 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002609 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002610 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002611 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002612 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002613 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002614 return button;
2615 }
2616
Joe Onoratocb109a02011-01-18 17:57:41 -08002617 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002618 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002619 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002620 */
2621 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002622 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002623 }
2624
2625 private void processLegacyAction(Action action, RemoteViews button) {
2626 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002627 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002628 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2629 mContext.getResources().getColor(
2630 R.color.notification_action_legacy_color_filter),
2631 PorterDuff.Mode.MULTIPLY);
2632 }
2633 }
2634 }
2635
2636 private CharSequence processLegacyText(CharSequence charSequence) {
2637 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002638 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002639 } else {
2640 return charSequence;
2641 }
2642 }
2643
Dan Sandler26e81cf2014-05-06 10:01:27 -04002644 /**
2645 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2646 */
2647 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2648 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2649 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002650 }
2651 }
2652
Dan Sandler26e81cf2014-05-06 10:01:27 -04002653 /**
2654 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2655 * if it's grayscale).
2656 */
2657 // TODO: also check bounds, transparency, that sort of thing.
2658 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2659 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2660 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002661 } else {
2662 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002663 }
2664 }
2665
Dan Sandler26e81cf2014-05-06 10:01:27 -04002666 /**
2667 * Add a colored circle behind the largeIcon slot.
2668 */
2669 private void applyLargeIconBackground(RemoteViews contentView) {
2670 contentView.setInt(R.id.icon, "setBackgroundResource",
2671 R.drawable.notification_icon_legacy_bg_inset);
2672
2673 contentView.setDrawableParameters(
2674 R.id.icon,
2675 true,
2676 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002677 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002678 PorterDuff.Mode.SRC_ATOP,
2679 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002680 }
2681
Dan Sandler190d58d2014-05-15 09:33:39 -04002682 private void removeLargeIconBackground(RemoteViews contentView) {
2683 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2684 }
2685
Dan Sandler26e81cf2014-05-06 10:01:27 -04002686 /**
2687 * Recolor small icons when used in the R.id.right_icon slot.
2688 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002689 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002690 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002691 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002692 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2693 0xFFFFFFFF,
2694 PorterDuff.Mode.SRC_ATOP, -1);
2695
2696 contentView.setInt(R.id.right_icon,
2697 "setBackgroundResource",
2698 R.drawable.notification_icon_legacy_bg);
2699
2700 contentView.setDrawableParameters(
2701 R.id.right_icon,
2702 true,
2703 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002704 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04002705 PorterDuff.Mode.SRC_ATOP,
2706 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002707 }
2708 }
2709
Jorim Jaggi74419312014-06-10 20:57:21 +02002710 private int sanitizeColor() {
2711 if (mColor != COLOR_DEFAULT) {
2712 mColor |= 0xFF000000; // no alpha for custom colors
2713 }
2714 return mColor;
2715 }
2716
Dan Sandler26e81cf2014-05-06 10:01:27 -04002717 private int resolveColor() {
2718 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02002719 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002720 }
2721 return mColor;
2722 }
2723
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002724 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002725 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002726 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002727 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002728 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002729 Notification n = new Notification();
2730 n.when = mWhen;
2731 n.icon = mSmallIcon;
2732 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002733 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002734
Jorim Jaggi74419312014-06-10 20:57:21 +02002735 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002736
Joe Onorato46439ce2010-11-19 13:56:21 -08002737 n.contentView = makeContentView();
2738 n.contentIntent = mContentIntent;
2739 n.deleteIntent = mDeleteIntent;
2740 n.fullScreenIntent = mFullScreenIntent;
2741 n.tickerText = mTickerText;
2742 n.tickerView = makeTickerView();
2743 n.largeIcon = mLargeIcon;
2744 n.sound = mSound;
2745 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002746 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002747 n.vibrate = mVibrate;
2748 n.ledARGB = mLedArgb;
2749 n.ledOnMS = mLedOnMs;
2750 n.ledOffMS = mLedOffMs;
2751 n.defaults = mDefaults;
2752 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002753 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002754 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002755 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002756 n.flags |= FLAG_SHOW_LIGHTS;
2757 }
2758 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2759 n.flags |= FLAG_SHOW_LIGHTS;
2760 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002761 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002762 n.mGroupKey = mGroupKey;
2763 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002764 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002765 if (mActions.size() > 0) {
2766 n.actions = new Action[mActions.size()];
2767 mActions.toArray(n.actions);
2768 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002769 n.visibility = mVisibility;
2770
2771 if (mPublicVersion != null) {
2772 n.publicVersion = new Notification();
2773 mPublicVersion.cloneInto(n.publicVersion, true);
2774 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002775
Joe Onorato46439ce2010-11-19 13:56:21 -08002776 return n;
2777 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002778
2779 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002780 * Capture, in the provided bundle, semantic information used in the construction of
2781 * this Notification object.
2782 * @hide
2783 */
Griff Hazen720042b2014-02-24 15:46:56 -08002784 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002785 // Store original information used in the construction of this object
2786 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2787 extras.putCharSequence(EXTRA_TEXT, mContentText);
2788 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2789 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2790 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2791 extras.putInt(EXTRA_PROGRESS, mProgress);
2792 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2793 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2794 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2795 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002796 if (mLargeIcon != null) {
2797 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2798 }
Chris Wrendde75302014-03-26 17:24:15 -04002799 if (!mPeople.isEmpty()) {
2800 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2801 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002802 }
2803
2804 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002805 * @deprecated Use {@link #build()} instead.
2806 */
2807 @Deprecated
2808 public Notification getNotification() {
2809 return build();
2810 }
2811
2812 /**
2813 * Combine all of the options that have been set and return a new {@link Notification}
2814 * object.
2815 */
2816 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002817 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002818
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002819 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002820 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002821 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002822
2823 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2824
Griff Hazen720042b2014-02-24 15:46:56 -08002825 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002826 if (mStyle != null) {
2827 mStyle.addExtras(n.extras);
2828 }
2829
2830 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002831 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002832
2833 /**
2834 * Apply this Builder to an existing {@link Notification} object.
2835 *
2836 * @hide
2837 */
2838 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002839 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002840 return n;
2841 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002842
2843
2844 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002845 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002846 }
2847
2848 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002849 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002850 }
2851
2852 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002853 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002854 }
2855
2856 private int getBigTextLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002857 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002858 }
2859
2860 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002861 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002862 }
2863
2864 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002865 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002866 }
2867
2868 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07002869 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002870 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002871 }
2872
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002873 /**
2874 * An object that can apply a rich notification style to a {@link Notification.Builder}
2875 * object.
2876 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002877 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002878 private CharSequence mBigContentTitle;
2879 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002880 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002881
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002882 protected Builder mBuilder;
2883
Chris Wrend6297db2012-05-03 16:20:13 -04002884 /**
2885 * Overrides ContentTitle in the big form of the template.
2886 * This defaults to the value passed to setContentTitle().
2887 */
2888 protected void internalSetBigContentTitle(CharSequence title) {
2889 mBigContentTitle = title;
2890 }
2891
2892 /**
2893 * Set the first line of text after the detail section in the big form of the template.
2894 */
2895 protected void internalSetSummaryText(CharSequence cs) {
2896 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002897 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002898 }
2899
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002900 public void setBuilder(Builder builder) {
2901 if (mBuilder != builder) {
2902 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002903 if (mBuilder != null) {
2904 mBuilder.setStyle(this);
2905 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002906 }
2907 }
2908
Chris Wrend6297db2012-05-03 16:20:13 -04002909 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002910 if (mBuilder == null) {
2911 throw new IllegalArgumentException("Style requires a valid Builder object");
2912 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002913 }
Chris Wrend6297db2012-05-03 16:20:13 -04002914
2915 protected RemoteViews getStandardView(int layoutId) {
2916 checkBuilder();
2917
2918 if (mBigContentTitle != null) {
2919 mBuilder.setContentTitle(mBigContentTitle);
2920 }
2921
Chris Wrend6297db2012-05-03 16:20:13 -04002922 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2923
Chris Wrend6297db2012-05-03 16:20:13 -04002924 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2925 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002926 } else {
2927 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002928 }
2929
Daniel Sandler619738c2012-06-07 16:33:08 -04002930 // The last line defaults to the subtext, but can be replaced by mSummaryText
2931 final CharSequence overflowText =
2932 mSummaryTextSet ? mSummaryText
2933 : mBuilder.mSubText;
2934 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002935 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04002936 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002937 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002938 } else {
2939 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2940 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002941 }
2942
2943 return contentView;
2944 }
2945
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002946 /**
2947 * @hide
2948 */
2949 public void addExtras(Bundle extras) {
2950 if (mSummaryTextSet) {
2951 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2952 }
2953 if (mBigContentTitle != null) {
2954 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2955 }
Chris Wren91ad5632013-06-05 15:05:57 -04002956 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002957 }
2958
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002959 /**
2960 * @hide
2961 */
2962 public abstract Notification buildStyled(Notification wip);
2963
2964 /**
2965 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2966 * attached to.
2967 *
2968 * @return the fully constructed Notification.
2969 */
2970 public Notification build() {
2971 checkBuilder();
2972 return mBuilder.build();
2973 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002974 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002975
2976 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002977 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002978 *
Robert Ly91c5ce32014-06-08 15:37:00 -07002979 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002980 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07002981 * Notification notif = new Notification.Builder(mContext)
2982 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
2983 * .setContentText(subject)
2984 * .setSmallIcon(R.drawable.new_post)
2985 * .setLargeIcon(aBitmap)
2986 * .setStyle(new Notification.BigPictureStyle()
2987 * .bigPicture(aBigBitmap))
2988 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002989 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002990 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002991 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002992 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002993 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002994 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002995 private Bitmap mBigLargeIcon;
2996 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002997
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002998 public BigPictureStyle() {
2999 }
3000
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003001 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003002 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003003 }
3004
Chris Wrend6297db2012-05-03 16:20:13 -04003005 /**
3006 * Overrides ContentTitle in the big form of the template.
3007 * This defaults to the value passed to setContentTitle().
3008 */
3009 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003010 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003011 return this;
3012 }
3013
3014 /**
3015 * Set the first line of text after the detail section in the big form of the template.
3016 */
3017 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003018 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003019 return this;
3020 }
3021
Chris Wren0bd664d2012-08-01 13:56:56 -04003022 /**
3023 * Provide the bitmap to be used as the payload for the BigPicture notification.
3024 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003025 public BigPictureStyle bigPicture(Bitmap b) {
3026 mPicture = b;
3027 return this;
3028 }
3029
Chris Wren3745a3d2012-05-22 15:11:52 -04003030 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003031 * Override the large icon when the big notification is shown.
3032 */
3033 public BigPictureStyle bigLargeIcon(Bitmap b) {
3034 mBigLargeIconSet = true;
3035 mBigLargeIcon = b;
3036 return this;
3037 }
3038
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003039 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003040 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003041
3042 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3043
3044 return contentView;
3045 }
3046
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003047 /**
3048 * @hide
3049 */
3050 public void addExtras(Bundle extras) {
3051 super.addExtras(extras);
3052
3053 if (mBigLargeIconSet) {
3054 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3055 }
3056 extras.putParcelable(EXTRA_PICTURE, mPicture);
3057 }
3058
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003059 /**
3060 * @hide
3061 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003062 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003063 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003064 if (mBigLargeIconSet ) {
3065 mBuilder.mLargeIcon = mBigLargeIcon;
3066 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003067 wip.bigContentView = makeBigContentView();
3068 return wip;
3069 }
3070 }
3071
3072 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003073 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003074 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003075 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003076 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003077 * Notification notif = new Notification.Builder(mContext)
3078 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3079 * .setContentText(subject)
3080 * .setSmallIcon(R.drawable.new_mail)
3081 * .setLargeIcon(aBitmap)
3082 * .setStyle(new Notification.BigTextStyle()
3083 * .bigText(aVeryLongString))
3084 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003085 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003086 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003087 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003088 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003089 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003090 private CharSequence mBigText;
3091
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003092 public BigTextStyle() {
3093 }
3094
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003095 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003096 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003097 }
3098
Chris Wrend6297db2012-05-03 16:20:13 -04003099 /**
3100 * Overrides ContentTitle in the big form of the template.
3101 * This defaults to the value passed to setContentTitle().
3102 */
3103 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003104 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003105 return this;
3106 }
3107
3108 /**
3109 * Set the first line of text after the detail section in the big form of the template.
3110 */
3111 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003112 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003113 return this;
3114 }
3115
Chris Wren0bd664d2012-08-01 13:56:56 -04003116 /**
3117 * Provide the longer text to be displayed in the big form of the
3118 * template in place of the content text.
3119 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003120 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003121 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003122 return this;
3123 }
3124
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003125 /**
3126 * @hide
3127 */
3128 public void addExtras(Bundle extras) {
3129 super.addExtras(extras);
3130
3131 extras.putCharSequence(EXTRA_TEXT, mBigText);
3132 }
3133
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003134 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003135 // Remove the content text so line3 only shows if you have a summary
3136 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003137 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003138
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003139 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003140
Daniel Sandler619738c2012-06-07 16:33:08 -04003141 if (hadThreeLines) {
3142 // vertical centering
3143 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
3144 }
3145
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003146 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003147 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003148 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003149
3150 return contentView;
3151 }
3152
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003153 /**
3154 * @hide
3155 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003156 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003157 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003158 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003159
3160 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
3161
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003162 return wip;
3163 }
3164 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003165
3166 /**
3167 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003168 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003169 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003170 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003171 * Notification notif = new Notification.Builder(mContext)
3172 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3173 * .setContentText(subject)
3174 * .setSmallIcon(R.drawable.new_mail)
3175 * .setLargeIcon(aBitmap)
3176 * .setStyle(new Notification.InboxStyle()
3177 * .addLine(str1)
3178 * .addLine(str2)
3179 * .setContentTitle(&quot;&quot;)
3180 * .setSummaryText(&quot;+3 more&quot;))
3181 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003182 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003183 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003184 * @see Notification#bigContentView
3185 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003186 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003187 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3188
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003189 public InboxStyle() {
3190 }
3191
Daniel Sandler879c5e02012-04-17 16:46:51 -04003192 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003193 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003194 }
3195
Chris Wrend6297db2012-05-03 16:20:13 -04003196 /**
3197 * Overrides ContentTitle in the big form of the template.
3198 * This defaults to the value passed to setContentTitle().
3199 */
3200 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003201 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003202 return this;
3203 }
3204
3205 /**
3206 * Set the first line of text after the detail section in the big form of the template.
3207 */
3208 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003209 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003210 return this;
3211 }
3212
Chris Wren0bd664d2012-08-01 13:56:56 -04003213 /**
3214 * Append a line to the digest section of the Inbox notification.
3215 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003216 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003217 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003218 return this;
3219 }
3220
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003221 /**
3222 * @hide
3223 */
3224 public void addExtras(Bundle extras) {
3225 super.addExtras(extras);
3226 CharSequence[] a = new CharSequence[mTexts.size()];
3227 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3228 }
3229
Daniel Sandler879c5e02012-04-17 16:46:51 -04003230 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003231 // Remove the content text so line3 disappears unless you have a summary
3232 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003233 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003234
Chris Wrend6297db2012-05-03 16:20:13 -04003235 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003236
Chris Wrend6297db2012-05-03 16:20:13 -04003237 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 -04003238 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003239
Chris Wren4ed80d52012-05-17 09:30:03 -04003240 // Make sure all rows are gone in case we reuse a view.
3241 for (int rowId : rowIds) {
3242 contentView.setViewVisibility(rowId, View.GONE);
3243 }
3244
Chris Wren683ab002012-09-20 10:35:54 -04003245
Daniel Sandler879c5e02012-04-17 16:46:51 -04003246 int i=0;
3247 while (i < mTexts.size() && i < rowIds.length) {
3248 CharSequence str = mTexts.get(i);
3249 if (str != null && !str.equals("")) {
3250 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003251 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003252 }
3253 i++;
3254 }
3255
Chris Wren683ab002012-09-20 10:35:54 -04003256 contentView.setViewVisibility(R.id.inbox_end_pad,
3257 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3258
3259 contentView.setViewVisibility(R.id.inbox_more,
3260 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003261
Daniel Sandler879c5e02012-04-17 16:46:51 -04003262 return contentView;
3263 }
3264
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003265 /**
3266 * @hide
3267 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003268 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003269 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003270 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003271
Daniel Sandler879c5e02012-04-17 16:46:51 -04003272 return wip;
3273 }
3274 }
Dan Sandler842dd772014-05-15 09:36:47 -04003275
3276 /**
3277 * Notification style for media playback notifications.
3278 *
3279 * In the expanded form, {@link Notification#bigContentView}, up to 5
3280 * {@link Notification.Action}s specified with
3281 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3282 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3283 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3284 * treated as album artwork.
3285 *
3286 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3287 * {@link Notification#contentView}; by providing action indices to
3288 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3289 * in the standard view alongside the usual content.
3290 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003291 * Notifications created with MediaStyle will have their category set to
3292 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
3293 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
3294 *
Jeff Browndba34ba2014-06-24 20:46:03 -07003295 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
3296 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04003297 * the System UI can identify this as a notification representing an active media session
3298 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3299 *
3300 * To use this style with your Notification, feed it to
3301 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3302 * <pre class="prettyprint">
3303 * Notification noti = new Notification.Builder()
3304 * .setSmallIcon(R.drawable.ic_stat_player)
3305 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3306 * .setContentText(&quot;Artist - Album&quot;) // if you use
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003307 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token)
3308 * .setStyle(<b>new Notification.MediaStyle()</b>
3309 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04003310 * .build();
3311 * </pre>
3312 *
3313 * @see Notification#bigContentView
3314 */
3315 public static class MediaStyle extends Style {
3316 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3317 static final int MAX_MEDIA_BUTTONS = 5;
3318
3319 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07003320 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04003321
3322 public MediaStyle() {
3323 }
3324
3325 public MediaStyle(Builder builder) {
3326 setBuilder(builder);
3327 }
3328
3329 /**
3330 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3331 * notification view.
3332 */
3333 public MediaStyle setShowActionsInCompactView(int...actions) {
3334 mActionsToShowInCompact = actions;
3335 return this;
3336 }
3337
3338 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07003339 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
3340 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04003341 */
Jeff Browndba34ba2014-06-24 20:46:03 -07003342 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04003343 mToken = token;
3344 return this;
3345 }
3346
3347 @Override
3348 public Notification buildStyled(Notification wip) {
3349 wip.contentView = makeMediaContentView();
3350 wip.bigContentView = makeMediaBigContentView();
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003351 if (wip.category == null) {
3352 wip.category = Notification.CATEGORY_TRANSPORT;
3353 }
Dan Sandler842dd772014-05-15 09:36:47 -04003354 return wip;
3355 }
3356
3357 /** @hide */
3358 @Override
3359 public void addExtras(Bundle extras) {
3360 super.addExtras(extras);
3361
3362 if (mToken != null) {
3363 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3364 }
3365 }
3366
3367 private RemoteViews generateMediaActionButton(Action action) {
3368 final boolean tombstone = (action.actionIntent == null);
3369 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07003370 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04003371 button.setImageViewResource(R.id.action0, action.icon);
3372 if (!tombstone) {
3373 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3374 }
3375 button.setContentDescription(R.id.action0, action.title);
3376 return button;
3377 }
3378
3379 private RemoteViews makeMediaContentView() {
3380 RemoteViews view = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003381 R.layout.notification_template_material_media, true /* 1U */);
Dan Sandler842dd772014-05-15 09:36:47 -04003382
3383 final int numActions = mBuilder.mActions.size();
3384 final int N = mActionsToShowInCompact == null
3385 ? 0
3386 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3387 if (N > 0) {
3388 view.removeAllViews(R.id.actions);
3389 for (int i = 0; i < N; i++) {
3390 if (i >= numActions) {
3391 throw new IllegalArgumentException(String.format(
3392 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3393 i, numActions - 1));
3394 }
3395
3396 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3397 final RemoteViews button = generateMediaActionButton(action);
3398 view.addView(R.id.actions, button);
3399 }
3400 }
3401 return view;
3402 }
3403
3404 private RemoteViews makeMediaBigContentView() {
3405 RemoteViews big = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003406 R.layout.notification_template_material_big_media, false);
Dan Sandler842dd772014-05-15 09:36:47 -04003407
3408 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3409 if (N > 0) {
3410 big.removeAllViews(R.id.actions);
3411 for (int i=0; i<N; i++) {
3412 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3413 big.addView(R.id.actions, button);
3414 }
3415 }
3416 return big;
3417 }
3418 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003419
3420 /**
3421 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
3422 * metadata or change options on a notification builder.
3423 */
3424 public interface Extender {
3425 /**
3426 * Apply this extender to a notification builder.
3427 * @param builder the builder to be modified.
3428 * @return the build object for chaining.
3429 */
3430 public Builder extend(Builder builder);
3431 }
3432
3433 /**
3434 * Helper class to add wearable extensions to notifications.
3435 * <p class="note"> See
3436 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
3437 * for Android Wear</a> for more information on how to use this class.
3438 * <p>
3439 * To create a notification with wearable extensions:
3440 * <ol>
3441 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
3442 * properties.
3443 * <li>Create a {@link android.app.Notification.WearableExtender}.
3444 * <li>Set wearable-specific properties using the
3445 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
3446 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
3447 * notification.
3448 * <li>Post the notification to the notification system with the
3449 * {@code NotificationManager.notify(...)} methods.
3450 * </ol>
3451 *
3452 * <pre class="prettyprint">
3453 * Notification notif = new Notification.Builder(mContext)
3454 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3455 * .setContentText(subject)
3456 * .setSmallIcon(R.drawable.new_mail)
3457 * .extend(new Notification.WearableExtender()
3458 * .setContentIcon(R.drawable.new_mail))
3459 * .build();
3460 * NotificationManager notificationManger =
3461 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3462 * notificationManger.notify(0, notif);</pre>
3463 *
3464 * <p>Wearable extensions can be accessed on an existing notification by using the
3465 * {@code WearableExtender(Notification)} constructor,
3466 * and then using the {@code get} methods to access values.
3467 *
3468 * <pre class="prettyprint">
3469 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
3470 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07003471 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003472 */
3473 public static final class WearableExtender implements Extender {
3474 /**
3475 * Sentinel value for an action index that is unset.
3476 */
3477 public static final int UNSET_ACTION_INDEX = -1;
3478
3479 /**
3480 * Size value for use with {@link #setCustomSizePreset} to show this notification with
3481 * default sizing.
3482 * <p>For custom display notifications created using {@link #setDisplayIntent},
3483 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
3484 * on their content.
3485 */
3486 public static final int SIZE_DEFAULT = 0;
3487
3488 /**
3489 * Size value for use with {@link #setCustomSizePreset} to show this notification
3490 * with an extra small size.
3491 * <p>This value is only applicable for custom display notifications created using
3492 * {@link #setDisplayIntent}.
3493 */
3494 public static final int SIZE_XSMALL = 1;
3495
3496 /**
3497 * Size value for use with {@link #setCustomSizePreset} to show this notification
3498 * with a small size.
3499 * <p>This value is only applicable for custom display notifications created using
3500 * {@link #setDisplayIntent}.
3501 */
3502 public static final int SIZE_SMALL = 2;
3503
3504 /**
3505 * Size value for use with {@link #setCustomSizePreset} to show this notification
3506 * with a medium size.
3507 * <p>This value is only applicable for custom display notifications created using
3508 * {@link #setDisplayIntent}.
3509 */
3510 public static final int SIZE_MEDIUM = 3;
3511
3512 /**
3513 * Size value for use with {@link #setCustomSizePreset} to show this notification
3514 * with a large size.
3515 * <p>This value is only applicable for custom display notifications created using
3516 * {@link #setDisplayIntent}.
3517 */
3518 public static final int SIZE_LARGE = 4;
3519
Griff Hazend5f11f92014-05-27 15:40:09 -07003520 /**
3521 * Size value for use with {@link #setCustomSizePreset} to show this notification
3522 * full screen.
3523 * <p>This value is only applicable for custom display notifications created using
3524 * {@link #setDisplayIntent}.
3525 */
3526 public static final int SIZE_FULL_SCREEN = 5;
3527
Griff Hazen61a9e862014-05-22 16:05:19 -07003528 /** Notification extra which contains wearable extensions */
3529 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
3530
3531 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
3532 private static final String KEY_ACTIONS = "actions";
3533 private static final String KEY_FLAGS = "flags";
3534 private static final String KEY_DISPLAY_INTENT = "displayIntent";
3535 private static final String KEY_PAGES = "pages";
3536 private static final String KEY_BACKGROUND = "background";
3537 private static final String KEY_CONTENT_ICON = "contentIcon";
3538 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
3539 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
3540 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
3541 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
3542 private static final String KEY_GRAVITY = "gravity";
3543
3544 // Flags bitwise-ored to mFlags
3545 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
3546 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
3547 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
3548 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
3549
3550 // Default value for flags integer
3551 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
3552
3553 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
3554 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
3555
3556 private ArrayList<Action> mActions = new ArrayList<Action>();
3557 private int mFlags = DEFAULT_FLAGS;
3558 private PendingIntent mDisplayIntent;
3559 private ArrayList<Notification> mPages = new ArrayList<Notification>();
3560 private Bitmap mBackground;
3561 private int mContentIcon;
3562 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
3563 private int mContentActionIndex = UNSET_ACTION_INDEX;
3564 private int mCustomSizePreset = SIZE_DEFAULT;
3565 private int mCustomContentHeight;
3566 private int mGravity = DEFAULT_GRAVITY;
3567
3568 /**
3569 * Create a {@link android.app.Notification.WearableExtender} with default
3570 * options.
3571 */
3572 public WearableExtender() {
3573 }
3574
3575 public WearableExtender(Notification notif) {
3576 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
3577 if (wearableBundle != null) {
3578 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
3579 if (actions != null) {
3580 mActions.addAll(actions);
3581 }
3582
3583 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
3584 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
3585
3586 Notification[] pages = getNotificationArrayFromBundle(
3587 wearableBundle, KEY_PAGES);
3588 if (pages != null) {
3589 Collections.addAll(mPages, pages);
3590 }
3591
3592 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
3593 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
3594 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
3595 DEFAULT_CONTENT_ICON_GRAVITY);
3596 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
3597 UNSET_ACTION_INDEX);
3598 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
3599 SIZE_DEFAULT);
3600 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
3601 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
3602 }
3603 }
3604
3605 /**
3606 * Apply wearable extensions to a notification that is being built. This is typically
3607 * called by the {@link android.app.Notification.Builder#extend} method of
3608 * {@link android.app.Notification.Builder}.
3609 */
3610 @Override
3611 public Notification.Builder extend(Notification.Builder builder) {
3612 Bundle wearableBundle = new Bundle();
3613
3614 if (!mActions.isEmpty()) {
3615 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
3616 }
3617 if (mFlags != DEFAULT_FLAGS) {
3618 wearableBundle.putInt(KEY_FLAGS, mFlags);
3619 }
3620 if (mDisplayIntent != null) {
3621 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
3622 }
3623 if (!mPages.isEmpty()) {
3624 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
3625 new Notification[mPages.size()]));
3626 }
3627 if (mBackground != null) {
3628 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
3629 }
3630 if (mContentIcon != 0) {
3631 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
3632 }
3633 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
3634 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
3635 }
3636 if (mContentActionIndex != UNSET_ACTION_INDEX) {
3637 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
3638 mContentActionIndex);
3639 }
3640 if (mCustomSizePreset != SIZE_DEFAULT) {
3641 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
3642 }
3643 if (mCustomContentHeight != 0) {
3644 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
3645 }
3646 if (mGravity != DEFAULT_GRAVITY) {
3647 wearableBundle.putInt(KEY_GRAVITY, mGravity);
3648 }
3649
3650 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
3651 return builder;
3652 }
3653
3654 @Override
3655 public WearableExtender clone() {
3656 WearableExtender that = new WearableExtender();
3657 that.mActions = new ArrayList<Action>(this.mActions);
3658 that.mFlags = this.mFlags;
3659 that.mDisplayIntent = this.mDisplayIntent;
3660 that.mPages = new ArrayList<Notification>(this.mPages);
3661 that.mBackground = this.mBackground;
3662 that.mContentIcon = this.mContentIcon;
3663 that.mContentIconGravity = this.mContentIconGravity;
3664 that.mContentActionIndex = this.mContentActionIndex;
3665 that.mCustomSizePreset = this.mCustomSizePreset;
3666 that.mCustomContentHeight = this.mCustomContentHeight;
3667 that.mGravity = this.mGravity;
3668 return that;
3669 }
3670
3671 /**
3672 * Add a wearable action to this notification.
3673 *
3674 * <p>When wearable actions are added using this method, the set of actions that
3675 * show on a wearable device splits from devices that only show actions added
3676 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3677 * of which actions display on different devices.
3678 *
3679 * @param action the action to add to this notification
3680 * @return this object for method chaining
3681 * @see android.app.Notification.Action
3682 */
3683 public WearableExtender addAction(Action action) {
3684 mActions.add(action);
3685 return this;
3686 }
3687
3688 /**
3689 * Adds wearable actions to this notification.
3690 *
3691 * <p>When wearable actions are added using this method, the set of actions that
3692 * show on a wearable device splits from devices that only show actions added
3693 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3694 * of which actions display on different devices.
3695 *
3696 * @param actions the actions to add to this notification
3697 * @return this object for method chaining
3698 * @see android.app.Notification.Action
3699 */
3700 public WearableExtender addActions(List<Action> actions) {
3701 mActions.addAll(actions);
3702 return this;
3703 }
3704
3705 /**
3706 * Clear all wearable actions present on this builder.
3707 * @return this object for method chaining.
3708 * @see #addAction
3709 */
3710 public WearableExtender clearActions() {
3711 mActions.clear();
3712 return this;
3713 }
3714
3715 /**
3716 * Get the wearable actions present on this notification.
3717 */
3718 public List<Action> getActions() {
3719 return mActions;
3720 }
3721
3722 /**
3723 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07003724 * this notification. The {@link PendingIntent} provided should be for an activity.
3725 *
3726 * <pre class="prettyprint">
3727 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
3728 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
3729 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3730 * Notification notif = new Notification.Builder(context)
3731 * .extend(new Notification.WearableExtender()
3732 * .setDisplayIntent(displayPendingIntent)
3733 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
3734 * .build();</pre>
3735 *
3736 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07003737 * should have an empty task affinity. It is also recommended to use the device
3738 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07003739 *
3740 * <p>Example AndroidManifest.xml entry:
3741 * <pre class="prettyprint">
3742 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
3743 * android:exported=&quot;true&quot;
3744 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07003745 * android:taskAffinity=&quot;&quot;
3746 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003747 *
3748 * @param intent the {@link PendingIntent} for an activity
3749 * @return this object for method chaining
3750 * @see android.app.Notification.WearableExtender#getDisplayIntent
3751 */
3752 public WearableExtender setDisplayIntent(PendingIntent intent) {
3753 mDisplayIntent = intent;
3754 return this;
3755 }
3756
3757 /**
3758 * Get the intent to launch inside of an activity view when displaying this
3759 * notification. This {@code PendingIntent} should be for an activity.
3760 */
3761 public PendingIntent getDisplayIntent() {
3762 return mDisplayIntent;
3763 }
3764
3765 /**
3766 * Add an additional page of content to display with this notification. The current
3767 * notification forms the first page, and pages added using this function form
3768 * subsequent pages. This field can be used to separate a notification into multiple
3769 * sections.
3770 *
3771 * @param page the notification to add as another page
3772 * @return this object for method chaining
3773 * @see android.app.Notification.WearableExtender#getPages
3774 */
3775 public WearableExtender addPage(Notification page) {
3776 mPages.add(page);
3777 return this;
3778 }
3779
3780 /**
3781 * Add additional pages of content to display with this notification. The current
3782 * notification forms the first page, and pages added using this function form
3783 * subsequent pages. This field can be used to separate a notification into multiple
3784 * sections.
3785 *
3786 * @param pages a list of notifications
3787 * @return this object for method chaining
3788 * @see android.app.Notification.WearableExtender#getPages
3789 */
3790 public WearableExtender addPages(List<Notification> pages) {
3791 mPages.addAll(pages);
3792 return this;
3793 }
3794
3795 /**
3796 * Clear all additional pages present on this builder.
3797 * @return this object for method chaining.
3798 * @see #addPage
3799 */
3800 public WearableExtender clearPages() {
3801 mPages.clear();
3802 return this;
3803 }
3804
3805 /**
3806 * Get the array of additional pages of content for displaying this notification. The
3807 * current notification forms the first page, and elements within this array form
3808 * subsequent pages. This field can be used to separate a notification into multiple
3809 * sections.
3810 * @return the pages for this notification
3811 */
3812 public List<Notification> getPages() {
3813 return mPages;
3814 }
3815
3816 /**
3817 * Set a background image to be displayed behind the notification content.
3818 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3819 * will work with any notification style.
3820 *
3821 * @param background the background bitmap
3822 * @return this object for method chaining
3823 * @see android.app.Notification.WearableExtender#getBackground
3824 */
3825 public WearableExtender setBackground(Bitmap background) {
3826 mBackground = background;
3827 return this;
3828 }
3829
3830 /**
3831 * Get a background image to be displayed behind the notification content.
3832 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3833 * will work with any notification style.
3834 *
3835 * @return the background image
3836 * @see android.app.Notification.WearableExtender#setBackground
3837 */
3838 public Bitmap getBackground() {
3839 return mBackground;
3840 }
3841
3842 /**
3843 * Set an icon that goes with the content of this notification.
3844 */
3845 public WearableExtender setContentIcon(int icon) {
3846 mContentIcon = icon;
3847 return this;
3848 }
3849
3850 /**
3851 * Get an icon that goes with the content of this notification.
3852 */
3853 public int getContentIcon() {
3854 return mContentIcon;
3855 }
3856
3857 /**
3858 * Set the gravity that the content icon should have within the notification display.
3859 * Supported values include {@link android.view.Gravity#START} and
3860 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3861 * @see #setContentIcon
3862 */
3863 public WearableExtender setContentIconGravity(int contentIconGravity) {
3864 mContentIconGravity = contentIconGravity;
3865 return this;
3866 }
3867
3868 /**
3869 * Get the gravity that the content icon should have within the notification display.
3870 * Supported values include {@link android.view.Gravity#START} and
3871 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3872 * @see #getContentIcon
3873 */
3874 public int getContentIconGravity() {
3875 return mContentIconGravity;
3876 }
3877
3878 /**
3879 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07003880 * this notification. This action will no longer display separately from the
3881 * notification's content.
3882 *
Griff Hazenca48d352014-05-28 22:37:13 -07003883 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003884 * set, although the list of available actions comes from the main notification and not
3885 * from the child page's notification.
3886 *
3887 * @param actionIndex The index of the action to hoist onto the current notification page.
3888 * If wearable actions were added to the main notification, this index
3889 * will apply to that list, otherwise it will apply to the regular
3890 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07003891 */
3892 public WearableExtender setContentAction(int actionIndex) {
3893 mContentActionIndex = actionIndex;
3894 return this;
3895 }
3896
3897 /**
Griff Hazenca48d352014-05-28 22:37:13 -07003898 * Get the index of the notification action, if any, that was specified as being clickable
3899 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07003900 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07003901 *
Griff Hazenca48d352014-05-28 22:37:13 -07003902 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07003903 * set, although the list of available actions comes from the main notification and not
3904 * from the child page's notification.
3905 *
3906 * <p>If wearable specific actions were added to the main notification, this index will
3907 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07003908 *
3909 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07003910 */
3911 public int getContentAction() {
3912 return mContentActionIndex;
3913 }
3914
3915 /**
3916 * Set the gravity that this notification should have within the available viewport space.
3917 * Supported values include {@link android.view.Gravity#TOP},
3918 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3919 * The default value is {@link android.view.Gravity#BOTTOM}.
3920 */
3921 public WearableExtender setGravity(int gravity) {
3922 mGravity = gravity;
3923 return this;
3924 }
3925
3926 /**
3927 * Get the gravity that this notification should have within the available viewport space.
3928 * Supported values include {@link android.view.Gravity#TOP},
3929 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3930 * The default value is {@link android.view.Gravity#BOTTOM}.
3931 */
3932 public int getGravity() {
3933 return mGravity;
3934 }
3935
3936 /**
3937 * Set the custom size preset for the display of this notification out of the available
3938 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3939 * {@link #SIZE_LARGE}.
3940 * <p>Some custom size presets are only applicable for custom display notifications created
3941 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
3942 * documentation for the preset in question. See also
3943 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
3944 */
3945 public WearableExtender setCustomSizePreset(int sizePreset) {
3946 mCustomSizePreset = sizePreset;
3947 return this;
3948 }
3949
3950 /**
3951 * Get the custom size preset for the display of this notification out of the available
3952 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3953 * {@link #SIZE_LARGE}.
3954 * <p>Some custom size presets are only applicable for custom display notifications created
3955 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
3956 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
3957 */
3958 public int getCustomSizePreset() {
3959 return mCustomSizePreset;
3960 }
3961
3962 /**
3963 * Set the custom height in pixels for the display of this notification's content.
3964 * <p>This option is only available for custom display notifications created
3965 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
3966 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
3967 * {@link #getCustomContentHeight}.
3968 */
3969 public WearableExtender setCustomContentHeight(int height) {
3970 mCustomContentHeight = height;
3971 return this;
3972 }
3973
3974 /**
3975 * Get the custom height in pixels for the display of this notification's content.
3976 * <p>This option is only available for custom display notifications created
3977 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
3978 * {@link #setCustomContentHeight}.
3979 */
3980 public int getCustomContentHeight() {
3981 return mCustomContentHeight;
3982 }
3983
3984 /**
3985 * Set whether the scrolling position for the contents of this notification should start
3986 * at the bottom of the contents instead of the top when the contents are too long to
3987 * display within the screen. Default is false (start scroll at the top).
3988 */
3989 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
3990 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
3991 return this;
3992 }
3993
3994 /**
3995 * Get whether the scrolling position for the contents of this notification should start
3996 * at the bottom of the contents instead of the top when the contents are too long to
3997 * display within the screen. Default is false (start scroll at the top).
3998 */
3999 public boolean getStartScrollBottom() {
4000 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
4001 }
4002
4003 /**
4004 * Set whether the content intent is available when the wearable device is not connected
4005 * to a companion device. The user can still trigger this intent when the wearable device
4006 * is offline, but a visual hint will indicate that the content intent may not be available.
4007 * Defaults to true.
4008 */
4009 public WearableExtender setContentIntentAvailableOffline(
4010 boolean contentIntentAvailableOffline) {
4011 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
4012 return this;
4013 }
4014
4015 /**
4016 * Get whether the content intent is available when the wearable device is not connected
4017 * to a companion device. The user can still trigger this intent when the wearable device
4018 * is offline, but a visual hint will indicate that the content intent may not be available.
4019 * Defaults to true.
4020 */
4021 public boolean getContentIntentAvailableOffline() {
4022 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
4023 }
4024
4025 /**
4026 * Set a hint that this notification's icon should not be displayed.
4027 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
4028 * @return this object for method chaining
4029 */
4030 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
4031 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
4032 return this;
4033 }
4034
4035 /**
4036 * Get a hint that this notification's icon should not be displayed.
4037 * @return {@code true} if this icon should not be displayed, false otherwise.
4038 * The default value is {@code false} if this was never set.
4039 */
4040 public boolean getHintHideIcon() {
4041 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
4042 }
4043
4044 /**
4045 * Set a visual hint that only the background image of this notification should be
4046 * displayed, and other semantic content should be hidden. This hint is only applicable
4047 * to sub-pages added using {@link #addPage}.
4048 */
4049 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
4050 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
4051 return this;
4052 }
4053
4054 /**
4055 * Get a visual hint that only the background image of this notification should be
4056 * displayed, and other semantic content should be hidden. This hint is only applicable
4057 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
4058 */
4059 public boolean getHintShowBackgroundOnly() {
4060 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
4061 }
4062
4063 private void setFlag(int mask, boolean value) {
4064 if (value) {
4065 mFlags |= mask;
4066 } else {
4067 mFlags &= ~mask;
4068 }
4069 }
4070 }
4071
4072 /**
4073 * Get an array of Notification objects from a parcelable array bundle field.
4074 * Update the bundle to have a typed array so fetches in the future don't need
4075 * to do an array copy.
4076 */
4077 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4078 Parcelable[] array = bundle.getParcelableArray(key);
4079 if (array instanceof Notification[] || array == null) {
4080 return (Notification[]) array;
4081 }
4082 Notification[] typedArray = Arrays.copyOf(array, array.length,
4083 Notification[].class);
4084 bundle.putParcelableArray(key, typedArray);
4085 return typedArray;
4086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087}