blob: c4a937822cd91d7fcd6852c58e5b37341350a674 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040024import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010027import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040028import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020029import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020030import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040031import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070033import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010034import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010035import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040036import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040037import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070038import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070039import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040041import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020042import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Parcel;
45import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040046import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070047import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070048import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080049import android.text.SpannableStringBuilder;
50import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080052import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070053import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080054import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070055import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080056import android.text.style.RelativeSizeSpan;
57import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070058import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040059import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050060import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070061import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080062import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080063import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070064import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070065import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.widget.RemoteViews;
67
Griff Hazen959591e2014-05-15 22:26:18 -070068import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070069import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070070import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070071
Tor Norbyed9273d62013-05-30 15:59:53 -070072import java.lang.annotation.Retention;
73import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020074import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050075import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070076import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070077import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070078import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050079import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081/**
82 * A class that represents how a persistent notification is to be presented to
83 * the user using the {@link android.app.NotificationManager}.
84 *
Joe Onoratocb109a02011-01-18 17:57:41 -080085 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
86 * easier to construct Notifications.</p>
87 *
Joe Fernandez558459f2011-10-13 16:47:36 -070088 * <div class="special reference">
89 * <h3>Developer Guides</h3>
90 * <p>For a guide to creating notifications, read the
91 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
92 * developer guide.</p>
93 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 */
95public class Notification implements Parcelable
96{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040097 private static final String TAG = "Notification";
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400100 * An activity that provides a user interface for adjusting notification preferences for its
101 * containing application. Optional but recommended for apps that post
102 * {@link android.app.Notification Notifications}.
103 */
104 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
105 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
106 = "android.intent.category.NOTIFICATION_PREFERENCES";
107
108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * Use all default values (where applicable).
110 */
111 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 /**
114 * Use the default notification sound. This will ignore any given
115 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500116 *
Chris Wren47c20a12014-06-18 17:27:29 -0400117 * <p>
118 * A notification that is noisy is more likely to be presented as a heads-up notification.
119 * </p>
120 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500122 */
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final int DEFAULT_SOUND = 1;
125
126 /**
127 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700129 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130 *
Chris Wren47c20a12014-06-18 17:27:29 -0400131 * <p>
132 * A notification that vibrates is more likely to be presented as a heads-up notification.
133 * </p>
134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500136 */
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
141 * Use the default notification lights. This will ignore the
142 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
143 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146 */
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200151 * Maximum length of CharSequences accepted by Builder and friends.
152 *
153 * <p>
154 * Avoids spamming the system with overly large strings such as full e-mails.
155 */
156 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
157
158 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800159 * Maximum entries of reply text that are accepted by Builder and friends.
160 */
161 private static final int MAX_REPLY_HISTORY = 5;
162
163 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800165 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * Default value: {@link System#currentTimeMillis() Now}.
167 *
168 * Choose a timestamp that will be most relevant to the user. For most finite events, this
169 * corresponds to the time the event happened (or will happen, in the case of events that have
170 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800171 * timestamped according to when the activity began.
172 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500173 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800174 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 * <ul>
176 * <li>Notification of a new chat message should be stamped when the message was received.</li>
177 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
178 * <li>Notification of a completed file download should be stamped when the download finished.</li>
179 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
180 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
181 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800182 * </ul>
183 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700184 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
185 * anymore by default and must be opted into by using
186 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 */
188 public long when;
189
190 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700191 * The creation time of the notification
192 */
193 private long creationTime;
194
195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400197 *
198 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
Dan Sandler86647982015-05-13 23:41:13 -0400200 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700201 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 public int icon;
203
204 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800205 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
206 * leave it at its default value of 0.
207 *
208 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700209 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800210 */
211 public int iconLevel;
212
213 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500214 * The number of events that this notification represents. For example, in a new mail
215 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500217 * The system may or may not use this field to modify the appearance of the notification. For
218 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
219 * superimposed over the icon in the status bar. Starting with
220 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
221 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700224 *
225 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
227 public int number;
228
229 /**
230 * The intent to execute when the expanded status entry is clicked. If
231 * this is an activity, it must include the
232 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800233 * that you take care of task management as described in the
234 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800235 * Stack</a> document. In particular, make sure to read the notification section
236 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
237 * Notifications</a> for the correct ways to launch an application from a
238 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
240 public PendingIntent contentIntent;
241
242 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500243 * The intent to execute when the notification is explicitly dismissed by the user, either with
244 * the "Clear All" button or by swiping it away individually.
245 *
246 * This probably shouldn't be launching an activity since several of those will be sent
247 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
249 public PendingIntent deleteIntent;
250
251 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700252 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800253 *
Chris Wren47c20a12014-06-18 17:27:29 -0400254 * <p>
255 * The system UI may choose to display a heads-up notification, instead of
256 * launching this intent, while the user is using the device.
257 * </p>
258 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800259 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400260 */
261 public PendingIntent fullScreenIntent;
262
263 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400264 * Text that summarizes this notification for accessibility services.
265 *
266 * As of the L release, this text is no longer shown on screen, but it is still useful to
267 * accessibility services (where it serves as an audible announcement of the notification's
268 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400269 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800270 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 */
272 public CharSequence tickerText;
273
274 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400275 * Formerly, a view showing the {@link #tickerText}.
276 *
277 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400278 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400279 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800280 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400281
282 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400283 * The view that will represent this notification in the notification list (which is pulled
284 * down from the status bar).
285 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500286 * As of N, this field may be null. The notification view is determined by the inputs
287 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400288 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400290 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 public RemoteViews contentView;
292
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400293 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400294 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400295 * opportunity to show more detail. The system UI may choose to show this
296 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400297 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500298 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
300 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400301 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400302 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400303 public RemoteViews bigContentView;
304
Chris Wren8fd39ec2014-02-27 17:43:26 -0500305
306 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400307 * A medium-format version of {@link #contentView}, providing the Notification an
308 * opportunity to add action buttons to contentView. At its discretion, the system UI may
309 * choose to show this as a heads-up notification, which will pop up so the user can see
310 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400311 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500312 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400313 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
314 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500315 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400316 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500317 public RemoteViews headsUpContentView;
318
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400319 /**
Dan Sandler86647982015-05-13 23:41:13 -0400320 * A large bitmap to be shown in the notification content area.
321 *
322 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
Dan Sandler86647982015-05-13 23:41:13 -0400324 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800325 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
327 /**
328 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500329 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400331 * A notification that is noisy is more likely to be presented as a heads-up notification.
332 * </p>
333 *
334 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500335 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 * </p>
337 */
338 public Uri sound;
339
340 /**
341 * Use this constant as the value for audioStreamType to request that
342 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700343 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400344 *
345 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700347 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 public static final int STREAM_DEFAULT = -1;
349
350 /**
351 * The audio stream type to use when playing the sound.
352 * Should be one of the STREAM_ constants from
353 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400354 *
355 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700357 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 public int audioStreamType = STREAM_DEFAULT;
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400361 * The default value of {@link #audioAttributes}.
362 */
363 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
364 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
365 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
366 .build();
367
368 /**
369 * The {@link AudioAttributes audio attributes} to use when playing the sound.
370 */
371 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
372
373 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500374 * The pattern with which to vibrate.
375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * <p>
377 * To vibrate the default pattern, see {@link #defaults}.
378 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500379 *
Chris Wren47c20a12014-06-18 17:27:29 -0400380 * <p>
381 * A notification that vibrates is more likely to be presented as a heads-up notification.
382 * </p>
383 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 * @see android.os.Vibrator#vibrate(long[],int)
385 */
386 public long[] vibrate;
387
388 /**
389 * The color of the led. The hardware will do its best approximation.
390 *
391 * @see #FLAG_SHOW_LIGHTS
392 * @see #flags
393 */
Tor Norbye80756e32015-03-02 09:39:27 -0800394 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 public int ledARGB;
396
397 /**
398 * The number of milliseconds for the LED to be on while it's flashing.
399 * The hardware will do its best approximation.
400 *
401 * @see #FLAG_SHOW_LIGHTS
402 * @see #flags
403 */
404 public int ledOnMS;
405
406 /**
407 * The number of milliseconds for the LED to be off while it's flashing.
408 * The hardware will do its best approximation.
409 *
410 * @see #FLAG_SHOW_LIGHTS
411 * @see #flags
412 */
413 public int ledOffMS;
414
415 /**
416 * Specifies which values should be taken from the defaults.
417 * <p>
418 * To set, OR the desired from {@link #DEFAULT_SOUND},
419 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
420 * values, use {@link #DEFAULT_ALL}.
421 * </p>
422 */
423 public int defaults;
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * Bit to be bitwise-ored into the {@link #flags} field that should be
427 * set if you want the LED on for this notification.
428 * <ul>
429 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
430 * or 0 for both ledOnMS and ledOffMS.</li>
431 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
432 * <li>To flash the LED, pass the number of milliseconds that it should
433 * be on and off to ledOnMS and ledOffMS.</li>
434 * </ul>
435 * <p>
436 * Since hardware varies, you are not guaranteed that any of the values
437 * you pass are honored exactly. Use the system defaults (TODO) if possible
438 * because they will be set to values that work on any given hardware.
439 * <p>
440 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 */
443 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
444
445 /**
446 * Bit to be bitwise-ored into the {@link #flags} field that should be
447 * set if this notification is in reference to something that is ongoing,
448 * like a phone call. It should not be set if this notification is in
449 * reference to something that happened at a particular point in time,
450 * like a missed phone call.
451 */
452 public static final int FLAG_ONGOING_EVENT = 0x00000002;
453
454 /**
455 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700456 * the audio will be repeated until the notification is
457 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 */
459 public static final int FLAG_INSISTENT = 0x00000004;
460
461 /**
462 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700463 * set if you would only like the sound, vibrate and ticker to be played
464 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 */
466 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
467
468 /**
469 * Bit to be bitwise-ored into the {@link #flags} field that should be
470 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500471 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
473 public static final int FLAG_AUTO_CANCEL = 0x00000010;
474
475 /**
476 * Bit to be bitwise-ored into the {@link #flags} field that should be
477 * set if the notification should not be canceled when the user clicks
478 * the Clear all button.
479 */
480 public static final int FLAG_NO_CLEAR = 0x00000020;
481
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700482 /**
483 * Bit to be bitwise-ored into the {@link #flags} field that should be
484 * set if this notification represents a currently running service. This
485 * will normally be set for you by {@link Service#startForeground}.
486 */
487 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
488
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400489 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800491 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500492 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400493 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500494 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400495
Griff Hazendfcb0802014-02-11 12:00:00 -0800496 /**
497 * Bit to be bitswise-ored into the {@link #flags} field that should be
498 * set if this notification is relevant to the current device only
499 * and it is not recommended that it bridge to other devices.
500 */
501 public static final int FLAG_LOCAL_ONLY = 0x00000100;
502
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700503 /**
504 * Bit to be bitswise-ored into the {@link #flags} field that should be
505 * set if this notification is the group summary for a group of notifications.
506 * Grouped notifications may display in a cluster or stack on devices which
507 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
508 */
509 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
510
Julia Reynoldse46bb372016-03-17 11:05:58 -0400511 /**
512 * Bit to be bitswise-ored into the {@link #flags} field that should be
513 * set if this notification is the group summary for an auto-group of notifications.
514 *
515 * @hide
516 */
517 @SystemApi
518 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public int flags;
521
Tor Norbyed9273d62013-05-30 15:59:53 -0700522 /** @hide */
523 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
524 @Retention(RetentionPolicy.SOURCE)
525 public @interface Priority {}
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500528 * Default notification {@link #priority}. If your application does not prioritize its own
529 * notifications, use this value for all notifications.
530 */
531 public static final int PRIORITY_DEFAULT = 0;
532
533 /**
534 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
535 * items smaller, or at a different position in the list, compared with your app's
536 * {@link #PRIORITY_DEFAULT} items.
537 */
538 public static final int PRIORITY_LOW = -1;
539
540 /**
541 * Lowest {@link #priority}; these items might not be shown to the user except under special
542 * circumstances, such as detailed notification logs.
543 */
544 public static final int PRIORITY_MIN = -2;
545
546 /**
547 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
548 * show these items larger, or at a different position in notification lists, compared with
549 * your app's {@link #PRIORITY_DEFAULT} items.
550 */
551 public static final int PRIORITY_HIGH = 1;
552
553 /**
554 * Highest {@link #priority}, for your application's most important items that require the
555 * user's prompt attention or input.
556 */
557 public static final int PRIORITY_MAX = 2;
558
559 /**
560 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800561 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500562 * Priority is an indication of how much of the user's valuable attention should be consumed by
563 * this notification. Low-priority notifications may be hidden from the user in certain
564 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500565 * system will make a determination about how to interpret this priority when presenting
566 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400567 *
568 * <p>
569 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
570 * as a heads-up notification.
571 * </p>
572 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500573 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700574 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800576
Dan Sandler26e81cf2014-05-06 10:01:27 -0400577 /**
578 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
579 * to be applied by the standard Style templates when presenting this notification.
580 *
581 * The current template design constructs a colorful header image by overlaying the
582 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
583 * ignored.
584 */
Tor Norbye80756e32015-03-02 09:39:27 -0800585 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400586 public int color = COLOR_DEFAULT;
587
588 /**
589 * Special value of {@link #color} telling the system not to decorate this notification with
590 * any special color but instead use default colors when presenting this notification.
591 */
Tor Norbye80756e32015-03-02 09:39:27 -0800592 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400593 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600594
595 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800596 * Special value of {@link #color} used as a place holder for an invalid color.
597 */
598 @ColorInt
599 private static final int COLOR_INVALID = 1;
600
601 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700602 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
603 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600604 * lockscreen).
605 *
606 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
607 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
608 * shown in all situations, but the contents are only available if the device is unlocked for
609 * the appropriate user.
610 *
611 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
612 * can be read even in an "insecure" context (that is, above a secure lockscreen).
613 * To modify the public version of this notification—for example, to redact some portions—see
614 * {@link Builder#setPublicVersion(Notification)}.
615 *
616 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
617 * and ticker until the user has bypassed the lockscreen.
618 */
619 public int visibility;
620
Griff Hazenfc3922d2014-08-20 11:56:44 -0700621 /**
622 * Notification visibility: Show this notification in its entirety on all lockscreens.
623 *
624 * {@see #visibility}
625 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600626 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700627
628 /**
629 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
630 * private information on secure lockscreens.
631 *
632 * {@see #visibility}
633 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600634 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700635
636 /**
637 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
638 *
639 * {@see #visibility}
640 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600641 public static final int VISIBILITY_SECRET = -1;
642
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400644 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400646 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647
648 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400649 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400651 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652
653 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400654 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400656 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657
658 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400659 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400661 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662
663 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400664 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400666 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667
668 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400669 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500670 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400671 public static final String CATEGORY_ALARM = "alarm";
672
673 /**
674 * Notification category: progress of a long-running background operation.
675 */
676 public static final String CATEGORY_PROGRESS = "progress";
677
678 /**
679 * Notification category: social network or sharing update.
680 */
681 public static final String CATEGORY_SOCIAL = "social";
682
683 /**
684 * Notification category: error in background operation or authentication status.
685 */
686 public static final String CATEGORY_ERROR = "err";
687
688 /**
689 * Notification category: media transport control for playback.
690 */
691 public static final String CATEGORY_TRANSPORT = "transport";
692
693 /**
694 * Notification category: system or device status update. Reserved for system use.
695 */
696 public static final String CATEGORY_SYSTEM = "sys";
697
698 /**
699 * Notification category: indication of running background service.
700 */
701 public static final String CATEGORY_SERVICE = "service";
702
703 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400704 * Notification category: a specific, timely recommendation for a single thing.
705 * For example, a news app might want to recommend a news story it believes the user will
706 * want to read next.
707 */
708 public static final String CATEGORY_RECOMMENDATION = "recommendation";
709
710 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400711 * Notification category: ongoing information about device or contextual status.
712 */
713 public static final String CATEGORY_STATUS = "status";
714
715 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400716 * Notification category: user-scheduled reminder.
717 */
718 public static final String CATEGORY_REMINDER = "reminder";
719
720 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400721 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
722 * that best describes this Notification. May be used by the system for ranking and filtering.
723 */
724 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500725
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700726 private String mGroupKey;
727
728 /**
729 * Get the key used to group this notification into a cluster or stack
730 * with other notifications on devices which support such rendering.
731 */
732 public String getGroup() {
733 return mGroupKey;
734 }
735
736 private String mSortKey;
737
738 /**
739 * Get a sort key that orders this notification among other notifications from the
740 * same package. This can be useful if an external sort was already applied and an app
741 * would like to preserve this. Notifications will be sorted lexicographically using this
742 * value, although providing different priorities in addition to providing sort key may
743 * cause this value to be ignored.
744 *
745 * <p>This sort key can also be used to order members of a notification group. See
746 * {@link Builder#setGroup}.
747 *
748 * @see String#compareTo(String)
749 */
750 public String getSortKey() {
751 return mSortKey;
752 }
753
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500754 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400755 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400756 * <p>
757 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
758 * APIs, and are intended to be used by
759 * {@link android.service.notification.NotificationListenerService} implementations to extract
760 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500761 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400762 public Bundle extras = new Bundle();
763
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400764 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700765 * All pending intents in the notification as the system needs to be able to access them but
766 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700767 * custom parcelable objects.
768 *
769 * @hide
770 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700771 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700772
773 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774 * {@link #extras} key: this is the title of the notification,
775 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
776 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500777 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400778
779 /**
780 * {@link #extras} key: this is the title of the notification when shown in expanded form,
781 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
782 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400783 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784
785 /**
786 * {@link #extras} key: this is the main text payload, as supplied to
787 * {@link Builder#setContentText(CharSequence)}.
788 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500789 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400790
791 /**
792 * {@link #extras} key: this is a third line of text, as supplied to
793 * {@link Builder#setSubText(CharSequence)}.
794 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400795 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400796
797 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800798 * {@link #extras} key: this is the remote input history, as supplied to
799 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700800 *
801 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
802 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
803 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
804 * notifications once the other party has responded).
805 *
806 * The extra with this key is of type CharSequence[] and contains the most recent entry at
807 * the 0 index, the second most recent at the 1 index, etc.
808 *
809 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800810 */
811 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
812
813 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814 * {@link #extras} key: this is a small piece of additional text as supplied to
815 * {@link Builder#setContentInfo(CharSequence)}.
816 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400817 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400818
819 /**
820 * {@link #extras} key: this is a line of summary information intended to be shown
821 * alongside expanded notifications, as supplied to (e.g.)
822 * {@link BigTextStyle#setSummaryText(CharSequence)}.
823 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400824 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825
826 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200827 * {@link #extras} key: this is the longer text shown in the big form of a
828 * {@link BigTextStyle} notification, as supplied to
829 * {@link BigTextStyle#bigText(CharSequence)}.
830 */
831 public static final String EXTRA_BIG_TEXT = "android.bigText";
832
833 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400834 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
835 * supplied to {@link Builder#setSmallIcon(int)}.
836 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500837 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838
839 /**
840 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
841 * notification payload, as
842 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: this is a bitmap to be used instead of the one from
848 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
849 * shown in its expanded form, as supplied to
850 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400853
854 /**
855 * {@link #extras} key: this is the progress value supplied to
856 * {@link Builder#setProgress(int, int, boolean)}.
857 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400858 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400859
860 /**
861 * {@link #extras} key: this is the maximum value supplied to
862 * {@link Builder#setProgress(int, int, boolean)}.
863 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400864 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400865
866 /**
867 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
868 * {@link Builder#setProgress(int, int, boolean)}.
869 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400870 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400871
872 /**
873 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
874 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
875 * {@link Builder#setUsesChronometer(boolean)}.
876 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400877 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400878
879 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800880 * {@link #extras} key: whether the chronometer set on the notification should count down
881 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700882 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800883 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700884 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800885
886 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400887 * {@link #extras} key: whether {@link #when} should be shown,
888 * as supplied to {@link Builder#setShowWhen(boolean)}.
889 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400890 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400891
892 /**
893 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
894 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
895 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400896 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400897
898 /**
899 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
900 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
901 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400902 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400903
904 /**
905 * {@link #extras} key: A string representing the name of the specific
906 * {@link android.app.Notification.Style} used to create this notification.
907 */
Chris Wren91ad5632013-06-05 15:05:57 -0400908 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400909
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400910 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400911 * {@link #extras} key: A String array containing the people that this notification relates to,
912 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400913 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400914 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500915
916 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400917 * Allow certain system-generated notifications to appear before the device is provisioned.
918 * Only available to notifications coming from the android package.
919 * @hide
920 */
921 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
922
923 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700924 * {@link #extras} key: A
925 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
926 * in the background when the notification is selected. The URI must point to an image stream
927 * suitable for passing into
928 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
929 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
930 * URI used for this purpose must require no permissions to read the image data.
931 */
932 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
933
934 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400935 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700936 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400937 * {@link android.app.Notification.MediaStyle} notification.
938 */
939 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
940
941 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100942 * {@link #extras} key: the indices of actions to be shown in the compact view,
943 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
944 */
945 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
946
Christoph Studer943aa672014-08-03 20:31:16 +0200947 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400948 * {@link #extras} key: the username to be displayed for all messages sent by the user including
949 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700950 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
951 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400952 */
953 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
954
955 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700956 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400957 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400958 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400959 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400960
961 /**
962 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
963 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700964 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
965 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400966 */
967 public static final String EXTRA_MESSAGES = "android.messages";
968
969 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100970 * {@link #extras} key: the user that built the notification.
971 *
972 * @hide
973 */
974 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
975
976 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400977 * @hide
978 */
979 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
980
Selim Cinek247fa012016-02-18 09:50:48 -0800981 /**
982 * @hide
983 */
984 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
985
Dan Sandler80eaa592016-04-14 23:34:54 -0400986 /** @hide */
987 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400988 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
989
Dan Sandlerd63f9322015-05-06 15:18:49 -0400990 private Icon mSmallIcon;
991 private Icon mLargeIcon;
992
Chris Wren51c75102013-07-16 20:49:17 -0400993 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400994 * Structure to encapsulate a named action that can be shown as part of this notification.
995 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
996 * selected by the user.
997 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700998 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
999 * or {@link Notification.Builder#addAction(Notification.Action)}
1000 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001001 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001002 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -07001003 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001004 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001005 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001006 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -07001007
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001008 /**
1009 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001010 *
1011 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001012 */
Dan Sandler86647982015-05-13 23:41:13 -04001013 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001014 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001015
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001016 /**
1017 * Title of the action.
1018 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001019 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001020
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001021 /**
1022 * Intent to send when the user invokes this action. May be null, in which case the action
1023 * may be rendered in a disabled presentation by the system UI.
1024 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001025 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001026
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001027 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001028 if (in.readInt() != 0) {
1029 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001030 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1031 icon = mIcon.getResId();
1032 }
Dan Sandler86647982015-05-13 23:41:13 -04001033 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001034 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1035 if (in.readInt() == 1) {
1036 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1037 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001038 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001039 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001040 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001041 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001042
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 /**
Dan Sandler86647982015-05-13 23:41:13 -04001044 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001045 */
Dan Sandler86647982015-05-13 23:41:13 -04001046 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001047 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001048 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001049 }
1050
Dan Sandler86647982015-05-13 23:41:13 -04001051 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001052 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001053 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001054 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1055 this.icon = icon.getResId();
1056 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001057 this.title = title;
1058 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001059 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001060 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001061 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001062 }
1063
1064 /**
Dan Sandler86647982015-05-13 23:41:13 -04001065 * Return an icon representing the action.
1066 */
1067 public Icon getIcon() {
1068 if (mIcon == null && icon != 0) {
1069 // you snuck an icon in here without using the builder; let's try to keep it
1070 mIcon = Icon.createWithResource("", icon);
1071 }
1072 return mIcon;
1073 }
1074
1075 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001076 * Get additional metadata carried around with this Action.
1077 */
1078 public Bundle getExtras() {
1079 return mExtras;
1080 }
1081
1082 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001083 * Return whether the platform should automatically generate possible replies for this
1084 * {@link Action}
1085 */
1086 public boolean getAllowGeneratedReplies() {
1087 return mAllowGeneratedReplies;
1088 }
1089
1090 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001091 * Get the list of inputs to be collected from the user when this action is sent.
1092 * May return null if no remote inputs were added.
1093 */
1094 public RemoteInput[] getRemoteInputs() {
1095 return mRemoteInputs;
1096 }
1097
1098 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001099 * Builder class for {@link Action} objects.
1100 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001101 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001102 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001103 private final CharSequence mTitle;
1104 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001105 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001106 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001107 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001108
1109 /**
1110 * Construct a new builder for {@link Action} object.
1111 * @param icon icon to show for this action
1112 * @param title the title of the action
1113 * @param intent the {@link PendingIntent} to fire when users trigger this action
1114 */
Dan Sandler86647982015-05-13 23:41:13 -04001115 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001116 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001117 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1118 }
1119
1120 /**
1121 * Construct a new builder for {@link Action} object.
1122 * @param icon icon to show for this action
1123 * @param title the title of the action
1124 * @param intent the {@link PendingIntent} to fire when users trigger this action
1125 */
1126 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001127 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001128 }
1129
1130 /**
1131 * Construct a new builder for {@link Action} object using the fields from an
1132 * {@link Action}.
1133 * @param action the action to read fields from.
1134 */
1135 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001136 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001137 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001138 }
1139
Dan Sandler86647982015-05-13 23:41:13 -04001140 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001141 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001142 mIcon = icon;
1143 mTitle = title;
1144 mIntent = intent;
1145 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001146 if (remoteInputs != null) {
1147 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1148 Collections.addAll(mRemoteInputs, remoteInputs);
1149 }
Griff Hazen959591e2014-05-15 22:26:18 -07001150 }
1151
1152 /**
1153 * Merge additional metadata into this builder.
1154 *
1155 * <p>Values within the Bundle will replace existing extras values in this Builder.
1156 *
1157 * @see Notification.Action#extras
1158 */
1159 public Builder addExtras(Bundle extras) {
1160 if (extras != null) {
1161 mExtras.putAll(extras);
1162 }
1163 return this;
1164 }
1165
1166 /**
1167 * Get the metadata Bundle used by this Builder.
1168 *
1169 * <p>The returned Bundle is shared with this Builder.
1170 */
1171 public Bundle getExtras() {
1172 return mExtras;
1173 }
1174
1175 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001176 * Add an input to be collected from the user when this action is sent.
1177 * Response values can be retrieved from the fired intent by using the
1178 * {@link RemoteInput#getResultsFromIntent} function.
1179 * @param remoteInput a {@link RemoteInput} to add to the action
1180 * @return this object for method chaining
1181 */
1182 public Builder addRemoteInput(RemoteInput remoteInput) {
1183 if (mRemoteInputs == null) {
1184 mRemoteInputs = new ArrayList<RemoteInput>();
1185 }
1186 mRemoteInputs.add(remoteInput);
1187 return this;
1188 }
1189
1190 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001191 * Set whether the platform should automatically generate possible replies to add to
1192 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1193 * {@link RemoteInput}, this has no effect.
1194 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1195 * otherwise
1196 * @return this object for method chaining
1197 * The default value is {@code false}
1198 */
1199 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1200 mAllowGeneratedReplies = allowGeneratedReplies;
1201 return this;
1202 }
1203
1204 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001205 * Apply an extender to this action builder. Extenders may be used to add
1206 * metadata or change options on this builder.
1207 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001208 public Builder extend(Extender extender) {
1209 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001210 return this;
1211 }
1212
1213 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001214 * Combine all of the options that have been set and return a new {@link Action}
1215 * object.
1216 * @return the built action
1217 */
1218 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001219 RemoteInput[] remoteInputs = mRemoteInputs != null
1220 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001221 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1222 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001223 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001224 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001225
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001226 @Override
1227 public Action clone() {
1228 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001229 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001230 title,
1231 actionIntent, // safe to alias
1232 new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001233 getRemoteInputs(),
1234 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001235 }
1236 @Override
1237 public int describeContents() {
1238 return 0;
1239 }
1240 @Override
1241 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001242 final Icon ic = getIcon();
1243 if (ic != null) {
1244 out.writeInt(1);
1245 ic.writeToParcel(out, 0);
1246 } else {
1247 out.writeInt(0);
1248 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001249 TextUtils.writeToParcel(title, out, flags);
1250 if (actionIntent != null) {
1251 out.writeInt(1);
1252 actionIntent.writeToParcel(out, flags);
1253 } else {
1254 out.writeInt(0);
1255 }
Griff Hazen959591e2014-05-15 22:26:18 -07001256 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001257 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001258 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001259 }
Griff Hazen959591e2014-05-15 22:26:18 -07001260 public static final Parcelable.Creator<Action> CREATOR =
1261 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001262 public Action createFromParcel(Parcel in) {
1263 return new Action(in);
1264 }
1265 public Action[] newArray(int size) {
1266 return new Action[size];
1267 }
1268 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001269
1270 /**
1271 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1272 * metadata or change options on an action builder.
1273 */
1274 public interface Extender {
1275 /**
1276 * Apply this extender to a notification action builder.
1277 * @param builder the builder to be modified.
1278 * @return the build object for chaining.
1279 */
1280 public Builder extend(Builder builder);
1281 }
1282
1283 /**
1284 * Wearable extender for notification actions. To add extensions to an action,
1285 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1286 * the {@code WearableExtender()} constructor and apply it to a
1287 * {@link android.app.Notification.Action.Builder} using
1288 * {@link android.app.Notification.Action.Builder#extend}.
1289 *
1290 * <pre class="prettyprint">
1291 * Notification.Action action = new Notification.Action.Builder(
1292 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001293 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001294 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001295 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001296 */
1297 public static final class WearableExtender implements Extender {
1298 /** Notification action extra which contains wearable extensions */
1299 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1300
Pete Gastaf6781d2014-10-07 15:17:05 -04001301 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001302 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001303 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1304 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1305 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001306
1307 // Flags bitwise-ored to mFlags
1308 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001309 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001310 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001311
1312 // Default value for flags integer
1313 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1314
1315 private int mFlags = DEFAULT_FLAGS;
1316
Pete Gastaf6781d2014-10-07 15:17:05 -04001317 private CharSequence mInProgressLabel;
1318 private CharSequence mConfirmLabel;
1319 private CharSequence mCancelLabel;
1320
Griff Hazen61a9e862014-05-22 16:05:19 -07001321 /**
1322 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1323 * options.
1324 */
1325 public WearableExtender() {
1326 }
1327
1328 /**
1329 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1330 * wearable options present in an existing notification action.
1331 * @param action the notification action to inspect.
1332 */
1333 public WearableExtender(Action action) {
1334 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1335 if (wearableBundle != null) {
1336 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001337 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1338 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1339 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001340 }
1341 }
1342
1343 /**
1344 * Apply wearable extensions to a notification action that is being built. This is
1345 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1346 * method of {@link android.app.Notification.Action.Builder}.
1347 */
1348 @Override
1349 public Action.Builder extend(Action.Builder builder) {
1350 Bundle wearableBundle = new Bundle();
1351
1352 if (mFlags != DEFAULT_FLAGS) {
1353 wearableBundle.putInt(KEY_FLAGS, mFlags);
1354 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001355 if (mInProgressLabel != null) {
1356 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1357 }
1358 if (mConfirmLabel != null) {
1359 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1360 }
1361 if (mCancelLabel != null) {
1362 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1363 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001364
1365 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1366 return builder;
1367 }
1368
1369 @Override
1370 public WearableExtender clone() {
1371 WearableExtender that = new WearableExtender();
1372 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001373 that.mInProgressLabel = this.mInProgressLabel;
1374 that.mConfirmLabel = this.mConfirmLabel;
1375 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001376 return that;
1377 }
1378
1379 /**
1380 * Set whether this action is available when the wearable device is not connected to
1381 * a companion device. The user can still trigger this action when the wearable device is
1382 * offline, but a visual hint will indicate that the action may not be available.
1383 * Defaults to true.
1384 */
1385 public WearableExtender setAvailableOffline(boolean availableOffline) {
1386 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1387 return this;
1388 }
1389
1390 /**
1391 * Get whether this action is available when the wearable device is not connected to
1392 * a companion device. The user can still trigger this action when the wearable device is
1393 * offline, but a visual hint will indicate that the action may not be available.
1394 * Defaults to true.
1395 */
1396 public boolean isAvailableOffline() {
1397 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1398 }
1399
1400 private void setFlag(int mask, boolean value) {
1401 if (value) {
1402 mFlags |= mask;
1403 } else {
1404 mFlags &= ~mask;
1405 }
1406 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001407
1408 /**
1409 * Set a label to display while the wearable is preparing to automatically execute the
1410 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1411 *
1412 * @param label the label to display while the action is being prepared to execute
1413 * @return this object for method chaining
1414 */
1415 public WearableExtender setInProgressLabel(CharSequence label) {
1416 mInProgressLabel = label;
1417 return this;
1418 }
1419
1420 /**
1421 * Get the label to display while the wearable is preparing to automatically execute
1422 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1423 *
1424 * @return the label to display while the action is being prepared to execute
1425 */
1426 public CharSequence getInProgressLabel() {
1427 return mInProgressLabel;
1428 }
1429
1430 /**
1431 * Set a label to display to confirm that the action should be executed.
1432 * This is usually an imperative verb like "Send".
1433 *
1434 * @param label the label to confirm the action should be executed
1435 * @return this object for method chaining
1436 */
1437 public WearableExtender setConfirmLabel(CharSequence label) {
1438 mConfirmLabel = label;
1439 return this;
1440 }
1441
1442 /**
1443 * Get the label to display to confirm that the action should be executed.
1444 * This is usually an imperative verb like "Send".
1445 *
1446 * @return the label to confirm the action should be executed
1447 */
1448 public CharSequence getConfirmLabel() {
1449 return mConfirmLabel;
1450 }
1451
1452 /**
1453 * Set a label to display to cancel the action.
1454 * This is usually an imperative verb, like "Cancel".
1455 *
1456 * @param label the label to display to cancel the action
1457 * @return this object for method chaining
1458 */
1459 public WearableExtender setCancelLabel(CharSequence label) {
1460 mCancelLabel = label;
1461 return this;
1462 }
1463
1464 /**
1465 * Get the label to display to cancel the action.
1466 * This is usually an imperative verb like "Cancel".
1467 *
1468 * @return the label to display to cancel the action
1469 */
1470 public CharSequence getCancelLabel() {
1471 return mCancelLabel;
1472 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001473
1474 /**
1475 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1476 * platform that it can generate the appropriate transitions.
1477 * @param hintLaunchesActivity {@code true} if the content intent will launch
1478 * an activity and transitions should be generated, false otherwise.
1479 * @return this object for method chaining
1480 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001481 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001482 boolean hintLaunchesActivity) {
1483 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1484 return this;
1485 }
1486
1487 /**
1488 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1489 * platform that it can generate the appropriate transitions
1490 * @return {@code true} if the content intent will launch an activity and transitions
1491 * should be generated, false otherwise. The default value is {@code false} if this was
1492 * never set.
1493 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001494 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001495 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1496 }
Alex Hills9f087612016-06-07 09:08:59 -04001497
1498 /**
1499 * Set a hint that this Action should be displayed inline.
1500 *
1501 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1502 * otherwise
1503 * @return this object for method chaining
1504 */
1505 public WearableExtender setHintDisplayActionInline(
1506 boolean hintDisplayInline) {
1507 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1508 return this;
1509 }
1510
1511 /**
1512 * Get a hint that this Action should be displayed inline.
1513 *
1514 * @return {@code true} if the Action should be displayed inline, {@code false}
1515 * otherwise. The default value is {@code false} if this was never set.
1516 */
1517 public boolean getHintDisplayActionInline() {
1518 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1519 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001520 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001521 }
1522
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001523 /**
1524 * Array of all {@link Action} structures attached to this notification by
1525 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1526 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1527 * interface for invoking actions.
1528 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001529 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001530
1531 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001532 * Replacement version of this notification whose content will be shown
1533 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1534 * and {@link #VISIBILITY_PUBLIC}.
1535 */
1536 public Notification publicVersion;
1537
1538 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001539 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001540 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 */
1542 public Notification()
1543 {
1544 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001545 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001546 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548
1549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 * @hide
1551 */
1552 public Notification(Context context, int icon, CharSequence tickerText, long when,
1553 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1554 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001555 new Builder(context)
1556 .setWhen(when)
1557 .setSmallIcon(icon)
1558 .setTicker(tickerText)
1559 .setContentTitle(contentTitle)
1560 .setContentText(contentText)
1561 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1562 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 }
1564
1565 /**
1566 * Constructs a Notification object with the information needed to
1567 * have a status bar icon without the standard expanded view.
1568 *
1569 * @param icon The resource id of the icon to put in the status bar.
1570 * @param tickerText The text that flows by in the status bar when the notification first
1571 * activates.
1572 * @param when The time to show in the time field. In the System.currentTimeMillis
1573 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001574 *
1575 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001577 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 public Notification(int icon, CharSequence tickerText, long when)
1579 {
1580 this.icon = icon;
1581 this.tickerText = tickerText;
1582 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001583 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 }
1585
1586 /**
1587 * Unflatten the notification from a parcel.
1588 */
Svet Ganovddb94882016-06-23 19:55:24 -07001589 @SuppressWarnings("unchecked")
1590 public Notification(Parcel parcel) {
1591 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1592 // intents in extras are always written as the last entry.
1593 readFromParcelImpl(parcel);
1594 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001595 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001596 }
1597
1598 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 {
1600 int version = parcel.readInt();
1601
1602 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001603 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001604 if (parcel.readInt() != 0) {
1605 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001606 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1607 icon = mSmallIcon.getResId();
1608 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 number = parcel.readInt();
1611 if (parcel.readInt() != 0) {
1612 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1613 }
1614 if (parcel.readInt() != 0) {
1615 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1616 }
1617 if (parcel.readInt() != 0) {
1618 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1619 }
1620 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001621 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001622 }
1623 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1625 }
Joe Onorato561d3852010-11-20 18:09:34 -08001626 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001627 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 defaults = parcel.readInt();
1630 flags = parcel.readInt();
1631 if (parcel.readInt() != 0) {
1632 sound = Uri.CREATOR.createFromParcel(parcel);
1633 }
1634
1635 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001636 if (parcel.readInt() != 0) {
1637 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 vibrate = parcel.createLongArray();
1640 ledARGB = parcel.readInt();
1641 ledOnMS = parcel.readInt();
1642 ledOffMS = parcel.readInt();
1643 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001644
1645 if (parcel.readInt() != 0) {
1646 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1647 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001648
1649 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001650
John Spurlockfd7f1e02014-03-18 16:41:57 -04001651 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001652
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001653 mGroupKey = parcel.readString();
1654
1655 mSortKey = parcel.readString();
1656
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001657 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001658
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001659 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1660
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001661 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001662 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1663 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001664
Chris Wren8fd39ec2014-02-27 17:43:26 -05001665 if (parcel.readInt() != 0) {
1666 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1667 }
1668
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001669 visibility = parcel.readInt();
1670
1671 if (parcel.readInt() != 0) {
1672 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1673 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001674
1675 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
1677
Andy Stadler110988c2010-12-03 14:29:16 -08001678 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001679 public Notification clone() {
1680 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001681 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001682 return that;
1683 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001684
Daniel Sandler1a497d32013-04-18 14:52:45 -04001685 /**
1686 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1687 * of this into that.
1688 * @hide
1689 */
1690 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001691 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001692 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001693 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001694 that.number = this.number;
1695
1696 // PendingIntents are global, so there's no reason (or way) to clone them.
1697 that.contentIntent = this.contentIntent;
1698 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001699 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001700
1701 if (this.tickerText != null) {
1702 that.tickerText = this.tickerText.toString();
1703 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001704 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001705 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001706 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001707 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001708 that.contentView = this.contentView.clone();
1709 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001710 if (heavy && this.mLargeIcon != null) {
1711 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001712 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001713 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001714 that.sound = this.sound; // android.net.Uri is immutable
1715 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001716 if (this.audioAttributes != null) {
1717 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1718 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001719
1720 final long[] vibrate = this.vibrate;
1721 if (vibrate != null) {
1722 final int N = vibrate.length;
1723 final long[] vib = that.vibrate = new long[N];
1724 System.arraycopy(vibrate, 0, vib, 0, N);
1725 }
1726
1727 that.ledARGB = this.ledARGB;
1728 that.ledOnMS = this.ledOnMS;
1729 that.ledOffMS = this.ledOffMS;
1730 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001731
Joe Onorato18e69df2010-05-17 22:26:12 -07001732 that.flags = this.flags;
1733
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001734 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001735
John Spurlockfd7f1e02014-03-18 16:41:57 -04001736 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001737
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001738 that.mGroupKey = this.mGroupKey;
1739
1740 that.mSortKey = this.mSortKey;
1741
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001742 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001743 try {
1744 that.extras = new Bundle(this.extras);
1745 // will unparcel
1746 that.extras.size();
1747 } catch (BadParcelableException e) {
1748 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1749 that.extras = null;
1750 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001751 }
1752
Felipe Lemedd85da62016-06-28 11:29:54 -07001753 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1754 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001755 }
1756
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001757 if (this.actions != null) {
1758 that.actions = new Action[this.actions.length];
1759 for(int i=0; i<this.actions.length; i++) {
1760 that.actions[i] = this.actions[i].clone();
1761 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001762 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001763
Daniel Sandler1a497d32013-04-18 14:52:45 -04001764 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001765 that.bigContentView = this.bigContentView.clone();
1766 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001767
Chris Wren8fd39ec2014-02-27 17:43:26 -05001768 if (heavy && this.headsUpContentView != null) {
1769 that.headsUpContentView = this.headsUpContentView.clone();
1770 }
1771
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001772 that.visibility = this.visibility;
1773
1774 if (this.publicVersion != null) {
1775 that.publicVersion = new Notification();
1776 this.publicVersion.cloneInto(that.publicVersion, heavy);
1777 }
1778
Dan Sandler26e81cf2014-05-06 10:01:27 -04001779 that.color = this.color;
1780
Daniel Sandler1a497d32013-04-18 14:52:45 -04001781 if (!heavy) {
1782 that.lightenPayload(); // will clean out extras
1783 }
1784 }
1785
1786 /**
1787 * Removes heavyweight parts of the Notification object for archival or for sending to
1788 * listeners when the full contents are not necessary.
1789 * @hide
1790 */
1791 public final void lightenPayload() {
1792 tickerView = null;
1793 contentView = null;
1794 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001795 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001796 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001797 if (extras != null && !extras.isEmpty()) {
1798 final Set<String> keyset = extras.keySet();
1799 final int N = keyset.size();
1800 final String[] keys = keyset.toArray(new String[N]);
1801 for (int i=0; i<N; i++) {
1802 final String key = keys[i];
1803 final Object obj = extras.get(key);
1804 if (obj != null &&
1805 ( obj instanceof Parcelable
1806 || obj instanceof Parcelable[]
1807 || obj instanceof SparseArray
1808 || obj instanceof ArrayList)) {
1809 extras.remove(key);
1810 }
1811 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001812 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001813 }
1814
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001815 /**
1816 * Make sure this CharSequence is safe to put into a bundle, which basically
1817 * means it had better not be some custom Parcelable implementation.
1818 * @hide
1819 */
1820 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001821 if (cs == null) return cs;
1822 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1823 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1824 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001825 if (cs instanceof Parcelable) {
1826 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1827 + " instance is a custom Parcelable and not allowed in Notification");
1828 return cs.toString();
1829 }
Selim Cinek60a54252016-02-26 17:03:25 -08001830 return removeTextSizeSpans(cs);
1831 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001832
Selim Cinek60a54252016-02-26 17:03:25 -08001833 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1834 if (charSequence instanceof Spanned) {
1835 Spanned ss = (Spanned) charSequence;
1836 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1837 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1838 for (Object span : spans) {
1839 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001840 if (resultSpan instanceof CharacterStyle) {
1841 resultSpan = ((CharacterStyle) span).getUnderlying();
1842 }
1843 if (resultSpan instanceof TextAppearanceSpan) {
1844 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001845 resultSpan = new TextAppearanceSpan(
1846 originalSpan.getFamily(),
1847 originalSpan.getTextStyle(),
1848 -1,
1849 originalSpan.getTextColor(),
1850 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001851 } else if (resultSpan instanceof RelativeSizeSpan
1852 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001853 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001854 } else {
1855 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001856 }
1857 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1858 ss.getSpanFlags(span));
1859 }
1860 return builder;
1861 }
1862 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001863 }
1864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 public int describeContents() {
1866 return 0;
1867 }
1868
1869 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001870 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 */
Svet Ganovddb94882016-06-23 19:55:24 -07001872 public void writeToParcel(Parcel parcel, int flags) {
1873 // We need to mark all pending intents getting into the notification
1874 // system as being put there to later allow the notification ranker
1875 // to launch them and by doing so add the app to the battery saver white
1876 // list for a short period of time. The problem is that the system
1877 // cannot look into the extras as there may be parcelables there that
1878 // the platform does not know how to handle. To go around that we have
1879 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001880 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001881 if (collectPendingIntents) {
1882 PendingIntent.setOnMarshaledListener(
1883 (PendingIntent intent, Parcel out, int outFlags) -> {
1884 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001885 if (allPendingIntents == null) {
1886 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001887 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001888 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001889 }
1890 });
1891 }
1892 try {
1893 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1894 // want to intercept all pending events written to the pacel.
1895 writeToParcelImpl(parcel, flags);
1896 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001897 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001898 } finally {
1899 if (collectPendingIntents) {
1900 PendingIntent.setOnMarshaledListener(null);
1901 }
1902 }
1903 }
1904
1905 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 parcel.writeInt(1);
1907
1908 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001909 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001910 if (mSmallIcon == null && icon != 0) {
1911 // you snuck an icon in here without using the builder; let's try to keep it
1912 mSmallIcon = Icon.createWithResource("", icon);
1913 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001914 if (mSmallIcon != null) {
1915 parcel.writeInt(1);
1916 mSmallIcon.writeToParcel(parcel, 0);
1917 } else {
1918 parcel.writeInt(0);
1919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 parcel.writeInt(number);
1921 if (contentIntent != null) {
1922 parcel.writeInt(1);
1923 contentIntent.writeToParcel(parcel, 0);
1924 } else {
1925 parcel.writeInt(0);
1926 }
1927 if (deleteIntent != null) {
1928 parcel.writeInt(1);
1929 deleteIntent.writeToParcel(parcel, 0);
1930 } else {
1931 parcel.writeInt(0);
1932 }
1933 if (tickerText != null) {
1934 parcel.writeInt(1);
1935 TextUtils.writeToParcel(tickerText, parcel, flags);
1936 } else {
1937 parcel.writeInt(0);
1938 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001939 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001940 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001941 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001942 } else {
1943 parcel.writeInt(0);
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (contentView != null) {
1946 parcel.writeInt(1);
1947 contentView.writeToParcel(parcel, 0);
1948 } else {
1949 parcel.writeInt(0);
1950 }
Selim Cinek279fa862016-06-14 10:57:25 -07001951 if (mLargeIcon == null && largeIcon != null) {
1952 // you snuck an icon in here without using the builder; let's try to keep it
1953 mLargeIcon = Icon.createWithBitmap(largeIcon);
1954 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001955 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001956 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001957 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001958 } else {
1959 parcel.writeInt(0);
1960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961
1962 parcel.writeInt(defaults);
1963 parcel.writeInt(this.flags);
1964
1965 if (sound != null) {
1966 parcel.writeInt(1);
1967 sound.writeToParcel(parcel, 0);
1968 } else {
1969 parcel.writeInt(0);
1970 }
1971 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001972
1973 if (audioAttributes != null) {
1974 parcel.writeInt(1);
1975 audioAttributes.writeToParcel(parcel, 0);
1976 } else {
1977 parcel.writeInt(0);
1978 }
1979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 parcel.writeLongArray(vibrate);
1981 parcel.writeInt(ledARGB);
1982 parcel.writeInt(ledOnMS);
1983 parcel.writeInt(ledOffMS);
1984 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001985
1986 if (fullScreenIntent != null) {
1987 parcel.writeInt(1);
1988 fullScreenIntent.writeToParcel(parcel, 0);
1989 } else {
1990 parcel.writeInt(0);
1991 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001992
1993 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001994
John Spurlockfd7f1e02014-03-18 16:41:57 -04001995 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001996
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001997 parcel.writeString(mGroupKey);
1998
1999 parcel.writeString(mSortKey);
2000
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002001 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002002
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002003 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002004
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002005 if (bigContentView != null) {
2006 parcel.writeInt(1);
2007 bigContentView.writeToParcel(parcel, 0);
2008 } else {
2009 parcel.writeInt(0);
2010 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002011
Chris Wren8fd39ec2014-02-27 17:43:26 -05002012 if (headsUpContentView != null) {
2013 parcel.writeInt(1);
2014 headsUpContentView.writeToParcel(parcel, 0);
2015 } else {
2016 parcel.writeInt(0);
2017 }
2018
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002019 parcel.writeInt(visibility);
2020
2021 if (publicVersion != null) {
2022 parcel.writeInt(1);
2023 publicVersion.writeToParcel(parcel, 0);
2024 } else {
2025 parcel.writeInt(0);
2026 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002027
2028 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 }
2030
2031 /**
2032 * Parcelable.Creator that instantiates Notification objects
2033 */
2034 public static final Parcelable.Creator<Notification> CREATOR
2035 = new Parcelable.Creator<Notification>()
2036 {
2037 public Notification createFromParcel(Parcel parcel)
2038 {
2039 return new Notification(parcel);
2040 }
2041
2042 public Notification[] newArray(int size)
2043 {
2044 return new Notification[size];
2045 }
2046 };
2047
2048 /**
2049 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2050 * layout.
2051 *
2052 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2053 * in the view.</p>
2054 * @param context The context for your application / activity.
2055 * @param contentTitle The title that goes in the expanded entry.
2056 * @param contentText The text that goes in the expanded entry.
2057 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2058 * If this is an activity, it must include the
2059 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002060 * that you take care of task management as described in the
2061 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2062 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002063 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002064 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002065 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002067 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 public void setLatestEventInfo(Context context,
2069 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002070 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2071 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2072 new Throwable());
2073 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002074
Selim Cinek4ac6f602016-06-13 15:47:03 -07002075 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2076 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2077 }
2078
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002079 // ensure that any information already set directly is preserved
2080 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002081
2082 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002084 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
2086 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002087 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002089 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002090
2091 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093
Julia Reynoldsda303542015-11-23 14:00:20 -05002094 /**
2095 * @hide
2096 */
2097 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002098 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2099 }
2100
2101 /**
2102 * @hide
2103 */
2104 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2105 Notification notification) {
2106 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2107 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002108 }
2109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 @Override
2111 public String toString() {
2112 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002113 sb.append("Notification(pri=");
2114 sb.append(priority);
2115 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002116 if (contentView != null) {
2117 sb.append(contentView.getPackage());
2118 sb.append("/0x");
2119 sb.append(Integer.toHexString(contentView.getLayoutId()));
2120 } else {
2121 sb.append("null");
2122 }
2123 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002124 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2125 sb.append("default");
2126 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 int N = this.vibrate.length-1;
2128 sb.append("[");
2129 for (int i=0; i<N; i++) {
2130 sb.append(this.vibrate[i]);
2131 sb.append(',');
2132 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002133 if (N != -1) {
2134 sb.append(this.vibrate[N]);
2135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 } else {
2138 sb.append("null");
2139 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002140 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002141 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002143 } else if (this.sound != null) {
2144 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 } else {
2146 sb.append("null");
2147 }
Chris Wren365b6d32015-07-16 10:39:26 -04002148 if (this.tickerText != null) {
2149 sb.append(" tick");
2150 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002151 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002153 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002154 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002155 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002156 if (this.category != null) {
2157 sb.append(" category=");
2158 sb.append(this.category);
2159 }
2160 if (this.mGroupKey != null) {
2161 sb.append(" groupKey=");
2162 sb.append(this.mGroupKey);
2163 }
2164 if (this.mSortKey != null) {
2165 sb.append(" sortKey=");
2166 sb.append(this.mSortKey);
2167 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002168 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002169 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002170 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002171 }
2172 sb.append(" vis=");
2173 sb.append(visibilityToString(this.visibility));
2174 if (this.publicVersion != null) {
2175 sb.append(" publicVersion=");
2176 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002177 }
2178 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 return sb.toString();
2180 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002181
Dan Sandler1b718782014-07-18 12:43:45 -04002182 /**
2183 * {@hide}
2184 */
2185 public static String visibilityToString(int vis) {
2186 switch (vis) {
2187 case VISIBILITY_PRIVATE:
2188 return "PRIVATE";
2189 case VISIBILITY_PUBLIC:
2190 return "PUBLIC";
2191 case VISIBILITY_SECRET:
2192 return "SECRET";
2193 default:
2194 return "UNKNOWN(" + String.valueOf(vis) + ")";
2195 }
2196 }
2197
Joe Onoratocb109a02011-01-18 17:57:41 -08002198 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002199 * {@hide}
2200 */
2201 public static String priorityToString(@Priority int pri) {
2202 switch (pri) {
2203 case PRIORITY_MIN:
2204 return "MIN";
2205 case PRIORITY_LOW:
2206 return "LOW";
2207 case PRIORITY_DEFAULT:
2208 return "DEFAULT";
2209 case PRIORITY_HIGH:
2210 return "HIGH";
2211 case PRIORITY_MAX:
2212 return "MAX";
2213 default:
2214 return "UNKNOWN(" + String.valueOf(pri) + ")";
2215 }
2216 }
2217
2218 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002219 * The small icon representing this notification in the status bar and content view.
2220 *
2221 * @return the small icon representing this notification.
2222 *
2223 * @see Builder#getSmallIcon()
2224 * @see Builder#setSmallIcon(Icon)
2225 */
2226 public Icon getSmallIcon() {
2227 return mSmallIcon;
2228 }
2229
2230 /**
2231 * Used when notifying to clean up legacy small icons.
2232 * @hide
2233 */
2234 public void setSmallIcon(Icon icon) {
2235 mSmallIcon = icon;
2236 }
2237
2238 /**
2239 * The large icon shown in this notification's content view.
2240 * @see Builder#getLargeIcon()
2241 * @see Builder#setLargeIcon(Icon)
2242 */
2243 public Icon getLargeIcon() {
2244 return mLargeIcon;
2245 }
2246
2247 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002248 * @hide
2249 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002250 public boolean isGroupSummary() {
2251 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2252 }
2253
2254 /**
2255 * @hide
2256 */
2257 public boolean isGroupChild() {
2258 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2259 }
2260
2261 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002262 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002263 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002264 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002265 * content views using the platform's notification layout template. If your app supports
2266 * versions of Android as old as API level 4, you can instead use
2267 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2268 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2269 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002270 *
Scott Main183bf112012-08-13 19:12:13 -07002271 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002272 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002273 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002274 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002275 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2276 * .setContentText(subject)
2277 * .setSmallIcon(R.drawable.new_mail)
2278 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002279 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002280 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002281 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002282 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002283 /**
2284 * @hide
2285 */
2286 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2287 "android.rebuild.contentViewActionCount";
2288 /**
2289 * @hide
2290 */
2291 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2292 = "android.rebuild.bigViewActionCount";
2293 /**
2294 * @hide
2295 */
2296 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2297 = "android.rebuild.hudViewActionCount";
2298
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002299 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002300
Joe Onorato46439ce2010-11-19 13:56:21 -08002301 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002302 private Notification mN;
2303 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002304 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002305 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2306 private ArrayList<String> mPersonList = new ArrayList<String>();
2307 private NotificationColorUtil mColorUtil;
2308 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002309
2310 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002311 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2312 */
2313 private int mCachedContrastColor = COLOR_INVALID;
2314 private int mCachedContrastColorIsFor = COLOR_INVALID;
2315
2316 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002317 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002318 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002319
2320 * <table>
2321 * <tr><th align=right>priority</th>
2322 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2323 * <tr><th align=right>when</th>
2324 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2325 * <tr><th align=right>audio stream</th>
2326 * <td>{@link #STREAM_DEFAULT}</td></tr>
2327 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002329
2330 * @param context
2331 * A {@link Context} that will be used by the Builder to construct the
2332 * RemoteViews. The Context will not be held past the lifetime of this Builder
2333 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002334 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002335 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002336 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002337 }
2338
Joe Onoratocb109a02011-01-18 17:57:41 -08002339 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002340 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002341 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002342 public Builder(Context context, Notification toAdopt) {
2343 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002344
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002345 if (toAdopt == null) {
2346 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002347 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2348 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2349 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002350 mN.priority = PRIORITY_DEFAULT;
2351 mN.visibility = VISIBILITY_PRIVATE;
2352 } else {
2353 mN = toAdopt;
2354 if (mN.actions != null) {
2355 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002356 }
2357
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002358 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2359 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2360 }
2361
Selim Cinek4ac6f602016-06-13 15:47:03 -07002362 if (mN.getSmallIcon() == null && mN.icon != 0) {
2363 setSmallIcon(mN.icon);
2364 }
2365
2366 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2367 setLargeIcon(mN.largeIcon);
2368 }
2369
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002370 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2371 if (!TextUtils.isEmpty(templateClass)) {
2372 final Class<? extends Style> styleClass
2373 = getNotificationStyleClass(templateClass);
2374 if (styleClass == null) {
2375 Log.d(TAG, "Unknown style class: " + templateClass);
2376 } else {
2377 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002378 final Constructor<? extends Style> ctor =
2379 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002380 ctor.setAccessible(true);
2381 final Style style = ctor.newInstance();
2382 style.restoreFromExtras(mN.extras);
2383
2384 if (style != null) {
2385 setStyle(style);
2386 }
2387 } catch (Throwable t) {
2388 Log.e(TAG, "Could not create Style", t);
2389 }
2390 }
2391 }
2392
2393 }
2394 }
2395
2396 private NotificationColorUtil getColorUtil() {
2397 if (!mColorUtilInited) {
2398 mColorUtilInited = true;
2399 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2400 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002401 }
2402 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002403 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002404 }
2405
2406 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002407 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002408 *
2409 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2410 * shown anymore by default and must be opted into by using
2411 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002412 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002413 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002414 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002415 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002416 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002417 return this;
2418 }
2419
Joe Onoratocb109a02011-01-18 17:57:41 -08002420 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002421 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002422 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002423 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2424 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002425 */
2426 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002427 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002428 return this;
2429 }
2430
2431 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002432 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002433 *
2434 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002435 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002436 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002437 * Useful when showing an elapsed time (like an ongoing phone call).
2438 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002439 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002440 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002441 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002442 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002443 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002444 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002445 */
2446 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002447 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002448 return this;
2449 }
2450
2451 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002452 * Sets the Chronometer to count down instead of counting up.
2453 *
2454 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2455 * If it isn't set the chronometer will count up.
2456 *
2457 * @see #setUsesChronometer(boolean)
2458 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002459 public Builder setChronometerCountDown(boolean countDown) {
2460 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002461 return this;
2462 }
2463
2464 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002465 * Set the small icon resource, which will be used to represent the notification in the
2466 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002467 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002468
2469 * The platform template for the expanded view will draw this icon in the left, unless a
2470 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2471 * icon will be moved to the right-hand side.
2472 *
2473
2474 * @param icon
2475 * A resource ID in the application's package of the drawable to use.
2476 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002477 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002478 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002479 return setSmallIcon(icon != 0
2480 ? Icon.createWithResource(mContext, icon)
2481 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002482 }
2483
Joe Onoratocb109a02011-01-18 17:57:41 -08002484 /**
2485 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2486 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2487 * LevelListDrawable}.
2488 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002489 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002490 * @param level The level to use for the icon.
2491 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002492 * @see Notification#icon
2493 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002494 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002495 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002496 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002497 return setSmallIcon(icon);
2498 }
2499
2500 /**
2501 * Set the small icon, which will be used to represent the notification in the
2502 * status bar and content view (unless overriden there by a
2503 * {@link #setLargeIcon(Bitmap) large icon}).
2504 *
2505 * @param icon An Icon object to use.
2506 * @see Notification#icon
2507 */
2508 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002509 mN.setSmallIcon(icon);
2510 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2511 mN.icon = icon.getResId();
2512 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002513 return this;
2514 }
2515
Joe Onoratocb109a02011-01-18 17:57:41 -08002516 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002517 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002518 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002519 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002520 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002521 return this;
2522 }
2523
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002525 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002526 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002527 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002528 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002529 return this;
2530 }
2531
Joe Onoratocb109a02011-01-18 17:57:41 -08002532 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002533 * This provides some additional information that is displayed in the notification. No
2534 * guarantees are given where exactly it is displayed.
2535 *
2536 * <p>This information should only be provided if it provides an essential
2537 * benefit to the understanding of the notification. The more text you provide the
2538 * less readable it becomes. For example, an email client should only provide the account
2539 * name here if more than one email account has been added.</p>
2540 *
2541 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2542 * notification header area.
2543 *
2544 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2545 * this will be shown in the third line of text in the platform notification template.
2546 * You should not be using {@link #setProgress(int, int, boolean)} at the
2547 * same time on those versions; they occupy the same place.
2548 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002549 */
2550 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002551 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002552 return this;
2553 }
2554
2555 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002556 * Set the remote input history.
2557 *
2558 * This should be set to the most recent inputs that have been sent
2559 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2560 * longer relevant (e.g. for chat notifications once the other party has responded).
2561 *
2562 * The most recent input must be stored at the 0 index, the second most recent at the
2563 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2564 * and how much of each individual input is shown.
2565 *
2566 * <p>Note: The reply text will only be shown on notifications that have least one action
2567 * with a {@code RemoteInput}.</p>
2568 */
2569 public Builder setRemoteInputHistory(CharSequence[] text) {
2570 if (text == null) {
2571 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2572 } else {
2573 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2574 CharSequence[] safe = new CharSequence[N];
2575 for (int i = 0; i < N; i++) {
2576 safe[i] = safeCharSequence(text[i]);
2577 }
2578 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2579 }
2580 return this;
2581 }
2582
2583 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002584 * Set the large number at the right-hand side of the notification. This is
2585 * equivalent to setContentInfo, although it might show the number in a different
2586 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002587 *
2588 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002589 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002590 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002591 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002592 return this;
2593 }
2594
Joe Onoratocb109a02011-01-18 17:57:41 -08002595 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002596 * A small piece of additional information pertaining to this notification.
2597 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002598 * The platform template will draw this on the last line of the notification, at the far
2599 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002600 *
2601 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2602 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2603 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002604 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002605 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002606 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002607 return this;
2608 }
2609
Joe Onoratocb109a02011-01-18 17:57:41 -08002610 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002611 * Set the progress this notification represents.
2612 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002613 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002614 */
2615 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002616 mN.extras.putInt(EXTRA_PROGRESS, progress);
2617 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2618 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002619 return this;
2620 }
2621
2622 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002623 * Supply a custom RemoteViews to use instead of the platform template.
2624 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002625 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002626 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002627 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002628 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002629 return setCustomContentView(views);
2630 }
2631
2632 /**
2633 * Supply custom RemoteViews to use instead of the platform template.
2634 *
2635 * This will override the layout that would otherwise be constructed by this Builder
2636 * object.
2637 */
2638 public Builder setCustomContentView(RemoteViews contentView) {
2639 mN.contentView = contentView;
2640 return this;
2641 }
2642
2643 /**
2644 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2645 *
2646 * This will override the expanded layout that would otherwise be constructed by this
2647 * Builder object.
2648 */
2649 public Builder setCustomBigContentView(RemoteViews contentView) {
2650 mN.bigContentView = contentView;
2651 return this;
2652 }
2653
2654 /**
2655 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2656 *
2657 * This will override the heads-up layout that would otherwise be constructed by this
2658 * Builder object.
2659 */
2660 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2661 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002662 return this;
2663 }
2664
Joe Onoratocb109a02011-01-18 17:57:41 -08002665 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002666 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2667 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002668 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2669 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2670 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002671 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002672 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002673 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002674 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002675 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002676 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002677 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002678 return this;
2679 }
2680
Joe Onoratocb109a02011-01-18 17:57:41 -08002681 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002682 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2683 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002684 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002685 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002686 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002687 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002688 return this;
2689 }
2690
Joe Onoratocb109a02011-01-18 17:57:41 -08002691 /**
2692 * An intent to launch instead of posting the notification to the status bar.
2693 * Only for use with extremely high-priority notifications demanding the user's
2694 * <strong>immediate</strong> attention, such as an incoming phone call or
2695 * alarm clock that the user has explicitly set to a particular time.
2696 * If this facility is used for something else, please give the user an option
2697 * to turn it off and use a normal notification, as this can be extremely
2698 * disruptive.
2699 *
Chris Wren47c20a12014-06-18 17:27:29 -04002700 * <p>
2701 * The system UI may choose to display a heads-up notification, instead of
2702 * launching this intent, while the user is using the device.
2703 * </p>
2704 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002705 * @param intent The pending intent to launch.
2706 * @param highPriority Passing true will cause this notification to be sent
2707 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002708 *
2709 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002710 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002711 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002712 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002713 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2714 return this;
2715 }
2716
Joe Onoratocb109a02011-01-18 17:57:41 -08002717 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002718 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002719 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002720 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002721 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002722 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002723 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002724 return this;
2725 }
2726
Joe Onoratocb109a02011-01-18 17:57:41 -08002727 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002728 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002729 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002730 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002731 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002732 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002733 setTicker(tickerText);
2734 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002735 return this;
2736 }
2737
Joe Onoratocb109a02011-01-18 17:57:41 -08002738 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002739 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002740 *
2741 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002742 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2743 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002744 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002745 public Builder setLargeIcon(Bitmap b) {
2746 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2747 }
2748
2749 /**
2750 * Add a large icon to the notification content view.
2751 *
2752 * In the platform template, this image will be shown on the left of the notification view
2753 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2754 * badge atop the large icon).
2755 */
2756 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002757 mN.mLargeIcon = icon;
2758 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002759 return this;
2760 }
2761
Joe Onoratocb109a02011-01-18 17:57:41 -08002762 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002763 * Set the sound to play.
2764 *
John Spurlockc0650f022014-07-19 13:22:39 -04002765 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2766 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002767 *
Chris Wren47c20a12014-06-18 17:27:29 -04002768 * <p>
2769 * A notification that is noisy is more likely to be presented as a heads-up notification.
2770 * </p>
2771 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002772 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002773 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002774 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002775 mN.sound = sound;
2776 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002777 return this;
2778 }
2779
Joe Onoratocb109a02011-01-18 17:57:41 -08002780 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002781 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002782 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002783 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2784 *
Chris Wren47c20a12014-06-18 17:27:29 -04002785 * <p>
2786 * A notification that is noisy is more likely to be presented as a heads-up notification.
2787 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002788 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002789 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002790 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002791 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002792 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002793 mN.sound = sound;
2794 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002795 return this;
2796 }
2797
Joe Onoratocb109a02011-01-18 17:57:41 -08002798 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002799 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2800 * use during playback.
2801 *
2802 * <p>
2803 * A notification that is noisy is more likely to be presented as a heads-up notification.
2804 * </p>
2805 *
2806 * @see Notification#sound
2807 */
2808 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002809 mN.sound = sound;
2810 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002811 return this;
2812 }
2813
2814 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002815 * Set the vibration pattern to use.
2816 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002817 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2818 * <code>pattern</code> parameter.
2819 *
Chris Wren47c20a12014-06-18 17:27:29 -04002820 * <p>
2821 * A notification that vibrates is more likely to be presented as a heads-up notification.
2822 * </p>
2823 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002824 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002825 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002826 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002827 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002828 return this;
2829 }
2830
Joe Onoratocb109a02011-01-18 17:57:41 -08002831 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002832 * Set the desired color for the indicator LED on the device, as well as the
2833 * blink duty cycle (specified in milliseconds).
2834 *
2835
2836 * Not all devices will honor all (or even any) of these values.
2837 *
2838
2839 * @see Notification#ledARGB
2840 * @see Notification#ledOnMS
2841 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002842 */
Tor Norbye80756e32015-03-02 09:39:27 -08002843 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002844 mN.ledARGB = argb;
2845 mN.ledOnMS = onMs;
2846 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002847 if (onMs != 0 || offMs != 0) {
2848 mN.flags |= FLAG_SHOW_LIGHTS;
2849 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002850 return this;
2851 }
2852
Joe Onoratocb109a02011-01-18 17:57:41 -08002853 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002854 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002855 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002856
2857 * Ongoing notifications cannot be dismissed by the user, so your application or service
2858 * must take care of canceling them.
2859 *
2860
2861 * They are typically used to indicate a background task that the user is actively engaged
2862 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2863 * (e.g., a file download, sync operation, active network connection).
2864 *
2865
2866 * @see Notification#FLAG_ONGOING_EVENT
2867 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002868 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002869 public Builder setOngoing(boolean ongoing) {
2870 setFlag(FLAG_ONGOING_EVENT, ongoing);
2871 return this;
2872 }
2873
Joe Onoratocb109a02011-01-18 17:57:41 -08002874 /**
2875 * Set this flag if you would only like the sound, vibrate
2876 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002877 *
2878 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002879 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002880 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2881 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2882 return this;
2883 }
2884
Joe Onoratocb109a02011-01-18 17:57:41 -08002885 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002886 * Make this notification automatically dismissed when the user touches it. The
2887 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2888 *
2889 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002890 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002891 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002892 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002893 return this;
2894 }
2895
Joe Onoratocb109a02011-01-18 17:57:41 -08002896 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002897 * Set whether or not this notification should not bridge to other devices.
2898 *
2899 * <p>Some notifications can be bridged to other devices for remote display.
2900 * This hint can be set to recommend this notification not be bridged.
2901 */
2902 public Builder setLocalOnly(boolean localOnly) {
2903 setFlag(FLAG_LOCAL_ONLY, localOnly);
2904 return this;
2905 }
2906
2907 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002908 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002909 * <p>
2910 * The value should be one or more of the following fields combined with
2911 * bitwise-or:
2912 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2913 * <p>
2914 * For all default values, use {@link #DEFAULT_ALL}.
2915 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002916 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002917 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002918 return this;
2919 }
2920
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002921 /**
2922 * Set the priority of this notification.
2923 *
2924 * @see Notification#priority
2925 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002926 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002927 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002928 return this;
2929 }
Joe Malin8d40d042012-11-05 11:36:40 -08002930
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002931 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002932 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002933 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002934 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002935 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002936 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002937 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002938 return this;
2939 }
2940
2941 /**
Chris Wrendde75302014-03-26 17:24:15 -04002942 * Add a person that is relevant to this notification.
2943 *
Chris Wrene6c48932014-09-29 17:19:27 -04002944 * <P>
2945 * Depending on user preferences, this annotation may allow the notification to pass
2946 * through interruption filters, and to appear more prominently in the user interface.
2947 * </P>
2948 *
2949 * <P>
2950 * The person should be specified by the {@code String} representation of a
2951 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2952 * </P>
2953 *
2954 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2955 * URIs. The path part of these URIs must exist in the contacts database, in the
2956 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2957 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2958 * </P>
2959 *
2960 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002961 * @see Notification#EXTRA_PEOPLE
2962 */
Chris Wrene6c48932014-09-29 17:19:27 -04002963 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002964 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002965 return this;
2966 }
2967
2968 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002969 * Set this notification to be part of a group of notifications sharing the same key.
2970 * Grouped notifications may display in a cluster or stack on devices which
2971 * support such rendering.
2972 *
2973 * <p>To make this notification the summary for its group, also call
2974 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2975 * {@link #setSortKey}.
2976 * @param groupKey The group key of the group.
2977 * @return this object for method chaining
2978 */
2979 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002980 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002981 return this;
2982 }
2983
2984 /**
2985 * Set this notification to be the group summary for a group of notifications.
2986 * Grouped notifications may display in a cluster or stack on devices which
2987 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2988 * @param isGroupSummary Whether this notification should be a group summary.
2989 * @return this object for method chaining
2990 */
2991 public Builder setGroupSummary(boolean isGroupSummary) {
2992 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2993 return this;
2994 }
2995
2996 /**
2997 * Set a sort key that orders this notification among other notifications from the
2998 * same package. This can be useful if an external sort was already applied and an app
2999 * would like to preserve this. Notifications will be sorted lexicographically using this
3000 * value, although providing different priorities in addition to providing sort key may
3001 * cause this value to be ignored.
3002 *
3003 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003004 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003005 *
3006 * @see String#compareTo(String)
3007 */
3008 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003009 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003010 return this;
3011 }
3012
3013 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003014 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003015 *
Griff Hazen720042b2014-02-24 15:46:56 -08003016 * <p>Values within the Bundle will replace existing extras values in this Builder.
3017 *
3018 * @see Notification#extras
3019 */
Griff Hazen959591e2014-05-15 22:26:18 -07003020 public Builder addExtras(Bundle extras) {
3021 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003022 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003023 }
3024 return this;
3025 }
3026
3027 /**
3028 * Set metadata for this notification.
3029 *
3030 * <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 -04003031 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003032 * called.
3033 *
Griff Hazen720042b2014-02-24 15:46:56 -08003034 * <p>Replaces any existing extras values with those from the provided Bundle.
3035 * Use {@link #addExtras} to merge in metadata instead.
3036 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003037 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003038 */
Griff Hazen959591e2014-05-15 22:26:18 -07003039 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003040 if (extras != null) {
3041 mUserExtras = extras;
3042 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003043 return this;
3044 }
3045
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003046 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003047 * Get the current metadata Bundle used by this notification Builder.
3048 *
3049 * <p>The returned Bundle is shared with this Builder.
3050 *
3051 * <p>The current contents of this Bundle are copied into the Notification each time
3052 * {@link #build()} is called.
3053 *
3054 * @see Notification#extras
3055 */
3056 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003057 return mUserExtras;
3058 }
3059
3060 private Bundle getAllExtras() {
3061 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3062 saveExtras.putAll(mN.extras);
3063 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003064 }
3065
3066 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003067 * Add an action to this notification. Actions are typically displayed by
3068 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003069 * <p>
3070 * Every action must have an icon (32dp square and matching the
3071 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3072 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3073 * <p>
3074 * A notification in its expanded form can display up to 3 actions, from left to right in
3075 * the order they were added. Actions will not be displayed when the notification is
3076 * collapsed, however, so be sure that any essential functions may be accessed by the user
3077 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003078 *
3079 * @param icon Resource ID of a drawable that represents the action.
3080 * @param title Text describing the action.
3081 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003082 *
3083 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003084 */
Dan Sandler86647982015-05-13 23:41:13 -04003085 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003086 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003087 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003088 return this;
3089 }
3090
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003091 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003092 * Add an action to this notification. Actions are typically displayed by
3093 * the system as a button adjacent to the notification content.
3094 * <p>
3095 * Every action must have an icon (32dp square and matching the
3096 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3097 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3098 * <p>
3099 * A notification in its expanded form can display up to 3 actions, from left to right in
3100 * the order they were added. Actions will not be displayed when the notification is
3101 * collapsed, however, so be sure that any essential functions may be accessed by the user
3102 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3103 *
3104 * @param action The action to add.
3105 */
3106 public Builder addAction(Action action) {
3107 mActions.add(action);
3108 return this;
3109 }
3110
3111 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003112 * Alter the complete list of actions attached to this notification.
3113 * @see #addAction(Action).
3114 *
3115 * @param actions
3116 * @return
3117 */
3118 public Builder setActions(Action... actions) {
3119 mActions.clear();
3120 for (int i = 0; i < actions.length; i++) {
3121 mActions.add(actions[i]);
3122 }
3123 return this;
3124 }
3125
3126 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003127 * Add a rich notification style to be applied at build time.
3128 *
3129 * @param style Object responsible for modifying the notification style.
3130 */
3131 public Builder setStyle(Style style) {
3132 if (mStyle != style) {
3133 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003134 if (mStyle != null) {
3135 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003136 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3137 } else {
3138 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003139 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003140 }
3141 return this;
3142 }
3143
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003144 /**
3145 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003146 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003147 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3148 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3149 *
3150 * @return The same Builder.
3151 */
3152 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003153 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003154 return this;
3155 }
3156
3157 /**
3158 * Supply a replacement Notification whose contents should be shown in insecure contexts
3159 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3160 * @param n A replacement notification, presumably with some or all info redacted.
3161 * @return The same Builder.
3162 */
3163 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003164 if (n != null) {
3165 mN.publicVersion = new Notification();
3166 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3167 } else {
3168 mN.publicVersion = null;
3169 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003170 return this;
3171 }
3172
Griff Hazenb720abe2014-05-20 13:15:30 -07003173 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003174 * Apply an extender to this notification builder. Extenders may be used to add
3175 * metadata or change options on this builder.
3176 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003177 public Builder extend(Extender extender) {
3178 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003179 return this;
3180 }
3181
Dan Sandler4e787062015-06-17 15:09:48 -04003182 /**
3183 * @hide
3184 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003185 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003186 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003187 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003188 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003189 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003190 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003191 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003192 }
3193
Dan Sandler26e81cf2014-05-06 10:01:27 -04003194 /**
3195 * Sets {@link Notification#color}.
3196 *
3197 * @param argb The accent color to use
3198 *
3199 * @return The same Builder.
3200 */
Tor Norbye80756e32015-03-02 09:39:27 -08003201 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003202 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003203 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003204 return this;
3205 }
3206
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003207 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003208 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3209 // This user can never be a badged profile,
3210 // and also includes USER_ALL system notifications.
3211 return null;
3212 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003213 // Note: This assumes that the current user can read the profile badge of the
3214 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003215 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003216 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003217 }
3218
3219 private Bitmap getProfileBadge() {
3220 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003221 if (badge == null) {
3222 return null;
3223 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003224 final int size = mContext.getResources().getDimensionPixelSize(
3225 R.dimen.notification_badge_size);
3226 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003227 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003228 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003229 badge.draw(canvas);
3230 return bitmap;
3231 }
3232
Selim Cinekc848c3a2016-01-13 15:27:30 -08003233 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003234 Bitmap profileBadge = getProfileBadge();
3235
Kenny Guy98193ea2014-07-24 19:54:37 +01003236 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003237 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3238 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003239 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003240 }
3241
Christoph Studerfe718432014-09-01 18:21:18 +02003242 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003243 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003244 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003245 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003246 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003247 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003248 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003249 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003250 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003251 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003252 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003253 }
3254
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003255 /**
3256 * Resets the notification header to its original state
3257 */
3258 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003259 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3260 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003261 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003262 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003263 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003264 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003265 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003266 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003267 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003268 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003269 contentView.setImageViewIcon(R.id.profile_badge, null);
3270 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003271 }
3272
3273 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003274 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3275 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003276 }
3277
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003278 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003279 return applyStandardTemplate(resId, true /* hasProgress */);
3280 }
3281
3282 /**
3283 * @param hasProgress whether the progress bar should be shown and set
3284 */
3285 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003286 final Bundle ex = mN.extras;
3287
3288 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3289 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3290 return applyStandardTemplate(resId, hasProgress, title, text);
3291 }
3292
3293 /**
3294 * @param hasProgress whether the progress bar should be shown and set
3295 */
3296 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3297 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003298 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003299
Christoph Studerfe718432014-09-01 18:21:18 +02003300 resetStandardTemplate(contentView);
3301
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003302 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003303
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003304 bindNotificationHeader(contentView);
3305 bindLargeIcon(contentView);
Selim Cinek954cc232016-05-20 13:29:23 -07003306 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003307 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003308 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003309 contentView.setTextViewText(R.id.title, title);
Selim Cinek954cc232016-05-20 13:29:23 -07003310 contentView.setViewLayoutWidth(R.id.title, showProgress
3311 ? ViewGroup.LayoutParams.WRAP_CONTENT
3312 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003313 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07003314 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003315 int textId = showProgress ? com.android.internal.R.id.text_line_1
3316 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003317 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003318 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003319 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003320
Selim Cinek279fa862016-06-14 10:57:25 -07003321 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003322
Selim Cinek29603462015-11-17 19:04:39 -08003323 return contentView;
3324 }
3325
Selim Cinek860b6da2015-12-16 19:02:19 -08003326 /**
3327 * @param remoteView the remote view to update the minheight in
3328 * @param hasMinHeight does it have a mimHeight
3329 * @hide
3330 */
3331 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3332 int minHeight = 0;
3333 if (hasMinHeight) {
3334 // we need to set the minHeight of the notification
3335 minHeight = mContext.getResources().getDimensionPixelSize(
3336 com.android.internal.R.dimen.notification_min_content_height);
3337 }
3338 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3339 }
3340
Selim Cinek29603462015-11-17 19:04:39 -08003341 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003342 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3343 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3344 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3345 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003346 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003347 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003348 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003349 contentView.setProgressBackgroundTintList(
3350 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3351 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003352 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003353 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003354 contentView.setProgressTintList(R.id.progress, colorStateList);
3355 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003356 }
Selim Cinek29603462015-11-17 19:04:39 -08003357 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003358 } else {
3359 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003360 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003361 }
Joe Onorato561d3852010-11-20 18:09:34 -08003362 }
3363
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003364 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003365 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3366 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3367 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003368 if (mN.mLargeIcon != null) {
3369 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3370 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3371 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003372 int endMargin = R.dimen.notification_content_picture_margin;
3373 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3374 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3375 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003376 }
3377 }
3378
3379 private void bindNotificationHeader(RemoteViews contentView) {
3380 bindSmallIcon(contentView);
3381 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003382 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003383 bindHeaderChronometerAndTime(contentView);
3384 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003385 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003386 }
3387
3388 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003389 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003390 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003391 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003392 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003393 }
3394
3395 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3396 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003397 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003398 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3399 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3400 contentView.setLong(R.id.chronometer, "setBase",
3401 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3402 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003403 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003404 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003405 } else {
3406 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3407 contentView.setLong(R.id.time, "setTime", mN.when);
3408 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003409 } else {
3410 // We still want a time to be set but gone, such that we can show and hide it
3411 // on demand in case it's a child notification without anything in the header
3412 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003413 }
3414 }
3415
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003416 private void bindHeaderText(RemoteViews contentView) {
3417 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3418 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003419 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003420 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003421 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003422 if (headerText == null
3423 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3424 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3425 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3426 }
3427 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003428 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003429 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3430 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3431 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003432 }
3433 }
3434
Adrian Rooseba05822016-04-22 17:09:27 -07003435 /**
3436 * @hide
3437 */
3438 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003439 CharSequence name = null;
3440 final PackageManager pm = mContext.getPackageManager();
3441 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3442 // only system packages which lump together a bunch of unrelated stuff
3443 // may substitute a different name to make the purpose of the
3444 // notification more clear. the correct package label should always
3445 // be accessible via SystemUI.
3446 final String pkg = mContext.getPackageName();
3447 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3448 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3449 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3450 name = subName;
3451 } else {
3452 Log.w(TAG, "warning: pkg "
3453 + pkg + " attempting to substitute app name '" + subName
3454 + "' without holding perm "
3455 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3456 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003457 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003458 if (TextUtils.isEmpty(name)) {
3459 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3460 }
3461 if (TextUtils.isEmpty(name)) {
3462 // still nothing?
3463 return null;
3464 }
3465
3466 return String.valueOf(name);
3467 }
3468 private void bindHeaderAppName(RemoteViews contentView) {
3469 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003470 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003471 }
3472
3473 private void bindSmallIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003474 if (mN.mSmallIcon == null && mN.icon != 0) {
3475 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3476 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003477 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003478 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3479 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003480 processSmallIconColor(mN.mSmallIcon, contentView);
3481 }
3482
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003483 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003484 * @return true if the built notification will show the time or the chronometer; false
3485 * otherwise
3486 */
3487 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003488 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003489 }
3490
Christoph Studerfe718432014-09-01 18:21:18 +02003491 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003492 // actions_container is only reset when there are no actions to avoid focus issues with
3493 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003494 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003495 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003496
3497 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3498 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3499
3500 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3501 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3502 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3503 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003504
3505 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003506 }
3507
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003508 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003509 final Bundle ex = mN.extras;
3510
3511 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3512 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3513 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3514 }
3515
3516 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3517 CharSequence title, CharSequence text) {
3518 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003519
Christoph Studerfe718432014-09-01 18:21:18 +02003520 resetStandardTemplateWithActions(big);
3521
Adrian Roose458aa82015-12-08 16:17:19 -08003522 boolean validRemoteInput = false;
3523
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003524 int N = mActions.size();
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003525 boolean emphazisedMode = mN.fullScreenIntent != null;
3526 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003527 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003528 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003529 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roosf852a422016-06-03 13:33:43 -07003530 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3531 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003532 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003533 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003534 Action action = mActions.get(i);
3535 validRemoteInput |= hasValidRemoteInput(action);
3536
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003537 final RemoteViews button = generateActionButton(action, emphazisedMode,
3538 i % 2 != 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003539 big.addView(R.id.actions, button);
3540 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003541 } else {
3542 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003543 }
Adrian Roose458aa82015-12-08 16:17:19 -08003544
3545 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3546 if (validRemoteInput && replyText != null
3547 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3548 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3549 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3550
3551 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3552 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3553 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3554
3555 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3556 big.setViewVisibility(
3557 R.id.notification_material_reply_text_3, View.VISIBLE);
3558 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3559 }
3560 }
3561 }
3562
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003563 return big;
3564 }
3565
Adrian Roose458aa82015-12-08 16:17:19 -08003566 private boolean hasValidRemoteInput(Action action) {
3567 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3568 // Weird actions
3569 return false;
3570 }
3571
3572 RemoteInput[] remoteInputs = action.getRemoteInputs();
3573 if (remoteInputs == null) {
3574 return false;
3575 }
3576
3577 for (RemoteInput r : remoteInputs) {
3578 CharSequence[] choices = r.getChoices();
3579 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3580 return true;
3581 }
3582 }
3583 return false;
3584 }
3585
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003586 /**
3587 * Construct a RemoteViews for the final 1U notification layout. In order:
3588 * 1. Custom contentView from the caller
3589 * 2. Style's proposed content view
3590 * 3. Standard template view
3591 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003592 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003593 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003594 return mN.contentView;
3595 } else if (mStyle != null) {
3596 final RemoteViews styleView = mStyle.makeContentView();
3597 if (styleView != null) {
3598 return styleView;
3599 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003600 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003601 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003602 }
3603
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003604 /**
3605 * Construct a RemoteViews for the final big notification layout.
3606 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003607 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003608 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003609 if (mN.bigContentView != null
3610 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003611 return mN.bigContentView;
3612 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003613 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003614 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003615 } else if (mActions.size() != 0) {
3616 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003617 }
3618 adaptNotificationHeaderForBigContentView(result);
3619 return result;
3620 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003621
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003622 /**
3623 * Construct a RemoteViews for the final notification header only
3624 *
3625 * @hide
3626 */
3627 public RemoteViews makeNotificationHeader() {
3628 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3629 R.layout.notification_template_header);
3630 resetNotificationHeader(header);
3631 bindNotificationHeader(header);
3632 return header;
3633 }
3634
Selim Cinek29603462015-11-17 19:04:39 -08003635 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003636 if (result != null) {
3637 result.setViewVisibility(R.id.text_line_1, View.GONE);
3638 }
Selim Cinek29603462015-11-17 19:04:39 -08003639 }
3640
Selim Cinek850a8542015-11-11 11:48:36 -05003641 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003642 if (result != null) {
3643 result.setBoolean(R.id.notification_header, "setExpanded", true);
3644 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003645 }
3646
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003647 /**
3648 * Construct a RemoteViews for the final heads-up notification layout.
3649 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003650 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003651 if (mN.headsUpContentView != null
3652 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003653 return mN.headsUpContentView;
3654 } else if (mStyle != null) {
3655 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3656 if (styleView != null) {
3657 return styleView;
3658 }
3659 } else if (mActions.size() == 0) {
3660 return null;
3661 }
3662
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003663 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003664 }
3665
Selim Cinek624c02db2015-12-14 21:00:02 -08003666 /**
3667 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3668 *
3669 * @hide
3670 */
3671 public RemoteViews makePublicContentView() {
3672 if (mN.publicVersion != null) {
3673 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003674 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003675 }
3676 Bundle savedBundle = mN.extras;
3677 Style style = mStyle;
3678 mStyle = null;
3679 Icon largeIcon = mN.mLargeIcon;
3680 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003681 Bitmap largeIconLegacy = mN.largeIcon;
3682 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003683 Bundle publicExtras = new Bundle();
3684 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3685 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3686 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3687 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003688 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3689 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003690 publicExtras.putCharSequence(EXTRA_TITLE,
3691 mContext.getString(R.string.notification_hidden_text));
3692 mN.extras = publicExtras;
3693 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3694 mN.extras = savedBundle;
3695 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003696 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003697 mStyle = style;
3698 return publicView;
3699 }
3700
3701
Chris Wren8fd39ec2014-02-27 17:43:26 -05003702
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003703 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
3704 boolean oddAction) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003705 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003706 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003707 emphazisedMode ? getEmphasizedActionLayoutResource()
3708 : tombstone ? getActionTombstoneLayoutResource()
3709 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04003710 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003711 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003712 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003713 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003714 if (action.mRemoteInputs != null) {
3715 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3716 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003717 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07003718 // change the background bgColor
3719 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
3720 : R.color.notification_action_list_dark);
3721 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003722 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07003723 CharSequence title = action.title;
3724 ColorStateList[] outResultColor = null;
3725 if (isLegacy()) {
3726 title = clearColorSpans(title);
3727 } else {
3728 outResultColor = new ColorStateList[1];
3729 title = ensureColorSpanContrast(title, bgColor, outResultColor);
3730 }
3731 button.setTextViewText(R.id.action0, title);
3732 if (outResultColor != null && outResultColor[0] != null) {
3733 // We need to set the text color as well since changing a text to uppercase
3734 // clears its spans.
3735 button.setTextColor(R.id.action0, outResultColor[0]);
3736 } else if (mN.color != COLOR_DEFAULT) {
3737 button.setTextColor(R.id.action0,resolveContrastColor());
3738 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003739 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07003740 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003741 if (mN.color != COLOR_DEFAULT) {
3742 button.setTextColor(R.id.action0, resolveContrastColor());
3743 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003744 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003745 return button;
3746 }
3747
Joe Onoratocb109a02011-01-18 17:57:41 -08003748 /**
Selim Cinek981962e2016-07-20 20:41:58 -07003749 * Clears all color spans of a text
3750 * @param charSequence the input text
3751 * @return the same text but without color spans
3752 */
3753 private CharSequence clearColorSpans(CharSequence charSequence) {
3754 if (charSequence instanceof Spanned) {
3755 Spanned ss = (Spanned) charSequence;
3756 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3757 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3758 for (Object span : spans) {
3759 Object resultSpan = span;
3760 if (resultSpan instanceof CharacterStyle) {
3761 resultSpan = ((CharacterStyle) span).getUnderlying();
3762 }
3763 if (resultSpan instanceof TextAppearanceSpan) {
3764 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3765 if (originalSpan.getTextColor() != null) {
3766 resultSpan = new TextAppearanceSpan(
3767 originalSpan.getFamily(),
3768 originalSpan.getTextStyle(),
3769 originalSpan.getTextSize(),
3770 null,
3771 originalSpan.getLinkTextColor());
3772 }
3773 } else if (resultSpan instanceof ForegroundColorSpan
3774 || (resultSpan instanceof BackgroundColorSpan)) {
3775 continue;
3776 } else {
3777 resultSpan = span;
3778 }
3779 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
3780 ss.getSpanFlags(span));
3781 }
3782 return builder;
3783 }
3784 return charSequence;
3785 }
3786
3787 /**
3788 * Ensures contrast on color spans against a background color. also returns the color of the
3789 * text if a span was found that spans over the whole text.
3790 *
3791 * @param charSequence the charSequence on which the spans are
3792 * @param background the background color to ensure the contrast against
3793 * @param outResultColor an array in which a color will be returned as the first element if
3794 * there exists a full length color span.
3795 * @return the contrasted charSequence
3796 */
3797 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
3798 ColorStateList[] outResultColor) {
3799 if (charSequence instanceof Spanned) {
3800 Spanned ss = (Spanned) charSequence;
3801 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3802 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3803 for (Object span : spans) {
3804 Object resultSpan = span;
3805 int spanStart = ss.getSpanStart(span);
3806 int spanEnd = ss.getSpanEnd(span);
3807 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
3808 if (resultSpan instanceof CharacterStyle) {
3809 resultSpan = ((CharacterStyle) span).getUnderlying();
3810 }
3811 if (resultSpan instanceof TextAppearanceSpan) {
3812 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3813 ColorStateList textColor = originalSpan.getTextColor();
3814 if (textColor != null) {
3815 int[] colors = textColor.getColors();
3816 int[] newColors = new int[colors.length];
3817 for (int i = 0; i < newColors.length; i++) {
3818 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
3819 colors[i], background);
3820 }
3821 textColor = new ColorStateList(textColor.getStates().clone(),
3822 newColors);
3823 resultSpan = new TextAppearanceSpan(
3824 originalSpan.getFamily(),
3825 originalSpan.getTextStyle(),
3826 originalSpan.getTextSize(),
3827 textColor,
3828 originalSpan.getLinkTextColor());
3829 if (fullLength) {
3830 outResultColor[0] = new ColorStateList(
3831 textColor.getStates().clone(), newColors);
3832 }
3833 }
3834 } else if (resultSpan instanceof ForegroundColorSpan) {
3835 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
3836 int foregroundColor = originalSpan.getForegroundColor();
3837 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
3838 foregroundColor, background);
3839 resultSpan = new ForegroundColorSpan(foregroundColor);
3840 if (fullLength) {
3841 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
3842 }
3843 } else {
3844 resultSpan = span;
3845 }
3846
3847 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
3848 }
3849 return builder;
3850 }
3851 return charSequence;
3852 }
3853
3854 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003855 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003856 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003857 */
3858 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003859 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003860 }
3861
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003862 private CharSequence processLegacyText(CharSequence charSequence) {
3863 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003864 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003865 } else {
3866 return charSequence;
3867 }
3868 }
3869
Dan Sandler26e81cf2014-05-06 10:01:27 -04003870 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003871 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003872 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003873 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003874 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3875 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003876 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003877 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003878
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003879 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003880 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003881 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003882 }
3883
Dan Sandler26e81cf2014-05-06 10:01:27 -04003884 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003885 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003886 * if it's grayscale).
3887 */
3888 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003889 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3890 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003891 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003892 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003893 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003894 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003895 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003896 }
3897
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003898 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003899 if (mN.color != COLOR_DEFAULT) {
3900 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003901 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003902 }
3903
Adrian Roos4ff3b122016-02-01 12:26:13 -08003904 int resolveContrastColor() {
3905 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3906 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003907 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003908 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3909
3910 mCachedContrastColorIsFor = mN.color;
3911 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003912 }
3913
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003914 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003915 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003916 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003917 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003918 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003919 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003920 mN.actions = new Action[mActions.size()];
3921 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003922 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003923 if (!mPersonList.isEmpty()) {
3924 mN.extras.putStringArray(EXTRA_PEOPLE,
3925 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003926 }
Selim Cinek247fa012016-02-18 09:50:48 -08003927 if (mN.bigContentView != null || mN.contentView != null
3928 || mN.headsUpContentView != null) {
3929 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3930 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003931 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003932 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003933
Julia Reynolds3b848122016-02-26 10:45:32 -05003934 /**
3935 * Creates a Builder from an existing notification so further changes can be made.
3936 * @param context The context for your application / activity.
3937 * @param n The notification to create a Builder from.
3938 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003939 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003940 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003941 ApplicationInfo applicationInfo = n.extras.getParcelable(
3942 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003943 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003944 if (applicationInfo != null) {
3945 try {
3946 builderContext = context.createApplicationContext(applicationInfo,
3947 Context.CONTEXT_RESTRICTED);
3948 } catch (NameNotFoundException e) {
3949 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3950 builderContext = context; // try with our context
3951 }
3952 } else {
3953 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003954 }
3955
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003956 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003957 }
3958
3959 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003960 Class<? extends Style>[] classes = new Class[] {
3961 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003962 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3963 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003964 for (Class<? extends Style> innerClass : classes) {
3965 if (templateClass.equals(innerClass.getName())) {
3966 return innerClass;
3967 }
3968 }
3969 return null;
3970 }
3971
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003972 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003973 * @deprecated Use {@link #build()} instead.
3974 */
3975 @Deprecated
3976 public Notification getNotification() {
3977 return build();
3978 }
3979
3980 /**
3981 * Combine all of the options that have been set and return a new {@link Notification}
3982 * object.
3983 */
3984 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003985 // first, add any extras from the calling code
3986 if (mUserExtras != null) {
3987 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003988 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003990 mN.creationTime = System.currentTimeMillis();
3991
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003992 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003993 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003994
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003995 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003996
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003997 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003998 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003999 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004000
Adrian Roos5081c0d2016-02-26 16:04:19 -08004001 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4002 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004003 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004004 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004005 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4006 mN.contentView.getSequenceNumber());
4007 }
4008 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004009 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004010 if (mN.bigContentView != null) {
4011 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4012 mN.bigContentView.getSequenceNumber());
4013 }
4014 }
4015 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004016 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004017 if (mN.headsUpContentView != null) {
4018 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4019 mN.headsUpContentView.getSequenceNumber());
4020 }
4021 }
4022 }
4023
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004024 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4025 mN.flags |= FLAG_SHOW_LIGHTS;
4026 }
4027
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004028 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004029 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004030
4031 /**
4032 * Apply this Builder to an existing {@link Notification} object.
4033 *
4034 * @hide
4035 */
4036 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004037 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004038 return n;
4039 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004040
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004041 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004042 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4043 * change.
4044 *
4045 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4046 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004047 * @hide
4048 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004049 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004050 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004051
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004052 // Only strip views for known Styles because we won't know how to
4053 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004054 if (!TextUtils.isEmpty(templateClass)
4055 && getNotificationStyleClass(templateClass) == null) {
4056 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004057 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004058
4059 // Only strip unmodified BuilderRemoteViews.
4060 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004061 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004062 n.contentView.getSequenceNumber();
4063 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004064 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004065 n.bigContentView.getSequenceNumber();
4066 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004067 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004068 n.headsUpContentView.getSequenceNumber();
4069
4070 // Nothing to do here, no need to clone.
4071 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4072 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004073 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004074
4075 Notification clone = n.clone();
4076 if (stripContentView) {
4077 clone.contentView = null;
4078 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4079 }
4080 if (stripBigContentView) {
4081 clone.bigContentView = null;
4082 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4083 }
4084 if (stripHeadsUpContentView) {
4085 clone.headsUpContentView = null;
4086 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4087 }
4088 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004089 }
4090
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004091 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004092 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004093 }
4094
4095 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004096 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004097 }
4098
4099 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004100 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004101 }
4102
4103 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004104 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004105 }
4106
4107 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004108 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004109 }
4110
Adrian Roosc1a80b02016-04-05 14:54:55 -07004111 private int getMessagingLayoutResource() {
4112 return R.layout.notification_template_material_messaging;
4113 }
4114
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004115 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004116 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004117 }
4118
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004119 private int getEmphasizedActionLayoutResource() {
4120 return R.layout.notification_material_action_emphasized;
4121 }
4122
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004123 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004124 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004125 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004126 }
4127
Selim Cinek279fa862016-06-14 10:57:25 -07004128 private boolean hasLargeIcon() {
4129 return mLargeIcon != null || largeIcon != null;
4130 }
4131
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004132 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004133 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004134 * @hide
4135 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004136 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004137 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4138 }
4139
4140 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004141 * @return true if the notification will show a chronometer; false otherwise
4142 * @hide
4143 */
4144 public boolean showsChronometer() {
4145 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4146 }
4147
4148 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004149 * An object that can apply a rich notification style to a {@link Notification.Builder}
4150 * object.
4151 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004152 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004153 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004154
4155 /**
4156 * @hide
4157 */
4158 protected CharSequence mSummaryText = null;
4159
4160 /**
4161 * @hide
4162 */
4163 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004164
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004165 protected Builder mBuilder;
4166
Chris Wrend6297db2012-05-03 16:20:13 -04004167 /**
4168 * Overrides ContentTitle in the big form of the template.
4169 * This defaults to the value passed to setContentTitle().
4170 */
4171 protected void internalSetBigContentTitle(CharSequence title) {
4172 mBigContentTitle = title;
4173 }
4174
4175 /**
4176 * Set the first line of text after the detail section in the big form of the template.
4177 */
4178 protected void internalSetSummaryText(CharSequence cs) {
4179 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004180 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004181 }
4182
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004183 public void setBuilder(Builder builder) {
4184 if (mBuilder != builder) {
4185 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004186 if (mBuilder != null) {
4187 mBuilder.setStyle(this);
4188 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004189 }
4190 }
4191
Chris Wrend6297db2012-05-03 16:20:13 -04004192 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004193 if (mBuilder == null) {
4194 throw new IllegalArgumentException("Style requires a valid Builder object");
4195 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004196 }
Chris Wrend6297db2012-05-03 16:20:13 -04004197
4198 protected RemoteViews getStandardView(int layoutId) {
4199 checkBuilder();
4200
Christoph Studer4600f9b2014-07-22 22:44:43 +02004201 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004202 CharSequence oldBuilderContentTitle =
4203 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004204 if (mBigContentTitle != null) {
4205 mBuilder.setContentTitle(mBigContentTitle);
4206 }
4207
Chris Wrend6297db2012-05-03 16:20:13 -04004208 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4209
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004210 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004211
Chris Wrend6297db2012-05-03 16:20:13 -04004212 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4213 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004214 } else {
4215 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004216 }
4217
Chris Wrend6297db2012-05-03 16:20:13 -04004218 return contentView;
4219 }
4220
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004221 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004222 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4223 * The default implementation has nothing additional to add.
4224 * @hide
4225 */
4226 public RemoteViews makeContentView() {
4227 return null;
4228 }
4229
4230 /**
4231 * Construct a Style-specific RemoteViews for the final big notification layout.
4232 * @hide
4233 */
4234 public RemoteViews makeBigContentView() {
4235 return null;
4236 }
4237
4238 /**
4239 * Construct a Style-specific RemoteViews for the final HUN layout.
4240 * @hide
4241 */
4242 public RemoteViews makeHeadsUpContentView() {
4243 return null;
4244 }
4245
4246 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004247 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004248 * @hide
4249 */
4250 public void addExtras(Bundle extras) {
4251 if (mSummaryTextSet) {
4252 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4253 }
4254 if (mBigContentTitle != null) {
4255 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4256 }
Chris Wren91ad5632013-06-05 15:05:57 -04004257 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004258 }
4259
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004260 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004261 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004262 * @hide
4263 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004264 protected void restoreFromExtras(Bundle extras) {
4265 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4266 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4267 mSummaryTextSet = true;
4268 }
4269 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4270 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4271 }
4272 }
4273
4274
4275 /**
4276 * @hide
4277 */
4278 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004279 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004280 return wip;
4281 }
4282
Daniel Sandler0ec46202015-06-24 01:27:05 -04004283 /**
4284 * @hide
4285 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004286 public void purgeResources() {}
4287
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004288 /**
4289 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4290 * attached to.
4291 *
4292 * @return the fully constructed Notification.
4293 */
4294 public Notification build() {
4295 checkBuilder();
4296 return mBuilder.build();
4297 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004298
4299 /**
4300 * @hide
4301 * @return true if the style positions the progress bar on the second line; false if the
4302 * style hides the progress bar
4303 */
4304 protected boolean hasProgress() {
4305 return true;
4306 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004307
4308 /**
4309 * @hide
4310 * @return Whether we should put the summary be put into the notification header
4311 */
4312 public boolean hasSummaryInHeader() {
4313 return true;
4314 }
Selim Cinek593610c2016-02-16 18:42:57 -08004315
4316 /**
4317 * @hide
4318 * @return Whether custom content views are displayed inline in the style
4319 */
4320 public boolean displayCustomViewInline() {
4321 return false;
4322 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004323 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004324
4325 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004326 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004327 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004328 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004329 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004330 * Notification notif = new Notification.Builder(mContext)
4331 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4332 * .setContentText(subject)
4333 * .setSmallIcon(R.drawable.new_post)
4334 * .setLargeIcon(aBitmap)
4335 * .setStyle(new Notification.BigPictureStyle()
4336 * .bigPicture(aBigBitmap))
4337 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004338 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004339 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004340 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004341 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004342 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004343 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004344 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004345 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004346
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004347 public BigPictureStyle() {
4348 }
4349
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004350 /**
4351 * @deprecated use {@code BigPictureStyle()}.
4352 */
4353 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004354 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004355 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004356 }
4357
Chris Wrend6297db2012-05-03 16:20:13 -04004358 /**
4359 * Overrides ContentTitle in the big form of the template.
4360 * This defaults to the value passed to setContentTitle().
4361 */
4362 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004363 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004364 return this;
4365 }
4366
4367 /**
4368 * Set the first line of text after the detail section in the big form of the template.
4369 */
4370 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004371 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004372 return this;
4373 }
4374
Chris Wren0bd664d2012-08-01 13:56:56 -04004375 /**
4376 * Provide the bitmap to be used as the payload for the BigPicture notification.
4377 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004378 public BigPictureStyle bigPicture(Bitmap b) {
4379 mPicture = b;
4380 return this;
4381 }
4382
Chris Wren3745a3d2012-05-22 15:11:52 -04004383 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004384 * Override the large icon when the big notification is shown.
4385 */
4386 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004387 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4388 }
4389
4390 /**
4391 * Override the large icon when the big notification is shown.
4392 */
4393 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004394 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004395 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004396 return this;
4397 }
4398
Riley Andrews0394a0c2015-11-03 23:36:52 -08004399 /** @hide */
4400 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4401
Daniel Sandler0ec46202015-06-24 01:27:05 -04004402 /**
4403 * @hide
4404 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004405 @Override
4406 public void purgeResources() {
4407 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004408 if (mPicture != null &&
4409 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004410 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004411 mPicture = mPicture.createAshmemBitmap();
4412 }
4413 if (mBigLargeIcon != null) {
4414 mBigLargeIcon.convertToAshmem();
4415 }
4416 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004417
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004418 /**
4419 * @hide
4420 */
4421 public RemoteViews makeBigContentView() {
4422 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004423 // This covers the following cases:
4424 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004425 // mN.mLargeIcon
4426 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004427 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004428 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004429 oldLargeIcon = mBuilder.mN.mLargeIcon;
4430 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004431 }
4432
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004433 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004434 if (mSummaryTextSet) {
4435 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004436 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004437 }
Selim Cinek279fa862016-06-14 10:57:25 -07004438 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004439
Christoph Studer5c510ee2014-12-15 16:32:27 +01004440 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004441 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004442 }
4443
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004444 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004445 return contentView;
4446 }
4447
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004448 /**
4449 * @hide
4450 */
4451 public void addExtras(Bundle extras) {
4452 super.addExtras(extras);
4453
4454 if (mBigLargeIconSet) {
4455 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4456 }
4457 extras.putParcelable(EXTRA_PICTURE, mPicture);
4458 }
4459
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004460 /**
4461 * @hide
4462 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004463 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004464 protected void restoreFromExtras(Bundle extras) {
4465 super.restoreFromExtras(extras);
4466
4467 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004468 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004469 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004470 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004471 mPicture = extras.getParcelable(EXTRA_PICTURE);
4472 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004473
4474 /**
4475 * @hide
4476 */
4477 @Override
4478 public boolean hasSummaryInHeader() {
4479 return false;
4480 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004481 }
4482
4483 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004484 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004485 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004486 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004487 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004488 * Notification notif = new Notification.Builder(mContext)
4489 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4490 * .setContentText(subject)
4491 * .setSmallIcon(R.drawable.new_mail)
4492 * .setLargeIcon(aBitmap)
4493 * .setStyle(new Notification.BigTextStyle()
4494 * .bigText(aVeryLongString))
4495 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004496 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004497 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004498 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004499 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004500 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004501
4502 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004503 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004504
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004505 private CharSequence mBigText;
4506
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004507 public BigTextStyle() {
4508 }
4509
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004510 /**
4511 * @deprecated use {@code BigTextStyle()}.
4512 */
4513 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004514 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004515 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004516 }
4517
Chris Wrend6297db2012-05-03 16:20:13 -04004518 /**
4519 * Overrides ContentTitle in the big form of the template.
4520 * This defaults to the value passed to setContentTitle().
4521 */
4522 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004523 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004524 return this;
4525 }
4526
4527 /**
4528 * Set the first line of text after the detail section in the big form of the template.
4529 */
4530 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004531 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004532 return this;
4533 }
4534
Chris Wren0bd664d2012-08-01 13:56:56 -04004535 /**
4536 * Provide the longer text to be displayed in the big form of the
4537 * template in place of the content text.
4538 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004539 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004540 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004541 return this;
4542 }
4543
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004544 /**
4545 * @hide
4546 */
4547 public void addExtras(Bundle extras) {
4548 super.addExtras(extras);
4549
Christoph Studer4600f9b2014-07-22 22:44:43 +02004550 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4551 }
4552
4553 /**
4554 * @hide
4555 */
4556 @Override
4557 protected void restoreFromExtras(Bundle extras) {
4558 super.restoreFromExtras(extras);
4559
4560 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004561 }
4562
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004563 /**
4564 * @hide
4565 */
4566 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004567
4568 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004569 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004570 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004571
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004572 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004573
Selim Cinek75998782016-04-26 10:39:17 -07004574 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004575
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004576 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004577 if (TextUtils.isEmpty(bigTextText)) {
4578 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4579 // experience
4580 bigTextText = mBuilder.processLegacyText(text);
4581 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004582 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004583
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004584 return contentView;
4585 }
4586
Adrian Roosb1f427c2016-05-26 12:27:15 -07004587 static void applyBigTextContentView(Builder builder,
4588 RemoteViews contentView, CharSequence bigTextText) {
4589 contentView.setTextViewText(R.id.big_text, bigTextText);
4590 contentView.setViewVisibility(R.id.big_text,
4591 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4592 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004593 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004594 }
4595
4596 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004597 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004598 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004599 if (hasActions) {
4600 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4601 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004602 return lineCount;
4603 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004604 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004605
4606 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004607 * Helper class for generating large-format notifications that include multiple back-and-forth
4608 * messages of varying types between any number of people.
4609 *
4610 * <br>
4611 * If the platform does not provide large-format notifications, this method has no effect. The
4612 * user will always see the normal notification view.
4613 * <br>
4614 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4615 * so:
4616 * <pre class="prettyprint">
4617 *
4618 * Notification noti = new Notification.Builder()
4619 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4620 * .setContentText(subject)
4621 * .setSmallIcon(R.drawable.new_message)
4622 * .setLargeIcon(aBitmap)
4623 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4624 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4625 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4626 * .build();
4627 * </pre>
4628 */
4629 public static class MessagingStyle extends Style {
4630
4631 /**
4632 * The maximum number of messages that will be retained in the Notification itself (the
4633 * number displayed is up to the platform).
4634 */
4635 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4636
4637 CharSequence mUserDisplayName;
4638 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004639 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004640
4641 MessagingStyle() {
4642 }
4643
4644 /**
4645 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4646 * posting app reposts the notification with those messages after they've been actually
4647 * sent and in previous messages sent by the user added in
4648 * {@link #addMessage(Notification.MessagingStyle.Message)}
4649 */
4650 public MessagingStyle(CharSequence userDisplayName) {
4651 mUserDisplayName = userDisplayName;
4652 }
4653
4654 /**
4655 * Returns the name to be displayed for any replies sent by the user
4656 */
4657 public CharSequence getUserDisplayName() {
4658 return mUserDisplayName;
4659 }
4660
4661 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004662 * Sets the title to be displayed on this conversation. This should only be used for
4663 * group messaging and left unset for one-on-one conversations.
4664 * @param conversationTitle
4665 * @return this object for method chaining.
4666 */
4667 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4668 mConversationTitle = conversationTitle;
4669 return this;
4670 }
4671
4672 /**
4673 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4674 * should be for one-on-one conversations
4675 */
4676 public CharSequence getConversationTitle() {
4677 return mConversationTitle;
4678 }
4679
4680 /**
4681 * Adds a message for display by this notification. Convenience call for a simple
4682 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4683 * @param text A {@link CharSequence} to be displayed as the message content
4684 * @param timestamp Time at which the message arrived
4685 * @param sender A {@link CharSequence} to be used for displaying the name of the
4686 * sender. Should be <code>null</code> for messages by the current user, in which case
4687 * the platform will insert {@link #getUserDisplayName()}.
4688 * Should be unique amongst all individuals in the conversation, and should be
4689 * consistent during re-posts of the notification.
4690 *
4691 * @see Message#Message(CharSequence, long, CharSequence)
4692 *
4693 * @return this object for method chaining
4694 */
4695 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4696 mMessages.add(new Message(text, timestamp, sender));
4697 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4698 mMessages.remove(0);
4699 }
4700 return this;
4701 }
4702
4703 /**
4704 * Adds a {@link Message} for display in this notification.
4705 * @param message The {@link Message} to be displayed
4706 * @return this object for method chaining
4707 */
4708 public MessagingStyle addMessage(Message message) {
4709 mMessages.add(message);
4710 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4711 mMessages.remove(0);
4712 }
4713 return this;
4714 }
4715
4716 /**
4717 * Gets the list of {@code Message} objects that represent the notification
4718 */
4719 public List<Message> getMessages() {
4720 return mMessages;
4721 }
4722
4723 /**
4724 * @hide
4725 */
4726 @Override
4727 public void addExtras(Bundle extras) {
4728 super.addExtras(extras);
4729 if (mUserDisplayName != null) {
4730 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4731 }
4732 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004733 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004734 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004735 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4736 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004737 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004738
4739 fixTitleAndTextExtras(extras);
4740 }
4741
4742 private void fixTitleAndTextExtras(Bundle extras) {
4743 Message m = findLatestIncomingMessage();
4744 CharSequence text = (m == null) ? null : m.mText;
4745 CharSequence sender = m == null ? null
4746 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4747 CharSequence title;
4748 if (!TextUtils.isEmpty(mConversationTitle)) {
4749 if (!TextUtils.isEmpty(sender)) {
4750 BidiFormatter bidi = BidiFormatter.getInstance();
4751 title = mBuilder.mContext.getString(
4752 com.android.internal.R.string.notification_messaging_title_template,
4753 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4754 } else {
4755 title = mConversationTitle;
4756 }
4757 } else {
4758 title = sender;
4759 }
4760
4761 if (title != null) {
4762 extras.putCharSequence(EXTRA_TITLE, title);
4763 }
4764 if (text != null) {
4765 extras.putCharSequence(EXTRA_TEXT, text);
4766 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004767 }
4768
4769 /**
4770 * @hide
4771 */
4772 @Override
4773 protected void restoreFromExtras(Bundle extras) {
4774 super.restoreFromExtras(extras);
4775
4776 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004777 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4778 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004779 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004780 if (parcelables != null && parcelables instanceof Parcelable[]) {
4781 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004782 }
4783 }
4784
4785 /**
4786 * @hide
4787 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004788 @Override
4789 public RemoteViews makeContentView() {
4790 Message m = findLatestIncomingMessage();
4791 CharSequence title = mConversationTitle != null
4792 ? mConversationTitle
4793 : (m == null) ? null : m.mSender;
4794 CharSequence text = (m == null)
4795 ? null
4796 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004797
Adrian Roosc1a80b02016-04-05 14:54:55 -07004798 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4799 false /* hasProgress */,
4800 title,
4801 text);
4802 }
4803
4804 private Message findLatestIncomingMessage() {
4805 for (int i = mMessages.size() - 1; i >= 0; i--) {
4806 Message m = mMessages.get(i);
4807 // Incoming messages have a non-empty sender.
4808 if (!TextUtils.isEmpty(m.mSender)) {
4809 return m;
4810 }
4811 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004812 if (!mMessages.isEmpty()) {
4813 // No incoming messages, fall back to outgoing message
4814 return mMessages.get(mMessages.size() - 1);
4815 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004816 return null;
4817 }
4818
4819 /**
4820 * @hide
4821 */
4822 @Override
4823 public RemoteViews makeBigContentView() {
4824 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4825 ? super.mBigContentTitle
4826 : mConversationTitle;
4827 boolean hasTitle = !TextUtils.isEmpty(title);
4828
Adrian Roosfeafa052016-06-01 17:09:45 -07004829 if (mMessages.size() == 1) {
4830 // Special case for a single message: Use the big text style
4831 // so the collapsed and expanded versions match nicely.
4832 CharSequence bigTitle;
4833 CharSequence text;
4834 if (hasTitle) {
4835 bigTitle = title;
4836 text = makeMessageLine(mMessages.get(0));
4837 } else {
4838 bigTitle = mMessages.get(0).mSender;
4839 text = mMessages.get(0).mText;
4840 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004841 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4842 mBuilder.getBigTextLayoutResource(),
Adrian Roosfeafa052016-06-01 17:09:45 -07004843 false /* progress */, bigTitle, null /* text */);
Adrian Roosb1f427c2016-05-26 12:27:15 -07004844 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4845 return contentView;
4846 }
4847
Adrian Roos48d746a2016-04-12 14:57:28 -07004848 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004849 mBuilder.getMessagingLayoutResource(),
4850 false /* hasProgress */,
4851 title,
4852 null /* text */);
4853
4854 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4855 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4856
4857 // Make sure all rows are gone in case we reuse a view.
4858 for (int rowId : rowIds) {
4859 contentView.setViewVisibility(rowId, View.GONE);
4860 }
4861
4862 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004863 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
4864 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004865 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07004866 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004867
Adrian Roosfeafa052016-06-01 17:09:45 -07004868 int contractedChildId = View.NO_ID;
4869 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07004870 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4871 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4872 Message m = mMessages.get(firstMessage + i);
4873 int rowId = rowIds[i];
4874
4875 contentView.setViewVisibility(rowId, View.VISIBLE);
4876 contentView.setTextViewText(rowId, makeMessageLine(m));
4877
Adrian Roosfeafa052016-06-01 17:09:45 -07004878 if (contractedMessage == m) {
4879 contractedChildId = rowId;
4880 }
4881
Adrian Roosc1a80b02016-04-05 14:54:55 -07004882 i++;
4883 }
Adrian Roosfeafa052016-06-01 17:09:45 -07004884 // Record this here to allow transformation between the contracted and expanded views.
4885 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
4886 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04004887 return contentView;
4888 }
4889
Adrian Roosc1a80b02016-04-05 14:54:55 -07004890 private CharSequence makeMessageLine(Message m) {
4891 BidiFormatter bidi = BidiFormatter.getInstance();
4892 SpannableStringBuilder sb = new SpannableStringBuilder();
4893 if (TextUtils.isEmpty(m.mSender)) {
4894 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4895 sb.append(bidi.unicodeWrap(replyName),
4896 makeFontColorSpan(mBuilder.resolveContrastColor()),
4897 0 /* flags */);
4898 } else {
4899 sb.append(bidi.unicodeWrap(m.mSender),
4900 makeFontColorSpan(Color.BLACK),
4901 0 /* flags */);
4902 }
4903 CharSequence text = m.mText == null ? "" : m.mText;
4904 sb.append(" ").append(bidi.unicodeWrap(text));
4905 return sb;
4906 }
4907
Adrian Roosdedd1df2016-04-26 16:38:47 -07004908 /**
4909 * @hide
4910 */
4911 @Override
4912 public RemoteViews makeHeadsUpContentView() {
4913 Message m = findLatestIncomingMessage();
4914 CharSequence title = mConversationTitle != null
4915 ? mConversationTitle
4916 : (m == null) ? null : m.mSender;
4917 CharSequence text = (m == null)
4918 ? null
4919 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4920
4921 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4922 false /* hasProgress */,
4923 title,
4924 text);
4925 }
4926
Adrian Roosc1a80b02016-04-05 14:54:55 -07004927 private static TextAppearanceSpan makeFontColorSpan(int color) {
4928 return new TextAppearanceSpan(null, 0, 0,
4929 ColorStateList.valueOf(color), null);
4930 }
4931
Alex Hillsd9b04d92016-04-11 16:38:16 -04004932 public static final class Message {
4933
4934 static final String KEY_TEXT = "text";
4935 static final String KEY_TIMESTAMP = "time";
4936 static final String KEY_SENDER = "sender";
4937 static final String KEY_DATA_MIME_TYPE = "type";
4938 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004939
4940 private final CharSequence mText;
4941 private final long mTimestamp;
4942 private final CharSequence mSender;
4943
4944 private String mDataMimeType;
4945 private Uri mDataUri;
4946
4947 /**
4948 * Constructor
4949 * @param text A {@link CharSequence} to be displayed as the message content
4950 * @param timestamp Time at which the message arrived
4951 * @param sender A {@link CharSequence} to be used for displaying the name of the
4952 * sender. Should be <code>null</code> for messages by the current user, in which case
4953 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4954 * Should be unique amongst all individuals in the conversation, and should be
4955 * consistent during re-posts of the notification.
4956 */
4957 public Message(CharSequence text, long timestamp, CharSequence sender){
4958 mText = text;
4959 mTimestamp = timestamp;
4960 mSender = sender;
4961 }
4962
4963 /**
4964 * Sets a binary blob of data and an associated MIME type for a message. In the case
4965 * where the platform doesn't support the MIME type, the original text provided in the
4966 * constructor will be used.
4967 * @param dataMimeType The MIME type of the content. See
4968 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4969 * types on Android and Android Wear.
4970 * @param dataUri The uri containing the content whose type is given by the MIME type.
4971 * <p class="note">
4972 * <ol>
4973 * <li>Notification Listeners including the System UI need permission to access the
4974 * data the Uri points to. The recommended ways to do this are:</li>
4975 * <li>Store the data in your own ContentProvider, making sure that other apps have
4976 * the correct permission to access your provider. The preferred mechanism for
4977 * providing access is to use per-URI permissions which are temporary and only
4978 * grant access to the receiving application. An easy way to create a
4979 * ContentProvider like this is to use the FileProvider helper class.</li>
4980 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4981 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4982 * also store non-media types (see MediaStore.Files for more info). Files can be
4983 * inserted into the MediaStore using scanFile() after which a content:// style
4984 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4985 * Note that once added to the system MediaStore the content is accessible to any
4986 * app on the device.</li>
4987 * </ol>
4988 * @return this object for method chaining
4989 */
4990 public Message setData(String dataMimeType, Uri dataUri) {
4991 mDataMimeType = dataMimeType;
4992 mDataUri = dataUri;
4993 return this;
4994 }
4995
Alex Hillsfc737de2016-03-23 17:33:02 -04004996 /**
4997 * Get the text to be used for this message, or the fallback text if a type and content
4998 * Uri have been set
4999 */
5000 public CharSequence getText() {
5001 return mText;
5002 }
5003
5004 /**
5005 * Get the time at which this message arrived
5006 */
5007 public long getTimestamp() {
5008 return mTimestamp;
5009 }
5010
5011 /**
5012 * Get the text used to display the contact's name in the messaging experience
5013 */
5014 public CharSequence getSender() {
5015 return mSender;
5016 }
5017
5018 /**
5019 * Get the MIME type of the data pointed to by the Uri
5020 */
5021 public String getDataMimeType() {
5022 return mDataMimeType;
5023 }
5024
5025 /**
5026 * Get the the Uri pointing to the content of the message. Can be null, in which case
5027 * {@see #getText()} is used.
5028 */
5029 public Uri getDataUri() {
5030 return mDataUri;
5031 }
5032
Alex Hillsd9b04d92016-04-11 16:38:16 -04005033 private Bundle toBundle() {
5034 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005035 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005036 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005037 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005038 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005039 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005040 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005041 }
5042 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005043 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005044 }
5045 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005046 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005047 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005048 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005049 }
5050
Alex Hillsd9b04d92016-04-11 16:38:16 -04005051 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5052 Bundle[] bundles = new Bundle[messages.size()];
5053 final int N = messages.size();
5054 for (int i = 0; i < N; i++) {
5055 bundles[i] = messages.get(i).toBundle();
5056 }
5057 return bundles;
5058 }
5059
Adrian Roosdedd1df2016-04-26 16:38:47 -07005060 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005061 List<Message> messages = new ArrayList<>(bundles.length);
5062 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005063 if (bundles[i] instanceof Bundle) {
5064 Message message = getMessageFromBundle((Bundle)bundles[i]);
5065 if (message != null) {
5066 messages.add(message);
5067 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005068 }
5069 }
5070 return messages;
5071 }
5072
5073 static Message getMessageFromBundle(Bundle bundle) {
5074 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005075 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005076 return null;
5077 } else {
5078 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5079 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5080 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5081 bundle.containsKey(KEY_DATA_URI)) {
5082
5083 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5084 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005085 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005086 return message;
5087 }
5088 } catch (ClassCastException e) {
5089 return null;
5090 }
5091 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005092 }
5093 }
5094
5095 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005096 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005097 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005098 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005099 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005100 * Notification notif = new Notification.Builder(mContext)
5101 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5102 * .setContentText(subject)
5103 * .setSmallIcon(R.drawable.new_mail)
5104 * .setLargeIcon(aBitmap)
5105 * .setStyle(new Notification.InboxStyle()
5106 * .addLine(str1)
5107 * .addLine(str2)
5108 * .setContentTitle(&quot;&quot;)
5109 * .setSummaryText(&quot;+3 more&quot;))
5110 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005111 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005112 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005113 * @see Notification#bigContentView
5114 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005115 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005116 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5117
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005118 public InboxStyle() {
5119 }
5120
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005121 /**
5122 * @deprecated use {@code InboxStyle()}.
5123 */
5124 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005125 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005126 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005127 }
5128
Chris Wrend6297db2012-05-03 16:20:13 -04005129 /**
5130 * Overrides ContentTitle in the big form of the template.
5131 * This defaults to the value passed to setContentTitle().
5132 */
5133 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005134 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005135 return this;
5136 }
5137
5138 /**
5139 * Set the first line of text after the detail section in the big form of the template.
5140 */
5141 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005142 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005143 return this;
5144 }
5145
Chris Wren0bd664d2012-08-01 13:56:56 -04005146 /**
5147 * Append a line to the digest section of the Inbox notification.
5148 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005149 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005150 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005151 return this;
5152 }
5153
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005154 /**
5155 * @hide
5156 */
5157 public void addExtras(Bundle extras) {
5158 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005159
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005160 CharSequence[] a = new CharSequence[mTexts.size()];
5161 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5162 }
5163
Christoph Studer4600f9b2014-07-22 22:44:43 +02005164 /**
5165 * @hide
5166 */
5167 @Override
5168 protected void restoreFromExtras(Bundle extras) {
5169 super.restoreFromExtras(extras);
5170
5171 mTexts.clear();
5172 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5173 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5174 }
5175 }
5176
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005177 /**
5178 * @hide
5179 */
5180 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005181 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005182 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005183 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5184 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005185
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005186 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005187
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005188 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005189
Chris Wrend6297db2012-05-03 16:20:13 -04005190 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 -04005191 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005192
Chris Wren4ed80d52012-05-17 09:30:03 -04005193 // Make sure all rows are gone in case we reuse a view.
5194 for (int rowId : rowIds) {
5195 contentView.setViewVisibility(rowId, View.GONE);
5196 }
5197
Daniel Sandler879c5e02012-04-17 16:46:51 -04005198 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005199 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5200 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005201 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005202 int onlyViewId = 0;
5203 int maxRows = rowIds.length;
5204 if (mBuilder.mActions.size() > 0) {
5205 maxRows--;
5206 }
5207 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005208 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005209 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005210 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005211 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005212 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005213 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005214 if (first) {
5215 onlyViewId = rowIds[i];
5216 } else {
5217 onlyViewId = 0;
5218 }
Selim Cinek247fa012016-02-18 09:50:48 -08005219 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005220 }
5221 i++;
5222 }
Selim Cinek07c80172016-04-21 16:40:47 -07005223 if (onlyViewId != 0) {
5224 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5225 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5226 R.dimen.notification_text_margin_top);
5227 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5228 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005229
Daniel Sandler879c5e02012-04-17 16:46:51 -04005230 return contentView;
5231 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005232
Selim Cinek247fa012016-02-18 09:50:48 -08005233 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005234 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005235 if (first) {
5236 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5237 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5238 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005239 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005240 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005241 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005242 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005243 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005244 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005245 }
Dan Sandler842dd772014-05-15 09:36:47 -04005246
5247 /**
5248 * Notification style for media playback notifications.
5249 *
5250 * In the expanded form, {@link Notification#bigContentView}, up to 5
5251 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005252 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005253 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5254 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5255 * treated as album artwork.
5256 *
5257 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5258 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005259 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005260 * in the standard view alongside the usual content.
5261 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005262 * Notifications created with MediaStyle will have their category set to
5263 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5264 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5265 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005266 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5267 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005268 * the System UI can identify this as a notification representing an active media session
5269 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5270 *
5271 * To use this style with your Notification, feed it to
5272 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5273 * <pre class="prettyprint">
5274 * Notification noti = new Notification.Builder()
5275 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005276 * .setContentTitle(&quot;Track title&quot;)
5277 * .setContentText(&quot;Artist - Album&quot;)
5278 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005279 * .setStyle(<b>new Notification.MediaStyle()</b>
5280 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005281 * .build();
5282 * </pre>
5283 *
5284 * @see Notification#bigContentView
5285 */
5286 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005287 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005288 static final int MAX_MEDIA_BUTTONS = 5;
5289
5290 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005291 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005292
5293 public MediaStyle() {
5294 }
5295
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005296 /**
5297 * @deprecated use {@code MediaStyle()}.
5298 */
5299 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005300 public MediaStyle(Builder builder) {
5301 setBuilder(builder);
5302 }
5303
5304 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005305 * 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 -04005306 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005307 *
5308 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005309 */
5310 public MediaStyle setShowActionsInCompactView(int...actions) {
5311 mActionsToShowInCompact = actions;
5312 return this;
5313 }
5314
5315 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005316 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5317 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005318 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005319 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005320 mToken = token;
5321 return this;
5322 }
5323
Christoph Studer4600f9b2014-07-22 22:44:43 +02005324 /**
5325 * @hide
5326 */
Dan Sandler842dd772014-05-15 09:36:47 -04005327 @Override
5328 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005329 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005330 if (wip.category == null) {
5331 wip.category = Notification.CATEGORY_TRANSPORT;
5332 }
Dan Sandler842dd772014-05-15 09:36:47 -04005333 return wip;
5334 }
5335
Christoph Studer4600f9b2014-07-22 22:44:43 +02005336 /**
5337 * @hide
5338 */
5339 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005340 public RemoteViews makeContentView() {
5341 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005342 }
5343
5344 /**
5345 * @hide
5346 */
5347 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005348 public RemoteViews makeBigContentView() {
5349 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005350 }
5351
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005352 /**
5353 * @hide
5354 */
5355 @Override
5356 public RemoteViews makeHeadsUpContentView() {
5357 RemoteViews expanded = makeMediaBigContentView();
5358 return expanded != null ? expanded : makeMediaContentView();
5359 }
5360
Dan Sandler842dd772014-05-15 09:36:47 -04005361 /** @hide */
5362 @Override
5363 public void addExtras(Bundle extras) {
5364 super.addExtras(extras);
5365
5366 if (mToken != null) {
5367 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5368 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005369 if (mActionsToShowInCompact != null) {
5370 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5371 }
Dan Sandler842dd772014-05-15 09:36:47 -04005372 }
5373
Christoph Studer4600f9b2014-07-22 22:44:43 +02005374 /**
5375 * @hide
5376 */
5377 @Override
5378 protected void restoreFromExtras(Bundle extras) {
5379 super.restoreFromExtras(extras);
5380
5381 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5382 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5383 }
5384 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5385 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5386 }
5387 }
5388
Selim Cinek5bf069a2015-11-10 19:14:27 -05005389 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005390 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005391 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005392 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005393 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005394 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5395 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005396 if (!tombstone) {
5397 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5398 }
5399 button.setContentDescription(R.id.action0, action.title);
5400 return button;
5401 }
5402
5403 private RemoteViews makeMediaContentView() {
5404 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005405 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005406
5407 final int numActions = mBuilder.mActions.size();
5408 final int N = mActionsToShowInCompact == null
5409 ? 0
5410 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5411 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005412 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005413 for (int i = 0; i < N; i++) {
5414 if (i >= numActions) {
5415 throw new IllegalArgumentException(String.format(
5416 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5417 i, numActions - 1));
5418 }
5419
5420 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005421 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005422 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005423 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005424 }
5425 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005426 handleImage(view);
5427 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005428 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005429 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005430 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005431 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005432 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005433 return view;
5434 }
5435
5436 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005437 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005438 // Dont add an expanded view if there is no more content to be revealed
5439 int actionsInCompact = mActionsToShowInCompact == null
5440 ? 0
5441 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005442 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005443 return null;
5444 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005445 RemoteViews big = mBuilder.applyStandardTemplate(
5446 R.layout.notification_template_material_big_media,
5447 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005448
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005449 if (actionCount > 0) {
5450 big.removeAllViews(com.android.internal.R.id.media_actions);
5451 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005452 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005453 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005454 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005455 }
5456 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005457 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005458 return big;
5459 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005460
Selim Cinek5bf069a2015-11-10 19:14:27 -05005461 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005462 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005463 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5464 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005465 }
5466 }
5467
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005468 /**
5469 * @hide
5470 */
5471 @Override
5472 protected boolean hasProgress() {
5473 return false;
5474 }
Dan Sandler842dd772014-05-15 09:36:47 -04005475 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005476
Selim Cinek593610c2016-02-16 18:42:57 -08005477 /**
5478 * Notification style for custom views that are decorated by the system
5479 *
5480 * <p>Instead of providing a notification that is completely custom, a developer can set this
5481 * style and still obtain system decorations like the notification header with the expand
5482 * affordance and actions.
5483 *
5484 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5485 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5486 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5487 * corresponding custom views to display.
5488 *
5489 * To use this style with your Notification, feed it to
5490 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5491 * <pre class="prettyprint">
5492 * Notification noti = new Notification.Builder()
5493 * .setSmallIcon(R.drawable.ic_stat_player)
5494 * .setLargeIcon(albumArtBitmap))
5495 * .setCustomContentView(contentView);
5496 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5497 * .build();
5498 * </pre>
5499 */
5500 public static class DecoratedCustomViewStyle extends Style {
5501
5502 public DecoratedCustomViewStyle() {
5503 }
5504
Selim Cinek593610c2016-02-16 18:42:57 -08005505 /**
5506 * @hide
5507 */
5508 public boolean displayCustomViewInline() {
5509 return true;
5510 }
5511
5512 /**
5513 * @hide
5514 */
5515 @Override
5516 public RemoteViews makeContentView() {
5517 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5518 }
5519
5520 /**
5521 * @hide
5522 */
5523 @Override
5524 public RemoteViews makeBigContentView() {
5525 return makeDecoratedBigContentView();
5526 }
5527
5528 /**
5529 * @hide
5530 */
5531 @Override
5532 public RemoteViews makeHeadsUpContentView() {
5533 return makeDecoratedHeadsUpContentView();
5534 }
5535
Selim Cinek593610c2016-02-16 18:42:57 -08005536 private RemoteViews makeDecoratedHeadsUpContentView() {
5537 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5538 ? mBuilder.mN.contentView
5539 : mBuilder.mN.headsUpContentView;
5540 if (mBuilder.mActions.size() == 0) {
5541 return makeStandardTemplateWithCustomContent(headsUpContentView);
5542 }
5543 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5544 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005545 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005546 return remoteViews;
5547 }
5548
Selim Cinek593610c2016-02-16 18:42:57 -08005549 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5550 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5551 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005552 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005553 return remoteViews;
5554 }
5555
Selim Cinek593610c2016-02-16 18:42:57 -08005556 private RemoteViews makeDecoratedBigContentView() {
5557 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5558 ? mBuilder.mN.contentView
5559 : mBuilder.mN.bigContentView;
5560 if (mBuilder.mActions.size() == 0) {
5561 return makeStandardTemplateWithCustomContent(bigContentView);
5562 }
5563 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5564 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005565 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005566 return remoteViews;
5567 }
Selim Cinek247fa012016-02-18 09:50:48 -08005568
5569 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5570 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005571 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005572 // Need to clone customContent before adding, because otherwise it can no longer be
5573 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005574 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005575 remoteViews.removeAllViews(R.id.notification_main_column);
5576 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005577 }
Selim Cinek247fa012016-02-18 09:50:48 -08005578 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005579 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005580 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005581 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005582 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005583 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005584 }
Selim Cinek593610c2016-02-16 18:42:57 -08005585 }
5586
Selim Cinek03eb3b72016-02-18 10:39:45 -08005587 /**
5588 * Notification style for media custom views that are decorated by the system
5589 *
5590 * <p>Instead of providing a media notification that is completely custom, a developer can set
5591 * this style and still obtain system decorations like the notification header with the expand
5592 * affordance and actions.
5593 *
5594 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5595 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5596 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5597 * corresponding custom views to display.
5598 *
5599 * To use this style with your Notification, feed it to
5600 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5601 * <pre class="prettyprint">
5602 * Notification noti = new Notification.Builder()
5603 * .setSmallIcon(R.drawable.ic_stat_player)
5604 * .setLargeIcon(albumArtBitmap))
5605 * .setCustomContentView(contentView);
5606 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5607 * .setMediaSession(mySession))
5608 * .build();
5609 * </pre>
5610 *
5611 * @see android.app.Notification.DecoratedCustomViewStyle
5612 * @see android.app.Notification.MediaStyle
5613 */
5614 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5615
5616 public DecoratedMediaCustomViewStyle() {
5617 }
5618
Selim Cinek03eb3b72016-02-18 10:39:45 -08005619 /**
5620 * @hide
5621 */
5622 public boolean displayCustomViewInline() {
5623 return true;
5624 }
5625
5626 /**
5627 * @hide
5628 */
5629 @Override
5630 public RemoteViews makeContentView() {
5631 RemoteViews remoteViews = super.makeContentView();
5632 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5633 mBuilder.mN.contentView);
5634 }
5635
5636 /**
5637 * @hide
5638 */
5639 @Override
5640 public RemoteViews makeBigContentView() {
5641 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5642 ? mBuilder.mN.bigContentView
5643 : mBuilder.mN.contentView;
5644 return makeBigContentViewWithCustomContent(customRemoteView);
5645 }
5646
5647 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5648 RemoteViews remoteViews = super.makeBigContentView();
5649 if (remoteViews != null) {
5650 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5651 customRemoteView);
5652 } else if (customRemoteView != mBuilder.mN.contentView){
5653 remoteViews = super.makeContentView();
5654 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5655 customRemoteView);
5656 } else {
5657 return null;
5658 }
5659 }
5660
5661 /**
5662 * @hide
5663 */
5664 @Override
5665 public RemoteViews makeHeadsUpContentView() {
5666 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5667 ? mBuilder.mN.headsUpContentView
5668 : mBuilder.mN.contentView;
5669 return makeBigContentViewWithCustomContent(customRemoteView);
5670 }
5671
5672 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5673 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005674 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005675 // Need to clone customContent before adding, because otherwise it can no longer be
5676 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005677 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005678 remoteViews.removeAllViews(id);
5679 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005680 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005681 return remoteViews;
5682 }
5683 }
5684
Christoph Studer4600f9b2014-07-22 22:44:43 +02005685 // When adding a new Style subclass here, don't forget to update
5686 // Builder.getNotificationStyleClass.
5687
Griff Hazen61a9e862014-05-22 16:05:19 -07005688 /**
5689 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5690 * metadata or change options on a notification builder.
5691 */
5692 public interface Extender {
5693 /**
5694 * Apply this extender to a notification builder.
5695 * @param builder the builder to be modified.
5696 * @return the build object for chaining.
5697 */
5698 public Builder extend(Builder builder);
5699 }
5700
5701 /**
5702 * Helper class to add wearable extensions to notifications.
5703 * <p class="note"> See
5704 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5705 * for Android Wear</a> for more information on how to use this class.
5706 * <p>
5707 * To create a notification with wearable extensions:
5708 * <ol>
5709 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5710 * properties.
5711 * <li>Create a {@link android.app.Notification.WearableExtender}.
5712 * <li>Set wearable-specific properties using the
5713 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5714 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5715 * notification.
5716 * <li>Post the notification to the notification system with the
5717 * {@code NotificationManager.notify(...)} methods.
5718 * </ol>
5719 *
5720 * <pre class="prettyprint">
5721 * Notification notif = new Notification.Builder(mContext)
5722 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5723 * .setContentText(subject)
5724 * .setSmallIcon(R.drawable.new_mail)
5725 * .extend(new Notification.WearableExtender()
5726 * .setContentIcon(R.drawable.new_mail))
5727 * .build();
5728 * NotificationManager notificationManger =
5729 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5730 * notificationManger.notify(0, notif);</pre>
5731 *
5732 * <p>Wearable extensions can be accessed on an existing notification by using the
5733 * {@code WearableExtender(Notification)} constructor,
5734 * and then using the {@code get} methods to access values.
5735 *
5736 * <pre class="prettyprint">
5737 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5738 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005739 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005740 */
5741 public static final class WearableExtender implements Extender {
5742 /**
5743 * Sentinel value for an action index that is unset.
5744 */
5745 public static final int UNSET_ACTION_INDEX = -1;
5746
5747 /**
5748 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5749 * default sizing.
5750 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005751 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005752 * on their content.
5753 */
5754 public static final int SIZE_DEFAULT = 0;
5755
5756 /**
5757 * Size value for use with {@link #setCustomSizePreset} to show this notification
5758 * with an extra small size.
5759 * <p>This value is only applicable for custom display notifications created using
5760 * {@link #setDisplayIntent}.
5761 */
5762 public static final int SIZE_XSMALL = 1;
5763
5764 /**
5765 * Size value for use with {@link #setCustomSizePreset} to show this notification
5766 * with a small size.
5767 * <p>This value is only applicable for custom display notifications created using
5768 * {@link #setDisplayIntent}.
5769 */
5770 public static final int SIZE_SMALL = 2;
5771
5772 /**
5773 * Size value for use with {@link #setCustomSizePreset} to show this notification
5774 * with a medium size.
5775 * <p>This value is only applicable for custom display notifications created using
5776 * {@link #setDisplayIntent}.
5777 */
5778 public static final int SIZE_MEDIUM = 3;
5779
5780 /**
5781 * Size value for use with {@link #setCustomSizePreset} to show this notification
5782 * with a large size.
5783 * <p>This value is only applicable for custom display notifications created using
5784 * {@link #setDisplayIntent}.
5785 */
5786 public static final int SIZE_LARGE = 4;
5787
Griff Hazend5f11f92014-05-27 15:40:09 -07005788 /**
5789 * Size value for use with {@link #setCustomSizePreset} to show this notification
5790 * full screen.
5791 * <p>This value is only applicable for custom display notifications created using
5792 * {@link #setDisplayIntent}.
5793 */
5794 public static final int SIZE_FULL_SCREEN = 5;
5795
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005796 /**
5797 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5798 * short amount of time when this notification is displayed on the screen. This
5799 * is the default value.
5800 */
5801 public static final int SCREEN_TIMEOUT_SHORT = 0;
5802
5803 /**
5804 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5805 * for a longer amount of time when this notification is displayed on the screen.
5806 */
5807 public static final int SCREEN_TIMEOUT_LONG = -1;
5808
Griff Hazen61a9e862014-05-22 16:05:19 -07005809 /** Notification extra which contains wearable extensions */
5810 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5811
Pete Gastaf6781d2014-10-07 15:17:05 -04005812 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005813 private static final String KEY_ACTIONS = "actions";
5814 private static final String KEY_FLAGS = "flags";
5815 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5816 private static final String KEY_PAGES = "pages";
5817 private static final String KEY_BACKGROUND = "background";
5818 private static final String KEY_CONTENT_ICON = "contentIcon";
5819 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5820 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5821 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5822 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5823 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005824 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005825 private static final String KEY_DISMISSAL_ID = "dismissalId";
Griff Hazen61a9e862014-05-22 16:05:19 -07005826
5827 // Flags bitwise-ored to mFlags
5828 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5829 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5830 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5831 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005832 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005833 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005834 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005835
5836 // Default value for flags integer
5837 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5838
5839 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5840 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5841
5842 private ArrayList<Action> mActions = new ArrayList<Action>();
5843 private int mFlags = DEFAULT_FLAGS;
5844 private PendingIntent mDisplayIntent;
5845 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5846 private Bitmap mBackground;
5847 private int mContentIcon;
5848 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5849 private int mContentActionIndex = UNSET_ACTION_INDEX;
5850 private int mCustomSizePreset = SIZE_DEFAULT;
5851 private int mCustomContentHeight;
5852 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005853 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005854 private String mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005855
5856 /**
5857 * Create a {@link android.app.Notification.WearableExtender} with default
5858 * options.
5859 */
5860 public WearableExtender() {
5861 }
5862
5863 public WearableExtender(Notification notif) {
5864 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5865 if (wearableBundle != null) {
5866 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5867 if (actions != null) {
5868 mActions.addAll(actions);
5869 }
5870
5871 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5872 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5873
5874 Notification[] pages = getNotificationArrayFromBundle(
5875 wearableBundle, KEY_PAGES);
5876 if (pages != null) {
5877 Collections.addAll(mPages, pages);
5878 }
5879
5880 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5881 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5882 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5883 DEFAULT_CONTENT_ICON_GRAVITY);
5884 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5885 UNSET_ACTION_INDEX);
5886 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5887 SIZE_DEFAULT);
5888 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5889 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005890 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005891 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Griff Hazen61a9e862014-05-22 16:05:19 -07005892 }
5893 }
5894
5895 /**
5896 * Apply wearable extensions to a notification that is being built. This is typically
5897 * called by the {@link android.app.Notification.Builder#extend} method of
5898 * {@link android.app.Notification.Builder}.
5899 */
5900 @Override
5901 public Notification.Builder extend(Notification.Builder builder) {
5902 Bundle wearableBundle = new Bundle();
5903
5904 if (!mActions.isEmpty()) {
5905 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5906 }
5907 if (mFlags != DEFAULT_FLAGS) {
5908 wearableBundle.putInt(KEY_FLAGS, mFlags);
5909 }
5910 if (mDisplayIntent != null) {
5911 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5912 }
5913 if (!mPages.isEmpty()) {
5914 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5915 new Notification[mPages.size()]));
5916 }
5917 if (mBackground != null) {
5918 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5919 }
5920 if (mContentIcon != 0) {
5921 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5922 }
5923 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5924 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5925 }
5926 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5927 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5928 mContentActionIndex);
5929 }
5930 if (mCustomSizePreset != SIZE_DEFAULT) {
5931 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5932 }
5933 if (mCustomContentHeight != 0) {
5934 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5935 }
5936 if (mGravity != DEFAULT_GRAVITY) {
5937 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5938 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005939 if (mHintScreenTimeout != 0) {
5940 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5941 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005942 if (mDismissalId != null) {
5943 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5944 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005945
5946 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5947 return builder;
5948 }
5949
5950 @Override
5951 public WearableExtender clone() {
5952 WearableExtender that = new WearableExtender();
5953 that.mActions = new ArrayList<Action>(this.mActions);
5954 that.mFlags = this.mFlags;
5955 that.mDisplayIntent = this.mDisplayIntent;
5956 that.mPages = new ArrayList<Notification>(this.mPages);
5957 that.mBackground = this.mBackground;
5958 that.mContentIcon = this.mContentIcon;
5959 that.mContentIconGravity = this.mContentIconGravity;
5960 that.mContentActionIndex = this.mContentActionIndex;
5961 that.mCustomSizePreset = this.mCustomSizePreset;
5962 that.mCustomContentHeight = this.mCustomContentHeight;
5963 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005964 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005965 that.mDismissalId = this.mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005966 return that;
5967 }
5968
5969 /**
5970 * Add a wearable action to this notification.
5971 *
5972 * <p>When wearable actions are added using this method, the set of actions that
5973 * show on a wearable device splits from devices that only show actions added
5974 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5975 * of which actions display on different devices.
5976 *
5977 * @param action the action to add to this notification
5978 * @return this object for method chaining
5979 * @see android.app.Notification.Action
5980 */
5981 public WearableExtender addAction(Action action) {
5982 mActions.add(action);
5983 return this;
5984 }
5985
5986 /**
5987 * Adds wearable actions 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 actions the actions to add to this notification
5995 * @return this object for method chaining
5996 * @see android.app.Notification.Action
5997 */
5998 public WearableExtender addActions(List<Action> actions) {
5999 mActions.addAll(actions);
6000 return this;
6001 }
6002
6003 /**
6004 * Clear all wearable actions present on this builder.
6005 * @return this object for method chaining.
6006 * @see #addAction
6007 */
6008 public WearableExtender clearActions() {
6009 mActions.clear();
6010 return this;
6011 }
6012
6013 /**
6014 * Get the wearable actions present on this notification.
6015 */
6016 public List<Action> getActions() {
6017 return mActions;
6018 }
6019
6020 /**
6021 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006022 * this notification. The {@link PendingIntent} provided should be for an activity.
6023 *
6024 * <pre class="prettyprint">
6025 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6026 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6027 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6028 * Notification notif = new Notification.Builder(context)
6029 * .extend(new Notification.WearableExtender()
6030 * .setDisplayIntent(displayPendingIntent)
6031 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6032 * .build();</pre>
6033 *
6034 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006035 * should have an empty task affinity. It is also recommended to use the device
6036 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006037 *
6038 * <p>Example AndroidManifest.xml entry:
6039 * <pre class="prettyprint">
6040 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6041 * android:exported=&quot;true&quot;
6042 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006043 * android:taskAffinity=&quot;&quot;
6044 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006045 *
6046 * @param intent the {@link PendingIntent} for an activity
6047 * @return this object for method chaining
6048 * @see android.app.Notification.WearableExtender#getDisplayIntent
6049 */
6050 public WearableExtender setDisplayIntent(PendingIntent intent) {
6051 mDisplayIntent = intent;
6052 return this;
6053 }
6054
6055 /**
6056 * Get the intent to launch inside of an activity view when displaying this
6057 * notification. This {@code PendingIntent} should be for an activity.
6058 */
6059 public PendingIntent getDisplayIntent() {
6060 return mDisplayIntent;
6061 }
6062
6063 /**
6064 * Add an additional page of content to display with this notification. The current
6065 * notification forms the first page, and pages added using this function form
6066 * subsequent pages. This field can be used to separate a notification into multiple
6067 * sections.
6068 *
6069 * @param page the notification to add as another page
6070 * @return this object for method chaining
6071 * @see android.app.Notification.WearableExtender#getPages
6072 */
6073 public WearableExtender addPage(Notification page) {
6074 mPages.add(page);
6075 return this;
6076 }
6077
6078 /**
6079 * Add additional pages of content to display with this notification. The current
6080 * notification forms the first page, and pages added using this function form
6081 * subsequent pages. This field can be used to separate a notification into multiple
6082 * sections.
6083 *
6084 * @param pages a list of notifications
6085 * @return this object for method chaining
6086 * @see android.app.Notification.WearableExtender#getPages
6087 */
6088 public WearableExtender addPages(List<Notification> pages) {
6089 mPages.addAll(pages);
6090 return this;
6091 }
6092
6093 /**
6094 * Clear all additional pages present on this builder.
6095 * @return this object for method chaining.
6096 * @see #addPage
6097 */
6098 public WearableExtender clearPages() {
6099 mPages.clear();
6100 return this;
6101 }
6102
6103 /**
6104 * Get the array of additional pages of content for displaying this notification. The
6105 * current notification forms the first page, and elements within this array form
6106 * subsequent pages. This field can be used to separate a notification into multiple
6107 * sections.
6108 * @return the pages for this notification
6109 */
6110 public List<Notification> getPages() {
6111 return mPages;
6112 }
6113
6114 /**
6115 * Set a background image to be displayed behind the notification content.
6116 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6117 * will work with any notification style.
6118 *
6119 * @param background the background bitmap
6120 * @return this object for method chaining
6121 * @see android.app.Notification.WearableExtender#getBackground
6122 */
6123 public WearableExtender setBackground(Bitmap background) {
6124 mBackground = background;
6125 return this;
6126 }
6127
6128 /**
6129 * Get a background image to be displayed behind the notification content.
6130 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6131 * will work with any notification style.
6132 *
6133 * @return the background image
6134 * @see android.app.Notification.WearableExtender#setBackground
6135 */
6136 public Bitmap getBackground() {
6137 return mBackground;
6138 }
6139
6140 /**
6141 * Set an icon that goes with the content of this notification.
6142 */
6143 public WearableExtender setContentIcon(int icon) {
6144 mContentIcon = icon;
6145 return this;
6146 }
6147
6148 /**
6149 * Get an icon that goes with the content of this notification.
6150 */
6151 public int getContentIcon() {
6152 return mContentIcon;
6153 }
6154
6155 /**
6156 * Set the gravity that the content icon should have within the notification display.
6157 * Supported values include {@link android.view.Gravity#START} and
6158 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6159 * @see #setContentIcon
6160 */
6161 public WearableExtender setContentIconGravity(int contentIconGravity) {
6162 mContentIconGravity = contentIconGravity;
6163 return this;
6164 }
6165
6166 /**
6167 * Get the gravity that the content icon should have within the notification display.
6168 * Supported values include {@link android.view.Gravity#START} and
6169 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6170 * @see #getContentIcon
6171 */
6172 public int getContentIconGravity() {
6173 return mContentIconGravity;
6174 }
6175
6176 /**
6177 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006178 * this notification. This action will no longer display separately from the
6179 * notification's content.
6180 *
Griff Hazenca48d352014-05-28 22:37:13 -07006181 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006182 * set, although the list of available actions comes from the main notification and not
6183 * from the child page's notification.
6184 *
6185 * @param actionIndex The index of the action to hoist onto the current notification page.
6186 * If wearable actions were added to the main notification, this index
6187 * will apply to that list, otherwise it will apply to the regular
6188 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006189 */
6190 public WearableExtender setContentAction(int actionIndex) {
6191 mContentActionIndex = actionIndex;
6192 return this;
6193 }
6194
6195 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006196 * Get the index of the notification action, if any, that was specified as being clickable
6197 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006198 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006199 *
Griff Hazenca48d352014-05-28 22:37:13 -07006200 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006201 * set, although the list of available actions comes from the main notification and not
6202 * from the child page's notification.
6203 *
6204 * <p>If wearable specific actions were added to the main notification, this index will
6205 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006206 *
6207 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006208 */
6209 public int getContentAction() {
6210 return mContentActionIndex;
6211 }
6212
6213 /**
6214 * Set the gravity that this notification should have within the available viewport space.
6215 * Supported values include {@link android.view.Gravity#TOP},
6216 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6217 * The default value is {@link android.view.Gravity#BOTTOM}.
6218 */
6219 public WearableExtender setGravity(int gravity) {
6220 mGravity = gravity;
6221 return this;
6222 }
6223
6224 /**
6225 * Get the gravity that this notification should have within the available viewport space.
6226 * Supported values include {@link android.view.Gravity#TOP},
6227 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6228 * The default value is {@link android.view.Gravity#BOTTOM}.
6229 */
6230 public int getGravity() {
6231 return mGravity;
6232 }
6233
6234 /**
6235 * Set the custom size preset for the display of this notification out of the available
6236 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6237 * {@link #SIZE_LARGE}.
6238 * <p>Some custom size presets are only applicable for custom display notifications created
6239 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6240 * documentation for the preset in question. See also
6241 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6242 */
6243 public WearableExtender setCustomSizePreset(int sizePreset) {
6244 mCustomSizePreset = sizePreset;
6245 return this;
6246 }
6247
6248 /**
6249 * Get the custom size preset for the display of this notification out of the available
6250 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6251 * {@link #SIZE_LARGE}.
6252 * <p>Some custom size presets are only applicable for custom display notifications created
6253 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6254 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6255 */
6256 public int getCustomSizePreset() {
6257 return mCustomSizePreset;
6258 }
6259
6260 /**
6261 * Set the custom height in pixels for the display of this notification's content.
6262 * <p>This option is only available for custom display notifications created
6263 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6264 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6265 * {@link #getCustomContentHeight}.
6266 */
6267 public WearableExtender setCustomContentHeight(int height) {
6268 mCustomContentHeight = height;
6269 return this;
6270 }
6271
6272 /**
6273 * Get the custom height in pixels for the display of this notification's content.
6274 * <p>This option is only available for custom display notifications created
6275 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6276 * {@link #setCustomContentHeight}.
6277 */
6278 public int getCustomContentHeight() {
6279 return mCustomContentHeight;
6280 }
6281
6282 /**
6283 * Set whether the scrolling position for the contents of this notification should start
6284 * at the bottom of the contents instead of the top when the contents are too long to
6285 * display within the screen. Default is false (start scroll at the top).
6286 */
6287 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6288 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6289 return this;
6290 }
6291
6292 /**
6293 * Get whether the scrolling position for the contents of this notification should start
6294 * at the bottom of the contents instead of the top when the contents are too long to
6295 * display within the screen. Default is false (start scroll at the top).
6296 */
6297 public boolean getStartScrollBottom() {
6298 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6299 }
6300
6301 /**
6302 * Set whether the content intent is available when the wearable device is not connected
6303 * to a companion device. The user can still trigger this intent when the wearable device
6304 * is offline, but a visual hint will indicate that the content intent may not be available.
6305 * Defaults to true.
6306 */
6307 public WearableExtender setContentIntentAvailableOffline(
6308 boolean contentIntentAvailableOffline) {
6309 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6310 return this;
6311 }
6312
6313 /**
6314 * Get whether the content intent is available when the wearable device is not connected
6315 * to a companion device. The user can still trigger this intent when the wearable device
6316 * is offline, but a visual hint will indicate that the content intent may not be available.
6317 * Defaults to true.
6318 */
6319 public boolean getContentIntentAvailableOffline() {
6320 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6321 }
6322
6323 /**
6324 * Set a hint that this notification's icon should not be displayed.
6325 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6326 * @return this object for method chaining
6327 */
6328 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6329 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6330 return this;
6331 }
6332
6333 /**
6334 * Get a hint that this notification's icon should not be displayed.
6335 * @return {@code true} if this icon should not be displayed, false otherwise.
6336 * The default value is {@code false} if this was never set.
6337 */
6338 public boolean getHintHideIcon() {
6339 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6340 }
6341
6342 /**
6343 * Set a visual hint that only the background image of this notification should be
6344 * displayed, and other semantic content should be hidden. This hint is only applicable
6345 * to sub-pages added using {@link #addPage}.
6346 */
6347 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6348 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6349 return this;
6350 }
6351
6352 /**
6353 * Get a visual hint that only the background image of this notification should be
6354 * displayed, and other semantic content should be hidden. This hint is only applicable
6355 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6356 */
6357 public boolean getHintShowBackgroundOnly() {
6358 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6359 }
6360
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006361 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006362 * Set a hint that this notification's background should not be clipped if possible,
6363 * and should instead be resized to fully display on the screen, retaining the aspect
6364 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006365 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6366 * @return this object for method chaining
6367 */
6368 public WearableExtender setHintAvoidBackgroundClipping(
6369 boolean hintAvoidBackgroundClipping) {
6370 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6371 return this;
6372 }
6373
6374 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006375 * Get a hint that this notification's background should not be clipped if possible,
6376 * and should instead be resized to fully display on the screen, retaining the aspect
6377 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006378 * @return {@code true} if it's ok if the background is clipped on the screen, false
6379 * otherwise. The default value is {@code false} if this was never set.
6380 */
6381 public boolean getHintAvoidBackgroundClipping() {
6382 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6383 }
6384
6385 /**
6386 * Set a hint that the screen should remain on for at least this duration when
6387 * this notification is displayed on the screen.
6388 * @param timeout The requested screen timeout in milliseconds. Can also be either
6389 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6390 * @return this object for method chaining
6391 */
6392 public WearableExtender setHintScreenTimeout(int timeout) {
6393 mHintScreenTimeout = timeout;
6394 return this;
6395 }
6396
6397 /**
6398 * Get the duration, in milliseconds, that the screen should remain on for
6399 * when this notification is displayed.
6400 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6401 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6402 */
6403 public int getHintScreenTimeout() {
6404 return mHintScreenTimeout;
6405 }
6406
Alex Hills9ab3a232016-04-05 14:54:56 -04006407 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006408 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6409 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6410 * qr codes, as well as other simple black-and-white tickets.
6411 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6412 * @return this object for method chaining
6413 */
6414 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6415 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6416 return this;
6417 }
6418
6419 /**
6420 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6421 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6422 * qr codes, as well as other simple black-and-white tickets.
6423 * @return {@code true} if it should be displayed in ambient, false otherwise
6424 * otherwise. The default value is {@code false} if this was never set.
6425 */
6426 public boolean getHintAmbientBigPicture() {
6427 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6428 }
6429
6430 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006431 * Set a hint that this notification's content intent will launch an {@link Activity}
6432 * directly, telling the platform that it can generate the appropriate transitions.
6433 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6434 * an activity and transitions should be generated, false otherwise.
6435 * @return this object for method chaining
6436 */
6437 public WearableExtender setHintContentIntentLaunchesActivity(
6438 boolean hintContentIntentLaunchesActivity) {
6439 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6440 return this;
6441 }
6442
6443 /**
6444 * Get a hint that this notification's content intent will launch an {@link Activity}
6445 * directly, telling the platform that it can generate the appropriate transitions
6446 * @return {@code true} if the content intent will launch an activity and transitions should
6447 * be generated, false otherwise. The default value is {@code false} if this was never set.
6448 */
6449 public boolean getHintContentIntentLaunchesActivity() {
6450 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6451 }
6452
Nadia Benbernou948627e2016-04-14 14:41:08 -04006453 /**
6454 * When you post a notification, if you set the dismissal id field, then when that
6455 * notification is canceled, notifications on other wearables and the paired Android phone
6456 * having that same dismissal id will also be canceled. Note that this only works if you
6457 * have notification bridge mode set to NO_BRIDGING in your Wear app manifest. See
6458 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
6459 * Notifications</a> for more information on how to use the bridge mode feature.
6460 * @param dismissalId the dismissal id of the notification.
6461 * @return this object for method chaining
6462 */
6463 public WearableExtender setDismissalId(String dismissalId) {
6464 mDismissalId = dismissalId;
6465 return this;
6466 }
6467
6468 /**
6469 * Returns the dismissal id of the notification.
6470 * @return the dismissal id of the notification or null if it has not been set.
6471 */
6472 public String getDismissalId() {
6473 return mDismissalId;
6474 }
6475
Griff Hazen61a9e862014-05-22 16:05:19 -07006476 private void setFlag(int mask, boolean value) {
6477 if (value) {
6478 mFlags |= mask;
6479 } else {
6480 mFlags &= ~mask;
6481 }
6482 }
6483 }
6484
6485 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006486 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6487 * with car extensions:
6488 *
6489 * <ol>
6490 * <li>Create an {@link Notification.Builder}, setting any desired
6491 * properties.
6492 * <li>Create a {@link CarExtender}.
6493 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6494 * {@link CarExtender}.
6495 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6496 * to apply the extensions to a notification.
6497 * </ol>
6498 *
6499 * <pre class="prettyprint">
6500 * Notification notification = new Notification.Builder(context)
6501 * ...
6502 * .extend(new CarExtender()
6503 * .set*(...))
6504 * .build();
6505 * </pre>
6506 *
6507 * <p>Car extensions can be accessed on an existing notification by using the
6508 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6509 * to access values.
6510 */
6511 public static final class CarExtender implements Extender {
6512 private static final String TAG = "CarExtender";
6513
6514 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6515 private static final String EXTRA_LARGE_ICON = "large_icon";
6516 private static final String EXTRA_CONVERSATION = "car_conversation";
6517 private static final String EXTRA_COLOR = "app_color";
6518
6519 private Bitmap mLargeIcon;
6520 private UnreadConversation mUnreadConversation;
6521 private int mColor = Notification.COLOR_DEFAULT;
6522
6523 /**
6524 * Create a {@link CarExtender} with default options.
6525 */
6526 public CarExtender() {
6527 }
6528
6529 /**
6530 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6531 *
6532 * @param notif The notification from which to copy options.
6533 */
6534 public CarExtender(Notification notif) {
6535 Bundle carBundle = notif.extras == null ?
6536 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6537 if (carBundle != null) {
6538 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6539 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6540
6541 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6542 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6543 }
6544 }
6545
6546 /**
6547 * Apply car extensions to a notification that is being built. This is typically called by
6548 * the {@link Notification.Builder#extend(Notification.Extender)}
6549 * method of {@link Notification.Builder}.
6550 */
6551 @Override
6552 public Notification.Builder extend(Notification.Builder builder) {
6553 Bundle carExtensions = new Bundle();
6554
6555 if (mLargeIcon != null) {
6556 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6557 }
6558 if (mColor != Notification.COLOR_DEFAULT) {
6559 carExtensions.putInt(EXTRA_COLOR, mColor);
6560 }
6561
6562 if (mUnreadConversation != null) {
6563 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6564 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6565 }
6566
6567 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6568 return builder;
6569 }
6570
6571 /**
6572 * Sets the accent color to use when Android Auto presents the notification.
6573 *
6574 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6575 * to accent the displayed notification. However, not all colors are acceptable in an
6576 * automotive setting. This method can be used to override the color provided in the
6577 * notification in such a situation.
6578 */
Tor Norbye80756e32015-03-02 09:39:27 -08006579 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006580 mColor = color;
6581 return this;
6582 }
6583
6584 /**
6585 * Gets the accent color.
6586 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006587 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006588 */
Tor Norbye80756e32015-03-02 09:39:27 -08006589 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006590 public int getColor() {
6591 return mColor;
6592 }
6593
6594 /**
6595 * Sets the large icon of the car notification.
6596 *
6597 * If no large icon is set in the extender, Android Auto will display the icon
6598 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6599 *
6600 * @param largeIcon The large icon to use in the car notification.
6601 * @return This object for method chaining.
6602 */
6603 public CarExtender setLargeIcon(Bitmap largeIcon) {
6604 mLargeIcon = largeIcon;
6605 return this;
6606 }
6607
6608 /**
6609 * Gets the large icon used in this car notification, or null if no icon has been set.
6610 *
6611 * @return The large icon for the car notification.
6612 * @see CarExtender#setLargeIcon
6613 */
6614 public Bitmap getLargeIcon() {
6615 return mLargeIcon;
6616 }
6617
6618 /**
6619 * Sets the unread conversation in a message notification.
6620 *
6621 * @param unreadConversation The unread part of the conversation this notification conveys.
6622 * @return This object for method chaining.
6623 */
6624 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6625 mUnreadConversation = unreadConversation;
6626 return this;
6627 }
6628
6629 /**
6630 * Returns the unread conversation conveyed by this notification.
6631 * @see #setUnreadConversation(UnreadConversation)
6632 */
6633 public UnreadConversation getUnreadConversation() {
6634 return mUnreadConversation;
6635 }
6636
6637 /**
6638 * A class which holds the unread messages from a conversation.
6639 */
6640 public static class UnreadConversation {
6641 private static final String KEY_AUTHOR = "author";
6642 private static final String KEY_TEXT = "text";
6643 private static final String KEY_MESSAGES = "messages";
6644 private static final String KEY_REMOTE_INPUT = "remote_input";
6645 private static final String KEY_ON_REPLY = "on_reply";
6646 private static final String KEY_ON_READ = "on_read";
6647 private static final String KEY_PARTICIPANTS = "participants";
6648 private static final String KEY_TIMESTAMP = "timestamp";
6649
6650 private final String[] mMessages;
6651 private final RemoteInput mRemoteInput;
6652 private final PendingIntent mReplyPendingIntent;
6653 private final PendingIntent mReadPendingIntent;
6654 private final String[] mParticipants;
6655 private final long mLatestTimestamp;
6656
6657 UnreadConversation(String[] messages, RemoteInput remoteInput,
6658 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6659 String[] participants, long latestTimestamp) {
6660 mMessages = messages;
6661 mRemoteInput = remoteInput;
6662 mReadPendingIntent = readPendingIntent;
6663 mReplyPendingIntent = replyPendingIntent;
6664 mParticipants = participants;
6665 mLatestTimestamp = latestTimestamp;
6666 }
6667
6668 /**
6669 * Gets the list of messages conveyed by this notification.
6670 */
6671 public String[] getMessages() {
6672 return mMessages;
6673 }
6674
6675 /**
6676 * Gets the remote input that will be used to convey the response to a message list, or
6677 * null if no such remote input exists.
6678 */
6679 public RemoteInput getRemoteInput() {
6680 return mRemoteInput;
6681 }
6682
6683 /**
6684 * Gets the pending intent that will be triggered when the user replies to this
6685 * notification.
6686 */
6687 public PendingIntent getReplyPendingIntent() {
6688 return mReplyPendingIntent;
6689 }
6690
6691 /**
6692 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6693 * in this object's message list.
6694 */
6695 public PendingIntent getReadPendingIntent() {
6696 return mReadPendingIntent;
6697 }
6698
6699 /**
6700 * Gets the participants in the conversation.
6701 */
6702 public String[] getParticipants() {
6703 return mParticipants;
6704 }
6705
6706 /**
6707 * Gets the firs participant in the conversation.
6708 */
6709 public String getParticipant() {
6710 return mParticipants.length > 0 ? mParticipants[0] : null;
6711 }
6712
6713 /**
6714 * Gets the timestamp of the conversation.
6715 */
6716 public long getLatestTimestamp() {
6717 return mLatestTimestamp;
6718 }
6719
6720 Bundle getBundleForUnreadConversation() {
6721 Bundle b = new Bundle();
6722 String author = null;
6723 if (mParticipants != null && mParticipants.length > 1) {
6724 author = mParticipants[0];
6725 }
6726 Parcelable[] messages = new Parcelable[mMessages.length];
6727 for (int i = 0; i < messages.length; i++) {
6728 Bundle m = new Bundle();
6729 m.putString(KEY_TEXT, mMessages[i]);
6730 m.putString(KEY_AUTHOR, author);
6731 messages[i] = m;
6732 }
6733 b.putParcelableArray(KEY_MESSAGES, messages);
6734 if (mRemoteInput != null) {
6735 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6736 }
6737 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6738 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6739 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6740 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6741 return b;
6742 }
6743
6744 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6745 if (b == null) {
6746 return null;
6747 }
6748 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6749 String[] messages = null;
6750 if (parcelableMessages != null) {
6751 String[] tmp = new String[parcelableMessages.length];
6752 boolean success = true;
6753 for (int i = 0; i < tmp.length; i++) {
6754 if (!(parcelableMessages[i] instanceof Bundle)) {
6755 success = false;
6756 break;
6757 }
6758 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6759 if (tmp[i] == null) {
6760 success = false;
6761 break;
6762 }
6763 }
6764 if (success) {
6765 messages = tmp;
6766 } else {
6767 return null;
6768 }
6769 }
6770
6771 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6772 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6773
6774 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6775
6776 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6777 if (participants == null || participants.length != 1) {
6778 return null;
6779 }
6780
6781 return new UnreadConversation(messages,
6782 remoteInput,
6783 onReply,
6784 onRead,
6785 participants, b.getLong(KEY_TIMESTAMP));
6786 }
6787 };
6788
6789 /**
6790 * Builder class for {@link CarExtender.UnreadConversation} objects.
6791 */
6792 public static class Builder {
6793 private final List<String> mMessages = new ArrayList<String>();
6794 private final String mParticipant;
6795 private RemoteInput mRemoteInput;
6796 private PendingIntent mReadPendingIntent;
6797 private PendingIntent mReplyPendingIntent;
6798 private long mLatestTimestamp;
6799
6800 /**
6801 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6802 *
6803 * @param name The name of the other participant in the conversation.
6804 */
6805 public Builder(String name) {
6806 mParticipant = name;
6807 }
6808
6809 /**
6810 * Appends a new unread message to the list of messages for this conversation.
6811 *
6812 * The messages should be added from oldest to newest.
6813 *
6814 * @param message The text of the new unread message.
6815 * @return This object for method chaining.
6816 */
6817 public Builder addMessage(String message) {
6818 mMessages.add(message);
6819 return this;
6820 }
6821
6822 /**
6823 * Sets the pending intent and remote input which will convey the reply to this
6824 * notification.
6825 *
6826 * @param pendingIntent The pending intent which will be triggered on a reply.
6827 * @param remoteInput The remote input parcelable which will carry the reply.
6828 * @return This object for method chaining.
6829 *
6830 * @see CarExtender.UnreadConversation#getRemoteInput
6831 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6832 */
6833 public Builder setReplyAction(
6834 PendingIntent pendingIntent, RemoteInput remoteInput) {
6835 mRemoteInput = remoteInput;
6836 mReplyPendingIntent = pendingIntent;
6837
6838 return this;
6839 }
6840
6841 /**
6842 * Sets the pending intent that will be sent once the messages in this notification
6843 * are read.
6844 *
6845 * @param pendingIntent The pending intent to use.
6846 * @return This object for method chaining.
6847 */
6848 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6849 mReadPendingIntent = pendingIntent;
6850 return this;
6851 }
6852
6853 /**
6854 * Sets the timestamp of the most recent message in an unread conversation.
6855 *
6856 * If a messaging notification has been posted by your application and has not
6857 * yet been cancelled, posting a later notification with the same id and tag
6858 * but without a newer timestamp may result in Android Auto not displaying a
6859 * heads up notification for the later notification.
6860 *
6861 * @param timestamp The timestamp of the most recent message in the conversation.
6862 * @return This object for method chaining.
6863 */
6864 public Builder setLatestTimestamp(long timestamp) {
6865 mLatestTimestamp = timestamp;
6866 return this;
6867 }
6868
6869 /**
6870 * Builds a new unread conversation object.
6871 *
6872 * @return The new unread conversation object.
6873 */
6874 public UnreadConversation build() {
6875 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6876 String[] participants = { mParticipant };
6877 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6878 mReadPendingIntent, participants, mLatestTimestamp);
6879 }
6880 }
6881 }
6882
6883 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006884 * Get an array of Notification objects from a parcelable array bundle field.
6885 * Update the bundle to have a typed array so fetches in the future don't need
6886 * to do an array copy.
6887 */
6888 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6889 Parcelable[] array = bundle.getParcelableArray(key);
6890 if (array instanceof Notification[] || array == null) {
6891 return (Notification[]) array;
6892 }
6893 Notification[] typedArray = Arrays.copyOf(array, array.length,
6894 Notification[].class);
6895 bundle.putParcelableArray(key, typedArray);
6896 return typedArray;
6897 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006898
6899 private static class BuilderRemoteViews extends RemoteViews {
6900 public BuilderRemoteViews(Parcel parcel) {
6901 super(parcel);
6902 }
6903
Kenny Guy77320062014-08-27 21:37:15 +01006904 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6905 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006906 }
6907
6908 @Override
6909 public BuilderRemoteViews clone() {
6910 Parcel p = Parcel.obtain();
6911 writeToParcel(p, 0);
6912 p.setDataPosition(0);
6913 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6914 p.recycle();
6915 return brv;
6916 }
6917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918}