blob: 5dd7b1ade37754c5cf17b93bc0ed163542c6dd93 [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;
Griff Hazen61a9e862014-05-22 16:05:19 -070058import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080059import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080060import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070061import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.widget.RemoteViews;
63
Griff Hazen959591e2014-05-15 22:26:18 -070064import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070065import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070066
Tor Norbyed9273d62013-05-30 15:59:53 -070067import java.lang.annotation.Retention;
68import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020069import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050070import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070071import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070072import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070073import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050074import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076/**
77 * A class that represents how a persistent notification is to be presented to
78 * the user using the {@link android.app.NotificationManager}.
79 *
Joe Onoratocb109a02011-01-18 17:57:41 -080080 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
81 * easier to construct Notifications.</p>
82 *
Joe Fernandez558459f2011-10-13 16:47:36 -070083 * <div class="special reference">
84 * <h3>Developer Guides</h3>
85 * <p>For a guide to creating notifications, read the
86 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
87 * developer guide.</p>
88 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 */
90public class Notification implements Parcelable
91{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040092 private static final String TAG = "Notification";
93
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040095 * An activity that provides a user interface for adjusting notification preferences for its
96 * containing application. Optional but recommended for apps that post
97 * {@link android.app.Notification Notifications}.
98 */
99 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
100 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
101 = "android.intent.category.NOTIFICATION_PREFERENCES";
102
103 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * Use all default values (where applicable).
105 */
106 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 /**
109 * Use the default notification sound. This will ignore any given
110 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111 *
Chris Wren47c20a12014-06-18 17:27:29 -0400112 * <p>
113 * A notification that is noisy is more likely to be presented as a heads-up notification.
114 * </p>
115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 */
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public static final int DEFAULT_SOUND = 1;
120
121 /**
122 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700124 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500125 *
Chris Wren47c20a12014-06-18 17:27:29 -0400126 * <p>
127 * A notification that vibrates is more likely to be presented as a heads-up notification.
128 * </p>
129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131 */
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
136 * Use the default notification lights. This will ignore the
137 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
138 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 */
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200146 * Maximum length of CharSequences accepted by Builder and friends.
147 *
148 * <p>
149 * Avoids spamming the system with overly large strings such as full e-mails.
150 */
151 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
152
153 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800154 * Maximum entries of reply text that are accepted by Builder and friends.
155 */
156 private static final int MAX_REPLY_HISTORY = 5;
157
158 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500159 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800160 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161 * Default value: {@link System#currentTimeMillis() Now}.
162 *
163 * Choose a timestamp that will be most relevant to the user. For most finite events, this
164 * corresponds to the time the event happened (or will happen, in the case of events that have
165 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800166 * timestamped according to when the activity began.
167 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800169 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 * <ul>
171 * <li>Notification of a new chat message should be stamped when the message was received.</li>
172 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
173 * <li>Notification of a completed file download should be stamped when the download finished.</li>
174 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
175 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
176 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800177 * </ul>
178 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700179 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
180 * anymore by default and must be opted into by using
181 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 */
183 public long when;
184
185 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700186 * The creation time of the notification
187 */
188 private long creationTime;
189
190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400192 *
193 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
Dan Sandler86647982015-05-13 23:41:13 -0400195 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700196 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public int icon;
198
199 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800200 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
201 * leave it at its default value of 0.
202 *
203 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700204 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800205 */
206 public int iconLevel;
207
208 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500209 * The number of events that this notification represents. For example, in a new mail
210 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800211 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500212 * The system may or may not use this field to modify the appearance of the notification. For
213 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
214 * superimposed over the icon in the status bar. Starting with
215 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
216 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800217 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500218 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700219 *
220 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 */
222 public int number;
223
224 /**
225 * The intent to execute when the expanded status entry is clicked. If
226 * this is an activity, it must include the
227 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800228 * that you take care of task management as described in the
229 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800230 * Stack</a> document. In particular, make sure to read the notification section
231 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
232 * Notifications</a> for the correct ways to launch an application from a
233 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 */
235 public PendingIntent contentIntent;
236
237 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500238 * The intent to execute when the notification is explicitly dismissed by the user, either with
239 * the "Clear All" button or by swiping it away individually.
240 *
241 * This probably shouldn't be launching an activity since several of those will be sent
242 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 */
244 public PendingIntent deleteIntent;
245
246 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700247 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800248 *
Chris Wren47c20a12014-06-18 17:27:29 -0400249 * <p>
250 * The system UI may choose to display a heads-up notification, instead of
251 * launching this intent, while the user is using the device.
252 * </p>
253 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800254 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400255 */
256 public PendingIntent fullScreenIntent;
257
258 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400259 * Text that summarizes this notification for accessibility services.
260 *
261 * As of the L release, this text is no longer shown on screen, but it is still useful to
262 * accessibility services (where it serves as an audible announcement of the notification's
263 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400264 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800265 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 */
267 public CharSequence tickerText;
268
269 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400270 * Formerly, a view showing the {@link #tickerText}.
271 *
272 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400273 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400274 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800275 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400276
277 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400278 * The view that will represent this notification in the notification list (which is pulled
279 * down from the status bar).
280 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500281 * As of N, this field may be null. The notification view is determined by the inputs
282 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400283 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400285 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 public RemoteViews contentView;
287
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400288 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400289 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400290 * opportunity to show more detail. The system UI may choose to show this
291 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400292 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500293 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400294 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
295 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400296 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400297 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400298 public RemoteViews bigContentView;
299
Chris Wren8fd39ec2014-02-27 17:43:26 -0500300
301 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400302 * A medium-format version of {@link #contentView}, providing the Notification an
303 * opportunity to add action buttons to contentView. At its discretion, the system UI may
304 * choose to show this as a heads-up notification, which will pop up so the user can see
305 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400306 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500307 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400308 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
309 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500310 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400311 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500312 public RemoteViews headsUpContentView;
313
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400314 /**
Dan Sandler86647982015-05-13 23:41:13 -0400315 * A large bitmap to be shown in the notification content area.
316 *
317 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 */
Dan Sandler86647982015-05-13 23:41:13 -0400319 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800320 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321
322 /**
323 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500324 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400326 * A notification that is noisy is more likely to be presented as a heads-up notification.
327 * </p>
328 *
329 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500330 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * </p>
332 */
333 public Uri sound;
334
335 /**
336 * Use this constant as the value for audioStreamType to request that
337 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700338 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400339 *
340 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700342 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 public static final int STREAM_DEFAULT = -1;
344
345 /**
346 * The audio stream type to use when playing the sound.
347 * Should be one of the STREAM_ constants from
348 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400349 *
350 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700352 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 public int audioStreamType = STREAM_DEFAULT;
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400356 * The default value of {@link #audioAttributes}.
357 */
358 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
359 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
360 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
361 .build();
362
363 /**
364 * The {@link AudioAttributes audio attributes} to use when playing the sound.
365 */
366 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
367
368 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500369 * The pattern with which to vibrate.
370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 * <p>
372 * To vibrate the default pattern, see {@link #defaults}.
373 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500374 *
Chris Wren47c20a12014-06-18 17:27:29 -0400375 * <p>
376 * A notification that vibrates is more likely to be presented as a heads-up notification.
377 * </p>
378 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 * @see android.os.Vibrator#vibrate(long[],int)
380 */
381 public long[] vibrate;
382
383 /**
384 * The color of the led. The hardware will do its best approximation.
385 *
386 * @see #FLAG_SHOW_LIGHTS
387 * @see #flags
388 */
Tor Norbye80756e32015-03-02 09:39:27 -0800389 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 public int ledARGB;
391
392 /**
393 * The number of milliseconds for the LED to be on while it's flashing.
394 * The hardware will do its best approximation.
395 *
396 * @see #FLAG_SHOW_LIGHTS
397 * @see #flags
398 */
399 public int ledOnMS;
400
401 /**
402 * The number of milliseconds for the LED to be off while it's flashing.
403 * The hardware will do its best approximation.
404 *
405 * @see #FLAG_SHOW_LIGHTS
406 * @see #flags
407 */
408 public int ledOffMS;
409
410 /**
411 * Specifies which values should be taken from the defaults.
412 * <p>
413 * To set, OR the desired from {@link #DEFAULT_SOUND},
414 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
415 * values, use {@link #DEFAULT_ALL}.
416 * </p>
417 */
418 public int defaults;
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 /**
421 * Bit to be bitwise-ored into the {@link #flags} field that should be
422 * set if you want the LED on for this notification.
423 * <ul>
424 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
425 * or 0 for both ledOnMS and ledOffMS.</li>
426 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
427 * <li>To flash the LED, pass the number of milliseconds that it should
428 * be on and off to ledOnMS and ledOffMS.</li>
429 * </ul>
430 * <p>
431 * Since hardware varies, you are not guaranteed that any of the values
432 * you pass are honored exactly. Use the system defaults (TODO) if possible
433 * because they will be set to values that work on any given hardware.
434 * <p>
435 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 */
438 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
439
440 /**
441 * Bit to be bitwise-ored into the {@link #flags} field that should be
442 * set if this notification is in reference to something that is ongoing,
443 * like a phone call. It should not be set if this notification is in
444 * reference to something that happened at a particular point in time,
445 * like a missed phone call.
446 */
447 public static final int FLAG_ONGOING_EVENT = 0x00000002;
448
449 /**
450 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700451 * the audio will be repeated until the notification is
452 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 */
454 public static final int FLAG_INSISTENT = 0x00000004;
455
456 /**
457 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700458 * set if you would only like the sound, vibrate and ticker to be played
459 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
461 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
462
463 /**
464 * Bit to be bitwise-ored into the {@link #flags} field that should be
465 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500466 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 */
468 public static final int FLAG_AUTO_CANCEL = 0x00000010;
469
470 /**
471 * Bit to be bitwise-ored into the {@link #flags} field that should be
472 * set if the notification should not be canceled when the user clicks
473 * the Clear all button.
474 */
475 public static final int FLAG_NO_CLEAR = 0x00000020;
476
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700477 /**
478 * Bit to be bitwise-ored into the {@link #flags} field that should be
479 * set if this notification represents a currently running service. This
480 * will normally be set for you by {@link Service#startForeground}.
481 */
482 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
483
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400484 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500485 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800486 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500487 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400488 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500489 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400490
Griff Hazendfcb0802014-02-11 12:00:00 -0800491 /**
492 * Bit to be bitswise-ored into the {@link #flags} field that should be
493 * set if this notification is relevant to the current device only
494 * and it is not recommended that it bridge to other devices.
495 */
496 public static final int FLAG_LOCAL_ONLY = 0x00000100;
497
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700498 /**
499 * Bit to be bitswise-ored into the {@link #flags} field that should be
500 * set if this notification is the group summary for a group of notifications.
501 * Grouped notifications may display in a cluster or stack on devices which
502 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
503 */
504 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
505
Julia Reynoldse46bb372016-03-17 11:05:58 -0400506 /**
507 * Bit to be bitswise-ored into the {@link #flags} field that should be
508 * set if this notification is the group summary for an auto-group of notifications.
509 *
510 * @hide
511 */
512 @SystemApi
513 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 public int flags;
516
Tor Norbyed9273d62013-05-30 15:59:53 -0700517 /** @hide */
518 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
519 @Retention(RetentionPolicy.SOURCE)
520 public @interface Priority {}
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500523 * Default notification {@link #priority}. If your application does not prioritize its own
524 * notifications, use this value for all notifications.
525 */
526 public static final int PRIORITY_DEFAULT = 0;
527
528 /**
529 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
530 * items smaller, or at a different position in the list, compared with your app's
531 * {@link #PRIORITY_DEFAULT} items.
532 */
533 public static final int PRIORITY_LOW = -1;
534
535 /**
536 * Lowest {@link #priority}; these items might not be shown to the user except under special
537 * circumstances, such as detailed notification logs.
538 */
539 public static final int PRIORITY_MIN = -2;
540
541 /**
542 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
543 * show these items larger, or at a different position in notification lists, compared with
544 * your app's {@link #PRIORITY_DEFAULT} items.
545 */
546 public static final int PRIORITY_HIGH = 1;
547
548 /**
549 * Highest {@link #priority}, for your application's most important items that require the
550 * user's prompt attention or input.
551 */
552 public static final int PRIORITY_MAX = 2;
553
554 /**
555 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800556 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500557 * Priority is an indication of how much of the user's valuable attention should be consumed by
558 * this notification. Low-priority notifications may be hidden from the user in certain
559 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500560 * system will make a determination about how to interpret this priority when presenting
561 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400562 *
563 * <p>
564 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
565 * as a heads-up notification.
566 * </p>
567 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500568 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700569 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800571
Dan Sandler26e81cf2014-05-06 10:01:27 -0400572 /**
573 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
574 * to be applied by the standard Style templates when presenting this notification.
575 *
576 * The current template design constructs a colorful header image by overlaying the
577 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
578 * ignored.
579 */
Tor Norbye80756e32015-03-02 09:39:27 -0800580 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400581 public int color = COLOR_DEFAULT;
582
583 /**
584 * Special value of {@link #color} telling the system not to decorate this notification with
585 * any special color but instead use default colors when presenting this notification.
586 */
Tor Norbye80756e32015-03-02 09:39:27 -0800587 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400588 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600589
590 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800591 * Special value of {@link #color} used as a place holder for an invalid color.
592 */
593 @ColorInt
594 private static final int COLOR_INVALID = 1;
595
596 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700597 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
598 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600599 * lockscreen).
600 *
601 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
602 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
603 * shown in all situations, but the contents are only available if the device is unlocked for
604 * the appropriate user.
605 *
606 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
607 * can be read even in an "insecure" context (that is, above a secure lockscreen).
608 * To modify the public version of this notification—for example, to redact some portions—see
609 * {@link Builder#setPublicVersion(Notification)}.
610 *
611 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
612 * and ticker until the user has bypassed the lockscreen.
613 */
614 public int visibility;
615
Griff Hazenfc3922d2014-08-20 11:56:44 -0700616 /**
617 * Notification visibility: Show this notification in its entirety on all lockscreens.
618 *
619 * {@see #visibility}
620 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600621 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700622
623 /**
624 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
625 * private information on secure lockscreens.
626 *
627 * {@see #visibility}
628 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600629 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700630
631 /**
632 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
633 *
634 * {@see #visibility}
635 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600636 public static final int VISIBILITY_SECRET = -1;
637
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500638 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400639 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500640 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400641 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500642
643 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400644 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400646 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647
648 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400649 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400651 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652
653 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400654 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400656 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657
658 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400659 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400661 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662
663 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400664 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400666 public static final String CATEGORY_ALARM = "alarm";
667
668 /**
669 * Notification category: progress of a long-running background operation.
670 */
671 public static final String CATEGORY_PROGRESS = "progress";
672
673 /**
674 * Notification category: social network or sharing update.
675 */
676 public static final String CATEGORY_SOCIAL = "social";
677
678 /**
679 * Notification category: error in background operation or authentication status.
680 */
681 public static final String CATEGORY_ERROR = "err";
682
683 /**
684 * Notification category: media transport control for playback.
685 */
686 public static final String CATEGORY_TRANSPORT = "transport";
687
688 /**
689 * Notification category: system or device status update. Reserved for system use.
690 */
691 public static final String CATEGORY_SYSTEM = "sys";
692
693 /**
694 * Notification category: indication of running background service.
695 */
696 public static final String CATEGORY_SERVICE = "service";
697
698 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400699 * Notification category: a specific, timely recommendation for a single thing.
700 * For example, a news app might want to recommend a news story it believes the user will
701 * want to read next.
702 */
703 public static final String CATEGORY_RECOMMENDATION = "recommendation";
704
705 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400706 * Notification category: ongoing information about device or contextual status.
707 */
708 public static final String CATEGORY_STATUS = "status";
709
710 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400711 * Notification category: user-scheduled reminder.
712 */
713 public static final String CATEGORY_REMINDER = "reminder";
714
715 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400716 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
717 * that best describes this Notification. May be used by the system for ranking and filtering.
718 */
719 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500720
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700721 private String mGroupKey;
722
723 /**
724 * Get the key used to group this notification into a cluster or stack
725 * with other notifications on devices which support such rendering.
726 */
727 public String getGroup() {
728 return mGroupKey;
729 }
730
731 private String mSortKey;
732
733 /**
734 * Get a sort key that orders this notification among other notifications from the
735 * same package. This can be useful if an external sort was already applied and an app
736 * would like to preserve this. Notifications will be sorted lexicographically using this
737 * value, although providing different priorities in addition to providing sort key may
738 * cause this value to be ignored.
739 *
740 * <p>This sort key can also be used to order members of a notification group. See
741 * {@link Builder#setGroup}.
742 *
743 * @see String#compareTo(String)
744 */
745 public String getSortKey() {
746 return mSortKey;
747 }
748
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500749 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400750 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400751 * <p>
752 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
753 * APIs, and are intended to be used by
754 * {@link android.service.notification.NotificationListenerService} implementations to extract
755 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500756 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400757 public Bundle extras = new Bundle();
758
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400759 /**
760 * {@link #extras} key: this is the title of the notification,
761 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
762 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500763 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400764
765 /**
766 * {@link #extras} key: this is the title of the notification when shown in expanded form,
767 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
768 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400769 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400770
771 /**
772 * {@link #extras} key: this is the main text payload, as supplied to
773 * {@link Builder#setContentText(CharSequence)}.
774 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500775 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400776
777 /**
778 * {@link #extras} key: this is a third line of text, as supplied to
779 * {@link Builder#setSubText(CharSequence)}.
780 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400781 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400782
783 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800784 * {@link #extras} key: this is the remote input history, as supplied to
785 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700786 *
787 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
788 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
789 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
790 * notifications once the other party has responded).
791 *
792 * The extra with this key is of type CharSequence[] and contains the most recent entry at
793 * the 0 index, the second most recent at the 1 index, etc.
794 *
795 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800796 */
797 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
798
799 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400800 * {@link #extras} key: this is a small piece of additional text as supplied to
801 * {@link Builder#setContentInfo(CharSequence)}.
802 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400803 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400804
805 /**
806 * {@link #extras} key: this is a line of summary information intended to be shown
807 * alongside expanded notifications, as supplied to (e.g.)
808 * {@link BigTextStyle#setSummaryText(CharSequence)}.
809 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400810 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400811
812 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200813 * {@link #extras} key: this is the longer text shown in the big form of a
814 * {@link BigTextStyle} notification, as supplied to
815 * {@link BigTextStyle#bigText(CharSequence)}.
816 */
817 public static final String EXTRA_BIG_TEXT = "android.bigText";
818
819 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
821 * supplied to {@link Builder#setSmallIcon(int)}.
822 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500823 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400824
825 /**
826 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
827 * notification payload, as
828 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
829 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400830 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400831
832 /**
833 * {@link #extras} key: this is a bitmap to be used instead of the one from
834 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
835 * shown in its expanded form, as supplied to
836 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
837 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400838 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839
840 /**
841 * {@link #extras} key: this is the progress value supplied to
842 * {@link Builder#setProgress(int, int, boolean)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: this is the maximum value supplied to
848 * {@link Builder#setProgress(int, int, boolean)}.
849 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400850 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400851
852 /**
853 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
854 * {@link Builder#setProgress(int, int, boolean)}.
855 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400856 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400857
858 /**
859 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
860 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
861 * {@link Builder#setUsesChronometer(boolean)}.
862 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400863 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864
865 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800866 * {@link #extras} key: whether the chronometer set on the notification should count down
867 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700868 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800869 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700870 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800871
872 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400873 * {@link #extras} key: whether {@link #when} should be shown,
874 * as supplied to {@link Builder#setShowWhen(boolean)}.
875 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400876 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400877
878 /**
879 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
880 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
881 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400882 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400883
884 /**
885 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
886 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
887 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400888 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400889
890 /**
891 * {@link #extras} key: A string representing the name of the specific
892 * {@link android.app.Notification.Style} used to create this notification.
893 */
Chris Wren91ad5632013-06-05 15:05:57 -0400894 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400895
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400897 * {@link #extras} key: A String array containing the people that this notification relates to,
898 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400899 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400900 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500901
902 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400903 * Allow certain system-generated notifications to appear before the device is provisioned.
904 * Only available to notifications coming from the android package.
905 * @hide
906 */
907 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
908
909 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700910 * {@link #extras} key: A
911 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
912 * in the background when the notification is selected. The URI must point to an image stream
913 * suitable for passing into
914 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
915 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
916 * URI used for this purpose must require no permissions to read the image data.
917 */
918 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
919
920 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400921 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700922 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400923 * {@link android.app.Notification.MediaStyle} notification.
924 */
925 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
926
927 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100928 * {@link #extras} key: the indices of actions to be shown in the compact view,
929 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
930 */
931 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
932
Christoph Studer943aa672014-08-03 20:31:16 +0200933 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400934 * {@link #extras} key: the username to be displayed for all messages sent by the user including
935 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700936 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
937 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400938 */
939 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
940
941 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700942 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400943 * 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
Adrian Roos96b7e202016-05-17 13:50:38 -0700950 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
951 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400952 */
953 public static final String EXTRA_MESSAGES = "android.messages";
954
955 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100956 * {@link #extras} key: the user that built the notification.
957 *
958 * @hide
959 */
960 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
961
962 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400963 * @hide
964 */
965 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
966
Selim Cinek247fa012016-02-18 09:50:48 -0800967 /**
968 * @hide
969 */
970 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
971
Dan Sandler80eaa592016-04-14 23:34:54 -0400972 /** @hide */
973 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400974 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
975
Dan Sandlerd63f9322015-05-06 15:18:49 -0400976 private Icon mSmallIcon;
977 private Icon mLargeIcon;
978
Chris Wren51c75102013-07-16 20:49:17 -0400979 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400980 * Structure to encapsulate a named action that can be shown as part of this notification.
981 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
982 * selected by the user.
983 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700984 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
985 * or {@link Notification.Builder#addAction(Notification.Action)}
986 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400987 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500988 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700989 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400990 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700991 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -0400992 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -0700993
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400994 /**
995 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400996 *
997 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400998 */
Dan Sandler86647982015-05-13 23:41:13 -0400999 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001000 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001001
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001002 /**
1003 * Title of the action.
1004 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001005 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001006
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001007 /**
1008 * Intent to send when the user invokes this action. May be null, in which case the action
1009 * may be rendered in a disabled presentation by the system UI.
1010 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001011 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001012
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001013 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001014 if (in.readInt() != 0) {
1015 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001016 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1017 icon = mIcon.getResId();
1018 }
Dan Sandler86647982015-05-13 23:41:13 -04001019 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1021 if (in.readInt() == 1) {
1022 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1023 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001024 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001025 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001026 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001027 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001028
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001029 /**
Dan Sandler86647982015-05-13 23:41:13 -04001030 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001031 */
Dan Sandler86647982015-05-13 23:41:13 -04001032 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001033 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001034 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001035 }
1036
Dan Sandler86647982015-05-13 23:41:13 -04001037 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001038 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001039 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001040 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1041 this.icon = icon.getResId();
1042 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 this.title = title;
1044 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001045 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001046 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001047 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001048 }
1049
1050 /**
Dan Sandler86647982015-05-13 23:41:13 -04001051 * Return an icon representing the action.
1052 */
1053 public Icon getIcon() {
1054 if (mIcon == null && icon != 0) {
1055 // you snuck an icon in here without using the builder; let's try to keep it
1056 mIcon = Icon.createWithResource("", icon);
1057 }
1058 return mIcon;
1059 }
1060
1061 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001062 * Get additional metadata carried around with this Action.
1063 */
1064 public Bundle getExtras() {
1065 return mExtras;
1066 }
1067
1068 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001069 * Return whether the platform should automatically generate possible replies for this
1070 * {@link Action}
1071 */
1072 public boolean getAllowGeneratedReplies() {
1073 return mAllowGeneratedReplies;
1074 }
1075
1076 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001077 * Get the list of inputs to be collected from the user when this action is sent.
1078 * May return null if no remote inputs were added.
1079 */
1080 public RemoteInput[] getRemoteInputs() {
1081 return mRemoteInputs;
1082 }
1083
1084 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001085 * Builder class for {@link Action} objects.
1086 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001087 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001088 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001089 private final CharSequence mTitle;
1090 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001091 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001092 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001093 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001094
1095 /**
1096 * Construct a new builder for {@link Action} object.
1097 * @param icon icon to show for this action
1098 * @param title the title of the action
1099 * @param intent the {@link PendingIntent} to fire when users trigger this action
1100 */
Dan Sandler86647982015-05-13 23:41:13 -04001101 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001102 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001103 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1104 }
1105
1106 /**
1107 * Construct a new builder for {@link Action} object.
1108 * @param icon icon to show for this action
1109 * @param title the title of the action
1110 * @param intent the {@link PendingIntent} to fire when users trigger this action
1111 */
1112 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001113 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001114 }
1115
1116 /**
1117 * Construct a new builder for {@link Action} object using the fields from an
1118 * {@link Action}.
1119 * @param action the action to read fields from.
1120 */
1121 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001122 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001123 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001124 }
1125
Dan Sandler86647982015-05-13 23:41:13 -04001126 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001127 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001128 mIcon = icon;
1129 mTitle = title;
1130 mIntent = intent;
1131 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001132 if (remoteInputs != null) {
1133 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1134 Collections.addAll(mRemoteInputs, remoteInputs);
1135 }
Griff Hazen959591e2014-05-15 22:26:18 -07001136 }
1137
1138 /**
1139 * Merge additional metadata into this builder.
1140 *
1141 * <p>Values within the Bundle will replace existing extras values in this Builder.
1142 *
1143 * @see Notification.Action#extras
1144 */
1145 public Builder addExtras(Bundle extras) {
1146 if (extras != null) {
1147 mExtras.putAll(extras);
1148 }
1149 return this;
1150 }
1151
1152 /**
1153 * Get the metadata Bundle used by this Builder.
1154 *
1155 * <p>The returned Bundle is shared with this Builder.
1156 */
1157 public Bundle getExtras() {
1158 return mExtras;
1159 }
1160
1161 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001162 * Add an input to be collected from the user when this action is sent.
1163 * Response values can be retrieved from the fired intent by using the
1164 * {@link RemoteInput#getResultsFromIntent} function.
1165 * @param remoteInput a {@link RemoteInput} to add to the action
1166 * @return this object for method chaining
1167 */
1168 public Builder addRemoteInput(RemoteInput remoteInput) {
1169 if (mRemoteInputs == null) {
1170 mRemoteInputs = new ArrayList<RemoteInput>();
1171 }
1172 mRemoteInputs.add(remoteInput);
1173 return this;
1174 }
1175
1176 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001177 * Set whether the platform should automatically generate possible replies to add to
1178 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1179 * {@link RemoteInput}, this has no effect.
1180 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1181 * otherwise
1182 * @return this object for method chaining
1183 * The default value is {@code false}
1184 */
1185 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1186 mAllowGeneratedReplies = allowGeneratedReplies;
1187 return this;
1188 }
1189
1190 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001191 * Apply an extender to this action builder. Extenders may be used to add
1192 * metadata or change options on this builder.
1193 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001194 public Builder extend(Extender extender) {
1195 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001196 return this;
1197 }
1198
1199 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001200 * Combine all of the options that have been set and return a new {@link Action}
1201 * object.
1202 * @return the built action
1203 */
1204 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001205 RemoteInput[] remoteInputs = mRemoteInputs != null
1206 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001207 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1208 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001209 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001210 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001211
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001212 @Override
1213 public Action clone() {
1214 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001215 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001216 title,
1217 actionIntent, // safe to alias
1218 new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001219 getRemoteInputs(),
1220 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001221 }
1222 @Override
1223 public int describeContents() {
1224 return 0;
1225 }
1226 @Override
1227 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001228 final Icon ic = getIcon();
1229 if (ic != null) {
1230 out.writeInt(1);
1231 ic.writeToParcel(out, 0);
1232 } else {
1233 out.writeInt(0);
1234 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001235 TextUtils.writeToParcel(title, out, flags);
1236 if (actionIntent != null) {
1237 out.writeInt(1);
1238 actionIntent.writeToParcel(out, flags);
1239 } else {
1240 out.writeInt(0);
1241 }
Griff Hazen959591e2014-05-15 22:26:18 -07001242 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001243 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001244 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001245 }
Griff Hazen959591e2014-05-15 22:26:18 -07001246 public static final Parcelable.Creator<Action> CREATOR =
1247 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001248 public Action createFromParcel(Parcel in) {
1249 return new Action(in);
1250 }
1251 public Action[] newArray(int size) {
1252 return new Action[size];
1253 }
1254 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001255
1256 /**
1257 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1258 * metadata or change options on an action builder.
1259 */
1260 public interface Extender {
1261 /**
1262 * Apply this extender to a notification action builder.
1263 * @param builder the builder to be modified.
1264 * @return the build object for chaining.
1265 */
1266 public Builder extend(Builder builder);
1267 }
1268
1269 /**
1270 * Wearable extender for notification actions. To add extensions to an action,
1271 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1272 * the {@code WearableExtender()} constructor and apply it to a
1273 * {@link android.app.Notification.Action.Builder} using
1274 * {@link android.app.Notification.Action.Builder#extend}.
1275 *
1276 * <pre class="prettyprint">
1277 * Notification.Action action = new Notification.Action.Builder(
1278 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001279 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001280 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001281 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001282 */
1283 public static final class WearableExtender implements Extender {
1284 /** Notification action extra which contains wearable extensions */
1285 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1286
Pete Gastaf6781d2014-10-07 15:17:05 -04001287 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001288 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001289 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1290 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1291 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001292
1293 // Flags bitwise-ored to mFlags
1294 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001295 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Griff Hazen61a9e862014-05-22 16:05:19 -07001296
1297 // Default value for flags integer
1298 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1299
1300 private int mFlags = DEFAULT_FLAGS;
1301
Pete Gastaf6781d2014-10-07 15:17:05 -04001302 private CharSequence mInProgressLabel;
1303 private CharSequence mConfirmLabel;
1304 private CharSequence mCancelLabel;
1305
Griff Hazen61a9e862014-05-22 16:05:19 -07001306 /**
1307 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1308 * options.
1309 */
1310 public WearableExtender() {
1311 }
1312
1313 /**
1314 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1315 * wearable options present in an existing notification action.
1316 * @param action the notification action to inspect.
1317 */
1318 public WearableExtender(Action action) {
1319 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1320 if (wearableBundle != null) {
1321 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001322 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1323 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1324 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001325 }
1326 }
1327
1328 /**
1329 * Apply wearable extensions to a notification action that is being built. This is
1330 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1331 * method of {@link android.app.Notification.Action.Builder}.
1332 */
1333 @Override
1334 public Action.Builder extend(Action.Builder builder) {
1335 Bundle wearableBundle = new Bundle();
1336
1337 if (mFlags != DEFAULT_FLAGS) {
1338 wearableBundle.putInt(KEY_FLAGS, mFlags);
1339 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001340 if (mInProgressLabel != null) {
1341 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1342 }
1343 if (mConfirmLabel != null) {
1344 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1345 }
1346 if (mCancelLabel != null) {
1347 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1348 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001349
1350 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1351 return builder;
1352 }
1353
1354 @Override
1355 public WearableExtender clone() {
1356 WearableExtender that = new WearableExtender();
1357 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001358 that.mInProgressLabel = this.mInProgressLabel;
1359 that.mConfirmLabel = this.mConfirmLabel;
1360 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001361 return that;
1362 }
1363
1364 /**
1365 * Set whether this action is available when the wearable device is not connected to
1366 * a companion device. The user can still trigger this action when the wearable device is
1367 * offline, but a visual hint will indicate that the action may not be available.
1368 * Defaults to true.
1369 */
1370 public WearableExtender setAvailableOffline(boolean availableOffline) {
1371 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1372 return this;
1373 }
1374
1375 /**
1376 * Get whether this action is available when the wearable device is not connected to
1377 * a companion device. The user can still trigger this action when the wearable device is
1378 * offline, but a visual hint will indicate that the action may not be available.
1379 * Defaults to true.
1380 */
1381 public boolean isAvailableOffline() {
1382 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1383 }
1384
1385 private void setFlag(int mask, boolean value) {
1386 if (value) {
1387 mFlags |= mask;
1388 } else {
1389 mFlags &= ~mask;
1390 }
1391 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001392
1393 /**
1394 * Set a label to display while the wearable is preparing to automatically execute the
1395 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1396 *
1397 * @param label the label to display while the action is being prepared to execute
1398 * @return this object for method chaining
1399 */
1400 public WearableExtender setInProgressLabel(CharSequence label) {
1401 mInProgressLabel = label;
1402 return this;
1403 }
1404
1405 /**
1406 * Get the label to display while the wearable is preparing to automatically execute
1407 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1408 *
1409 * @return the label to display while the action is being prepared to execute
1410 */
1411 public CharSequence getInProgressLabel() {
1412 return mInProgressLabel;
1413 }
1414
1415 /**
1416 * Set a label to display to confirm that the action should be executed.
1417 * This is usually an imperative verb like "Send".
1418 *
1419 * @param label the label to confirm the action should be executed
1420 * @return this object for method chaining
1421 */
1422 public WearableExtender setConfirmLabel(CharSequence label) {
1423 mConfirmLabel = label;
1424 return this;
1425 }
1426
1427 /**
1428 * Get the label to display to confirm that the action should be executed.
1429 * This is usually an imperative verb like "Send".
1430 *
1431 * @return the label to confirm the action should be executed
1432 */
1433 public CharSequence getConfirmLabel() {
1434 return mConfirmLabel;
1435 }
1436
1437 /**
1438 * Set a label to display to cancel the action.
1439 * This is usually an imperative verb, like "Cancel".
1440 *
1441 * @param label the label to display to cancel the action
1442 * @return this object for method chaining
1443 */
1444 public WearableExtender setCancelLabel(CharSequence label) {
1445 mCancelLabel = label;
1446 return this;
1447 }
1448
1449 /**
1450 * Get the label to display to cancel the action.
1451 * This is usually an imperative verb like "Cancel".
1452 *
1453 * @return the label to display to cancel the action
1454 */
1455 public CharSequence getCancelLabel() {
1456 return mCancelLabel;
1457 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001458
1459 /**
1460 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1461 * platform that it can generate the appropriate transitions.
1462 * @param hintLaunchesActivity {@code true} if the content intent will launch
1463 * an activity and transitions should be generated, false otherwise.
1464 * @return this object for method chaining
1465 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001466 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001467 boolean hintLaunchesActivity) {
1468 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1469 return this;
1470 }
1471
1472 /**
1473 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1474 * platform that it can generate the appropriate transitions
1475 * @return {@code true} if the content intent will launch an activity and transitions
1476 * should be generated, false otherwise. The default value is {@code false} if this was
1477 * never set.
1478 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001479 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001480 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1481 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001482 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001483 }
1484
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001485 /**
1486 * Array of all {@link Action} structures attached to this notification by
1487 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1488 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1489 * interface for invoking actions.
1490 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001491 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001492
1493 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001494 * Replacement version of this notification whose content will be shown
1495 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1496 * and {@link #VISIBILITY_PUBLIC}.
1497 */
1498 public Notification publicVersion;
1499
1500 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001501 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001502 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 */
1504 public Notification()
1505 {
1506 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001507 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001508 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 }
1510
1511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 * @hide
1513 */
1514 public Notification(Context context, int icon, CharSequence tickerText, long when,
1515 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1516 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001517 new Builder(context)
1518 .setWhen(when)
1519 .setSmallIcon(icon)
1520 .setTicker(tickerText)
1521 .setContentTitle(contentTitle)
1522 .setContentText(contentText)
1523 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1524 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526
1527 /**
1528 * Constructs a Notification object with the information needed to
1529 * have a status bar icon without the standard expanded view.
1530 *
1531 * @param icon The resource id of the icon to put in the status bar.
1532 * @param tickerText The text that flows by in the status bar when the notification first
1533 * activates.
1534 * @param when The time to show in the time field. In the System.currentTimeMillis
1535 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001536 *
1537 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001539 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 public Notification(int icon, CharSequence tickerText, long when)
1541 {
1542 this.icon = icon;
1543 this.tickerText = tickerText;
1544 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001545 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
1547
1548 /**
1549 * Unflatten the notification from a parcel.
1550 */
1551 public Notification(Parcel parcel)
1552 {
1553 int version = parcel.readInt();
1554
1555 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001556 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001557 if (parcel.readInt() != 0) {
1558 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001559 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1560 icon = mSmallIcon.getResId();
1561 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 number = parcel.readInt();
1564 if (parcel.readInt() != 0) {
1565 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1566 }
1567 if (parcel.readInt() != 0) {
1568 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1569 }
1570 if (parcel.readInt() != 0) {
1571 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1572 }
1573 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001574 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001575 }
1576 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1578 }
Joe Onorato561d3852010-11-20 18:09:34 -08001579 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001580 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 defaults = parcel.readInt();
1583 flags = parcel.readInt();
1584 if (parcel.readInt() != 0) {
1585 sound = Uri.CREATOR.createFromParcel(parcel);
1586 }
1587
1588 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001589 if (parcel.readInt() != 0) {
1590 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 vibrate = parcel.createLongArray();
1593 ledARGB = parcel.readInt();
1594 ledOnMS = parcel.readInt();
1595 ledOffMS = parcel.readInt();
1596 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001597
1598 if (parcel.readInt() != 0) {
1599 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1600 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001601
1602 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001603
John Spurlockfd7f1e02014-03-18 16:41:57 -04001604 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001605
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001606 mGroupKey = parcel.readString();
1607
1608 mSortKey = parcel.readString();
1609
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001610 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001611
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001612 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1613
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001614 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001615 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1616 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001617
Chris Wren8fd39ec2014-02-27 17:43:26 -05001618 if (parcel.readInt() != 0) {
1619 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1620 }
1621
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001622 visibility = parcel.readInt();
1623
1624 if (parcel.readInt() != 0) {
1625 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1626 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001627
1628 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630
Andy Stadler110988c2010-12-03 14:29:16 -08001631 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001632 public Notification clone() {
1633 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001634 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001635 return that;
1636 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001637
Daniel Sandler1a497d32013-04-18 14:52:45 -04001638 /**
1639 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1640 * of this into that.
1641 * @hide
1642 */
1643 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001644 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001645 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001646 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001647 that.number = this.number;
1648
1649 // PendingIntents are global, so there's no reason (or way) to clone them.
1650 that.contentIntent = this.contentIntent;
1651 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001652 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001653
1654 if (this.tickerText != null) {
1655 that.tickerText = this.tickerText.toString();
1656 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001657 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001658 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001659 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001660 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001661 that.contentView = this.contentView.clone();
1662 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001663 if (heavy && this.mLargeIcon != null) {
1664 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001665 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001666 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001667 that.sound = this.sound; // android.net.Uri is immutable
1668 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001669 if (this.audioAttributes != null) {
1670 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1671 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001672
1673 final long[] vibrate = this.vibrate;
1674 if (vibrate != null) {
1675 final int N = vibrate.length;
1676 final long[] vib = that.vibrate = new long[N];
1677 System.arraycopy(vibrate, 0, vib, 0, N);
1678 }
1679
1680 that.ledARGB = this.ledARGB;
1681 that.ledOnMS = this.ledOnMS;
1682 that.ledOffMS = this.ledOffMS;
1683 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001684
Joe Onorato18e69df2010-05-17 22:26:12 -07001685 that.flags = this.flags;
1686
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001687 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001688
John Spurlockfd7f1e02014-03-18 16:41:57 -04001689 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001690
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001691 that.mGroupKey = this.mGroupKey;
1692
1693 that.mSortKey = this.mSortKey;
1694
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001695 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001696 try {
1697 that.extras = new Bundle(this.extras);
1698 // will unparcel
1699 that.extras.size();
1700 } catch (BadParcelableException e) {
1701 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1702 that.extras = null;
1703 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001704 }
1705
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001706 if (this.actions != null) {
1707 that.actions = new Action[this.actions.length];
1708 for(int i=0; i<this.actions.length; i++) {
1709 that.actions[i] = this.actions[i].clone();
1710 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001711 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001712
Daniel Sandler1a497d32013-04-18 14:52:45 -04001713 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001714 that.bigContentView = this.bigContentView.clone();
1715 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001716
Chris Wren8fd39ec2014-02-27 17:43:26 -05001717 if (heavy && this.headsUpContentView != null) {
1718 that.headsUpContentView = this.headsUpContentView.clone();
1719 }
1720
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001721 that.visibility = this.visibility;
1722
1723 if (this.publicVersion != null) {
1724 that.publicVersion = new Notification();
1725 this.publicVersion.cloneInto(that.publicVersion, heavy);
1726 }
1727
Dan Sandler26e81cf2014-05-06 10:01:27 -04001728 that.color = this.color;
1729
Daniel Sandler1a497d32013-04-18 14:52:45 -04001730 if (!heavy) {
1731 that.lightenPayload(); // will clean out extras
1732 }
1733 }
1734
1735 /**
1736 * Removes heavyweight parts of the Notification object for archival or for sending to
1737 * listeners when the full contents are not necessary.
1738 * @hide
1739 */
1740 public final void lightenPayload() {
1741 tickerView = null;
1742 contentView = null;
1743 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001744 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001745 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001746 if (extras != null && !extras.isEmpty()) {
1747 final Set<String> keyset = extras.keySet();
1748 final int N = keyset.size();
1749 final String[] keys = keyset.toArray(new String[N]);
1750 for (int i=0; i<N; i++) {
1751 final String key = keys[i];
1752 final Object obj = extras.get(key);
1753 if (obj != null &&
1754 ( obj instanceof Parcelable
1755 || obj instanceof Parcelable[]
1756 || obj instanceof SparseArray
1757 || obj instanceof ArrayList)) {
1758 extras.remove(key);
1759 }
1760 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001761 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001762 }
1763
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001764 /**
1765 * Make sure this CharSequence is safe to put into a bundle, which basically
1766 * means it had better not be some custom Parcelable implementation.
1767 * @hide
1768 */
1769 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001770 if (cs == null) return cs;
1771 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1772 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1773 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001774 if (cs instanceof Parcelable) {
1775 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1776 + " instance is a custom Parcelable and not allowed in Notification");
1777 return cs.toString();
1778 }
Selim Cinek60a54252016-02-26 17:03:25 -08001779 return removeTextSizeSpans(cs);
1780 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001781
Selim Cinek60a54252016-02-26 17:03:25 -08001782 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1783 if (charSequence instanceof Spanned) {
1784 Spanned ss = (Spanned) charSequence;
1785 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1786 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1787 for (Object span : spans) {
1788 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001789 if (resultSpan instanceof CharacterStyle) {
1790 resultSpan = ((CharacterStyle) span).getUnderlying();
1791 }
1792 if (resultSpan instanceof TextAppearanceSpan) {
1793 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001794 resultSpan = new TextAppearanceSpan(
1795 originalSpan.getFamily(),
1796 originalSpan.getTextStyle(),
1797 -1,
1798 originalSpan.getTextColor(),
1799 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001800 } else if (resultSpan instanceof RelativeSizeSpan
1801 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001802 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001803 } else {
1804 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001805 }
1806 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1807 ss.getSpanFlags(span));
1808 }
1809 return builder;
1810 }
1811 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001812 }
1813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 public int describeContents() {
1815 return 0;
1816 }
1817
1818 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001819 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 */
1821 public void writeToParcel(Parcel parcel, int flags)
1822 {
1823 parcel.writeInt(1);
1824
1825 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001826 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001827 if (mSmallIcon == null && icon != 0) {
1828 // you snuck an icon in here without using the builder; let's try to keep it
1829 mSmallIcon = Icon.createWithResource("", icon);
1830 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001831 if (mSmallIcon != null) {
1832 parcel.writeInt(1);
1833 mSmallIcon.writeToParcel(parcel, 0);
1834 } else {
1835 parcel.writeInt(0);
1836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 parcel.writeInt(number);
1838 if (contentIntent != null) {
1839 parcel.writeInt(1);
1840 contentIntent.writeToParcel(parcel, 0);
1841 } else {
1842 parcel.writeInt(0);
1843 }
1844 if (deleteIntent != null) {
1845 parcel.writeInt(1);
1846 deleteIntent.writeToParcel(parcel, 0);
1847 } else {
1848 parcel.writeInt(0);
1849 }
1850 if (tickerText != null) {
1851 parcel.writeInt(1);
1852 TextUtils.writeToParcel(tickerText, parcel, flags);
1853 } else {
1854 parcel.writeInt(0);
1855 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001856 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001857 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001858 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001859 } else {
1860 parcel.writeInt(0);
1861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 if (contentView != null) {
1863 parcel.writeInt(1);
1864 contentView.writeToParcel(parcel, 0);
1865 } else {
1866 parcel.writeInt(0);
1867 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001868 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001869 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001870 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001871 } else {
1872 parcel.writeInt(0);
1873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874
1875 parcel.writeInt(defaults);
1876 parcel.writeInt(this.flags);
1877
1878 if (sound != null) {
1879 parcel.writeInt(1);
1880 sound.writeToParcel(parcel, 0);
1881 } else {
1882 parcel.writeInt(0);
1883 }
1884 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001885
1886 if (audioAttributes != null) {
1887 parcel.writeInt(1);
1888 audioAttributes.writeToParcel(parcel, 0);
1889 } else {
1890 parcel.writeInt(0);
1891 }
1892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 parcel.writeLongArray(vibrate);
1894 parcel.writeInt(ledARGB);
1895 parcel.writeInt(ledOnMS);
1896 parcel.writeInt(ledOffMS);
1897 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001898
1899 if (fullScreenIntent != null) {
1900 parcel.writeInt(1);
1901 fullScreenIntent.writeToParcel(parcel, 0);
1902 } else {
1903 parcel.writeInt(0);
1904 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001905
1906 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001907
John Spurlockfd7f1e02014-03-18 16:41:57 -04001908 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001909
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001910 parcel.writeString(mGroupKey);
1911
1912 parcel.writeString(mSortKey);
1913
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001914 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001915
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001916 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001917
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001918 if (bigContentView != null) {
1919 parcel.writeInt(1);
1920 bigContentView.writeToParcel(parcel, 0);
1921 } else {
1922 parcel.writeInt(0);
1923 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001924
Chris Wren8fd39ec2014-02-27 17:43:26 -05001925 if (headsUpContentView != null) {
1926 parcel.writeInt(1);
1927 headsUpContentView.writeToParcel(parcel, 0);
1928 } else {
1929 parcel.writeInt(0);
1930 }
1931
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001932 parcel.writeInt(visibility);
1933
1934 if (publicVersion != null) {
1935 parcel.writeInt(1);
1936 publicVersion.writeToParcel(parcel, 0);
1937 } else {
1938 parcel.writeInt(0);
1939 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001940
1941 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
1943
1944 /**
1945 * Parcelable.Creator that instantiates Notification objects
1946 */
1947 public static final Parcelable.Creator<Notification> CREATOR
1948 = new Parcelable.Creator<Notification>()
1949 {
1950 public Notification createFromParcel(Parcel parcel)
1951 {
1952 return new Notification(parcel);
1953 }
1954
1955 public Notification[] newArray(int size)
1956 {
1957 return new Notification[size];
1958 }
1959 };
1960
1961 /**
1962 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1963 * layout.
1964 *
1965 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1966 * in the view.</p>
1967 * @param context The context for your application / activity.
1968 * @param contentTitle The title that goes in the expanded entry.
1969 * @param contentText The text that goes in the expanded entry.
1970 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1971 * If this is an activity, it must include the
1972 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001973 * that you take care of task management as described in the
1974 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1975 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001976 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001977 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001978 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001980 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 public void setLatestEventInfo(Context context,
1982 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001983 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1984 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1985 new Throwable());
1986 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001987
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001988 // ensure that any information already set directly is preserved
1989 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001990
1991 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001993 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
1995 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001996 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001998 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001999
2000 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002
Julia Reynoldsda303542015-11-23 14:00:20 -05002003 /**
2004 * @hide
2005 */
2006 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002007 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2008 }
2009
2010 /**
2011 * @hide
2012 */
2013 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2014 Notification notification) {
2015 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2016 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002017 }
2018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 @Override
2020 public String toString() {
2021 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002022 sb.append("Notification(pri=");
2023 sb.append(priority);
2024 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002025 if (contentView != null) {
2026 sb.append(contentView.getPackage());
2027 sb.append("/0x");
2028 sb.append(Integer.toHexString(contentView.getLayoutId()));
2029 } else {
2030 sb.append("null");
2031 }
2032 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002033 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2034 sb.append("default");
2035 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 int N = this.vibrate.length-1;
2037 sb.append("[");
2038 for (int i=0; i<N; i++) {
2039 sb.append(this.vibrate[i]);
2040 sb.append(',');
2041 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002042 if (N != -1) {
2043 sb.append(this.vibrate[N]);
2044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 } else {
2047 sb.append("null");
2048 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002049 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002050 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002052 } else if (this.sound != null) {
2053 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 } else {
2055 sb.append("null");
2056 }
Chris Wren365b6d32015-07-16 10:39:26 -04002057 if (this.tickerText != null) {
2058 sb.append(" tick");
2059 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002060 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002062 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002063 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002064 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002065 if (this.category != null) {
2066 sb.append(" category=");
2067 sb.append(this.category);
2068 }
2069 if (this.mGroupKey != null) {
2070 sb.append(" groupKey=");
2071 sb.append(this.mGroupKey);
2072 }
2073 if (this.mSortKey != null) {
2074 sb.append(" sortKey=");
2075 sb.append(this.mSortKey);
2076 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002077 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002078 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002079 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002080 }
2081 sb.append(" vis=");
2082 sb.append(visibilityToString(this.visibility));
2083 if (this.publicVersion != null) {
2084 sb.append(" publicVersion=");
2085 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002086 }
2087 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 return sb.toString();
2089 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002090
Dan Sandler1b718782014-07-18 12:43:45 -04002091 /**
2092 * {@hide}
2093 */
2094 public static String visibilityToString(int vis) {
2095 switch (vis) {
2096 case VISIBILITY_PRIVATE:
2097 return "PRIVATE";
2098 case VISIBILITY_PUBLIC:
2099 return "PUBLIC";
2100 case VISIBILITY_SECRET:
2101 return "SECRET";
2102 default:
2103 return "UNKNOWN(" + String.valueOf(vis) + ")";
2104 }
2105 }
2106
Joe Onoratocb109a02011-01-18 17:57:41 -08002107 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002108 * {@hide}
2109 */
2110 public static String priorityToString(@Priority int pri) {
2111 switch (pri) {
2112 case PRIORITY_MIN:
2113 return "MIN";
2114 case PRIORITY_LOW:
2115 return "LOW";
2116 case PRIORITY_DEFAULT:
2117 return "DEFAULT";
2118 case PRIORITY_HIGH:
2119 return "HIGH";
2120 case PRIORITY_MAX:
2121 return "MAX";
2122 default:
2123 return "UNKNOWN(" + String.valueOf(pri) + ")";
2124 }
2125 }
2126
2127 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002128 * The small icon representing this notification in the status bar and content view.
2129 *
2130 * @return the small icon representing this notification.
2131 *
2132 * @see Builder#getSmallIcon()
2133 * @see Builder#setSmallIcon(Icon)
2134 */
2135 public Icon getSmallIcon() {
2136 return mSmallIcon;
2137 }
2138
2139 /**
2140 * Used when notifying to clean up legacy small icons.
2141 * @hide
2142 */
2143 public void setSmallIcon(Icon icon) {
2144 mSmallIcon = icon;
2145 }
2146
2147 /**
2148 * The large icon shown in this notification's content view.
2149 * @see Builder#getLargeIcon()
2150 * @see Builder#setLargeIcon(Icon)
2151 */
2152 public Icon getLargeIcon() {
2153 return mLargeIcon;
2154 }
2155
2156 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002157 * @hide
2158 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002159 public boolean isGroupSummary() {
2160 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2161 }
2162
2163 /**
2164 * @hide
2165 */
2166 public boolean isGroupChild() {
2167 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2168 }
2169
2170 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002171 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002172 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002173 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002174 * content views using the platform's notification layout template. If your app supports
2175 * versions of Android as old as API level 4, you can instead use
2176 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2177 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2178 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002179 *
Scott Main183bf112012-08-13 19:12:13 -07002180 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002181 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002182 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002183 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002184 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2185 * .setContentText(subject)
2186 * .setSmallIcon(R.drawable.new_mail)
2187 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002188 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002189 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002190 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002191 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002192 /**
2193 * @hide
2194 */
2195 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2196 "android.rebuild.contentViewActionCount";
2197 /**
2198 * @hide
2199 */
2200 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2201 = "android.rebuild.bigViewActionCount";
2202 /**
2203 * @hide
2204 */
2205 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2206 = "android.rebuild.hudViewActionCount";
2207
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002208 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002209
Joe Onorato46439ce2010-11-19 13:56:21 -08002210 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002211 private Notification mN;
2212 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002213 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002214 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2215 private ArrayList<String> mPersonList = new ArrayList<String>();
2216 private NotificationColorUtil mColorUtil;
2217 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002218
2219 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002220 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2221 */
2222 private int mCachedContrastColor = COLOR_INVALID;
2223 private int mCachedContrastColorIsFor = COLOR_INVALID;
2224
2225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002226 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002228
2229 * <table>
2230 * <tr><th align=right>priority</th>
2231 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2232 * <tr><th align=right>when</th>
2233 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2234 * <tr><th align=right>audio stream</th>
2235 * <td>{@link #STREAM_DEFAULT}</td></tr>
2236 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002237 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002238
2239 * @param context
2240 * A {@link Context} that will be used by the Builder to construct the
2241 * RemoteViews. The Context will not be held past the lifetime of this Builder
2242 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002243 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002244 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002245 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002246 }
2247
Joe Onoratocb109a02011-01-18 17:57:41 -08002248 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002249 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002250 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002251 public Builder(Context context, Notification toAdopt) {
2252 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002253
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002254 if (toAdopt == null) {
2255 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002256 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2257 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2258 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002259 mN.priority = PRIORITY_DEFAULT;
2260 mN.visibility = VISIBILITY_PRIVATE;
2261 } else {
2262 mN = toAdopt;
2263 if (mN.actions != null) {
2264 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002265 }
2266
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002267 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2268 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2269 }
2270
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002271 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2272 if (!TextUtils.isEmpty(templateClass)) {
2273 final Class<? extends Style> styleClass
2274 = getNotificationStyleClass(templateClass);
2275 if (styleClass == null) {
2276 Log.d(TAG, "Unknown style class: " + templateClass);
2277 } else {
2278 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002279 final Constructor<? extends Style> ctor =
2280 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002281 ctor.setAccessible(true);
2282 final Style style = ctor.newInstance();
2283 style.restoreFromExtras(mN.extras);
2284
2285 if (style != null) {
2286 setStyle(style);
2287 }
2288 } catch (Throwable t) {
2289 Log.e(TAG, "Could not create Style", t);
2290 }
2291 }
2292 }
2293
2294 }
2295 }
2296
2297 private NotificationColorUtil getColorUtil() {
2298 if (!mColorUtilInited) {
2299 mColorUtilInited = true;
2300 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2301 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002302 }
2303 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002304 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002305 }
2306
2307 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002308 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002309 *
2310 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2311 * shown anymore by default and must be opted into by using
2312 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002313 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002314 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002315 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002316 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002317 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002318 return this;
2319 }
2320
Joe Onoratocb109a02011-01-18 17:57:41 -08002321 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002322 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002323 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002324 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2325 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002326 */
2327 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002328 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002329 return this;
2330 }
2331
2332 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002333 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002334 *
2335 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002336 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002337 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002338 * Useful when showing an elapsed time (like an ongoing phone call).
2339 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002340 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002341 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002342 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002343 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002344 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002345 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002346 */
2347 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002348 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002349 return this;
2350 }
2351
2352 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002353 * Sets the Chronometer to count down instead of counting up.
2354 *
2355 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2356 * If it isn't set the chronometer will count up.
2357 *
2358 * @see #setUsesChronometer(boolean)
2359 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002360 public Builder setChronometerCountDown(boolean countDown) {
2361 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002362 return this;
2363 }
2364
2365 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002366 * Set the small icon resource, which will be used to represent the notification in the
2367 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002368 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002369
2370 * The platform template for the expanded view will draw this icon in the left, unless a
2371 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2372 * icon will be moved to the right-hand side.
2373 *
2374
2375 * @param icon
2376 * A resource ID in the application's package of the drawable to use.
2377 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002378 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002379 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002380 return setSmallIcon(icon != 0
2381 ? Icon.createWithResource(mContext, icon)
2382 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002383 }
2384
Joe Onoratocb109a02011-01-18 17:57:41 -08002385 /**
2386 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2387 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2388 * LevelListDrawable}.
2389 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002390 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002391 * @param level The level to use for the icon.
2392 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002393 * @see Notification#icon
2394 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002395 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002396 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002397 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002398 return setSmallIcon(icon);
2399 }
2400
2401 /**
2402 * Set the small icon, which will be used to represent the notification in the
2403 * status bar and content view (unless overriden there by a
2404 * {@link #setLargeIcon(Bitmap) large icon}).
2405 *
2406 * @param icon An Icon object to use.
2407 * @see Notification#icon
2408 */
2409 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002410 mN.setSmallIcon(icon);
2411 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2412 mN.icon = icon.getResId();
2413 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002414 return this;
2415 }
2416
Joe Onoratocb109a02011-01-18 17:57:41 -08002417 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002418 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002419 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002420 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002421 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002422 return this;
2423 }
2424
Joe Onoratocb109a02011-01-18 17:57:41 -08002425 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002426 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002427 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002428 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002429 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002430 return this;
2431 }
2432
Joe Onoratocb109a02011-01-18 17:57:41 -08002433 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002434 * This provides some additional information that is displayed in the notification. No
2435 * guarantees are given where exactly it is displayed.
2436 *
2437 * <p>This information should only be provided if it provides an essential
2438 * benefit to the understanding of the notification. The more text you provide the
2439 * less readable it becomes. For example, an email client should only provide the account
2440 * name here if more than one email account has been added.</p>
2441 *
2442 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2443 * notification header area.
2444 *
2445 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2446 * this will be shown in the third line of text in the platform notification template.
2447 * You should not be using {@link #setProgress(int, int, boolean)} at the
2448 * same time on those versions; they occupy the same place.
2449 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002450 */
2451 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002452 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002453 return this;
2454 }
2455
2456 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002457 * Set the remote input history.
2458 *
2459 * This should be set to the most recent inputs that have been sent
2460 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2461 * longer relevant (e.g. for chat notifications once the other party has responded).
2462 *
2463 * The most recent input must be stored at the 0 index, the second most recent at the
2464 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2465 * and how much of each individual input is shown.
2466 *
2467 * <p>Note: The reply text will only be shown on notifications that have least one action
2468 * with a {@code RemoteInput}.</p>
2469 */
2470 public Builder setRemoteInputHistory(CharSequence[] text) {
2471 if (text == null) {
2472 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2473 } else {
2474 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2475 CharSequence[] safe = new CharSequence[N];
2476 for (int i = 0; i < N; i++) {
2477 safe[i] = safeCharSequence(text[i]);
2478 }
2479 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2480 }
2481 return this;
2482 }
2483
2484 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002485 * Set the large number at the right-hand side of the notification. This is
2486 * equivalent to setContentInfo, although it might show the number in a different
2487 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002488 *
2489 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002490 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002491 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002492 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002493 return this;
2494 }
2495
Joe Onoratocb109a02011-01-18 17:57:41 -08002496 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002497 * A small piece of additional information pertaining to this notification.
2498 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002499 * The platform template will draw this on the last line of the notification, at the far
2500 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002501 *
2502 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2503 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2504 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002505 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002506 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002507 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002508 return this;
2509 }
2510
Joe Onoratocb109a02011-01-18 17:57:41 -08002511 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002512 * Set the progress this notification represents.
2513 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002514 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002515 */
2516 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002517 mN.extras.putInt(EXTRA_PROGRESS, progress);
2518 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2519 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002520 return this;
2521 }
2522
2523 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002524 * Supply a custom RemoteViews to use instead of the platform template.
2525 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002526 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002527 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002528 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002529 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002530 return setCustomContentView(views);
2531 }
2532
2533 /**
2534 * Supply custom RemoteViews to use instead of the platform template.
2535 *
2536 * This will override the layout that would otherwise be constructed by this Builder
2537 * object.
2538 */
2539 public Builder setCustomContentView(RemoteViews contentView) {
2540 mN.contentView = contentView;
2541 return this;
2542 }
2543
2544 /**
2545 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2546 *
2547 * This will override the expanded layout that would otherwise be constructed by this
2548 * Builder object.
2549 */
2550 public Builder setCustomBigContentView(RemoteViews contentView) {
2551 mN.bigContentView = contentView;
2552 return this;
2553 }
2554
2555 /**
2556 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2557 *
2558 * This will override the heads-up layout that would otherwise be constructed by this
2559 * Builder object.
2560 */
2561 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2562 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002563 return this;
2564 }
2565
Joe Onoratocb109a02011-01-18 17:57:41 -08002566 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002567 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2568 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002569 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2570 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2571 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002572 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002573 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002574 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002575 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002576 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002577 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002578 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002579 return this;
2580 }
2581
Joe Onoratocb109a02011-01-18 17:57:41 -08002582 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002583 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2584 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002585 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002586 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002587 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002588 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002589 return this;
2590 }
2591
Joe Onoratocb109a02011-01-18 17:57:41 -08002592 /**
2593 * An intent to launch instead of posting the notification to the status bar.
2594 * Only for use with extremely high-priority notifications demanding the user's
2595 * <strong>immediate</strong> attention, such as an incoming phone call or
2596 * alarm clock that the user has explicitly set to a particular time.
2597 * If this facility is used for something else, please give the user an option
2598 * to turn it off and use a normal notification, as this can be extremely
2599 * disruptive.
2600 *
Chris Wren47c20a12014-06-18 17:27:29 -04002601 * <p>
2602 * The system UI may choose to display a heads-up notification, instead of
2603 * launching this intent, while the user is using the device.
2604 * </p>
2605 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002606 * @param intent The pending intent to launch.
2607 * @param highPriority Passing true will cause this notification to be sent
2608 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002609 *
2610 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002611 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002612 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002613 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002614 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2615 return this;
2616 }
2617
Joe Onoratocb109a02011-01-18 17:57:41 -08002618 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002619 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002620 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002621 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002622 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002623 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002624 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002625 return this;
2626 }
2627
Joe Onoratocb109a02011-01-18 17:57:41 -08002628 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002629 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002630 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002631 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002632 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002633 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002634 setTicker(tickerText);
2635 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002636 return this;
2637 }
2638
Joe Onoratocb109a02011-01-18 17:57:41 -08002639 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002640 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002641 *
2642 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002643 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2644 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002645 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002646 public Builder setLargeIcon(Bitmap b) {
2647 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2648 }
2649
2650 /**
2651 * Add a large icon to the notification content view.
2652 *
2653 * In the platform template, this image will be shown on the left of the notification view
2654 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2655 * badge atop the large icon).
2656 */
2657 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002658 mN.mLargeIcon = icon;
2659 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002660 return this;
2661 }
2662
Joe Onoratocb109a02011-01-18 17:57:41 -08002663 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002664 * Set the sound to play.
2665 *
John Spurlockc0650f022014-07-19 13:22:39 -04002666 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2667 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002668 *
Chris Wren47c20a12014-06-18 17:27:29 -04002669 * <p>
2670 * A notification that is noisy is more likely to be presented as a heads-up notification.
2671 * </p>
2672 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002673 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002674 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002675 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002676 mN.sound = sound;
2677 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002678 return this;
2679 }
2680
Joe Onoratocb109a02011-01-18 17:57:41 -08002681 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002682 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002683 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002684 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2685 *
Chris Wren47c20a12014-06-18 17:27:29 -04002686 * <p>
2687 * A notification that is noisy is more likely to be presented as a heads-up notification.
2688 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002689 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002690 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002691 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002692 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002693 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002694 mN.sound = sound;
2695 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002696 return this;
2697 }
2698
Joe Onoratocb109a02011-01-18 17:57:41 -08002699 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002700 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2701 * use during playback.
2702 *
2703 * <p>
2704 * A notification that is noisy is more likely to be presented as a heads-up notification.
2705 * </p>
2706 *
2707 * @see Notification#sound
2708 */
2709 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002710 mN.sound = sound;
2711 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002712 return this;
2713 }
2714
2715 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002716 * Set the vibration pattern to use.
2717 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002718 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2719 * <code>pattern</code> parameter.
2720 *
Chris Wren47c20a12014-06-18 17:27:29 -04002721 * <p>
2722 * A notification that vibrates is more likely to be presented as a heads-up notification.
2723 * </p>
2724 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002725 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002726 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002727 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002728 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002729 return this;
2730 }
2731
Joe Onoratocb109a02011-01-18 17:57:41 -08002732 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002733 * Set the desired color for the indicator LED on the device, as well as the
2734 * blink duty cycle (specified in milliseconds).
2735 *
2736
2737 * Not all devices will honor all (or even any) of these values.
2738 *
2739
2740 * @see Notification#ledARGB
2741 * @see Notification#ledOnMS
2742 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002743 */
Tor Norbye80756e32015-03-02 09:39:27 -08002744 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002745 mN.ledARGB = argb;
2746 mN.ledOnMS = onMs;
2747 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002748 if (onMs != 0 || offMs != 0) {
2749 mN.flags |= FLAG_SHOW_LIGHTS;
2750 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002751 return this;
2752 }
2753
Joe Onoratocb109a02011-01-18 17:57:41 -08002754 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002755 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002756 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002757
2758 * Ongoing notifications cannot be dismissed by the user, so your application or service
2759 * must take care of canceling them.
2760 *
2761
2762 * They are typically used to indicate a background task that the user is actively engaged
2763 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2764 * (e.g., a file download, sync operation, active network connection).
2765 *
2766
2767 * @see Notification#FLAG_ONGOING_EVENT
2768 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002769 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002770 public Builder setOngoing(boolean ongoing) {
2771 setFlag(FLAG_ONGOING_EVENT, ongoing);
2772 return this;
2773 }
2774
Joe Onoratocb109a02011-01-18 17:57:41 -08002775 /**
2776 * Set this flag if you would only like the sound, vibrate
2777 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002778 *
2779 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002780 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002781 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2782 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2783 return this;
2784 }
2785
Joe Onoratocb109a02011-01-18 17:57:41 -08002786 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002787 * Make this notification automatically dismissed when the user touches it. The
2788 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2789 *
2790 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002791 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002792 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002793 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002794 return this;
2795 }
2796
Joe Onoratocb109a02011-01-18 17:57:41 -08002797 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002798 * Set whether or not this notification should not bridge to other devices.
2799 *
2800 * <p>Some notifications can be bridged to other devices for remote display.
2801 * This hint can be set to recommend this notification not be bridged.
2802 */
2803 public Builder setLocalOnly(boolean localOnly) {
2804 setFlag(FLAG_LOCAL_ONLY, localOnly);
2805 return this;
2806 }
2807
2808 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002809 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002810 * <p>
2811 * The value should be one or more of the following fields combined with
2812 * bitwise-or:
2813 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2814 * <p>
2815 * For all default values, use {@link #DEFAULT_ALL}.
2816 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002817 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002818 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002819 return this;
2820 }
2821
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002822 /**
2823 * Set the priority of this notification.
2824 *
2825 * @see Notification#priority
2826 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002827 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002828 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002829 return this;
2830 }
Joe Malin8d40d042012-11-05 11:36:40 -08002831
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002832 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002833 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002834 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002835 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002836 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002837 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002838 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002839 return this;
2840 }
2841
2842 /**
Chris Wrendde75302014-03-26 17:24:15 -04002843 * Add a person that is relevant to this notification.
2844 *
Chris Wrene6c48932014-09-29 17:19:27 -04002845 * <P>
2846 * Depending on user preferences, this annotation may allow the notification to pass
2847 * through interruption filters, and to appear more prominently in the user interface.
2848 * </P>
2849 *
2850 * <P>
2851 * The person should be specified by the {@code String} representation of a
2852 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2853 * </P>
2854 *
2855 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2856 * URIs. The path part of these URIs must exist in the contacts database, in the
2857 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2858 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2859 * </P>
2860 *
2861 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002862 * @see Notification#EXTRA_PEOPLE
2863 */
Chris Wrene6c48932014-09-29 17:19:27 -04002864 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002865 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002866 return this;
2867 }
2868
2869 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002870 * Set this notification to be part of a group of notifications sharing the same key.
2871 * Grouped notifications may display in a cluster or stack on devices which
2872 * support such rendering.
2873 *
2874 * <p>To make this notification the summary for its group, also call
2875 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2876 * {@link #setSortKey}.
2877 * @param groupKey The group key of the group.
2878 * @return this object for method chaining
2879 */
2880 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002881 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002882 return this;
2883 }
2884
2885 /**
2886 * Set this notification to be the group summary for a group of notifications.
2887 * Grouped notifications may display in a cluster or stack on devices which
2888 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2889 * @param isGroupSummary Whether this notification should be a group summary.
2890 * @return this object for method chaining
2891 */
2892 public Builder setGroupSummary(boolean isGroupSummary) {
2893 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2894 return this;
2895 }
2896
2897 /**
2898 * Set a sort key that orders this notification among other notifications from the
2899 * same package. This can be useful if an external sort was already applied and an app
2900 * would like to preserve this. Notifications will be sorted lexicographically using this
2901 * value, although providing different priorities in addition to providing sort key may
2902 * cause this value to be ignored.
2903 *
2904 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002905 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002906 *
2907 * @see String#compareTo(String)
2908 */
2909 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002910 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002911 return this;
2912 }
2913
2914 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002915 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002916 *
Griff Hazen720042b2014-02-24 15:46:56 -08002917 * <p>Values within the Bundle will replace existing extras values in this Builder.
2918 *
2919 * @see Notification#extras
2920 */
Griff Hazen959591e2014-05-15 22:26:18 -07002921 public Builder addExtras(Bundle extras) {
2922 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002923 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002924 }
2925 return this;
2926 }
2927
2928 /**
2929 * Set metadata for this notification.
2930 *
2931 * <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 -04002932 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002933 * called.
2934 *
Griff Hazen720042b2014-02-24 15:46:56 -08002935 * <p>Replaces any existing extras values with those from the provided Bundle.
2936 * Use {@link #addExtras} to merge in metadata instead.
2937 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002938 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002939 */
Griff Hazen959591e2014-05-15 22:26:18 -07002940 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002941 if (extras != null) {
2942 mUserExtras = extras;
2943 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002944 return this;
2945 }
2946
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002947 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002948 * Get the current metadata Bundle used by this notification Builder.
2949 *
2950 * <p>The returned Bundle is shared with this Builder.
2951 *
2952 * <p>The current contents of this Bundle are copied into the Notification each time
2953 * {@link #build()} is called.
2954 *
2955 * @see Notification#extras
2956 */
2957 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002958 return mUserExtras;
2959 }
2960
2961 private Bundle getAllExtras() {
2962 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2963 saveExtras.putAll(mN.extras);
2964 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002965 }
2966
2967 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002968 * Add an action to this notification. Actions are typically displayed by
2969 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002970 * <p>
2971 * Every action must have an icon (32dp square and matching the
2972 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2973 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2974 * <p>
2975 * A notification in its expanded form can display up to 3 actions, from left to right in
2976 * the order they were added. Actions will not be displayed when the notification is
2977 * collapsed, however, so be sure that any essential functions may be accessed by the user
2978 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002979 *
2980 * @param icon Resource ID of a drawable that represents the action.
2981 * @param title Text describing the action.
2982 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002983 *
2984 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002985 */
Dan Sandler86647982015-05-13 23:41:13 -04002986 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002987 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002988 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002989 return this;
2990 }
2991
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002992 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002993 * Add an action to this notification. Actions are typically displayed by
2994 * the system as a button adjacent to the notification content.
2995 * <p>
2996 * Every action must have an icon (32dp square and matching the
2997 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2998 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2999 * <p>
3000 * A notification in its expanded form can display up to 3 actions, from left to right in
3001 * the order they were added. Actions will not be displayed when the notification is
3002 * collapsed, however, so be sure that any essential functions may be accessed by the user
3003 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3004 *
3005 * @param action The action to add.
3006 */
3007 public Builder addAction(Action action) {
3008 mActions.add(action);
3009 return this;
3010 }
3011
3012 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003013 * Alter the complete list of actions attached to this notification.
3014 * @see #addAction(Action).
3015 *
3016 * @param actions
3017 * @return
3018 */
3019 public Builder setActions(Action... actions) {
3020 mActions.clear();
3021 for (int i = 0; i < actions.length; i++) {
3022 mActions.add(actions[i]);
3023 }
3024 return this;
3025 }
3026
3027 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003028 * Add a rich notification style to be applied at build time.
3029 *
3030 * @param style Object responsible for modifying the notification style.
3031 */
3032 public Builder setStyle(Style style) {
3033 if (mStyle != style) {
3034 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003035 if (mStyle != null) {
3036 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003037 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3038 } else {
3039 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003040 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003041 }
3042 return this;
3043 }
3044
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003045 /**
3046 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003047 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003048 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3049 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3050 *
3051 * @return The same Builder.
3052 */
3053 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003054 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003055 return this;
3056 }
3057
3058 /**
3059 * Supply a replacement Notification whose contents should be shown in insecure contexts
3060 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3061 * @param n A replacement notification, presumably with some or all info redacted.
3062 * @return The same Builder.
3063 */
3064 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003065 if (n != null) {
3066 mN.publicVersion = new Notification();
3067 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3068 } else {
3069 mN.publicVersion = null;
3070 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003071 return this;
3072 }
3073
Griff Hazenb720abe2014-05-20 13:15:30 -07003074 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003075 * Apply an extender to this notification builder. Extenders may be used to add
3076 * metadata or change options on this builder.
3077 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003078 public Builder extend(Extender extender) {
3079 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003080 return this;
3081 }
3082
Dan Sandler4e787062015-06-17 15:09:48 -04003083 /**
3084 * @hide
3085 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003086 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003087 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003088 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003089 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003090 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003091 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003092 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003093 }
3094
Dan Sandler26e81cf2014-05-06 10:01:27 -04003095 /**
3096 * Sets {@link Notification#color}.
3097 *
3098 * @param argb The accent color to use
3099 *
3100 * @return The same Builder.
3101 */
Tor Norbye80756e32015-03-02 09:39:27 -08003102 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003103 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003104 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003105 return this;
3106 }
3107
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003108 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003109 // Note: This assumes that the current user can read the profile badge of the
3110 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003111 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003112 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003113 }
3114
3115 private Bitmap getProfileBadge() {
3116 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003117 if (badge == null) {
3118 return null;
3119 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003120 final int size = mContext.getResources().getDimensionPixelSize(
3121 R.dimen.notification_badge_size);
3122 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003123 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003124 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003125 badge.draw(canvas);
3126 return bitmap;
3127 }
3128
Selim Cinekc848c3a2016-01-13 15:27:30 -08003129 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003130 Bitmap profileBadge = getProfileBadge();
3131
Kenny Guy98193ea2014-07-24 19:54:37 +01003132 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003133 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3134 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003135 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003136 }
3137
Christoph Studerfe718432014-09-01 18:21:18 +02003138 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003139 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003140 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003141 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003142 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003143 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003144 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003145 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003146 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003147 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003148 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003149 }
3150
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003151 /**
3152 * Resets the notification header to its original state
3153 */
3154 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003155 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003156 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003157 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003158 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003159 contentView.setViewVisibility(R.id.header_text, View.GONE);
3160 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003161 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003162 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003163 contentView.setImageViewIcon(R.id.profile_badge, null);
3164 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003165 }
3166
3167 private void resetContentMargins(RemoteViews contentView) {
3168 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003169 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003170 }
3171
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003172 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003173 return applyStandardTemplate(resId, true /* hasProgress */);
3174 }
3175
3176 /**
3177 * @param hasProgress whether the progress bar should be shown and set
3178 */
3179 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003180 final Bundle ex = mN.extras;
3181
3182 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3183 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3184 return applyStandardTemplate(resId, hasProgress, title, text);
3185 }
3186
3187 /**
3188 * @param hasProgress whether the progress bar should be shown and set
3189 */
3190 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3191 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003192 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003193
Christoph Studerfe718432014-09-01 18:21:18 +02003194 resetStandardTemplate(contentView);
3195
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003196 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003197
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003198 bindNotificationHeader(contentView);
3199 bindLargeIcon(contentView);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003200 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003201 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003202 contentView.setTextViewText(R.id.title, title);
Joe Onorato561d3852010-11-20 18:09:34 -08003203 }
Selim Cinek29603462015-11-17 19:04:39 -08003204 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003205 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003206 int textId = showProgress ? com.android.internal.R.id.text_line_1
3207 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003208 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003209 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003210 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003211
Selim Cinek860b6da2015-12-16 19:02:19 -08003212 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003213
Selim Cinek29603462015-11-17 19:04:39 -08003214 return contentView;
3215 }
3216
Selim Cinek860b6da2015-12-16 19:02:19 -08003217 /**
3218 * @param remoteView the remote view to update the minheight in
3219 * @param hasMinHeight does it have a mimHeight
3220 * @hide
3221 */
3222 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3223 int minHeight = 0;
3224 if (hasMinHeight) {
3225 // we need to set the minHeight of the notification
3226 minHeight = mContext.getResources().getDimensionPixelSize(
3227 com.android.internal.R.dimen.notification_min_content_height);
3228 }
3229 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3230 }
3231
Selim Cinek29603462015-11-17 19:04:39 -08003232 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003233 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3234 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3235 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3236 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003237 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003238 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003239 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003240 contentView.setProgressBackgroundTintList(
3241 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3242 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003243 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003244 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003245 contentView.setProgressTintList(R.id.progress, colorStateList);
3246 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003247 }
Selim Cinek29603462015-11-17 19:04:39 -08003248 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003249 } else {
3250 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003251 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003252 }
Joe Onorato561d3852010-11-20 18:09:34 -08003253 }
3254
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003255 private void bindLargeIcon(RemoteViews contentView) {
3256 if (mN.mLargeIcon != null) {
3257 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3258 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3259 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3260 int endMargin = mContext.getResources().getDimensionPixelSize(
3261 R.dimen.notification_content_picture_margin);
3262 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003263 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003264 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003265 }
3266 }
3267
3268 private void bindNotificationHeader(RemoteViews contentView) {
3269 bindSmallIcon(contentView);
3270 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003271 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003272 bindHeaderChronometerAndTime(contentView);
3273 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003274 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003275 }
3276
3277 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003278 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003279 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003280 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003281 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003282 }
3283
3284 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3285 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003286 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003287 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3288 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3289 contentView.setLong(R.id.chronometer, "setBase",
3290 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3291 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003292 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003293 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003294 } else {
3295 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3296 contentView.setLong(R.id.time, "setTime", mN.when);
3297 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003298 } else {
3299 // We still want a time to be set but gone, such that we can show and hide it
3300 // on demand in case it's a child notification without anything in the header
3301 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003302 }
3303 }
3304
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003305 private void bindHeaderText(RemoteViews contentView) {
3306 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3307 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003308 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003309 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003310 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003311 if (headerText == null
3312 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3313 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3314 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3315 }
3316 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003317 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003318 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3319 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3320 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003321 }
3322 }
3323
Adrian Rooseba05822016-04-22 17:09:27 -07003324 /**
3325 * @hide
3326 */
3327 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003328 CharSequence name = null;
3329 final PackageManager pm = mContext.getPackageManager();
3330 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3331 // only system packages which lump together a bunch of unrelated stuff
3332 // may substitute a different name to make the purpose of the
3333 // notification more clear. the correct package label should always
3334 // be accessible via SystemUI.
3335 final String pkg = mContext.getPackageName();
3336 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3337 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3338 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3339 name = subName;
3340 } else {
3341 Log.w(TAG, "warning: pkg "
3342 + pkg + " attempting to substitute app name '" + subName
3343 + "' without holding perm "
3344 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3345 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003346 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003347 if (TextUtils.isEmpty(name)) {
3348 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3349 }
3350 if (TextUtils.isEmpty(name)) {
3351 // still nothing?
3352 return null;
3353 }
3354
3355 return String.valueOf(name);
3356 }
3357 private void bindHeaderAppName(RemoteViews contentView) {
3358 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003359 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003360 }
3361
3362 private void bindSmallIcon(RemoteViews contentView) {
3363 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3364 processSmallIconColor(mN.mSmallIcon, contentView);
3365 }
3366
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003367 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003368 * @return true if the built notification will show the time or the chronometer; false
3369 * otherwise
3370 */
3371 private boolean showsTimeOrChronometer() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003372 return mN.showsTimeOrChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003373 }
3374
Christoph Studerfe718432014-09-01 18:21:18 +02003375 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003376 // actions_container is only reset when there are no actions to avoid focus issues with
3377 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003378 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003379 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003380
3381 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3382 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3383
3384 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3385 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3386 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3387 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003388 }
3389
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003390 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003391 final Bundle ex = mN.extras;
3392
3393 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3394 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3395 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3396 }
3397
3398 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3399 CharSequence title, CharSequence text) {
3400 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003401
Christoph Studerfe718432014-09-01 18:21:18 +02003402 resetStandardTemplateWithActions(big);
3403
Adrian Roose458aa82015-12-08 16:17:19 -08003404 boolean validRemoteInput = false;
3405
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003406 int N = mActions.size();
3407 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003408 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003409 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003410 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003411 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003412 Action action = mActions.get(i);
3413 validRemoteInput |= hasValidRemoteInput(action);
3414
3415 final RemoteViews button = generateActionButton(action);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003416 big.addView(R.id.actions, button);
3417 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003418 } else {
3419 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003420 }
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));
Adrian Roos96b7e202016-05-17 13:50:38 -07003563 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3564 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_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
Selim Cinek75998782016-04-26 10:39:17 -07004289 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004290 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004291
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004292 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004293
Selim Cinek75998782016-04-26 10:39:17 -07004294 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004295
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004296 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004297 if (TextUtils.isEmpty(bigTextText)) {
4298 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4299 // experience
4300 bigTextText = mBuilder.processLegacyText(text);
4301 }
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004302 contentView.setTextViewText(R.id.big_text, bigTextText);
4303 contentView.setViewVisibility(R.id.big_text,
4304 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004305 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004306 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4307
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004308 return contentView;
4309 }
4310
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004311 private int calculateMaxLines() {
4312 int lineCount = MAX_LINES;
4313 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004314 if (hasActions) {
4315 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4316 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004317 return lineCount;
4318 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004319 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004320
4321 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004322 * Helper class for generating large-format notifications that include multiple back-and-forth
4323 * messages of varying types between any number of people.
4324 *
4325 * <br>
4326 * If the platform does not provide large-format notifications, this method has no effect. The
4327 * user will always see the normal notification view.
4328 * <br>
4329 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4330 * so:
4331 * <pre class="prettyprint">
4332 *
4333 * Notification noti = new Notification.Builder()
4334 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4335 * .setContentText(subject)
4336 * .setSmallIcon(R.drawable.new_message)
4337 * .setLargeIcon(aBitmap)
4338 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4339 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4340 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4341 * .build();
4342 * </pre>
4343 */
4344 public static class MessagingStyle extends Style {
4345
4346 /**
4347 * The maximum number of messages that will be retained in the Notification itself (the
4348 * number displayed is up to the platform).
4349 */
4350 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4351
4352 CharSequence mUserDisplayName;
4353 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004354 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004355
4356 MessagingStyle() {
4357 }
4358
4359 /**
4360 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4361 * posting app reposts the notification with those messages after they've been actually
4362 * sent and in previous messages sent by the user added in
4363 * {@link #addMessage(Notification.MessagingStyle.Message)}
4364 */
4365 public MessagingStyle(CharSequence userDisplayName) {
4366 mUserDisplayName = userDisplayName;
4367 }
4368
4369 /**
4370 * Returns the name to be displayed for any replies sent by the user
4371 */
4372 public CharSequence getUserDisplayName() {
4373 return mUserDisplayName;
4374 }
4375
4376 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004377 * Sets the title to be displayed on this conversation. This should only be used for
4378 * group messaging and left unset for one-on-one conversations.
4379 * @param conversationTitle
4380 * @return this object for method chaining.
4381 */
4382 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4383 mConversationTitle = conversationTitle;
4384 return this;
4385 }
4386
4387 /**
4388 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4389 * should be for one-on-one conversations
4390 */
4391 public CharSequence getConversationTitle() {
4392 return mConversationTitle;
4393 }
4394
4395 /**
4396 * Adds a message for display by this notification. Convenience call for a simple
4397 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4398 * @param text A {@link CharSequence} to be displayed as the message content
4399 * @param timestamp Time at which the message arrived
4400 * @param sender A {@link CharSequence} to be used for displaying the name of the
4401 * sender. Should be <code>null</code> for messages by the current user, in which case
4402 * the platform will insert {@link #getUserDisplayName()}.
4403 * Should be unique amongst all individuals in the conversation, and should be
4404 * consistent during re-posts of the notification.
4405 *
4406 * @see Message#Message(CharSequence, long, CharSequence)
4407 *
4408 * @return this object for method chaining
4409 */
4410 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4411 mMessages.add(new Message(text, timestamp, sender));
4412 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4413 mMessages.remove(0);
4414 }
4415 return this;
4416 }
4417
4418 /**
4419 * Adds a {@link Message} for display in this notification.
4420 * @param message The {@link Message} to be displayed
4421 * @return this object for method chaining
4422 */
4423 public MessagingStyle addMessage(Message message) {
4424 mMessages.add(message);
4425 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4426 mMessages.remove(0);
4427 }
4428 return this;
4429 }
4430
4431 /**
4432 * Gets the list of {@code Message} objects that represent the notification
4433 */
4434 public List<Message> getMessages() {
4435 return mMessages;
4436 }
4437
4438 /**
4439 * @hide
4440 */
4441 @Override
4442 public void addExtras(Bundle extras) {
4443 super.addExtras(extras);
4444 if (mUserDisplayName != null) {
4445 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4446 }
4447 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004448 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004449 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004450 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4451 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004452 }
4453 }
4454
4455 /**
4456 * @hide
4457 */
4458 @Override
4459 protected void restoreFromExtras(Bundle extras) {
4460 super.restoreFromExtras(extras);
4461
4462 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004463 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4464 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004465 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004466 if (parcelables != null && parcelables instanceof Parcelable[]) {
4467 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004468 }
4469 }
4470
4471 /**
4472 * @hide
4473 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004474 @Override
4475 public RemoteViews makeContentView() {
4476 Message m = findLatestIncomingMessage();
4477 CharSequence title = mConversationTitle != null
4478 ? mConversationTitle
4479 : (m == null) ? null : m.mSender;
4480 CharSequence text = (m == null)
4481 ? null
4482 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004483
Adrian Roosc1a80b02016-04-05 14:54:55 -07004484 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4485 false /* hasProgress */,
4486 title,
4487 text);
4488 }
4489
4490 private Message findLatestIncomingMessage() {
4491 for (int i = mMessages.size() - 1; i >= 0; i--) {
4492 Message m = mMessages.get(i);
4493 // Incoming messages have a non-empty sender.
4494 if (!TextUtils.isEmpty(m.mSender)) {
4495 return m;
4496 }
4497 }
4498 return null;
4499 }
4500
4501 /**
4502 * @hide
4503 */
4504 @Override
4505 public RemoteViews makeBigContentView() {
4506 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4507 ? super.mBigContentTitle
4508 : mConversationTitle;
4509 boolean hasTitle = !TextUtils.isEmpty(title);
4510
Adrian Roos48d746a2016-04-12 14:57:28 -07004511 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004512 mBuilder.getMessagingLayoutResource(),
4513 false /* hasProgress */,
4514 title,
4515 null /* text */);
4516
4517 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4518 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4519
4520 // Make sure all rows are gone in case we reuse a view.
4521 for (int rowId : rowIds) {
4522 contentView.setViewVisibility(rowId, View.GONE);
4523 }
4524
4525 int i=0;
4526 int titlePadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4527 R.dimen.notification_messaging_spacing);
4528 contentView.setViewLayoutMarginBottom(R.id.line1, hasTitle ? titlePadding : 0);
4529 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
4530 mBuilder.mN.mLargeIcon == null ? 0 : (hasTitle ? 1 : 2));
4531
4532 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4533 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4534 Message m = mMessages.get(firstMessage + i);
4535 int rowId = rowIds[i];
4536
4537 contentView.setViewVisibility(rowId, View.VISIBLE);
4538 contentView.setTextViewText(rowId, makeMessageLine(m));
4539
4540 i++;
4541 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004542 return contentView;
4543 }
4544
Adrian Roosc1a80b02016-04-05 14:54:55 -07004545 private CharSequence makeMessageLine(Message m) {
4546 BidiFormatter bidi = BidiFormatter.getInstance();
4547 SpannableStringBuilder sb = new SpannableStringBuilder();
4548 if (TextUtils.isEmpty(m.mSender)) {
4549 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4550 sb.append(bidi.unicodeWrap(replyName),
4551 makeFontColorSpan(mBuilder.resolveContrastColor()),
4552 0 /* flags */);
4553 } else {
4554 sb.append(bidi.unicodeWrap(m.mSender),
4555 makeFontColorSpan(Color.BLACK),
4556 0 /* flags */);
4557 }
4558 CharSequence text = m.mText == null ? "" : m.mText;
4559 sb.append(" ").append(bidi.unicodeWrap(text));
4560 return sb;
4561 }
4562
Adrian Roosdedd1df2016-04-26 16:38:47 -07004563 /**
4564 * @hide
4565 */
4566 @Override
4567 public RemoteViews makeHeadsUpContentView() {
4568 Message m = findLatestIncomingMessage();
4569 CharSequence title = mConversationTitle != null
4570 ? mConversationTitle
4571 : (m == null) ? null : m.mSender;
4572 CharSequence text = (m == null)
4573 ? null
4574 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4575
4576 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4577 false /* hasProgress */,
4578 title,
4579 text);
4580 }
4581
Adrian Roosc1a80b02016-04-05 14:54:55 -07004582 private static TextAppearanceSpan makeFontColorSpan(int color) {
4583 return new TextAppearanceSpan(null, 0, 0,
4584 ColorStateList.valueOf(color), null);
4585 }
4586
Alex Hillsd9b04d92016-04-11 16:38:16 -04004587 public static final class Message {
4588
4589 static final String KEY_TEXT = "text";
4590 static final String KEY_TIMESTAMP = "time";
4591 static final String KEY_SENDER = "sender";
4592 static final String KEY_DATA_MIME_TYPE = "type";
4593 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004594
4595 private final CharSequence mText;
4596 private final long mTimestamp;
4597 private final CharSequence mSender;
4598
4599 private String mDataMimeType;
4600 private Uri mDataUri;
4601
4602 /**
4603 * Constructor
4604 * @param text A {@link CharSequence} to be displayed as the message content
4605 * @param timestamp Time at which the message arrived
4606 * @param sender A {@link CharSequence} to be used for displaying the name of the
4607 * sender. Should be <code>null</code> for messages by the current user, in which case
4608 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4609 * Should be unique amongst all individuals in the conversation, and should be
4610 * consistent during re-posts of the notification.
4611 */
4612 public Message(CharSequence text, long timestamp, CharSequence sender){
4613 mText = text;
4614 mTimestamp = timestamp;
4615 mSender = sender;
4616 }
4617
4618 /**
4619 * Sets a binary blob of data and an associated MIME type for a message. In the case
4620 * where the platform doesn't support the MIME type, the original text provided in the
4621 * constructor will be used.
4622 * @param dataMimeType The MIME type of the content. See
4623 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4624 * types on Android and Android Wear.
4625 * @param dataUri The uri containing the content whose type is given by the MIME type.
4626 * <p class="note">
4627 * <ol>
4628 * <li>Notification Listeners including the System UI need permission to access the
4629 * data the Uri points to. The recommended ways to do this are:</li>
4630 * <li>Store the data in your own ContentProvider, making sure that other apps have
4631 * the correct permission to access your provider. The preferred mechanism for
4632 * providing access is to use per-URI permissions which are temporary and only
4633 * grant access to the receiving application. An easy way to create a
4634 * ContentProvider like this is to use the FileProvider helper class.</li>
4635 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4636 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4637 * also store non-media types (see MediaStore.Files for more info). Files can be
4638 * inserted into the MediaStore using scanFile() after which a content:// style
4639 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4640 * Note that once added to the system MediaStore the content is accessible to any
4641 * app on the device.</li>
4642 * </ol>
4643 * @return this object for method chaining
4644 */
4645 public Message setData(String dataMimeType, Uri dataUri) {
4646 mDataMimeType = dataMimeType;
4647 mDataUri = dataUri;
4648 return this;
4649 }
4650
Alex Hillsfc737de2016-03-23 17:33:02 -04004651 /**
4652 * Get the text to be used for this message, or the fallback text if a type and content
4653 * Uri have been set
4654 */
4655 public CharSequence getText() {
4656 return mText;
4657 }
4658
4659 /**
4660 * Get the time at which this message arrived
4661 */
4662 public long getTimestamp() {
4663 return mTimestamp;
4664 }
4665
4666 /**
4667 * Get the text used to display the contact's name in the messaging experience
4668 */
4669 public CharSequence getSender() {
4670 return mSender;
4671 }
4672
4673 /**
4674 * Get the MIME type of the data pointed to by the Uri
4675 */
4676 public String getDataMimeType() {
4677 return mDataMimeType;
4678 }
4679
4680 /**
4681 * Get the the Uri pointing to the content of the message. Can be null, in which case
4682 * {@see #getText()} is used.
4683 */
4684 public Uri getDataUri() {
4685 return mDataUri;
4686 }
4687
Alex Hillsd9b04d92016-04-11 16:38:16 -04004688 private Bundle toBundle() {
4689 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04004690 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004691 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04004692 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004693 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04004694 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004695 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04004696 }
4697 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004698 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04004699 }
4700 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004701 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04004702 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004703 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04004704 }
4705
Alex Hillsd9b04d92016-04-11 16:38:16 -04004706 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
4707 Bundle[] bundles = new Bundle[messages.size()];
4708 final int N = messages.size();
4709 for (int i = 0; i < N; i++) {
4710 bundles[i] = messages.get(i).toBundle();
4711 }
4712 return bundles;
4713 }
4714
Adrian Roosdedd1df2016-04-26 16:38:47 -07004715 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004716 List<Message> messages = new ArrayList<>(bundles.length);
4717 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07004718 if (bundles[i] instanceof Bundle) {
4719 Message message = getMessageFromBundle((Bundle)bundles[i]);
4720 if (message != null) {
4721 messages.add(message);
4722 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004723 }
4724 }
4725 return messages;
4726 }
4727
4728 static Message getMessageFromBundle(Bundle bundle) {
4729 try {
4730 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP) ||
4731 !bundle.containsKey(KEY_SENDER)) {
4732 return null;
4733 } else {
4734 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
4735 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
4736 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
4737 bundle.containsKey(KEY_DATA_URI)) {
4738
4739 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
4740 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04004741 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004742 return message;
4743 }
4744 } catch (ClassCastException e) {
4745 return null;
4746 }
4747 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004748 }
4749 }
4750
4751 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004752 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004753 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004754 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004755 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004756 * Notification notif = new Notification.Builder(mContext)
4757 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4758 * .setContentText(subject)
4759 * .setSmallIcon(R.drawable.new_mail)
4760 * .setLargeIcon(aBitmap)
4761 * .setStyle(new Notification.InboxStyle()
4762 * .addLine(str1)
4763 * .addLine(str2)
4764 * .setContentTitle(&quot;&quot;)
4765 * .setSummaryText(&quot;+3 more&quot;))
4766 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004767 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004768 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004769 * @see Notification#bigContentView
4770 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004771 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004772 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4773
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004774 public InboxStyle() {
4775 }
4776
Daniel Sandler879c5e02012-04-17 16:46:51 -04004777 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004778 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004779 }
4780
Chris Wrend6297db2012-05-03 16:20:13 -04004781 /**
4782 * Overrides ContentTitle in the big form of the template.
4783 * This defaults to the value passed to setContentTitle().
4784 */
4785 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004786 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004787 return this;
4788 }
4789
4790 /**
4791 * Set the first line of text after the detail section in the big form of the template.
4792 */
4793 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004794 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004795 return this;
4796 }
4797
Chris Wren0bd664d2012-08-01 13:56:56 -04004798 /**
4799 * Append a line to the digest section of the Inbox notification.
4800 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004801 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004802 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004803 return this;
4804 }
4805
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004806 /**
4807 * @hide
4808 */
4809 public void addExtras(Bundle extras) {
4810 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004811
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004812 CharSequence[] a = new CharSequence[mTexts.size()];
4813 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4814 }
4815
Christoph Studer4600f9b2014-07-22 22:44:43 +02004816 /**
4817 * @hide
4818 */
4819 @Override
4820 protected void restoreFromExtras(Bundle extras) {
4821 super.restoreFromExtras(extras);
4822
4823 mTexts.clear();
4824 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4825 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4826 }
4827 }
4828
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004829 /**
4830 * @hide
4831 */
4832 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004833 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004834 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004835 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4836 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004837
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004838 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004839
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004840 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004841
Chris Wrend6297db2012-05-03 16:20:13 -04004842 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 -04004843 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004844
Chris Wren4ed80d52012-05-17 09:30:03 -04004845 // Make sure all rows are gone in case we reuse a view.
4846 for (int rowId : rowIds) {
4847 contentView.setViewVisibility(rowId, View.GONE);
4848 }
4849
Daniel Sandler879c5e02012-04-17 16:46:51 -04004850 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07004851 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4852 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08004853 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07004854 int onlyViewId = 0;
4855 int maxRows = rowIds.length;
4856 if (mBuilder.mActions.size() > 0) {
4857 maxRows--;
4858 }
4859 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004860 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07004861 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004862 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004863 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07004864 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004865 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07004866 if (first) {
4867 onlyViewId = rowIds[i];
4868 } else {
4869 onlyViewId = 0;
4870 }
Selim Cinek247fa012016-02-18 09:50:48 -08004871 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004872 }
4873 i++;
4874 }
Selim Cinek07c80172016-04-21 16:40:47 -07004875 if (onlyViewId != 0) {
4876 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
4877 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4878 R.dimen.notification_text_margin_top);
4879 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
4880 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004881
Daniel Sandler879c5e02012-04-17 16:46:51 -04004882 return contentView;
4883 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004884
Selim Cinek247fa012016-02-18 09:50:48 -08004885 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004886 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004887 if (first) {
4888 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4889 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4890 boolean hasProgress = max != 0 || ind;
4891 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4892 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4893 R.dimen.notification_content_picture_margin);
4894 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004895 }
4896 contentView.setViewLayoutMarginEnd(id, endMargin);
4897 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004898 }
Dan Sandler842dd772014-05-15 09:36:47 -04004899
4900 /**
4901 * Notification style for media playback notifications.
4902 *
4903 * In the expanded form, {@link Notification#bigContentView}, up to 5
4904 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004905 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004906 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4907 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4908 * treated as album artwork.
4909 *
4910 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4911 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004912 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004913 * in the standard view alongside the usual content.
4914 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004915 * Notifications created with MediaStyle will have their category set to
4916 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4917 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4918 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004919 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4920 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004921 * the System UI can identify this as a notification representing an active media session
4922 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4923 *
4924 * To use this style with your Notification, feed it to
4925 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4926 * <pre class="prettyprint">
4927 * Notification noti = new Notification.Builder()
4928 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004929 * .setContentTitle(&quot;Track title&quot;)
4930 * .setContentText(&quot;Artist - Album&quot;)
4931 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004932 * .setStyle(<b>new Notification.MediaStyle()</b>
4933 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004934 * .build();
4935 * </pre>
4936 *
4937 * @see Notification#bigContentView
4938 */
4939 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004940 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004941 static final int MAX_MEDIA_BUTTONS = 5;
4942
4943 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004944 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004945
4946 public MediaStyle() {
4947 }
4948
4949 public MediaStyle(Builder builder) {
4950 setBuilder(builder);
4951 }
4952
4953 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004954 * 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 -04004955 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004956 *
4957 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004958 */
4959 public MediaStyle setShowActionsInCompactView(int...actions) {
4960 mActionsToShowInCompact = actions;
4961 return this;
4962 }
4963
4964 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004965 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4966 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004967 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004968 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004969 mToken = token;
4970 return this;
4971 }
4972
Christoph Studer4600f9b2014-07-22 22:44:43 +02004973 /**
4974 * @hide
4975 */
Dan Sandler842dd772014-05-15 09:36:47 -04004976 @Override
4977 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004978 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004979 if (wip.category == null) {
4980 wip.category = Notification.CATEGORY_TRANSPORT;
4981 }
Dan Sandler842dd772014-05-15 09:36:47 -04004982 return wip;
4983 }
4984
Christoph Studer4600f9b2014-07-22 22:44:43 +02004985 /**
4986 * @hide
4987 */
4988 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004989 public RemoteViews makeContentView() {
4990 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004991 }
4992
4993 /**
4994 * @hide
4995 */
4996 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004997 public RemoteViews makeBigContentView() {
4998 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004999 }
5000
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005001 /**
5002 * @hide
5003 */
5004 @Override
5005 public RemoteViews makeHeadsUpContentView() {
5006 RemoteViews expanded = makeMediaBigContentView();
5007 return expanded != null ? expanded : makeMediaContentView();
5008 }
5009
Dan Sandler842dd772014-05-15 09:36:47 -04005010 /** @hide */
5011 @Override
5012 public void addExtras(Bundle extras) {
5013 super.addExtras(extras);
5014
5015 if (mToken != null) {
5016 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5017 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005018 if (mActionsToShowInCompact != null) {
5019 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5020 }
Dan Sandler842dd772014-05-15 09:36:47 -04005021 }
5022
Christoph Studer4600f9b2014-07-22 22:44:43 +02005023 /**
5024 * @hide
5025 */
5026 @Override
5027 protected void restoreFromExtras(Bundle extras) {
5028 super.restoreFromExtras(extras);
5029
5030 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5031 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5032 }
5033 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5034 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5035 }
5036 }
5037
Selim Cinek5bf069a2015-11-10 19:14:27 -05005038 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005039 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005040 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005041 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005042 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005043 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5044 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005045 if (!tombstone) {
5046 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5047 }
5048 button.setContentDescription(R.id.action0, action.title);
5049 return button;
5050 }
5051
5052 private RemoteViews makeMediaContentView() {
5053 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005054 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005055
5056 final int numActions = mBuilder.mActions.size();
5057 final int N = mActionsToShowInCompact == null
5058 ? 0
5059 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5060 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005061 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005062 for (int i = 0; i < N; i++) {
5063 if (i >= numActions) {
5064 throw new IllegalArgumentException(String.format(
5065 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5066 i, numActions - 1));
5067 }
5068
5069 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005070 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005071 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005072 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005073 }
5074 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005075 handleImage(view);
5076 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08005077 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5078 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005079 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08005080 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5081 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08005082 }
5083 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005084 return view;
5085 }
5086
5087 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005088 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005089 // Dont add an expanded view if there is no more content to be revealed
5090 int actionsInCompact = mActionsToShowInCompact == null
5091 ? 0
5092 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5093 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
5094 return null;
5095 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005096 RemoteViews big = mBuilder.applyStandardTemplate(
5097 R.layout.notification_template_material_big_media,
5098 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005099
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005100 if (actionCount > 0) {
5101 big.removeAllViews(com.android.internal.R.id.media_actions);
5102 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005103 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005104 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005105 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005106 }
5107 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005108 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005109 return big;
5110 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005111
Selim Cinek5bf069a2015-11-10 19:14:27 -05005112 private void handleImage(RemoteViews contentView) {
5113 if (mBuilder.mN.mLargeIcon != null) {
5114 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005115 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005116 }
5117 }
5118
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005119 /**
5120 * @hide
5121 */
5122 @Override
5123 protected boolean hasProgress() {
5124 return false;
5125 }
Dan Sandler842dd772014-05-15 09:36:47 -04005126 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005127
Selim Cinek593610c2016-02-16 18:42:57 -08005128 /**
5129 * Notification style for custom views that are decorated by the system
5130 *
5131 * <p>Instead of providing a notification that is completely custom, a developer can set this
5132 * style and still obtain system decorations like the notification header with the expand
5133 * affordance and actions.
5134 *
5135 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5136 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5137 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5138 * corresponding custom views to display.
5139 *
5140 * To use this style with your Notification, feed it to
5141 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5142 * <pre class="prettyprint">
5143 * Notification noti = new Notification.Builder()
5144 * .setSmallIcon(R.drawable.ic_stat_player)
5145 * .setLargeIcon(albumArtBitmap))
5146 * .setCustomContentView(contentView);
5147 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5148 * .build();
5149 * </pre>
5150 */
5151 public static class DecoratedCustomViewStyle extends Style {
5152
5153 public DecoratedCustomViewStyle() {
5154 }
5155
5156 public DecoratedCustomViewStyle(Builder builder) {
5157 setBuilder(builder);
5158 }
5159
5160 /**
5161 * @hide
5162 */
5163 public boolean displayCustomViewInline() {
5164 return true;
5165 }
5166
5167 /**
5168 * @hide
5169 */
5170 @Override
5171 public RemoteViews makeContentView() {
5172 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5173 }
5174
5175 /**
5176 * @hide
5177 */
5178 @Override
5179 public RemoteViews makeBigContentView() {
5180 return makeDecoratedBigContentView();
5181 }
5182
5183 /**
5184 * @hide
5185 */
5186 @Override
5187 public RemoteViews makeHeadsUpContentView() {
5188 return makeDecoratedHeadsUpContentView();
5189 }
5190
Selim Cinek593610c2016-02-16 18:42:57 -08005191 private RemoteViews makeDecoratedHeadsUpContentView() {
5192 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5193 ? mBuilder.mN.contentView
5194 : mBuilder.mN.headsUpContentView;
5195 if (mBuilder.mActions.size() == 0) {
5196 return makeStandardTemplateWithCustomContent(headsUpContentView);
5197 }
5198 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5199 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005200 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005201 return remoteViews;
5202 }
5203
Selim Cinek593610c2016-02-16 18:42:57 -08005204 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5205 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5206 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005207 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005208 return remoteViews;
5209 }
5210
Selim Cinek593610c2016-02-16 18:42:57 -08005211 private RemoteViews makeDecoratedBigContentView() {
5212 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5213 ? mBuilder.mN.contentView
5214 : mBuilder.mN.bigContentView;
5215 if (mBuilder.mActions.size() == 0) {
5216 return makeStandardTemplateWithCustomContent(bigContentView);
5217 }
5218 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5219 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005220 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005221 return remoteViews;
5222 }
Selim Cinek247fa012016-02-18 09:50:48 -08005223
5224 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5225 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005226 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005227 // Need to clone customContent before adding, because otherwise it can no longer be
5228 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005229 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005230 remoteViews.removeAllViews(R.id.notification_main_column);
5231 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005232 }
Selim Cinek247fa012016-02-18 09:50:48 -08005233 // also update the end margin if there is an image
5234 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5235 R.dimen.notification_content_margin_end);
5236 if (mBuilder.mN.mLargeIcon != null) {
5237 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5238 R.dimen.notification_content_picture_margin);
5239 }
5240 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
5241 }
Selim Cinek593610c2016-02-16 18:42:57 -08005242 }
5243
Selim Cinek03eb3b72016-02-18 10:39:45 -08005244 /**
5245 * Notification style for media custom views that are decorated by the system
5246 *
5247 * <p>Instead of providing a media notification that is completely custom, a developer can set
5248 * this style and still obtain system decorations like the notification header with the expand
5249 * affordance and actions.
5250 *
5251 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5252 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5253 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5254 * corresponding custom views to display.
5255 *
5256 * To use this style with your Notification, feed it to
5257 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5258 * <pre class="prettyprint">
5259 * Notification noti = new Notification.Builder()
5260 * .setSmallIcon(R.drawable.ic_stat_player)
5261 * .setLargeIcon(albumArtBitmap))
5262 * .setCustomContentView(contentView);
5263 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5264 * .setMediaSession(mySession))
5265 * .build();
5266 * </pre>
5267 *
5268 * @see android.app.Notification.DecoratedCustomViewStyle
5269 * @see android.app.Notification.MediaStyle
5270 */
5271 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5272
5273 public DecoratedMediaCustomViewStyle() {
5274 }
5275
5276 public DecoratedMediaCustomViewStyle(Builder builder) {
5277 setBuilder(builder);
5278 }
5279
5280 /**
5281 * @hide
5282 */
5283 public boolean displayCustomViewInline() {
5284 return true;
5285 }
5286
5287 /**
5288 * @hide
5289 */
5290 @Override
5291 public RemoteViews makeContentView() {
5292 RemoteViews remoteViews = super.makeContentView();
5293 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5294 mBuilder.mN.contentView);
5295 }
5296
5297 /**
5298 * @hide
5299 */
5300 @Override
5301 public RemoteViews makeBigContentView() {
5302 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5303 ? mBuilder.mN.bigContentView
5304 : mBuilder.mN.contentView;
5305 return makeBigContentViewWithCustomContent(customRemoteView);
5306 }
5307
5308 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5309 RemoteViews remoteViews = super.makeBigContentView();
5310 if (remoteViews != null) {
5311 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5312 customRemoteView);
5313 } else if (customRemoteView != mBuilder.mN.contentView){
5314 remoteViews = super.makeContentView();
5315 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5316 customRemoteView);
5317 } else {
5318 return null;
5319 }
5320 }
5321
5322 /**
5323 * @hide
5324 */
5325 @Override
5326 public RemoteViews makeHeadsUpContentView() {
5327 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5328 ? mBuilder.mN.headsUpContentView
5329 : mBuilder.mN.contentView;
5330 return makeBigContentViewWithCustomContent(customRemoteView);
5331 }
5332
5333 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5334 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005335 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005336 // Need to clone customContent before adding, because otherwise it can no longer be
5337 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005338 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005339 remoteViews.removeAllViews(id);
5340 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005341 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005342 return remoteViews;
5343 }
5344 }
5345
Christoph Studer4600f9b2014-07-22 22:44:43 +02005346 // When adding a new Style subclass here, don't forget to update
5347 // Builder.getNotificationStyleClass.
5348
Griff Hazen61a9e862014-05-22 16:05:19 -07005349 /**
5350 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5351 * metadata or change options on a notification builder.
5352 */
5353 public interface Extender {
5354 /**
5355 * Apply this extender to a notification builder.
5356 * @param builder the builder to be modified.
5357 * @return the build object for chaining.
5358 */
5359 public Builder extend(Builder builder);
5360 }
5361
5362 /**
5363 * Helper class to add wearable extensions to notifications.
5364 * <p class="note"> See
5365 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5366 * for Android Wear</a> for more information on how to use this class.
5367 * <p>
5368 * To create a notification with wearable extensions:
5369 * <ol>
5370 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5371 * properties.
5372 * <li>Create a {@link android.app.Notification.WearableExtender}.
5373 * <li>Set wearable-specific properties using the
5374 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5375 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5376 * notification.
5377 * <li>Post the notification to the notification system with the
5378 * {@code NotificationManager.notify(...)} methods.
5379 * </ol>
5380 *
5381 * <pre class="prettyprint">
5382 * Notification notif = new Notification.Builder(mContext)
5383 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5384 * .setContentText(subject)
5385 * .setSmallIcon(R.drawable.new_mail)
5386 * .extend(new Notification.WearableExtender()
5387 * .setContentIcon(R.drawable.new_mail))
5388 * .build();
5389 * NotificationManager notificationManger =
5390 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5391 * notificationManger.notify(0, notif);</pre>
5392 *
5393 * <p>Wearable extensions can be accessed on an existing notification by using the
5394 * {@code WearableExtender(Notification)} constructor,
5395 * and then using the {@code get} methods to access values.
5396 *
5397 * <pre class="prettyprint">
5398 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5399 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005400 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005401 */
5402 public static final class WearableExtender implements Extender {
5403 /**
5404 * Sentinel value for an action index that is unset.
5405 */
5406 public static final int UNSET_ACTION_INDEX = -1;
5407
5408 /**
5409 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5410 * default sizing.
5411 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005412 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005413 * on their content.
5414 */
5415 public static final int SIZE_DEFAULT = 0;
5416
5417 /**
5418 * Size value for use with {@link #setCustomSizePreset} to show this notification
5419 * with an extra small size.
5420 * <p>This value is only applicable for custom display notifications created using
5421 * {@link #setDisplayIntent}.
5422 */
5423 public static final int SIZE_XSMALL = 1;
5424
5425 /**
5426 * Size value for use with {@link #setCustomSizePreset} to show this notification
5427 * with a small size.
5428 * <p>This value is only applicable for custom display notifications created using
5429 * {@link #setDisplayIntent}.
5430 */
5431 public static final int SIZE_SMALL = 2;
5432
5433 /**
5434 * Size value for use with {@link #setCustomSizePreset} to show this notification
5435 * with a medium size.
5436 * <p>This value is only applicable for custom display notifications created using
5437 * {@link #setDisplayIntent}.
5438 */
5439 public static final int SIZE_MEDIUM = 3;
5440
5441 /**
5442 * Size value for use with {@link #setCustomSizePreset} to show this notification
5443 * with a large size.
5444 * <p>This value is only applicable for custom display notifications created using
5445 * {@link #setDisplayIntent}.
5446 */
5447 public static final int SIZE_LARGE = 4;
5448
Griff Hazend5f11f92014-05-27 15:40:09 -07005449 /**
5450 * Size value for use with {@link #setCustomSizePreset} to show this notification
5451 * full screen.
5452 * <p>This value is only applicable for custom display notifications created using
5453 * {@link #setDisplayIntent}.
5454 */
5455 public static final int SIZE_FULL_SCREEN = 5;
5456
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005457 /**
5458 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5459 * short amount of time when this notification is displayed on the screen. This
5460 * is the default value.
5461 */
5462 public static final int SCREEN_TIMEOUT_SHORT = 0;
5463
5464 /**
5465 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5466 * for a longer amount of time when this notification is displayed on the screen.
5467 */
5468 public static final int SCREEN_TIMEOUT_LONG = -1;
5469
Griff Hazen61a9e862014-05-22 16:05:19 -07005470 /** Notification extra which contains wearable extensions */
5471 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5472
Pete Gastaf6781d2014-10-07 15:17:05 -04005473 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005474 private static final String KEY_ACTIONS = "actions";
5475 private static final String KEY_FLAGS = "flags";
5476 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5477 private static final String KEY_PAGES = "pages";
5478 private static final String KEY_BACKGROUND = "background";
5479 private static final String KEY_CONTENT_ICON = "contentIcon";
5480 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5481 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5482 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5483 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5484 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005485 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005486 private static final String KEY_DISMISSAL_ID = "dismissalId";
Griff Hazen61a9e862014-05-22 16:05:19 -07005487
5488 // Flags bitwise-ored to mFlags
5489 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5490 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5491 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5492 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005493 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005494 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005495 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005496
5497 // Default value for flags integer
5498 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5499
5500 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5501 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5502
5503 private ArrayList<Action> mActions = new ArrayList<Action>();
5504 private int mFlags = DEFAULT_FLAGS;
5505 private PendingIntent mDisplayIntent;
5506 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5507 private Bitmap mBackground;
5508 private int mContentIcon;
5509 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5510 private int mContentActionIndex = UNSET_ACTION_INDEX;
5511 private int mCustomSizePreset = SIZE_DEFAULT;
5512 private int mCustomContentHeight;
5513 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005514 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005515 private String mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005516
5517 /**
5518 * Create a {@link android.app.Notification.WearableExtender} with default
5519 * options.
5520 */
5521 public WearableExtender() {
5522 }
5523
5524 public WearableExtender(Notification notif) {
5525 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5526 if (wearableBundle != null) {
5527 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5528 if (actions != null) {
5529 mActions.addAll(actions);
5530 }
5531
5532 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5533 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5534
5535 Notification[] pages = getNotificationArrayFromBundle(
5536 wearableBundle, KEY_PAGES);
5537 if (pages != null) {
5538 Collections.addAll(mPages, pages);
5539 }
5540
5541 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5542 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5543 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5544 DEFAULT_CONTENT_ICON_GRAVITY);
5545 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5546 UNSET_ACTION_INDEX);
5547 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5548 SIZE_DEFAULT);
5549 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5550 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005551 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005552 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Griff Hazen61a9e862014-05-22 16:05:19 -07005553 }
5554 }
5555
5556 /**
5557 * Apply wearable extensions to a notification that is being built. This is typically
5558 * called by the {@link android.app.Notification.Builder#extend} method of
5559 * {@link android.app.Notification.Builder}.
5560 */
5561 @Override
5562 public Notification.Builder extend(Notification.Builder builder) {
5563 Bundle wearableBundle = new Bundle();
5564
5565 if (!mActions.isEmpty()) {
5566 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5567 }
5568 if (mFlags != DEFAULT_FLAGS) {
5569 wearableBundle.putInt(KEY_FLAGS, mFlags);
5570 }
5571 if (mDisplayIntent != null) {
5572 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5573 }
5574 if (!mPages.isEmpty()) {
5575 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5576 new Notification[mPages.size()]));
5577 }
5578 if (mBackground != null) {
5579 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5580 }
5581 if (mContentIcon != 0) {
5582 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5583 }
5584 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5585 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5586 }
5587 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5588 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5589 mContentActionIndex);
5590 }
5591 if (mCustomSizePreset != SIZE_DEFAULT) {
5592 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5593 }
5594 if (mCustomContentHeight != 0) {
5595 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5596 }
5597 if (mGravity != DEFAULT_GRAVITY) {
5598 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5599 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005600 if (mHintScreenTimeout != 0) {
5601 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5602 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005603 if (mDismissalId != null) {
5604 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5605 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005606
5607 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5608 return builder;
5609 }
5610
5611 @Override
5612 public WearableExtender clone() {
5613 WearableExtender that = new WearableExtender();
5614 that.mActions = new ArrayList<Action>(this.mActions);
5615 that.mFlags = this.mFlags;
5616 that.mDisplayIntent = this.mDisplayIntent;
5617 that.mPages = new ArrayList<Notification>(this.mPages);
5618 that.mBackground = this.mBackground;
5619 that.mContentIcon = this.mContentIcon;
5620 that.mContentIconGravity = this.mContentIconGravity;
5621 that.mContentActionIndex = this.mContentActionIndex;
5622 that.mCustomSizePreset = this.mCustomSizePreset;
5623 that.mCustomContentHeight = this.mCustomContentHeight;
5624 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005625 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005626 that.mDismissalId = this.mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005627 return that;
5628 }
5629
5630 /**
5631 * Add a wearable action to this notification.
5632 *
5633 * <p>When wearable actions are added using this method, the set of actions that
5634 * show on a wearable device splits from devices that only show actions added
5635 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5636 * of which actions display on different devices.
5637 *
5638 * @param action the action to add to this notification
5639 * @return this object for method chaining
5640 * @see android.app.Notification.Action
5641 */
5642 public WearableExtender addAction(Action action) {
5643 mActions.add(action);
5644 return this;
5645 }
5646
5647 /**
5648 * Adds wearable actions to this notification.
5649 *
5650 * <p>When wearable actions are added using this method, the set of actions that
5651 * show on a wearable device splits from devices that only show actions added
5652 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5653 * of which actions display on different devices.
5654 *
5655 * @param actions the actions to add to this notification
5656 * @return this object for method chaining
5657 * @see android.app.Notification.Action
5658 */
5659 public WearableExtender addActions(List<Action> actions) {
5660 mActions.addAll(actions);
5661 return this;
5662 }
5663
5664 /**
5665 * Clear all wearable actions present on this builder.
5666 * @return this object for method chaining.
5667 * @see #addAction
5668 */
5669 public WearableExtender clearActions() {
5670 mActions.clear();
5671 return this;
5672 }
5673
5674 /**
5675 * Get the wearable actions present on this notification.
5676 */
5677 public List<Action> getActions() {
5678 return mActions;
5679 }
5680
5681 /**
5682 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005683 * this notification. The {@link PendingIntent} provided should be for an activity.
5684 *
5685 * <pre class="prettyprint">
5686 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5687 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5688 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5689 * Notification notif = new Notification.Builder(context)
5690 * .extend(new Notification.WearableExtender()
5691 * .setDisplayIntent(displayPendingIntent)
5692 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5693 * .build();</pre>
5694 *
5695 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005696 * should have an empty task affinity. It is also recommended to use the device
5697 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005698 *
5699 * <p>Example AndroidManifest.xml entry:
5700 * <pre class="prettyprint">
5701 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5702 * android:exported=&quot;true&quot;
5703 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005704 * android:taskAffinity=&quot;&quot;
5705 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005706 *
5707 * @param intent the {@link PendingIntent} for an activity
5708 * @return this object for method chaining
5709 * @see android.app.Notification.WearableExtender#getDisplayIntent
5710 */
5711 public WearableExtender setDisplayIntent(PendingIntent intent) {
5712 mDisplayIntent = intent;
5713 return this;
5714 }
5715
5716 /**
5717 * Get the intent to launch inside of an activity view when displaying this
5718 * notification. This {@code PendingIntent} should be for an activity.
5719 */
5720 public PendingIntent getDisplayIntent() {
5721 return mDisplayIntent;
5722 }
5723
5724 /**
5725 * Add an additional page of content to display with this notification. The current
5726 * notification forms the first page, and pages added using this function form
5727 * subsequent pages. This field can be used to separate a notification into multiple
5728 * sections.
5729 *
5730 * @param page the notification to add as another page
5731 * @return this object for method chaining
5732 * @see android.app.Notification.WearableExtender#getPages
5733 */
5734 public WearableExtender addPage(Notification page) {
5735 mPages.add(page);
5736 return this;
5737 }
5738
5739 /**
5740 * Add additional pages of content to display with this notification. The current
5741 * notification forms the first page, and pages added using this function form
5742 * subsequent pages. This field can be used to separate a notification into multiple
5743 * sections.
5744 *
5745 * @param pages a list of notifications
5746 * @return this object for method chaining
5747 * @see android.app.Notification.WearableExtender#getPages
5748 */
5749 public WearableExtender addPages(List<Notification> pages) {
5750 mPages.addAll(pages);
5751 return this;
5752 }
5753
5754 /**
5755 * Clear all additional pages present on this builder.
5756 * @return this object for method chaining.
5757 * @see #addPage
5758 */
5759 public WearableExtender clearPages() {
5760 mPages.clear();
5761 return this;
5762 }
5763
5764 /**
5765 * Get the array of additional pages of content for displaying this notification. The
5766 * current notification forms the first page, and elements within this array form
5767 * subsequent pages. This field can be used to separate a notification into multiple
5768 * sections.
5769 * @return the pages for this notification
5770 */
5771 public List<Notification> getPages() {
5772 return mPages;
5773 }
5774
5775 /**
5776 * Set a background image to be displayed behind the notification content.
5777 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5778 * will work with any notification style.
5779 *
5780 * @param background the background bitmap
5781 * @return this object for method chaining
5782 * @see android.app.Notification.WearableExtender#getBackground
5783 */
5784 public WearableExtender setBackground(Bitmap background) {
5785 mBackground = background;
5786 return this;
5787 }
5788
5789 /**
5790 * Get a background image to be displayed behind the notification content.
5791 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5792 * will work with any notification style.
5793 *
5794 * @return the background image
5795 * @see android.app.Notification.WearableExtender#setBackground
5796 */
5797 public Bitmap getBackground() {
5798 return mBackground;
5799 }
5800
5801 /**
5802 * Set an icon that goes with the content of this notification.
5803 */
5804 public WearableExtender setContentIcon(int icon) {
5805 mContentIcon = icon;
5806 return this;
5807 }
5808
5809 /**
5810 * Get an icon that goes with the content of this notification.
5811 */
5812 public int getContentIcon() {
5813 return mContentIcon;
5814 }
5815
5816 /**
5817 * Set the gravity that the content icon should have within the notification display.
5818 * Supported values include {@link android.view.Gravity#START} and
5819 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5820 * @see #setContentIcon
5821 */
5822 public WearableExtender setContentIconGravity(int contentIconGravity) {
5823 mContentIconGravity = contentIconGravity;
5824 return this;
5825 }
5826
5827 /**
5828 * Get the gravity that the content icon should have within the notification display.
5829 * Supported values include {@link android.view.Gravity#START} and
5830 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5831 * @see #getContentIcon
5832 */
5833 public int getContentIconGravity() {
5834 return mContentIconGravity;
5835 }
5836
5837 /**
5838 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005839 * this notification. This action will no longer display separately from the
5840 * notification's content.
5841 *
Griff Hazenca48d352014-05-28 22:37:13 -07005842 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005843 * set, although the list of available actions comes from the main notification and not
5844 * from the child page's notification.
5845 *
5846 * @param actionIndex The index of the action to hoist onto the current notification page.
5847 * If wearable actions were added to the main notification, this index
5848 * will apply to that list, otherwise it will apply to the regular
5849 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005850 */
5851 public WearableExtender setContentAction(int actionIndex) {
5852 mContentActionIndex = actionIndex;
5853 return this;
5854 }
5855
5856 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005857 * Get the index of the notification action, if any, that was specified as being clickable
5858 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005859 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005860 *
Griff Hazenca48d352014-05-28 22:37:13 -07005861 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005862 * set, although the list of available actions comes from the main notification and not
5863 * from the child page's notification.
5864 *
5865 * <p>If wearable specific actions were added to the main notification, this index will
5866 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005867 *
5868 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005869 */
5870 public int getContentAction() {
5871 return mContentActionIndex;
5872 }
5873
5874 /**
5875 * Set the gravity that this notification should have within the available viewport space.
5876 * Supported values include {@link android.view.Gravity#TOP},
5877 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5878 * The default value is {@link android.view.Gravity#BOTTOM}.
5879 */
5880 public WearableExtender setGravity(int gravity) {
5881 mGravity = gravity;
5882 return this;
5883 }
5884
5885 /**
5886 * Get the gravity that this notification should have within the available viewport space.
5887 * Supported values include {@link android.view.Gravity#TOP},
5888 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5889 * The default value is {@link android.view.Gravity#BOTTOM}.
5890 */
5891 public int getGravity() {
5892 return mGravity;
5893 }
5894
5895 /**
5896 * Set the custom size preset for the display of this notification out of the available
5897 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5898 * {@link #SIZE_LARGE}.
5899 * <p>Some custom size presets are only applicable for custom display notifications created
5900 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5901 * documentation for the preset in question. See also
5902 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5903 */
5904 public WearableExtender setCustomSizePreset(int sizePreset) {
5905 mCustomSizePreset = sizePreset;
5906 return this;
5907 }
5908
5909 /**
5910 * Get the custom size preset for the display of this notification out of the available
5911 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5912 * {@link #SIZE_LARGE}.
5913 * <p>Some custom size presets are only applicable for custom display notifications created
5914 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5915 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5916 */
5917 public int getCustomSizePreset() {
5918 return mCustomSizePreset;
5919 }
5920
5921 /**
5922 * Set the custom height in pixels for the display of this notification's content.
5923 * <p>This option is only available for custom display notifications created
5924 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5925 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5926 * {@link #getCustomContentHeight}.
5927 */
5928 public WearableExtender setCustomContentHeight(int height) {
5929 mCustomContentHeight = height;
5930 return this;
5931 }
5932
5933 /**
5934 * Get the custom height in pixels for the display of this notification's content.
5935 * <p>This option is only available for custom display notifications created
5936 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5937 * {@link #setCustomContentHeight}.
5938 */
5939 public int getCustomContentHeight() {
5940 return mCustomContentHeight;
5941 }
5942
5943 /**
5944 * Set whether the scrolling position for the contents of this notification should start
5945 * at the bottom of the contents instead of the top when the contents are too long to
5946 * display within the screen. Default is false (start scroll at the top).
5947 */
5948 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5949 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5950 return this;
5951 }
5952
5953 /**
5954 * Get whether the scrolling position for the contents of this notification should start
5955 * at the bottom of the contents instead of the top when the contents are too long to
5956 * display within the screen. Default is false (start scroll at the top).
5957 */
5958 public boolean getStartScrollBottom() {
5959 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5960 }
5961
5962 /**
5963 * Set whether the content intent is available when the wearable device is not connected
5964 * to a companion device. The user can still trigger this intent when the wearable device
5965 * is offline, but a visual hint will indicate that the content intent may not be available.
5966 * Defaults to true.
5967 */
5968 public WearableExtender setContentIntentAvailableOffline(
5969 boolean contentIntentAvailableOffline) {
5970 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5971 return this;
5972 }
5973
5974 /**
5975 * Get whether the content intent is available when the wearable device is not connected
5976 * to a companion device. The user can still trigger this intent when the wearable device
5977 * is offline, but a visual hint will indicate that the content intent may not be available.
5978 * Defaults to true.
5979 */
5980 public boolean getContentIntentAvailableOffline() {
5981 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5982 }
5983
5984 /**
5985 * Set a hint that this notification's icon should not be displayed.
5986 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5987 * @return this object for method chaining
5988 */
5989 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5990 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5991 return this;
5992 }
5993
5994 /**
5995 * Get a hint that this notification's icon should not be displayed.
5996 * @return {@code true} if this icon should not be displayed, false otherwise.
5997 * The default value is {@code false} if this was never set.
5998 */
5999 public boolean getHintHideIcon() {
6000 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6001 }
6002
6003 /**
6004 * Set a visual hint that only the background image of this notification should be
6005 * displayed, and other semantic content should be hidden. This hint is only applicable
6006 * to sub-pages added using {@link #addPage}.
6007 */
6008 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6009 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6010 return this;
6011 }
6012
6013 /**
6014 * Get a visual hint that only the background image of this notification should be
6015 * displayed, and other semantic content should be hidden. This hint is only applicable
6016 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6017 */
6018 public boolean getHintShowBackgroundOnly() {
6019 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6020 }
6021
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006022 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006023 * Set a hint that this notification's background should not be clipped if possible,
6024 * and should instead be resized to fully display on the screen, retaining the aspect
6025 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006026 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6027 * @return this object for method chaining
6028 */
6029 public WearableExtender setHintAvoidBackgroundClipping(
6030 boolean hintAvoidBackgroundClipping) {
6031 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6032 return this;
6033 }
6034
6035 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006036 * Get a hint that this notification's background should not be clipped if possible,
6037 * and should instead be resized to fully display on the screen, retaining the aspect
6038 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006039 * @return {@code true} if it's ok if the background is clipped on the screen, false
6040 * otherwise. The default value is {@code false} if this was never set.
6041 */
6042 public boolean getHintAvoidBackgroundClipping() {
6043 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6044 }
6045
6046 /**
6047 * Set a hint that the screen should remain on for at least this duration when
6048 * this notification is displayed on the screen.
6049 * @param timeout The requested screen timeout in milliseconds. Can also be either
6050 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6051 * @return this object for method chaining
6052 */
6053 public WearableExtender setHintScreenTimeout(int timeout) {
6054 mHintScreenTimeout = timeout;
6055 return this;
6056 }
6057
6058 /**
6059 * Get the duration, in milliseconds, that the screen should remain on for
6060 * when this notification is displayed.
6061 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6062 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6063 */
6064 public int getHintScreenTimeout() {
6065 return mHintScreenTimeout;
6066 }
6067
Alex Hills9ab3a232016-04-05 14:54:56 -04006068 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006069 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6070 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6071 * qr codes, as well as other simple black-and-white tickets.
6072 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6073 * @return this object for method chaining
6074 */
6075 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6076 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6077 return this;
6078 }
6079
6080 /**
6081 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6082 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6083 * qr codes, as well as other simple black-and-white tickets.
6084 * @return {@code true} if it should be displayed in ambient, false otherwise
6085 * otherwise. The default value is {@code false} if this was never set.
6086 */
6087 public boolean getHintAmbientBigPicture() {
6088 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6089 }
6090
6091 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006092 * Set a hint that this notification's content intent will launch an {@link Activity}
6093 * directly, telling the platform that it can generate the appropriate transitions.
6094 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6095 * an activity and transitions should be generated, false otherwise.
6096 * @return this object for method chaining
6097 */
6098 public WearableExtender setHintContentIntentLaunchesActivity(
6099 boolean hintContentIntentLaunchesActivity) {
6100 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6101 return this;
6102 }
6103
6104 /**
6105 * Get a hint that this notification's content intent will launch an {@link Activity}
6106 * directly, telling the platform that it can generate the appropriate transitions
6107 * @return {@code true} if the content intent will launch an activity and transitions should
6108 * be generated, false otherwise. The default value is {@code false} if this was never set.
6109 */
6110 public boolean getHintContentIntentLaunchesActivity() {
6111 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6112 }
6113
Nadia Benbernou948627e2016-04-14 14:41:08 -04006114 /**
6115 * When you post a notification, if you set the dismissal id field, then when that
6116 * notification is canceled, notifications on other wearables and the paired Android phone
6117 * having that same dismissal id will also be canceled. Note that this only works if you
6118 * have notification bridge mode set to NO_BRIDGING in your Wear app manifest. See
6119 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
6120 * Notifications</a> for more information on how to use the bridge mode feature.
6121 * @param dismissalId the dismissal id of the notification.
6122 * @return this object for method chaining
6123 */
6124 public WearableExtender setDismissalId(String dismissalId) {
6125 mDismissalId = dismissalId;
6126 return this;
6127 }
6128
6129 /**
6130 * Returns the dismissal id of the notification.
6131 * @return the dismissal id of the notification or null if it has not been set.
6132 */
6133 public String getDismissalId() {
6134 return mDismissalId;
6135 }
6136
Griff Hazen61a9e862014-05-22 16:05:19 -07006137 private void setFlag(int mask, boolean value) {
6138 if (value) {
6139 mFlags |= mask;
6140 } else {
6141 mFlags &= ~mask;
6142 }
6143 }
6144 }
6145
6146 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006147 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6148 * with car extensions:
6149 *
6150 * <ol>
6151 * <li>Create an {@link Notification.Builder}, setting any desired
6152 * properties.
6153 * <li>Create a {@link CarExtender}.
6154 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6155 * {@link CarExtender}.
6156 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6157 * to apply the extensions to a notification.
6158 * </ol>
6159 *
6160 * <pre class="prettyprint">
6161 * Notification notification = new Notification.Builder(context)
6162 * ...
6163 * .extend(new CarExtender()
6164 * .set*(...))
6165 * .build();
6166 * </pre>
6167 *
6168 * <p>Car extensions can be accessed on an existing notification by using the
6169 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6170 * to access values.
6171 */
6172 public static final class CarExtender implements Extender {
6173 private static final String TAG = "CarExtender";
6174
6175 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6176 private static final String EXTRA_LARGE_ICON = "large_icon";
6177 private static final String EXTRA_CONVERSATION = "car_conversation";
6178 private static final String EXTRA_COLOR = "app_color";
6179
6180 private Bitmap mLargeIcon;
6181 private UnreadConversation mUnreadConversation;
6182 private int mColor = Notification.COLOR_DEFAULT;
6183
6184 /**
6185 * Create a {@link CarExtender} with default options.
6186 */
6187 public CarExtender() {
6188 }
6189
6190 /**
6191 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6192 *
6193 * @param notif The notification from which to copy options.
6194 */
6195 public CarExtender(Notification notif) {
6196 Bundle carBundle = notif.extras == null ?
6197 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6198 if (carBundle != null) {
6199 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6200 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6201
6202 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6203 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6204 }
6205 }
6206
6207 /**
6208 * Apply car extensions to a notification that is being built. This is typically called by
6209 * the {@link Notification.Builder#extend(Notification.Extender)}
6210 * method of {@link Notification.Builder}.
6211 */
6212 @Override
6213 public Notification.Builder extend(Notification.Builder builder) {
6214 Bundle carExtensions = new Bundle();
6215
6216 if (mLargeIcon != null) {
6217 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6218 }
6219 if (mColor != Notification.COLOR_DEFAULT) {
6220 carExtensions.putInt(EXTRA_COLOR, mColor);
6221 }
6222
6223 if (mUnreadConversation != null) {
6224 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6225 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6226 }
6227
6228 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6229 return builder;
6230 }
6231
6232 /**
6233 * Sets the accent color to use when Android Auto presents the notification.
6234 *
6235 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6236 * to accent the displayed notification. However, not all colors are acceptable in an
6237 * automotive setting. This method can be used to override the color provided in the
6238 * notification in such a situation.
6239 */
Tor Norbye80756e32015-03-02 09:39:27 -08006240 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006241 mColor = color;
6242 return this;
6243 }
6244
6245 /**
6246 * Gets the accent color.
6247 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006248 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006249 */
Tor Norbye80756e32015-03-02 09:39:27 -08006250 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006251 public int getColor() {
6252 return mColor;
6253 }
6254
6255 /**
6256 * Sets the large icon of the car notification.
6257 *
6258 * If no large icon is set in the extender, Android Auto will display the icon
6259 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6260 *
6261 * @param largeIcon The large icon to use in the car notification.
6262 * @return This object for method chaining.
6263 */
6264 public CarExtender setLargeIcon(Bitmap largeIcon) {
6265 mLargeIcon = largeIcon;
6266 return this;
6267 }
6268
6269 /**
6270 * Gets the large icon used in this car notification, or null if no icon has been set.
6271 *
6272 * @return The large icon for the car notification.
6273 * @see CarExtender#setLargeIcon
6274 */
6275 public Bitmap getLargeIcon() {
6276 return mLargeIcon;
6277 }
6278
6279 /**
6280 * Sets the unread conversation in a message notification.
6281 *
6282 * @param unreadConversation The unread part of the conversation this notification conveys.
6283 * @return This object for method chaining.
6284 */
6285 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6286 mUnreadConversation = unreadConversation;
6287 return this;
6288 }
6289
6290 /**
6291 * Returns the unread conversation conveyed by this notification.
6292 * @see #setUnreadConversation(UnreadConversation)
6293 */
6294 public UnreadConversation getUnreadConversation() {
6295 return mUnreadConversation;
6296 }
6297
6298 /**
6299 * A class which holds the unread messages from a conversation.
6300 */
6301 public static class UnreadConversation {
6302 private static final String KEY_AUTHOR = "author";
6303 private static final String KEY_TEXT = "text";
6304 private static final String KEY_MESSAGES = "messages";
6305 private static final String KEY_REMOTE_INPUT = "remote_input";
6306 private static final String KEY_ON_REPLY = "on_reply";
6307 private static final String KEY_ON_READ = "on_read";
6308 private static final String KEY_PARTICIPANTS = "participants";
6309 private static final String KEY_TIMESTAMP = "timestamp";
6310
6311 private final String[] mMessages;
6312 private final RemoteInput mRemoteInput;
6313 private final PendingIntent mReplyPendingIntent;
6314 private final PendingIntent mReadPendingIntent;
6315 private final String[] mParticipants;
6316 private final long mLatestTimestamp;
6317
6318 UnreadConversation(String[] messages, RemoteInput remoteInput,
6319 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6320 String[] participants, long latestTimestamp) {
6321 mMessages = messages;
6322 mRemoteInput = remoteInput;
6323 mReadPendingIntent = readPendingIntent;
6324 mReplyPendingIntent = replyPendingIntent;
6325 mParticipants = participants;
6326 mLatestTimestamp = latestTimestamp;
6327 }
6328
6329 /**
6330 * Gets the list of messages conveyed by this notification.
6331 */
6332 public String[] getMessages() {
6333 return mMessages;
6334 }
6335
6336 /**
6337 * Gets the remote input that will be used to convey the response to a message list, or
6338 * null if no such remote input exists.
6339 */
6340 public RemoteInput getRemoteInput() {
6341 return mRemoteInput;
6342 }
6343
6344 /**
6345 * Gets the pending intent that will be triggered when the user replies to this
6346 * notification.
6347 */
6348 public PendingIntent getReplyPendingIntent() {
6349 return mReplyPendingIntent;
6350 }
6351
6352 /**
6353 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6354 * in this object's message list.
6355 */
6356 public PendingIntent getReadPendingIntent() {
6357 return mReadPendingIntent;
6358 }
6359
6360 /**
6361 * Gets the participants in the conversation.
6362 */
6363 public String[] getParticipants() {
6364 return mParticipants;
6365 }
6366
6367 /**
6368 * Gets the firs participant in the conversation.
6369 */
6370 public String getParticipant() {
6371 return mParticipants.length > 0 ? mParticipants[0] : null;
6372 }
6373
6374 /**
6375 * Gets the timestamp of the conversation.
6376 */
6377 public long getLatestTimestamp() {
6378 return mLatestTimestamp;
6379 }
6380
6381 Bundle getBundleForUnreadConversation() {
6382 Bundle b = new Bundle();
6383 String author = null;
6384 if (mParticipants != null && mParticipants.length > 1) {
6385 author = mParticipants[0];
6386 }
6387 Parcelable[] messages = new Parcelable[mMessages.length];
6388 for (int i = 0; i < messages.length; i++) {
6389 Bundle m = new Bundle();
6390 m.putString(KEY_TEXT, mMessages[i]);
6391 m.putString(KEY_AUTHOR, author);
6392 messages[i] = m;
6393 }
6394 b.putParcelableArray(KEY_MESSAGES, messages);
6395 if (mRemoteInput != null) {
6396 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6397 }
6398 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6399 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6400 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6401 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6402 return b;
6403 }
6404
6405 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6406 if (b == null) {
6407 return null;
6408 }
6409 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6410 String[] messages = null;
6411 if (parcelableMessages != null) {
6412 String[] tmp = new String[parcelableMessages.length];
6413 boolean success = true;
6414 for (int i = 0; i < tmp.length; i++) {
6415 if (!(parcelableMessages[i] instanceof Bundle)) {
6416 success = false;
6417 break;
6418 }
6419 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6420 if (tmp[i] == null) {
6421 success = false;
6422 break;
6423 }
6424 }
6425 if (success) {
6426 messages = tmp;
6427 } else {
6428 return null;
6429 }
6430 }
6431
6432 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6433 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6434
6435 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6436
6437 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6438 if (participants == null || participants.length != 1) {
6439 return null;
6440 }
6441
6442 return new UnreadConversation(messages,
6443 remoteInput,
6444 onReply,
6445 onRead,
6446 participants, b.getLong(KEY_TIMESTAMP));
6447 }
6448 };
6449
6450 /**
6451 * Builder class for {@link CarExtender.UnreadConversation} objects.
6452 */
6453 public static class Builder {
6454 private final List<String> mMessages = new ArrayList<String>();
6455 private final String mParticipant;
6456 private RemoteInput mRemoteInput;
6457 private PendingIntent mReadPendingIntent;
6458 private PendingIntent mReplyPendingIntent;
6459 private long mLatestTimestamp;
6460
6461 /**
6462 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6463 *
6464 * @param name The name of the other participant in the conversation.
6465 */
6466 public Builder(String name) {
6467 mParticipant = name;
6468 }
6469
6470 /**
6471 * Appends a new unread message to the list of messages for this conversation.
6472 *
6473 * The messages should be added from oldest to newest.
6474 *
6475 * @param message The text of the new unread message.
6476 * @return This object for method chaining.
6477 */
6478 public Builder addMessage(String message) {
6479 mMessages.add(message);
6480 return this;
6481 }
6482
6483 /**
6484 * Sets the pending intent and remote input which will convey the reply to this
6485 * notification.
6486 *
6487 * @param pendingIntent The pending intent which will be triggered on a reply.
6488 * @param remoteInput The remote input parcelable which will carry the reply.
6489 * @return This object for method chaining.
6490 *
6491 * @see CarExtender.UnreadConversation#getRemoteInput
6492 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6493 */
6494 public Builder setReplyAction(
6495 PendingIntent pendingIntent, RemoteInput remoteInput) {
6496 mRemoteInput = remoteInput;
6497 mReplyPendingIntent = pendingIntent;
6498
6499 return this;
6500 }
6501
6502 /**
6503 * Sets the pending intent that will be sent once the messages in this notification
6504 * are read.
6505 *
6506 * @param pendingIntent The pending intent to use.
6507 * @return This object for method chaining.
6508 */
6509 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6510 mReadPendingIntent = pendingIntent;
6511 return this;
6512 }
6513
6514 /**
6515 * Sets the timestamp of the most recent message in an unread conversation.
6516 *
6517 * If a messaging notification has been posted by your application and has not
6518 * yet been cancelled, posting a later notification with the same id and tag
6519 * but without a newer timestamp may result in Android Auto not displaying a
6520 * heads up notification for the later notification.
6521 *
6522 * @param timestamp The timestamp of the most recent message in the conversation.
6523 * @return This object for method chaining.
6524 */
6525 public Builder setLatestTimestamp(long timestamp) {
6526 mLatestTimestamp = timestamp;
6527 return this;
6528 }
6529
6530 /**
6531 * Builds a new unread conversation object.
6532 *
6533 * @return The new unread conversation object.
6534 */
6535 public UnreadConversation build() {
6536 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6537 String[] participants = { mParticipant };
6538 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6539 mReadPendingIntent, participants, mLatestTimestamp);
6540 }
6541 }
6542 }
6543
6544 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006545 * Get an array of Notification objects from a parcelable array bundle field.
6546 * Update the bundle to have a typed array so fetches in the future don't need
6547 * to do an array copy.
6548 */
6549 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6550 Parcelable[] array = bundle.getParcelableArray(key);
6551 if (array instanceof Notification[] || array == null) {
6552 return (Notification[]) array;
6553 }
6554 Notification[] typedArray = Arrays.copyOf(array, array.length,
6555 Notification[].class);
6556 bundle.putParcelableArray(key, typedArray);
6557 return typedArray;
6558 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006559
6560 private static class BuilderRemoteViews extends RemoteViews {
6561 public BuilderRemoteViews(Parcel parcel) {
6562 super(parcel);
6563 }
6564
Kenny Guy77320062014-08-27 21:37:15 +01006565 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6566 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006567 }
6568
6569 @Override
6570 public BuilderRemoteViews clone() {
6571 Parcel p = Parcel.obtain();
6572 writeToParcel(p, 0);
6573 p.setDataPosition(0);
6574 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6575 p.recycle();
6576 return brv;
6577 }
6578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579}