blob: 21525bcae6e2aa687eab78fc1a79f8151aa54b5a [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;
Christoph Studer4600f9b2014-07-22 22:44:43 +020024import android.content.pm.PackageManager.NameNotFoundException;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040025import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040026import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010027import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010028import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010029import android.graphics.drawable.Drawable;
John Spurlockc0650f022014-07-19 13:22:39 -040030import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070031import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070032import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040034import android.os.BadParcelableException;
Christoph Studer1cda67f2014-07-31 17:29:20 +020035import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050036import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Parcel;
38import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040039import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070040import android.os.UserHandle;
Kenny Guy8a0101b2014-05-08 23:34:12 +010041import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040043import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040044import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070045import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080046import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070047import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.widget.RemoteViews;
49
Griff Hazen959591e2014-05-15 22:26:18 -070050import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070051import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070052
Tor Norbyed9273d62013-05-30 15:59:53 -070053import java.lang.annotation.Retention;
54import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020055import java.lang.reflect.Constructor;
Andy Stadler110988c2010-12-03 14:29:16 -080056import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050057import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070058import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070059import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070060import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062/**
63 * A class that represents how a persistent notification is to be presented to
64 * the user using the {@link android.app.NotificationManager}.
65 *
Joe Onoratocb109a02011-01-18 17:57:41 -080066 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
67 * easier to construct Notifications.</p>
68 *
Joe Fernandez558459f2011-10-13 16:47:36 -070069 * <div class="special reference">
70 * <h3>Developer Guides</h3>
71 * <p>For a guide to creating notifications, read the
72 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
73 * developer guide.</p>
74 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 */
76public class Notification implements Parcelable
77{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040078 private static final String TAG = "Notification";
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040081 * An activity that provides a user interface for adjusting notification preferences for its
82 * containing application. Optional but recommended for apps that post
83 * {@link android.app.Notification Notifications}.
84 */
85 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
86 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
87 = "android.intent.category.NOTIFICATION_PREFERENCES";
88
89 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * Use all default values (where applicable).
91 */
92 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 /**
95 * Use the default notification sound. This will ignore any given
96 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050097 *
Chris Wren47c20a12014-06-18 17:27:29 -040098 * <p>
99 * A notification that is noisy is more likely to be presented as a heads-up notification.
100 * </p>
101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500103 */
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int DEFAULT_SOUND = 1;
106
107 /**
108 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700110 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111 *
Chris Wren47c20a12014-06-18 17:27:29 -0400112 * <p>
113 * A notification that vibrates is more likely to be presented as a heads-up notification.
114 * </p>
115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 */
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 /**
122 * Use the default notification lights. This will ignore the
123 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
124 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500127 */
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800133 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134 * Default value: {@link System#currentTimeMillis() Now}.
135 *
136 * Choose a timestamp that will be most relevant to the user. For most finite events, this
137 * corresponds to the time the event happened (or will happen, in the case of events that have
138 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800139 * timestamped according to when the activity began.
140 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500143 * <ul>
144 * <li>Notification of a new chat message should be stamped when the message was received.</li>
145 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
146 * <li>Notification of a completed file download should be stamped when the download finished.</li>
147 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
148 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
149 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800150 * </ul>
151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
153 public long when;
154
155 /**
156 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500157 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 */
159 public int icon;
160
161 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800162 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
163 * leave it at its default value of 0.
164 *
165 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700166 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800167 */
168 public int iconLevel;
169
170 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 * The number of events that this notification represents. For example, in a new mail
172 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800173 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * The system may or may not use this field to modify the appearance of the notification. For
175 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
176 * superimposed over the icon in the status bar. Starting with
177 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
178 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800179 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500180 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
182 public int number;
183
184 /**
185 * The intent to execute when the expanded status entry is clicked. If
186 * this is an activity, it must include the
187 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800188 * that you take care of task management as described in the
189 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800190 * Stack</a> document. In particular, make sure to read the notification section
191 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
192 * Notifications</a> for the correct ways to launch an application from a
193 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
195 public PendingIntent contentIntent;
196
197 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 * The intent to execute when the notification is explicitly dismissed by the user, either with
199 * the "Clear All" button or by swiping it away individually.
200 *
201 * This probably shouldn't be launching an activity since several of those will be sent
202 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 */
204 public PendingIntent deleteIntent;
205
206 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700207 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800208 *
Chris Wren47c20a12014-06-18 17:27:29 -0400209 * <p>
210 * The system UI may choose to display a heads-up notification, instead of
211 * launching this intent, while the user is using the device.
212 * </p>
213 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800214 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400215 */
216 public PendingIntent fullScreenIntent;
217
218 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400219 * Text that summarizes this notification for accessibility services.
220 *
221 * As of the L release, this text is no longer shown on screen, but it is still useful to
222 * accessibility services (where it serves as an audible announcement of the notification's
223 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400224 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800225 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
227 public CharSequence tickerText;
228
229 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400230 * Formerly, a view showing the {@link #tickerText}.
231 *
232 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400233 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400234 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800235 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400236
237 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400238 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
240 public RemoteViews contentView;
241
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400242 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400243 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400244 * opportunity to show more detail. The system UI may choose to show this
245 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400246 */
247 public RemoteViews bigContentView;
248
Chris Wren8fd39ec2014-02-27 17:43:26 -0500249
250 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400251 * A medium-format version of {@link #contentView}, providing the Notification an
252 * opportunity to add action buttons to contentView. At its discretion, the system UI may
253 * choose to show this as a heads-up notification, which will pop up so the user can see
254 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500255 */
256 public RemoteViews headsUpContentView;
257
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400258 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800259 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800261 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 /**
264 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500265 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400267 * A notification that is noisy is more likely to be presented as a heads-up notification.
268 * </p>
269 *
270 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500271 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * </p>
273 */
274 public Uri sound;
275
276 /**
277 * Use this constant as the value for audioStreamType to request that
278 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700279 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400280 *
281 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
283 public static final int STREAM_DEFAULT = -1;
284
285 /**
286 * The audio stream type to use when playing the sound.
287 * Should be one of the STREAM_ constants from
288 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400289 *
290 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 */
292 public int audioStreamType = STREAM_DEFAULT;
293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400295 * The default value of {@link #audioAttributes}.
296 */
297 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
298 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
299 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
300 .build();
301
302 /**
303 * The {@link AudioAttributes audio attributes} to use when playing the sound.
304 */
305 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
306
307 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500308 * The pattern with which to vibrate.
309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 * <p>
311 * To vibrate the default pattern, see {@link #defaults}.
312 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500313 *
Chris Wren47c20a12014-06-18 17:27:29 -0400314 * <p>
315 * A notification that vibrates is more likely to be presented as a heads-up notification.
316 * </p>
317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * @see android.os.Vibrator#vibrate(long[],int)
319 */
320 public long[] vibrate;
321
322 /**
323 * The color of the led. The hardware will do its best approximation.
324 *
325 * @see #FLAG_SHOW_LIGHTS
326 * @see #flags
327 */
328 public int ledARGB;
329
330 /**
331 * The number of milliseconds for the LED to be on while it's flashing.
332 * The hardware will do its best approximation.
333 *
334 * @see #FLAG_SHOW_LIGHTS
335 * @see #flags
336 */
337 public int ledOnMS;
338
339 /**
340 * The number of milliseconds for the LED to be off while it's flashing.
341 * The hardware will do its best approximation.
342 *
343 * @see #FLAG_SHOW_LIGHTS
344 * @see #flags
345 */
346 public int ledOffMS;
347
348 /**
349 * Specifies which values should be taken from the defaults.
350 * <p>
351 * To set, OR the desired from {@link #DEFAULT_SOUND},
352 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
353 * values, use {@link #DEFAULT_ALL}.
354 * </p>
355 */
356 public int defaults;
357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * Bit to be bitwise-ored into the {@link #flags} field that should be
360 * set if you want the LED on for this notification.
361 * <ul>
362 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
363 * or 0 for both ledOnMS and ledOffMS.</li>
364 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
365 * <li>To flash the LED, pass the number of milliseconds that it should
366 * be on and off to ledOnMS and ledOffMS.</li>
367 * </ul>
368 * <p>
369 * Since hardware varies, you are not guaranteed that any of the values
370 * you pass are honored exactly. Use the system defaults (TODO) if possible
371 * because they will be set to values that work on any given hardware.
372 * <p>
373 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500374 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 */
376 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
377
378 /**
379 * Bit to be bitwise-ored into the {@link #flags} field that should be
380 * set if this notification is in reference to something that is ongoing,
381 * like a phone call. It should not be set if this notification is in
382 * reference to something that happened at a particular point in time,
383 * like a missed phone call.
384 */
385 public static final int FLAG_ONGOING_EVENT = 0x00000002;
386
387 /**
388 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700389 * the audio will be repeated until the notification is
390 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 */
392 public static final int FLAG_INSISTENT = 0x00000004;
393
394 /**
395 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700396 * set if you would only like the sound, vibrate and ticker to be played
397 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 */
399 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
400
401 /**
402 * Bit to be bitwise-ored into the {@link #flags} field that should be
403 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500404 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 */
407 public static final int FLAG_AUTO_CANCEL = 0x00000010;
408
409 /**
410 * Bit to be bitwise-ored into the {@link #flags} field that should be
411 * set if the notification should not be canceled when the user clicks
412 * the Clear all button.
413 */
414 public static final int FLAG_NO_CLEAR = 0x00000020;
415
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700416 /**
417 * Bit to be bitwise-ored into the {@link #flags} field that should be
418 * set if this notification represents a currently running service. This
419 * will normally be set for you by {@link Service#startForeground}.
420 */
421 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
422
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400423 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500424 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800425 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500426 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400427 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500428 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400429
Griff Hazendfcb0802014-02-11 12:00:00 -0800430 /**
431 * Bit to be bitswise-ored into the {@link #flags} field that should be
432 * set if this notification is relevant to the current device only
433 * and it is not recommended that it bridge to other devices.
434 */
435 public static final int FLAG_LOCAL_ONLY = 0x00000100;
436
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700437 /**
438 * Bit to be bitswise-ored into the {@link #flags} field that should be
439 * set if this notification is the group summary for a group of notifications.
440 * Grouped notifications may display in a cluster or stack on devices which
441 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
442 */
443 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 public int flags;
446
Tor Norbyed9273d62013-05-30 15:59:53 -0700447 /** @hide */
448 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
449 @Retention(RetentionPolicy.SOURCE)
450 public @interface Priority {}
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500453 * Default notification {@link #priority}. If your application does not prioritize its own
454 * notifications, use this value for all notifications.
455 */
456 public static final int PRIORITY_DEFAULT = 0;
457
458 /**
459 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
460 * items smaller, or at a different position in the list, compared with your app's
461 * {@link #PRIORITY_DEFAULT} items.
462 */
463 public static final int PRIORITY_LOW = -1;
464
465 /**
466 * Lowest {@link #priority}; these items might not be shown to the user except under special
467 * circumstances, such as detailed notification logs.
468 */
469 public static final int PRIORITY_MIN = -2;
470
471 /**
472 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
473 * show these items larger, or at a different position in notification lists, compared with
474 * your app's {@link #PRIORITY_DEFAULT} items.
475 */
476 public static final int PRIORITY_HIGH = 1;
477
478 /**
479 * Highest {@link #priority}, for your application's most important items that require the
480 * user's prompt attention or input.
481 */
482 public static final int PRIORITY_MAX = 2;
483
484 /**
485 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800486 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500487 * Priority is an indication of how much of the user's valuable attention should be consumed by
488 * this notification. Low-priority notifications may be hidden from the user in certain
489 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500490 * system will make a determination about how to interpret this priority when presenting
491 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400492 *
493 * <p>
494 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
495 * as a heads-up notification.
496 * </p>
497 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500498 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700499 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500500 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800501
Dan Sandler26e81cf2014-05-06 10:01:27 -0400502 /**
503 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
504 * to be applied by the standard Style templates when presenting this notification.
505 *
506 * The current template design constructs a colorful header image by overlaying the
507 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
508 * ignored.
509 */
510 public int color = COLOR_DEFAULT;
511
512 /**
513 * Special value of {@link #color} telling the system not to decorate this notification with
514 * any special color but instead use default colors when presenting this notification.
515 */
516 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600517
518 /**
519 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
520 * the notification's presence and contents in untrusted situations (namely, on the secure
521 * lockscreen).
522 *
523 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
524 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
525 * shown in all situations, but the contents are only available if the device is unlocked for
526 * the appropriate user.
527 *
528 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
529 * can be read even in an "insecure" context (that is, above a secure lockscreen).
530 * To modify the public version of this notification—for example, to redact some portions—see
531 * {@link Builder#setPublicVersion(Notification)}.
532 *
533 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
534 * and ticker until the user has bypassed the lockscreen.
535 */
536 public int visibility;
537
538 public static final int VISIBILITY_PUBLIC = 1;
539 public static final int VISIBILITY_PRIVATE = 0;
540 public static final int VISIBILITY_SECRET = -1;
541
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500542 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400543 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500544 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400545 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500546
547 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400548 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500549 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400550 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500551
552 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400553 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500554 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400555 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500556
557 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400558 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500559 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400560 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500561
562 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400563 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500564 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400565 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500566
567 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400568 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500569 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400570 public static final String CATEGORY_ALARM = "alarm";
571
572 /**
573 * Notification category: progress of a long-running background operation.
574 */
575 public static final String CATEGORY_PROGRESS = "progress";
576
577 /**
578 * Notification category: social network or sharing update.
579 */
580 public static final String CATEGORY_SOCIAL = "social";
581
582 /**
583 * Notification category: error in background operation or authentication status.
584 */
585 public static final String CATEGORY_ERROR = "err";
586
587 /**
588 * Notification category: media transport control for playback.
589 */
590 public static final String CATEGORY_TRANSPORT = "transport";
591
592 /**
593 * Notification category: system or device status update. Reserved for system use.
594 */
595 public static final String CATEGORY_SYSTEM = "sys";
596
597 /**
598 * Notification category: indication of running background service.
599 */
600 public static final String CATEGORY_SERVICE = "service";
601
602 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400603 * Notification category: a specific, timely recommendation for a single thing.
604 * For example, a news app might want to recommend a news story it believes the user will
605 * want to read next.
606 */
607 public static final String CATEGORY_RECOMMENDATION = "recommendation";
608
609 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400610 * Notification category: ongoing information about device or contextual status.
611 */
612 public static final String CATEGORY_STATUS = "status";
613
614 /**
615 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
616 * that best describes this Notification. May be used by the system for ranking and filtering.
617 */
618 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700620 private String mGroupKey;
621
622 /**
623 * Get the key used to group this notification into a cluster or stack
624 * with other notifications on devices which support such rendering.
625 */
626 public String getGroup() {
627 return mGroupKey;
628 }
629
630 private String mSortKey;
631
632 /**
633 * Get a sort key that orders this notification among other notifications from the
634 * same package. This can be useful if an external sort was already applied and an app
635 * would like to preserve this. Notifications will be sorted lexicographically using this
636 * value, although providing different priorities in addition to providing sort key may
637 * cause this value to be ignored.
638 *
639 * <p>This sort key can also be used to order members of a notification group. See
640 * {@link Builder#setGroup}.
641 *
642 * @see String#compareTo(String)
643 */
644 public String getSortKey() {
645 return mSortKey;
646 }
647
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400649 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400650 * <p>
651 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
652 * APIs, and are intended to be used by
653 * {@link android.service.notification.NotificationListenerService} implementations to extract
654 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400656 public Bundle extras = new Bundle();
657
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400658 /**
659 * {@link #extras} key: this is the title of the notification,
660 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
661 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500662 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400663
664 /**
665 * {@link #extras} key: this is the title of the notification when shown in expanded form,
666 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
667 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400668 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400669
670 /**
671 * {@link #extras} key: this is the main text payload, as supplied to
672 * {@link Builder#setContentText(CharSequence)}.
673 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500674 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400675
676 /**
677 * {@link #extras} key: this is a third line of text, as supplied to
678 * {@link Builder#setSubText(CharSequence)}.
679 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400680 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400681
682 /**
683 * {@link #extras} key: this is a small piece of additional text as supplied to
684 * {@link Builder#setContentInfo(CharSequence)}.
685 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400686 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400687
688 /**
689 * {@link #extras} key: this is a line of summary information intended to be shown
690 * alongside expanded notifications, as supplied to (e.g.)
691 * {@link BigTextStyle#setSummaryText(CharSequence)}.
692 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400693 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400694
695 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200696 * {@link #extras} key: this is the longer text shown in the big form of a
697 * {@link BigTextStyle} notification, as supplied to
698 * {@link BigTextStyle#bigText(CharSequence)}.
699 */
700 public static final String EXTRA_BIG_TEXT = "android.bigText";
701
702 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400703 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
704 * supplied to {@link Builder#setSmallIcon(int)}.
705 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500706 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400707
708 /**
709 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
710 * notification payload, as
711 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
712 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400713 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400714
715 /**
716 * {@link #extras} key: this is a bitmap to be used instead of the one from
717 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
718 * shown in its expanded form, as supplied to
719 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
720 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400721 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400722
723 /**
724 * {@link #extras} key: this is the progress value supplied to
725 * {@link Builder#setProgress(int, int, boolean)}.
726 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400727 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400728
729 /**
730 * {@link #extras} key: this is the maximum value supplied to
731 * {@link Builder#setProgress(int, int, boolean)}.
732 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400733 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400734
735 /**
736 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
737 * {@link Builder#setProgress(int, int, boolean)}.
738 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400739 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400740
741 /**
742 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
743 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
744 * {@link Builder#setUsesChronometer(boolean)}.
745 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400746 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400747
748 /**
749 * {@link #extras} key: whether {@link #when} should be shown,
750 * as supplied to {@link Builder#setShowWhen(boolean)}.
751 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400752 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400753
754 /**
755 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
756 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
757 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400758 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400759
760 /**
761 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
762 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
763 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400764 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400765
766 /**
767 * {@link #extras} key: A string representing the name of the specific
768 * {@link android.app.Notification.Style} used to create this notification.
769 */
Chris Wren91ad5632013-06-05 15:05:57 -0400770 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400771
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400772 /**
773 * {@link #extras} key: An array of people that this notification relates to, specified
774 * by contacts provider contact URI.
775 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400776 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500777
778 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400779 * {@link #extras} key: used to provide hints about the appropriateness of
780 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400781 * @hide
782 */
783 public static final String EXTRA_AS_HEADS_UP = "headsup";
784
785 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400786 * Allow certain system-generated notifications to appear before the device is provisioned.
787 * Only available to notifications coming from the android package.
788 * @hide
789 */
790 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
791
792 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700793 * {@link #extras} key: A
794 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
795 * in the background when the notification is selected. The URI must point to an image stream
796 * suitable for passing into
797 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
798 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
799 * URI used for this purpose must require no permissions to read the image data.
800 */
801 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
802
803 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400804 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700805 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400806 * {@link android.app.Notification.MediaStyle} notification.
807 */
808 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
809
810 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100811 * {@link #extras} key: the indices of actions to be shown in the compact view,
812 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
813 */
814 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
815
Christoph Studer943aa672014-08-03 20:31:16 +0200816 /**
Christoph Studer7ac80e62014-08-04 16:01:57 +0200817 * {@link #extras} key: the user that built the notification.
Christoph Studer943aa672014-08-03 20:31:16 +0200818 *
819 * @hide
820 */
Christoph Studer7ac80e62014-08-04 16:01:57 +0200821 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
Christoph Studer943aa672014-08-03 20:31:16 +0200822
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100823 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400824 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
825 * displayed in the heads up space.
826 *
827 * <p>
828 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
829 * full-screen intent when posted.
830 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400831 * @hide
832 */
833 public static final int HEADS_UP_NEVER = 0;
834
835 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400836 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
837 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400838 * @hide
839 */
840 public static final int HEADS_UP_ALLOWED = 1;
841
842 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400843 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
844 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400845 * @hide
846 */
847 public static final int HEADS_UP_REQUESTED = 2;
848
849 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400850 * Structure to encapsulate a named action that can be shown as part of this notification.
851 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
852 * selected by the user.
853 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700854 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
855 * or {@link Notification.Builder#addAction(Notification.Action)}
856 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400857 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500858 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700859 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700860 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700861
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400862 /**
863 * Small icon representing the action.
864 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400865 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700866
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400867 /**
868 * Title of the action.
869 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400870 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700871
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400872 /**
873 * Intent to send when the user invokes this action. May be null, in which case the action
874 * may be rendered in a disabled presentation by the system UI.
875 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400876 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700877
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400878 private Action(Parcel in) {
879 icon = in.readInt();
880 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
881 if (in.readInt() == 1) {
882 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
883 }
Griff Hazen959591e2014-05-15 22:26:18 -0700884 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700885 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400886 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700887
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400888 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700889 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890 */
891 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700892 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700893 }
894
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700895 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
896 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400897 this.icon = icon;
898 this.title = title;
899 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700900 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700901 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700902 }
903
904 /**
905 * Get additional metadata carried around with this Action.
906 */
907 public Bundle getExtras() {
908 return mExtras;
909 }
910
911 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700912 * Get the list of inputs to be collected from the user when this action is sent.
913 * May return null if no remote inputs were added.
914 */
915 public RemoteInput[] getRemoteInputs() {
916 return mRemoteInputs;
917 }
918
919 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700920 * Builder class for {@link Action} objects.
921 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700922 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700923 private final int mIcon;
924 private final CharSequence mTitle;
925 private final PendingIntent mIntent;
926 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700927 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700928
929 /**
930 * Construct a new builder for {@link Action} object.
931 * @param icon icon to show for this action
932 * @param title the title of the action
933 * @param intent the {@link PendingIntent} to fire when users trigger this action
934 */
935 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700936 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700937 }
938
939 /**
940 * Construct a new builder for {@link Action} object using the fields from an
941 * {@link Action}.
942 * @param action the action to read fields from.
943 */
944 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700945 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
946 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700947 }
948
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700949 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
950 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700951 mIcon = icon;
952 mTitle = title;
953 mIntent = intent;
954 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700955 if (remoteInputs != null) {
956 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
957 Collections.addAll(mRemoteInputs, remoteInputs);
958 }
Griff Hazen959591e2014-05-15 22:26:18 -0700959 }
960
961 /**
962 * Merge additional metadata into this builder.
963 *
964 * <p>Values within the Bundle will replace existing extras values in this Builder.
965 *
966 * @see Notification.Action#extras
967 */
968 public Builder addExtras(Bundle extras) {
969 if (extras != null) {
970 mExtras.putAll(extras);
971 }
972 return this;
973 }
974
975 /**
976 * Get the metadata Bundle used by this Builder.
977 *
978 * <p>The returned Bundle is shared with this Builder.
979 */
980 public Bundle getExtras() {
981 return mExtras;
982 }
983
984 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700985 * Add an input to be collected from the user when this action is sent.
986 * Response values can be retrieved from the fired intent by using the
987 * {@link RemoteInput#getResultsFromIntent} function.
988 * @param remoteInput a {@link RemoteInput} to add to the action
989 * @return this object for method chaining
990 */
991 public Builder addRemoteInput(RemoteInput remoteInput) {
992 if (mRemoteInputs == null) {
993 mRemoteInputs = new ArrayList<RemoteInput>();
994 }
995 mRemoteInputs.add(remoteInput);
996 return this;
997 }
998
999 /**
1000 * Apply an extender to this action builder. Extenders may be used to add
1001 * metadata or change options on this builder.
1002 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001003 public Builder extend(Extender extender) {
1004 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001005 return this;
1006 }
1007
1008 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001009 * Combine all of the options that have been set and return a new {@link Action}
1010 * object.
1011 * @return the built action
1012 */
1013 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001014 RemoteInput[] remoteInputs = mRemoteInputs != null
1015 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1016 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001017 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001018 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001019
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020 @Override
1021 public Action clone() {
1022 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001023 icon,
1024 title,
1025 actionIntent, // safe to alias
1026 new Bundle(mExtras),
1027 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001028 }
1029 @Override
1030 public int describeContents() {
1031 return 0;
1032 }
1033 @Override
1034 public void writeToParcel(Parcel out, int flags) {
1035 out.writeInt(icon);
1036 TextUtils.writeToParcel(title, out, flags);
1037 if (actionIntent != null) {
1038 out.writeInt(1);
1039 actionIntent.writeToParcel(out, flags);
1040 } else {
1041 out.writeInt(0);
1042 }
Griff Hazen959591e2014-05-15 22:26:18 -07001043 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001044 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001045 }
Griff Hazen959591e2014-05-15 22:26:18 -07001046 public static final Parcelable.Creator<Action> CREATOR =
1047 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001048 public Action createFromParcel(Parcel in) {
1049 return new Action(in);
1050 }
1051 public Action[] newArray(int size) {
1052 return new Action[size];
1053 }
1054 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001055
1056 /**
1057 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1058 * metadata or change options on an action builder.
1059 */
1060 public interface Extender {
1061 /**
1062 * Apply this extender to a notification action builder.
1063 * @param builder the builder to be modified.
1064 * @return the build object for chaining.
1065 */
1066 public Builder extend(Builder builder);
1067 }
1068
1069 /**
1070 * Wearable extender for notification actions. To add extensions to an action,
1071 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1072 * the {@code WearableExtender()} constructor and apply it to a
1073 * {@link android.app.Notification.Action.Builder} using
1074 * {@link android.app.Notification.Action.Builder#extend}.
1075 *
1076 * <pre class="prettyprint">
1077 * Notification.Action action = new Notification.Action.Builder(
1078 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001079 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001080 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001081 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001082 */
1083 public static final class WearableExtender implements Extender {
1084 /** Notification action extra which contains wearable extensions */
1085 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1086
1087 private static final String KEY_FLAGS = "flags";
1088
1089 // Flags bitwise-ored to mFlags
1090 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1091
1092 // Default value for flags integer
1093 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1094
1095 private int mFlags = DEFAULT_FLAGS;
1096
1097 /**
1098 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1099 * options.
1100 */
1101 public WearableExtender() {
1102 }
1103
1104 /**
1105 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1106 * wearable options present in an existing notification action.
1107 * @param action the notification action to inspect.
1108 */
1109 public WearableExtender(Action action) {
1110 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1111 if (wearableBundle != null) {
1112 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1113 }
1114 }
1115
1116 /**
1117 * Apply wearable extensions to a notification action that is being built. This is
1118 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1119 * method of {@link android.app.Notification.Action.Builder}.
1120 */
1121 @Override
1122 public Action.Builder extend(Action.Builder builder) {
1123 Bundle wearableBundle = new Bundle();
1124
1125 if (mFlags != DEFAULT_FLAGS) {
1126 wearableBundle.putInt(KEY_FLAGS, mFlags);
1127 }
1128
1129 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1130 return builder;
1131 }
1132
1133 @Override
1134 public WearableExtender clone() {
1135 WearableExtender that = new WearableExtender();
1136 that.mFlags = this.mFlags;
1137 return that;
1138 }
1139
1140 /**
1141 * Set whether this action is available when the wearable device is not connected to
1142 * a companion device. The user can still trigger this action when the wearable device is
1143 * offline, but a visual hint will indicate that the action may not be available.
1144 * Defaults to true.
1145 */
1146 public WearableExtender setAvailableOffline(boolean availableOffline) {
1147 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1148 return this;
1149 }
1150
1151 /**
1152 * Get whether this action is available when the wearable device is not connected to
1153 * a companion device. The user can still trigger this action when the wearable device is
1154 * offline, but a visual hint will indicate that the action may not be available.
1155 * Defaults to true.
1156 */
1157 public boolean isAvailableOffline() {
1158 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1159 }
1160
1161 private void setFlag(int mask, boolean value) {
1162 if (value) {
1163 mFlags |= mask;
1164 } else {
1165 mFlags &= ~mask;
1166 }
1167 }
1168 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001169 }
1170
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001171 /**
1172 * Array of all {@link Action} structures attached to this notification by
1173 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1174 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1175 * interface for invoking actions.
1176 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001177 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001178
1179 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001180 * Replacement version of this notification whose content will be shown
1181 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1182 * and {@link #VISIBILITY_PUBLIC}.
1183 */
1184 public Notification publicVersion;
1185
1186 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001187 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001188 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 */
1190 public Notification()
1191 {
1192 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001193 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
1196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 * @hide
1198 */
1199 public Notification(Context context, int icon, CharSequence tickerText, long when,
1200 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1201 {
1202 this.when = when;
1203 this.icon = icon;
1204 this.tickerText = tickerText;
1205 setLatestEventInfo(context, contentTitle, contentText,
1206 PendingIntent.getActivity(context, 0, contentIntent, 0));
1207 }
1208
1209 /**
1210 * Constructs a Notification object with the information needed to
1211 * have a status bar icon without the standard expanded view.
1212 *
1213 * @param icon The resource id of the icon to put in the status bar.
1214 * @param tickerText The text that flows by in the status bar when the notification first
1215 * activates.
1216 * @param when The time to show in the time field. In the System.currentTimeMillis
1217 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001218 *
1219 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001221 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public Notification(int icon, CharSequence tickerText, long when)
1223 {
1224 this.icon = icon;
1225 this.tickerText = tickerText;
1226 this.when = when;
1227 }
1228
1229 /**
1230 * Unflatten the notification from a parcel.
1231 */
1232 public Notification(Parcel parcel)
1233 {
1234 int version = parcel.readInt();
1235
1236 when = parcel.readLong();
1237 icon = parcel.readInt();
1238 number = parcel.readInt();
1239 if (parcel.readInt() != 0) {
1240 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1241 }
1242 if (parcel.readInt() != 0) {
1243 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1244 }
1245 if (parcel.readInt() != 0) {
1246 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1247 }
1248 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001249 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001250 }
1251 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1253 }
Joe Onorato561d3852010-11-20 18:09:34 -08001254 if (parcel.readInt() != 0) {
1255 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 defaults = parcel.readInt();
1258 flags = parcel.readInt();
1259 if (parcel.readInt() != 0) {
1260 sound = Uri.CREATOR.createFromParcel(parcel);
1261 }
1262
1263 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001264 if (parcel.readInt() != 0) {
1265 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 vibrate = parcel.createLongArray();
1268 ledARGB = parcel.readInt();
1269 ledOnMS = parcel.readInt();
1270 ledOffMS = parcel.readInt();
1271 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001272
1273 if (parcel.readInt() != 0) {
1274 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1275 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001276
1277 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001278
John Spurlockfd7f1e02014-03-18 16:41:57 -04001279 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001280
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001281 mGroupKey = parcel.readString();
1282
1283 mSortKey = parcel.readString();
1284
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001285 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001286
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001287 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1288
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001289 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001290 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1291 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001292
Chris Wren8fd39ec2014-02-27 17:43:26 -05001293 if (parcel.readInt() != 0) {
1294 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1295 }
1296
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001297 visibility = parcel.readInt();
1298
1299 if (parcel.readInt() != 0) {
1300 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1301 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001302
1303 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 }
1305
Andy Stadler110988c2010-12-03 14:29:16 -08001306 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001307 public Notification clone() {
1308 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001309 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001310 return that;
1311 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001312
Daniel Sandler1a497d32013-04-18 14:52:45 -04001313 /**
1314 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1315 * of this into that.
1316 * @hide
1317 */
1318 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001319 that.when = this.when;
1320 that.icon = this.icon;
1321 that.number = this.number;
1322
1323 // PendingIntents are global, so there's no reason (or way) to clone them.
1324 that.contentIntent = this.contentIntent;
1325 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001326 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001327
1328 if (this.tickerText != null) {
1329 that.tickerText = this.tickerText.toString();
1330 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001331 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001332 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001333 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001334 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001335 that.contentView = this.contentView.clone();
1336 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001337 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001338 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1339 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001340 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001341 that.sound = this.sound; // android.net.Uri is immutable
1342 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001343 if (this.audioAttributes != null) {
1344 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1345 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001346
1347 final long[] vibrate = this.vibrate;
1348 if (vibrate != null) {
1349 final int N = vibrate.length;
1350 final long[] vib = that.vibrate = new long[N];
1351 System.arraycopy(vibrate, 0, vib, 0, N);
1352 }
1353
1354 that.ledARGB = this.ledARGB;
1355 that.ledOnMS = this.ledOnMS;
1356 that.ledOffMS = this.ledOffMS;
1357 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001358
Joe Onorato18e69df2010-05-17 22:26:12 -07001359 that.flags = this.flags;
1360
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001361 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001362
John Spurlockfd7f1e02014-03-18 16:41:57 -04001363 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001364
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001365 that.mGroupKey = this.mGroupKey;
1366
1367 that.mSortKey = this.mSortKey;
1368
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001369 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001370 try {
1371 that.extras = new Bundle(this.extras);
1372 // will unparcel
1373 that.extras.size();
1374 } catch (BadParcelableException e) {
1375 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1376 that.extras = null;
1377 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001378 }
1379
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001380 if (this.actions != null) {
1381 that.actions = new Action[this.actions.length];
1382 for(int i=0; i<this.actions.length; i++) {
1383 that.actions[i] = this.actions[i].clone();
1384 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001385 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001386
Daniel Sandler1a497d32013-04-18 14:52:45 -04001387 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001388 that.bigContentView = this.bigContentView.clone();
1389 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001390
Chris Wren8fd39ec2014-02-27 17:43:26 -05001391 if (heavy && this.headsUpContentView != null) {
1392 that.headsUpContentView = this.headsUpContentView.clone();
1393 }
1394
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001395 that.visibility = this.visibility;
1396
1397 if (this.publicVersion != null) {
1398 that.publicVersion = new Notification();
1399 this.publicVersion.cloneInto(that.publicVersion, heavy);
1400 }
1401
Dan Sandler26e81cf2014-05-06 10:01:27 -04001402 that.color = this.color;
1403
Daniel Sandler1a497d32013-04-18 14:52:45 -04001404 if (!heavy) {
1405 that.lightenPayload(); // will clean out extras
1406 }
1407 }
1408
1409 /**
1410 * Removes heavyweight parts of the Notification object for archival or for sending to
1411 * listeners when the full contents are not necessary.
1412 * @hide
1413 */
1414 public final void lightenPayload() {
1415 tickerView = null;
1416 contentView = null;
1417 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001418 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001419 largeIcon = null;
1420 if (extras != null) {
1421 extras.remove(Notification.EXTRA_LARGE_ICON);
1422 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1423 extras.remove(Notification.EXTRA_PICTURE);
1424 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001425 }
1426
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001427 /**
1428 * Make sure this CharSequence is safe to put into a bundle, which basically
1429 * means it had better not be some custom Parcelable implementation.
1430 * @hide
1431 */
1432 public static CharSequence safeCharSequence(CharSequence cs) {
1433 if (cs instanceof Parcelable) {
1434 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1435 + " instance is a custom Parcelable and not allowed in Notification");
1436 return cs.toString();
1437 }
1438
1439 return cs;
1440 }
1441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 public int describeContents() {
1443 return 0;
1444 }
1445
1446 /**
1447 * Flatten this notification from a parcel.
1448 */
1449 public void writeToParcel(Parcel parcel, int flags)
1450 {
1451 parcel.writeInt(1);
1452
1453 parcel.writeLong(when);
1454 parcel.writeInt(icon);
1455 parcel.writeInt(number);
1456 if (contentIntent != null) {
1457 parcel.writeInt(1);
1458 contentIntent.writeToParcel(parcel, 0);
1459 } else {
1460 parcel.writeInt(0);
1461 }
1462 if (deleteIntent != null) {
1463 parcel.writeInt(1);
1464 deleteIntent.writeToParcel(parcel, 0);
1465 } else {
1466 parcel.writeInt(0);
1467 }
1468 if (tickerText != null) {
1469 parcel.writeInt(1);
1470 TextUtils.writeToParcel(tickerText, parcel, flags);
1471 } else {
1472 parcel.writeInt(0);
1473 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001474 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001475 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001476 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001477 } else {
1478 parcel.writeInt(0);
1479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 if (contentView != null) {
1481 parcel.writeInt(1);
1482 contentView.writeToParcel(parcel, 0);
1483 } else {
1484 parcel.writeInt(0);
1485 }
Joe Onorato561d3852010-11-20 18:09:34 -08001486 if (largeIcon != null) {
1487 parcel.writeInt(1);
1488 largeIcon.writeToParcel(parcel, 0);
1489 } else {
1490 parcel.writeInt(0);
1491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492
1493 parcel.writeInt(defaults);
1494 parcel.writeInt(this.flags);
1495
1496 if (sound != null) {
1497 parcel.writeInt(1);
1498 sound.writeToParcel(parcel, 0);
1499 } else {
1500 parcel.writeInt(0);
1501 }
1502 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001503
1504 if (audioAttributes != null) {
1505 parcel.writeInt(1);
1506 audioAttributes.writeToParcel(parcel, 0);
1507 } else {
1508 parcel.writeInt(0);
1509 }
1510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 parcel.writeLongArray(vibrate);
1512 parcel.writeInt(ledARGB);
1513 parcel.writeInt(ledOnMS);
1514 parcel.writeInt(ledOffMS);
1515 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001516
1517 if (fullScreenIntent != null) {
1518 parcel.writeInt(1);
1519 fullScreenIntent.writeToParcel(parcel, 0);
1520 } else {
1521 parcel.writeInt(0);
1522 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001523
1524 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001525
John Spurlockfd7f1e02014-03-18 16:41:57 -04001526 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001527
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001528 parcel.writeString(mGroupKey);
1529
1530 parcel.writeString(mSortKey);
1531
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001532 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001533
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001534 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001535
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001536 if (bigContentView != null) {
1537 parcel.writeInt(1);
1538 bigContentView.writeToParcel(parcel, 0);
1539 } else {
1540 parcel.writeInt(0);
1541 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001542
Chris Wren8fd39ec2014-02-27 17:43:26 -05001543 if (headsUpContentView != null) {
1544 parcel.writeInt(1);
1545 headsUpContentView.writeToParcel(parcel, 0);
1546 } else {
1547 parcel.writeInt(0);
1548 }
1549
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001550 parcel.writeInt(visibility);
1551
1552 if (publicVersion != null) {
1553 parcel.writeInt(1);
1554 publicVersion.writeToParcel(parcel, 0);
1555 } else {
1556 parcel.writeInt(0);
1557 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001558
1559 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561
1562 /**
1563 * Parcelable.Creator that instantiates Notification objects
1564 */
1565 public static final Parcelable.Creator<Notification> CREATOR
1566 = new Parcelable.Creator<Notification>()
1567 {
1568 public Notification createFromParcel(Parcel parcel)
1569 {
1570 return new Notification(parcel);
1571 }
1572
1573 public Notification[] newArray(int size)
1574 {
1575 return new Notification[size];
1576 }
1577 };
1578
1579 /**
1580 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1581 * layout.
1582 *
1583 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1584 * in the view.</p>
1585 * @param context The context for your application / activity.
1586 * @param contentTitle The title that goes in the expanded entry.
1587 * @param contentText The text that goes in the expanded entry.
1588 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1589 * If this is an activity, it must include the
1590 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001591 * that you take care of task management as described in the
1592 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1593 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001594 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001595 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001597 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 public void setLatestEventInfo(Context context,
1599 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001600 Notification.Builder builder = new Notification.Builder(context);
1601
1602 // First, ensure that key pieces of information that may have been set directly
1603 // are preserved
1604 builder.setWhen(this.when);
1605 builder.setSmallIcon(this.icon);
1606 builder.setPriority(this.priority);
1607 builder.setTicker(this.tickerText);
1608 builder.setNumber(this.number);
1609 builder.mFlags = this.flags;
1610 builder.setSound(this.sound, this.audioStreamType);
1611 builder.setDefaults(this.defaults);
1612 builder.setVibrate(this.vibrate);
1613
1614 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001616 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
1618 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001619 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001621 builder.setContentIntent(contentIntent);
1622 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
1624
1625 @Override
1626 public String toString() {
1627 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001628 sb.append("Notification(pri=");
1629 sb.append(priority);
1630 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001631 if (contentView != null) {
1632 sb.append(contentView.getPackage());
1633 sb.append("/0x");
1634 sb.append(Integer.toHexString(contentView.getLayoutId()));
1635 } else {
1636 sb.append("null");
1637 }
1638 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001639 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1640 sb.append("default");
1641 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 int N = this.vibrate.length-1;
1643 sb.append("[");
1644 for (int i=0; i<N; i++) {
1645 sb.append(this.vibrate[i]);
1646 sb.append(',');
1647 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001648 if (N != -1) {
1649 sb.append(this.vibrate[N]);
1650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 } else {
1653 sb.append("null");
1654 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001655 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001656 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001658 } else if (this.sound != null) {
1659 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 } else {
1661 sb.append("null");
1662 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001663 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001665 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001666 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001667 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001668 if (this.category != null) {
1669 sb.append(" category=");
1670 sb.append(this.category);
1671 }
1672 if (this.mGroupKey != null) {
1673 sb.append(" groupKey=");
1674 sb.append(this.mGroupKey);
1675 }
1676 if (this.mSortKey != null) {
1677 sb.append(" sortKey=");
1678 sb.append(this.mSortKey);
1679 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001680 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001681 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001682 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001683 }
1684 sb.append(" vis=");
1685 sb.append(visibilityToString(this.visibility));
1686 if (this.publicVersion != null) {
1687 sb.append(" publicVersion=");
1688 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001689 }
1690 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 return sb.toString();
1692 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001693
Dan Sandler1b718782014-07-18 12:43:45 -04001694 /**
1695 * {@hide}
1696 */
1697 public static String visibilityToString(int vis) {
1698 switch (vis) {
1699 case VISIBILITY_PRIVATE:
1700 return "PRIVATE";
1701 case VISIBILITY_PUBLIC:
1702 return "PUBLIC";
1703 case VISIBILITY_SECRET:
1704 return "SECRET";
1705 default:
1706 return "UNKNOWN(" + String.valueOf(vis) + ")";
1707 }
1708 }
1709
Jeff Sharkey6d515712012-09-20 16:06:08 -07001710 /** {@hide} */
1711 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001712 if (user.getIdentifier() == UserHandle.USER_ALL) {
1713 user = UserHandle.OWNER;
1714 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001715 if (tickerView != null) {
1716 tickerView.setUser(user);
1717 }
1718 if (contentView != null) {
1719 contentView.setUser(user);
1720 }
1721 if (bigContentView != null) {
1722 bigContentView.setUser(user);
1723 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001724 if (headsUpContentView != null) {
1725 headsUpContentView.setUser(user);
1726 }
Christoph Studerfd193262014-07-28 15:44:04 +02001727 if (publicVersion != null) {
1728 publicVersion.setUser(user);
1729 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001730 }
1731
Joe Onoratocb109a02011-01-18 17:57:41 -08001732 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001733 * @hide
1734 */
1735 public boolean isValid() {
1736 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1737 // for legacy reasons.
1738 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1739 }
1740
1741 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001742 * @hide
1743 */
1744 public boolean isGroupSummary() {
1745 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1746 }
1747
1748 /**
1749 * @hide
1750 */
1751 public boolean isGroupChild() {
1752 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1753 }
1754
1755 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001756 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001757 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001758 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001759 * content views using the platform's notification layout template. If your app supports
1760 * versions of Android as old as API level 4, you can instead use
1761 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1762 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1763 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001764 *
Scott Main183bf112012-08-13 19:12:13 -07001765 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001766 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001767 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001768 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001769 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1770 * .setContentText(subject)
1771 * .setSmallIcon(R.drawable.new_mail)
1772 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001773 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001774 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001775 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001776 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001777 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001778
Christoph Studer4600f9b2014-07-22 22:44:43 +02001779 /**
1780 * @hide
1781 */
1782 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1783
1784 /**
1785 * @hide
1786 */
1787 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1788 /**
1789 * @hide
1790 */
1791 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1792 /**
1793 * @hide
1794 */
1795 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1796 "android.rebuild.contentViewActionCount";
1797 /**
1798 * @hide
1799 */
1800 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1801 = "android.rebuild.bigView";
1802 /**
1803 * @hide
1804 */
1805 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1806 = "android.rebuild.bigViewActionCount";
1807 /**
1808 * @hide
1809 */
1810 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
1811 = "android.rebuild.hudView";
1812 /**
1813 * @hide
1814 */
1815 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
1816 = "android.rebuild.hudViewActionCount";
1817
1818 /**
1819 * The package name of the context used to create the notification via a Builder.
1820 */
1821 private static final String EXTRA_REBUILD_CONTEXT_PACKAGE =
1822 "android.rebuild.contextPackage";
1823
1824 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
1825 // memory intensive resources.
1826 private static final boolean STRIP_AND_REBUILD = true;
1827
Joe Onorato46439ce2010-11-19 13:56:21 -08001828 private Context mContext;
1829
1830 private long mWhen;
1831 private int mSmallIcon;
1832 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001833 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001834 private CharSequence mContentTitle;
1835 private CharSequence mContentText;
1836 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001837 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001838 private PendingIntent mContentIntent;
1839 private RemoteViews mContentView;
1840 private PendingIntent mDeleteIntent;
1841 private PendingIntent mFullScreenIntent;
1842 private CharSequence mTickerText;
1843 private RemoteViews mTickerView;
1844 private Bitmap mLargeIcon;
1845 private Uri mSound;
1846 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001847 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08001848 private long[] mVibrate;
1849 private int mLedArgb;
1850 private int mLedOnMs;
1851 private int mLedOffMs;
1852 private int mDefaults;
1853 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001854 private int mProgressMax;
1855 private int mProgress;
1856 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001857 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001858 private String mGroupKey;
1859 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001860 private Bundle mExtras;
1861 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001862 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001863 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001864 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001865 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001866 private int mVisibility = VISIBILITY_PRIVATE;
1867 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001868 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001869 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001870 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02001871
1872 /**
1873 * The user that built the notification originally.
1874 */
1875 private int mOriginatingUserId;
Christoph Studer4600f9b2014-07-22 22:44:43 +02001876
1877 /**
1878 * Contains extras related to rebuilding during the build phase.
1879 */
1880 private Bundle mRebuildBundle = new Bundle();
1881 /**
1882 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
1883 * Null otherwise.
1884 */
1885 private Notification mRebuildNotification = null;
1886
Joe Onoratocb109a02011-01-18 17:57:41 -08001887 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001888 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001889 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001890
1891 * <table>
1892 * <tr><th align=right>priority</th>
1893 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1894 * <tr><th align=right>when</th>
1895 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1896 * <tr><th align=right>audio stream</th>
1897 * <td>{@link #STREAM_DEFAULT}</td></tr>
1898 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001899 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001900
1901 * @param context
1902 * A {@link Context} that will be used by the Builder to construct the
1903 * RemoteViews. The Context will not be held past the lifetime of this Builder
1904 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001905 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001906 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001907 /*
1908 * Important compatibility note!
1909 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1910 * constructor for later use. At this point Activities - themselves subclasses of
1911 * ContextWrapper - do not have their inner Context populated yet. This means that
1912 * any calls to Context methods from within this constructor can cause NPEs in existing
1913 * apps. Any data populated from mContext should therefore be populated lazily to
1914 * preserve compatibility.
1915 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001916 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001917
1918 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001919 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001920 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04001921 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001922 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001923 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001924
Christoph Studer1cda67f2014-07-31 17:29:20 +02001925 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.L ?
1926 NotificationColorUtil.getInstance() : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08001927 }
1928
Joe Onoratocb109a02011-01-18 17:57:41 -08001929 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001930 * Creates a Builder for rebuilding the given Notification.
1931 * <p>
1932 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
1933 */
1934 private Builder(Context context, Notification n) {
1935 this(context);
1936 mRebuildNotification = n;
1937 restoreFromNotification(n);
1938
1939 Style style = null;
1940 Bundle extras = n.extras;
1941 String templateClass = extras.getString(EXTRA_TEMPLATE);
1942 if (!TextUtils.isEmpty(templateClass)) {
1943 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
1944 if (styleClass == null) {
1945 Log.d(TAG, "Unknown style class: " + styleClass);
1946 return;
1947 }
1948
1949 try {
1950 Constructor<? extends Style> constructor = styleClass.getConstructor();
1951 style = constructor.newInstance();
1952 style.restoreFromExtras(extras);
1953 } catch (Throwable t) {
1954 Log.e(TAG, "Could not create Style", t);
1955 return;
1956 }
1957 }
1958 if (style != null) {
1959 setStyle(style);
1960 }
1961 }
1962
1963 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001964 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001965 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001966 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001967 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001968 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001969 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001970 public Builder setWhen(long when) {
1971 mWhen = when;
1972 return this;
1973 }
1974
Joe Onoratocb109a02011-01-18 17:57:41 -08001975 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001976 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001977 * in the content view.
1978 */
1979 public Builder setShowWhen(boolean show) {
1980 mShowWhen = show;
1981 return this;
1982 }
1983
1984 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001985 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001986 *
1987 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001988 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001989 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001990 * Useful when showing an elapsed time (like an ongoing phone call).
1991 *
1992 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001993 * @see Notification#when
1994 */
1995 public Builder setUsesChronometer(boolean b) {
1996 mUseChronometer = b;
1997 return this;
1998 }
1999
2000 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002001 * Set the small icon resource, which will be used to represent the notification in the
2002 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002003 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002004
2005 * The platform template for the expanded view will draw this icon in the left, unless a
2006 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2007 * icon will be moved to the right-hand side.
2008 *
2009
2010 * @param icon
2011 * A resource ID in the application's package of the drawable to use.
2012 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002013 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002014 public Builder setSmallIcon(int icon) {
2015 mSmallIcon = icon;
2016 return this;
2017 }
2018
Joe Onoratocb109a02011-01-18 17:57:41 -08002019 /**
2020 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2021 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2022 * LevelListDrawable}.
2023 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002024 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002025 * @param level The level to use for the icon.
2026 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002027 * @see Notification#icon
2028 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002029 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002030 public Builder setSmallIcon(int icon, int level) {
2031 mSmallIcon = icon;
2032 mSmallIconLevel = level;
2033 return this;
2034 }
2035
Joe Onoratocb109a02011-01-18 17:57:41 -08002036 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002037 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002038 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002039 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002040 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002041 return this;
2042 }
2043
Joe Onoratocb109a02011-01-18 17:57:41 -08002044 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002045 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002046 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002047 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002048 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002049 return this;
2050 }
2051
Joe Onoratocb109a02011-01-18 17:57:41 -08002052 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002053 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002054 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2055 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002056 */
2057 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002058 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002059 return this;
2060 }
2061
2062 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002063 * Set the large number at the right-hand side of the notification. This is
2064 * equivalent to setContentInfo, although it might show the number in a different
2065 * font size for readability.
2066 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002067 public Builder setNumber(int number) {
2068 mNumber = number;
2069 return this;
2070 }
2071
Joe Onoratocb109a02011-01-18 17:57:41 -08002072 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002073 * A small piece of additional information pertaining to this notification.
2074 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002075 * The platform template will draw this on the last line of the notification, at the far
2076 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002077 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002078 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002079 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002080 return this;
2081 }
2082
Joe Onoratocb109a02011-01-18 17:57:41 -08002083 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002084 * Set the progress this notification represents.
2085 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002086 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002087 */
2088 public Builder setProgress(int max, int progress, boolean indeterminate) {
2089 mProgressMax = max;
2090 mProgress = progress;
2091 mProgressIndeterminate = indeterminate;
2092 return this;
2093 }
2094
2095 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002096 * Supply a custom RemoteViews to use instead of the platform template.
2097 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002098 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002099 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002100 public Builder setContent(RemoteViews views) {
2101 mContentView = views;
2102 return this;
2103 }
2104
Joe Onoratocb109a02011-01-18 17:57:41 -08002105 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002106 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2107 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002108 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2109 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2110 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002111 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002112 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002113 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002114 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002115 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002116 public Builder setContentIntent(PendingIntent intent) {
2117 mContentIntent = intent;
2118 return this;
2119 }
2120
Joe Onoratocb109a02011-01-18 17:57:41 -08002121 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002122 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2123 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002124 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002125 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002126 public Builder setDeleteIntent(PendingIntent intent) {
2127 mDeleteIntent = intent;
2128 return this;
2129 }
2130
Joe Onoratocb109a02011-01-18 17:57:41 -08002131 /**
2132 * An intent to launch instead of posting the notification to the status bar.
2133 * Only for use with extremely high-priority notifications demanding the user's
2134 * <strong>immediate</strong> attention, such as an incoming phone call or
2135 * alarm clock that the user has explicitly set to a particular time.
2136 * If this facility is used for something else, please give the user an option
2137 * to turn it off and use a normal notification, as this can be extremely
2138 * disruptive.
2139 *
Chris Wren47c20a12014-06-18 17:27:29 -04002140 * <p>
2141 * The system UI may choose to display a heads-up notification, instead of
2142 * launching this intent, while the user is using the device.
2143 * </p>
2144 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002145 * @param intent The pending intent to launch.
2146 * @param highPriority Passing true will cause this notification to be sent
2147 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002148 *
2149 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002150 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002151 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2152 mFullScreenIntent = intent;
2153 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2154 return this;
2155 }
2156
Joe Onoratocb109a02011-01-18 17:57:41 -08002157 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002158 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002159 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002160 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002161 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002162 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002163 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002164 return this;
2165 }
2166
Joe Onoratocb109a02011-01-18 17:57:41 -08002167 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002168 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002169 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002170 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002171 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002172 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002173 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002174 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002175 return this;
2176 }
2177
Joe Onoratocb109a02011-01-18 17:57:41 -08002178 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002179 * Add a large icon to the notification (and the ticker on some devices).
2180 *
2181 * In the platform template, this image will be shown on the left of the notification view
2182 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
2183 *
2184 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08002185 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002186 public Builder setLargeIcon(Bitmap icon) {
2187 mLargeIcon = icon;
2188 return this;
2189 }
2190
Joe Onoratocb109a02011-01-18 17:57:41 -08002191 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002192 * Set the sound to play.
2193 *
John Spurlockc0650f022014-07-19 13:22:39 -04002194 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2195 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002196 *
Chris Wren47c20a12014-06-18 17:27:29 -04002197 * <p>
2198 * A notification that is noisy is more likely to be presented as a heads-up notification.
2199 * </p>
2200 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002201 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002202 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002203 public Builder setSound(Uri sound) {
2204 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002205 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002206 return this;
2207 }
2208
Joe Onoratocb109a02011-01-18 17:57:41 -08002209 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002210 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002211 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002212 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2213 *
Chris Wren47c20a12014-06-18 17:27:29 -04002214 * <p>
2215 * A notification that is noisy is more likely to be presented as a heads-up notification.
2216 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002217 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002218 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002219 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002220 public Builder setSound(Uri sound, int streamType) {
2221 mSound = sound;
2222 mAudioStreamType = streamType;
2223 return this;
2224 }
2225
Joe Onoratocb109a02011-01-18 17:57:41 -08002226 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002227 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2228 * use during playback.
2229 *
2230 * <p>
2231 * A notification that is noisy is more likely to be presented as a heads-up notification.
2232 * </p>
2233 *
2234 * @see Notification#sound
2235 */
2236 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2237 mSound = sound;
2238 mAudioAttributes = audioAttributes;
2239 return this;
2240 }
2241
2242 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002243 * Set the vibration pattern to use.
2244 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002245 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2246 * <code>pattern</code> parameter.
2247 *
Chris Wren47c20a12014-06-18 17:27:29 -04002248 * <p>
2249 * A notification that vibrates is more likely to be presented as a heads-up notification.
2250 * </p>
2251 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002252 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002253 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002254 public Builder setVibrate(long[] pattern) {
2255 mVibrate = pattern;
2256 return this;
2257 }
2258
Joe Onoratocb109a02011-01-18 17:57:41 -08002259 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002260 * Set the desired color for the indicator LED on the device, as well as the
2261 * blink duty cycle (specified in milliseconds).
2262 *
2263
2264 * Not all devices will honor all (or even any) of these values.
2265 *
2266
2267 * @see Notification#ledARGB
2268 * @see Notification#ledOnMS
2269 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002270 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002271 public Builder setLights(int argb, int onMs, int offMs) {
2272 mLedArgb = argb;
2273 mLedOnMs = onMs;
2274 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002275 return this;
2276 }
2277
Joe Onoratocb109a02011-01-18 17:57:41 -08002278 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002280 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002281
2282 * Ongoing notifications cannot be dismissed by the user, so your application or service
2283 * must take care of canceling them.
2284 *
2285
2286 * They are typically used to indicate a background task that the user is actively engaged
2287 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2288 * (e.g., a file download, sync operation, active network connection).
2289 *
2290
2291 * @see Notification#FLAG_ONGOING_EVENT
2292 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002293 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002294 public Builder setOngoing(boolean ongoing) {
2295 setFlag(FLAG_ONGOING_EVENT, ongoing);
2296 return this;
2297 }
2298
Joe Onoratocb109a02011-01-18 17:57:41 -08002299 /**
2300 * Set this flag if you would only like the sound, vibrate
2301 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002302 *
2303 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002304 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002305 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2306 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2307 return this;
2308 }
2309
Joe Onoratocb109a02011-01-18 17:57:41 -08002310 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002311 * Make this notification automatically dismissed when the user touches it. The
2312 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2313 *
2314 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002315 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002316 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002317 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002318 return this;
2319 }
2320
Joe Onoratocb109a02011-01-18 17:57:41 -08002321 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002322 * Set whether or not this notification should not bridge to other devices.
2323 *
2324 * <p>Some notifications can be bridged to other devices for remote display.
2325 * This hint can be set to recommend this notification not be bridged.
2326 */
2327 public Builder setLocalOnly(boolean localOnly) {
2328 setFlag(FLAG_LOCAL_ONLY, localOnly);
2329 return this;
2330 }
2331
2332 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002333 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002334 * <p>
2335 * The value should be one or more of the following fields combined with
2336 * bitwise-or:
2337 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2338 * <p>
2339 * For all default values, use {@link #DEFAULT_ALL}.
2340 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002341 public Builder setDefaults(int defaults) {
2342 mDefaults = defaults;
2343 return this;
2344 }
2345
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002346 /**
2347 * Set the priority of this notification.
2348 *
2349 * @see Notification#priority
2350 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002351 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002352 mPriority = pri;
2353 return this;
2354 }
Joe Malin8d40d042012-11-05 11:36:40 -08002355
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002356 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002357 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002358 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002359 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002360 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002361 public Builder setCategory(String category) {
2362 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002363 return this;
2364 }
2365
2366 /**
Chris Wrendde75302014-03-26 17:24:15 -04002367 * Add a person that is relevant to this notification.
2368 *
2369 * @see Notification#EXTRA_PEOPLE
2370 */
2371 public Builder addPerson(String handle) {
2372 mPeople.add(handle);
2373 return this;
2374 }
2375
2376 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002377 * Set this notification to be part of a group of notifications sharing the same key.
2378 * Grouped notifications may display in a cluster or stack on devices which
2379 * support such rendering.
2380 *
2381 * <p>To make this notification the summary for its group, also call
2382 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2383 * {@link #setSortKey}.
2384 * @param groupKey The group key of the group.
2385 * @return this object for method chaining
2386 */
2387 public Builder setGroup(String groupKey) {
2388 mGroupKey = groupKey;
2389 return this;
2390 }
2391
2392 /**
2393 * Set this notification to be the group summary for a group of notifications.
2394 * Grouped notifications may display in a cluster or stack on devices which
2395 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2396 * @param isGroupSummary Whether this notification should be a group summary.
2397 * @return this object for method chaining
2398 */
2399 public Builder setGroupSummary(boolean isGroupSummary) {
2400 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2401 return this;
2402 }
2403
2404 /**
2405 * Set a sort key that orders this notification among other notifications from the
2406 * same package. This can be useful if an external sort was already applied and an app
2407 * would like to preserve this. Notifications will be sorted lexicographically using this
2408 * value, although providing different priorities in addition to providing sort key may
2409 * cause this value to be ignored.
2410 *
2411 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002412 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002413 *
2414 * @see String#compareTo(String)
2415 */
2416 public Builder setSortKey(String sortKey) {
2417 mSortKey = sortKey;
2418 return this;
2419 }
2420
2421 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002422 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002423 *
Griff Hazen720042b2014-02-24 15:46:56 -08002424 * <p>Values within the Bundle will replace existing extras values in this Builder.
2425 *
2426 * @see Notification#extras
2427 */
Griff Hazen959591e2014-05-15 22:26:18 -07002428 public Builder addExtras(Bundle extras) {
2429 if (extras != null) {
2430 if (mExtras == null) {
2431 mExtras = new Bundle(extras);
2432 } else {
2433 mExtras.putAll(extras);
2434 }
Griff Hazen720042b2014-02-24 15:46:56 -08002435 }
2436 return this;
2437 }
2438
2439 /**
2440 * Set metadata for this notification.
2441 *
2442 * <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 -04002443 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002444 * called.
2445 *
Griff Hazen720042b2014-02-24 15:46:56 -08002446 * <p>Replaces any existing extras values with those from the provided Bundle.
2447 * Use {@link #addExtras} to merge in metadata instead.
2448 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002449 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002450 */
Griff Hazen959591e2014-05-15 22:26:18 -07002451 public Builder setExtras(Bundle extras) {
2452 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002453 return this;
2454 }
2455
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002456 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002457 * Get the current metadata Bundle used by this notification Builder.
2458 *
2459 * <p>The returned Bundle is shared with this Builder.
2460 *
2461 * <p>The current contents of this Bundle are copied into the Notification each time
2462 * {@link #build()} is called.
2463 *
2464 * @see Notification#extras
2465 */
2466 public Bundle getExtras() {
2467 if (mExtras == null) {
2468 mExtras = new Bundle();
2469 }
2470 return mExtras;
2471 }
2472
2473 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002474 * Add an action to this notification. Actions are typically displayed by
2475 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002476 * <p>
2477 * Every action must have an icon (32dp square and matching the
2478 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2479 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2480 * <p>
2481 * A notification in its expanded form can display up to 3 actions, from left to right in
2482 * the order they were added. Actions will not be displayed when the notification is
2483 * collapsed, however, so be sure that any essential functions may be accessed by the user
2484 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002485 *
2486 * @param icon Resource ID of a drawable that represents the action.
2487 * @param title Text describing the action.
2488 * @param intent PendingIntent to be fired when the action is invoked.
2489 */
2490 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002491 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002492 return this;
2493 }
2494
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002495 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002496 * Add an action to this notification. Actions are typically displayed by
2497 * the system as a button adjacent to the notification content.
2498 * <p>
2499 * Every action must have an icon (32dp square and matching the
2500 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2501 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2502 * <p>
2503 * A notification in its expanded form can display up to 3 actions, from left to right in
2504 * the order they were added. Actions will not be displayed when the notification is
2505 * collapsed, however, so be sure that any essential functions may be accessed by the user
2506 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2507 *
2508 * @param action The action to add.
2509 */
2510 public Builder addAction(Action action) {
2511 mActions.add(action);
2512 return this;
2513 }
2514
2515 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002516 * Add a rich notification style to be applied at build time.
2517 *
2518 * @param style Object responsible for modifying the notification style.
2519 */
2520 public Builder setStyle(Style style) {
2521 if (mStyle != style) {
2522 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002523 if (mStyle != null) {
2524 mStyle.setBuilder(this);
2525 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002526 }
2527 return this;
2528 }
2529
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002530 /**
2531 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002532 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002533 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2534 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2535 *
2536 * @return The same Builder.
2537 */
2538 public Builder setVisibility(int visibility) {
2539 mVisibility = visibility;
2540 return this;
2541 }
2542
2543 /**
2544 * Supply a replacement Notification whose contents should be shown in insecure contexts
2545 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2546 * @param n A replacement notification, presumably with some or all info redacted.
2547 * @return The same Builder.
2548 */
2549 public Builder setPublicVersion(Notification n) {
2550 mPublicVersion = n;
2551 return this;
2552 }
2553
Griff Hazenb720abe2014-05-20 13:15:30 -07002554 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002555 * Apply an extender to this notification builder. Extenders may be used to add
2556 * metadata or change options on this builder.
2557 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002558 public Builder extend(Extender extender) {
2559 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002560 return this;
2561 }
2562
Joe Onorato46439ce2010-11-19 13:56:21 -08002563 private void setFlag(int mask, boolean value) {
2564 if (value) {
2565 mFlags |= mask;
2566 } else {
2567 mFlags &= ~mask;
2568 }
2569 }
2570
Dan Sandler26e81cf2014-05-06 10:01:27 -04002571 /**
2572 * Sets {@link Notification#color}.
2573 *
2574 * @param argb The accent color to use
2575 *
2576 * @return The same Builder.
2577 */
2578 public Builder setColor(int argb) {
2579 mColor = argb;
2580 return this;
2581 }
2582
Kenny Guy8a0101b2014-05-08 23:34:12 +01002583 private Bitmap getProfileBadge() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002584 // Note: This assumes that the current user can read the profile badge of the
2585 // originating user.
Kenny Guy8a0101b2014-05-08 23:34:12 +01002586 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Christoph Studer7ac80e62014-08-04 16:01:57 +02002587 Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId));
Kenny Guy8a0101b2014-05-08 23:34:12 +01002588 if (badge == null) {
2589 return null;
2590 }
2591 final int width = badge.getIntrinsicWidth();
2592 final int height = badge.getIntrinsicHeight();
2593 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2594 Canvas canvas = new Canvas(bitmap);
2595 badge.setBounds(0, 0, width, height);
2596 badge.draw(canvas);
2597 return bitmap;
2598 }
2599
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002600 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002601 Bitmap profileIcon = getProfileBadge();
Christoph Studer4600f9b2014-07-22 22:44:43 +02002602 RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002603 boolean showLine3 = false;
2604 boolean showLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002605
Dan Sandler26e81cf2014-05-06 10:01:27 -04002606 if (mPriority < PRIORITY_LOW) {
2607 // TODO: Low priority presentation
Daniel Sandlere95658c2012-05-10 00:33:54 -04002608 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02002609 if (profileIcon != null) {
2610 contentView.setImageViewBitmap(R.id.profile_icon, profileIcon);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002611 contentView.setViewVisibility(R.id.profile_icon, View.VISIBLE);
2612 } else {
2613 contentView.setViewVisibility(R.id.profile_icon, View.GONE);
2614 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002615 if (mLargeIcon != null) {
2616 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002617 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002618 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2619 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2620 processSmallRightIcon(mSmallIcon, contentView);
2621 } else { // small icon at left
2622 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2623 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2624 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002625 }
2626 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002627 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002628 }
2629 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002630 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002631 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002632 }
2633 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002634 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002635 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002636 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002637 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002638 final int tooBig = mContext.getResources().getInteger(
2639 R.integer.status_bar_notification_info_maxnum);
2640 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002641 contentView.setTextViewText(R.id.info, processLegacyText(
2642 mContext.getResources().getString(
2643 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002644 } else {
2645 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002646 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002647 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002648 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002649 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002650 } else {
2651 contentView.setViewVisibility(R.id.info, View.GONE);
2652 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002653
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002654 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002655 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002656 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002657 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002658 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002659 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2660 showLine2 = true;
2661 } else {
2662 contentView.setViewVisibility(R.id.text2, View.GONE);
2663 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002664 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002665 contentView.setViewVisibility(R.id.text2, View.GONE);
2666 if (mProgressMax != 0 || mProgressIndeterminate) {
2667 contentView.setProgressBar(
2668 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2669 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002670 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002671 } else {
2672 contentView.setViewVisibility(R.id.progress, View.GONE);
2673 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002674 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002675 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002676 if (fitIn1U) {
2677 // need to shrink all the type to make sure everything fits
2678 final Resources res = mContext.getResources();
2679 final float subTextSize = res.getDimensionPixelSize(
2680 R.dimen.notification_subtext_size);
2681 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2682 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002683 // vertical centering
2684 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2685 }
2686
Daniel Sandler0c890492012-09-12 17:23:10 -07002687 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002688 if (mUseChronometer) {
2689 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2690 contentView.setLong(R.id.chronometer, "setBase",
2691 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2692 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2693 } else {
2694 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2695 contentView.setLong(R.id.time, "setTime", mWhen);
2696 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002697 } else {
2698 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002699 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002700
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002701 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002702 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002703 return contentView;
2704 }
2705
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002706 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002707 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002708
2709 int N = mActions.size();
2710 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002711 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002712 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002713 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002714 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002715 for (int i=0; i<N; i++) {
2716 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002717 big.addView(R.id.actions, button);
2718 }
2719 }
2720 return big;
2721 }
2722
Joe Onorato46439ce2010-11-19 13:56:21 -08002723 private RemoteViews makeContentView() {
2724 if (mContentView != null) {
2725 return mContentView;
2726 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002727 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002728 }
2729 }
2730
2731 private RemoteViews makeTickerView() {
2732 if (mTickerView != null) {
2733 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002734 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002735 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08002736 }
2737
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002738 private RemoteViews makeBigContentView() {
2739 if (mActions.size() == 0) return null;
2740
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002741 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002742 }
2743
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002744 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002745 if (mActions.size() == 0) return null;
2746
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002747 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002748 }
2749
2750
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002751 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002752 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002753 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002754 tombstone ? getActionTombstoneLayoutResource()
2755 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002756 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002757 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002758 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002759 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002760 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002761 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002762 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002763 return button;
2764 }
2765
Joe Onoratocb109a02011-01-18 17:57:41 -08002766 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002767 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002768 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002769 */
2770 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002771 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002772 }
2773
2774 private void processLegacyAction(Action action, RemoteViews button) {
2775 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002776 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002777 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2778 mContext.getResources().getColor(
2779 R.color.notification_action_legacy_color_filter),
2780 PorterDuff.Mode.MULTIPLY);
2781 }
2782 }
2783 }
2784
2785 private CharSequence processLegacyText(CharSequence charSequence) {
2786 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002787 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002788 } else {
2789 return charSequence;
2790 }
2791 }
2792
Dan Sandler26e81cf2014-05-06 10:01:27 -04002793 /**
2794 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2795 */
2796 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2797 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2798 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002799 }
2800 }
2801
Dan Sandler26e81cf2014-05-06 10:01:27 -04002802 /**
2803 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2804 * if it's grayscale).
2805 */
2806 // TODO: also check bounds, transparency, that sort of thing.
2807 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2808 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2809 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002810 } else {
2811 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002812 }
2813 }
2814
Dan Sandler26e81cf2014-05-06 10:01:27 -04002815 /**
2816 * Add a colored circle behind the largeIcon slot.
2817 */
2818 private void applyLargeIconBackground(RemoteViews contentView) {
2819 contentView.setInt(R.id.icon, "setBackgroundResource",
2820 R.drawable.notification_icon_legacy_bg_inset);
2821
2822 contentView.setDrawableParameters(
2823 R.id.icon,
2824 true,
2825 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002826 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002827 PorterDuff.Mode.SRC_ATOP,
2828 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002829 }
2830
Dan Sandler190d58d2014-05-15 09:33:39 -04002831 private void removeLargeIconBackground(RemoteViews contentView) {
2832 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2833 }
2834
Dan Sandler26e81cf2014-05-06 10:01:27 -04002835 /**
2836 * Recolor small icons when used in the R.id.right_icon slot.
2837 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002838 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002839 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002840 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002841 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2842 0xFFFFFFFF,
2843 PorterDuff.Mode.SRC_ATOP, -1);
2844
2845 contentView.setInt(R.id.right_icon,
2846 "setBackgroundResource",
2847 R.drawable.notification_icon_legacy_bg);
2848
2849 contentView.setDrawableParameters(
2850 R.id.right_icon,
2851 true,
2852 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002853 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04002854 PorterDuff.Mode.SRC_ATOP,
2855 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002856 }
2857 }
2858
Jorim Jaggi74419312014-06-10 20:57:21 +02002859 private int sanitizeColor() {
2860 if (mColor != COLOR_DEFAULT) {
2861 mColor |= 0xFF000000; // no alpha for custom colors
2862 }
2863 return mColor;
2864 }
2865
Dan Sandler26e81cf2014-05-06 10:01:27 -04002866 private int resolveColor() {
2867 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02002868 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002869 }
2870 return mColor;
2871 }
2872
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002873 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002874 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002875 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002876 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002877 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002878 Notification n = new Notification();
2879 n.when = mWhen;
2880 n.icon = mSmallIcon;
2881 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002882 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002883
Jorim Jaggi74419312014-06-10 20:57:21 +02002884 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002885
Christoph Studer4600f9b2014-07-22 22:44:43 +02002886 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08002887 n.contentIntent = mContentIntent;
2888 n.deleteIntent = mDeleteIntent;
2889 n.fullScreenIntent = mFullScreenIntent;
2890 n.tickerText = mTickerText;
2891 n.tickerView = makeTickerView();
2892 n.largeIcon = mLargeIcon;
2893 n.sound = mSound;
2894 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002895 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002896 n.vibrate = mVibrate;
2897 n.ledARGB = mLedArgb;
2898 n.ledOnMS = mLedOnMs;
2899 n.ledOffMS = mLedOffMs;
2900 n.defaults = mDefaults;
2901 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002902 setBuilderBigContentView(n, makeBigContentView());
2903 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04002904 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002905 n.flags |= FLAG_SHOW_LIGHTS;
2906 }
2907 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2908 n.flags |= FLAG_SHOW_LIGHTS;
2909 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002910 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002911 n.mGroupKey = mGroupKey;
2912 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002913 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002914 if (mActions.size() > 0) {
2915 n.actions = new Action[mActions.size()];
2916 mActions.toArray(n.actions);
2917 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002918 n.visibility = mVisibility;
2919
2920 if (mPublicVersion != null) {
2921 n.publicVersion = new Notification();
2922 mPublicVersion.cloneInto(n.publicVersion, true);
2923 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002924 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08002925 return n;
2926 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002927
2928 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002929 * Capture, in the provided bundle, semantic information used in the construction of
2930 * this Notification object.
2931 * @hide
2932 */
Griff Hazen720042b2014-02-24 15:46:56 -08002933 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002934 // Store original information used in the construction of this object
Christoph Studer7ac80e62014-08-04 16:01:57 +02002935 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002936 extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002937 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2938 extras.putCharSequence(EXTRA_TEXT, mContentText);
2939 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2940 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2941 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2942 extras.putInt(EXTRA_PROGRESS, mProgress);
2943 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2944 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2945 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2946 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002947 if (mLargeIcon != null) {
2948 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2949 }
Chris Wrendde75302014-03-26 17:24:15 -04002950 if (!mPeople.isEmpty()) {
2951 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2952 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002953 // NOTE: If you're adding new extras also update restoreFromNotification().
2954 }
2955
2956
2957 /**
2958 * @hide
2959 */
2960 public static void stripForDelivery(Notification n) {
2961 if (!STRIP_AND_REBUILD) {
2962 return;
2963 }
2964
2965 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
2966 // Only strip views for known Styles because we won't know how to
2967 // re-create them otherwise.
2968 boolean stripViews = TextUtils.isEmpty(templateClass) ||
2969 getNotificationStyleClass(templateClass) != null;
2970
2971 boolean isStripped = false;
2972
2973 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
2974 // TODO: Would like to check for equality here, but if the notification
2975 // has been cloned, we can't.
2976 n.largeIcon = null;
2977 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
2978 isStripped = true;
2979 }
2980 // Get rid of unmodified BuilderRemoteViews.
2981
2982 if (stripViews &&
2983 n.contentView instanceof BuilderRemoteViews &&
2984 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
2985 n.contentView.getSequenceNumber()) {
2986 n.contentView = null;
2987 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
2988 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
2989 isStripped = true;
2990 }
2991 if (stripViews &&
2992 n.bigContentView instanceof BuilderRemoteViews &&
2993 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
2994 n.bigContentView.getSequenceNumber()) {
2995 n.bigContentView = null;
2996 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
2997 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
2998 isStripped = true;
2999 }
3000 if (stripViews &&
3001 n.headsUpContentView instanceof BuilderRemoteViews &&
3002 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3003 n.headsUpContentView.getSequenceNumber()) {
3004 n.headsUpContentView = null;
3005 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3006 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3007 isStripped = true;
3008 }
3009
3010 if (isStripped) {
3011 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3012 }
3013 }
3014
3015 /**
3016 * @hide
3017 */
3018 public static Notification rebuild(Context context, Notification n) {
3019 Bundle extras = n.extras;
3020 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3021 extras.remove(EXTRA_NEEDS_REBUILD);
3022
3023 // Re-create notification context so we can access app resources.
3024 String packageName = extras.getString(EXTRA_REBUILD_CONTEXT_PACKAGE);
3025 Context builderContext;
3026 try {
3027 builderContext = context.createPackageContext(packageName,
3028 Context.CONTEXT_RESTRICTED);
3029 } catch (NameNotFoundException e) {
3030 Log.e(TAG, "Package name " + packageName + " not found");
3031 builderContext = context; // try with our context
3032 }
3033
3034 Builder b = new Builder(builderContext, n);
3035 return b.rebuild();
3036 }
3037
3038 /**
3039 * Rebuilds the notification passed in to the rebuild-constructor
3040 * {@link #Builder(Context, Notification)}.
3041 *
3042 * <p>
3043 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3044 *
3045 * @hide
3046 */
3047 private Notification rebuild() {
3048 if (mRebuildNotification == null) {
3049 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3050 }
3051
3052 Bundle extras = mRebuildNotification.extras;
3053
3054 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3055 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3056 }
3057 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3058
3059 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3060 setBuilderContentView(mRebuildNotification, makeContentView());
3061 if (mStyle != null) {
3062 mStyle.populateContentView(mRebuildNotification);
3063 }
3064 }
3065 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3066
3067 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3068 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3069 if (mStyle != null) {
3070 mStyle.populateBigContentView(mRebuildNotification);
3071 }
3072 }
3073 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3074
3075 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3076 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3077 if (mStyle != null) {
3078 mStyle.populateHeadsUpContentView(mRebuildNotification);
3079 }
3080 }
3081 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3082
3083 return mRebuildNotification;
3084 }
3085
3086 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3087 Class<? extends Style>[] classes = new Class[]{
3088 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3089 for (Class<? extends Style> innerClass : classes) {
3090 if (templateClass.equals(innerClass.getName())) {
3091 return innerClass;
3092 }
3093 }
3094 return null;
3095 }
3096
3097 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3098 n.contentView = contentView;
3099 if (contentView instanceof BuilderRemoteViews) {
3100 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3101 contentView.getSequenceNumber());
3102 }
3103 }
3104
3105 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3106 n.bigContentView = bigContentView;
3107 if (bigContentView instanceof BuilderRemoteViews) {
3108 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3109 bigContentView.getSequenceNumber());
3110 }
3111 }
3112
3113 private void setBuilderHeadsUpContentView(Notification n,
3114 RemoteViews headsUpContentView) {
3115 n.headsUpContentView = headsUpContentView;
3116 if (headsUpContentView instanceof BuilderRemoteViews) {
3117 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3118 headsUpContentView.getSequenceNumber());
3119 }
3120 }
3121
3122 private void restoreFromNotification(Notification n) {
3123
3124 // Notification fields.
3125 mWhen = n.when;
3126 mSmallIcon = n.icon;
3127 mSmallIconLevel = n.iconLevel;
3128 mNumber = n.number;
3129
3130 mColor = n.color;
3131
3132 mContentView = n.contentView;
3133 mDeleteIntent = n.deleteIntent;
3134 mFullScreenIntent = n.fullScreenIntent;
3135 mTickerText = n.tickerText;
3136 mTickerView = n.tickerView;
3137 mLargeIcon = n.largeIcon;
3138 mSound = n.sound;
3139 mAudioStreamType = n.audioStreamType;
3140 mAudioAttributes = n.audioAttributes;
3141
3142 mVibrate = n.vibrate;
3143 mLedArgb = n.ledARGB;
3144 mLedOnMs = n.ledOnMS;
3145 mLedOffMs = n.ledOffMS;
3146 mDefaults = n.defaults;
3147 mFlags = n.flags;
3148
3149 mCategory = n.category;
3150 mGroupKey = n.mGroupKey;
3151 mSortKey = n.mSortKey;
3152 mPriority = n.priority;
3153 mActions.clear();
3154 if (n.actions != null) {
3155 Collections.addAll(mActions, n.actions);
3156 }
3157 mVisibility = n.visibility;
3158
3159 mPublicVersion = n.publicVersion;
3160
3161 // Extras.
3162 Bundle extras = n.extras;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003163 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003164 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3165 mContentText = extras.getCharSequence(EXTRA_TEXT);
3166 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3167 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
3168 mSmallIcon = extras.getInt(EXTRA_SMALL_ICON);
3169 mProgress = extras.getInt(EXTRA_PROGRESS);
3170 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3171 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3172 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3173 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3174 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3175 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3176 }
3177 if (extras.containsKey(EXTRA_PEOPLE)) {
3178 mPeople.clear();
3179 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3180 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003181 }
3182
3183 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003184 * @deprecated Use {@link #build()} instead.
3185 */
3186 @Deprecated
3187 public Notification getNotification() {
3188 return build();
3189 }
3190
3191 /**
3192 * Combine all of the options that have been set and return a new {@link Notification}
3193 * object.
3194 */
3195 public Notification build() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003196 mOriginatingUserId = mContext.getUserId();
Christoph Studer943aa672014-08-03 20:31:16 +02003197
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003198 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003199
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003200 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003201 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003202 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003203
Christoph Studer4600f9b2014-07-22 22:44:43 +02003204 if (mExtras != null) {
3205 n.extras.putAll(mExtras);
3206 }
3207
3208 if (mRebuildBundle.size() > 0) {
3209 n.extras.putAll(mRebuildBundle);
3210 mRebuildBundle.clear();
3211 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003212
Griff Hazen720042b2014-02-24 15:46:56 -08003213 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003214 if (mStyle != null) {
3215 mStyle.addExtras(n.extras);
3216 }
3217
3218 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003219 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003220
3221 /**
3222 * Apply this Builder to an existing {@link Notification} object.
3223 *
3224 * @hide
3225 */
3226 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003227 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003228 return n;
3229 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003230
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003231 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003232 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003233 }
3234
3235 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003236 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003237 }
3238
3239 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003240 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003241 }
3242
3243 private int getBigTextLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003244 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003245 }
3246
3247 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003248 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003249 }
3250
3251 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003252 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003253 }
3254
3255 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003256 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003257 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003258 }
3259
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003260 /**
3261 * An object that can apply a rich notification style to a {@link Notification.Builder}
3262 * object.
3263 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003264 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003265 private CharSequence mBigContentTitle;
3266 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04003267 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003268
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003269 protected Builder mBuilder;
3270
Chris Wrend6297db2012-05-03 16:20:13 -04003271 /**
3272 * Overrides ContentTitle in the big form of the template.
3273 * This defaults to the value passed to setContentTitle().
3274 */
3275 protected void internalSetBigContentTitle(CharSequence title) {
3276 mBigContentTitle = title;
3277 }
3278
3279 /**
3280 * Set the first line of text after the detail section in the big form of the template.
3281 */
3282 protected void internalSetSummaryText(CharSequence cs) {
3283 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003284 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003285 }
3286
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003287 public void setBuilder(Builder builder) {
3288 if (mBuilder != builder) {
3289 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003290 if (mBuilder != null) {
3291 mBuilder.setStyle(this);
3292 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003293 }
3294 }
3295
Chris Wrend6297db2012-05-03 16:20:13 -04003296 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003297 if (mBuilder == null) {
3298 throw new IllegalArgumentException("Style requires a valid Builder object");
3299 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003300 }
Chris Wrend6297db2012-05-03 16:20:13 -04003301
3302 protected RemoteViews getStandardView(int layoutId) {
3303 checkBuilder();
3304
Christoph Studer4600f9b2014-07-22 22:44:43 +02003305 // Nasty.
3306 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003307 if (mBigContentTitle != null) {
3308 mBuilder.setContentTitle(mBigContentTitle);
3309 }
3310
Chris Wrend6297db2012-05-03 16:20:13 -04003311 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3312
Christoph Studer4600f9b2014-07-22 22:44:43 +02003313 mBuilder.mContentTitle = oldBuilderContentTitle;
3314
Chris Wrend6297db2012-05-03 16:20:13 -04003315 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3316 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003317 } else {
3318 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003319 }
3320
Daniel Sandler619738c2012-06-07 16:33:08 -04003321 // The last line defaults to the subtext, but can be replaced by mSummaryText
3322 final CharSequence overflowText =
3323 mSummaryTextSet ? mSummaryText
3324 : mBuilder.mSubText;
3325 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003326 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003327 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003328 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003329 } else {
3330 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3331 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003332 }
3333
3334 return contentView;
3335 }
3336
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003337 /**
3338 * @hide
3339 */
3340 public void addExtras(Bundle extras) {
3341 if (mSummaryTextSet) {
3342 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3343 }
3344 if (mBigContentTitle != null) {
3345 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3346 }
Chris Wren91ad5632013-06-05 15:05:57 -04003347 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003348 }
3349
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003350 /**
3351 * @hide
3352 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003353 protected void restoreFromExtras(Bundle extras) {
3354 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3355 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3356 mSummaryTextSet = true;
3357 }
3358 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3359 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3360 }
3361 }
3362
3363
3364 /**
3365 * @hide
3366 */
3367 public Notification buildStyled(Notification wip) {
3368 populateTickerView(wip);
3369 populateContentView(wip);
3370 populateBigContentView(wip);
3371 populateHeadsUpContentView(wip);
3372 return wip;
3373 }
3374
3375 // The following methods are split out so we can re-create notification partially.
3376 /**
3377 * @hide
3378 */
3379 protected void populateTickerView(Notification wip) {}
3380 /**
3381 * @hide
3382 */
3383 protected void populateContentView(Notification wip) {}
3384
3385 /**
3386 * @hide
3387 */
3388 protected void populateBigContentView(Notification wip) {}
3389
3390 /**
3391 * @hide
3392 */
3393 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003394
3395 /**
3396 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3397 * attached to.
3398 *
3399 * @return the fully constructed Notification.
3400 */
3401 public Notification build() {
3402 checkBuilder();
3403 return mBuilder.build();
3404 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003405 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003406
3407 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003408 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003409 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003410 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003411 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003412 * Notification notif = new Notification.Builder(mContext)
3413 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3414 * .setContentText(subject)
3415 * .setSmallIcon(R.drawable.new_post)
3416 * .setLargeIcon(aBitmap)
3417 * .setStyle(new Notification.BigPictureStyle()
3418 * .bigPicture(aBigBitmap))
3419 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003420 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003421 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003422 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003423 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003424 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003425 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04003426 private Bitmap mBigLargeIcon;
3427 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003428
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003429 public BigPictureStyle() {
3430 }
3431
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003432 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003433 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003434 }
3435
Chris Wrend6297db2012-05-03 16:20:13 -04003436 /**
3437 * Overrides ContentTitle in the big form of the template.
3438 * This defaults to the value passed to setContentTitle().
3439 */
3440 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003441 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003442 return this;
3443 }
3444
3445 /**
3446 * Set the first line of text after the detail section in the big form of the template.
3447 */
3448 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003449 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003450 return this;
3451 }
3452
Chris Wren0bd664d2012-08-01 13:56:56 -04003453 /**
3454 * Provide the bitmap to be used as the payload for the BigPicture notification.
3455 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003456 public BigPictureStyle bigPicture(Bitmap b) {
3457 mPicture = b;
3458 return this;
3459 }
3460
Chris Wren3745a3d2012-05-22 15:11:52 -04003461 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003462 * Override the large icon when the big notification is shown.
3463 */
3464 public BigPictureStyle bigLargeIcon(Bitmap b) {
3465 mBigLargeIconSet = true;
3466 mBigLargeIcon = b;
3467 return this;
3468 }
3469
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003470 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003471 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003472
3473 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3474
3475 return contentView;
3476 }
3477
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003478 /**
3479 * @hide
3480 */
3481 public void addExtras(Bundle extras) {
3482 super.addExtras(extras);
3483
3484 if (mBigLargeIconSet) {
3485 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3486 }
3487 extras.putParcelable(EXTRA_PICTURE, mPicture);
3488 }
3489
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003490 /**
3491 * @hide
3492 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003493 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003494 protected void restoreFromExtras(Bundle extras) {
3495 super.restoreFromExtras(extras);
3496
3497 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
3498 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003499 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003500 mPicture = extras.getParcelable(EXTRA_PICTURE);
3501 }
3502
3503 /**
3504 * @hide
3505 */
3506 @Override
3507 public void populateBigContentView(Notification wip) {
3508 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003509 }
3510 }
3511
3512 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003513 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003514 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003515 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003516 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003517 * Notification notif = new Notification.Builder(mContext)
3518 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3519 * .setContentText(subject)
3520 * .setSmallIcon(R.drawable.new_mail)
3521 * .setLargeIcon(aBitmap)
3522 * .setStyle(new Notification.BigTextStyle()
3523 * .bigText(aVeryLongString))
3524 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003525 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003526 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003527 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003528 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003529 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003530 private CharSequence mBigText;
3531
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003532 public BigTextStyle() {
3533 }
3534
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003535 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003536 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003537 }
3538
Chris Wrend6297db2012-05-03 16:20:13 -04003539 /**
3540 * Overrides ContentTitle in the big form of the template.
3541 * This defaults to the value passed to setContentTitle().
3542 */
3543 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003544 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003545 return this;
3546 }
3547
3548 /**
3549 * Set the first line of text after the detail section in the big form of the template.
3550 */
3551 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003552 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003553 return this;
3554 }
3555
Chris Wren0bd664d2012-08-01 13:56:56 -04003556 /**
3557 * Provide the longer text to be displayed in the big form of the
3558 * template in place of the content text.
3559 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003560 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003561 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003562 return this;
3563 }
3564
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003565 /**
3566 * @hide
3567 */
3568 public void addExtras(Bundle extras) {
3569 super.addExtras(extras);
3570
Christoph Studer4600f9b2014-07-22 22:44:43 +02003571 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
3572 }
3573
3574 /**
3575 * @hide
3576 */
3577 @Override
3578 protected void restoreFromExtras(Bundle extras) {
3579 super.restoreFromExtras(extras);
3580
3581 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003582 }
3583
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003584 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003585 // Remove the content text so line3 only shows if you have a summary
3586 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003587
3588 // Nasty
3589 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003590 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003591
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003592 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003593
Christoph Studer4600f9b2014-07-22 22:44:43 +02003594 mBuilder.mContentText = oldBuilderContentText;
3595
Daniel Sandler619738c2012-06-07 16:33:08 -04003596 if (hadThreeLines) {
3597 // vertical centering
3598 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
3599 }
3600
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003601 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003602 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003603 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003604
3605 return contentView;
3606 }
3607
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003608 /**
3609 * @hide
3610 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003611 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003612 public void populateBigContentView(Notification wip) {
3613 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003614 }
3615 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003616
3617 /**
3618 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003619 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003620 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003621 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003622 * Notification notif = new Notification.Builder(mContext)
3623 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3624 * .setContentText(subject)
3625 * .setSmallIcon(R.drawable.new_mail)
3626 * .setLargeIcon(aBitmap)
3627 * .setStyle(new Notification.InboxStyle()
3628 * .addLine(str1)
3629 * .addLine(str2)
3630 * .setContentTitle(&quot;&quot;)
3631 * .setSummaryText(&quot;+3 more&quot;))
3632 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003633 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003634 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003635 * @see Notification#bigContentView
3636 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003637 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003638 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3639
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003640 public InboxStyle() {
3641 }
3642
Daniel Sandler879c5e02012-04-17 16:46:51 -04003643 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003644 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003645 }
3646
Chris Wrend6297db2012-05-03 16:20:13 -04003647 /**
3648 * Overrides ContentTitle in the big form of the template.
3649 * This defaults to the value passed to setContentTitle().
3650 */
3651 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003652 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003653 return this;
3654 }
3655
3656 /**
3657 * Set the first line of text after the detail section in the big form of the template.
3658 */
3659 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003660 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003661 return this;
3662 }
3663
Chris Wren0bd664d2012-08-01 13:56:56 -04003664 /**
3665 * Append a line to the digest section of the Inbox notification.
3666 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003667 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003668 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003669 return this;
3670 }
3671
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003672 /**
3673 * @hide
3674 */
3675 public void addExtras(Bundle extras) {
3676 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003677
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003678 CharSequence[] a = new CharSequence[mTexts.size()];
3679 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3680 }
3681
Christoph Studer4600f9b2014-07-22 22:44:43 +02003682 /**
3683 * @hide
3684 */
3685 @Override
3686 protected void restoreFromExtras(Bundle extras) {
3687 super.restoreFromExtras(extras);
3688
3689 mTexts.clear();
3690 if (extras.containsKey(EXTRA_TEXT_LINES)) {
3691 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
3692 }
3693 }
3694
Daniel Sandler879c5e02012-04-17 16:46:51 -04003695 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003696 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02003697
3698 // Nasty
3699 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04003700 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003701
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003702 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003703
Christoph Studer4600f9b2014-07-22 22:44:43 +02003704 mBuilder.mContentText = oldBuilderContentText;
3705
Chris Wrend6297db2012-05-03 16:20:13 -04003706 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003707
Chris Wrend6297db2012-05-03 16:20:13 -04003708 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 -04003709 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003710
Chris Wren4ed80d52012-05-17 09:30:03 -04003711 // Make sure all rows are gone in case we reuse a view.
3712 for (int rowId : rowIds) {
3713 contentView.setViewVisibility(rowId, View.GONE);
3714 }
3715
Chris Wren683ab002012-09-20 10:35:54 -04003716
Daniel Sandler879c5e02012-04-17 16:46:51 -04003717 int i=0;
3718 while (i < mTexts.size() && i < rowIds.length) {
3719 CharSequence str = mTexts.get(i);
3720 if (str != null && !str.equals("")) {
3721 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003722 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003723 }
3724 i++;
3725 }
3726
Chris Wren683ab002012-09-20 10:35:54 -04003727 contentView.setViewVisibility(R.id.inbox_end_pad,
3728 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3729
3730 contentView.setViewVisibility(R.id.inbox_more,
3731 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003732
Daniel Sandler879c5e02012-04-17 16:46:51 -04003733 return contentView;
3734 }
3735
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003736 /**
3737 * @hide
3738 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003739 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003740 public void populateBigContentView(Notification wip) {
3741 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04003742 }
3743 }
Dan Sandler842dd772014-05-15 09:36:47 -04003744
3745 /**
3746 * Notification style for media playback notifications.
3747 *
3748 * In the expanded form, {@link Notification#bigContentView}, up to 5
3749 * {@link Notification.Action}s specified with
3750 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3751 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3752 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3753 * treated as album artwork.
3754 *
3755 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3756 * {@link Notification#contentView}; by providing action indices to
3757 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3758 * in the standard view alongside the usual content.
3759 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003760 * Notifications created with MediaStyle will have their category set to
3761 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
3762 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
3763 *
Jeff Browndba34ba2014-06-24 20:46:03 -07003764 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
3765 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04003766 * the System UI can identify this as a notification representing an active media session
3767 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3768 *
3769 * To use this style with your Notification, feed it to
3770 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3771 * <pre class="prettyprint">
3772 * Notification noti = new Notification.Builder()
3773 * .setSmallIcon(R.drawable.ic_stat_player)
3774 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3775 * .setContentText(&quot;Artist - Album&quot;) // if you use
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003776 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token)
3777 * .setStyle(<b>new Notification.MediaStyle()</b>
3778 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04003779 * .build();
3780 * </pre>
3781 *
3782 * @see Notification#bigContentView
3783 */
3784 public static class MediaStyle extends Style {
3785 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3786 static final int MAX_MEDIA_BUTTONS = 5;
3787
3788 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07003789 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04003790
3791 public MediaStyle() {
3792 }
3793
3794 public MediaStyle(Builder builder) {
3795 setBuilder(builder);
3796 }
3797
3798 /**
3799 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3800 * notification view.
3801 */
3802 public MediaStyle setShowActionsInCompactView(int...actions) {
3803 mActionsToShowInCompact = actions;
3804 return this;
3805 }
3806
3807 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07003808 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
3809 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04003810 */
Jeff Browndba34ba2014-06-24 20:46:03 -07003811 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04003812 mToken = token;
3813 return this;
3814 }
3815
Christoph Studer4600f9b2014-07-22 22:44:43 +02003816 /**
3817 * @hide
3818 */
Dan Sandler842dd772014-05-15 09:36:47 -04003819 @Override
3820 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003821 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003822 if (wip.category == null) {
3823 wip.category = Notification.CATEGORY_TRANSPORT;
3824 }
Dan Sandler842dd772014-05-15 09:36:47 -04003825 return wip;
3826 }
3827
Christoph Studer4600f9b2014-07-22 22:44:43 +02003828 /**
3829 * @hide
3830 */
3831 @Override
3832 public void populateContentView(Notification wip) {
3833 mBuilder.setBuilderContentView(wip, makeMediaContentView());
3834 }
3835
3836 /**
3837 * @hide
3838 */
3839 @Override
3840 public void populateBigContentView(Notification wip) {
3841 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
3842 }
3843
Dan Sandler842dd772014-05-15 09:36:47 -04003844 /** @hide */
3845 @Override
3846 public void addExtras(Bundle extras) {
3847 super.addExtras(extras);
3848
3849 if (mToken != null) {
3850 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3851 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01003852 if (mActionsToShowInCompact != null) {
3853 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
3854 }
Dan Sandler842dd772014-05-15 09:36:47 -04003855 }
3856
Christoph Studer4600f9b2014-07-22 22:44:43 +02003857 /**
3858 * @hide
3859 */
3860 @Override
3861 protected void restoreFromExtras(Bundle extras) {
3862 super.restoreFromExtras(extras);
3863
3864 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
3865 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
3866 }
3867 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
3868 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
3869 }
3870 }
3871
Dan Sandler842dd772014-05-15 09:36:47 -04003872 private RemoteViews generateMediaActionButton(Action action) {
3873 final boolean tombstone = (action.actionIntent == null);
3874 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07003875 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04003876 button.setImageViewResource(R.id.action0, action.icon);
3877 if (!tombstone) {
3878 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3879 }
3880 button.setContentDescription(R.id.action0, action.title);
3881 return button;
3882 }
3883
3884 private RemoteViews makeMediaContentView() {
3885 RemoteViews view = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003886 R.layout.notification_template_material_media, true /* 1U */);
Dan Sandler842dd772014-05-15 09:36:47 -04003887
3888 final int numActions = mBuilder.mActions.size();
3889 final int N = mActionsToShowInCompact == null
3890 ? 0
3891 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3892 if (N > 0) {
3893 view.removeAllViews(R.id.actions);
3894 for (int i = 0; i < N; i++) {
3895 if (i >= numActions) {
3896 throw new IllegalArgumentException(String.format(
3897 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3898 i, numActions - 1));
3899 }
3900
3901 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3902 final RemoteViews button = generateMediaActionButton(action);
3903 view.addView(R.id.actions, button);
3904 }
3905 }
3906 return view;
3907 }
3908
3909 private RemoteViews makeMediaBigContentView() {
3910 RemoteViews big = mBuilder.applyStandardTemplate(
Alan Viverette3cb07a462014-06-06 14:19:53 -07003911 R.layout.notification_template_material_big_media, false);
Dan Sandler842dd772014-05-15 09:36:47 -04003912
3913 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3914 if (N > 0) {
3915 big.removeAllViews(R.id.actions);
3916 for (int i=0; i<N; i++) {
3917 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3918 big.addView(R.id.actions, button);
3919 }
3920 }
3921 return big;
3922 }
3923 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003924
Christoph Studer4600f9b2014-07-22 22:44:43 +02003925 // When adding a new Style subclass here, don't forget to update
3926 // Builder.getNotificationStyleClass.
3927
Griff Hazen61a9e862014-05-22 16:05:19 -07003928 /**
3929 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
3930 * metadata or change options on a notification builder.
3931 */
3932 public interface Extender {
3933 /**
3934 * Apply this extender to a notification builder.
3935 * @param builder the builder to be modified.
3936 * @return the build object for chaining.
3937 */
3938 public Builder extend(Builder builder);
3939 }
3940
3941 /**
3942 * Helper class to add wearable extensions to notifications.
3943 * <p class="note"> See
3944 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
3945 * for Android Wear</a> for more information on how to use this class.
3946 * <p>
3947 * To create a notification with wearable extensions:
3948 * <ol>
3949 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
3950 * properties.
3951 * <li>Create a {@link android.app.Notification.WearableExtender}.
3952 * <li>Set wearable-specific properties using the
3953 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
3954 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
3955 * notification.
3956 * <li>Post the notification to the notification system with the
3957 * {@code NotificationManager.notify(...)} methods.
3958 * </ol>
3959 *
3960 * <pre class="prettyprint">
3961 * Notification notif = new Notification.Builder(mContext)
3962 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3963 * .setContentText(subject)
3964 * .setSmallIcon(R.drawable.new_mail)
3965 * .extend(new Notification.WearableExtender()
3966 * .setContentIcon(R.drawable.new_mail))
3967 * .build();
3968 * NotificationManager notificationManger =
3969 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3970 * notificationManger.notify(0, notif);</pre>
3971 *
3972 * <p>Wearable extensions can be accessed on an existing notification by using the
3973 * {@code WearableExtender(Notification)} constructor,
3974 * and then using the {@code get} methods to access values.
3975 *
3976 * <pre class="prettyprint">
3977 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
3978 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07003979 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003980 */
3981 public static final class WearableExtender implements Extender {
3982 /**
3983 * Sentinel value for an action index that is unset.
3984 */
3985 public static final int UNSET_ACTION_INDEX = -1;
3986
3987 /**
3988 * Size value for use with {@link #setCustomSizePreset} to show this notification with
3989 * default sizing.
3990 * <p>For custom display notifications created using {@link #setDisplayIntent},
3991 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
3992 * on their content.
3993 */
3994 public static final int SIZE_DEFAULT = 0;
3995
3996 /**
3997 * Size value for use with {@link #setCustomSizePreset} to show this notification
3998 * with an extra small size.
3999 * <p>This value is only applicable for custom display notifications created using
4000 * {@link #setDisplayIntent}.
4001 */
4002 public static final int SIZE_XSMALL = 1;
4003
4004 /**
4005 * Size value for use with {@link #setCustomSizePreset} to show this notification
4006 * with a small size.
4007 * <p>This value is only applicable for custom display notifications created using
4008 * {@link #setDisplayIntent}.
4009 */
4010 public static final int SIZE_SMALL = 2;
4011
4012 /**
4013 * Size value for use with {@link #setCustomSizePreset} to show this notification
4014 * with a medium size.
4015 * <p>This value is only applicable for custom display notifications created using
4016 * {@link #setDisplayIntent}.
4017 */
4018 public static final int SIZE_MEDIUM = 3;
4019
4020 /**
4021 * Size value for use with {@link #setCustomSizePreset} to show this notification
4022 * with a large size.
4023 * <p>This value is only applicable for custom display notifications created using
4024 * {@link #setDisplayIntent}.
4025 */
4026 public static final int SIZE_LARGE = 4;
4027
Griff Hazend5f11f92014-05-27 15:40:09 -07004028 /**
4029 * Size value for use with {@link #setCustomSizePreset} to show this notification
4030 * full screen.
4031 * <p>This value is only applicable for custom display notifications created using
4032 * {@link #setDisplayIntent}.
4033 */
4034 public static final int SIZE_FULL_SCREEN = 5;
4035
Griff Hazen61a9e862014-05-22 16:05:19 -07004036 /** Notification extra which contains wearable extensions */
4037 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4038
4039 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
4040 private static final String KEY_ACTIONS = "actions";
4041 private static final String KEY_FLAGS = "flags";
4042 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4043 private static final String KEY_PAGES = "pages";
4044 private static final String KEY_BACKGROUND = "background";
4045 private static final String KEY_CONTENT_ICON = "contentIcon";
4046 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4047 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4048 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4049 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4050 private static final String KEY_GRAVITY = "gravity";
4051
4052 // Flags bitwise-ored to mFlags
4053 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4054 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4055 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4056 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
4057
4058 // Default value for flags integer
4059 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4060
4061 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4062 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4063
4064 private ArrayList<Action> mActions = new ArrayList<Action>();
4065 private int mFlags = DEFAULT_FLAGS;
4066 private PendingIntent mDisplayIntent;
4067 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4068 private Bitmap mBackground;
4069 private int mContentIcon;
4070 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4071 private int mContentActionIndex = UNSET_ACTION_INDEX;
4072 private int mCustomSizePreset = SIZE_DEFAULT;
4073 private int mCustomContentHeight;
4074 private int mGravity = DEFAULT_GRAVITY;
4075
4076 /**
4077 * Create a {@link android.app.Notification.WearableExtender} with default
4078 * options.
4079 */
4080 public WearableExtender() {
4081 }
4082
4083 public WearableExtender(Notification notif) {
4084 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4085 if (wearableBundle != null) {
4086 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4087 if (actions != null) {
4088 mActions.addAll(actions);
4089 }
4090
4091 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4092 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4093
4094 Notification[] pages = getNotificationArrayFromBundle(
4095 wearableBundle, KEY_PAGES);
4096 if (pages != null) {
4097 Collections.addAll(mPages, pages);
4098 }
4099
4100 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4101 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4102 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4103 DEFAULT_CONTENT_ICON_GRAVITY);
4104 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4105 UNSET_ACTION_INDEX);
4106 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4107 SIZE_DEFAULT);
4108 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4109 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
4110 }
4111 }
4112
4113 /**
4114 * Apply wearable extensions to a notification that is being built. This is typically
4115 * called by the {@link android.app.Notification.Builder#extend} method of
4116 * {@link android.app.Notification.Builder}.
4117 */
4118 @Override
4119 public Notification.Builder extend(Notification.Builder builder) {
4120 Bundle wearableBundle = new Bundle();
4121
4122 if (!mActions.isEmpty()) {
4123 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4124 }
4125 if (mFlags != DEFAULT_FLAGS) {
4126 wearableBundle.putInt(KEY_FLAGS, mFlags);
4127 }
4128 if (mDisplayIntent != null) {
4129 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4130 }
4131 if (!mPages.isEmpty()) {
4132 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4133 new Notification[mPages.size()]));
4134 }
4135 if (mBackground != null) {
4136 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4137 }
4138 if (mContentIcon != 0) {
4139 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4140 }
4141 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4142 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4143 }
4144 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4145 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4146 mContentActionIndex);
4147 }
4148 if (mCustomSizePreset != SIZE_DEFAULT) {
4149 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4150 }
4151 if (mCustomContentHeight != 0) {
4152 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4153 }
4154 if (mGravity != DEFAULT_GRAVITY) {
4155 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4156 }
4157
4158 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4159 return builder;
4160 }
4161
4162 @Override
4163 public WearableExtender clone() {
4164 WearableExtender that = new WearableExtender();
4165 that.mActions = new ArrayList<Action>(this.mActions);
4166 that.mFlags = this.mFlags;
4167 that.mDisplayIntent = this.mDisplayIntent;
4168 that.mPages = new ArrayList<Notification>(this.mPages);
4169 that.mBackground = this.mBackground;
4170 that.mContentIcon = this.mContentIcon;
4171 that.mContentIconGravity = this.mContentIconGravity;
4172 that.mContentActionIndex = this.mContentActionIndex;
4173 that.mCustomSizePreset = this.mCustomSizePreset;
4174 that.mCustomContentHeight = this.mCustomContentHeight;
4175 that.mGravity = this.mGravity;
4176 return that;
4177 }
4178
4179 /**
4180 * Add a wearable action to this notification.
4181 *
4182 * <p>When wearable actions are added using this method, the set of actions that
4183 * show on a wearable device splits from devices that only show actions added
4184 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4185 * of which actions display on different devices.
4186 *
4187 * @param action the action to add to this notification
4188 * @return this object for method chaining
4189 * @see android.app.Notification.Action
4190 */
4191 public WearableExtender addAction(Action action) {
4192 mActions.add(action);
4193 return this;
4194 }
4195
4196 /**
4197 * Adds wearable actions to this notification.
4198 *
4199 * <p>When wearable actions are added using this method, the set of actions that
4200 * show on a wearable device splits from devices that only show actions added
4201 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4202 * of which actions display on different devices.
4203 *
4204 * @param actions the actions to add to this notification
4205 * @return this object for method chaining
4206 * @see android.app.Notification.Action
4207 */
4208 public WearableExtender addActions(List<Action> actions) {
4209 mActions.addAll(actions);
4210 return this;
4211 }
4212
4213 /**
4214 * Clear all wearable actions present on this builder.
4215 * @return this object for method chaining.
4216 * @see #addAction
4217 */
4218 public WearableExtender clearActions() {
4219 mActions.clear();
4220 return this;
4221 }
4222
4223 /**
4224 * Get the wearable actions present on this notification.
4225 */
4226 public List<Action> getActions() {
4227 return mActions;
4228 }
4229
4230 /**
4231 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004232 * this notification. The {@link PendingIntent} provided should be for an activity.
4233 *
4234 * <pre class="prettyprint">
4235 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4236 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4237 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4238 * Notification notif = new Notification.Builder(context)
4239 * .extend(new Notification.WearableExtender()
4240 * .setDisplayIntent(displayPendingIntent)
4241 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4242 * .build();</pre>
4243 *
4244 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004245 * should have an empty task affinity. It is also recommended to use the device
4246 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004247 *
4248 * <p>Example AndroidManifest.xml entry:
4249 * <pre class="prettyprint">
4250 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4251 * android:exported=&quot;true&quot;
4252 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004253 * android:taskAffinity=&quot;&quot;
4254 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004255 *
4256 * @param intent the {@link PendingIntent} for an activity
4257 * @return this object for method chaining
4258 * @see android.app.Notification.WearableExtender#getDisplayIntent
4259 */
4260 public WearableExtender setDisplayIntent(PendingIntent intent) {
4261 mDisplayIntent = intent;
4262 return this;
4263 }
4264
4265 /**
4266 * Get the intent to launch inside of an activity view when displaying this
4267 * notification. This {@code PendingIntent} should be for an activity.
4268 */
4269 public PendingIntent getDisplayIntent() {
4270 return mDisplayIntent;
4271 }
4272
4273 /**
4274 * Add an additional page of content to display with this notification. The current
4275 * notification forms the first page, and pages added using this function form
4276 * subsequent pages. This field can be used to separate a notification into multiple
4277 * sections.
4278 *
4279 * @param page the notification to add as another page
4280 * @return this object for method chaining
4281 * @see android.app.Notification.WearableExtender#getPages
4282 */
4283 public WearableExtender addPage(Notification page) {
4284 mPages.add(page);
4285 return this;
4286 }
4287
4288 /**
4289 * Add additional pages of content to display with this notification. The current
4290 * notification forms the first page, and pages added using this function form
4291 * subsequent pages. This field can be used to separate a notification into multiple
4292 * sections.
4293 *
4294 * @param pages a list of notifications
4295 * @return this object for method chaining
4296 * @see android.app.Notification.WearableExtender#getPages
4297 */
4298 public WearableExtender addPages(List<Notification> pages) {
4299 mPages.addAll(pages);
4300 return this;
4301 }
4302
4303 /**
4304 * Clear all additional pages present on this builder.
4305 * @return this object for method chaining.
4306 * @see #addPage
4307 */
4308 public WearableExtender clearPages() {
4309 mPages.clear();
4310 return this;
4311 }
4312
4313 /**
4314 * Get the array of additional pages of content for displaying this notification. The
4315 * current notification forms the first page, and elements within this array form
4316 * subsequent pages. This field can be used to separate a notification into multiple
4317 * sections.
4318 * @return the pages for this notification
4319 */
4320 public List<Notification> getPages() {
4321 return mPages;
4322 }
4323
4324 /**
4325 * Set a background image to be displayed behind the notification content.
4326 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4327 * will work with any notification style.
4328 *
4329 * @param background the background bitmap
4330 * @return this object for method chaining
4331 * @see android.app.Notification.WearableExtender#getBackground
4332 */
4333 public WearableExtender setBackground(Bitmap background) {
4334 mBackground = background;
4335 return this;
4336 }
4337
4338 /**
4339 * Get a background image to be displayed behind the notification content.
4340 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4341 * will work with any notification style.
4342 *
4343 * @return the background image
4344 * @see android.app.Notification.WearableExtender#setBackground
4345 */
4346 public Bitmap getBackground() {
4347 return mBackground;
4348 }
4349
4350 /**
4351 * Set an icon that goes with the content of this notification.
4352 */
4353 public WearableExtender setContentIcon(int icon) {
4354 mContentIcon = icon;
4355 return this;
4356 }
4357
4358 /**
4359 * Get an icon that goes with the content of this notification.
4360 */
4361 public int getContentIcon() {
4362 return mContentIcon;
4363 }
4364
4365 /**
4366 * Set the gravity that the content icon should have within the notification display.
4367 * Supported values include {@link android.view.Gravity#START} and
4368 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4369 * @see #setContentIcon
4370 */
4371 public WearableExtender setContentIconGravity(int contentIconGravity) {
4372 mContentIconGravity = contentIconGravity;
4373 return this;
4374 }
4375
4376 /**
4377 * Get the gravity that the content icon should have within the notification display.
4378 * Supported values include {@link android.view.Gravity#START} and
4379 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4380 * @see #getContentIcon
4381 */
4382 public int getContentIconGravity() {
4383 return mContentIconGravity;
4384 }
4385
4386 /**
4387 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004388 * this notification. This action will no longer display separately from the
4389 * notification's content.
4390 *
Griff Hazenca48d352014-05-28 22:37:13 -07004391 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004392 * set, although the list of available actions comes from the main notification and not
4393 * from the child page's notification.
4394 *
4395 * @param actionIndex The index of the action to hoist onto the current notification page.
4396 * If wearable actions were added to the main notification, this index
4397 * will apply to that list, otherwise it will apply to the regular
4398 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004399 */
4400 public WearableExtender setContentAction(int actionIndex) {
4401 mContentActionIndex = actionIndex;
4402 return this;
4403 }
4404
4405 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004406 * Get the index of the notification action, if any, that was specified as being clickable
4407 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004408 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004409 *
Griff Hazenca48d352014-05-28 22:37:13 -07004410 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004411 * set, although the list of available actions comes from the main notification and not
4412 * from the child page's notification.
4413 *
4414 * <p>If wearable specific actions were added to the main notification, this index will
4415 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004416 *
4417 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004418 */
4419 public int getContentAction() {
4420 return mContentActionIndex;
4421 }
4422
4423 /**
4424 * Set the gravity that this notification should have within the available viewport space.
4425 * Supported values include {@link android.view.Gravity#TOP},
4426 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4427 * The default value is {@link android.view.Gravity#BOTTOM}.
4428 */
4429 public WearableExtender setGravity(int gravity) {
4430 mGravity = gravity;
4431 return this;
4432 }
4433
4434 /**
4435 * Get the gravity that this notification should have within the available viewport space.
4436 * Supported values include {@link android.view.Gravity#TOP},
4437 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4438 * The default value is {@link android.view.Gravity#BOTTOM}.
4439 */
4440 public int getGravity() {
4441 return mGravity;
4442 }
4443
4444 /**
4445 * Set the custom size preset for the display of this notification out of the available
4446 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4447 * {@link #SIZE_LARGE}.
4448 * <p>Some custom size presets are only applicable for custom display notifications created
4449 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
4450 * documentation for the preset in question. See also
4451 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
4452 */
4453 public WearableExtender setCustomSizePreset(int sizePreset) {
4454 mCustomSizePreset = sizePreset;
4455 return this;
4456 }
4457
4458 /**
4459 * Get the custom size preset for the display of this notification out of the available
4460 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4461 * {@link #SIZE_LARGE}.
4462 * <p>Some custom size presets are only applicable for custom display notifications created
4463 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
4464 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
4465 */
4466 public int getCustomSizePreset() {
4467 return mCustomSizePreset;
4468 }
4469
4470 /**
4471 * Set the custom height in pixels for the display of this notification's content.
4472 * <p>This option is only available for custom display notifications created
4473 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
4474 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
4475 * {@link #getCustomContentHeight}.
4476 */
4477 public WearableExtender setCustomContentHeight(int height) {
4478 mCustomContentHeight = height;
4479 return this;
4480 }
4481
4482 /**
4483 * Get the custom height in pixels for the display of this notification's content.
4484 * <p>This option is only available for custom display notifications created
4485 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
4486 * {@link #setCustomContentHeight}.
4487 */
4488 public int getCustomContentHeight() {
4489 return mCustomContentHeight;
4490 }
4491
4492 /**
4493 * Set whether the scrolling position for the contents of this notification should start
4494 * at the bottom of the contents instead of the top when the contents are too long to
4495 * display within the screen. Default is false (start scroll at the top).
4496 */
4497 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
4498 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
4499 return this;
4500 }
4501
4502 /**
4503 * Get whether the scrolling position for the contents of this notification should start
4504 * at the bottom of the contents instead of the top when the contents are too long to
4505 * display within the screen. Default is false (start scroll at the top).
4506 */
4507 public boolean getStartScrollBottom() {
4508 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
4509 }
4510
4511 /**
4512 * Set whether the content intent is available when the wearable device is not connected
4513 * to a companion device. The user can still trigger this intent when the wearable device
4514 * is offline, but a visual hint will indicate that the content intent may not be available.
4515 * Defaults to true.
4516 */
4517 public WearableExtender setContentIntentAvailableOffline(
4518 boolean contentIntentAvailableOffline) {
4519 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
4520 return this;
4521 }
4522
4523 /**
4524 * Get whether the content intent is available when the wearable device is not connected
4525 * to a companion device. The user can still trigger this intent when the wearable device
4526 * is offline, but a visual hint will indicate that the content intent may not be available.
4527 * Defaults to true.
4528 */
4529 public boolean getContentIntentAvailableOffline() {
4530 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
4531 }
4532
4533 /**
4534 * Set a hint that this notification's icon should not be displayed.
4535 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
4536 * @return this object for method chaining
4537 */
4538 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
4539 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
4540 return this;
4541 }
4542
4543 /**
4544 * Get a hint that this notification's icon should not be displayed.
4545 * @return {@code true} if this icon should not be displayed, false otherwise.
4546 * The default value is {@code false} if this was never set.
4547 */
4548 public boolean getHintHideIcon() {
4549 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
4550 }
4551
4552 /**
4553 * Set a visual hint that only the background image of this notification should be
4554 * displayed, and other semantic content should be hidden. This hint is only applicable
4555 * to sub-pages added using {@link #addPage}.
4556 */
4557 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
4558 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
4559 return this;
4560 }
4561
4562 /**
4563 * Get a visual hint that only the background image of this notification should be
4564 * displayed, and other semantic content should be hidden. This hint is only applicable
4565 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
4566 */
4567 public boolean getHintShowBackgroundOnly() {
4568 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
4569 }
4570
4571 private void setFlag(int mask, boolean value) {
4572 if (value) {
4573 mFlags |= mask;
4574 } else {
4575 mFlags &= ~mask;
4576 }
4577 }
4578 }
4579
4580 /**
4581 * Get an array of Notification objects from a parcelable array bundle field.
4582 * Update the bundle to have a typed array so fetches in the future don't need
4583 * to do an array copy.
4584 */
4585 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4586 Parcelable[] array = bundle.getParcelableArray(key);
4587 if (array instanceof Notification[] || array == null) {
4588 return (Notification[]) array;
4589 }
4590 Notification[] typedArray = Arrays.copyOf(array, array.length,
4591 Notification[].class);
4592 bundle.putParcelableArray(key, typedArray);
4593 return typedArray;
4594 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004595
4596 private static class BuilderRemoteViews extends RemoteViews {
4597 public BuilderRemoteViews(Parcel parcel) {
4598 super(parcel);
4599 }
4600
4601 public BuilderRemoteViews(String packageName, int layoutId) {
4602 super(packageName, layoutId);
4603 }
4604
4605 @Override
4606 public BuilderRemoteViews clone() {
4607 Parcel p = Parcel.obtain();
4608 writeToParcel(p, 0);
4609 p.setDataPosition(0);
4610 BuilderRemoteViews brv = new BuilderRemoteViews(p);
4611 p.recycle();
4612 return brv;
4613 }
4614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004615}