blob: bcdf3f49251651be19ffa14fab8bf54477952c3b [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;
Alex Hillsfd590442016-10-07 09:52:44 -040022import android.annotation.NonNull;
Daniel Sandler01df1c62014-06-09 10:54:01 -040023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040025import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010028import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040029import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020030import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020031import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040032import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070034import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010035import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010036import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040037import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040038import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070039import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070040import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040042import android.os.BadParcelableException;
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 Cinek981962e2016-07-20 20:41:58 -070054import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080055import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070056import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080057import android.text.style.RelativeSizeSpan;
58import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070059import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040060import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050061import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070062import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080063import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080064import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070065import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070066import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.widget.RemoteViews;
68
Griff Hazen959591e2014-05-15 22:26:18 -070069import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070070import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070071import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070072
Tor Norbyed9273d62013-05-30 15:59:53 -070073import java.lang.annotation.Retention;
74import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020075import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050076import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070077import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070078import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070079import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050080import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082/**
83 * A class that represents how a persistent notification is to be presented to
84 * the user using the {@link android.app.NotificationManager}.
85 *
Joe Onoratocb109a02011-01-18 17:57:41 -080086 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
87 * easier to construct Notifications.</p>
88 *
Joe Fernandez558459f2011-10-13 16:47:36 -070089 * <div class="special reference">
90 * <h3>Developer Guides</h3>
91 * <p>For a guide to creating notifications, read the
92 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
93 * developer guide.</p>
94 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
96public class Notification implements Parcelable
97{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040098 private static final String TAG = "Notification";
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400101 * An activity that provides a user interface for adjusting notification preferences for its
102 * containing application. Optional but recommended for apps that post
103 * {@link android.app.Notification Notifications}.
104 */
105 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
106 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
107 = "android.intent.category.NOTIFICATION_PREFERENCES";
108
109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * Use all default values (where applicable).
111 */
112 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /**
115 * Use the default notification sound. This will ignore any given
116 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 *
Chris Wren47c20a12014-06-18 17:27:29 -0400118 * <p>
119 * A notification that is noisy is more likely to be presented as a heads-up notification.
120 * </p>
121 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 */
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final int DEFAULT_SOUND = 1;
126
127 /**
128 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700130 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131 *
Chris Wren47c20a12014-06-18 17:27:29 -0400132 * <p>
133 * A notification that vibrates is more likely to be presented as a heads-up notification.
134 * </p>
135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137 */
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /**
142 * Use the default notification lights. This will ignore the
143 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
144 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147 */
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200152 * Maximum length of CharSequences accepted by Builder and friends.
153 *
154 * <p>
155 * Avoids spamming the system with overly large strings such as full e-mails.
156 */
157 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
158
159 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800160 * Maximum entries of reply text that are accepted by Builder and friends.
161 */
162 private static final int MAX_REPLY_HISTORY = 5;
163
164 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500165 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800166 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * Default value: {@link System#currentTimeMillis() Now}.
168 *
169 * Choose a timestamp that will be most relevant to the user. For most finite events, this
170 * corresponds to the time the event happened (or will happen, in the case of events that have
171 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800172 * timestamped according to when the activity began.
173 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800175 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 * <ul>
177 * <li>Notification of a new chat message should be stamped when the message was received.</li>
178 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
179 * <li>Notification of a completed file download should be stamped when the download finished.</li>
180 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
181 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
182 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800183 * </ul>
184 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700185 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
186 * anymore by default and must be opted into by using
187 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 */
189 public long when;
190
191 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700192 * The creation time of the notification
193 */
194 private long creationTime;
195
196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400198 *
199 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 */
Dan Sandler86647982015-05-13 23:41:13 -0400201 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700202 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public int icon;
204
205 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800206 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
207 * leave it at its default value of 0.
208 *
209 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700210 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800211 */
212 public int iconLevel;
213
214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500215 * The number of events that this notification represents. For example, in a new mail
216 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800217 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500218 * The system may or may not use this field to modify the appearance of the notification. For
219 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
220 * superimposed over the icon in the status bar. Starting with
221 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
222 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800223 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500224 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700225 *
226 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public int number;
229
230 /**
231 * The intent to execute when the expanded status entry is clicked. If
232 * this is an activity, it must include the
233 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800234 * that you take care of task management as described in the
235 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800236 * Stack</a> document. In particular, make sure to read the notification section
237 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
238 * Notifications</a> for the correct ways to launch an application from a
239 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
241 public PendingIntent contentIntent;
242
243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500244 * The intent to execute when the notification is explicitly dismissed by the user, either with
245 * the "Clear All" button or by swiping it away individually.
246 *
247 * This probably shouldn't be launching an activity since several of those will be sent
248 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public PendingIntent deleteIntent;
251
252 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700253 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800254 *
Chris Wren47c20a12014-06-18 17:27:29 -0400255 * <p>
256 * The system UI may choose to display a heads-up notification, instead of
257 * launching this intent, while the user is using the device.
258 * </p>
259 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800260 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400261 */
262 public PendingIntent fullScreenIntent;
263
264 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400265 * Text that summarizes this notification for accessibility services.
266 *
267 * As of the L release, this text is no longer shown on screen, but it is still useful to
268 * accessibility services (where it serves as an audible announcement of the notification's
269 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400270 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800271 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public CharSequence tickerText;
274
275 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400276 * Formerly, a view showing the {@link #tickerText}.
277 *
278 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400279 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400280 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800281 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400282
283 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400284 * The view that will represent this notification in the notification list (which is pulled
285 * down from the status bar).
286 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500287 * As of N, this field may be null. The notification view is determined by the inputs
288 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400289 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 public RemoteViews contentView;
293
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400294 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400295 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400296 * opportunity to show more detail. The system UI may choose to show this
297 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400298 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500299 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400300 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
301 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400302 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400303 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400304 public RemoteViews bigContentView;
305
Chris Wren8fd39ec2014-02-27 17:43:26 -0500306
307 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400308 * A medium-format version of {@link #contentView}, providing the Notification an
309 * opportunity to add action buttons to contentView. At its discretion, the system UI may
310 * choose to show this as a heads-up notification, which will pop up so the user can see
311 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500313 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
315 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500316 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400317 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500318 public RemoteViews headsUpContentView;
319
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400320 /**
Dan Sandler86647982015-05-13 23:41:13 -0400321 * A large bitmap to be shown in the notification content area.
322 *
323 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 */
Dan Sandler86647982015-05-13 23:41:13 -0400325 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800326 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
328 /**
329 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400332 * A notification that is noisy is more likely to be presented as a heads-up notification.
333 * </p>
334 *
335 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500336 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * </p>
338 */
339 public Uri sound;
340
341 /**
342 * Use this constant as the value for audioStreamType to request that
343 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700344 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400345 *
346 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700348 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 public static final int STREAM_DEFAULT = -1;
350
351 /**
352 * The audio stream type to use when playing the sound.
353 * Should be one of the STREAM_ constants from
354 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400355 *
356 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700358 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 public int audioStreamType = STREAM_DEFAULT;
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400362 * The default value of {@link #audioAttributes}.
363 */
364 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
365 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
366 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
367 .build();
368
369 /**
370 * The {@link AudioAttributes audio attributes} to use when playing the sound.
371 */
372 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
373
374 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500375 * The pattern with which to vibrate.
376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * <p>
378 * To vibrate the default pattern, see {@link #defaults}.
379 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500380 *
Chris Wren47c20a12014-06-18 17:27:29 -0400381 * <p>
382 * A notification that vibrates is more likely to be presented as a heads-up notification.
383 * </p>
384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * @see android.os.Vibrator#vibrate(long[],int)
386 */
387 public long[] vibrate;
388
389 /**
390 * The color of the led. The hardware will do its best approximation.
391 *
392 * @see #FLAG_SHOW_LIGHTS
393 * @see #flags
394 */
Tor Norbye80756e32015-03-02 09:39:27 -0800395 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 public int ledARGB;
397
398 /**
399 * The number of milliseconds for the LED to be on while it's flashing.
400 * The hardware will do its best approximation.
401 *
402 * @see #FLAG_SHOW_LIGHTS
403 * @see #flags
404 */
405 public int ledOnMS;
406
407 /**
408 * The number of milliseconds for the LED to be off while it's flashing.
409 * The hardware will do its best approximation.
410 *
411 * @see #FLAG_SHOW_LIGHTS
412 * @see #flags
413 */
414 public int ledOffMS;
415
416 /**
417 * Specifies which values should be taken from the defaults.
418 * <p>
419 * To set, OR the desired from {@link #DEFAULT_SOUND},
420 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
421 * values, use {@link #DEFAULT_ALL}.
422 * </p>
423 */
424 public int defaults;
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 /**
427 * Bit to be bitwise-ored into the {@link #flags} field that should be
428 * set if you want the LED on for this notification.
429 * <ul>
430 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
431 * or 0 for both ledOnMS and ledOffMS.</li>
432 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
433 * <li>To flash the LED, pass the number of milliseconds that it should
434 * be on and off to ledOnMS and ledOffMS.</li>
435 * </ul>
436 * <p>
437 * Since hardware varies, you are not guaranteed that any of the values
438 * you pass are honored exactly. Use the system defaults (TODO) if possible
439 * because they will be set to values that work on any given hardware.
440 * <p>
441 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
444 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
445
446 /**
447 * Bit to be bitwise-ored into the {@link #flags} field that should be
448 * set if this notification is in reference to something that is ongoing,
449 * like a phone call. It should not be set if this notification is in
450 * reference to something that happened at a particular point in time,
451 * like a missed phone call.
452 */
453 public static final int FLAG_ONGOING_EVENT = 0x00000002;
454
455 /**
456 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700457 * the audio will be repeated until the notification is
458 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
460 public static final int FLAG_INSISTENT = 0x00000004;
461
462 /**
463 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700464 * set if you would only like the sound, vibrate and ticker to be played
465 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 */
467 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
468
469 /**
470 * Bit to be bitwise-ored into the {@link #flags} field that should be
471 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500472 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
474 public static final int FLAG_AUTO_CANCEL = 0x00000010;
475
476 /**
477 * Bit to be bitwise-ored into the {@link #flags} field that should be
478 * set if the notification should not be canceled when the user clicks
479 * the Clear all button.
480 */
481 public static final int FLAG_NO_CLEAR = 0x00000020;
482
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700483 /**
484 * Bit to be bitwise-ored into the {@link #flags} field that should be
485 * set if this notification represents a currently running service. This
486 * will normally be set for you by {@link Service#startForeground}.
487 */
488 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
489
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400490 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500491 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800492 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500493 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400494 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500495 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400496
Griff Hazendfcb0802014-02-11 12:00:00 -0800497 /**
498 * Bit to be bitswise-ored into the {@link #flags} field that should be
499 * set if this notification is relevant to the current device only
500 * and it is not recommended that it bridge to other devices.
501 */
502 public static final int FLAG_LOCAL_ONLY = 0x00000100;
503
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700504 /**
505 * Bit to be bitswise-ored into the {@link #flags} field that should be
506 * set if this notification is the group summary for a group of notifications.
507 * Grouped notifications may display in a cluster or stack on devices which
508 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
509 */
510 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
511
Julia Reynoldse46bb372016-03-17 11:05:58 -0400512 /**
513 * Bit to be bitswise-ored into the {@link #flags} field that should be
514 * set if this notification is the group summary for an auto-group of notifications.
515 *
516 * @hide
517 */
518 @SystemApi
519 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public int flags;
522
Tor Norbyed9273d62013-05-30 15:59:53 -0700523 /** @hide */
524 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
525 @Retention(RetentionPolicy.SOURCE)
526 public @interface Priority {}
527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500529 * Default notification {@link #priority}. If your application does not prioritize its own
530 * notifications, use this value for all notifications.
531 */
532 public static final int PRIORITY_DEFAULT = 0;
533
534 /**
535 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
536 * items smaller, or at a different position in the list, compared with your app's
537 * {@link #PRIORITY_DEFAULT} items.
538 */
539 public static final int PRIORITY_LOW = -1;
540
541 /**
542 * Lowest {@link #priority}; these items might not be shown to the user except under special
543 * circumstances, such as detailed notification logs.
544 */
545 public static final int PRIORITY_MIN = -2;
546
547 /**
548 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
549 * show these items larger, or at a different position in notification lists, compared with
550 * your app's {@link #PRIORITY_DEFAULT} items.
551 */
552 public static final int PRIORITY_HIGH = 1;
553
554 /**
555 * Highest {@link #priority}, for your application's most important items that require the
556 * user's prompt attention or input.
557 */
558 public static final int PRIORITY_MAX = 2;
559
560 /**
561 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800562 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500563 * Priority is an indication of how much of the user's valuable attention should be consumed by
564 * this notification. Low-priority notifications may be hidden from the user in certain
565 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500566 * system will make a determination about how to interpret this priority when presenting
567 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400568 *
569 * <p>
570 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
571 * as a heads-up notification.
572 * </p>
573 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500574 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700575 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500576 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800577
Dan Sandler26e81cf2014-05-06 10:01:27 -0400578 /**
579 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
580 * to be applied by the standard Style templates when presenting this notification.
581 *
582 * The current template design constructs a colorful header image by overlaying the
583 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
584 * ignored.
585 */
Tor Norbye80756e32015-03-02 09:39:27 -0800586 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400587 public int color = COLOR_DEFAULT;
588
589 /**
590 * Special value of {@link #color} telling the system not to decorate this notification with
591 * any special color but instead use default colors when presenting this notification.
592 */
Tor Norbye80756e32015-03-02 09:39:27 -0800593 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400594 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600595
596 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800597 * Special value of {@link #color} used as a place holder for an invalid color.
598 */
599 @ColorInt
600 private static final int COLOR_INVALID = 1;
601
602 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700603 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
604 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600605 * lockscreen).
606 *
607 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
608 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
609 * shown in all situations, but the contents are only available if the device is unlocked for
610 * the appropriate user.
611 *
612 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
613 * can be read even in an "insecure" context (that is, above a secure lockscreen).
614 * To modify the public version of this notification—for example, to redact some portions—see
615 * {@link Builder#setPublicVersion(Notification)}.
616 *
617 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
618 * and ticker until the user has bypassed the lockscreen.
619 */
620 public int visibility;
621
Griff Hazenfc3922d2014-08-20 11:56:44 -0700622 /**
623 * Notification visibility: Show this notification in its entirety on all lockscreens.
624 *
625 * {@see #visibility}
626 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600627 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700628
629 /**
630 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
631 * private information on secure lockscreens.
632 *
633 * {@see #visibility}
634 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600635 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700636
637 /**
638 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
639 *
640 * {@see #visibility}
641 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600642 public static final int VISIBILITY_SECRET = -1;
643
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400647 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648
649 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400652 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653
654 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400655 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400657 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658
659 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400662 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663
664 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400665 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400667 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500668
669 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400670 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400672 public static final String CATEGORY_ALARM = "alarm";
673
674 /**
675 * Notification category: progress of a long-running background operation.
676 */
677 public static final String CATEGORY_PROGRESS = "progress";
678
679 /**
680 * Notification category: social network or sharing update.
681 */
682 public static final String CATEGORY_SOCIAL = "social";
683
684 /**
685 * Notification category: error in background operation or authentication status.
686 */
687 public static final String CATEGORY_ERROR = "err";
688
689 /**
690 * Notification category: media transport control for playback.
691 */
692 public static final String CATEGORY_TRANSPORT = "transport";
693
694 /**
695 * Notification category: system or device status update. Reserved for system use.
696 */
697 public static final String CATEGORY_SYSTEM = "sys";
698
699 /**
700 * Notification category: indication of running background service.
701 */
702 public static final String CATEGORY_SERVICE = "service";
703
704 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400705 * Notification category: a specific, timely recommendation for a single thing.
706 * For example, a news app might want to recommend a news story it believes the user will
707 * want to read next.
708 */
709 public static final String CATEGORY_RECOMMENDATION = "recommendation";
710
711 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400712 * Notification category: ongoing information about device or contextual status.
713 */
714 public static final String CATEGORY_STATUS = "status";
715
716 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400717 * Notification category: user-scheduled reminder.
718 */
719 public static final String CATEGORY_REMINDER = "reminder";
720
721 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400722 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
723 * that best describes this Notification. May be used by the system for ranking and filtering.
724 */
725 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700727 private String mGroupKey;
728
729 /**
730 * Get the key used to group this notification into a cluster or stack
731 * with other notifications on devices which support such rendering.
732 */
733 public String getGroup() {
734 return mGroupKey;
735 }
736
737 private String mSortKey;
738
739 /**
740 * Get a sort key that orders this notification among other notifications from the
741 * same package. This can be useful if an external sort was already applied and an app
742 * would like to preserve this. Notifications will be sorted lexicographically using this
743 * value, although providing different priorities in addition to providing sort key may
744 * cause this value to be ignored.
745 *
746 * <p>This sort key can also be used to order members of a notification group. See
747 * {@link Builder#setGroup}.
748 *
749 * @see String#compareTo(String)
750 */
751 public String getSortKey() {
752 return mSortKey;
753 }
754
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400756 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400757 * <p>
758 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
759 * APIs, and are intended to be used by
760 * {@link android.service.notification.NotificationListenerService} implementations to extract
761 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500762 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400763 public Bundle extras = new Bundle();
764
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400765 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700766 * All pending intents in the notification as the system needs to be able to access them but
767 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700768 * custom parcelable objects.
769 *
770 * @hide
771 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700772 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700773
774 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400775 * {@link #extras} key: this is the title of the notification,
776 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
777 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500778 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400779
780 /**
781 * {@link #extras} key: this is the title of the notification when shown in expanded form,
782 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
783 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400784 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400785
786 /**
787 * {@link #extras} key: this is the main text payload, as supplied to
788 * {@link Builder#setContentText(CharSequence)}.
789 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500790 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400791
792 /**
793 * {@link #extras} key: this is a third line of text, as supplied to
794 * {@link Builder#setSubText(CharSequence)}.
795 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400796 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400797
798 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800799 * {@link #extras} key: this is the remote input history, as supplied to
800 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700801 *
802 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
803 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
804 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
805 * notifications once the other party has responded).
806 *
807 * The extra with this key is of type CharSequence[] and contains the most recent entry at
808 * the 0 index, the second most recent at the 1 index, etc.
809 *
810 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800811 */
812 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
813
814 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400815 * {@link #extras} key: this is a small piece of additional text as supplied to
816 * {@link Builder#setContentInfo(CharSequence)}.
817 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400818 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400819
820 /**
821 * {@link #extras} key: this is a line of summary information intended to be shown
822 * alongside expanded notifications, as supplied to (e.g.)
823 * {@link BigTextStyle#setSummaryText(CharSequence)}.
824 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400825 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400826
827 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200828 * {@link #extras} key: this is the longer text shown in the big form of a
829 * {@link BigTextStyle} notification, as supplied to
830 * {@link BigTextStyle#bigText(CharSequence)}.
831 */
832 public static final String EXTRA_BIG_TEXT = "android.bigText";
833
834 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400835 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
836 * supplied to {@link Builder#setSmallIcon(int)}.
837 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500838 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839
840 /**
841 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
842 * notification payload, as
843 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
844 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846
847 /**
848 * {@link #extras} key: this is a bitmap to be used instead of the one from
849 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
850 * shown in its expanded form, as supplied to
851 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
852 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400853 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400854
855 /**
856 * {@link #extras} key: this is the progress value supplied to
857 * {@link Builder#setProgress(int, int, boolean)}.
858 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400859 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400860
861 /**
862 * {@link #extras} key: this is the maximum value supplied to
863 * {@link Builder#setProgress(int, int, boolean)}.
864 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400865 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400866
867 /**
868 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
869 * {@link Builder#setProgress(int, int, boolean)}.
870 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400871 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400872
873 /**
874 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
875 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
876 * {@link Builder#setUsesChronometer(boolean)}.
877 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400878 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400879
880 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800881 * {@link #extras} key: whether the chronometer set on the notification should count down
882 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700883 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800884 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700885 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800886
887 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400888 * {@link #extras} key: whether {@link #when} should be shown,
889 * as supplied to {@link Builder#setShowWhen(boolean)}.
890 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400891 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400892
893 /**
894 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
895 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
896 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400897 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898
899 /**
900 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
901 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
902 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400903 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400904
905 /**
906 * {@link #extras} key: A string representing the name of the specific
907 * {@link android.app.Notification.Style} used to create this notification.
908 */
Chris Wren91ad5632013-06-05 15:05:57 -0400909 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400912 * {@link #extras} key: A String array containing the people that this notification relates to,
913 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400914 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400915 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500916
917 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400918 * Allow certain system-generated notifications to appear before the device is provisioned.
919 * Only available to notifications coming from the android package.
920 * @hide
921 */
922 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
923
924 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700925 * {@link #extras} key: A
926 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
927 * in the background when the notification is selected. The URI must point to an image stream
928 * suitable for passing into
929 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
930 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
931 * URI used for this purpose must require no permissions to read the image data.
932 */
933 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
934
935 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400936 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700937 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400938 * {@link android.app.Notification.MediaStyle} notification.
939 */
940 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
941
942 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100943 * {@link #extras} key: the indices of actions to be shown in the compact view,
944 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
945 */
946 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
947
Christoph Studer943aa672014-08-03 20:31:16 +0200948 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400949 * {@link #extras} key: the username to be displayed for all messages sent by the user including
950 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700951 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
952 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400953 */
954 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
955
956 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700957 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400958 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400959 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400960 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400961
962 /**
963 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
964 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700965 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
966 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400967 */
968 public static final String EXTRA_MESSAGES = "android.messages";
969
970 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100971 * {@link #extras} key: the user that built the notification.
972 *
973 * @hide
974 */
975 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
976
977 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400978 * @hide
979 */
980 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
981
Selim Cinek247fa012016-02-18 09:50:48 -0800982 /**
983 * @hide
984 */
985 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
986
Dan Sandler80eaa592016-04-14 23:34:54 -0400987 /** @hide */
988 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400989 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
990
Dan Sandlerd63f9322015-05-06 15:18:49 -0400991 private Icon mSmallIcon;
992 private Icon mLargeIcon;
993
Chris Wren51c75102013-07-16 20:49:17 -0400994 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400995 * Structure to encapsulate a named action that can be shown as part of this notification.
996 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
997 * selected by the user.
998 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700999 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1000 * or {@link Notification.Builder#addAction(Notification.Action)}
1001 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001002 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001003 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -07001004 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001005 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001006 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001007 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -07001008
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 /**
1010 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001011 *
1012 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013 */
Dan Sandler86647982015-05-13 23:41:13 -04001014 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001015 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001016
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001017 /**
1018 * Title of the action.
1019 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001021
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 /**
1023 * Intent to send when the user invokes this action. May be null, in which case the action
1024 * may be rendered in a disabled presentation by the system UI.
1025 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001026 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001027
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001028 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001029 if (in.readInt() != 0) {
1030 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001031 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1032 icon = mIcon.getResId();
1033 }
Dan Sandler86647982015-05-13 23:41:13 -04001034 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001035 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1036 if (in.readInt() == 1) {
1037 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1038 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001039 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001040 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001041 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001042 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001043
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044 /**
Dan Sandler86647982015-05-13 23:41:13 -04001045 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046 */
Dan Sandler86647982015-05-13 23:41:13 -04001047 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001048 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001049 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001050 }
1051
Adrian Roos7af53622016-10-12 13:44:05 -07001052 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001053 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001054 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001055 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001056 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1057 this.icon = icon.getResId();
1058 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001059 this.title = title;
1060 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001061 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001062 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001063 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001064 }
1065
1066 /**
Dan Sandler86647982015-05-13 23:41:13 -04001067 * Return an icon representing the action.
1068 */
1069 public Icon getIcon() {
1070 if (mIcon == null && icon != 0) {
1071 // you snuck an icon in here without using the builder; let's try to keep it
1072 mIcon = Icon.createWithResource("", icon);
1073 }
1074 return mIcon;
1075 }
1076
1077 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001078 * Get additional metadata carried around with this Action.
1079 */
1080 public Bundle getExtras() {
1081 return mExtras;
1082 }
1083
1084 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001085 * Return whether the platform should automatically generate possible replies for this
1086 * {@link Action}
1087 */
1088 public boolean getAllowGeneratedReplies() {
1089 return mAllowGeneratedReplies;
1090 }
1091
1092 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001093 * Get the list of inputs to be collected from the user when this action is sent.
1094 * May return null if no remote inputs were added.
1095 */
1096 public RemoteInput[] getRemoteInputs() {
1097 return mRemoteInputs;
1098 }
1099
1100 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001101 * Builder class for {@link Action} objects.
1102 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001103 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001104 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001105 private final CharSequence mTitle;
1106 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001107 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001108 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001109 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001110
1111 /**
1112 * Construct a new builder for {@link Action} object.
1113 * @param icon icon to show for this action
1114 * @param title the title of the action
1115 * @param intent the {@link PendingIntent} to fire when users trigger this action
1116 */
Dan Sandler86647982015-05-13 23:41:13 -04001117 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001118 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001119 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001120 }
1121
1122 /**
1123 * Construct a new builder for {@link Action} object.
1124 * @param icon icon to show for this action
1125 * @param title the title of the action
1126 * @param intent the {@link PendingIntent} to fire when users trigger this action
1127 */
1128 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001129 this(icon, title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001130 }
1131
1132 /**
1133 * Construct a new builder for {@link Action} object using the fields from an
1134 * {@link Action}.
1135 * @param action the action to read fields from.
1136 */
1137 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001138 this(action.getIcon(), action.title, action.actionIntent,
1139 new Bundle(action.mExtras), action.getRemoteInputs(),
1140 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001141 }
1142
Dan Sandler86647982015-05-13 23:41:13 -04001143 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001144 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001145 mIcon = icon;
1146 mTitle = title;
1147 mIntent = intent;
1148 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001149 if (remoteInputs != null) {
1150 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1151 Collections.addAll(mRemoteInputs, remoteInputs);
1152 }
Adrian Roos7af53622016-10-12 13:44:05 -07001153 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001154 }
1155
1156 /**
1157 * Merge additional metadata into this builder.
1158 *
1159 * <p>Values within the Bundle will replace existing extras values in this Builder.
1160 *
1161 * @see Notification.Action#extras
1162 */
1163 public Builder addExtras(Bundle extras) {
1164 if (extras != null) {
1165 mExtras.putAll(extras);
1166 }
1167 return this;
1168 }
1169
1170 /**
1171 * Get the metadata Bundle used by this Builder.
1172 *
1173 * <p>The returned Bundle is shared with this Builder.
1174 */
1175 public Bundle getExtras() {
1176 return mExtras;
1177 }
1178
1179 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001180 * Add an input to be collected from the user when this action is sent.
1181 * Response values can be retrieved from the fired intent by using the
1182 * {@link RemoteInput#getResultsFromIntent} function.
1183 * @param remoteInput a {@link RemoteInput} to add to the action
1184 * @return this object for method chaining
1185 */
1186 public Builder addRemoteInput(RemoteInput remoteInput) {
1187 if (mRemoteInputs == null) {
1188 mRemoteInputs = new ArrayList<RemoteInput>();
1189 }
1190 mRemoteInputs.add(remoteInput);
1191 return this;
1192 }
1193
1194 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001195 * Set whether the platform should automatically generate possible replies to add to
1196 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1197 * {@link RemoteInput}, this has no effect.
1198 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1199 * otherwise
1200 * @return this object for method chaining
1201 * The default value is {@code false}
1202 */
1203 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1204 mAllowGeneratedReplies = allowGeneratedReplies;
1205 return this;
1206 }
1207
1208 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001209 * Apply an extender to this action builder. Extenders may be used to add
1210 * metadata or change options on this builder.
1211 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001212 public Builder extend(Extender extender) {
1213 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001214 return this;
1215 }
1216
1217 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001218 * Combine all of the options that have been set and return a new {@link Action}
1219 * object.
1220 * @return the built action
1221 */
1222 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001223 RemoteInput[] remoteInputs = mRemoteInputs != null
1224 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001225 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1226 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001227 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001228 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001229
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001230 @Override
1231 public Action clone() {
1232 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001233 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001234 title,
1235 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001236 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001237 getRemoteInputs(),
1238 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001239 }
1240 @Override
1241 public int describeContents() {
1242 return 0;
1243 }
1244 @Override
1245 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001246 final Icon ic = getIcon();
1247 if (ic != null) {
1248 out.writeInt(1);
1249 ic.writeToParcel(out, 0);
1250 } else {
1251 out.writeInt(0);
1252 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001253 TextUtils.writeToParcel(title, out, flags);
1254 if (actionIntent != null) {
1255 out.writeInt(1);
1256 actionIntent.writeToParcel(out, flags);
1257 } else {
1258 out.writeInt(0);
1259 }
Griff Hazen959591e2014-05-15 22:26:18 -07001260 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001261 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001262 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001263 }
Griff Hazen959591e2014-05-15 22:26:18 -07001264 public static final Parcelable.Creator<Action> CREATOR =
1265 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001266 public Action createFromParcel(Parcel in) {
1267 return new Action(in);
1268 }
1269 public Action[] newArray(int size) {
1270 return new Action[size];
1271 }
1272 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001273
1274 /**
1275 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1276 * metadata or change options on an action builder.
1277 */
1278 public interface Extender {
1279 /**
1280 * Apply this extender to a notification action builder.
1281 * @param builder the builder to be modified.
1282 * @return the build object for chaining.
1283 */
1284 public Builder extend(Builder builder);
1285 }
1286
1287 /**
1288 * Wearable extender for notification actions. To add extensions to an action,
1289 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1290 * the {@code WearableExtender()} constructor and apply it to a
1291 * {@link android.app.Notification.Action.Builder} using
1292 * {@link android.app.Notification.Action.Builder#extend}.
1293 *
1294 * <pre class="prettyprint">
1295 * Notification.Action action = new Notification.Action.Builder(
1296 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001297 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001298 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001299 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001300 */
1301 public static final class WearableExtender implements Extender {
1302 /** Notification action extra which contains wearable extensions */
1303 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1304
Pete Gastaf6781d2014-10-07 15:17:05 -04001305 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001306 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001307 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1308 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1309 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001310
1311 // Flags bitwise-ored to mFlags
1312 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001313 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001314 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001315
1316 // Default value for flags integer
1317 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1318
1319 private int mFlags = DEFAULT_FLAGS;
1320
Pete Gastaf6781d2014-10-07 15:17:05 -04001321 private CharSequence mInProgressLabel;
1322 private CharSequence mConfirmLabel;
1323 private CharSequence mCancelLabel;
1324
Griff Hazen61a9e862014-05-22 16:05:19 -07001325 /**
1326 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1327 * options.
1328 */
1329 public WearableExtender() {
1330 }
1331
1332 /**
1333 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1334 * wearable options present in an existing notification action.
1335 * @param action the notification action to inspect.
1336 */
1337 public WearableExtender(Action action) {
1338 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1339 if (wearableBundle != null) {
1340 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001341 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1342 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1343 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001344 }
1345 }
1346
1347 /**
1348 * Apply wearable extensions to a notification action that is being built. This is
1349 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1350 * method of {@link android.app.Notification.Action.Builder}.
1351 */
1352 @Override
1353 public Action.Builder extend(Action.Builder builder) {
1354 Bundle wearableBundle = new Bundle();
1355
1356 if (mFlags != DEFAULT_FLAGS) {
1357 wearableBundle.putInt(KEY_FLAGS, mFlags);
1358 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001359 if (mInProgressLabel != null) {
1360 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1361 }
1362 if (mConfirmLabel != null) {
1363 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1364 }
1365 if (mCancelLabel != null) {
1366 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1367 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001368
1369 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1370 return builder;
1371 }
1372
1373 @Override
1374 public WearableExtender clone() {
1375 WearableExtender that = new WearableExtender();
1376 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001377 that.mInProgressLabel = this.mInProgressLabel;
1378 that.mConfirmLabel = this.mConfirmLabel;
1379 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001380 return that;
1381 }
1382
1383 /**
1384 * Set whether this action is available when the wearable device is not connected to
1385 * a companion device. The user can still trigger this action when the wearable device is
1386 * offline, but a visual hint will indicate that the action may not be available.
1387 * Defaults to true.
1388 */
1389 public WearableExtender setAvailableOffline(boolean availableOffline) {
1390 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1391 return this;
1392 }
1393
1394 /**
1395 * Get whether this action is available when the wearable device is not connected to
1396 * a companion device. The user can still trigger this action when the wearable device is
1397 * offline, but a visual hint will indicate that the action may not be available.
1398 * Defaults to true.
1399 */
1400 public boolean isAvailableOffline() {
1401 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1402 }
1403
1404 private void setFlag(int mask, boolean value) {
1405 if (value) {
1406 mFlags |= mask;
1407 } else {
1408 mFlags &= ~mask;
1409 }
1410 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001411
1412 /**
1413 * Set a label to display while the wearable is preparing to automatically execute the
1414 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1415 *
1416 * @param label the label to display while the action is being prepared to execute
1417 * @return this object for method chaining
1418 */
1419 public WearableExtender setInProgressLabel(CharSequence label) {
1420 mInProgressLabel = label;
1421 return this;
1422 }
1423
1424 /**
1425 * Get the label to display while the wearable is preparing to automatically execute
1426 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1427 *
1428 * @return the label to display while the action is being prepared to execute
1429 */
1430 public CharSequence getInProgressLabel() {
1431 return mInProgressLabel;
1432 }
1433
1434 /**
1435 * Set a label to display to confirm that the action should be executed.
1436 * This is usually an imperative verb like "Send".
1437 *
1438 * @param label the label to confirm the action should be executed
1439 * @return this object for method chaining
1440 */
1441 public WearableExtender setConfirmLabel(CharSequence label) {
1442 mConfirmLabel = label;
1443 return this;
1444 }
1445
1446 /**
1447 * Get the label to display to confirm that the action should be executed.
1448 * This is usually an imperative verb like "Send".
1449 *
1450 * @return the label to confirm the action should be executed
1451 */
1452 public CharSequence getConfirmLabel() {
1453 return mConfirmLabel;
1454 }
1455
1456 /**
1457 * Set a label to display to cancel the action.
1458 * This is usually an imperative verb, like "Cancel".
1459 *
1460 * @param label the label to display to cancel the action
1461 * @return this object for method chaining
1462 */
1463 public WearableExtender setCancelLabel(CharSequence label) {
1464 mCancelLabel = label;
1465 return this;
1466 }
1467
1468 /**
1469 * Get the label to display to cancel the action.
1470 * This is usually an imperative verb like "Cancel".
1471 *
1472 * @return the label to display to cancel the action
1473 */
1474 public CharSequence getCancelLabel() {
1475 return mCancelLabel;
1476 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001477
1478 /**
1479 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1480 * platform that it can generate the appropriate transitions.
1481 * @param hintLaunchesActivity {@code true} if the content intent will launch
1482 * an activity and transitions should be generated, false otherwise.
1483 * @return this object for method chaining
1484 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001485 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001486 boolean hintLaunchesActivity) {
1487 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1488 return this;
1489 }
1490
1491 /**
1492 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1493 * platform that it can generate the appropriate transitions
1494 * @return {@code true} if the content intent will launch an activity and transitions
1495 * should be generated, false otherwise. The default value is {@code false} if this was
1496 * never set.
1497 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001498 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001499 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1500 }
Alex Hills9f087612016-06-07 09:08:59 -04001501
1502 /**
1503 * Set a hint that this Action should be displayed inline.
1504 *
1505 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1506 * otherwise
1507 * @return this object for method chaining
1508 */
1509 public WearableExtender setHintDisplayActionInline(
1510 boolean hintDisplayInline) {
1511 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1512 return this;
1513 }
1514
1515 /**
1516 * Get a hint that this Action should be displayed inline.
1517 *
1518 * @return {@code true} if the Action should be displayed inline, {@code false}
1519 * otherwise. The default value is {@code false} if this was never set.
1520 */
1521 public boolean getHintDisplayActionInline() {
1522 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1523 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001524 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001525 }
1526
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001527 /**
1528 * Array of all {@link Action} structures attached to this notification by
1529 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1530 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1531 * interface for invoking actions.
1532 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001533 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001534
1535 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001536 * Replacement version of this notification whose content will be shown
1537 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1538 * and {@link #VISIBILITY_PUBLIC}.
1539 */
1540 public Notification publicVersion;
1541
1542 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001543 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001544 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 */
1546 public Notification()
1547 {
1548 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001549 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001550 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552
1553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 * @hide
1555 */
1556 public Notification(Context context, int icon, CharSequence tickerText, long when,
1557 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1558 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001559 new Builder(context)
1560 .setWhen(when)
1561 .setSmallIcon(icon)
1562 .setTicker(tickerText)
1563 .setContentTitle(contentTitle)
1564 .setContentText(contentText)
1565 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1566 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568
1569 /**
1570 * Constructs a Notification object with the information needed to
1571 * have a status bar icon without the standard expanded view.
1572 *
1573 * @param icon The resource id of the icon to put in the status bar.
1574 * @param tickerText The text that flows by in the status bar when the notification first
1575 * activates.
1576 * @param when The time to show in the time field. In the System.currentTimeMillis
1577 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001578 *
1579 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001581 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 public Notification(int icon, CharSequence tickerText, long when)
1583 {
1584 this.icon = icon;
1585 this.tickerText = tickerText;
1586 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001587 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
1589
1590 /**
1591 * Unflatten the notification from a parcel.
1592 */
Svet Ganovddb94882016-06-23 19:55:24 -07001593 @SuppressWarnings("unchecked")
1594 public Notification(Parcel parcel) {
1595 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1596 // intents in extras are always written as the last entry.
1597 readFromParcelImpl(parcel);
1598 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001599 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001600 }
1601
1602 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 {
1604 int version = parcel.readInt();
1605
1606 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001607 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001608 if (parcel.readInt() != 0) {
1609 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001610 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1611 icon = mSmallIcon.getResId();
1612 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 number = parcel.readInt();
1615 if (parcel.readInt() != 0) {
1616 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1617 }
1618 if (parcel.readInt() != 0) {
1619 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1620 }
1621 if (parcel.readInt() != 0) {
1622 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1623 }
1624 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001625 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001626 }
1627 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1629 }
Joe Onorato561d3852010-11-20 18:09:34 -08001630 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001631 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 defaults = parcel.readInt();
1634 flags = parcel.readInt();
1635 if (parcel.readInt() != 0) {
1636 sound = Uri.CREATOR.createFromParcel(parcel);
1637 }
1638
1639 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001640 if (parcel.readInt() != 0) {
1641 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 vibrate = parcel.createLongArray();
1644 ledARGB = parcel.readInt();
1645 ledOnMS = parcel.readInt();
1646 ledOffMS = parcel.readInt();
1647 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001648
1649 if (parcel.readInt() != 0) {
1650 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1651 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001652
1653 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001654
John Spurlockfd7f1e02014-03-18 16:41:57 -04001655 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001656
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001657 mGroupKey = parcel.readString();
1658
1659 mSortKey = parcel.readString();
1660
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001661 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001662
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001663 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1664
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001665 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001666 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1667 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001668
Chris Wren8fd39ec2014-02-27 17:43:26 -05001669 if (parcel.readInt() != 0) {
1670 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1671 }
1672
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001673 visibility = parcel.readInt();
1674
1675 if (parcel.readInt() != 0) {
1676 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1677 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001678
1679 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681
Andy Stadler110988c2010-12-03 14:29:16 -08001682 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001683 public Notification clone() {
1684 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001685 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001686 return that;
1687 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001688
Daniel Sandler1a497d32013-04-18 14:52:45 -04001689 /**
1690 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1691 * of this into that.
1692 * @hide
1693 */
1694 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001695 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001696 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001697 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001698 that.number = this.number;
1699
1700 // PendingIntents are global, so there's no reason (or way) to clone them.
1701 that.contentIntent = this.contentIntent;
1702 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001703 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001704
1705 if (this.tickerText != null) {
1706 that.tickerText = this.tickerText.toString();
1707 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001708 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001709 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001710 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001711 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001712 that.contentView = this.contentView.clone();
1713 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001714 if (heavy && this.mLargeIcon != null) {
1715 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001716 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001717 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001718 that.sound = this.sound; // android.net.Uri is immutable
1719 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001720 if (this.audioAttributes != null) {
1721 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1722 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001723
1724 final long[] vibrate = this.vibrate;
1725 if (vibrate != null) {
1726 final int N = vibrate.length;
1727 final long[] vib = that.vibrate = new long[N];
1728 System.arraycopy(vibrate, 0, vib, 0, N);
1729 }
1730
1731 that.ledARGB = this.ledARGB;
1732 that.ledOnMS = this.ledOnMS;
1733 that.ledOffMS = this.ledOffMS;
1734 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001735
Joe Onorato18e69df2010-05-17 22:26:12 -07001736 that.flags = this.flags;
1737
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001738 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001739
John Spurlockfd7f1e02014-03-18 16:41:57 -04001740 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001741
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001742 that.mGroupKey = this.mGroupKey;
1743
1744 that.mSortKey = this.mSortKey;
1745
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001746 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001747 try {
1748 that.extras = new Bundle(this.extras);
1749 // will unparcel
1750 that.extras.size();
1751 } catch (BadParcelableException e) {
1752 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1753 that.extras = null;
1754 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001755 }
1756
Felipe Lemedd85da62016-06-28 11:29:54 -07001757 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1758 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001759 }
1760
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001761 if (this.actions != null) {
1762 that.actions = new Action[this.actions.length];
1763 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08001764 if ( this.actions[i] != null) {
1765 that.actions[i] = this.actions[i].clone();
1766 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001767 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001768 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001769
Daniel Sandler1a497d32013-04-18 14:52:45 -04001770 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001771 that.bigContentView = this.bigContentView.clone();
1772 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001773
Chris Wren8fd39ec2014-02-27 17:43:26 -05001774 if (heavy && this.headsUpContentView != null) {
1775 that.headsUpContentView = this.headsUpContentView.clone();
1776 }
1777
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001778 that.visibility = this.visibility;
1779
1780 if (this.publicVersion != null) {
1781 that.publicVersion = new Notification();
1782 this.publicVersion.cloneInto(that.publicVersion, heavy);
1783 }
1784
Dan Sandler26e81cf2014-05-06 10:01:27 -04001785 that.color = this.color;
1786
Daniel Sandler1a497d32013-04-18 14:52:45 -04001787 if (!heavy) {
1788 that.lightenPayload(); // will clean out extras
1789 }
1790 }
1791
1792 /**
1793 * Removes heavyweight parts of the Notification object for archival or for sending to
1794 * listeners when the full contents are not necessary.
1795 * @hide
1796 */
1797 public final void lightenPayload() {
1798 tickerView = null;
1799 contentView = null;
1800 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001801 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001802 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001803 if (extras != null && !extras.isEmpty()) {
1804 final Set<String> keyset = extras.keySet();
1805 final int N = keyset.size();
1806 final String[] keys = keyset.toArray(new String[N]);
1807 for (int i=0; i<N; i++) {
1808 final String key = keys[i];
1809 final Object obj = extras.get(key);
1810 if (obj != null &&
1811 ( obj instanceof Parcelable
1812 || obj instanceof Parcelable[]
1813 || obj instanceof SparseArray
1814 || obj instanceof ArrayList)) {
1815 extras.remove(key);
1816 }
1817 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001818 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001819 }
1820
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001821 /**
1822 * Make sure this CharSequence is safe to put into a bundle, which basically
1823 * means it had better not be some custom Parcelable implementation.
1824 * @hide
1825 */
1826 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001827 if (cs == null) return cs;
1828 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1829 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1830 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001831 if (cs instanceof Parcelable) {
1832 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1833 + " instance is a custom Parcelable and not allowed in Notification");
1834 return cs.toString();
1835 }
Selim Cinek60a54252016-02-26 17:03:25 -08001836 return removeTextSizeSpans(cs);
1837 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001838
Selim Cinek60a54252016-02-26 17:03:25 -08001839 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1840 if (charSequence instanceof Spanned) {
1841 Spanned ss = (Spanned) charSequence;
1842 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1843 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1844 for (Object span : spans) {
1845 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001846 if (resultSpan instanceof CharacterStyle) {
1847 resultSpan = ((CharacterStyle) span).getUnderlying();
1848 }
1849 if (resultSpan instanceof TextAppearanceSpan) {
1850 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001851 resultSpan = new TextAppearanceSpan(
1852 originalSpan.getFamily(),
1853 originalSpan.getTextStyle(),
1854 -1,
1855 originalSpan.getTextColor(),
1856 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001857 } else if (resultSpan instanceof RelativeSizeSpan
1858 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001859 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001860 } else {
1861 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001862 }
1863 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1864 ss.getSpanFlags(span));
1865 }
1866 return builder;
1867 }
1868 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001869 }
1870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 public int describeContents() {
1872 return 0;
1873 }
1874
1875 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001876 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 */
Svet Ganovddb94882016-06-23 19:55:24 -07001878 public void writeToParcel(Parcel parcel, int flags) {
1879 // We need to mark all pending intents getting into the notification
1880 // system as being put there to later allow the notification ranker
1881 // to launch them and by doing so add the app to the battery saver white
1882 // list for a short period of time. The problem is that the system
1883 // cannot look into the extras as there may be parcelables there that
1884 // the platform does not know how to handle. To go around that we have
1885 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001886 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001887 if (collectPendingIntents) {
1888 PendingIntent.setOnMarshaledListener(
1889 (PendingIntent intent, Parcel out, int outFlags) -> {
1890 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001891 if (allPendingIntents == null) {
1892 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001893 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001894 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001895 }
1896 });
1897 }
1898 try {
1899 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1900 // want to intercept all pending events written to the pacel.
1901 writeToParcelImpl(parcel, flags);
1902 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001903 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001904 } finally {
1905 if (collectPendingIntents) {
1906 PendingIntent.setOnMarshaledListener(null);
1907 }
1908 }
1909 }
1910
1911 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 parcel.writeInt(1);
1913
1914 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001915 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001916 if (mSmallIcon == null && icon != 0) {
1917 // you snuck an icon in here without using the builder; let's try to keep it
1918 mSmallIcon = Icon.createWithResource("", icon);
1919 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001920 if (mSmallIcon != null) {
1921 parcel.writeInt(1);
1922 mSmallIcon.writeToParcel(parcel, 0);
1923 } else {
1924 parcel.writeInt(0);
1925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 parcel.writeInt(number);
1927 if (contentIntent != null) {
1928 parcel.writeInt(1);
1929 contentIntent.writeToParcel(parcel, 0);
1930 } else {
1931 parcel.writeInt(0);
1932 }
1933 if (deleteIntent != null) {
1934 parcel.writeInt(1);
1935 deleteIntent.writeToParcel(parcel, 0);
1936 } else {
1937 parcel.writeInt(0);
1938 }
1939 if (tickerText != null) {
1940 parcel.writeInt(1);
1941 TextUtils.writeToParcel(tickerText, parcel, flags);
1942 } else {
1943 parcel.writeInt(0);
1944 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001945 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001946 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001947 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001948 } else {
1949 parcel.writeInt(0);
1950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 if (contentView != null) {
1952 parcel.writeInt(1);
1953 contentView.writeToParcel(parcel, 0);
1954 } else {
1955 parcel.writeInt(0);
1956 }
Selim Cinek279fa862016-06-14 10:57:25 -07001957 if (mLargeIcon == null && largeIcon != null) {
1958 // you snuck an icon in here without using the builder; let's try to keep it
1959 mLargeIcon = Icon.createWithBitmap(largeIcon);
1960 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001961 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001962 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001963 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001964 } else {
1965 parcel.writeInt(0);
1966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967
1968 parcel.writeInt(defaults);
1969 parcel.writeInt(this.flags);
1970
1971 if (sound != null) {
1972 parcel.writeInt(1);
1973 sound.writeToParcel(parcel, 0);
1974 } else {
1975 parcel.writeInt(0);
1976 }
1977 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001978
1979 if (audioAttributes != null) {
1980 parcel.writeInt(1);
1981 audioAttributes.writeToParcel(parcel, 0);
1982 } else {
1983 parcel.writeInt(0);
1984 }
1985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 parcel.writeLongArray(vibrate);
1987 parcel.writeInt(ledARGB);
1988 parcel.writeInt(ledOnMS);
1989 parcel.writeInt(ledOffMS);
1990 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001991
1992 if (fullScreenIntent != null) {
1993 parcel.writeInt(1);
1994 fullScreenIntent.writeToParcel(parcel, 0);
1995 } else {
1996 parcel.writeInt(0);
1997 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001998
1999 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002000
John Spurlockfd7f1e02014-03-18 16:41:57 -04002001 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002002
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002003 parcel.writeString(mGroupKey);
2004
2005 parcel.writeString(mSortKey);
2006
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002007 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002008
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002009 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002010
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002011 if (bigContentView != null) {
2012 parcel.writeInt(1);
2013 bigContentView.writeToParcel(parcel, 0);
2014 } else {
2015 parcel.writeInt(0);
2016 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002017
Chris Wren8fd39ec2014-02-27 17:43:26 -05002018 if (headsUpContentView != null) {
2019 parcel.writeInt(1);
2020 headsUpContentView.writeToParcel(parcel, 0);
2021 } else {
2022 parcel.writeInt(0);
2023 }
2024
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002025 parcel.writeInt(visibility);
2026
2027 if (publicVersion != null) {
2028 parcel.writeInt(1);
2029 publicVersion.writeToParcel(parcel, 0);
2030 } else {
2031 parcel.writeInt(0);
2032 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002033
2034 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036
2037 /**
2038 * Parcelable.Creator that instantiates Notification objects
2039 */
2040 public static final Parcelable.Creator<Notification> CREATOR
2041 = new Parcelable.Creator<Notification>()
2042 {
2043 public Notification createFromParcel(Parcel parcel)
2044 {
2045 return new Notification(parcel);
2046 }
2047
2048 public Notification[] newArray(int size)
2049 {
2050 return new Notification[size];
2051 }
2052 };
2053
2054 /**
2055 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2056 * layout.
2057 *
2058 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2059 * in the view.</p>
2060 * @param context The context for your application / activity.
2061 * @param contentTitle The title that goes in the expanded entry.
2062 * @param contentText The text that goes in the expanded entry.
2063 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2064 * If this is an activity, it must include the
2065 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002066 * that you take care of task management as described in the
2067 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2068 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002069 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002070 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002071 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002073 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 public void setLatestEventInfo(Context context,
2075 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002076 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2077 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2078 new Throwable());
2079 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002080
Selim Cinek4ac6f602016-06-13 15:47:03 -07002081 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2082 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2083 }
2084
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002085 // ensure that any information already set directly is preserved
2086 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002087
2088 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002090 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002093 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002095 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002096
2097 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 }
2099
Julia Reynoldsda303542015-11-23 14:00:20 -05002100 /**
2101 * @hide
2102 */
2103 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002104 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2105 }
2106
2107 /**
2108 * @hide
2109 */
2110 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2111 Notification notification) {
2112 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2113 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002114 }
2115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 @Override
2117 public String toString() {
2118 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002119 sb.append("Notification(pri=");
2120 sb.append(priority);
2121 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002122 if (contentView != null) {
2123 sb.append(contentView.getPackage());
2124 sb.append("/0x");
2125 sb.append(Integer.toHexString(contentView.getLayoutId()));
2126 } else {
2127 sb.append("null");
2128 }
2129 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002130 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2131 sb.append("default");
2132 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 int N = this.vibrate.length-1;
2134 sb.append("[");
2135 for (int i=0; i<N; i++) {
2136 sb.append(this.vibrate[i]);
2137 sb.append(',');
2138 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002139 if (N != -1) {
2140 sb.append(this.vibrate[N]);
2141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 } else {
2144 sb.append("null");
2145 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002146 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002147 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002149 } else if (this.sound != null) {
2150 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 } else {
2152 sb.append("null");
2153 }
Chris Wren365b6d32015-07-16 10:39:26 -04002154 if (this.tickerText != null) {
2155 sb.append(" tick");
2156 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002157 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002159 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002160 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002161 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002162 if (this.category != null) {
2163 sb.append(" category=");
2164 sb.append(this.category);
2165 }
2166 if (this.mGroupKey != null) {
2167 sb.append(" groupKey=");
2168 sb.append(this.mGroupKey);
2169 }
2170 if (this.mSortKey != null) {
2171 sb.append(" sortKey=");
2172 sb.append(this.mSortKey);
2173 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002174 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002175 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002176 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002177 }
2178 sb.append(" vis=");
2179 sb.append(visibilityToString(this.visibility));
2180 if (this.publicVersion != null) {
2181 sb.append(" publicVersion=");
2182 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002183 }
2184 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 return sb.toString();
2186 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002187
Dan Sandler1b718782014-07-18 12:43:45 -04002188 /**
2189 * {@hide}
2190 */
2191 public static String visibilityToString(int vis) {
2192 switch (vis) {
2193 case VISIBILITY_PRIVATE:
2194 return "PRIVATE";
2195 case VISIBILITY_PUBLIC:
2196 return "PUBLIC";
2197 case VISIBILITY_SECRET:
2198 return "SECRET";
2199 default:
2200 return "UNKNOWN(" + String.valueOf(vis) + ")";
2201 }
2202 }
2203
Joe Onoratocb109a02011-01-18 17:57:41 -08002204 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002205 * {@hide}
2206 */
2207 public static String priorityToString(@Priority int pri) {
2208 switch (pri) {
2209 case PRIORITY_MIN:
2210 return "MIN";
2211 case PRIORITY_LOW:
2212 return "LOW";
2213 case PRIORITY_DEFAULT:
2214 return "DEFAULT";
2215 case PRIORITY_HIGH:
2216 return "HIGH";
2217 case PRIORITY_MAX:
2218 return "MAX";
2219 default:
2220 return "UNKNOWN(" + String.valueOf(pri) + ")";
2221 }
2222 }
2223
2224 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002225 * The small icon representing this notification in the status bar and content view.
2226 *
2227 * @return the small icon representing this notification.
2228 *
2229 * @see Builder#getSmallIcon()
2230 * @see Builder#setSmallIcon(Icon)
2231 */
2232 public Icon getSmallIcon() {
2233 return mSmallIcon;
2234 }
2235
2236 /**
2237 * Used when notifying to clean up legacy small icons.
2238 * @hide
2239 */
2240 public void setSmallIcon(Icon icon) {
2241 mSmallIcon = icon;
2242 }
2243
2244 /**
2245 * The large icon shown in this notification's content view.
2246 * @see Builder#getLargeIcon()
2247 * @see Builder#setLargeIcon(Icon)
2248 */
2249 public Icon getLargeIcon() {
2250 return mLargeIcon;
2251 }
2252
2253 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002254 * @hide
2255 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002256 public boolean isGroupSummary() {
2257 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2258 }
2259
2260 /**
2261 * @hide
2262 */
2263 public boolean isGroupChild() {
2264 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2265 }
2266
2267 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002268 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002269 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002270 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002271 * content views using the platform's notification layout template. If your app supports
2272 * versions of Android as old as API level 4, you can instead use
2273 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2274 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2275 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002276 *
Scott Main183bf112012-08-13 19:12:13 -07002277 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002278 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002280 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002281 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2282 * .setContentText(subject)
2283 * .setSmallIcon(R.drawable.new_mail)
2284 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002285 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002286 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002287 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002288 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002289 /**
2290 * @hide
2291 */
2292 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2293 "android.rebuild.contentViewActionCount";
2294 /**
2295 * @hide
2296 */
2297 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2298 = "android.rebuild.bigViewActionCount";
2299 /**
2300 * @hide
2301 */
2302 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2303 = "android.rebuild.hudViewActionCount";
2304
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002305 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002306
Joe Onorato46439ce2010-11-19 13:56:21 -08002307 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002308 private Notification mN;
2309 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002310 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002311 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2312 private ArrayList<String> mPersonList = new ArrayList<String>();
2313 private NotificationColorUtil mColorUtil;
2314 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002315
2316 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002317 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2318 */
2319 private int mCachedContrastColor = COLOR_INVALID;
2320 private int mCachedContrastColorIsFor = COLOR_INVALID;
2321
2322 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002323 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002324 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002325
2326 * <table>
2327 * <tr><th align=right>priority</th>
2328 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2329 * <tr><th align=right>when</th>
2330 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2331 * <tr><th align=right>audio stream</th>
2332 * <td>{@link #STREAM_DEFAULT}</td></tr>
2333 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002334 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002335
2336 * @param context
2337 * A {@link Context} that will be used by the Builder to construct the
2338 * RemoteViews. The Context will not be held past the lifetime of this Builder
2339 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002340 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002341 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002342 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002343 }
2344
Joe Onoratocb109a02011-01-18 17:57:41 -08002345 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002346 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002347 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002348 public Builder(Context context, Notification toAdopt) {
2349 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002350
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002351 if (toAdopt == null) {
2352 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002353 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2354 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2355 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002356 mN.priority = PRIORITY_DEFAULT;
2357 mN.visibility = VISIBILITY_PRIVATE;
2358 } else {
2359 mN = toAdopt;
2360 if (mN.actions != null) {
2361 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002362 }
2363
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002364 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2365 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2366 }
2367
Selim Cinek4ac6f602016-06-13 15:47:03 -07002368 if (mN.getSmallIcon() == null && mN.icon != 0) {
2369 setSmallIcon(mN.icon);
2370 }
2371
2372 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2373 setLargeIcon(mN.largeIcon);
2374 }
2375
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002376 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2377 if (!TextUtils.isEmpty(templateClass)) {
2378 final Class<? extends Style> styleClass
2379 = getNotificationStyleClass(templateClass);
2380 if (styleClass == null) {
2381 Log.d(TAG, "Unknown style class: " + templateClass);
2382 } else {
2383 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002384 final Constructor<? extends Style> ctor =
2385 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002386 ctor.setAccessible(true);
2387 final Style style = ctor.newInstance();
2388 style.restoreFromExtras(mN.extras);
2389
2390 if (style != null) {
2391 setStyle(style);
2392 }
2393 } catch (Throwable t) {
2394 Log.e(TAG, "Could not create Style", t);
2395 }
2396 }
2397 }
2398
2399 }
2400 }
2401
2402 private NotificationColorUtil getColorUtil() {
2403 if (!mColorUtilInited) {
2404 mColorUtilInited = true;
2405 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2406 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002407 }
2408 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002409 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002410 }
2411
2412 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002413 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002414 *
2415 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2416 * shown anymore by default and must be opted into by using
2417 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002418 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002419 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002420 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002421 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002422 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002423 return this;
2424 }
2425
Joe Onoratocb109a02011-01-18 17:57:41 -08002426 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002427 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002428 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002429 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2430 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002431 */
2432 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002433 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002434 return this;
2435 }
2436
2437 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002438 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002439 *
2440 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002441 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002442 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002443 * Useful when showing an elapsed time (like an ongoing phone call).
2444 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002445 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002446 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002447 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002448 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002449 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002450 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002451 */
2452 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002453 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002454 return this;
2455 }
2456
2457 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002458 * Sets the Chronometer to count down instead of counting up.
2459 *
2460 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2461 * If it isn't set the chronometer will count up.
2462 *
2463 * @see #setUsesChronometer(boolean)
2464 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002465 public Builder setChronometerCountDown(boolean countDown) {
2466 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002467 return this;
2468 }
2469
2470 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002471 * Set the small icon resource, which will be used to represent the notification in the
2472 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002473 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002474
2475 * The platform template for the expanded view will draw this icon in the left, unless a
2476 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2477 * icon will be moved to the right-hand side.
2478 *
2479
2480 * @param icon
2481 * A resource ID in the application's package of the drawable to use.
2482 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002483 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002484 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002485 return setSmallIcon(icon != 0
2486 ? Icon.createWithResource(mContext, icon)
2487 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002488 }
2489
Joe Onoratocb109a02011-01-18 17:57:41 -08002490 /**
2491 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2492 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2493 * LevelListDrawable}.
2494 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002495 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002496 * @param level The level to use for the icon.
2497 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002498 * @see Notification#icon
2499 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002500 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002501 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002502 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002503 return setSmallIcon(icon);
2504 }
2505
2506 /**
2507 * Set the small icon, which will be used to represent the notification in the
2508 * status bar and content view (unless overriden there by a
2509 * {@link #setLargeIcon(Bitmap) large icon}).
2510 *
2511 * @param icon An Icon object to use.
2512 * @see Notification#icon
2513 */
2514 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002515 mN.setSmallIcon(icon);
2516 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2517 mN.icon = icon.getResId();
2518 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002519 return this;
2520 }
2521
Joe Onoratocb109a02011-01-18 17:57:41 -08002522 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002523 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002525 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002526 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002527 return this;
2528 }
2529
Joe Onoratocb109a02011-01-18 17:57:41 -08002530 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002531 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002532 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002533 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002534 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002535 return this;
2536 }
2537
Joe Onoratocb109a02011-01-18 17:57:41 -08002538 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002539 * This provides some additional information that is displayed in the notification. No
2540 * guarantees are given where exactly it is displayed.
2541 *
2542 * <p>This information should only be provided if it provides an essential
2543 * benefit to the understanding of the notification. The more text you provide the
2544 * less readable it becomes. For example, an email client should only provide the account
2545 * name here if more than one email account has been added.</p>
2546 *
2547 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2548 * notification header area.
2549 *
2550 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2551 * this will be shown in the third line of text in the platform notification template.
2552 * You should not be using {@link #setProgress(int, int, boolean)} at the
2553 * same time on those versions; they occupy the same place.
2554 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002555 */
2556 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002557 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002558 return this;
2559 }
2560
2561 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002562 * Set the remote input history.
2563 *
2564 * This should be set to the most recent inputs that have been sent
2565 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2566 * longer relevant (e.g. for chat notifications once the other party has responded).
2567 *
2568 * The most recent input must be stored at the 0 index, the second most recent at the
2569 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2570 * and how much of each individual input is shown.
2571 *
2572 * <p>Note: The reply text will only be shown on notifications that have least one action
2573 * with a {@code RemoteInput}.</p>
2574 */
2575 public Builder setRemoteInputHistory(CharSequence[] text) {
2576 if (text == null) {
2577 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2578 } else {
2579 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2580 CharSequence[] safe = new CharSequence[N];
2581 for (int i = 0; i < N; i++) {
2582 safe[i] = safeCharSequence(text[i]);
2583 }
2584 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2585 }
2586 return this;
2587 }
2588
2589 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002590 * Set the large number at the right-hand side of the notification. This is
2591 * equivalent to setContentInfo, although it might show the number in a different
2592 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002593 *
2594 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002595 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002596 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002597 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002598 return this;
2599 }
2600
Joe Onoratocb109a02011-01-18 17:57:41 -08002601 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002602 * A small piece of additional information pertaining to this notification.
2603 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002604 * The platform template will draw this on the last line of the notification, at the far
2605 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002606 *
2607 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2608 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2609 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002610 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002611 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002612 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002613 return this;
2614 }
2615
Joe Onoratocb109a02011-01-18 17:57:41 -08002616 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 * Set the progress this notification represents.
2618 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002619 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002620 */
2621 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002622 mN.extras.putInt(EXTRA_PROGRESS, progress);
2623 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2624 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002625 return this;
2626 }
2627
2628 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002629 * Supply a custom RemoteViews to use instead of the platform template.
2630 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002631 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002632 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002633 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002634 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002635 return setCustomContentView(views);
2636 }
2637
2638 /**
2639 * Supply custom RemoteViews to use instead of the platform template.
2640 *
2641 * This will override the layout that would otherwise be constructed by this Builder
2642 * object.
2643 */
2644 public Builder setCustomContentView(RemoteViews contentView) {
2645 mN.contentView = contentView;
2646 return this;
2647 }
2648
2649 /**
2650 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2651 *
2652 * This will override the expanded layout that would otherwise be constructed by this
2653 * Builder object.
2654 */
2655 public Builder setCustomBigContentView(RemoteViews contentView) {
2656 mN.bigContentView = contentView;
2657 return this;
2658 }
2659
2660 /**
2661 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2662 *
2663 * This will override the heads-up layout that would otherwise be constructed by this
2664 * Builder object.
2665 */
2666 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2667 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002668 return this;
2669 }
2670
Joe Onoratocb109a02011-01-18 17:57:41 -08002671 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002672 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2673 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002674 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2675 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2676 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002677 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002678 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002679 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002680 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002681 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002682 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002683 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002684 return this;
2685 }
2686
Joe Onoratocb109a02011-01-18 17:57:41 -08002687 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002688 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2689 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002690 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002691 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002692 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002693 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002694 return this;
2695 }
2696
Joe Onoratocb109a02011-01-18 17:57:41 -08002697 /**
2698 * An intent to launch instead of posting the notification to the status bar.
2699 * Only for use with extremely high-priority notifications demanding the user's
2700 * <strong>immediate</strong> attention, such as an incoming phone call or
2701 * alarm clock that the user has explicitly set to a particular time.
2702 * If this facility is used for something else, please give the user an option
2703 * to turn it off and use a normal notification, as this can be extremely
2704 * disruptive.
2705 *
Chris Wren47c20a12014-06-18 17:27:29 -04002706 * <p>
2707 * The system UI may choose to display a heads-up notification, instead of
2708 * launching this intent, while the user is using the device.
2709 * </p>
2710 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002711 * @param intent The pending intent to launch.
2712 * @param highPriority Passing true will cause this notification to be sent
2713 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002714 *
2715 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002716 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002717 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002718 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002719 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2720 return this;
2721 }
2722
Joe Onoratocb109a02011-01-18 17:57:41 -08002723 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002724 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002725 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002726 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002727 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002728 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002729 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002730 return this;
2731 }
2732
Joe Onoratocb109a02011-01-18 17:57:41 -08002733 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002734 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002735 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002736 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002737 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002738 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002739 setTicker(tickerText);
2740 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002741 return this;
2742 }
2743
Joe Onoratocb109a02011-01-18 17:57:41 -08002744 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002745 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002746 *
2747 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002748 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2749 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002750 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002751 public Builder setLargeIcon(Bitmap b) {
2752 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2753 }
2754
2755 /**
2756 * Add a large icon to the notification content view.
2757 *
2758 * In the platform template, this image will be shown on the left of the notification view
2759 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2760 * badge atop the large icon).
2761 */
2762 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002763 mN.mLargeIcon = icon;
2764 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002765 return this;
2766 }
2767
Joe Onoratocb109a02011-01-18 17:57:41 -08002768 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002769 * Set the sound to play.
2770 *
John Spurlockc0650f022014-07-19 13:22:39 -04002771 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2772 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002773 *
Chris Wren47c20a12014-06-18 17:27:29 -04002774 * <p>
2775 * A notification that is noisy is more likely to be presented as a heads-up notification.
2776 * </p>
2777 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002778 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002779 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002780 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002781 mN.sound = sound;
2782 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002783 return this;
2784 }
2785
Joe Onoratocb109a02011-01-18 17:57:41 -08002786 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002787 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002788 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002789 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2790 *
Chris Wren47c20a12014-06-18 17:27:29 -04002791 * <p>
2792 * A notification that is noisy is more likely to be presented as a heads-up notification.
2793 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002794 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002795 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002796 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002797 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002798 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002799 mN.sound = sound;
2800 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002801 return this;
2802 }
2803
Joe Onoratocb109a02011-01-18 17:57:41 -08002804 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002805 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2806 * use during playback.
2807 *
2808 * <p>
2809 * A notification that is noisy is more likely to be presented as a heads-up notification.
2810 * </p>
2811 *
2812 * @see Notification#sound
2813 */
2814 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002815 mN.sound = sound;
2816 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002817 return this;
2818 }
2819
2820 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002821 * Set the vibration pattern to use.
2822 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002823 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2824 * <code>pattern</code> parameter.
2825 *
Chris Wren47c20a12014-06-18 17:27:29 -04002826 * <p>
2827 * A notification that vibrates is more likely to be presented as a heads-up notification.
2828 * </p>
2829 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002830 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002831 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002832 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002833 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002834 return this;
2835 }
2836
Joe Onoratocb109a02011-01-18 17:57:41 -08002837 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002838 * Set the desired color for the indicator LED on the device, as well as the
2839 * blink duty cycle (specified in milliseconds).
2840 *
2841
2842 * Not all devices will honor all (or even any) of these values.
2843 *
2844
2845 * @see Notification#ledARGB
2846 * @see Notification#ledOnMS
2847 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002848 */
Tor Norbye80756e32015-03-02 09:39:27 -08002849 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002850 mN.ledARGB = argb;
2851 mN.ledOnMS = onMs;
2852 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002853 if (onMs != 0 || offMs != 0) {
2854 mN.flags |= FLAG_SHOW_LIGHTS;
2855 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002856 return this;
2857 }
2858
Joe Onoratocb109a02011-01-18 17:57:41 -08002859 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002860 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002861 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002862
2863 * Ongoing notifications cannot be dismissed by the user, so your application or service
2864 * must take care of canceling them.
2865 *
2866
2867 * They are typically used to indicate a background task that the user is actively engaged
2868 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2869 * (e.g., a file download, sync operation, active network connection).
2870 *
2871
2872 * @see Notification#FLAG_ONGOING_EVENT
2873 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002874 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002875 public Builder setOngoing(boolean ongoing) {
2876 setFlag(FLAG_ONGOING_EVENT, ongoing);
2877 return this;
2878 }
2879
Joe Onoratocb109a02011-01-18 17:57:41 -08002880 /**
2881 * Set this flag if you would only like the sound, vibrate
2882 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002883 *
2884 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002885 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002886 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2887 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2888 return this;
2889 }
2890
Joe Onoratocb109a02011-01-18 17:57:41 -08002891 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002892 * Make this notification automatically dismissed when the user touches it. The
2893 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2894 *
2895 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002896 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002897 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002898 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002899 return this;
2900 }
2901
Joe Onoratocb109a02011-01-18 17:57:41 -08002902 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002903 * Set whether or not this notification should not bridge to other devices.
2904 *
2905 * <p>Some notifications can be bridged to other devices for remote display.
2906 * This hint can be set to recommend this notification not be bridged.
2907 */
2908 public Builder setLocalOnly(boolean localOnly) {
2909 setFlag(FLAG_LOCAL_ONLY, localOnly);
2910 return this;
2911 }
2912
2913 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002914 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002915 * <p>
2916 * The value should be one or more of the following fields combined with
2917 * bitwise-or:
2918 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2919 * <p>
2920 * For all default values, use {@link #DEFAULT_ALL}.
2921 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002922 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002923 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002924 return this;
2925 }
2926
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002927 /**
2928 * Set the priority of this notification.
2929 *
2930 * @see Notification#priority
2931 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002932 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002933 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002934 return this;
2935 }
Joe Malin8d40d042012-11-05 11:36:40 -08002936
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002937 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002938 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002939 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002940 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002941 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002942 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002943 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002944 return this;
2945 }
2946
2947 /**
Chris Wrendde75302014-03-26 17:24:15 -04002948 * Add a person that is relevant to this notification.
2949 *
Chris Wrene6c48932014-09-29 17:19:27 -04002950 * <P>
2951 * Depending on user preferences, this annotation may allow the notification to pass
2952 * through interruption filters, and to appear more prominently in the user interface.
2953 * </P>
2954 *
2955 * <P>
2956 * The person should be specified by the {@code String} representation of a
2957 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2958 * </P>
2959 *
2960 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2961 * URIs. The path part of these URIs must exist in the contacts database, in the
2962 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2963 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2964 * </P>
2965 *
2966 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002967 * @see Notification#EXTRA_PEOPLE
2968 */
Chris Wrene6c48932014-09-29 17:19:27 -04002969 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002970 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002971 return this;
2972 }
2973
2974 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002975 * Set this notification to be part of a group of notifications sharing the same key.
2976 * Grouped notifications may display in a cluster or stack on devices which
2977 * support such rendering.
2978 *
2979 * <p>To make this notification the summary for its group, also call
2980 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2981 * {@link #setSortKey}.
2982 * @param groupKey The group key of the group.
2983 * @return this object for method chaining
2984 */
2985 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002986 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002987 return this;
2988 }
2989
2990 /**
2991 * Set this notification to be the group summary for a group of notifications.
2992 * Grouped notifications may display in a cluster or stack on devices which
2993 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2994 * @param isGroupSummary Whether this notification should be a group summary.
2995 * @return this object for method chaining
2996 */
2997 public Builder setGroupSummary(boolean isGroupSummary) {
2998 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2999 return this;
3000 }
3001
3002 /**
3003 * Set a sort key that orders this notification among other notifications from the
3004 * same package. This can be useful if an external sort was already applied and an app
3005 * would like to preserve this. Notifications will be sorted lexicographically using this
3006 * value, although providing different priorities in addition to providing sort key may
3007 * cause this value to be ignored.
3008 *
3009 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003010 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003011 *
3012 * @see String#compareTo(String)
3013 */
3014 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003015 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003016 return this;
3017 }
3018
3019 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003020 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003021 *
Griff Hazen720042b2014-02-24 15:46:56 -08003022 * <p>Values within the Bundle will replace existing extras values in this Builder.
3023 *
3024 * @see Notification#extras
3025 */
Griff Hazen959591e2014-05-15 22:26:18 -07003026 public Builder addExtras(Bundle extras) {
3027 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003028 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003029 }
3030 return this;
3031 }
3032
3033 /**
3034 * Set metadata for this notification.
3035 *
3036 * <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 -04003037 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003038 * called.
3039 *
Griff Hazen720042b2014-02-24 15:46:56 -08003040 * <p>Replaces any existing extras values with those from the provided Bundle.
3041 * Use {@link #addExtras} to merge in metadata instead.
3042 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003043 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003044 */
Griff Hazen959591e2014-05-15 22:26:18 -07003045 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003046 if (extras != null) {
3047 mUserExtras = extras;
3048 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003049 return this;
3050 }
3051
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003052 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003053 * Get the current metadata Bundle used by this notification Builder.
3054 *
3055 * <p>The returned Bundle is shared with this Builder.
3056 *
3057 * <p>The current contents of this Bundle are copied into the Notification each time
3058 * {@link #build()} is called.
3059 *
3060 * @see Notification#extras
3061 */
3062 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003063 return mUserExtras;
3064 }
3065
3066 private Bundle getAllExtras() {
3067 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3068 saveExtras.putAll(mN.extras);
3069 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003070 }
3071
3072 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003073 * Add an action to this notification. Actions are typically displayed by
3074 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003075 * <p>
3076 * Every action must have an icon (32dp square and matching the
3077 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3078 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3079 * <p>
3080 * A notification in its expanded form can display up to 3 actions, from left to right in
3081 * the order they were added. Actions will not be displayed when the notification is
3082 * collapsed, however, so be sure that any essential functions may be accessed by the user
3083 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003084 *
3085 * @param icon Resource ID of a drawable that represents the action.
3086 * @param title Text describing the action.
3087 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003088 *
3089 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003090 */
Dan Sandler86647982015-05-13 23:41:13 -04003091 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003092 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003093 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003094 return this;
3095 }
3096
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003097 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003098 * Add an action to this notification. Actions are typically displayed by
3099 * the system as a button adjacent to the notification content.
3100 * <p>
3101 * Every action must have an icon (32dp square and matching the
3102 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3103 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3104 * <p>
3105 * A notification in its expanded form can display up to 3 actions, from left to right in
3106 * the order they were added. Actions will not be displayed when the notification is
3107 * collapsed, however, so be sure that any essential functions may be accessed by the user
3108 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3109 *
3110 * @param action The action to add.
3111 */
3112 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003113 if (action != null) {
3114 mActions.add(action);
3115 }
Griff Hazen959591e2014-05-15 22:26:18 -07003116 return this;
3117 }
3118
3119 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003120 * Alter the complete list of actions attached to this notification.
3121 * @see #addAction(Action).
3122 *
3123 * @param actions
3124 * @return
3125 */
3126 public Builder setActions(Action... actions) {
3127 mActions.clear();
3128 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003129 if (actions[i] != null) {
3130 mActions.add(actions[i]);
3131 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003132 }
3133 return this;
3134 }
3135
3136 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003137 * Add a rich notification style to be applied at build time.
3138 *
3139 * @param style Object responsible for modifying the notification style.
3140 */
3141 public Builder setStyle(Style style) {
3142 if (mStyle != style) {
3143 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003144 if (mStyle != null) {
3145 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003146 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3147 } else {
3148 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003149 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003150 }
3151 return this;
3152 }
3153
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003154 /**
3155 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003156 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003157 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3158 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3159 *
3160 * @return The same Builder.
3161 */
3162 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003163 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003164 return this;
3165 }
3166
3167 /**
3168 * Supply a replacement Notification whose contents should be shown in insecure contexts
3169 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3170 * @param n A replacement notification, presumably with some or all info redacted.
3171 * @return The same Builder.
3172 */
3173 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003174 if (n != null) {
3175 mN.publicVersion = new Notification();
3176 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3177 } else {
3178 mN.publicVersion = null;
3179 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003180 return this;
3181 }
3182
Griff Hazenb720abe2014-05-20 13:15:30 -07003183 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003184 * Apply an extender to this notification builder. Extenders may be used to add
3185 * metadata or change options on this builder.
3186 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003187 public Builder extend(Extender extender) {
3188 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003189 return this;
3190 }
3191
Dan Sandler4e787062015-06-17 15:09:48 -04003192 /**
3193 * @hide
3194 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003195 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003196 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003197 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003198 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003199 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003200 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003201 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003202 }
3203
Dan Sandler26e81cf2014-05-06 10:01:27 -04003204 /**
3205 * Sets {@link Notification#color}.
3206 *
3207 * @param argb The accent color to use
3208 *
3209 * @return The same Builder.
3210 */
Tor Norbye80756e32015-03-02 09:39:27 -08003211 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003212 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003213 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003214 return this;
3215 }
3216
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003217 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003218 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3219 // This user can never be a badged profile,
3220 // and also includes USER_ALL system notifications.
3221 return null;
3222 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003223 // Note: This assumes that the current user can read the profile badge of the
3224 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003225 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003226 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003227 }
3228
3229 private Bitmap getProfileBadge() {
3230 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003231 if (badge == null) {
3232 return null;
3233 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003234 final int size = mContext.getResources().getDimensionPixelSize(
3235 R.dimen.notification_badge_size);
3236 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003237 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003238 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003239 badge.draw(canvas);
3240 return bitmap;
3241 }
3242
Selim Cinekc848c3a2016-01-13 15:27:30 -08003243 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003244 Bitmap profileBadge = getProfileBadge();
3245
Kenny Guy98193ea2014-07-24 19:54:37 +01003246 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003247 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3248 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003249 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003250 }
3251
Christoph Studerfe718432014-09-01 18:21:18 +02003252 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003253 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003254 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003255 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003256 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003257 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003258 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003259 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003260 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003261 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003262 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003263 }
3264
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003265 /**
3266 * Resets the notification header to its original state
3267 */
3268 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003269 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3270 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003271 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003272 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003273 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003274 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003275 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003276 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003277 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003278 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003279 contentView.setImageViewIcon(R.id.profile_badge, null);
3280 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003281 }
3282
3283 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003284 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3285 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003286 }
3287
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003288 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003289 return applyStandardTemplate(resId, true /* hasProgress */);
3290 }
3291
3292 /**
3293 * @param hasProgress whether the progress bar should be shown and set
3294 */
3295 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003296 final Bundle ex = mN.extras;
3297
3298 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3299 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3300 return applyStandardTemplate(resId, hasProgress, title, text);
3301 }
3302
3303 /**
3304 * @param hasProgress whether the progress bar should be shown and set
3305 */
3306 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3307 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003308 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003309
Christoph Studerfe718432014-09-01 18:21:18 +02003310 resetStandardTemplate(contentView);
3311
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003312 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003313
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003314 bindNotificationHeader(contentView);
3315 bindLargeIcon(contentView);
Selim Cinek954cc232016-05-20 13:29:23 -07003316 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003317 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003318 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003319 contentView.setTextViewText(R.id.title, title);
Selim Cinek954cc232016-05-20 13:29:23 -07003320 contentView.setViewLayoutWidth(R.id.title, showProgress
3321 ? ViewGroup.LayoutParams.WRAP_CONTENT
3322 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003323 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07003324 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003325 int textId = showProgress ? com.android.internal.R.id.text_line_1
3326 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003327 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003328 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003329 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003330
Selim Cinek279fa862016-06-14 10:57:25 -07003331 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003332
Selim Cinek29603462015-11-17 19:04:39 -08003333 return contentView;
3334 }
3335
Selim Cinek860b6da2015-12-16 19:02:19 -08003336 /**
3337 * @param remoteView the remote view to update the minheight in
3338 * @param hasMinHeight does it have a mimHeight
3339 * @hide
3340 */
3341 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3342 int minHeight = 0;
3343 if (hasMinHeight) {
3344 // we need to set the minHeight of the notification
3345 minHeight = mContext.getResources().getDimensionPixelSize(
3346 com.android.internal.R.dimen.notification_min_content_height);
3347 }
3348 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3349 }
3350
Selim Cinek29603462015-11-17 19:04:39 -08003351 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003352 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3353 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3354 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3355 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003356 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003357 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003358 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003359 contentView.setProgressBackgroundTintList(
3360 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3361 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003362 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003363 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003364 contentView.setProgressTintList(R.id.progress, colorStateList);
3365 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003366 }
Selim Cinek29603462015-11-17 19:04:39 -08003367 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003368 } else {
3369 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003370 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003371 }
Joe Onorato561d3852010-11-20 18:09:34 -08003372 }
3373
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003374 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003375 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3376 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3377 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003378 if (mN.mLargeIcon != null) {
3379 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3380 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3381 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003382 int endMargin = R.dimen.notification_content_picture_margin;
3383 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3384 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3385 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003386 }
3387 }
3388
3389 private void bindNotificationHeader(RemoteViews contentView) {
3390 bindSmallIcon(contentView);
3391 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003392 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003393 bindHeaderChronometerAndTime(contentView);
3394 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003395 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003396 }
3397
3398 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003399 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003400 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003401 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003402 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003403 }
3404
3405 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3406 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003407 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003408 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3409 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3410 contentView.setLong(R.id.chronometer, "setBase",
3411 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3412 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003413 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003414 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003415 } else {
3416 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3417 contentView.setLong(R.id.time, "setTime", mN.when);
3418 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003419 } else {
3420 // We still want a time to be set but gone, such that we can show and hide it
3421 // on demand in case it's a child notification without anything in the header
3422 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003423 }
3424 }
3425
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003426 private void bindHeaderText(RemoteViews contentView) {
3427 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3428 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003429 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003430 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003431 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003432 if (headerText == null
3433 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3434 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3435 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3436 }
3437 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003438 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003439 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3440 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3441 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003442 }
3443 }
3444
Adrian Rooseba05822016-04-22 17:09:27 -07003445 /**
3446 * @hide
3447 */
3448 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003449 CharSequence name = null;
3450 final PackageManager pm = mContext.getPackageManager();
3451 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3452 // only system packages which lump together a bunch of unrelated stuff
3453 // may substitute a different name to make the purpose of the
3454 // notification more clear. the correct package label should always
3455 // be accessible via SystemUI.
3456 final String pkg = mContext.getPackageName();
3457 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3458 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3459 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3460 name = subName;
3461 } else {
3462 Log.w(TAG, "warning: pkg "
3463 + pkg + " attempting to substitute app name '" + subName
3464 + "' without holding perm "
3465 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3466 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003467 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003468 if (TextUtils.isEmpty(name)) {
3469 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3470 }
3471 if (TextUtils.isEmpty(name)) {
3472 // still nothing?
3473 return null;
3474 }
3475
3476 return String.valueOf(name);
3477 }
3478 private void bindHeaderAppName(RemoteViews contentView) {
3479 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003480 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003481 }
3482
3483 private void bindSmallIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003484 if (mN.mSmallIcon == null && mN.icon != 0) {
3485 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3486 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003487 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003488 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3489 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003490 processSmallIconColor(mN.mSmallIcon, contentView);
3491 }
3492
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003493 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003494 * @return true if the built notification will show the time or the chronometer; false
3495 * otherwise
3496 */
3497 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003498 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003499 }
3500
Christoph Studerfe718432014-09-01 18:21:18 +02003501 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003502 // actions_container is only reset when there are no actions to avoid focus issues with
3503 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003504 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003505 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003506
3507 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3508 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3509
3510 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3511 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3512 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3513 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003514
3515 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003516 }
3517
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003518 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003519 final Bundle ex = mN.extras;
3520
3521 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3522 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3523 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3524 }
3525
3526 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3527 CharSequence title, CharSequence text) {
3528 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003529
Christoph Studerfe718432014-09-01 18:21:18 +02003530 resetStandardTemplateWithActions(big);
3531
Adrian Roose458aa82015-12-08 16:17:19 -08003532 boolean validRemoteInput = false;
3533
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003534 int N = mActions.size();
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003535 boolean emphazisedMode = mN.fullScreenIntent != null;
3536 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003537 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003538 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003539 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roosf852a422016-06-03 13:33:43 -07003540 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3541 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003542 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003543 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003544 Action action = mActions.get(i);
3545 validRemoteInput |= hasValidRemoteInput(action);
3546
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003547 final RemoteViews button = generateActionButton(action, emphazisedMode,
3548 i % 2 != 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003549 big.addView(R.id.actions, button);
3550 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003551 } else {
3552 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003553 }
Adrian Roose458aa82015-12-08 16:17:19 -08003554
3555 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3556 if (validRemoteInput && replyText != null
3557 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3558 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3559 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3560
3561 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3562 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3563 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3564
3565 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3566 big.setViewVisibility(
3567 R.id.notification_material_reply_text_3, View.VISIBLE);
3568 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3569 }
3570 }
3571 }
3572
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003573 return big;
3574 }
3575
Adrian Roose458aa82015-12-08 16:17:19 -08003576 private boolean hasValidRemoteInput(Action action) {
3577 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3578 // Weird actions
3579 return false;
3580 }
3581
3582 RemoteInput[] remoteInputs = action.getRemoteInputs();
3583 if (remoteInputs == null) {
3584 return false;
3585 }
3586
3587 for (RemoteInput r : remoteInputs) {
3588 CharSequence[] choices = r.getChoices();
3589 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3590 return true;
3591 }
3592 }
3593 return false;
3594 }
3595
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003596 /**
3597 * Construct a RemoteViews for the final 1U notification layout. In order:
3598 * 1. Custom contentView from the caller
3599 * 2. Style's proposed content view
3600 * 3. Standard template view
3601 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003602 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003603 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003604 return mN.contentView;
3605 } else if (mStyle != null) {
3606 final RemoteViews styleView = mStyle.makeContentView();
3607 if (styleView != null) {
3608 return styleView;
3609 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003610 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003611 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003612 }
3613
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003614 /**
3615 * Construct a RemoteViews for the final big notification layout.
3616 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003617 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003618 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003619 if (mN.bigContentView != null
3620 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003621 return mN.bigContentView;
3622 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003623 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003624 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003625 } else if (mActions.size() != 0) {
3626 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003627 }
3628 adaptNotificationHeaderForBigContentView(result);
3629 return result;
3630 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003631
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003632 /**
3633 * Construct a RemoteViews for the final notification header only
3634 *
3635 * @hide
3636 */
3637 public RemoteViews makeNotificationHeader() {
3638 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3639 R.layout.notification_template_header);
3640 resetNotificationHeader(header);
3641 bindNotificationHeader(header);
3642 return header;
3643 }
3644
Selim Cinek29603462015-11-17 19:04:39 -08003645 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003646 if (result != null) {
3647 result.setViewVisibility(R.id.text_line_1, View.GONE);
3648 }
Selim Cinek29603462015-11-17 19:04:39 -08003649 }
3650
Selim Cinek850a8542015-11-11 11:48:36 -05003651 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003652 if (result != null) {
3653 result.setBoolean(R.id.notification_header, "setExpanded", true);
3654 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003655 }
3656
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003657 /**
3658 * Construct a RemoteViews for the final heads-up notification layout.
3659 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003660 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003661 if (mN.headsUpContentView != null
3662 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003663 return mN.headsUpContentView;
3664 } else if (mStyle != null) {
3665 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3666 if (styleView != null) {
3667 return styleView;
3668 }
3669 } else if (mActions.size() == 0) {
3670 return null;
3671 }
3672
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003673 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003674 }
3675
Selim Cinek624c02db2015-12-14 21:00:02 -08003676 /**
3677 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3678 *
3679 * @hide
3680 */
3681 public RemoteViews makePublicContentView() {
3682 if (mN.publicVersion != null) {
3683 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003684 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003685 }
3686 Bundle savedBundle = mN.extras;
3687 Style style = mStyle;
3688 mStyle = null;
3689 Icon largeIcon = mN.mLargeIcon;
3690 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003691 Bitmap largeIconLegacy = mN.largeIcon;
3692 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003693 Bundle publicExtras = new Bundle();
3694 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3695 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3696 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3697 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003698 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3699 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003700 publicExtras.putCharSequence(EXTRA_TITLE,
3701 mContext.getString(R.string.notification_hidden_text));
3702 mN.extras = publicExtras;
3703 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3704 mN.extras = savedBundle;
3705 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003706 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003707 mStyle = style;
3708 return publicView;
3709 }
3710
3711
Chris Wren8fd39ec2014-02-27 17:43:26 -05003712
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003713 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
3714 boolean oddAction) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003715 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003716 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003717 emphazisedMode ? getEmphasizedActionLayoutResource()
3718 : tombstone ? getActionTombstoneLayoutResource()
3719 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04003720 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003721 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003722 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003723 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003724 if (action.mRemoteInputs != null) {
3725 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3726 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003727 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07003728 // change the background bgColor
3729 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
3730 : R.color.notification_action_list_dark);
3731 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003732 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07003733 CharSequence title = action.title;
3734 ColorStateList[] outResultColor = null;
3735 if (isLegacy()) {
3736 title = clearColorSpans(title);
3737 } else {
3738 outResultColor = new ColorStateList[1];
3739 title = ensureColorSpanContrast(title, bgColor, outResultColor);
3740 }
3741 button.setTextViewText(R.id.action0, title);
3742 if (outResultColor != null && outResultColor[0] != null) {
3743 // We need to set the text color as well since changing a text to uppercase
3744 // clears its spans.
3745 button.setTextColor(R.id.action0, outResultColor[0]);
3746 } else if (mN.color != COLOR_DEFAULT) {
3747 button.setTextColor(R.id.action0,resolveContrastColor());
3748 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003749 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07003750 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003751 if (mN.color != COLOR_DEFAULT) {
3752 button.setTextColor(R.id.action0, resolveContrastColor());
3753 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003754 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003755 return button;
3756 }
3757
Joe Onoratocb109a02011-01-18 17:57:41 -08003758 /**
Selim Cinek981962e2016-07-20 20:41:58 -07003759 * Clears all color spans of a text
3760 * @param charSequence the input text
3761 * @return the same text but without color spans
3762 */
3763 private CharSequence clearColorSpans(CharSequence charSequence) {
3764 if (charSequence instanceof Spanned) {
3765 Spanned ss = (Spanned) charSequence;
3766 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3767 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3768 for (Object span : spans) {
3769 Object resultSpan = span;
3770 if (resultSpan instanceof CharacterStyle) {
3771 resultSpan = ((CharacterStyle) span).getUnderlying();
3772 }
3773 if (resultSpan instanceof TextAppearanceSpan) {
3774 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3775 if (originalSpan.getTextColor() != null) {
3776 resultSpan = new TextAppearanceSpan(
3777 originalSpan.getFamily(),
3778 originalSpan.getTextStyle(),
3779 originalSpan.getTextSize(),
3780 null,
3781 originalSpan.getLinkTextColor());
3782 }
3783 } else if (resultSpan instanceof ForegroundColorSpan
3784 || (resultSpan instanceof BackgroundColorSpan)) {
3785 continue;
3786 } else {
3787 resultSpan = span;
3788 }
3789 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
3790 ss.getSpanFlags(span));
3791 }
3792 return builder;
3793 }
3794 return charSequence;
3795 }
3796
3797 /**
3798 * Ensures contrast on color spans against a background color. also returns the color of the
3799 * text if a span was found that spans over the whole text.
3800 *
3801 * @param charSequence the charSequence on which the spans are
3802 * @param background the background color to ensure the contrast against
3803 * @param outResultColor an array in which a color will be returned as the first element if
3804 * there exists a full length color span.
3805 * @return the contrasted charSequence
3806 */
3807 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
3808 ColorStateList[] outResultColor) {
3809 if (charSequence instanceof Spanned) {
3810 Spanned ss = (Spanned) charSequence;
3811 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3812 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3813 for (Object span : spans) {
3814 Object resultSpan = span;
3815 int spanStart = ss.getSpanStart(span);
3816 int spanEnd = ss.getSpanEnd(span);
3817 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
3818 if (resultSpan instanceof CharacterStyle) {
3819 resultSpan = ((CharacterStyle) span).getUnderlying();
3820 }
3821 if (resultSpan instanceof TextAppearanceSpan) {
3822 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3823 ColorStateList textColor = originalSpan.getTextColor();
3824 if (textColor != null) {
3825 int[] colors = textColor.getColors();
3826 int[] newColors = new int[colors.length];
3827 for (int i = 0; i < newColors.length; i++) {
3828 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
3829 colors[i], background);
3830 }
3831 textColor = new ColorStateList(textColor.getStates().clone(),
3832 newColors);
3833 resultSpan = new TextAppearanceSpan(
3834 originalSpan.getFamily(),
3835 originalSpan.getTextStyle(),
3836 originalSpan.getTextSize(),
3837 textColor,
3838 originalSpan.getLinkTextColor());
3839 if (fullLength) {
3840 outResultColor[0] = new ColorStateList(
3841 textColor.getStates().clone(), newColors);
3842 }
3843 }
3844 } else if (resultSpan instanceof ForegroundColorSpan) {
3845 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
3846 int foregroundColor = originalSpan.getForegroundColor();
3847 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
3848 foregroundColor, background);
3849 resultSpan = new ForegroundColorSpan(foregroundColor);
3850 if (fullLength) {
3851 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
3852 }
3853 } else {
3854 resultSpan = span;
3855 }
3856
3857 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
3858 }
3859 return builder;
3860 }
3861 return charSequence;
3862 }
3863
3864 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003865 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003866 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003867 */
3868 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003869 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003870 }
3871
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003872 private CharSequence processLegacyText(CharSequence charSequence) {
3873 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003874 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003875 } else {
3876 return charSequence;
3877 }
3878 }
3879
Dan Sandler26e81cf2014-05-06 10:01:27 -04003880 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003881 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003882 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003883 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003884 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3885 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003886 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003887 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003888
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003889 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003890 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003891 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003892 }
3893
Dan Sandler26e81cf2014-05-06 10:01:27 -04003894 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003895 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003896 * if it's grayscale).
3897 */
3898 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003899 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3900 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003901 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003902 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003903 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003904 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003905 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003906 }
3907
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003908 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003909 if (mN.color != COLOR_DEFAULT) {
3910 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003911 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003912 }
3913
Adrian Roos4ff3b122016-02-01 12:26:13 -08003914 int resolveContrastColor() {
3915 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3916 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003917 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003918 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3919
3920 mCachedContrastColorIsFor = mN.color;
3921 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003922 }
3923
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003924 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003925 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003926 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003927 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003928 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003929 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003930 mN.actions = new Action[mActions.size()];
3931 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003932 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003933 if (!mPersonList.isEmpty()) {
3934 mN.extras.putStringArray(EXTRA_PEOPLE,
3935 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003936 }
Selim Cinek247fa012016-02-18 09:50:48 -08003937 if (mN.bigContentView != null || mN.contentView != null
3938 || mN.headsUpContentView != null) {
3939 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3940 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003941 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003942 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003943
Julia Reynolds3b848122016-02-26 10:45:32 -05003944 /**
3945 * Creates a Builder from an existing notification so further changes can be made.
3946 * @param context The context for your application / activity.
3947 * @param n The notification to create a Builder from.
3948 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003949 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003950 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003951 ApplicationInfo applicationInfo = n.extras.getParcelable(
3952 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003953 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003954 if (applicationInfo != null) {
3955 try {
3956 builderContext = context.createApplicationContext(applicationInfo,
3957 Context.CONTEXT_RESTRICTED);
3958 } catch (NameNotFoundException e) {
3959 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3960 builderContext = context; // try with our context
3961 }
3962 } else {
3963 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003964 }
3965
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003966 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003967 }
3968
3969 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003970 Class<? extends Style>[] classes = new Class[] {
3971 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003972 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3973 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003974 for (Class<? extends Style> innerClass : classes) {
3975 if (templateClass.equals(innerClass.getName())) {
3976 return innerClass;
3977 }
3978 }
3979 return null;
3980 }
3981
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003982 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003983 * @deprecated Use {@link #build()} instead.
3984 */
3985 @Deprecated
3986 public Notification getNotification() {
3987 return build();
3988 }
3989
3990 /**
3991 * Combine all of the options that have been set and return a new {@link Notification}
3992 * object.
3993 */
3994 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003995 // first, add any extras from the calling code
3996 if (mUserExtras != null) {
3997 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003998 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003999
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004000 mN.creationTime = System.currentTimeMillis();
4001
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004002 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004003 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004004
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004005 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004006
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004007 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004008 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004009 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004010
Adrian Roos5081c0d2016-02-26 16:04:19 -08004011 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4012 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004013 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004014 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004015 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4016 mN.contentView.getSequenceNumber());
4017 }
4018 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004019 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004020 if (mN.bigContentView != null) {
4021 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4022 mN.bigContentView.getSequenceNumber());
4023 }
4024 }
4025 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004026 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004027 if (mN.headsUpContentView != null) {
4028 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4029 mN.headsUpContentView.getSequenceNumber());
4030 }
4031 }
4032 }
4033
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004034 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4035 mN.flags |= FLAG_SHOW_LIGHTS;
4036 }
4037
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004038 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004039 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004040
4041 /**
4042 * Apply this Builder to an existing {@link Notification} object.
4043 *
4044 * @hide
4045 */
4046 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004047 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004048 return n;
4049 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004050
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004051 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004052 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4053 * change.
4054 *
4055 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4056 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004057 * @hide
4058 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004059 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004060 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004061
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004062 // Only strip views for known Styles because we won't know how to
4063 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004064 if (!TextUtils.isEmpty(templateClass)
4065 && getNotificationStyleClass(templateClass) == null) {
4066 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004067 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004068
4069 // Only strip unmodified BuilderRemoteViews.
4070 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004071 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004072 n.contentView.getSequenceNumber();
4073 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004074 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004075 n.bigContentView.getSequenceNumber();
4076 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004077 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004078 n.headsUpContentView.getSequenceNumber();
4079
4080 // Nothing to do here, no need to clone.
4081 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4082 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004083 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004084
4085 Notification clone = n.clone();
4086 if (stripContentView) {
4087 clone.contentView = null;
4088 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4089 }
4090 if (stripBigContentView) {
4091 clone.bigContentView = null;
4092 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4093 }
4094 if (stripHeadsUpContentView) {
4095 clone.headsUpContentView = null;
4096 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4097 }
4098 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004099 }
4100
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004101 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004102 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004103 }
4104
4105 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004106 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004107 }
4108
4109 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004110 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004111 }
4112
4113 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004114 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004115 }
4116
4117 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004118 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004119 }
4120
Adrian Roosc1a80b02016-04-05 14:54:55 -07004121 private int getMessagingLayoutResource() {
4122 return R.layout.notification_template_material_messaging;
4123 }
4124
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004125 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004126 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004127 }
4128
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004129 private int getEmphasizedActionLayoutResource() {
4130 return R.layout.notification_material_action_emphasized;
4131 }
4132
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004133 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004134 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004135 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004136 }
4137
Selim Cinek279fa862016-06-14 10:57:25 -07004138 private boolean hasLargeIcon() {
4139 return mLargeIcon != null || largeIcon != null;
4140 }
4141
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004142 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004143 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004144 * @hide
4145 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004146 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004147 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4148 }
4149
4150 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004151 * @return true if the notification will show a chronometer; false otherwise
4152 * @hide
4153 */
4154 public boolean showsChronometer() {
4155 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4156 }
4157
4158 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004159 * An object that can apply a rich notification style to a {@link Notification.Builder}
4160 * object.
4161 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004162 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004163 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004164
4165 /**
4166 * @hide
4167 */
4168 protected CharSequence mSummaryText = null;
4169
4170 /**
4171 * @hide
4172 */
4173 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004174
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004175 protected Builder mBuilder;
4176
Chris Wrend6297db2012-05-03 16:20:13 -04004177 /**
4178 * Overrides ContentTitle in the big form of the template.
4179 * This defaults to the value passed to setContentTitle().
4180 */
4181 protected void internalSetBigContentTitle(CharSequence title) {
4182 mBigContentTitle = title;
4183 }
4184
4185 /**
4186 * Set the first line of text after the detail section in the big form of the template.
4187 */
4188 protected void internalSetSummaryText(CharSequence cs) {
4189 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004190 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004191 }
4192
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004193 public void setBuilder(Builder builder) {
4194 if (mBuilder != builder) {
4195 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004196 if (mBuilder != null) {
4197 mBuilder.setStyle(this);
4198 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004199 }
4200 }
4201
Chris Wrend6297db2012-05-03 16:20:13 -04004202 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004203 if (mBuilder == null) {
4204 throw new IllegalArgumentException("Style requires a valid Builder object");
4205 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004206 }
Chris Wrend6297db2012-05-03 16:20:13 -04004207
4208 protected RemoteViews getStandardView(int layoutId) {
4209 checkBuilder();
4210
Christoph Studer4600f9b2014-07-22 22:44:43 +02004211 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004212 CharSequence oldBuilderContentTitle =
4213 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004214 if (mBigContentTitle != null) {
4215 mBuilder.setContentTitle(mBigContentTitle);
4216 }
4217
Chris Wrend6297db2012-05-03 16:20:13 -04004218 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4219
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004220 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004221
Chris Wrend6297db2012-05-03 16:20:13 -04004222 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4223 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004224 } else {
4225 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004226 }
4227
Chris Wrend6297db2012-05-03 16:20:13 -04004228 return contentView;
4229 }
4230
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004231 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004232 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4233 * The default implementation has nothing additional to add.
4234 * @hide
4235 */
4236 public RemoteViews makeContentView() {
4237 return null;
4238 }
4239
4240 /**
4241 * Construct a Style-specific RemoteViews for the final big notification layout.
4242 * @hide
4243 */
4244 public RemoteViews makeBigContentView() {
4245 return null;
4246 }
4247
4248 /**
4249 * Construct a Style-specific RemoteViews for the final HUN layout.
4250 * @hide
4251 */
4252 public RemoteViews makeHeadsUpContentView() {
4253 return null;
4254 }
4255
4256 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004257 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004258 * @hide
4259 */
4260 public void addExtras(Bundle extras) {
4261 if (mSummaryTextSet) {
4262 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4263 }
4264 if (mBigContentTitle != null) {
4265 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4266 }
Chris Wren91ad5632013-06-05 15:05:57 -04004267 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004268 }
4269
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004270 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004271 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004272 * @hide
4273 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004274 protected void restoreFromExtras(Bundle extras) {
4275 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4276 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4277 mSummaryTextSet = true;
4278 }
4279 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4280 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4281 }
4282 }
4283
4284
4285 /**
4286 * @hide
4287 */
4288 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004289 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004290 return wip;
4291 }
4292
Daniel Sandler0ec46202015-06-24 01:27:05 -04004293 /**
4294 * @hide
4295 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004296 public void purgeResources() {}
4297
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004298 /**
4299 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4300 * attached to.
4301 *
4302 * @return the fully constructed Notification.
4303 */
4304 public Notification build() {
4305 checkBuilder();
4306 return mBuilder.build();
4307 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004308
4309 /**
4310 * @hide
4311 * @return true if the style positions the progress bar on the second line; false if the
4312 * style hides the progress bar
4313 */
4314 protected boolean hasProgress() {
4315 return true;
4316 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004317
4318 /**
4319 * @hide
4320 * @return Whether we should put the summary be put into the notification header
4321 */
4322 public boolean hasSummaryInHeader() {
4323 return true;
4324 }
Selim Cinek593610c2016-02-16 18:42:57 -08004325
4326 /**
4327 * @hide
4328 * @return Whether custom content views are displayed inline in the style
4329 */
4330 public boolean displayCustomViewInline() {
4331 return false;
4332 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004333 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004334
4335 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004336 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004337 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004338 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004339 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004340 * Notification notif = new Notification.Builder(mContext)
4341 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4342 * .setContentText(subject)
4343 * .setSmallIcon(R.drawable.new_post)
4344 * .setLargeIcon(aBitmap)
4345 * .setStyle(new Notification.BigPictureStyle()
4346 * .bigPicture(aBigBitmap))
4347 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004348 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004349 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004350 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004351 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004352 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004353 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004354 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004355 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004356
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004357 public BigPictureStyle() {
4358 }
4359
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004360 /**
4361 * @deprecated use {@code BigPictureStyle()}.
4362 */
4363 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004364 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004365 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004366 }
4367
Chris Wrend6297db2012-05-03 16:20:13 -04004368 /**
4369 * Overrides ContentTitle in the big form of the template.
4370 * This defaults to the value passed to setContentTitle().
4371 */
4372 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004373 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004374 return this;
4375 }
4376
4377 /**
4378 * Set the first line of text after the detail section in the big form of the template.
4379 */
4380 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004381 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004382 return this;
4383 }
4384
Chris Wren0bd664d2012-08-01 13:56:56 -04004385 /**
4386 * Provide the bitmap to be used as the payload for the BigPicture notification.
4387 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004388 public BigPictureStyle bigPicture(Bitmap b) {
4389 mPicture = b;
4390 return this;
4391 }
4392
Chris Wren3745a3d2012-05-22 15:11:52 -04004393 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004394 * Override the large icon when the big notification is shown.
4395 */
4396 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004397 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4398 }
4399
4400 /**
4401 * Override the large icon when the big notification is shown.
4402 */
4403 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004404 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004405 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004406 return this;
4407 }
4408
Riley Andrews0394a0c2015-11-03 23:36:52 -08004409 /** @hide */
4410 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4411
Daniel Sandler0ec46202015-06-24 01:27:05 -04004412 /**
4413 * @hide
4414 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004415 @Override
4416 public void purgeResources() {
4417 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004418 if (mPicture != null &&
4419 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004420 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004421 mPicture = mPicture.createAshmemBitmap();
4422 }
4423 if (mBigLargeIcon != null) {
4424 mBigLargeIcon.convertToAshmem();
4425 }
4426 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004427
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004428 /**
4429 * @hide
4430 */
4431 public RemoteViews makeBigContentView() {
4432 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004433 // This covers the following cases:
4434 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004435 // mN.mLargeIcon
4436 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004437 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07004438 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004439 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004440 oldLargeIcon = mBuilder.mN.mLargeIcon;
4441 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004442 // The legacy largeIcon might not allow us to clear the image, as it's taken in
4443 // replacement if the other one is null. Because we're restoring these legacy icons
4444 // for old listeners, this is in general non-null.
4445 largeIconLegacy = mBuilder.mN.largeIcon;
4446 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004447 }
4448
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004449 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004450 if (mSummaryTextSet) {
4451 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004452 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004453 }
Selim Cinek279fa862016-06-14 10:57:25 -07004454 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004455
Christoph Studer5c510ee2014-12-15 16:32:27 +01004456 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004457 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004458 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004459 }
4460
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004461 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004462 return contentView;
4463 }
4464
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004465 /**
4466 * @hide
4467 */
4468 public void addExtras(Bundle extras) {
4469 super.addExtras(extras);
4470
4471 if (mBigLargeIconSet) {
4472 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4473 }
4474 extras.putParcelable(EXTRA_PICTURE, mPicture);
4475 }
4476
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004477 /**
4478 * @hide
4479 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004480 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004481 protected void restoreFromExtras(Bundle extras) {
4482 super.restoreFromExtras(extras);
4483
4484 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004485 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004486 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004487 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004488 mPicture = extras.getParcelable(EXTRA_PICTURE);
4489 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004490
4491 /**
4492 * @hide
4493 */
4494 @Override
4495 public boolean hasSummaryInHeader() {
4496 return false;
4497 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004498 }
4499
4500 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004501 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004502 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004503 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004504 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004505 * Notification notif = new Notification.Builder(mContext)
4506 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4507 * .setContentText(subject)
4508 * .setSmallIcon(R.drawable.new_mail)
4509 * .setLargeIcon(aBitmap)
4510 * .setStyle(new Notification.BigTextStyle()
4511 * .bigText(aVeryLongString))
4512 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004513 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004514 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004515 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004516 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004517 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004518
4519 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004520 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004521
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004522 private CharSequence mBigText;
4523
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004524 public BigTextStyle() {
4525 }
4526
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004527 /**
4528 * @deprecated use {@code BigTextStyle()}.
4529 */
4530 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004531 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004532 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004533 }
4534
Chris Wrend6297db2012-05-03 16:20:13 -04004535 /**
4536 * Overrides ContentTitle in the big form of the template.
4537 * This defaults to the value passed to setContentTitle().
4538 */
4539 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004540 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004541 return this;
4542 }
4543
4544 /**
4545 * Set the first line of text after the detail section in the big form of the template.
4546 */
4547 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004548 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004549 return this;
4550 }
4551
Chris Wren0bd664d2012-08-01 13:56:56 -04004552 /**
4553 * Provide the longer text to be displayed in the big form of the
4554 * template in place of the content text.
4555 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004556 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004557 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004558 return this;
4559 }
4560
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004561 /**
4562 * @hide
4563 */
4564 public void addExtras(Bundle extras) {
4565 super.addExtras(extras);
4566
Christoph Studer4600f9b2014-07-22 22:44:43 +02004567 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4568 }
4569
4570 /**
4571 * @hide
4572 */
4573 @Override
4574 protected void restoreFromExtras(Bundle extras) {
4575 super.restoreFromExtras(extras);
4576
4577 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004578 }
4579
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004580 /**
4581 * @hide
4582 */
4583 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004584
4585 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004586 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004587 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004588
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004589 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004590
Selim Cinek75998782016-04-26 10:39:17 -07004591 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004592
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004593 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004594 if (TextUtils.isEmpty(bigTextText)) {
4595 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4596 // experience
4597 bigTextText = mBuilder.processLegacyText(text);
4598 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004599 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004600
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004601 return contentView;
4602 }
4603
Adrian Roosb1f427c2016-05-26 12:27:15 -07004604 static void applyBigTextContentView(Builder builder,
4605 RemoteViews contentView, CharSequence bigTextText) {
4606 contentView.setTextViewText(R.id.big_text, bigTextText);
4607 contentView.setViewVisibility(R.id.big_text,
4608 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4609 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004610 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004611 }
4612
4613 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004614 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004615 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004616 if (hasActions) {
4617 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4618 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004619 return lineCount;
4620 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004621 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004622
4623 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004624 * Helper class for generating large-format notifications that include multiple back-and-forth
4625 * messages of varying types between any number of people.
4626 *
4627 * <br>
4628 * If the platform does not provide large-format notifications, this method has no effect. The
4629 * user will always see the normal notification view.
4630 * <br>
4631 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4632 * so:
4633 * <pre class="prettyprint">
4634 *
4635 * Notification noti = new Notification.Builder()
4636 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4637 * .setContentText(subject)
4638 * .setSmallIcon(R.drawable.new_message)
4639 * .setLargeIcon(aBitmap)
4640 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4641 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4642 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4643 * .build();
4644 * </pre>
4645 */
4646 public static class MessagingStyle extends Style {
4647
4648 /**
4649 * The maximum number of messages that will be retained in the Notification itself (the
4650 * number displayed is up to the platform).
4651 */
4652 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4653
4654 CharSequence mUserDisplayName;
4655 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004656 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004657
4658 MessagingStyle() {
4659 }
4660
4661 /**
Alex Hillsfd590442016-10-07 09:52:44 -04004662 * @param userDisplayName Required - the name to be displayed for any replies sent by the
4663 * user before the posting app reposts the notification with those messages after they've
4664 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04004665 * {@link #addMessage(Notification.MessagingStyle.Message)}
4666 */
Alex Hillsfd590442016-10-07 09:52:44 -04004667 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04004668 mUserDisplayName = userDisplayName;
4669 }
4670
4671 /**
4672 * Returns the name to be displayed for any replies sent by the user
4673 */
4674 public CharSequence getUserDisplayName() {
4675 return mUserDisplayName;
4676 }
4677
4678 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004679 * Sets the title to be displayed on this conversation. This should only be used for
4680 * group messaging and left unset for one-on-one conversations.
4681 * @param conversationTitle
4682 * @return this object for method chaining.
4683 */
4684 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4685 mConversationTitle = conversationTitle;
4686 return this;
4687 }
4688
4689 /**
4690 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4691 * should be for one-on-one conversations
4692 */
4693 public CharSequence getConversationTitle() {
4694 return mConversationTitle;
4695 }
4696
4697 /**
4698 * Adds a message for display by this notification. Convenience call for a simple
4699 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4700 * @param text A {@link CharSequence} to be displayed as the message content
4701 * @param timestamp Time at which the message arrived
4702 * @param sender A {@link CharSequence} to be used for displaying the name of the
4703 * sender. Should be <code>null</code> for messages by the current user, in which case
4704 * the platform will insert {@link #getUserDisplayName()}.
4705 * Should be unique amongst all individuals in the conversation, and should be
4706 * consistent during re-posts of the notification.
4707 *
4708 * @see Message#Message(CharSequence, long, CharSequence)
4709 *
4710 * @return this object for method chaining
4711 */
4712 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4713 mMessages.add(new Message(text, timestamp, sender));
4714 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4715 mMessages.remove(0);
4716 }
4717 return this;
4718 }
4719
4720 /**
4721 * Adds a {@link Message} for display in this notification.
4722 * @param message The {@link Message} to be displayed
4723 * @return this object for method chaining
4724 */
4725 public MessagingStyle addMessage(Message message) {
4726 mMessages.add(message);
4727 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4728 mMessages.remove(0);
4729 }
4730 return this;
4731 }
4732
4733 /**
4734 * Gets the list of {@code Message} objects that represent the notification
4735 */
4736 public List<Message> getMessages() {
4737 return mMessages;
4738 }
4739
4740 /**
4741 * @hide
4742 */
4743 @Override
4744 public void addExtras(Bundle extras) {
4745 super.addExtras(extras);
4746 if (mUserDisplayName != null) {
4747 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4748 }
4749 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004750 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004751 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004752 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4753 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004754 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004755
4756 fixTitleAndTextExtras(extras);
4757 }
4758
4759 private void fixTitleAndTextExtras(Bundle extras) {
4760 Message m = findLatestIncomingMessage();
4761 CharSequence text = (m == null) ? null : m.mText;
4762 CharSequence sender = m == null ? null
4763 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4764 CharSequence title;
4765 if (!TextUtils.isEmpty(mConversationTitle)) {
4766 if (!TextUtils.isEmpty(sender)) {
4767 BidiFormatter bidi = BidiFormatter.getInstance();
4768 title = mBuilder.mContext.getString(
4769 com.android.internal.R.string.notification_messaging_title_template,
4770 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4771 } else {
4772 title = mConversationTitle;
4773 }
4774 } else {
4775 title = sender;
4776 }
4777
4778 if (title != null) {
4779 extras.putCharSequence(EXTRA_TITLE, title);
4780 }
4781 if (text != null) {
4782 extras.putCharSequence(EXTRA_TEXT, text);
4783 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004784 }
4785
4786 /**
4787 * @hide
4788 */
4789 @Override
4790 protected void restoreFromExtras(Bundle extras) {
4791 super.restoreFromExtras(extras);
4792
4793 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004794 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4795 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004796 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004797 if (parcelables != null && parcelables instanceof Parcelable[]) {
4798 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004799 }
4800 }
4801
4802 /**
4803 * @hide
4804 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004805 @Override
4806 public RemoteViews makeContentView() {
4807 Message m = findLatestIncomingMessage();
4808 CharSequence title = mConversationTitle != null
4809 ? mConversationTitle
4810 : (m == null) ? null : m.mSender;
4811 CharSequence text = (m == null)
4812 ? null
4813 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004814
Adrian Roosc1a80b02016-04-05 14:54:55 -07004815 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4816 false /* hasProgress */,
4817 title,
4818 text);
4819 }
4820
4821 private Message findLatestIncomingMessage() {
4822 for (int i = mMessages.size() - 1; i >= 0; i--) {
4823 Message m = mMessages.get(i);
4824 // Incoming messages have a non-empty sender.
4825 if (!TextUtils.isEmpty(m.mSender)) {
4826 return m;
4827 }
4828 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004829 if (!mMessages.isEmpty()) {
4830 // No incoming messages, fall back to outgoing message
4831 return mMessages.get(mMessages.size() - 1);
4832 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004833 return null;
4834 }
4835
4836 /**
4837 * @hide
4838 */
4839 @Override
4840 public RemoteViews makeBigContentView() {
4841 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4842 ? super.mBigContentTitle
4843 : mConversationTitle;
4844 boolean hasTitle = !TextUtils.isEmpty(title);
4845
Adrian Roosfeafa052016-06-01 17:09:45 -07004846 if (mMessages.size() == 1) {
4847 // Special case for a single message: Use the big text style
4848 // so the collapsed and expanded versions match nicely.
4849 CharSequence bigTitle;
4850 CharSequence text;
4851 if (hasTitle) {
4852 bigTitle = title;
4853 text = makeMessageLine(mMessages.get(0));
4854 } else {
4855 bigTitle = mMessages.get(0).mSender;
4856 text = mMessages.get(0).mText;
4857 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004858 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4859 mBuilder.getBigTextLayoutResource(),
Adrian Roosfeafa052016-06-01 17:09:45 -07004860 false /* progress */, bigTitle, null /* text */);
Adrian Roosb1f427c2016-05-26 12:27:15 -07004861 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4862 return contentView;
4863 }
4864
Adrian Roos48d746a2016-04-12 14:57:28 -07004865 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004866 mBuilder.getMessagingLayoutResource(),
4867 false /* hasProgress */,
4868 title,
4869 null /* text */);
4870
4871 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4872 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4873
4874 // Make sure all rows are gone in case we reuse a view.
4875 for (int rowId : rowIds) {
4876 contentView.setViewVisibility(rowId, View.GONE);
4877 }
4878
4879 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004880 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
4881 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004882 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07004883 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004884
Adrian Roosfeafa052016-06-01 17:09:45 -07004885 int contractedChildId = View.NO_ID;
4886 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07004887 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4888 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4889 Message m = mMessages.get(firstMessage + i);
4890 int rowId = rowIds[i];
4891
4892 contentView.setViewVisibility(rowId, View.VISIBLE);
4893 contentView.setTextViewText(rowId, makeMessageLine(m));
4894
Adrian Roosfeafa052016-06-01 17:09:45 -07004895 if (contractedMessage == m) {
4896 contractedChildId = rowId;
4897 }
4898
Adrian Roosc1a80b02016-04-05 14:54:55 -07004899 i++;
4900 }
Adrian Roosfeafa052016-06-01 17:09:45 -07004901 // Record this here to allow transformation between the contracted and expanded views.
4902 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
4903 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04004904 return contentView;
4905 }
4906
Adrian Roosc1a80b02016-04-05 14:54:55 -07004907 private CharSequence makeMessageLine(Message m) {
4908 BidiFormatter bidi = BidiFormatter.getInstance();
4909 SpannableStringBuilder sb = new SpannableStringBuilder();
4910 if (TextUtils.isEmpty(m.mSender)) {
4911 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4912 sb.append(bidi.unicodeWrap(replyName),
4913 makeFontColorSpan(mBuilder.resolveContrastColor()),
4914 0 /* flags */);
4915 } else {
4916 sb.append(bidi.unicodeWrap(m.mSender),
4917 makeFontColorSpan(Color.BLACK),
4918 0 /* flags */);
4919 }
4920 CharSequence text = m.mText == null ? "" : m.mText;
4921 sb.append(" ").append(bidi.unicodeWrap(text));
4922 return sb;
4923 }
4924
Adrian Roosdedd1df2016-04-26 16:38:47 -07004925 /**
4926 * @hide
4927 */
4928 @Override
4929 public RemoteViews makeHeadsUpContentView() {
4930 Message m = findLatestIncomingMessage();
4931 CharSequence title = mConversationTitle != null
4932 ? mConversationTitle
4933 : (m == null) ? null : m.mSender;
4934 CharSequence text = (m == null)
4935 ? null
4936 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4937
4938 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4939 false /* hasProgress */,
4940 title,
4941 text);
4942 }
4943
Adrian Roosc1a80b02016-04-05 14:54:55 -07004944 private static TextAppearanceSpan makeFontColorSpan(int color) {
4945 return new TextAppearanceSpan(null, 0, 0,
4946 ColorStateList.valueOf(color), null);
4947 }
4948
Alex Hillsd9b04d92016-04-11 16:38:16 -04004949 public static final class Message {
4950
4951 static final String KEY_TEXT = "text";
4952 static final String KEY_TIMESTAMP = "time";
4953 static final String KEY_SENDER = "sender";
4954 static final String KEY_DATA_MIME_TYPE = "type";
4955 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004956
4957 private final CharSequence mText;
4958 private final long mTimestamp;
4959 private final CharSequence mSender;
4960
4961 private String mDataMimeType;
4962 private Uri mDataUri;
4963
4964 /**
4965 * Constructor
4966 * @param text A {@link CharSequence} to be displayed as the message content
4967 * @param timestamp Time at which the message arrived
4968 * @param sender A {@link CharSequence} to be used for displaying the name of the
4969 * sender. Should be <code>null</code> for messages by the current user, in which case
4970 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4971 * Should be unique amongst all individuals in the conversation, and should be
4972 * consistent during re-posts of the notification.
4973 */
4974 public Message(CharSequence text, long timestamp, CharSequence sender){
4975 mText = text;
4976 mTimestamp = timestamp;
4977 mSender = sender;
4978 }
4979
4980 /**
4981 * Sets a binary blob of data and an associated MIME type for a message. In the case
4982 * where the platform doesn't support the MIME type, the original text provided in the
4983 * constructor will be used.
4984 * @param dataMimeType The MIME type of the content. See
4985 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4986 * types on Android and Android Wear.
4987 * @param dataUri The uri containing the content whose type is given by the MIME type.
4988 * <p class="note">
4989 * <ol>
4990 * <li>Notification Listeners including the System UI need permission to access the
4991 * data the Uri points to. The recommended ways to do this are:</li>
4992 * <li>Store the data in your own ContentProvider, making sure that other apps have
4993 * the correct permission to access your provider. The preferred mechanism for
4994 * providing access is to use per-URI permissions which are temporary and only
4995 * grant access to the receiving application. An easy way to create a
4996 * ContentProvider like this is to use the FileProvider helper class.</li>
4997 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4998 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4999 * also store non-media types (see MediaStore.Files for more info). Files can be
5000 * inserted into the MediaStore using scanFile() after which a content:// style
5001 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5002 * Note that once added to the system MediaStore the content is accessible to any
5003 * app on the device.</li>
5004 * </ol>
5005 * @return this object for method chaining
5006 */
5007 public Message setData(String dataMimeType, Uri dataUri) {
5008 mDataMimeType = dataMimeType;
5009 mDataUri = dataUri;
5010 return this;
5011 }
5012
Alex Hillsfc737de2016-03-23 17:33:02 -04005013 /**
5014 * Get the text to be used for this message, or the fallback text if a type and content
5015 * Uri have been set
5016 */
5017 public CharSequence getText() {
5018 return mText;
5019 }
5020
5021 /**
5022 * Get the time at which this message arrived
5023 */
5024 public long getTimestamp() {
5025 return mTimestamp;
5026 }
5027
5028 /**
5029 * Get the text used to display the contact's name in the messaging experience
5030 */
5031 public CharSequence getSender() {
5032 return mSender;
5033 }
5034
5035 /**
5036 * Get the MIME type of the data pointed to by the Uri
5037 */
5038 public String getDataMimeType() {
5039 return mDataMimeType;
5040 }
5041
5042 /**
5043 * Get the the Uri pointing to the content of the message. Can be null, in which case
5044 * {@see #getText()} is used.
5045 */
5046 public Uri getDataUri() {
5047 return mDataUri;
5048 }
5049
Alex Hillsd9b04d92016-04-11 16:38:16 -04005050 private Bundle toBundle() {
5051 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005052 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005053 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005054 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005055 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005056 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005057 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005058 }
5059 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005060 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005061 }
5062 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005063 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005064 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005065 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005066 }
5067
Alex Hillsd9b04d92016-04-11 16:38:16 -04005068 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5069 Bundle[] bundles = new Bundle[messages.size()];
5070 final int N = messages.size();
5071 for (int i = 0; i < N; i++) {
5072 bundles[i] = messages.get(i).toBundle();
5073 }
5074 return bundles;
5075 }
5076
Adrian Roosdedd1df2016-04-26 16:38:47 -07005077 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005078 List<Message> messages = new ArrayList<>(bundles.length);
5079 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005080 if (bundles[i] instanceof Bundle) {
5081 Message message = getMessageFromBundle((Bundle)bundles[i]);
5082 if (message != null) {
5083 messages.add(message);
5084 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005085 }
5086 }
5087 return messages;
5088 }
5089
5090 static Message getMessageFromBundle(Bundle bundle) {
5091 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005092 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005093 return null;
5094 } else {
5095 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5096 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5097 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5098 bundle.containsKey(KEY_DATA_URI)) {
5099
5100 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5101 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005102 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005103 return message;
5104 }
5105 } catch (ClassCastException e) {
5106 return null;
5107 }
5108 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005109 }
5110 }
5111
5112 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005113 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005114 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005115 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005116 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005117 * Notification notif = new Notification.Builder(mContext)
5118 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5119 * .setContentText(subject)
5120 * .setSmallIcon(R.drawable.new_mail)
5121 * .setLargeIcon(aBitmap)
5122 * .setStyle(new Notification.InboxStyle()
5123 * .addLine(str1)
5124 * .addLine(str2)
5125 * .setContentTitle(&quot;&quot;)
5126 * .setSummaryText(&quot;+3 more&quot;))
5127 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005128 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005129 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005130 * @see Notification#bigContentView
5131 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005132 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005133 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5134
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005135 public InboxStyle() {
5136 }
5137
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005138 /**
5139 * @deprecated use {@code InboxStyle()}.
5140 */
5141 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005142 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005143 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005144 }
5145
Chris Wrend6297db2012-05-03 16:20:13 -04005146 /**
5147 * Overrides ContentTitle in the big form of the template.
5148 * This defaults to the value passed to setContentTitle().
5149 */
5150 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005151 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005152 return this;
5153 }
5154
5155 /**
5156 * Set the first line of text after the detail section in the big form of the template.
5157 */
5158 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005159 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005160 return this;
5161 }
5162
Chris Wren0bd664d2012-08-01 13:56:56 -04005163 /**
5164 * Append a line to the digest section of the Inbox notification.
5165 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005166 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005167 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005168 return this;
5169 }
5170
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005171 /**
5172 * @hide
5173 */
5174 public void addExtras(Bundle extras) {
5175 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005176
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005177 CharSequence[] a = new CharSequence[mTexts.size()];
5178 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5179 }
5180
Christoph Studer4600f9b2014-07-22 22:44:43 +02005181 /**
5182 * @hide
5183 */
5184 @Override
5185 protected void restoreFromExtras(Bundle extras) {
5186 super.restoreFromExtras(extras);
5187
5188 mTexts.clear();
5189 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5190 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5191 }
5192 }
5193
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005194 /**
5195 * @hide
5196 */
5197 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005198 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005199 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005200 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5201 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005202
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005203 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005204
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005205 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005206
Chris Wrend6297db2012-05-03 16:20:13 -04005207 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 -04005208 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005209
Chris Wren4ed80d52012-05-17 09:30:03 -04005210 // Make sure all rows are gone in case we reuse a view.
5211 for (int rowId : rowIds) {
5212 contentView.setViewVisibility(rowId, View.GONE);
5213 }
5214
Daniel Sandler879c5e02012-04-17 16:46:51 -04005215 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005216 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5217 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005218 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005219 int onlyViewId = 0;
5220 int maxRows = rowIds.length;
5221 if (mBuilder.mActions.size() > 0) {
5222 maxRows--;
5223 }
5224 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005225 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005226 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005227 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005228 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005229 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005230 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005231 if (first) {
5232 onlyViewId = rowIds[i];
5233 } else {
5234 onlyViewId = 0;
5235 }
Selim Cinek247fa012016-02-18 09:50:48 -08005236 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005237 }
5238 i++;
5239 }
Selim Cinek07c80172016-04-21 16:40:47 -07005240 if (onlyViewId != 0) {
5241 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5242 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5243 R.dimen.notification_text_margin_top);
5244 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5245 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005246
Daniel Sandler879c5e02012-04-17 16:46:51 -04005247 return contentView;
5248 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005249
Selim Cinek247fa012016-02-18 09:50:48 -08005250 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005251 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005252 if (first) {
5253 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5254 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5255 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005256 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005257 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005258 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005259 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005260 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005261 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005262 }
Dan Sandler842dd772014-05-15 09:36:47 -04005263
5264 /**
5265 * Notification style for media playback notifications.
5266 *
5267 * In the expanded form, {@link Notification#bigContentView}, up to 5
5268 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005269 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005270 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5271 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5272 * treated as album artwork.
5273 *
5274 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5275 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005276 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005277 * in the standard view alongside the usual content.
5278 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005279 * Notifications created with MediaStyle will have their category set to
5280 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5281 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5282 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005283 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5284 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005285 * the System UI can identify this as a notification representing an active media session
5286 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5287 *
5288 * To use this style with your Notification, feed it to
5289 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5290 * <pre class="prettyprint">
5291 * Notification noti = new Notification.Builder()
5292 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005293 * .setContentTitle(&quot;Track title&quot;)
5294 * .setContentText(&quot;Artist - Album&quot;)
5295 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005296 * .setStyle(<b>new Notification.MediaStyle()</b>
5297 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005298 * .build();
5299 * </pre>
5300 *
5301 * @see Notification#bigContentView
5302 */
5303 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005304 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005305 static final int MAX_MEDIA_BUTTONS = 5;
5306
5307 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005308 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005309
5310 public MediaStyle() {
5311 }
5312
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005313 /**
5314 * @deprecated use {@code MediaStyle()}.
5315 */
5316 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005317 public MediaStyle(Builder builder) {
5318 setBuilder(builder);
5319 }
5320
5321 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005322 * 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 -04005323 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005324 *
5325 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005326 */
5327 public MediaStyle setShowActionsInCompactView(int...actions) {
5328 mActionsToShowInCompact = actions;
5329 return this;
5330 }
5331
5332 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005333 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5334 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005335 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005336 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005337 mToken = token;
5338 return this;
5339 }
5340
Christoph Studer4600f9b2014-07-22 22:44:43 +02005341 /**
5342 * @hide
5343 */
Dan Sandler842dd772014-05-15 09:36:47 -04005344 @Override
5345 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005346 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005347 if (wip.category == null) {
5348 wip.category = Notification.CATEGORY_TRANSPORT;
5349 }
Dan Sandler842dd772014-05-15 09:36:47 -04005350 return wip;
5351 }
5352
Christoph Studer4600f9b2014-07-22 22:44:43 +02005353 /**
5354 * @hide
5355 */
5356 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005357 public RemoteViews makeContentView() {
5358 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005359 }
5360
5361 /**
5362 * @hide
5363 */
5364 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005365 public RemoteViews makeBigContentView() {
5366 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005367 }
5368
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005369 /**
5370 * @hide
5371 */
5372 @Override
5373 public RemoteViews makeHeadsUpContentView() {
5374 RemoteViews expanded = makeMediaBigContentView();
5375 return expanded != null ? expanded : makeMediaContentView();
5376 }
5377
Dan Sandler842dd772014-05-15 09:36:47 -04005378 /** @hide */
5379 @Override
5380 public void addExtras(Bundle extras) {
5381 super.addExtras(extras);
5382
5383 if (mToken != null) {
5384 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5385 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005386 if (mActionsToShowInCompact != null) {
5387 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5388 }
Dan Sandler842dd772014-05-15 09:36:47 -04005389 }
5390
Christoph Studer4600f9b2014-07-22 22:44:43 +02005391 /**
5392 * @hide
5393 */
5394 @Override
5395 protected void restoreFromExtras(Bundle extras) {
5396 super.restoreFromExtras(extras);
5397
5398 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5399 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5400 }
5401 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5402 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5403 }
5404 }
5405
Selim Cinek5bf069a2015-11-10 19:14:27 -05005406 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005407 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005408 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005409 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005410 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005411 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5412 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005413 if (!tombstone) {
5414 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5415 }
5416 button.setContentDescription(R.id.action0, action.title);
5417 return button;
5418 }
5419
5420 private RemoteViews makeMediaContentView() {
5421 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005422 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005423
5424 final int numActions = mBuilder.mActions.size();
5425 final int N = mActionsToShowInCompact == null
5426 ? 0
5427 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5428 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005429 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005430 for (int i = 0; i < N; i++) {
5431 if (i >= numActions) {
5432 throw new IllegalArgumentException(String.format(
5433 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5434 i, numActions - 1));
5435 }
5436
5437 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005438 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005439 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005440 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005441 }
5442 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005443 handleImage(view);
5444 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005445 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005446 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005447 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005448 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005449 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005450 return view;
5451 }
5452
5453 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005454 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005455 // Dont add an expanded view if there is no more content to be revealed
5456 int actionsInCompact = mActionsToShowInCompact == null
5457 ? 0
5458 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005459 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005460 return null;
5461 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005462 RemoteViews big = mBuilder.applyStandardTemplate(
5463 R.layout.notification_template_material_big_media,
5464 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005465
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005466 if (actionCount > 0) {
5467 big.removeAllViews(com.android.internal.R.id.media_actions);
5468 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005469 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005470 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005471 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005472 }
5473 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005474 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005475 return big;
5476 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005477
Selim Cinek5bf069a2015-11-10 19:14:27 -05005478 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005479 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005480 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5481 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005482 }
5483 }
5484
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005485 /**
5486 * @hide
5487 */
5488 @Override
5489 protected boolean hasProgress() {
5490 return false;
5491 }
Dan Sandler842dd772014-05-15 09:36:47 -04005492 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005493
Selim Cinek593610c2016-02-16 18:42:57 -08005494 /**
5495 * Notification style for custom views that are decorated by the system
5496 *
5497 * <p>Instead of providing a notification that is completely custom, a developer can set this
5498 * style and still obtain system decorations like the notification header with the expand
5499 * affordance and actions.
5500 *
5501 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5502 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5503 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5504 * corresponding custom views to display.
5505 *
5506 * To use this style with your Notification, feed it to
5507 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5508 * <pre class="prettyprint">
5509 * Notification noti = new Notification.Builder()
5510 * .setSmallIcon(R.drawable.ic_stat_player)
5511 * .setLargeIcon(albumArtBitmap))
5512 * .setCustomContentView(contentView);
5513 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5514 * .build();
5515 * </pre>
5516 */
5517 public static class DecoratedCustomViewStyle extends Style {
5518
5519 public DecoratedCustomViewStyle() {
5520 }
5521
Selim Cinek593610c2016-02-16 18:42:57 -08005522 /**
5523 * @hide
5524 */
5525 public boolean displayCustomViewInline() {
5526 return true;
5527 }
5528
5529 /**
5530 * @hide
5531 */
5532 @Override
5533 public RemoteViews makeContentView() {
5534 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5535 }
5536
5537 /**
5538 * @hide
5539 */
5540 @Override
5541 public RemoteViews makeBigContentView() {
5542 return makeDecoratedBigContentView();
5543 }
5544
5545 /**
5546 * @hide
5547 */
5548 @Override
5549 public RemoteViews makeHeadsUpContentView() {
5550 return makeDecoratedHeadsUpContentView();
5551 }
5552
Selim Cinek593610c2016-02-16 18:42:57 -08005553 private RemoteViews makeDecoratedHeadsUpContentView() {
5554 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5555 ? mBuilder.mN.contentView
5556 : mBuilder.mN.headsUpContentView;
5557 if (mBuilder.mActions.size() == 0) {
5558 return makeStandardTemplateWithCustomContent(headsUpContentView);
5559 }
5560 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5561 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005562 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005563 return remoteViews;
5564 }
5565
Selim Cinek593610c2016-02-16 18:42:57 -08005566 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5567 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5568 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005569 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005570 return remoteViews;
5571 }
5572
Selim Cinek593610c2016-02-16 18:42:57 -08005573 private RemoteViews makeDecoratedBigContentView() {
5574 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5575 ? mBuilder.mN.contentView
5576 : mBuilder.mN.bigContentView;
5577 if (mBuilder.mActions.size() == 0) {
5578 return makeStandardTemplateWithCustomContent(bigContentView);
5579 }
5580 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5581 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005582 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005583 return remoteViews;
5584 }
Selim Cinek247fa012016-02-18 09:50:48 -08005585
5586 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5587 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005588 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005589 // Need to clone customContent before adding, because otherwise it can no longer be
5590 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005591 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005592 remoteViews.removeAllViews(R.id.notification_main_column);
5593 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005594 }
Selim Cinek247fa012016-02-18 09:50:48 -08005595 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005596 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005597 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005598 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005599 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005600 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005601 }
Selim Cinek593610c2016-02-16 18:42:57 -08005602 }
5603
Selim Cinek03eb3b72016-02-18 10:39:45 -08005604 /**
5605 * Notification style for media custom views that are decorated by the system
5606 *
5607 * <p>Instead of providing a media notification that is completely custom, a developer can set
5608 * this style and still obtain system decorations like the notification header with the expand
5609 * affordance and actions.
5610 *
5611 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5612 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5613 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5614 * corresponding custom views to display.
5615 *
5616 * To use this style with your Notification, feed it to
5617 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5618 * <pre class="prettyprint">
5619 * Notification noti = new Notification.Builder()
5620 * .setSmallIcon(R.drawable.ic_stat_player)
5621 * .setLargeIcon(albumArtBitmap))
5622 * .setCustomContentView(contentView);
5623 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5624 * .setMediaSession(mySession))
5625 * .build();
5626 * </pre>
5627 *
5628 * @see android.app.Notification.DecoratedCustomViewStyle
5629 * @see android.app.Notification.MediaStyle
5630 */
5631 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5632
5633 public DecoratedMediaCustomViewStyle() {
5634 }
5635
Selim Cinek03eb3b72016-02-18 10:39:45 -08005636 /**
5637 * @hide
5638 */
5639 public boolean displayCustomViewInline() {
5640 return true;
5641 }
5642
5643 /**
5644 * @hide
5645 */
5646 @Override
5647 public RemoteViews makeContentView() {
5648 RemoteViews remoteViews = super.makeContentView();
5649 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5650 mBuilder.mN.contentView);
5651 }
5652
5653 /**
5654 * @hide
5655 */
5656 @Override
5657 public RemoteViews makeBigContentView() {
5658 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5659 ? mBuilder.mN.bigContentView
5660 : mBuilder.mN.contentView;
5661 return makeBigContentViewWithCustomContent(customRemoteView);
5662 }
5663
5664 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5665 RemoteViews remoteViews = super.makeBigContentView();
5666 if (remoteViews != null) {
5667 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5668 customRemoteView);
5669 } else if (customRemoteView != mBuilder.mN.contentView){
5670 remoteViews = super.makeContentView();
5671 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5672 customRemoteView);
5673 } else {
5674 return null;
5675 }
5676 }
5677
5678 /**
5679 * @hide
5680 */
5681 @Override
5682 public RemoteViews makeHeadsUpContentView() {
5683 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5684 ? mBuilder.mN.headsUpContentView
5685 : mBuilder.mN.contentView;
5686 return makeBigContentViewWithCustomContent(customRemoteView);
5687 }
5688
5689 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5690 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005691 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005692 // Need to clone customContent before adding, because otherwise it can no longer be
5693 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005694 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005695 remoteViews.removeAllViews(id);
5696 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005697 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005698 return remoteViews;
5699 }
5700 }
5701
Christoph Studer4600f9b2014-07-22 22:44:43 +02005702 // When adding a new Style subclass here, don't forget to update
5703 // Builder.getNotificationStyleClass.
5704
Griff Hazen61a9e862014-05-22 16:05:19 -07005705 /**
5706 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5707 * metadata or change options on a notification builder.
5708 */
5709 public interface Extender {
5710 /**
5711 * Apply this extender to a notification builder.
5712 * @param builder the builder to be modified.
5713 * @return the build object for chaining.
5714 */
5715 public Builder extend(Builder builder);
5716 }
5717
5718 /**
5719 * Helper class to add wearable extensions to notifications.
5720 * <p class="note"> See
5721 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5722 * for Android Wear</a> for more information on how to use this class.
5723 * <p>
5724 * To create a notification with wearable extensions:
5725 * <ol>
5726 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5727 * properties.
5728 * <li>Create a {@link android.app.Notification.WearableExtender}.
5729 * <li>Set wearable-specific properties using the
5730 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5731 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5732 * notification.
5733 * <li>Post the notification to the notification system with the
5734 * {@code NotificationManager.notify(...)} methods.
5735 * </ol>
5736 *
5737 * <pre class="prettyprint">
5738 * Notification notif = new Notification.Builder(mContext)
5739 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5740 * .setContentText(subject)
5741 * .setSmallIcon(R.drawable.new_mail)
5742 * .extend(new Notification.WearableExtender()
5743 * .setContentIcon(R.drawable.new_mail))
5744 * .build();
5745 * NotificationManager notificationManger =
5746 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5747 * notificationManger.notify(0, notif);</pre>
5748 *
5749 * <p>Wearable extensions can be accessed on an existing notification by using the
5750 * {@code WearableExtender(Notification)} constructor,
5751 * and then using the {@code get} methods to access values.
5752 *
5753 * <pre class="prettyprint">
5754 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5755 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005756 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005757 */
5758 public static final class WearableExtender implements Extender {
5759 /**
5760 * Sentinel value for an action index that is unset.
5761 */
5762 public static final int UNSET_ACTION_INDEX = -1;
5763
5764 /**
5765 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5766 * default sizing.
5767 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005768 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005769 * on their content.
5770 */
5771 public static final int SIZE_DEFAULT = 0;
5772
5773 /**
5774 * Size value for use with {@link #setCustomSizePreset} to show this notification
5775 * with an extra small size.
5776 * <p>This value is only applicable for custom display notifications created using
5777 * {@link #setDisplayIntent}.
5778 */
5779 public static final int SIZE_XSMALL = 1;
5780
5781 /**
5782 * Size value for use with {@link #setCustomSizePreset} to show this notification
5783 * with a small size.
5784 * <p>This value is only applicable for custom display notifications created using
5785 * {@link #setDisplayIntent}.
5786 */
5787 public static final int SIZE_SMALL = 2;
5788
5789 /**
5790 * Size value for use with {@link #setCustomSizePreset} to show this notification
5791 * with a medium size.
5792 * <p>This value is only applicable for custom display notifications created using
5793 * {@link #setDisplayIntent}.
5794 */
5795 public static final int SIZE_MEDIUM = 3;
5796
5797 /**
5798 * Size value for use with {@link #setCustomSizePreset} to show this notification
5799 * with a large size.
5800 * <p>This value is only applicable for custom display notifications created using
5801 * {@link #setDisplayIntent}.
5802 */
5803 public static final int SIZE_LARGE = 4;
5804
Griff Hazend5f11f92014-05-27 15:40:09 -07005805 /**
5806 * Size value for use with {@link #setCustomSizePreset} to show this notification
5807 * full screen.
5808 * <p>This value is only applicable for custom display notifications created using
5809 * {@link #setDisplayIntent}.
5810 */
5811 public static final int SIZE_FULL_SCREEN = 5;
5812
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005813 /**
5814 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5815 * short amount of time when this notification is displayed on the screen. This
5816 * is the default value.
5817 */
5818 public static final int SCREEN_TIMEOUT_SHORT = 0;
5819
5820 /**
5821 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5822 * for a longer amount of time when this notification is displayed on the screen.
5823 */
5824 public static final int SCREEN_TIMEOUT_LONG = -1;
5825
Griff Hazen61a9e862014-05-22 16:05:19 -07005826 /** Notification extra which contains wearable extensions */
5827 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5828
Pete Gastaf6781d2014-10-07 15:17:05 -04005829 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005830 private static final String KEY_ACTIONS = "actions";
5831 private static final String KEY_FLAGS = "flags";
5832 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5833 private static final String KEY_PAGES = "pages";
5834 private static final String KEY_BACKGROUND = "background";
5835 private static final String KEY_CONTENT_ICON = "contentIcon";
5836 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5837 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5838 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5839 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5840 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005841 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005842 private static final String KEY_DISMISSAL_ID = "dismissalId";
Griff Hazen61a9e862014-05-22 16:05:19 -07005843
5844 // Flags bitwise-ored to mFlags
5845 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5846 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5847 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5848 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005849 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005850 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005851 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005852
5853 // Default value for flags integer
5854 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5855
5856 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5857 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5858
5859 private ArrayList<Action> mActions = new ArrayList<Action>();
5860 private int mFlags = DEFAULT_FLAGS;
5861 private PendingIntent mDisplayIntent;
5862 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5863 private Bitmap mBackground;
5864 private int mContentIcon;
5865 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5866 private int mContentActionIndex = UNSET_ACTION_INDEX;
5867 private int mCustomSizePreset = SIZE_DEFAULT;
5868 private int mCustomContentHeight;
5869 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005870 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005871 private String mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005872
5873 /**
5874 * Create a {@link android.app.Notification.WearableExtender} with default
5875 * options.
5876 */
5877 public WearableExtender() {
5878 }
5879
5880 public WearableExtender(Notification notif) {
5881 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5882 if (wearableBundle != null) {
5883 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5884 if (actions != null) {
5885 mActions.addAll(actions);
5886 }
5887
5888 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5889 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5890
5891 Notification[] pages = getNotificationArrayFromBundle(
5892 wearableBundle, KEY_PAGES);
5893 if (pages != null) {
5894 Collections.addAll(mPages, pages);
5895 }
5896
5897 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5898 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5899 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5900 DEFAULT_CONTENT_ICON_GRAVITY);
5901 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5902 UNSET_ACTION_INDEX);
5903 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5904 SIZE_DEFAULT);
5905 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5906 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005907 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005908 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Griff Hazen61a9e862014-05-22 16:05:19 -07005909 }
5910 }
5911
5912 /**
5913 * Apply wearable extensions to a notification that is being built. This is typically
5914 * called by the {@link android.app.Notification.Builder#extend} method of
5915 * {@link android.app.Notification.Builder}.
5916 */
5917 @Override
5918 public Notification.Builder extend(Notification.Builder builder) {
5919 Bundle wearableBundle = new Bundle();
5920
5921 if (!mActions.isEmpty()) {
5922 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5923 }
5924 if (mFlags != DEFAULT_FLAGS) {
5925 wearableBundle.putInt(KEY_FLAGS, mFlags);
5926 }
5927 if (mDisplayIntent != null) {
5928 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5929 }
5930 if (!mPages.isEmpty()) {
5931 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5932 new Notification[mPages.size()]));
5933 }
5934 if (mBackground != null) {
5935 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5936 }
5937 if (mContentIcon != 0) {
5938 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5939 }
5940 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5941 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5942 }
5943 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5944 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5945 mContentActionIndex);
5946 }
5947 if (mCustomSizePreset != SIZE_DEFAULT) {
5948 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5949 }
5950 if (mCustomContentHeight != 0) {
5951 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5952 }
5953 if (mGravity != DEFAULT_GRAVITY) {
5954 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5955 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005956 if (mHintScreenTimeout != 0) {
5957 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5958 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005959 if (mDismissalId != null) {
5960 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5961 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005962
5963 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5964 return builder;
5965 }
5966
5967 @Override
5968 public WearableExtender clone() {
5969 WearableExtender that = new WearableExtender();
5970 that.mActions = new ArrayList<Action>(this.mActions);
5971 that.mFlags = this.mFlags;
5972 that.mDisplayIntent = this.mDisplayIntent;
5973 that.mPages = new ArrayList<Notification>(this.mPages);
5974 that.mBackground = this.mBackground;
5975 that.mContentIcon = this.mContentIcon;
5976 that.mContentIconGravity = this.mContentIconGravity;
5977 that.mContentActionIndex = this.mContentActionIndex;
5978 that.mCustomSizePreset = this.mCustomSizePreset;
5979 that.mCustomContentHeight = this.mCustomContentHeight;
5980 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005981 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005982 that.mDismissalId = this.mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005983 return that;
5984 }
5985
5986 /**
5987 * Add a wearable action to this notification.
5988 *
5989 * <p>When wearable actions are added using this method, the set of actions that
5990 * show on a wearable device splits from devices that only show actions added
5991 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5992 * of which actions display on different devices.
5993 *
5994 * @param action the action to add to this notification
5995 * @return this object for method chaining
5996 * @see android.app.Notification.Action
5997 */
5998 public WearableExtender addAction(Action action) {
5999 mActions.add(action);
6000 return this;
6001 }
6002
6003 /**
6004 * Adds wearable actions to this notification.
6005 *
6006 * <p>When wearable actions are added using this method, the set of actions that
6007 * show on a wearable device splits from devices that only show actions added
6008 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6009 * of which actions display on different devices.
6010 *
6011 * @param actions the actions to add to this notification
6012 * @return this object for method chaining
6013 * @see android.app.Notification.Action
6014 */
6015 public WearableExtender addActions(List<Action> actions) {
6016 mActions.addAll(actions);
6017 return this;
6018 }
6019
6020 /**
6021 * Clear all wearable actions present on this builder.
6022 * @return this object for method chaining.
6023 * @see #addAction
6024 */
6025 public WearableExtender clearActions() {
6026 mActions.clear();
6027 return this;
6028 }
6029
6030 /**
6031 * Get the wearable actions present on this notification.
6032 */
6033 public List<Action> getActions() {
6034 return mActions;
6035 }
6036
6037 /**
6038 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006039 * this notification. The {@link PendingIntent} provided should be for an activity.
6040 *
6041 * <pre class="prettyprint">
6042 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6043 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6044 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6045 * Notification notif = new Notification.Builder(context)
6046 * .extend(new Notification.WearableExtender()
6047 * .setDisplayIntent(displayPendingIntent)
6048 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6049 * .build();</pre>
6050 *
6051 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006052 * should have an empty task affinity. It is also recommended to use the device
6053 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006054 *
6055 * <p>Example AndroidManifest.xml entry:
6056 * <pre class="prettyprint">
6057 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6058 * android:exported=&quot;true&quot;
6059 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006060 * android:taskAffinity=&quot;&quot;
6061 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006062 *
6063 * @param intent the {@link PendingIntent} for an activity
6064 * @return this object for method chaining
6065 * @see android.app.Notification.WearableExtender#getDisplayIntent
6066 */
6067 public WearableExtender setDisplayIntent(PendingIntent intent) {
6068 mDisplayIntent = intent;
6069 return this;
6070 }
6071
6072 /**
6073 * Get the intent to launch inside of an activity view when displaying this
6074 * notification. This {@code PendingIntent} should be for an activity.
6075 */
6076 public PendingIntent getDisplayIntent() {
6077 return mDisplayIntent;
6078 }
6079
6080 /**
6081 * Add an additional page of content to display with this notification. The current
6082 * notification forms the first page, and pages added using this function form
6083 * subsequent pages. This field can be used to separate a notification into multiple
6084 * sections.
6085 *
6086 * @param page the notification to add as another page
6087 * @return this object for method chaining
6088 * @see android.app.Notification.WearableExtender#getPages
6089 */
6090 public WearableExtender addPage(Notification page) {
6091 mPages.add(page);
6092 return this;
6093 }
6094
6095 /**
6096 * Add additional pages of content to display with this notification. The current
6097 * notification forms the first page, and pages added using this function form
6098 * subsequent pages. This field can be used to separate a notification into multiple
6099 * sections.
6100 *
6101 * @param pages a list of notifications
6102 * @return this object for method chaining
6103 * @see android.app.Notification.WearableExtender#getPages
6104 */
6105 public WearableExtender addPages(List<Notification> pages) {
6106 mPages.addAll(pages);
6107 return this;
6108 }
6109
6110 /**
6111 * Clear all additional pages present on this builder.
6112 * @return this object for method chaining.
6113 * @see #addPage
6114 */
6115 public WearableExtender clearPages() {
6116 mPages.clear();
6117 return this;
6118 }
6119
6120 /**
6121 * Get the array of additional pages of content for displaying this notification. The
6122 * current notification forms the first page, and elements within this array form
6123 * subsequent pages. This field can be used to separate a notification into multiple
6124 * sections.
6125 * @return the pages for this notification
6126 */
6127 public List<Notification> getPages() {
6128 return mPages;
6129 }
6130
6131 /**
6132 * Set a background image to be displayed behind the notification content.
6133 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6134 * will work with any notification style.
6135 *
6136 * @param background the background bitmap
6137 * @return this object for method chaining
6138 * @see android.app.Notification.WearableExtender#getBackground
6139 */
6140 public WearableExtender setBackground(Bitmap background) {
6141 mBackground = background;
6142 return this;
6143 }
6144
6145 /**
6146 * Get a background image to be displayed behind the notification content.
6147 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6148 * will work with any notification style.
6149 *
6150 * @return the background image
6151 * @see android.app.Notification.WearableExtender#setBackground
6152 */
6153 public Bitmap getBackground() {
6154 return mBackground;
6155 }
6156
6157 /**
6158 * Set an icon that goes with the content of this notification.
6159 */
6160 public WearableExtender setContentIcon(int icon) {
6161 mContentIcon = icon;
6162 return this;
6163 }
6164
6165 /**
6166 * Get an icon that goes with the content of this notification.
6167 */
6168 public int getContentIcon() {
6169 return mContentIcon;
6170 }
6171
6172 /**
6173 * Set the gravity that the content icon should have within the notification display.
6174 * Supported values include {@link android.view.Gravity#START} and
6175 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6176 * @see #setContentIcon
6177 */
6178 public WearableExtender setContentIconGravity(int contentIconGravity) {
6179 mContentIconGravity = contentIconGravity;
6180 return this;
6181 }
6182
6183 /**
6184 * Get the gravity that the content icon should have within the notification display.
6185 * Supported values include {@link android.view.Gravity#START} and
6186 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6187 * @see #getContentIcon
6188 */
6189 public int getContentIconGravity() {
6190 return mContentIconGravity;
6191 }
6192
6193 /**
6194 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006195 * this notification. This action will no longer display separately from the
6196 * notification's content.
6197 *
Griff Hazenca48d352014-05-28 22:37:13 -07006198 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006199 * set, although the list of available actions comes from the main notification and not
6200 * from the child page's notification.
6201 *
6202 * @param actionIndex The index of the action to hoist onto the current notification page.
6203 * If wearable actions were added to the main notification, this index
6204 * will apply to that list, otherwise it will apply to the regular
6205 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006206 */
6207 public WearableExtender setContentAction(int actionIndex) {
6208 mContentActionIndex = actionIndex;
6209 return this;
6210 }
6211
6212 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006213 * Get the index of the notification action, if any, that was specified as being clickable
6214 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006215 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006216 *
Griff Hazenca48d352014-05-28 22:37:13 -07006217 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006218 * set, although the list of available actions comes from the main notification and not
6219 * from the child page's notification.
6220 *
6221 * <p>If wearable specific actions were added to the main notification, this index will
6222 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006223 *
6224 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006225 */
6226 public int getContentAction() {
6227 return mContentActionIndex;
6228 }
6229
6230 /**
6231 * Set the gravity that this notification should have within the available viewport space.
6232 * Supported values include {@link android.view.Gravity#TOP},
6233 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6234 * The default value is {@link android.view.Gravity#BOTTOM}.
6235 */
6236 public WearableExtender setGravity(int gravity) {
6237 mGravity = gravity;
6238 return this;
6239 }
6240
6241 /**
6242 * Get the gravity that this notification should have within the available viewport space.
6243 * Supported values include {@link android.view.Gravity#TOP},
6244 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6245 * The default value is {@link android.view.Gravity#BOTTOM}.
6246 */
6247 public int getGravity() {
6248 return mGravity;
6249 }
6250
6251 /**
6252 * Set the custom size preset for the display of this notification out of the available
6253 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6254 * {@link #SIZE_LARGE}.
6255 * <p>Some custom size presets are only applicable for custom display notifications created
6256 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6257 * documentation for the preset in question. See also
6258 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6259 */
6260 public WearableExtender setCustomSizePreset(int sizePreset) {
6261 mCustomSizePreset = sizePreset;
6262 return this;
6263 }
6264
6265 /**
6266 * Get the custom size preset for the display of this notification out of the available
6267 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6268 * {@link #SIZE_LARGE}.
6269 * <p>Some custom size presets are only applicable for custom display notifications created
6270 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6271 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6272 */
6273 public int getCustomSizePreset() {
6274 return mCustomSizePreset;
6275 }
6276
6277 /**
6278 * Set the custom height in pixels for the display of this notification's content.
6279 * <p>This option is only available for custom display notifications created
6280 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6281 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6282 * {@link #getCustomContentHeight}.
6283 */
6284 public WearableExtender setCustomContentHeight(int height) {
6285 mCustomContentHeight = height;
6286 return this;
6287 }
6288
6289 /**
6290 * Get the custom height in pixels for the display of this notification's content.
6291 * <p>This option is only available for custom display notifications created
6292 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6293 * {@link #setCustomContentHeight}.
6294 */
6295 public int getCustomContentHeight() {
6296 return mCustomContentHeight;
6297 }
6298
6299 /**
6300 * Set whether the scrolling position for the contents of this notification should start
6301 * at the bottom of the contents instead of the top when the contents are too long to
6302 * display within the screen. Default is false (start scroll at the top).
6303 */
6304 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6305 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6306 return this;
6307 }
6308
6309 /**
6310 * Get whether the scrolling position for the contents of this notification should start
6311 * at the bottom of the contents instead of the top when the contents are too long to
6312 * display within the screen. Default is false (start scroll at the top).
6313 */
6314 public boolean getStartScrollBottom() {
6315 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6316 }
6317
6318 /**
6319 * Set whether the content intent is available when the wearable device is not connected
6320 * to a companion device. The user can still trigger this intent when the wearable device
6321 * is offline, but a visual hint will indicate that the content intent may not be available.
6322 * Defaults to true.
6323 */
6324 public WearableExtender setContentIntentAvailableOffline(
6325 boolean contentIntentAvailableOffline) {
6326 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6327 return this;
6328 }
6329
6330 /**
6331 * Get whether the content intent is available when the wearable device is not connected
6332 * to a companion device. The user can still trigger this intent when the wearable device
6333 * is offline, but a visual hint will indicate that the content intent may not be available.
6334 * Defaults to true.
6335 */
6336 public boolean getContentIntentAvailableOffline() {
6337 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6338 }
6339
6340 /**
6341 * Set a hint that this notification's icon should not be displayed.
6342 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6343 * @return this object for method chaining
6344 */
6345 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6346 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6347 return this;
6348 }
6349
6350 /**
6351 * Get a hint that this notification's icon should not be displayed.
6352 * @return {@code true} if this icon should not be displayed, false otherwise.
6353 * The default value is {@code false} if this was never set.
6354 */
6355 public boolean getHintHideIcon() {
6356 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6357 }
6358
6359 /**
6360 * Set a visual hint that only the background image of this notification should be
6361 * displayed, and other semantic content should be hidden. This hint is only applicable
6362 * to sub-pages added using {@link #addPage}.
6363 */
6364 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6365 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6366 return this;
6367 }
6368
6369 /**
6370 * Get a visual hint that only the background image of this notification should be
6371 * displayed, and other semantic content should be hidden. This hint is only applicable
6372 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6373 */
6374 public boolean getHintShowBackgroundOnly() {
6375 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6376 }
6377
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006378 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006379 * Set a hint that this notification's background should not be clipped if possible,
6380 * and should instead be resized to fully display on the screen, retaining the aspect
6381 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006382 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6383 * @return this object for method chaining
6384 */
6385 public WearableExtender setHintAvoidBackgroundClipping(
6386 boolean hintAvoidBackgroundClipping) {
6387 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6388 return this;
6389 }
6390
6391 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006392 * Get a hint that this notification's background should not be clipped if possible,
6393 * and should instead be resized to fully display on the screen, retaining the aspect
6394 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006395 * @return {@code true} if it's ok if the background is clipped on the screen, false
6396 * otherwise. The default value is {@code false} if this was never set.
6397 */
6398 public boolean getHintAvoidBackgroundClipping() {
6399 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6400 }
6401
6402 /**
6403 * Set a hint that the screen should remain on for at least this duration when
6404 * this notification is displayed on the screen.
6405 * @param timeout The requested screen timeout in milliseconds. Can also be either
6406 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6407 * @return this object for method chaining
6408 */
6409 public WearableExtender setHintScreenTimeout(int timeout) {
6410 mHintScreenTimeout = timeout;
6411 return this;
6412 }
6413
6414 /**
6415 * Get the duration, in milliseconds, that the screen should remain on for
6416 * when this notification is displayed.
6417 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6418 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6419 */
6420 public int getHintScreenTimeout() {
6421 return mHintScreenTimeout;
6422 }
6423
Alex Hills9ab3a232016-04-05 14:54:56 -04006424 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006425 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6426 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6427 * qr codes, as well as other simple black-and-white tickets.
6428 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6429 * @return this object for method chaining
6430 */
6431 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6432 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6433 return this;
6434 }
6435
6436 /**
6437 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6438 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6439 * qr codes, as well as other simple black-and-white tickets.
6440 * @return {@code true} if it should be displayed in ambient, false otherwise
6441 * otherwise. The default value is {@code false} if this was never set.
6442 */
6443 public boolean getHintAmbientBigPicture() {
6444 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6445 }
6446
6447 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006448 * Set a hint that this notification's content intent will launch an {@link Activity}
6449 * directly, telling the platform that it can generate the appropriate transitions.
6450 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6451 * an activity and transitions should be generated, false otherwise.
6452 * @return this object for method chaining
6453 */
6454 public WearableExtender setHintContentIntentLaunchesActivity(
6455 boolean hintContentIntentLaunchesActivity) {
6456 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6457 return this;
6458 }
6459
6460 /**
6461 * Get a hint that this notification's content intent will launch an {@link Activity}
6462 * directly, telling the platform that it can generate the appropriate transitions
6463 * @return {@code true} if the content intent will launch an activity and transitions should
6464 * be generated, false otherwise. The default value is {@code false} if this was never set.
6465 */
6466 public boolean getHintContentIntentLaunchesActivity() {
6467 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6468 }
6469
Nadia Benbernou948627e2016-04-14 14:41:08 -04006470 /**
6471 * When you post a notification, if you set the dismissal id field, then when that
6472 * notification is canceled, notifications on other wearables and the paired Android phone
6473 * having that same dismissal id will also be canceled. Note that this only works if you
6474 * have notification bridge mode set to NO_BRIDGING in your Wear app manifest. See
6475 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
6476 * Notifications</a> for more information on how to use the bridge mode feature.
6477 * @param dismissalId the dismissal id of the notification.
6478 * @return this object for method chaining
6479 */
6480 public WearableExtender setDismissalId(String dismissalId) {
6481 mDismissalId = dismissalId;
6482 return this;
6483 }
6484
6485 /**
6486 * Returns the dismissal id of the notification.
6487 * @return the dismissal id of the notification or null if it has not been set.
6488 */
6489 public String getDismissalId() {
6490 return mDismissalId;
6491 }
6492
Griff Hazen61a9e862014-05-22 16:05:19 -07006493 private void setFlag(int mask, boolean value) {
6494 if (value) {
6495 mFlags |= mask;
6496 } else {
6497 mFlags &= ~mask;
6498 }
6499 }
6500 }
6501
6502 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006503 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6504 * with car extensions:
6505 *
6506 * <ol>
6507 * <li>Create an {@link Notification.Builder}, setting any desired
6508 * properties.
6509 * <li>Create a {@link CarExtender}.
6510 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6511 * {@link CarExtender}.
6512 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6513 * to apply the extensions to a notification.
6514 * </ol>
6515 *
6516 * <pre class="prettyprint">
6517 * Notification notification = new Notification.Builder(context)
6518 * ...
6519 * .extend(new CarExtender()
6520 * .set*(...))
6521 * .build();
6522 * </pre>
6523 *
6524 * <p>Car extensions can be accessed on an existing notification by using the
6525 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6526 * to access values.
6527 */
6528 public static final class CarExtender implements Extender {
6529 private static final String TAG = "CarExtender";
6530
6531 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6532 private static final String EXTRA_LARGE_ICON = "large_icon";
6533 private static final String EXTRA_CONVERSATION = "car_conversation";
6534 private static final String EXTRA_COLOR = "app_color";
6535
6536 private Bitmap mLargeIcon;
6537 private UnreadConversation mUnreadConversation;
6538 private int mColor = Notification.COLOR_DEFAULT;
6539
6540 /**
6541 * Create a {@link CarExtender} with default options.
6542 */
6543 public CarExtender() {
6544 }
6545
6546 /**
6547 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6548 *
6549 * @param notif The notification from which to copy options.
6550 */
6551 public CarExtender(Notification notif) {
6552 Bundle carBundle = notif.extras == null ?
6553 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6554 if (carBundle != null) {
6555 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6556 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6557
6558 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6559 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6560 }
6561 }
6562
6563 /**
6564 * Apply car extensions to a notification that is being built. This is typically called by
6565 * the {@link Notification.Builder#extend(Notification.Extender)}
6566 * method of {@link Notification.Builder}.
6567 */
6568 @Override
6569 public Notification.Builder extend(Notification.Builder builder) {
6570 Bundle carExtensions = new Bundle();
6571
6572 if (mLargeIcon != null) {
6573 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6574 }
6575 if (mColor != Notification.COLOR_DEFAULT) {
6576 carExtensions.putInt(EXTRA_COLOR, mColor);
6577 }
6578
6579 if (mUnreadConversation != null) {
6580 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6581 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6582 }
6583
6584 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6585 return builder;
6586 }
6587
6588 /**
6589 * Sets the accent color to use when Android Auto presents the notification.
6590 *
6591 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6592 * to accent the displayed notification. However, not all colors are acceptable in an
6593 * automotive setting. This method can be used to override the color provided in the
6594 * notification in such a situation.
6595 */
Tor Norbye80756e32015-03-02 09:39:27 -08006596 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006597 mColor = color;
6598 return this;
6599 }
6600
6601 /**
6602 * Gets the accent color.
6603 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006604 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006605 */
Tor Norbye80756e32015-03-02 09:39:27 -08006606 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006607 public int getColor() {
6608 return mColor;
6609 }
6610
6611 /**
6612 * Sets the large icon of the car notification.
6613 *
6614 * If no large icon is set in the extender, Android Auto will display the icon
6615 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6616 *
6617 * @param largeIcon The large icon to use in the car notification.
6618 * @return This object for method chaining.
6619 */
6620 public CarExtender setLargeIcon(Bitmap largeIcon) {
6621 mLargeIcon = largeIcon;
6622 return this;
6623 }
6624
6625 /**
6626 * Gets the large icon used in this car notification, or null if no icon has been set.
6627 *
6628 * @return The large icon for the car notification.
6629 * @see CarExtender#setLargeIcon
6630 */
6631 public Bitmap getLargeIcon() {
6632 return mLargeIcon;
6633 }
6634
6635 /**
6636 * Sets the unread conversation in a message notification.
6637 *
6638 * @param unreadConversation The unread part of the conversation this notification conveys.
6639 * @return This object for method chaining.
6640 */
6641 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6642 mUnreadConversation = unreadConversation;
6643 return this;
6644 }
6645
6646 /**
6647 * Returns the unread conversation conveyed by this notification.
6648 * @see #setUnreadConversation(UnreadConversation)
6649 */
6650 public UnreadConversation getUnreadConversation() {
6651 return mUnreadConversation;
6652 }
6653
6654 /**
6655 * A class which holds the unread messages from a conversation.
6656 */
6657 public static class UnreadConversation {
6658 private static final String KEY_AUTHOR = "author";
6659 private static final String KEY_TEXT = "text";
6660 private static final String KEY_MESSAGES = "messages";
6661 private static final String KEY_REMOTE_INPUT = "remote_input";
6662 private static final String KEY_ON_REPLY = "on_reply";
6663 private static final String KEY_ON_READ = "on_read";
6664 private static final String KEY_PARTICIPANTS = "participants";
6665 private static final String KEY_TIMESTAMP = "timestamp";
6666
6667 private final String[] mMessages;
6668 private final RemoteInput mRemoteInput;
6669 private final PendingIntent mReplyPendingIntent;
6670 private final PendingIntent mReadPendingIntent;
6671 private final String[] mParticipants;
6672 private final long mLatestTimestamp;
6673
6674 UnreadConversation(String[] messages, RemoteInput remoteInput,
6675 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6676 String[] participants, long latestTimestamp) {
6677 mMessages = messages;
6678 mRemoteInput = remoteInput;
6679 mReadPendingIntent = readPendingIntent;
6680 mReplyPendingIntent = replyPendingIntent;
6681 mParticipants = participants;
6682 mLatestTimestamp = latestTimestamp;
6683 }
6684
6685 /**
6686 * Gets the list of messages conveyed by this notification.
6687 */
6688 public String[] getMessages() {
6689 return mMessages;
6690 }
6691
6692 /**
6693 * Gets the remote input that will be used to convey the response to a message list, or
6694 * null if no such remote input exists.
6695 */
6696 public RemoteInput getRemoteInput() {
6697 return mRemoteInput;
6698 }
6699
6700 /**
6701 * Gets the pending intent that will be triggered when the user replies to this
6702 * notification.
6703 */
6704 public PendingIntent getReplyPendingIntent() {
6705 return mReplyPendingIntent;
6706 }
6707
6708 /**
6709 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6710 * in this object's message list.
6711 */
6712 public PendingIntent getReadPendingIntent() {
6713 return mReadPendingIntent;
6714 }
6715
6716 /**
6717 * Gets the participants in the conversation.
6718 */
6719 public String[] getParticipants() {
6720 return mParticipants;
6721 }
6722
6723 /**
6724 * Gets the firs participant in the conversation.
6725 */
6726 public String getParticipant() {
6727 return mParticipants.length > 0 ? mParticipants[0] : null;
6728 }
6729
6730 /**
6731 * Gets the timestamp of the conversation.
6732 */
6733 public long getLatestTimestamp() {
6734 return mLatestTimestamp;
6735 }
6736
6737 Bundle getBundleForUnreadConversation() {
6738 Bundle b = new Bundle();
6739 String author = null;
6740 if (mParticipants != null && mParticipants.length > 1) {
6741 author = mParticipants[0];
6742 }
6743 Parcelable[] messages = new Parcelable[mMessages.length];
6744 for (int i = 0; i < messages.length; i++) {
6745 Bundle m = new Bundle();
6746 m.putString(KEY_TEXT, mMessages[i]);
6747 m.putString(KEY_AUTHOR, author);
6748 messages[i] = m;
6749 }
6750 b.putParcelableArray(KEY_MESSAGES, messages);
6751 if (mRemoteInput != null) {
6752 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6753 }
6754 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6755 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6756 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6757 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6758 return b;
6759 }
6760
6761 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6762 if (b == null) {
6763 return null;
6764 }
6765 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6766 String[] messages = null;
6767 if (parcelableMessages != null) {
6768 String[] tmp = new String[parcelableMessages.length];
6769 boolean success = true;
6770 for (int i = 0; i < tmp.length; i++) {
6771 if (!(parcelableMessages[i] instanceof Bundle)) {
6772 success = false;
6773 break;
6774 }
6775 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6776 if (tmp[i] == null) {
6777 success = false;
6778 break;
6779 }
6780 }
6781 if (success) {
6782 messages = tmp;
6783 } else {
6784 return null;
6785 }
6786 }
6787
6788 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6789 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6790
6791 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6792
6793 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6794 if (participants == null || participants.length != 1) {
6795 return null;
6796 }
6797
6798 return new UnreadConversation(messages,
6799 remoteInput,
6800 onReply,
6801 onRead,
6802 participants, b.getLong(KEY_TIMESTAMP));
6803 }
6804 };
6805
6806 /**
6807 * Builder class for {@link CarExtender.UnreadConversation} objects.
6808 */
6809 public static class Builder {
6810 private final List<String> mMessages = new ArrayList<String>();
6811 private final String mParticipant;
6812 private RemoteInput mRemoteInput;
6813 private PendingIntent mReadPendingIntent;
6814 private PendingIntent mReplyPendingIntent;
6815 private long mLatestTimestamp;
6816
6817 /**
6818 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6819 *
6820 * @param name The name of the other participant in the conversation.
6821 */
6822 public Builder(String name) {
6823 mParticipant = name;
6824 }
6825
6826 /**
6827 * Appends a new unread message to the list of messages for this conversation.
6828 *
6829 * The messages should be added from oldest to newest.
6830 *
6831 * @param message The text of the new unread message.
6832 * @return This object for method chaining.
6833 */
6834 public Builder addMessage(String message) {
6835 mMessages.add(message);
6836 return this;
6837 }
6838
6839 /**
6840 * Sets the pending intent and remote input which will convey the reply to this
6841 * notification.
6842 *
6843 * @param pendingIntent The pending intent which will be triggered on a reply.
6844 * @param remoteInput The remote input parcelable which will carry the reply.
6845 * @return This object for method chaining.
6846 *
6847 * @see CarExtender.UnreadConversation#getRemoteInput
6848 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6849 */
6850 public Builder setReplyAction(
6851 PendingIntent pendingIntent, RemoteInput remoteInput) {
6852 mRemoteInput = remoteInput;
6853 mReplyPendingIntent = pendingIntent;
6854
6855 return this;
6856 }
6857
6858 /**
6859 * Sets the pending intent that will be sent once the messages in this notification
6860 * are read.
6861 *
6862 * @param pendingIntent The pending intent to use.
6863 * @return This object for method chaining.
6864 */
6865 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6866 mReadPendingIntent = pendingIntent;
6867 return this;
6868 }
6869
6870 /**
6871 * Sets the timestamp of the most recent message in an unread conversation.
6872 *
6873 * If a messaging notification has been posted by your application and has not
6874 * yet been cancelled, posting a later notification with the same id and tag
6875 * but without a newer timestamp may result in Android Auto not displaying a
6876 * heads up notification for the later notification.
6877 *
6878 * @param timestamp The timestamp of the most recent message in the conversation.
6879 * @return This object for method chaining.
6880 */
6881 public Builder setLatestTimestamp(long timestamp) {
6882 mLatestTimestamp = timestamp;
6883 return this;
6884 }
6885
6886 /**
6887 * Builds a new unread conversation object.
6888 *
6889 * @return The new unread conversation object.
6890 */
6891 public UnreadConversation build() {
6892 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6893 String[] participants = { mParticipant };
6894 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6895 mReadPendingIntent, participants, mLatestTimestamp);
6896 }
6897 }
6898 }
6899
6900 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006901 * Get an array of Notification objects from a parcelable array bundle field.
6902 * Update the bundle to have a typed array so fetches in the future don't need
6903 * to do an array copy.
6904 */
6905 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6906 Parcelable[] array = bundle.getParcelableArray(key);
6907 if (array instanceof Notification[] || array == null) {
6908 return (Notification[]) array;
6909 }
6910 Notification[] typedArray = Arrays.copyOf(array, array.length,
6911 Notification[].class);
6912 bundle.putParcelableArray(key, typedArray);
6913 return typedArray;
6914 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006915
6916 private static class BuilderRemoteViews extends RemoteViews {
6917 public BuilderRemoteViews(Parcel parcel) {
6918 super(parcel);
6919 }
6920
Kenny Guy77320062014-08-27 21:37:15 +01006921 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6922 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006923 }
6924
6925 @Override
6926 public BuilderRemoteViews clone() {
6927 Parcel p = Parcel.obtain();
6928 writeToParcel(p, 0);
6929 p.setDataPosition(0);
6930 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6931 p.recycle();
6932 return brv;
6933 }
6934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935}