blob: 4108e3a0bbba1c4e9c000000c25efd23c00a5f42 [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
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004078 /**
4079 * @deprecated use {@code BigPictureStyle()}.
4080 */
4081 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004082 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004083 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004084 }
4085
Chris Wrend6297db2012-05-03 16:20:13 -04004086 /**
4087 * Overrides ContentTitle in the big form of the template.
4088 * This defaults to the value passed to setContentTitle().
4089 */
4090 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004091 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004092 return this;
4093 }
4094
4095 /**
4096 * Set the first line of text after the detail section in the big form of the template.
4097 */
4098 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004099 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004100 return this;
4101 }
4102
Chris Wren0bd664d2012-08-01 13:56:56 -04004103 /**
4104 * Provide the bitmap to be used as the payload for the BigPicture notification.
4105 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004106 public BigPictureStyle bigPicture(Bitmap b) {
4107 mPicture = b;
4108 return this;
4109 }
4110
Chris Wren3745a3d2012-05-22 15:11:52 -04004111 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004112 * Override the large icon when the big notification is shown.
4113 */
4114 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004115 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4116 }
4117
4118 /**
4119 * Override the large icon when the big notification is shown.
4120 */
4121 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004122 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004123 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004124 return this;
4125 }
4126
Riley Andrews0394a0c2015-11-03 23:36:52 -08004127 /** @hide */
4128 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4129
Daniel Sandler0ec46202015-06-24 01:27:05 -04004130 /**
4131 * @hide
4132 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004133 @Override
4134 public void purgeResources() {
4135 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004136 if (mPicture != null &&
4137 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004138 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004139 mPicture = mPicture.createAshmemBitmap();
4140 }
4141 if (mBigLargeIcon != null) {
4142 mBigLargeIcon.convertToAshmem();
4143 }
4144 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004145
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004146 /**
4147 * @hide
4148 */
4149 public RemoteViews makeBigContentView() {
4150 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004151 // This covers the following cases:
4152 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004153 // mN.mLargeIcon
4154 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004155 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004156 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004157 oldLargeIcon = mBuilder.mN.mLargeIcon;
4158 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004159 }
4160
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004161 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004162 if (mSummaryTextSet) {
4163 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004164 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004165 }
Selim Cinek860b6da2015-12-16 19:02:19 -08004166 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08004167
Christoph Studer5c510ee2014-12-15 16:32:27 +01004168 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004169 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004170 }
4171
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004172 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004173 return contentView;
4174 }
4175
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004176 /**
4177 * @hide
4178 */
4179 public void addExtras(Bundle extras) {
4180 super.addExtras(extras);
4181
4182 if (mBigLargeIconSet) {
4183 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4184 }
4185 extras.putParcelable(EXTRA_PICTURE, mPicture);
4186 }
4187
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004188 /**
4189 * @hide
4190 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004191 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004192 protected void restoreFromExtras(Bundle extras) {
4193 super.restoreFromExtras(extras);
4194
4195 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004196 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004197 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004198 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004199 mPicture = extras.getParcelable(EXTRA_PICTURE);
4200 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004201
4202 /**
4203 * @hide
4204 */
4205 @Override
4206 public boolean hasSummaryInHeader() {
4207 return false;
4208 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004209 }
4210
4211 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004212 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004213 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004214 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004215 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004216 * Notification notif = new Notification.Builder(mContext)
4217 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4218 * .setContentText(subject)
4219 * .setSmallIcon(R.drawable.new_mail)
4220 * .setLargeIcon(aBitmap)
4221 * .setStyle(new Notification.BigTextStyle()
4222 * .bigText(aVeryLongString))
4223 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004224 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004225 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004226 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004227 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004228 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004229
4230 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004231 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004232
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004233 private CharSequence mBigText;
4234
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004235 public BigTextStyle() {
4236 }
4237
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004238 /**
4239 * @deprecated use {@code BigTextStyle()}.
4240 */
4241 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004242 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004243 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004244 }
4245
Chris Wrend6297db2012-05-03 16:20:13 -04004246 /**
4247 * Overrides ContentTitle in the big form of the template.
4248 * This defaults to the value passed to setContentTitle().
4249 */
4250 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004251 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004252 return this;
4253 }
4254
4255 /**
4256 * Set the first line of text after the detail section in the big form of the template.
4257 */
4258 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004259 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004260 return this;
4261 }
4262
Chris Wren0bd664d2012-08-01 13:56:56 -04004263 /**
4264 * Provide the longer text to be displayed in the big form of the
4265 * template in place of the content text.
4266 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004267 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004268 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004269 return this;
4270 }
4271
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004272 /**
4273 * @hide
4274 */
4275 public void addExtras(Bundle extras) {
4276 super.addExtras(extras);
4277
Christoph Studer4600f9b2014-07-22 22:44:43 +02004278 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4279 }
4280
4281 /**
4282 * @hide
4283 */
4284 @Override
4285 protected void restoreFromExtras(Bundle extras) {
4286 super.restoreFromExtras(extras);
4287
4288 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004289 }
4290
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004291 /**
4292 * @hide
4293 */
4294 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004295
4296 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004297 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004298 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004299
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004300 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004301
Selim Cinek75998782016-04-26 10:39:17 -07004302 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004303
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004304 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004305 if (TextUtils.isEmpty(bigTextText)) {
4306 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4307 // experience
4308 bigTextText = mBuilder.processLegacyText(text);
4309 }
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004310 contentView.setTextViewText(R.id.big_text, bigTextText);
4311 contentView.setViewVisibility(R.id.big_text,
4312 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004313 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004314 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4315
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004316 return contentView;
4317 }
4318
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004319 private int calculateMaxLines() {
4320 int lineCount = MAX_LINES;
4321 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004322 if (hasActions) {
4323 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4324 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004325 return lineCount;
4326 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004327 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004328
4329 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004330 * Helper class for generating large-format notifications that include multiple back-and-forth
4331 * messages of varying types between any number of people.
4332 *
4333 * <br>
4334 * If the platform does not provide large-format notifications, this method has no effect. The
4335 * user will always see the normal notification view.
4336 * <br>
4337 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4338 * so:
4339 * <pre class="prettyprint">
4340 *
4341 * Notification noti = new Notification.Builder()
4342 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4343 * .setContentText(subject)
4344 * .setSmallIcon(R.drawable.new_message)
4345 * .setLargeIcon(aBitmap)
4346 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4347 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4348 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4349 * .build();
4350 * </pre>
4351 */
4352 public static class MessagingStyle extends Style {
4353
4354 /**
4355 * The maximum number of messages that will be retained in the Notification itself (the
4356 * number displayed is up to the platform).
4357 */
4358 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4359
4360 CharSequence mUserDisplayName;
4361 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004362 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004363
4364 MessagingStyle() {
4365 }
4366
4367 /**
4368 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4369 * posting app reposts the notification with those messages after they've been actually
4370 * sent and in previous messages sent by the user added in
4371 * {@link #addMessage(Notification.MessagingStyle.Message)}
4372 */
4373 public MessagingStyle(CharSequence userDisplayName) {
4374 mUserDisplayName = userDisplayName;
4375 }
4376
4377 /**
4378 * Returns the name to be displayed for any replies sent by the user
4379 */
4380 public CharSequence getUserDisplayName() {
4381 return mUserDisplayName;
4382 }
4383
4384 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004385 * Sets the title to be displayed on this conversation. This should only be used for
4386 * group messaging and left unset for one-on-one conversations.
4387 * @param conversationTitle
4388 * @return this object for method chaining.
4389 */
4390 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4391 mConversationTitle = conversationTitle;
4392 return this;
4393 }
4394
4395 /**
4396 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4397 * should be for one-on-one conversations
4398 */
4399 public CharSequence getConversationTitle() {
4400 return mConversationTitle;
4401 }
4402
4403 /**
4404 * Adds a message for display by this notification. Convenience call for a simple
4405 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4406 * @param text A {@link CharSequence} to be displayed as the message content
4407 * @param timestamp Time at which the message arrived
4408 * @param sender A {@link CharSequence} to be used for displaying the name of the
4409 * sender. Should be <code>null</code> for messages by the current user, in which case
4410 * the platform will insert {@link #getUserDisplayName()}.
4411 * Should be unique amongst all individuals in the conversation, and should be
4412 * consistent during re-posts of the notification.
4413 *
4414 * @see Message#Message(CharSequence, long, CharSequence)
4415 *
4416 * @return this object for method chaining
4417 */
4418 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4419 mMessages.add(new Message(text, timestamp, sender));
4420 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4421 mMessages.remove(0);
4422 }
4423 return this;
4424 }
4425
4426 /**
4427 * Adds a {@link Message} for display in this notification.
4428 * @param message The {@link Message} to be displayed
4429 * @return this object for method chaining
4430 */
4431 public MessagingStyle addMessage(Message message) {
4432 mMessages.add(message);
4433 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4434 mMessages.remove(0);
4435 }
4436 return this;
4437 }
4438
4439 /**
4440 * Gets the list of {@code Message} objects that represent the notification
4441 */
4442 public List<Message> getMessages() {
4443 return mMessages;
4444 }
4445
4446 /**
4447 * @hide
4448 */
4449 @Override
4450 public void addExtras(Bundle extras) {
4451 super.addExtras(extras);
4452 if (mUserDisplayName != null) {
4453 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4454 }
4455 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004456 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004457 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004458 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4459 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004460 }
4461 }
4462
4463 /**
4464 * @hide
4465 */
4466 @Override
4467 protected void restoreFromExtras(Bundle extras) {
4468 super.restoreFromExtras(extras);
4469
4470 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004471 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4472 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004473 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004474 if (parcelables != null && parcelables instanceof Parcelable[]) {
4475 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004476 }
4477 }
4478
4479 /**
4480 * @hide
4481 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004482 @Override
4483 public RemoteViews makeContentView() {
4484 Message m = findLatestIncomingMessage();
4485 CharSequence title = mConversationTitle != null
4486 ? mConversationTitle
4487 : (m == null) ? null : m.mSender;
4488 CharSequence text = (m == null)
4489 ? null
4490 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004491
Adrian Roosc1a80b02016-04-05 14:54:55 -07004492 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4493 false /* hasProgress */,
4494 title,
4495 text);
4496 }
4497
4498 private Message findLatestIncomingMessage() {
4499 for (int i = mMessages.size() - 1; i >= 0; i--) {
4500 Message m = mMessages.get(i);
4501 // Incoming messages have a non-empty sender.
4502 if (!TextUtils.isEmpty(m.mSender)) {
4503 return m;
4504 }
4505 }
4506 return null;
4507 }
4508
4509 /**
4510 * @hide
4511 */
4512 @Override
4513 public RemoteViews makeBigContentView() {
4514 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4515 ? super.mBigContentTitle
4516 : mConversationTitle;
4517 boolean hasTitle = !TextUtils.isEmpty(title);
4518
Adrian Roos48d746a2016-04-12 14:57:28 -07004519 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004520 mBuilder.getMessagingLayoutResource(),
4521 false /* hasProgress */,
4522 title,
4523 null /* text */);
4524
4525 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4526 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4527
4528 // Make sure all rows are gone in case we reuse a view.
4529 for (int rowId : rowIds) {
4530 contentView.setViewVisibility(rowId, View.GONE);
4531 }
4532
4533 int i=0;
4534 int titlePadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4535 R.dimen.notification_messaging_spacing);
4536 contentView.setViewLayoutMarginBottom(R.id.line1, hasTitle ? titlePadding : 0);
4537 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
4538 mBuilder.mN.mLargeIcon == null ? 0 : (hasTitle ? 1 : 2));
4539
4540 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4541 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4542 Message m = mMessages.get(firstMessage + i);
4543 int rowId = rowIds[i];
4544
4545 contentView.setViewVisibility(rowId, View.VISIBLE);
4546 contentView.setTextViewText(rowId, makeMessageLine(m));
4547
4548 i++;
4549 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004550 return contentView;
4551 }
4552
Adrian Roosc1a80b02016-04-05 14:54:55 -07004553 private CharSequence makeMessageLine(Message m) {
4554 BidiFormatter bidi = BidiFormatter.getInstance();
4555 SpannableStringBuilder sb = new SpannableStringBuilder();
4556 if (TextUtils.isEmpty(m.mSender)) {
4557 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4558 sb.append(bidi.unicodeWrap(replyName),
4559 makeFontColorSpan(mBuilder.resolveContrastColor()),
4560 0 /* flags */);
4561 } else {
4562 sb.append(bidi.unicodeWrap(m.mSender),
4563 makeFontColorSpan(Color.BLACK),
4564 0 /* flags */);
4565 }
4566 CharSequence text = m.mText == null ? "" : m.mText;
4567 sb.append(" ").append(bidi.unicodeWrap(text));
4568 return sb;
4569 }
4570
Adrian Roosdedd1df2016-04-26 16:38:47 -07004571 /**
4572 * @hide
4573 */
4574 @Override
4575 public RemoteViews makeHeadsUpContentView() {
4576 Message m = findLatestIncomingMessage();
4577 CharSequence title = mConversationTitle != null
4578 ? mConversationTitle
4579 : (m == null) ? null : m.mSender;
4580 CharSequence text = (m == null)
4581 ? null
4582 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4583
4584 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4585 false /* hasProgress */,
4586 title,
4587 text);
4588 }
4589
Adrian Roosc1a80b02016-04-05 14:54:55 -07004590 private static TextAppearanceSpan makeFontColorSpan(int color) {
4591 return new TextAppearanceSpan(null, 0, 0,
4592 ColorStateList.valueOf(color), null);
4593 }
4594
Alex Hillsd9b04d92016-04-11 16:38:16 -04004595 public static final class Message {
4596
4597 static final String KEY_TEXT = "text";
4598 static final String KEY_TIMESTAMP = "time";
4599 static final String KEY_SENDER = "sender";
4600 static final String KEY_DATA_MIME_TYPE = "type";
4601 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004602
4603 private final CharSequence mText;
4604 private final long mTimestamp;
4605 private final CharSequence mSender;
4606
4607 private String mDataMimeType;
4608 private Uri mDataUri;
4609
4610 /**
4611 * Constructor
4612 * @param text A {@link CharSequence} to be displayed as the message content
4613 * @param timestamp Time at which the message arrived
4614 * @param sender A {@link CharSequence} to be used for displaying the name of the
4615 * sender. Should be <code>null</code> for messages by the current user, in which case
4616 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4617 * Should be unique amongst all individuals in the conversation, and should be
4618 * consistent during re-posts of the notification.
4619 */
4620 public Message(CharSequence text, long timestamp, CharSequence sender){
4621 mText = text;
4622 mTimestamp = timestamp;
4623 mSender = sender;
4624 }
4625
4626 /**
4627 * Sets a binary blob of data and an associated MIME type for a message. In the case
4628 * where the platform doesn't support the MIME type, the original text provided in the
4629 * constructor will be used.
4630 * @param dataMimeType The MIME type of the content. See
4631 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4632 * types on Android and Android Wear.
4633 * @param dataUri The uri containing the content whose type is given by the MIME type.
4634 * <p class="note">
4635 * <ol>
4636 * <li>Notification Listeners including the System UI need permission to access the
4637 * data the Uri points to. The recommended ways to do this are:</li>
4638 * <li>Store the data in your own ContentProvider, making sure that other apps have
4639 * the correct permission to access your provider. The preferred mechanism for
4640 * providing access is to use per-URI permissions which are temporary and only
4641 * grant access to the receiving application. An easy way to create a
4642 * ContentProvider like this is to use the FileProvider helper class.</li>
4643 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4644 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4645 * also store non-media types (see MediaStore.Files for more info). Files can be
4646 * inserted into the MediaStore using scanFile() after which a content:// style
4647 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4648 * Note that once added to the system MediaStore the content is accessible to any
4649 * app on the device.</li>
4650 * </ol>
4651 * @return this object for method chaining
4652 */
4653 public Message setData(String dataMimeType, Uri dataUri) {
4654 mDataMimeType = dataMimeType;
4655 mDataUri = dataUri;
4656 return this;
4657 }
4658
Alex Hillsfc737de2016-03-23 17:33:02 -04004659 /**
4660 * Get the text to be used for this message, or the fallback text if a type and content
4661 * Uri have been set
4662 */
4663 public CharSequence getText() {
4664 return mText;
4665 }
4666
4667 /**
4668 * Get the time at which this message arrived
4669 */
4670 public long getTimestamp() {
4671 return mTimestamp;
4672 }
4673
4674 /**
4675 * Get the text used to display the contact's name in the messaging experience
4676 */
4677 public CharSequence getSender() {
4678 return mSender;
4679 }
4680
4681 /**
4682 * Get the MIME type of the data pointed to by the Uri
4683 */
4684 public String getDataMimeType() {
4685 return mDataMimeType;
4686 }
4687
4688 /**
4689 * Get the the Uri pointing to the content of the message. Can be null, in which case
4690 * {@see #getText()} is used.
4691 */
4692 public Uri getDataUri() {
4693 return mDataUri;
4694 }
4695
Alex Hillsd9b04d92016-04-11 16:38:16 -04004696 private Bundle toBundle() {
4697 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04004698 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004699 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04004700 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004701 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04004702 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004703 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04004704 }
4705 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004706 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04004707 }
4708 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004709 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04004710 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004711 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04004712 }
4713
Alex Hillsd9b04d92016-04-11 16:38:16 -04004714 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
4715 Bundle[] bundles = new Bundle[messages.size()];
4716 final int N = messages.size();
4717 for (int i = 0; i < N; i++) {
4718 bundles[i] = messages.get(i).toBundle();
4719 }
4720 return bundles;
4721 }
4722
Adrian Roosdedd1df2016-04-26 16:38:47 -07004723 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004724 List<Message> messages = new ArrayList<>(bundles.length);
4725 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07004726 if (bundles[i] instanceof Bundle) {
4727 Message message = getMessageFromBundle((Bundle)bundles[i]);
4728 if (message != null) {
4729 messages.add(message);
4730 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004731 }
4732 }
4733 return messages;
4734 }
4735
4736 static Message getMessageFromBundle(Bundle bundle) {
4737 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07004738 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004739 return null;
4740 } else {
4741 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
4742 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
4743 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
4744 bundle.containsKey(KEY_DATA_URI)) {
4745
4746 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
4747 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04004748 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004749 return message;
4750 }
4751 } catch (ClassCastException e) {
4752 return null;
4753 }
4754 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004755 }
4756 }
4757
4758 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004759 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004760 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004761 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004762 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004763 * Notification notif = new Notification.Builder(mContext)
4764 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4765 * .setContentText(subject)
4766 * .setSmallIcon(R.drawable.new_mail)
4767 * .setLargeIcon(aBitmap)
4768 * .setStyle(new Notification.InboxStyle()
4769 * .addLine(str1)
4770 * .addLine(str2)
4771 * .setContentTitle(&quot;&quot;)
4772 * .setSummaryText(&quot;+3 more&quot;))
4773 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004774 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004775 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004776 * @see Notification#bigContentView
4777 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004778 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004779 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4780
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004781 public InboxStyle() {
4782 }
4783
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004784 /**
4785 * @deprecated use {@code InboxStyle()}.
4786 */
4787 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04004788 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004789 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004790 }
4791
Chris Wrend6297db2012-05-03 16:20:13 -04004792 /**
4793 * Overrides ContentTitle in the big form of the template.
4794 * This defaults to the value passed to setContentTitle().
4795 */
4796 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004797 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004798 return this;
4799 }
4800
4801 /**
4802 * Set the first line of text after the detail section in the big form of the template.
4803 */
4804 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004805 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004806 return this;
4807 }
4808
Chris Wren0bd664d2012-08-01 13:56:56 -04004809 /**
4810 * Append a line to the digest section of the Inbox notification.
4811 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004812 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004813 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004814 return this;
4815 }
4816
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004817 /**
4818 * @hide
4819 */
4820 public void addExtras(Bundle extras) {
4821 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004822
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004823 CharSequence[] a = new CharSequence[mTexts.size()];
4824 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4825 }
4826
Christoph Studer4600f9b2014-07-22 22:44:43 +02004827 /**
4828 * @hide
4829 */
4830 @Override
4831 protected void restoreFromExtras(Bundle extras) {
4832 super.restoreFromExtras(extras);
4833
4834 mTexts.clear();
4835 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4836 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4837 }
4838 }
4839
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004840 /**
4841 * @hide
4842 */
4843 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004844 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004845 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004846 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4847 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004848
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004849 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004850
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004851 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004852
Chris Wrend6297db2012-05-03 16:20:13 -04004853 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 -04004854 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004855
Chris Wren4ed80d52012-05-17 09:30:03 -04004856 // Make sure all rows are gone in case we reuse a view.
4857 for (int rowId : rowIds) {
4858 contentView.setViewVisibility(rowId, View.GONE);
4859 }
4860
Daniel Sandler879c5e02012-04-17 16:46:51 -04004861 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07004862 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4863 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08004864 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07004865 int onlyViewId = 0;
4866 int maxRows = rowIds.length;
4867 if (mBuilder.mActions.size() > 0) {
4868 maxRows--;
4869 }
4870 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004871 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07004872 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004873 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004874 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07004875 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004876 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07004877 if (first) {
4878 onlyViewId = rowIds[i];
4879 } else {
4880 onlyViewId = 0;
4881 }
Selim Cinek247fa012016-02-18 09:50:48 -08004882 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004883 }
4884 i++;
4885 }
Selim Cinek07c80172016-04-21 16:40:47 -07004886 if (onlyViewId != 0) {
4887 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
4888 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4889 R.dimen.notification_text_margin_top);
4890 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
4891 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004892
Daniel Sandler879c5e02012-04-17 16:46:51 -04004893 return contentView;
4894 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004895
Selim Cinek247fa012016-02-18 09:50:48 -08004896 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004897 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004898 if (first) {
4899 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4900 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4901 boolean hasProgress = max != 0 || ind;
4902 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4903 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4904 R.dimen.notification_content_picture_margin);
4905 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004906 }
4907 contentView.setViewLayoutMarginEnd(id, endMargin);
4908 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004909 }
Dan Sandler842dd772014-05-15 09:36:47 -04004910
4911 /**
4912 * Notification style for media playback notifications.
4913 *
4914 * In the expanded form, {@link Notification#bigContentView}, up to 5
4915 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004916 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004917 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4918 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4919 * treated as album artwork.
4920 *
4921 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4922 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004923 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004924 * in the standard view alongside the usual content.
4925 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004926 * Notifications created with MediaStyle will have their category set to
4927 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4928 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4929 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004930 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4931 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004932 * the System UI can identify this as a notification representing an active media session
4933 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4934 *
4935 * To use this style with your Notification, feed it to
4936 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4937 * <pre class="prettyprint">
4938 * Notification noti = new Notification.Builder()
4939 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004940 * .setContentTitle(&quot;Track title&quot;)
4941 * .setContentText(&quot;Artist - Album&quot;)
4942 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004943 * .setStyle(<b>new Notification.MediaStyle()</b>
4944 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004945 * .build();
4946 * </pre>
4947 *
4948 * @see Notification#bigContentView
4949 */
4950 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004951 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004952 static final int MAX_MEDIA_BUTTONS = 5;
4953
4954 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004955 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004956
4957 public MediaStyle() {
4958 }
4959
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004960 /**
4961 * @deprecated use {@code MediaStyle()}.
4962 */
4963 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04004964 public MediaStyle(Builder builder) {
4965 setBuilder(builder);
4966 }
4967
4968 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004969 * 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 -04004970 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004971 *
4972 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004973 */
4974 public MediaStyle setShowActionsInCompactView(int...actions) {
4975 mActionsToShowInCompact = actions;
4976 return this;
4977 }
4978
4979 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004980 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4981 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004982 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004983 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004984 mToken = token;
4985 return this;
4986 }
4987
Christoph Studer4600f9b2014-07-22 22:44:43 +02004988 /**
4989 * @hide
4990 */
Dan Sandler842dd772014-05-15 09:36:47 -04004991 @Override
4992 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004993 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004994 if (wip.category == null) {
4995 wip.category = Notification.CATEGORY_TRANSPORT;
4996 }
Dan Sandler842dd772014-05-15 09:36:47 -04004997 return wip;
4998 }
4999
Christoph Studer4600f9b2014-07-22 22:44:43 +02005000 /**
5001 * @hide
5002 */
5003 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005004 public RemoteViews makeContentView() {
5005 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005006 }
5007
5008 /**
5009 * @hide
5010 */
5011 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005012 public RemoteViews makeBigContentView() {
5013 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005014 }
5015
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005016 /**
5017 * @hide
5018 */
5019 @Override
5020 public RemoteViews makeHeadsUpContentView() {
5021 RemoteViews expanded = makeMediaBigContentView();
5022 return expanded != null ? expanded : makeMediaContentView();
5023 }
5024
Dan Sandler842dd772014-05-15 09:36:47 -04005025 /** @hide */
5026 @Override
5027 public void addExtras(Bundle extras) {
5028 super.addExtras(extras);
5029
5030 if (mToken != null) {
5031 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5032 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005033 if (mActionsToShowInCompact != null) {
5034 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5035 }
Dan Sandler842dd772014-05-15 09:36:47 -04005036 }
5037
Christoph Studer4600f9b2014-07-22 22:44:43 +02005038 /**
5039 * @hide
5040 */
5041 @Override
5042 protected void restoreFromExtras(Bundle extras) {
5043 super.restoreFromExtras(extras);
5044
5045 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5046 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5047 }
5048 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5049 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5050 }
5051 }
5052
Selim Cinek5bf069a2015-11-10 19:14:27 -05005053 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005054 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005055 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005056 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005057 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005058 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5059 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005060 if (!tombstone) {
5061 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5062 }
5063 button.setContentDescription(R.id.action0, action.title);
5064 return button;
5065 }
5066
5067 private RemoteViews makeMediaContentView() {
5068 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005069 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005070
5071 final int numActions = mBuilder.mActions.size();
5072 final int N = mActionsToShowInCompact == null
5073 ? 0
5074 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5075 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005076 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005077 for (int i = 0; i < N; i++) {
5078 if (i >= numActions) {
5079 throw new IllegalArgumentException(String.format(
5080 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5081 i, numActions - 1));
5082 }
5083
5084 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005085 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005086 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005087 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005088 }
5089 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005090 handleImage(view);
5091 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08005092 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5093 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005094 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08005095 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5096 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08005097 }
5098 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005099 return view;
5100 }
5101
5102 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005103 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005104 // Dont add an expanded view if there is no more content to be revealed
5105 int actionsInCompact = mActionsToShowInCompact == null
5106 ? 0
5107 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5108 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
5109 return null;
5110 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005111 RemoteViews big = mBuilder.applyStandardTemplate(
5112 R.layout.notification_template_material_big_media,
5113 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005114
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005115 if (actionCount > 0) {
5116 big.removeAllViews(com.android.internal.R.id.media_actions);
5117 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005118 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005119 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005120 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005121 }
5122 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005123 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005124 return big;
5125 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005126
Selim Cinek5bf069a2015-11-10 19:14:27 -05005127 private void handleImage(RemoteViews contentView) {
5128 if (mBuilder.mN.mLargeIcon != null) {
5129 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005130 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005131 }
5132 }
5133
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005134 /**
5135 * @hide
5136 */
5137 @Override
5138 protected boolean hasProgress() {
5139 return false;
5140 }
Dan Sandler842dd772014-05-15 09:36:47 -04005141 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005142
Selim Cinek593610c2016-02-16 18:42:57 -08005143 /**
5144 * Notification style for custom views that are decorated by the system
5145 *
5146 * <p>Instead of providing a notification that is completely custom, a developer can set this
5147 * style and still obtain system decorations like the notification header with the expand
5148 * affordance and actions.
5149 *
5150 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5151 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5152 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5153 * corresponding custom views to display.
5154 *
5155 * To use this style with your Notification, feed it to
5156 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5157 * <pre class="prettyprint">
5158 * Notification noti = new Notification.Builder()
5159 * .setSmallIcon(R.drawable.ic_stat_player)
5160 * .setLargeIcon(albumArtBitmap))
5161 * .setCustomContentView(contentView);
5162 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5163 * .build();
5164 * </pre>
5165 */
5166 public static class DecoratedCustomViewStyle extends Style {
5167
5168 public DecoratedCustomViewStyle() {
5169 }
5170
Selim Cinek593610c2016-02-16 18:42:57 -08005171 /**
5172 * @hide
5173 */
5174 public boolean displayCustomViewInline() {
5175 return true;
5176 }
5177
5178 /**
5179 * @hide
5180 */
5181 @Override
5182 public RemoteViews makeContentView() {
5183 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5184 }
5185
5186 /**
5187 * @hide
5188 */
5189 @Override
5190 public RemoteViews makeBigContentView() {
5191 return makeDecoratedBigContentView();
5192 }
5193
5194 /**
5195 * @hide
5196 */
5197 @Override
5198 public RemoteViews makeHeadsUpContentView() {
5199 return makeDecoratedHeadsUpContentView();
5200 }
5201
Selim Cinek593610c2016-02-16 18:42:57 -08005202 private RemoteViews makeDecoratedHeadsUpContentView() {
5203 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5204 ? mBuilder.mN.contentView
5205 : mBuilder.mN.headsUpContentView;
5206 if (mBuilder.mActions.size() == 0) {
5207 return makeStandardTemplateWithCustomContent(headsUpContentView);
5208 }
5209 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5210 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005211 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005212 return remoteViews;
5213 }
5214
Selim Cinek593610c2016-02-16 18:42:57 -08005215 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5216 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5217 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005218 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005219 return remoteViews;
5220 }
5221
Selim Cinek593610c2016-02-16 18:42:57 -08005222 private RemoteViews makeDecoratedBigContentView() {
5223 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5224 ? mBuilder.mN.contentView
5225 : mBuilder.mN.bigContentView;
5226 if (mBuilder.mActions.size() == 0) {
5227 return makeStandardTemplateWithCustomContent(bigContentView);
5228 }
5229 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5230 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005231 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005232 return remoteViews;
5233 }
Selim Cinek247fa012016-02-18 09:50:48 -08005234
5235 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5236 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005237 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005238 // Need to clone customContent before adding, because otherwise it can no longer be
5239 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005240 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005241 remoteViews.removeAllViews(R.id.notification_main_column);
5242 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005243 }
Selim Cinek247fa012016-02-18 09:50:48 -08005244 // also update the end margin if there is an image
5245 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5246 R.dimen.notification_content_margin_end);
5247 if (mBuilder.mN.mLargeIcon != null) {
5248 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5249 R.dimen.notification_content_picture_margin);
5250 }
5251 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
5252 }
Selim Cinek593610c2016-02-16 18:42:57 -08005253 }
5254
Selim Cinek03eb3b72016-02-18 10:39:45 -08005255 /**
5256 * Notification style for media custom views that are decorated by the system
5257 *
5258 * <p>Instead of providing a media notification that is completely custom, a developer can set
5259 * this style and still obtain system decorations like the notification header with the expand
5260 * affordance and actions.
5261 *
5262 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5263 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5264 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5265 * corresponding custom views to display.
5266 *
5267 * To use this style with your Notification, feed it to
5268 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5269 * <pre class="prettyprint">
5270 * Notification noti = new Notification.Builder()
5271 * .setSmallIcon(R.drawable.ic_stat_player)
5272 * .setLargeIcon(albumArtBitmap))
5273 * .setCustomContentView(contentView);
5274 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5275 * .setMediaSession(mySession))
5276 * .build();
5277 * </pre>
5278 *
5279 * @see android.app.Notification.DecoratedCustomViewStyle
5280 * @see android.app.Notification.MediaStyle
5281 */
5282 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5283
5284 public DecoratedMediaCustomViewStyle() {
5285 }
5286
Selim Cinek03eb3b72016-02-18 10:39:45 -08005287 /**
5288 * @hide
5289 */
5290 public boolean displayCustomViewInline() {
5291 return true;
5292 }
5293
5294 /**
5295 * @hide
5296 */
5297 @Override
5298 public RemoteViews makeContentView() {
5299 RemoteViews remoteViews = super.makeContentView();
5300 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5301 mBuilder.mN.contentView);
5302 }
5303
5304 /**
5305 * @hide
5306 */
5307 @Override
5308 public RemoteViews makeBigContentView() {
5309 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5310 ? mBuilder.mN.bigContentView
5311 : mBuilder.mN.contentView;
5312 return makeBigContentViewWithCustomContent(customRemoteView);
5313 }
5314
5315 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5316 RemoteViews remoteViews = super.makeBigContentView();
5317 if (remoteViews != null) {
5318 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5319 customRemoteView);
5320 } else if (customRemoteView != mBuilder.mN.contentView){
5321 remoteViews = super.makeContentView();
5322 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5323 customRemoteView);
5324 } else {
5325 return null;
5326 }
5327 }
5328
5329 /**
5330 * @hide
5331 */
5332 @Override
5333 public RemoteViews makeHeadsUpContentView() {
5334 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5335 ? mBuilder.mN.headsUpContentView
5336 : mBuilder.mN.contentView;
5337 return makeBigContentViewWithCustomContent(customRemoteView);
5338 }
5339
5340 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5341 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005342 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005343 // Need to clone customContent before adding, because otherwise it can no longer be
5344 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005345 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005346 remoteViews.removeAllViews(id);
5347 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005348 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005349 return remoteViews;
5350 }
5351 }
5352
Christoph Studer4600f9b2014-07-22 22:44:43 +02005353 // When adding a new Style subclass here, don't forget to update
5354 // Builder.getNotificationStyleClass.
5355
Griff Hazen61a9e862014-05-22 16:05:19 -07005356 /**
5357 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5358 * metadata or change options on a notification builder.
5359 */
5360 public interface Extender {
5361 /**
5362 * Apply this extender to a notification builder.
5363 * @param builder the builder to be modified.
5364 * @return the build object for chaining.
5365 */
5366 public Builder extend(Builder builder);
5367 }
5368
5369 /**
5370 * Helper class to add wearable extensions to notifications.
5371 * <p class="note"> See
5372 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5373 * for Android Wear</a> for more information on how to use this class.
5374 * <p>
5375 * To create a notification with wearable extensions:
5376 * <ol>
5377 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5378 * properties.
5379 * <li>Create a {@link android.app.Notification.WearableExtender}.
5380 * <li>Set wearable-specific properties using the
5381 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5382 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5383 * notification.
5384 * <li>Post the notification to the notification system with the
5385 * {@code NotificationManager.notify(...)} methods.
5386 * </ol>
5387 *
5388 * <pre class="prettyprint">
5389 * Notification notif = new Notification.Builder(mContext)
5390 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5391 * .setContentText(subject)
5392 * .setSmallIcon(R.drawable.new_mail)
5393 * .extend(new Notification.WearableExtender()
5394 * .setContentIcon(R.drawable.new_mail))
5395 * .build();
5396 * NotificationManager notificationManger =
5397 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5398 * notificationManger.notify(0, notif);</pre>
5399 *
5400 * <p>Wearable extensions can be accessed on an existing notification by using the
5401 * {@code WearableExtender(Notification)} constructor,
5402 * and then using the {@code get} methods to access values.
5403 *
5404 * <pre class="prettyprint">
5405 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5406 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005407 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005408 */
5409 public static final class WearableExtender implements Extender {
5410 /**
5411 * Sentinel value for an action index that is unset.
5412 */
5413 public static final int UNSET_ACTION_INDEX = -1;
5414
5415 /**
5416 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5417 * default sizing.
5418 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005419 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005420 * on their content.
5421 */
5422 public static final int SIZE_DEFAULT = 0;
5423
5424 /**
5425 * Size value for use with {@link #setCustomSizePreset} to show this notification
5426 * with an extra small size.
5427 * <p>This value is only applicable for custom display notifications created using
5428 * {@link #setDisplayIntent}.
5429 */
5430 public static final int SIZE_XSMALL = 1;
5431
5432 /**
5433 * Size value for use with {@link #setCustomSizePreset} to show this notification
5434 * with a small size.
5435 * <p>This value is only applicable for custom display notifications created using
5436 * {@link #setDisplayIntent}.
5437 */
5438 public static final int SIZE_SMALL = 2;
5439
5440 /**
5441 * Size value for use with {@link #setCustomSizePreset} to show this notification
5442 * with a medium size.
5443 * <p>This value is only applicable for custom display notifications created using
5444 * {@link #setDisplayIntent}.
5445 */
5446 public static final int SIZE_MEDIUM = 3;
5447
5448 /**
5449 * Size value for use with {@link #setCustomSizePreset} to show this notification
5450 * with a large size.
5451 * <p>This value is only applicable for custom display notifications created using
5452 * {@link #setDisplayIntent}.
5453 */
5454 public static final int SIZE_LARGE = 4;
5455
Griff Hazend5f11f92014-05-27 15:40:09 -07005456 /**
5457 * Size value for use with {@link #setCustomSizePreset} to show this notification
5458 * full screen.
5459 * <p>This value is only applicable for custom display notifications created using
5460 * {@link #setDisplayIntent}.
5461 */
5462 public static final int SIZE_FULL_SCREEN = 5;
5463
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005464 /**
5465 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5466 * short amount of time when this notification is displayed on the screen. This
5467 * is the default value.
5468 */
5469 public static final int SCREEN_TIMEOUT_SHORT = 0;
5470
5471 /**
5472 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5473 * for a longer amount of time when this notification is displayed on the screen.
5474 */
5475 public static final int SCREEN_TIMEOUT_LONG = -1;
5476
Griff Hazen61a9e862014-05-22 16:05:19 -07005477 /** Notification extra which contains wearable extensions */
5478 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5479
Pete Gastaf6781d2014-10-07 15:17:05 -04005480 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005481 private static final String KEY_ACTIONS = "actions";
5482 private static final String KEY_FLAGS = "flags";
5483 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5484 private static final String KEY_PAGES = "pages";
5485 private static final String KEY_BACKGROUND = "background";
5486 private static final String KEY_CONTENT_ICON = "contentIcon";
5487 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5488 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5489 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5490 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5491 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005492 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005493 private static final String KEY_DISMISSAL_ID = "dismissalId";
Griff Hazen61a9e862014-05-22 16:05:19 -07005494
5495 // Flags bitwise-ored to mFlags
5496 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5497 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5498 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5499 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005500 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005501 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005502 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005503
5504 // Default value for flags integer
5505 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5506
5507 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5508 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5509
5510 private ArrayList<Action> mActions = new ArrayList<Action>();
5511 private int mFlags = DEFAULT_FLAGS;
5512 private PendingIntent mDisplayIntent;
5513 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5514 private Bitmap mBackground;
5515 private int mContentIcon;
5516 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5517 private int mContentActionIndex = UNSET_ACTION_INDEX;
5518 private int mCustomSizePreset = SIZE_DEFAULT;
5519 private int mCustomContentHeight;
5520 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005521 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005522 private String mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005523
5524 /**
5525 * Create a {@link android.app.Notification.WearableExtender} with default
5526 * options.
5527 */
5528 public WearableExtender() {
5529 }
5530
5531 public WearableExtender(Notification notif) {
5532 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5533 if (wearableBundle != null) {
5534 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5535 if (actions != null) {
5536 mActions.addAll(actions);
5537 }
5538
5539 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5540 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5541
5542 Notification[] pages = getNotificationArrayFromBundle(
5543 wearableBundle, KEY_PAGES);
5544 if (pages != null) {
5545 Collections.addAll(mPages, pages);
5546 }
5547
5548 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5549 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5550 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5551 DEFAULT_CONTENT_ICON_GRAVITY);
5552 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5553 UNSET_ACTION_INDEX);
5554 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5555 SIZE_DEFAULT);
5556 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5557 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005558 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005559 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Griff Hazen61a9e862014-05-22 16:05:19 -07005560 }
5561 }
5562
5563 /**
5564 * Apply wearable extensions to a notification that is being built. This is typically
5565 * called by the {@link android.app.Notification.Builder#extend} method of
5566 * {@link android.app.Notification.Builder}.
5567 */
5568 @Override
5569 public Notification.Builder extend(Notification.Builder builder) {
5570 Bundle wearableBundle = new Bundle();
5571
5572 if (!mActions.isEmpty()) {
5573 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5574 }
5575 if (mFlags != DEFAULT_FLAGS) {
5576 wearableBundle.putInt(KEY_FLAGS, mFlags);
5577 }
5578 if (mDisplayIntent != null) {
5579 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5580 }
5581 if (!mPages.isEmpty()) {
5582 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5583 new Notification[mPages.size()]));
5584 }
5585 if (mBackground != null) {
5586 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5587 }
5588 if (mContentIcon != 0) {
5589 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5590 }
5591 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5592 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5593 }
5594 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5595 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5596 mContentActionIndex);
5597 }
5598 if (mCustomSizePreset != SIZE_DEFAULT) {
5599 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5600 }
5601 if (mCustomContentHeight != 0) {
5602 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5603 }
5604 if (mGravity != DEFAULT_GRAVITY) {
5605 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5606 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005607 if (mHintScreenTimeout != 0) {
5608 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5609 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005610 if (mDismissalId != null) {
5611 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5612 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005613
5614 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5615 return builder;
5616 }
5617
5618 @Override
5619 public WearableExtender clone() {
5620 WearableExtender that = new WearableExtender();
5621 that.mActions = new ArrayList<Action>(this.mActions);
5622 that.mFlags = this.mFlags;
5623 that.mDisplayIntent = this.mDisplayIntent;
5624 that.mPages = new ArrayList<Notification>(this.mPages);
5625 that.mBackground = this.mBackground;
5626 that.mContentIcon = this.mContentIcon;
5627 that.mContentIconGravity = this.mContentIconGravity;
5628 that.mContentActionIndex = this.mContentActionIndex;
5629 that.mCustomSizePreset = this.mCustomSizePreset;
5630 that.mCustomContentHeight = this.mCustomContentHeight;
5631 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005632 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005633 that.mDismissalId = this.mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005634 return that;
5635 }
5636
5637 /**
5638 * Add a wearable action to this notification.
5639 *
5640 * <p>When wearable actions are added using this method, the set of actions that
5641 * show on a wearable device splits from devices that only show actions added
5642 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5643 * of which actions display on different devices.
5644 *
5645 * @param action the action to add to this notification
5646 * @return this object for method chaining
5647 * @see android.app.Notification.Action
5648 */
5649 public WearableExtender addAction(Action action) {
5650 mActions.add(action);
5651 return this;
5652 }
5653
5654 /**
5655 * Adds wearable actions to this notification.
5656 *
5657 * <p>When wearable actions are added using this method, the set of actions that
5658 * show on a wearable device splits from devices that only show actions added
5659 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5660 * of which actions display on different devices.
5661 *
5662 * @param actions the actions to add to this notification
5663 * @return this object for method chaining
5664 * @see android.app.Notification.Action
5665 */
5666 public WearableExtender addActions(List<Action> actions) {
5667 mActions.addAll(actions);
5668 return this;
5669 }
5670
5671 /**
5672 * Clear all wearable actions present on this builder.
5673 * @return this object for method chaining.
5674 * @see #addAction
5675 */
5676 public WearableExtender clearActions() {
5677 mActions.clear();
5678 return this;
5679 }
5680
5681 /**
5682 * Get the wearable actions present on this notification.
5683 */
5684 public List<Action> getActions() {
5685 return mActions;
5686 }
5687
5688 /**
5689 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005690 * this notification. The {@link PendingIntent} provided should be for an activity.
5691 *
5692 * <pre class="prettyprint">
5693 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5694 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5695 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5696 * Notification notif = new Notification.Builder(context)
5697 * .extend(new Notification.WearableExtender()
5698 * .setDisplayIntent(displayPendingIntent)
5699 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5700 * .build();</pre>
5701 *
5702 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005703 * should have an empty task affinity. It is also recommended to use the device
5704 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005705 *
5706 * <p>Example AndroidManifest.xml entry:
5707 * <pre class="prettyprint">
5708 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5709 * android:exported=&quot;true&quot;
5710 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005711 * android:taskAffinity=&quot;&quot;
5712 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005713 *
5714 * @param intent the {@link PendingIntent} for an activity
5715 * @return this object for method chaining
5716 * @see android.app.Notification.WearableExtender#getDisplayIntent
5717 */
5718 public WearableExtender setDisplayIntent(PendingIntent intent) {
5719 mDisplayIntent = intent;
5720 return this;
5721 }
5722
5723 /**
5724 * Get the intent to launch inside of an activity view when displaying this
5725 * notification. This {@code PendingIntent} should be for an activity.
5726 */
5727 public PendingIntent getDisplayIntent() {
5728 return mDisplayIntent;
5729 }
5730
5731 /**
5732 * Add an additional page of content to display with this notification. The current
5733 * notification forms the first page, and pages added using this function form
5734 * subsequent pages. This field can be used to separate a notification into multiple
5735 * sections.
5736 *
5737 * @param page the notification to add as another page
5738 * @return this object for method chaining
5739 * @see android.app.Notification.WearableExtender#getPages
5740 */
5741 public WearableExtender addPage(Notification page) {
5742 mPages.add(page);
5743 return this;
5744 }
5745
5746 /**
5747 * Add additional pages of content to display with this notification. The current
5748 * notification forms the first page, and pages added using this function form
5749 * subsequent pages. This field can be used to separate a notification into multiple
5750 * sections.
5751 *
5752 * @param pages a list of notifications
5753 * @return this object for method chaining
5754 * @see android.app.Notification.WearableExtender#getPages
5755 */
5756 public WearableExtender addPages(List<Notification> pages) {
5757 mPages.addAll(pages);
5758 return this;
5759 }
5760
5761 /**
5762 * Clear all additional pages present on this builder.
5763 * @return this object for method chaining.
5764 * @see #addPage
5765 */
5766 public WearableExtender clearPages() {
5767 mPages.clear();
5768 return this;
5769 }
5770
5771 /**
5772 * Get the array of additional pages of content for displaying this notification. The
5773 * current notification forms the first page, and elements within this array form
5774 * subsequent pages. This field can be used to separate a notification into multiple
5775 * sections.
5776 * @return the pages for this notification
5777 */
5778 public List<Notification> getPages() {
5779 return mPages;
5780 }
5781
5782 /**
5783 * Set a background image to be displayed behind the notification content.
5784 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5785 * will work with any notification style.
5786 *
5787 * @param background the background bitmap
5788 * @return this object for method chaining
5789 * @see android.app.Notification.WearableExtender#getBackground
5790 */
5791 public WearableExtender setBackground(Bitmap background) {
5792 mBackground = background;
5793 return this;
5794 }
5795
5796 /**
5797 * Get a background image to be displayed behind the notification content.
5798 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5799 * will work with any notification style.
5800 *
5801 * @return the background image
5802 * @see android.app.Notification.WearableExtender#setBackground
5803 */
5804 public Bitmap getBackground() {
5805 return mBackground;
5806 }
5807
5808 /**
5809 * Set an icon that goes with the content of this notification.
5810 */
5811 public WearableExtender setContentIcon(int icon) {
5812 mContentIcon = icon;
5813 return this;
5814 }
5815
5816 /**
5817 * Get an icon that goes with the content of this notification.
5818 */
5819 public int getContentIcon() {
5820 return mContentIcon;
5821 }
5822
5823 /**
5824 * Set the gravity that the content icon should have within the notification display.
5825 * Supported values include {@link android.view.Gravity#START} and
5826 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5827 * @see #setContentIcon
5828 */
5829 public WearableExtender setContentIconGravity(int contentIconGravity) {
5830 mContentIconGravity = contentIconGravity;
5831 return this;
5832 }
5833
5834 /**
5835 * Get the gravity that the content icon should have within the notification display.
5836 * Supported values include {@link android.view.Gravity#START} and
5837 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5838 * @see #getContentIcon
5839 */
5840 public int getContentIconGravity() {
5841 return mContentIconGravity;
5842 }
5843
5844 /**
5845 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005846 * this notification. This action will no longer display separately from the
5847 * notification's content.
5848 *
Griff Hazenca48d352014-05-28 22:37:13 -07005849 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005850 * set, although the list of available actions comes from the main notification and not
5851 * from the child page's notification.
5852 *
5853 * @param actionIndex The index of the action to hoist onto the current notification page.
5854 * If wearable actions were added to the main notification, this index
5855 * will apply to that list, otherwise it will apply to the regular
5856 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005857 */
5858 public WearableExtender setContentAction(int actionIndex) {
5859 mContentActionIndex = actionIndex;
5860 return this;
5861 }
5862
5863 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005864 * Get the index of the notification action, if any, that was specified as being clickable
5865 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005866 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005867 *
Griff Hazenca48d352014-05-28 22:37:13 -07005868 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005869 * set, although the list of available actions comes from the main notification and not
5870 * from the child page's notification.
5871 *
5872 * <p>If wearable specific actions were added to the main notification, this index will
5873 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005874 *
5875 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005876 */
5877 public int getContentAction() {
5878 return mContentActionIndex;
5879 }
5880
5881 /**
5882 * Set the gravity that this notification should have within the available viewport space.
5883 * Supported values include {@link android.view.Gravity#TOP},
5884 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5885 * The default value is {@link android.view.Gravity#BOTTOM}.
5886 */
5887 public WearableExtender setGravity(int gravity) {
5888 mGravity = gravity;
5889 return this;
5890 }
5891
5892 /**
5893 * Get the gravity that this notification should have within the available viewport space.
5894 * Supported values include {@link android.view.Gravity#TOP},
5895 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5896 * The default value is {@link android.view.Gravity#BOTTOM}.
5897 */
5898 public int getGravity() {
5899 return mGravity;
5900 }
5901
5902 /**
5903 * Set the custom size preset for the display of this notification out of the available
5904 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5905 * {@link #SIZE_LARGE}.
5906 * <p>Some custom size presets are only applicable for custom display notifications created
5907 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5908 * documentation for the preset in question. See also
5909 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5910 */
5911 public WearableExtender setCustomSizePreset(int sizePreset) {
5912 mCustomSizePreset = sizePreset;
5913 return this;
5914 }
5915
5916 /**
5917 * Get the custom size preset for the display of this notification out of the available
5918 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5919 * {@link #SIZE_LARGE}.
5920 * <p>Some custom size presets are only applicable for custom display notifications created
5921 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5922 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5923 */
5924 public int getCustomSizePreset() {
5925 return mCustomSizePreset;
5926 }
5927
5928 /**
5929 * Set the custom height in pixels for the display of this notification's content.
5930 * <p>This option is only available for custom display notifications created
5931 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5932 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5933 * {@link #getCustomContentHeight}.
5934 */
5935 public WearableExtender setCustomContentHeight(int height) {
5936 mCustomContentHeight = height;
5937 return this;
5938 }
5939
5940 /**
5941 * Get the custom height in pixels for the display of this notification's content.
5942 * <p>This option is only available for custom display notifications created
5943 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5944 * {@link #setCustomContentHeight}.
5945 */
5946 public int getCustomContentHeight() {
5947 return mCustomContentHeight;
5948 }
5949
5950 /**
5951 * Set whether the scrolling position for the contents of this notification should start
5952 * at the bottom of the contents instead of the top when the contents are too long to
5953 * display within the screen. Default is false (start scroll at the top).
5954 */
5955 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5956 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5957 return this;
5958 }
5959
5960 /**
5961 * Get whether the scrolling position for the contents of this notification should start
5962 * at the bottom of the contents instead of the top when the contents are too long to
5963 * display within the screen. Default is false (start scroll at the top).
5964 */
5965 public boolean getStartScrollBottom() {
5966 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5967 }
5968
5969 /**
5970 * Set whether the content intent is available when the wearable device is not connected
5971 * to a companion device. The user can still trigger this intent when the wearable device
5972 * is offline, but a visual hint will indicate that the content intent may not be available.
5973 * Defaults to true.
5974 */
5975 public WearableExtender setContentIntentAvailableOffline(
5976 boolean contentIntentAvailableOffline) {
5977 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5978 return this;
5979 }
5980
5981 /**
5982 * Get whether the content intent is available when the wearable device is not connected
5983 * to a companion device. The user can still trigger this intent when the wearable device
5984 * is offline, but a visual hint will indicate that the content intent may not be available.
5985 * Defaults to true.
5986 */
5987 public boolean getContentIntentAvailableOffline() {
5988 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5989 }
5990
5991 /**
5992 * Set a hint that this notification's icon should not be displayed.
5993 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5994 * @return this object for method chaining
5995 */
5996 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5997 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5998 return this;
5999 }
6000
6001 /**
6002 * Get a hint that this notification's icon should not be displayed.
6003 * @return {@code true} if this icon should not be displayed, false otherwise.
6004 * The default value is {@code false} if this was never set.
6005 */
6006 public boolean getHintHideIcon() {
6007 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6008 }
6009
6010 /**
6011 * Set a visual hint that only the background image of this notification should be
6012 * displayed, and other semantic content should be hidden. This hint is only applicable
6013 * to sub-pages added using {@link #addPage}.
6014 */
6015 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6016 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6017 return this;
6018 }
6019
6020 /**
6021 * Get a visual hint that only the background image of this notification should be
6022 * displayed, and other semantic content should be hidden. This hint is only applicable
6023 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6024 */
6025 public boolean getHintShowBackgroundOnly() {
6026 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6027 }
6028
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006029 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006030 * Set a hint that this notification's background should not be clipped if possible,
6031 * and should instead be resized to fully display on the screen, retaining the aspect
6032 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006033 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6034 * @return this object for method chaining
6035 */
6036 public WearableExtender setHintAvoidBackgroundClipping(
6037 boolean hintAvoidBackgroundClipping) {
6038 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6039 return this;
6040 }
6041
6042 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006043 * Get a hint that this notification's background should not be clipped if possible,
6044 * and should instead be resized to fully display on the screen, retaining the aspect
6045 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006046 * @return {@code true} if it's ok if the background is clipped on the screen, false
6047 * otherwise. The default value is {@code false} if this was never set.
6048 */
6049 public boolean getHintAvoidBackgroundClipping() {
6050 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6051 }
6052
6053 /**
6054 * Set a hint that the screen should remain on for at least this duration when
6055 * this notification is displayed on the screen.
6056 * @param timeout The requested screen timeout in milliseconds. Can also be either
6057 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6058 * @return this object for method chaining
6059 */
6060 public WearableExtender setHintScreenTimeout(int timeout) {
6061 mHintScreenTimeout = timeout;
6062 return this;
6063 }
6064
6065 /**
6066 * Get the duration, in milliseconds, that the screen should remain on for
6067 * when this notification is displayed.
6068 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6069 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6070 */
6071 public int getHintScreenTimeout() {
6072 return mHintScreenTimeout;
6073 }
6074
Alex Hills9ab3a232016-04-05 14:54:56 -04006075 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006076 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6077 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6078 * qr codes, as well as other simple black-and-white tickets.
6079 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6080 * @return this object for method chaining
6081 */
6082 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6083 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6084 return this;
6085 }
6086
6087 /**
6088 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6089 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6090 * qr codes, as well as other simple black-and-white tickets.
6091 * @return {@code true} if it should be displayed in ambient, false otherwise
6092 * otherwise. The default value is {@code false} if this was never set.
6093 */
6094 public boolean getHintAmbientBigPicture() {
6095 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6096 }
6097
6098 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006099 * Set a hint that this notification's content intent will launch an {@link Activity}
6100 * directly, telling the platform that it can generate the appropriate transitions.
6101 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6102 * an activity and transitions should be generated, false otherwise.
6103 * @return this object for method chaining
6104 */
6105 public WearableExtender setHintContentIntentLaunchesActivity(
6106 boolean hintContentIntentLaunchesActivity) {
6107 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6108 return this;
6109 }
6110
6111 /**
6112 * Get a hint that this notification's content intent will launch an {@link Activity}
6113 * directly, telling the platform that it can generate the appropriate transitions
6114 * @return {@code true} if the content intent will launch an activity and transitions should
6115 * be generated, false otherwise. The default value is {@code false} if this was never set.
6116 */
6117 public boolean getHintContentIntentLaunchesActivity() {
6118 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6119 }
6120
Nadia Benbernou948627e2016-04-14 14:41:08 -04006121 /**
6122 * When you post a notification, if you set the dismissal id field, then when that
6123 * notification is canceled, notifications on other wearables and the paired Android phone
6124 * having that same dismissal id will also be canceled. Note that this only works if you
6125 * have notification bridge mode set to NO_BRIDGING in your Wear app manifest. See
6126 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
6127 * Notifications</a> for more information on how to use the bridge mode feature.
6128 * @param dismissalId the dismissal id of the notification.
6129 * @return this object for method chaining
6130 */
6131 public WearableExtender setDismissalId(String dismissalId) {
6132 mDismissalId = dismissalId;
6133 return this;
6134 }
6135
6136 /**
6137 * Returns the dismissal id of the notification.
6138 * @return the dismissal id of the notification or null if it has not been set.
6139 */
6140 public String getDismissalId() {
6141 return mDismissalId;
6142 }
6143
Griff Hazen61a9e862014-05-22 16:05:19 -07006144 private void setFlag(int mask, boolean value) {
6145 if (value) {
6146 mFlags |= mask;
6147 } else {
6148 mFlags &= ~mask;
6149 }
6150 }
6151 }
6152
6153 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006154 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6155 * with car extensions:
6156 *
6157 * <ol>
6158 * <li>Create an {@link Notification.Builder}, setting any desired
6159 * properties.
6160 * <li>Create a {@link CarExtender}.
6161 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6162 * {@link CarExtender}.
6163 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6164 * to apply the extensions to a notification.
6165 * </ol>
6166 *
6167 * <pre class="prettyprint">
6168 * Notification notification = new Notification.Builder(context)
6169 * ...
6170 * .extend(new CarExtender()
6171 * .set*(...))
6172 * .build();
6173 * </pre>
6174 *
6175 * <p>Car extensions can be accessed on an existing notification by using the
6176 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6177 * to access values.
6178 */
6179 public static final class CarExtender implements Extender {
6180 private static final String TAG = "CarExtender";
6181
6182 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6183 private static final String EXTRA_LARGE_ICON = "large_icon";
6184 private static final String EXTRA_CONVERSATION = "car_conversation";
6185 private static final String EXTRA_COLOR = "app_color";
6186
6187 private Bitmap mLargeIcon;
6188 private UnreadConversation mUnreadConversation;
6189 private int mColor = Notification.COLOR_DEFAULT;
6190
6191 /**
6192 * Create a {@link CarExtender} with default options.
6193 */
6194 public CarExtender() {
6195 }
6196
6197 /**
6198 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6199 *
6200 * @param notif The notification from which to copy options.
6201 */
6202 public CarExtender(Notification notif) {
6203 Bundle carBundle = notif.extras == null ?
6204 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6205 if (carBundle != null) {
6206 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6207 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6208
6209 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6210 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6211 }
6212 }
6213
6214 /**
6215 * Apply car extensions to a notification that is being built. This is typically called by
6216 * the {@link Notification.Builder#extend(Notification.Extender)}
6217 * method of {@link Notification.Builder}.
6218 */
6219 @Override
6220 public Notification.Builder extend(Notification.Builder builder) {
6221 Bundle carExtensions = new Bundle();
6222
6223 if (mLargeIcon != null) {
6224 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6225 }
6226 if (mColor != Notification.COLOR_DEFAULT) {
6227 carExtensions.putInt(EXTRA_COLOR, mColor);
6228 }
6229
6230 if (mUnreadConversation != null) {
6231 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6232 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6233 }
6234
6235 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6236 return builder;
6237 }
6238
6239 /**
6240 * Sets the accent color to use when Android Auto presents the notification.
6241 *
6242 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6243 * to accent the displayed notification. However, not all colors are acceptable in an
6244 * automotive setting. This method can be used to override the color provided in the
6245 * notification in such a situation.
6246 */
Tor Norbye80756e32015-03-02 09:39:27 -08006247 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006248 mColor = color;
6249 return this;
6250 }
6251
6252 /**
6253 * Gets the accent color.
6254 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006255 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006256 */
Tor Norbye80756e32015-03-02 09:39:27 -08006257 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006258 public int getColor() {
6259 return mColor;
6260 }
6261
6262 /**
6263 * Sets the large icon of the car notification.
6264 *
6265 * If no large icon is set in the extender, Android Auto will display the icon
6266 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6267 *
6268 * @param largeIcon The large icon to use in the car notification.
6269 * @return This object for method chaining.
6270 */
6271 public CarExtender setLargeIcon(Bitmap largeIcon) {
6272 mLargeIcon = largeIcon;
6273 return this;
6274 }
6275
6276 /**
6277 * Gets the large icon used in this car notification, or null if no icon has been set.
6278 *
6279 * @return The large icon for the car notification.
6280 * @see CarExtender#setLargeIcon
6281 */
6282 public Bitmap getLargeIcon() {
6283 return mLargeIcon;
6284 }
6285
6286 /**
6287 * Sets the unread conversation in a message notification.
6288 *
6289 * @param unreadConversation The unread part of the conversation this notification conveys.
6290 * @return This object for method chaining.
6291 */
6292 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6293 mUnreadConversation = unreadConversation;
6294 return this;
6295 }
6296
6297 /**
6298 * Returns the unread conversation conveyed by this notification.
6299 * @see #setUnreadConversation(UnreadConversation)
6300 */
6301 public UnreadConversation getUnreadConversation() {
6302 return mUnreadConversation;
6303 }
6304
6305 /**
6306 * A class which holds the unread messages from a conversation.
6307 */
6308 public static class UnreadConversation {
6309 private static final String KEY_AUTHOR = "author";
6310 private static final String KEY_TEXT = "text";
6311 private static final String KEY_MESSAGES = "messages";
6312 private static final String KEY_REMOTE_INPUT = "remote_input";
6313 private static final String KEY_ON_REPLY = "on_reply";
6314 private static final String KEY_ON_READ = "on_read";
6315 private static final String KEY_PARTICIPANTS = "participants";
6316 private static final String KEY_TIMESTAMP = "timestamp";
6317
6318 private final String[] mMessages;
6319 private final RemoteInput mRemoteInput;
6320 private final PendingIntent mReplyPendingIntent;
6321 private final PendingIntent mReadPendingIntent;
6322 private final String[] mParticipants;
6323 private final long mLatestTimestamp;
6324
6325 UnreadConversation(String[] messages, RemoteInput remoteInput,
6326 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6327 String[] participants, long latestTimestamp) {
6328 mMessages = messages;
6329 mRemoteInput = remoteInput;
6330 mReadPendingIntent = readPendingIntent;
6331 mReplyPendingIntent = replyPendingIntent;
6332 mParticipants = participants;
6333 mLatestTimestamp = latestTimestamp;
6334 }
6335
6336 /**
6337 * Gets the list of messages conveyed by this notification.
6338 */
6339 public String[] getMessages() {
6340 return mMessages;
6341 }
6342
6343 /**
6344 * Gets the remote input that will be used to convey the response to a message list, or
6345 * null if no such remote input exists.
6346 */
6347 public RemoteInput getRemoteInput() {
6348 return mRemoteInput;
6349 }
6350
6351 /**
6352 * Gets the pending intent that will be triggered when the user replies to this
6353 * notification.
6354 */
6355 public PendingIntent getReplyPendingIntent() {
6356 return mReplyPendingIntent;
6357 }
6358
6359 /**
6360 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6361 * in this object's message list.
6362 */
6363 public PendingIntent getReadPendingIntent() {
6364 return mReadPendingIntent;
6365 }
6366
6367 /**
6368 * Gets the participants in the conversation.
6369 */
6370 public String[] getParticipants() {
6371 return mParticipants;
6372 }
6373
6374 /**
6375 * Gets the firs participant in the conversation.
6376 */
6377 public String getParticipant() {
6378 return mParticipants.length > 0 ? mParticipants[0] : null;
6379 }
6380
6381 /**
6382 * Gets the timestamp of the conversation.
6383 */
6384 public long getLatestTimestamp() {
6385 return mLatestTimestamp;
6386 }
6387
6388 Bundle getBundleForUnreadConversation() {
6389 Bundle b = new Bundle();
6390 String author = null;
6391 if (mParticipants != null && mParticipants.length > 1) {
6392 author = mParticipants[0];
6393 }
6394 Parcelable[] messages = new Parcelable[mMessages.length];
6395 for (int i = 0; i < messages.length; i++) {
6396 Bundle m = new Bundle();
6397 m.putString(KEY_TEXT, mMessages[i]);
6398 m.putString(KEY_AUTHOR, author);
6399 messages[i] = m;
6400 }
6401 b.putParcelableArray(KEY_MESSAGES, messages);
6402 if (mRemoteInput != null) {
6403 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6404 }
6405 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6406 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6407 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6408 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6409 return b;
6410 }
6411
6412 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6413 if (b == null) {
6414 return null;
6415 }
6416 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6417 String[] messages = null;
6418 if (parcelableMessages != null) {
6419 String[] tmp = new String[parcelableMessages.length];
6420 boolean success = true;
6421 for (int i = 0; i < tmp.length; i++) {
6422 if (!(parcelableMessages[i] instanceof Bundle)) {
6423 success = false;
6424 break;
6425 }
6426 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6427 if (tmp[i] == null) {
6428 success = false;
6429 break;
6430 }
6431 }
6432 if (success) {
6433 messages = tmp;
6434 } else {
6435 return null;
6436 }
6437 }
6438
6439 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6440 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6441
6442 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6443
6444 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6445 if (participants == null || participants.length != 1) {
6446 return null;
6447 }
6448
6449 return new UnreadConversation(messages,
6450 remoteInput,
6451 onReply,
6452 onRead,
6453 participants, b.getLong(KEY_TIMESTAMP));
6454 }
6455 };
6456
6457 /**
6458 * Builder class for {@link CarExtender.UnreadConversation} objects.
6459 */
6460 public static class Builder {
6461 private final List<String> mMessages = new ArrayList<String>();
6462 private final String mParticipant;
6463 private RemoteInput mRemoteInput;
6464 private PendingIntent mReadPendingIntent;
6465 private PendingIntent mReplyPendingIntent;
6466 private long mLatestTimestamp;
6467
6468 /**
6469 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6470 *
6471 * @param name The name of the other participant in the conversation.
6472 */
6473 public Builder(String name) {
6474 mParticipant = name;
6475 }
6476
6477 /**
6478 * Appends a new unread message to the list of messages for this conversation.
6479 *
6480 * The messages should be added from oldest to newest.
6481 *
6482 * @param message The text of the new unread message.
6483 * @return This object for method chaining.
6484 */
6485 public Builder addMessage(String message) {
6486 mMessages.add(message);
6487 return this;
6488 }
6489
6490 /**
6491 * Sets the pending intent and remote input which will convey the reply to this
6492 * notification.
6493 *
6494 * @param pendingIntent The pending intent which will be triggered on a reply.
6495 * @param remoteInput The remote input parcelable which will carry the reply.
6496 * @return This object for method chaining.
6497 *
6498 * @see CarExtender.UnreadConversation#getRemoteInput
6499 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6500 */
6501 public Builder setReplyAction(
6502 PendingIntent pendingIntent, RemoteInput remoteInput) {
6503 mRemoteInput = remoteInput;
6504 mReplyPendingIntent = pendingIntent;
6505
6506 return this;
6507 }
6508
6509 /**
6510 * Sets the pending intent that will be sent once the messages in this notification
6511 * are read.
6512 *
6513 * @param pendingIntent The pending intent to use.
6514 * @return This object for method chaining.
6515 */
6516 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6517 mReadPendingIntent = pendingIntent;
6518 return this;
6519 }
6520
6521 /**
6522 * Sets the timestamp of the most recent message in an unread conversation.
6523 *
6524 * If a messaging notification has been posted by your application and has not
6525 * yet been cancelled, posting a later notification with the same id and tag
6526 * but without a newer timestamp may result in Android Auto not displaying a
6527 * heads up notification for the later notification.
6528 *
6529 * @param timestamp The timestamp of the most recent message in the conversation.
6530 * @return This object for method chaining.
6531 */
6532 public Builder setLatestTimestamp(long timestamp) {
6533 mLatestTimestamp = timestamp;
6534 return this;
6535 }
6536
6537 /**
6538 * Builds a new unread conversation object.
6539 *
6540 * @return The new unread conversation object.
6541 */
6542 public UnreadConversation build() {
6543 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6544 String[] participants = { mParticipant };
6545 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6546 mReadPendingIntent, participants, mLatestTimestamp);
6547 }
6548 }
6549 }
6550
6551 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006552 * Get an array of Notification objects from a parcelable array bundle field.
6553 * Update the bundle to have a typed array so fetches in the future don't need
6554 * to do an array copy.
6555 */
6556 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6557 Parcelable[] array = bundle.getParcelableArray(key);
6558 if (array instanceof Notification[] || array == null) {
6559 return (Notification[]) array;
6560 }
6561 Notification[] typedArray = Arrays.copyOf(array, array.length,
6562 Notification[].class);
6563 bundle.putParcelableArray(key, typedArray);
6564 return typedArray;
6565 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006566
6567 private static class BuilderRemoteViews extends RemoteViews {
6568 public BuilderRemoteViews(Parcel parcel) {
6569 super(parcel);
6570 }
6571
Kenny Guy77320062014-08-27 21:37:15 +01006572 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6573 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006574 }
6575
6576 @Override
6577 public BuilderRemoteViews clone() {
6578 Parcel p = Parcel.obtain();
6579 writeToParcel(p, 0);
6580 p.setDataPosition(0);
6581 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6582 p.recycle();
6583 return brv;
6584 }
6585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586}