blob: 7682af8def811d7c155b72dd53c122f4c7a0e8e0 [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 Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040024import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010027import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040028import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020029import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020030import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040031import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070033import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010034import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010035import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040036import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040037import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070038import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070039import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040041import android.os.BadParcelableException;
Svet Ganovddb94882016-06-23 19:55:24 -070042import android.os.BaseBundle;
Christoph Studer239f8352014-08-25 15:13:18 +020043import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050044import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040047import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070048import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070049import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080050import android.text.SpannableStringBuilder;
51import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080053import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080054import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080055import android.text.style.RelativeSizeSpan;
56import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070057import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040058import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050059import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070060import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080061import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080062import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070063import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070064import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.widget.RemoteViews;
66
Griff Hazen959591e2014-05-15 22:26:18 -070067import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070068import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070069import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070070
Tor Norbyed9273d62013-05-30 15:59:53 -070071import java.lang.annotation.Retention;
72import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020073import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050074import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070075import java.util.Arrays;
Svet Ganovddb94882016-06-23 19:55:24 -070076import java.util.Collection;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070077import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070078import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050079import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081/**
82 * A class that represents how a persistent notification is to be presented to
83 * the user using the {@link android.app.NotificationManager}.
84 *
Joe Onoratocb109a02011-01-18 17:57:41 -080085 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
86 * easier to construct Notifications.</p>
87 *
Joe Fernandez558459f2011-10-13 16:47:36 -070088 * <div class="special reference">
89 * <h3>Developer Guides</h3>
90 * <p>For a guide to creating notifications, read the
91 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
92 * developer guide.</p>
93 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 */
95public class Notification implements Parcelable
96{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040097 private static final String TAG = "Notification";
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400100 * An activity that provides a user interface for adjusting notification preferences for its
101 * containing application. Optional but recommended for apps that post
102 * {@link android.app.Notification Notifications}.
103 */
104 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
105 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
106 = "android.intent.category.NOTIFICATION_PREFERENCES";
107
108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * Use all default values (where applicable).
110 */
111 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 /**
114 * Use the default notification sound. This will ignore any given
115 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500116 *
Chris Wren47c20a12014-06-18 17:27:29 -0400117 * <p>
118 * A notification that is noisy is more likely to be presented as a heads-up notification.
119 * </p>
120 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500122 */
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final int DEFAULT_SOUND = 1;
125
126 /**
127 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700129 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130 *
Chris Wren47c20a12014-06-18 17:27:29 -0400131 * <p>
132 * A notification that vibrates is more likely to be presented as a heads-up notification.
133 * </p>
134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500136 */
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
141 * Use the default notification lights. This will ignore the
142 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
143 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146 */
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200151 * Maximum length of CharSequences accepted by Builder and friends.
152 *
153 * <p>
154 * Avoids spamming the system with overly large strings such as full e-mails.
155 */
156 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
157
158 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800159 * Maximum entries of reply text that are accepted by Builder and friends.
160 */
161 private static final int MAX_REPLY_HISTORY = 5;
162
163 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800165 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * Default value: {@link System#currentTimeMillis() Now}.
167 *
168 * Choose a timestamp that will be most relevant to the user. For most finite events, this
169 * corresponds to the time the event happened (or will happen, in the case of events that have
170 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800171 * timestamped according to when the activity began.
172 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500173 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800174 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 * <ul>
176 * <li>Notification of a new chat message should be stamped when the message was received.</li>
177 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
178 * <li>Notification of a completed file download should be stamped when the download finished.</li>
179 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
180 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
181 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800182 * </ul>
183 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700184 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
185 * anymore by default and must be opted into by using
186 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 */
188 public long when;
189
190 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700191 * The creation time of the notification
192 */
193 private long creationTime;
194
195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400197 *
198 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
Dan Sandler86647982015-05-13 23:41:13 -0400200 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700201 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 public int icon;
203
204 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800205 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
206 * leave it at its default value of 0.
207 *
208 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700209 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800210 */
211 public int iconLevel;
212
213 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500214 * The number of events that this notification represents. For example, in a new mail
215 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500217 * The system may or may not use this field to modify the appearance of the notification. For
218 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
219 * superimposed over the icon in the status bar. Starting with
220 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
221 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700224 *
225 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
227 public int number;
228
229 /**
230 * The intent to execute when the expanded status entry is clicked. If
231 * this is an activity, it must include the
232 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800233 * that you take care of task management as described in the
234 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800235 * Stack</a> document. In particular, make sure to read the notification section
236 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
237 * Notifications</a> for the correct ways to launch an application from a
238 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
240 public PendingIntent contentIntent;
241
242 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500243 * The intent to execute when the notification is explicitly dismissed by the user, either with
244 * the "Clear All" button or by swiping it away individually.
245 *
246 * This probably shouldn't be launching an activity since several of those will be sent
247 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
249 public PendingIntent deleteIntent;
250
251 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700252 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800253 *
Chris Wren47c20a12014-06-18 17:27:29 -0400254 * <p>
255 * The system UI may choose to display a heads-up notification, instead of
256 * launching this intent, while the user is using the device.
257 * </p>
258 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800259 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400260 */
261 public PendingIntent fullScreenIntent;
262
263 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400264 * Text that summarizes this notification for accessibility services.
265 *
266 * As of the L release, this text is no longer shown on screen, but it is still useful to
267 * accessibility services (where it serves as an audible announcement of the notification's
268 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400269 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800270 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 */
272 public CharSequence tickerText;
273
274 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400275 * Formerly, a view showing the {@link #tickerText}.
276 *
277 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400278 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400279 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800280 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400281
282 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400283 * The view that will represent this notification in the notification list (which is pulled
284 * down from the status bar).
285 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500286 * As of N, this field may be null. The notification view is determined by the inputs
287 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400288 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400290 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 public RemoteViews contentView;
292
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400293 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400294 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400295 * opportunity to show more detail. The system UI may choose to show this
296 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400297 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500298 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
300 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400301 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400302 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400303 public RemoteViews bigContentView;
304
Chris Wren8fd39ec2014-02-27 17:43:26 -0500305
306 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400307 * A medium-format version of {@link #contentView}, providing the Notification an
308 * opportunity to add action buttons to contentView. At its discretion, the system UI may
309 * choose to show this as a heads-up notification, which will pop up so the user can see
310 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400311 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500312 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400313 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
314 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500315 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400316 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500317 public RemoteViews headsUpContentView;
318
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400319 /**
Dan Sandler86647982015-05-13 23:41:13 -0400320 * A large bitmap to be shown in the notification content area.
321 *
322 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
Dan Sandler86647982015-05-13 23:41:13 -0400324 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800325 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
327 /**
328 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500329 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400331 * A notification that is noisy is more likely to be presented as a heads-up notification.
332 * </p>
333 *
334 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500335 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 * </p>
337 */
338 public Uri sound;
339
340 /**
341 * Use this constant as the value for audioStreamType to request that
342 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700343 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400344 *
345 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700347 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 public static final int STREAM_DEFAULT = -1;
349
350 /**
351 * The audio stream type to use when playing the sound.
352 * Should be one of the STREAM_ constants from
353 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400354 *
355 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700357 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 public int audioStreamType = STREAM_DEFAULT;
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400361 * The default value of {@link #audioAttributes}.
362 */
363 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
364 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
365 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
366 .build();
367
368 /**
369 * The {@link AudioAttributes audio attributes} to use when playing the sound.
370 */
371 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
372
373 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500374 * The pattern with which to vibrate.
375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * <p>
377 * To vibrate the default pattern, see {@link #defaults}.
378 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500379 *
Chris Wren47c20a12014-06-18 17:27:29 -0400380 * <p>
381 * A notification that vibrates is more likely to be presented as a heads-up notification.
382 * </p>
383 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 * @see android.os.Vibrator#vibrate(long[],int)
385 */
386 public long[] vibrate;
387
388 /**
389 * The color of the led. The hardware will do its best approximation.
390 *
391 * @see #FLAG_SHOW_LIGHTS
392 * @see #flags
393 */
Tor Norbye80756e32015-03-02 09:39:27 -0800394 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 public int ledARGB;
396
397 /**
398 * The number of milliseconds for the LED to be on while it's flashing.
399 * The hardware will do its best approximation.
400 *
401 * @see #FLAG_SHOW_LIGHTS
402 * @see #flags
403 */
404 public int ledOnMS;
405
406 /**
407 * The number of milliseconds for the LED to be off while it's flashing.
408 * The hardware will do its best approximation.
409 *
410 * @see #FLAG_SHOW_LIGHTS
411 * @see #flags
412 */
413 public int ledOffMS;
414
415 /**
416 * Specifies which values should be taken from the defaults.
417 * <p>
418 * To set, OR the desired from {@link #DEFAULT_SOUND},
419 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
420 * values, use {@link #DEFAULT_ALL}.
421 * </p>
422 */
423 public int defaults;
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * Bit to be bitwise-ored into the {@link #flags} field that should be
427 * set if you want the LED on for this notification.
428 * <ul>
429 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
430 * or 0 for both ledOnMS and ledOffMS.</li>
431 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
432 * <li>To flash the LED, pass the number of milliseconds that it should
433 * be on and off to ledOnMS and ledOffMS.</li>
434 * </ul>
435 * <p>
436 * Since hardware varies, you are not guaranteed that any of the values
437 * you pass are honored exactly. Use the system defaults (TODO) if possible
438 * because they will be set to values that work on any given hardware.
439 * <p>
440 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 */
443 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
444
445 /**
446 * Bit to be bitwise-ored into the {@link #flags} field that should be
447 * set if this notification is in reference to something that is ongoing,
448 * like a phone call. It should not be set if this notification is in
449 * reference to something that happened at a particular point in time,
450 * like a missed phone call.
451 */
452 public static final int FLAG_ONGOING_EVENT = 0x00000002;
453
454 /**
455 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700456 * the audio will be repeated until the notification is
457 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 */
459 public static final int FLAG_INSISTENT = 0x00000004;
460
461 /**
462 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700463 * set if you would only like the sound, vibrate and ticker to be played
464 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 */
466 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
467
468 /**
469 * Bit to be bitwise-ored into the {@link #flags} field that should be
470 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500471 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
473 public static final int FLAG_AUTO_CANCEL = 0x00000010;
474
475 /**
476 * Bit to be bitwise-ored into the {@link #flags} field that should be
477 * set if the notification should not be canceled when the user clicks
478 * the Clear all button.
479 */
480 public static final int FLAG_NO_CLEAR = 0x00000020;
481
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700482 /**
483 * Bit to be bitwise-ored into the {@link #flags} field that should be
484 * set if this notification represents a currently running service. This
485 * will normally be set for you by {@link Service#startForeground}.
486 */
487 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
488
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400489 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800491 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500492 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400493 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500494 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400495
Griff Hazendfcb0802014-02-11 12:00:00 -0800496 /**
497 * Bit to be bitswise-ored into the {@link #flags} field that should be
498 * set if this notification is relevant to the current device only
499 * and it is not recommended that it bridge to other devices.
500 */
501 public static final int FLAG_LOCAL_ONLY = 0x00000100;
502
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700503 /**
504 * Bit to be bitswise-ored into the {@link #flags} field that should be
505 * set if this notification is the group summary for a group of notifications.
506 * Grouped notifications may display in a cluster or stack on devices which
507 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
508 */
509 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
510
Julia Reynoldse46bb372016-03-17 11:05:58 -0400511 /**
512 * Bit to be bitswise-ored into the {@link #flags} field that should be
513 * set if this notification is the group summary for an auto-group of notifications.
514 *
515 * @hide
516 */
517 @SystemApi
518 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public int flags;
521
Tor Norbyed9273d62013-05-30 15:59:53 -0700522 /** @hide */
523 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
524 @Retention(RetentionPolicy.SOURCE)
525 public @interface Priority {}
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500528 * Default notification {@link #priority}. If your application does not prioritize its own
529 * notifications, use this value for all notifications.
530 */
531 public static final int PRIORITY_DEFAULT = 0;
532
533 /**
534 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
535 * items smaller, or at a different position in the list, compared with your app's
536 * {@link #PRIORITY_DEFAULT} items.
537 */
538 public static final int PRIORITY_LOW = -1;
539
540 /**
541 * Lowest {@link #priority}; these items might not be shown to the user except under special
542 * circumstances, such as detailed notification logs.
543 */
544 public static final int PRIORITY_MIN = -2;
545
546 /**
547 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
548 * show these items larger, or at a different position in notification lists, compared with
549 * your app's {@link #PRIORITY_DEFAULT} items.
550 */
551 public static final int PRIORITY_HIGH = 1;
552
553 /**
554 * Highest {@link #priority}, for your application's most important items that require the
555 * user's prompt attention or input.
556 */
557 public static final int PRIORITY_MAX = 2;
558
559 /**
560 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800561 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500562 * Priority is an indication of how much of the user's valuable attention should be consumed by
563 * this notification. Low-priority notifications may be hidden from the user in certain
564 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500565 * system will make a determination about how to interpret this priority when presenting
566 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400567 *
568 * <p>
569 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
570 * as a heads-up notification.
571 * </p>
572 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500573 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700574 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800576
Dan Sandler26e81cf2014-05-06 10:01:27 -0400577 /**
578 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
579 * to be applied by the standard Style templates when presenting this notification.
580 *
581 * The current template design constructs a colorful header image by overlaying the
582 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
583 * ignored.
584 */
Tor Norbye80756e32015-03-02 09:39:27 -0800585 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400586 public int color = COLOR_DEFAULT;
587
588 /**
589 * Special value of {@link #color} telling the system not to decorate this notification with
590 * any special color but instead use default colors when presenting this notification.
591 */
Tor Norbye80756e32015-03-02 09:39:27 -0800592 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400593 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600594
595 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800596 * Special value of {@link #color} used as a place holder for an invalid color.
597 */
598 @ColorInt
599 private static final int COLOR_INVALID = 1;
600
601 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700602 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
603 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600604 * lockscreen).
605 *
606 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
607 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
608 * shown in all situations, but the contents are only available if the device is unlocked for
609 * the appropriate user.
610 *
611 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
612 * can be read even in an "insecure" context (that is, above a secure lockscreen).
613 * To modify the public version of this notification—for example, to redact some portions—see
614 * {@link Builder#setPublicVersion(Notification)}.
615 *
616 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
617 * and ticker until the user has bypassed the lockscreen.
618 */
619 public int visibility;
620
Griff Hazenfc3922d2014-08-20 11:56:44 -0700621 /**
622 * Notification visibility: Show this notification in its entirety on all lockscreens.
623 *
624 * {@see #visibility}
625 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600626 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700627
628 /**
629 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
630 * private information on secure lockscreens.
631 *
632 * {@see #visibility}
633 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600634 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700635
636 /**
637 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
638 *
639 * {@see #visibility}
640 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600641 public static final int VISIBILITY_SECRET = -1;
642
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400644 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400646 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647
648 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400649 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400651 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652
653 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400654 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400656 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657
658 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400659 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400661 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662
663 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400664 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400666 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667
668 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400669 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500670 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400671 public static final String CATEGORY_ALARM = "alarm";
672
673 /**
674 * Notification category: progress of a long-running background operation.
675 */
676 public static final String CATEGORY_PROGRESS = "progress";
677
678 /**
679 * Notification category: social network or sharing update.
680 */
681 public static final String CATEGORY_SOCIAL = "social";
682
683 /**
684 * Notification category: error in background operation or authentication status.
685 */
686 public static final String CATEGORY_ERROR = "err";
687
688 /**
689 * Notification category: media transport control for playback.
690 */
691 public static final String CATEGORY_TRANSPORT = "transport";
692
693 /**
694 * Notification category: system or device status update. Reserved for system use.
695 */
696 public static final String CATEGORY_SYSTEM = "sys";
697
698 /**
699 * Notification category: indication of running background service.
700 */
701 public static final String CATEGORY_SERVICE = "service";
702
703 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400704 * Notification category: a specific, timely recommendation for a single thing.
705 * For example, a news app might want to recommend a news story it believes the user will
706 * want to read next.
707 */
708 public static final String CATEGORY_RECOMMENDATION = "recommendation";
709
710 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400711 * Notification category: ongoing information about device or contextual status.
712 */
713 public static final String CATEGORY_STATUS = "status";
714
715 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400716 * Notification category: user-scheduled reminder.
717 */
718 public static final String CATEGORY_REMINDER = "reminder";
719
720 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400721 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
722 * that best describes this Notification. May be used by the system for ranking and filtering.
723 */
724 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500725
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700726 private String mGroupKey;
727
728 /**
729 * Get the key used to group this notification into a cluster or stack
730 * with other notifications on devices which support such rendering.
731 */
732 public String getGroup() {
733 return mGroupKey;
734 }
735
736 private String mSortKey;
737
738 /**
739 * Get a sort key that orders this notification among other notifications from the
740 * same package. This can be useful if an external sort was already applied and an app
741 * would like to preserve this. Notifications will be sorted lexicographically using this
742 * value, although providing different priorities in addition to providing sort key may
743 * cause this value to be ignored.
744 *
745 * <p>This sort key can also be used to order members of a notification group. See
746 * {@link Builder#setGroup}.
747 *
748 * @see String#compareTo(String)
749 */
750 public String getSortKey() {
751 return mSortKey;
752 }
753
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500754 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400755 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400756 * <p>
757 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
758 * APIs, and are intended to be used by
759 * {@link android.service.notification.NotificationListenerService} implementations to extract
760 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500761 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400762 public Bundle extras = new Bundle();
763
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400764 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700765 * All pending intents in the notification as the system needs to be able to access them but
766 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700767 * custom parcelable objects.
768 *
769 * @hide
770 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700771 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700772
773 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774 * {@link #extras} key: this is the title of the notification,
775 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
776 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500777 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400778
779 /**
780 * {@link #extras} key: this is the title of the notification when shown in expanded form,
781 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
782 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400783 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784
785 /**
786 * {@link #extras} key: this is the main text payload, as supplied to
787 * {@link Builder#setContentText(CharSequence)}.
788 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500789 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400790
791 /**
792 * {@link #extras} key: this is a third line of text, as supplied to
793 * {@link Builder#setSubText(CharSequence)}.
794 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400795 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400796
797 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800798 * {@link #extras} key: this is the remote input history, as supplied to
799 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700800 *
801 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
802 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
803 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
804 * notifications once the other party has responded).
805 *
806 * The extra with this key is of type CharSequence[] and contains the most recent entry at
807 * the 0 index, the second most recent at the 1 index, etc.
808 *
809 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800810 */
811 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
812
813 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814 * {@link #extras} key: this is a small piece of additional text as supplied to
815 * {@link Builder#setContentInfo(CharSequence)}.
816 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400817 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400818
819 /**
820 * {@link #extras} key: this is a line of summary information intended to be shown
821 * alongside expanded notifications, as supplied to (e.g.)
822 * {@link BigTextStyle#setSummaryText(CharSequence)}.
823 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400824 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825
826 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200827 * {@link #extras} key: this is the longer text shown in the big form of a
828 * {@link BigTextStyle} notification, as supplied to
829 * {@link BigTextStyle#bigText(CharSequence)}.
830 */
831 public static final String EXTRA_BIG_TEXT = "android.bigText";
832
833 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400834 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
835 * supplied to {@link Builder#setSmallIcon(int)}.
836 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500837 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838
839 /**
840 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
841 * notification payload, as
842 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: this is a bitmap to be used instead of the one from
848 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
849 * shown in its expanded form, as supplied to
850 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400853
854 /**
855 * {@link #extras} key: this is the progress value supplied to
856 * {@link Builder#setProgress(int, int, boolean)}.
857 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400858 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400859
860 /**
861 * {@link #extras} key: this is the maximum value supplied to
862 * {@link Builder#setProgress(int, int, boolean)}.
863 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400864 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400865
866 /**
867 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
868 * {@link Builder#setProgress(int, int, boolean)}.
869 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400870 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400871
872 /**
873 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
874 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
875 * {@link Builder#setUsesChronometer(boolean)}.
876 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400877 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400878
879 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800880 * {@link #extras} key: whether the chronometer set on the notification should count down
881 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700882 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800883 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700884 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800885
886 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400887 * {@link #extras} key: whether {@link #when} should be shown,
888 * as supplied to {@link Builder#setShowWhen(boolean)}.
889 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400890 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400891
892 /**
893 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
894 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
895 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400896 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400897
898 /**
899 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
900 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
901 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400902 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400903
904 /**
905 * {@link #extras} key: A string representing the name of the specific
906 * {@link android.app.Notification.Style} used to create this notification.
907 */
Chris Wren91ad5632013-06-05 15:05:57 -0400908 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400909
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400910 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400911 * {@link #extras} key: A String array containing the people that this notification relates to,
912 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400913 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400914 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500915
916 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400917 * Allow certain system-generated notifications to appear before the device is provisioned.
918 * Only available to notifications coming from the android package.
919 * @hide
920 */
921 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
922
923 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700924 * {@link #extras} key: A
925 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
926 * in the background when the notification is selected. The URI must point to an image stream
927 * suitable for passing into
928 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
929 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
930 * URI used for this purpose must require no permissions to read the image data.
931 */
932 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
933
934 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400935 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700936 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400937 * {@link android.app.Notification.MediaStyle} notification.
938 */
939 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
940
941 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100942 * {@link #extras} key: the indices of actions to be shown in the compact view,
943 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
944 */
945 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
946
Christoph Studer943aa672014-08-03 20:31:16 +0200947 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400948 * {@link #extras} key: the username to be displayed for all messages sent by the user including
949 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700950 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
951 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400952 */
953 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
954
955 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700956 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400957 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400958 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400959 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400960
961 /**
962 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
963 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700964 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
965 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400966 */
967 public static final String EXTRA_MESSAGES = "android.messages";
968
969 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100970 * {@link #extras} key: the user that built the notification.
971 *
972 * @hide
973 */
974 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
975
976 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400977 * @hide
978 */
979 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
980
Selim Cinek247fa012016-02-18 09:50:48 -0800981 /**
982 * @hide
983 */
984 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
985
Dan Sandler80eaa592016-04-14 23:34:54 -0400986 /** @hide */
987 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400988 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
989
Dan Sandlerd63f9322015-05-06 15:18:49 -0400990 private Icon mSmallIcon;
991 private Icon mLargeIcon;
992
Chris Wren51c75102013-07-16 20:49:17 -0400993 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400994 * Structure to encapsulate a named action that can be shown as part of this notification.
995 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
996 * selected by the user.
997 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700998 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
999 * or {@link Notification.Builder#addAction(Notification.Action)}
1000 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001001 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001002 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -07001003 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001004 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001005 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001006 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -07001007
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001008 /**
1009 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001010 *
1011 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001012 */
Dan Sandler86647982015-05-13 23:41:13 -04001013 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001014 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001015
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001016 /**
1017 * Title of the action.
1018 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001019 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001020
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001021 /**
1022 * Intent to send when the user invokes this action. May be null, in which case the action
1023 * may be rendered in a disabled presentation by the system UI.
1024 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001025 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001026
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001027 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001028 if (in.readInt() != 0) {
1029 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001030 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1031 icon = mIcon.getResId();
1032 }
Dan Sandler86647982015-05-13 23:41:13 -04001033 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001034 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1035 if (in.readInt() == 1) {
1036 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1037 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001038 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001039 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001040 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001041 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001042
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 /**
Dan Sandler86647982015-05-13 23:41:13 -04001044 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001045 */
Dan Sandler86647982015-05-13 23:41:13 -04001046 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001047 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001048 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001049 }
1050
Dan Sandler86647982015-05-13 23:41:13 -04001051 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001052 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001053 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001054 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1055 this.icon = icon.getResId();
1056 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001057 this.title = title;
1058 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001059 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001060 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001061 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001062 }
1063
1064 /**
Dan Sandler86647982015-05-13 23:41:13 -04001065 * Return an icon representing the action.
1066 */
1067 public Icon getIcon() {
1068 if (mIcon == null && icon != 0) {
1069 // you snuck an icon in here without using the builder; let's try to keep it
1070 mIcon = Icon.createWithResource("", icon);
1071 }
1072 return mIcon;
1073 }
1074
1075 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001076 * Get additional metadata carried around with this Action.
1077 */
1078 public Bundle getExtras() {
1079 return mExtras;
1080 }
1081
1082 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001083 * Return whether the platform should automatically generate possible replies for this
1084 * {@link Action}
1085 */
1086 public boolean getAllowGeneratedReplies() {
1087 return mAllowGeneratedReplies;
1088 }
1089
1090 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001091 * Get the list of inputs to be collected from the user when this action is sent.
1092 * May return null if no remote inputs were added.
1093 */
1094 public RemoteInput[] getRemoteInputs() {
1095 return mRemoteInputs;
1096 }
1097
1098 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001099 * Builder class for {@link Action} objects.
1100 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001101 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001102 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001103 private final CharSequence mTitle;
1104 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001105 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001106 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001107 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001108
1109 /**
1110 * Construct a new builder for {@link Action} object.
1111 * @param icon icon to show for this action
1112 * @param title the title of the action
1113 * @param intent the {@link PendingIntent} to fire when users trigger this action
1114 */
Dan Sandler86647982015-05-13 23:41:13 -04001115 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001116 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001117 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1118 }
1119
1120 /**
1121 * Construct a new builder for {@link Action} object.
1122 * @param icon icon to show for this action
1123 * @param title the title of the action
1124 * @param intent the {@link PendingIntent} to fire when users trigger this action
1125 */
1126 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001127 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001128 }
1129
1130 /**
1131 * Construct a new builder for {@link Action} object using the fields from an
1132 * {@link Action}.
1133 * @param action the action to read fields from.
1134 */
1135 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001136 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001137 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001138 }
1139
Dan Sandler86647982015-05-13 23:41:13 -04001140 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001141 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001142 mIcon = icon;
1143 mTitle = title;
1144 mIntent = intent;
1145 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001146 if (remoteInputs != null) {
1147 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1148 Collections.addAll(mRemoteInputs, remoteInputs);
1149 }
Griff Hazen959591e2014-05-15 22:26:18 -07001150 }
1151
1152 /**
1153 * Merge additional metadata into this builder.
1154 *
1155 * <p>Values within the Bundle will replace existing extras values in this Builder.
1156 *
1157 * @see Notification.Action#extras
1158 */
1159 public Builder addExtras(Bundle extras) {
1160 if (extras != null) {
1161 mExtras.putAll(extras);
1162 }
1163 return this;
1164 }
1165
1166 /**
1167 * Get the metadata Bundle used by this Builder.
1168 *
1169 * <p>The returned Bundle is shared with this Builder.
1170 */
1171 public Bundle getExtras() {
1172 return mExtras;
1173 }
1174
1175 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001176 * Add an input to be collected from the user when this action is sent.
1177 * Response values can be retrieved from the fired intent by using the
1178 * {@link RemoteInput#getResultsFromIntent} function.
1179 * @param remoteInput a {@link RemoteInput} to add to the action
1180 * @return this object for method chaining
1181 */
1182 public Builder addRemoteInput(RemoteInput remoteInput) {
1183 if (mRemoteInputs == null) {
1184 mRemoteInputs = new ArrayList<RemoteInput>();
1185 }
1186 mRemoteInputs.add(remoteInput);
1187 return this;
1188 }
1189
1190 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001191 * Set whether the platform should automatically generate possible replies to add to
1192 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1193 * {@link RemoteInput}, this has no effect.
1194 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1195 * otherwise
1196 * @return this object for method chaining
1197 * The default value is {@code false}
1198 */
1199 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1200 mAllowGeneratedReplies = allowGeneratedReplies;
1201 return this;
1202 }
1203
1204 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001205 * Apply an extender to this action builder. Extenders may be used to add
1206 * metadata or change options on this builder.
1207 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001208 public Builder extend(Extender extender) {
1209 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001210 return this;
1211 }
1212
1213 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001214 * Combine all of the options that have been set and return a new {@link Action}
1215 * object.
1216 * @return the built action
1217 */
1218 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001219 RemoteInput[] remoteInputs = mRemoteInputs != null
1220 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001221 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1222 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001223 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001224 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001225
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001226 @Override
1227 public Action clone() {
1228 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001229 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001230 title,
1231 actionIntent, // safe to alias
1232 new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001233 getRemoteInputs(),
1234 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001235 }
1236 @Override
1237 public int describeContents() {
1238 return 0;
1239 }
1240 @Override
1241 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001242 final Icon ic = getIcon();
1243 if (ic != null) {
1244 out.writeInt(1);
1245 ic.writeToParcel(out, 0);
1246 } else {
1247 out.writeInt(0);
1248 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001249 TextUtils.writeToParcel(title, out, flags);
1250 if (actionIntent != null) {
1251 out.writeInt(1);
1252 actionIntent.writeToParcel(out, flags);
1253 } else {
1254 out.writeInt(0);
1255 }
Griff Hazen959591e2014-05-15 22:26:18 -07001256 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001257 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001258 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001259 }
Griff Hazen959591e2014-05-15 22:26:18 -07001260 public static final Parcelable.Creator<Action> CREATOR =
1261 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001262 public Action createFromParcel(Parcel in) {
1263 return new Action(in);
1264 }
1265 public Action[] newArray(int size) {
1266 return new Action[size];
1267 }
1268 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001269
1270 /**
1271 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1272 * metadata or change options on an action builder.
1273 */
1274 public interface Extender {
1275 /**
1276 * Apply this extender to a notification action builder.
1277 * @param builder the builder to be modified.
1278 * @return the build object for chaining.
1279 */
1280 public Builder extend(Builder builder);
1281 }
1282
1283 /**
1284 * Wearable extender for notification actions. To add extensions to an action,
1285 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1286 * the {@code WearableExtender()} constructor and apply it to a
1287 * {@link android.app.Notification.Action.Builder} using
1288 * {@link android.app.Notification.Action.Builder#extend}.
1289 *
1290 * <pre class="prettyprint">
1291 * Notification.Action action = new Notification.Action.Builder(
1292 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001293 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001294 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001295 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001296 */
1297 public static final class WearableExtender implements Extender {
1298 /** Notification action extra which contains wearable extensions */
1299 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1300
Pete Gastaf6781d2014-10-07 15:17:05 -04001301 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001302 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001303 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1304 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1305 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001306
1307 // Flags bitwise-ored to mFlags
1308 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001309 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Griff Hazen61a9e862014-05-22 16:05:19 -07001310
1311 // Default value for flags integer
1312 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1313
1314 private int mFlags = DEFAULT_FLAGS;
1315
Pete Gastaf6781d2014-10-07 15:17:05 -04001316 private CharSequence mInProgressLabel;
1317 private CharSequence mConfirmLabel;
1318 private CharSequence mCancelLabel;
1319
Griff Hazen61a9e862014-05-22 16:05:19 -07001320 /**
1321 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1322 * options.
1323 */
1324 public WearableExtender() {
1325 }
1326
1327 /**
1328 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1329 * wearable options present in an existing notification action.
1330 * @param action the notification action to inspect.
1331 */
1332 public WearableExtender(Action action) {
1333 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1334 if (wearableBundle != null) {
1335 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001336 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1337 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1338 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001339 }
1340 }
1341
1342 /**
1343 * Apply wearable extensions to a notification action that is being built. This is
1344 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1345 * method of {@link android.app.Notification.Action.Builder}.
1346 */
1347 @Override
1348 public Action.Builder extend(Action.Builder builder) {
1349 Bundle wearableBundle = new Bundle();
1350
1351 if (mFlags != DEFAULT_FLAGS) {
1352 wearableBundle.putInt(KEY_FLAGS, mFlags);
1353 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001354 if (mInProgressLabel != null) {
1355 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1356 }
1357 if (mConfirmLabel != null) {
1358 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1359 }
1360 if (mCancelLabel != null) {
1361 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1362 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001363
1364 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1365 return builder;
1366 }
1367
1368 @Override
1369 public WearableExtender clone() {
1370 WearableExtender that = new WearableExtender();
1371 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001372 that.mInProgressLabel = this.mInProgressLabel;
1373 that.mConfirmLabel = this.mConfirmLabel;
1374 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001375 return that;
1376 }
1377
1378 /**
1379 * Set whether this action is available when the wearable device is not connected to
1380 * a companion device. The user can still trigger this action when the wearable device is
1381 * offline, but a visual hint will indicate that the action may not be available.
1382 * Defaults to true.
1383 */
1384 public WearableExtender setAvailableOffline(boolean availableOffline) {
1385 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1386 return this;
1387 }
1388
1389 /**
1390 * Get whether this action is available when the wearable device is not connected to
1391 * a companion device. The user can still trigger this action when the wearable device is
1392 * offline, but a visual hint will indicate that the action may not be available.
1393 * Defaults to true.
1394 */
1395 public boolean isAvailableOffline() {
1396 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1397 }
1398
1399 private void setFlag(int mask, boolean value) {
1400 if (value) {
1401 mFlags |= mask;
1402 } else {
1403 mFlags &= ~mask;
1404 }
1405 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001406
1407 /**
1408 * Set a label to display while the wearable is preparing to automatically execute the
1409 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1410 *
1411 * @param label the label to display while the action is being prepared to execute
1412 * @return this object for method chaining
1413 */
1414 public WearableExtender setInProgressLabel(CharSequence label) {
1415 mInProgressLabel = label;
1416 return this;
1417 }
1418
1419 /**
1420 * Get the label to display while the wearable is preparing to automatically execute
1421 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1422 *
1423 * @return the label to display while the action is being prepared to execute
1424 */
1425 public CharSequence getInProgressLabel() {
1426 return mInProgressLabel;
1427 }
1428
1429 /**
1430 * Set a label to display to confirm that the action should be executed.
1431 * This is usually an imperative verb like "Send".
1432 *
1433 * @param label the label to confirm the action should be executed
1434 * @return this object for method chaining
1435 */
1436 public WearableExtender setConfirmLabel(CharSequence label) {
1437 mConfirmLabel = label;
1438 return this;
1439 }
1440
1441 /**
1442 * Get the label to display to confirm that the action should be executed.
1443 * This is usually an imperative verb like "Send".
1444 *
1445 * @return the label to confirm the action should be executed
1446 */
1447 public CharSequence getConfirmLabel() {
1448 return mConfirmLabel;
1449 }
1450
1451 /**
1452 * Set a label to display to cancel the action.
1453 * This is usually an imperative verb, like "Cancel".
1454 *
1455 * @param label the label to display to cancel the action
1456 * @return this object for method chaining
1457 */
1458 public WearableExtender setCancelLabel(CharSequence label) {
1459 mCancelLabel = label;
1460 return this;
1461 }
1462
1463 /**
1464 * Get the label to display to cancel the action.
1465 * This is usually an imperative verb like "Cancel".
1466 *
1467 * @return the label to display to cancel the action
1468 */
1469 public CharSequence getCancelLabel() {
1470 return mCancelLabel;
1471 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001472
1473 /**
1474 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1475 * platform that it can generate the appropriate transitions.
1476 * @param hintLaunchesActivity {@code true} if the content intent will launch
1477 * an activity and transitions should be generated, false otherwise.
1478 * @return this object for method chaining
1479 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001480 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001481 boolean hintLaunchesActivity) {
1482 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1483 return this;
1484 }
1485
1486 /**
1487 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1488 * platform that it can generate the appropriate transitions
1489 * @return {@code true} if the content intent will launch an activity and transitions
1490 * should be generated, false otherwise. The default value is {@code false} if this was
1491 * never set.
1492 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001493 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001494 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1495 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001496 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001497 }
1498
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001499 /**
1500 * Array of all {@link Action} structures attached to this notification by
1501 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1502 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1503 * interface for invoking actions.
1504 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001505 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001506
1507 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001508 * Replacement version of this notification whose content will be shown
1509 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1510 * and {@link #VISIBILITY_PUBLIC}.
1511 */
1512 public Notification publicVersion;
1513
1514 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001515 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001516 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 */
1518 public Notification()
1519 {
1520 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001521 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001522 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524
1525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 * @hide
1527 */
1528 public Notification(Context context, int icon, CharSequence tickerText, long when,
1529 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1530 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001531 new Builder(context)
1532 .setWhen(when)
1533 .setSmallIcon(icon)
1534 .setTicker(tickerText)
1535 .setContentTitle(contentTitle)
1536 .setContentText(contentText)
1537 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1538 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540
1541 /**
1542 * Constructs a Notification object with the information needed to
1543 * have a status bar icon without the standard expanded view.
1544 *
1545 * @param icon The resource id of the icon to put in the status bar.
1546 * @param tickerText The text that flows by in the status bar when the notification first
1547 * activates.
1548 * @param when The time to show in the time field. In the System.currentTimeMillis
1549 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001550 *
1551 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001553 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 public Notification(int icon, CharSequence tickerText, long when)
1555 {
1556 this.icon = icon;
1557 this.tickerText = tickerText;
1558 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001559 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561
1562 /**
1563 * Unflatten the notification from a parcel.
1564 */
Svet Ganovddb94882016-06-23 19:55:24 -07001565 @SuppressWarnings("unchecked")
1566 public Notification(Parcel parcel) {
1567 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1568 // intents in extras are always written as the last entry.
1569 readFromParcelImpl(parcel);
1570 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001571 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001572 }
1573
1574 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 {
1576 int version = parcel.readInt();
1577
1578 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001579 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001580 if (parcel.readInt() != 0) {
1581 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001582 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1583 icon = mSmallIcon.getResId();
1584 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 number = parcel.readInt();
1587 if (parcel.readInt() != 0) {
1588 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1589 }
1590 if (parcel.readInt() != 0) {
1591 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1592 }
1593 if (parcel.readInt() != 0) {
1594 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1595 }
1596 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001597 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001598 }
1599 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1601 }
Joe Onorato561d3852010-11-20 18:09:34 -08001602 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001603 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 defaults = parcel.readInt();
1606 flags = parcel.readInt();
1607 if (parcel.readInt() != 0) {
1608 sound = Uri.CREATOR.createFromParcel(parcel);
1609 }
1610
1611 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001612 if (parcel.readInt() != 0) {
1613 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 vibrate = parcel.createLongArray();
1616 ledARGB = parcel.readInt();
1617 ledOnMS = parcel.readInt();
1618 ledOffMS = parcel.readInt();
1619 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001620
1621 if (parcel.readInt() != 0) {
1622 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1623 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001624
1625 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001626
John Spurlockfd7f1e02014-03-18 16:41:57 -04001627 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001628
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001629 mGroupKey = parcel.readString();
1630
1631 mSortKey = parcel.readString();
1632
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001633 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001634
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001635 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1636
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001637 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001638 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1639 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001640
Chris Wren8fd39ec2014-02-27 17:43:26 -05001641 if (parcel.readInt() != 0) {
1642 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1643 }
1644
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001645 visibility = parcel.readInt();
1646
1647 if (parcel.readInt() != 0) {
1648 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1649 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001650
1651 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
1653
Andy Stadler110988c2010-12-03 14:29:16 -08001654 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001655 public Notification clone() {
1656 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001657 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001658 return that;
1659 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001660
Daniel Sandler1a497d32013-04-18 14:52:45 -04001661 /**
1662 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1663 * of this into that.
1664 * @hide
1665 */
1666 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001667 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001668 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001669 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001670 that.number = this.number;
1671
1672 // PendingIntents are global, so there's no reason (or way) to clone them.
1673 that.contentIntent = this.contentIntent;
1674 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001675 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001676
1677 if (this.tickerText != null) {
1678 that.tickerText = this.tickerText.toString();
1679 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001680 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001681 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001682 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001683 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001684 that.contentView = this.contentView.clone();
1685 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001686 if (heavy && this.mLargeIcon != null) {
1687 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001688 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001689 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001690 that.sound = this.sound; // android.net.Uri is immutable
1691 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001692 if (this.audioAttributes != null) {
1693 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1694 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001695
1696 final long[] vibrate = this.vibrate;
1697 if (vibrate != null) {
1698 final int N = vibrate.length;
1699 final long[] vib = that.vibrate = new long[N];
1700 System.arraycopy(vibrate, 0, vib, 0, N);
1701 }
1702
1703 that.ledARGB = this.ledARGB;
1704 that.ledOnMS = this.ledOnMS;
1705 that.ledOffMS = this.ledOffMS;
1706 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001707
Joe Onorato18e69df2010-05-17 22:26:12 -07001708 that.flags = this.flags;
1709
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001710 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001711
John Spurlockfd7f1e02014-03-18 16:41:57 -04001712 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001713
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001714 that.mGroupKey = this.mGroupKey;
1715
1716 that.mSortKey = this.mSortKey;
1717
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001718 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001719 try {
1720 that.extras = new Bundle(this.extras);
1721 // will unparcel
1722 that.extras.size();
1723 } catch (BadParcelableException e) {
1724 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1725 that.extras = null;
1726 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001727 }
1728
Felipe Lemedd85da62016-06-28 11:29:54 -07001729 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1730 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001731 }
1732
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001733 if (this.actions != null) {
1734 that.actions = new Action[this.actions.length];
1735 for(int i=0; i<this.actions.length; i++) {
1736 that.actions[i] = this.actions[i].clone();
1737 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001738 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001739
Daniel Sandler1a497d32013-04-18 14:52:45 -04001740 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001741 that.bigContentView = this.bigContentView.clone();
1742 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001743
Chris Wren8fd39ec2014-02-27 17:43:26 -05001744 if (heavy && this.headsUpContentView != null) {
1745 that.headsUpContentView = this.headsUpContentView.clone();
1746 }
1747
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001748 that.visibility = this.visibility;
1749
1750 if (this.publicVersion != null) {
1751 that.publicVersion = new Notification();
1752 this.publicVersion.cloneInto(that.publicVersion, heavy);
1753 }
1754
Dan Sandler26e81cf2014-05-06 10:01:27 -04001755 that.color = this.color;
1756
Daniel Sandler1a497d32013-04-18 14:52:45 -04001757 if (!heavy) {
1758 that.lightenPayload(); // will clean out extras
1759 }
1760 }
1761
1762 /**
1763 * Removes heavyweight parts of the Notification object for archival or for sending to
1764 * listeners when the full contents are not necessary.
1765 * @hide
1766 */
1767 public final void lightenPayload() {
1768 tickerView = null;
1769 contentView = null;
1770 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001771 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001772 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001773 if (extras != null && !extras.isEmpty()) {
1774 final Set<String> keyset = extras.keySet();
1775 final int N = keyset.size();
1776 final String[] keys = keyset.toArray(new String[N]);
1777 for (int i=0; i<N; i++) {
1778 final String key = keys[i];
1779 final Object obj = extras.get(key);
1780 if (obj != null &&
1781 ( obj instanceof Parcelable
1782 || obj instanceof Parcelable[]
1783 || obj instanceof SparseArray
1784 || obj instanceof ArrayList)) {
1785 extras.remove(key);
1786 }
1787 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001788 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001789 }
1790
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001791 /**
1792 * Make sure this CharSequence is safe to put into a bundle, which basically
1793 * means it had better not be some custom Parcelable implementation.
1794 * @hide
1795 */
1796 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001797 if (cs == null) return cs;
1798 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1799 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1800 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001801 if (cs instanceof Parcelable) {
1802 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1803 + " instance is a custom Parcelable and not allowed in Notification");
1804 return cs.toString();
1805 }
Selim Cinek60a54252016-02-26 17:03:25 -08001806 return removeTextSizeSpans(cs);
1807 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001808
Selim Cinek60a54252016-02-26 17:03:25 -08001809 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1810 if (charSequence instanceof Spanned) {
1811 Spanned ss = (Spanned) charSequence;
1812 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1813 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1814 for (Object span : spans) {
1815 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001816 if (resultSpan instanceof CharacterStyle) {
1817 resultSpan = ((CharacterStyle) span).getUnderlying();
1818 }
1819 if (resultSpan instanceof TextAppearanceSpan) {
1820 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001821 resultSpan = new TextAppearanceSpan(
1822 originalSpan.getFamily(),
1823 originalSpan.getTextStyle(),
1824 -1,
1825 originalSpan.getTextColor(),
1826 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001827 } else if (resultSpan instanceof RelativeSizeSpan
1828 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001829 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001830 } else {
1831 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001832 }
1833 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1834 ss.getSpanFlags(span));
1835 }
1836 return builder;
1837 }
1838 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001839 }
1840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 public int describeContents() {
1842 return 0;
1843 }
1844
1845 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001846 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 */
Svet Ganovddb94882016-06-23 19:55:24 -07001848 public void writeToParcel(Parcel parcel, int flags) {
1849 // We need to mark all pending intents getting into the notification
1850 // system as being put there to later allow the notification ranker
1851 // to launch them and by doing so add the app to the battery saver white
1852 // list for a short period of time. The problem is that the system
1853 // cannot look into the extras as there may be parcelables there that
1854 // the platform does not know how to handle. To go around that we have
1855 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001856 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001857 if (collectPendingIntents) {
1858 PendingIntent.setOnMarshaledListener(
1859 (PendingIntent intent, Parcel out, int outFlags) -> {
1860 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001861 if (allPendingIntents == null) {
1862 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001863 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001864 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001865 }
1866 });
1867 }
1868 try {
1869 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1870 // want to intercept all pending events written to the pacel.
1871 writeToParcelImpl(parcel, flags);
1872 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001873 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001874 } finally {
1875 if (collectPendingIntents) {
1876 PendingIntent.setOnMarshaledListener(null);
1877 }
1878 }
1879 }
1880
1881 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 parcel.writeInt(1);
1883
1884 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001885 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001886 if (mSmallIcon == null && icon != 0) {
1887 // you snuck an icon in here without using the builder; let's try to keep it
1888 mSmallIcon = Icon.createWithResource("", icon);
1889 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001890 if (mSmallIcon != null) {
1891 parcel.writeInt(1);
1892 mSmallIcon.writeToParcel(parcel, 0);
1893 } else {
1894 parcel.writeInt(0);
1895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 parcel.writeInt(number);
1897 if (contentIntent != null) {
1898 parcel.writeInt(1);
1899 contentIntent.writeToParcel(parcel, 0);
1900 } else {
1901 parcel.writeInt(0);
1902 }
1903 if (deleteIntent != null) {
1904 parcel.writeInt(1);
1905 deleteIntent.writeToParcel(parcel, 0);
1906 } else {
1907 parcel.writeInt(0);
1908 }
1909 if (tickerText != null) {
1910 parcel.writeInt(1);
1911 TextUtils.writeToParcel(tickerText, parcel, flags);
1912 } else {
1913 parcel.writeInt(0);
1914 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001915 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001916 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001917 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001918 } else {
1919 parcel.writeInt(0);
1920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 if (contentView != null) {
1922 parcel.writeInt(1);
1923 contentView.writeToParcel(parcel, 0);
1924 } else {
1925 parcel.writeInt(0);
1926 }
Selim Cinek279fa862016-06-14 10:57:25 -07001927 if (mLargeIcon == null && largeIcon != null) {
1928 // you snuck an icon in here without using the builder; let's try to keep it
1929 mLargeIcon = Icon.createWithBitmap(largeIcon);
1930 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001931 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001932 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001933 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001934 } else {
1935 parcel.writeInt(0);
1936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937
1938 parcel.writeInt(defaults);
1939 parcel.writeInt(this.flags);
1940
1941 if (sound != null) {
1942 parcel.writeInt(1);
1943 sound.writeToParcel(parcel, 0);
1944 } else {
1945 parcel.writeInt(0);
1946 }
1947 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001948
1949 if (audioAttributes != null) {
1950 parcel.writeInt(1);
1951 audioAttributes.writeToParcel(parcel, 0);
1952 } else {
1953 parcel.writeInt(0);
1954 }
1955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 parcel.writeLongArray(vibrate);
1957 parcel.writeInt(ledARGB);
1958 parcel.writeInt(ledOnMS);
1959 parcel.writeInt(ledOffMS);
1960 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001961
1962 if (fullScreenIntent != null) {
1963 parcel.writeInt(1);
1964 fullScreenIntent.writeToParcel(parcel, 0);
1965 } else {
1966 parcel.writeInt(0);
1967 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001968
1969 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001970
John Spurlockfd7f1e02014-03-18 16:41:57 -04001971 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001972
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001973 parcel.writeString(mGroupKey);
1974
1975 parcel.writeString(mSortKey);
1976
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001977 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001978
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001979 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001980
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001981 if (bigContentView != null) {
1982 parcel.writeInt(1);
1983 bigContentView.writeToParcel(parcel, 0);
1984 } else {
1985 parcel.writeInt(0);
1986 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001987
Chris Wren8fd39ec2014-02-27 17:43:26 -05001988 if (headsUpContentView != null) {
1989 parcel.writeInt(1);
1990 headsUpContentView.writeToParcel(parcel, 0);
1991 } else {
1992 parcel.writeInt(0);
1993 }
1994
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001995 parcel.writeInt(visibility);
1996
1997 if (publicVersion != null) {
1998 parcel.writeInt(1);
1999 publicVersion.writeToParcel(parcel, 0);
2000 } else {
2001 parcel.writeInt(0);
2002 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002003
2004 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
2006
2007 /**
2008 * Parcelable.Creator that instantiates Notification objects
2009 */
2010 public static final Parcelable.Creator<Notification> CREATOR
2011 = new Parcelable.Creator<Notification>()
2012 {
2013 public Notification createFromParcel(Parcel parcel)
2014 {
2015 return new Notification(parcel);
2016 }
2017
2018 public Notification[] newArray(int size)
2019 {
2020 return new Notification[size];
2021 }
2022 };
2023
2024 /**
2025 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2026 * layout.
2027 *
2028 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2029 * in the view.</p>
2030 * @param context The context for your application / activity.
2031 * @param contentTitle The title that goes in the expanded entry.
2032 * @param contentText The text that goes in the expanded entry.
2033 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2034 * If this is an activity, it must include the
2035 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002036 * that you take care of task management as described in the
2037 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2038 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002039 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002040 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002041 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002043 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 public void setLatestEventInfo(Context context,
2045 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002046 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2047 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2048 new Throwable());
2049 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002050
Selim Cinek4ac6f602016-06-13 15:47:03 -07002051 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2052 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2053 }
2054
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002055 // ensure that any information already set directly is preserved
2056 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002057
2058 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002060 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002063 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002065 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002066
2067 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
2069
Julia Reynoldsda303542015-11-23 14:00:20 -05002070 /**
2071 * @hide
2072 */
2073 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002074 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2075 }
2076
2077 /**
2078 * @hide
2079 */
2080 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2081 Notification notification) {
2082 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2083 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002084 }
2085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 @Override
2087 public String toString() {
2088 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089 sb.append("Notification(pri=");
2090 sb.append(priority);
2091 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002092 if (contentView != null) {
2093 sb.append(contentView.getPackage());
2094 sb.append("/0x");
2095 sb.append(Integer.toHexString(contentView.getLayoutId()));
2096 } else {
2097 sb.append("null");
2098 }
2099 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002100 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2101 sb.append("default");
2102 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 int N = this.vibrate.length-1;
2104 sb.append("[");
2105 for (int i=0; i<N; i++) {
2106 sb.append(this.vibrate[i]);
2107 sb.append(',');
2108 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002109 if (N != -1) {
2110 sb.append(this.vibrate[N]);
2111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 } else {
2114 sb.append("null");
2115 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002116 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002117 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002119 } else if (this.sound != null) {
2120 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 } else {
2122 sb.append("null");
2123 }
Chris Wren365b6d32015-07-16 10:39:26 -04002124 if (this.tickerText != null) {
2125 sb.append(" tick");
2126 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002127 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002129 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002130 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002131 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002132 if (this.category != null) {
2133 sb.append(" category=");
2134 sb.append(this.category);
2135 }
2136 if (this.mGroupKey != null) {
2137 sb.append(" groupKey=");
2138 sb.append(this.mGroupKey);
2139 }
2140 if (this.mSortKey != null) {
2141 sb.append(" sortKey=");
2142 sb.append(this.mSortKey);
2143 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002144 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002145 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002146 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002147 }
2148 sb.append(" vis=");
2149 sb.append(visibilityToString(this.visibility));
2150 if (this.publicVersion != null) {
2151 sb.append(" publicVersion=");
2152 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002153 }
2154 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 return sb.toString();
2156 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002157
Dan Sandler1b718782014-07-18 12:43:45 -04002158 /**
2159 * {@hide}
2160 */
2161 public static String visibilityToString(int vis) {
2162 switch (vis) {
2163 case VISIBILITY_PRIVATE:
2164 return "PRIVATE";
2165 case VISIBILITY_PUBLIC:
2166 return "PUBLIC";
2167 case VISIBILITY_SECRET:
2168 return "SECRET";
2169 default:
2170 return "UNKNOWN(" + String.valueOf(vis) + ")";
2171 }
2172 }
2173
Joe Onoratocb109a02011-01-18 17:57:41 -08002174 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002175 * {@hide}
2176 */
2177 public static String priorityToString(@Priority int pri) {
2178 switch (pri) {
2179 case PRIORITY_MIN:
2180 return "MIN";
2181 case PRIORITY_LOW:
2182 return "LOW";
2183 case PRIORITY_DEFAULT:
2184 return "DEFAULT";
2185 case PRIORITY_HIGH:
2186 return "HIGH";
2187 case PRIORITY_MAX:
2188 return "MAX";
2189 default:
2190 return "UNKNOWN(" + String.valueOf(pri) + ")";
2191 }
2192 }
2193
2194 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002195 * The small icon representing this notification in the status bar and content view.
2196 *
2197 * @return the small icon representing this notification.
2198 *
2199 * @see Builder#getSmallIcon()
2200 * @see Builder#setSmallIcon(Icon)
2201 */
2202 public Icon getSmallIcon() {
2203 return mSmallIcon;
2204 }
2205
2206 /**
2207 * Used when notifying to clean up legacy small icons.
2208 * @hide
2209 */
2210 public void setSmallIcon(Icon icon) {
2211 mSmallIcon = icon;
2212 }
2213
2214 /**
2215 * The large icon shown in this notification's content view.
2216 * @see Builder#getLargeIcon()
2217 * @see Builder#setLargeIcon(Icon)
2218 */
2219 public Icon getLargeIcon() {
2220 return mLargeIcon;
2221 }
2222
2223 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002224 * @hide
2225 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002226 public boolean isGroupSummary() {
2227 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2228 }
2229
2230 /**
2231 * @hide
2232 */
2233 public boolean isGroupChild() {
2234 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2235 }
2236
2237 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002238 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002239 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002240 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002241 * content views using the platform's notification layout template. If your app supports
2242 * versions of Android as old as API level 4, you can instead use
2243 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2244 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2245 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002246 *
Scott Main183bf112012-08-13 19:12:13 -07002247 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002248 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002249 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002250 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002251 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2252 * .setContentText(subject)
2253 * .setSmallIcon(R.drawable.new_mail)
2254 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002255 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002256 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002257 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002258 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002259 /**
2260 * @hide
2261 */
2262 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2263 "android.rebuild.contentViewActionCount";
2264 /**
2265 * @hide
2266 */
2267 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2268 = "android.rebuild.bigViewActionCount";
2269 /**
2270 * @hide
2271 */
2272 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2273 = "android.rebuild.hudViewActionCount";
2274
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002275 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002276
Joe Onorato46439ce2010-11-19 13:56:21 -08002277 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002278 private Notification mN;
2279 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002280 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002281 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2282 private ArrayList<String> mPersonList = new ArrayList<String>();
2283 private NotificationColorUtil mColorUtil;
2284 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002285
2286 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002287 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2288 */
2289 private int mCachedContrastColor = COLOR_INVALID;
2290 private int mCachedContrastColorIsFor = COLOR_INVALID;
2291
2292 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002293 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002294 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002295
2296 * <table>
2297 * <tr><th align=right>priority</th>
2298 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2299 * <tr><th align=right>when</th>
2300 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2301 * <tr><th align=right>audio stream</th>
2302 * <td>{@link #STREAM_DEFAULT}</td></tr>
2303 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002304 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002305
2306 * @param context
2307 * A {@link Context} that will be used by the Builder to construct the
2308 * RemoteViews. The Context will not be held past the lifetime of this Builder
2309 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002310 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002311 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002312 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002313 }
2314
Joe Onoratocb109a02011-01-18 17:57:41 -08002315 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002316 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002317 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002318 public Builder(Context context, Notification toAdopt) {
2319 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002320
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002321 if (toAdopt == null) {
2322 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002323 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2324 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2325 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002326 mN.priority = PRIORITY_DEFAULT;
2327 mN.visibility = VISIBILITY_PRIVATE;
2328 } else {
2329 mN = toAdopt;
2330 if (mN.actions != null) {
2331 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002332 }
2333
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002334 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2335 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2336 }
2337
Selim Cinek4ac6f602016-06-13 15:47:03 -07002338 if (mN.getSmallIcon() == null && mN.icon != 0) {
2339 setSmallIcon(mN.icon);
2340 }
2341
2342 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2343 setLargeIcon(mN.largeIcon);
2344 }
2345
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002346 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2347 if (!TextUtils.isEmpty(templateClass)) {
2348 final Class<? extends Style> styleClass
2349 = getNotificationStyleClass(templateClass);
2350 if (styleClass == null) {
2351 Log.d(TAG, "Unknown style class: " + templateClass);
2352 } else {
2353 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002354 final Constructor<? extends Style> ctor =
2355 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002356 ctor.setAccessible(true);
2357 final Style style = ctor.newInstance();
2358 style.restoreFromExtras(mN.extras);
2359
2360 if (style != null) {
2361 setStyle(style);
2362 }
2363 } catch (Throwable t) {
2364 Log.e(TAG, "Could not create Style", t);
2365 }
2366 }
2367 }
2368
2369 }
2370 }
2371
2372 private NotificationColorUtil getColorUtil() {
2373 if (!mColorUtilInited) {
2374 mColorUtilInited = true;
2375 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2376 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002377 }
2378 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002379 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002380 }
2381
2382 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002383 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002384 *
2385 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2386 * shown anymore by default and must be opted into by using
2387 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002388 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002390 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002391 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002392 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002393 return this;
2394 }
2395
Joe Onoratocb109a02011-01-18 17:57:41 -08002396 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002397 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002398 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002399 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2400 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002401 */
2402 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002403 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002404 return this;
2405 }
2406
2407 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002408 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002409 *
2410 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002411 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002412 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002413 * Useful when showing an elapsed time (like an ongoing phone call).
2414 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002415 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002416 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002417 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002418 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002419 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002420 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002421 */
2422 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002423 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002424 return this;
2425 }
2426
2427 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002428 * Sets the Chronometer to count down instead of counting up.
2429 *
2430 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2431 * If it isn't set the chronometer will count up.
2432 *
2433 * @see #setUsesChronometer(boolean)
2434 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002435 public Builder setChronometerCountDown(boolean countDown) {
2436 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002437 return this;
2438 }
2439
2440 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002441 * Set the small icon resource, which will be used to represent the notification in the
2442 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002443 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002444
2445 * The platform template for the expanded view will draw this icon in the left, unless a
2446 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2447 * icon will be moved to the right-hand side.
2448 *
2449
2450 * @param icon
2451 * A resource ID in the application's package of the drawable to use.
2452 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002453 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002454 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002455 return setSmallIcon(icon != 0
2456 ? Icon.createWithResource(mContext, icon)
2457 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002458 }
2459
Joe Onoratocb109a02011-01-18 17:57:41 -08002460 /**
2461 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2462 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2463 * LevelListDrawable}.
2464 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002465 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002466 * @param level The level to use for the icon.
2467 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002468 * @see Notification#icon
2469 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002470 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002471 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002472 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002473 return setSmallIcon(icon);
2474 }
2475
2476 /**
2477 * Set the small icon, which will be used to represent the notification in the
2478 * status bar and content view (unless overriden there by a
2479 * {@link #setLargeIcon(Bitmap) large icon}).
2480 *
2481 * @param icon An Icon object to use.
2482 * @see Notification#icon
2483 */
2484 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002485 mN.setSmallIcon(icon);
2486 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2487 mN.icon = icon.getResId();
2488 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002489 return this;
2490 }
2491
Joe Onoratocb109a02011-01-18 17:57:41 -08002492 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002493 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002494 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002495 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002496 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002497 return this;
2498 }
2499
Joe Onoratocb109a02011-01-18 17:57:41 -08002500 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002501 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002502 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002503 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002504 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002505 return this;
2506 }
2507
Joe Onoratocb109a02011-01-18 17:57:41 -08002508 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002509 * This provides some additional information that is displayed in the notification. No
2510 * guarantees are given where exactly it is displayed.
2511 *
2512 * <p>This information should only be provided if it provides an essential
2513 * benefit to the understanding of the notification. The more text you provide the
2514 * less readable it becomes. For example, an email client should only provide the account
2515 * name here if more than one email account has been added.</p>
2516 *
2517 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2518 * notification header area.
2519 *
2520 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2521 * this will be shown in the third line of text in the platform notification template.
2522 * You should not be using {@link #setProgress(int, int, boolean)} at the
2523 * same time on those versions; they occupy the same place.
2524 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002525 */
2526 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002527 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002528 return this;
2529 }
2530
2531 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002532 * Set the remote input history.
2533 *
2534 * This should be set to the most recent inputs that have been sent
2535 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2536 * longer relevant (e.g. for chat notifications once the other party has responded).
2537 *
2538 * The most recent input must be stored at the 0 index, the second most recent at the
2539 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2540 * and how much of each individual input is shown.
2541 *
2542 * <p>Note: The reply text will only be shown on notifications that have least one action
2543 * with a {@code RemoteInput}.</p>
2544 */
2545 public Builder setRemoteInputHistory(CharSequence[] text) {
2546 if (text == null) {
2547 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2548 } else {
2549 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2550 CharSequence[] safe = new CharSequence[N];
2551 for (int i = 0; i < N; i++) {
2552 safe[i] = safeCharSequence(text[i]);
2553 }
2554 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2555 }
2556 return this;
2557 }
2558
2559 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002560 * Set the large number at the right-hand side of the notification. This is
2561 * equivalent to setContentInfo, although it might show the number in a different
2562 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002563 *
2564 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002565 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002566 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002567 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002568 return this;
2569 }
2570
Joe Onoratocb109a02011-01-18 17:57:41 -08002571 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002572 * A small piece of additional information pertaining to this notification.
2573 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002574 * The platform template will draw this on the last line of the notification, at the far
2575 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002576 *
2577 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2578 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2579 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002580 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002581 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002582 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002583 return this;
2584 }
2585
Joe Onoratocb109a02011-01-18 17:57:41 -08002586 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002587 * Set the progress this notification represents.
2588 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002589 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002590 */
2591 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002592 mN.extras.putInt(EXTRA_PROGRESS, progress);
2593 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2594 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002595 return this;
2596 }
2597
2598 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002599 * Supply a custom RemoteViews to use instead of the platform template.
2600 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002601 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002602 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002603 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002604 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002605 return setCustomContentView(views);
2606 }
2607
2608 /**
2609 * Supply custom RemoteViews to use instead of the platform template.
2610 *
2611 * This will override the layout that would otherwise be constructed by this Builder
2612 * object.
2613 */
2614 public Builder setCustomContentView(RemoteViews contentView) {
2615 mN.contentView = contentView;
2616 return this;
2617 }
2618
2619 /**
2620 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2621 *
2622 * This will override the expanded layout that would otherwise be constructed by this
2623 * Builder object.
2624 */
2625 public Builder setCustomBigContentView(RemoteViews contentView) {
2626 mN.bigContentView = contentView;
2627 return this;
2628 }
2629
2630 /**
2631 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2632 *
2633 * This will override the heads-up layout that would otherwise be constructed by this
2634 * Builder object.
2635 */
2636 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2637 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002638 return this;
2639 }
2640
Joe Onoratocb109a02011-01-18 17:57:41 -08002641 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002642 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2643 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002644 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2645 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2646 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002647 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002648 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002649 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002650 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002651 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002652 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002653 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002654 return this;
2655 }
2656
Joe Onoratocb109a02011-01-18 17:57:41 -08002657 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002658 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2659 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002660 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002661 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002662 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002663 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002664 return this;
2665 }
2666
Joe Onoratocb109a02011-01-18 17:57:41 -08002667 /**
2668 * An intent to launch instead of posting the notification to the status bar.
2669 * Only for use with extremely high-priority notifications demanding the user's
2670 * <strong>immediate</strong> attention, such as an incoming phone call or
2671 * alarm clock that the user has explicitly set to a particular time.
2672 * If this facility is used for something else, please give the user an option
2673 * to turn it off and use a normal notification, as this can be extremely
2674 * disruptive.
2675 *
Chris Wren47c20a12014-06-18 17:27:29 -04002676 * <p>
2677 * The system UI may choose to display a heads-up notification, instead of
2678 * launching this intent, while the user is using the device.
2679 * </p>
2680 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002681 * @param intent The pending intent to launch.
2682 * @param highPriority Passing true will cause this notification to be sent
2683 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002684 *
2685 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002686 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002687 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002688 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002689 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2690 return this;
2691 }
2692
Joe Onoratocb109a02011-01-18 17:57:41 -08002693 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002694 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002695 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002696 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002697 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002698 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002699 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002700 return this;
2701 }
2702
Joe Onoratocb109a02011-01-18 17:57:41 -08002703 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002704 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002705 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002706 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002707 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002708 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002709 setTicker(tickerText);
2710 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002711 return this;
2712 }
2713
Joe Onoratocb109a02011-01-18 17:57:41 -08002714 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002715 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002716 *
2717 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002718 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2719 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002720 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002721 public Builder setLargeIcon(Bitmap b) {
2722 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2723 }
2724
2725 /**
2726 * Add a large icon to the notification content view.
2727 *
2728 * In the platform template, this image will be shown on the left of the notification view
2729 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2730 * badge atop the large icon).
2731 */
2732 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002733 mN.mLargeIcon = icon;
2734 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002735 return this;
2736 }
2737
Joe Onoratocb109a02011-01-18 17:57:41 -08002738 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002739 * Set the sound to play.
2740 *
John Spurlockc0650f022014-07-19 13:22:39 -04002741 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2742 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002743 *
Chris Wren47c20a12014-06-18 17:27:29 -04002744 * <p>
2745 * A notification that is noisy is more likely to be presented as a heads-up notification.
2746 * </p>
2747 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002748 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002749 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002750 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002751 mN.sound = sound;
2752 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002753 return this;
2754 }
2755
Joe Onoratocb109a02011-01-18 17:57:41 -08002756 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002757 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002758 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002759 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2760 *
Chris Wren47c20a12014-06-18 17:27:29 -04002761 * <p>
2762 * A notification that is noisy is more likely to be presented as a heads-up notification.
2763 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002764 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002765 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002766 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002767 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002768 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002769 mN.sound = sound;
2770 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002771 return this;
2772 }
2773
Joe Onoratocb109a02011-01-18 17:57:41 -08002774 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002775 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2776 * use during playback.
2777 *
2778 * <p>
2779 * A notification that is noisy is more likely to be presented as a heads-up notification.
2780 * </p>
2781 *
2782 * @see Notification#sound
2783 */
2784 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002785 mN.sound = sound;
2786 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002787 return this;
2788 }
2789
2790 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002791 * Set the vibration pattern to use.
2792 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002793 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2794 * <code>pattern</code> parameter.
2795 *
Chris Wren47c20a12014-06-18 17:27:29 -04002796 * <p>
2797 * A notification that vibrates is more likely to be presented as a heads-up notification.
2798 * </p>
2799 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002800 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002801 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002802 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002803 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002804 return this;
2805 }
2806
Joe Onoratocb109a02011-01-18 17:57:41 -08002807 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002808 * Set the desired color for the indicator LED on the device, as well as the
2809 * blink duty cycle (specified in milliseconds).
2810 *
2811
2812 * Not all devices will honor all (or even any) of these values.
2813 *
2814
2815 * @see Notification#ledARGB
2816 * @see Notification#ledOnMS
2817 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002818 */
Tor Norbye80756e32015-03-02 09:39:27 -08002819 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002820 mN.ledARGB = argb;
2821 mN.ledOnMS = onMs;
2822 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002823 if (onMs != 0 || offMs != 0) {
2824 mN.flags |= FLAG_SHOW_LIGHTS;
2825 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002826 return this;
2827 }
2828
Joe Onoratocb109a02011-01-18 17:57:41 -08002829 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002830 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002831 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002832
2833 * Ongoing notifications cannot be dismissed by the user, so your application or service
2834 * must take care of canceling them.
2835 *
2836
2837 * They are typically used to indicate a background task that the user is actively engaged
2838 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2839 * (e.g., a file download, sync operation, active network connection).
2840 *
2841
2842 * @see Notification#FLAG_ONGOING_EVENT
2843 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002844 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002845 public Builder setOngoing(boolean ongoing) {
2846 setFlag(FLAG_ONGOING_EVENT, ongoing);
2847 return this;
2848 }
2849
Joe Onoratocb109a02011-01-18 17:57:41 -08002850 /**
2851 * Set this flag if you would only like the sound, vibrate
2852 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002853 *
2854 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002855 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002856 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2857 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2858 return this;
2859 }
2860
Joe Onoratocb109a02011-01-18 17:57:41 -08002861 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002862 * Make this notification automatically dismissed when the user touches it. The
2863 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2864 *
2865 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002866 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002867 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002868 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002869 return this;
2870 }
2871
Joe Onoratocb109a02011-01-18 17:57:41 -08002872 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002873 * Set whether or not this notification should not bridge to other devices.
2874 *
2875 * <p>Some notifications can be bridged to other devices for remote display.
2876 * This hint can be set to recommend this notification not be bridged.
2877 */
2878 public Builder setLocalOnly(boolean localOnly) {
2879 setFlag(FLAG_LOCAL_ONLY, localOnly);
2880 return this;
2881 }
2882
2883 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002884 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002885 * <p>
2886 * The value should be one or more of the following fields combined with
2887 * bitwise-or:
2888 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2889 * <p>
2890 * For all default values, use {@link #DEFAULT_ALL}.
2891 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002892 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002893 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002894 return this;
2895 }
2896
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002897 /**
2898 * Set the priority of this notification.
2899 *
2900 * @see Notification#priority
2901 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002902 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002903 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002904 return this;
2905 }
Joe Malin8d40d042012-11-05 11:36:40 -08002906
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002907 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002908 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002909 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002910 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002911 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002912 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002913 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002914 return this;
2915 }
2916
2917 /**
Chris Wrendde75302014-03-26 17:24:15 -04002918 * Add a person that is relevant to this notification.
2919 *
Chris Wrene6c48932014-09-29 17:19:27 -04002920 * <P>
2921 * Depending on user preferences, this annotation may allow the notification to pass
2922 * through interruption filters, and to appear more prominently in the user interface.
2923 * </P>
2924 *
2925 * <P>
2926 * The person should be specified by the {@code String} representation of a
2927 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2928 * </P>
2929 *
2930 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2931 * URIs. The path part of these URIs must exist in the contacts database, in the
2932 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2933 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2934 * </P>
2935 *
2936 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002937 * @see Notification#EXTRA_PEOPLE
2938 */
Chris Wrene6c48932014-09-29 17:19:27 -04002939 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002940 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002941 return this;
2942 }
2943
2944 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002945 * Set this notification to be part of a group of notifications sharing the same key.
2946 * Grouped notifications may display in a cluster or stack on devices which
2947 * support such rendering.
2948 *
2949 * <p>To make this notification the summary for its group, also call
2950 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2951 * {@link #setSortKey}.
2952 * @param groupKey The group key of the group.
2953 * @return this object for method chaining
2954 */
2955 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002956 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002957 return this;
2958 }
2959
2960 /**
2961 * Set this notification to be the group summary for a group of notifications.
2962 * Grouped notifications may display in a cluster or stack on devices which
2963 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2964 * @param isGroupSummary Whether this notification should be a group summary.
2965 * @return this object for method chaining
2966 */
2967 public Builder setGroupSummary(boolean isGroupSummary) {
2968 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2969 return this;
2970 }
2971
2972 /**
2973 * Set a sort key that orders this notification among other notifications from the
2974 * same package. This can be useful if an external sort was already applied and an app
2975 * would like to preserve this. Notifications will be sorted lexicographically using this
2976 * value, although providing different priorities in addition to providing sort key may
2977 * cause this value to be ignored.
2978 *
2979 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002980 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002981 *
2982 * @see String#compareTo(String)
2983 */
2984 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002985 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002986 return this;
2987 }
2988
2989 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002990 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002991 *
Griff Hazen720042b2014-02-24 15:46:56 -08002992 * <p>Values within the Bundle will replace existing extras values in this Builder.
2993 *
2994 * @see Notification#extras
2995 */
Griff Hazen959591e2014-05-15 22:26:18 -07002996 public Builder addExtras(Bundle extras) {
2997 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002998 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002999 }
3000 return this;
3001 }
3002
3003 /**
3004 * Set metadata for this notification.
3005 *
3006 * <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 -04003007 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003008 * called.
3009 *
Griff Hazen720042b2014-02-24 15:46:56 -08003010 * <p>Replaces any existing extras values with those from the provided Bundle.
3011 * Use {@link #addExtras} to merge in metadata instead.
3012 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003013 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003014 */
Griff Hazen959591e2014-05-15 22:26:18 -07003015 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003016 if (extras != null) {
3017 mUserExtras = extras;
3018 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003019 return this;
3020 }
3021
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003022 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003023 * Get the current metadata Bundle used by this notification Builder.
3024 *
3025 * <p>The returned Bundle is shared with this Builder.
3026 *
3027 * <p>The current contents of this Bundle are copied into the Notification each time
3028 * {@link #build()} is called.
3029 *
3030 * @see Notification#extras
3031 */
3032 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003033 return mUserExtras;
3034 }
3035
3036 private Bundle getAllExtras() {
3037 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3038 saveExtras.putAll(mN.extras);
3039 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003040 }
3041
3042 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003043 * Add an action to this notification. Actions are typically displayed by
3044 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003045 * <p>
3046 * Every action must have an icon (32dp square and matching the
3047 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3048 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3049 * <p>
3050 * A notification in its expanded form can display up to 3 actions, from left to right in
3051 * the order they were added. Actions will not be displayed when the notification is
3052 * collapsed, however, so be sure that any essential functions may be accessed by the user
3053 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003054 *
3055 * @param icon Resource ID of a drawable that represents the action.
3056 * @param title Text describing the action.
3057 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003058 *
3059 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003060 */
Dan Sandler86647982015-05-13 23:41:13 -04003061 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003062 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003063 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003064 return this;
3065 }
3066
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003067 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003068 * Add an action to this notification. Actions are typically displayed by
3069 * the system as a button adjacent to the notification content.
3070 * <p>
3071 * Every action must have an icon (32dp square and matching the
3072 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3073 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3074 * <p>
3075 * A notification in its expanded form can display up to 3 actions, from left to right in
3076 * the order they were added. Actions will not be displayed when the notification is
3077 * collapsed, however, so be sure that any essential functions may be accessed by the user
3078 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3079 *
3080 * @param action The action to add.
3081 */
3082 public Builder addAction(Action action) {
3083 mActions.add(action);
3084 return this;
3085 }
3086
3087 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003088 * Alter the complete list of actions attached to this notification.
3089 * @see #addAction(Action).
3090 *
3091 * @param actions
3092 * @return
3093 */
3094 public Builder setActions(Action... actions) {
3095 mActions.clear();
3096 for (int i = 0; i < actions.length; i++) {
3097 mActions.add(actions[i]);
3098 }
3099 return this;
3100 }
3101
3102 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003103 * Add a rich notification style to be applied at build time.
3104 *
3105 * @param style Object responsible for modifying the notification style.
3106 */
3107 public Builder setStyle(Style style) {
3108 if (mStyle != style) {
3109 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003110 if (mStyle != null) {
3111 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003112 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3113 } else {
3114 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003115 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003116 }
3117 return this;
3118 }
3119
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003120 /**
3121 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003122 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003123 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3124 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3125 *
3126 * @return The same Builder.
3127 */
3128 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003129 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003130 return this;
3131 }
3132
3133 /**
3134 * Supply a replacement Notification whose contents should be shown in insecure contexts
3135 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3136 * @param n A replacement notification, presumably with some or all info redacted.
3137 * @return The same Builder.
3138 */
3139 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003140 if (n != null) {
3141 mN.publicVersion = new Notification();
3142 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3143 } else {
3144 mN.publicVersion = null;
3145 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003146 return this;
3147 }
3148
Griff Hazenb720abe2014-05-20 13:15:30 -07003149 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003150 * Apply an extender to this notification builder. Extenders may be used to add
3151 * metadata or change options on this builder.
3152 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003153 public Builder extend(Extender extender) {
3154 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003155 return this;
3156 }
3157
Dan Sandler4e787062015-06-17 15:09:48 -04003158 /**
3159 * @hide
3160 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003161 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003162 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003163 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003164 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003165 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003166 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003167 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003168 }
3169
Dan Sandler26e81cf2014-05-06 10:01:27 -04003170 /**
3171 * Sets {@link Notification#color}.
3172 *
3173 * @param argb The accent color to use
3174 *
3175 * @return The same Builder.
3176 */
Tor Norbye80756e32015-03-02 09:39:27 -08003177 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003178 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003179 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003180 return this;
3181 }
3182
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003183 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003184 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3185 // This user can never be a badged profile,
3186 // and also includes USER_ALL system notifications.
3187 return null;
3188 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003189 // Note: This assumes that the current user can read the profile badge of the
3190 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003191 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003192 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003193 }
3194
3195 private Bitmap getProfileBadge() {
3196 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003197 if (badge == null) {
3198 return null;
3199 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003200 final int size = mContext.getResources().getDimensionPixelSize(
3201 R.dimen.notification_badge_size);
3202 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003203 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003204 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003205 badge.draw(canvas);
3206 return bitmap;
3207 }
3208
Selim Cinekc848c3a2016-01-13 15:27:30 -08003209 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003210 Bitmap profileBadge = getProfileBadge();
3211
Kenny Guy98193ea2014-07-24 19:54:37 +01003212 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003213 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3214 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003215 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003216 }
3217
Christoph Studerfe718432014-09-01 18:21:18 +02003218 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003219 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003220 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003221 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003222 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003223 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003224 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003225 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003226 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003227 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003228 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003229 }
3230
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003231 /**
3232 * Resets the notification header to its original state
3233 */
3234 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003235 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003236 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003237 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003238 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003239 contentView.setViewVisibility(R.id.header_text, View.GONE);
3240 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003241 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003242 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003243 contentView.setImageViewIcon(R.id.profile_badge, null);
3244 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003245 }
3246
3247 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003248 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3249 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003250 }
3251
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003252 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003253 return applyStandardTemplate(resId, true /* hasProgress */);
3254 }
3255
3256 /**
3257 * @param hasProgress whether the progress bar should be shown and set
3258 */
3259 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003260 final Bundle ex = mN.extras;
3261
3262 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3263 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3264 return applyStandardTemplate(resId, hasProgress, title, text);
3265 }
3266
3267 /**
3268 * @param hasProgress whether the progress bar should be shown and set
3269 */
3270 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3271 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003272 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003273
Christoph Studerfe718432014-09-01 18:21:18 +02003274 resetStandardTemplate(contentView);
3275
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003276 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003277
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003278 bindNotificationHeader(contentView);
3279 bindLargeIcon(contentView);
Selim Cinek954cc232016-05-20 13:29:23 -07003280 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003281 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003282 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003283 contentView.setTextViewText(R.id.title, title);
Selim Cinek954cc232016-05-20 13:29:23 -07003284 contentView.setViewLayoutWidth(R.id.title, showProgress
3285 ? ViewGroup.LayoutParams.WRAP_CONTENT
3286 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003287 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07003288 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003289 int textId = showProgress ? com.android.internal.R.id.text_line_1
3290 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003291 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003292 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003293 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003294
Selim Cinek279fa862016-06-14 10:57:25 -07003295 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003296
Selim Cinek29603462015-11-17 19:04:39 -08003297 return contentView;
3298 }
3299
Selim Cinek860b6da2015-12-16 19:02:19 -08003300 /**
3301 * @param remoteView the remote view to update the minheight in
3302 * @param hasMinHeight does it have a mimHeight
3303 * @hide
3304 */
3305 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3306 int minHeight = 0;
3307 if (hasMinHeight) {
3308 // we need to set the minHeight of the notification
3309 minHeight = mContext.getResources().getDimensionPixelSize(
3310 com.android.internal.R.dimen.notification_min_content_height);
3311 }
3312 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3313 }
3314
Selim Cinek29603462015-11-17 19:04:39 -08003315 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003316 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3317 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3318 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3319 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003320 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003321 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003322 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003323 contentView.setProgressBackgroundTintList(
3324 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3325 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003326 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003327 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003328 contentView.setProgressTintList(R.id.progress, colorStateList);
3329 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003330 }
Selim Cinek29603462015-11-17 19:04:39 -08003331 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003332 } else {
3333 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003334 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003335 }
Joe Onorato561d3852010-11-20 18:09:34 -08003336 }
3337
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003338 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003339 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3340 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3341 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003342 if (mN.mLargeIcon != null) {
3343 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3344 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3345 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003346 int endMargin = R.dimen.notification_content_picture_margin;
3347 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3348 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3349 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003350 }
3351 }
3352
3353 private void bindNotificationHeader(RemoteViews contentView) {
3354 bindSmallIcon(contentView);
3355 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003356 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003357 bindHeaderChronometerAndTime(contentView);
3358 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003359 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003360 }
3361
3362 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003363 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003364 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003365 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003366 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003367 }
3368
3369 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3370 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003371 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003372 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3373 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3374 contentView.setLong(R.id.chronometer, "setBase",
3375 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3376 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003377 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003378 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003379 } else {
3380 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3381 contentView.setLong(R.id.time, "setTime", mN.when);
3382 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003383 } else {
3384 // We still want a time to be set but gone, such that we can show and hide it
3385 // on demand in case it's a child notification without anything in the header
3386 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003387 }
3388 }
3389
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003390 private void bindHeaderText(RemoteViews contentView) {
3391 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3392 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003393 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003394 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003395 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003396 if (headerText == null
3397 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3398 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3399 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3400 }
3401 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003402 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003403 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3404 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3405 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003406 }
3407 }
3408
Adrian Rooseba05822016-04-22 17:09:27 -07003409 /**
3410 * @hide
3411 */
3412 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003413 CharSequence name = null;
3414 final PackageManager pm = mContext.getPackageManager();
3415 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3416 // only system packages which lump together a bunch of unrelated stuff
3417 // may substitute a different name to make the purpose of the
3418 // notification more clear. the correct package label should always
3419 // be accessible via SystemUI.
3420 final String pkg = mContext.getPackageName();
3421 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3422 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3423 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3424 name = subName;
3425 } else {
3426 Log.w(TAG, "warning: pkg "
3427 + pkg + " attempting to substitute app name '" + subName
3428 + "' without holding perm "
3429 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3430 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003431 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003432 if (TextUtils.isEmpty(name)) {
3433 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3434 }
3435 if (TextUtils.isEmpty(name)) {
3436 // still nothing?
3437 return null;
3438 }
3439
3440 return String.valueOf(name);
3441 }
3442 private void bindHeaderAppName(RemoteViews contentView) {
3443 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003444 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003445 }
3446
3447 private void bindSmallIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003448 if (mN.mSmallIcon == null && mN.icon != 0) {
3449 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3450 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003451 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3452 processSmallIconColor(mN.mSmallIcon, contentView);
3453 }
3454
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003455 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003456 * @return true if the built notification will show the time or the chronometer; false
3457 * otherwise
3458 */
3459 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003460 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003461 }
3462
Christoph Studerfe718432014-09-01 18:21:18 +02003463 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003464 // actions_container is only reset when there are no actions to avoid focus issues with
3465 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003466 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003467 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003468
3469 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3470 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3471
3472 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3473 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3474 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3475 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003476
3477 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003478 }
3479
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003480 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003481 final Bundle ex = mN.extras;
3482
3483 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3484 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3485 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3486 }
3487
3488 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3489 CharSequence title, CharSequence text) {
3490 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003491
Christoph Studerfe718432014-09-01 18:21:18 +02003492 resetStandardTemplateWithActions(big);
3493
Adrian Roose458aa82015-12-08 16:17:19 -08003494 boolean validRemoteInput = false;
3495
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003496 int N = mActions.size();
3497 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003498 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003499 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roosf852a422016-06-03 13:33:43 -07003500 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3501 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003502 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003503 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003504 Action action = mActions.get(i);
3505 validRemoteInput |= hasValidRemoteInput(action);
3506
3507 final RemoteViews button = generateActionButton(action);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003508 big.addView(R.id.actions, button);
3509 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003510 } else {
3511 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003512 }
Adrian Roose458aa82015-12-08 16:17:19 -08003513
3514 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3515 if (validRemoteInput && replyText != null
3516 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3517 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3518 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3519
3520 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3521 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3522 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3523
3524 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3525 big.setViewVisibility(
3526 R.id.notification_material_reply_text_3, View.VISIBLE);
3527 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3528 }
3529 }
3530 }
3531
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003532 return big;
3533 }
3534
Adrian Roose458aa82015-12-08 16:17:19 -08003535 private boolean hasValidRemoteInput(Action action) {
3536 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3537 // Weird actions
3538 return false;
3539 }
3540
3541 RemoteInput[] remoteInputs = action.getRemoteInputs();
3542 if (remoteInputs == null) {
3543 return false;
3544 }
3545
3546 for (RemoteInput r : remoteInputs) {
3547 CharSequence[] choices = r.getChoices();
3548 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3549 return true;
3550 }
3551 }
3552 return false;
3553 }
3554
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003555 /**
3556 * Construct a RemoteViews for the final 1U notification layout. In order:
3557 * 1. Custom contentView from the caller
3558 * 2. Style's proposed content view
3559 * 3. Standard template view
3560 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003561 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003562 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003563 return mN.contentView;
3564 } else if (mStyle != null) {
3565 final RemoteViews styleView = mStyle.makeContentView();
3566 if (styleView != null) {
3567 return styleView;
3568 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003569 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003570 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003571 }
3572
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003573 /**
3574 * Construct a RemoteViews for the final big notification layout.
3575 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003576 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003577 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003578 if (mN.bigContentView != null
3579 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003580 return mN.bigContentView;
3581 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003582 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003583 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003584 } else if (mActions.size() != 0) {
3585 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003586 }
3587 adaptNotificationHeaderForBigContentView(result);
3588 return result;
3589 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003590
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003591 /**
3592 * Construct a RemoteViews for the final notification header only
3593 *
3594 * @hide
3595 */
3596 public RemoteViews makeNotificationHeader() {
3597 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3598 R.layout.notification_template_header);
3599 resetNotificationHeader(header);
3600 bindNotificationHeader(header);
3601 return header;
3602 }
3603
Selim Cinek29603462015-11-17 19:04:39 -08003604 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003605 if (result != null) {
3606 result.setViewVisibility(R.id.text_line_1, View.GONE);
3607 }
Selim Cinek29603462015-11-17 19:04:39 -08003608 }
3609
Selim Cinek850a8542015-11-11 11:48:36 -05003610 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003611 if (result != null) {
3612 result.setBoolean(R.id.notification_header, "setExpanded", true);
3613 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003614 }
3615
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003616 /**
3617 * Construct a RemoteViews for the final heads-up notification layout.
3618 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003619 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003620 if (mN.headsUpContentView != null
3621 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003622 return mN.headsUpContentView;
3623 } else if (mStyle != null) {
3624 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3625 if (styleView != null) {
3626 return styleView;
3627 }
3628 } else if (mActions.size() == 0) {
3629 return null;
3630 }
3631
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003632 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003633 }
3634
Selim Cinek624c02db2015-12-14 21:00:02 -08003635 /**
3636 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3637 *
3638 * @hide
3639 */
3640 public RemoteViews makePublicContentView() {
3641 if (mN.publicVersion != null) {
3642 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003643 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003644 }
3645 Bundle savedBundle = mN.extras;
3646 Style style = mStyle;
3647 mStyle = null;
3648 Icon largeIcon = mN.mLargeIcon;
3649 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003650 Bitmap largeIconLegacy = mN.largeIcon;
3651 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003652 Bundle publicExtras = new Bundle();
3653 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3654 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3655 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3656 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003657 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3658 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003659 publicExtras.putCharSequence(EXTRA_TITLE,
3660 mContext.getString(R.string.notification_hidden_text));
3661 mN.extras = publicExtras;
3662 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3663 mN.extras = savedBundle;
3664 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003665 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003666 mStyle = style;
3667 return publicView;
3668 }
3669
3670
Chris Wren8fd39ec2014-02-27 17:43:26 -05003671
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003672 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003673 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003674 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003675 tombstone ? getActionTombstoneLayoutResource()
3676 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003677 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003678 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003679 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003680 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003681 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003682 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003683 if (action.mRemoteInputs != null) {
3684 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3685 }
3686 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003687 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003688 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003689 return button;
3690 }
3691
Joe Onoratocb109a02011-01-18 17:57:41 -08003692 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003693 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003694 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003695 */
3696 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003697 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003698 }
3699
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003700 private CharSequence processLegacyText(CharSequence charSequence) {
3701 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003702 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003703 } else {
3704 return charSequence;
3705 }
3706 }
3707
Dan Sandler26e81cf2014-05-06 10:01:27 -04003708 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003709 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003710 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003711 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003712 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3713 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003714 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003715 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003716
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003717 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003718 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003719 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003720 }
3721
Dan Sandler26e81cf2014-05-06 10:01:27 -04003722 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003723 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003724 * if it's grayscale).
3725 */
3726 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003727 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3728 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003729 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003730 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003731 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003732 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003733 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003734 }
3735
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003736 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003737 if (mN.color != COLOR_DEFAULT) {
3738 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003739 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003740 }
3741
Adrian Roos4ff3b122016-02-01 12:26:13 -08003742 int resolveContrastColor() {
3743 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3744 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003745 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003746 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3747
3748 mCachedContrastColorIsFor = mN.color;
3749 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003750 }
3751
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003752 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003753 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003754 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003755 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003756 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003757 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003758 mN.actions = new Action[mActions.size()];
3759 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003760 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003761 if (!mPersonList.isEmpty()) {
3762 mN.extras.putStringArray(EXTRA_PEOPLE,
3763 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003764 }
Selim Cinek247fa012016-02-18 09:50:48 -08003765 if (mN.bigContentView != null || mN.contentView != null
3766 || mN.headsUpContentView != null) {
3767 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3768 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003769 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003770 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003771
Julia Reynolds3b848122016-02-26 10:45:32 -05003772 /**
3773 * Creates a Builder from an existing notification so further changes can be made.
3774 * @param context The context for your application / activity.
3775 * @param n The notification to create a Builder from.
3776 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003777 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003778 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003779 ApplicationInfo applicationInfo = n.extras.getParcelable(
3780 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003781 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003782 if (applicationInfo != null) {
3783 try {
3784 builderContext = context.createApplicationContext(applicationInfo,
3785 Context.CONTEXT_RESTRICTED);
3786 } catch (NameNotFoundException e) {
3787 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3788 builderContext = context; // try with our context
3789 }
3790 } else {
3791 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003792 }
3793
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003794 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003795 }
3796
3797 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003798 Class<? extends Style>[] classes = new Class[] {
3799 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003800 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3801 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003802 for (Class<? extends Style> innerClass : classes) {
3803 if (templateClass.equals(innerClass.getName())) {
3804 return innerClass;
3805 }
3806 }
3807 return null;
3808 }
3809
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003810 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003811 * @deprecated Use {@link #build()} instead.
3812 */
3813 @Deprecated
3814 public Notification getNotification() {
3815 return build();
3816 }
3817
3818 /**
3819 * Combine all of the options that have been set and return a new {@link Notification}
3820 * object.
3821 */
3822 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003823 // first, add any extras from the calling code
3824 if (mUserExtras != null) {
3825 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003826 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003827
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003828 mN.creationTime = System.currentTimeMillis();
3829
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003830 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003831 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003832
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003833 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003834
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003835 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003836 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003837 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003838
Adrian Roos5081c0d2016-02-26 16:04:19 -08003839 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3840 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003841 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003842 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003843 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3844 mN.contentView.getSequenceNumber());
3845 }
3846 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003847 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003848 if (mN.bigContentView != null) {
3849 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3850 mN.bigContentView.getSequenceNumber());
3851 }
3852 }
3853 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003854 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003855 if (mN.headsUpContentView != null) {
3856 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3857 mN.headsUpContentView.getSequenceNumber());
3858 }
3859 }
3860 }
3861
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003862 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3863 mN.flags |= FLAG_SHOW_LIGHTS;
3864 }
3865
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003866 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003867 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003868
3869 /**
3870 * Apply this Builder to an existing {@link Notification} object.
3871 *
3872 * @hide
3873 */
3874 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003875 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003876 return n;
3877 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003878
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003879 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003880 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3881 * change.
3882 *
3883 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3884 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003885 * @hide
3886 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003887 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003888 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003889
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003890 // Only strip views for known Styles because we won't know how to
3891 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003892 if (!TextUtils.isEmpty(templateClass)
3893 && getNotificationStyleClass(templateClass) == null) {
3894 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003895 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003896
3897 // Only strip unmodified BuilderRemoteViews.
3898 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003899 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003900 n.contentView.getSequenceNumber();
3901 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003902 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003903 n.bigContentView.getSequenceNumber();
3904 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003905 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003906 n.headsUpContentView.getSequenceNumber();
3907
3908 // Nothing to do here, no need to clone.
3909 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3910 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003911 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003912
3913 Notification clone = n.clone();
3914 if (stripContentView) {
3915 clone.contentView = null;
3916 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3917 }
3918 if (stripBigContentView) {
3919 clone.bigContentView = null;
3920 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3921 }
3922 if (stripHeadsUpContentView) {
3923 clone.headsUpContentView = null;
3924 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3925 }
3926 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003927 }
3928
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003929 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003930 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003931 }
3932
3933 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003934 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003935 }
3936
3937 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003938 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003939 }
3940
3941 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003942 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003943 }
3944
3945 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003946 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003947 }
3948
Adrian Roosc1a80b02016-04-05 14:54:55 -07003949 private int getMessagingLayoutResource() {
3950 return R.layout.notification_template_material_messaging;
3951 }
3952
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003953 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003954 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003955 }
3956
3957 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003958 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003959 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003960 }
3961
Selim Cinek279fa862016-06-14 10:57:25 -07003962 private boolean hasLargeIcon() {
3963 return mLargeIcon != null || largeIcon != null;
3964 }
3965
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003966 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07003967 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003968 * @hide
3969 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07003970 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003971 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
3972 }
3973
3974 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07003975 * @return true if the notification will show a chronometer; false otherwise
3976 * @hide
3977 */
3978 public boolean showsChronometer() {
3979 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3980 }
3981
3982 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003983 * An object that can apply a rich notification style to a {@link Notification.Builder}
3984 * object.
3985 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003986 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003987 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003988
3989 /**
3990 * @hide
3991 */
3992 protected CharSequence mSummaryText = null;
3993
3994 /**
3995 * @hide
3996 */
3997 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003998
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003999 protected Builder mBuilder;
4000
Chris Wrend6297db2012-05-03 16:20:13 -04004001 /**
4002 * Overrides ContentTitle in the big form of the template.
4003 * This defaults to the value passed to setContentTitle().
4004 */
4005 protected void internalSetBigContentTitle(CharSequence title) {
4006 mBigContentTitle = title;
4007 }
4008
4009 /**
4010 * Set the first line of text after the detail section in the big form of the template.
4011 */
4012 protected void internalSetSummaryText(CharSequence cs) {
4013 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004014 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004015 }
4016
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004017 public void setBuilder(Builder builder) {
4018 if (mBuilder != builder) {
4019 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004020 if (mBuilder != null) {
4021 mBuilder.setStyle(this);
4022 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004023 }
4024 }
4025
Chris Wrend6297db2012-05-03 16:20:13 -04004026 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004027 if (mBuilder == null) {
4028 throw new IllegalArgumentException("Style requires a valid Builder object");
4029 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004030 }
Chris Wrend6297db2012-05-03 16:20:13 -04004031
4032 protected RemoteViews getStandardView(int layoutId) {
4033 checkBuilder();
4034
Christoph Studer4600f9b2014-07-22 22:44:43 +02004035 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004036 CharSequence oldBuilderContentTitle =
4037 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004038 if (mBigContentTitle != null) {
4039 mBuilder.setContentTitle(mBigContentTitle);
4040 }
4041
Chris Wrend6297db2012-05-03 16:20:13 -04004042 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4043
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004044 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004045
Chris Wrend6297db2012-05-03 16:20:13 -04004046 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4047 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004048 } else {
4049 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004050 }
4051
Chris Wrend6297db2012-05-03 16:20:13 -04004052 return contentView;
4053 }
4054
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004055 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004056 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4057 * The default implementation has nothing additional to add.
4058 * @hide
4059 */
4060 public RemoteViews makeContentView() {
4061 return null;
4062 }
4063
4064 /**
4065 * Construct a Style-specific RemoteViews for the final big notification layout.
4066 * @hide
4067 */
4068 public RemoteViews makeBigContentView() {
4069 return null;
4070 }
4071
4072 /**
4073 * Construct a Style-specific RemoteViews for the final HUN layout.
4074 * @hide
4075 */
4076 public RemoteViews makeHeadsUpContentView() {
4077 return null;
4078 }
4079
4080 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004081 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004082 * @hide
4083 */
4084 public void addExtras(Bundle extras) {
4085 if (mSummaryTextSet) {
4086 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4087 }
4088 if (mBigContentTitle != null) {
4089 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4090 }
Chris Wren91ad5632013-06-05 15:05:57 -04004091 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004092 }
4093
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004094 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004095 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004096 * @hide
4097 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004098 protected void restoreFromExtras(Bundle extras) {
4099 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4100 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4101 mSummaryTextSet = true;
4102 }
4103 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4104 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4105 }
4106 }
4107
4108
4109 /**
4110 * @hide
4111 */
4112 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004113 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004114 return wip;
4115 }
4116
Daniel Sandler0ec46202015-06-24 01:27:05 -04004117 /**
4118 * @hide
4119 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004120 public void purgeResources() {}
4121
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004122 /**
4123 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4124 * attached to.
4125 *
4126 * @return the fully constructed Notification.
4127 */
4128 public Notification build() {
4129 checkBuilder();
4130 return mBuilder.build();
4131 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004132
4133 /**
4134 * @hide
4135 * @return true if the style positions the progress bar on the second line; false if the
4136 * style hides the progress bar
4137 */
4138 protected boolean hasProgress() {
4139 return true;
4140 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004141
4142 /**
4143 * @hide
4144 * @return Whether we should put the summary be put into the notification header
4145 */
4146 public boolean hasSummaryInHeader() {
4147 return true;
4148 }
Selim Cinek593610c2016-02-16 18:42:57 -08004149
4150 /**
4151 * @hide
4152 * @return Whether custom content views are displayed inline in the style
4153 */
4154 public boolean displayCustomViewInline() {
4155 return false;
4156 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004157 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004158
4159 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004160 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004161 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004162 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004163 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004164 * Notification notif = new Notification.Builder(mContext)
4165 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4166 * .setContentText(subject)
4167 * .setSmallIcon(R.drawable.new_post)
4168 * .setLargeIcon(aBitmap)
4169 * .setStyle(new Notification.BigPictureStyle()
4170 * .bigPicture(aBigBitmap))
4171 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004172 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004173 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004174 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004175 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004176 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004177 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004178 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004179 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004180
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004181 public BigPictureStyle() {
4182 }
4183
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004184 /**
4185 * @deprecated use {@code BigPictureStyle()}.
4186 */
4187 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004188 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004189 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004190 }
4191
Chris Wrend6297db2012-05-03 16:20:13 -04004192 /**
4193 * Overrides ContentTitle in the big form of the template.
4194 * This defaults to the value passed to setContentTitle().
4195 */
4196 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004197 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004198 return this;
4199 }
4200
4201 /**
4202 * Set the first line of text after the detail section in the big form of the template.
4203 */
4204 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004205 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004206 return this;
4207 }
4208
Chris Wren0bd664d2012-08-01 13:56:56 -04004209 /**
4210 * Provide the bitmap to be used as the payload for the BigPicture notification.
4211 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004212 public BigPictureStyle bigPicture(Bitmap b) {
4213 mPicture = b;
4214 return this;
4215 }
4216
Chris Wren3745a3d2012-05-22 15:11:52 -04004217 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004218 * Override the large icon when the big notification is shown.
4219 */
4220 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004221 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4222 }
4223
4224 /**
4225 * Override the large icon when the big notification is shown.
4226 */
4227 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004228 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004229 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004230 return this;
4231 }
4232
Riley Andrews0394a0c2015-11-03 23:36:52 -08004233 /** @hide */
4234 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4235
Daniel Sandler0ec46202015-06-24 01:27:05 -04004236 /**
4237 * @hide
4238 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004239 @Override
4240 public void purgeResources() {
4241 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004242 if (mPicture != null &&
4243 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004244 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004245 mPicture = mPicture.createAshmemBitmap();
4246 }
4247 if (mBigLargeIcon != null) {
4248 mBigLargeIcon.convertToAshmem();
4249 }
4250 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004251
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004252 /**
4253 * @hide
4254 */
4255 public RemoteViews makeBigContentView() {
4256 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004257 // This covers the following cases:
4258 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004259 // mN.mLargeIcon
4260 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004261 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004262 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004263 oldLargeIcon = mBuilder.mN.mLargeIcon;
4264 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004265 }
4266
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004267 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004268 if (mSummaryTextSet) {
4269 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004270 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004271 }
Selim Cinek279fa862016-06-14 10:57:25 -07004272 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004273
Christoph Studer5c510ee2014-12-15 16:32:27 +01004274 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004275 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004276 }
4277
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004278 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004279 return contentView;
4280 }
4281
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004282 /**
4283 * @hide
4284 */
4285 public void addExtras(Bundle extras) {
4286 super.addExtras(extras);
4287
4288 if (mBigLargeIconSet) {
4289 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4290 }
4291 extras.putParcelable(EXTRA_PICTURE, mPicture);
4292 }
4293
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004294 /**
4295 * @hide
4296 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004297 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004298 protected void restoreFromExtras(Bundle extras) {
4299 super.restoreFromExtras(extras);
4300
4301 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004302 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004303 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004304 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004305 mPicture = extras.getParcelable(EXTRA_PICTURE);
4306 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004307
4308 /**
4309 * @hide
4310 */
4311 @Override
4312 public boolean hasSummaryInHeader() {
4313 return false;
4314 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004315 }
4316
4317 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004318 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004319 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004320 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004321 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004322 * Notification notif = new Notification.Builder(mContext)
4323 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4324 * .setContentText(subject)
4325 * .setSmallIcon(R.drawable.new_mail)
4326 * .setLargeIcon(aBitmap)
4327 * .setStyle(new Notification.BigTextStyle()
4328 * .bigText(aVeryLongString))
4329 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004330 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004331 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004332 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004333 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004334 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004335
4336 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004337 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004338
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004339 private CharSequence mBigText;
4340
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004341 public BigTextStyle() {
4342 }
4343
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004344 /**
4345 * @deprecated use {@code BigTextStyle()}.
4346 */
4347 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004348 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004349 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004350 }
4351
Chris Wrend6297db2012-05-03 16:20:13 -04004352 /**
4353 * Overrides ContentTitle in the big form of the template.
4354 * This defaults to the value passed to setContentTitle().
4355 */
4356 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004357 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004358 return this;
4359 }
4360
4361 /**
4362 * Set the first line of text after the detail section in the big form of the template.
4363 */
4364 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004365 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004366 return this;
4367 }
4368
Chris Wren0bd664d2012-08-01 13:56:56 -04004369 /**
4370 * Provide the longer text to be displayed in the big form of the
4371 * template in place of the content text.
4372 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004373 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004374 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004375 return this;
4376 }
4377
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004378 /**
4379 * @hide
4380 */
4381 public void addExtras(Bundle extras) {
4382 super.addExtras(extras);
4383
Christoph Studer4600f9b2014-07-22 22:44:43 +02004384 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4385 }
4386
4387 /**
4388 * @hide
4389 */
4390 @Override
4391 protected void restoreFromExtras(Bundle extras) {
4392 super.restoreFromExtras(extras);
4393
4394 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004395 }
4396
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004397 /**
4398 * @hide
4399 */
4400 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004401
4402 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004403 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004404 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004405
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004406 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004407
Selim Cinek75998782016-04-26 10:39:17 -07004408 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004409
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004410 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004411 if (TextUtils.isEmpty(bigTextText)) {
4412 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4413 // experience
4414 bigTextText = mBuilder.processLegacyText(text);
4415 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004416 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004417
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004418 return contentView;
4419 }
4420
Adrian Roosb1f427c2016-05-26 12:27:15 -07004421 static void applyBigTextContentView(Builder builder,
4422 RemoteViews contentView, CharSequence bigTextText) {
4423 contentView.setTextViewText(R.id.big_text, bigTextText);
4424 contentView.setViewVisibility(R.id.big_text,
4425 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4426 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004427 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004428 }
4429
4430 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004431 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004432 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004433 if (hasActions) {
4434 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4435 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004436 return lineCount;
4437 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004438 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004439
4440 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004441 * Helper class for generating large-format notifications that include multiple back-and-forth
4442 * messages of varying types between any number of people.
4443 *
4444 * <br>
4445 * If the platform does not provide large-format notifications, this method has no effect. The
4446 * user will always see the normal notification view.
4447 * <br>
4448 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4449 * so:
4450 * <pre class="prettyprint">
4451 *
4452 * Notification noti = new Notification.Builder()
4453 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4454 * .setContentText(subject)
4455 * .setSmallIcon(R.drawable.new_message)
4456 * .setLargeIcon(aBitmap)
4457 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4458 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4459 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4460 * .build();
4461 * </pre>
4462 */
4463 public static class MessagingStyle extends Style {
4464
4465 /**
4466 * The maximum number of messages that will be retained in the Notification itself (the
4467 * number displayed is up to the platform).
4468 */
4469 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4470
4471 CharSequence mUserDisplayName;
4472 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004473 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004474
4475 MessagingStyle() {
4476 }
4477
4478 /**
4479 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4480 * posting app reposts the notification with those messages after they've been actually
4481 * sent and in previous messages sent by the user added in
4482 * {@link #addMessage(Notification.MessagingStyle.Message)}
4483 */
4484 public MessagingStyle(CharSequence userDisplayName) {
4485 mUserDisplayName = userDisplayName;
4486 }
4487
4488 /**
4489 * Returns the name to be displayed for any replies sent by the user
4490 */
4491 public CharSequence getUserDisplayName() {
4492 return mUserDisplayName;
4493 }
4494
4495 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004496 * Sets the title to be displayed on this conversation. This should only be used for
4497 * group messaging and left unset for one-on-one conversations.
4498 * @param conversationTitle
4499 * @return this object for method chaining.
4500 */
4501 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4502 mConversationTitle = conversationTitle;
4503 return this;
4504 }
4505
4506 /**
4507 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4508 * should be for one-on-one conversations
4509 */
4510 public CharSequence getConversationTitle() {
4511 return mConversationTitle;
4512 }
4513
4514 /**
4515 * Adds a message for display by this notification. Convenience call for a simple
4516 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4517 * @param text A {@link CharSequence} to be displayed as the message content
4518 * @param timestamp Time at which the message arrived
4519 * @param sender A {@link CharSequence} to be used for displaying the name of the
4520 * sender. Should be <code>null</code> for messages by the current user, in which case
4521 * the platform will insert {@link #getUserDisplayName()}.
4522 * Should be unique amongst all individuals in the conversation, and should be
4523 * consistent during re-posts of the notification.
4524 *
4525 * @see Message#Message(CharSequence, long, CharSequence)
4526 *
4527 * @return this object for method chaining
4528 */
4529 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4530 mMessages.add(new Message(text, timestamp, sender));
4531 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4532 mMessages.remove(0);
4533 }
4534 return this;
4535 }
4536
4537 /**
4538 * Adds a {@link Message} for display in this notification.
4539 * @param message The {@link Message} to be displayed
4540 * @return this object for method chaining
4541 */
4542 public MessagingStyle addMessage(Message message) {
4543 mMessages.add(message);
4544 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4545 mMessages.remove(0);
4546 }
4547 return this;
4548 }
4549
4550 /**
4551 * Gets the list of {@code Message} objects that represent the notification
4552 */
4553 public List<Message> getMessages() {
4554 return mMessages;
4555 }
4556
4557 /**
4558 * @hide
4559 */
4560 @Override
4561 public void addExtras(Bundle extras) {
4562 super.addExtras(extras);
4563 if (mUserDisplayName != null) {
4564 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4565 }
4566 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004567 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004568 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004569 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4570 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004571 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004572
4573 fixTitleAndTextExtras(extras);
4574 }
4575
4576 private void fixTitleAndTextExtras(Bundle extras) {
4577 Message m = findLatestIncomingMessage();
4578 CharSequence text = (m == null) ? null : m.mText;
4579 CharSequence sender = m == null ? null
4580 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4581 CharSequence title;
4582 if (!TextUtils.isEmpty(mConversationTitle)) {
4583 if (!TextUtils.isEmpty(sender)) {
4584 BidiFormatter bidi = BidiFormatter.getInstance();
4585 title = mBuilder.mContext.getString(
4586 com.android.internal.R.string.notification_messaging_title_template,
4587 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4588 } else {
4589 title = mConversationTitle;
4590 }
4591 } else {
4592 title = sender;
4593 }
4594
4595 if (title != null) {
4596 extras.putCharSequence(EXTRA_TITLE, title);
4597 }
4598 if (text != null) {
4599 extras.putCharSequence(EXTRA_TEXT, text);
4600 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004601 }
4602
4603 /**
4604 * @hide
4605 */
4606 @Override
4607 protected void restoreFromExtras(Bundle extras) {
4608 super.restoreFromExtras(extras);
4609
4610 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004611 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4612 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004613 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004614 if (parcelables != null && parcelables instanceof Parcelable[]) {
4615 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004616 }
4617 }
4618
4619 /**
4620 * @hide
4621 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004622 @Override
4623 public RemoteViews makeContentView() {
4624 Message m = findLatestIncomingMessage();
4625 CharSequence title = mConversationTitle != null
4626 ? mConversationTitle
4627 : (m == null) ? null : m.mSender;
4628 CharSequence text = (m == null)
4629 ? null
4630 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004631
Adrian Roosc1a80b02016-04-05 14:54:55 -07004632 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4633 false /* hasProgress */,
4634 title,
4635 text);
4636 }
4637
4638 private Message findLatestIncomingMessage() {
4639 for (int i = mMessages.size() - 1; i >= 0; i--) {
4640 Message m = mMessages.get(i);
4641 // Incoming messages have a non-empty sender.
4642 if (!TextUtils.isEmpty(m.mSender)) {
4643 return m;
4644 }
4645 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004646 if (!mMessages.isEmpty()) {
4647 // No incoming messages, fall back to outgoing message
4648 return mMessages.get(mMessages.size() - 1);
4649 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004650 return null;
4651 }
4652
4653 /**
4654 * @hide
4655 */
4656 @Override
4657 public RemoteViews makeBigContentView() {
4658 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4659 ? super.mBigContentTitle
4660 : mConversationTitle;
4661 boolean hasTitle = !TextUtils.isEmpty(title);
4662
Adrian Roosfeafa052016-06-01 17:09:45 -07004663 if (mMessages.size() == 1) {
4664 // Special case for a single message: Use the big text style
4665 // so the collapsed and expanded versions match nicely.
4666 CharSequence bigTitle;
4667 CharSequence text;
4668 if (hasTitle) {
4669 bigTitle = title;
4670 text = makeMessageLine(mMessages.get(0));
4671 } else {
4672 bigTitle = mMessages.get(0).mSender;
4673 text = mMessages.get(0).mText;
4674 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004675 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4676 mBuilder.getBigTextLayoutResource(),
Adrian Roosfeafa052016-06-01 17:09:45 -07004677 false /* progress */, bigTitle, null /* text */);
Adrian Roosb1f427c2016-05-26 12:27:15 -07004678 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4679 return contentView;
4680 }
4681
Adrian Roos48d746a2016-04-12 14:57:28 -07004682 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004683 mBuilder.getMessagingLayoutResource(),
4684 false /* hasProgress */,
4685 title,
4686 null /* text */);
4687
4688 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4689 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4690
4691 // Make sure all rows are gone in case we reuse a view.
4692 for (int rowId : rowIds) {
4693 contentView.setViewVisibility(rowId, View.GONE);
4694 }
4695
4696 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004697 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
4698 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004699 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07004700 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004701
Adrian Roosfeafa052016-06-01 17:09:45 -07004702 int contractedChildId = View.NO_ID;
4703 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07004704 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4705 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4706 Message m = mMessages.get(firstMessage + i);
4707 int rowId = rowIds[i];
4708
4709 contentView.setViewVisibility(rowId, View.VISIBLE);
4710 contentView.setTextViewText(rowId, makeMessageLine(m));
4711
Adrian Roosfeafa052016-06-01 17:09:45 -07004712 if (contractedMessage == m) {
4713 contractedChildId = rowId;
4714 }
4715
Adrian Roosc1a80b02016-04-05 14:54:55 -07004716 i++;
4717 }
Adrian Roosfeafa052016-06-01 17:09:45 -07004718 // Record this here to allow transformation between the contracted and expanded views.
4719 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
4720 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04004721 return contentView;
4722 }
4723
Adrian Roosc1a80b02016-04-05 14:54:55 -07004724 private CharSequence makeMessageLine(Message m) {
4725 BidiFormatter bidi = BidiFormatter.getInstance();
4726 SpannableStringBuilder sb = new SpannableStringBuilder();
4727 if (TextUtils.isEmpty(m.mSender)) {
4728 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4729 sb.append(bidi.unicodeWrap(replyName),
4730 makeFontColorSpan(mBuilder.resolveContrastColor()),
4731 0 /* flags */);
4732 } else {
4733 sb.append(bidi.unicodeWrap(m.mSender),
4734 makeFontColorSpan(Color.BLACK),
4735 0 /* flags */);
4736 }
4737 CharSequence text = m.mText == null ? "" : m.mText;
4738 sb.append(" ").append(bidi.unicodeWrap(text));
4739 return sb;
4740 }
4741
Adrian Roosdedd1df2016-04-26 16:38:47 -07004742 /**
4743 * @hide
4744 */
4745 @Override
4746 public RemoteViews makeHeadsUpContentView() {
4747 Message m = findLatestIncomingMessage();
4748 CharSequence title = mConversationTitle != null
4749 ? mConversationTitle
4750 : (m == null) ? null : m.mSender;
4751 CharSequence text = (m == null)
4752 ? null
4753 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4754
4755 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4756 false /* hasProgress */,
4757 title,
4758 text);
4759 }
4760
Adrian Roosc1a80b02016-04-05 14:54:55 -07004761 private static TextAppearanceSpan makeFontColorSpan(int color) {
4762 return new TextAppearanceSpan(null, 0, 0,
4763 ColorStateList.valueOf(color), null);
4764 }
4765
Alex Hillsd9b04d92016-04-11 16:38:16 -04004766 public static final class Message {
4767
4768 static final String KEY_TEXT = "text";
4769 static final String KEY_TIMESTAMP = "time";
4770 static final String KEY_SENDER = "sender";
4771 static final String KEY_DATA_MIME_TYPE = "type";
4772 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004773
4774 private final CharSequence mText;
4775 private final long mTimestamp;
4776 private final CharSequence mSender;
4777
4778 private String mDataMimeType;
4779 private Uri mDataUri;
4780
4781 /**
4782 * Constructor
4783 * @param text A {@link CharSequence} to be displayed as the message content
4784 * @param timestamp Time at which the message arrived
4785 * @param sender A {@link CharSequence} to be used for displaying the name of the
4786 * sender. Should be <code>null</code> for messages by the current user, in which case
4787 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4788 * Should be unique amongst all individuals in the conversation, and should be
4789 * consistent during re-posts of the notification.
4790 */
4791 public Message(CharSequence text, long timestamp, CharSequence sender){
4792 mText = text;
4793 mTimestamp = timestamp;
4794 mSender = sender;
4795 }
4796
4797 /**
4798 * Sets a binary blob of data and an associated MIME type for a message. In the case
4799 * where the platform doesn't support the MIME type, the original text provided in the
4800 * constructor will be used.
4801 * @param dataMimeType The MIME type of the content. See
4802 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4803 * types on Android and Android Wear.
4804 * @param dataUri The uri containing the content whose type is given by the MIME type.
4805 * <p class="note">
4806 * <ol>
4807 * <li>Notification Listeners including the System UI need permission to access the
4808 * data the Uri points to. The recommended ways to do this are:</li>
4809 * <li>Store the data in your own ContentProvider, making sure that other apps have
4810 * the correct permission to access your provider. The preferred mechanism for
4811 * providing access is to use per-URI permissions which are temporary and only
4812 * grant access to the receiving application. An easy way to create a
4813 * ContentProvider like this is to use the FileProvider helper class.</li>
4814 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4815 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4816 * also store non-media types (see MediaStore.Files for more info). Files can be
4817 * inserted into the MediaStore using scanFile() after which a content:// style
4818 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4819 * Note that once added to the system MediaStore the content is accessible to any
4820 * app on the device.</li>
4821 * </ol>
4822 * @return this object for method chaining
4823 */
4824 public Message setData(String dataMimeType, Uri dataUri) {
4825 mDataMimeType = dataMimeType;
4826 mDataUri = dataUri;
4827 return this;
4828 }
4829
Alex Hillsfc737de2016-03-23 17:33:02 -04004830 /**
4831 * Get the text to be used for this message, or the fallback text if a type and content
4832 * Uri have been set
4833 */
4834 public CharSequence getText() {
4835 return mText;
4836 }
4837
4838 /**
4839 * Get the time at which this message arrived
4840 */
4841 public long getTimestamp() {
4842 return mTimestamp;
4843 }
4844
4845 /**
4846 * Get the text used to display the contact's name in the messaging experience
4847 */
4848 public CharSequence getSender() {
4849 return mSender;
4850 }
4851
4852 /**
4853 * Get the MIME type of the data pointed to by the Uri
4854 */
4855 public String getDataMimeType() {
4856 return mDataMimeType;
4857 }
4858
4859 /**
4860 * Get the the Uri pointing to the content of the message. Can be null, in which case
4861 * {@see #getText()} is used.
4862 */
4863 public Uri getDataUri() {
4864 return mDataUri;
4865 }
4866
Alex Hillsd9b04d92016-04-11 16:38:16 -04004867 private Bundle toBundle() {
4868 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04004869 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004870 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04004871 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004872 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04004873 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004874 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04004875 }
4876 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004877 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04004878 }
4879 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004880 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04004881 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004882 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04004883 }
4884
Alex Hillsd9b04d92016-04-11 16:38:16 -04004885 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
4886 Bundle[] bundles = new Bundle[messages.size()];
4887 final int N = messages.size();
4888 for (int i = 0; i < N; i++) {
4889 bundles[i] = messages.get(i).toBundle();
4890 }
4891 return bundles;
4892 }
4893
Adrian Roosdedd1df2016-04-26 16:38:47 -07004894 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004895 List<Message> messages = new ArrayList<>(bundles.length);
4896 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07004897 if (bundles[i] instanceof Bundle) {
4898 Message message = getMessageFromBundle((Bundle)bundles[i]);
4899 if (message != null) {
4900 messages.add(message);
4901 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004902 }
4903 }
4904 return messages;
4905 }
4906
4907 static Message getMessageFromBundle(Bundle bundle) {
4908 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07004909 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004910 return null;
4911 } else {
4912 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
4913 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
4914 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
4915 bundle.containsKey(KEY_DATA_URI)) {
4916
4917 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
4918 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04004919 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004920 return message;
4921 }
4922 } catch (ClassCastException e) {
4923 return null;
4924 }
4925 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004926 }
4927 }
4928
4929 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004930 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004931 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004932 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004933 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004934 * Notification notif = new Notification.Builder(mContext)
4935 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4936 * .setContentText(subject)
4937 * .setSmallIcon(R.drawable.new_mail)
4938 * .setLargeIcon(aBitmap)
4939 * .setStyle(new Notification.InboxStyle()
4940 * .addLine(str1)
4941 * .addLine(str2)
4942 * .setContentTitle(&quot;&quot;)
4943 * .setSummaryText(&quot;+3 more&quot;))
4944 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004945 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004946 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004947 * @see Notification#bigContentView
4948 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004949 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004950 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4951
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004952 public InboxStyle() {
4953 }
4954
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004955 /**
4956 * @deprecated use {@code InboxStyle()}.
4957 */
4958 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04004959 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004960 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004961 }
4962
Chris Wrend6297db2012-05-03 16:20:13 -04004963 /**
4964 * Overrides ContentTitle in the big form of the template.
4965 * This defaults to the value passed to setContentTitle().
4966 */
4967 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004968 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004969 return this;
4970 }
4971
4972 /**
4973 * Set the first line of text after the detail section in the big form of the template.
4974 */
4975 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004976 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004977 return this;
4978 }
4979
Chris Wren0bd664d2012-08-01 13:56:56 -04004980 /**
4981 * Append a line to the digest section of the Inbox notification.
4982 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004983 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004984 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004985 return this;
4986 }
4987
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004988 /**
4989 * @hide
4990 */
4991 public void addExtras(Bundle extras) {
4992 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004993
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004994 CharSequence[] a = new CharSequence[mTexts.size()];
4995 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4996 }
4997
Christoph Studer4600f9b2014-07-22 22:44:43 +02004998 /**
4999 * @hide
5000 */
5001 @Override
5002 protected void restoreFromExtras(Bundle extras) {
5003 super.restoreFromExtras(extras);
5004
5005 mTexts.clear();
5006 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5007 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5008 }
5009 }
5010
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005011 /**
5012 * @hide
5013 */
5014 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005015 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005016 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005017 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5018 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005019
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005020 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005021
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005022 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005023
Chris Wrend6297db2012-05-03 16:20:13 -04005024 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 -04005025 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005026
Chris Wren4ed80d52012-05-17 09:30:03 -04005027 // Make sure all rows are gone in case we reuse a view.
5028 for (int rowId : rowIds) {
5029 contentView.setViewVisibility(rowId, View.GONE);
5030 }
5031
Daniel Sandler879c5e02012-04-17 16:46:51 -04005032 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005033 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5034 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005035 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005036 int onlyViewId = 0;
5037 int maxRows = rowIds.length;
5038 if (mBuilder.mActions.size() > 0) {
5039 maxRows--;
5040 }
5041 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005042 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005043 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005044 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005045 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005046 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005047 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005048 if (first) {
5049 onlyViewId = rowIds[i];
5050 } else {
5051 onlyViewId = 0;
5052 }
Selim Cinek247fa012016-02-18 09:50:48 -08005053 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005054 }
5055 i++;
5056 }
Selim Cinek07c80172016-04-21 16:40:47 -07005057 if (onlyViewId != 0) {
5058 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5059 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5060 R.dimen.notification_text_margin_top);
5061 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5062 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005063
Daniel Sandler879c5e02012-04-17 16:46:51 -04005064 return contentView;
5065 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005066
Selim Cinek247fa012016-02-18 09:50:48 -08005067 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005068 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005069 if (first) {
5070 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5071 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5072 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005073 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005074 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005075 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005076 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005077 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005078 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005079 }
Dan Sandler842dd772014-05-15 09:36:47 -04005080
5081 /**
5082 * Notification style for media playback notifications.
5083 *
5084 * In the expanded form, {@link Notification#bigContentView}, up to 5
5085 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005086 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005087 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5088 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5089 * treated as album artwork.
5090 *
5091 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5092 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005093 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005094 * in the standard view alongside the usual content.
5095 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005096 * Notifications created with MediaStyle will have their category set to
5097 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5098 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5099 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005100 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5101 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005102 * the System UI can identify this as a notification representing an active media session
5103 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5104 *
5105 * To use this style with your Notification, feed it to
5106 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5107 * <pre class="prettyprint">
5108 * Notification noti = new Notification.Builder()
5109 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005110 * .setContentTitle(&quot;Track title&quot;)
5111 * .setContentText(&quot;Artist - Album&quot;)
5112 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005113 * .setStyle(<b>new Notification.MediaStyle()</b>
5114 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005115 * .build();
5116 * </pre>
5117 *
5118 * @see Notification#bigContentView
5119 */
5120 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005121 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005122 static final int MAX_MEDIA_BUTTONS = 5;
5123
5124 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005125 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005126
5127 public MediaStyle() {
5128 }
5129
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005130 /**
5131 * @deprecated use {@code MediaStyle()}.
5132 */
5133 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005134 public MediaStyle(Builder builder) {
5135 setBuilder(builder);
5136 }
5137
5138 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005139 * Request up to 3 actions (by index in the order of addition) to be shown in the compact
Dan Sandler842dd772014-05-15 09:36:47 -04005140 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005141 *
5142 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005143 */
5144 public MediaStyle setShowActionsInCompactView(int...actions) {
5145 mActionsToShowInCompact = actions;
5146 return this;
5147 }
5148
5149 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005150 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5151 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005152 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005153 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005154 mToken = token;
5155 return this;
5156 }
5157
Christoph Studer4600f9b2014-07-22 22:44:43 +02005158 /**
5159 * @hide
5160 */
Dan Sandler842dd772014-05-15 09:36:47 -04005161 @Override
5162 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005163 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005164 if (wip.category == null) {
5165 wip.category = Notification.CATEGORY_TRANSPORT;
5166 }
Dan Sandler842dd772014-05-15 09:36:47 -04005167 return wip;
5168 }
5169
Christoph Studer4600f9b2014-07-22 22:44:43 +02005170 /**
5171 * @hide
5172 */
5173 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005174 public RemoteViews makeContentView() {
5175 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005176 }
5177
5178 /**
5179 * @hide
5180 */
5181 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005182 public RemoteViews makeBigContentView() {
5183 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005184 }
5185
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005186 /**
5187 * @hide
5188 */
5189 @Override
5190 public RemoteViews makeHeadsUpContentView() {
5191 RemoteViews expanded = makeMediaBigContentView();
5192 return expanded != null ? expanded : makeMediaContentView();
5193 }
5194
Dan Sandler842dd772014-05-15 09:36:47 -04005195 /** @hide */
5196 @Override
5197 public void addExtras(Bundle extras) {
5198 super.addExtras(extras);
5199
5200 if (mToken != null) {
5201 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5202 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005203 if (mActionsToShowInCompact != null) {
5204 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5205 }
Dan Sandler842dd772014-05-15 09:36:47 -04005206 }
5207
Christoph Studer4600f9b2014-07-22 22:44:43 +02005208 /**
5209 * @hide
5210 */
5211 @Override
5212 protected void restoreFromExtras(Bundle extras) {
5213 super.restoreFromExtras(extras);
5214
5215 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5216 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5217 }
5218 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5219 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5220 }
5221 }
5222
Selim Cinek5bf069a2015-11-10 19:14:27 -05005223 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005224 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005225 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005226 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005227 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005228 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5229 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005230 if (!tombstone) {
5231 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5232 }
5233 button.setContentDescription(R.id.action0, action.title);
5234 return button;
5235 }
5236
5237 private RemoteViews makeMediaContentView() {
5238 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005239 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005240
5241 final int numActions = mBuilder.mActions.size();
5242 final int N = mActionsToShowInCompact == null
5243 ? 0
5244 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5245 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005246 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005247 for (int i = 0; i < N; i++) {
5248 if (i >= numActions) {
5249 throw new IllegalArgumentException(String.format(
5250 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5251 i, numActions - 1));
5252 }
5253
5254 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005255 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005256 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005257 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005258 }
5259 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005260 handleImage(view);
5261 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005262 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005263 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005264 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005265 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005266 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005267 return view;
5268 }
5269
5270 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005271 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005272 // Dont add an expanded view if there is no more content to be revealed
5273 int actionsInCompact = mActionsToShowInCompact == null
5274 ? 0
5275 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005276 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005277 return null;
5278 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005279 RemoteViews big = mBuilder.applyStandardTemplate(
5280 R.layout.notification_template_material_big_media,
5281 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005282
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005283 if (actionCount > 0) {
5284 big.removeAllViews(com.android.internal.R.id.media_actions);
5285 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005286 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005287 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005288 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005289 }
5290 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005291 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005292 return big;
5293 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005294
Selim Cinek5bf069a2015-11-10 19:14:27 -05005295 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005296 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005297 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5298 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005299 }
5300 }
5301
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005302 /**
5303 * @hide
5304 */
5305 @Override
5306 protected boolean hasProgress() {
5307 return false;
5308 }
Dan Sandler842dd772014-05-15 09:36:47 -04005309 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005310
Selim Cinek593610c2016-02-16 18:42:57 -08005311 /**
5312 * Notification style for custom views that are decorated by the system
5313 *
5314 * <p>Instead of providing a notification that is completely custom, a developer can set this
5315 * style and still obtain system decorations like the notification header with the expand
5316 * affordance and actions.
5317 *
5318 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5319 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5320 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5321 * corresponding custom views to display.
5322 *
5323 * To use this style with your Notification, feed it to
5324 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5325 * <pre class="prettyprint">
5326 * Notification noti = new Notification.Builder()
5327 * .setSmallIcon(R.drawable.ic_stat_player)
5328 * .setLargeIcon(albumArtBitmap))
5329 * .setCustomContentView(contentView);
5330 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5331 * .build();
5332 * </pre>
5333 */
5334 public static class DecoratedCustomViewStyle extends Style {
5335
5336 public DecoratedCustomViewStyle() {
5337 }
5338
Selim Cinek593610c2016-02-16 18:42:57 -08005339 /**
5340 * @hide
5341 */
5342 public boolean displayCustomViewInline() {
5343 return true;
5344 }
5345
5346 /**
5347 * @hide
5348 */
5349 @Override
5350 public RemoteViews makeContentView() {
5351 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5352 }
5353
5354 /**
5355 * @hide
5356 */
5357 @Override
5358 public RemoteViews makeBigContentView() {
5359 return makeDecoratedBigContentView();
5360 }
5361
5362 /**
5363 * @hide
5364 */
5365 @Override
5366 public RemoteViews makeHeadsUpContentView() {
5367 return makeDecoratedHeadsUpContentView();
5368 }
5369
Selim Cinek593610c2016-02-16 18:42:57 -08005370 private RemoteViews makeDecoratedHeadsUpContentView() {
5371 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5372 ? mBuilder.mN.contentView
5373 : mBuilder.mN.headsUpContentView;
5374 if (mBuilder.mActions.size() == 0) {
5375 return makeStandardTemplateWithCustomContent(headsUpContentView);
5376 }
5377 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5378 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005379 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005380 return remoteViews;
5381 }
5382
Selim Cinek593610c2016-02-16 18:42:57 -08005383 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5384 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5385 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005386 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005387 return remoteViews;
5388 }
5389
Selim Cinek593610c2016-02-16 18:42:57 -08005390 private RemoteViews makeDecoratedBigContentView() {
5391 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5392 ? mBuilder.mN.contentView
5393 : mBuilder.mN.bigContentView;
5394 if (mBuilder.mActions.size() == 0) {
5395 return makeStandardTemplateWithCustomContent(bigContentView);
5396 }
5397 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5398 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005399 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005400 return remoteViews;
5401 }
Selim Cinek247fa012016-02-18 09:50:48 -08005402
5403 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5404 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005405 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005406 // Need to clone customContent before adding, because otherwise it can no longer be
5407 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005408 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005409 remoteViews.removeAllViews(R.id.notification_main_column);
5410 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005411 }
Selim Cinek247fa012016-02-18 09:50:48 -08005412 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005413 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005414 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005415 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005416 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005417 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005418 }
Selim Cinek593610c2016-02-16 18:42:57 -08005419 }
5420
Selim Cinek03eb3b72016-02-18 10:39:45 -08005421 /**
5422 * Notification style for media custom views that are decorated by the system
5423 *
5424 * <p>Instead of providing a media notification that is completely custom, a developer can set
5425 * this style and still obtain system decorations like the notification header with the expand
5426 * affordance and actions.
5427 *
5428 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5429 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5430 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5431 * corresponding custom views to display.
5432 *
5433 * To use this style with your Notification, feed it to
5434 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5435 * <pre class="prettyprint">
5436 * Notification noti = new Notification.Builder()
5437 * .setSmallIcon(R.drawable.ic_stat_player)
5438 * .setLargeIcon(albumArtBitmap))
5439 * .setCustomContentView(contentView);
5440 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5441 * .setMediaSession(mySession))
5442 * .build();
5443 * </pre>
5444 *
5445 * @see android.app.Notification.DecoratedCustomViewStyle
5446 * @see android.app.Notification.MediaStyle
5447 */
5448 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5449
5450 public DecoratedMediaCustomViewStyle() {
5451 }
5452
Selim Cinek03eb3b72016-02-18 10:39:45 -08005453 /**
5454 * @hide
5455 */
5456 public boolean displayCustomViewInline() {
5457 return true;
5458 }
5459
5460 /**
5461 * @hide
5462 */
5463 @Override
5464 public RemoteViews makeContentView() {
5465 RemoteViews remoteViews = super.makeContentView();
5466 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5467 mBuilder.mN.contentView);
5468 }
5469
5470 /**
5471 * @hide
5472 */
5473 @Override
5474 public RemoteViews makeBigContentView() {
5475 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5476 ? mBuilder.mN.bigContentView
5477 : mBuilder.mN.contentView;
5478 return makeBigContentViewWithCustomContent(customRemoteView);
5479 }
5480
5481 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5482 RemoteViews remoteViews = super.makeBigContentView();
5483 if (remoteViews != null) {
5484 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5485 customRemoteView);
5486 } else if (customRemoteView != mBuilder.mN.contentView){
5487 remoteViews = super.makeContentView();
5488 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5489 customRemoteView);
5490 } else {
5491 return null;
5492 }
5493 }
5494
5495 /**
5496 * @hide
5497 */
5498 @Override
5499 public RemoteViews makeHeadsUpContentView() {
5500 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5501 ? mBuilder.mN.headsUpContentView
5502 : mBuilder.mN.contentView;
5503 return makeBigContentViewWithCustomContent(customRemoteView);
5504 }
5505
5506 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5507 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005508 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005509 // Need to clone customContent before adding, because otherwise it can no longer be
5510 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005511 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005512 remoteViews.removeAllViews(id);
5513 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005514 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005515 return remoteViews;
5516 }
5517 }
5518
Christoph Studer4600f9b2014-07-22 22:44:43 +02005519 // When adding a new Style subclass here, don't forget to update
5520 // Builder.getNotificationStyleClass.
5521
Griff Hazen61a9e862014-05-22 16:05:19 -07005522 /**
5523 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5524 * metadata or change options on a notification builder.
5525 */
5526 public interface Extender {
5527 /**
5528 * Apply this extender to a notification builder.
5529 * @param builder the builder to be modified.
5530 * @return the build object for chaining.
5531 */
5532 public Builder extend(Builder builder);
5533 }
5534
5535 /**
5536 * Helper class to add wearable extensions to notifications.
5537 * <p class="note"> See
5538 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5539 * for Android Wear</a> for more information on how to use this class.
5540 * <p>
5541 * To create a notification with wearable extensions:
5542 * <ol>
5543 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5544 * properties.
5545 * <li>Create a {@link android.app.Notification.WearableExtender}.
5546 * <li>Set wearable-specific properties using the
5547 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5548 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5549 * notification.
5550 * <li>Post the notification to the notification system with the
5551 * {@code NotificationManager.notify(...)} methods.
5552 * </ol>
5553 *
5554 * <pre class="prettyprint">
5555 * Notification notif = new Notification.Builder(mContext)
5556 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5557 * .setContentText(subject)
5558 * .setSmallIcon(R.drawable.new_mail)
5559 * .extend(new Notification.WearableExtender()
5560 * .setContentIcon(R.drawable.new_mail))
5561 * .build();
5562 * NotificationManager notificationManger =
5563 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5564 * notificationManger.notify(0, notif);</pre>
5565 *
5566 * <p>Wearable extensions can be accessed on an existing notification by using the
5567 * {@code WearableExtender(Notification)} constructor,
5568 * and then using the {@code get} methods to access values.
5569 *
5570 * <pre class="prettyprint">
5571 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5572 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005573 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005574 */
5575 public static final class WearableExtender implements Extender {
5576 /**
5577 * Sentinel value for an action index that is unset.
5578 */
5579 public static final int UNSET_ACTION_INDEX = -1;
5580
5581 /**
5582 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5583 * default sizing.
5584 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005585 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005586 * on their content.
5587 */
5588 public static final int SIZE_DEFAULT = 0;
5589
5590 /**
5591 * Size value for use with {@link #setCustomSizePreset} to show this notification
5592 * with an extra small size.
5593 * <p>This value is only applicable for custom display notifications created using
5594 * {@link #setDisplayIntent}.
5595 */
5596 public static final int SIZE_XSMALL = 1;
5597
5598 /**
5599 * Size value for use with {@link #setCustomSizePreset} to show this notification
5600 * with a small size.
5601 * <p>This value is only applicable for custom display notifications created using
5602 * {@link #setDisplayIntent}.
5603 */
5604 public static final int SIZE_SMALL = 2;
5605
5606 /**
5607 * Size value for use with {@link #setCustomSizePreset} to show this notification
5608 * with a medium size.
5609 * <p>This value is only applicable for custom display notifications created using
5610 * {@link #setDisplayIntent}.
5611 */
5612 public static final int SIZE_MEDIUM = 3;
5613
5614 /**
5615 * Size value for use with {@link #setCustomSizePreset} to show this notification
5616 * with a large size.
5617 * <p>This value is only applicable for custom display notifications created using
5618 * {@link #setDisplayIntent}.
5619 */
5620 public static final int SIZE_LARGE = 4;
5621
Griff Hazend5f11f92014-05-27 15:40:09 -07005622 /**
5623 * Size value for use with {@link #setCustomSizePreset} to show this notification
5624 * full screen.
5625 * <p>This value is only applicable for custom display notifications created using
5626 * {@link #setDisplayIntent}.
5627 */
5628 public static final int SIZE_FULL_SCREEN = 5;
5629
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005630 /**
5631 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5632 * short amount of time when this notification is displayed on the screen. This
5633 * is the default value.
5634 */
5635 public static final int SCREEN_TIMEOUT_SHORT = 0;
5636
5637 /**
5638 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5639 * for a longer amount of time when this notification is displayed on the screen.
5640 */
5641 public static final int SCREEN_TIMEOUT_LONG = -1;
5642
Griff Hazen61a9e862014-05-22 16:05:19 -07005643 /** Notification extra which contains wearable extensions */
5644 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5645
Pete Gastaf6781d2014-10-07 15:17:05 -04005646 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005647 private static final String KEY_ACTIONS = "actions";
5648 private static final String KEY_FLAGS = "flags";
5649 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5650 private static final String KEY_PAGES = "pages";
5651 private static final String KEY_BACKGROUND = "background";
5652 private static final String KEY_CONTENT_ICON = "contentIcon";
5653 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5654 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5655 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5656 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5657 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005658 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005659 private static final String KEY_DISMISSAL_ID = "dismissalId";
Griff Hazen61a9e862014-05-22 16:05:19 -07005660
5661 // Flags bitwise-ored to mFlags
5662 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5663 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5664 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5665 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005666 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005667 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005668 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005669
5670 // Default value for flags integer
5671 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5672
5673 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5674 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5675
5676 private ArrayList<Action> mActions = new ArrayList<Action>();
5677 private int mFlags = DEFAULT_FLAGS;
5678 private PendingIntent mDisplayIntent;
5679 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5680 private Bitmap mBackground;
5681 private int mContentIcon;
5682 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5683 private int mContentActionIndex = UNSET_ACTION_INDEX;
5684 private int mCustomSizePreset = SIZE_DEFAULT;
5685 private int mCustomContentHeight;
5686 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005687 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005688 private String mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005689
5690 /**
5691 * Create a {@link android.app.Notification.WearableExtender} with default
5692 * options.
5693 */
5694 public WearableExtender() {
5695 }
5696
5697 public WearableExtender(Notification notif) {
5698 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5699 if (wearableBundle != null) {
5700 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5701 if (actions != null) {
5702 mActions.addAll(actions);
5703 }
5704
5705 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5706 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5707
5708 Notification[] pages = getNotificationArrayFromBundle(
5709 wearableBundle, KEY_PAGES);
5710 if (pages != null) {
5711 Collections.addAll(mPages, pages);
5712 }
5713
5714 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5715 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5716 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5717 DEFAULT_CONTENT_ICON_GRAVITY);
5718 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5719 UNSET_ACTION_INDEX);
5720 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5721 SIZE_DEFAULT);
5722 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5723 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005724 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005725 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Griff Hazen61a9e862014-05-22 16:05:19 -07005726 }
5727 }
5728
5729 /**
5730 * Apply wearable extensions to a notification that is being built. This is typically
5731 * called by the {@link android.app.Notification.Builder#extend} method of
5732 * {@link android.app.Notification.Builder}.
5733 */
5734 @Override
5735 public Notification.Builder extend(Notification.Builder builder) {
5736 Bundle wearableBundle = new Bundle();
5737
5738 if (!mActions.isEmpty()) {
5739 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5740 }
5741 if (mFlags != DEFAULT_FLAGS) {
5742 wearableBundle.putInt(KEY_FLAGS, mFlags);
5743 }
5744 if (mDisplayIntent != null) {
5745 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5746 }
5747 if (!mPages.isEmpty()) {
5748 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5749 new Notification[mPages.size()]));
5750 }
5751 if (mBackground != null) {
5752 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5753 }
5754 if (mContentIcon != 0) {
5755 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5756 }
5757 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5758 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5759 }
5760 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5761 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5762 mContentActionIndex);
5763 }
5764 if (mCustomSizePreset != SIZE_DEFAULT) {
5765 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5766 }
5767 if (mCustomContentHeight != 0) {
5768 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5769 }
5770 if (mGravity != DEFAULT_GRAVITY) {
5771 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5772 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005773 if (mHintScreenTimeout != 0) {
5774 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5775 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005776 if (mDismissalId != null) {
5777 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5778 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005779
5780 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5781 return builder;
5782 }
5783
5784 @Override
5785 public WearableExtender clone() {
5786 WearableExtender that = new WearableExtender();
5787 that.mActions = new ArrayList<Action>(this.mActions);
5788 that.mFlags = this.mFlags;
5789 that.mDisplayIntent = this.mDisplayIntent;
5790 that.mPages = new ArrayList<Notification>(this.mPages);
5791 that.mBackground = this.mBackground;
5792 that.mContentIcon = this.mContentIcon;
5793 that.mContentIconGravity = this.mContentIconGravity;
5794 that.mContentActionIndex = this.mContentActionIndex;
5795 that.mCustomSizePreset = this.mCustomSizePreset;
5796 that.mCustomContentHeight = this.mCustomContentHeight;
5797 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005798 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005799 that.mDismissalId = this.mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005800 return that;
5801 }
5802
5803 /**
5804 * Add a wearable action to this notification.
5805 *
5806 * <p>When wearable actions are added using this method, the set of actions that
5807 * show on a wearable device splits from devices that only show actions added
5808 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5809 * of which actions display on different devices.
5810 *
5811 * @param action the action to add to this notification
5812 * @return this object for method chaining
5813 * @see android.app.Notification.Action
5814 */
5815 public WearableExtender addAction(Action action) {
5816 mActions.add(action);
5817 return this;
5818 }
5819
5820 /**
5821 * Adds wearable actions to this notification.
5822 *
5823 * <p>When wearable actions are added using this method, the set of actions that
5824 * show on a wearable device splits from devices that only show actions added
5825 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5826 * of which actions display on different devices.
5827 *
5828 * @param actions the actions to add to this notification
5829 * @return this object for method chaining
5830 * @see android.app.Notification.Action
5831 */
5832 public WearableExtender addActions(List<Action> actions) {
5833 mActions.addAll(actions);
5834 return this;
5835 }
5836
5837 /**
5838 * Clear all wearable actions present on this builder.
5839 * @return this object for method chaining.
5840 * @see #addAction
5841 */
5842 public WearableExtender clearActions() {
5843 mActions.clear();
5844 return this;
5845 }
5846
5847 /**
5848 * Get the wearable actions present on this notification.
5849 */
5850 public List<Action> getActions() {
5851 return mActions;
5852 }
5853
5854 /**
5855 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005856 * this notification. The {@link PendingIntent} provided should be for an activity.
5857 *
5858 * <pre class="prettyprint">
5859 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5860 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5861 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5862 * Notification notif = new Notification.Builder(context)
5863 * .extend(new Notification.WearableExtender()
5864 * .setDisplayIntent(displayPendingIntent)
5865 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5866 * .build();</pre>
5867 *
5868 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005869 * should have an empty task affinity. It is also recommended to use the device
5870 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005871 *
5872 * <p>Example AndroidManifest.xml entry:
5873 * <pre class="prettyprint">
5874 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5875 * android:exported=&quot;true&quot;
5876 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005877 * android:taskAffinity=&quot;&quot;
5878 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005879 *
5880 * @param intent the {@link PendingIntent} for an activity
5881 * @return this object for method chaining
5882 * @see android.app.Notification.WearableExtender#getDisplayIntent
5883 */
5884 public WearableExtender setDisplayIntent(PendingIntent intent) {
5885 mDisplayIntent = intent;
5886 return this;
5887 }
5888
5889 /**
5890 * Get the intent to launch inside of an activity view when displaying this
5891 * notification. This {@code PendingIntent} should be for an activity.
5892 */
5893 public PendingIntent getDisplayIntent() {
5894 return mDisplayIntent;
5895 }
5896
5897 /**
5898 * Add an additional page of content to display with this notification. The current
5899 * notification forms the first page, and pages added using this function form
5900 * subsequent pages. This field can be used to separate a notification into multiple
5901 * sections.
5902 *
5903 * @param page the notification to add as another page
5904 * @return this object for method chaining
5905 * @see android.app.Notification.WearableExtender#getPages
5906 */
5907 public WearableExtender addPage(Notification page) {
5908 mPages.add(page);
5909 return this;
5910 }
5911
5912 /**
5913 * Add additional pages of content to display with this notification. The current
5914 * notification forms the first page, and pages added using this function form
5915 * subsequent pages. This field can be used to separate a notification into multiple
5916 * sections.
5917 *
5918 * @param pages a list of notifications
5919 * @return this object for method chaining
5920 * @see android.app.Notification.WearableExtender#getPages
5921 */
5922 public WearableExtender addPages(List<Notification> pages) {
5923 mPages.addAll(pages);
5924 return this;
5925 }
5926
5927 /**
5928 * Clear all additional pages present on this builder.
5929 * @return this object for method chaining.
5930 * @see #addPage
5931 */
5932 public WearableExtender clearPages() {
5933 mPages.clear();
5934 return this;
5935 }
5936
5937 /**
5938 * Get the array of additional pages of content for displaying this notification. The
5939 * current notification forms the first page, and elements within this array form
5940 * subsequent pages. This field can be used to separate a notification into multiple
5941 * sections.
5942 * @return the pages for this notification
5943 */
5944 public List<Notification> getPages() {
5945 return mPages;
5946 }
5947
5948 /**
5949 * Set a background image to be displayed behind the notification content.
5950 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5951 * will work with any notification style.
5952 *
5953 * @param background the background bitmap
5954 * @return this object for method chaining
5955 * @see android.app.Notification.WearableExtender#getBackground
5956 */
5957 public WearableExtender setBackground(Bitmap background) {
5958 mBackground = background;
5959 return this;
5960 }
5961
5962 /**
5963 * Get a background image to be displayed behind the notification content.
5964 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5965 * will work with any notification style.
5966 *
5967 * @return the background image
5968 * @see android.app.Notification.WearableExtender#setBackground
5969 */
5970 public Bitmap getBackground() {
5971 return mBackground;
5972 }
5973
5974 /**
5975 * Set an icon that goes with the content of this notification.
5976 */
5977 public WearableExtender setContentIcon(int icon) {
5978 mContentIcon = icon;
5979 return this;
5980 }
5981
5982 /**
5983 * Get an icon that goes with the content of this notification.
5984 */
5985 public int getContentIcon() {
5986 return mContentIcon;
5987 }
5988
5989 /**
5990 * Set the gravity that the content icon should have within the notification display.
5991 * Supported values include {@link android.view.Gravity#START} and
5992 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5993 * @see #setContentIcon
5994 */
5995 public WearableExtender setContentIconGravity(int contentIconGravity) {
5996 mContentIconGravity = contentIconGravity;
5997 return this;
5998 }
5999
6000 /**
6001 * Get the gravity that the content icon should have within the notification display.
6002 * Supported values include {@link android.view.Gravity#START} and
6003 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6004 * @see #getContentIcon
6005 */
6006 public int getContentIconGravity() {
6007 return mContentIconGravity;
6008 }
6009
6010 /**
6011 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006012 * this notification. This action will no longer display separately from the
6013 * notification's content.
6014 *
Griff Hazenca48d352014-05-28 22:37:13 -07006015 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006016 * set, although the list of available actions comes from the main notification and not
6017 * from the child page's notification.
6018 *
6019 * @param actionIndex The index of the action to hoist onto the current notification page.
6020 * If wearable actions were added to the main notification, this index
6021 * will apply to that list, otherwise it will apply to the regular
6022 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006023 */
6024 public WearableExtender setContentAction(int actionIndex) {
6025 mContentActionIndex = actionIndex;
6026 return this;
6027 }
6028
6029 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006030 * Get the index of the notification action, if any, that was specified as being clickable
6031 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006032 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006033 *
Griff Hazenca48d352014-05-28 22:37:13 -07006034 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006035 * set, although the list of available actions comes from the main notification and not
6036 * from the child page's notification.
6037 *
6038 * <p>If wearable specific actions were added to the main notification, this index will
6039 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006040 *
6041 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006042 */
6043 public int getContentAction() {
6044 return mContentActionIndex;
6045 }
6046
6047 /**
6048 * Set the gravity that this notification should have within the available viewport space.
6049 * Supported values include {@link android.view.Gravity#TOP},
6050 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6051 * The default value is {@link android.view.Gravity#BOTTOM}.
6052 */
6053 public WearableExtender setGravity(int gravity) {
6054 mGravity = gravity;
6055 return this;
6056 }
6057
6058 /**
6059 * Get the gravity that this notification should have within the available viewport space.
6060 * Supported values include {@link android.view.Gravity#TOP},
6061 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6062 * The default value is {@link android.view.Gravity#BOTTOM}.
6063 */
6064 public int getGravity() {
6065 return mGravity;
6066 }
6067
6068 /**
6069 * Set the custom size preset for the display of this notification out of the available
6070 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6071 * {@link #SIZE_LARGE}.
6072 * <p>Some custom size presets are only applicable for custom display notifications created
6073 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6074 * documentation for the preset in question. See also
6075 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6076 */
6077 public WearableExtender setCustomSizePreset(int sizePreset) {
6078 mCustomSizePreset = sizePreset;
6079 return this;
6080 }
6081
6082 /**
6083 * Get the custom size preset for the display of this notification out of the available
6084 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6085 * {@link #SIZE_LARGE}.
6086 * <p>Some custom size presets are only applicable for custom display notifications created
6087 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6088 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6089 */
6090 public int getCustomSizePreset() {
6091 return mCustomSizePreset;
6092 }
6093
6094 /**
6095 * Set the custom height in pixels for the display of this notification's content.
6096 * <p>This option is only available for custom display notifications created
6097 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6098 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6099 * {@link #getCustomContentHeight}.
6100 */
6101 public WearableExtender setCustomContentHeight(int height) {
6102 mCustomContentHeight = height;
6103 return this;
6104 }
6105
6106 /**
6107 * Get the custom height in pixels for the display of this notification's content.
6108 * <p>This option is only available for custom display notifications created
6109 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6110 * {@link #setCustomContentHeight}.
6111 */
6112 public int getCustomContentHeight() {
6113 return mCustomContentHeight;
6114 }
6115
6116 /**
6117 * Set whether the scrolling position for the contents of this notification should start
6118 * at the bottom of the contents instead of the top when the contents are too long to
6119 * display within the screen. Default is false (start scroll at the top).
6120 */
6121 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6122 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6123 return this;
6124 }
6125
6126 /**
6127 * Get whether the scrolling position for the contents of this notification should start
6128 * at the bottom of the contents instead of the top when the contents are too long to
6129 * display within the screen. Default is false (start scroll at the top).
6130 */
6131 public boolean getStartScrollBottom() {
6132 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6133 }
6134
6135 /**
6136 * Set whether the content intent is available when the wearable device is not connected
6137 * to a companion device. The user can still trigger this intent when the wearable device
6138 * is offline, but a visual hint will indicate that the content intent may not be available.
6139 * Defaults to true.
6140 */
6141 public WearableExtender setContentIntentAvailableOffline(
6142 boolean contentIntentAvailableOffline) {
6143 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6144 return this;
6145 }
6146
6147 /**
6148 * Get whether the content intent is available when the wearable device is not connected
6149 * to a companion device. The user can still trigger this intent when the wearable device
6150 * is offline, but a visual hint will indicate that the content intent may not be available.
6151 * Defaults to true.
6152 */
6153 public boolean getContentIntentAvailableOffline() {
6154 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6155 }
6156
6157 /**
6158 * Set a hint that this notification's icon should not be displayed.
6159 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6160 * @return this object for method chaining
6161 */
6162 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6163 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6164 return this;
6165 }
6166
6167 /**
6168 * Get a hint that this notification's icon should not be displayed.
6169 * @return {@code true} if this icon should not be displayed, false otherwise.
6170 * The default value is {@code false} if this was never set.
6171 */
6172 public boolean getHintHideIcon() {
6173 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6174 }
6175
6176 /**
6177 * Set a visual hint that only the background image of this notification should be
6178 * displayed, and other semantic content should be hidden. This hint is only applicable
6179 * to sub-pages added using {@link #addPage}.
6180 */
6181 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6182 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6183 return this;
6184 }
6185
6186 /**
6187 * Get a visual hint that only the background image of this notification should be
6188 * displayed, and other semantic content should be hidden. This hint is only applicable
6189 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6190 */
6191 public boolean getHintShowBackgroundOnly() {
6192 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6193 }
6194
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006195 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006196 * Set a hint that this notification's background should not be clipped if possible,
6197 * and should instead be resized to fully display on the screen, retaining the aspect
6198 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006199 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6200 * @return this object for method chaining
6201 */
6202 public WearableExtender setHintAvoidBackgroundClipping(
6203 boolean hintAvoidBackgroundClipping) {
6204 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6205 return this;
6206 }
6207
6208 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006209 * Get a hint that this notification's background should not be clipped if possible,
6210 * and should instead be resized to fully display on the screen, retaining the aspect
6211 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006212 * @return {@code true} if it's ok if the background is clipped on the screen, false
6213 * otherwise. The default value is {@code false} if this was never set.
6214 */
6215 public boolean getHintAvoidBackgroundClipping() {
6216 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6217 }
6218
6219 /**
6220 * Set a hint that the screen should remain on for at least this duration when
6221 * this notification is displayed on the screen.
6222 * @param timeout The requested screen timeout in milliseconds. Can also be either
6223 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6224 * @return this object for method chaining
6225 */
6226 public WearableExtender setHintScreenTimeout(int timeout) {
6227 mHintScreenTimeout = timeout;
6228 return this;
6229 }
6230
6231 /**
6232 * Get the duration, in milliseconds, that the screen should remain on for
6233 * when this notification is displayed.
6234 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6235 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6236 */
6237 public int getHintScreenTimeout() {
6238 return mHintScreenTimeout;
6239 }
6240
Alex Hills9ab3a232016-04-05 14:54:56 -04006241 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006242 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6243 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6244 * qr codes, as well as other simple black-and-white tickets.
6245 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6246 * @return this object for method chaining
6247 */
6248 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6249 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6250 return this;
6251 }
6252
6253 /**
6254 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6255 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6256 * qr codes, as well as other simple black-and-white tickets.
6257 * @return {@code true} if it should be displayed in ambient, false otherwise
6258 * otherwise. The default value is {@code false} if this was never set.
6259 */
6260 public boolean getHintAmbientBigPicture() {
6261 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6262 }
6263
6264 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006265 * Set a hint that this notification's content intent will launch an {@link Activity}
6266 * directly, telling the platform that it can generate the appropriate transitions.
6267 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6268 * an activity and transitions should be generated, false otherwise.
6269 * @return this object for method chaining
6270 */
6271 public WearableExtender setHintContentIntentLaunchesActivity(
6272 boolean hintContentIntentLaunchesActivity) {
6273 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6274 return this;
6275 }
6276
6277 /**
6278 * Get a hint that this notification's content intent will launch an {@link Activity}
6279 * directly, telling the platform that it can generate the appropriate transitions
6280 * @return {@code true} if the content intent will launch an activity and transitions should
6281 * be generated, false otherwise. The default value is {@code false} if this was never set.
6282 */
6283 public boolean getHintContentIntentLaunchesActivity() {
6284 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6285 }
6286
Nadia Benbernou948627e2016-04-14 14:41:08 -04006287 /**
6288 * When you post a notification, if you set the dismissal id field, then when that
6289 * notification is canceled, notifications on other wearables and the paired Android phone
6290 * having that same dismissal id will also be canceled. Note that this only works if you
6291 * have notification bridge mode set to NO_BRIDGING in your Wear app manifest. See
6292 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
6293 * Notifications</a> for more information on how to use the bridge mode feature.
6294 * @param dismissalId the dismissal id of the notification.
6295 * @return this object for method chaining
6296 */
6297 public WearableExtender setDismissalId(String dismissalId) {
6298 mDismissalId = dismissalId;
6299 return this;
6300 }
6301
6302 /**
6303 * Returns the dismissal id of the notification.
6304 * @return the dismissal id of the notification or null if it has not been set.
6305 */
6306 public String getDismissalId() {
6307 return mDismissalId;
6308 }
6309
Griff Hazen61a9e862014-05-22 16:05:19 -07006310 private void setFlag(int mask, boolean value) {
6311 if (value) {
6312 mFlags |= mask;
6313 } else {
6314 mFlags &= ~mask;
6315 }
6316 }
6317 }
6318
6319 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006320 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6321 * with car extensions:
6322 *
6323 * <ol>
6324 * <li>Create an {@link Notification.Builder}, setting any desired
6325 * properties.
6326 * <li>Create a {@link CarExtender}.
6327 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6328 * {@link CarExtender}.
6329 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6330 * to apply the extensions to a notification.
6331 * </ol>
6332 *
6333 * <pre class="prettyprint">
6334 * Notification notification = new Notification.Builder(context)
6335 * ...
6336 * .extend(new CarExtender()
6337 * .set*(...))
6338 * .build();
6339 * </pre>
6340 *
6341 * <p>Car extensions can be accessed on an existing notification by using the
6342 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6343 * to access values.
6344 */
6345 public static final class CarExtender implements Extender {
6346 private static final String TAG = "CarExtender";
6347
6348 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6349 private static final String EXTRA_LARGE_ICON = "large_icon";
6350 private static final String EXTRA_CONVERSATION = "car_conversation";
6351 private static final String EXTRA_COLOR = "app_color";
6352
6353 private Bitmap mLargeIcon;
6354 private UnreadConversation mUnreadConversation;
6355 private int mColor = Notification.COLOR_DEFAULT;
6356
6357 /**
6358 * Create a {@link CarExtender} with default options.
6359 */
6360 public CarExtender() {
6361 }
6362
6363 /**
6364 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6365 *
6366 * @param notif The notification from which to copy options.
6367 */
6368 public CarExtender(Notification notif) {
6369 Bundle carBundle = notif.extras == null ?
6370 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6371 if (carBundle != null) {
6372 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6373 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6374
6375 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6376 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6377 }
6378 }
6379
6380 /**
6381 * Apply car extensions to a notification that is being built. This is typically called by
6382 * the {@link Notification.Builder#extend(Notification.Extender)}
6383 * method of {@link Notification.Builder}.
6384 */
6385 @Override
6386 public Notification.Builder extend(Notification.Builder builder) {
6387 Bundle carExtensions = new Bundle();
6388
6389 if (mLargeIcon != null) {
6390 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6391 }
6392 if (mColor != Notification.COLOR_DEFAULT) {
6393 carExtensions.putInt(EXTRA_COLOR, mColor);
6394 }
6395
6396 if (mUnreadConversation != null) {
6397 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6398 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6399 }
6400
6401 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6402 return builder;
6403 }
6404
6405 /**
6406 * Sets the accent color to use when Android Auto presents the notification.
6407 *
6408 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6409 * to accent the displayed notification. However, not all colors are acceptable in an
6410 * automotive setting. This method can be used to override the color provided in the
6411 * notification in such a situation.
6412 */
Tor Norbye80756e32015-03-02 09:39:27 -08006413 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006414 mColor = color;
6415 return this;
6416 }
6417
6418 /**
6419 * Gets the accent color.
6420 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006421 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006422 */
Tor Norbye80756e32015-03-02 09:39:27 -08006423 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006424 public int getColor() {
6425 return mColor;
6426 }
6427
6428 /**
6429 * Sets the large icon of the car notification.
6430 *
6431 * If no large icon is set in the extender, Android Auto will display the icon
6432 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6433 *
6434 * @param largeIcon The large icon to use in the car notification.
6435 * @return This object for method chaining.
6436 */
6437 public CarExtender setLargeIcon(Bitmap largeIcon) {
6438 mLargeIcon = largeIcon;
6439 return this;
6440 }
6441
6442 /**
6443 * Gets the large icon used in this car notification, or null if no icon has been set.
6444 *
6445 * @return The large icon for the car notification.
6446 * @see CarExtender#setLargeIcon
6447 */
6448 public Bitmap getLargeIcon() {
6449 return mLargeIcon;
6450 }
6451
6452 /**
6453 * Sets the unread conversation in a message notification.
6454 *
6455 * @param unreadConversation The unread part of the conversation this notification conveys.
6456 * @return This object for method chaining.
6457 */
6458 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6459 mUnreadConversation = unreadConversation;
6460 return this;
6461 }
6462
6463 /**
6464 * Returns the unread conversation conveyed by this notification.
6465 * @see #setUnreadConversation(UnreadConversation)
6466 */
6467 public UnreadConversation getUnreadConversation() {
6468 return mUnreadConversation;
6469 }
6470
6471 /**
6472 * A class which holds the unread messages from a conversation.
6473 */
6474 public static class UnreadConversation {
6475 private static final String KEY_AUTHOR = "author";
6476 private static final String KEY_TEXT = "text";
6477 private static final String KEY_MESSAGES = "messages";
6478 private static final String KEY_REMOTE_INPUT = "remote_input";
6479 private static final String KEY_ON_REPLY = "on_reply";
6480 private static final String KEY_ON_READ = "on_read";
6481 private static final String KEY_PARTICIPANTS = "participants";
6482 private static final String KEY_TIMESTAMP = "timestamp";
6483
6484 private final String[] mMessages;
6485 private final RemoteInput mRemoteInput;
6486 private final PendingIntent mReplyPendingIntent;
6487 private final PendingIntent mReadPendingIntent;
6488 private final String[] mParticipants;
6489 private final long mLatestTimestamp;
6490
6491 UnreadConversation(String[] messages, RemoteInput remoteInput,
6492 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6493 String[] participants, long latestTimestamp) {
6494 mMessages = messages;
6495 mRemoteInput = remoteInput;
6496 mReadPendingIntent = readPendingIntent;
6497 mReplyPendingIntent = replyPendingIntent;
6498 mParticipants = participants;
6499 mLatestTimestamp = latestTimestamp;
6500 }
6501
6502 /**
6503 * Gets the list of messages conveyed by this notification.
6504 */
6505 public String[] getMessages() {
6506 return mMessages;
6507 }
6508
6509 /**
6510 * Gets the remote input that will be used to convey the response to a message list, or
6511 * null if no such remote input exists.
6512 */
6513 public RemoteInput getRemoteInput() {
6514 return mRemoteInput;
6515 }
6516
6517 /**
6518 * Gets the pending intent that will be triggered when the user replies to this
6519 * notification.
6520 */
6521 public PendingIntent getReplyPendingIntent() {
6522 return mReplyPendingIntent;
6523 }
6524
6525 /**
6526 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6527 * in this object's message list.
6528 */
6529 public PendingIntent getReadPendingIntent() {
6530 return mReadPendingIntent;
6531 }
6532
6533 /**
6534 * Gets the participants in the conversation.
6535 */
6536 public String[] getParticipants() {
6537 return mParticipants;
6538 }
6539
6540 /**
6541 * Gets the firs participant in the conversation.
6542 */
6543 public String getParticipant() {
6544 return mParticipants.length > 0 ? mParticipants[0] : null;
6545 }
6546
6547 /**
6548 * Gets the timestamp of the conversation.
6549 */
6550 public long getLatestTimestamp() {
6551 return mLatestTimestamp;
6552 }
6553
6554 Bundle getBundleForUnreadConversation() {
6555 Bundle b = new Bundle();
6556 String author = null;
6557 if (mParticipants != null && mParticipants.length > 1) {
6558 author = mParticipants[0];
6559 }
6560 Parcelable[] messages = new Parcelable[mMessages.length];
6561 for (int i = 0; i < messages.length; i++) {
6562 Bundle m = new Bundle();
6563 m.putString(KEY_TEXT, mMessages[i]);
6564 m.putString(KEY_AUTHOR, author);
6565 messages[i] = m;
6566 }
6567 b.putParcelableArray(KEY_MESSAGES, messages);
6568 if (mRemoteInput != null) {
6569 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6570 }
6571 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6572 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6573 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6574 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6575 return b;
6576 }
6577
6578 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6579 if (b == null) {
6580 return null;
6581 }
6582 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6583 String[] messages = null;
6584 if (parcelableMessages != null) {
6585 String[] tmp = new String[parcelableMessages.length];
6586 boolean success = true;
6587 for (int i = 0; i < tmp.length; i++) {
6588 if (!(parcelableMessages[i] instanceof Bundle)) {
6589 success = false;
6590 break;
6591 }
6592 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6593 if (tmp[i] == null) {
6594 success = false;
6595 break;
6596 }
6597 }
6598 if (success) {
6599 messages = tmp;
6600 } else {
6601 return null;
6602 }
6603 }
6604
6605 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6606 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6607
6608 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6609
6610 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6611 if (participants == null || participants.length != 1) {
6612 return null;
6613 }
6614
6615 return new UnreadConversation(messages,
6616 remoteInput,
6617 onReply,
6618 onRead,
6619 participants, b.getLong(KEY_TIMESTAMP));
6620 }
6621 };
6622
6623 /**
6624 * Builder class for {@link CarExtender.UnreadConversation} objects.
6625 */
6626 public static class Builder {
6627 private final List<String> mMessages = new ArrayList<String>();
6628 private final String mParticipant;
6629 private RemoteInput mRemoteInput;
6630 private PendingIntent mReadPendingIntent;
6631 private PendingIntent mReplyPendingIntent;
6632 private long mLatestTimestamp;
6633
6634 /**
6635 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6636 *
6637 * @param name The name of the other participant in the conversation.
6638 */
6639 public Builder(String name) {
6640 mParticipant = name;
6641 }
6642
6643 /**
6644 * Appends a new unread message to the list of messages for this conversation.
6645 *
6646 * The messages should be added from oldest to newest.
6647 *
6648 * @param message The text of the new unread message.
6649 * @return This object for method chaining.
6650 */
6651 public Builder addMessage(String message) {
6652 mMessages.add(message);
6653 return this;
6654 }
6655
6656 /**
6657 * Sets the pending intent and remote input which will convey the reply to this
6658 * notification.
6659 *
6660 * @param pendingIntent The pending intent which will be triggered on a reply.
6661 * @param remoteInput The remote input parcelable which will carry the reply.
6662 * @return This object for method chaining.
6663 *
6664 * @see CarExtender.UnreadConversation#getRemoteInput
6665 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6666 */
6667 public Builder setReplyAction(
6668 PendingIntent pendingIntent, RemoteInput remoteInput) {
6669 mRemoteInput = remoteInput;
6670 mReplyPendingIntent = pendingIntent;
6671
6672 return this;
6673 }
6674
6675 /**
6676 * Sets the pending intent that will be sent once the messages in this notification
6677 * are read.
6678 *
6679 * @param pendingIntent The pending intent to use.
6680 * @return This object for method chaining.
6681 */
6682 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6683 mReadPendingIntent = pendingIntent;
6684 return this;
6685 }
6686
6687 /**
6688 * Sets the timestamp of the most recent message in an unread conversation.
6689 *
6690 * If a messaging notification has been posted by your application and has not
6691 * yet been cancelled, posting a later notification with the same id and tag
6692 * but without a newer timestamp may result in Android Auto not displaying a
6693 * heads up notification for the later notification.
6694 *
6695 * @param timestamp The timestamp of the most recent message in the conversation.
6696 * @return This object for method chaining.
6697 */
6698 public Builder setLatestTimestamp(long timestamp) {
6699 mLatestTimestamp = timestamp;
6700 return this;
6701 }
6702
6703 /**
6704 * Builds a new unread conversation object.
6705 *
6706 * @return The new unread conversation object.
6707 */
6708 public UnreadConversation build() {
6709 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6710 String[] participants = { mParticipant };
6711 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6712 mReadPendingIntent, participants, mLatestTimestamp);
6713 }
6714 }
6715 }
6716
6717 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006718 * Get an array of Notification objects from a parcelable array bundle field.
6719 * Update the bundle to have a typed array so fetches in the future don't need
6720 * to do an array copy.
6721 */
6722 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6723 Parcelable[] array = bundle.getParcelableArray(key);
6724 if (array instanceof Notification[] || array == null) {
6725 return (Notification[]) array;
6726 }
6727 Notification[] typedArray = Arrays.copyOf(array, array.length,
6728 Notification[].class);
6729 bundle.putParcelableArray(key, typedArray);
6730 return typedArray;
6731 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006732
6733 private static class BuilderRemoteViews extends RemoteViews {
6734 public BuilderRemoteViews(Parcel parcel) {
6735 super(parcel);
6736 }
6737
Kenny Guy77320062014-08-27 21:37:15 +01006738 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6739 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006740 }
6741
6742 @Override
6743 public BuilderRemoteViews clone() {
6744 Parcel p = Parcel.obtain();
6745 writeToParcel(p, 0);
6746 p.setDataPosition(0);
6747 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6748 p.recycle();
6749 return brv;
6750 }
6751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752}