blob: bef25c2080ce5a9b981f6f681f6f03d2277d16c3 [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 Cinek89991a22016-03-07 19:51:54 -080053import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080054import android.text.style.RelativeSizeSpan;
55import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040056import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050057import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040058import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070059import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080060import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080061import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080062import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070063import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.widget.RemoteViews;
65
Griff Hazen959591e2014-05-15 22:26:18 -070066import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070067import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070068
Tor Norbyed9273d62013-05-30 15:59:53 -070069import java.lang.annotation.Retention;
70import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020071import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050072import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070073import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070074import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070075import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050076import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078/**
79 * A class that represents how a persistent notification is to be presented to
80 * the user using the {@link android.app.NotificationManager}.
81 *
Joe Onoratocb109a02011-01-18 17:57:41 -080082 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
83 * easier to construct Notifications.</p>
84 *
Joe Fernandez558459f2011-10-13 16:47:36 -070085 * <div class="special reference">
86 * <h3>Developer Guides</h3>
87 * <p>For a guide to creating notifications, read the
88 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
89 * developer guide.</p>
90 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 */
92public class Notification implements Parcelable
93{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040094 private static final String TAG = "Notification";
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040097 * An activity that provides a user interface for adjusting notification preferences for its
98 * containing application. Optional but recommended for apps that post
99 * {@link android.app.Notification Notifications}.
100 */
101 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
102 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
103 = "android.intent.category.NOTIFICATION_PREFERENCES";
104
105 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * Use all default values (where applicable).
107 */
108 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 /**
111 * Use the default notification sound. This will ignore any given
112 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113 *
Chris Wren47c20a12014-06-18 17:27:29 -0400114 * <p>
115 * A notification that is noisy is more likely to be presented as a heads-up notification.
116 * </p>
117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500119 */
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public static final int DEFAULT_SOUND = 1;
122
123 /**
124 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500125 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700126 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500127 *
Chris Wren47c20a12014-06-18 17:27:29 -0400128 * <p>
129 * A notification that vibrates is more likely to be presented as a heads-up notification.
130 * </p>
131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500133 */
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 /**
138 * Use the default notification lights. This will ignore the
139 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
140 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500143 */
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200148 * Maximum length of CharSequences accepted by Builder and friends.
149 *
150 * <p>
151 * Avoids spamming the system with overly large strings such as full e-mails.
152 */
153 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
154
155 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800156 * Maximum entries of reply text that are accepted by Builder and friends.
157 */
158 private static final int MAX_REPLY_HISTORY = 5;
159
160 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800162 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500163 * Default value: {@link System#currentTimeMillis() Now}.
164 *
165 * Choose a timestamp that will be most relevant to the user. For most finite events, this
166 * corresponds to the time the event happened (or will happen, in the case of events that have
167 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800168 * timestamped according to when the activity began.
169 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800171 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500172 * <ul>
173 * <li>Notification of a new chat message should be stamped when the message was received.</li>
174 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
175 * <li>Notification of a completed file download should be stamped when the download finished.</li>
176 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
177 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
178 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800179 * </ul>
180 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700181 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
182 * anymore by default and must be opted into by using
183 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 */
185 public long when;
186
187 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700188 * The creation time of the notification
189 */
190 private long creationTime;
191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400194 *
195 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 */
Dan Sandler86647982015-05-13 23:41:13 -0400197 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700198 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public int icon;
200
201 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800202 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
203 * leave it at its default value of 0.
204 *
205 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700206 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800207 */
208 public int iconLevel;
209
210 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500211 * The number of events that this notification represents. For example, in a new mail
212 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800213 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500214 * The system may or may not use this field to modify the appearance of the notification. For
215 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
216 * superimposed over the icon in the status bar. Starting with
217 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
218 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800219 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500220 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700221 *
222 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 */
224 public int number;
225
226 /**
227 * The intent to execute when the expanded status entry is clicked. If
228 * this is an activity, it must include the
229 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800230 * that you take care of task management as described in the
231 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800232 * Stack</a> document. In particular, make sure to read the notification section
233 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
234 * Notifications</a> for the correct ways to launch an application from a
235 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 */
237 public PendingIntent contentIntent;
238
239 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500240 * The intent to execute when the notification is explicitly dismissed by the user, either with
241 * the "Clear All" button or by swiping it away individually.
242 *
243 * This probably shouldn't be launching an activity since several of those will be sent
244 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 */
246 public PendingIntent deleteIntent;
247
248 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700249 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800250 *
Chris Wren47c20a12014-06-18 17:27:29 -0400251 * <p>
252 * The system UI may choose to display a heads-up notification, instead of
253 * launching this intent, while the user is using the device.
254 * </p>
255 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800256 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400257 */
258 public PendingIntent fullScreenIntent;
259
260 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400261 * Text that summarizes this notification for accessibility services.
262 *
263 * As of the L release, this text is no longer shown on screen, but it is still useful to
264 * accessibility services (where it serves as an audible announcement of the notification's
265 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400266 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800267 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 */
269 public CharSequence tickerText;
270
271 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400272 * Formerly, a view showing the {@link #tickerText}.
273 *
274 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400275 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400276 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800277 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400278
279 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400280 * The view that will represent this notification in the notification list (which is pulled
281 * down from the status bar).
282 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500283 * As of N, this field may be null. The notification view is determined by the inputs
284 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400285 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400287 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 public RemoteViews contentView;
289
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400290 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400291 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400292 * opportunity to show more detail. The system UI may choose to show this
293 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400294 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500295 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400296 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
297 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400298 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400300 public RemoteViews bigContentView;
301
Chris Wren8fd39ec2014-02-27 17:43:26 -0500302
303 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400304 * A medium-format version of {@link #contentView}, providing the Notification an
305 * opportunity to add action buttons to contentView. At its discretion, the system UI may
306 * choose to show this as a heads-up notification, which will pop up so the user can see
307 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400308 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500309 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400310 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
311 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500312 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400313 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500314 public RemoteViews headsUpContentView;
315
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400316 /**
Dan Sandler86647982015-05-13 23:41:13 -0400317 * A large bitmap to be shown in the notification content area.
318 *
319 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 */
Dan Sandler86647982015-05-13 23:41:13 -0400321 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800322 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323
324 /**
325 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400328 * A notification that is noisy is more likely to be presented as a heads-up notification.
329 * </p>
330 *
331 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500332 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * </p>
334 */
335 public Uri sound;
336
337 /**
338 * Use this constant as the value for audioStreamType to request that
339 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700340 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400341 *
342 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700344 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 public static final int STREAM_DEFAULT = -1;
346
347 /**
348 * The audio stream type to use when playing the sound.
349 * Should be one of the STREAM_ constants from
350 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400351 *
352 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700354 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 public int audioStreamType = STREAM_DEFAULT;
356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400358 * The default value of {@link #audioAttributes}.
359 */
360 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
361 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
362 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
363 .build();
364
365 /**
366 * The {@link AudioAttributes audio attributes} to use when playing the sound.
367 */
368 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
369
370 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500371 * The pattern with which to vibrate.
372 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 * <p>
374 * To vibrate the default pattern, see {@link #defaults}.
375 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500376 *
Chris Wren47c20a12014-06-18 17:27:29 -0400377 * <p>
378 * A notification that vibrates is more likely to be presented as a heads-up notification.
379 * </p>
380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * @see android.os.Vibrator#vibrate(long[],int)
382 */
383 public long[] vibrate;
384
385 /**
386 * The color of the led. The hardware will do its best approximation.
387 *
388 * @see #FLAG_SHOW_LIGHTS
389 * @see #flags
390 */
Tor Norbye80756e32015-03-02 09:39:27 -0800391 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 public int ledARGB;
393
394 /**
395 * The number of milliseconds for the LED to be on while it's flashing.
396 * The hardware will do its best approximation.
397 *
398 * @see #FLAG_SHOW_LIGHTS
399 * @see #flags
400 */
401 public int ledOnMS;
402
403 /**
404 * The number of milliseconds for the LED to be off while it's flashing.
405 * The hardware will do its best approximation.
406 *
407 * @see #FLAG_SHOW_LIGHTS
408 * @see #flags
409 */
410 public int ledOffMS;
411
412 /**
413 * Specifies which values should be taken from the defaults.
414 * <p>
415 * To set, OR the desired from {@link #DEFAULT_SOUND},
416 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
417 * values, use {@link #DEFAULT_ALL}.
418 * </p>
419 */
420 public int defaults;
421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 /**
423 * Bit to be bitwise-ored into the {@link #flags} field that should be
424 * set if you want the LED on for this notification.
425 * <ul>
426 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
427 * or 0 for both ledOnMS and ledOffMS.</li>
428 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
429 * <li>To flash the LED, pass the number of milliseconds that it should
430 * be on and off to ledOnMS and ledOffMS.</li>
431 * </ul>
432 * <p>
433 * Since hardware varies, you are not guaranteed that any of the values
434 * you pass are honored exactly. Use the system defaults (TODO) if possible
435 * because they will be set to values that work on any given hardware.
436 * <p>
437 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500438 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 */
440 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
441
442 /**
443 * Bit to be bitwise-ored into the {@link #flags} field that should be
444 * set if this notification is in reference to something that is ongoing,
445 * like a phone call. It should not be set if this notification is in
446 * reference to something that happened at a particular point in time,
447 * like a missed phone call.
448 */
449 public static final int FLAG_ONGOING_EVENT = 0x00000002;
450
451 /**
452 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700453 * the audio will be repeated until the notification is
454 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
456 public static final int FLAG_INSISTENT = 0x00000004;
457
458 /**
459 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700460 * set if you would only like the sound, vibrate and ticker to be played
461 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 */
463 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
464
465 /**
466 * Bit to be bitwise-ored into the {@link #flags} field that should be
467 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500468 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
470 public static final int FLAG_AUTO_CANCEL = 0x00000010;
471
472 /**
473 * Bit to be bitwise-ored into the {@link #flags} field that should be
474 * set if the notification should not be canceled when the user clicks
475 * the Clear all button.
476 */
477 public static final int FLAG_NO_CLEAR = 0x00000020;
478
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700479 /**
480 * Bit to be bitwise-ored into the {@link #flags} field that should be
481 * set if this notification represents a currently running service. This
482 * will normally be set for you by {@link Service#startForeground}.
483 */
484 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
485
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400486 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500487 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800488 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500489 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400490 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500491 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400492
Griff Hazendfcb0802014-02-11 12:00:00 -0800493 /**
494 * Bit to be bitswise-ored into the {@link #flags} field that should be
495 * set if this notification is relevant to the current device only
496 * and it is not recommended that it bridge to other devices.
497 */
498 public static final int FLAG_LOCAL_ONLY = 0x00000100;
499
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700500 /**
501 * Bit to be bitswise-ored into the {@link #flags} field that should be
502 * set if this notification is the group summary for a group of notifications.
503 * Grouped notifications may display in a cluster or stack on devices which
504 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
505 */
506 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
507
Julia Reynoldse46bb372016-03-17 11:05:58 -0400508 /**
509 * Bit to be bitswise-ored into the {@link #flags} field that should be
510 * set if this notification is the group summary for an auto-group of notifications.
511 *
512 * @hide
513 */
514 @SystemApi
515 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 public int flags;
518
Tor Norbyed9273d62013-05-30 15:59:53 -0700519 /** @hide */
520 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
521 @Retention(RetentionPolicy.SOURCE)
522 public @interface Priority {}
523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500525 * Default notification {@link #priority}. If your application does not prioritize its own
526 * notifications, use this value for all notifications.
527 */
528 public static final int PRIORITY_DEFAULT = 0;
529
530 /**
531 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
532 * items smaller, or at a different position in the list, compared with your app's
533 * {@link #PRIORITY_DEFAULT} items.
534 */
535 public static final int PRIORITY_LOW = -1;
536
537 /**
538 * Lowest {@link #priority}; these items might not be shown to the user except under special
539 * circumstances, such as detailed notification logs.
540 */
541 public static final int PRIORITY_MIN = -2;
542
543 /**
544 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
545 * show these items larger, or at a different position in notification lists, compared with
546 * your app's {@link #PRIORITY_DEFAULT} items.
547 */
548 public static final int PRIORITY_HIGH = 1;
549
550 /**
551 * Highest {@link #priority}, for your application's most important items that require the
552 * user's prompt attention or input.
553 */
554 public static final int PRIORITY_MAX = 2;
555
556 /**
557 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800558 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500559 * Priority is an indication of how much of the user's valuable attention should be consumed by
560 * this notification. Low-priority notifications may be hidden from the user in certain
561 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500562 * system will make a determination about how to interpret this priority when presenting
563 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400564 *
565 * <p>
566 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
567 * as a heads-up notification.
568 * </p>
569 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700571 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500572 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800573
Dan Sandler26e81cf2014-05-06 10:01:27 -0400574 /**
575 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
576 * to be applied by the standard Style templates when presenting this notification.
577 *
578 * The current template design constructs a colorful header image by overlaying the
579 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
580 * ignored.
581 */
Tor Norbye80756e32015-03-02 09:39:27 -0800582 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400583 public int color = COLOR_DEFAULT;
584
585 /**
586 * Special value of {@link #color} telling the system not to decorate this notification with
587 * any special color but instead use default colors when presenting this notification.
588 */
Tor Norbye80756e32015-03-02 09:39:27 -0800589 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400590 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600591
592 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800593 * Special value of {@link #color} used as a place holder for an invalid color.
594 */
595 @ColorInt
596 private static final int COLOR_INVALID = 1;
597
598 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700599 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
600 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600601 * lockscreen).
602 *
603 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
604 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
605 * shown in all situations, but the contents are only available if the device is unlocked for
606 * the appropriate user.
607 *
608 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
609 * can be read even in an "insecure" context (that is, above a secure lockscreen).
610 * To modify the public version of this notification—for example, to redact some portions—see
611 * {@link Builder#setPublicVersion(Notification)}.
612 *
613 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
614 * and ticker until the user has bypassed the lockscreen.
615 */
616 public int visibility;
617
Griff Hazenfc3922d2014-08-20 11:56:44 -0700618 /**
619 * Notification visibility: Show this notification in its entirety on all lockscreens.
620 *
621 * {@see #visibility}
622 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600623 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700624
625 /**
626 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
627 * private information on secure lockscreens.
628 *
629 * {@see #visibility}
630 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600631 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700632
633 /**
634 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
635 *
636 * {@see #visibility}
637 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600638 public static final int VISIBILITY_SECRET = -1;
639
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500640 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400641 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500642 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400643 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644
645 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400646 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400648 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649
650 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400651 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400653 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654
655 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400656 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400658 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659
660 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400661 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400663 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664
665 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400666 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400668 public static final String CATEGORY_ALARM = "alarm";
669
670 /**
671 * Notification category: progress of a long-running background operation.
672 */
673 public static final String CATEGORY_PROGRESS = "progress";
674
675 /**
676 * Notification category: social network or sharing update.
677 */
678 public static final String CATEGORY_SOCIAL = "social";
679
680 /**
681 * Notification category: error in background operation or authentication status.
682 */
683 public static final String CATEGORY_ERROR = "err";
684
685 /**
686 * Notification category: media transport control for playback.
687 */
688 public static final String CATEGORY_TRANSPORT = "transport";
689
690 /**
691 * Notification category: system or device status update. Reserved for system use.
692 */
693 public static final String CATEGORY_SYSTEM = "sys";
694
695 /**
696 * Notification category: indication of running background service.
697 */
698 public static final String CATEGORY_SERVICE = "service";
699
700 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400701 * Notification category: a specific, timely recommendation for a single thing.
702 * For example, a news app might want to recommend a news story it believes the user will
703 * want to read next.
704 */
705 public static final String CATEGORY_RECOMMENDATION = "recommendation";
706
707 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400708 * Notification category: ongoing information about device or contextual status.
709 */
710 public static final String CATEGORY_STATUS = "status";
711
712 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400713 * Notification category: user-scheduled reminder.
714 */
715 public static final String CATEGORY_REMINDER = "reminder";
716
717 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400718 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
719 * that best describes this Notification. May be used by the system for ranking and filtering.
720 */
721 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500722
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700723 private String mGroupKey;
724
725 /**
726 * Get the key used to group this notification into a cluster or stack
727 * with other notifications on devices which support such rendering.
728 */
729 public String getGroup() {
730 return mGroupKey;
731 }
732
733 private String mSortKey;
734
735 /**
736 * Get a sort key that orders this notification among other notifications from the
737 * same package. This can be useful if an external sort was already applied and an app
738 * would like to preserve this. Notifications will be sorted lexicographically using this
739 * value, although providing different priorities in addition to providing sort key may
740 * cause this value to be ignored.
741 *
742 * <p>This sort key can also be used to order members of a notification group. See
743 * {@link Builder#setGroup}.
744 *
745 * @see String#compareTo(String)
746 */
747 public String getSortKey() {
748 return mSortKey;
749 }
750
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500751 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400752 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400753 * <p>
754 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
755 * APIs, and are intended to be used by
756 * {@link android.service.notification.NotificationListenerService} implementations to extract
757 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500758 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400759 public Bundle extras = new Bundle();
760
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400761 /**
762 * {@link #extras} key: this is the title of the notification,
763 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
764 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500765 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400766
767 /**
768 * {@link #extras} key: this is the title of the notification when shown in expanded form,
769 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
770 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400771 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400772
773 /**
774 * {@link #extras} key: this is the main text payload, as supplied to
775 * {@link Builder#setContentText(CharSequence)}.
776 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500777 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400778
779 /**
780 * {@link #extras} key: this is a third line of text, as supplied to
781 * {@link Builder#setSubText(CharSequence)}.
782 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400783 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784
785 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800786 * {@link #extras} key: this is the remote input history, as supplied to
787 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700788 *
789 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
790 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
791 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
792 * notifications once the other party has responded).
793 *
794 * The extra with this key is of type CharSequence[] and contains the most recent entry at
795 * the 0 index, the second most recent at the 1 index, etc.
796 *
797 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800798 */
799 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
800
801 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400802 * {@link #extras} key: this is a small piece of additional text as supplied to
803 * {@link Builder#setContentInfo(CharSequence)}.
804 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400805 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400806
807 /**
808 * {@link #extras} key: this is a line of summary information intended to be shown
809 * alongside expanded notifications, as supplied to (e.g.)
810 * {@link BigTextStyle#setSummaryText(CharSequence)}.
811 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400812 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400813
814 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200815 * {@link #extras} key: this is the longer text shown in the big form of a
816 * {@link BigTextStyle} notification, as supplied to
817 * {@link BigTextStyle#bigText(CharSequence)}.
818 */
819 public static final String EXTRA_BIG_TEXT = "android.bigText";
820
821 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400822 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
823 * supplied to {@link Builder#setSmallIcon(int)}.
824 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500825 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400826
827 /**
828 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
829 * notification payload, as
830 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
831 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400832 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400833
834 /**
835 * {@link #extras} key: this is a bitmap to be used instead of the one from
836 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
837 * shown in its expanded form, as supplied to
838 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
839 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400840 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400841
842 /**
843 * {@link #extras} key: this is the progress value supplied to
844 * {@link Builder#setProgress(int, int, boolean)}.
845 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400846 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400847
848 /**
849 * {@link #extras} key: this is the maximum value supplied to
850 * {@link Builder#setProgress(int, int, boolean)}.
851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400853
854 /**
855 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
856 * {@link Builder#setProgress(int, int, boolean)}.
857 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400858 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400859
860 /**
861 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
862 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
863 * {@link Builder#setUsesChronometer(boolean)}.
864 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400865 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400866
867 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800868 * {@link #extras} key: whether the chronometer set on the notification should count down
869 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
870 */
871 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
872
873 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874 * {@link #extras} key: whether {@link #when} should be shown,
875 * as supplied to {@link Builder#setShowWhen(boolean)}.
876 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400877 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400878
879 /**
880 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
881 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
882 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400883 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400884
885 /**
886 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
887 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
888 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400889 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400890
891 /**
892 * {@link #extras} key: A string representing the name of the specific
893 * {@link android.app.Notification.Style} used to create this notification.
894 */
Chris Wren91ad5632013-06-05 15:05:57 -0400895 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400896
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400897 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400898 * {@link #extras} key: A String array containing the people that this notification relates to,
899 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400900 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400901 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500902
903 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400904 * Allow certain system-generated notifications to appear before the device is provisioned.
905 * Only available to notifications coming from the android package.
906 * @hide
907 */
908 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
909
910 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700911 * {@link #extras} key: A
912 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
913 * in the background when the notification is selected. The URI must point to an image stream
914 * suitable for passing into
915 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
916 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
917 * URI used for this purpose must require no permissions to read the image data.
918 */
919 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
920
921 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400922 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700923 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400924 * {@link android.app.Notification.MediaStyle} notification.
925 */
926 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
927
928 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100929 * {@link #extras} key: the indices of actions to be shown in the compact view,
930 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
931 */
932 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
933
Christoph Studer943aa672014-08-03 20:31:16 +0200934 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400935 * {@link #extras} key: the username to be displayed for all messages sent by the user including
936 * direct replies
937 * {@link android.app.Notification.MessagingStyle} notification.
938 */
939 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
940
941 /**
Alex Hillsd9b04d92016-04-11 16:38:16 -0400942 * {@link #extras} key: a {@link String} to be displayed as the title to a conversation
943 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400944 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400945 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400946
947 /**
948 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
949 * bundles provided by a
950 * {@link android.app.Notification.MessagingStyle} notification.
951 */
952 public static final String EXTRA_MESSAGES = "android.messages";
953
954 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100955 * {@link #extras} key: the user that built the notification.
956 *
957 * @hide
958 */
959 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
960
961 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400962 * @hide
963 */
964 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
965
Selim Cinek247fa012016-02-18 09:50:48 -0800966 /**
967 * @hide
968 */
969 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
970
Dan Sandler80eaa592016-04-14 23:34:54 -0400971 /** @hide */
972 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400973 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
974
Dan Sandlerd63f9322015-05-06 15:18:49 -0400975 private Icon mSmallIcon;
976 private Icon mLargeIcon;
977
Chris Wren51c75102013-07-16 20:49:17 -0400978 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400979 * Structure to encapsulate a named action that can be shown as part of this notification.
980 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
981 * selected by the user.
982 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700983 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
984 * or {@link Notification.Builder#addAction(Notification.Action)}
985 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400986 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500987 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700988 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400989 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700990 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -0400991 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -0700992
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400993 /**
994 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400995 *
996 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400997 */
Dan Sandler86647982015-05-13 23:41:13 -0400998 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400999 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001000
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001001 /**
1002 * Title of the action.
1003 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001004 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001005
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001006 /**
1007 * Intent to send when the user invokes this action. May be null, in which case the action
1008 * may be rendered in a disabled presentation by the system UI.
1009 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001010 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001011
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001012 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001013 if (in.readInt() != 0) {
1014 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001015 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1016 icon = mIcon.getResId();
1017 }
Dan Sandler86647982015-05-13 23:41:13 -04001018 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001019 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1020 if (in.readInt() == 1) {
1021 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1022 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001023 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001024 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001025 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001026 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001027
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001028 /**
Dan Sandler86647982015-05-13 23:41:13 -04001029 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001030 */
Dan Sandler86647982015-05-13 23:41:13 -04001031 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001033 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001034 }
1035
Dan Sandler86647982015-05-13 23:41:13 -04001036 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001037 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001038 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001039 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1040 this.icon = icon.getResId();
1041 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001042 this.title = title;
1043 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001044 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001045 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001046 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001047 }
1048
1049 /**
Dan Sandler86647982015-05-13 23:41:13 -04001050 * Return an icon representing the action.
1051 */
1052 public Icon getIcon() {
1053 if (mIcon == null && icon != 0) {
1054 // you snuck an icon in here without using the builder; let's try to keep it
1055 mIcon = Icon.createWithResource("", icon);
1056 }
1057 return mIcon;
1058 }
1059
1060 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001061 * Get additional metadata carried around with this Action.
1062 */
1063 public Bundle getExtras() {
1064 return mExtras;
1065 }
1066
1067 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001068 * Return whether the platform should automatically generate possible replies for this
1069 * {@link Action}
1070 */
1071 public boolean getAllowGeneratedReplies() {
1072 return mAllowGeneratedReplies;
1073 }
1074
1075 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001076 * Get the list of inputs to be collected from the user when this action is sent.
1077 * May return null if no remote inputs were added.
1078 */
1079 public RemoteInput[] getRemoteInputs() {
1080 return mRemoteInputs;
1081 }
1082
1083 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001084 * Builder class for {@link Action} objects.
1085 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001086 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001087 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001088 private final CharSequence mTitle;
1089 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001090 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001091 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001092 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001093
1094 /**
1095 * Construct a new builder for {@link Action} object.
1096 * @param icon icon to show for this action
1097 * @param title the title of the action
1098 * @param intent the {@link PendingIntent} to fire when users trigger this action
1099 */
Dan Sandler86647982015-05-13 23:41:13 -04001100 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001101 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001102 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1103 }
1104
1105 /**
1106 * Construct a new builder for {@link Action} object.
1107 * @param icon icon to show for this action
1108 * @param title the title of the action
1109 * @param intent the {@link PendingIntent} to fire when users trigger this action
1110 */
1111 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001112 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001113 }
1114
1115 /**
1116 * Construct a new builder for {@link Action} object using the fields from an
1117 * {@link Action}.
1118 * @param action the action to read fields from.
1119 */
1120 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001121 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001122 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001123 }
1124
Dan Sandler86647982015-05-13 23:41:13 -04001125 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001126 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001127 mIcon = icon;
1128 mTitle = title;
1129 mIntent = intent;
1130 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001131 if (remoteInputs != null) {
1132 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1133 Collections.addAll(mRemoteInputs, remoteInputs);
1134 }
Griff Hazen959591e2014-05-15 22:26:18 -07001135 }
1136
1137 /**
1138 * Merge additional metadata into this builder.
1139 *
1140 * <p>Values within the Bundle will replace existing extras values in this Builder.
1141 *
1142 * @see Notification.Action#extras
1143 */
1144 public Builder addExtras(Bundle extras) {
1145 if (extras != null) {
1146 mExtras.putAll(extras);
1147 }
1148 return this;
1149 }
1150
1151 /**
1152 * Get the metadata Bundle used by this Builder.
1153 *
1154 * <p>The returned Bundle is shared with this Builder.
1155 */
1156 public Bundle getExtras() {
1157 return mExtras;
1158 }
1159
1160 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001161 * Add an input to be collected from the user when this action is sent.
1162 * Response values can be retrieved from the fired intent by using the
1163 * {@link RemoteInput#getResultsFromIntent} function.
1164 * @param remoteInput a {@link RemoteInput} to add to the action
1165 * @return this object for method chaining
1166 */
1167 public Builder addRemoteInput(RemoteInput remoteInput) {
1168 if (mRemoteInputs == null) {
1169 mRemoteInputs = new ArrayList<RemoteInput>();
1170 }
1171 mRemoteInputs.add(remoteInput);
1172 return this;
1173 }
1174
1175 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001176 * Set whether the platform should automatically generate possible replies to add to
1177 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1178 * {@link RemoteInput}, this has no effect.
1179 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1180 * otherwise
1181 * @return this object for method chaining
1182 * The default value is {@code false}
1183 */
1184 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1185 mAllowGeneratedReplies = allowGeneratedReplies;
1186 return this;
1187 }
1188
1189 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001190 * Apply an extender to this action builder. Extenders may be used to add
1191 * metadata or change options on this builder.
1192 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001193 public Builder extend(Extender extender) {
1194 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001195 return this;
1196 }
1197
1198 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001199 * Combine all of the options that have been set and return a new {@link Action}
1200 * object.
1201 * @return the built action
1202 */
1203 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001204 RemoteInput[] remoteInputs = mRemoteInputs != null
1205 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001206 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1207 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001208 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001209 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001210
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001211 @Override
1212 public Action clone() {
1213 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001214 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001215 title,
1216 actionIntent, // safe to alias
1217 new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001218 getRemoteInputs(),
1219 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001220 }
1221 @Override
1222 public int describeContents() {
1223 return 0;
1224 }
1225 @Override
1226 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001227 final Icon ic = getIcon();
1228 if (ic != null) {
1229 out.writeInt(1);
1230 ic.writeToParcel(out, 0);
1231 } else {
1232 out.writeInt(0);
1233 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001234 TextUtils.writeToParcel(title, out, flags);
1235 if (actionIntent != null) {
1236 out.writeInt(1);
1237 actionIntent.writeToParcel(out, flags);
1238 } else {
1239 out.writeInt(0);
1240 }
Griff Hazen959591e2014-05-15 22:26:18 -07001241 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001242 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001243 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001244 }
Griff Hazen959591e2014-05-15 22:26:18 -07001245 public static final Parcelable.Creator<Action> CREATOR =
1246 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001247 public Action createFromParcel(Parcel in) {
1248 return new Action(in);
1249 }
1250 public Action[] newArray(int size) {
1251 return new Action[size];
1252 }
1253 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001254
1255 /**
1256 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1257 * metadata or change options on an action builder.
1258 */
1259 public interface Extender {
1260 /**
1261 * Apply this extender to a notification action builder.
1262 * @param builder the builder to be modified.
1263 * @return the build object for chaining.
1264 */
1265 public Builder extend(Builder builder);
1266 }
1267
1268 /**
1269 * Wearable extender for notification actions. To add extensions to an action,
1270 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1271 * the {@code WearableExtender()} constructor and apply it to a
1272 * {@link android.app.Notification.Action.Builder} using
1273 * {@link android.app.Notification.Action.Builder#extend}.
1274 *
1275 * <pre class="prettyprint">
1276 * Notification.Action action = new Notification.Action.Builder(
1277 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001278 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001279 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001280 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001281 */
1282 public static final class WearableExtender implements Extender {
1283 /** Notification action extra which contains wearable extensions */
1284 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1285
Pete Gastaf6781d2014-10-07 15:17:05 -04001286 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001287 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001288 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1289 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1290 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001291
1292 // Flags bitwise-ored to mFlags
1293 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001294 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Griff Hazen61a9e862014-05-22 16:05:19 -07001295
1296 // Default value for flags integer
1297 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1298
1299 private int mFlags = DEFAULT_FLAGS;
1300
Pete Gastaf6781d2014-10-07 15:17:05 -04001301 private CharSequence mInProgressLabel;
1302 private CharSequence mConfirmLabel;
1303 private CharSequence mCancelLabel;
1304
Griff Hazen61a9e862014-05-22 16:05:19 -07001305 /**
1306 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1307 * options.
1308 */
1309 public WearableExtender() {
1310 }
1311
1312 /**
1313 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1314 * wearable options present in an existing notification action.
1315 * @param action the notification action to inspect.
1316 */
1317 public WearableExtender(Action action) {
1318 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1319 if (wearableBundle != null) {
1320 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001321 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1322 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1323 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001324 }
1325 }
1326
1327 /**
1328 * Apply wearable extensions to a notification action that is being built. This is
1329 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1330 * method of {@link android.app.Notification.Action.Builder}.
1331 */
1332 @Override
1333 public Action.Builder extend(Action.Builder builder) {
1334 Bundle wearableBundle = new Bundle();
1335
1336 if (mFlags != DEFAULT_FLAGS) {
1337 wearableBundle.putInt(KEY_FLAGS, mFlags);
1338 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001339 if (mInProgressLabel != null) {
1340 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1341 }
1342 if (mConfirmLabel != null) {
1343 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1344 }
1345 if (mCancelLabel != null) {
1346 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1347 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001348
1349 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1350 return builder;
1351 }
1352
1353 @Override
1354 public WearableExtender clone() {
1355 WearableExtender that = new WearableExtender();
1356 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001357 that.mInProgressLabel = this.mInProgressLabel;
1358 that.mConfirmLabel = this.mConfirmLabel;
1359 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001360 return that;
1361 }
1362
1363 /**
1364 * Set whether this action is available when the wearable device is not connected to
1365 * a companion device. The user can still trigger this action when the wearable device is
1366 * offline, but a visual hint will indicate that the action may not be available.
1367 * Defaults to true.
1368 */
1369 public WearableExtender setAvailableOffline(boolean availableOffline) {
1370 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1371 return this;
1372 }
1373
1374 /**
1375 * Get whether this action is available when the wearable device is not connected to
1376 * a companion device. The user can still trigger this action when the wearable device is
1377 * offline, but a visual hint will indicate that the action may not be available.
1378 * Defaults to true.
1379 */
1380 public boolean isAvailableOffline() {
1381 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1382 }
1383
1384 private void setFlag(int mask, boolean value) {
1385 if (value) {
1386 mFlags |= mask;
1387 } else {
1388 mFlags &= ~mask;
1389 }
1390 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001391
1392 /**
1393 * Set a label to display while the wearable is preparing to automatically execute the
1394 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1395 *
1396 * @param label the label to display while the action is being prepared to execute
1397 * @return this object for method chaining
1398 */
1399 public WearableExtender setInProgressLabel(CharSequence label) {
1400 mInProgressLabel = label;
1401 return this;
1402 }
1403
1404 /**
1405 * Get the label to display while the wearable is preparing to automatically execute
1406 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1407 *
1408 * @return the label to display while the action is being prepared to execute
1409 */
1410 public CharSequence getInProgressLabel() {
1411 return mInProgressLabel;
1412 }
1413
1414 /**
1415 * Set a label to display to confirm that the action should be executed.
1416 * This is usually an imperative verb like "Send".
1417 *
1418 * @param label the label to confirm the action should be executed
1419 * @return this object for method chaining
1420 */
1421 public WearableExtender setConfirmLabel(CharSequence label) {
1422 mConfirmLabel = label;
1423 return this;
1424 }
1425
1426 /**
1427 * Get the label to display to confirm that the action should be executed.
1428 * This is usually an imperative verb like "Send".
1429 *
1430 * @return the label to confirm the action should be executed
1431 */
1432 public CharSequence getConfirmLabel() {
1433 return mConfirmLabel;
1434 }
1435
1436 /**
1437 * Set a label to display to cancel the action.
1438 * This is usually an imperative verb, like "Cancel".
1439 *
1440 * @param label the label to display to cancel the action
1441 * @return this object for method chaining
1442 */
1443 public WearableExtender setCancelLabel(CharSequence label) {
1444 mCancelLabel = label;
1445 return this;
1446 }
1447
1448 /**
1449 * Get the label to display to cancel the action.
1450 * This is usually an imperative verb like "Cancel".
1451 *
1452 * @return the label to display to cancel the action
1453 */
1454 public CharSequence getCancelLabel() {
1455 return mCancelLabel;
1456 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001457
1458 /**
1459 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1460 * platform that it can generate the appropriate transitions.
1461 * @param hintLaunchesActivity {@code true} if the content intent will launch
1462 * an activity and transitions should be generated, false otherwise.
1463 * @return this object for method chaining
1464 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001465 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001466 boolean hintLaunchesActivity) {
1467 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1468 return this;
1469 }
1470
1471 /**
1472 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1473 * platform that it can generate the appropriate transitions
1474 * @return {@code true} if the content intent will launch an activity and transitions
1475 * should be generated, false otherwise. The default value is {@code false} if this was
1476 * never set.
1477 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001478 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001479 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1480 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001481 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001482 }
1483
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001484 /**
1485 * Array of all {@link Action} structures attached to this notification by
1486 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1487 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1488 * interface for invoking actions.
1489 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001490 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001491
1492 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001493 * Replacement version of this notification whose content will be shown
1494 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1495 * and {@link #VISIBILITY_PUBLIC}.
1496 */
1497 public Notification publicVersion;
1498
1499 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001500 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001501 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 */
1503 public Notification()
1504 {
1505 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001506 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001507 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509
1510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 * @hide
1512 */
1513 public Notification(Context context, int icon, CharSequence tickerText, long when,
1514 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1515 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001516 new Builder(context)
1517 .setWhen(when)
1518 .setSmallIcon(icon)
1519 .setTicker(tickerText)
1520 .setContentTitle(contentTitle)
1521 .setContentText(contentText)
1522 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1523 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525
1526 /**
1527 * Constructs a Notification object with the information needed to
1528 * have a status bar icon without the standard expanded view.
1529 *
1530 * @param icon The resource id of the icon to put in the status bar.
1531 * @param tickerText The text that flows by in the status bar when the notification first
1532 * activates.
1533 * @param when The time to show in the time field. In the System.currentTimeMillis
1534 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001535 *
1536 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001538 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 public Notification(int icon, CharSequence tickerText, long when)
1540 {
1541 this.icon = icon;
1542 this.tickerText = tickerText;
1543 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001544 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 }
1546
1547 /**
1548 * Unflatten the notification from a parcel.
1549 */
1550 public Notification(Parcel parcel)
1551 {
1552 int version = parcel.readInt();
1553
1554 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001555 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001556 if (parcel.readInt() != 0) {
1557 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001558 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1559 icon = mSmallIcon.getResId();
1560 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 number = parcel.readInt();
1563 if (parcel.readInt() != 0) {
1564 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1565 }
1566 if (parcel.readInt() != 0) {
1567 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1568 }
1569 if (parcel.readInt() != 0) {
1570 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1571 }
1572 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001573 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001574 }
1575 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1577 }
Joe Onorato561d3852010-11-20 18:09:34 -08001578 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001579 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 defaults = parcel.readInt();
1582 flags = parcel.readInt();
1583 if (parcel.readInt() != 0) {
1584 sound = Uri.CREATOR.createFromParcel(parcel);
1585 }
1586
1587 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001588 if (parcel.readInt() != 0) {
1589 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 vibrate = parcel.createLongArray();
1592 ledARGB = parcel.readInt();
1593 ledOnMS = parcel.readInt();
1594 ledOffMS = parcel.readInt();
1595 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001596
1597 if (parcel.readInt() != 0) {
1598 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1599 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001600
1601 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001602
John Spurlockfd7f1e02014-03-18 16:41:57 -04001603 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001604
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001605 mGroupKey = parcel.readString();
1606
1607 mSortKey = parcel.readString();
1608
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001609 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001610
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001611 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1612
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001613 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001614 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1615 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001616
Chris Wren8fd39ec2014-02-27 17:43:26 -05001617 if (parcel.readInt() != 0) {
1618 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1619 }
1620
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001621 visibility = parcel.readInt();
1622
1623 if (parcel.readInt() != 0) {
1624 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1625 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001626
1627 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
1629
Andy Stadler110988c2010-12-03 14:29:16 -08001630 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001631 public Notification clone() {
1632 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001633 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001634 return that;
1635 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001636
Daniel Sandler1a497d32013-04-18 14:52:45 -04001637 /**
1638 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1639 * of this into that.
1640 * @hide
1641 */
1642 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001643 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001644 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001645 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001646 that.number = this.number;
1647
1648 // PendingIntents are global, so there's no reason (or way) to clone them.
1649 that.contentIntent = this.contentIntent;
1650 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001651 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001652
1653 if (this.tickerText != null) {
1654 that.tickerText = this.tickerText.toString();
1655 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001656 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001657 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001658 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001659 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001660 that.contentView = this.contentView.clone();
1661 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001662 if (heavy && this.mLargeIcon != null) {
1663 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001664 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001665 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001666 that.sound = this.sound; // android.net.Uri is immutable
1667 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001668 if (this.audioAttributes != null) {
1669 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1670 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001671
1672 final long[] vibrate = this.vibrate;
1673 if (vibrate != null) {
1674 final int N = vibrate.length;
1675 final long[] vib = that.vibrate = new long[N];
1676 System.arraycopy(vibrate, 0, vib, 0, N);
1677 }
1678
1679 that.ledARGB = this.ledARGB;
1680 that.ledOnMS = this.ledOnMS;
1681 that.ledOffMS = this.ledOffMS;
1682 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001683
Joe Onorato18e69df2010-05-17 22:26:12 -07001684 that.flags = this.flags;
1685
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001686 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001687
John Spurlockfd7f1e02014-03-18 16:41:57 -04001688 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001689
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001690 that.mGroupKey = this.mGroupKey;
1691
1692 that.mSortKey = this.mSortKey;
1693
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001694 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001695 try {
1696 that.extras = new Bundle(this.extras);
1697 // will unparcel
1698 that.extras.size();
1699 } catch (BadParcelableException e) {
1700 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1701 that.extras = null;
1702 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001703 }
1704
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001705 if (this.actions != null) {
1706 that.actions = new Action[this.actions.length];
1707 for(int i=0; i<this.actions.length; i++) {
1708 that.actions[i] = this.actions[i].clone();
1709 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001710 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001711
Daniel Sandler1a497d32013-04-18 14:52:45 -04001712 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001713 that.bigContentView = this.bigContentView.clone();
1714 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001715
Chris Wren8fd39ec2014-02-27 17:43:26 -05001716 if (heavy && this.headsUpContentView != null) {
1717 that.headsUpContentView = this.headsUpContentView.clone();
1718 }
1719
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001720 that.visibility = this.visibility;
1721
1722 if (this.publicVersion != null) {
1723 that.publicVersion = new Notification();
1724 this.publicVersion.cloneInto(that.publicVersion, heavy);
1725 }
1726
Dan Sandler26e81cf2014-05-06 10:01:27 -04001727 that.color = this.color;
1728
Daniel Sandler1a497d32013-04-18 14:52:45 -04001729 if (!heavy) {
1730 that.lightenPayload(); // will clean out extras
1731 }
1732 }
1733
1734 /**
1735 * Removes heavyweight parts of the Notification object for archival or for sending to
1736 * listeners when the full contents are not necessary.
1737 * @hide
1738 */
1739 public final void lightenPayload() {
1740 tickerView = null;
1741 contentView = null;
1742 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001743 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001744 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001745 if (extras != null && !extras.isEmpty()) {
1746 final Set<String> keyset = extras.keySet();
1747 final int N = keyset.size();
1748 final String[] keys = keyset.toArray(new String[N]);
1749 for (int i=0; i<N; i++) {
1750 final String key = keys[i];
1751 final Object obj = extras.get(key);
1752 if (obj != null &&
1753 ( obj instanceof Parcelable
1754 || obj instanceof Parcelable[]
1755 || obj instanceof SparseArray
1756 || obj instanceof ArrayList)) {
1757 extras.remove(key);
1758 }
1759 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001760 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001761 }
1762
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001763 /**
1764 * Make sure this CharSequence is safe to put into a bundle, which basically
1765 * means it had better not be some custom Parcelable implementation.
1766 * @hide
1767 */
1768 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001769 if (cs == null) return cs;
1770 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1771 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1772 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001773 if (cs instanceof Parcelable) {
1774 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1775 + " instance is a custom Parcelable and not allowed in Notification");
1776 return cs.toString();
1777 }
Selim Cinek60a54252016-02-26 17:03:25 -08001778 return removeTextSizeSpans(cs);
1779 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001780
Selim Cinek60a54252016-02-26 17:03:25 -08001781 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1782 if (charSequence instanceof Spanned) {
1783 Spanned ss = (Spanned) charSequence;
1784 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1785 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1786 for (Object span : spans) {
1787 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001788 if (resultSpan instanceof CharacterStyle) {
1789 resultSpan = ((CharacterStyle) span).getUnderlying();
1790 }
1791 if (resultSpan instanceof TextAppearanceSpan) {
1792 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001793 resultSpan = new TextAppearanceSpan(
1794 originalSpan.getFamily(),
1795 originalSpan.getTextStyle(),
1796 -1,
1797 originalSpan.getTextColor(),
1798 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001799 } else if (resultSpan instanceof RelativeSizeSpan
1800 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001801 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001802 } else {
1803 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001804 }
1805 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1806 ss.getSpanFlags(span));
1807 }
1808 return builder;
1809 }
1810 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001811 }
1812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 public int describeContents() {
1814 return 0;
1815 }
1816
1817 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001818 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 */
1820 public void writeToParcel(Parcel parcel, int flags)
1821 {
1822 parcel.writeInt(1);
1823
1824 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001825 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001826 if (mSmallIcon == null && icon != 0) {
1827 // you snuck an icon in here without using the builder; let's try to keep it
1828 mSmallIcon = Icon.createWithResource("", icon);
1829 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001830 if (mSmallIcon != null) {
1831 parcel.writeInt(1);
1832 mSmallIcon.writeToParcel(parcel, 0);
1833 } else {
1834 parcel.writeInt(0);
1835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 parcel.writeInt(number);
1837 if (contentIntent != null) {
1838 parcel.writeInt(1);
1839 contentIntent.writeToParcel(parcel, 0);
1840 } else {
1841 parcel.writeInt(0);
1842 }
1843 if (deleteIntent != null) {
1844 parcel.writeInt(1);
1845 deleteIntent.writeToParcel(parcel, 0);
1846 } else {
1847 parcel.writeInt(0);
1848 }
1849 if (tickerText != null) {
1850 parcel.writeInt(1);
1851 TextUtils.writeToParcel(tickerText, parcel, flags);
1852 } else {
1853 parcel.writeInt(0);
1854 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001855 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001856 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001857 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001858 } else {
1859 parcel.writeInt(0);
1860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 if (contentView != null) {
1862 parcel.writeInt(1);
1863 contentView.writeToParcel(parcel, 0);
1864 } else {
1865 parcel.writeInt(0);
1866 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001867 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001868 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001869 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001870 } else {
1871 parcel.writeInt(0);
1872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873
1874 parcel.writeInt(defaults);
1875 parcel.writeInt(this.flags);
1876
1877 if (sound != null) {
1878 parcel.writeInt(1);
1879 sound.writeToParcel(parcel, 0);
1880 } else {
1881 parcel.writeInt(0);
1882 }
1883 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001884
1885 if (audioAttributes != null) {
1886 parcel.writeInt(1);
1887 audioAttributes.writeToParcel(parcel, 0);
1888 } else {
1889 parcel.writeInt(0);
1890 }
1891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 parcel.writeLongArray(vibrate);
1893 parcel.writeInt(ledARGB);
1894 parcel.writeInt(ledOnMS);
1895 parcel.writeInt(ledOffMS);
1896 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001897
1898 if (fullScreenIntent != null) {
1899 parcel.writeInt(1);
1900 fullScreenIntent.writeToParcel(parcel, 0);
1901 } else {
1902 parcel.writeInt(0);
1903 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001904
1905 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001906
John Spurlockfd7f1e02014-03-18 16:41:57 -04001907 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001908
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001909 parcel.writeString(mGroupKey);
1910
1911 parcel.writeString(mSortKey);
1912
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001913 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001914
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001915 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001916
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001917 if (bigContentView != null) {
1918 parcel.writeInt(1);
1919 bigContentView.writeToParcel(parcel, 0);
1920 } else {
1921 parcel.writeInt(0);
1922 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001923
Chris Wren8fd39ec2014-02-27 17:43:26 -05001924 if (headsUpContentView != null) {
1925 parcel.writeInt(1);
1926 headsUpContentView.writeToParcel(parcel, 0);
1927 } else {
1928 parcel.writeInt(0);
1929 }
1930
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001931 parcel.writeInt(visibility);
1932
1933 if (publicVersion != null) {
1934 parcel.writeInt(1);
1935 publicVersion.writeToParcel(parcel, 0);
1936 } else {
1937 parcel.writeInt(0);
1938 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001939
1940 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
1942
1943 /**
1944 * Parcelable.Creator that instantiates Notification objects
1945 */
1946 public static final Parcelable.Creator<Notification> CREATOR
1947 = new Parcelable.Creator<Notification>()
1948 {
1949 public Notification createFromParcel(Parcel parcel)
1950 {
1951 return new Notification(parcel);
1952 }
1953
1954 public Notification[] newArray(int size)
1955 {
1956 return new Notification[size];
1957 }
1958 };
1959
1960 /**
1961 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1962 * layout.
1963 *
1964 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1965 * in the view.</p>
1966 * @param context The context for your application / activity.
1967 * @param contentTitle The title that goes in the expanded entry.
1968 * @param contentText The text that goes in the expanded entry.
1969 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1970 * If this is an activity, it must include the
1971 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001972 * that you take care of task management as described in the
1973 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1974 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001975 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001976 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001977 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001979 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 public void setLatestEventInfo(Context context,
1981 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001982 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1983 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1984 new Throwable());
1985 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001986
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001987 // ensure that any information already set directly is preserved
1988 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001989
1990 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001992 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
1994 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001995 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001997 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001998
1999 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001
Julia Reynoldsda303542015-11-23 14:00:20 -05002002 /**
2003 * @hide
2004 */
2005 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002006 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2007 }
2008
2009 /**
2010 * @hide
2011 */
2012 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2013 Notification notification) {
2014 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2015 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002016 }
2017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 @Override
2019 public String toString() {
2020 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002021 sb.append("Notification(pri=");
2022 sb.append(priority);
2023 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002024 if (contentView != null) {
2025 sb.append(contentView.getPackage());
2026 sb.append("/0x");
2027 sb.append(Integer.toHexString(contentView.getLayoutId()));
2028 } else {
2029 sb.append("null");
2030 }
2031 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002032 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2033 sb.append("default");
2034 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 int N = this.vibrate.length-1;
2036 sb.append("[");
2037 for (int i=0; i<N; i++) {
2038 sb.append(this.vibrate[i]);
2039 sb.append(',');
2040 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002041 if (N != -1) {
2042 sb.append(this.vibrate[N]);
2043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 } else {
2046 sb.append("null");
2047 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002048 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002049 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002051 } else if (this.sound != null) {
2052 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 } else {
2054 sb.append("null");
2055 }
Chris Wren365b6d32015-07-16 10:39:26 -04002056 if (this.tickerText != null) {
2057 sb.append(" tick");
2058 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002059 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002061 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002062 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002063 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002064 if (this.category != null) {
2065 sb.append(" category=");
2066 sb.append(this.category);
2067 }
2068 if (this.mGroupKey != null) {
2069 sb.append(" groupKey=");
2070 sb.append(this.mGroupKey);
2071 }
2072 if (this.mSortKey != null) {
2073 sb.append(" sortKey=");
2074 sb.append(this.mSortKey);
2075 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002076 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002077 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002078 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002079 }
2080 sb.append(" vis=");
2081 sb.append(visibilityToString(this.visibility));
2082 if (this.publicVersion != null) {
2083 sb.append(" publicVersion=");
2084 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002085 }
2086 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 return sb.toString();
2088 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002089
Dan Sandler1b718782014-07-18 12:43:45 -04002090 /**
2091 * {@hide}
2092 */
2093 public static String visibilityToString(int vis) {
2094 switch (vis) {
2095 case VISIBILITY_PRIVATE:
2096 return "PRIVATE";
2097 case VISIBILITY_PUBLIC:
2098 return "PUBLIC";
2099 case VISIBILITY_SECRET:
2100 return "SECRET";
2101 default:
2102 return "UNKNOWN(" + String.valueOf(vis) + ")";
2103 }
2104 }
2105
Joe Onoratocb109a02011-01-18 17:57:41 -08002106 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002107 * {@hide}
2108 */
2109 public static String priorityToString(@Priority int pri) {
2110 switch (pri) {
2111 case PRIORITY_MIN:
2112 return "MIN";
2113 case PRIORITY_LOW:
2114 return "LOW";
2115 case PRIORITY_DEFAULT:
2116 return "DEFAULT";
2117 case PRIORITY_HIGH:
2118 return "HIGH";
2119 case PRIORITY_MAX:
2120 return "MAX";
2121 default:
2122 return "UNKNOWN(" + String.valueOf(pri) + ")";
2123 }
2124 }
2125
2126 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002127 * The small icon representing this notification in the status bar and content view.
2128 *
2129 * @return the small icon representing this notification.
2130 *
2131 * @see Builder#getSmallIcon()
2132 * @see Builder#setSmallIcon(Icon)
2133 */
2134 public Icon getSmallIcon() {
2135 return mSmallIcon;
2136 }
2137
2138 /**
2139 * Used when notifying to clean up legacy small icons.
2140 * @hide
2141 */
2142 public void setSmallIcon(Icon icon) {
2143 mSmallIcon = icon;
2144 }
2145
2146 /**
2147 * The large icon shown in this notification's content view.
2148 * @see Builder#getLargeIcon()
2149 * @see Builder#setLargeIcon(Icon)
2150 */
2151 public Icon getLargeIcon() {
2152 return mLargeIcon;
2153 }
2154
2155 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002156 * @hide
2157 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002158 public boolean isGroupSummary() {
2159 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2160 }
2161
2162 /**
2163 * @hide
2164 */
2165 public boolean isGroupChild() {
2166 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2167 }
2168
2169 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002170 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002171 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002172 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002173 * content views using the platform's notification layout template. If your app supports
2174 * versions of Android as old as API level 4, you can instead use
2175 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2176 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2177 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002178 *
Scott Main183bf112012-08-13 19:12:13 -07002179 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002180 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002181 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002182 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002183 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2184 * .setContentText(subject)
2185 * .setSmallIcon(R.drawable.new_mail)
2186 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002187 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002188 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002189 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002190 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002191 /**
2192 * @hide
2193 */
2194 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2195 "android.rebuild.contentViewActionCount";
2196 /**
2197 * @hide
2198 */
2199 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2200 = "android.rebuild.bigViewActionCount";
2201 /**
2202 * @hide
2203 */
2204 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2205 = "android.rebuild.hudViewActionCount";
2206
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002207 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002208
Joe Onorato46439ce2010-11-19 13:56:21 -08002209 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002210 private Notification mN;
2211 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002212 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002213 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2214 private ArrayList<String> mPersonList = new ArrayList<String>();
2215 private NotificationColorUtil mColorUtil;
2216 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002217
2218 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002219 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2220 */
2221 private int mCachedContrastColor = COLOR_INVALID;
2222 private int mCachedContrastColorIsFor = COLOR_INVALID;
2223
2224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002225 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002227
2228 * <table>
2229 * <tr><th align=right>priority</th>
2230 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2231 * <tr><th align=right>when</th>
2232 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2233 * <tr><th align=right>audio stream</th>
2234 * <td>{@link #STREAM_DEFAULT}</td></tr>
2235 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002236 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002237
2238 * @param context
2239 * A {@link Context} that will be used by the Builder to construct the
2240 * RemoteViews. The Context will not be held past the lifetime of this Builder
2241 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002242 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002243 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002244 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002245 }
2246
Joe Onoratocb109a02011-01-18 17:57:41 -08002247 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002248 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002249 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002250 public Builder(Context context, Notification toAdopt) {
2251 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002252
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002253 if (toAdopt == null) {
2254 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002255 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2256 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2257 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002258 mN.priority = PRIORITY_DEFAULT;
2259 mN.visibility = VISIBILITY_PRIVATE;
2260 } else {
2261 mN = toAdopt;
2262 if (mN.actions != null) {
2263 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002264 }
2265
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002266 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2267 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2268 }
2269
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002270 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2271 if (!TextUtils.isEmpty(templateClass)) {
2272 final Class<? extends Style> styleClass
2273 = getNotificationStyleClass(templateClass);
2274 if (styleClass == null) {
2275 Log.d(TAG, "Unknown style class: " + templateClass);
2276 } else {
2277 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002278 final Constructor<? extends Style> ctor =
2279 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002280 ctor.setAccessible(true);
2281 final Style style = ctor.newInstance();
2282 style.restoreFromExtras(mN.extras);
2283
2284 if (style != null) {
2285 setStyle(style);
2286 }
2287 } catch (Throwable t) {
2288 Log.e(TAG, "Could not create Style", t);
2289 }
2290 }
2291 }
2292
2293 }
2294 }
2295
2296 private NotificationColorUtil getColorUtil() {
2297 if (!mColorUtilInited) {
2298 mColorUtilInited = true;
2299 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2300 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002301 }
2302 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002303 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002304 }
2305
2306 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002307 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002308 *
2309 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2310 * shown anymore by default and must be opted into by using
2311 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002312 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002313 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002314 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002315 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002316 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002317 return this;
2318 }
2319
Joe Onoratocb109a02011-01-18 17:57:41 -08002320 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002321 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002322 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002323 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2324 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002325 */
2326 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002327 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002328 return this;
2329 }
2330
2331 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002332 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002333 *
2334 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002335 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002336 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002337 * Useful when showing an elapsed time (like an ongoing phone call).
2338 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002339 * The counter can also be set to count down to <code>when</code> when using
2340 * {@link #setChronometerCountsDown(boolean)}.
2341 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002342 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002343 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002344 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002345 */
2346 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002347 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002348 return this;
2349 }
2350
2351 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002352 * Sets the Chronometer to count down instead of counting up.
2353 *
2354 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2355 * If it isn't set the chronometer will count up.
2356 *
2357 * @see #setUsesChronometer(boolean)
2358 */
2359 public Builder setChronometerCountsDown(boolean countsDown) {
2360 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2361 return this;
2362 }
2363
2364 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002365 * Set the small icon resource, which will be used to represent the notification in the
2366 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002367 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002368
2369 * The platform template for the expanded view will draw this icon in the left, unless a
2370 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2371 * icon will be moved to the right-hand side.
2372 *
2373
2374 * @param icon
2375 * A resource ID in the application's package of the drawable to use.
2376 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002377 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002378 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002379 return setSmallIcon(icon != 0
2380 ? Icon.createWithResource(mContext, icon)
2381 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002382 }
2383
Joe Onoratocb109a02011-01-18 17:57:41 -08002384 /**
2385 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2386 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2387 * LevelListDrawable}.
2388 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002390 * @param level The level to use for the icon.
2391 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002392 * @see Notification#icon
2393 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002394 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002395 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002396 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002397 return setSmallIcon(icon);
2398 }
2399
2400 /**
2401 * Set the small icon, which will be used to represent the notification in the
2402 * status bar and content view (unless overriden there by a
2403 * {@link #setLargeIcon(Bitmap) large icon}).
2404 *
2405 * @param icon An Icon object to use.
2406 * @see Notification#icon
2407 */
2408 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002409 mN.setSmallIcon(icon);
2410 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2411 mN.icon = icon.getResId();
2412 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002413 return this;
2414 }
2415
Joe Onoratocb109a02011-01-18 17:57:41 -08002416 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002417 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002418 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002419 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002420 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002421 return this;
2422 }
2423
Joe Onoratocb109a02011-01-18 17:57:41 -08002424 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002425 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002426 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002427 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002428 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002429 return this;
2430 }
2431
Joe Onoratocb109a02011-01-18 17:57:41 -08002432 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002433 * This provides some additional information that is displayed in the notification. No
2434 * guarantees are given where exactly it is displayed.
2435 *
2436 * <p>This information should only be provided if it provides an essential
2437 * benefit to the understanding of the notification. The more text you provide the
2438 * less readable it becomes. For example, an email client should only provide the account
2439 * name here if more than one email account has been added.</p>
2440 *
2441 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2442 * notification header area.
2443 *
2444 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2445 * this will be shown in the third line of text in the platform notification template.
2446 * You should not be using {@link #setProgress(int, int, boolean)} at the
2447 * same time on those versions; they occupy the same place.
2448 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002449 */
2450 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002451 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002452 return this;
2453 }
2454
2455 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002456 * Set the remote input history.
2457 *
2458 * This should be set to the most recent inputs that have been sent
2459 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2460 * longer relevant (e.g. for chat notifications once the other party has responded).
2461 *
2462 * The most recent input must be stored at the 0 index, the second most recent at the
2463 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2464 * and how much of each individual input is shown.
2465 *
2466 * <p>Note: The reply text will only be shown on notifications that have least one action
2467 * with a {@code RemoteInput}.</p>
2468 */
2469 public Builder setRemoteInputHistory(CharSequence[] text) {
2470 if (text == null) {
2471 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2472 } else {
2473 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2474 CharSequence[] safe = new CharSequence[N];
2475 for (int i = 0; i < N; i++) {
2476 safe[i] = safeCharSequence(text[i]);
2477 }
2478 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2479 }
2480 return this;
2481 }
2482
2483 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002484 * Set the large number at the right-hand side of the notification. This is
2485 * equivalent to setContentInfo, although it might show the number in a different
2486 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002487 *
2488 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002489 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002490 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002491 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002492 return this;
2493 }
2494
Joe Onoratocb109a02011-01-18 17:57:41 -08002495 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002496 * A small piece of additional information pertaining to this notification.
2497 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002498 * The platform template will draw this on the last line of the notification, at the far
2499 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002500 *
2501 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2502 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2503 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002504 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002505 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002506 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002507 return this;
2508 }
2509
Joe Onoratocb109a02011-01-18 17:57:41 -08002510 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002511 * Set the progress this notification represents.
2512 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002513 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002514 */
2515 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002516 mN.extras.putInt(EXTRA_PROGRESS, progress);
2517 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2518 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002519 return this;
2520 }
2521
2522 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002523 * Supply a custom RemoteViews to use instead of the platform template.
2524 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002525 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002526 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002527 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002528 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002529 return setCustomContentView(views);
2530 }
2531
2532 /**
2533 * Supply custom RemoteViews to use instead of the platform template.
2534 *
2535 * This will override the layout that would otherwise be constructed by this Builder
2536 * object.
2537 */
2538 public Builder setCustomContentView(RemoteViews contentView) {
2539 mN.contentView = contentView;
2540 return this;
2541 }
2542
2543 /**
2544 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2545 *
2546 * This will override the expanded layout that would otherwise be constructed by this
2547 * Builder object.
2548 */
2549 public Builder setCustomBigContentView(RemoteViews contentView) {
2550 mN.bigContentView = contentView;
2551 return this;
2552 }
2553
2554 /**
2555 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2556 *
2557 * This will override the heads-up layout that would otherwise be constructed by this
2558 * Builder object.
2559 */
2560 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2561 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002562 return this;
2563 }
2564
Joe Onoratocb109a02011-01-18 17:57:41 -08002565 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002566 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2567 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002568 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2569 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2570 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002571 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002572 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002573 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002574 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002575 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002576 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002577 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002578 return this;
2579 }
2580
Joe Onoratocb109a02011-01-18 17:57:41 -08002581 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002582 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2583 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002584 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002585 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002586 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002587 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002588 return this;
2589 }
2590
Joe Onoratocb109a02011-01-18 17:57:41 -08002591 /**
2592 * An intent to launch instead of posting the notification to the status bar.
2593 * Only for use with extremely high-priority notifications demanding the user's
2594 * <strong>immediate</strong> attention, such as an incoming phone call or
2595 * alarm clock that the user has explicitly set to a particular time.
2596 * If this facility is used for something else, please give the user an option
2597 * to turn it off and use a normal notification, as this can be extremely
2598 * disruptive.
2599 *
Chris Wren47c20a12014-06-18 17:27:29 -04002600 * <p>
2601 * The system UI may choose to display a heads-up notification, instead of
2602 * launching this intent, while the user is using the device.
2603 * </p>
2604 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 * @param intent The pending intent to launch.
2606 * @param highPriority Passing true will cause this notification to be sent
2607 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002608 *
2609 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002610 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002611 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002612 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002613 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2614 return this;
2615 }
2616
Joe Onoratocb109a02011-01-18 17:57:41 -08002617 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002618 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002619 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002620 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002621 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002622 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002623 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002624 return this;
2625 }
2626
Joe Onoratocb109a02011-01-18 17:57:41 -08002627 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002628 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002629 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002630 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002631 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002632 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002633 setTicker(tickerText);
2634 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002635 return this;
2636 }
2637
Joe Onoratocb109a02011-01-18 17:57:41 -08002638 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002639 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002640 *
2641 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002642 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2643 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002644 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002645 public Builder setLargeIcon(Bitmap b) {
2646 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2647 }
2648
2649 /**
2650 * Add a large icon to the notification content view.
2651 *
2652 * In the platform template, this image will be shown on the left of the notification view
2653 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2654 * badge atop the large icon).
2655 */
2656 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002657 mN.mLargeIcon = icon;
2658 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002659 return this;
2660 }
2661
Joe Onoratocb109a02011-01-18 17:57:41 -08002662 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002663 * Set the sound to play.
2664 *
John Spurlockc0650f022014-07-19 13:22:39 -04002665 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2666 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002667 *
Chris Wren47c20a12014-06-18 17:27:29 -04002668 * <p>
2669 * A notification that is noisy is more likely to be presented as a heads-up notification.
2670 * </p>
2671 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002672 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002673 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002674 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002675 mN.sound = sound;
2676 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002677 return this;
2678 }
2679
Joe Onoratocb109a02011-01-18 17:57:41 -08002680 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002681 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002682 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002683 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2684 *
Chris Wren47c20a12014-06-18 17:27:29 -04002685 * <p>
2686 * A notification that is noisy is more likely to be presented as a heads-up notification.
2687 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002688 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002689 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002690 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002691 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002692 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002693 mN.sound = sound;
2694 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002695 return this;
2696 }
2697
Joe Onoratocb109a02011-01-18 17:57:41 -08002698 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002699 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2700 * use during playback.
2701 *
2702 * <p>
2703 * A notification that is noisy is more likely to be presented as a heads-up notification.
2704 * </p>
2705 *
2706 * @see Notification#sound
2707 */
2708 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002709 mN.sound = sound;
2710 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002711 return this;
2712 }
2713
2714 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002715 * Set the vibration pattern to use.
2716 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002717 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2718 * <code>pattern</code> parameter.
2719 *
Chris Wren47c20a12014-06-18 17:27:29 -04002720 * <p>
2721 * A notification that vibrates is more likely to be presented as a heads-up notification.
2722 * </p>
2723 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002724 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002725 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002726 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002727 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002728 return this;
2729 }
2730
Joe Onoratocb109a02011-01-18 17:57:41 -08002731 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002732 * Set the desired color for the indicator LED on the device, as well as the
2733 * blink duty cycle (specified in milliseconds).
2734 *
2735
2736 * Not all devices will honor all (or even any) of these values.
2737 *
2738
2739 * @see Notification#ledARGB
2740 * @see Notification#ledOnMS
2741 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002742 */
Tor Norbye80756e32015-03-02 09:39:27 -08002743 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002744 mN.ledARGB = argb;
2745 mN.ledOnMS = onMs;
2746 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002747 if (onMs != 0 || offMs != 0) {
2748 mN.flags |= FLAG_SHOW_LIGHTS;
2749 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002750 return this;
2751 }
2752
Joe Onoratocb109a02011-01-18 17:57:41 -08002753 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002754 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002755 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002756
2757 * Ongoing notifications cannot be dismissed by the user, so your application or service
2758 * must take care of canceling them.
2759 *
2760
2761 * They are typically used to indicate a background task that the user is actively engaged
2762 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2763 * (e.g., a file download, sync operation, active network connection).
2764 *
2765
2766 * @see Notification#FLAG_ONGOING_EVENT
2767 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002768 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002769 public Builder setOngoing(boolean ongoing) {
2770 setFlag(FLAG_ONGOING_EVENT, ongoing);
2771 return this;
2772 }
2773
Joe Onoratocb109a02011-01-18 17:57:41 -08002774 /**
2775 * Set this flag if you would only like the sound, vibrate
2776 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002777 *
2778 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002779 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002780 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2781 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2782 return this;
2783 }
2784
Joe Onoratocb109a02011-01-18 17:57:41 -08002785 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002786 * Make this notification automatically dismissed when the user touches it. The
2787 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2788 *
2789 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002790 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002791 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002792 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002793 return this;
2794 }
2795
Joe Onoratocb109a02011-01-18 17:57:41 -08002796 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002797 * Set whether or not this notification should not bridge to other devices.
2798 *
2799 * <p>Some notifications can be bridged to other devices for remote display.
2800 * This hint can be set to recommend this notification not be bridged.
2801 */
2802 public Builder setLocalOnly(boolean localOnly) {
2803 setFlag(FLAG_LOCAL_ONLY, localOnly);
2804 return this;
2805 }
2806
2807 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002808 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002809 * <p>
2810 * The value should be one or more of the following fields combined with
2811 * bitwise-or:
2812 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2813 * <p>
2814 * For all default values, use {@link #DEFAULT_ALL}.
2815 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002816 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002817 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002818 return this;
2819 }
2820
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002821 /**
2822 * Set the priority of this notification.
2823 *
2824 * @see Notification#priority
2825 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002826 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002827 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002828 return this;
2829 }
Joe Malin8d40d042012-11-05 11:36:40 -08002830
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002831 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002832 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002833 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002834 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002835 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002836 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002837 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002838 return this;
2839 }
2840
2841 /**
Chris Wrendde75302014-03-26 17:24:15 -04002842 * Add a person that is relevant to this notification.
2843 *
Chris Wrene6c48932014-09-29 17:19:27 -04002844 * <P>
2845 * Depending on user preferences, this annotation may allow the notification to pass
2846 * through interruption filters, and to appear more prominently in the user interface.
2847 * </P>
2848 *
2849 * <P>
2850 * The person should be specified by the {@code String} representation of a
2851 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2852 * </P>
2853 *
2854 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2855 * URIs. The path part of these URIs must exist in the contacts database, in the
2856 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2857 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2858 * </P>
2859 *
2860 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002861 * @see Notification#EXTRA_PEOPLE
2862 */
Chris Wrene6c48932014-09-29 17:19:27 -04002863 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002864 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002865 return this;
2866 }
2867
2868 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002869 * Set this notification to be part of a group of notifications sharing the same key.
2870 * Grouped notifications may display in a cluster or stack on devices which
2871 * support such rendering.
2872 *
2873 * <p>To make this notification the summary for its group, also call
2874 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2875 * {@link #setSortKey}.
2876 * @param groupKey The group key of the group.
2877 * @return this object for method chaining
2878 */
2879 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002880 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002881 return this;
2882 }
2883
2884 /**
2885 * Set this notification to be the group summary for a group of notifications.
2886 * Grouped notifications may display in a cluster or stack on devices which
2887 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2888 * @param isGroupSummary Whether this notification should be a group summary.
2889 * @return this object for method chaining
2890 */
2891 public Builder setGroupSummary(boolean isGroupSummary) {
2892 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2893 return this;
2894 }
2895
2896 /**
2897 * Set a sort key that orders this notification among other notifications from the
2898 * same package. This can be useful if an external sort was already applied and an app
2899 * would like to preserve this. Notifications will be sorted lexicographically using this
2900 * value, although providing different priorities in addition to providing sort key may
2901 * cause this value to be ignored.
2902 *
2903 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002904 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002905 *
2906 * @see String#compareTo(String)
2907 */
2908 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002909 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002910 return this;
2911 }
2912
2913 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002914 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002915 *
Griff Hazen720042b2014-02-24 15:46:56 -08002916 * <p>Values within the Bundle will replace existing extras values in this Builder.
2917 *
2918 * @see Notification#extras
2919 */
Griff Hazen959591e2014-05-15 22:26:18 -07002920 public Builder addExtras(Bundle extras) {
2921 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002922 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002923 }
2924 return this;
2925 }
2926
2927 /**
2928 * Set metadata for this notification.
2929 *
2930 * <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 -04002931 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002932 * called.
2933 *
Griff Hazen720042b2014-02-24 15:46:56 -08002934 * <p>Replaces any existing extras values with those from the provided Bundle.
2935 * Use {@link #addExtras} to merge in metadata instead.
2936 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002937 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002938 */
Griff Hazen959591e2014-05-15 22:26:18 -07002939 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002940 if (extras != null) {
2941 mUserExtras = extras;
2942 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002943 return this;
2944 }
2945
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002946 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002947 * Get the current metadata Bundle used by this notification Builder.
2948 *
2949 * <p>The returned Bundle is shared with this Builder.
2950 *
2951 * <p>The current contents of this Bundle are copied into the Notification each time
2952 * {@link #build()} is called.
2953 *
2954 * @see Notification#extras
2955 */
2956 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002957 return mUserExtras;
2958 }
2959
2960 private Bundle getAllExtras() {
2961 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2962 saveExtras.putAll(mN.extras);
2963 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002964 }
2965
2966 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002967 * Add an action to this notification. Actions are typically displayed by
2968 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002969 * <p>
2970 * Every action must have an icon (32dp square and matching the
2971 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2972 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2973 * <p>
2974 * A notification in its expanded form can display up to 3 actions, from left to right in
2975 * the order they were added. Actions will not be displayed when the notification is
2976 * collapsed, however, so be sure that any essential functions may be accessed by the user
2977 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002978 *
2979 * @param icon Resource ID of a drawable that represents the action.
2980 * @param title Text describing the action.
2981 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002982 *
2983 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002984 */
Dan Sandler86647982015-05-13 23:41:13 -04002985 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002986 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002987 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002988 return this;
2989 }
2990
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002991 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002992 * Add an action to this notification. Actions are typically displayed by
2993 * the system as a button adjacent to the notification content.
2994 * <p>
2995 * Every action must have an icon (32dp square and matching the
2996 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2997 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2998 * <p>
2999 * A notification in its expanded form can display up to 3 actions, from left to right in
3000 * the order they were added. Actions will not be displayed when the notification is
3001 * collapsed, however, so be sure that any essential functions may be accessed by the user
3002 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3003 *
3004 * @param action The action to add.
3005 */
3006 public Builder addAction(Action action) {
3007 mActions.add(action);
3008 return this;
3009 }
3010
3011 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003012 * Alter the complete list of actions attached to this notification.
3013 * @see #addAction(Action).
3014 *
3015 * @param actions
3016 * @return
3017 */
3018 public Builder setActions(Action... actions) {
3019 mActions.clear();
3020 for (int i = 0; i < actions.length; i++) {
3021 mActions.add(actions[i]);
3022 }
3023 return this;
3024 }
3025
3026 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003027 * Add a rich notification style to be applied at build time.
3028 *
3029 * @param style Object responsible for modifying the notification style.
3030 */
3031 public Builder setStyle(Style style) {
3032 if (mStyle != style) {
3033 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003034 if (mStyle != null) {
3035 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003036 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3037 } else {
3038 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003039 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003040 }
3041 return this;
3042 }
3043
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003044 /**
3045 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003046 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003047 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3048 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3049 *
3050 * @return The same Builder.
3051 */
3052 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003053 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003054 return this;
3055 }
3056
3057 /**
3058 * Supply a replacement Notification whose contents should be shown in insecure contexts
3059 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3060 * @param n A replacement notification, presumably with some or all info redacted.
3061 * @return The same Builder.
3062 */
3063 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003064 if (n != null) {
3065 mN.publicVersion = new Notification();
3066 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3067 } else {
3068 mN.publicVersion = null;
3069 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003070 return this;
3071 }
3072
Griff Hazenb720abe2014-05-20 13:15:30 -07003073 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003074 * Apply an extender to this notification builder. Extenders may be used to add
3075 * metadata or change options on this builder.
3076 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003077 public Builder extend(Extender extender) {
3078 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003079 return this;
3080 }
3081
Dan Sandler4e787062015-06-17 15:09:48 -04003082 /**
3083 * @hide
3084 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003085 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003086 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003087 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003088 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003089 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003090 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003091 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003092 }
3093
Dan Sandler26e81cf2014-05-06 10:01:27 -04003094 /**
3095 * Sets {@link Notification#color}.
3096 *
3097 * @param argb The accent color to use
3098 *
3099 * @return The same Builder.
3100 */
Tor Norbye80756e32015-03-02 09:39:27 -08003101 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003102 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003103 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003104 return this;
3105 }
3106
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003107 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003108 // Note: This assumes that the current user can read the profile badge of the
3109 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003110 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003111 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003112 }
3113
3114 private Bitmap getProfileBadge() {
3115 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003116 if (badge == null) {
3117 return null;
3118 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003119 final int size = mContext.getResources().getDimensionPixelSize(
3120 R.dimen.notification_badge_size);
3121 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003122 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003123 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003124 badge.draw(canvas);
3125 return bitmap;
3126 }
3127
Selim Cinekc848c3a2016-01-13 15:27:30 -08003128 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003129 Bitmap profileBadge = getProfileBadge();
3130
Kenny Guy98193ea2014-07-24 19:54:37 +01003131 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003132 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3133 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003134 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003135 }
3136
Christoph Studerfe718432014-09-01 18:21:18 +02003137 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003138 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003139 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003140 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003141 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003142 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003143 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003144 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003145 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003146 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003147 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003148 }
3149
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003150 /**
3151 * Resets the notification header to its original state
3152 */
3153 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003154 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003155 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003156 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003157 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003158 contentView.setViewVisibility(R.id.header_text, View.GONE);
3159 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003160 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003161 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003162 contentView.setImageViewIcon(R.id.profile_badge, null);
3163 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003164 }
3165
3166 private void resetContentMargins(RemoteViews contentView) {
3167 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003168 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003169 }
3170
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003171 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003172 return applyStandardTemplate(resId, true /* hasProgress */);
3173 }
3174
3175 /**
3176 * @param hasProgress whether the progress bar should be shown and set
3177 */
3178 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003179 final Bundle ex = mN.extras;
3180
3181 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3182 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3183 return applyStandardTemplate(resId, hasProgress, title, text);
3184 }
3185
3186 /**
3187 * @param hasProgress whether the progress bar should be shown and set
3188 */
3189 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3190 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003191 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003192
Christoph Studerfe718432014-09-01 18:21:18 +02003193 resetStandardTemplate(contentView);
3194
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003195 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003196
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003197 bindNotificationHeader(contentView);
3198 bindLargeIcon(contentView);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003199 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003200 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003201 contentView.setTextViewText(R.id.title, title);
Joe Onorato561d3852010-11-20 18:09:34 -08003202 }
Selim Cinek29603462015-11-17 19:04:39 -08003203 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003204 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003205 int textId = showProgress ? com.android.internal.R.id.text_line_1
3206 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003207 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003208 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003209 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003210
Selim Cinek860b6da2015-12-16 19:02:19 -08003211 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003212
Selim Cinek29603462015-11-17 19:04:39 -08003213 return contentView;
3214 }
3215
Selim Cinek860b6da2015-12-16 19:02:19 -08003216 /**
3217 * @param remoteView the remote view to update the minheight in
3218 * @param hasMinHeight does it have a mimHeight
3219 * @hide
3220 */
3221 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3222 int minHeight = 0;
3223 if (hasMinHeight) {
3224 // we need to set the minHeight of the notification
3225 minHeight = mContext.getResources().getDimensionPixelSize(
3226 com.android.internal.R.dimen.notification_min_content_height);
3227 }
3228 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3229 }
3230
Selim Cinek29603462015-11-17 19:04:39 -08003231 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003232 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3233 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3234 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3235 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003236 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003237 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003238 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003239 contentView.setProgressBackgroundTintList(
3240 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3241 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003242 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003243 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003244 contentView.setProgressTintList(R.id.progress, colorStateList);
3245 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003246 }
Selim Cinek29603462015-11-17 19:04:39 -08003247 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003248 } else {
3249 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003250 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003251 }
Joe Onorato561d3852010-11-20 18:09:34 -08003252 }
3253
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003254 private void bindLargeIcon(RemoteViews contentView) {
3255 if (mN.mLargeIcon != null) {
3256 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3257 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3258 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3259 int endMargin = mContext.getResources().getDimensionPixelSize(
3260 R.dimen.notification_content_picture_margin);
3261 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003262 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003263 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003264 }
3265 }
3266
3267 private void bindNotificationHeader(RemoteViews contentView) {
3268 bindSmallIcon(contentView);
3269 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003270 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003271 bindHeaderChronometerAndTime(contentView);
3272 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003273 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003274 }
3275
3276 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003277 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003278 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003279 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003280 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003281 }
3282
3283 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3284 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003285 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003286 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3287 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3288 contentView.setLong(R.id.chronometer, "setBase",
3289 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3290 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003291 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003292 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003293 } else {
3294 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3295 contentView.setLong(R.id.time, "setTime", mN.when);
3296 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003297 } else {
3298 // We still want a time to be set but gone, such that we can show and hide it
3299 // on demand in case it's a child notification without anything in the header
3300 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003301 }
3302 }
3303
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003304 private void bindHeaderText(RemoteViews contentView) {
3305 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3306 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003307 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003308 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003309 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003310 if (headerText == null
3311 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3312 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3313 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3314 }
3315 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003316 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003317 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3318 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3319 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003320 }
3321 }
3322
Adrian Rooseba05822016-04-22 17:09:27 -07003323 /**
3324 * @hide
3325 */
3326 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003327 CharSequence name = null;
3328 final PackageManager pm = mContext.getPackageManager();
3329 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3330 // only system packages which lump together a bunch of unrelated stuff
3331 // may substitute a different name to make the purpose of the
3332 // notification more clear. the correct package label should always
3333 // be accessible via SystemUI.
3334 final String pkg = mContext.getPackageName();
3335 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3336 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3337 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3338 name = subName;
3339 } else {
3340 Log.w(TAG, "warning: pkg "
3341 + pkg + " attempting to substitute app name '" + subName
3342 + "' without holding perm "
3343 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3344 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003345 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003346 if (TextUtils.isEmpty(name)) {
3347 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3348 }
3349 if (TextUtils.isEmpty(name)) {
3350 // still nothing?
3351 return null;
3352 }
3353
3354 return String.valueOf(name);
3355 }
3356 private void bindHeaderAppName(RemoteViews contentView) {
3357 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003358 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003359 }
3360
3361 private void bindSmallIcon(RemoteViews contentView) {
3362 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3363 processSmallIconColor(mN.mSmallIcon, contentView);
3364 }
3365
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003366 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003367 * @return true if the built notification will show the time or the chronometer; false
3368 * otherwise
3369 */
3370 private boolean showsTimeOrChronometer() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003371 return mN.showsTimeOrChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003372 }
3373
Christoph Studerfe718432014-09-01 18:21:18 +02003374 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003375 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003376 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003377 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003378
3379 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3380 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3381
3382 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3383 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3384 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3385 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003386 }
3387
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003388 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003389 final Bundle ex = mN.extras;
3390
3391 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3392 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3393 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3394 }
3395
3396 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3397 CharSequence title, CharSequence text) {
3398 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003399
Christoph Studerfe718432014-09-01 18:21:18 +02003400 resetStandardTemplateWithActions(big);
3401
Adrian Roose458aa82015-12-08 16:17:19 -08003402 boolean validRemoteInput = false;
3403
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003404 int N = mActions.size();
3405 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003406 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003407 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003408 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003409 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003410 Action action = mActions.get(i);
3411 validRemoteInput |= hasValidRemoteInput(action);
3412
3413 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003414 if (i == N - 1) {
3415 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3416 ViewGroup.LayoutParams.MATCH_PARENT);
3417 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003418 big.addView(R.id.actions, button);
3419 }
3420 }
Adrian Roose458aa82015-12-08 16:17:19 -08003421
3422 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3423 if (validRemoteInput && replyText != null
3424 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3425 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3426 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3427
3428 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3429 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3430 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3431
3432 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3433 big.setViewVisibility(
3434 R.id.notification_material_reply_text_3, View.VISIBLE);
3435 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3436 }
3437 }
3438 }
3439
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003440 return big;
3441 }
3442
Adrian Roose458aa82015-12-08 16:17:19 -08003443 private boolean hasValidRemoteInput(Action action) {
3444 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3445 // Weird actions
3446 return false;
3447 }
3448
3449 RemoteInput[] remoteInputs = action.getRemoteInputs();
3450 if (remoteInputs == null) {
3451 return false;
3452 }
3453
3454 for (RemoteInput r : remoteInputs) {
3455 CharSequence[] choices = r.getChoices();
3456 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3457 return true;
3458 }
3459 }
3460 return false;
3461 }
3462
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 /**
3464 * Construct a RemoteViews for the final 1U notification layout. In order:
3465 * 1. Custom contentView from the caller
3466 * 2. Style's proposed content view
3467 * 3. Standard template view
3468 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003469 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003470 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003471 return mN.contentView;
3472 } else if (mStyle != null) {
3473 final RemoteViews styleView = mStyle.makeContentView();
3474 if (styleView != null) {
3475 return styleView;
3476 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003477 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003478 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003479 }
3480
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003481 /**
3482 * Construct a RemoteViews for the final big notification layout.
3483 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003484 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003485 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003486 if (mN.bigContentView != null
3487 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003488 return mN.bigContentView;
3489 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003490 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003491 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003492 } else if (mActions.size() != 0) {
3493 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003494 }
3495 adaptNotificationHeaderForBigContentView(result);
3496 return result;
3497 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003498
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003499 /**
3500 * Construct a RemoteViews for the final notification header only
3501 *
3502 * @hide
3503 */
3504 public RemoteViews makeNotificationHeader() {
3505 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3506 R.layout.notification_template_header);
3507 resetNotificationHeader(header);
3508 bindNotificationHeader(header);
3509 return header;
3510 }
3511
Selim Cinek29603462015-11-17 19:04:39 -08003512 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003513 if (result != null) {
3514 result.setViewVisibility(R.id.text_line_1, View.GONE);
3515 }
Selim Cinek29603462015-11-17 19:04:39 -08003516 }
3517
Selim Cinek850a8542015-11-11 11:48:36 -05003518 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003519 if (result != null) {
3520 result.setBoolean(R.id.notification_header, "setExpanded", true);
3521 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003522 }
3523
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003524 /**
3525 * Construct a RemoteViews for the final heads-up notification layout.
3526 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003527 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003528 if (mN.headsUpContentView != null
3529 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003530 return mN.headsUpContentView;
3531 } else if (mStyle != null) {
3532 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3533 if (styleView != null) {
3534 return styleView;
3535 }
3536 } else if (mActions.size() == 0) {
3537 return null;
3538 }
3539
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003540 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003541 }
3542
Selim Cinek624c02db2015-12-14 21:00:02 -08003543 /**
3544 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3545 *
3546 * @hide
3547 */
3548 public RemoteViews makePublicContentView() {
3549 if (mN.publicVersion != null) {
3550 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003551 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003552 }
3553 Bundle savedBundle = mN.extras;
3554 Style style = mStyle;
3555 mStyle = null;
3556 Icon largeIcon = mN.mLargeIcon;
3557 mN.mLargeIcon = null;
3558 Bundle publicExtras = new Bundle();
3559 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3560 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3561 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3562 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003563 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3564 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003565 publicExtras.putCharSequence(EXTRA_TITLE,
3566 mContext.getString(R.string.notification_hidden_text));
3567 mN.extras = publicExtras;
3568 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3569 mN.extras = savedBundle;
3570 mN.mLargeIcon = largeIcon;
3571 mStyle = style;
3572 return publicView;
3573 }
3574
3575
Chris Wren8fd39ec2014-02-27 17:43:26 -05003576
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003577 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003578 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003579 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003580 tombstone ? getActionTombstoneLayoutResource()
3581 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003582 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003583 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003584 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003585 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003586 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003587 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003588 if (action.mRemoteInputs != null) {
3589 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3590 }
3591 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003592 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003593 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003594 return button;
3595 }
3596
Joe Onoratocb109a02011-01-18 17:57:41 -08003597 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003598 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003599 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003600 */
3601 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003602 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003603 }
3604
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003605 private CharSequence processLegacyText(CharSequence charSequence) {
3606 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003607 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003608 } else {
3609 return charSequence;
3610 }
3611 }
3612
Dan Sandler26e81cf2014-05-06 10:01:27 -04003613 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003614 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003615 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003616 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003617 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3618 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003619 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003620 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003621
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003622 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003623 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003624 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003625 }
3626
Dan Sandler26e81cf2014-05-06 10:01:27 -04003627 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003628 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003629 * if it's grayscale).
3630 */
3631 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003632 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3633 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003634 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003635 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003636 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003637 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003638 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003639 }
3640
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003641 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003642 if (mN.color != COLOR_DEFAULT) {
3643 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003644 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003645 }
3646
Adrian Roos4ff3b122016-02-01 12:26:13 -08003647 int resolveContrastColor() {
3648 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3649 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003650 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003651 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3652
3653 mCachedContrastColorIsFor = mN.color;
3654 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003655 }
3656
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003657 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003658 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003659 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003660 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003661 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003662 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003663 mN.actions = new Action[mActions.size()];
3664 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003665 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003666 if (!mPersonList.isEmpty()) {
3667 mN.extras.putStringArray(EXTRA_PEOPLE,
3668 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003669 }
Selim Cinek247fa012016-02-18 09:50:48 -08003670 if (mN.bigContentView != null || mN.contentView != null
3671 || mN.headsUpContentView != null) {
3672 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3673 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003674 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003675 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003676
Julia Reynolds3b848122016-02-26 10:45:32 -05003677 /**
3678 * Creates a Builder from an existing notification so further changes can be made.
3679 * @param context The context for your application / activity.
3680 * @param n The notification to create a Builder from.
3681 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003682 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003683 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003684 ApplicationInfo applicationInfo = n.extras.getParcelable(
3685 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003686 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003687 if (applicationInfo != null) {
3688 try {
3689 builderContext = context.createApplicationContext(applicationInfo,
3690 Context.CONTEXT_RESTRICTED);
3691 } catch (NameNotFoundException e) {
3692 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3693 builderContext = context; // try with our context
3694 }
3695 } else {
3696 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003697 }
3698
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003699 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003700 }
3701
3702 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003703 Class<? extends Style>[] classes = new Class[] {
3704 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003705 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3706 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003707 for (Class<? extends Style> innerClass : classes) {
3708 if (templateClass.equals(innerClass.getName())) {
3709 return innerClass;
3710 }
3711 }
3712 return null;
3713 }
3714
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003715 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003716 * @deprecated Use {@link #build()} instead.
3717 */
3718 @Deprecated
3719 public Notification getNotification() {
3720 return build();
3721 }
3722
3723 /**
3724 * Combine all of the options that have been set and return a new {@link Notification}
3725 * object.
3726 */
3727 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003728 // first, add any extras from the calling code
3729 if (mUserExtras != null) {
3730 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003731 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003732
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003733 mN.creationTime = System.currentTimeMillis();
3734
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003735 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003736 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003737
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003738 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003739
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003740 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003742 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003743
Adrian Roos5081c0d2016-02-26 16:04:19 -08003744 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3745 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003746 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003747 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003748 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3749 mN.contentView.getSequenceNumber());
3750 }
3751 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003752 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003753 if (mN.bigContentView != null) {
3754 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3755 mN.bigContentView.getSequenceNumber());
3756 }
3757 }
3758 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003759 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003760 if (mN.headsUpContentView != null) {
3761 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3762 mN.headsUpContentView.getSequenceNumber());
3763 }
3764 }
3765 }
3766
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003767 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3768 mN.flags |= FLAG_SHOW_LIGHTS;
3769 }
3770
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003771 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003772 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003773
3774 /**
3775 * Apply this Builder to an existing {@link Notification} object.
3776 *
3777 * @hide
3778 */
3779 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003780 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003781 return n;
3782 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003783
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003784 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003785 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3786 * change.
3787 *
3788 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3789 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003790 * @hide
3791 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003792 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003793 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003794
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003795 // Only strip views for known Styles because we won't know how to
3796 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003797 if (!TextUtils.isEmpty(templateClass)
3798 && getNotificationStyleClass(templateClass) == null) {
3799 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003800 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003801
3802 // Only strip unmodified BuilderRemoteViews.
3803 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003804 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003805 n.contentView.getSequenceNumber();
3806 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003807 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003808 n.bigContentView.getSequenceNumber();
3809 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003810 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003811 n.headsUpContentView.getSequenceNumber();
3812
3813 // Nothing to do here, no need to clone.
3814 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3815 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003816 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003817
3818 Notification clone = n.clone();
3819 if (stripContentView) {
3820 clone.contentView = null;
3821 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3822 }
3823 if (stripBigContentView) {
3824 clone.bigContentView = null;
3825 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3826 }
3827 if (stripHeadsUpContentView) {
3828 clone.headsUpContentView = null;
3829 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3830 }
3831 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003832 }
3833
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003834 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003835 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003836 }
3837
3838 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003839 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003840 }
3841
3842 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003843 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003844 }
3845
3846 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003847 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003848 }
3849
3850 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003851 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003852 }
3853
Adrian Roosc1a80b02016-04-05 14:54:55 -07003854 private int getMessagingLayoutResource() {
3855 return R.layout.notification_template_material_messaging;
3856 }
3857
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003858 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003859 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003860 }
3861
3862 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003863 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003864 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003865 }
3866
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003867 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003868 * @return true if the notification will show the time or the chronometer; false
3869 * otherwise
3870 * @hide
3871 */
3872 public boolean showsTimeOrChronometer() {
3873 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
3874 }
3875
3876 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003877 * An object that can apply a rich notification style to a {@link Notification.Builder}
3878 * object.
3879 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003880 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003881 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003882
3883 /**
3884 * @hide
3885 */
3886 protected CharSequence mSummaryText = null;
3887
3888 /**
3889 * @hide
3890 */
3891 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003892
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003893 protected Builder mBuilder;
3894
Chris Wrend6297db2012-05-03 16:20:13 -04003895 /**
3896 * Overrides ContentTitle in the big form of the template.
3897 * This defaults to the value passed to setContentTitle().
3898 */
3899 protected void internalSetBigContentTitle(CharSequence title) {
3900 mBigContentTitle = title;
3901 }
3902
3903 /**
3904 * Set the first line of text after the detail section in the big form of the template.
3905 */
3906 protected void internalSetSummaryText(CharSequence cs) {
3907 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003908 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003909 }
3910
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003911 public void setBuilder(Builder builder) {
3912 if (mBuilder != builder) {
3913 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003914 if (mBuilder != null) {
3915 mBuilder.setStyle(this);
3916 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003917 }
3918 }
3919
Chris Wrend6297db2012-05-03 16:20:13 -04003920 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003921 if (mBuilder == null) {
3922 throw new IllegalArgumentException("Style requires a valid Builder object");
3923 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003924 }
Chris Wrend6297db2012-05-03 16:20:13 -04003925
3926 protected RemoteViews getStandardView(int layoutId) {
3927 checkBuilder();
3928
Christoph Studer4600f9b2014-07-22 22:44:43 +02003929 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003930 CharSequence oldBuilderContentTitle =
3931 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003932 if (mBigContentTitle != null) {
3933 mBuilder.setContentTitle(mBigContentTitle);
3934 }
3935
Chris Wrend6297db2012-05-03 16:20:13 -04003936 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3937
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003938 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003939
Chris Wrend6297db2012-05-03 16:20:13 -04003940 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3941 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003942 } else {
3943 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003944 }
3945
Chris Wrend6297db2012-05-03 16:20:13 -04003946 return contentView;
3947 }
3948
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003949 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003950 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3951 * The default implementation has nothing additional to add.
3952 * @hide
3953 */
3954 public RemoteViews makeContentView() {
3955 return null;
3956 }
3957
3958 /**
3959 * Construct a Style-specific RemoteViews for the final big notification layout.
3960 * @hide
3961 */
3962 public RemoteViews makeBigContentView() {
3963 return null;
3964 }
3965
3966 /**
3967 * Construct a Style-specific RemoteViews for the final HUN layout.
3968 * @hide
3969 */
3970 public RemoteViews makeHeadsUpContentView() {
3971 return null;
3972 }
3973
3974 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003975 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003976 * @hide
3977 */
3978 public void addExtras(Bundle extras) {
3979 if (mSummaryTextSet) {
3980 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3981 }
3982 if (mBigContentTitle != null) {
3983 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3984 }
Chris Wren91ad5632013-06-05 15:05:57 -04003985 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003986 }
3987
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003988 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003990 * @hide
3991 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003992 protected void restoreFromExtras(Bundle extras) {
3993 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3994 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3995 mSummaryTextSet = true;
3996 }
3997 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3998 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3999 }
4000 }
4001
4002
4003 /**
4004 * @hide
4005 */
4006 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004007 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004008 return wip;
4009 }
4010
Daniel Sandler0ec46202015-06-24 01:27:05 -04004011 /**
4012 * @hide
4013 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004014 public void purgeResources() {}
4015
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004016 /**
4017 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4018 * attached to.
4019 *
4020 * @return the fully constructed Notification.
4021 */
4022 public Notification build() {
4023 checkBuilder();
4024 return mBuilder.build();
4025 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004026
4027 /**
4028 * @hide
4029 * @return true if the style positions the progress bar on the second line; false if the
4030 * style hides the progress bar
4031 */
4032 protected boolean hasProgress() {
4033 return true;
4034 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004035
4036 /**
4037 * @hide
4038 * @return Whether we should put the summary be put into the notification header
4039 */
4040 public boolean hasSummaryInHeader() {
4041 return true;
4042 }
Selim Cinek593610c2016-02-16 18:42:57 -08004043
4044 /**
4045 * @hide
4046 * @return Whether custom content views are displayed inline in the style
4047 */
4048 public boolean displayCustomViewInline() {
4049 return false;
4050 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004051 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004052
4053 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004054 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004055 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004056 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004057 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004058 * Notification notif = new Notification.Builder(mContext)
4059 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4060 * .setContentText(subject)
4061 * .setSmallIcon(R.drawable.new_post)
4062 * .setLargeIcon(aBitmap)
4063 * .setStyle(new Notification.BigPictureStyle()
4064 * .bigPicture(aBigBitmap))
4065 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004066 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004067 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004068 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004069 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004070 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004071 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004072 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004073 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004074
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004075 public BigPictureStyle() {
4076 }
4077
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004078 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004079 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004080 }
4081
Chris Wrend6297db2012-05-03 16:20:13 -04004082 /**
4083 * Overrides ContentTitle in the big form of the template.
4084 * This defaults to the value passed to setContentTitle().
4085 */
4086 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004087 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004088 return this;
4089 }
4090
4091 /**
4092 * Set the first line of text after the detail section in the big form of the template.
4093 */
4094 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004095 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004096 return this;
4097 }
4098
Chris Wren0bd664d2012-08-01 13:56:56 -04004099 /**
4100 * Provide the bitmap to be used as the payload for the BigPicture notification.
4101 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004102 public BigPictureStyle bigPicture(Bitmap b) {
4103 mPicture = b;
4104 return this;
4105 }
4106
Chris Wren3745a3d2012-05-22 15:11:52 -04004107 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004108 * Override the large icon when the big notification is shown.
4109 */
4110 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004111 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4112 }
4113
4114 /**
4115 * Override the large icon when the big notification is shown.
4116 */
4117 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004118 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004119 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004120 return this;
4121 }
4122
Riley Andrews0394a0c2015-11-03 23:36:52 -08004123 /** @hide */
4124 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4125
Daniel Sandler0ec46202015-06-24 01:27:05 -04004126 /**
4127 * @hide
4128 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004129 @Override
4130 public void purgeResources() {
4131 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004132 if (mPicture != null &&
4133 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004134 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004135 mPicture = mPicture.createAshmemBitmap();
4136 }
4137 if (mBigLargeIcon != null) {
4138 mBigLargeIcon.convertToAshmem();
4139 }
4140 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004141
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004142 /**
4143 * @hide
4144 */
4145 public RemoteViews makeBigContentView() {
4146 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004147 // This covers the following cases:
4148 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004149 // mN.mLargeIcon
4150 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004151 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004152 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004153 oldLargeIcon = mBuilder.mN.mLargeIcon;
4154 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004155 }
4156
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004157 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004158 if (mSummaryTextSet) {
4159 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004160 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004161 }
Selim Cinek860b6da2015-12-16 19:02:19 -08004162 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08004163
Christoph Studer5c510ee2014-12-15 16:32:27 +01004164 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004165 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004166 }
4167
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004168 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004169 return contentView;
4170 }
4171
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004172 /**
4173 * @hide
4174 */
4175 public void addExtras(Bundle extras) {
4176 super.addExtras(extras);
4177
4178 if (mBigLargeIconSet) {
4179 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4180 }
4181 extras.putParcelable(EXTRA_PICTURE, mPicture);
4182 }
4183
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004184 /**
4185 * @hide
4186 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004187 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004188 protected void restoreFromExtras(Bundle extras) {
4189 super.restoreFromExtras(extras);
4190
4191 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004192 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004193 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004194 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004195 mPicture = extras.getParcelable(EXTRA_PICTURE);
4196 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004197
4198 /**
4199 * @hide
4200 */
4201 @Override
4202 public boolean hasSummaryInHeader() {
4203 return false;
4204 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004205 }
4206
4207 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004208 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004209 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004210 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004211 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004212 * Notification notif = new Notification.Builder(mContext)
4213 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4214 * .setContentText(subject)
4215 * .setSmallIcon(R.drawable.new_mail)
4216 * .setLargeIcon(aBitmap)
4217 * .setStyle(new Notification.BigTextStyle()
4218 * .bigText(aVeryLongString))
4219 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004220 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004221 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004222 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004223 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004224 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004225
4226 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004227 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004228
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004229 private CharSequence mBigText;
4230
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004231 public BigTextStyle() {
4232 }
4233
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004234 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004235 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004236 }
4237
Chris Wrend6297db2012-05-03 16:20:13 -04004238 /**
4239 * Overrides ContentTitle in the big form of the template.
4240 * This defaults to the value passed to setContentTitle().
4241 */
4242 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004243 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004244 return this;
4245 }
4246
4247 /**
4248 * Set the first line of text after the detail section in the big form of the template.
4249 */
4250 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004251 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004252 return this;
4253 }
4254
Chris Wren0bd664d2012-08-01 13:56:56 -04004255 /**
4256 * Provide the longer text to be displayed in the big form of the
4257 * template in place of the content text.
4258 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004259 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004260 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004261 return this;
4262 }
4263
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004264 /**
4265 * @hide
4266 */
4267 public void addExtras(Bundle extras) {
4268 super.addExtras(extras);
4269
Christoph Studer4600f9b2014-07-22 22:44:43 +02004270 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4271 }
4272
4273 /**
4274 * @hide
4275 */
4276 @Override
4277 protected void restoreFromExtras(Bundle extras) {
4278 super.restoreFromExtras(extras);
4279
4280 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004281 }
4282
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004283 /**
4284 * @hide
4285 */
4286 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004287
4288 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004289 CharSequence oldBuilderContentText =
4290 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4291 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004292
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004293 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004294
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004295 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004296
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004297 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4298 contentView.setTextViewText(R.id.big_text, bigTextText);
4299 contentView.setViewVisibility(R.id.big_text,
4300 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004301 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004302 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4303
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004304 return contentView;
4305 }
4306
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004307 private int calculateMaxLines() {
4308 int lineCount = MAX_LINES;
4309 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004310 if (hasActions) {
4311 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4312 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004313 return lineCount;
4314 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004315 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004316
4317 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004318 * Helper class for generating large-format notifications that include multiple back-and-forth
4319 * messages of varying types between any number of people.
4320 *
4321 * <br>
4322 * If the platform does not provide large-format notifications, this method has no effect. The
4323 * user will always see the normal notification view.
4324 * <br>
4325 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4326 * so:
4327 * <pre class="prettyprint">
4328 *
4329 * Notification noti = new Notification.Builder()
4330 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4331 * .setContentText(subject)
4332 * .setSmallIcon(R.drawable.new_message)
4333 * .setLargeIcon(aBitmap)
4334 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4335 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4336 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4337 * .build();
4338 * </pre>
4339 */
4340 public static class MessagingStyle extends Style {
4341
4342 /**
4343 * The maximum number of messages that will be retained in the Notification itself (the
4344 * number displayed is up to the platform).
4345 */
4346 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4347
4348 CharSequence mUserDisplayName;
4349 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004350 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004351
4352 MessagingStyle() {
4353 }
4354
4355 /**
4356 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4357 * posting app reposts the notification with those messages after they've been actually
4358 * sent and in previous messages sent by the user added in
4359 * {@link #addMessage(Notification.MessagingStyle.Message)}
4360 */
4361 public MessagingStyle(CharSequence userDisplayName) {
4362 mUserDisplayName = userDisplayName;
4363 }
4364
4365 /**
4366 * Returns the name to be displayed for any replies sent by the user
4367 */
4368 public CharSequence getUserDisplayName() {
4369 return mUserDisplayName;
4370 }
4371
4372 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004373 * Sets the title to be displayed on this conversation. This should only be used for
4374 * group messaging and left unset for one-on-one conversations.
4375 * @param conversationTitle
4376 * @return this object for method chaining.
4377 */
4378 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4379 mConversationTitle = conversationTitle;
4380 return this;
4381 }
4382
4383 /**
4384 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4385 * should be for one-on-one conversations
4386 */
4387 public CharSequence getConversationTitle() {
4388 return mConversationTitle;
4389 }
4390
4391 /**
4392 * Adds a message for display by this notification. Convenience call for a simple
4393 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4394 * @param text A {@link CharSequence} to be displayed as the message content
4395 * @param timestamp Time at which the message arrived
4396 * @param sender A {@link CharSequence} to be used for displaying the name of the
4397 * sender. Should be <code>null</code> for messages by the current user, in which case
4398 * the platform will insert {@link #getUserDisplayName()}.
4399 * Should be unique amongst all individuals in the conversation, and should be
4400 * consistent during re-posts of the notification.
4401 *
4402 * @see Message#Message(CharSequence, long, CharSequence)
4403 *
4404 * @return this object for method chaining
4405 */
4406 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4407 mMessages.add(new Message(text, timestamp, sender));
4408 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4409 mMessages.remove(0);
4410 }
4411 return this;
4412 }
4413
4414 /**
4415 * Adds a {@link Message} for display in this notification.
4416 * @param message The {@link Message} to be displayed
4417 * @return this object for method chaining
4418 */
4419 public MessagingStyle addMessage(Message message) {
4420 mMessages.add(message);
4421 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4422 mMessages.remove(0);
4423 }
4424 return this;
4425 }
4426
4427 /**
4428 * Gets the list of {@code Message} objects that represent the notification
4429 */
4430 public List<Message> getMessages() {
4431 return mMessages;
4432 }
4433
4434 /**
4435 * @hide
4436 */
4437 @Override
4438 public void addExtras(Bundle extras) {
4439 super.addExtras(extras);
4440 if (mUserDisplayName != null) {
4441 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4442 }
4443 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004444 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004445 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004446 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4447 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004448 }
4449 }
4450
4451 /**
4452 * @hide
4453 */
4454 @Override
4455 protected void restoreFromExtras(Bundle extras) {
4456 super.restoreFromExtras(extras);
4457
4458 mMessages.clear();
4459 mUserDisplayName = extras.getString(EXTRA_SELF_DISPLAY_NAME);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004460 mConversationTitle = extras.getString(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004461 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
4462 if (parcelables != null && parcelables instanceof Bundle[]) {
4463 mMessages = Message.getMessagesFromBundleArray((Bundle[]) parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004464 }
4465 }
4466
4467 /**
4468 * @hide
4469 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004470 @Override
4471 public RemoteViews makeContentView() {
4472 Message m = findLatestIncomingMessage();
4473 CharSequence title = mConversationTitle != null
4474 ? mConversationTitle
4475 : (m == null) ? null : m.mSender;
4476 CharSequence text = (m == null)
4477 ? null
4478 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004479
Adrian Roosc1a80b02016-04-05 14:54:55 -07004480 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4481 false /* hasProgress */,
4482 title,
4483 text);
4484 }
4485
4486 private Message findLatestIncomingMessage() {
4487 for (int i = mMessages.size() - 1; i >= 0; i--) {
4488 Message m = mMessages.get(i);
4489 // Incoming messages have a non-empty sender.
4490 if (!TextUtils.isEmpty(m.mSender)) {
4491 return m;
4492 }
4493 }
4494 return null;
4495 }
4496
4497 /**
4498 * @hide
4499 */
4500 @Override
4501 public RemoteViews makeBigContentView() {
4502 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4503 ? super.mBigContentTitle
4504 : mConversationTitle;
4505 boolean hasTitle = !TextUtils.isEmpty(title);
4506
Adrian Roos48d746a2016-04-12 14:57:28 -07004507 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004508 mBuilder.getMessagingLayoutResource(),
4509 false /* hasProgress */,
4510 title,
4511 null /* text */);
4512
4513 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4514 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4515
4516 // Make sure all rows are gone in case we reuse a view.
4517 for (int rowId : rowIds) {
4518 contentView.setViewVisibility(rowId, View.GONE);
4519 }
4520
4521 int i=0;
4522 int titlePadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4523 R.dimen.notification_messaging_spacing);
4524 contentView.setViewLayoutMarginBottom(R.id.line1, hasTitle ? titlePadding : 0);
4525 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
4526 mBuilder.mN.mLargeIcon == null ? 0 : (hasTitle ? 1 : 2));
4527
4528 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4529 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4530 Message m = mMessages.get(firstMessage + i);
4531 int rowId = rowIds[i];
4532
4533 contentView.setViewVisibility(rowId, View.VISIBLE);
4534 contentView.setTextViewText(rowId, makeMessageLine(m));
4535
4536 i++;
4537 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004538 return contentView;
4539 }
4540
Adrian Roosc1a80b02016-04-05 14:54:55 -07004541 private CharSequence makeMessageLine(Message m) {
4542 BidiFormatter bidi = BidiFormatter.getInstance();
4543 SpannableStringBuilder sb = new SpannableStringBuilder();
4544 if (TextUtils.isEmpty(m.mSender)) {
4545 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4546 sb.append(bidi.unicodeWrap(replyName),
4547 makeFontColorSpan(mBuilder.resolveContrastColor()),
4548 0 /* flags */);
4549 } else {
4550 sb.append(bidi.unicodeWrap(m.mSender),
4551 makeFontColorSpan(Color.BLACK),
4552 0 /* flags */);
4553 }
4554 CharSequence text = m.mText == null ? "" : m.mText;
4555 sb.append(" ").append(bidi.unicodeWrap(text));
4556 return sb;
4557 }
4558
4559 private static TextAppearanceSpan makeFontColorSpan(int color) {
4560 return new TextAppearanceSpan(null, 0, 0,
4561 ColorStateList.valueOf(color), null);
4562 }
4563
Alex Hillsd9b04d92016-04-11 16:38:16 -04004564 public static final class Message {
4565
4566 static final String KEY_TEXT = "text";
4567 static final String KEY_TIMESTAMP = "time";
4568 static final String KEY_SENDER = "sender";
4569 static final String KEY_DATA_MIME_TYPE = "type";
4570 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004571
4572 private final CharSequence mText;
4573 private final long mTimestamp;
4574 private final CharSequence mSender;
4575
4576 private String mDataMimeType;
4577 private Uri mDataUri;
4578
4579 /**
4580 * Constructor
4581 * @param text A {@link CharSequence} to be displayed as the message content
4582 * @param timestamp Time at which the message arrived
4583 * @param sender A {@link CharSequence} to be used for displaying the name of the
4584 * sender. Should be <code>null</code> for messages by the current user, in which case
4585 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4586 * Should be unique amongst all individuals in the conversation, and should be
4587 * consistent during re-posts of the notification.
4588 */
4589 public Message(CharSequence text, long timestamp, CharSequence sender){
4590 mText = text;
4591 mTimestamp = timestamp;
4592 mSender = sender;
4593 }
4594
4595 /**
4596 * Sets a binary blob of data and an associated MIME type for a message. In the case
4597 * where the platform doesn't support the MIME type, the original text provided in the
4598 * constructor will be used.
4599 * @param dataMimeType The MIME type of the content. See
4600 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4601 * types on Android and Android Wear.
4602 * @param dataUri The uri containing the content whose type is given by the MIME type.
4603 * <p class="note">
4604 * <ol>
4605 * <li>Notification Listeners including the System UI need permission to access the
4606 * data the Uri points to. The recommended ways to do this are:</li>
4607 * <li>Store the data in your own ContentProvider, making sure that other apps have
4608 * the correct permission to access your provider. The preferred mechanism for
4609 * providing access is to use per-URI permissions which are temporary and only
4610 * grant access to the receiving application. An easy way to create a
4611 * ContentProvider like this is to use the FileProvider helper class.</li>
4612 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4613 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4614 * also store non-media types (see MediaStore.Files for more info). Files can be
4615 * inserted into the MediaStore using scanFile() after which a content:// style
4616 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4617 * Note that once added to the system MediaStore the content is accessible to any
4618 * app on the device.</li>
4619 * </ol>
4620 * @return this object for method chaining
4621 */
4622 public Message setData(String dataMimeType, Uri dataUri) {
4623 mDataMimeType = dataMimeType;
4624 mDataUri = dataUri;
4625 return this;
4626 }
4627
Alex Hillsfc737de2016-03-23 17:33:02 -04004628 /**
4629 * Get the text to be used for this message, or the fallback text if a type and content
4630 * Uri have been set
4631 */
4632 public CharSequence getText() {
4633 return mText;
4634 }
4635
4636 /**
4637 * Get the time at which this message arrived
4638 */
4639 public long getTimestamp() {
4640 return mTimestamp;
4641 }
4642
4643 /**
4644 * Get the text used to display the contact's name in the messaging experience
4645 */
4646 public CharSequence getSender() {
4647 return mSender;
4648 }
4649
4650 /**
4651 * Get the MIME type of the data pointed to by the Uri
4652 */
4653 public String getDataMimeType() {
4654 return mDataMimeType;
4655 }
4656
4657 /**
4658 * Get the the Uri pointing to the content of the message. Can be null, in which case
4659 * {@see #getText()} is used.
4660 */
4661 public Uri getDataUri() {
4662 return mDataUri;
4663 }
4664
Alex Hillsd9b04d92016-04-11 16:38:16 -04004665 private Bundle toBundle() {
4666 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04004667 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004668 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04004669 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004670 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04004671 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004672 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04004673 }
4674 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004675 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04004676 }
4677 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004678 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04004679 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004680 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04004681 }
4682
Alex Hillsd9b04d92016-04-11 16:38:16 -04004683 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
4684 Bundle[] bundles = new Bundle[messages.size()];
4685 final int N = messages.size();
4686 for (int i = 0; i < N; i++) {
4687 bundles[i] = messages.get(i).toBundle();
4688 }
4689 return bundles;
4690 }
4691
4692 static List<Message> getMessagesFromBundleArray(Bundle[] bundles) {
4693 List<Message> messages = new ArrayList<>(bundles.length);
4694 for (int i = 0; i < bundles.length; i++) {
4695 Message message = getMessageFromBundle(bundles[i]);
4696 if (message != null) {
4697 messages.add(message);
4698 }
4699 }
4700 return messages;
4701 }
4702
4703 static Message getMessageFromBundle(Bundle bundle) {
4704 try {
4705 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP) ||
4706 !bundle.containsKey(KEY_SENDER)) {
4707 return null;
4708 } else {
4709 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
4710 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
4711 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
4712 bundle.containsKey(KEY_DATA_URI)) {
4713
4714 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
4715 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04004716 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004717 return message;
4718 }
4719 } catch (ClassCastException e) {
4720 return null;
4721 }
4722 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004723 }
4724 }
4725
4726 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004727 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004728 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004729 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004730 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004731 * Notification notif = new Notification.Builder(mContext)
4732 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4733 * .setContentText(subject)
4734 * .setSmallIcon(R.drawable.new_mail)
4735 * .setLargeIcon(aBitmap)
4736 * .setStyle(new Notification.InboxStyle()
4737 * .addLine(str1)
4738 * .addLine(str2)
4739 * .setContentTitle(&quot;&quot;)
4740 * .setSummaryText(&quot;+3 more&quot;))
4741 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004742 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004743 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004744 * @see Notification#bigContentView
4745 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004746 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004747 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4748
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004749 public InboxStyle() {
4750 }
4751
Daniel Sandler879c5e02012-04-17 16:46:51 -04004752 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004753 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004754 }
4755
Chris Wrend6297db2012-05-03 16:20:13 -04004756 /**
4757 * Overrides ContentTitle in the big form of the template.
4758 * This defaults to the value passed to setContentTitle().
4759 */
4760 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004761 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004762 return this;
4763 }
4764
4765 /**
4766 * Set the first line of text after the detail section in the big form of the template.
4767 */
4768 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004769 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004770 return this;
4771 }
4772
Chris Wren0bd664d2012-08-01 13:56:56 -04004773 /**
4774 * Append a line to the digest section of the Inbox notification.
4775 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004776 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004777 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004778 return this;
4779 }
4780
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004781 /**
4782 * @hide
4783 */
4784 public void addExtras(Bundle extras) {
4785 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004786
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004787 CharSequence[] a = new CharSequence[mTexts.size()];
4788 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4789 }
4790
Christoph Studer4600f9b2014-07-22 22:44:43 +02004791 /**
4792 * @hide
4793 */
4794 @Override
4795 protected void restoreFromExtras(Bundle extras) {
4796 super.restoreFromExtras(extras);
4797
4798 mTexts.clear();
4799 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4800 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4801 }
4802 }
4803
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004804 /**
4805 * @hide
4806 */
4807 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004808 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004809 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004810 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4811 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004812
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004813 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004814
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004815 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004816
Chris Wrend6297db2012-05-03 16:20:13 -04004817 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 -04004818 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004819
Chris Wren4ed80d52012-05-17 09:30:03 -04004820 // Make sure all rows are gone in case we reuse a view.
4821 for (int rowId : rowIds) {
4822 contentView.setViewVisibility(rowId, View.GONE);
4823 }
4824
Daniel Sandler879c5e02012-04-17 16:46:51 -04004825 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07004826 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4827 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08004828 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07004829 int onlyViewId = 0;
4830 int maxRows = rowIds.length;
4831 if (mBuilder.mActions.size() > 0) {
4832 maxRows--;
4833 }
4834 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004835 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07004836 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004837 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004838 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07004839 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004840 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07004841 if (first) {
4842 onlyViewId = rowIds[i];
4843 } else {
4844 onlyViewId = 0;
4845 }
Selim Cinek247fa012016-02-18 09:50:48 -08004846 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004847 }
4848 i++;
4849 }
Selim Cinek07c80172016-04-21 16:40:47 -07004850 if (onlyViewId != 0) {
4851 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
4852 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4853 R.dimen.notification_text_margin_top);
4854 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
4855 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004856
Daniel Sandler879c5e02012-04-17 16:46:51 -04004857 return contentView;
4858 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004859
Selim Cinek247fa012016-02-18 09:50:48 -08004860 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004861 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004862 if (first) {
4863 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4864 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4865 boolean hasProgress = max != 0 || ind;
4866 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4867 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4868 R.dimen.notification_content_picture_margin);
4869 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004870 }
4871 contentView.setViewLayoutMarginEnd(id, endMargin);
4872 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004873 }
Dan Sandler842dd772014-05-15 09:36:47 -04004874
4875 /**
4876 * Notification style for media playback notifications.
4877 *
4878 * In the expanded form, {@link Notification#bigContentView}, up to 5
4879 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004880 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004881 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4882 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4883 * treated as album artwork.
4884 *
4885 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4886 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004887 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004888 * in the standard view alongside the usual content.
4889 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004890 * Notifications created with MediaStyle will have their category set to
4891 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4892 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4893 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004894 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4895 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004896 * the System UI can identify this as a notification representing an active media session
4897 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4898 *
4899 * To use this style with your Notification, feed it to
4900 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4901 * <pre class="prettyprint">
4902 * Notification noti = new Notification.Builder()
4903 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004904 * .setContentTitle(&quot;Track title&quot;)
4905 * .setContentText(&quot;Artist - Album&quot;)
4906 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004907 * .setStyle(<b>new Notification.MediaStyle()</b>
4908 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004909 * .build();
4910 * </pre>
4911 *
4912 * @see Notification#bigContentView
4913 */
4914 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004915 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004916 static final int MAX_MEDIA_BUTTONS = 5;
4917
4918 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004919 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004920
4921 public MediaStyle() {
4922 }
4923
4924 public MediaStyle(Builder builder) {
4925 setBuilder(builder);
4926 }
4927
4928 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004929 * 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 -04004930 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004931 *
4932 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004933 */
4934 public MediaStyle setShowActionsInCompactView(int...actions) {
4935 mActionsToShowInCompact = actions;
4936 return this;
4937 }
4938
4939 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004940 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4941 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004942 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004943 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004944 mToken = token;
4945 return this;
4946 }
4947
Christoph Studer4600f9b2014-07-22 22:44:43 +02004948 /**
4949 * @hide
4950 */
Dan Sandler842dd772014-05-15 09:36:47 -04004951 @Override
4952 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004953 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004954 if (wip.category == null) {
4955 wip.category = Notification.CATEGORY_TRANSPORT;
4956 }
Dan Sandler842dd772014-05-15 09:36:47 -04004957 return wip;
4958 }
4959
Christoph Studer4600f9b2014-07-22 22:44:43 +02004960 /**
4961 * @hide
4962 */
4963 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004964 public RemoteViews makeContentView() {
4965 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004966 }
4967
4968 /**
4969 * @hide
4970 */
4971 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004972 public RemoteViews makeBigContentView() {
4973 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004974 }
4975
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004976 /**
4977 * @hide
4978 */
4979 @Override
4980 public RemoteViews makeHeadsUpContentView() {
4981 RemoteViews expanded = makeMediaBigContentView();
4982 return expanded != null ? expanded : makeMediaContentView();
4983 }
4984
Dan Sandler842dd772014-05-15 09:36:47 -04004985 /** @hide */
4986 @Override
4987 public void addExtras(Bundle extras) {
4988 super.addExtras(extras);
4989
4990 if (mToken != null) {
4991 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4992 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004993 if (mActionsToShowInCompact != null) {
4994 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4995 }
Dan Sandler842dd772014-05-15 09:36:47 -04004996 }
4997
Christoph Studer4600f9b2014-07-22 22:44:43 +02004998 /**
4999 * @hide
5000 */
5001 @Override
5002 protected void restoreFromExtras(Bundle extras) {
5003 super.restoreFromExtras(extras);
5004
5005 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5006 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5007 }
5008 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5009 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5010 }
5011 }
5012
Selim Cinek5bf069a2015-11-10 19:14:27 -05005013 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005014 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005015 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005016 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005017 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005018 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5019 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005020 if (!tombstone) {
5021 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5022 }
5023 button.setContentDescription(R.id.action0, action.title);
5024 return button;
5025 }
5026
5027 private RemoteViews makeMediaContentView() {
5028 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005029 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005030
5031 final int numActions = mBuilder.mActions.size();
5032 final int N = mActionsToShowInCompact == null
5033 ? 0
5034 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5035 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005036 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005037 for (int i = 0; i < N; i++) {
5038 if (i >= numActions) {
5039 throw new IllegalArgumentException(String.format(
5040 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5041 i, numActions - 1));
5042 }
5043
5044 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005045 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005046 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005047 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005048 }
5049 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005050 handleImage(view);
5051 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08005052 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5053 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005054 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08005055 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5056 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08005057 }
5058 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005059 return view;
5060 }
5061
5062 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005063 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005064 // Dont add an expanded view if there is no more content to be revealed
5065 int actionsInCompact = mActionsToShowInCompact == null
5066 ? 0
5067 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5068 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
5069 return null;
5070 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005071 RemoteViews big = mBuilder.applyStandardTemplate(
5072 R.layout.notification_template_material_big_media,
5073 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005074
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005075 if (actionCount > 0) {
5076 big.removeAllViews(com.android.internal.R.id.media_actions);
5077 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005078 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005079 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005080 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005081 }
5082 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005083 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005084 return big;
5085 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005086
Selim Cinek5bf069a2015-11-10 19:14:27 -05005087 private void handleImage(RemoteViews contentView) {
5088 if (mBuilder.mN.mLargeIcon != null) {
5089 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005090 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005091 }
5092 }
5093
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005094 /**
5095 * @hide
5096 */
5097 @Override
5098 protected boolean hasProgress() {
5099 return false;
5100 }
Dan Sandler842dd772014-05-15 09:36:47 -04005101 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005102
Selim Cinek593610c2016-02-16 18:42:57 -08005103 /**
5104 * Notification style for custom views that are decorated by the system
5105 *
5106 * <p>Instead of providing a notification that is completely custom, a developer can set this
5107 * style and still obtain system decorations like the notification header with the expand
5108 * affordance and actions.
5109 *
5110 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5111 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5112 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5113 * corresponding custom views to display.
5114 *
5115 * To use this style with your Notification, feed it to
5116 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5117 * <pre class="prettyprint">
5118 * Notification noti = new Notification.Builder()
5119 * .setSmallIcon(R.drawable.ic_stat_player)
5120 * .setLargeIcon(albumArtBitmap))
5121 * .setCustomContentView(contentView);
5122 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5123 * .build();
5124 * </pre>
5125 */
5126 public static class DecoratedCustomViewStyle extends Style {
5127
5128 public DecoratedCustomViewStyle() {
5129 }
5130
5131 public DecoratedCustomViewStyle(Builder builder) {
5132 setBuilder(builder);
5133 }
5134
5135 /**
5136 * @hide
5137 */
5138 public boolean displayCustomViewInline() {
5139 return true;
5140 }
5141
5142 /**
5143 * @hide
5144 */
5145 @Override
5146 public RemoteViews makeContentView() {
5147 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5148 }
5149
5150 /**
5151 * @hide
5152 */
5153 @Override
5154 public RemoteViews makeBigContentView() {
5155 return makeDecoratedBigContentView();
5156 }
5157
5158 /**
5159 * @hide
5160 */
5161 @Override
5162 public RemoteViews makeHeadsUpContentView() {
5163 return makeDecoratedHeadsUpContentView();
5164 }
5165
Selim Cinek593610c2016-02-16 18:42:57 -08005166 private RemoteViews makeDecoratedHeadsUpContentView() {
5167 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5168 ? mBuilder.mN.contentView
5169 : mBuilder.mN.headsUpContentView;
5170 if (mBuilder.mActions.size() == 0) {
5171 return makeStandardTemplateWithCustomContent(headsUpContentView);
5172 }
5173 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5174 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005175 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005176 return remoteViews;
5177 }
5178
Selim Cinek593610c2016-02-16 18:42:57 -08005179 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5180 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5181 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005182 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005183 return remoteViews;
5184 }
5185
Selim Cinek593610c2016-02-16 18:42:57 -08005186 private RemoteViews makeDecoratedBigContentView() {
5187 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5188 ? mBuilder.mN.contentView
5189 : mBuilder.mN.bigContentView;
5190 if (mBuilder.mActions.size() == 0) {
5191 return makeStandardTemplateWithCustomContent(bigContentView);
5192 }
5193 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5194 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005195 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005196 return remoteViews;
5197 }
Selim Cinek247fa012016-02-18 09:50:48 -08005198
5199 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5200 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005201 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005202 // Need to clone customContent before adding, because otherwise it can no longer be
5203 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005204 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005205 remoteViews.removeAllViews(R.id.notification_main_column);
5206 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005207 }
Selim Cinek247fa012016-02-18 09:50:48 -08005208 // also update the end margin if there is an image
5209 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5210 R.dimen.notification_content_margin_end);
5211 if (mBuilder.mN.mLargeIcon != null) {
5212 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5213 R.dimen.notification_content_picture_margin);
5214 }
5215 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
5216 }
Selim Cinek593610c2016-02-16 18:42:57 -08005217 }
5218
Selim Cinek03eb3b72016-02-18 10:39:45 -08005219 /**
5220 * Notification style for media custom views that are decorated by the system
5221 *
5222 * <p>Instead of providing a media notification that is completely custom, a developer can set
5223 * this style and still obtain system decorations like the notification header with the expand
5224 * affordance and actions.
5225 *
5226 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5227 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5228 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5229 * corresponding custom views to display.
5230 *
5231 * To use this style with your Notification, feed it to
5232 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5233 * <pre class="prettyprint">
5234 * Notification noti = new Notification.Builder()
5235 * .setSmallIcon(R.drawable.ic_stat_player)
5236 * .setLargeIcon(albumArtBitmap))
5237 * .setCustomContentView(contentView);
5238 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5239 * .setMediaSession(mySession))
5240 * .build();
5241 * </pre>
5242 *
5243 * @see android.app.Notification.DecoratedCustomViewStyle
5244 * @see android.app.Notification.MediaStyle
5245 */
5246 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5247
5248 public DecoratedMediaCustomViewStyle() {
5249 }
5250
5251 public DecoratedMediaCustomViewStyle(Builder builder) {
5252 setBuilder(builder);
5253 }
5254
5255 /**
5256 * @hide
5257 */
5258 public boolean displayCustomViewInline() {
5259 return true;
5260 }
5261
5262 /**
5263 * @hide
5264 */
5265 @Override
5266 public RemoteViews makeContentView() {
5267 RemoteViews remoteViews = super.makeContentView();
5268 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5269 mBuilder.mN.contentView);
5270 }
5271
5272 /**
5273 * @hide
5274 */
5275 @Override
5276 public RemoteViews makeBigContentView() {
5277 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5278 ? mBuilder.mN.bigContentView
5279 : mBuilder.mN.contentView;
5280 return makeBigContentViewWithCustomContent(customRemoteView);
5281 }
5282
5283 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5284 RemoteViews remoteViews = super.makeBigContentView();
5285 if (remoteViews != null) {
5286 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5287 customRemoteView);
5288 } else if (customRemoteView != mBuilder.mN.contentView){
5289 remoteViews = super.makeContentView();
5290 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5291 customRemoteView);
5292 } else {
5293 return null;
5294 }
5295 }
5296
5297 /**
5298 * @hide
5299 */
5300 @Override
5301 public RemoteViews makeHeadsUpContentView() {
5302 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5303 ? mBuilder.mN.headsUpContentView
5304 : mBuilder.mN.contentView;
5305 return makeBigContentViewWithCustomContent(customRemoteView);
5306 }
5307
5308 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5309 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005310 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005311 // Need to clone customContent before adding, because otherwise it can no longer be
5312 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005313 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005314 remoteViews.removeAllViews(id);
5315 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005316 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005317 return remoteViews;
5318 }
5319 }
5320
Christoph Studer4600f9b2014-07-22 22:44:43 +02005321 // When adding a new Style subclass here, don't forget to update
5322 // Builder.getNotificationStyleClass.
5323
Griff Hazen61a9e862014-05-22 16:05:19 -07005324 /**
5325 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5326 * metadata or change options on a notification builder.
5327 */
5328 public interface Extender {
5329 /**
5330 * Apply this extender to a notification builder.
5331 * @param builder the builder to be modified.
5332 * @return the build object for chaining.
5333 */
5334 public Builder extend(Builder builder);
5335 }
5336
5337 /**
5338 * Helper class to add wearable extensions to notifications.
5339 * <p class="note"> See
5340 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5341 * for Android Wear</a> for more information on how to use this class.
5342 * <p>
5343 * To create a notification with wearable extensions:
5344 * <ol>
5345 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5346 * properties.
5347 * <li>Create a {@link android.app.Notification.WearableExtender}.
5348 * <li>Set wearable-specific properties using the
5349 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5350 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5351 * notification.
5352 * <li>Post the notification to the notification system with the
5353 * {@code NotificationManager.notify(...)} methods.
5354 * </ol>
5355 *
5356 * <pre class="prettyprint">
5357 * Notification notif = new Notification.Builder(mContext)
5358 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5359 * .setContentText(subject)
5360 * .setSmallIcon(R.drawable.new_mail)
5361 * .extend(new Notification.WearableExtender()
5362 * .setContentIcon(R.drawable.new_mail))
5363 * .build();
5364 * NotificationManager notificationManger =
5365 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5366 * notificationManger.notify(0, notif);</pre>
5367 *
5368 * <p>Wearable extensions can be accessed on an existing notification by using the
5369 * {@code WearableExtender(Notification)} constructor,
5370 * and then using the {@code get} methods to access values.
5371 *
5372 * <pre class="prettyprint">
5373 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5374 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005375 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005376 */
5377 public static final class WearableExtender implements Extender {
5378 /**
5379 * Sentinel value for an action index that is unset.
5380 */
5381 public static final int UNSET_ACTION_INDEX = -1;
5382
5383 /**
5384 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5385 * default sizing.
5386 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005387 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005388 * on their content.
5389 */
5390 public static final int SIZE_DEFAULT = 0;
5391
5392 /**
5393 * Size value for use with {@link #setCustomSizePreset} to show this notification
5394 * with an extra small size.
5395 * <p>This value is only applicable for custom display notifications created using
5396 * {@link #setDisplayIntent}.
5397 */
5398 public static final int SIZE_XSMALL = 1;
5399
5400 /**
5401 * Size value for use with {@link #setCustomSizePreset} to show this notification
5402 * with a small size.
5403 * <p>This value is only applicable for custom display notifications created using
5404 * {@link #setDisplayIntent}.
5405 */
5406 public static final int SIZE_SMALL = 2;
5407
5408 /**
5409 * Size value for use with {@link #setCustomSizePreset} to show this notification
5410 * with a medium size.
5411 * <p>This value is only applicable for custom display notifications created using
5412 * {@link #setDisplayIntent}.
5413 */
5414 public static final int SIZE_MEDIUM = 3;
5415
5416 /**
5417 * Size value for use with {@link #setCustomSizePreset} to show this notification
5418 * with a large size.
5419 * <p>This value is only applicable for custom display notifications created using
5420 * {@link #setDisplayIntent}.
5421 */
5422 public static final int SIZE_LARGE = 4;
5423
Griff Hazend5f11f92014-05-27 15:40:09 -07005424 /**
5425 * Size value for use with {@link #setCustomSizePreset} to show this notification
5426 * full screen.
5427 * <p>This value is only applicable for custom display notifications created using
5428 * {@link #setDisplayIntent}.
5429 */
5430 public static final int SIZE_FULL_SCREEN = 5;
5431
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005432 /**
5433 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5434 * short amount of time when this notification is displayed on the screen. This
5435 * is the default value.
5436 */
5437 public static final int SCREEN_TIMEOUT_SHORT = 0;
5438
5439 /**
5440 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5441 * for a longer amount of time when this notification is displayed on the screen.
5442 */
5443 public static final int SCREEN_TIMEOUT_LONG = -1;
5444
Griff Hazen61a9e862014-05-22 16:05:19 -07005445 /** Notification extra which contains wearable extensions */
5446 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5447
Pete Gastaf6781d2014-10-07 15:17:05 -04005448 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005449 private static final String KEY_ACTIONS = "actions";
5450 private static final String KEY_FLAGS = "flags";
5451 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5452 private static final String KEY_PAGES = "pages";
5453 private static final String KEY_BACKGROUND = "background";
5454 private static final String KEY_CONTENT_ICON = "contentIcon";
5455 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5456 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5457 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5458 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5459 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005460 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07005461
5462 // Flags bitwise-ored to mFlags
5463 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5464 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5465 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5466 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005467 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005468 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005469 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005470
5471 // Default value for flags integer
5472 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5473
5474 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5475 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5476
5477 private ArrayList<Action> mActions = new ArrayList<Action>();
5478 private int mFlags = DEFAULT_FLAGS;
5479 private PendingIntent mDisplayIntent;
5480 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5481 private Bitmap mBackground;
5482 private int mContentIcon;
5483 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5484 private int mContentActionIndex = UNSET_ACTION_INDEX;
5485 private int mCustomSizePreset = SIZE_DEFAULT;
5486 private int mCustomContentHeight;
5487 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005488 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005489
5490 /**
5491 * Create a {@link android.app.Notification.WearableExtender} with default
5492 * options.
5493 */
5494 public WearableExtender() {
5495 }
5496
5497 public WearableExtender(Notification notif) {
5498 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5499 if (wearableBundle != null) {
5500 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5501 if (actions != null) {
5502 mActions.addAll(actions);
5503 }
5504
5505 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5506 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5507
5508 Notification[] pages = getNotificationArrayFromBundle(
5509 wearableBundle, KEY_PAGES);
5510 if (pages != null) {
5511 Collections.addAll(mPages, pages);
5512 }
5513
5514 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5515 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5516 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5517 DEFAULT_CONTENT_ICON_GRAVITY);
5518 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5519 UNSET_ACTION_INDEX);
5520 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5521 SIZE_DEFAULT);
5522 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5523 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005524 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07005525 }
5526 }
5527
5528 /**
5529 * Apply wearable extensions to a notification that is being built. This is typically
5530 * called by the {@link android.app.Notification.Builder#extend} method of
5531 * {@link android.app.Notification.Builder}.
5532 */
5533 @Override
5534 public Notification.Builder extend(Notification.Builder builder) {
5535 Bundle wearableBundle = new Bundle();
5536
5537 if (!mActions.isEmpty()) {
5538 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5539 }
5540 if (mFlags != DEFAULT_FLAGS) {
5541 wearableBundle.putInt(KEY_FLAGS, mFlags);
5542 }
5543 if (mDisplayIntent != null) {
5544 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5545 }
5546 if (!mPages.isEmpty()) {
5547 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5548 new Notification[mPages.size()]));
5549 }
5550 if (mBackground != null) {
5551 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5552 }
5553 if (mContentIcon != 0) {
5554 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5555 }
5556 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5557 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5558 }
5559 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5560 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5561 mContentActionIndex);
5562 }
5563 if (mCustomSizePreset != SIZE_DEFAULT) {
5564 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5565 }
5566 if (mCustomContentHeight != 0) {
5567 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5568 }
5569 if (mGravity != DEFAULT_GRAVITY) {
5570 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5571 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005572 if (mHintScreenTimeout != 0) {
5573 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5574 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005575
5576 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5577 return builder;
5578 }
5579
5580 @Override
5581 public WearableExtender clone() {
5582 WearableExtender that = new WearableExtender();
5583 that.mActions = new ArrayList<Action>(this.mActions);
5584 that.mFlags = this.mFlags;
5585 that.mDisplayIntent = this.mDisplayIntent;
5586 that.mPages = new ArrayList<Notification>(this.mPages);
5587 that.mBackground = this.mBackground;
5588 that.mContentIcon = this.mContentIcon;
5589 that.mContentIconGravity = this.mContentIconGravity;
5590 that.mContentActionIndex = this.mContentActionIndex;
5591 that.mCustomSizePreset = this.mCustomSizePreset;
5592 that.mCustomContentHeight = this.mCustomContentHeight;
5593 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005594 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005595 return that;
5596 }
5597
5598 /**
5599 * Add a wearable action to this notification.
5600 *
5601 * <p>When wearable actions are added using this method, the set of actions that
5602 * show on a wearable device splits from devices that only show actions added
5603 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5604 * of which actions display on different devices.
5605 *
5606 * @param action the action to add to this notification
5607 * @return this object for method chaining
5608 * @see android.app.Notification.Action
5609 */
5610 public WearableExtender addAction(Action action) {
5611 mActions.add(action);
5612 return this;
5613 }
5614
5615 /**
5616 * Adds wearable actions to this notification.
5617 *
5618 * <p>When wearable actions are added using this method, the set of actions that
5619 * show on a wearable device splits from devices that only show actions added
5620 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5621 * of which actions display on different devices.
5622 *
5623 * @param actions the actions to add to this notification
5624 * @return this object for method chaining
5625 * @see android.app.Notification.Action
5626 */
5627 public WearableExtender addActions(List<Action> actions) {
5628 mActions.addAll(actions);
5629 return this;
5630 }
5631
5632 /**
5633 * Clear all wearable actions present on this builder.
5634 * @return this object for method chaining.
5635 * @see #addAction
5636 */
5637 public WearableExtender clearActions() {
5638 mActions.clear();
5639 return this;
5640 }
5641
5642 /**
5643 * Get the wearable actions present on this notification.
5644 */
5645 public List<Action> getActions() {
5646 return mActions;
5647 }
5648
5649 /**
5650 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005651 * this notification. The {@link PendingIntent} provided should be for an activity.
5652 *
5653 * <pre class="prettyprint">
5654 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5655 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5656 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5657 * Notification notif = new Notification.Builder(context)
5658 * .extend(new Notification.WearableExtender()
5659 * .setDisplayIntent(displayPendingIntent)
5660 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5661 * .build();</pre>
5662 *
5663 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005664 * should have an empty task affinity. It is also recommended to use the device
5665 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005666 *
5667 * <p>Example AndroidManifest.xml entry:
5668 * <pre class="prettyprint">
5669 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5670 * android:exported=&quot;true&quot;
5671 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005672 * android:taskAffinity=&quot;&quot;
5673 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005674 *
5675 * @param intent the {@link PendingIntent} for an activity
5676 * @return this object for method chaining
5677 * @see android.app.Notification.WearableExtender#getDisplayIntent
5678 */
5679 public WearableExtender setDisplayIntent(PendingIntent intent) {
5680 mDisplayIntent = intent;
5681 return this;
5682 }
5683
5684 /**
5685 * Get the intent to launch inside of an activity view when displaying this
5686 * notification. This {@code PendingIntent} should be for an activity.
5687 */
5688 public PendingIntent getDisplayIntent() {
5689 return mDisplayIntent;
5690 }
5691
5692 /**
5693 * Add an additional page of content to display with this notification. The current
5694 * notification forms the first page, and pages added using this function form
5695 * subsequent pages. This field can be used to separate a notification into multiple
5696 * sections.
5697 *
5698 * @param page the notification to add as another page
5699 * @return this object for method chaining
5700 * @see android.app.Notification.WearableExtender#getPages
5701 */
5702 public WearableExtender addPage(Notification page) {
5703 mPages.add(page);
5704 return this;
5705 }
5706
5707 /**
5708 * Add additional pages of content to display with this notification. The current
5709 * notification forms the first page, and pages added using this function form
5710 * subsequent pages. This field can be used to separate a notification into multiple
5711 * sections.
5712 *
5713 * @param pages a list of notifications
5714 * @return this object for method chaining
5715 * @see android.app.Notification.WearableExtender#getPages
5716 */
5717 public WearableExtender addPages(List<Notification> pages) {
5718 mPages.addAll(pages);
5719 return this;
5720 }
5721
5722 /**
5723 * Clear all additional pages present on this builder.
5724 * @return this object for method chaining.
5725 * @see #addPage
5726 */
5727 public WearableExtender clearPages() {
5728 mPages.clear();
5729 return this;
5730 }
5731
5732 /**
5733 * Get the array of additional pages of content for displaying this notification. The
5734 * current notification forms the first page, and elements within this array form
5735 * subsequent pages. This field can be used to separate a notification into multiple
5736 * sections.
5737 * @return the pages for this notification
5738 */
5739 public List<Notification> getPages() {
5740 return mPages;
5741 }
5742
5743 /**
5744 * Set a background image to be displayed behind the notification content.
5745 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5746 * will work with any notification style.
5747 *
5748 * @param background the background bitmap
5749 * @return this object for method chaining
5750 * @see android.app.Notification.WearableExtender#getBackground
5751 */
5752 public WearableExtender setBackground(Bitmap background) {
5753 mBackground = background;
5754 return this;
5755 }
5756
5757 /**
5758 * Get a background image to be displayed behind the notification content.
5759 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5760 * will work with any notification style.
5761 *
5762 * @return the background image
5763 * @see android.app.Notification.WearableExtender#setBackground
5764 */
5765 public Bitmap getBackground() {
5766 return mBackground;
5767 }
5768
5769 /**
5770 * Set an icon that goes with the content of this notification.
5771 */
5772 public WearableExtender setContentIcon(int icon) {
5773 mContentIcon = icon;
5774 return this;
5775 }
5776
5777 /**
5778 * Get an icon that goes with the content of this notification.
5779 */
5780 public int getContentIcon() {
5781 return mContentIcon;
5782 }
5783
5784 /**
5785 * Set the gravity that the content icon should have within the notification display.
5786 * Supported values include {@link android.view.Gravity#START} and
5787 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5788 * @see #setContentIcon
5789 */
5790 public WearableExtender setContentIconGravity(int contentIconGravity) {
5791 mContentIconGravity = contentIconGravity;
5792 return this;
5793 }
5794
5795 /**
5796 * Get the gravity that the content icon should have within the notification display.
5797 * Supported values include {@link android.view.Gravity#START} and
5798 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5799 * @see #getContentIcon
5800 */
5801 public int getContentIconGravity() {
5802 return mContentIconGravity;
5803 }
5804
5805 /**
5806 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005807 * this notification. This action will no longer display separately from the
5808 * notification's content.
5809 *
Griff Hazenca48d352014-05-28 22:37:13 -07005810 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005811 * set, although the list of available actions comes from the main notification and not
5812 * from the child page's notification.
5813 *
5814 * @param actionIndex The index of the action to hoist onto the current notification page.
5815 * If wearable actions were added to the main notification, this index
5816 * will apply to that list, otherwise it will apply to the regular
5817 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005818 */
5819 public WearableExtender setContentAction(int actionIndex) {
5820 mContentActionIndex = actionIndex;
5821 return this;
5822 }
5823
5824 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005825 * Get the index of the notification action, if any, that was specified as being clickable
5826 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005827 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005828 *
Griff Hazenca48d352014-05-28 22:37:13 -07005829 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005830 * set, although the list of available actions comes from the main notification and not
5831 * from the child page's notification.
5832 *
5833 * <p>If wearable specific actions were added to the main notification, this index will
5834 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005835 *
5836 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005837 */
5838 public int getContentAction() {
5839 return mContentActionIndex;
5840 }
5841
5842 /**
5843 * Set the gravity that this notification should have within the available viewport space.
5844 * Supported values include {@link android.view.Gravity#TOP},
5845 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5846 * The default value is {@link android.view.Gravity#BOTTOM}.
5847 */
5848 public WearableExtender setGravity(int gravity) {
5849 mGravity = gravity;
5850 return this;
5851 }
5852
5853 /**
5854 * Get the gravity that this notification should have within the available viewport space.
5855 * Supported values include {@link android.view.Gravity#TOP},
5856 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5857 * The default value is {@link android.view.Gravity#BOTTOM}.
5858 */
5859 public int getGravity() {
5860 return mGravity;
5861 }
5862
5863 /**
5864 * Set the custom size preset for the display of this notification out of the available
5865 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5866 * {@link #SIZE_LARGE}.
5867 * <p>Some custom size presets are only applicable for custom display notifications created
5868 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5869 * documentation for the preset in question. See also
5870 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5871 */
5872 public WearableExtender setCustomSizePreset(int sizePreset) {
5873 mCustomSizePreset = sizePreset;
5874 return this;
5875 }
5876
5877 /**
5878 * Get the custom size preset for the display of this notification out of the available
5879 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5880 * {@link #SIZE_LARGE}.
5881 * <p>Some custom size presets are only applicable for custom display notifications created
5882 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5883 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5884 */
5885 public int getCustomSizePreset() {
5886 return mCustomSizePreset;
5887 }
5888
5889 /**
5890 * Set the custom height in pixels for the display of this notification's content.
5891 * <p>This option is only available for custom display notifications created
5892 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5893 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5894 * {@link #getCustomContentHeight}.
5895 */
5896 public WearableExtender setCustomContentHeight(int height) {
5897 mCustomContentHeight = height;
5898 return this;
5899 }
5900
5901 /**
5902 * Get the custom height in pixels for the display of this notification's content.
5903 * <p>This option is only available for custom display notifications created
5904 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5905 * {@link #setCustomContentHeight}.
5906 */
5907 public int getCustomContentHeight() {
5908 return mCustomContentHeight;
5909 }
5910
5911 /**
5912 * Set whether the scrolling position for the contents of this notification should start
5913 * at the bottom of the contents instead of the top when the contents are too long to
5914 * display within the screen. Default is false (start scroll at the top).
5915 */
5916 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5917 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5918 return this;
5919 }
5920
5921 /**
5922 * Get whether the scrolling position for the contents of this notification should start
5923 * at the bottom of the contents instead of the top when the contents are too long to
5924 * display within the screen. Default is false (start scroll at the top).
5925 */
5926 public boolean getStartScrollBottom() {
5927 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5928 }
5929
5930 /**
5931 * Set whether the content intent is available when the wearable device is not connected
5932 * to a companion device. The user can still trigger this intent when the wearable device
5933 * is offline, but a visual hint will indicate that the content intent may not be available.
5934 * Defaults to true.
5935 */
5936 public WearableExtender setContentIntentAvailableOffline(
5937 boolean contentIntentAvailableOffline) {
5938 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5939 return this;
5940 }
5941
5942 /**
5943 * Get whether the content intent is available when the wearable device is not connected
5944 * to a companion device. The user can still trigger this intent when the wearable device
5945 * is offline, but a visual hint will indicate that the content intent may not be available.
5946 * Defaults to true.
5947 */
5948 public boolean getContentIntentAvailableOffline() {
5949 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5950 }
5951
5952 /**
5953 * Set a hint that this notification's icon should not be displayed.
5954 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5955 * @return this object for method chaining
5956 */
5957 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5958 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5959 return this;
5960 }
5961
5962 /**
5963 * Get a hint that this notification's icon should not be displayed.
5964 * @return {@code true} if this icon should not be displayed, false otherwise.
5965 * The default value is {@code false} if this was never set.
5966 */
5967 public boolean getHintHideIcon() {
5968 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5969 }
5970
5971 /**
5972 * Set a visual hint that only the background image of this notification should be
5973 * displayed, and other semantic content should be hidden. This hint is only applicable
5974 * to sub-pages added using {@link #addPage}.
5975 */
5976 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5977 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5978 return this;
5979 }
5980
5981 /**
5982 * Get a visual hint that only the background image of this notification should be
5983 * displayed, and other semantic content should be hidden. This hint is only applicable
5984 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5985 */
5986 public boolean getHintShowBackgroundOnly() {
5987 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5988 }
5989
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005990 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005991 * Set a hint that this notification's background should not be clipped if possible,
5992 * and should instead be resized to fully display on the screen, retaining the aspect
5993 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005994 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5995 * @return this object for method chaining
5996 */
5997 public WearableExtender setHintAvoidBackgroundClipping(
5998 boolean hintAvoidBackgroundClipping) {
5999 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6000 return this;
6001 }
6002
6003 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006004 * Get a hint that this notification's background should not be clipped if possible,
6005 * and should instead be resized to fully display on the screen, retaining the aspect
6006 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006007 * @return {@code true} if it's ok if the background is clipped on the screen, false
6008 * otherwise. The default value is {@code false} if this was never set.
6009 */
6010 public boolean getHintAvoidBackgroundClipping() {
6011 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6012 }
6013
6014 /**
6015 * Set a hint that the screen should remain on for at least this duration when
6016 * this notification is displayed on the screen.
6017 * @param timeout The requested screen timeout in milliseconds. Can also be either
6018 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6019 * @return this object for method chaining
6020 */
6021 public WearableExtender setHintScreenTimeout(int timeout) {
6022 mHintScreenTimeout = timeout;
6023 return this;
6024 }
6025
6026 /**
6027 * Get the duration, in milliseconds, that the screen should remain on for
6028 * when this notification is displayed.
6029 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6030 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6031 */
6032 public int getHintScreenTimeout() {
6033 return mHintScreenTimeout;
6034 }
6035
Alex Hills9ab3a232016-04-05 14:54:56 -04006036 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006037 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6038 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6039 * qr codes, as well as other simple black-and-white tickets.
6040 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6041 * @return this object for method chaining
6042 */
6043 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6044 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6045 return this;
6046 }
6047
6048 /**
6049 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6050 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6051 * qr codes, as well as other simple black-and-white tickets.
6052 * @return {@code true} if it should be displayed in ambient, false otherwise
6053 * otherwise. The default value is {@code false} if this was never set.
6054 */
6055 public boolean getHintAmbientBigPicture() {
6056 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6057 }
6058
6059 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006060 * Set a hint that this notification's content intent will launch an {@link Activity}
6061 * directly, telling the platform that it can generate the appropriate transitions.
6062 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6063 * an activity and transitions should be generated, false otherwise.
6064 * @return this object for method chaining
6065 */
6066 public WearableExtender setHintContentIntentLaunchesActivity(
6067 boolean hintContentIntentLaunchesActivity) {
6068 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6069 return this;
6070 }
6071
6072 /**
6073 * Get a hint that this notification's content intent will launch an {@link Activity}
6074 * directly, telling the platform that it can generate the appropriate transitions
6075 * @return {@code true} if the content intent will launch an activity and transitions should
6076 * be generated, false otherwise. The default value is {@code false} if this was never set.
6077 */
6078 public boolean getHintContentIntentLaunchesActivity() {
6079 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6080 }
6081
Griff Hazen61a9e862014-05-22 16:05:19 -07006082 private void setFlag(int mask, boolean value) {
6083 if (value) {
6084 mFlags |= mask;
6085 } else {
6086 mFlags &= ~mask;
6087 }
6088 }
6089 }
6090
6091 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006092 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6093 * with car extensions:
6094 *
6095 * <ol>
6096 * <li>Create an {@link Notification.Builder}, setting any desired
6097 * properties.
6098 * <li>Create a {@link CarExtender}.
6099 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6100 * {@link CarExtender}.
6101 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6102 * to apply the extensions to a notification.
6103 * </ol>
6104 *
6105 * <pre class="prettyprint">
6106 * Notification notification = new Notification.Builder(context)
6107 * ...
6108 * .extend(new CarExtender()
6109 * .set*(...))
6110 * .build();
6111 * </pre>
6112 *
6113 * <p>Car extensions can be accessed on an existing notification by using the
6114 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6115 * to access values.
6116 */
6117 public static final class CarExtender implements Extender {
6118 private static final String TAG = "CarExtender";
6119
6120 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6121 private static final String EXTRA_LARGE_ICON = "large_icon";
6122 private static final String EXTRA_CONVERSATION = "car_conversation";
6123 private static final String EXTRA_COLOR = "app_color";
6124
6125 private Bitmap mLargeIcon;
6126 private UnreadConversation mUnreadConversation;
6127 private int mColor = Notification.COLOR_DEFAULT;
6128
6129 /**
6130 * Create a {@link CarExtender} with default options.
6131 */
6132 public CarExtender() {
6133 }
6134
6135 /**
6136 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6137 *
6138 * @param notif The notification from which to copy options.
6139 */
6140 public CarExtender(Notification notif) {
6141 Bundle carBundle = notif.extras == null ?
6142 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6143 if (carBundle != null) {
6144 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6145 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6146
6147 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6148 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6149 }
6150 }
6151
6152 /**
6153 * Apply car extensions to a notification that is being built. This is typically called by
6154 * the {@link Notification.Builder#extend(Notification.Extender)}
6155 * method of {@link Notification.Builder}.
6156 */
6157 @Override
6158 public Notification.Builder extend(Notification.Builder builder) {
6159 Bundle carExtensions = new Bundle();
6160
6161 if (mLargeIcon != null) {
6162 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6163 }
6164 if (mColor != Notification.COLOR_DEFAULT) {
6165 carExtensions.putInt(EXTRA_COLOR, mColor);
6166 }
6167
6168 if (mUnreadConversation != null) {
6169 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6170 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6171 }
6172
6173 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6174 return builder;
6175 }
6176
6177 /**
6178 * Sets the accent color to use when Android Auto presents the notification.
6179 *
6180 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6181 * to accent the displayed notification. However, not all colors are acceptable in an
6182 * automotive setting. This method can be used to override the color provided in the
6183 * notification in such a situation.
6184 */
Tor Norbye80756e32015-03-02 09:39:27 -08006185 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006186 mColor = color;
6187 return this;
6188 }
6189
6190 /**
6191 * Gets the accent color.
6192 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006193 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006194 */
Tor Norbye80756e32015-03-02 09:39:27 -08006195 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006196 public int getColor() {
6197 return mColor;
6198 }
6199
6200 /**
6201 * Sets the large icon of the car notification.
6202 *
6203 * If no large icon is set in the extender, Android Auto will display the icon
6204 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6205 *
6206 * @param largeIcon The large icon to use in the car notification.
6207 * @return This object for method chaining.
6208 */
6209 public CarExtender setLargeIcon(Bitmap largeIcon) {
6210 mLargeIcon = largeIcon;
6211 return this;
6212 }
6213
6214 /**
6215 * Gets the large icon used in this car notification, or null if no icon has been set.
6216 *
6217 * @return The large icon for the car notification.
6218 * @see CarExtender#setLargeIcon
6219 */
6220 public Bitmap getLargeIcon() {
6221 return mLargeIcon;
6222 }
6223
6224 /**
6225 * Sets the unread conversation in a message notification.
6226 *
6227 * @param unreadConversation The unread part of the conversation this notification conveys.
6228 * @return This object for method chaining.
6229 */
6230 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6231 mUnreadConversation = unreadConversation;
6232 return this;
6233 }
6234
6235 /**
6236 * Returns the unread conversation conveyed by this notification.
6237 * @see #setUnreadConversation(UnreadConversation)
6238 */
6239 public UnreadConversation getUnreadConversation() {
6240 return mUnreadConversation;
6241 }
6242
6243 /**
6244 * A class which holds the unread messages from a conversation.
6245 */
6246 public static class UnreadConversation {
6247 private static final String KEY_AUTHOR = "author";
6248 private static final String KEY_TEXT = "text";
6249 private static final String KEY_MESSAGES = "messages";
6250 private static final String KEY_REMOTE_INPUT = "remote_input";
6251 private static final String KEY_ON_REPLY = "on_reply";
6252 private static final String KEY_ON_READ = "on_read";
6253 private static final String KEY_PARTICIPANTS = "participants";
6254 private static final String KEY_TIMESTAMP = "timestamp";
6255
6256 private final String[] mMessages;
6257 private final RemoteInput mRemoteInput;
6258 private final PendingIntent mReplyPendingIntent;
6259 private final PendingIntent mReadPendingIntent;
6260 private final String[] mParticipants;
6261 private final long mLatestTimestamp;
6262
6263 UnreadConversation(String[] messages, RemoteInput remoteInput,
6264 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6265 String[] participants, long latestTimestamp) {
6266 mMessages = messages;
6267 mRemoteInput = remoteInput;
6268 mReadPendingIntent = readPendingIntent;
6269 mReplyPendingIntent = replyPendingIntent;
6270 mParticipants = participants;
6271 mLatestTimestamp = latestTimestamp;
6272 }
6273
6274 /**
6275 * Gets the list of messages conveyed by this notification.
6276 */
6277 public String[] getMessages() {
6278 return mMessages;
6279 }
6280
6281 /**
6282 * Gets the remote input that will be used to convey the response to a message list, or
6283 * null if no such remote input exists.
6284 */
6285 public RemoteInput getRemoteInput() {
6286 return mRemoteInput;
6287 }
6288
6289 /**
6290 * Gets the pending intent that will be triggered when the user replies to this
6291 * notification.
6292 */
6293 public PendingIntent getReplyPendingIntent() {
6294 return mReplyPendingIntent;
6295 }
6296
6297 /**
6298 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6299 * in this object's message list.
6300 */
6301 public PendingIntent getReadPendingIntent() {
6302 return mReadPendingIntent;
6303 }
6304
6305 /**
6306 * Gets the participants in the conversation.
6307 */
6308 public String[] getParticipants() {
6309 return mParticipants;
6310 }
6311
6312 /**
6313 * Gets the firs participant in the conversation.
6314 */
6315 public String getParticipant() {
6316 return mParticipants.length > 0 ? mParticipants[0] : null;
6317 }
6318
6319 /**
6320 * Gets the timestamp of the conversation.
6321 */
6322 public long getLatestTimestamp() {
6323 return mLatestTimestamp;
6324 }
6325
6326 Bundle getBundleForUnreadConversation() {
6327 Bundle b = new Bundle();
6328 String author = null;
6329 if (mParticipants != null && mParticipants.length > 1) {
6330 author = mParticipants[0];
6331 }
6332 Parcelable[] messages = new Parcelable[mMessages.length];
6333 for (int i = 0; i < messages.length; i++) {
6334 Bundle m = new Bundle();
6335 m.putString(KEY_TEXT, mMessages[i]);
6336 m.putString(KEY_AUTHOR, author);
6337 messages[i] = m;
6338 }
6339 b.putParcelableArray(KEY_MESSAGES, messages);
6340 if (mRemoteInput != null) {
6341 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6342 }
6343 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6344 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6345 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6346 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6347 return b;
6348 }
6349
6350 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6351 if (b == null) {
6352 return null;
6353 }
6354 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6355 String[] messages = null;
6356 if (parcelableMessages != null) {
6357 String[] tmp = new String[parcelableMessages.length];
6358 boolean success = true;
6359 for (int i = 0; i < tmp.length; i++) {
6360 if (!(parcelableMessages[i] instanceof Bundle)) {
6361 success = false;
6362 break;
6363 }
6364 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6365 if (tmp[i] == null) {
6366 success = false;
6367 break;
6368 }
6369 }
6370 if (success) {
6371 messages = tmp;
6372 } else {
6373 return null;
6374 }
6375 }
6376
6377 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6378 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6379
6380 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6381
6382 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6383 if (participants == null || participants.length != 1) {
6384 return null;
6385 }
6386
6387 return new UnreadConversation(messages,
6388 remoteInput,
6389 onReply,
6390 onRead,
6391 participants, b.getLong(KEY_TIMESTAMP));
6392 }
6393 };
6394
6395 /**
6396 * Builder class for {@link CarExtender.UnreadConversation} objects.
6397 */
6398 public static class Builder {
6399 private final List<String> mMessages = new ArrayList<String>();
6400 private final String mParticipant;
6401 private RemoteInput mRemoteInput;
6402 private PendingIntent mReadPendingIntent;
6403 private PendingIntent mReplyPendingIntent;
6404 private long mLatestTimestamp;
6405
6406 /**
6407 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6408 *
6409 * @param name The name of the other participant in the conversation.
6410 */
6411 public Builder(String name) {
6412 mParticipant = name;
6413 }
6414
6415 /**
6416 * Appends a new unread message to the list of messages for this conversation.
6417 *
6418 * The messages should be added from oldest to newest.
6419 *
6420 * @param message The text of the new unread message.
6421 * @return This object for method chaining.
6422 */
6423 public Builder addMessage(String message) {
6424 mMessages.add(message);
6425 return this;
6426 }
6427
6428 /**
6429 * Sets the pending intent and remote input which will convey the reply to this
6430 * notification.
6431 *
6432 * @param pendingIntent The pending intent which will be triggered on a reply.
6433 * @param remoteInput The remote input parcelable which will carry the reply.
6434 * @return This object for method chaining.
6435 *
6436 * @see CarExtender.UnreadConversation#getRemoteInput
6437 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6438 */
6439 public Builder setReplyAction(
6440 PendingIntent pendingIntent, RemoteInput remoteInput) {
6441 mRemoteInput = remoteInput;
6442 mReplyPendingIntent = pendingIntent;
6443
6444 return this;
6445 }
6446
6447 /**
6448 * Sets the pending intent that will be sent once the messages in this notification
6449 * are read.
6450 *
6451 * @param pendingIntent The pending intent to use.
6452 * @return This object for method chaining.
6453 */
6454 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6455 mReadPendingIntent = pendingIntent;
6456 return this;
6457 }
6458
6459 /**
6460 * Sets the timestamp of the most recent message in an unread conversation.
6461 *
6462 * If a messaging notification has been posted by your application and has not
6463 * yet been cancelled, posting a later notification with the same id and tag
6464 * but without a newer timestamp may result in Android Auto not displaying a
6465 * heads up notification for the later notification.
6466 *
6467 * @param timestamp The timestamp of the most recent message in the conversation.
6468 * @return This object for method chaining.
6469 */
6470 public Builder setLatestTimestamp(long timestamp) {
6471 mLatestTimestamp = timestamp;
6472 return this;
6473 }
6474
6475 /**
6476 * Builds a new unread conversation object.
6477 *
6478 * @return The new unread conversation object.
6479 */
6480 public UnreadConversation build() {
6481 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6482 String[] participants = { mParticipant };
6483 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6484 mReadPendingIntent, participants, mLatestTimestamp);
6485 }
6486 }
6487 }
6488
6489 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006490 * Get an array of Notification objects from a parcelable array bundle field.
6491 * Update the bundle to have a typed array so fetches in the future don't need
6492 * to do an array copy.
6493 */
6494 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6495 Parcelable[] array = bundle.getParcelableArray(key);
6496 if (array instanceof Notification[] || array == null) {
6497 return (Notification[]) array;
6498 }
6499 Notification[] typedArray = Arrays.copyOf(array, array.length,
6500 Notification[].class);
6501 bundle.putParcelableArray(key, typedArray);
6502 return typedArray;
6503 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006504
6505 private static class BuilderRemoteViews extends RemoteViews {
6506 public BuilderRemoteViews(Parcel parcel) {
6507 super(parcel);
6508 }
6509
Kenny Guy77320062014-08-27 21:37:15 +01006510 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6511 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006512 }
6513
6514 @Override
6515 public BuilderRemoteViews clone() {
6516 Parcel p = Parcel.obtain();
6517 writeToParcel(p, 0);
6518 p.setDataPosition(0);
6519 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6520 p.recycle();
6521 return brv;
6522 }
6523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524}