blob: b33aace6c69a5e879a4e37b49f767f4b65eaf4df [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 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700227 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 public int number;
229
230 /**
231 * The intent to execute when the expanded status entry is clicked. If
232 * this is an activity, it must include the
233 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800234 * that you take care of task management as described in the
235 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800236 * Stack</a> document. In particular, make sure to read the notification section
237 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
238 * Notifications</a> for the correct ways to launch an application from a
239 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
241 public PendingIntent contentIntent;
242
243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500244 * The intent to execute when the notification is explicitly dismissed by the user, either with
245 * the "Clear All" button or by swiping it away individually.
246 *
247 * This probably shouldn't be launching an activity since several of those will be sent
248 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public PendingIntent deleteIntent;
251
252 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700253 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800254 *
Chris Wren47c20a12014-06-18 17:27:29 -0400255 * <p>
256 * The system UI may choose to display a heads-up notification, instead of
257 * launching this intent, while the user is using the device.
258 * </p>
259 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800260 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400261 */
262 public PendingIntent fullScreenIntent;
263
264 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400265 * Text that summarizes this notification for accessibility services.
266 *
267 * As of the L release, this text is no longer shown on screen, but it is still useful to
268 * accessibility services (where it serves as an audible announcement of the notification's
269 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400270 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800271 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public CharSequence tickerText;
274
275 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400276 * Formerly, a view showing the {@link #tickerText}.
277 *
278 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400279 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400280 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800281 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400282
283 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400284 * The view that will represent this notification in the notification list (which is pulled
285 * down from the status bar).
286 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500287 * As of N, this field may be null. The notification view is determined by the inputs
288 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400289 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 public RemoteViews contentView;
293
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400294 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400295 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400296 * opportunity to show more detail. The system UI may choose to show this
297 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400298 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500299 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400300 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
301 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400302 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400303 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400304 public RemoteViews bigContentView;
305
Chris Wren8fd39ec2014-02-27 17:43:26 -0500306
307 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400308 * A medium-format version of {@link #contentView}, providing the Notification an
309 * opportunity to add action buttons to contentView. At its discretion, the system UI may
310 * choose to show this as a heads-up notification, which will pop up so the user can see
311 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500313 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
315 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500316 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400317 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500318 public RemoteViews headsUpContentView;
319
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400320 /**
Dan Sandler86647982015-05-13 23:41:13 -0400321 * A large bitmap to be shown in the notification content area.
322 *
323 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 */
Dan Sandler86647982015-05-13 23:41:13 -0400325 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800326 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
328 /**
329 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400332 * A notification that is noisy is more likely to be presented as a heads-up notification.
333 * </p>
334 *
335 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500336 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * </p>
338 */
339 public Uri sound;
340
341 /**
342 * Use this constant as the value for audioStreamType to request that
343 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700344 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400345 *
346 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700348 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 public static final int STREAM_DEFAULT = -1;
350
351 /**
352 * The audio stream type to use when playing the sound.
353 * Should be one of the STREAM_ constants from
354 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400355 *
356 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700358 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 public int audioStreamType = STREAM_DEFAULT;
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400362 * The default value of {@link #audioAttributes}.
363 */
364 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
365 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
366 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
367 .build();
368
369 /**
370 * The {@link AudioAttributes audio attributes} to use when playing the sound.
371 */
372 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
373
374 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500375 * The pattern with which to vibrate.
376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * <p>
378 * To vibrate the default pattern, see {@link #defaults}.
379 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500380 *
Chris Wren47c20a12014-06-18 17:27:29 -0400381 * <p>
382 * A notification that vibrates is more likely to be presented as a heads-up notification.
383 * </p>
384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * @see android.os.Vibrator#vibrate(long[],int)
386 */
387 public long[] vibrate;
388
389 /**
390 * The color of the led. The hardware will do its best approximation.
391 *
392 * @see #FLAG_SHOW_LIGHTS
393 * @see #flags
394 */
Tor Norbye80756e32015-03-02 09:39:27 -0800395 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 public int ledARGB;
397
398 /**
399 * The number of milliseconds for the LED to be on while it's flashing.
400 * The hardware will do its best approximation.
401 *
402 * @see #FLAG_SHOW_LIGHTS
403 * @see #flags
404 */
405 public int ledOnMS;
406
407 /**
408 * The number of milliseconds for the LED to be off while it's flashing.
409 * The hardware will do its best approximation.
410 *
411 * @see #FLAG_SHOW_LIGHTS
412 * @see #flags
413 */
414 public int ledOffMS;
415
416 /**
417 * Specifies which values should be taken from the defaults.
418 * <p>
419 * To set, OR the desired from {@link #DEFAULT_SOUND},
420 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
421 * values, use {@link #DEFAULT_ALL}.
422 * </p>
423 */
424 public int defaults;
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 /**
427 * Bit to be bitwise-ored into the {@link #flags} field that should be
428 * set if you want the LED on for this notification.
429 * <ul>
430 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
431 * or 0 for both ledOnMS and ledOffMS.</li>
432 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
433 * <li>To flash the LED, pass the number of milliseconds that it should
434 * be on and off to ledOnMS and ledOffMS.</li>
435 * </ul>
436 * <p>
437 * Since hardware varies, you are not guaranteed that any of the values
438 * you pass are honored exactly. Use the system defaults (TODO) if possible
439 * because they will be set to values that work on any given hardware.
440 * <p>
441 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
444 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
445
446 /**
447 * Bit to be bitwise-ored into the {@link #flags} field that should be
448 * set if this notification is in reference to something that is ongoing,
449 * like a phone call. It should not be set if this notification is in
450 * reference to something that happened at a particular point in time,
451 * like a missed phone call.
452 */
453 public static final int FLAG_ONGOING_EVENT = 0x00000002;
454
455 /**
456 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700457 * the audio will be repeated until the notification is
458 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
460 public static final int FLAG_INSISTENT = 0x00000004;
461
462 /**
463 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700464 * set if you would only like the sound, vibrate and ticker to be played
465 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 */
467 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
468
469 /**
470 * Bit to be bitwise-ored into the {@link #flags} field that should be
471 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500472 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
474 public static final int FLAG_AUTO_CANCEL = 0x00000010;
475
476 /**
477 * Bit to be bitwise-ored into the {@link #flags} field that should be
478 * set if the notification should not be canceled when the user clicks
479 * the Clear all button.
480 */
481 public static final int FLAG_NO_CLEAR = 0x00000020;
482
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700483 /**
484 * Bit to be bitwise-ored into the {@link #flags} field that should be
485 * set if this notification represents a currently running service. This
486 * will normally be set for you by {@link Service#startForeground}.
487 */
488 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
489
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400490 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500491 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800492 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500493 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400494 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700495 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500496 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400497
Griff Hazendfcb0802014-02-11 12:00:00 -0800498 /**
499 * Bit to be bitswise-ored into the {@link #flags} field that should be
500 * set if this notification is relevant to the current device only
501 * and it is not recommended that it bridge to other devices.
502 */
503 public static final int FLAG_LOCAL_ONLY = 0x00000100;
504
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700505 /**
506 * Bit to be bitswise-ored into the {@link #flags} field that should be
507 * set if this notification is the group summary for a group of notifications.
508 * Grouped notifications may display in a cluster or stack on devices which
509 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
510 */
511 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
512
Julia Reynoldse46bb372016-03-17 11:05:58 -0400513 /**
514 * Bit to be bitswise-ored into the {@link #flags} field that should be
515 * set if this notification is the group summary for an auto-group of notifications.
516 *
517 * @hide
518 */
519 @SystemApi
520 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public int flags;
523
Tor Norbyed9273d62013-05-30 15:59:53 -0700524 /** @hide */
525 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
526 @Retention(RetentionPolicy.SOURCE)
527 public @interface Priority {}
528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500530 * Default notification {@link #priority}. If your application does not prioritize its own
531 * notifications, use this value for all notifications.
532 */
533 public static final int PRIORITY_DEFAULT = 0;
534
535 /**
536 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
537 * items smaller, or at a different position in the list, compared with your app's
538 * {@link #PRIORITY_DEFAULT} items.
539 */
540 public static final int PRIORITY_LOW = -1;
541
542 /**
543 * Lowest {@link #priority}; these items might not be shown to the user except under special
544 * circumstances, such as detailed notification logs.
545 */
546 public static final int PRIORITY_MIN = -2;
547
548 /**
549 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
550 * show these items larger, or at a different position in notification lists, compared with
551 * your app's {@link #PRIORITY_DEFAULT} items.
552 */
553 public static final int PRIORITY_HIGH = 1;
554
555 /**
556 * Highest {@link #priority}, for your application's most important items that require the
557 * user's prompt attention or input.
558 */
559 public static final int PRIORITY_MAX = 2;
560
561 /**
562 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800563 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500564 * Priority is an indication of how much of the user's valuable attention should be consumed by
565 * this notification. Low-priority notifications may be hidden from the user in certain
566 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500567 * system will make a determination about how to interpret this priority when presenting
568 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400569 *
570 * <p>
571 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
572 * as a heads-up notification.
573 * </p>
574 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700576 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800578
Dan Sandler26e81cf2014-05-06 10:01:27 -0400579 /**
580 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
581 * to be applied by the standard Style templates when presenting this notification.
582 *
583 * The current template design constructs a colorful header image by overlaying the
584 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
585 * ignored.
586 */
Tor Norbye80756e32015-03-02 09:39:27 -0800587 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400588 public int color = COLOR_DEFAULT;
589
590 /**
591 * Special value of {@link #color} telling the system not to decorate this notification with
592 * any special color but instead use default colors when presenting this notification.
593 */
Tor Norbye80756e32015-03-02 09:39:27 -0800594 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400595 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600596
597 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800598 * Special value of {@link #color} used as a place holder for an invalid color.
599 */
600 @ColorInt
601 private static final int COLOR_INVALID = 1;
602
603 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700604 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
605 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600606 * lockscreen).
607 *
608 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
609 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
610 * shown in all situations, but the contents are only available if the device is unlocked for
611 * the appropriate user.
612 *
613 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
614 * can be read even in an "insecure" context (that is, above a secure lockscreen).
615 * To modify the public version of this notification—for example, to redact some portions—see
616 * {@link Builder#setPublicVersion(Notification)}.
617 *
618 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
619 * and ticker until the user has bypassed the lockscreen.
620 */
621 public int visibility;
622
Griff Hazenfc3922d2014-08-20 11:56:44 -0700623 /**
624 * Notification visibility: Show this notification in its entirety on all lockscreens.
625 *
626 * {@see #visibility}
627 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600628 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700629
630 /**
631 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
632 * private information on secure lockscreens.
633 *
634 * {@see #visibility}
635 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600636 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700637
638 /**
639 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
640 *
641 * {@see #visibility}
642 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600643 public static final int VISIBILITY_SECRET = -1;
644
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400646 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400648 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649
650 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400651 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400653 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654
655 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400656 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400658 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659
660 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400661 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400663 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664
665 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400666 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400668 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669
670 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400671 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500672 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400673 public static final String CATEGORY_ALARM = "alarm";
674
675 /**
676 * Notification category: progress of a long-running background operation.
677 */
678 public static final String CATEGORY_PROGRESS = "progress";
679
680 /**
681 * Notification category: social network or sharing update.
682 */
683 public static final String CATEGORY_SOCIAL = "social";
684
685 /**
686 * Notification category: error in background operation or authentication status.
687 */
688 public static final String CATEGORY_ERROR = "err";
689
690 /**
691 * Notification category: media transport control for playback.
692 */
693 public static final String CATEGORY_TRANSPORT = "transport";
694
695 /**
696 * Notification category: system or device status update. Reserved for system use.
697 */
698 public static final String CATEGORY_SYSTEM = "sys";
699
700 /**
701 * Notification category: indication of running background service.
702 */
703 public static final String CATEGORY_SERVICE = "service";
704
705 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400706 * Notification category: a specific, timely recommendation for a single thing.
707 * For example, a news app might want to recommend a news story it believes the user will
708 * want to read next.
709 */
710 public static final String CATEGORY_RECOMMENDATION = "recommendation";
711
712 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400713 * Notification category: ongoing information about device or contextual status.
714 */
715 public static final String CATEGORY_STATUS = "status";
716
717 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400718 * Notification category: user-scheduled reminder.
719 */
720 public static final String CATEGORY_REMINDER = "reminder";
721
722 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400723 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
724 * that best describes this Notification. May be used by the system for ranking and filtering.
725 */
726 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500727
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700728 private String mGroupKey;
729
730 /**
731 * Get the key used to group this notification into a cluster or stack
732 * with other notifications on devices which support such rendering.
733 */
734 public String getGroup() {
735 return mGroupKey;
736 }
737
738 private String mSortKey;
739
740 /**
741 * Get a sort key that orders this notification among other notifications from the
742 * same package. This can be useful if an external sort was already applied and an app
743 * would like to preserve this. Notifications will be sorted lexicographically using this
744 * value, although providing different priorities in addition to providing sort key may
745 * cause this value to be ignored.
746 *
747 * <p>This sort key can also be used to order members of a notification group. See
748 * {@link Builder#setGroup}.
749 *
750 * @see String#compareTo(String)
751 */
752 public String getSortKey() {
753 return mSortKey;
754 }
755
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500756 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400757 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400758 * <p>
759 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
760 * APIs, and are intended to be used by
761 * {@link android.service.notification.NotificationListenerService} implementations to extract
762 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500763 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400764 public Bundle extras = new Bundle();
765
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400766 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700767 * All pending intents in the notification as the system needs to be able to access them but
768 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700769 * custom parcelable objects.
770 *
771 * @hide
772 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700773 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700774
775 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400776 * {@link #extras} key: this is the title of the notification,
777 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
778 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500779 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780
781 /**
782 * {@link #extras} key: this is the title of the notification when shown in expanded form,
783 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
784 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400785 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400786
787 /**
788 * {@link #extras} key: this is the main text payload, as supplied to
789 * {@link Builder#setContentText(CharSequence)}.
790 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500791 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400792
793 /**
794 * {@link #extras} key: this is a third line of text, as supplied to
795 * {@link Builder#setSubText(CharSequence)}.
796 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400797 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400798
799 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800800 * {@link #extras} key: this is the remote input history, as supplied to
801 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700802 *
803 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
804 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
805 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
806 * notifications once the other party has responded).
807 *
808 * The extra with this key is of type CharSequence[] and contains the most recent entry at
809 * the 0 index, the second most recent at the 1 index, etc.
810 *
811 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800812 */
813 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
814
815 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400816 * {@link #extras} key: this is a small piece of additional text as supplied to
817 * {@link Builder#setContentInfo(CharSequence)}.
818 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400819 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820
821 /**
822 * {@link #extras} key: this is a line of summary information intended to be shown
823 * alongside expanded notifications, as supplied to (e.g.)
824 * {@link BigTextStyle#setSummaryText(CharSequence)}.
825 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400826 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400827
828 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200829 * {@link #extras} key: this is the longer text shown in the big form of a
830 * {@link BigTextStyle} notification, as supplied to
831 * {@link BigTextStyle#bigText(CharSequence)}.
832 */
833 public static final String EXTRA_BIG_TEXT = "android.bigText";
834
835 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400836 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
837 * supplied to {@link Builder#setSmallIcon(int)}.
838 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500839 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400840
841 /**
842 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
843 * notification payload, as
844 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
845 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400846 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400847
848 /**
849 * {@link #extras} key: this is a bitmap to be used instead of the one from
850 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
851 * shown in its expanded form, as supplied to
852 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
853 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400854 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400855
856 /**
857 * {@link #extras} key: this is the progress value supplied to
858 * {@link Builder#setProgress(int, int, boolean)}.
859 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400860 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400861
862 /**
863 * {@link #extras} key: this is the maximum value supplied to
864 * {@link Builder#setProgress(int, int, boolean)}.
865 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400866 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400867
868 /**
869 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
870 * {@link Builder#setProgress(int, int, boolean)}.
871 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400872 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400873
874 /**
875 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
876 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
877 * {@link Builder#setUsesChronometer(boolean)}.
878 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400879 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880
881 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800882 * {@link #extras} key: whether the chronometer set on the notification should count down
883 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700884 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800885 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700886 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800887
888 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400889 * {@link #extras} key: whether {@link #when} should be shown,
890 * as supplied to {@link Builder#setShowWhen(boolean)}.
891 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400892 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400893
894 /**
895 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
896 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
897 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400898 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400899
900 /**
901 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
902 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
903 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400904 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400905
906 /**
907 * {@link #extras} key: A string representing the name of the specific
908 * {@link android.app.Notification.Style} used to create this notification.
909 */
Chris Wren91ad5632013-06-05 15:05:57 -0400910 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400911
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400913 * {@link #extras} key: A String array containing the people that this notification relates to,
914 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400916 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500917
918 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400919 * Allow certain system-generated notifications to appear before the device is provisioned.
920 * Only available to notifications coming from the android package.
921 * @hide
922 */
923 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
924
925 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700926 * {@link #extras} key: A
927 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
928 * in the background when the notification is selected. The URI must point to an image stream
929 * suitable for passing into
930 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
931 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
932 * URI used for this purpose must require no permissions to read the image data.
933 */
934 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
935
936 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400937 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700938 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400939 * {@link android.app.Notification.MediaStyle} notification.
940 */
941 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
942
943 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100944 * {@link #extras} key: the indices of actions to be shown in the compact view,
945 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
946 */
947 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
948
Christoph Studer943aa672014-08-03 20:31:16 +0200949 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400950 * {@link #extras} key: the username to be displayed for all messages sent by the user including
951 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700952 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
953 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400954 */
955 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
956
957 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700958 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400959 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400960 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400961 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400962
963 /**
964 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
965 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700966 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
967 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400968 */
969 public static final String EXTRA_MESSAGES = "android.messages";
970
971 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100972 * {@link #extras} key: the user that built the notification.
973 *
974 * @hide
975 */
976 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
977
978 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400979 * @hide
980 */
981 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
982
Selim Cinek247fa012016-02-18 09:50:48 -0800983 /**
984 * @hide
985 */
986 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
987
Dan Sandler80eaa592016-04-14 23:34:54 -0400988 /** @hide */
989 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400990 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
991
Dan Sandlerd63f9322015-05-06 15:18:49 -0400992 private Icon mSmallIcon;
993 private Icon mLargeIcon;
994
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400995 private String mChannelId;
996
Chris Wren51c75102013-07-16 20:49:17 -0400997 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400998 * Structure to encapsulate a named action that can be shown as part of this notification.
999 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1000 * selected by the user.
1001 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001002 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1003 * or {@link Notification.Builder#addAction(Notification.Action)}
1004 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001005 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001006 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -07001007 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001008 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001009 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001010 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -07001011
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001012 /**
1013 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001014 *
1015 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001016 */
Dan Sandler86647982015-05-13 23:41:13 -04001017 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001018 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001019
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001020 /**
1021 * Title of the action.
1022 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001023 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001024
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001025 /**
1026 * Intent to send when the user invokes this action. May be null, in which case the action
1027 * may be rendered in a disabled presentation by the system UI.
1028 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001029 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001030
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001031 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001032 if (in.readInt() != 0) {
1033 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001034 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1035 icon = mIcon.getResId();
1036 }
Dan Sandler86647982015-05-13 23:41:13 -04001037 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001038 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1039 if (in.readInt() == 1) {
1040 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1041 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001042 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001043 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001044 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001045 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001046
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001047 /**
Dan Sandler86647982015-05-13 23:41:13 -04001048 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001049 */
Dan Sandler86647982015-05-13 23:41:13 -04001050 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001051 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001052 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001053 }
1054
Dan Sandler86647982015-05-13 23:41:13 -04001055 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001056 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001057 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001058 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1059 this.icon = icon.getResId();
1060 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001061 this.title = title;
1062 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001063 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001064 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001065 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001066 }
1067
1068 /**
Dan Sandler86647982015-05-13 23:41:13 -04001069 * Return an icon representing the action.
1070 */
1071 public Icon getIcon() {
1072 if (mIcon == null && icon != 0) {
1073 // you snuck an icon in here without using the builder; let's try to keep it
1074 mIcon = Icon.createWithResource("", icon);
1075 }
1076 return mIcon;
1077 }
1078
1079 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001080 * Get additional metadata carried around with this Action.
1081 */
1082 public Bundle getExtras() {
1083 return mExtras;
1084 }
1085
1086 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001087 * Return whether the platform should automatically generate possible replies for this
1088 * {@link Action}
1089 */
1090 public boolean getAllowGeneratedReplies() {
1091 return mAllowGeneratedReplies;
1092 }
1093
1094 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001095 * Get the list of inputs to be collected from the user when this action is sent.
1096 * May return null if no remote inputs were added.
1097 */
1098 public RemoteInput[] getRemoteInputs() {
1099 return mRemoteInputs;
1100 }
1101
1102 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001103 * Builder class for {@link Action} objects.
1104 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001105 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001106 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001107 private final CharSequence mTitle;
1108 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001109 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001110 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001111 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001112
1113 /**
1114 * Construct a new builder for {@link Action} object.
1115 * @param icon icon to show for this action
1116 * @param title the title of the action
1117 * @param intent the {@link PendingIntent} to fire when users trigger this action
1118 */
Dan Sandler86647982015-05-13 23:41:13 -04001119 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001120 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001121 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1122 }
1123
1124 /**
1125 * Construct a new builder for {@link Action} object.
1126 * @param icon icon to show for this action
1127 * @param title the title of the action
1128 * @param intent the {@link PendingIntent} to fire when users trigger this action
1129 */
1130 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001131 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001132 }
1133
1134 /**
1135 * Construct a new builder for {@link Action} object using the fields from an
1136 * {@link Action}.
1137 * @param action the action to read fields from.
1138 */
1139 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001140 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001141 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001142 }
1143
Dan Sandler86647982015-05-13 23:41:13 -04001144 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001145 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001146 mIcon = icon;
1147 mTitle = title;
1148 mIntent = intent;
1149 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001150 if (remoteInputs != null) {
1151 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1152 Collections.addAll(mRemoteInputs, remoteInputs);
1153 }
Griff Hazen959591e2014-05-15 22:26:18 -07001154 }
1155
1156 /**
1157 * Merge additional metadata into this builder.
1158 *
1159 * <p>Values within the Bundle will replace existing extras values in this Builder.
1160 *
1161 * @see Notification.Action#extras
1162 */
1163 public Builder addExtras(Bundle extras) {
1164 if (extras != null) {
1165 mExtras.putAll(extras);
1166 }
1167 return this;
1168 }
1169
1170 /**
1171 * Get the metadata Bundle used by this Builder.
1172 *
1173 * <p>The returned Bundle is shared with this Builder.
1174 */
1175 public Bundle getExtras() {
1176 return mExtras;
1177 }
1178
1179 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001180 * Add an input to be collected from the user when this action is sent.
1181 * Response values can be retrieved from the fired intent by using the
1182 * {@link RemoteInput#getResultsFromIntent} function.
1183 * @param remoteInput a {@link RemoteInput} to add to the action
1184 * @return this object for method chaining
1185 */
1186 public Builder addRemoteInput(RemoteInput remoteInput) {
1187 if (mRemoteInputs == null) {
1188 mRemoteInputs = new ArrayList<RemoteInput>();
1189 }
1190 mRemoteInputs.add(remoteInput);
1191 return this;
1192 }
1193
1194 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001195 * Set whether the platform should automatically generate possible replies to add to
1196 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1197 * {@link RemoteInput}, this has no effect.
1198 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1199 * otherwise
1200 * @return this object for method chaining
1201 * The default value is {@code false}
1202 */
1203 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1204 mAllowGeneratedReplies = allowGeneratedReplies;
1205 return this;
1206 }
1207
1208 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001209 * Apply an extender to this action builder. Extenders may be used to add
1210 * metadata or change options on this builder.
1211 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001212 public Builder extend(Extender extender) {
1213 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001214 return this;
1215 }
1216
1217 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001218 * Combine all of the options that have been set and return a new {@link Action}
1219 * object.
1220 * @return the built action
1221 */
1222 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001223 RemoteInput[] remoteInputs = mRemoteInputs != null
1224 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001225 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1226 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001227 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001228 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001229
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001230 @Override
1231 public Action clone() {
1232 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001233 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001234 title,
1235 actionIntent, // safe to alias
1236 new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001237 getRemoteInputs(),
1238 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001239 }
1240 @Override
1241 public int describeContents() {
1242 return 0;
1243 }
1244 @Override
1245 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001246 final Icon ic = getIcon();
1247 if (ic != null) {
1248 out.writeInt(1);
1249 ic.writeToParcel(out, 0);
1250 } else {
1251 out.writeInt(0);
1252 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001253 TextUtils.writeToParcel(title, out, flags);
1254 if (actionIntent != null) {
1255 out.writeInt(1);
1256 actionIntent.writeToParcel(out, flags);
1257 } else {
1258 out.writeInt(0);
1259 }
Griff Hazen959591e2014-05-15 22:26:18 -07001260 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001261 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001262 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001263 }
Griff Hazen959591e2014-05-15 22:26:18 -07001264 public static final Parcelable.Creator<Action> CREATOR =
1265 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001266 public Action createFromParcel(Parcel in) {
1267 return new Action(in);
1268 }
1269 public Action[] newArray(int size) {
1270 return new Action[size];
1271 }
1272 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001273
1274 /**
1275 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1276 * metadata or change options on an action builder.
1277 */
1278 public interface Extender {
1279 /**
1280 * Apply this extender to a notification action builder.
1281 * @param builder the builder to be modified.
1282 * @return the build object for chaining.
1283 */
1284 public Builder extend(Builder builder);
1285 }
1286
1287 /**
1288 * Wearable extender for notification actions. To add extensions to an action,
1289 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1290 * the {@code WearableExtender()} constructor and apply it to a
1291 * {@link android.app.Notification.Action.Builder} using
1292 * {@link android.app.Notification.Action.Builder#extend}.
1293 *
1294 * <pre class="prettyprint">
1295 * Notification.Action action = new Notification.Action.Builder(
1296 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001297 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001298 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001299 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001300 */
1301 public static final class WearableExtender implements Extender {
1302 /** Notification action extra which contains wearable extensions */
1303 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1304
Pete Gastaf6781d2014-10-07 15:17:05 -04001305 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001306 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001307 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1308 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1309 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001310
1311 // Flags bitwise-ored to mFlags
1312 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001313 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001314 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001315
1316 // Default value for flags integer
1317 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1318
1319 private int mFlags = DEFAULT_FLAGS;
1320
Pete Gastaf6781d2014-10-07 15:17:05 -04001321 private CharSequence mInProgressLabel;
1322 private CharSequence mConfirmLabel;
1323 private CharSequence mCancelLabel;
1324
Griff Hazen61a9e862014-05-22 16:05:19 -07001325 /**
1326 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1327 * options.
1328 */
1329 public WearableExtender() {
1330 }
1331
1332 /**
1333 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1334 * wearable options present in an existing notification action.
1335 * @param action the notification action to inspect.
1336 */
1337 public WearableExtender(Action action) {
1338 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1339 if (wearableBundle != null) {
1340 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001341 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1342 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1343 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001344 }
1345 }
1346
1347 /**
1348 * Apply wearable extensions to a notification action that is being built. This is
1349 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1350 * method of {@link android.app.Notification.Action.Builder}.
1351 */
1352 @Override
1353 public Action.Builder extend(Action.Builder builder) {
1354 Bundle wearableBundle = new Bundle();
1355
1356 if (mFlags != DEFAULT_FLAGS) {
1357 wearableBundle.putInt(KEY_FLAGS, mFlags);
1358 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001359 if (mInProgressLabel != null) {
1360 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1361 }
1362 if (mConfirmLabel != null) {
1363 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1364 }
1365 if (mCancelLabel != null) {
1366 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1367 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001368
1369 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1370 return builder;
1371 }
1372
1373 @Override
1374 public WearableExtender clone() {
1375 WearableExtender that = new WearableExtender();
1376 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001377 that.mInProgressLabel = this.mInProgressLabel;
1378 that.mConfirmLabel = this.mConfirmLabel;
1379 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001380 return that;
1381 }
1382
1383 /**
1384 * Set whether this action is available when the wearable device is not connected to
1385 * a companion device. The user can still trigger this action when the wearable device is
1386 * offline, but a visual hint will indicate that the action may not be available.
1387 * Defaults to true.
1388 */
1389 public WearableExtender setAvailableOffline(boolean availableOffline) {
1390 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1391 return this;
1392 }
1393
1394 /**
1395 * Get whether this action is available when the wearable device is not connected to
1396 * a companion device. The user can still trigger this action when the wearable device is
1397 * offline, but a visual hint will indicate that the action may not be available.
1398 * Defaults to true.
1399 */
1400 public boolean isAvailableOffline() {
1401 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1402 }
1403
1404 private void setFlag(int mask, boolean value) {
1405 if (value) {
1406 mFlags |= mask;
1407 } else {
1408 mFlags &= ~mask;
1409 }
1410 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001411
1412 /**
1413 * Set a label to display while the wearable is preparing to automatically execute the
1414 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1415 *
1416 * @param label the label to display while the action is being prepared to execute
1417 * @return this object for method chaining
1418 */
1419 public WearableExtender setInProgressLabel(CharSequence label) {
1420 mInProgressLabel = label;
1421 return this;
1422 }
1423
1424 /**
1425 * Get the label to display while the wearable is preparing to automatically execute
1426 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1427 *
1428 * @return the label to display while the action is being prepared to execute
1429 */
1430 public CharSequence getInProgressLabel() {
1431 return mInProgressLabel;
1432 }
1433
1434 /**
1435 * Set a label to display to confirm that the action should be executed.
1436 * This is usually an imperative verb like "Send".
1437 *
1438 * @param label the label to confirm the action should be executed
1439 * @return this object for method chaining
1440 */
1441 public WearableExtender setConfirmLabel(CharSequence label) {
1442 mConfirmLabel = label;
1443 return this;
1444 }
1445
1446 /**
1447 * Get the label to display to confirm that the action should be executed.
1448 * This is usually an imperative verb like "Send".
1449 *
1450 * @return the label to confirm the action should be executed
1451 */
1452 public CharSequence getConfirmLabel() {
1453 return mConfirmLabel;
1454 }
1455
1456 /**
1457 * Set a label to display to cancel the action.
1458 * This is usually an imperative verb, like "Cancel".
1459 *
1460 * @param label the label to display to cancel the action
1461 * @return this object for method chaining
1462 */
1463 public WearableExtender setCancelLabel(CharSequence label) {
1464 mCancelLabel = label;
1465 return this;
1466 }
1467
1468 /**
1469 * Get the label to display to cancel the action.
1470 * This is usually an imperative verb like "Cancel".
1471 *
1472 * @return the label to display to cancel the action
1473 */
1474 public CharSequence getCancelLabel() {
1475 return mCancelLabel;
1476 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001477
1478 /**
1479 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1480 * platform that it can generate the appropriate transitions.
1481 * @param hintLaunchesActivity {@code true} if the content intent will launch
1482 * an activity and transitions should be generated, false otherwise.
1483 * @return this object for method chaining
1484 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001485 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001486 boolean hintLaunchesActivity) {
1487 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1488 return this;
1489 }
1490
1491 /**
1492 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1493 * platform that it can generate the appropriate transitions
1494 * @return {@code true} if the content intent will launch an activity and transitions
1495 * should be generated, false otherwise. The default value is {@code false} if this was
1496 * never set.
1497 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001498 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001499 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1500 }
Alex Hills9f087612016-06-07 09:08:59 -04001501
1502 /**
1503 * Set a hint that this Action should be displayed inline.
1504 *
1505 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1506 * otherwise
1507 * @return this object for method chaining
1508 */
1509 public WearableExtender setHintDisplayActionInline(
1510 boolean hintDisplayInline) {
1511 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1512 return this;
1513 }
1514
1515 /**
1516 * Get a hint that this Action should be displayed inline.
1517 *
1518 * @return {@code true} if the Action should be displayed inline, {@code false}
1519 * otherwise. The default value is {@code false} if this was never set.
1520 */
1521 public boolean getHintDisplayActionInline() {
1522 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1523 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001524 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001525 }
1526
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001527 /**
1528 * Array of all {@link Action} structures attached to this notification by
1529 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1530 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1531 * interface for invoking actions.
1532 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001533 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001534
1535 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001536 * Replacement version of this notification whose content will be shown
1537 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1538 * and {@link #VISIBILITY_PUBLIC}.
1539 */
1540 public Notification publicVersion;
1541
1542 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001543 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001544 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 */
1546 public Notification()
1547 {
1548 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001549 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001550 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552
1553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 * @hide
1555 */
1556 public Notification(Context context, int icon, CharSequence tickerText, long when,
1557 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1558 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001559 new Builder(context)
1560 .setWhen(when)
1561 .setSmallIcon(icon)
1562 .setTicker(tickerText)
1563 .setContentTitle(contentTitle)
1564 .setContentText(contentText)
1565 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1566 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568
1569 /**
1570 * Constructs a Notification object with the information needed to
1571 * have a status bar icon without the standard expanded view.
1572 *
1573 * @param icon The resource id of the icon to put in the status bar.
1574 * @param tickerText The text that flows by in the status bar when the notification first
1575 * activates.
1576 * @param when The time to show in the time field. In the System.currentTimeMillis
1577 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001578 *
1579 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001581 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 public Notification(int icon, CharSequence tickerText, long when)
1583 {
1584 this.icon = icon;
1585 this.tickerText = tickerText;
1586 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001587 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
1589
1590 /**
1591 * Unflatten the notification from a parcel.
1592 */
Svet Ganovddb94882016-06-23 19:55:24 -07001593 @SuppressWarnings("unchecked")
1594 public Notification(Parcel parcel) {
1595 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1596 // intents in extras are always written as the last entry.
1597 readFromParcelImpl(parcel);
1598 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001599 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001600 }
1601
1602 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 {
1604 int version = parcel.readInt();
1605
1606 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001607 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001608 if (parcel.readInt() != 0) {
1609 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001610 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1611 icon = mSmallIcon.getResId();
1612 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 number = parcel.readInt();
1615 if (parcel.readInt() != 0) {
1616 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1617 }
1618 if (parcel.readInt() != 0) {
1619 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1620 }
1621 if (parcel.readInt() != 0) {
1622 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1623 }
1624 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001625 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001626 }
1627 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1629 }
Joe Onorato561d3852010-11-20 18:09:34 -08001630 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001631 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 defaults = parcel.readInt();
1634 flags = parcel.readInt();
1635 if (parcel.readInt() != 0) {
1636 sound = Uri.CREATOR.createFromParcel(parcel);
1637 }
1638
1639 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001640 if (parcel.readInt() != 0) {
1641 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 vibrate = parcel.createLongArray();
1644 ledARGB = parcel.readInt();
1645 ledOnMS = parcel.readInt();
1646 ledOffMS = parcel.readInt();
1647 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001648
1649 if (parcel.readInt() != 0) {
1650 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1651 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001652
1653 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001654
John Spurlockfd7f1e02014-03-18 16:41:57 -04001655 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001656
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001657 mGroupKey = parcel.readString();
1658
1659 mSortKey = parcel.readString();
1660
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001661 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001662
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001663 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1664
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001665 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001666 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1667 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001668
Chris Wren8fd39ec2014-02-27 17:43:26 -05001669 if (parcel.readInt() != 0) {
1670 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1671 }
1672
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001673 visibility = parcel.readInt();
1674
1675 if (parcel.readInt() != 0) {
1676 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1677 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001678
1679 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001680
1681 if (parcel.readInt() != 0) {
1682 mChannelId = parcel.readString();
1683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685
Andy Stadler110988c2010-12-03 14:29:16 -08001686 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001687 public Notification clone() {
1688 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001689 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001690 return that;
1691 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001692
Daniel Sandler1a497d32013-04-18 14:52:45 -04001693 /**
1694 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1695 * of this into that.
1696 * @hide
1697 */
1698 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001699 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001700 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001701 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001702 that.number = this.number;
1703
1704 // PendingIntents are global, so there's no reason (or way) to clone them.
1705 that.contentIntent = this.contentIntent;
1706 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001707 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001708
1709 if (this.tickerText != null) {
1710 that.tickerText = this.tickerText.toString();
1711 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001712 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001713 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001714 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001715 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001716 that.contentView = this.contentView.clone();
1717 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001718 if (heavy && this.mLargeIcon != null) {
1719 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001720 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001721 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001722 that.sound = this.sound; // android.net.Uri is immutable
1723 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001724 if (this.audioAttributes != null) {
1725 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1726 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001727
1728 final long[] vibrate = this.vibrate;
1729 if (vibrate != null) {
1730 final int N = vibrate.length;
1731 final long[] vib = that.vibrate = new long[N];
1732 System.arraycopy(vibrate, 0, vib, 0, N);
1733 }
1734
1735 that.ledARGB = this.ledARGB;
1736 that.ledOnMS = this.ledOnMS;
1737 that.ledOffMS = this.ledOffMS;
1738 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001739
Joe Onorato18e69df2010-05-17 22:26:12 -07001740 that.flags = this.flags;
1741
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001742 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001743
John Spurlockfd7f1e02014-03-18 16:41:57 -04001744 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001745
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001746 that.mGroupKey = this.mGroupKey;
1747
1748 that.mSortKey = this.mSortKey;
1749
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001750 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001751 try {
1752 that.extras = new Bundle(this.extras);
1753 // will unparcel
1754 that.extras.size();
1755 } catch (BadParcelableException e) {
1756 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1757 that.extras = null;
1758 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001759 }
1760
Felipe Lemedd85da62016-06-28 11:29:54 -07001761 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1762 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001763 }
1764
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001765 if (this.actions != null) {
1766 that.actions = new Action[this.actions.length];
1767 for(int i=0; i<this.actions.length; i++) {
1768 that.actions[i] = this.actions[i].clone();
1769 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001770 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001771
Daniel Sandler1a497d32013-04-18 14:52:45 -04001772 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001773 that.bigContentView = this.bigContentView.clone();
1774 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001775
Chris Wren8fd39ec2014-02-27 17:43:26 -05001776 if (heavy && this.headsUpContentView != null) {
1777 that.headsUpContentView = this.headsUpContentView.clone();
1778 }
1779
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001780 that.visibility = this.visibility;
1781
1782 if (this.publicVersion != null) {
1783 that.publicVersion = new Notification();
1784 this.publicVersion.cloneInto(that.publicVersion, heavy);
1785 }
1786
Dan Sandler26e81cf2014-05-06 10:01:27 -04001787 that.color = this.color;
1788
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001789 that.mChannelId = this.mChannelId;
1790
Daniel Sandler1a497d32013-04-18 14:52:45 -04001791 if (!heavy) {
1792 that.lightenPayload(); // will clean out extras
1793 }
1794 }
1795
1796 /**
1797 * Removes heavyweight parts of the Notification object for archival or for sending to
1798 * listeners when the full contents are not necessary.
1799 * @hide
1800 */
1801 public final void lightenPayload() {
1802 tickerView = null;
1803 contentView = null;
1804 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001805 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001806 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001807 if (extras != null && !extras.isEmpty()) {
1808 final Set<String> keyset = extras.keySet();
1809 final int N = keyset.size();
1810 final String[] keys = keyset.toArray(new String[N]);
1811 for (int i=0; i<N; i++) {
1812 final String key = keys[i];
1813 final Object obj = extras.get(key);
1814 if (obj != null &&
1815 ( obj instanceof Parcelable
1816 || obj instanceof Parcelable[]
1817 || obj instanceof SparseArray
1818 || obj instanceof ArrayList)) {
1819 extras.remove(key);
1820 }
1821 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001822 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001823 }
1824
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001825 /**
1826 * Make sure this CharSequence is safe to put into a bundle, which basically
1827 * means it had better not be some custom Parcelable implementation.
1828 * @hide
1829 */
1830 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001831 if (cs == null) return cs;
1832 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1833 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1834 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001835 if (cs instanceof Parcelable) {
1836 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1837 + " instance is a custom Parcelable and not allowed in Notification");
1838 return cs.toString();
1839 }
Selim Cinek60a54252016-02-26 17:03:25 -08001840 return removeTextSizeSpans(cs);
1841 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001842
Selim Cinek60a54252016-02-26 17:03:25 -08001843 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1844 if (charSequence instanceof Spanned) {
1845 Spanned ss = (Spanned) charSequence;
1846 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1847 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1848 for (Object span : spans) {
1849 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001850 if (resultSpan instanceof CharacterStyle) {
1851 resultSpan = ((CharacterStyle) span).getUnderlying();
1852 }
1853 if (resultSpan instanceof TextAppearanceSpan) {
1854 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001855 resultSpan = new TextAppearanceSpan(
1856 originalSpan.getFamily(),
1857 originalSpan.getTextStyle(),
1858 -1,
1859 originalSpan.getTextColor(),
1860 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001861 } else if (resultSpan instanceof RelativeSizeSpan
1862 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001863 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001864 } else {
1865 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001866 }
1867 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1868 ss.getSpanFlags(span));
1869 }
1870 return builder;
1871 }
1872 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001873 }
1874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 public int describeContents() {
1876 return 0;
1877 }
1878
1879 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001880 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 */
Svet Ganovddb94882016-06-23 19:55:24 -07001882 public void writeToParcel(Parcel parcel, int flags) {
1883 // We need to mark all pending intents getting into the notification
1884 // system as being put there to later allow the notification ranker
1885 // to launch them and by doing so add the app to the battery saver white
1886 // list for a short period of time. The problem is that the system
1887 // cannot look into the extras as there may be parcelables there that
1888 // the platform does not know how to handle. To go around that we have
1889 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001890 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001891 if (collectPendingIntents) {
1892 PendingIntent.setOnMarshaledListener(
1893 (PendingIntent intent, Parcel out, int outFlags) -> {
1894 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001895 if (allPendingIntents == null) {
1896 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001897 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001898 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001899 }
1900 });
1901 }
1902 try {
1903 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1904 // want to intercept all pending events written to the pacel.
1905 writeToParcelImpl(parcel, flags);
1906 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001907 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001908 } finally {
1909 if (collectPendingIntents) {
1910 PendingIntent.setOnMarshaledListener(null);
1911 }
1912 }
1913 }
1914
1915 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 parcel.writeInt(1);
1917
1918 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001919 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001920 if (mSmallIcon == null && icon != 0) {
1921 // you snuck an icon in here without using the builder; let's try to keep it
1922 mSmallIcon = Icon.createWithResource("", icon);
1923 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001924 if (mSmallIcon != null) {
1925 parcel.writeInt(1);
1926 mSmallIcon.writeToParcel(parcel, 0);
1927 } else {
1928 parcel.writeInt(0);
1929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 parcel.writeInt(number);
1931 if (contentIntent != null) {
1932 parcel.writeInt(1);
1933 contentIntent.writeToParcel(parcel, 0);
1934 } else {
1935 parcel.writeInt(0);
1936 }
1937 if (deleteIntent != null) {
1938 parcel.writeInt(1);
1939 deleteIntent.writeToParcel(parcel, 0);
1940 } else {
1941 parcel.writeInt(0);
1942 }
1943 if (tickerText != null) {
1944 parcel.writeInt(1);
1945 TextUtils.writeToParcel(tickerText, parcel, flags);
1946 } else {
1947 parcel.writeInt(0);
1948 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001949 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001950 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001951 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001952 } else {
1953 parcel.writeInt(0);
1954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 if (contentView != null) {
1956 parcel.writeInt(1);
1957 contentView.writeToParcel(parcel, 0);
1958 } else {
1959 parcel.writeInt(0);
1960 }
Selim Cinek279fa862016-06-14 10:57:25 -07001961 if (mLargeIcon == null && largeIcon != null) {
1962 // you snuck an icon in here without using the builder; let's try to keep it
1963 mLargeIcon = Icon.createWithBitmap(largeIcon);
1964 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001965 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001966 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001967 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001968 } else {
1969 parcel.writeInt(0);
1970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971
1972 parcel.writeInt(defaults);
1973 parcel.writeInt(this.flags);
1974
1975 if (sound != null) {
1976 parcel.writeInt(1);
1977 sound.writeToParcel(parcel, 0);
1978 } else {
1979 parcel.writeInt(0);
1980 }
1981 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001982
1983 if (audioAttributes != null) {
1984 parcel.writeInt(1);
1985 audioAttributes.writeToParcel(parcel, 0);
1986 } else {
1987 parcel.writeInt(0);
1988 }
1989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 parcel.writeLongArray(vibrate);
1991 parcel.writeInt(ledARGB);
1992 parcel.writeInt(ledOnMS);
1993 parcel.writeInt(ledOffMS);
1994 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001995
1996 if (fullScreenIntent != null) {
1997 parcel.writeInt(1);
1998 fullScreenIntent.writeToParcel(parcel, 0);
1999 } else {
2000 parcel.writeInt(0);
2001 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002002
2003 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002004
John Spurlockfd7f1e02014-03-18 16:41:57 -04002005 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002006
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002007 parcel.writeString(mGroupKey);
2008
2009 parcel.writeString(mSortKey);
2010
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002011 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002012
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002013 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002014
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002015 if (bigContentView != null) {
2016 parcel.writeInt(1);
2017 bigContentView.writeToParcel(parcel, 0);
2018 } else {
2019 parcel.writeInt(0);
2020 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002021
Chris Wren8fd39ec2014-02-27 17:43:26 -05002022 if (headsUpContentView != null) {
2023 parcel.writeInt(1);
2024 headsUpContentView.writeToParcel(parcel, 0);
2025 } else {
2026 parcel.writeInt(0);
2027 }
2028
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002029 parcel.writeInt(visibility);
2030
2031 if (publicVersion != null) {
2032 parcel.writeInt(1);
2033 publicVersion.writeToParcel(parcel, 0);
2034 } else {
2035 parcel.writeInt(0);
2036 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002037
2038 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002039
2040 if (mChannelId != null) {
2041 parcel.writeInt(1);
2042 parcel.writeString(mChannelId);
2043 } else {
2044 parcel.writeInt(0);
2045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
2047
2048 /**
2049 * Parcelable.Creator that instantiates Notification objects
2050 */
2051 public static final Parcelable.Creator<Notification> CREATOR
2052 = new Parcelable.Creator<Notification>()
2053 {
2054 public Notification createFromParcel(Parcel parcel)
2055 {
2056 return new Notification(parcel);
2057 }
2058
2059 public Notification[] newArray(int size)
2060 {
2061 return new Notification[size];
2062 }
2063 };
2064
2065 /**
2066 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2067 * layout.
2068 *
2069 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2070 * in the view.</p>
2071 * @param context The context for your application / activity.
2072 * @param contentTitle The title that goes in the expanded entry.
2073 * @param contentText The text that goes in the expanded entry.
2074 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2075 * If this is an activity, it must include the
2076 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002077 * that you take care of task management as described in the
2078 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2079 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002080 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002081 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002082 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002084 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 public void setLatestEventInfo(Context context,
2086 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002087 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2088 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2089 new Throwable());
2090 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002091
Selim Cinek4ac6f602016-06-13 15:47:03 -07002092 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2093 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2094 }
2095
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002096 // ensure that any information already set directly is preserved
2097 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002098
2099 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002101 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
2103 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002104 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002106 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002107
2108 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 }
2110
Julia Reynoldsda303542015-11-23 14:00:20 -05002111 /**
2112 * @hide
2113 */
2114 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002115 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2116 }
2117
2118 /**
2119 * @hide
2120 */
2121 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2122 Notification notification) {
2123 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2124 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002125 }
2126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 @Override
2128 public String toString() {
2129 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002130 sb.append("Notification(pri=");
2131 sb.append(priority);
2132 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002133 if (contentView != null) {
2134 sb.append(contentView.getPackage());
2135 sb.append("/0x");
2136 sb.append(Integer.toHexString(contentView.getLayoutId()));
2137 } else {
2138 sb.append("null");
2139 }
2140 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002141 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2142 sb.append("default");
2143 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 int N = this.vibrate.length-1;
2145 sb.append("[");
2146 for (int i=0; i<N; i++) {
2147 sb.append(this.vibrate[i]);
2148 sb.append(',');
2149 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002150 if (N != -1) {
2151 sb.append(this.vibrate[N]);
2152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 } else {
2155 sb.append("null");
2156 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002157 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002158 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002160 } else if (this.sound != null) {
2161 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 } else {
2163 sb.append("null");
2164 }
Chris Wren365b6d32015-07-16 10:39:26 -04002165 if (this.tickerText != null) {
2166 sb.append(" tick");
2167 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002168 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002170 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002171 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002172 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002173 if (this.category != null) {
2174 sb.append(" category=");
2175 sb.append(this.category);
2176 }
2177 if (this.mGroupKey != null) {
2178 sb.append(" groupKey=");
2179 sb.append(this.mGroupKey);
2180 }
2181 if (this.mSortKey != null) {
2182 sb.append(" sortKey=");
2183 sb.append(this.mSortKey);
2184 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002185 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002186 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002187 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002188 }
2189 sb.append(" vis=");
2190 sb.append(visibilityToString(this.visibility));
2191 if (this.publicVersion != null) {
2192 sb.append(" publicVersion=");
2193 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002194 }
2195 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 return sb.toString();
2197 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002198
Dan Sandler1b718782014-07-18 12:43:45 -04002199 /**
2200 * {@hide}
2201 */
2202 public static String visibilityToString(int vis) {
2203 switch (vis) {
2204 case VISIBILITY_PRIVATE:
2205 return "PRIVATE";
2206 case VISIBILITY_PUBLIC:
2207 return "PUBLIC";
2208 case VISIBILITY_SECRET:
2209 return "SECRET";
2210 default:
2211 return "UNKNOWN(" + String.valueOf(vis) + ")";
2212 }
2213 }
2214
Joe Onoratocb109a02011-01-18 17:57:41 -08002215 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002216 * {@hide}
2217 */
2218 public static String priorityToString(@Priority int pri) {
2219 switch (pri) {
2220 case PRIORITY_MIN:
2221 return "MIN";
2222 case PRIORITY_LOW:
2223 return "LOW";
2224 case PRIORITY_DEFAULT:
2225 return "DEFAULT";
2226 case PRIORITY_HIGH:
2227 return "HIGH";
2228 case PRIORITY_MAX:
2229 return "MAX";
2230 default:
2231 return "UNKNOWN(" + String.valueOf(pri) + ")";
2232 }
2233 }
2234
2235 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002236 * Returns the id of the channel this notification posts to.
2237 */
2238 public String getNotificationChannel() {
2239 return mChannelId;
2240 }
2241
2242 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002243 * The small icon representing this notification in the status bar and content view.
2244 *
2245 * @return the small icon representing this notification.
2246 *
2247 * @see Builder#getSmallIcon()
2248 * @see Builder#setSmallIcon(Icon)
2249 */
2250 public Icon getSmallIcon() {
2251 return mSmallIcon;
2252 }
2253
2254 /**
2255 * Used when notifying to clean up legacy small icons.
2256 * @hide
2257 */
2258 public void setSmallIcon(Icon icon) {
2259 mSmallIcon = icon;
2260 }
2261
2262 /**
2263 * The large icon shown in this notification's content view.
2264 * @see Builder#getLargeIcon()
2265 * @see Builder#setLargeIcon(Icon)
2266 */
2267 public Icon getLargeIcon() {
2268 return mLargeIcon;
2269 }
2270
2271 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002272 * @hide
2273 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002274 public boolean isGroupSummary() {
2275 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2276 }
2277
2278 /**
2279 * @hide
2280 */
2281 public boolean isGroupChild() {
2282 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2283 }
2284
2285 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002286 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002287 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002288 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002289 * content views using the platform's notification layout template. If your app supports
2290 * versions of Android as old as API level 4, you can instead use
2291 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2292 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2293 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002294 *
Scott Main183bf112012-08-13 19:12:13 -07002295 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002296 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002297 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002298 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002299 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2300 * .setContentText(subject)
2301 * .setSmallIcon(R.drawable.new_mail)
2302 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002303 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002304 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002305 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002306 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002307 /**
2308 * @hide
2309 */
2310 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2311 "android.rebuild.contentViewActionCount";
2312 /**
2313 * @hide
2314 */
2315 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2316 = "android.rebuild.bigViewActionCount";
2317 /**
2318 * @hide
2319 */
2320 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2321 = "android.rebuild.hudViewActionCount";
2322
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002323 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002324
Joe Onorato46439ce2010-11-19 13:56:21 -08002325 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002326 private Notification mN;
2327 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002328 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002329 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2330 private ArrayList<String> mPersonList = new ArrayList<String>();
2331 private NotificationColorUtil mColorUtil;
2332 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002333
2334 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002335 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2336 */
2337 private int mCachedContrastColor = COLOR_INVALID;
2338 private int mCachedContrastColorIsFor = COLOR_INVALID;
2339
2340 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002341 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002342 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002343
2344 * <table>
2345 * <tr><th align=right>priority</th>
2346 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2347 * <tr><th align=right>when</th>
2348 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2349 * <tr><th align=right>audio stream</th>
2350 * <td>{@link #STREAM_DEFAULT}</td></tr>
2351 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002352 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002353
2354 * @param context
2355 * A {@link Context} that will be used by the Builder to construct the
2356 * RemoteViews. The Context will not be held past the lifetime of this Builder
2357 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002358 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002359 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002360 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002361 }
2362
Joe Onoratocb109a02011-01-18 17:57:41 -08002363 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002364 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002365 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002366 public Builder(Context context, Notification toAdopt) {
2367 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002368
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002369 if (toAdopt == null) {
2370 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002371 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2372 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2373 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002374 mN.priority = PRIORITY_DEFAULT;
2375 mN.visibility = VISIBILITY_PRIVATE;
2376 } else {
2377 mN = toAdopt;
2378 if (mN.actions != null) {
2379 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002380 }
2381
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002382 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2383 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2384 }
2385
Selim Cinek4ac6f602016-06-13 15:47:03 -07002386 if (mN.getSmallIcon() == null && mN.icon != 0) {
2387 setSmallIcon(mN.icon);
2388 }
2389
2390 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2391 setLargeIcon(mN.largeIcon);
2392 }
2393
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002394 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2395 if (!TextUtils.isEmpty(templateClass)) {
2396 final Class<? extends Style> styleClass
2397 = getNotificationStyleClass(templateClass);
2398 if (styleClass == null) {
2399 Log.d(TAG, "Unknown style class: " + templateClass);
2400 } else {
2401 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002402 final Constructor<? extends Style> ctor =
2403 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002404 ctor.setAccessible(true);
2405 final Style style = ctor.newInstance();
2406 style.restoreFromExtras(mN.extras);
2407
2408 if (style != null) {
2409 setStyle(style);
2410 }
2411 } catch (Throwable t) {
2412 Log.e(TAG, "Could not create Style", t);
2413 }
2414 }
2415 }
2416
2417 }
2418 }
2419
2420 private NotificationColorUtil getColorUtil() {
2421 if (!mColorUtilInited) {
2422 mColorUtilInited = true;
2423 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2424 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002425 }
2426 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002427 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002428 }
2429
2430 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002431 * Specifies the channel the notification should be delivered on.
2432 */
2433 public Builder setChannel(String channelId) {
2434 mN.mChannelId = channelId;
2435 return this;
2436 }
2437
2438 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002439 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002440 *
2441 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2442 * shown anymore by default and must be opted into by using
2443 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002444 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002445 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002446 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002447 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002448 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002449 return this;
2450 }
2451
Joe Onoratocb109a02011-01-18 17:57:41 -08002452 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002453 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002454 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002455 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2456 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002457 */
2458 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002459 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002460 return this;
2461 }
2462
2463 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002464 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002465 *
2466 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002467 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002468 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002469 * Useful when showing an elapsed time (like an ongoing phone call).
2470 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002471 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002472 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002473 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002474 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002475 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002476 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002477 */
2478 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002479 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002480 return this;
2481 }
2482
2483 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002484 * Sets the Chronometer to count down instead of counting up.
2485 *
2486 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2487 * If it isn't set the chronometer will count up.
2488 *
2489 * @see #setUsesChronometer(boolean)
2490 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002491 public Builder setChronometerCountDown(boolean countDown) {
2492 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002493 return this;
2494 }
2495
2496 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002497 * Set the small icon resource, which will be used to represent the notification in the
2498 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002499 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002500
2501 * The platform template for the expanded view will draw this icon in the left, unless a
2502 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2503 * icon will be moved to the right-hand side.
2504 *
2505
2506 * @param icon
2507 * A resource ID in the application's package of the drawable to use.
2508 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002509 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002510 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002511 return setSmallIcon(icon != 0
2512 ? Icon.createWithResource(mContext, icon)
2513 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002514 }
2515
Joe Onoratocb109a02011-01-18 17:57:41 -08002516 /**
2517 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2518 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2519 * LevelListDrawable}.
2520 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002521 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002522 * @param level The level to use for the icon.
2523 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002524 * @see Notification#icon
2525 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002526 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002527 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002528 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002529 return setSmallIcon(icon);
2530 }
2531
2532 /**
2533 * Set the small icon, which will be used to represent the notification in the
2534 * status bar and content view (unless overriden there by a
2535 * {@link #setLargeIcon(Bitmap) large icon}).
2536 *
2537 * @param icon An Icon object to use.
2538 * @see Notification#icon
2539 */
2540 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002541 mN.setSmallIcon(icon);
2542 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2543 mN.icon = icon.getResId();
2544 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002545 return this;
2546 }
2547
Joe Onoratocb109a02011-01-18 17:57:41 -08002548 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002549 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002550 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002551 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002552 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002553 return this;
2554 }
2555
Joe Onoratocb109a02011-01-18 17:57:41 -08002556 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002557 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002558 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002559 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002560 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002561 return this;
2562 }
2563
Joe Onoratocb109a02011-01-18 17:57:41 -08002564 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002565 * This provides some additional information that is displayed in the notification. No
2566 * guarantees are given where exactly it is displayed.
2567 *
2568 * <p>This information should only be provided if it provides an essential
2569 * benefit to the understanding of the notification. The more text you provide the
2570 * less readable it becomes. For example, an email client should only provide the account
2571 * name here if more than one email account has been added.</p>
2572 *
2573 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2574 * notification header area.
2575 *
2576 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2577 * this will be shown in the third line of text in the platform notification template.
2578 * You should not be using {@link #setProgress(int, int, boolean)} at the
2579 * same time on those versions; they occupy the same place.
2580 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002581 */
2582 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002583 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002584 return this;
2585 }
2586
2587 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002588 * Set the remote input history.
2589 *
2590 * This should be set to the most recent inputs that have been sent
2591 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2592 * longer relevant (e.g. for chat notifications once the other party has responded).
2593 *
2594 * The most recent input must be stored at the 0 index, the second most recent at the
2595 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2596 * and how much of each individual input is shown.
2597 *
2598 * <p>Note: The reply text will only be shown on notifications that have least one action
2599 * with a {@code RemoteInput}.</p>
2600 */
2601 public Builder setRemoteInputHistory(CharSequence[] text) {
2602 if (text == null) {
2603 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2604 } else {
2605 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2606 CharSequence[] safe = new CharSequence[N];
2607 for (int i = 0; i < N; i++) {
2608 safe[i] = safeCharSequence(text[i]);
2609 }
2610 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2611 }
2612 return this;
2613 }
2614
2615 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002616 * Set the large number at the right-hand side of the notification. This is
2617 * equivalent to setContentInfo, although it might show the number in a different
2618 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002619 *
2620 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002621 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002622 @Deprecated
Joe Onorato8595a3d2010-11-19 18:12:07 -08002623 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002624 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002625 return this;
2626 }
2627
Joe Onoratocb109a02011-01-18 17:57:41 -08002628 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002629 * A small piece of additional information pertaining to this notification.
2630 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002631 * The platform template will draw this on the last line of the notification, at the far
2632 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002633 *
2634 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2635 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2636 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002637 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002638 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002639 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002640 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002641 return this;
2642 }
2643
Joe Onoratocb109a02011-01-18 17:57:41 -08002644 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002645 * Set the progress this notification represents.
2646 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002647 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002648 */
2649 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002650 mN.extras.putInt(EXTRA_PROGRESS, progress);
2651 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2652 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002653 return this;
2654 }
2655
2656 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002657 * Supply a custom RemoteViews to use instead of the platform template.
2658 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002659 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002660 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002661 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002662 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002663 return setCustomContentView(views);
2664 }
2665
2666 /**
2667 * Supply custom RemoteViews to use instead of the platform template.
2668 *
2669 * This will override the layout that would otherwise be constructed by this Builder
2670 * object.
2671 */
2672 public Builder setCustomContentView(RemoteViews contentView) {
2673 mN.contentView = contentView;
2674 return this;
2675 }
2676
2677 /**
2678 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2679 *
2680 * This will override the expanded layout that would otherwise be constructed by this
2681 * Builder object.
2682 */
2683 public Builder setCustomBigContentView(RemoteViews contentView) {
2684 mN.bigContentView = contentView;
2685 return this;
2686 }
2687
2688 /**
2689 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2690 *
2691 * This will override the heads-up layout that would otherwise be constructed by this
2692 * Builder object.
2693 */
2694 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2695 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002696 return this;
2697 }
2698
Joe Onoratocb109a02011-01-18 17:57:41 -08002699 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002700 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2701 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002702 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2703 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2704 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002705 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002706 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002707 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002708 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002709 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002710 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002711 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002712 return this;
2713 }
2714
Joe Onoratocb109a02011-01-18 17:57:41 -08002715 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002716 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2717 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002718 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002719 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002720 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002721 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002722 return this;
2723 }
2724
Joe Onoratocb109a02011-01-18 17:57:41 -08002725 /**
2726 * An intent to launch instead of posting the notification to the status bar.
2727 * Only for use with extremely high-priority notifications demanding the user's
2728 * <strong>immediate</strong> attention, such as an incoming phone call or
2729 * alarm clock that the user has explicitly set to a particular time.
2730 * If this facility is used for something else, please give the user an option
2731 * to turn it off and use a normal notification, as this can be extremely
2732 * disruptive.
2733 *
Chris Wren47c20a12014-06-18 17:27:29 -04002734 * <p>
2735 * The system UI may choose to display a heads-up notification, instead of
2736 * launching this intent, while the user is using the device.
2737 * </p>
2738 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002739 * @param intent The pending intent to launch.
2740 * @param highPriority Passing true will cause this notification to be sent
2741 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002742 *
2743 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002744 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002745 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002746 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002747 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2748 return this;
2749 }
2750
Joe Onoratocb109a02011-01-18 17:57:41 -08002751 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002752 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002753 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002754 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002755 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002756 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002757 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002758 return this;
2759 }
2760
Joe Onoratocb109a02011-01-18 17:57:41 -08002761 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002762 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002763 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002764 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002765 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002766 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002767 setTicker(tickerText);
2768 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002769 return this;
2770 }
2771
Joe Onoratocb109a02011-01-18 17:57:41 -08002772 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002773 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002774 *
2775 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002776 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2777 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002778 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002779 public Builder setLargeIcon(Bitmap b) {
2780 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2781 }
2782
2783 /**
2784 * Add a large icon to the notification content view.
2785 *
2786 * In the platform template, this image will be shown on the left of the notification view
2787 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2788 * badge atop the large icon).
2789 */
2790 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002791 mN.mLargeIcon = icon;
2792 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002793 return this;
2794 }
2795
Joe Onoratocb109a02011-01-18 17:57:41 -08002796 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002797 * Set the sound to play.
2798 *
John Spurlockc0650f022014-07-19 13:22:39 -04002799 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2800 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002801 *
Chris Wren47c20a12014-06-18 17:27:29 -04002802 * <p>
2803 * A notification that is noisy is more likely to be presented as a heads-up notification.
2804 * </p>
2805 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002806 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002807 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002808 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002809 mN.sound = sound;
2810 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002811 return this;
2812 }
2813
Joe Onoratocb109a02011-01-18 17:57:41 -08002814 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002815 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002816 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002817 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2818 *
Chris Wren47c20a12014-06-18 17:27:29 -04002819 * <p>
2820 * A notification that is noisy is more likely to be presented as a heads-up notification.
2821 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002822 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002823 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002824 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002825 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002826 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002827 mN.sound = sound;
2828 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002829 return this;
2830 }
2831
Joe Onoratocb109a02011-01-18 17:57:41 -08002832 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002833 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2834 * use during playback.
2835 *
2836 * <p>
2837 * A notification that is noisy is more likely to be presented as a heads-up notification.
2838 * </p>
2839 *
2840 * @see Notification#sound
2841 */
2842 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002843 mN.sound = sound;
2844 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002845 return this;
2846 }
2847
2848 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002849 * Set the vibration pattern to use.
2850 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002851 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2852 * <code>pattern</code> parameter.
2853 *
Chris Wren47c20a12014-06-18 17:27:29 -04002854 * <p>
2855 * A notification that vibrates is more likely to be presented as a heads-up notification.
2856 * </p>
2857 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002858 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002859 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002860 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002861 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002862 return this;
2863 }
2864
Joe Onoratocb109a02011-01-18 17:57:41 -08002865 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002866 * Set the desired color for the indicator LED on the device, as well as the
2867 * blink duty cycle (specified in milliseconds).
2868 *
2869
2870 * Not all devices will honor all (or even any) of these values.
2871 *
2872
2873 * @see Notification#ledARGB
2874 * @see Notification#ledOnMS
2875 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002876 */
Tor Norbye80756e32015-03-02 09:39:27 -08002877 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002878 mN.ledARGB = argb;
2879 mN.ledOnMS = onMs;
2880 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002881 if (onMs != 0 || offMs != 0) {
2882 mN.flags |= FLAG_SHOW_LIGHTS;
2883 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002884 return this;
2885 }
2886
Joe Onoratocb109a02011-01-18 17:57:41 -08002887 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002888 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002889 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002890
2891 * Ongoing notifications cannot be dismissed by the user, so your application or service
2892 * must take care of canceling them.
2893 *
2894
2895 * They are typically used to indicate a background task that the user is actively engaged
2896 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2897 * (e.g., a file download, sync operation, active network connection).
2898 *
2899
2900 * @see Notification#FLAG_ONGOING_EVENT
2901 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002902 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002903 public Builder setOngoing(boolean ongoing) {
2904 setFlag(FLAG_ONGOING_EVENT, ongoing);
2905 return this;
2906 }
2907
Joe Onoratocb109a02011-01-18 17:57:41 -08002908 /**
2909 * Set this flag if you would only like the sound, vibrate
2910 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002911 *
2912 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002913 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002914 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2915 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2916 return this;
2917 }
2918
Joe Onoratocb109a02011-01-18 17:57:41 -08002919 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002920 * Make this notification automatically dismissed when the user touches it. The
2921 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2922 *
2923 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002924 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002925 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002926 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002927 return this;
2928 }
2929
Joe Onoratocb109a02011-01-18 17:57:41 -08002930 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002931 * Set whether or not this notification should not bridge to other devices.
2932 *
2933 * <p>Some notifications can be bridged to other devices for remote display.
2934 * This hint can be set to recommend this notification not be bridged.
2935 */
2936 public Builder setLocalOnly(boolean localOnly) {
2937 setFlag(FLAG_LOCAL_ONLY, localOnly);
2938 return this;
2939 }
2940
2941 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002942 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002943 * <p>
2944 * The value should be one or more of the following fields combined with
2945 * bitwise-or:
2946 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2947 * <p>
2948 * For all default values, use {@link #DEFAULT_ALL}.
2949 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002950 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002951 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002952 return this;
2953 }
2954
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002955 /**
2956 * Set the priority of this notification.
2957 *
2958 * @see Notification#priority
2959 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002960 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002961 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002962 return this;
2963 }
Joe Malin8d40d042012-11-05 11:36:40 -08002964
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002965 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002966 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002967 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002968 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002969 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002970 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002971 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002972 return this;
2973 }
2974
2975 /**
Chris Wrendde75302014-03-26 17:24:15 -04002976 * Add a person that is relevant to this notification.
2977 *
Chris Wrene6c48932014-09-29 17:19:27 -04002978 * <P>
2979 * Depending on user preferences, this annotation may allow the notification to pass
2980 * through interruption filters, and to appear more prominently in the user interface.
2981 * </P>
2982 *
2983 * <P>
2984 * The person should be specified by the {@code String} representation of a
2985 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2986 * </P>
2987 *
2988 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2989 * URIs. The path part of these URIs must exist in the contacts database, in the
2990 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2991 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2992 * </P>
2993 *
2994 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002995 * @see Notification#EXTRA_PEOPLE
2996 */
Chris Wrene6c48932014-09-29 17:19:27 -04002997 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002998 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002999 return this;
3000 }
3001
3002 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003003 * Set this notification to be part of a group of notifications sharing the same key.
3004 * Grouped notifications may display in a cluster or stack on devices which
3005 * support such rendering.
3006 *
3007 * <p>To make this notification the summary for its group, also call
3008 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3009 * {@link #setSortKey}.
3010 * @param groupKey The group key of the group.
3011 * @return this object for method chaining
3012 */
3013 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003014 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003015 return this;
3016 }
3017
3018 /**
3019 * Set this notification to be the group summary for a group of notifications.
3020 * Grouped notifications may display in a cluster or stack on devices which
3021 * support such rendering. Requires a group key also be set using {@link #setGroup}.
3022 * @param isGroupSummary Whether this notification should be a group summary.
3023 * @return this object for method chaining
3024 */
3025 public Builder setGroupSummary(boolean isGroupSummary) {
3026 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3027 return this;
3028 }
3029
3030 /**
3031 * Set a sort key that orders this notification among other notifications from the
3032 * same package. This can be useful if an external sort was already applied and an app
3033 * would like to preserve this. Notifications will be sorted lexicographically using this
3034 * value, although providing different priorities in addition to providing sort key may
3035 * cause this value to be ignored.
3036 *
3037 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003038 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003039 *
3040 * @see String#compareTo(String)
3041 */
3042 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003043 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003044 return this;
3045 }
3046
3047 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003048 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003049 *
Griff Hazen720042b2014-02-24 15:46:56 -08003050 * <p>Values within the Bundle will replace existing extras values in this Builder.
3051 *
3052 * @see Notification#extras
3053 */
Griff Hazen959591e2014-05-15 22:26:18 -07003054 public Builder addExtras(Bundle extras) {
3055 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003056 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003057 }
3058 return this;
3059 }
3060
3061 /**
3062 * Set metadata for this notification.
3063 *
3064 * <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 -04003065 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003066 * called.
3067 *
Griff Hazen720042b2014-02-24 15:46:56 -08003068 * <p>Replaces any existing extras values with those from the provided Bundle.
3069 * Use {@link #addExtras} to merge in metadata instead.
3070 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003071 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003072 */
Griff Hazen959591e2014-05-15 22:26:18 -07003073 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003074 if (extras != null) {
3075 mUserExtras = extras;
3076 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003077 return this;
3078 }
3079
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003080 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003081 * Get the current metadata Bundle used by this notification Builder.
3082 *
3083 * <p>The returned Bundle is shared with this Builder.
3084 *
3085 * <p>The current contents of this Bundle are copied into the Notification each time
3086 * {@link #build()} is called.
3087 *
3088 * @see Notification#extras
3089 */
3090 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003091 return mUserExtras;
3092 }
3093
3094 private Bundle getAllExtras() {
3095 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3096 saveExtras.putAll(mN.extras);
3097 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003098 }
3099
3100 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003101 * Add an action to this notification. Actions are typically displayed by
3102 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003103 * <p>
3104 * Every action must have an icon (32dp square and matching the
3105 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3106 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3107 * <p>
3108 * A notification in its expanded form can display up to 3 actions, from left to right in
3109 * the order they were added. Actions will not be displayed when the notification is
3110 * collapsed, however, so be sure that any essential functions may be accessed by the user
3111 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003112 *
3113 * @param icon Resource ID of a drawable that represents the action.
3114 * @param title Text describing the action.
3115 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003116 *
3117 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003118 */
Dan Sandler86647982015-05-13 23:41:13 -04003119 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003120 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003121 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003122 return this;
3123 }
3124
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003125 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003126 * Add an action to this notification. Actions are typically displayed by
3127 * the system as a button adjacent to the notification content.
3128 * <p>
3129 * Every action must have an icon (32dp square and matching the
3130 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3131 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3132 * <p>
3133 * A notification in its expanded form can display up to 3 actions, from left to right in
3134 * the order they were added. Actions will not be displayed when the notification is
3135 * collapsed, however, so be sure that any essential functions may be accessed by the user
3136 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3137 *
3138 * @param action The action to add.
3139 */
3140 public Builder addAction(Action action) {
3141 mActions.add(action);
3142 return this;
3143 }
3144
3145 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003146 * Alter the complete list of actions attached to this notification.
3147 * @see #addAction(Action).
3148 *
3149 * @param actions
3150 * @return
3151 */
3152 public Builder setActions(Action... actions) {
3153 mActions.clear();
3154 for (int i = 0; i < actions.length; i++) {
3155 mActions.add(actions[i]);
3156 }
3157 return this;
3158 }
3159
3160 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003161 * Add a rich notification style to be applied at build time.
3162 *
3163 * @param style Object responsible for modifying the notification style.
3164 */
3165 public Builder setStyle(Style style) {
3166 if (mStyle != style) {
3167 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003168 if (mStyle != null) {
3169 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003170 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3171 } else {
3172 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003173 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003174 }
3175 return this;
3176 }
3177
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003178 /**
3179 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003180 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003181 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3182 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3183 *
3184 * @return The same Builder.
3185 */
3186 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003187 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003188 return this;
3189 }
3190
3191 /**
3192 * Supply a replacement Notification whose contents should be shown in insecure contexts
3193 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3194 * @param n A replacement notification, presumably with some or all info redacted.
3195 * @return The same Builder.
3196 */
3197 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003198 if (n != null) {
3199 mN.publicVersion = new Notification();
3200 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3201 } else {
3202 mN.publicVersion = null;
3203 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003204 return this;
3205 }
3206
Griff Hazenb720abe2014-05-20 13:15:30 -07003207 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003208 * Apply an extender to this notification builder. Extenders may be used to add
3209 * metadata or change options on this builder.
3210 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003211 public Builder extend(Extender extender) {
3212 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003213 return this;
3214 }
3215
Dan Sandler4e787062015-06-17 15:09:48 -04003216 /**
3217 * @hide
3218 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003219 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003220 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003221 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003222 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003223 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003224 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003225 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003226 }
3227
Dan Sandler26e81cf2014-05-06 10:01:27 -04003228 /**
3229 * Sets {@link Notification#color}.
3230 *
3231 * @param argb The accent color to use
3232 *
3233 * @return The same Builder.
3234 */
Tor Norbye80756e32015-03-02 09:39:27 -08003235 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003236 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003237 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003238 return this;
3239 }
3240
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003241 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003242 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3243 // This user can never be a badged profile,
3244 // and also includes USER_ALL system notifications.
3245 return null;
3246 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003247 // Note: This assumes that the current user can read the profile badge of the
3248 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003249 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003250 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003251 }
3252
3253 private Bitmap getProfileBadge() {
3254 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003255 if (badge == null) {
3256 return null;
3257 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003258 final int size = mContext.getResources().getDimensionPixelSize(
3259 R.dimen.notification_badge_size);
3260 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003261 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003262 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003263 badge.draw(canvas);
3264 return bitmap;
3265 }
3266
Selim Cinekc848c3a2016-01-13 15:27:30 -08003267 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003268 Bitmap profileBadge = getProfileBadge();
3269
Kenny Guy98193ea2014-07-24 19:54:37 +01003270 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003271 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3272 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003273 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003274 }
3275
Christoph Studerfe718432014-09-01 18:21:18 +02003276 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003277 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003278 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003279 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003280 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003281 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003282 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003283 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003284 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003285 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003286 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003287 }
3288
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003289 /**
3290 * Resets the notification header to its original state
3291 */
3292 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003293 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3294 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003295 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003296 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003297 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003298 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003299 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003300 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003301 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003302 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003303 contentView.setImageViewIcon(R.id.profile_badge, null);
3304 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003305 }
3306
3307 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003308 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3309 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003310 }
3311
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003312 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003313 return applyStandardTemplate(resId, true /* hasProgress */);
3314 }
3315
3316 /**
3317 * @param hasProgress whether the progress bar should be shown and set
3318 */
3319 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003320 final Bundle ex = mN.extras;
3321
3322 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3323 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3324 return applyStandardTemplate(resId, hasProgress, title, text);
3325 }
3326
3327 /**
3328 * @param hasProgress whether the progress bar should be shown and set
3329 */
3330 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3331 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003332 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003333
Christoph Studerfe718432014-09-01 18:21:18 +02003334 resetStandardTemplate(contentView);
3335
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003337
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003338 bindNotificationHeader(contentView);
3339 bindLargeIcon(contentView);
Selim Cinek954cc232016-05-20 13:29:23 -07003340 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003341 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003342 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003343 contentView.setTextViewText(R.id.title, title);
Selim Cinek954cc232016-05-20 13:29:23 -07003344 contentView.setViewLayoutWidth(R.id.title, showProgress
3345 ? ViewGroup.LayoutParams.WRAP_CONTENT
3346 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003347 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07003348 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003349 int textId = showProgress ? com.android.internal.R.id.text_line_1
3350 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003351 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003352 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003353 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003354
Selim Cinek279fa862016-06-14 10:57:25 -07003355 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003356
Selim Cinek29603462015-11-17 19:04:39 -08003357 return contentView;
3358 }
3359
Selim Cinek860b6da2015-12-16 19:02:19 -08003360 /**
3361 * @param remoteView the remote view to update the minheight in
3362 * @param hasMinHeight does it have a mimHeight
3363 * @hide
3364 */
3365 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3366 int minHeight = 0;
3367 if (hasMinHeight) {
3368 // we need to set the minHeight of the notification
3369 minHeight = mContext.getResources().getDimensionPixelSize(
3370 com.android.internal.R.dimen.notification_min_content_height);
3371 }
3372 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3373 }
3374
Selim Cinek29603462015-11-17 19:04:39 -08003375 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003376 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3377 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3378 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3379 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003380 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003381 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003382 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003383 contentView.setProgressBackgroundTintList(
3384 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3385 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003386 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003387 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003388 contentView.setProgressTintList(R.id.progress, colorStateList);
3389 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003390 }
Selim Cinek29603462015-11-17 19:04:39 -08003391 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003392 } else {
3393 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003394 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003395 }
Joe Onorato561d3852010-11-20 18:09:34 -08003396 }
3397
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003398 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003399 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3400 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3401 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003402 if (mN.mLargeIcon != null) {
3403 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3404 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3405 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003406 int endMargin = R.dimen.notification_content_picture_margin;
3407 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3408 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3409 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003410 }
3411 }
3412
3413 private void bindNotificationHeader(RemoteViews contentView) {
3414 bindSmallIcon(contentView);
3415 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003416 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003417 bindHeaderChronometerAndTime(contentView);
3418 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003419 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003420 }
3421
3422 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003423 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003424 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003425 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003426 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003427 }
3428
3429 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3430 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003431 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003432 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3433 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3434 contentView.setLong(R.id.chronometer, "setBase",
3435 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3436 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003437 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003438 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003439 } else {
3440 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3441 contentView.setLong(R.id.time, "setTime", mN.when);
3442 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003443 } else {
3444 // We still want a time to be set but gone, such that we can show and hide it
3445 // on demand in case it's a child notification without anything in the header
3446 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003447 }
3448 }
3449
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003450 private void bindHeaderText(RemoteViews contentView) {
3451 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3452 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003453 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003454 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003455 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003456 if (headerText == null
3457 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3458 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3459 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3460 }
3461 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003462 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003463 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3464 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3465 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003466 }
3467 }
3468
Adrian Rooseba05822016-04-22 17:09:27 -07003469 /**
3470 * @hide
3471 */
3472 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003473 CharSequence name = null;
3474 final PackageManager pm = mContext.getPackageManager();
3475 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3476 // only system packages which lump together a bunch of unrelated stuff
3477 // may substitute a different name to make the purpose of the
3478 // notification more clear. the correct package label should always
3479 // be accessible via SystemUI.
3480 final String pkg = mContext.getPackageName();
3481 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3482 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3483 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3484 name = subName;
3485 } else {
3486 Log.w(TAG, "warning: pkg "
3487 + pkg + " attempting to substitute app name '" + subName
3488 + "' without holding perm "
3489 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3490 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003491 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003492 if (TextUtils.isEmpty(name)) {
3493 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3494 }
3495 if (TextUtils.isEmpty(name)) {
3496 // still nothing?
3497 return null;
3498 }
3499
3500 return String.valueOf(name);
3501 }
3502 private void bindHeaderAppName(RemoteViews contentView) {
3503 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003504 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003505 }
3506
3507 private void bindSmallIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003508 if (mN.mSmallIcon == null && mN.icon != 0) {
3509 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3510 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003511 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003512 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3513 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003514 processSmallIconColor(mN.mSmallIcon, contentView);
3515 }
3516
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003517 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003518 * @return true if the built notification will show the time or the chronometer; false
3519 * otherwise
3520 */
3521 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003522 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003523 }
3524
Christoph Studerfe718432014-09-01 18:21:18 +02003525 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003526 // actions_container is only reset when there are no actions to avoid focus issues with
3527 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003528 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003529 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003530
3531 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3532 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3533
3534 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3535 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3536 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3537 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003538
3539 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003540 }
3541
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003542 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003543 final Bundle ex = mN.extras;
3544
3545 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3546 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3547 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3548 }
3549
3550 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3551 CharSequence title, CharSequence text) {
3552 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003553
Christoph Studerfe718432014-09-01 18:21:18 +02003554 resetStandardTemplateWithActions(big);
3555
Adrian Roose458aa82015-12-08 16:17:19 -08003556 boolean validRemoteInput = false;
3557
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003558 int N = mActions.size();
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003559 boolean emphazisedMode = mN.fullScreenIntent != null;
3560 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003561 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003562 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003563 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roosf852a422016-06-03 13:33:43 -07003564 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3565 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003566 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003567 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003568 Action action = mActions.get(i);
3569 validRemoteInput |= hasValidRemoteInput(action);
3570
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003571 final RemoteViews button = generateActionButton(action, emphazisedMode,
3572 i % 2 != 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003573 big.addView(R.id.actions, button);
3574 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003575 } else {
3576 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003577 }
Adrian Roose458aa82015-12-08 16:17:19 -08003578
3579 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3580 if (validRemoteInput && replyText != null
3581 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3582 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3583 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3584
3585 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3586 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3587 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3588
3589 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3590 big.setViewVisibility(
3591 R.id.notification_material_reply_text_3, View.VISIBLE);
3592 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3593 }
3594 }
3595 }
3596
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003597 return big;
3598 }
3599
Adrian Roose458aa82015-12-08 16:17:19 -08003600 private boolean hasValidRemoteInput(Action action) {
3601 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3602 // Weird actions
3603 return false;
3604 }
3605
3606 RemoteInput[] remoteInputs = action.getRemoteInputs();
3607 if (remoteInputs == null) {
3608 return false;
3609 }
3610
3611 for (RemoteInput r : remoteInputs) {
3612 CharSequence[] choices = r.getChoices();
3613 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3614 return true;
3615 }
3616 }
3617 return false;
3618 }
3619
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003620 /**
3621 * Construct a RemoteViews for the final 1U notification layout. In order:
3622 * 1. Custom contentView from the caller
3623 * 2. Style's proposed content view
3624 * 3. Standard template view
3625 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003626 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003627 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003628 return mN.contentView;
3629 } else if (mStyle != null) {
3630 final RemoteViews styleView = mStyle.makeContentView();
3631 if (styleView != null) {
3632 return styleView;
3633 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003634 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003636 }
3637
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003638 /**
3639 * Construct a RemoteViews for the final big notification layout.
3640 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003641 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003642 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003643 if (mN.bigContentView != null
3644 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003645 return mN.bigContentView;
3646 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003647 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003648 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003649 } else if (mActions.size() != 0) {
3650 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003651 }
3652 adaptNotificationHeaderForBigContentView(result);
3653 return result;
3654 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003655
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003656 /**
3657 * Construct a RemoteViews for the final notification header only
3658 *
3659 * @hide
3660 */
3661 public RemoteViews makeNotificationHeader() {
3662 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3663 R.layout.notification_template_header);
3664 resetNotificationHeader(header);
3665 bindNotificationHeader(header);
3666 return header;
3667 }
3668
Selim Cinek29603462015-11-17 19:04:39 -08003669 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003670 if (result != null) {
3671 result.setViewVisibility(R.id.text_line_1, View.GONE);
3672 }
Selim Cinek29603462015-11-17 19:04:39 -08003673 }
3674
Selim Cinek850a8542015-11-11 11:48:36 -05003675 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003676 if (result != null) {
3677 result.setBoolean(R.id.notification_header, "setExpanded", true);
3678 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003679 }
3680
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003681 /**
3682 * Construct a RemoteViews for the final heads-up notification layout.
3683 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003684 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003685 if (mN.headsUpContentView != null
3686 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003687 return mN.headsUpContentView;
3688 } else if (mStyle != null) {
3689 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3690 if (styleView != null) {
3691 return styleView;
3692 }
3693 } else if (mActions.size() == 0) {
3694 return null;
3695 }
3696
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003697 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003698 }
3699
Selim Cinek624c02db2015-12-14 21:00:02 -08003700 /**
3701 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3702 *
3703 * @hide
3704 */
3705 public RemoteViews makePublicContentView() {
3706 if (mN.publicVersion != null) {
3707 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003708 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003709 }
3710 Bundle savedBundle = mN.extras;
3711 Style style = mStyle;
3712 mStyle = null;
3713 Icon largeIcon = mN.mLargeIcon;
3714 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003715 Bitmap largeIconLegacy = mN.largeIcon;
3716 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003717 Bundle publicExtras = new Bundle();
3718 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3719 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3720 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3721 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003722 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3723 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003724 publicExtras.putCharSequence(EXTRA_TITLE,
3725 mContext.getString(R.string.notification_hidden_text));
3726 mN.extras = publicExtras;
3727 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3728 mN.extras = savedBundle;
3729 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003730 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003731 mStyle = style;
3732 return publicView;
3733 }
3734
3735
Chris Wren8fd39ec2014-02-27 17:43:26 -05003736
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003737 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
3738 boolean oddAction) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003739 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003740 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003741 emphazisedMode ? getEmphasizedActionLayoutResource()
3742 : tombstone ? getActionTombstoneLayoutResource()
3743 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04003744 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003745 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003746 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003747 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003748 if (action.mRemoteInputs != null) {
3749 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3750 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003751 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07003752 // change the background bgColor
3753 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
3754 : R.color.notification_action_list_dark);
3755 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003756 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07003757 CharSequence title = action.title;
3758 ColorStateList[] outResultColor = null;
3759 if (isLegacy()) {
3760 title = clearColorSpans(title);
3761 } else {
3762 outResultColor = new ColorStateList[1];
3763 title = ensureColorSpanContrast(title, bgColor, outResultColor);
3764 }
3765 button.setTextViewText(R.id.action0, title);
3766 if (outResultColor != null && outResultColor[0] != null) {
3767 // We need to set the text color as well since changing a text to uppercase
3768 // clears its spans.
3769 button.setTextColor(R.id.action0, outResultColor[0]);
3770 } else if (mN.color != COLOR_DEFAULT) {
3771 button.setTextColor(R.id.action0,resolveContrastColor());
3772 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003773 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07003774 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003775 if (mN.color != COLOR_DEFAULT) {
3776 button.setTextColor(R.id.action0, resolveContrastColor());
3777 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003778 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003779 return button;
3780 }
3781
Joe Onoratocb109a02011-01-18 17:57:41 -08003782 /**
Selim Cinek981962e2016-07-20 20:41:58 -07003783 * Clears all color spans of a text
3784 * @param charSequence the input text
3785 * @return the same text but without color spans
3786 */
3787 private CharSequence clearColorSpans(CharSequence charSequence) {
3788 if (charSequence instanceof Spanned) {
3789 Spanned ss = (Spanned) charSequence;
3790 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3791 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3792 for (Object span : spans) {
3793 Object resultSpan = span;
3794 if (resultSpan instanceof CharacterStyle) {
3795 resultSpan = ((CharacterStyle) span).getUnderlying();
3796 }
3797 if (resultSpan instanceof TextAppearanceSpan) {
3798 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3799 if (originalSpan.getTextColor() != null) {
3800 resultSpan = new TextAppearanceSpan(
3801 originalSpan.getFamily(),
3802 originalSpan.getTextStyle(),
3803 originalSpan.getTextSize(),
3804 null,
3805 originalSpan.getLinkTextColor());
3806 }
3807 } else if (resultSpan instanceof ForegroundColorSpan
3808 || (resultSpan instanceof BackgroundColorSpan)) {
3809 continue;
3810 } else {
3811 resultSpan = span;
3812 }
3813 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
3814 ss.getSpanFlags(span));
3815 }
3816 return builder;
3817 }
3818 return charSequence;
3819 }
3820
3821 /**
3822 * Ensures contrast on color spans against a background color. also returns the color of the
3823 * text if a span was found that spans over the whole text.
3824 *
3825 * @param charSequence the charSequence on which the spans are
3826 * @param background the background color to ensure the contrast against
3827 * @param outResultColor an array in which a color will be returned as the first element if
3828 * there exists a full length color span.
3829 * @return the contrasted charSequence
3830 */
3831 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
3832 ColorStateList[] outResultColor) {
3833 if (charSequence instanceof Spanned) {
3834 Spanned ss = (Spanned) charSequence;
3835 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3836 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3837 for (Object span : spans) {
3838 Object resultSpan = span;
3839 int spanStart = ss.getSpanStart(span);
3840 int spanEnd = ss.getSpanEnd(span);
3841 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
3842 if (resultSpan instanceof CharacterStyle) {
3843 resultSpan = ((CharacterStyle) span).getUnderlying();
3844 }
3845 if (resultSpan instanceof TextAppearanceSpan) {
3846 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3847 ColorStateList textColor = originalSpan.getTextColor();
3848 if (textColor != null) {
3849 int[] colors = textColor.getColors();
3850 int[] newColors = new int[colors.length];
3851 for (int i = 0; i < newColors.length; i++) {
3852 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
3853 colors[i], background);
3854 }
3855 textColor = new ColorStateList(textColor.getStates().clone(),
3856 newColors);
3857 resultSpan = new TextAppearanceSpan(
3858 originalSpan.getFamily(),
3859 originalSpan.getTextStyle(),
3860 originalSpan.getTextSize(),
3861 textColor,
3862 originalSpan.getLinkTextColor());
3863 if (fullLength) {
3864 outResultColor[0] = new ColorStateList(
3865 textColor.getStates().clone(), newColors);
3866 }
3867 }
3868 } else if (resultSpan instanceof ForegroundColorSpan) {
3869 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
3870 int foregroundColor = originalSpan.getForegroundColor();
3871 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
3872 foregroundColor, background);
3873 resultSpan = new ForegroundColorSpan(foregroundColor);
3874 if (fullLength) {
3875 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
3876 }
3877 } else {
3878 resultSpan = span;
3879 }
3880
3881 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
3882 }
3883 return builder;
3884 }
3885 return charSequence;
3886 }
3887
3888 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003889 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003890 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003891 */
3892 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003893 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003894 }
3895
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003896 private CharSequence processLegacyText(CharSequence charSequence) {
3897 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003898 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003899 } else {
3900 return charSequence;
3901 }
3902 }
3903
Dan Sandler26e81cf2014-05-06 10:01:27 -04003904 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003905 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003906 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003907 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003908 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3909 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003910 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003911 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003912
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003913 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003914 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003915 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003916 }
3917
Dan Sandler26e81cf2014-05-06 10:01:27 -04003918 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003919 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003920 * if it's grayscale).
3921 */
3922 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003923 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3924 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003925 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003926 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003927 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003928 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003929 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003930 }
3931
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003932 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003933 if (mN.color != COLOR_DEFAULT) {
3934 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003935 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003936 }
3937
Adrian Roos4ff3b122016-02-01 12:26:13 -08003938 int resolveContrastColor() {
3939 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3940 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003941 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003942 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3943
3944 mCachedContrastColorIsFor = mN.color;
3945 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003946 }
3947
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003948 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003949 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003950 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003951 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003952 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003953 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003954 mN.actions = new Action[mActions.size()];
3955 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003956 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003957 if (!mPersonList.isEmpty()) {
3958 mN.extras.putStringArray(EXTRA_PEOPLE,
3959 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003960 }
Selim Cinek247fa012016-02-18 09:50:48 -08003961 if (mN.bigContentView != null || mN.contentView != null
3962 || mN.headsUpContentView != null) {
3963 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3964 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003965 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003966 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003967
Julia Reynolds3b848122016-02-26 10:45:32 -05003968 /**
3969 * Creates a Builder from an existing notification so further changes can be made.
3970 * @param context The context for your application / activity.
3971 * @param n The notification to create a Builder from.
3972 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003973 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003974 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003975 ApplicationInfo applicationInfo = n.extras.getParcelable(
3976 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003977 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003978 if (applicationInfo != null) {
3979 try {
3980 builderContext = context.createApplicationContext(applicationInfo,
3981 Context.CONTEXT_RESTRICTED);
3982 } catch (NameNotFoundException e) {
3983 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3984 builderContext = context; // try with our context
3985 }
3986 } else {
3987 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003988 }
3989
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003991 }
3992
3993 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003994 Class<? extends Style>[] classes = new Class[] {
3995 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003996 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3997 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003998 for (Class<? extends Style> innerClass : classes) {
3999 if (templateClass.equals(innerClass.getName())) {
4000 return innerClass;
4001 }
4002 }
4003 return null;
4004 }
4005
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004006 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004007 * @deprecated Use {@link #build()} instead.
4008 */
4009 @Deprecated
4010 public Notification getNotification() {
4011 return build();
4012 }
4013
4014 /**
4015 * Combine all of the options that have been set and return a new {@link Notification}
4016 * object.
4017 */
4018 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004019 // first, add any extras from the calling code
4020 if (mUserExtras != null) {
4021 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004022 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004023
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004024 mN.creationTime = System.currentTimeMillis();
4025
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004026 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004027 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004028
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004029 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004030
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004031 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004032 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004033 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004034
Adrian Roos5081c0d2016-02-26 16:04:19 -08004035 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4036 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004037 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004038 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004039 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4040 mN.contentView.getSequenceNumber());
4041 }
4042 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004043 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004044 if (mN.bigContentView != null) {
4045 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4046 mN.bigContentView.getSequenceNumber());
4047 }
4048 }
4049 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004050 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004051 if (mN.headsUpContentView != null) {
4052 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4053 mN.headsUpContentView.getSequenceNumber());
4054 }
4055 }
4056 }
4057
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004058 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4059 mN.flags |= FLAG_SHOW_LIGHTS;
4060 }
4061
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004062 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004063 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004064
4065 /**
4066 * Apply this Builder to an existing {@link Notification} object.
4067 *
4068 * @hide
4069 */
4070 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004071 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004072 return n;
4073 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004074
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004075 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004076 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4077 * change.
4078 *
4079 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4080 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004081 * @hide
4082 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004083 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004084 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004085
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004086 // Only strip views for known Styles because we won't know how to
4087 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004088 if (!TextUtils.isEmpty(templateClass)
4089 && getNotificationStyleClass(templateClass) == null) {
4090 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004091 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004092
4093 // Only strip unmodified BuilderRemoteViews.
4094 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004095 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004096 n.contentView.getSequenceNumber();
4097 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004098 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004099 n.bigContentView.getSequenceNumber();
4100 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004101 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004102 n.headsUpContentView.getSequenceNumber();
4103
4104 // Nothing to do here, no need to clone.
4105 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4106 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004107 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004108
4109 Notification clone = n.clone();
4110 if (stripContentView) {
4111 clone.contentView = null;
4112 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4113 }
4114 if (stripBigContentView) {
4115 clone.bigContentView = null;
4116 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4117 }
4118 if (stripHeadsUpContentView) {
4119 clone.headsUpContentView = null;
4120 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4121 }
4122 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004123 }
4124
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004125 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004126 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004127 }
4128
4129 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004130 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004131 }
4132
4133 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004134 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004135 }
4136
4137 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004138 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004139 }
4140
4141 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004142 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004143 }
4144
Adrian Roosc1a80b02016-04-05 14:54:55 -07004145 private int getMessagingLayoutResource() {
4146 return R.layout.notification_template_material_messaging;
4147 }
4148
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004149 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004150 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004151 }
4152
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004153 private int getEmphasizedActionLayoutResource() {
4154 return R.layout.notification_material_action_emphasized;
4155 }
4156
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004157 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004158 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004159 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004160 }
4161
Selim Cinek279fa862016-06-14 10:57:25 -07004162 private boolean hasLargeIcon() {
4163 return mLargeIcon != null || largeIcon != null;
4164 }
4165
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004166 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004167 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004168 * @hide
4169 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004170 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004171 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4172 }
4173
4174 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004175 * @return true if the notification will show a chronometer; false otherwise
4176 * @hide
4177 */
4178 public boolean showsChronometer() {
4179 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4180 }
4181
4182 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004183 * An object that can apply a rich notification style to a {@link Notification.Builder}
4184 * object.
4185 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004186 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004187 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004188
4189 /**
4190 * @hide
4191 */
4192 protected CharSequence mSummaryText = null;
4193
4194 /**
4195 * @hide
4196 */
4197 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004198
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004199 protected Builder mBuilder;
4200
Chris Wrend6297db2012-05-03 16:20:13 -04004201 /**
4202 * Overrides ContentTitle in the big form of the template.
4203 * This defaults to the value passed to setContentTitle().
4204 */
4205 protected void internalSetBigContentTitle(CharSequence title) {
4206 mBigContentTitle = title;
4207 }
4208
4209 /**
4210 * Set the first line of text after the detail section in the big form of the template.
4211 */
4212 protected void internalSetSummaryText(CharSequence cs) {
4213 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004214 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004215 }
4216
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004217 public void setBuilder(Builder builder) {
4218 if (mBuilder != builder) {
4219 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004220 if (mBuilder != null) {
4221 mBuilder.setStyle(this);
4222 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004223 }
4224 }
4225
Chris Wrend6297db2012-05-03 16:20:13 -04004226 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004227 if (mBuilder == null) {
4228 throw new IllegalArgumentException("Style requires a valid Builder object");
4229 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004230 }
Chris Wrend6297db2012-05-03 16:20:13 -04004231
4232 protected RemoteViews getStandardView(int layoutId) {
4233 checkBuilder();
4234
Christoph Studer4600f9b2014-07-22 22:44:43 +02004235 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004236 CharSequence oldBuilderContentTitle =
4237 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004238 if (mBigContentTitle != null) {
4239 mBuilder.setContentTitle(mBigContentTitle);
4240 }
4241
Chris Wrend6297db2012-05-03 16:20:13 -04004242 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4243
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004244 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004245
Chris Wrend6297db2012-05-03 16:20:13 -04004246 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4247 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004248 } else {
4249 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004250 }
4251
Chris Wrend6297db2012-05-03 16:20:13 -04004252 return contentView;
4253 }
4254
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004255 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004256 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4257 * The default implementation has nothing additional to add.
4258 * @hide
4259 */
4260 public RemoteViews makeContentView() {
4261 return null;
4262 }
4263
4264 /**
4265 * Construct a Style-specific RemoteViews for the final big notification layout.
4266 * @hide
4267 */
4268 public RemoteViews makeBigContentView() {
4269 return null;
4270 }
4271
4272 /**
4273 * Construct a Style-specific RemoteViews for the final HUN layout.
4274 * @hide
4275 */
4276 public RemoteViews makeHeadsUpContentView() {
4277 return null;
4278 }
4279
4280 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004281 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004282 * @hide
4283 */
4284 public void addExtras(Bundle extras) {
4285 if (mSummaryTextSet) {
4286 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4287 }
4288 if (mBigContentTitle != null) {
4289 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4290 }
Chris Wren91ad5632013-06-05 15:05:57 -04004291 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004292 }
4293
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004294 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004295 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004296 * @hide
4297 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004298 protected void restoreFromExtras(Bundle extras) {
4299 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4300 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4301 mSummaryTextSet = true;
4302 }
4303 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4304 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4305 }
4306 }
4307
4308
4309 /**
4310 * @hide
4311 */
4312 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004313 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004314 return wip;
4315 }
4316
Daniel Sandler0ec46202015-06-24 01:27:05 -04004317 /**
4318 * @hide
4319 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004320 public void purgeResources() {}
4321
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004322 /**
4323 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4324 * attached to.
4325 *
4326 * @return the fully constructed Notification.
4327 */
4328 public Notification build() {
4329 checkBuilder();
4330 return mBuilder.build();
4331 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004332
4333 /**
4334 * @hide
4335 * @return true if the style positions the progress bar on the second line; false if the
4336 * style hides the progress bar
4337 */
4338 protected boolean hasProgress() {
4339 return true;
4340 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004341
4342 /**
4343 * @hide
4344 * @return Whether we should put the summary be put into the notification header
4345 */
4346 public boolean hasSummaryInHeader() {
4347 return true;
4348 }
Selim Cinek593610c2016-02-16 18:42:57 -08004349
4350 /**
4351 * @hide
4352 * @return Whether custom content views are displayed inline in the style
4353 */
4354 public boolean displayCustomViewInline() {
4355 return false;
4356 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004357 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004358
4359 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004360 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004361 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004362 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004363 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004364 * Notification notif = new Notification.Builder(mContext)
4365 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4366 * .setContentText(subject)
4367 * .setSmallIcon(R.drawable.new_post)
4368 * .setLargeIcon(aBitmap)
4369 * .setStyle(new Notification.BigPictureStyle()
4370 * .bigPicture(aBigBitmap))
4371 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004372 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004373 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004374 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004375 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004376 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004377 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004378 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004379 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004380
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004381 public BigPictureStyle() {
4382 }
4383
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004384 /**
4385 * @deprecated use {@code BigPictureStyle()}.
4386 */
4387 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004388 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004389 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004390 }
4391
Chris Wrend6297db2012-05-03 16:20:13 -04004392 /**
4393 * Overrides ContentTitle in the big form of the template.
4394 * This defaults to the value passed to setContentTitle().
4395 */
4396 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004397 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004398 return this;
4399 }
4400
4401 /**
4402 * Set the first line of text after the detail section in the big form of the template.
4403 */
4404 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004405 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004406 return this;
4407 }
4408
Chris Wren0bd664d2012-08-01 13:56:56 -04004409 /**
4410 * Provide the bitmap to be used as the payload for the BigPicture notification.
4411 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004412 public BigPictureStyle bigPicture(Bitmap b) {
4413 mPicture = b;
4414 return this;
4415 }
4416
Chris Wren3745a3d2012-05-22 15:11:52 -04004417 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004418 * Override the large icon when the big notification is shown.
4419 */
4420 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004421 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4422 }
4423
4424 /**
4425 * Override the large icon when the big notification is shown.
4426 */
4427 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004428 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004429 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004430 return this;
4431 }
4432
Riley Andrews0394a0c2015-11-03 23:36:52 -08004433 /** @hide */
4434 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4435
Daniel Sandler0ec46202015-06-24 01:27:05 -04004436 /**
4437 * @hide
4438 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004439 @Override
4440 public void purgeResources() {
4441 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004442 if (mPicture != null &&
4443 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004444 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004445 mPicture = mPicture.createAshmemBitmap();
4446 }
4447 if (mBigLargeIcon != null) {
4448 mBigLargeIcon.convertToAshmem();
4449 }
4450 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004451
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004452 /**
4453 * @hide
4454 */
4455 public RemoteViews makeBigContentView() {
4456 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004457 // This covers the following cases:
4458 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004459 // mN.mLargeIcon
4460 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004461 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07004462 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004463 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004464 oldLargeIcon = mBuilder.mN.mLargeIcon;
4465 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004466 // The legacy largeIcon might not allow us to clear the image, as it's taken in
4467 // replacement if the other one is null. Because we're restoring these legacy icons
4468 // for old listeners, this is in general non-null.
4469 largeIconLegacy = mBuilder.mN.largeIcon;
4470 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004471 }
4472
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004473 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004474 if (mSummaryTextSet) {
4475 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004476 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004477 }
Selim Cinek279fa862016-06-14 10:57:25 -07004478 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004479
Christoph Studer5c510ee2014-12-15 16:32:27 +01004480 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004481 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004482 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004483 }
4484
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004485 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004486 return contentView;
4487 }
4488
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004489 /**
4490 * @hide
4491 */
4492 public void addExtras(Bundle extras) {
4493 super.addExtras(extras);
4494
4495 if (mBigLargeIconSet) {
4496 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4497 }
4498 extras.putParcelable(EXTRA_PICTURE, mPicture);
4499 }
4500
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004501 /**
4502 * @hide
4503 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004504 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004505 protected void restoreFromExtras(Bundle extras) {
4506 super.restoreFromExtras(extras);
4507
4508 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004509 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004510 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004511 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004512 mPicture = extras.getParcelable(EXTRA_PICTURE);
4513 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004514
4515 /**
4516 * @hide
4517 */
4518 @Override
4519 public boolean hasSummaryInHeader() {
4520 return false;
4521 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004522 }
4523
4524 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004525 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004526 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004527 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004528 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004529 * Notification notif = new Notification.Builder(mContext)
4530 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4531 * .setContentText(subject)
4532 * .setSmallIcon(R.drawable.new_mail)
4533 * .setLargeIcon(aBitmap)
4534 * .setStyle(new Notification.BigTextStyle()
4535 * .bigText(aVeryLongString))
4536 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004537 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004538 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004539 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004540 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004541 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004542
4543 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004544 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004545
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004546 private CharSequence mBigText;
4547
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004548 public BigTextStyle() {
4549 }
4550
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004551 /**
4552 * @deprecated use {@code BigTextStyle()}.
4553 */
4554 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004555 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004556 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004557 }
4558
Chris Wrend6297db2012-05-03 16:20:13 -04004559 /**
4560 * Overrides ContentTitle in the big form of the template.
4561 * This defaults to the value passed to setContentTitle().
4562 */
4563 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004564 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004565 return this;
4566 }
4567
4568 /**
4569 * Set the first line of text after the detail section in the big form of the template.
4570 */
4571 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004572 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004573 return this;
4574 }
4575
Chris Wren0bd664d2012-08-01 13:56:56 -04004576 /**
4577 * Provide the longer text to be displayed in the big form of the
4578 * template in place of the content text.
4579 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004580 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004581 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004582 return this;
4583 }
4584
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004585 /**
4586 * @hide
4587 */
4588 public void addExtras(Bundle extras) {
4589 super.addExtras(extras);
4590
Christoph Studer4600f9b2014-07-22 22:44:43 +02004591 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4592 }
4593
4594 /**
4595 * @hide
4596 */
4597 @Override
4598 protected void restoreFromExtras(Bundle extras) {
4599 super.restoreFromExtras(extras);
4600
4601 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004602 }
4603
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004604 /**
4605 * @hide
4606 */
4607 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004608
4609 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004610 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004611 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004612
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004613 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004614
Selim Cinek75998782016-04-26 10:39:17 -07004615 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004616
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004617 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004618 if (TextUtils.isEmpty(bigTextText)) {
4619 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4620 // experience
4621 bigTextText = mBuilder.processLegacyText(text);
4622 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004623 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004624
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004625 return contentView;
4626 }
4627
Adrian Roosb1f427c2016-05-26 12:27:15 -07004628 static void applyBigTextContentView(Builder builder,
4629 RemoteViews contentView, CharSequence bigTextText) {
4630 contentView.setTextViewText(R.id.big_text, bigTextText);
4631 contentView.setViewVisibility(R.id.big_text,
4632 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4633 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004634 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004635 }
4636
4637 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004638 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004639 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004640 if (hasActions) {
4641 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4642 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004643 return lineCount;
4644 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004645 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004646
4647 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004648 * Helper class for generating large-format notifications that include multiple back-and-forth
4649 * messages of varying types between any number of people.
4650 *
4651 * <br>
4652 * If the platform does not provide large-format notifications, this method has no effect. The
4653 * user will always see the normal notification view.
4654 * <br>
4655 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4656 * so:
4657 * <pre class="prettyprint">
4658 *
4659 * Notification noti = new Notification.Builder()
4660 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4661 * .setContentText(subject)
4662 * .setSmallIcon(R.drawable.new_message)
4663 * .setLargeIcon(aBitmap)
4664 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4665 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4666 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4667 * .build();
4668 * </pre>
4669 */
4670 public static class MessagingStyle extends Style {
4671
4672 /**
4673 * The maximum number of messages that will be retained in the Notification itself (the
4674 * number displayed is up to the platform).
4675 */
4676 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4677
4678 CharSequence mUserDisplayName;
4679 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004680 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004681
4682 MessagingStyle() {
4683 }
4684
4685 /**
4686 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4687 * posting app reposts the notification with those messages after they've been actually
4688 * sent and in previous messages sent by the user added in
4689 * {@link #addMessage(Notification.MessagingStyle.Message)}
4690 */
4691 public MessagingStyle(CharSequence userDisplayName) {
4692 mUserDisplayName = userDisplayName;
4693 }
4694
4695 /**
4696 * Returns the name to be displayed for any replies sent by the user
4697 */
4698 public CharSequence getUserDisplayName() {
4699 return mUserDisplayName;
4700 }
4701
4702 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004703 * Sets the title to be displayed on this conversation. This should only be used for
4704 * group messaging and left unset for one-on-one conversations.
4705 * @param conversationTitle
4706 * @return this object for method chaining.
4707 */
4708 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4709 mConversationTitle = conversationTitle;
4710 return this;
4711 }
4712
4713 /**
4714 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4715 * should be for one-on-one conversations
4716 */
4717 public CharSequence getConversationTitle() {
4718 return mConversationTitle;
4719 }
4720
4721 /**
4722 * Adds a message for display by this notification. Convenience call for a simple
4723 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4724 * @param text A {@link CharSequence} to be displayed as the message content
4725 * @param timestamp Time at which the message arrived
4726 * @param sender A {@link CharSequence} to be used for displaying the name of the
4727 * sender. Should be <code>null</code> for messages by the current user, in which case
4728 * the platform will insert {@link #getUserDisplayName()}.
4729 * Should be unique amongst all individuals in the conversation, and should be
4730 * consistent during re-posts of the notification.
4731 *
4732 * @see Message#Message(CharSequence, long, CharSequence)
4733 *
4734 * @return this object for method chaining
4735 */
4736 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4737 mMessages.add(new Message(text, timestamp, sender));
4738 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4739 mMessages.remove(0);
4740 }
4741 return this;
4742 }
4743
4744 /**
4745 * Adds a {@link Message} for display in this notification.
4746 * @param message The {@link Message} to be displayed
4747 * @return this object for method chaining
4748 */
4749 public MessagingStyle addMessage(Message message) {
4750 mMessages.add(message);
4751 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4752 mMessages.remove(0);
4753 }
4754 return this;
4755 }
4756
4757 /**
4758 * Gets the list of {@code Message} objects that represent the notification
4759 */
4760 public List<Message> getMessages() {
4761 return mMessages;
4762 }
4763
4764 /**
4765 * @hide
4766 */
4767 @Override
4768 public void addExtras(Bundle extras) {
4769 super.addExtras(extras);
4770 if (mUserDisplayName != null) {
4771 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4772 }
4773 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004774 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004775 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004776 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4777 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004778 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004779
4780 fixTitleAndTextExtras(extras);
4781 }
4782
4783 private void fixTitleAndTextExtras(Bundle extras) {
4784 Message m = findLatestIncomingMessage();
4785 CharSequence text = (m == null) ? null : m.mText;
4786 CharSequence sender = m == null ? null
4787 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4788 CharSequence title;
4789 if (!TextUtils.isEmpty(mConversationTitle)) {
4790 if (!TextUtils.isEmpty(sender)) {
4791 BidiFormatter bidi = BidiFormatter.getInstance();
4792 title = mBuilder.mContext.getString(
4793 com.android.internal.R.string.notification_messaging_title_template,
4794 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4795 } else {
4796 title = mConversationTitle;
4797 }
4798 } else {
4799 title = sender;
4800 }
4801
4802 if (title != null) {
4803 extras.putCharSequence(EXTRA_TITLE, title);
4804 }
4805 if (text != null) {
4806 extras.putCharSequence(EXTRA_TEXT, text);
4807 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004808 }
4809
4810 /**
4811 * @hide
4812 */
4813 @Override
4814 protected void restoreFromExtras(Bundle extras) {
4815 super.restoreFromExtras(extras);
4816
4817 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004818 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4819 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004820 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004821 if (parcelables != null && parcelables instanceof Parcelable[]) {
4822 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004823 }
4824 }
4825
4826 /**
4827 * @hide
4828 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004829 @Override
4830 public RemoteViews makeContentView() {
4831 Message m = findLatestIncomingMessage();
4832 CharSequence title = mConversationTitle != null
4833 ? mConversationTitle
4834 : (m == null) ? null : m.mSender;
4835 CharSequence text = (m == null)
4836 ? null
4837 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004838
Adrian Roosc1a80b02016-04-05 14:54:55 -07004839 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4840 false /* hasProgress */,
4841 title,
4842 text);
4843 }
4844
4845 private Message findLatestIncomingMessage() {
4846 for (int i = mMessages.size() - 1; i >= 0; i--) {
4847 Message m = mMessages.get(i);
4848 // Incoming messages have a non-empty sender.
4849 if (!TextUtils.isEmpty(m.mSender)) {
4850 return m;
4851 }
4852 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004853 if (!mMessages.isEmpty()) {
4854 // No incoming messages, fall back to outgoing message
4855 return mMessages.get(mMessages.size() - 1);
4856 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004857 return null;
4858 }
4859
4860 /**
4861 * @hide
4862 */
4863 @Override
4864 public RemoteViews makeBigContentView() {
4865 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4866 ? super.mBigContentTitle
4867 : mConversationTitle;
4868 boolean hasTitle = !TextUtils.isEmpty(title);
4869
Adrian Roosfeafa052016-06-01 17:09:45 -07004870 if (mMessages.size() == 1) {
4871 // Special case for a single message: Use the big text style
4872 // so the collapsed and expanded versions match nicely.
4873 CharSequence bigTitle;
4874 CharSequence text;
4875 if (hasTitle) {
4876 bigTitle = title;
4877 text = makeMessageLine(mMessages.get(0));
4878 } else {
4879 bigTitle = mMessages.get(0).mSender;
4880 text = mMessages.get(0).mText;
4881 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004882 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4883 mBuilder.getBigTextLayoutResource(),
Adrian Roosfeafa052016-06-01 17:09:45 -07004884 false /* progress */, bigTitle, null /* text */);
Adrian Roosb1f427c2016-05-26 12:27:15 -07004885 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4886 return contentView;
4887 }
4888
Adrian Roos48d746a2016-04-12 14:57:28 -07004889 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004890 mBuilder.getMessagingLayoutResource(),
4891 false /* hasProgress */,
4892 title,
4893 null /* text */);
4894
4895 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4896 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4897
4898 // Make sure all rows are gone in case we reuse a view.
4899 for (int rowId : rowIds) {
4900 contentView.setViewVisibility(rowId, View.GONE);
4901 }
4902
4903 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004904 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
4905 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004906 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07004907 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004908
Adrian Roosfeafa052016-06-01 17:09:45 -07004909 int contractedChildId = View.NO_ID;
4910 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07004911 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4912 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4913 Message m = mMessages.get(firstMessage + i);
4914 int rowId = rowIds[i];
4915
4916 contentView.setViewVisibility(rowId, View.VISIBLE);
4917 contentView.setTextViewText(rowId, makeMessageLine(m));
4918
Adrian Roosfeafa052016-06-01 17:09:45 -07004919 if (contractedMessage == m) {
4920 contractedChildId = rowId;
4921 }
4922
Adrian Roosc1a80b02016-04-05 14:54:55 -07004923 i++;
4924 }
Adrian Roosfeafa052016-06-01 17:09:45 -07004925 // Record this here to allow transformation between the contracted and expanded views.
4926 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
4927 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04004928 return contentView;
4929 }
4930
Adrian Roosc1a80b02016-04-05 14:54:55 -07004931 private CharSequence makeMessageLine(Message m) {
4932 BidiFormatter bidi = BidiFormatter.getInstance();
4933 SpannableStringBuilder sb = new SpannableStringBuilder();
4934 if (TextUtils.isEmpty(m.mSender)) {
4935 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4936 sb.append(bidi.unicodeWrap(replyName),
4937 makeFontColorSpan(mBuilder.resolveContrastColor()),
4938 0 /* flags */);
4939 } else {
4940 sb.append(bidi.unicodeWrap(m.mSender),
4941 makeFontColorSpan(Color.BLACK),
4942 0 /* flags */);
4943 }
4944 CharSequence text = m.mText == null ? "" : m.mText;
4945 sb.append(" ").append(bidi.unicodeWrap(text));
4946 return sb;
4947 }
4948
Adrian Roosdedd1df2016-04-26 16:38:47 -07004949 /**
4950 * @hide
4951 */
4952 @Override
4953 public RemoteViews makeHeadsUpContentView() {
4954 Message m = findLatestIncomingMessage();
4955 CharSequence title = mConversationTitle != null
4956 ? mConversationTitle
4957 : (m == null) ? null : m.mSender;
4958 CharSequence text = (m == null)
4959 ? null
4960 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4961
4962 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4963 false /* hasProgress */,
4964 title,
4965 text);
4966 }
4967
Adrian Roosc1a80b02016-04-05 14:54:55 -07004968 private static TextAppearanceSpan makeFontColorSpan(int color) {
4969 return new TextAppearanceSpan(null, 0, 0,
4970 ColorStateList.valueOf(color), null);
4971 }
4972
Alex Hillsd9b04d92016-04-11 16:38:16 -04004973 public static final class Message {
4974
4975 static final String KEY_TEXT = "text";
4976 static final String KEY_TIMESTAMP = "time";
4977 static final String KEY_SENDER = "sender";
4978 static final String KEY_DATA_MIME_TYPE = "type";
4979 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004980
4981 private final CharSequence mText;
4982 private final long mTimestamp;
4983 private final CharSequence mSender;
4984
4985 private String mDataMimeType;
4986 private Uri mDataUri;
4987
4988 /**
4989 * Constructor
4990 * @param text A {@link CharSequence} to be displayed as the message content
4991 * @param timestamp Time at which the message arrived
4992 * @param sender A {@link CharSequence} to be used for displaying the name of the
4993 * sender. Should be <code>null</code> for messages by the current user, in which case
4994 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4995 * Should be unique amongst all individuals in the conversation, and should be
4996 * consistent during re-posts of the notification.
4997 */
4998 public Message(CharSequence text, long timestamp, CharSequence sender){
4999 mText = text;
5000 mTimestamp = timestamp;
5001 mSender = sender;
5002 }
5003
5004 /**
5005 * Sets a binary blob of data and an associated MIME type for a message. In the case
5006 * where the platform doesn't support the MIME type, the original text provided in the
5007 * constructor will be used.
5008 * @param dataMimeType The MIME type of the content. See
5009 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5010 * types on Android and Android Wear.
5011 * @param dataUri The uri containing the content whose type is given by the MIME type.
5012 * <p class="note">
5013 * <ol>
5014 * <li>Notification Listeners including the System UI need permission to access the
5015 * data the Uri points to. The recommended ways to do this are:</li>
5016 * <li>Store the data in your own ContentProvider, making sure that other apps have
5017 * the correct permission to access your provider. The preferred mechanism for
5018 * providing access is to use per-URI permissions which are temporary and only
5019 * grant access to the receiving application. An easy way to create a
5020 * ContentProvider like this is to use the FileProvider helper class.</li>
5021 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5022 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5023 * also store non-media types (see MediaStore.Files for more info). Files can be
5024 * inserted into the MediaStore using scanFile() after which a content:// style
5025 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5026 * Note that once added to the system MediaStore the content is accessible to any
5027 * app on the device.</li>
5028 * </ol>
5029 * @return this object for method chaining
5030 */
5031 public Message setData(String dataMimeType, Uri dataUri) {
5032 mDataMimeType = dataMimeType;
5033 mDataUri = dataUri;
5034 return this;
5035 }
5036
Alex Hillsfc737de2016-03-23 17:33:02 -04005037 /**
5038 * Get the text to be used for this message, or the fallback text if a type and content
5039 * Uri have been set
5040 */
5041 public CharSequence getText() {
5042 return mText;
5043 }
5044
5045 /**
5046 * Get the time at which this message arrived
5047 */
5048 public long getTimestamp() {
5049 return mTimestamp;
5050 }
5051
5052 /**
5053 * Get the text used to display the contact's name in the messaging experience
5054 */
5055 public CharSequence getSender() {
5056 return mSender;
5057 }
5058
5059 /**
5060 * Get the MIME type of the data pointed to by the Uri
5061 */
5062 public String getDataMimeType() {
5063 return mDataMimeType;
5064 }
5065
5066 /**
5067 * Get the the Uri pointing to the content of the message. Can be null, in which case
5068 * {@see #getText()} is used.
5069 */
5070 public Uri getDataUri() {
5071 return mDataUri;
5072 }
5073
Alex Hillsd9b04d92016-04-11 16:38:16 -04005074 private Bundle toBundle() {
5075 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005076 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005077 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005078 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005079 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005080 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005081 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005082 }
5083 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005084 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005085 }
5086 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005087 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005088 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005089 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005090 }
5091
Alex Hillsd9b04d92016-04-11 16:38:16 -04005092 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5093 Bundle[] bundles = new Bundle[messages.size()];
5094 final int N = messages.size();
5095 for (int i = 0; i < N; i++) {
5096 bundles[i] = messages.get(i).toBundle();
5097 }
5098 return bundles;
5099 }
5100
Adrian Roosdedd1df2016-04-26 16:38:47 -07005101 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005102 List<Message> messages = new ArrayList<>(bundles.length);
5103 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005104 if (bundles[i] instanceof Bundle) {
5105 Message message = getMessageFromBundle((Bundle)bundles[i]);
5106 if (message != null) {
5107 messages.add(message);
5108 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005109 }
5110 }
5111 return messages;
5112 }
5113
5114 static Message getMessageFromBundle(Bundle bundle) {
5115 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005116 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005117 return null;
5118 } else {
5119 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5120 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5121 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5122 bundle.containsKey(KEY_DATA_URI)) {
5123
5124 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5125 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005126 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005127 return message;
5128 }
5129 } catch (ClassCastException e) {
5130 return null;
5131 }
5132 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005133 }
5134 }
5135
5136 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005137 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005138 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005139 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005140 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005141 * Notification notif = new Notification.Builder(mContext)
5142 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5143 * .setContentText(subject)
5144 * .setSmallIcon(R.drawable.new_mail)
5145 * .setLargeIcon(aBitmap)
5146 * .setStyle(new Notification.InboxStyle()
5147 * .addLine(str1)
5148 * .addLine(str2)
5149 * .setContentTitle(&quot;&quot;)
5150 * .setSummaryText(&quot;+3 more&quot;))
5151 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005152 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005153 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005154 * @see Notification#bigContentView
5155 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005156 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005157 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5158
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005159 public InboxStyle() {
5160 }
5161
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005162 /**
5163 * @deprecated use {@code InboxStyle()}.
5164 */
5165 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005166 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005167 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005168 }
5169
Chris Wrend6297db2012-05-03 16:20:13 -04005170 /**
5171 * Overrides ContentTitle in the big form of the template.
5172 * This defaults to the value passed to setContentTitle().
5173 */
5174 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005175 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005176 return this;
5177 }
5178
5179 /**
5180 * Set the first line of text after the detail section in the big form of the template.
5181 */
5182 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005183 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005184 return this;
5185 }
5186
Chris Wren0bd664d2012-08-01 13:56:56 -04005187 /**
5188 * Append a line to the digest section of the Inbox notification.
5189 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005190 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005191 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005192 return this;
5193 }
5194
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005195 /**
5196 * @hide
5197 */
5198 public void addExtras(Bundle extras) {
5199 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005200
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005201 CharSequence[] a = new CharSequence[mTexts.size()];
5202 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5203 }
5204
Christoph Studer4600f9b2014-07-22 22:44:43 +02005205 /**
5206 * @hide
5207 */
5208 @Override
5209 protected void restoreFromExtras(Bundle extras) {
5210 super.restoreFromExtras(extras);
5211
5212 mTexts.clear();
5213 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5214 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5215 }
5216 }
5217
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005218 /**
5219 * @hide
5220 */
5221 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005222 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005223 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005224 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5225 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005226
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005227 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005228
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005229 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005230
Chris Wrend6297db2012-05-03 16:20:13 -04005231 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 -04005232 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005233
Chris Wren4ed80d52012-05-17 09:30:03 -04005234 // Make sure all rows are gone in case we reuse a view.
5235 for (int rowId : rowIds) {
5236 contentView.setViewVisibility(rowId, View.GONE);
5237 }
5238
Daniel Sandler879c5e02012-04-17 16:46:51 -04005239 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005240 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5241 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005242 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005243 int onlyViewId = 0;
5244 int maxRows = rowIds.length;
5245 if (mBuilder.mActions.size() > 0) {
5246 maxRows--;
5247 }
5248 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005249 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005250 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005251 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005252 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005253 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005254 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005255 if (first) {
5256 onlyViewId = rowIds[i];
5257 } else {
5258 onlyViewId = 0;
5259 }
Selim Cinek247fa012016-02-18 09:50:48 -08005260 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005261 }
5262 i++;
5263 }
Selim Cinek07c80172016-04-21 16:40:47 -07005264 if (onlyViewId != 0) {
5265 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5266 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5267 R.dimen.notification_text_margin_top);
5268 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5269 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005270
Daniel Sandler879c5e02012-04-17 16:46:51 -04005271 return contentView;
5272 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005273
Selim Cinek247fa012016-02-18 09:50:48 -08005274 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005275 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005276 if (first) {
5277 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5278 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5279 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005280 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005281 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005282 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005283 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005284 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005285 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005286 }
Dan Sandler842dd772014-05-15 09:36:47 -04005287
5288 /**
5289 * Notification style for media playback notifications.
5290 *
5291 * In the expanded form, {@link Notification#bigContentView}, up to 5
5292 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005293 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005294 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5295 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5296 * treated as album artwork.
5297 *
5298 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5299 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005300 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005301 * in the standard view alongside the usual content.
5302 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005303 * Notifications created with MediaStyle will have their category set to
5304 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5305 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5306 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005307 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5308 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005309 * the System UI can identify this as a notification representing an active media session
5310 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5311 *
5312 * To use this style with your Notification, feed it to
5313 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5314 * <pre class="prettyprint">
5315 * Notification noti = new Notification.Builder()
5316 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005317 * .setContentTitle(&quot;Track title&quot;)
5318 * .setContentText(&quot;Artist - Album&quot;)
5319 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005320 * .setStyle(<b>new Notification.MediaStyle()</b>
5321 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005322 * .build();
5323 * </pre>
5324 *
5325 * @see Notification#bigContentView
5326 */
5327 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005328 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005329 static final int MAX_MEDIA_BUTTONS = 5;
5330
5331 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005332 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005333
5334 public MediaStyle() {
5335 }
5336
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005337 /**
5338 * @deprecated use {@code MediaStyle()}.
5339 */
5340 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005341 public MediaStyle(Builder builder) {
5342 setBuilder(builder);
5343 }
5344
5345 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005346 * 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 -04005347 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005348 *
5349 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005350 */
5351 public MediaStyle setShowActionsInCompactView(int...actions) {
5352 mActionsToShowInCompact = actions;
5353 return this;
5354 }
5355
5356 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005357 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5358 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005359 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005360 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005361 mToken = token;
5362 return this;
5363 }
5364
Christoph Studer4600f9b2014-07-22 22:44:43 +02005365 /**
5366 * @hide
5367 */
Dan Sandler842dd772014-05-15 09:36:47 -04005368 @Override
5369 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005370 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005371 if (wip.category == null) {
5372 wip.category = Notification.CATEGORY_TRANSPORT;
5373 }
Dan Sandler842dd772014-05-15 09:36:47 -04005374 return wip;
5375 }
5376
Christoph Studer4600f9b2014-07-22 22:44:43 +02005377 /**
5378 * @hide
5379 */
5380 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005381 public RemoteViews makeContentView() {
5382 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005383 }
5384
5385 /**
5386 * @hide
5387 */
5388 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005389 public RemoteViews makeBigContentView() {
5390 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005391 }
5392
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005393 /**
5394 * @hide
5395 */
5396 @Override
5397 public RemoteViews makeHeadsUpContentView() {
5398 RemoteViews expanded = makeMediaBigContentView();
5399 return expanded != null ? expanded : makeMediaContentView();
5400 }
5401
Dan Sandler842dd772014-05-15 09:36:47 -04005402 /** @hide */
5403 @Override
5404 public void addExtras(Bundle extras) {
5405 super.addExtras(extras);
5406
5407 if (mToken != null) {
5408 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5409 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005410 if (mActionsToShowInCompact != null) {
5411 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5412 }
Dan Sandler842dd772014-05-15 09:36:47 -04005413 }
5414
Christoph Studer4600f9b2014-07-22 22:44:43 +02005415 /**
5416 * @hide
5417 */
5418 @Override
5419 protected void restoreFromExtras(Bundle extras) {
5420 super.restoreFromExtras(extras);
5421
5422 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5423 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5424 }
5425 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5426 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5427 }
5428 }
5429
Selim Cinek5bf069a2015-11-10 19:14:27 -05005430 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005431 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005432 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005433 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005434 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005435 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5436 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005437 if (!tombstone) {
5438 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5439 }
5440 button.setContentDescription(R.id.action0, action.title);
5441 return button;
5442 }
5443
5444 private RemoteViews makeMediaContentView() {
5445 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005446 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005447
5448 final int numActions = mBuilder.mActions.size();
5449 final int N = mActionsToShowInCompact == null
5450 ? 0
5451 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5452 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005453 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005454 for (int i = 0; i < N; i++) {
5455 if (i >= numActions) {
5456 throw new IllegalArgumentException(String.format(
5457 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5458 i, numActions - 1));
5459 }
5460
5461 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005462 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005463 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005464 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005465 }
5466 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005467 handleImage(view);
5468 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005469 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005470 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005471 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005472 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005473 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005474 return view;
5475 }
5476
5477 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005478 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005479 // Dont add an expanded view if there is no more content to be revealed
5480 int actionsInCompact = mActionsToShowInCompact == null
5481 ? 0
5482 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005483 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005484 return null;
5485 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005486 RemoteViews big = mBuilder.applyStandardTemplate(
5487 R.layout.notification_template_material_big_media,
5488 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005489
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005490 if (actionCount > 0) {
5491 big.removeAllViews(com.android.internal.R.id.media_actions);
5492 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005493 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005494 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005495 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005496 }
5497 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005498 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005499 return big;
5500 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005501
Selim Cinek5bf069a2015-11-10 19:14:27 -05005502 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005503 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005504 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5505 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005506 }
5507 }
5508
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005509 /**
5510 * @hide
5511 */
5512 @Override
5513 protected boolean hasProgress() {
5514 return false;
5515 }
Dan Sandler842dd772014-05-15 09:36:47 -04005516 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005517
Selim Cinek593610c2016-02-16 18:42:57 -08005518 /**
5519 * Notification style for custom views that are decorated by the system
5520 *
5521 * <p>Instead of providing a notification that is completely custom, a developer can set this
5522 * style and still obtain system decorations like the notification header with the expand
5523 * affordance and actions.
5524 *
5525 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5526 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5527 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5528 * corresponding custom views to display.
5529 *
5530 * To use this style with your Notification, feed it to
5531 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5532 * <pre class="prettyprint">
5533 * Notification noti = new Notification.Builder()
5534 * .setSmallIcon(R.drawable.ic_stat_player)
5535 * .setLargeIcon(albumArtBitmap))
5536 * .setCustomContentView(contentView);
5537 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5538 * .build();
5539 * </pre>
5540 */
5541 public static class DecoratedCustomViewStyle extends Style {
5542
5543 public DecoratedCustomViewStyle() {
5544 }
5545
Selim Cinek593610c2016-02-16 18:42:57 -08005546 /**
5547 * @hide
5548 */
5549 public boolean displayCustomViewInline() {
5550 return true;
5551 }
5552
5553 /**
5554 * @hide
5555 */
5556 @Override
5557 public RemoteViews makeContentView() {
5558 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5559 }
5560
5561 /**
5562 * @hide
5563 */
5564 @Override
5565 public RemoteViews makeBigContentView() {
5566 return makeDecoratedBigContentView();
5567 }
5568
5569 /**
5570 * @hide
5571 */
5572 @Override
5573 public RemoteViews makeHeadsUpContentView() {
5574 return makeDecoratedHeadsUpContentView();
5575 }
5576
Selim Cinek593610c2016-02-16 18:42:57 -08005577 private RemoteViews makeDecoratedHeadsUpContentView() {
5578 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5579 ? mBuilder.mN.contentView
5580 : mBuilder.mN.headsUpContentView;
5581 if (mBuilder.mActions.size() == 0) {
5582 return makeStandardTemplateWithCustomContent(headsUpContentView);
5583 }
5584 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5585 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005586 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005587 return remoteViews;
5588 }
5589
Selim Cinek593610c2016-02-16 18:42:57 -08005590 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5591 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5592 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005593 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005594 return remoteViews;
5595 }
5596
Selim Cinek593610c2016-02-16 18:42:57 -08005597 private RemoteViews makeDecoratedBigContentView() {
5598 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5599 ? mBuilder.mN.contentView
5600 : mBuilder.mN.bigContentView;
5601 if (mBuilder.mActions.size() == 0) {
5602 return makeStandardTemplateWithCustomContent(bigContentView);
5603 }
5604 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5605 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005606 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005607 return remoteViews;
5608 }
Selim Cinek247fa012016-02-18 09:50:48 -08005609
5610 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5611 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005612 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005613 // Need to clone customContent before adding, because otherwise it can no longer be
5614 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005615 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005616 remoteViews.removeAllViews(R.id.notification_main_column);
5617 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005618 }
Selim Cinek247fa012016-02-18 09:50:48 -08005619 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005620 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005621 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005622 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005623 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005624 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005625 }
Selim Cinek593610c2016-02-16 18:42:57 -08005626 }
5627
Selim Cinek03eb3b72016-02-18 10:39:45 -08005628 /**
5629 * Notification style for media custom views that are decorated by the system
5630 *
5631 * <p>Instead of providing a media notification that is completely custom, a developer can set
5632 * this style and still obtain system decorations like the notification header with the expand
5633 * affordance and actions.
5634 *
5635 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5636 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5637 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5638 * corresponding custom views to display.
5639 *
5640 * To use this style with your Notification, feed it to
5641 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5642 * <pre class="prettyprint">
5643 * Notification noti = new Notification.Builder()
5644 * .setSmallIcon(R.drawable.ic_stat_player)
5645 * .setLargeIcon(albumArtBitmap))
5646 * .setCustomContentView(contentView);
5647 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5648 * .setMediaSession(mySession))
5649 * .build();
5650 * </pre>
5651 *
5652 * @see android.app.Notification.DecoratedCustomViewStyle
5653 * @see android.app.Notification.MediaStyle
5654 */
5655 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5656
5657 public DecoratedMediaCustomViewStyle() {
5658 }
5659
Selim Cinek03eb3b72016-02-18 10:39:45 -08005660 /**
5661 * @hide
5662 */
5663 public boolean displayCustomViewInline() {
5664 return true;
5665 }
5666
5667 /**
5668 * @hide
5669 */
5670 @Override
5671 public RemoteViews makeContentView() {
5672 RemoteViews remoteViews = super.makeContentView();
5673 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5674 mBuilder.mN.contentView);
5675 }
5676
5677 /**
5678 * @hide
5679 */
5680 @Override
5681 public RemoteViews makeBigContentView() {
5682 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5683 ? mBuilder.mN.bigContentView
5684 : mBuilder.mN.contentView;
5685 return makeBigContentViewWithCustomContent(customRemoteView);
5686 }
5687
5688 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5689 RemoteViews remoteViews = super.makeBigContentView();
5690 if (remoteViews != null) {
5691 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5692 customRemoteView);
5693 } else if (customRemoteView != mBuilder.mN.contentView){
5694 remoteViews = super.makeContentView();
5695 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5696 customRemoteView);
5697 } else {
5698 return null;
5699 }
5700 }
5701
5702 /**
5703 * @hide
5704 */
5705 @Override
5706 public RemoteViews makeHeadsUpContentView() {
5707 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5708 ? mBuilder.mN.headsUpContentView
5709 : mBuilder.mN.contentView;
5710 return makeBigContentViewWithCustomContent(customRemoteView);
5711 }
5712
5713 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5714 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005715 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005716 // Need to clone customContent before adding, because otherwise it can no longer be
5717 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005718 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005719 remoteViews.removeAllViews(id);
5720 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005721 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005722 return remoteViews;
5723 }
5724 }
5725
Christoph Studer4600f9b2014-07-22 22:44:43 +02005726 // When adding a new Style subclass here, don't forget to update
5727 // Builder.getNotificationStyleClass.
5728
Griff Hazen61a9e862014-05-22 16:05:19 -07005729 /**
5730 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5731 * metadata or change options on a notification builder.
5732 */
5733 public interface Extender {
5734 /**
5735 * Apply this extender to a notification builder.
5736 * @param builder the builder to be modified.
5737 * @return the build object for chaining.
5738 */
5739 public Builder extend(Builder builder);
5740 }
5741
5742 /**
5743 * Helper class to add wearable extensions to notifications.
5744 * <p class="note"> See
5745 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5746 * for Android Wear</a> for more information on how to use this class.
5747 * <p>
5748 * To create a notification with wearable extensions:
5749 * <ol>
5750 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5751 * properties.
5752 * <li>Create a {@link android.app.Notification.WearableExtender}.
5753 * <li>Set wearable-specific properties using the
5754 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5755 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5756 * notification.
5757 * <li>Post the notification to the notification system with the
5758 * {@code NotificationManager.notify(...)} methods.
5759 * </ol>
5760 *
5761 * <pre class="prettyprint">
5762 * Notification notif = new Notification.Builder(mContext)
5763 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5764 * .setContentText(subject)
5765 * .setSmallIcon(R.drawable.new_mail)
5766 * .extend(new Notification.WearableExtender()
5767 * .setContentIcon(R.drawable.new_mail))
5768 * .build();
5769 * NotificationManager notificationManger =
5770 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5771 * notificationManger.notify(0, notif);</pre>
5772 *
5773 * <p>Wearable extensions can be accessed on an existing notification by using the
5774 * {@code WearableExtender(Notification)} constructor,
5775 * and then using the {@code get} methods to access values.
5776 *
5777 * <pre class="prettyprint">
5778 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5779 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005780 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005781 */
5782 public static final class WearableExtender implements Extender {
5783 /**
5784 * Sentinel value for an action index that is unset.
5785 */
5786 public static final int UNSET_ACTION_INDEX = -1;
5787
5788 /**
5789 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5790 * default sizing.
5791 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005792 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005793 * on their content.
5794 */
5795 public static final int SIZE_DEFAULT = 0;
5796
5797 /**
5798 * Size value for use with {@link #setCustomSizePreset} to show this notification
5799 * with an extra small size.
5800 * <p>This value is only applicable for custom display notifications created using
5801 * {@link #setDisplayIntent}.
5802 */
5803 public static final int SIZE_XSMALL = 1;
5804
5805 /**
5806 * Size value for use with {@link #setCustomSizePreset} to show this notification
5807 * with a small size.
5808 * <p>This value is only applicable for custom display notifications created using
5809 * {@link #setDisplayIntent}.
5810 */
5811 public static final int SIZE_SMALL = 2;
5812
5813 /**
5814 * Size value for use with {@link #setCustomSizePreset} to show this notification
5815 * with a medium size.
5816 * <p>This value is only applicable for custom display notifications created using
5817 * {@link #setDisplayIntent}.
5818 */
5819 public static final int SIZE_MEDIUM = 3;
5820
5821 /**
5822 * Size value for use with {@link #setCustomSizePreset} to show this notification
5823 * with a large size.
5824 * <p>This value is only applicable for custom display notifications created using
5825 * {@link #setDisplayIntent}.
5826 */
5827 public static final int SIZE_LARGE = 4;
5828
Griff Hazend5f11f92014-05-27 15:40:09 -07005829 /**
5830 * Size value for use with {@link #setCustomSizePreset} to show this notification
5831 * full screen.
5832 * <p>This value is only applicable for custom display notifications created using
5833 * {@link #setDisplayIntent}.
5834 */
5835 public static final int SIZE_FULL_SCREEN = 5;
5836
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005837 /**
5838 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5839 * short amount of time when this notification is displayed on the screen. This
5840 * is the default value.
5841 */
5842 public static final int SCREEN_TIMEOUT_SHORT = 0;
5843
5844 /**
5845 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5846 * for a longer amount of time when this notification is displayed on the screen.
5847 */
5848 public static final int SCREEN_TIMEOUT_LONG = -1;
5849
Griff Hazen61a9e862014-05-22 16:05:19 -07005850 /** Notification extra which contains wearable extensions */
5851 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5852
Pete Gastaf6781d2014-10-07 15:17:05 -04005853 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005854 private static final String KEY_ACTIONS = "actions";
5855 private static final String KEY_FLAGS = "flags";
5856 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5857 private static final String KEY_PAGES = "pages";
5858 private static final String KEY_BACKGROUND = "background";
5859 private static final String KEY_CONTENT_ICON = "contentIcon";
5860 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5861 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5862 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5863 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5864 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005865 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005866 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005867 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07005868
5869 // Flags bitwise-ored to mFlags
5870 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5871 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5872 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5873 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005874 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005875 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005876 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005877
5878 // Default value for flags integer
5879 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5880
5881 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5882 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5883
5884 private ArrayList<Action> mActions = new ArrayList<Action>();
5885 private int mFlags = DEFAULT_FLAGS;
5886 private PendingIntent mDisplayIntent;
5887 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5888 private Bitmap mBackground;
5889 private int mContentIcon;
5890 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5891 private int mContentActionIndex = UNSET_ACTION_INDEX;
5892 private int mCustomSizePreset = SIZE_DEFAULT;
5893 private int mCustomContentHeight;
5894 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005895 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005896 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005897 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07005898
5899 /**
5900 * Create a {@link android.app.Notification.WearableExtender} with default
5901 * options.
5902 */
5903 public WearableExtender() {
5904 }
5905
5906 public WearableExtender(Notification notif) {
5907 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5908 if (wearableBundle != null) {
5909 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5910 if (actions != null) {
5911 mActions.addAll(actions);
5912 }
5913
5914 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5915 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5916
5917 Notification[] pages = getNotificationArrayFromBundle(
5918 wearableBundle, KEY_PAGES);
5919 if (pages != null) {
5920 Collections.addAll(mPages, pages);
5921 }
5922
5923 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5924 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5925 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5926 DEFAULT_CONTENT_ICON_GRAVITY);
5927 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5928 UNSET_ACTION_INDEX);
5929 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5930 SIZE_DEFAULT);
5931 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5932 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005933 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005934 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005935 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07005936 }
5937 }
5938
5939 /**
5940 * Apply wearable extensions to a notification that is being built. This is typically
5941 * called by the {@link android.app.Notification.Builder#extend} method of
5942 * {@link android.app.Notification.Builder}.
5943 */
5944 @Override
5945 public Notification.Builder extend(Notification.Builder builder) {
5946 Bundle wearableBundle = new Bundle();
5947
5948 if (!mActions.isEmpty()) {
5949 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5950 }
5951 if (mFlags != DEFAULT_FLAGS) {
5952 wearableBundle.putInt(KEY_FLAGS, mFlags);
5953 }
5954 if (mDisplayIntent != null) {
5955 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5956 }
5957 if (!mPages.isEmpty()) {
5958 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5959 new Notification[mPages.size()]));
5960 }
5961 if (mBackground != null) {
5962 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5963 }
5964 if (mContentIcon != 0) {
5965 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5966 }
5967 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5968 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5969 }
5970 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5971 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5972 mContentActionIndex);
5973 }
5974 if (mCustomSizePreset != SIZE_DEFAULT) {
5975 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5976 }
5977 if (mCustomContentHeight != 0) {
5978 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5979 }
5980 if (mGravity != DEFAULT_GRAVITY) {
5981 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5982 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005983 if (mHintScreenTimeout != 0) {
5984 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5985 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005986 if (mDismissalId != null) {
5987 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5988 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005989 if (mBridgeTag != null) {
5990 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
5991 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005992
5993 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5994 return builder;
5995 }
5996
5997 @Override
5998 public WearableExtender clone() {
5999 WearableExtender that = new WearableExtender();
6000 that.mActions = new ArrayList<Action>(this.mActions);
6001 that.mFlags = this.mFlags;
6002 that.mDisplayIntent = this.mDisplayIntent;
6003 that.mPages = new ArrayList<Notification>(this.mPages);
6004 that.mBackground = this.mBackground;
6005 that.mContentIcon = this.mContentIcon;
6006 that.mContentIconGravity = this.mContentIconGravity;
6007 that.mContentActionIndex = this.mContentActionIndex;
6008 that.mCustomSizePreset = this.mCustomSizePreset;
6009 that.mCustomContentHeight = this.mCustomContentHeight;
6010 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006011 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006012 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006013 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006014 return that;
6015 }
6016
6017 /**
6018 * Add a wearable action to this notification.
6019 *
6020 * <p>When wearable actions are added using this method, the set of actions that
6021 * show on a wearable device splits from devices that only show actions added
6022 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6023 * of which actions display on different devices.
6024 *
6025 * @param action the action to add to this notification
6026 * @return this object for method chaining
6027 * @see android.app.Notification.Action
6028 */
6029 public WearableExtender addAction(Action action) {
6030 mActions.add(action);
6031 return this;
6032 }
6033
6034 /**
6035 * Adds wearable actions to this notification.
6036 *
6037 * <p>When wearable actions are added using this method, the set of actions that
6038 * show on a wearable device splits from devices that only show actions added
6039 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6040 * of which actions display on different devices.
6041 *
6042 * @param actions the actions to add to this notification
6043 * @return this object for method chaining
6044 * @see android.app.Notification.Action
6045 */
6046 public WearableExtender addActions(List<Action> actions) {
6047 mActions.addAll(actions);
6048 return this;
6049 }
6050
6051 /**
6052 * Clear all wearable actions present on this builder.
6053 * @return this object for method chaining.
6054 * @see #addAction
6055 */
6056 public WearableExtender clearActions() {
6057 mActions.clear();
6058 return this;
6059 }
6060
6061 /**
6062 * Get the wearable actions present on this notification.
6063 */
6064 public List<Action> getActions() {
6065 return mActions;
6066 }
6067
6068 /**
6069 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006070 * this notification. The {@link PendingIntent} provided should be for an activity.
6071 *
6072 * <pre class="prettyprint">
6073 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6074 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6075 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6076 * Notification notif = new Notification.Builder(context)
6077 * .extend(new Notification.WearableExtender()
6078 * .setDisplayIntent(displayPendingIntent)
6079 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6080 * .build();</pre>
6081 *
6082 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006083 * should have an empty task affinity. It is also recommended to use the device
6084 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006085 *
6086 * <p>Example AndroidManifest.xml entry:
6087 * <pre class="prettyprint">
6088 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6089 * android:exported=&quot;true&quot;
6090 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006091 * android:taskAffinity=&quot;&quot;
6092 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006093 *
6094 * @param intent the {@link PendingIntent} for an activity
6095 * @return this object for method chaining
6096 * @see android.app.Notification.WearableExtender#getDisplayIntent
6097 */
6098 public WearableExtender setDisplayIntent(PendingIntent intent) {
6099 mDisplayIntent = intent;
6100 return this;
6101 }
6102
6103 /**
6104 * Get the intent to launch inside of an activity view when displaying this
6105 * notification. This {@code PendingIntent} should be for an activity.
6106 */
6107 public PendingIntent getDisplayIntent() {
6108 return mDisplayIntent;
6109 }
6110
6111 /**
6112 * Add an additional page of content to display with this notification. The current
6113 * notification forms the first page, and pages added using this function form
6114 * subsequent pages. This field can be used to separate a notification into multiple
6115 * sections.
6116 *
6117 * @param page the notification to add as another page
6118 * @return this object for method chaining
6119 * @see android.app.Notification.WearableExtender#getPages
6120 */
6121 public WearableExtender addPage(Notification page) {
6122 mPages.add(page);
6123 return this;
6124 }
6125
6126 /**
6127 * Add additional pages of content to display with this notification. The current
6128 * notification forms the first page, and pages added using this function form
6129 * subsequent pages. This field can be used to separate a notification into multiple
6130 * sections.
6131 *
6132 * @param pages a list of notifications
6133 * @return this object for method chaining
6134 * @see android.app.Notification.WearableExtender#getPages
6135 */
6136 public WearableExtender addPages(List<Notification> pages) {
6137 mPages.addAll(pages);
6138 return this;
6139 }
6140
6141 /**
6142 * Clear all additional pages present on this builder.
6143 * @return this object for method chaining.
6144 * @see #addPage
6145 */
6146 public WearableExtender clearPages() {
6147 mPages.clear();
6148 return this;
6149 }
6150
6151 /**
6152 * Get the array of additional pages of content for displaying this notification. The
6153 * current notification forms the first page, and elements within this array form
6154 * subsequent pages. This field can be used to separate a notification into multiple
6155 * sections.
6156 * @return the pages for this notification
6157 */
6158 public List<Notification> getPages() {
6159 return mPages;
6160 }
6161
6162 /**
6163 * Set a background image to be displayed behind the notification content.
6164 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6165 * will work with any notification style.
6166 *
6167 * @param background the background bitmap
6168 * @return this object for method chaining
6169 * @see android.app.Notification.WearableExtender#getBackground
6170 */
6171 public WearableExtender setBackground(Bitmap background) {
6172 mBackground = background;
6173 return this;
6174 }
6175
6176 /**
6177 * Get a background image to be displayed behind the notification content.
6178 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6179 * will work with any notification style.
6180 *
6181 * @return the background image
6182 * @see android.app.Notification.WearableExtender#setBackground
6183 */
6184 public Bitmap getBackground() {
6185 return mBackground;
6186 }
6187
6188 /**
6189 * Set an icon that goes with the content of this notification.
6190 */
6191 public WearableExtender setContentIcon(int icon) {
6192 mContentIcon = icon;
6193 return this;
6194 }
6195
6196 /**
6197 * Get an icon that goes with the content of this notification.
6198 */
6199 public int getContentIcon() {
6200 return mContentIcon;
6201 }
6202
6203 /**
6204 * Set the gravity that the content icon should have within the notification display.
6205 * Supported values include {@link android.view.Gravity#START} and
6206 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6207 * @see #setContentIcon
6208 */
6209 public WearableExtender setContentIconGravity(int contentIconGravity) {
6210 mContentIconGravity = contentIconGravity;
6211 return this;
6212 }
6213
6214 /**
6215 * Get the gravity that the content icon should have within the notification display.
6216 * Supported values include {@link android.view.Gravity#START} and
6217 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6218 * @see #getContentIcon
6219 */
6220 public int getContentIconGravity() {
6221 return mContentIconGravity;
6222 }
6223
6224 /**
6225 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006226 * this notification. This action will no longer display separately from the
6227 * notification's content.
6228 *
Griff Hazenca48d352014-05-28 22:37:13 -07006229 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006230 * set, although the list of available actions comes from the main notification and not
6231 * from the child page's notification.
6232 *
6233 * @param actionIndex The index of the action to hoist onto the current notification page.
6234 * If wearable actions were added to the main notification, this index
6235 * will apply to that list, otherwise it will apply to the regular
6236 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006237 */
6238 public WearableExtender setContentAction(int actionIndex) {
6239 mContentActionIndex = actionIndex;
6240 return this;
6241 }
6242
6243 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006244 * Get the index of the notification action, if any, that was specified as being clickable
6245 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006246 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006247 *
Griff Hazenca48d352014-05-28 22:37:13 -07006248 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006249 * set, although the list of available actions comes from the main notification and not
6250 * from the child page's notification.
6251 *
6252 * <p>If wearable specific actions were added to the main notification, this index will
6253 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006254 *
6255 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006256 */
6257 public int getContentAction() {
6258 return mContentActionIndex;
6259 }
6260
6261 /**
6262 * Set the gravity that this notification should have within the available viewport space.
6263 * Supported values include {@link android.view.Gravity#TOP},
6264 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6265 * The default value is {@link android.view.Gravity#BOTTOM}.
6266 */
6267 public WearableExtender setGravity(int gravity) {
6268 mGravity = gravity;
6269 return this;
6270 }
6271
6272 /**
6273 * Get the gravity that this notification should have within the available viewport space.
6274 * Supported values include {@link android.view.Gravity#TOP},
6275 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6276 * The default value is {@link android.view.Gravity#BOTTOM}.
6277 */
6278 public int getGravity() {
6279 return mGravity;
6280 }
6281
6282 /**
6283 * Set the custom size preset for the display of this notification out of the available
6284 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6285 * {@link #SIZE_LARGE}.
6286 * <p>Some custom size presets are only applicable for custom display notifications created
6287 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6288 * documentation for the preset in question. See also
6289 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6290 */
6291 public WearableExtender setCustomSizePreset(int sizePreset) {
6292 mCustomSizePreset = sizePreset;
6293 return this;
6294 }
6295
6296 /**
6297 * Get the custom size preset for the display of this notification out of the available
6298 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6299 * {@link #SIZE_LARGE}.
6300 * <p>Some custom size presets are only applicable for custom display notifications created
6301 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6302 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6303 */
6304 public int getCustomSizePreset() {
6305 return mCustomSizePreset;
6306 }
6307
6308 /**
6309 * Set the custom height in pixels for the display of this notification's content.
6310 * <p>This option is only available for custom display notifications created
6311 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6312 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6313 * {@link #getCustomContentHeight}.
6314 */
6315 public WearableExtender setCustomContentHeight(int height) {
6316 mCustomContentHeight = height;
6317 return this;
6318 }
6319
6320 /**
6321 * Get the custom height in pixels for the display of this notification's content.
6322 * <p>This option is only available for custom display notifications created
6323 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6324 * {@link #setCustomContentHeight}.
6325 */
6326 public int getCustomContentHeight() {
6327 return mCustomContentHeight;
6328 }
6329
6330 /**
6331 * Set whether the scrolling position for the contents of this notification should start
6332 * at the bottom of the contents instead of the top when the contents are too long to
6333 * display within the screen. Default is false (start scroll at the top).
6334 */
6335 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6336 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6337 return this;
6338 }
6339
6340 /**
6341 * Get whether the scrolling position for the contents of this notification should start
6342 * at the bottom of the contents instead of the top when the contents are too long to
6343 * display within the screen. Default is false (start scroll at the top).
6344 */
6345 public boolean getStartScrollBottom() {
6346 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6347 }
6348
6349 /**
6350 * Set whether the content intent is available when the wearable device is not connected
6351 * to a companion device. The user can still trigger this intent when the wearable device
6352 * is offline, but a visual hint will indicate that the content intent may not be available.
6353 * Defaults to true.
6354 */
6355 public WearableExtender setContentIntentAvailableOffline(
6356 boolean contentIntentAvailableOffline) {
6357 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6358 return this;
6359 }
6360
6361 /**
6362 * Get whether the content intent is available when the wearable device is not connected
6363 * to a companion device. The user can still trigger this intent when the wearable device
6364 * is offline, but a visual hint will indicate that the content intent may not be available.
6365 * Defaults to true.
6366 */
6367 public boolean getContentIntentAvailableOffline() {
6368 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6369 }
6370
6371 /**
6372 * Set a hint that this notification's icon should not be displayed.
6373 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6374 * @return this object for method chaining
6375 */
6376 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6377 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6378 return this;
6379 }
6380
6381 /**
6382 * Get a hint that this notification's icon should not be displayed.
6383 * @return {@code true} if this icon should not be displayed, false otherwise.
6384 * The default value is {@code false} if this was never set.
6385 */
6386 public boolean getHintHideIcon() {
6387 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6388 }
6389
6390 /**
6391 * Set a visual hint that only the background image of this notification should be
6392 * displayed, and other semantic content should be hidden. This hint is only applicable
6393 * to sub-pages added using {@link #addPage}.
6394 */
6395 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6396 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6397 return this;
6398 }
6399
6400 /**
6401 * Get a visual hint that only the background image of this notification should be
6402 * displayed, and other semantic content should be hidden. This hint is only applicable
6403 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6404 */
6405 public boolean getHintShowBackgroundOnly() {
6406 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6407 }
6408
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006409 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006410 * Set a hint that this notification's background should not be clipped if possible,
6411 * and should instead be resized to fully display on the screen, retaining the aspect
6412 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006413 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6414 * @return this object for method chaining
6415 */
6416 public WearableExtender setHintAvoidBackgroundClipping(
6417 boolean hintAvoidBackgroundClipping) {
6418 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6419 return this;
6420 }
6421
6422 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006423 * Get a hint that this notification's background should not be clipped if possible,
6424 * and should instead be resized to fully display on the screen, retaining the aspect
6425 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006426 * @return {@code true} if it's ok if the background is clipped on the screen, false
6427 * otherwise. The default value is {@code false} if this was never set.
6428 */
6429 public boolean getHintAvoidBackgroundClipping() {
6430 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6431 }
6432
6433 /**
6434 * Set a hint that the screen should remain on for at least this duration when
6435 * this notification is displayed on the screen.
6436 * @param timeout The requested screen timeout in milliseconds. Can also be either
6437 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6438 * @return this object for method chaining
6439 */
6440 public WearableExtender setHintScreenTimeout(int timeout) {
6441 mHintScreenTimeout = timeout;
6442 return this;
6443 }
6444
6445 /**
6446 * Get the duration, in milliseconds, that the screen should remain on for
6447 * when this notification is displayed.
6448 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6449 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6450 */
6451 public int getHintScreenTimeout() {
6452 return mHintScreenTimeout;
6453 }
6454
Alex Hills9ab3a232016-04-05 14:54:56 -04006455 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006456 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6457 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6458 * qr codes, as well as other simple black-and-white tickets.
6459 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6460 * @return this object for method chaining
6461 */
6462 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6463 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6464 return this;
6465 }
6466
6467 /**
6468 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6469 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6470 * qr codes, as well as other simple black-and-white tickets.
6471 * @return {@code true} if it should be displayed in ambient, false otherwise
6472 * otherwise. The default value is {@code false} if this was never set.
6473 */
6474 public boolean getHintAmbientBigPicture() {
6475 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6476 }
6477
6478 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006479 * Set a hint that this notification's content intent will launch an {@link Activity}
6480 * directly, telling the platform that it can generate the appropriate transitions.
6481 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6482 * an activity and transitions should be generated, false otherwise.
6483 * @return this object for method chaining
6484 */
6485 public WearableExtender setHintContentIntentLaunchesActivity(
6486 boolean hintContentIntentLaunchesActivity) {
6487 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6488 return this;
6489 }
6490
6491 /**
6492 * Get a hint that this notification's content intent will launch an {@link Activity}
6493 * directly, telling the platform that it can generate the appropriate transitions
6494 * @return {@code true} if the content intent will launch an activity and transitions should
6495 * be generated, false otherwise. The default value is {@code false} if this was never set.
6496 */
6497 public boolean getHintContentIntentLaunchesActivity() {
6498 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6499 }
6500
Nadia Benbernou948627e2016-04-14 14:41:08 -04006501 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006502 * Sets the dismissal id for this notification. If a notification is posted with a
6503 * dismissal id, then when that notification is canceled, notifications on other wearables
6504 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04006505 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006506 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04006507 * @param dismissalId the dismissal id of the notification.
6508 * @return this object for method chaining
6509 */
6510 public WearableExtender setDismissalId(String dismissalId) {
6511 mDismissalId = dismissalId;
6512 return this;
6513 }
6514
6515 /**
6516 * Returns the dismissal id of the notification.
6517 * @return the dismissal id of the notification or null if it has not been set.
6518 */
6519 public String getDismissalId() {
6520 return mDismissalId;
6521 }
6522
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006523 /**
6524 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
6525 * posted from a phone to provide finer-grained control on what notifications are bridged
6526 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
6527 * Features to Notifications</a> for more information.
6528 * @param bridgeTag the bridge tag of the notification.
6529 * @return this object for method chaining
6530 */
6531 public WearableExtender setBridgeTag(String bridgeTag) {
6532 mBridgeTag = bridgeTag;
6533 return this;
6534 }
6535
6536 /**
6537 * Returns the bridge tag of the notification.
6538 * @return the bridge tag or null if not present.
6539 */
6540 public String getBridgeTag() {
6541 return mBridgeTag;
6542 }
6543
Griff Hazen61a9e862014-05-22 16:05:19 -07006544 private void setFlag(int mask, boolean value) {
6545 if (value) {
6546 mFlags |= mask;
6547 } else {
6548 mFlags &= ~mask;
6549 }
6550 }
6551 }
6552
6553 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006554 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6555 * with car extensions:
6556 *
6557 * <ol>
6558 * <li>Create an {@link Notification.Builder}, setting any desired
6559 * properties.
6560 * <li>Create a {@link CarExtender}.
6561 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6562 * {@link CarExtender}.
6563 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6564 * to apply the extensions to a notification.
6565 * </ol>
6566 *
6567 * <pre class="prettyprint">
6568 * Notification notification = new Notification.Builder(context)
6569 * ...
6570 * .extend(new CarExtender()
6571 * .set*(...))
6572 * .build();
6573 * </pre>
6574 *
6575 * <p>Car extensions can be accessed on an existing notification by using the
6576 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6577 * to access values.
6578 */
6579 public static final class CarExtender implements Extender {
6580 private static final String TAG = "CarExtender";
6581
6582 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6583 private static final String EXTRA_LARGE_ICON = "large_icon";
6584 private static final String EXTRA_CONVERSATION = "car_conversation";
6585 private static final String EXTRA_COLOR = "app_color";
6586
6587 private Bitmap mLargeIcon;
6588 private UnreadConversation mUnreadConversation;
6589 private int mColor = Notification.COLOR_DEFAULT;
6590
6591 /**
6592 * Create a {@link CarExtender} with default options.
6593 */
6594 public CarExtender() {
6595 }
6596
6597 /**
6598 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6599 *
6600 * @param notif The notification from which to copy options.
6601 */
6602 public CarExtender(Notification notif) {
6603 Bundle carBundle = notif.extras == null ?
6604 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6605 if (carBundle != null) {
6606 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6607 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6608
6609 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6610 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6611 }
6612 }
6613
6614 /**
6615 * Apply car extensions to a notification that is being built. This is typically called by
6616 * the {@link Notification.Builder#extend(Notification.Extender)}
6617 * method of {@link Notification.Builder}.
6618 */
6619 @Override
6620 public Notification.Builder extend(Notification.Builder builder) {
6621 Bundle carExtensions = new Bundle();
6622
6623 if (mLargeIcon != null) {
6624 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6625 }
6626 if (mColor != Notification.COLOR_DEFAULT) {
6627 carExtensions.putInt(EXTRA_COLOR, mColor);
6628 }
6629
6630 if (mUnreadConversation != null) {
6631 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6632 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6633 }
6634
6635 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6636 return builder;
6637 }
6638
6639 /**
6640 * Sets the accent color to use when Android Auto presents the notification.
6641 *
6642 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6643 * to accent the displayed notification. However, not all colors are acceptable in an
6644 * automotive setting. This method can be used to override the color provided in the
6645 * notification in such a situation.
6646 */
Tor Norbye80756e32015-03-02 09:39:27 -08006647 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006648 mColor = color;
6649 return this;
6650 }
6651
6652 /**
6653 * Gets the accent color.
6654 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006655 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006656 */
Tor Norbye80756e32015-03-02 09:39:27 -08006657 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006658 public int getColor() {
6659 return mColor;
6660 }
6661
6662 /**
6663 * Sets the large icon of the car notification.
6664 *
6665 * If no large icon is set in the extender, Android Auto will display the icon
6666 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6667 *
6668 * @param largeIcon The large icon to use in the car notification.
6669 * @return This object for method chaining.
6670 */
6671 public CarExtender setLargeIcon(Bitmap largeIcon) {
6672 mLargeIcon = largeIcon;
6673 return this;
6674 }
6675
6676 /**
6677 * Gets the large icon used in this car notification, or null if no icon has been set.
6678 *
6679 * @return The large icon for the car notification.
6680 * @see CarExtender#setLargeIcon
6681 */
6682 public Bitmap getLargeIcon() {
6683 return mLargeIcon;
6684 }
6685
6686 /**
6687 * Sets the unread conversation in a message notification.
6688 *
6689 * @param unreadConversation The unread part of the conversation this notification conveys.
6690 * @return This object for method chaining.
6691 */
6692 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6693 mUnreadConversation = unreadConversation;
6694 return this;
6695 }
6696
6697 /**
6698 * Returns the unread conversation conveyed by this notification.
6699 * @see #setUnreadConversation(UnreadConversation)
6700 */
6701 public UnreadConversation getUnreadConversation() {
6702 return mUnreadConversation;
6703 }
6704
6705 /**
6706 * A class which holds the unread messages from a conversation.
6707 */
6708 public static class UnreadConversation {
6709 private static final String KEY_AUTHOR = "author";
6710 private static final String KEY_TEXT = "text";
6711 private static final String KEY_MESSAGES = "messages";
6712 private static final String KEY_REMOTE_INPUT = "remote_input";
6713 private static final String KEY_ON_REPLY = "on_reply";
6714 private static final String KEY_ON_READ = "on_read";
6715 private static final String KEY_PARTICIPANTS = "participants";
6716 private static final String KEY_TIMESTAMP = "timestamp";
6717
6718 private final String[] mMessages;
6719 private final RemoteInput mRemoteInput;
6720 private final PendingIntent mReplyPendingIntent;
6721 private final PendingIntent mReadPendingIntent;
6722 private final String[] mParticipants;
6723 private final long mLatestTimestamp;
6724
6725 UnreadConversation(String[] messages, RemoteInput remoteInput,
6726 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6727 String[] participants, long latestTimestamp) {
6728 mMessages = messages;
6729 mRemoteInput = remoteInput;
6730 mReadPendingIntent = readPendingIntent;
6731 mReplyPendingIntent = replyPendingIntent;
6732 mParticipants = participants;
6733 mLatestTimestamp = latestTimestamp;
6734 }
6735
6736 /**
6737 * Gets the list of messages conveyed by this notification.
6738 */
6739 public String[] getMessages() {
6740 return mMessages;
6741 }
6742
6743 /**
6744 * Gets the remote input that will be used to convey the response to a message list, or
6745 * null if no such remote input exists.
6746 */
6747 public RemoteInput getRemoteInput() {
6748 return mRemoteInput;
6749 }
6750
6751 /**
6752 * Gets the pending intent that will be triggered when the user replies to this
6753 * notification.
6754 */
6755 public PendingIntent getReplyPendingIntent() {
6756 return mReplyPendingIntent;
6757 }
6758
6759 /**
6760 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6761 * in this object's message list.
6762 */
6763 public PendingIntent getReadPendingIntent() {
6764 return mReadPendingIntent;
6765 }
6766
6767 /**
6768 * Gets the participants in the conversation.
6769 */
6770 public String[] getParticipants() {
6771 return mParticipants;
6772 }
6773
6774 /**
6775 * Gets the firs participant in the conversation.
6776 */
6777 public String getParticipant() {
6778 return mParticipants.length > 0 ? mParticipants[0] : null;
6779 }
6780
6781 /**
6782 * Gets the timestamp of the conversation.
6783 */
6784 public long getLatestTimestamp() {
6785 return mLatestTimestamp;
6786 }
6787
6788 Bundle getBundleForUnreadConversation() {
6789 Bundle b = new Bundle();
6790 String author = null;
6791 if (mParticipants != null && mParticipants.length > 1) {
6792 author = mParticipants[0];
6793 }
6794 Parcelable[] messages = new Parcelable[mMessages.length];
6795 for (int i = 0; i < messages.length; i++) {
6796 Bundle m = new Bundle();
6797 m.putString(KEY_TEXT, mMessages[i]);
6798 m.putString(KEY_AUTHOR, author);
6799 messages[i] = m;
6800 }
6801 b.putParcelableArray(KEY_MESSAGES, messages);
6802 if (mRemoteInput != null) {
6803 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6804 }
6805 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6806 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6807 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6808 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6809 return b;
6810 }
6811
6812 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6813 if (b == null) {
6814 return null;
6815 }
6816 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6817 String[] messages = null;
6818 if (parcelableMessages != null) {
6819 String[] tmp = new String[parcelableMessages.length];
6820 boolean success = true;
6821 for (int i = 0; i < tmp.length; i++) {
6822 if (!(parcelableMessages[i] instanceof Bundle)) {
6823 success = false;
6824 break;
6825 }
6826 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6827 if (tmp[i] == null) {
6828 success = false;
6829 break;
6830 }
6831 }
6832 if (success) {
6833 messages = tmp;
6834 } else {
6835 return null;
6836 }
6837 }
6838
6839 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6840 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6841
6842 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6843
6844 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6845 if (participants == null || participants.length != 1) {
6846 return null;
6847 }
6848
6849 return new UnreadConversation(messages,
6850 remoteInput,
6851 onReply,
6852 onRead,
6853 participants, b.getLong(KEY_TIMESTAMP));
6854 }
6855 };
6856
6857 /**
6858 * Builder class for {@link CarExtender.UnreadConversation} objects.
6859 */
6860 public static class Builder {
6861 private final List<String> mMessages = new ArrayList<String>();
6862 private final String mParticipant;
6863 private RemoteInput mRemoteInput;
6864 private PendingIntent mReadPendingIntent;
6865 private PendingIntent mReplyPendingIntent;
6866 private long mLatestTimestamp;
6867
6868 /**
6869 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6870 *
6871 * @param name The name of the other participant in the conversation.
6872 */
6873 public Builder(String name) {
6874 mParticipant = name;
6875 }
6876
6877 /**
6878 * Appends a new unread message to the list of messages for this conversation.
6879 *
6880 * The messages should be added from oldest to newest.
6881 *
6882 * @param message The text of the new unread message.
6883 * @return This object for method chaining.
6884 */
6885 public Builder addMessage(String message) {
6886 mMessages.add(message);
6887 return this;
6888 }
6889
6890 /**
6891 * Sets the pending intent and remote input which will convey the reply to this
6892 * notification.
6893 *
6894 * @param pendingIntent The pending intent which will be triggered on a reply.
6895 * @param remoteInput The remote input parcelable which will carry the reply.
6896 * @return This object for method chaining.
6897 *
6898 * @see CarExtender.UnreadConversation#getRemoteInput
6899 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6900 */
6901 public Builder setReplyAction(
6902 PendingIntent pendingIntent, RemoteInput remoteInput) {
6903 mRemoteInput = remoteInput;
6904 mReplyPendingIntent = pendingIntent;
6905
6906 return this;
6907 }
6908
6909 /**
6910 * Sets the pending intent that will be sent once the messages in this notification
6911 * are read.
6912 *
6913 * @param pendingIntent The pending intent to use.
6914 * @return This object for method chaining.
6915 */
6916 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6917 mReadPendingIntent = pendingIntent;
6918 return this;
6919 }
6920
6921 /**
6922 * Sets the timestamp of the most recent message in an unread conversation.
6923 *
6924 * If a messaging notification has been posted by your application and has not
6925 * yet been cancelled, posting a later notification with the same id and tag
6926 * but without a newer timestamp may result in Android Auto not displaying a
6927 * heads up notification for the later notification.
6928 *
6929 * @param timestamp The timestamp of the most recent message in the conversation.
6930 * @return This object for method chaining.
6931 */
6932 public Builder setLatestTimestamp(long timestamp) {
6933 mLatestTimestamp = timestamp;
6934 return this;
6935 }
6936
6937 /**
6938 * Builds a new unread conversation object.
6939 *
6940 * @return The new unread conversation object.
6941 */
6942 public UnreadConversation build() {
6943 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6944 String[] participants = { mParticipant };
6945 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6946 mReadPendingIntent, participants, mLatestTimestamp);
6947 }
6948 }
6949 }
6950
6951 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006952 * Get an array of Notification objects from a parcelable array bundle field.
6953 * Update the bundle to have a typed array so fetches in the future don't need
6954 * to do an array copy.
6955 */
6956 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6957 Parcelable[] array = bundle.getParcelableArray(key);
6958 if (array instanceof Notification[] || array == null) {
6959 return (Notification[]) array;
6960 }
6961 Notification[] typedArray = Arrays.copyOf(array, array.length,
6962 Notification[].class);
6963 bundle.putParcelableArray(key, typedArray);
6964 return typedArray;
6965 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006966
6967 private static class BuilderRemoteViews extends RemoteViews {
6968 public BuilderRemoteViews(Parcel parcel) {
6969 super(parcel);
6970 }
6971
Kenny Guy77320062014-08-27 21:37:15 +01006972 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6973 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006974 }
6975
6976 @Override
6977 public BuilderRemoteViews clone() {
6978 Parcel p = Parcel.obtain();
6979 writeToParcel(p, 0);
6980 p.setDataPosition(0);
6981 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6982 p.recycle();
6983 return brv;
6984 }
6985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986}