blob: 1c96108b8807af2ce2781ea698c56e0d6d4763d7 [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;
Alex Hillsfd590442016-10-07 09:52:44 -040022import android.annotation.NonNull;
Daniel Sandler01df1c62014-06-09 10:54:01 -040023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040025import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010028import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040029import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020030import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020031import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040032import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070034import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010035import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010036import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040037import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040038import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070039import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080040import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070041import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040043import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020044import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcel;
47import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040048import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070049import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070050import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080051import android.text.SpannableStringBuilder;
52import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080054import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070055import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080056import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070057import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080058import android.text.style.RelativeSizeSpan;
59import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070060import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040061import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050062import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070063import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080064import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080065import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070066import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070067import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.widget.RemoteViews;
69
Griff Hazen959591e2014-05-15 22:26:18 -070070import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070071import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070072import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070073
Tor Norbyed9273d62013-05-30 15:59:53 -070074import java.lang.annotation.Retention;
75import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020076import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050077import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070078import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070079import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070080import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050081import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083/**
84 * A class that represents how a persistent notification is to be presented to
85 * the user using the {@link android.app.NotificationManager}.
86 *
Joe Onoratocb109a02011-01-18 17:57:41 -080087 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
88 * easier to construct Notifications.</p>
89 *
Joe Fernandez558459f2011-10-13 16:47:36 -070090 * <div class="special reference">
91 * <h3>Developer Guides</h3>
92 * <p>For a guide to creating notifications, read the
93 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
94 * developer guide.</p>
95 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 */
97public class Notification implements Parcelable
98{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040099 private static final String TAG = "Notification";
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400102 * An activity that provides a user interface for adjusting notification preferences for its
103 * containing application. Optional but recommended for apps that post
104 * {@link android.app.Notification Notifications}.
105 */
106 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
107 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
108 = "android.intent.category.NOTIFICATION_PREFERENCES";
109
110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * Use all default values (where applicable).
112 */
113 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 /**
116 * Use the default notification sound. This will ignore any given
117 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118 *
Chris Wren47c20a12014-06-18 17:27:29 -0400119 * <p>
120 * A notification that is noisy is more likely to be presented as a heads-up notification.
121 * </p>
122 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124 */
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public static final int DEFAULT_SOUND = 1;
127
128 /**
129 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700131 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132 *
Chris Wren47c20a12014-06-18 17:27:29 -0400133 * <p>
134 * A notification that vibrates is more likely to be presented as a heads-up notification.
135 * </p>
136 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500138 */
139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /**
143 * Use the default notification lights. This will ignore the
144 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
145 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500148 */
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200153 * Maximum length of CharSequences accepted by Builder and friends.
154 *
155 * <p>
156 * Avoids spamming the system with overly large strings such as full e-mails.
157 */
158 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
159
160 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800161 * Maximum entries of reply text that are accepted by Builder and friends.
162 */
163 private static final int MAX_REPLY_HISTORY = 5;
164
165 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800167 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * Default value: {@link System#currentTimeMillis() Now}.
169 *
170 * Choose a timestamp that will be most relevant to the user. For most finite events, this
171 * corresponds to the time the event happened (or will happen, in the case of events that have
172 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800173 * timestamped according to when the activity began.
174 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800176 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500177 * <ul>
178 * <li>Notification of a new chat message should be stamped when the message was received.</li>
179 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
180 * <li>Notification of a completed file download should be stamped when the download finished.</li>
181 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
182 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
183 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800184 * </ul>
185 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700186 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
187 * anymore by default and must be opted into by using
188 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 */
190 public long when;
191
192 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700193 * The creation time of the notification
194 */
195 private long creationTime;
196
197 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400199 *
200 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 */
Dan Sandler86647982015-05-13 23:41:13 -0400202 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700203 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public int icon;
205
206 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800207 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
208 * leave it at its default value of 0.
209 *
210 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700211 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800212 */
213 public int iconLevel;
214
215 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500216 * The number of events that this notification represents. For example, in a new mail
217 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800218 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500219 * The system may or may not use this field to modify the appearance of the notification. For
220 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
221 * superimposed over the icon in the status bar. Starting with
222 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
223 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700226 *
227 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700229 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 public int number;
231
232 /**
233 * The intent to execute when the expanded status entry is clicked. If
234 * this is an activity, it must include the
235 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800236 * that you take care of task management as described in the
237 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800238 * Stack</a> document. In particular, make sure to read the notification section
239 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
240 * Notifications</a> for the correct ways to launch an application from a
241 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 */
243 public PendingIntent contentIntent;
244
245 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500246 * The intent to execute when the notification is explicitly dismissed by the user, either with
247 * the "Clear All" button or by swiping it away individually.
248 *
249 * This probably shouldn't be launching an activity since several of those will be sent
250 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
252 public PendingIntent deleteIntent;
253
254 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700255 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800256 *
Chris Wren47c20a12014-06-18 17:27:29 -0400257 * <p>
258 * The system UI may choose to display a heads-up notification, instead of
259 * launching this intent, while the user is using the device.
260 * </p>
261 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800262 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400263 */
264 public PendingIntent fullScreenIntent;
265
266 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400267 * Text that summarizes this notification for accessibility services.
268 *
269 * As of the L release, this text is no longer shown on screen, but it is still useful to
270 * accessibility services (where it serves as an audible announcement of the notification's
271 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400272 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800273 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 */
275 public CharSequence tickerText;
276
277 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400278 * Formerly, a view showing the {@link #tickerText}.
279 *
280 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400281 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400282 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800283 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400284
285 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400286 * The view that will represent this notification in the notification list (which is pulled
287 * down from the status bar).
288 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500289 * As of N, this field may be null. The notification view is determined by the inputs
290 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400293 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 public RemoteViews contentView;
295
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400296 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400297 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400298 * opportunity to show more detail. The system UI may choose to show this
299 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400300 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500301 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400302 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
303 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400304 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400305 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400306 public RemoteViews bigContentView;
307
Chris Wren8fd39ec2014-02-27 17:43:26 -0500308
309 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400310 * A medium-format version of {@link #contentView}, providing the Notification an
311 * opportunity to add action buttons to contentView. At its discretion, the system UI may
312 * choose to show this as a heads-up notification, which will pop up so the user can see
313 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500315 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400316 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
317 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500318 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400319 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500320 public RemoteViews headsUpContentView;
321
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400322 /**
Dan Sandler86647982015-05-13 23:41:13 -0400323 * A large bitmap to be shown in the notification content area.
324 *
325 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
Dan Sandler86647982015-05-13 23:41:13 -0400327 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800328 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
330 /**
331 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500332 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400334 * A notification that is noisy is more likely to be presented as a heads-up notification.
335 * </p>
336 *
337 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500338 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * </p>
340 */
341 public Uri sound;
342
343 /**
344 * Use this constant as the value for audioStreamType to request that
345 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700346 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400347 *
348 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700350 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 public static final int STREAM_DEFAULT = -1;
352
353 /**
354 * The audio stream type to use when playing the sound.
355 * Should be one of the STREAM_ constants from
356 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400357 *
358 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700360 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 public int audioStreamType = STREAM_DEFAULT;
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400364 * The default value of {@link #audioAttributes}.
365 */
366 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
367 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
368 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
369 .build();
370
371 /**
372 * The {@link AudioAttributes audio attributes} to use when playing the sound.
373 */
374 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
375
376 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500377 * The pattern with which to vibrate.
378 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 * <p>
380 * To vibrate the default pattern, see {@link #defaults}.
381 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500382 *
Chris Wren47c20a12014-06-18 17:27:29 -0400383 * <p>
384 * A notification that vibrates is more likely to be presented as a heads-up notification.
385 * </p>
386 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * @see android.os.Vibrator#vibrate(long[],int)
388 */
389 public long[] vibrate;
390
391 /**
392 * The color of the led. The hardware will do its best approximation.
393 *
394 * @see #FLAG_SHOW_LIGHTS
395 * @see #flags
396 */
Tor Norbye80756e32015-03-02 09:39:27 -0800397 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 public int ledARGB;
399
400 /**
401 * The number of milliseconds for the LED to be on while it's flashing.
402 * The hardware will do its best approximation.
403 *
404 * @see #FLAG_SHOW_LIGHTS
405 * @see #flags
406 */
407 public int ledOnMS;
408
409 /**
410 * The number of milliseconds for the LED to be off while it's flashing.
411 * The hardware will do its best approximation.
412 *
413 * @see #FLAG_SHOW_LIGHTS
414 * @see #flags
415 */
416 public int ledOffMS;
417
418 /**
419 * Specifies which values should be taken from the defaults.
420 * <p>
421 * To set, OR the desired from {@link #DEFAULT_SOUND},
422 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
423 * values, use {@link #DEFAULT_ALL}.
424 * </p>
425 */
426 public int defaults;
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 /**
429 * Bit to be bitwise-ored into the {@link #flags} field that should be
430 * set if you want the LED on for this notification.
431 * <ul>
432 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
433 * or 0 for both ledOnMS and ledOffMS.</li>
434 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
435 * <li>To flash the LED, pass the number of milliseconds that it should
436 * be on and off to ledOnMS and ledOffMS.</li>
437 * </ul>
438 * <p>
439 * Since hardware varies, you are not guaranteed that any of the values
440 * you pass are honored exactly. Use the system defaults (TODO) if possible
441 * because they will be set to values that work on any given hardware.
442 * <p>
443 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
446 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
447
448 /**
449 * Bit to be bitwise-ored into the {@link #flags} field that should be
450 * set if this notification is in reference to something that is ongoing,
451 * like a phone call. It should not be set if this notification is in
452 * reference to something that happened at a particular point in time,
453 * like a missed phone call.
454 */
455 public static final int FLAG_ONGOING_EVENT = 0x00000002;
456
457 /**
458 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700459 * the audio will be repeated until the notification is
460 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 */
462 public static final int FLAG_INSISTENT = 0x00000004;
463
464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700466 * set if you would only like the sound, vibrate and ticker to be played
467 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 */
469 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
470
471 /**
472 * Bit to be bitwise-ored into the {@link #flags} field that should be
473 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500474 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 */
476 public static final int FLAG_AUTO_CANCEL = 0x00000010;
477
478 /**
479 * Bit to be bitwise-ored into the {@link #flags} field that should be
480 * set if the notification should not be canceled when the user clicks
481 * the Clear all button.
482 */
483 public static final int FLAG_NO_CLEAR = 0x00000020;
484
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700485 /**
486 * Bit to be bitwise-ored into the {@link #flags} field that should be
487 * set if this notification represents a currently running service. This
488 * will normally be set for you by {@link Service#startForeground}.
489 */
490 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
491
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400492 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500493 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800494 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500495 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400496 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700497 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500498 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400499
Griff Hazendfcb0802014-02-11 12:00:00 -0800500 /**
501 * Bit to be bitswise-ored into the {@link #flags} field that should be
502 * set if this notification is relevant to the current device only
503 * and it is not recommended that it bridge to other devices.
504 */
505 public static final int FLAG_LOCAL_ONLY = 0x00000100;
506
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700507 /**
508 * Bit to be bitswise-ored into the {@link #flags} field that should be
509 * set if this notification is the group summary for a group of notifications.
510 * Grouped notifications may display in a cluster or stack on devices which
511 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
512 */
513 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
514
Julia Reynoldse46bb372016-03-17 11:05:58 -0400515 /**
516 * Bit to be bitswise-ored into the {@link #flags} field that should be
517 * set if this notification is the group summary for an auto-group of notifications.
518 *
519 * @hide
520 */
521 @SystemApi
522 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 public int flags;
525
Tor Norbyed9273d62013-05-30 15:59:53 -0700526 /** @hide */
527 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
528 @Retention(RetentionPolicy.SOURCE)
529 public @interface Priority {}
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500532 * Default notification {@link #priority}. If your application does not prioritize its own
533 * notifications, use this value for all notifications.
534 */
535 public static final int PRIORITY_DEFAULT = 0;
536
537 /**
538 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
539 * items smaller, or at a different position in the list, compared with your app's
540 * {@link #PRIORITY_DEFAULT} items.
541 */
542 public static final int PRIORITY_LOW = -1;
543
544 /**
545 * Lowest {@link #priority}; these items might not be shown to the user except under special
546 * circumstances, such as detailed notification logs.
547 */
548 public static final int PRIORITY_MIN = -2;
549
550 /**
551 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
552 * show these items larger, or at a different position in notification lists, compared with
553 * your app's {@link #PRIORITY_DEFAULT} items.
554 */
555 public static final int PRIORITY_HIGH = 1;
556
557 /**
558 * Highest {@link #priority}, for your application's most important items that require the
559 * user's prompt attention or input.
560 */
561 public static final int PRIORITY_MAX = 2;
562
563 /**
564 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800565 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500566 * Priority is an indication of how much of the user's valuable attention should be consumed by
567 * this notification. Low-priority notifications may be hidden from the user in certain
568 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500569 * system will make a determination about how to interpret this priority when presenting
570 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400571 *
572 * <p>
573 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
574 * as a heads-up notification.
575 * </p>
576 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700578 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800580
Dan Sandler26e81cf2014-05-06 10:01:27 -0400581 /**
582 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
583 * to be applied by the standard Style templates when presenting this notification.
584 *
585 * The current template design constructs a colorful header image by overlaying the
586 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
587 * ignored.
588 */
Tor Norbye80756e32015-03-02 09:39:27 -0800589 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400590 public int color = COLOR_DEFAULT;
591
592 /**
593 * Special value of {@link #color} telling the system not to decorate this notification with
594 * any special color but instead use default colors when presenting this notification.
595 */
Tor Norbye80756e32015-03-02 09:39:27 -0800596 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400597 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600598
599 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800600 * Special value of {@link #color} used as a place holder for an invalid color.
601 */
602 @ColorInt
603 private static final int COLOR_INVALID = 1;
604
605 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700606 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
607 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600608 * lockscreen).
609 *
610 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
611 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
612 * shown in all situations, but the contents are only available if the device is unlocked for
613 * the appropriate user.
614 *
615 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
616 * can be read even in an "insecure" context (that is, above a secure lockscreen).
617 * To modify the public version of this notification—for example, to redact some portions—see
618 * {@link Builder#setPublicVersion(Notification)}.
619 *
620 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
621 * and ticker until the user has bypassed the lockscreen.
622 */
623 public int visibility;
624
Griff Hazenfc3922d2014-08-20 11:56:44 -0700625 /**
626 * Notification visibility: Show this notification in its entirety on all lockscreens.
627 *
628 * {@see #visibility}
629 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600630 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700631
632 /**
633 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
634 * private information on secure lockscreens.
635 *
636 * {@see #visibility}
637 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600638 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700639
640 /**
641 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
642 *
643 * {@see #visibility}
644 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600645 public static final int VISIBILITY_SECRET = -1;
646
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400648 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651
652 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400653 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400655 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656
657 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400658 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661
662 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400663 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400665 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666
667 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400668 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400670 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671
672 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400673 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500674 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400675 public static final String CATEGORY_ALARM = "alarm";
676
677 /**
678 * Notification category: progress of a long-running background operation.
679 */
680 public static final String CATEGORY_PROGRESS = "progress";
681
682 /**
683 * Notification category: social network or sharing update.
684 */
685 public static final String CATEGORY_SOCIAL = "social";
686
687 /**
688 * Notification category: error in background operation or authentication status.
689 */
690 public static final String CATEGORY_ERROR = "err";
691
692 /**
693 * Notification category: media transport control for playback.
694 */
695 public static final String CATEGORY_TRANSPORT = "transport";
696
697 /**
698 * Notification category: system or device status update. Reserved for system use.
699 */
700 public static final String CATEGORY_SYSTEM = "sys";
701
702 /**
703 * Notification category: indication of running background service.
704 */
705 public static final String CATEGORY_SERVICE = "service";
706
707 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400708 * Notification category: a specific, timely recommendation for a single thing.
709 * For example, a news app might want to recommend a news story it believes the user will
710 * want to read next.
711 */
712 public static final String CATEGORY_RECOMMENDATION = "recommendation";
713
714 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400715 * Notification category: ongoing information about device or contextual status.
716 */
717 public static final String CATEGORY_STATUS = "status";
718
719 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400720 * Notification category: user-scheduled reminder.
721 */
722 public static final String CATEGORY_REMINDER = "reminder";
723
724 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400725 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
726 * that best describes this Notification. May be used by the system for ranking and filtering.
727 */
728 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700730 private String mGroupKey;
731
732 /**
733 * Get the key used to group this notification into a cluster or stack
734 * with other notifications on devices which support such rendering.
735 */
736 public String getGroup() {
737 return mGroupKey;
738 }
739
740 private String mSortKey;
741
742 /**
743 * Get a sort key that orders this notification among other notifications from the
744 * same package. This can be useful if an external sort was already applied and an app
745 * would like to preserve this. Notifications will be sorted lexicographically using this
746 * value, although providing different priorities in addition to providing sort key may
747 * cause this value to be ignored.
748 *
749 * <p>This sort key can also be used to order members of a notification group. See
750 * {@link Builder#setGroup}.
751 *
752 * @see String#compareTo(String)
753 */
754 public String getSortKey() {
755 return mSortKey;
756 }
757
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500758 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400759 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400760 * <p>
761 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
762 * APIs, and are intended to be used by
763 * {@link android.service.notification.NotificationListenerService} implementations to extract
764 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500765 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400766 public Bundle extras = new Bundle();
767
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400768 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700769 * All pending intents in the notification as the system needs to be able to access them but
770 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700771 * custom parcelable objects.
772 *
773 * @hide
774 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700775 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700776
777 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400778 * {@link #extras} key: this is the title of the notification,
779 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
780 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500781 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400782
783 /**
784 * {@link #extras} key: this is the title of the notification when shown in expanded form,
785 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
786 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400787 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400788
789 /**
790 * {@link #extras} key: this is the main text payload, as supplied to
791 * {@link Builder#setContentText(CharSequence)}.
792 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500793 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400794
795 /**
796 * {@link #extras} key: this is a third line of text, as supplied to
797 * {@link Builder#setSubText(CharSequence)}.
798 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400799 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400800
801 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800802 * {@link #extras} key: this is the remote input history, as supplied to
803 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700804 *
805 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
806 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
807 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
808 * notifications once the other party has responded).
809 *
810 * The extra with this key is of type CharSequence[] and contains the most recent entry at
811 * the 0 index, the second most recent at the 1 index, etc.
812 *
813 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800814 */
815 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
816
817 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400818 * {@link #extras} key: this is a small piece of additional text as supplied to
819 * {@link Builder#setContentInfo(CharSequence)}.
820 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400821 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400822
823 /**
824 * {@link #extras} key: this is a line of summary information intended to be shown
825 * alongside expanded notifications, as supplied to (e.g.)
826 * {@link BigTextStyle#setSummaryText(CharSequence)}.
827 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400828 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400829
830 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200831 * {@link #extras} key: this is the longer text shown in the big form of a
832 * {@link BigTextStyle} notification, as supplied to
833 * {@link BigTextStyle#bigText(CharSequence)}.
834 */
835 public static final String EXTRA_BIG_TEXT = "android.bigText";
836
837 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
839 * supplied to {@link Builder#setSmallIcon(int)}.
840 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500841 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400842
843 /**
844 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
845 * notification payload, as
846 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
847 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400848 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400849
850 /**
851 * {@link #extras} key: this is a bitmap to be used instead of the one from
852 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
853 * shown in its expanded form, as supplied to
854 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
855 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400856 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400857
858 /**
859 * {@link #extras} key: this is the progress value supplied to
860 * {@link Builder#setProgress(int, int, boolean)}.
861 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400862 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400863
864 /**
865 * {@link #extras} key: this is the maximum value supplied to
866 * {@link Builder#setProgress(int, int, boolean)}.
867 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400868 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400869
870 /**
871 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
872 * {@link Builder#setProgress(int, int, boolean)}.
873 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400874 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400875
876 /**
877 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
878 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
879 * {@link Builder#setUsesChronometer(boolean)}.
880 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400881 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400882
883 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800884 * {@link #extras} key: whether the chronometer set on the notification should count down
885 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700886 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800887 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700888 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800889
890 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400891 * {@link #extras} key: whether {@link #when} should be shown,
892 * as supplied to {@link Builder#setShowWhen(boolean)}.
893 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400894 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400895
896 /**
897 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
898 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
899 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400900 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400901
902 /**
903 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
904 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
905 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400906 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400907
908 /**
909 * {@link #extras} key: A string representing the name of the specific
910 * {@link android.app.Notification.Style} used to create this notification.
911 */
Chris Wren91ad5632013-06-05 15:05:57 -0400912 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400913
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400914 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400915 * {@link #extras} key: A String array containing the people that this notification relates to,
916 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400918 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500919
920 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400921 * Allow certain system-generated notifications to appear before the device is provisioned.
922 * Only available to notifications coming from the android package.
923 * @hide
924 */
925 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
926
927 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700928 * {@link #extras} key: A
929 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
930 * in the background when the notification is selected. The URI must point to an image stream
931 * suitable for passing into
932 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
933 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
934 * URI used for this purpose must require no permissions to read the image data.
935 */
936 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
937
938 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400939 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700940 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400941 * {@link android.app.Notification.MediaStyle} notification.
942 */
943 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
944
945 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100946 * {@link #extras} key: the indices of actions to be shown in the compact view,
947 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
948 */
949 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
950
Christoph Studer943aa672014-08-03 20:31:16 +0200951 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400952 * {@link #extras} key: the username to be displayed for all messages sent by the user including
953 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700954 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
955 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400956 */
957 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
958
959 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700960 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400961 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400962 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400963 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400964
965 /**
966 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
967 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700968 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
969 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400970 */
971 public static final String EXTRA_MESSAGES = "android.messages";
972
973 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100974 * {@link #extras} key: the user that built the notification.
975 *
976 * @hide
977 */
978 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
979
980 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400981 * @hide
982 */
983 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
984
Selim Cinek247fa012016-02-18 09:50:48 -0800985 /**
986 * @hide
987 */
988 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
989
Dan Sandler80eaa592016-04-14 23:34:54 -0400990 /** @hide */
991 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400992 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
993
Dan Sandlerd63f9322015-05-06 15:18:49 -0400994 private Icon mSmallIcon;
995 private Icon mLargeIcon;
996
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400997 private String mChannelId;
998
Chris Wren51c75102013-07-16 20:49:17 -0400999 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001000 * Structure to encapsulate a named action that can be shown as part of this notification.
1001 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1002 * selected by the user.
1003 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001004 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1005 * or {@link Notification.Builder#addAction(Notification.Action)}
1006 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001007 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001008 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -07001009 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001010 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001011 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001012 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001013
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001014 /**
1015 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001016 *
1017 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001018 */
Dan Sandler86647982015-05-13 23:41:13 -04001019 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001021
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 /**
1023 * Title of the action.
1024 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001025 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001026
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001027 /**
1028 * Intent to send when the user invokes this action. May be null, in which case the action
1029 * may be rendered in a disabled presentation by the system UI.
1030 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001031 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001032
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001033 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001034 if (in.readInt() != 0) {
1035 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001036 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1037 icon = mIcon.getResId();
1038 }
Dan Sandler86647982015-05-13 23:41:13 -04001039 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001040 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1041 if (in.readInt() == 1) {
1042 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1043 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001044 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001045 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001046 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001047 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001048
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001049 /**
Dan Sandler86647982015-05-13 23:41:13 -04001050 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001051 */
Dan Sandler86647982015-05-13 23:41:13 -04001052 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001053 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001054 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001055 }
1056
Adrian Roos7af53622016-10-12 13:44:05 -07001057 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001058 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001059 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001060 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001061 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1062 this.icon = icon.getResId();
1063 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001064 this.title = title;
1065 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001066 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001067 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001068 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001069 }
1070
1071 /**
Dan Sandler86647982015-05-13 23:41:13 -04001072 * Return an icon representing the action.
1073 */
1074 public Icon getIcon() {
1075 if (mIcon == null && icon != 0) {
1076 // you snuck an icon in here without using the builder; let's try to keep it
1077 mIcon = Icon.createWithResource("", icon);
1078 }
1079 return mIcon;
1080 }
1081
1082 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001083 * Get additional metadata carried around with this Action.
1084 */
1085 public Bundle getExtras() {
1086 return mExtras;
1087 }
1088
1089 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001090 * Return whether the platform should automatically generate possible replies for this
1091 * {@link Action}
1092 */
1093 public boolean getAllowGeneratedReplies() {
1094 return mAllowGeneratedReplies;
1095 }
1096
1097 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001098 * Get the list of inputs to be collected from the user when this action is sent.
1099 * May return null if no remote inputs were added.
1100 */
1101 public RemoteInput[] getRemoteInputs() {
1102 return mRemoteInputs;
1103 }
1104
1105 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001106 * Builder class for {@link Action} objects.
1107 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001108 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001109 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001110 private final CharSequence mTitle;
1111 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001112 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001113 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001114 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001115
1116 /**
1117 * Construct a new builder for {@link Action} object.
1118 * @param icon icon to show for this action
1119 * @param title the title of the action
1120 * @param intent the {@link PendingIntent} to fire when users trigger this action
1121 */
Dan Sandler86647982015-05-13 23:41:13 -04001122 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001123 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001124 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001125 }
1126
1127 /**
1128 * Construct a new builder for {@link Action} object.
1129 * @param icon icon to show for this action
1130 * @param title the title of the action
1131 * @param intent the {@link PendingIntent} to fire when users trigger this action
1132 */
1133 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001134 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001135 }
1136
1137 /**
1138 * Construct a new builder for {@link Action} object using the fields from an
1139 * {@link Action}.
1140 * @param action the action to read fields from.
1141 */
1142 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001143 this(action.getIcon(), action.title, action.actionIntent,
1144 new Bundle(action.mExtras), action.getRemoteInputs(),
1145 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001146 }
1147
Dan Sandler86647982015-05-13 23:41:13 -04001148 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001149 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001150 mIcon = icon;
1151 mTitle = title;
1152 mIntent = intent;
1153 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001154 if (remoteInputs != null) {
1155 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1156 Collections.addAll(mRemoteInputs, remoteInputs);
1157 }
Adrian Roos7af53622016-10-12 13:44:05 -07001158 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001159 }
1160
1161 /**
1162 * Merge additional metadata into this builder.
1163 *
1164 * <p>Values within the Bundle will replace existing extras values in this Builder.
1165 *
1166 * @see Notification.Action#extras
1167 */
1168 public Builder addExtras(Bundle extras) {
1169 if (extras != null) {
1170 mExtras.putAll(extras);
1171 }
1172 return this;
1173 }
1174
1175 /**
1176 * Get the metadata Bundle used by this Builder.
1177 *
1178 * <p>The returned Bundle is shared with this Builder.
1179 */
1180 public Bundle getExtras() {
1181 return mExtras;
1182 }
1183
1184 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001185 * Add an input to be collected from the user when this action is sent.
1186 * Response values can be retrieved from the fired intent by using the
1187 * {@link RemoteInput#getResultsFromIntent} function.
1188 * @param remoteInput a {@link RemoteInput} to add to the action
1189 * @return this object for method chaining
1190 */
1191 public Builder addRemoteInput(RemoteInput remoteInput) {
1192 if (mRemoteInputs == null) {
1193 mRemoteInputs = new ArrayList<RemoteInput>();
1194 }
1195 mRemoteInputs.add(remoteInput);
1196 return this;
1197 }
1198
1199 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001200 * Set whether the platform should automatically generate possible replies to add to
1201 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1202 * {@link RemoteInput}, this has no effect.
1203 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1204 * otherwise
1205 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001206 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001207 */
1208 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1209 mAllowGeneratedReplies = allowGeneratedReplies;
1210 return this;
1211 }
1212
1213 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001214 * Apply an extender to this action builder. Extenders may be used to add
1215 * metadata or change options on this builder.
1216 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001217 public Builder extend(Extender extender) {
1218 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001219 return this;
1220 }
1221
1222 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001223 * Combine all of the options that have been set and return a new {@link Action}
1224 * object.
1225 * @return the built action
1226 */
1227 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001228 RemoteInput[] remoteInputs = mRemoteInputs != null
1229 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001230 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1231 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001232 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001233 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001234
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001235 @Override
1236 public Action clone() {
1237 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001238 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001239 title,
1240 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001241 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001242 getRemoteInputs(),
1243 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001244 }
1245 @Override
1246 public int describeContents() {
1247 return 0;
1248 }
1249 @Override
1250 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001251 final Icon ic = getIcon();
1252 if (ic != null) {
1253 out.writeInt(1);
1254 ic.writeToParcel(out, 0);
1255 } else {
1256 out.writeInt(0);
1257 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001258 TextUtils.writeToParcel(title, out, flags);
1259 if (actionIntent != null) {
1260 out.writeInt(1);
1261 actionIntent.writeToParcel(out, flags);
1262 } else {
1263 out.writeInt(0);
1264 }
Griff Hazen959591e2014-05-15 22:26:18 -07001265 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001266 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001267 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001268 }
Griff Hazen959591e2014-05-15 22:26:18 -07001269 public static final Parcelable.Creator<Action> CREATOR =
1270 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001271 public Action createFromParcel(Parcel in) {
1272 return new Action(in);
1273 }
1274 public Action[] newArray(int size) {
1275 return new Action[size];
1276 }
1277 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001278
1279 /**
1280 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1281 * metadata or change options on an action builder.
1282 */
1283 public interface Extender {
1284 /**
1285 * Apply this extender to a notification action builder.
1286 * @param builder the builder to be modified.
1287 * @return the build object for chaining.
1288 */
1289 public Builder extend(Builder builder);
1290 }
1291
1292 /**
1293 * Wearable extender for notification actions. To add extensions to an action,
1294 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1295 * the {@code WearableExtender()} constructor and apply it to a
1296 * {@link android.app.Notification.Action.Builder} using
1297 * {@link android.app.Notification.Action.Builder#extend}.
1298 *
1299 * <pre class="prettyprint">
1300 * Notification.Action action = new Notification.Action.Builder(
1301 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001302 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001303 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001304 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001305 */
1306 public static final class WearableExtender implements Extender {
1307 /** Notification action extra which contains wearable extensions */
1308 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1309
Pete Gastaf6781d2014-10-07 15:17:05 -04001310 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001311 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001312 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1313 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1314 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001315
1316 // Flags bitwise-ored to mFlags
1317 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001318 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001319 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001320
1321 // Default value for flags integer
1322 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1323
1324 private int mFlags = DEFAULT_FLAGS;
1325
Pete Gastaf6781d2014-10-07 15:17:05 -04001326 private CharSequence mInProgressLabel;
1327 private CharSequence mConfirmLabel;
1328 private CharSequence mCancelLabel;
1329
Griff Hazen61a9e862014-05-22 16:05:19 -07001330 /**
1331 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1332 * options.
1333 */
1334 public WearableExtender() {
1335 }
1336
1337 /**
1338 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1339 * wearable options present in an existing notification action.
1340 * @param action the notification action to inspect.
1341 */
1342 public WearableExtender(Action action) {
1343 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1344 if (wearableBundle != null) {
1345 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001346 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1347 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1348 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001349 }
1350 }
1351
1352 /**
1353 * Apply wearable extensions to a notification action that is being built. This is
1354 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1355 * method of {@link android.app.Notification.Action.Builder}.
1356 */
1357 @Override
1358 public Action.Builder extend(Action.Builder builder) {
1359 Bundle wearableBundle = new Bundle();
1360
1361 if (mFlags != DEFAULT_FLAGS) {
1362 wearableBundle.putInt(KEY_FLAGS, mFlags);
1363 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001364 if (mInProgressLabel != null) {
1365 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1366 }
1367 if (mConfirmLabel != null) {
1368 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1369 }
1370 if (mCancelLabel != null) {
1371 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1372 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001373
1374 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1375 return builder;
1376 }
1377
1378 @Override
1379 public WearableExtender clone() {
1380 WearableExtender that = new WearableExtender();
1381 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001382 that.mInProgressLabel = this.mInProgressLabel;
1383 that.mConfirmLabel = this.mConfirmLabel;
1384 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001385 return that;
1386 }
1387
1388 /**
1389 * Set whether this action is available when the wearable device is not connected to
1390 * a companion device. The user can still trigger this action when the wearable device is
1391 * offline, but a visual hint will indicate that the action may not be available.
1392 * Defaults to true.
1393 */
1394 public WearableExtender setAvailableOffline(boolean availableOffline) {
1395 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1396 return this;
1397 }
1398
1399 /**
1400 * Get whether this action is available when the wearable device is not connected to
1401 * a companion device. The user can still trigger this action when the wearable device is
1402 * offline, but a visual hint will indicate that the action may not be available.
1403 * Defaults to true.
1404 */
1405 public boolean isAvailableOffline() {
1406 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1407 }
1408
1409 private void setFlag(int mask, boolean value) {
1410 if (value) {
1411 mFlags |= mask;
1412 } else {
1413 mFlags &= ~mask;
1414 }
1415 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001416
1417 /**
1418 * Set a label to display while the wearable is preparing to automatically execute the
1419 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1420 *
1421 * @param label the label to display while the action is being prepared to execute
1422 * @return this object for method chaining
1423 */
1424 public WearableExtender setInProgressLabel(CharSequence label) {
1425 mInProgressLabel = label;
1426 return this;
1427 }
1428
1429 /**
1430 * Get the label to display while the wearable is preparing to automatically execute
1431 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1432 *
1433 * @return the label to display while the action is being prepared to execute
1434 */
1435 public CharSequence getInProgressLabel() {
1436 return mInProgressLabel;
1437 }
1438
1439 /**
1440 * Set a label to display to confirm that the action should be executed.
1441 * This is usually an imperative verb like "Send".
1442 *
1443 * @param label the label to confirm the action should be executed
1444 * @return this object for method chaining
1445 */
1446 public WearableExtender setConfirmLabel(CharSequence label) {
1447 mConfirmLabel = label;
1448 return this;
1449 }
1450
1451 /**
1452 * Get the label to display to confirm that the action should be executed.
1453 * This is usually an imperative verb like "Send".
1454 *
1455 * @return the label to confirm the action should be executed
1456 */
1457 public CharSequence getConfirmLabel() {
1458 return mConfirmLabel;
1459 }
1460
1461 /**
1462 * Set a label to display to cancel the action.
1463 * This is usually an imperative verb, like "Cancel".
1464 *
1465 * @param label the label to display to cancel the action
1466 * @return this object for method chaining
1467 */
1468 public WearableExtender setCancelLabel(CharSequence label) {
1469 mCancelLabel = label;
1470 return this;
1471 }
1472
1473 /**
1474 * Get the label to display to cancel the action.
1475 * This is usually an imperative verb like "Cancel".
1476 *
1477 * @return the label to display to cancel the action
1478 */
1479 public CharSequence getCancelLabel() {
1480 return mCancelLabel;
1481 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001482
1483 /**
1484 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1485 * platform that it can generate the appropriate transitions.
1486 * @param hintLaunchesActivity {@code true} if the content intent will launch
1487 * an activity and transitions should be generated, false otherwise.
1488 * @return this object for method chaining
1489 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001490 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001491 boolean hintLaunchesActivity) {
1492 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1493 return this;
1494 }
1495
1496 /**
1497 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1498 * platform that it can generate the appropriate transitions
1499 * @return {@code true} if the content intent will launch an activity and transitions
1500 * should be generated, false otherwise. The default value is {@code false} if this was
1501 * never set.
1502 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001503 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001504 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1505 }
Alex Hills9f087612016-06-07 09:08:59 -04001506
1507 /**
1508 * Set a hint that this Action should be displayed inline.
1509 *
1510 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1511 * otherwise
1512 * @return this object for method chaining
1513 */
1514 public WearableExtender setHintDisplayActionInline(
1515 boolean hintDisplayInline) {
1516 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1517 return this;
1518 }
1519
1520 /**
1521 * Get a hint that this Action should be displayed inline.
1522 *
1523 * @return {@code true} if the Action should be displayed inline, {@code false}
1524 * otherwise. The default value is {@code false} if this was never set.
1525 */
1526 public boolean getHintDisplayActionInline() {
1527 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1528 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001529 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001530 }
1531
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001532 /**
1533 * Array of all {@link Action} structures attached to this notification by
1534 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1535 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1536 * interface for invoking actions.
1537 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001538 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001539
1540 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001541 * Replacement version of this notification whose content will be shown
1542 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1543 * and {@link #VISIBILITY_PUBLIC}.
1544 */
1545 public Notification publicVersion;
1546
1547 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001548 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001549 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 */
1551 public Notification()
1552 {
1553 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001554 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001555 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 }
1557
1558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 * @hide
1560 */
1561 public Notification(Context context, int icon, CharSequence tickerText, long when,
1562 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1563 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001564 new Builder(context)
1565 .setWhen(when)
1566 .setSmallIcon(icon)
1567 .setTicker(tickerText)
1568 .setContentTitle(contentTitle)
1569 .setContentText(contentText)
1570 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1571 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573
1574 /**
1575 * Constructs a Notification object with the information needed to
1576 * have a status bar icon without the standard expanded view.
1577 *
1578 * @param icon The resource id of the icon to put in the status bar.
1579 * @param tickerText The text that flows by in the status bar when the notification first
1580 * activates.
1581 * @param when The time to show in the time field. In the System.currentTimeMillis
1582 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001583 *
1584 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001586 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 public Notification(int icon, CharSequence tickerText, long when)
1588 {
1589 this.icon = icon;
1590 this.tickerText = tickerText;
1591 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001592 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594
1595 /**
1596 * Unflatten the notification from a parcel.
1597 */
Svet Ganovddb94882016-06-23 19:55:24 -07001598 @SuppressWarnings("unchecked")
1599 public Notification(Parcel parcel) {
1600 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1601 // intents in extras are always written as the last entry.
1602 readFromParcelImpl(parcel);
1603 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001604 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001605 }
1606
1607 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 {
1609 int version = parcel.readInt();
1610
1611 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001612 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001613 if (parcel.readInt() != 0) {
1614 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001615 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1616 icon = mSmallIcon.getResId();
1617 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 number = parcel.readInt();
1620 if (parcel.readInt() != 0) {
1621 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1622 }
1623 if (parcel.readInt() != 0) {
1624 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1625 }
1626 if (parcel.readInt() != 0) {
1627 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1628 }
1629 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001630 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001631 }
1632 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1634 }
Joe Onorato561d3852010-11-20 18:09:34 -08001635 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001636 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 defaults = parcel.readInt();
1639 flags = parcel.readInt();
1640 if (parcel.readInt() != 0) {
1641 sound = Uri.CREATOR.createFromParcel(parcel);
1642 }
1643
1644 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001645 if (parcel.readInt() != 0) {
1646 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 vibrate = parcel.createLongArray();
1649 ledARGB = parcel.readInt();
1650 ledOnMS = parcel.readInt();
1651 ledOffMS = parcel.readInt();
1652 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001653
1654 if (parcel.readInt() != 0) {
1655 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1656 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001657
1658 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001659
John Spurlockfd7f1e02014-03-18 16:41:57 -04001660 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001661
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001662 mGroupKey = parcel.readString();
1663
1664 mSortKey = parcel.readString();
1665
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001666 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001667
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001668 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1669
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001670 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001671 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1672 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001673
Chris Wren8fd39ec2014-02-27 17:43:26 -05001674 if (parcel.readInt() != 0) {
1675 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1676 }
1677
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001678 visibility = parcel.readInt();
1679
1680 if (parcel.readInt() != 0) {
1681 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1682 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001683
1684 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001685
1686 if (parcel.readInt() != 0) {
1687 mChannelId = parcel.readString();
1688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
1690
Andy Stadler110988c2010-12-03 14:29:16 -08001691 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001692 public Notification clone() {
1693 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001694 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001695 return that;
1696 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001697
Daniel Sandler1a497d32013-04-18 14:52:45 -04001698 /**
1699 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1700 * of this into that.
1701 * @hide
1702 */
1703 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001704 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001705 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001706 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001707 that.number = this.number;
1708
1709 // PendingIntents are global, so there's no reason (or way) to clone them.
1710 that.contentIntent = this.contentIntent;
1711 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001712 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001713
1714 if (this.tickerText != null) {
1715 that.tickerText = this.tickerText.toString();
1716 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001717 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001718 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001719 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001720 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001721 that.contentView = this.contentView.clone();
1722 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001723 if (heavy && this.mLargeIcon != null) {
1724 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001725 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001726 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001727 that.sound = this.sound; // android.net.Uri is immutable
1728 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001729 if (this.audioAttributes != null) {
1730 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1731 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001732
1733 final long[] vibrate = this.vibrate;
1734 if (vibrate != null) {
1735 final int N = vibrate.length;
1736 final long[] vib = that.vibrate = new long[N];
1737 System.arraycopy(vibrate, 0, vib, 0, N);
1738 }
1739
1740 that.ledARGB = this.ledARGB;
1741 that.ledOnMS = this.ledOnMS;
1742 that.ledOffMS = this.ledOffMS;
1743 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001744
Joe Onorato18e69df2010-05-17 22:26:12 -07001745 that.flags = this.flags;
1746
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001747 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001748
John Spurlockfd7f1e02014-03-18 16:41:57 -04001749 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001750
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001751 that.mGroupKey = this.mGroupKey;
1752
1753 that.mSortKey = this.mSortKey;
1754
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001755 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001756 try {
1757 that.extras = new Bundle(this.extras);
1758 // will unparcel
1759 that.extras.size();
1760 } catch (BadParcelableException e) {
1761 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1762 that.extras = null;
1763 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001764 }
1765
Felipe Lemedd85da62016-06-28 11:29:54 -07001766 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1767 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001768 }
1769
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001770 if (this.actions != null) {
1771 that.actions = new Action[this.actions.length];
1772 for(int i=0; i<this.actions.length; i++) {
1773 that.actions[i] = this.actions[i].clone();
1774 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001775 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001776
Daniel Sandler1a497d32013-04-18 14:52:45 -04001777 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001778 that.bigContentView = this.bigContentView.clone();
1779 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001780
Chris Wren8fd39ec2014-02-27 17:43:26 -05001781 if (heavy && this.headsUpContentView != null) {
1782 that.headsUpContentView = this.headsUpContentView.clone();
1783 }
1784
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001785 that.visibility = this.visibility;
1786
1787 if (this.publicVersion != null) {
1788 that.publicVersion = new Notification();
1789 this.publicVersion.cloneInto(that.publicVersion, heavy);
1790 }
1791
Dan Sandler26e81cf2014-05-06 10:01:27 -04001792 that.color = this.color;
1793
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001794 that.mChannelId = this.mChannelId;
1795
Daniel Sandler1a497d32013-04-18 14:52:45 -04001796 if (!heavy) {
1797 that.lightenPayload(); // will clean out extras
1798 }
1799 }
1800
1801 /**
1802 * Removes heavyweight parts of the Notification object for archival or for sending to
1803 * listeners when the full contents are not necessary.
1804 * @hide
1805 */
1806 public final void lightenPayload() {
1807 tickerView = null;
1808 contentView = null;
1809 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001810 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001811 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001812 if (extras != null && !extras.isEmpty()) {
1813 final Set<String> keyset = extras.keySet();
1814 final int N = keyset.size();
1815 final String[] keys = keyset.toArray(new String[N]);
1816 for (int i=0; i<N; i++) {
1817 final String key = keys[i];
1818 final Object obj = extras.get(key);
1819 if (obj != null &&
1820 ( obj instanceof Parcelable
1821 || obj instanceof Parcelable[]
1822 || obj instanceof SparseArray
1823 || obj instanceof ArrayList)) {
1824 extras.remove(key);
1825 }
1826 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001827 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001828 }
1829
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001830 /**
1831 * Make sure this CharSequence is safe to put into a bundle, which basically
1832 * means it had better not be some custom Parcelable implementation.
1833 * @hide
1834 */
1835 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001836 if (cs == null) return cs;
1837 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1838 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1839 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001840 if (cs instanceof Parcelable) {
1841 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1842 + " instance is a custom Parcelable and not allowed in Notification");
1843 return cs.toString();
1844 }
Selim Cinek60a54252016-02-26 17:03:25 -08001845 return removeTextSizeSpans(cs);
1846 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001847
Selim Cinek60a54252016-02-26 17:03:25 -08001848 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1849 if (charSequence instanceof Spanned) {
1850 Spanned ss = (Spanned) charSequence;
1851 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1852 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1853 for (Object span : spans) {
1854 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001855 if (resultSpan instanceof CharacterStyle) {
1856 resultSpan = ((CharacterStyle) span).getUnderlying();
1857 }
1858 if (resultSpan instanceof TextAppearanceSpan) {
1859 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001860 resultSpan = new TextAppearanceSpan(
1861 originalSpan.getFamily(),
1862 originalSpan.getTextStyle(),
1863 -1,
1864 originalSpan.getTextColor(),
1865 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001866 } else if (resultSpan instanceof RelativeSizeSpan
1867 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001868 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001869 } else {
1870 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001871 }
1872 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1873 ss.getSpanFlags(span));
1874 }
1875 return builder;
1876 }
1877 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001878 }
1879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 public int describeContents() {
1881 return 0;
1882 }
1883
1884 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001885 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 */
Svet Ganovddb94882016-06-23 19:55:24 -07001887 public void writeToParcel(Parcel parcel, int flags) {
1888 // We need to mark all pending intents getting into the notification
1889 // system as being put there to later allow the notification ranker
1890 // to launch them and by doing so add the app to the battery saver white
1891 // list for a short period of time. The problem is that the system
1892 // cannot look into the extras as there may be parcelables there that
1893 // the platform does not know how to handle. To go around that we have
1894 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001895 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001896 if (collectPendingIntents) {
1897 PendingIntent.setOnMarshaledListener(
1898 (PendingIntent intent, Parcel out, int outFlags) -> {
1899 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001900 if (allPendingIntents == null) {
1901 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001902 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001903 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001904 }
1905 });
1906 }
1907 try {
1908 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1909 // want to intercept all pending events written to the pacel.
1910 writeToParcelImpl(parcel, flags);
1911 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001912 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001913 } finally {
1914 if (collectPendingIntents) {
1915 PendingIntent.setOnMarshaledListener(null);
1916 }
1917 }
1918 }
1919
1920 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 parcel.writeInt(1);
1922
1923 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001924 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001925 if (mSmallIcon == null && icon != 0) {
1926 // you snuck an icon in here without using the builder; let's try to keep it
1927 mSmallIcon = Icon.createWithResource("", icon);
1928 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001929 if (mSmallIcon != null) {
1930 parcel.writeInt(1);
1931 mSmallIcon.writeToParcel(parcel, 0);
1932 } else {
1933 parcel.writeInt(0);
1934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 parcel.writeInt(number);
1936 if (contentIntent != null) {
1937 parcel.writeInt(1);
1938 contentIntent.writeToParcel(parcel, 0);
1939 } else {
1940 parcel.writeInt(0);
1941 }
1942 if (deleteIntent != null) {
1943 parcel.writeInt(1);
1944 deleteIntent.writeToParcel(parcel, 0);
1945 } else {
1946 parcel.writeInt(0);
1947 }
1948 if (tickerText != null) {
1949 parcel.writeInt(1);
1950 TextUtils.writeToParcel(tickerText, parcel, flags);
1951 } else {
1952 parcel.writeInt(0);
1953 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001954 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001955 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001956 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001957 } else {
1958 parcel.writeInt(0);
1959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 if (contentView != null) {
1961 parcel.writeInt(1);
1962 contentView.writeToParcel(parcel, 0);
1963 } else {
1964 parcel.writeInt(0);
1965 }
Selim Cinek279fa862016-06-14 10:57:25 -07001966 if (mLargeIcon == null && largeIcon != null) {
1967 // you snuck an icon in here without using the builder; let's try to keep it
1968 mLargeIcon = Icon.createWithBitmap(largeIcon);
1969 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001970 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001971 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001972 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001973 } else {
1974 parcel.writeInt(0);
1975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976
1977 parcel.writeInt(defaults);
1978 parcel.writeInt(this.flags);
1979
1980 if (sound != null) {
1981 parcel.writeInt(1);
1982 sound.writeToParcel(parcel, 0);
1983 } else {
1984 parcel.writeInt(0);
1985 }
1986 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001987
1988 if (audioAttributes != null) {
1989 parcel.writeInt(1);
1990 audioAttributes.writeToParcel(parcel, 0);
1991 } else {
1992 parcel.writeInt(0);
1993 }
1994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 parcel.writeLongArray(vibrate);
1996 parcel.writeInt(ledARGB);
1997 parcel.writeInt(ledOnMS);
1998 parcel.writeInt(ledOffMS);
1999 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002000
2001 if (fullScreenIntent != null) {
2002 parcel.writeInt(1);
2003 fullScreenIntent.writeToParcel(parcel, 0);
2004 } else {
2005 parcel.writeInt(0);
2006 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002007
2008 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002009
John Spurlockfd7f1e02014-03-18 16:41:57 -04002010 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002011
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002012 parcel.writeString(mGroupKey);
2013
2014 parcel.writeString(mSortKey);
2015
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002016 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002017
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002018 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002019
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002020 if (bigContentView != null) {
2021 parcel.writeInt(1);
2022 bigContentView.writeToParcel(parcel, 0);
2023 } else {
2024 parcel.writeInt(0);
2025 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002026
Chris Wren8fd39ec2014-02-27 17:43:26 -05002027 if (headsUpContentView != null) {
2028 parcel.writeInt(1);
2029 headsUpContentView.writeToParcel(parcel, 0);
2030 } else {
2031 parcel.writeInt(0);
2032 }
2033
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002034 parcel.writeInt(visibility);
2035
2036 if (publicVersion != null) {
2037 parcel.writeInt(1);
2038 publicVersion.writeToParcel(parcel, 0);
2039 } else {
2040 parcel.writeInt(0);
2041 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002042
2043 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002044
2045 if (mChannelId != null) {
2046 parcel.writeInt(1);
2047 parcel.writeString(mChannelId);
2048 } else {
2049 parcel.writeInt(0);
2050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
2052
2053 /**
2054 * Parcelable.Creator that instantiates Notification objects
2055 */
2056 public static final Parcelable.Creator<Notification> CREATOR
2057 = new Parcelable.Creator<Notification>()
2058 {
2059 public Notification createFromParcel(Parcel parcel)
2060 {
2061 return new Notification(parcel);
2062 }
2063
2064 public Notification[] newArray(int size)
2065 {
2066 return new Notification[size];
2067 }
2068 };
2069
2070 /**
2071 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2072 * layout.
2073 *
2074 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2075 * in the view.</p>
2076 * @param context The context for your application / activity.
2077 * @param contentTitle The title that goes in the expanded entry.
2078 * @param contentText The text that goes in the expanded entry.
2079 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2080 * If this is an activity, it must include the
2081 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002082 * that you take care of task management as described in the
2083 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2084 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002085 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002086 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002087 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002089 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 public void setLatestEventInfo(Context context,
2091 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002092 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2093 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2094 new Throwable());
2095 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002096
Selim Cinek4ac6f602016-06-13 15:47:03 -07002097 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2098 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2099 }
2100
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002101 // ensure that any information already set directly is preserved
2102 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002103
2104 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002106 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
2108 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002109 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002111 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002112
2113 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115
Julia Reynoldsda303542015-11-23 14:00:20 -05002116 /**
2117 * @hide
2118 */
2119 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002120 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2121 }
2122
2123 /**
2124 * @hide
2125 */
2126 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2127 Notification notification) {
2128 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2129 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002130 }
2131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 @Override
2133 public String toString() {
2134 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002135 sb.append("Notification(pri=");
2136 sb.append(priority);
2137 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002138 if (contentView != null) {
2139 sb.append(contentView.getPackage());
2140 sb.append("/0x");
2141 sb.append(Integer.toHexString(contentView.getLayoutId()));
2142 } else {
2143 sb.append("null");
2144 }
2145 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002146 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2147 sb.append("default");
2148 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 int N = this.vibrate.length-1;
2150 sb.append("[");
2151 for (int i=0; i<N; i++) {
2152 sb.append(this.vibrate[i]);
2153 sb.append(',');
2154 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002155 if (N != -1) {
2156 sb.append(this.vibrate[N]);
2157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 } else {
2160 sb.append("null");
2161 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002162 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002163 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002165 } else if (this.sound != null) {
2166 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 } else {
2168 sb.append("null");
2169 }
Chris Wren365b6d32015-07-16 10:39:26 -04002170 if (this.tickerText != null) {
2171 sb.append(" tick");
2172 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002173 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002175 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002176 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002177 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002178 if (this.category != null) {
2179 sb.append(" category=");
2180 sb.append(this.category);
2181 }
2182 if (this.mGroupKey != null) {
2183 sb.append(" groupKey=");
2184 sb.append(this.mGroupKey);
2185 }
2186 if (this.mSortKey != null) {
2187 sb.append(" sortKey=");
2188 sb.append(this.mSortKey);
2189 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002190 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002191 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002192 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002193 }
2194 sb.append(" vis=");
2195 sb.append(visibilityToString(this.visibility));
2196 if (this.publicVersion != null) {
2197 sb.append(" publicVersion=");
2198 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002199 }
2200 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 return sb.toString();
2202 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002203
Dan Sandler1b718782014-07-18 12:43:45 -04002204 /**
2205 * {@hide}
2206 */
2207 public static String visibilityToString(int vis) {
2208 switch (vis) {
2209 case VISIBILITY_PRIVATE:
2210 return "PRIVATE";
2211 case VISIBILITY_PUBLIC:
2212 return "PUBLIC";
2213 case VISIBILITY_SECRET:
2214 return "SECRET";
2215 default:
2216 return "UNKNOWN(" + String.valueOf(vis) + ")";
2217 }
2218 }
2219
Joe Onoratocb109a02011-01-18 17:57:41 -08002220 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002221 * {@hide}
2222 */
2223 public static String priorityToString(@Priority int pri) {
2224 switch (pri) {
2225 case PRIORITY_MIN:
2226 return "MIN";
2227 case PRIORITY_LOW:
2228 return "LOW";
2229 case PRIORITY_DEFAULT:
2230 return "DEFAULT";
2231 case PRIORITY_HIGH:
2232 return "HIGH";
2233 case PRIORITY_MAX:
2234 return "MAX";
2235 default:
2236 return "UNKNOWN(" + String.valueOf(pri) + ")";
2237 }
2238 }
2239
2240 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002241 * Returns the id of the channel this notification posts to.
2242 */
Julia Reynolds37856052016-11-11 09:20:07 -05002243 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002244 return mChannelId;
2245 }
2246
2247 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002248 * The small icon representing this notification in the status bar and content view.
2249 *
2250 * @return the small icon representing this notification.
2251 *
2252 * @see Builder#getSmallIcon()
2253 * @see Builder#setSmallIcon(Icon)
2254 */
2255 public Icon getSmallIcon() {
2256 return mSmallIcon;
2257 }
2258
2259 /**
2260 * Used when notifying to clean up legacy small icons.
2261 * @hide
2262 */
2263 public void setSmallIcon(Icon icon) {
2264 mSmallIcon = icon;
2265 }
2266
2267 /**
2268 * The large icon shown in this notification's content view.
2269 * @see Builder#getLargeIcon()
2270 * @see Builder#setLargeIcon(Icon)
2271 */
2272 public Icon getLargeIcon() {
2273 return mLargeIcon;
2274 }
2275
2276 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002277 * @hide
2278 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002279 public boolean isGroupSummary() {
2280 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2281 }
2282
2283 /**
2284 * @hide
2285 */
2286 public boolean isGroupChild() {
2287 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2288 }
2289
2290 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002291 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002292 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002293 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002294 * content views using the platform's notification layout template. If your app supports
2295 * versions of Android as old as API level 4, you can instead use
2296 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2297 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2298 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002299 *
Scott Main183bf112012-08-13 19:12:13 -07002300 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002301 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002302 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002303 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002304 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2305 * .setContentText(subject)
2306 * .setSmallIcon(R.drawable.new_mail)
2307 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002308 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002309 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002310 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002311 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002312 /**
2313 * @hide
2314 */
2315 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2316 "android.rebuild.contentViewActionCount";
2317 /**
2318 * @hide
2319 */
2320 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2321 = "android.rebuild.bigViewActionCount";
2322 /**
2323 * @hide
2324 */
2325 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2326 = "android.rebuild.hudViewActionCount";
2327
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002328 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002329
Joe Onorato46439ce2010-11-19 13:56:21 -08002330 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002331 private Notification mN;
2332 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002333 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002334 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2335 private ArrayList<String> mPersonList = new ArrayList<String>();
2336 private NotificationColorUtil mColorUtil;
2337 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002338
2339 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002340 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2341 */
2342 private int mCachedContrastColor = COLOR_INVALID;
2343 private int mCachedContrastColorIsFor = COLOR_INVALID;
2344
2345 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002346 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002347 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002348
2349 * <table>
2350 * <tr><th align=right>priority</th>
2351 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2352 * <tr><th align=right>when</th>
2353 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2354 * <tr><th align=right>audio stream</th>
2355 * <td>{@link #STREAM_DEFAULT}</td></tr>
2356 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002357 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002358
2359 * @param context
2360 * A {@link Context} that will be used by the Builder to construct the
2361 * RemoteViews. The Context will not be held past the lifetime of this Builder
2362 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002363 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002364 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002365 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002366 }
2367
Joe Onoratocb109a02011-01-18 17:57:41 -08002368 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002369 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002370 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002371 public Builder(Context context, Notification toAdopt) {
2372 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002373
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002374 if (toAdopt == null) {
2375 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002376 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2377 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2378 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002379 mN.priority = PRIORITY_DEFAULT;
2380 mN.visibility = VISIBILITY_PRIVATE;
2381 } else {
2382 mN = toAdopt;
2383 if (mN.actions != null) {
2384 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002385 }
2386
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002387 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2388 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2389 }
2390
Selim Cinek4ac6f602016-06-13 15:47:03 -07002391 if (mN.getSmallIcon() == null && mN.icon != 0) {
2392 setSmallIcon(mN.icon);
2393 }
2394
2395 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2396 setLargeIcon(mN.largeIcon);
2397 }
2398
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002399 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2400 if (!TextUtils.isEmpty(templateClass)) {
2401 final Class<? extends Style> styleClass
2402 = getNotificationStyleClass(templateClass);
2403 if (styleClass == null) {
2404 Log.d(TAG, "Unknown style class: " + templateClass);
2405 } else {
2406 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002407 final Constructor<? extends Style> ctor =
2408 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002409 ctor.setAccessible(true);
2410 final Style style = ctor.newInstance();
2411 style.restoreFromExtras(mN.extras);
2412
2413 if (style != null) {
2414 setStyle(style);
2415 }
2416 } catch (Throwable t) {
2417 Log.e(TAG, "Could not create Style", t);
2418 }
2419 }
2420 }
2421
2422 }
2423 }
2424
2425 private NotificationColorUtil getColorUtil() {
2426 if (!mColorUtilInited) {
2427 mColorUtilInited = true;
2428 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2429 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002430 }
2431 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002432 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002433 }
2434
2435 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002436 * Specifies the channel the notification should be delivered on.
2437 */
2438 public Builder setChannel(String channelId) {
2439 mN.mChannelId = channelId;
2440 return this;
2441 }
2442
2443 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002444 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002445 *
2446 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2447 * shown anymore by default and must be opted into by using
2448 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002449 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002450 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002451 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002452 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002453 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002454 return this;
2455 }
2456
Joe Onoratocb109a02011-01-18 17:57:41 -08002457 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002458 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002459 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002460 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2461 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002462 */
2463 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002464 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002465 return this;
2466 }
2467
2468 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002469 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002470 *
2471 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002472 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002473 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002474 * Useful when showing an elapsed time (like an ongoing phone call).
2475 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002476 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002477 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002478 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002479 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002480 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002481 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002482 */
2483 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002484 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002485 return this;
2486 }
2487
2488 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002489 * Sets the Chronometer to count down instead of counting up.
2490 *
2491 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2492 * If it isn't set the chronometer will count up.
2493 *
2494 * @see #setUsesChronometer(boolean)
2495 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002496 public Builder setChronometerCountDown(boolean countDown) {
2497 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002498 return this;
2499 }
2500
2501 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002502 * Set the small icon resource, which will be used to represent the notification in the
2503 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002504 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002505
2506 * The platform template for the expanded view will draw this icon in the left, unless a
2507 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2508 * icon will be moved to the right-hand side.
2509 *
2510
2511 * @param icon
2512 * A resource ID in the application's package of the drawable to use.
2513 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002514 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002515 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002516 return setSmallIcon(icon != 0
2517 ? Icon.createWithResource(mContext, icon)
2518 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002519 }
2520
Joe Onoratocb109a02011-01-18 17:57:41 -08002521 /**
2522 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2523 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2524 * LevelListDrawable}.
2525 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002526 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002527 * @param level The level to use for the icon.
2528 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002529 * @see Notification#icon
2530 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002531 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002532 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002533 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002534 return setSmallIcon(icon);
2535 }
2536
2537 /**
2538 * Set the small icon, which will be used to represent the notification in the
2539 * status bar and content view (unless overriden there by a
2540 * {@link #setLargeIcon(Bitmap) large icon}).
2541 *
2542 * @param icon An Icon object to use.
2543 * @see Notification#icon
2544 */
2545 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002546 mN.setSmallIcon(icon);
2547 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2548 mN.icon = icon.getResId();
2549 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002550 return this;
2551 }
2552
Joe Onoratocb109a02011-01-18 17:57:41 -08002553 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002554 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002555 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002556 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002557 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002558 return this;
2559 }
2560
Joe Onoratocb109a02011-01-18 17:57:41 -08002561 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002562 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002563 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002564 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002565 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002566 return this;
2567 }
2568
Joe Onoratocb109a02011-01-18 17:57:41 -08002569 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002570 * This provides some additional information that is displayed in the notification. No
2571 * guarantees are given where exactly it is displayed.
2572 *
2573 * <p>This information should only be provided if it provides an essential
2574 * benefit to the understanding of the notification. The more text you provide the
2575 * less readable it becomes. For example, an email client should only provide the account
2576 * name here if more than one email account has been added.</p>
2577 *
2578 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2579 * notification header area.
2580 *
2581 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2582 * this will be shown in the third line of text in the platform notification template.
2583 * You should not be using {@link #setProgress(int, int, boolean)} at the
2584 * same time on those versions; they occupy the same place.
2585 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002586 */
2587 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002588 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002589 return this;
2590 }
2591
2592 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002593 * Set the remote input history.
2594 *
2595 * This should be set to the most recent inputs that have been sent
2596 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2597 * longer relevant (e.g. for chat notifications once the other party has responded).
2598 *
2599 * The most recent input must be stored at the 0 index, the second most recent at the
2600 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2601 * and how much of each individual input is shown.
2602 *
2603 * <p>Note: The reply text will only be shown on notifications that have least one action
2604 * with a {@code RemoteInput}.</p>
2605 */
2606 public Builder setRemoteInputHistory(CharSequence[] text) {
2607 if (text == null) {
2608 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2609 } else {
2610 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2611 CharSequence[] safe = new CharSequence[N];
2612 for (int i = 0; i < N; i++) {
2613 safe[i] = safeCharSequence(text[i]);
2614 }
2615 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2616 }
2617 return this;
2618 }
2619
2620 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002621 * Set the large number at the right-hand side of the notification. This is
2622 * equivalent to setContentInfo, although it might show the number in a different
2623 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002624 *
2625 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002626 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002627 @Deprecated
Joe Onorato8595a3d2010-11-19 18:12:07 -08002628 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002629 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002630 return this;
2631 }
2632
Joe Onoratocb109a02011-01-18 17:57:41 -08002633 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002634 * A small piece of additional information pertaining to this notification.
2635 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002636 * The platform template will draw this on the last line of the notification, at the far
2637 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002638 *
2639 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2640 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2641 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002642 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002643 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002644 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002645 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002646 return this;
2647 }
2648
Joe Onoratocb109a02011-01-18 17:57:41 -08002649 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002650 * Set the progress this notification represents.
2651 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002652 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002653 */
2654 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002655 mN.extras.putInt(EXTRA_PROGRESS, progress);
2656 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2657 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002658 return this;
2659 }
2660
2661 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002662 * Supply a custom RemoteViews to use instead of the platform template.
2663 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002664 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002665 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002666 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002667 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002668 return setCustomContentView(views);
2669 }
2670
2671 /**
2672 * Supply custom RemoteViews to use instead of the platform template.
2673 *
2674 * This will override the layout that would otherwise be constructed by this Builder
2675 * object.
2676 */
2677 public Builder setCustomContentView(RemoteViews contentView) {
2678 mN.contentView = contentView;
2679 return this;
2680 }
2681
2682 /**
2683 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2684 *
2685 * This will override the expanded layout that would otherwise be constructed by this
2686 * Builder object.
2687 */
2688 public Builder setCustomBigContentView(RemoteViews contentView) {
2689 mN.bigContentView = contentView;
2690 return this;
2691 }
2692
2693 /**
2694 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2695 *
2696 * This will override the heads-up layout that would otherwise be constructed by this
2697 * Builder object.
2698 */
2699 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2700 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002701 return this;
2702 }
2703
Joe Onoratocb109a02011-01-18 17:57:41 -08002704 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002705 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2706 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002707 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2708 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2709 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002710 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002711 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002712 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002713 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002714 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002715 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002716 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002717 return this;
2718 }
2719
Joe Onoratocb109a02011-01-18 17:57:41 -08002720 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002721 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2722 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002723 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002724 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002725 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002726 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002727 return this;
2728 }
2729
Joe Onoratocb109a02011-01-18 17:57:41 -08002730 /**
2731 * An intent to launch instead of posting the notification to the status bar.
2732 * Only for use with extremely high-priority notifications demanding the user's
2733 * <strong>immediate</strong> attention, such as an incoming phone call or
2734 * alarm clock that the user has explicitly set to a particular time.
2735 * If this facility is used for something else, please give the user an option
2736 * to turn it off and use a normal notification, as this can be extremely
2737 * disruptive.
2738 *
Chris Wren47c20a12014-06-18 17:27:29 -04002739 * <p>
2740 * The system UI may choose to display a heads-up notification, instead of
2741 * launching this intent, while the user is using the device.
2742 * </p>
2743 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002744 * @param intent The pending intent to launch.
2745 * @param highPriority Passing true will cause this notification to be sent
2746 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002747 *
2748 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002749 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002750 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002751 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002752 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2753 return this;
2754 }
2755
Joe Onoratocb109a02011-01-18 17:57:41 -08002756 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002757 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002758 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002759 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002760 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002761 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002762 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002763 return this;
2764 }
2765
Joe Onoratocb109a02011-01-18 17:57:41 -08002766 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002767 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002768 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002769 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002770 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002771 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002772 setTicker(tickerText);
2773 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002774 return this;
2775 }
2776
Joe Onoratocb109a02011-01-18 17:57:41 -08002777 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002778 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002779 *
2780 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002781 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2782 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002783 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002784 public Builder setLargeIcon(Bitmap b) {
2785 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2786 }
2787
2788 /**
2789 * Add a large icon to the notification content view.
2790 *
2791 * In the platform template, this image will be shown on the left of the notification view
2792 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2793 * badge atop the large icon).
2794 */
2795 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002796 mN.mLargeIcon = icon;
2797 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002798 return this;
2799 }
2800
Joe Onoratocb109a02011-01-18 17:57:41 -08002801 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002802 * Set the sound to play.
2803 *
John Spurlockc0650f022014-07-19 13:22:39 -04002804 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2805 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002806 *
2807 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002808 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002809 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002810 mN.sound = sound;
2811 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002812 return this;
2813 }
2814
Joe Onoratocb109a02011-01-18 17:57:41 -08002815 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002816 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002817 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002818 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2819 *
John Spurlockc0650f022014-07-19 13:22:39 -04002820 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002821 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002822 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002823 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002824 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08002825 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002826 mN.sound = sound;
2827 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002828 return this;
2829 }
2830
Joe Onoratocb109a02011-01-18 17:57:41 -08002831 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002832 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2833 * use during playback.
2834 *
John Spurlockc0650f022014-07-19 13:22:39 -04002835 * @see Notification#sound
2836 */
2837 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002838 mN.sound = sound;
2839 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002840 return this;
2841 }
2842
2843 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002844 * Set the vibration pattern to use.
2845 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002846 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2847 * <code>pattern</code> parameter.
2848 *
Chris Wren47c20a12014-06-18 17:27:29 -04002849 * <p>
2850 * A notification that vibrates is more likely to be presented as a heads-up notification.
2851 * </p>
2852 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002853 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002854 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002855 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002856 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002857 return this;
2858 }
2859
Joe Onoratocb109a02011-01-18 17:57:41 -08002860 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002861 * Set the desired color for the indicator LED on the device, as well as the
2862 * blink duty cycle (specified in milliseconds).
2863 *
2864
2865 * Not all devices will honor all (or even any) of these values.
2866 *
2867
2868 * @see Notification#ledARGB
2869 * @see Notification#ledOnMS
2870 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002871 */
Tor Norbye80756e32015-03-02 09:39:27 -08002872 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002873 mN.ledARGB = argb;
2874 mN.ledOnMS = onMs;
2875 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002876 if (onMs != 0 || offMs != 0) {
2877 mN.flags |= FLAG_SHOW_LIGHTS;
2878 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002879 return this;
2880 }
2881
Joe Onoratocb109a02011-01-18 17:57:41 -08002882 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002883 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002884 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002885
2886 * Ongoing notifications cannot be dismissed by the user, so your application or service
2887 * must take care of canceling them.
2888 *
2889
2890 * They are typically used to indicate a background task that the user is actively engaged
2891 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2892 * (e.g., a file download, sync operation, active network connection).
2893 *
2894
2895 * @see Notification#FLAG_ONGOING_EVENT
2896 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002897 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002898 public Builder setOngoing(boolean ongoing) {
2899 setFlag(FLAG_ONGOING_EVENT, ongoing);
2900 return this;
2901 }
2902
Joe Onoratocb109a02011-01-18 17:57:41 -08002903 /**
2904 * Set this flag if you would only like the sound, vibrate
2905 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002906 *
2907 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002908 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002909 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2910 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2911 return this;
2912 }
2913
Joe Onoratocb109a02011-01-18 17:57:41 -08002914 /**
Julia Reynolds04499532016-09-13 14:04:53 -04002915 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002916 *
2917 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002918 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002919 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002920 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002921 return this;
2922 }
2923
Joe Onoratocb109a02011-01-18 17:57:41 -08002924 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002925 * Set whether or not this notification should not bridge to other devices.
2926 *
2927 * <p>Some notifications can be bridged to other devices for remote display.
2928 * This hint can be set to recommend this notification not be bridged.
2929 */
2930 public Builder setLocalOnly(boolean localOnly) {
2931 setFlag(FLAG_LOCAL_ONLY, localOnly);
2932 return this;
2933 }
2934
2935 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002936 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002937 * <p>
2938 * The value should be one or more of the following fields combined with
2939 * bitwise-or:
2940 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2941 * <p>
2942 * For all default values, use {@link #DEFAULT_ALL}.
2943 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002944 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002945 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002946 return this;
2947 }
2948
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002949 /**
2950 * Set the priority of this notification.
2951 *
2952 * @see Notification#priority
2953 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002954 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002955 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002956 return this;
2957 }
Joe Malin8d40d042012-11-05 11:36:40 -08002958
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002959 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002960 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002961 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002962 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002963 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002964 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002965 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002966 return this;
2967 }
2968
2969 /**
Chris Wrendde75302014-03-26 17:24:15 -04002970 * Add a person that is relevant to this notification.
2971 *
Chris Wrene6c48932014-09-29 17:19:27 -04002972 * <P>
2973 * Depending on user preferences, this annotation may allow the notification to pass
2974 * through interruption filters, and to appear more prominently in the user interface.
2975 * </P>
2976 *
2977 * <P>
2978 * The person should be specified by the {@code String} representation of a
2979 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2980 * </P>
2981 *
2982 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2983 * URIs. The path part of these URIs must exist in the contacts database, in the
2984 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2985 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2986 * </P>
2987 *
2988 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002989 * @see Notification#EXTRA_PEOPLE
2990 */
Chris Wrene6c48932014-09-29 17:19:27 -04002991 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002992 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002993 return this;
2994 }
2995
2996 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002997 * Set this notification to be part of a group of notifications sharing the same key.
2998 * Grouped notifications may display in a cluster or stack on devices which
2999 * support such rendering.
3000 *
3001 * <p>To make this notification the summary for its group, also call
3002 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3003 * {@link #setSortKey}.
3004 * @param groupKey The group key of the group.
3005 * @return this object for method chaining
3006 */
3007 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003008 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003009 return this;
3010 }
3011
3012 /**
3013 * Set this notification to be the group summary for a group of notifications.
3014 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003015 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3016 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003017 * @param isGroupSummary Whether this notification should be a group summary.
3018 * @return this object for method chaining
3019 */
3020 public Builder setGroupSummary(boolean isGroupSummary) {
3021 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3022 return this;
3023 }
3024
3025 /**
3026 * Set a sort key that orders this notification among other notifications from the
3027 * same package. This can be useful if an external sort was already applied and an app
3028 * would like to preserve this. Notifications will be sorted lexicographically using this
3029 * value, although providing different priorities in addition to providing sort key may
3030 * cause this value to be ignored.
3031 *
3032 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003033 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003034 *
3035 * @see String#compareTo(String)
3036 */
3037 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003038 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003039 return this;
3040 }
3041
3042 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003043 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003044 *
Griff Hazen720042b2014-02-24 15:46:56 -08003045 * <p>Values within the Bundle will replace existing extras values in this Builder.
3046 *
3047 * @see Notification#extras
3048 */
Griff Hazen959591e2014-05-15 22:26:18 -07003049 public Builder addExtras(Bundle extras) {
3050 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003051 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003052 }
3053 return this;
3054 }
3055
3056 /**
3057 * Set metadata for this notification.
3058 *
3059 * <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 -04003060 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003061 * called.
3062 *
Griff Hazen720042b2014-02-24 15:46:56 -08003063 * <p>Replaces any existing extras values with those from the provided Bundle.
3064 * Use {@link #addExtras} to merge in metadata instead.
3065 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003066 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003067 */
Griff Hazen959591e2014-05-15 22:26:18 -07003068 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003069 if (extras != null) {
3070 mUserExtras = extras;
3071 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003072 return this;
3073 }
3074
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003075 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003076 * Get the current metadata Bundle used by this notification Builder.
3077 *
3078 * <p>The returned Bundle is shared with this Builder.
3079 *
3080 * <p>The current contents of this Bundle are copied into the Notification each time
3081 * {@link #build()} is called.
3082 *
3083 * @see Notification#extras
3084 */
3085 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003086 return mUserExtras;
3087 }
3088
3089 private Bundle getAllExtras() {
3090 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3091 saveExtras.putAll(mN.extras);
3092 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003093 }
3094
3095 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003096 * Add an action to this notification. Actions are typically displayed by
3097 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003098 * <p>
3099 * Every action must have an icon (32dp square and matching the
3100 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3101 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3102 * <p>
3103 * A notification in its expanded form can display up to 3 actions, from left to right in
3104 * the order they were added. Actions will not be displayed when the notification is
3105 * collapsed, however, so be sure that any essential functions may be accessed by the user
3106 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003107 *
3108 * @param icon Resource ID of a drawable that represents the action.
3109 * @param title Text describing the action.
3110 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003111 *
3112 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003113 */
Dan Sandler86647982015-05-13 23:41:13 -04003114 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003115 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003116 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003117 return this;
3118 }
3119
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003120 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003121 * Add an action to this notification. Actions are typically displayed by
3122 * the system as a button adjacent to the notification content.
3123 * <p>
3124 * Every action must have an icon (32dp square and matching the
3125 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3126 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3127 * <p>
3128 * A notification in its expanded form can display up to 3 actions, from left to right in
3129 * the order they were added. Actions will not be displayed when the notification is
3130 * collapsed, however, so be sure that any essential functions may be accessed by the user
3131 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3132 *
3133 * @param action The action to add.
3134 */
3135 public Builder addAction(Action action) {
3136 mActions.add(action);
3137 return this;
3138 }
3139
3140 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003141 * Alter the complete list of actions attached to this notification.
3142 * @see #addAction(Action).
3143 *
3144 * @param actions
3145 * @return
3146 */
3147 public Builder setActions(Action... actions) {
3148 mActions.clear();
3149 for (int i = 0; i < actions.length; i++) {
3150 mActions.add(actions[i]);
3151 }
3152 return this;
3153 }
3154
3155 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003156 * Add a rich notification style to be applied at build time.
3157 *
3158 * @param style Object responsible for modifying the notification style.
3159 */
3160 public Builder setStyle(Style style) {
3161 if (mStyle != style) {
3162 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003163 if (mStyle != null) {
3164 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003165 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3166 } else {
3167 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003168 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003169 }
3170 return this;
3171 }
3172
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003173 /**
3174 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003175 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003176 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3177 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3178 *
3179 * @return The same Builder.
3180 */
3181 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003182 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003183 return this;
3184 }
3185
3186 /**
3187 * Supply a replacement Notification whose contents should be shown in insecure contexts
3188 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3189 * @param n A replacement notification, presumably with some or all info redacted.
3190 * @return The same Builder.
3191 */
3192 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003193 if (n != null) {
3194 mN.publicVersion = new Notification();
3195 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3196 } else {
3197 mN.publicVersion = null;
3198 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003199 return this;
3200 }
3201
Griff Hazenb720abe2014-05-20 13:15:30 -07003202 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003203 * Apply an extender to this notification builder. Extenders may be used to add
3204 * metadata or change options on this builder.
3205 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003206 public Builder extend(Extender extender) {
3207 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003208 return this;
3209 }
3210
Dan Sandler4e787062015-06-17 15:09:48 -04003211 /**
3212 * @hide
3213 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003214 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003215 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003216 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003217 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003218 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003219 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003220 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003221 }
3222
Dan Sandler26e81cf2014-05-06 10:01:27 -04003223 /**
3224 * Sets {@link Notification#color}.
3225 *
3226 * @param argb The accent color to use
3227 *
3228 * @return The same Builder.
3229 */
Tor Norbye80756e32015-03-02 09:39:27 -08003230 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003231 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003232 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003233 return this;
3234 }
3235
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003236 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003237 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3238 // This user can never be a badged profile,
3239 // and also includes USER_ALL system notifications.
3240 return null;
3241 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003242 // Note: This assumes that the current user can read the profile badge of the
3243 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003244 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003245 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003246 }
3247
3248 private Bitmap getProfileBadge() {
3249 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003250 if (badge == null) {
3251 return null;
3252 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003253 final int size = mContext.getResources().getDimensionPixelSize(
3254 R.dimen.notification_badge_size);
3255 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003256 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003257 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003258 badge.draw(canvas);
3259 return bitmap;
3260 }
3261
Selim Cinekc848c3a2016-01-13 15:27:30 -08003262 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003263 Bitmap profileBadge = getProfileBadge();
3264
Kenny Guy98193ea2014-07-24 19:54:37 +01003265 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003266 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3267 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003268 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003269 }
3270
Christoph Studerfe718432014-09-01 18:21:18 +02003271 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003272 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003273 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003274 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003275 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003276 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003277 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003278 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003279 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003280 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003281 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003282 }
3283
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003284 /**
3285 * Resets the notification header to its original state
3286 */
3287 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003288 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3289 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003290 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003291 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003292 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003293 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003294 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003295 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003296 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003297 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003298 contentView.setImageViewIcon(R.id.profile_badge, null);
3299 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003300 }
3301
3302 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003303 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3304 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003305 }
3306
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003307 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003308 return applyStandardTemplate(resId, true /* hasProgress */);
3309 }
3310
3311 /**
3312 * @param hasProgress whether the progress bar should be shown and set
3313 */
3314 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003315 final Bundle ex = mN.extras;
3316
3317 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3318 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3319 return applyStandardTemplate(resId, hasProgress, title, text);
3320 }
3321
3322 /**
3323 * @param hasProgress whether the progress bar should be shown and set
3324 */
3325 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3326 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003327 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003328
Christoph Studerfe718432014-09-01 18:21:18 +02003329 resetStandardTemplate(contentView);
3330
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003331 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003332
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003333 bindNotificationHeader(contentView);
3334 bindLargeIcon(contentView);
Selim Cinek954cc232016-05-20 13:29:23 -07003335 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003336 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003337 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003338 contentView.setTextViewText(R.id.title, title);
Selim Cinek954cc232016-05-20 13:29:23 -07003339 contentView.setViewLayoutWidth(R.id.title, showProgress
3340 ? ViewGroup.LayoutParams.WRAP_CONTENT
3341 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003342 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07003343 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003344 int textId = showProgress ? com.android.internal.R.id.text_line_1
3345 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003346 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003347 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003348 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003349
Selim Cinek279fa862016-06-14 10:57:25 -07003350 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003351
Selim Cinek29603462015-11-17 19:04:39 -08003352 return contentView;
3353 }
3354
Selim Cinek860b6da2015-12-16 19:02:19 -08003355 /**
3356 * @param remoteView the remote view to update the minheight in
3357 * @param hasMinHeight does it have a mimHeight
3358 * @hide
3359 */
3360 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3361 int minHeight = 0;
3362 if (hasMinHeight) {
3363 // we need to set the minHeight of the notification
3364 minHeight = mContext.getResources().getDimensionPixelSize(
3365 com.android.internal.R.dimen.notification_min_content_height);
3366 }
3367 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3368 }
3369
Selim Cinek29603462015-11-17 19:04:39 -08003370 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003371 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3372 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3373 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3374 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003375 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003376 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003377 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003378 contentView.setProgressBackgroundTintList(
3379 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3380 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003381 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003382 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003383 contentView.setProgressTintList(R.id.progress, colorStateList);
3384 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003385 }
Selim Cinek29603462015-11-17 19:04:39 -08003386 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003387 } else {
3388 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003389 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003390 }
Joe Onorato561d3852010-11-20 18:09:34 -08003391 }
3392
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003393 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003394 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3395 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3396 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003397 if (mN.mLargeIcon != null) {
3398 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3399 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3400 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003401 int endMargin = R.dimen.notification_content_picture_margin;
3402 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3403 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3404 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003405 }
3406 }
3407
3408 private void bindNotificationHeader(RemoteViews contentView) {
3409 bindSmallIcon(contentView);
3410 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003411 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003412 bindHeaderChronometerAndTime(contentView);
3413 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003414 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003415 }
3416
3417 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003418 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003419 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003420 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003421 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003422 }
3423
3424 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3425 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003426 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003427 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3428 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3429 contentView.setLong(R.id.chronometer, "setBase",
3430 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3431 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003432 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003433 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003434 } else {
3435 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3436 contentView.setLong(R.id.time, "setTime", mN.when);
3437 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003438 } else {
3439 // We still want a time to be set but gone, such that we can show and hide it
3440 // on demand in case it's a child notification without anything in the header
3441 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003442 }
3443 }
3444
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003445 private void bindHeaderText(RemoteViews contentView) {
3446 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3447 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003448 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003449 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003450 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003451 if (headerText == null
3452 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3453 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3454 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3455 }
3456 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003457 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003458 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3459 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3460 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003461 }
3462 }
3463
Adrian Rooseba05822016-04-22 17:09:27 -07003464 /**
3465 * @hide
3466 */
3467 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003468 CharSequence name = null;
3469 final PackageManager pm = mContext.getPackageManager();
3470 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3471 // only system packages which lump together a bunch of unrelated stuff
3472 // may substitute a different name to make the purpose of the
3473 // notification more clear. the correct package label should always
3474 // be accessible via SystemUI.
3475 final String pkg = mContext.getPackageName();
3476 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3477 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3478 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3479 name = subName;
3480 } else {
3481 Log.w(TAG, "warning: pkg "
3482 + pkg + " attempting to substitute app name '" + subName
3483 + "' without holding perm "
3484 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3485 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003486 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003487 if (TextUtils.isEmpty(name)) {
3488 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3489 }
3490 if (TextUtils.isEmpty(name)) {
3491 // still nothing?
3492 return null;
3493 }
3494
3495 return String.valueOf(name);
3496 }
3497 private void bindHeaderAppName(RemoteViews contentView) {
3498 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003499 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003500 }
3501
3502 private void bindSmallIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003503 if (mN.mSmallIcon == null && mN.icon != 0) {
3504 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3505 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003506 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003507 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3508 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003509 processSmallIconColor(mN.mSmallIcon, contentView);
3510 }
3511
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003512 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003513 * @return true if the built notification will show the time or the chronometer; false
3514 * otherwise
3515 */
3516 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003517 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003518 }
3519
Christoph Studerfe718432014-09-01 18:21:18 +02003520 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003521 // actions_container is only reset when there are no actions to avoid focus issues with
3522 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003523 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003524 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003525
3526 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3527 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3528
3529 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3530 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3531 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3532 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003533
3534 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003535 }
3536
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003537 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003538 final Bundle ex = mN.extras;
3539
3540 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3541 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3542 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3543 }
3544
3545 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3546 CharSequence title, CharSequence text) {
3547 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003548
Christoph Studerfe718432014-09-01 18:21:18 +02003549 resetStandardTemplateWithActions(big);
3550
Adrian Roose458aa82015-12-08 16:17:19 -08003551 boolean validRemoteInput = false;
3552
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003553 int N = mActions.size();
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003554 boolean emphazisedMode = mN.fullScreenIntent != null;
3555 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003556 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003557 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003558 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roosf852a422016-06-03 13:33:43 -07003559 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3560 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003561 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003562 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003563 Action action = mActions.get(i);
3564 validRemoteInput |= hasValidRemoteInput(action);
3565
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003566 final RemoteViews button = generateActionButton(action, emphazisedMode,
3567 i % 2 != 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003568 big.addView(R.id.actions, button);
3569 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003570 } else {
3571 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003572 }
Adrian Roose458aa82015-12-08 16:17:19 -08003573
3574 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3575 if (validRemoteInput && replyText != null
3576 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3577 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3578 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3579
3580 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3581 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3582 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3583
3584 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3585 big.setViewVisibility(
3586 R.id.notification_material_reply_text_3, View.VISIBLE);
3587 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3588 }
3589 }
3590 }
3591
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003592 return big;
3593 }
3594
Adrian Roose458aa82015-12-08 16:17:19 -08003595 private boolean hasValidRemoteInput(Action action) {
3596 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3597 // Weird actions
3598 return false;
3599 }
3600
3601 RemoteInput[] remoteInputs = action.getRemoteInputs();
3602 if (remoteInputs == null) {
3603 return false;
3604 }
3605
3606 for (RemoteInput r : remoteInputs) {
3607 CharSequence[] choices = r.getChoices();
3608 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3609 return true;
3610 }
3611 }
3612 return false;
3613 }
3614
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003615 /**
3616 * Construct a RemoteViews for the final 1U notification layout. In order:
3617 * 1. Custom contentView from the caller
3618 * 2. Style's proposed content view
3619 * 3. Standard template view
3620 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003621 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003622 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003623 return mN.contentView;
3624 } else if (mStyle != null) {
3625 final RemoteViews styleView = mStyle.makeContentView();
3626 if (styleView != null) {
3627 return styleView;
3628 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003629 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003630 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003631 }
3632
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003633 /**
3634 * Construct a RemoteViews for the final big notification layout.
3635 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003636 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003637 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003638 if (mN.bigContentView != null
3639 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003640 return mN.bigContentView;
3641 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003642 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003643 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003644 } else if (mActions.size() != 0) {
3645 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003646 }
3647 adaptNotificationHeaderForBigContentView(result);
3648 return result;
3649 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003650
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003651 /**
3652 * Construct a RemoteViews for the final notification header only
3653 *
3654 * @hide
3655 */
3656 public RemoteViews makeNotificationHeader() {
3657 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3658 R.layout.notification_template_header);
3659 resetNotificationHeader(header);
3660 bindNotificationHeader(header);
3661 return header;
3662 }
3663
Selim Cinek29603462015-11-17 19:04:39 -08003664 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003665 if (result != null) {
3666 result.setViewVisibility(R.id.text_line_1, View.GONE);
3667 }
Selim Cinek29603462015-11-17 19:04:39 -08003668 }
3669
Selim Cinek850a8542015-11-11 11:48:36 -05003670 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003671 if (result != null) {
3672 result.setBoolean(R.id.notification_header, "setExpanded", true);
3673 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003674 }
3675
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003676 /**
3677 * Construct a RemoteViews for the final heads-up notification layout.
3678 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003679 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003680 if (mN.headsUpContentView != null
3681 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003682 return mN.headsUpContentView;
3683 } else if (mStyle != null) {
3684 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3685 if (styleView != null) {
3686 return styleView;
3687 }
3688 } else if (mActions.size() == 0) {
3689 return null;
3690 }
3691
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003692 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003693 }
3694
Selim Cinek624c02db2015-12-14 21:00:02 -08003695 /**
3696 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3697 *
3698 * @hide
3699 */
3700 public RemoteViews makePublicContentView() {
3701 if (mN.publicVersion != null) {
3702 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003703 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003704 }
3705 Bundle savedBundle = mN.extras;
3706 Style style = mStyle;
3707 mStyle = null;
3708 Icon largeIcon = mN.mLargeIcon;
3709 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003710 Bitmap largeIconLegacy = mN.largeIcon;
3711 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003712 Bundle publicExtras = new Bundle();
3713 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3714 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3715 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3716 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003717 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3718 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003719 publicExtras.putCharSequence(EXTRA_TITLE,
3720 mContext.getString(R.string.notification_hidden_text));
3721 mN.extras = publicExtras;
3722 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3723 mN.extras = savedBundle;
3724 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003725 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003726 mStyle = style;
3727 return publicView;
3728 }
3729
3730
Chris Wren8fd39ec2014-02-27 17:43:26 -05003731
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003732 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
3733 boolean oddAction) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003734 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003735 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003736 emphazisedMode ? getEmphasizedActionLayoutResource()
3737 : tombstone ? getActionTombstoneLayoutResource()
3738 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04003739 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003740 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003741 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003742 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003743 if (action.mRemoteInputs != null) {
3744 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3745 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003746 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07003747 // change the background bgColor
3748 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
3749 : R.color.notification_action_list_dark);
3750 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003751 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07003752 CharSequence title = action.title;
3753 ColorStateList[] outResultColor = null;
3754 if (isLegacy()) {
3755 title = clearColorSpans(title);
3756 } else {
3757 outResultColor = new ColorStateList[1];
3758 title = ensureColorSpanContrast(title, bgColor, outResultColor);
3759 }
3760 button.setTextViewText(R.id.action0, title);
3761 if (outResultColor != null && outResultColor[0] != null) {
3762 // We need to set the text color as well since changing a text to uppercase
3763 // clears its spans.
3764 button.setTextColor(R.id.action0, outResultColor[0]);
3765 } else if (mN.color != COLOR_DEFAULT) {
3766 button.setTextColor(R.id.action0,resolveContrastColor());
3767 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003768 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07003769 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003770 if (mN.color != COLOR_DEFAULT) {
3771 button.setTextColor(R.id.action0, resolveContrastColor());
3772 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003773 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003774 return button;
3775 }
3776
Joe Onoratocb109a02011-01-18 17:57:41 -08003777 /**
Selim Cinek981962e2016-07-20 20:41:58 -07003778 * Clears all color spans of a text
3779 * @param charSequence the input text
3780 * @return the same text but without color spans
3781 */
3782 private CharSequence clearColorSpans(CharSequence charSequence) {
3783 if (charSequence instanceof Spanned) {
3784 Spanned ss = (Spanned) charSequence;
3785 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3786 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3787 for (Object span : spans) {
3788 Object resultSpan = span;
3789 if (resultSpan instanceof CharacterStyle) {
3790 resultSpan = ((CharacterStyle) span).getUnderlying();
3791 }
3792 if (resultSpan instanceof TextAppearanceSpan) {
3793 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3794 if (originalSpan.getTextColor() != null) {
3795 resultSpan = new TextAppearanceSpan(
3796 originalSpan.getFamily(),
3797 originalSpan.getTextStyle(),
3798 originalSpan.getTextSize(),
3799 null,
3800 originalSpan.getLinkTextColor());
3801 }
3802 } else if (resultSpan instanceof ForegroundColorSpan
3803 || (resultSpan instanceof BackgroundColorSpan)) {
3804 continue;
3805 } else {
3806 resultSpan = span;
3807 }
3808 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
3809 ss.getSpanFlags(span));
3810 }
3811 return builder;
3812 }
3813 return charSequence;
3814 }
3815
3816 /**
3817 * Ensures contrast on color spans against a background color. also returns the color of the
3818 * text if a span was found that spans over the whole text.
3819 *
3820 * @param charSequence the charSequence on which the spans are
3821 * @param background the background color to ensure the contrast against
3822 * @param outResultColor an array in which a color will be returned as the first element if
3823 * there exists a full length color span.
3824 * @return the contrasted charSequence
3825 */
3826 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
3827 ColorStateList[] outResultColor) {
3828 if (charSequence instanceof Spanned) {
3829 Spanned ss = (Spanned) charSequence;
3830 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3831 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3832 for (Object span : spans) {
3833 Object resultSpan = span;
3834 int spanStart = ss.getSpanStart(span);
3835 int spanEnd = ss.getSpanEnd(span);
3836 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
3837 if (resultSpan instanceof CharacterStyle) {
3838 resultSpan = ((CharacterStyle) span).getUnderlying();
3839 }
3840 if (resultSpan instanceof TextAppearanceSpan) {
3841 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3842 ColorStateList textColor = originalSpan.getTextColor();
3843 if (textColor != null) {
3844 int[] colors = textColor.getColors();
3845 int[] newColors = new int[colors.length];
3846 for (int i = 0; i < newColors.length; i++) {
3847 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
3848 colors[i], background);
3849 }
3850 textColor = new ColorStateList(textColor.getStates().clone(),
3851 newColors);
3852 resultSpan = new TextAppearanceSpan(
3853 originalSpan.getFamily(),
3854 originalSpan.getTextStyle(),
3855 originalSpan.getTextSize(),
3856 textColor,
3857 originalSpan.getLinkTextColor());
3858 if (fullLength) {
3859 outResultColor[0] = new ColorStateList(
3860 textColor.getStates().clone(), newColors);
3861 }
3862 }
3863 } else if (resultSpan instanceof ForegroundColorSpan) {
3864 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
3865 int foregroundColor = originalSpan.getForegroundColor();
3866 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
3867 foregroundColor, background);
3868 resultSpan = new ForegroundColorSpan(foregroundColor);
3869 if (fullLength) {
3870 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
3871 }
3872 } else {
3873 resultSpan = span;
3874 }
3875
3876 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
3877 }
3878 return builder;
3879 }
3880 return charSequence;
3881 }
3882
3883 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003884 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003885 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003886 */
3887 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003888 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003889 }
3890
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003891 private CharSequence processLegacyText(CharSequence charSequence) {
3892 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003893 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003894 } else {
3895 return charSequence;
3896 }
3897 }
3898
Dan Sandler26e81cf2014-05-06 10:01:27 -04003899 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003900 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003901 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003902 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003903 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3904 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003905 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003906 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003907
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003908 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003909 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003910 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003911 }
3912
Dan Sandler26e81cf2014-05-06 10:01:27 -04003913 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003914 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003915 * if it's grayscale).
3916 */
3917 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003918 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3919 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003920 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003921 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003922 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003923 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003924 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003925 }
3926
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003927 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003928 if (mN.color != COLOR_DEFAULT) {
3929 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003930 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003931 }
3932
Adrian Roos4ff3b122016-02-01 12:26:13 -08003933 int resolveContrastColor() {
3934 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3935 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003936 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003937 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3938
3939 mCachedContrastColorIsFor = mN.color;
3940 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003941 }
3942
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003943 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003944 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003945 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003946 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003947 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003948 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003949 mN.actions = new Action[mActions.size()];
3950 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003951 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003952 if (!mPersonList.isEmpty()) {
3953 mN.extras.putStringArray(EXTRA_PEOPLE,
3954 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003955 }
Selim Cinek247fa012016-02-18 09:50:48 -08003956 if (mN.bigContentView != null || mN.contentView != null
3957 || mN.headsUpContentView != null) {
3958 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3959 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003960 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003961 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003962
Julia Reynolds3b848122016-02-26 10:45:32 -05003963 /**
3964 * Creates a Builder from an existing notification so further changes can be made.
3965 * @param context The context for your application / activity.
3966 * @param n The notification to create a Builder from.
3967 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003968 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003969 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003970 ApplicationInfo applicationInfo = n.extras.getParcelable(
3971 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003972 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003973 if (applicationInfo != null) {
3974 try {
3975 builderContext = context.createApplicationContext(applicationInfo,
3976 Context.CONTEXT_RESTRICTED);
3977 } catch (NameNotFoundException e) {
3978 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3979 builderContext = context; // try with our context
3980 }
3981 } else {
3982 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003983 }
3984
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003985 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003986 }
3987
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003988 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003989 * @deprecated Use {@link #build()} instead.
3990 */
3991 @Deprecated
3992 public Notification getNotification() {
3993 return build();
3994 }
3995
3996 /**
3997 * Combine all of the options that have been set and return a new {@link Notification}
3998 * object.
3999 */
4000 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004001 // first, add any extras from the calling code
4002 if (mUserExtras != null) {
4003 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004004 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004005
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004006 mN.creationTime = System.currentTimeMillis();
4007
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004008 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004009 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004010
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004011 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004012
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004013 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004014 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004015 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004016
Adrian Roos5081c0d2016-02-26 16:04:19 -08004017 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4018 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004019 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004020 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004021 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4022 mN.contentView.getSequenceNumber());
4023 }
4024 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004025 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004026 if (mN.bigContentView != null) {
4027 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4028 mN.bigContentView.getSequenceNumber());
4029 }
4030 }
4031 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004032 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004033 if (mN.headsUpContentView != null) {
4034 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4035 mN.headsUpContentView.getSequenceNumber());
4036 }
4037 }
4038 }
4039
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004040 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4041 mN.flags |= FLAG_SHOW_LIGHTS;
4042 }
4043
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004044 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004045 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004046
4047 /**
4048 * Apply this Builder to an existing {@link Notification} object.
4049 *
4050 * @hide
4051 */
4052 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004053 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004054 return n;
4055 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004056
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004057 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004058 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4059 * change.
4060 *
4061 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4062 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004063 * @hide
4064 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004065 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004066 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004067
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004068 // Only strip views for known Styles because we won't know how to
4069 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004070 if (!TextUtils.isEmpty(templateClass)
4071 && getNotificationStyleClass(templateClass) == null) {
4072 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004073 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004074
4075 // Only strip unmodified BuilderRemoteViews.
4076 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004077 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004078 n.contentView.getSequenceNumber();
4079 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004080 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004081 n.bigContentView.getSequenceNumber();
4082 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004083 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004084 n.headsUpContentView.getSequenceNumber();
4085
4086 // Nothing to do here, no need to clone.
4087 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4088 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004089 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004090
4091 Notification clone = n.clone();
4092 if (stripContentView) {
4093 clone.contentView = null;
4094 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4095 }
4096 if (stripBigContentView) {
4097 clone.bigContentView = null;
4098 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4099 }
4100 if (stripHeadsUpContentView) {
4101 clone.headsUpContentView = null;
4102 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4103 }
4104 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004105 }
4106
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004107 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004108 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004109 }
4110
4111 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004112 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004113 }
4114
4115 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004116 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004117 }
4118
4119 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004120 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004121 }
4122
4123 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004124 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004125 }
4126
Adrian Roosc1a80b02016-04-05 14:54:55 -07004127 private int getMessagingLayoutResource() {
4128 return R.layout.notification_template_material_messaging;
4129 }
4130
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004131 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004132 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004133 }
4134
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004135 private int getEmphasizedActionLayoutResource() {
4136 return R.layout.notification_material_action_emphasized;
4137 }
4138
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004139 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004140 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004141 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004142 }
4143
Selim Cinek279fa862016-06-14 10:57:25 -07004144 private boolean hasLargeIcon() {
4145 return mLargeIcon != null || largeIcon != null;
4146 }
4147
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004148 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004149 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004150 * @hide
4151 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004152 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004153 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4154 }
4155
4156 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004157 * @return true if the notification will show a chronometer; false otherwise
4158 * @hide
4159 */
4160 public boolean showsChronometer() {
4161 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4162 }
4163
4164 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004165 * @hide
4166 */
4167 @SystemApi
4168 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4169 Class<? extends Style>[] classes = new Class[] {
4170 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4171 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4172 MessagingStyle.class };
4173 for (Class<? extends Style> innerClass : classes) {
4174 if (templateClass.equals(innerClass.getName())) {
4175 return innerClass;
4176 }
4177 }
4178 return null;
4179 }
4180
4181 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004182 * An object that can apply a rich notification style to a {@link Notification.Builder}
4183 * object.
4184 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004185 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004186 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004187
4188 /**
4189 * @hide
4190 */
4191 protected CharSequence mSummaryText = null;
4192
4193 /**
4194 * @hide
4195 */
4196 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004197
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004198 protected Builder mBuilder;
4199
Chris Wrend6297db2012-05-03 16:20:13 -04004200 /**
4201 * Overrides ContentTitle in the big form of the template.
4202 * This defaults to the value passed to setContentTitle().
4203 */
4204 protected void internalSetBigContentTitle(CharSequence title) {
4205 mBigContentTitle = title;
4206 }
4207
4208 /**
4209 * Set the first line of text after the detail section in the big form of the template.
4210 */
4211 protected void internalSetSummaryText(CharSequence cs) {
4212 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004213 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004214 }
4215
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004216 public void setBuilder(Builder builder) {
4217 if (mBuilder != builder) {
4218 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004219 if (mBuilder != null) {
4220 mBuilder.setStyle(this);
4221 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004222 }
4223 }
4224
Chris Wrend6297db2012-05-03 16:20:13 -04004225 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004226 if (mBuilder == null) {
4227 throw new IllegalArgumentException("Style requires a valid Builder object");
4228 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004229 }
Chris Wrend6297db2012-05-03 16:20:13 -04004230
4231 protected RemoteViews getStandardView(int layoutId) {
4232 checkBuilder();
4233
Christoph Studer4600f9b2014-07-22 22:44:43 +02004234 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004235 CharSequence oldBuilderContentTitle =
4236 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004237 if (mBigContentTitle != null) {
4238 mBuilder.setContentTitle(mBigContentTitle);
4239 }
4240
Chris Wrend6297db2012-05-03 16:20:13 -04004241 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4242
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004243 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004244
Chris Wrend6297db2012-05-03 16:20:13 -04004245 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4246 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004247 } else {
4248 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004249 }
4250
Chris Wrend6297db2012-05-03 16:20:13 -04004251 return contentView;
4252 }
4253
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004254 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004255 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4256 * The default implementation has nothing additional to add.
4257 * @hide
4258 */
4259 public RemoteViews makeContentView() {
4260 return null;
4261 }
4262
4263 /**
4264 * Construct a Style-specific RemoteViews for the final big notification layout.
4265 * @hide
4266 */
4267 public RemoteViews makeBigContentView() {
4268 return null;
4269 }
4270
4271 /**
4272 * Construct a Style-specific RemoteViews for the final HUN layout.
4273 * @hide
4274 */
4275 public RemoteViews makeHeadsUpContentView() {
4276 return null;
4277 }
4278
4279 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004280 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004281 * @hide
4282 */
4283 public void addExtras(Bundle extras) {
4284 if (mSummaryTextSet) {
4285 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4286 }
4287 if (mBigContentTitle != null) {
4288 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4289 }
Chris Wren91ad5632013-06-05 15:05:57 -04004290 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004291 }
4292
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004293 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004294 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004295 * @hide
4296 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004297 protected void restoreFromExtras(Bundle extras) {
4298 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4299 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4300 mSummaryTextSet = true;
4301 }
4302 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4303 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4304 }
4305 }
4306
4307
4308 /**
4309 * @hide
4310 */
4311 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004312 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004313 return wip;
4314 }
4315
Daniel Sandler0ec46202015-06-24 01:27:05 -04004316 /**
4317 * @hide
4318 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004319 public void purgeResources() {}
4320
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004321 /**
4322 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4323 * attached to.
4324 *
4325 * @return the fully constructed Notification.
4326 */
4327 public Notification build() {
4328 checkBuilder();
4329 return mBuilder.build();
4330 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004331
4332 /**
4333 * @hide
4334 * @return true if the style positions the progress bar on the second line; false if the
4335 * style hides the progress bar
4336 */
4337 protected boolean hasProgress() {
4338 return true;
4339 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004340
4341 /**
4342 * @hide
4343 * @return Whether we should put the summary be put into the notification header
4344 */
4345 public boolean hasSummaryInHeader() {
4346 return true;
4347 }
Selim Cinek593610c2016-02-16 18:42:57 -08004348
4349 /**
4350 * @hide
4351 * @return Whether custom content views are displayed inline in the style
4352 */
4353 public boolean displayCustomViewInline() {
4354 return false;
4355 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004356 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004357
4358 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004359 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004360 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004361 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004362 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004363 * Notification notif = new Notification.Builder(mContext)
4364 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4365 * .setContentText(subject)
4366 * .setSmallIcon(R.drawable.new_post)
4367 * .setLargeIcon(aBitmap)
4368 * .setStyle(new Notification.BigPictureStyle()
4369 * .bigPicture(aBigBitmap))
4370 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004371 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004372 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004373 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004374 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004375 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004376 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004377 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004378 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004379
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004380 public BigPictureStyle() {
4381 }
4382
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004383 /**
4384 * @deprecated use {@code BigPictureStyle()}.
4385 */
4386 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004387 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004388 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004389 }
4390
Chris Wrend6297db2012-05-03 16:20:13 -04004391 /**
4392 * Overrides ContentTitle in the big form of the template.
4393 * This defaults to the value passed to setContentTitle().
4394 */
4395 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004396 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004397 return this;
4398 }
4399
4400 /**
4401 * Set the first line of text after the detail section in the big form of the template.
4402 */
4403 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004404 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004405 return this;
4406 }
4407
Chris Wren0bd664d2012-08-01 13:56:56 -04004408 /**
4409 * Provide the bitmap to be used as the payload for the BigPicture notification.
4410 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004411 public BigPictureStyle bigPicture(Bitmap b) {
4412 mPicture = b;
4413 return this;
4414 }
4415
Chris Wren3745a3d2012-05-22 15:11:52 -04004416 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004417 * Override the large icon when the big notification is shown.
4418 */
4419 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004420 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4421 }
4422
4423 /**
4424 * Override the large icon when the big notification is shown.
4425 */
4426 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004427 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004428 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004429 return this;
4430 }
4431
Riley Andrews0394a0c2015-11-03 23:36:52 -08004432 /** @hide */
4433 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4434
Daniel Sandler0ec46202015-06-24 01:27:05 -04004435 /**
4436 * @hide
4437 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004438 @Override
4439 public void purgeResources() {
4440 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004441 if (mPicture != null &&
4442 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004443 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004444 mPicture = mPicture.createAshmemBitmap();
4445 }
4446 if (mBigLargeIcon != null) {
4447 mBigLargeIcon.convertToAshmem();
4448 }
4449 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004450
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004451 /**
4452 * @hide
4453 */
4454 public RemoteViews makeBigContentView() {
4455 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004456 // This covers the following cases:
4457 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004458 // mN.mLargeIcon
4459 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004460 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07004461 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004462 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004463 oldLargeIcon = mBuilder.mN.mLargeIcon;
4464 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004465 // The legacy largeIcon might not allow us to clear the image, as it's taken in
4466 // replacement if the other one is null. Because we're restoring these legacy icons
4467 // for old listeners, this is in general non-null.
4468 largeIconLegacy = mBuilder.mN.largeIcon;
4469 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004470 }
4471
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004472 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004473 if (mSummaryTextSet) {
4474 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004475 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004476 }
Selim Cinek279fa862016-06-14 10:57:25 -07004477 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004478
Christoph Studer5c510ee2014-12-15 16:32:27 +01004479 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004480 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004481 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004482 }
4483
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004484 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004485 return contentView;
4486 }
4487
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004488 /**
4489 * @hide
4490 */
4491 public void addExtras(Bundle extras) {
4492 super.addExtras(extras);
4493
4494 if (mBigLargeIconSet) {
4495 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4496 }
4497 extras.putParcelable(EXTRA_PICTURE, mPicture);
4498 }
4499
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004500 /**
4501 * @hide
4502 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004503 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004504 protected void restoreFromExtras(Bundle extras) {
4505 super.restoreFromExtras(extras);
4506
4507 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004508 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004509 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004510 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004511 mPicture = extras.getParcelable(EXTRA_PICTURE);
4512 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004513
4514 /**
4515 * @hide
4516 */
4517 @Override
4518 public boolean hasSummaryInHeader() {
4519 return false;
4520 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004521 }
4522
4523 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004524 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004525 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004526 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004527 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004528 * Notification notif = new Notification.Builder(mContext)
4529 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4530 * .setContentText(subject)
4531 * .setSmallIcon(R.drawable.new_mail)
4532 * .setLargeIcon(aBitmap)
4533 * .setStyle(new Notification.BigTextStyle()
4534 * .bigText(aVeryLongString))
4535 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004536 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004537 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004538 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004539 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004540 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004541
4542 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004543 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004544
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004545 private CharSequence mBigText;
4546
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004547 public BigTextStyle() {
4548 }
4549
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004550 /**
4551 * @deprecated use {@code BigTextStyle()}.
4552 */
4553 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004554 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004555 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004556 }
4557
Chris Wrend6297db2012-05-03 16:20:13 -04004558 /**
4559 * Overrides ContentTitle in the big form of the template.
4560 * This defaults to the value passed to setContentTitle().
4561 */
4562 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004563 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004564 return this;
4565 }
4566
4567 /**
4568 * Set the first line of text after the detail section in the big form of the template.
4569 */
4570 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004571 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004572 return this;
4573 }
4574
Chris Wren0bd664d2012-08-01 13:56:56 -04004575 /**
4576 * Provide the longer text to be displayed in the big form of the
4577 * template in place of the content text.
4578 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004579 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004580 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004581 return this;
4582 }
4583
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004584 /**
4585 * @hide
4586 */
4587 public void addExtras(Bundle extras) {
4588 super.addExtras(extras);
4589
Christoph Studer4600f9b2014-07-22 22:44:43 +02004590 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4591 }
4592
4593 /**
4594 * @hide
4595 */
4596 @Override
4597 protected void restoreFromExtras(Bundle extras) {
4598 super.restoreFromExtras(extras);
4599
4600 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004601 }
4602
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004603 /**
4604 * @hide
4605 */
4606 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004607
4608 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004609 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004610 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004611
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004612 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004613
Selim Cinek75998782016-04-26 10:39:17 -07004614 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004615
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004616 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004617 if (TextUtils.isEmpty(bigTextText)) {
4618 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4619 // experience
4620 bigTextText = mBuilder.processLegacyText(text);
4621 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004622 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004623
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004624 return contentView;
4625 }
4626
Adrian Roosb1f427c2016-05-26 12:27:15 -07004627 static void applyBigTextContentView(Builder builder,
4628 RemoteViews contentView, CharSequence bigTextText) {
4629 contentView.setTextViewText(R.id.big_text, bigTextText);
4630 contentView.setViewVisibility(R.id.big_text,
4631 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4632 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004633 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004634 }
4635
4636 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004637 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004638 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004639 if (hasActions) {
4640 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4641 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004642 return lineCount;
4643 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004644 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004645
4646 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004647 * Helper class for generating large-format notifications that include multiple back-and-forth
4648 * messages of varying types between any number of people.
4649 *
4650 * <br>
4651 * If the platform does not provide large-format notifications, this method has no effect. The
4652 * user will always see the normal notification view.
4653 * <br>
4654 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4655 * so:
4656 * <pre class="prettyprint">
4657 *
4658 * Notification noti = new Notification.Builder()
4659 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4660 * .setContentText(subject)
4661 * .setSmallIcon(R.drawable.new_message)
4662 * .setLargeIcon(aBitmap)
4663 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4664 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4665 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4666 * .build();
4667 * </pre>
4668 */
4669 public static class MessagingStyle extends Style {
4670
4671 /**
4672 * The maximum number of messages that will be retained in the Notification itself (the
4673 * number displayed is up to the platform).
4674 */
4675 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4676
4677 CharSequence mUserDisplayName;
4678 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004679 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004680
4681 MessagingStyle() {
4682 }
4683
4684 /**
Alex Hillsfd590442016-10-07 09:52:44 -04004685 * @param userDisplayName Required - the name to be displayed for any replies sent by the
4686 * user before the posting app reposts the notification with those messages after they've
4687 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04004688 * {@link #addMessage(Notification.MessagingStyle.Message)}
4689 */
Alex Hillsfd590442016-10-07 09:52:44 -04004690 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04004691 mUserDisplayName = userDisplayName;
4692 }
4693
4694 /**
4695 * Returns the name to be displayed for any replies sent by the user
4696 */
4697 public CharSequence getUserDisplayName() {
4698 return mUserDisplayName;
4699 }
4700
4701 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004702 * Sets the title to be displayed on this conversation. This should only be used for
4703 * group messaging and left unset for one-on-one conversations.
4704 * @param conversationTitle
4705 * @return this object for method chaining.
4706 */
4707 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4708 mConversationTitle = conversationTitle;
4709 return this;
4710 }
4711
4712 /**
4713 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4714 * should be for one-on-one conversations
4715 */
4716 public CharSequence getConversationTitle() {
4717 return mConversationTitle;
4718 }
4719
4720 /**
4721 * Adds a message for display by this notification. Convenience call for a simple
4722 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4723 * @param text A {@link CharSequence} to be displayed as the message content
4724 * @param timestamp Time at which the message arrived
4725 * @param sender A {@link CharSequence} to be used for displaying the name of the
4726 * sender. Should be <code>null</code> for messages by the current user, in which case
4727 * the platform will insert {@link #getUserDisplayName()}.
4728 * Should be unique amongst all individuals in the conversation, and should be
4729 * consistent during re-posts of the notification.
4730 *
4731 * @see Message#Message(CharSequence, long, CharSequence)
4732 *
4733 * @return this object for method chaining
4734 */
4735 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4736 mMessages.add(new Message(text, timestamp, sender));
4737 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4738 mMessages.remove(0);
4739 }
4740 return this;
4741 }
4742
4743 /**
4744 * Adds a {@link Message} for display in this notification.
4745 * @param message The {@link Message} to be displayed
4746 * @return this object for method chaining
4747 */
4748 public MessagingStyle addMessage(Message message) {
4749 mMessages.add(message);
4750 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4751 mMessages.remove(0);
4752 }
4753 return this;
4754 }
4755
4756 /**
4757 * Gets the list of {@code Message} objects that represent the notification
4758 */
4759 public List<Message> getMessages() {
4760 return mMessages;
4761 }
4762
4763 /**
4764 * @hide
4765 */
4766 @Override
4767 public void addExtras(Bundle extras) {
4768 super.addExtras(extras);
4769 if (mUserDisplayName != null) {
4770 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4771 }
4772 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004773 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004774 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004775 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4776 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004777 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004778
4779 fixTitleAndTextExtras(extras);
4780 }
4781
4782 private void fixTitleAndTextExtras(Bundle extras) {
4783 Message m = findLatestIncomingMessage();
4784 CharSequence text = (m == null) ? null : m.mText;
4785 CharSequence sender = m == null ? null
4786 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4787 CharSequence title;
4788 if (!TextUtils.isEmpty(mConversationTitle)) {
4789 if (!TextUtils.isEmpty(sender)) {
4790 BidiFormatter bidi = BidiFormatter.getInstance();
4791 title = mBuilder.mContext.getString(
4792 com.android.internal.R.string.notification_messaging_title_template,
4793 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4794 } else {
4795 title = mConversationTitle;
4796 }
4797 } else {
4798 title = sender;
4799 }
4800
4801 if (title != null) {
4802 extras.putCharSequence(EXTRA_TITLE, title);
4803 }
4804 if (text != null) {
4805 extras.putCharSequence(EXTRA_TEXT, text);
4806 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004807 }
4808
4809 /**
4810 * @hide
4811 */
4812 @Override
4813 protected void restoreFromExtras(Bundle extras) {
4814 super.restoreFromExtras(extras);
4815
4816 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004817 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4818 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004819 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004820 if (parcelables != null && parcelables instanceof Parcelable[]) {
4821 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004822 }
4823 }
4824
4825 /**
4826 * @hide
4827 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004828 @Override
4829 public RemoteViews makeContentView() {
4830 Message m = findLatestIncomingMessage();
4831 CharSequence title = mConversationTitle != null
4832 ? mConversationTitle
4833 : (m == null) ? null : m.mSender;
4834 CharSequence text = (m == null)
4835 ? null
4836 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004837
Adrian Roosc1a80b02016-04-05 14:54:55 -07004838 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4839 false /* hasProgress */,
4840 title,
4841 text);
4842 }
4843
4844 private Message findLatestIncomingMessage() {
4845 for (int i = mMessages.size() - 1; i >= 0; i--) {
4846 Message m = mMessages.get(i);
4847 // Incoming messages have a non-empty sender.
4848 if (!TextUtils.isEmpty(m.mSender)) {
4849 return m;
4850 }
4851 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004852 if (!mMessages.isEmpty()) {
4853 // No incoming messages, fall back to outgoing message
4854 return mMessages.get(mMessages.size() - 1);
4855 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004856 return null;
4857 }
4858
4859 /**
4860 * @hide
4861 */
4862 @Override
4863 public RemoteViews makeBigContentView() {
4864 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4865 ? super.mBigContentTitle
4866 : mConversationTitle;
4867 boolean hasTitle = !TextUtils.isEmpty(title);
4868
Adrian Roosfeafa052016-06-01 17:09:45 -07004869 if (mMessages.size() == 1) {
4870 // Special case for a single message: Use the big text style
4871 // so the collapsed and expanded versions match nicely.
4872 CharSequence bigTitle;
4873 CharSequence text;
4874 if (hasTitle) {
4875 bigTitle = title;
4876 text = makeMessageLine(mMessages.get(0));
4877 } else {
4878 bigTitle = mMessages.get(0).mSender;
4879 text = mMessages.get(0).mText;
4880 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004881 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4882 mBuilder.getBigTextLayoutResource(),
Adrian Roosfeafa052016-06-01 17:09:45 -07004883 false /* progress */, bigTitle, null /* text */);
Adrian Roosb1f427c2016-05-26 12:27:15 -07004884 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4885 return contentView;
4886 }
4887
Adrian Roos48d746a2016-04-12 14:57:28 -07004888 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004889 mBuilder.getMessagingLayoutResource(),
4890 false /* hasProgress */,
4891 title,
4892 null /* text */);
4893
4894 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4895 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4896
4897 // Make sure all rows are gone in case we reuse a view.
4898 for (int rowId : rowIds) {
4899 contentView.setViewVisibility(rowId, View.GONE);
4900 }
4901
4902 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004903 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
4904 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004905 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07004906 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004907
Adrian Roosfeafa052016-06-01 17:09:45 -07004908 int contractedChildId = View.NO_ID;
4909 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07004910 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4911 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4912 Message m = mMessages.get(firstMessage + i);
4913 int rowId = rowIds[i];
4914
4915 contentView.setViewVisibility(rowId, View.VISIBLE);
4916 contentView.setTextViewText(rowId, makeMessageLine(m));
4917
Adrian Roosfeafa052016-06-01 17:09:45 -07004918 if (contractedMessage == m) {
4919 contractedChildId = rowId;
4920 }
4921
Adrian Roosc1a80b02016-04-05 14:54:55 -07004922 i++;
4923 }
Adrian Roosfeafa052016-06-01 17:09:45 -07004924 // Record this here to allow transformation between the contracted and expanded views.
4925 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
4926 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04004927 return contentView;
4928 }
4929
Adrian Roosc1a80b02016-04-05 14:54:55 -07004930 private CharSequence makeMessageLine(Message m) {
4931 BidiFormatter bidi = BidiFormatter.getInstance();
4932 SpannableStringBuilder sb = new SpannableStringBuilder();
4933 if (TextUtils.isEmpty(m.mSender)) {
4934 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4935 sb.append(bidi.unicodeWrap(replyName),
4936 makeFontColorSpan(mBuilder.resolveContrastColor()),
4937 0 /* flags */);
4938 } else {
4939 sb.append(bidi.unicodeWrap(m.mSender),
4940 makeFontColorSpan(Color.BLACK),
4941 0 /* flags */);
4942 }
4943 CharSequence text = m.mText == null ? "" : m.mText;
4944 sb.append(" ").append(bidi.unicodeWrap(text));
4945 return sb;
4946 }
4947
Adrian Roosdedd1df2016-04-26 16:38:47 -07004948 /**
4949 * @hide
4950 */
4951 @Override
4952 public RemoteViews makeHeadsUpContentView() {
4953 Message m = findLatestIncomingMessage();
4954 CharSequence title = mConversationTitle != null
4955 ? mConversationTitle
4956 : (m == null) ? null : m.mSender;
4957 CharSequence text = (m == null)
4958 ? null
4959 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4960
4961 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4962 false /* hasProgress */,
4963 title,
4964 text);
4965 }
4966
Adrian Roosc1a80b02016-04-05 14:54:55 -07004967 private static TextAppearanceSpan makeFontColorSpan(int color) {
4968 return new TextAppearanceSpan(null, 0, 0,
4969 ColorStateList.valueOf(color), null);
4970 }
4971
Alex Hillsd9b04d92016-04-11 16:38:16 -04004972 public static final class Message {
4973
4974 static final String KEY_TEXT = "text";
4975 static final String KEY_TIMESTAMP = "time";
4976 static final String KEY_SENDER = "sender";
4977 static final String KEY_DATA_MIME_TYPE = "type";
4978 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004979
4980 private final CharSequence mText;
4981 private final long mTimestamp;
4982 private final CharSequence mSender;
4983
4984 private String mDataMimeType;
4985 private Uri mDataUri;
4986
4987 /**
4988 * Constructor
4989 * @param text A {@link CharSequence} to be displayed as the message content
4990 * @param timestamp Time at which the message arrived
4991 * @param sender A {@link CharSequence} to be used for displaying the name of the
4992 * sender. Should be <code>null</code> for messages by the current user, in which case
4993 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4994 * Should be unique amongst all individuals in the conversation, and should be
4995 * consistent during re-posts of the notification.
4996 */
4997 public Message(CharSequence text, long timestamp, CharSequence sender){
4998 mText = text;
4999 mTimestamp = timestamp;
5000 mSender = sender;
5001 }
5002
5003 /**
5004 * Sets a binary blob of data and an associated MIME type for a message. In the case
5005 * where the platform doesn't support the MIME type, the original text provided in the
5006 * constructor will be used.
5007 * @param dataMimeType The MIME type of the content. See
5008 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5009 * types on Android and Android Wear.
5010 * @param dataUri The uri containing the content whose type is given by the MIME type.
5011 * <p class="note">
5012 * <ol>
5013 * <li>Notification Listeners including the System UI need permission to access the
5014 * data the Uri points to. The recommended ways to do this are:</li>
5015 * <li>Store the data in your own ContentProvider, making sure that other apps have
5016 * the correct permission to access your provider. The preferred mechanism for
5017 * providing access is to use per-URI permissions which are temporary and only
5018 * grant access to the receiving application. An easy way to create a
5019 * ContentProvider like this is to use the FileProvider helper class.</li>
5020 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5021 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5022 * also store non-media types (see MediaStore.Files for more info). Files can be
5023 * inserted into the MediaStore using scanFile() after which a content:// style
5024 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5025 * Note that once added to the system MediaStore the content is accessible to any
5026 * app on the device.</li>
5027 * </ol>
5028 * @return this object for method chaining
5029 */
5030 public Message setData(String dataMimeType, Uri dataUri) {
5031 mDataMimeType = dataMimeType;
5032 mDataUri = dataUri;
5033 return this;
5034 }
5035
Alex Hillsfc737de2016-03-23 17:33:02 -04005036 /**
5037 * Get the text to be used for this message, or the fallback text if a type and content
5038 * Uri have been set
5039 */
5040 public CharSequence getText() {
5041 return mText;
5042 }
5043
5044 /**
5045 * Get the time at which this message arrived
5046 */
5047 public long getTimestamp() {
5048 return mTimestamp;
5049 }
5050
5051 /**
5052 * Get the text used to display the contact's name in the messaging experience
5053 */
5054 public CharSequence getSender() {
5055 return mSender;
5056 }
5057
5058 /**
5059 * Get the MIME type of the data pointed to by the Uri
5060 */
5061 public String getDataMimeType() {
5062 return mDataMimeType;
5063 }
5064
5065 /**
5066 * Get the the Uri pointing to the content of the message. Can be null, in which case
5067 * {@see #getText()} is used.
5068 */
5069 public Uri getDataUri() {
5070 return mDataUri;
5071 }
5072
Alex Hillsd9b04d92016-04-11 16:38:16 -04005073 private Bundle toBundle() {
5074 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005075 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005076 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005077 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005078 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005079 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005080 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005081 }
5082 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005083 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005084 }
5085 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005086 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005087 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005088 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005089 }
5090
Alex Hillsd9b04d92016-04-11 16:38:16 -04005091 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5092 Bundle[] bundles = new Bundle[messages.size()];
5093 final int N = messages.size();
5094 for (int i = 0; i < N; i++) {
5095 bundles[i] = messages.get(i).toBundle();
5096 }
5097 return bundles;
5098 }
5099
Adrian Roosdedd1df2016-04-26 16:38:47 -07005100 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005101 List<Message> messages = new ArrayList<>(bundles.length);
5102 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005103 if (bundles[i] instanceof Bundle) {
5104 Message message = getMessageFromBundle((Bundle)bundles[i]);
5105 if (message != null) {
5106 messages.add(message);
5107 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005108 }
5109 }
5110 return messages;
5111 }
5112
5113 static Message getMessageFromBundle(Bundle bundle) {
5114 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005115 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005116 return null;
5117 } else {
5118 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5119 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5120 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5121 bundle.containsKey(KEY_DATA_URI)) {
5122
5123 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5124 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005125 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005126 return message;
5127 }
5128 } catch (ClassCastException e) {
5129 return null;
5130 }
5131 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005132 }
5133 }
5134
5135 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005136 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005137 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005138 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005139 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005140 * Notification notif = new Notification.Builder(mContext)
5141 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5142 * .setContentText(subject)
5143 * .setSmallIcon(R.drawable.new_mail)
5144 * .setLargeIcon(aBitmap)
5145 * .setStyle(new Notification.InboxStyle()
5146 * .addLine(str1)
5147 * .addLine(str2)
5148 * .setContentTitle(&quot;&quot;)
5149 * .setSummaryText(&quot;+3 more&quot;))
5150 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005151 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005152 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005153 * @see Notification#bigContentView
5154 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005155 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005156 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5157
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005158 public InboxStyle() {
5159 }
5160
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005161 /**
5162 * @deprecated use {@code InboxStyle()}.
5163 */
5164 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005165 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005166 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005167 }
5168
Chris Wrend6297db2012-05-03 16:20:13 -04005169 /**
5170 * Overrides ContentTitle in the big form of the template.
5171 * This defaults to the value passed to setContentTitle().
5172 */
5173 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005174 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005175 return this;
5176 }
5177
5178 /**
5179 * Set the first line of text after the detail section in the big form of the template.
5180 */
5181 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005182 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005183 return this;
5184 }
5185
Chris Wren0bd664d2012-08-01 13:56:56 -04005186 /**
5187 * Append a line to the digest section of the Inbox notification.
5188 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005189 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005190 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005191 return this;
5192 }
5193
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005194 /**
5195 * @hide
5196 */
5197 public void addExtras(Bundle extras) {
5198 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005199
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005200 CharSequence[] a = new CharSequence[mTexts.size()];
5201 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5202 }
5203
Christoph Studer4600f9b2014-07-22 22:44:43 +02005204 /**
5205 * @hide
5206 */
5207 @Override
5208 protected void restoreFromExtras(Bundle extras) {
5209 super.restoreFromExtras(extras);
5210
5211 mTexts.clear();
5212 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5213 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5214 }
5215 }
5216
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005217 /**
5218 * @hide
5219 */
5220 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005221 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005222 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005223 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5224 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005225
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005226 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005227
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005228 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005229
Chris Wrend6297db2012-05-03 16:20:13 -04005230 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 -04005231 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005232
Chris Wren4ed80d52012-05-17 09:30:03 -04005233 // Make sure all rows are gone in case we reuse a view.
5234 for (int rowId : rowIds) {
5235 contentView.setViewVisibility(rowId, View.GONE);
5236 }
5237
Daniel Sandler879c5e02012-04-17 16:46:51 -04005238 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005239 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5240 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005241 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005242 int onlyViewId = 0;
5243 int maxRows = rowIds.length;
5244 if (mBuilder.mActions.size() > 0) {
5245 maxRows--;
5246 }
5247 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005248 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005249 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005250 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005251 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005252 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005253 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005254 if (first) {
5255 onlyViewId = rowIds[i];
5256 } else {
5257 onlyViewId = 0;
5258 }
Selim Cinek247fa012016-02-18 09:50:48 -08005259 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005260 }
5261 i++;
5262 }
Selim Cinek07c80172016-04-21 16:40:47 -07005263 if (onlyViewId != 0) {
5264 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5265 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5266 R.dimen.notification_text_margin_top);
5267 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5268 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005269
Daniel Sandler879c5e02012-04-17 16:46:51 -04005270 return contentView;
5271 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005272
Selim Cinek247fa012016-02-18 09:50:48 -08005273 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005274 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005275 if (first) {
5276 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5277 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5278 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005279 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005280 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005281 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005282 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005283 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005284 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005285 }
Dan Sandler842dd772014-05-15 09:36:47 -04005286
5287 /**
5288 * Notification style for media playback notifications.
5289 *
5290 * In the expanded form, {@link Notification#bigContentView}, up to 5
5291 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005292 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005293 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5294 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5295 * treated as album artwork.
5296 *
5297 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5298 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005299 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005300 * in the standard view alongside the usual content.
5301 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005302 * Notifications created with MediaStyle will have their category set to
5303 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5304 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5305 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005306 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5307 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005308 * the System UI can identify this as a notification representing an active media session
5309 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5310 *
5311 * To use this style with your Notification, feed it to
5312 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5313 * <pre class="prettyprint">
5314 * Notification noti = new Notification.Builder()
5315 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005316 * .setContentTitle(&quot;Track title&quot;)
5317 * .setContentText(&quot;Artist - Album&quot;)
5318 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005319 * .setStyle(<b>new Notification.MediaStyle()</b>
5320 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005321 * .build();
5322 * </pre>
5323 *
5324 * @see Notification#bigContentView
5325 */
5326 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005327 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005328 static final int MAX_MEDIA_BUTTONS = 5;
5329
5330 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005331 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005332
5333 public MediaStyle() {
5334 }
5335
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005336 /**
5337 * @deprecated use {@code MediaStyle()}.
5338 */
5339 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005340 public MediaStyle(Builder builder) {
5341 setBuilder(builder);
5342 }
5343
5344 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005345 * 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 -04005346 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005347 *
5348 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005349 */
5350 public MediaStyle setShowActionsInCompactView(int...actions) {
5351 mActionsToShowInCompact = actions;
5352 return this;
5353 }
5354
5355 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005356 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5357 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005358 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005359 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005360 mToken = token;
5361 return this;
5362 }
5363
Christoph Studer4600f9b2014-07-22 22:44:43 +02005364 /**
5365 * @hide
5366 */
Dan Sandler842dd772014-05-15 09:36:47 -04005367 @Override
5368 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005369 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005370 if (wip.category == null) {
5371 wip.category = Notification.CATEGORY_TRANSPORT;
5372 }
Dan Sandler842dd772014-05-15 09:36:47 -04005373 return wip;
5374 }
5375
Christoph Studer4600f9b2014-07-22 22:44:43 +02005376 /**
5377 * @hide
5378 */
5379 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005380 public RemoteViews makeContentView() {
5381 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005382 }
5383
5384 /**
5385 * @hide
5386 */
5387 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005388 public RemoteViews makeBigContentView() {
5389 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005390 }
5391
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005392 /**
5393 * @hide
5394 */
5395 @Override
5396 public RemoteViews makeHeadsUpContentView() {
5397 RemoteViews expanded = makeMediaBigContentView();
5398 return expanded != null ? expanded : makeMediaContentView();
5399 }
5400
Dan Sandler842dd772014-05-15 09:36:47 -04005401 /** @hide */
5402 @Override
5403 public void addExtras(Bundle extras) {
5404 super.addExtras(extras);
5405
5406 if (mToken != null) {
5407 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5408 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005409 if (mActionsToShowInCompact != null) {
5410 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5411 }
Dan Sandler842dd772014-05-15 09:36:47 -04005412 }
5413
Christoph Studer4600f9b2014-07-22 22:44:43 +02005414 /**
5415 * @hide
5416 */
5417 @Override
5418 protected void restoreFromExtras(Bundle extras) {
5419 super.restoreFromExtras(extras);
5420
5421 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5422 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5423 }
5424 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5425 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5426 }
5427 }
5428
Selim Cinek5bf069a2015-11-10 19:14:27 -05005429 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005430 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005431 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005432 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005433 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005434 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5435 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005436 if (!tombstone) {
5437 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5438 }
5439 button.setContentDescription(R.id.action0, action.title);
5440 return button;
5441 }
5442
5443 private RemoteViews makeMediaContentView() {
5444 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005445 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005446
5447 final int numActions = mBuilder.mActions.size();
5448 final int N = mActionsToShowInCompact == null
5449 ? 0
5450 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5451 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005452 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005453 for (int i = 0; i < N; i++) {
5454 if (i >= numActions) {
5455 throw new IllegalArgumentException(String.format(
5456 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5457 i, numActions - 1));
5458 }
5459
5460 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005461 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005462 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005463 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005464 }
5465 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005466 handleImage(view);
5467 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005468 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005469 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005470 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005471 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005472 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005473 return view;
5474 }
5475
5476 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005477 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005478 // Dont add an expanded view if there is no more content to be revealed
5479 int actionsInCompact = mActionsToShowInCompact == null
5480 ? 0
5481 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005482 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005483 return null;
5484 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005485 RemoteViews big = mBuilder.applyStandardTemplate(
5486 R.layout.notification_template_material_big_media,
5487 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005488
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005489 if (actionCount > 0) {
5490 big.removeAllViews(com.android.internal.R.id.media_actions);
5491 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005492 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005493 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005494 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005495 }
5496 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005497 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005498 return big;
5499 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005500
Selim Cinek5bf069a2015-11-10 19:14:27 -05005501 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005502 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005503 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5504 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005505 }
5506 }
5507
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005508 /**
5509 * @hide
5510 */
5511 @Override
5512 protected boolean hasProgress() {
5513 return false;
5514 }
Dan Sandler842dd772014-05-15 09:36:47 -04005515 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005516
Selim Cinek593610c2016-02-16 18:42:57 -08005517 /**
5518 * Notification style for custom views that are decorated by the system
5519 *
5520 * <p>Instead of providing a notification that is completely custom, a developer can set this
5521 * style and still obtain system decorations like the notification header with the expand
5522 * affordance and actions.
5523 *
5524 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5525 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5526 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5527 * corresponding custom views to display.
5528 *
5529 * To use this style with your Notification, feed it to
5530 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5531 * <pre class="prettyprint">
5532 * Notification noti = new Notification.Builder()
5533 * .setSmallIcon(R.drawable.ic_stat_player)
5534 * .setLargeIcon(albumArtBitmap))
5535 * .setCustomContentView(contentView);
5536 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5537 * .build();
5538 * </pre>
5539 */
5540 public static class DecoratedCustomViewStyle extends Style {
5541
5542 public DecoratedCustomViewStyle() {
5543 }
5544
Selim Cinek593610c2016-02-16 18:42:57 -08005545 /**
5546 * @hide
5547 */
5548 public boolean displayCustomViewInline() {
5549 return true;
5550 }
5551
5552 /**
5553 * @hide
5554 */
5555 @Override
5556 public RemoteViews makeContentView() {
5557 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5558 }
5559
5560 /**
5561 * @hide
5562 */
5563 @Override
5564 public RemoteViews makeBigContentView() {
5565 return makeDecoratedBigContentView();
5566 }
5567
5568 /**
5569 * @hide
5570 */
5571 @Override
5572 public RemoteViews makeHeadsUpContentView() {
5573 return makeDecoratedHeadsUpContentView();
5574 }
5575
Selim Cinek593610c2016-02-16 18:42:57 -08005576 private RemoteViews makeDecoratedHeadsUpContentView() {
5577 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5578 ? mBuilder.mN.contentView
5579 : mBuilder.mN.headsUpContentView;
5580 if (mBuilder.mActions.size() == 0) {
5581 return makeStandardTemplateWithCustomContent(headsUpContentView);
5582 }
5583 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5584 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005585 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005586 return remoteViews;
5587 }
5588
Selim Cinek593610c2016-02-16 18:42:57 -08005589 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5590 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5591 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005592 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005593 return remoteViews;
5594 }
5595
Selim Cinek593610c2016-02-16 18:42:57 -08005596 private RemoteViews makeDecoratedBigContentView() {
5597 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5598 ? mBuilder.mN.contentView
5599 : mBuilder.mN.bigContentView;
5600 if (mBuilder.mActions.size() == 0) {
5601 return makeStandardTemplateWithCustomContent(bigContentView);
5602 }
5603 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5604 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005605 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005606 return remoteViews;
5607 }
Selim Cinek247fa012016-02-18 09:50:48 -08005608
5609 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5610 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005611 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005612 // Need to clone customContent before adding, because otherwise it can no longer be
5613 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005614 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005615 remoteViews.removeAllViews(R.id.notification_main_column);
5616 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005617 }
Selim Cinek247fa012016-02-18 09:50:48 -08005618 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005619 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005620 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005621 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005622 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005623 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005624 }
Selim Cinek593610c2016-02-16 18:42:57 -08005625 }
5626
Selim Cinek03eb3b72016-02-18 10:39:45 -08005627 /**
5628 * Notification style for media custom views that are decorated by the system
5629 *
5630 * <p>Instead of providing a media notification that is completely custom, a developer can set
5631 * this style and still obtain system decorations like the notification header with the expand
5632 * affordance and actions.
5633 *
5634 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5635 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5636 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5637 * corresponding custom views to display.
5638 *
5639 * To use this style with your Notification, feed it to
5640 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5641 * <pre class="prettyprint">
5642 * Notification noti = new Notification.Builder()
5643 * .setSmallIcon(R.drawable.ic_stat_player)
5644 * .setLargeIcon(albumArtBitmap))
5645 * .setCustomContentView(contentView);
5646 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5647 * .setMediaSession(mySession))
5648 * .build();
5649 * </pre>
5650 *
5651 * @see android.app.Notification.DecoratedCustomViewStyle
5652 * @see android.app.Notification.MediaStyle
5653 */
5654 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5655
5656 public DecoratedMediaCustomViewStyle() {
5657 }
5658
Selim Cinek03eb3b72016-02-18 10:39:45 -08005659 /**
5660 * @hide
5661 */
5662 public boolean displayCustomViewInline() {
5663 return true;
5664 }
5665
5666 /**
5667 * @hide
5668 */
5669 @Override
5670 public RemoteViews makeContentView() {
5671 RemoteViews remoteViews = super.makeContentView();
5672 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5673 mBuilder.mN.contentView);
5674 }
5675
5676 /**
5677 * @hide
5678 */
5679 @Override
5680 public RemoteViews makeBigContentView() {
5681 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5682 ? mBuilder.mN.bigContentView
5683 : mBuilder.mN.contentView;
5684 return makeBigContentViewWithCustomContent(customRemoteView);
5685 }
5686
5687 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5688 RemoteViews remoteViews = super.makeBigContentView();
5689 if (remoteViews != null) {
5690 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5691 customRemoteView);
5692 } else if (customRemoteView != mBuilder.mN.contentView){
5693 remoteViews = super.makeContentView();
5694 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5695 customRemoteView);
5696 } else {
5697 return null;
5698 }
5699 }
5700
5701 /**
5702 * @hide
5703 */
5704 @Override
5705 public RemoteViews makeHeadsUpContentView() {
5706 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5707 ? mBuilder.mN.headsUpContentView
5708 : mBuilder.mN.contentView;
5709 return makeBigContentViewWithCustomContent(customRemoteView);
5710 }
5711
5712 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5713 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005714 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005715 // Need to clone customContent before adding, because otherwise it can no longer be
5716 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005717 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005718 remoteViews.removeAllViews(id);
5719 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005720 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005721 return remoteViews;
5722 }
5723 }
5724
Christoph Studer4600f9b2014-07-22 22:44:43 +02005725 // When adding a new Style subclass here, don't forget to update
5726 // Builder.getNotificationStyleClass.
5727
Griff Hazen61a9e862014-05-22 16:05:19 -07005728 /**
5729 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5730 * metadata or change options on a notification builder.
5731 */
5732 public interface Extender {
5733 /**
5734 * Apply this extender to a notification builder.
5735 * @param builder the builder to be modified.
5736 * @return the build object for chaining.
5737 */
5738 public Builder extend(Builder builder);
5739 }
5740
5741 /**
5742 * Helper class to add wearable extensions to notifications.
5743 * <p class="note"> See
5744 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5745 * for Android Wear</a> for more information on how to use this class.
5746 * <p>
5747 * To create a notification with wearable extensions:
5748 * <ol>
5749 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5750 * properties.
5751 * <li>Create a {@link android.app.Notification.WearableExtender}.
5752 * <li>Set wearable-specific properties using the
5753 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5754 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5755 * notification.
5756 * <li>Post the notification to the notification system with the
5757 * {@code NotificationManager.notify(...)} methods.
5758 * </ol>
5759 *
5760 * <pre class="prettyprint">
5761 * Notification notif = new Notification.Builder(mContext)
5762 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5763 * .setContentText(subject)
5764 * .setSmallIcon(R.drawable.new_mail)
5765 * .extend(new Notification.WearableExtender()
5766 * .setContentIcon(R.drawable.new_mail))
5767 * .build();
5768 * NotificationManager notificationManger =
5769 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5770 * notificationManger.notify(0, notif);</pre>
5771 *
5772 * <p>Wearable extensions can be accessed on an existing notification by using the
5773 * {@code WearableExtender(Notification)} constructor,
5774 * and then using the {@code get} methods to access values.
5775 *
5776 * <pre class="prettyprint">
5777 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5778 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005779 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005780 */
5781 public static final class WearableExtender implements Extender {
5782 /**
5783 * Sentinel value for an action index that is unset.
5784 */
5785 public static final int UNSET_ACTION_INDEX = -1;
5786
5787 /**
5788 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5789 * default sizing.
5790 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005791 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005792 * on their content.
5793 */
5794 public static final int SIZE_DEFAULT = 0;
5795
5796 /**
5797 * Size value for use with {@link #setCustomSizePreset} to show this notification
5798 * with an extra small size.
5799 * <p>This value is only applicable for custom display notifications created using
5800 * {@link #setDisplayIntent}.
5801 */
5802 public static final int SIZE_XSMALL = 1;
5803
5804 /**
5805 * Size value for use with {@link #setCustomSizePreset} to show this notification
5806 * with a small size.
5807 * <p>This value is only applicable for custom display notifications created using
5808 * {@link #setDisplayIntent}.
5809 */
5810 public static final int SIZE_SMALL = 2;
5811
5812 /**
5813 * Size value for use with {@link #setCustomSizePreset} to show this notification
5814 * with a medium size.
5815 * <p>This value is only applicable for custom display notifications created using
5816 * {@link #setDisplayIntent}.
5817 */
5818 public static final int SIZE_MEDIUM = 3;
5819
5820 /**
5821 * Size value for use with {@link #setCustomSizePreset} to show this notification
5822 * with a large size.
5823 * <p>This value is only applicable for custom display notifications created using
5824 * {@link #setDisplayIntent}.
5825 */
5826 public static final int SIZE_LARGE = 4;
5827
Griff Hazend5f11f92014-05-27 15:40:09 -07005828 /**
5829 * Size value for use with {@link #setCustomSizePreset} to show this notification
5830 * full screen.
5831 * <p>This value is only applicable for custom display notifications created using
5832 * {@link #setDisplayIntent}.
5833 */
5834 public static final int SIZE_FULL_SCREEN = 5;
5835
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005836 /**
5837 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5838 * short amount of time when this notification is displayed on the screen. This
5839 * is the default value.
5840 */
5841 public static final int SCREEN_TIMEOUT_SHORT = 0;
5842
5843 /**
5844 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5845 * for a longer amount of time when this notification is displayed on the screen.
5846 */
5847 public static final int SCREEN_TIMEOUT_LONG = -1;
5848
Griff Hazen61a9e862014-05-22 16:05:19 -07005849 /** Notification extra which contains wearable extensions */
5850 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5851
Pete Gastaf6781d2014-10-07 15:17:05 -04005852 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005853 private static final String KEY_ACTIONS = "actions";
5854 private static final String KEY_FLAGS = "flags";
5855 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5856 private static final String KEY_PAGES = "pages";
5857 private static final String KEY_BACKGROUND = "background";
5858 private static final String KEY_CONTENT_ICON = "contentIcon";
5859 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5860 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5861 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5862 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5863 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005864 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005865 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005866 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07005867
5868 // Flags bitwise-ored to mFlags
5869 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5870 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5871 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5872 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005873 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005874 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005875 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005876
5877 // Default value for flags integer
5878 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5879
5880 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5881 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5882
5883 private ArrayList<Action> mActions = new ArrayList<Action>();
5884 private int mFlags = DEFAULT_FLAGS;
5885 private PendingIntent mDisplayIntent;
5886 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5887 private Bitmap mBackground;
5888 private int mContentIcon;
5889 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5890 private int mContentActionIndex = UNSET_ACTION_INDEX;
5891 private int mCustomSizePreset = SIZE_DEFAULT;
5892 private int mCustomContentHeight;
5893 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005894 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005895 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005896 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07005897
5898 /**
5899 * Create a {@link android.app.Notification.WearableExtender} with default
5900 * options.
5901 */
5902 public WearableExtender() {
5903 }
5904
5905 public WearableExtender(Notification notif) {
5906 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5907 if (wearableBundle != null) {
5908 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5909 if (actions != null) {
5910 mActions.addAll(actions);
5911 }
5912
5913 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5914 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5915
5916 Notification[] pages = getNotificationArrayFromBundle(
5917 wearableBundle, KEY_PAGES);
5918 if (pages != null) {
5919 Collections.addAll(mPages, pages);
5920 }
5921
5922 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5923 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5924 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5925 DEFAULT_CONTENT_ICON_GRAVITY);
5926 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5927 UNSET_ACTION_INDEX);
5928 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5929 SIZE_DEFAULT);
5930 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5931 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005932 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005933 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005934 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07005935 }
5936 }
5937
5938 /**
5939 * Apply wearable extensions to a notification that is being built. This is typically
5940 * called by the {@link android.app.Notification.Builder#extend} method of
5941 * {@link android.app.Notification.Builder}.
5942 */
5943 @Override
5944 public Notification.Builder extend(Notification.Builder builder) {
5945 Bundle wearableBundle = new Bundle();
5946
5947 if (!mActions.isEmpty()) {
5948 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5949 }
5950 if (mFlags != DEFAULT_FLAGS) {
5951 wearableBundle.putInt(KEY_FLAGS, mFlags);
5952 }
5953 if (mDisplayIntent != null) {
5954 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5955 }
5956 if (!mPages.isEmpty()) {
5957 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5958 new Notification[mPages.size()]));
5959 }
5960 if (mBackground != null) {
5961 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5962 }
5963 if (mContentIcon != 0) {
5964 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5965 }
5966 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5967 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5968 }
5969 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5970 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5971 mContentActionIndex);
5972 }
5973 if (mCustomSizePreset != SIZE_DEFAULT) {
5974 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5975 }
5976 if (mCustomContentHeight != 0) {
5977 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5978 }
5979 if (mGravity != DEFAULT_GRAVITY) {
5980 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5981 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005982 if (mHintScreenTimeout != 0) {
5983 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5984 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005985 if (mDismissalId != null) {
5986 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5987 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005988 if (mBridgeTag != null) {
5989 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
5990 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005991
5992 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5993 return builder;
5994 }
5995
5996 @Override
5997 public WearableExtender clone() {
5998 WearableExtender that = new WearableExtender();
5999 that.mActions = new ArrayList<Action>(this.mActions);
6000 that.mFlags = this.mFlags;
6001 that.mDisplayIntent = this.mDisplayIntent;
6002 that.mPages = new ArrayList<Notification>(this.mPages);
6003 that.mBackground = this.mBackground;
6004 that.mContentIcon = this.mContentIcon;
6005 that.mContentIconGravity = this.mContentIconGravity;
6006 that.mContentActionIndex = this.mContentActionIndex;
6007 that.mCustomSizePreset = this.mCustomSizePreset;
6008 that.mCustomContentHeight = this.mCustomContentHeight;
6009 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006010 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006011 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006012 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006013 return that;
6014 }
6015
6016 /**
6017 * Add a wearable action to this notification.
6018 *
6019 * <p>When wearable actions are added using this method, the set of actions that
6020 * show on a wearable device splits from devices that only show actions added
6021 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6022 * of which actions display on different devices.
6023 *
6024 * @param action the action to add to this notification
6025 * @return this object for method chaining
6026 * @see android.app.Notification.Action
6027 */
6028 public WearableExtender addAction(Action action) {
6029 mActions.add(action);
6030 return this;
6031 }
6032
6033 /**
6034 * Adds wearable actions to this notification.
6035 *
6036 * <p>When wearable actions are added using this method, the set of actions that
6037 * show on a wearable device splits from devices that only show actions added
6038 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6039 * of which actions display on different devices.
6040 *
6041 * @param actions the actions to add to this notification
6042 * @return this object for method chaining
6043 * @see android.app.Notification.Action
6044 */
6045 public WearableExtender addActions(List<Action> actions) {
6046 mActions.addAll(actions);
6047 return this;
6048 }
6049
6050 /**
6051 * Clear all wearable actions present on this builder.
6052 * @return this object for method chaining.
6053 * @see #addAction
6054 */
6055 public WearableExtender clearActions() {
6056 mActions.clear();
6057 return this;
6058 }
6059
6060 /**
6061 * Get the wearable actions present on this notification.
6062 */
6063 public List<Action> getActions() {
6064 return mActions;
6065 }
6066
6067 /**
6068 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006069 * this notification. The {@link PendingIntent} provided should be for an activity.
6070 *
6071 * <pre class="prettyprint">
6072 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6073 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6074 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6075 * Notification notif = new Notification.Builder(context)
6076 * .extend(new Notification.WearableExtender()
6077 * .setDisplayIntent(displayPendingIntent)
6078 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6079 * .build();</pre>
6080 *
6081 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006082 * should have an empty task affinity. It is also recommended to use the device
6083 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006084 *
6085 * <p>Example AndroidManifest.xml entry:
6086 * <pre class="prettyprint">
6087 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6088 * android:exported=&quot;true&quot;
6089 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006090 * android:taskAffinity=&quot;&quot;
6091 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006092 *
6093 * @param intent the {@link PendingIntent} for an activity
6094 * @return this object for method chaining
6095 * @see android.app.Notification.WearableExtender#getDisplayIntent
6096 */
6097 public WearableExtender setDisplayIntent(PendingIntent intent) {
6098 mDisplayIntent = intent;
6099 return this;
6100 }
6101
6102 /**
6103 * Get the intent to launch inside of an activity view when displaying this
6104 * notification. This {@code PendingIntent} should be for an activity.
6105 */
6106 public PendingIntent getDisplayIntent() {
6107 return mDisplayIntent;
6108 }
6109
6110 /**
6111 * Add an additional page of content to display with this notification. The current
6112 * notification forms the first page, and pages added using this function form
6113 * subsequent pages. This field can be used to separate a notification into multiple
6114 * sections.
6115 *
6116 * @param page the notification to add as another page
6117 * @return this object for method chaining
6118 * @see android.app.Notification.WearableExtender#getPages
6119 */
6120 public WearableExtender addPage(Notification page) {
6121 mPages.add(page);
6122 return this;
6123 }
6124
6125 /**
6126 * Add additional pages of content to display with this notification. The current
6127 * notification forms the first page, and pages added using this function form
6128 * subsequent pages. This field can be used to separate a notification into multiple
6129 * sections.
6130 *
6131 * @param pages a list of notifications
6132 * @return this object for method chaining
6133 * @see android.app.Notification.WearableExtender#getPages
6134 */
6135 public WearableExtender addPages(List<Notification> pages) {
6136 mPages.addAll(pages);
6137 return this;
6138 }
6139
6140 /**
6141 * Clear all additional pages present on this builder.
6142 * @return this object for method chaining.
6143 * @see #addPage
6144 */
6145 public WearableExtender clearPages() {
6146 mPages.clear();
6147 return this;
6148 }
6149
6150 /**
6151 * Get the array of additional pages of content for displaying this notification. The
6152 * current notification forms the first page, and elements within this array form
6153 * subsequent pages. This field can be used to separate a notification into multiple
6154 * sections.
6155 * @return the pages for this notification
6156 */
6157 public List<Notification> getPages() {
6158 return mPages;
6159 }
6160
6161 /**
6162 * Set a background image to be displayed behind the notification content.
6163 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6164 * will work with any notification style.
6165 *
6166 * @param background the background bitmap
6167 * @return this object for method chaining
6168 * @see android.app.Notification.WearableExtender#getBackground
6169 */
6170 public WearableExtender setBackground(Bitmap background) {
6171 mBackground = background;
6172 return this;
6173 }
6174
6175 /**
6176 * Get a background image to be displayed behind the notification content.
6177 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6178 * will work with any notification style.
6179 *
6180 * @return the background image
6181 * @see android.app.Notification.WearableExtender#setBackground
6182 */
6183 public Bitmap getBackground() {
6184 return mBackground;
6185 }
6186
6187 /**
6188 * Set an icon that goes with the content of this notification.
6189 */
6190 public WearableExtender setContentIcon(int icon) {
6191 mContentIcon = icon;
6192 return this;
6193 }
6194
6195 /**
6196 * Get an icon that goes with the content of this notification.
6197 */
6198 public int getContentIcon() {
6199 return mContentIcon;
6200 }
6201
6202 /**
6203 * Set the gravity that the content icon should have within the notification display.
6204 * Supported values include {@link android.view.Gravity#START} and
6205 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6206 * @see #setContentIcon
6207 */
6208 public WearableExtender setContentIconGravity(int contentIconGravity) {
6209 mContentIconGravity = contentIconGravity;
6210 return this;
6211 }
6212
6213 /**
6214 * Get the gravity that the content icon should have within the notification display.
6215 * Supported values include {@link android.view.Gravity#START} and
6216 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6217 * @see #getContentIcon
6218 */
6219 public int getContentIconGravity() {
6220 return mContentIconGravity;
6221 }
6222
6223 /**
6224 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006225 * this notification. This action will no longer display separately from the
6226 * notification's content.
6227 *
Griff Hazenca48d352014-05-28 22:37:13 -07006228 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006229 * set, although the list of available actions comes from the main notification and not
6230 * from the child page's notification.
6231 *
6232 * @param actionIndex The index of the action to hoist onto the current notification page.
6233 * If wearable actions were added to the main notification, this index
6234 * will apply to that list, otherwise it will apply to the regular
6235 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006236 */
6237 public WearableExtender setContentAction(int actionIndex) {
6238 mContentActionIndex = actionIndex;
6239 return this;
6240 }
6241
6242 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006243 * Get the index of the notification action, if any, that was specified as being clickable
6244 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006245 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006246 *
Griff Hazenca48d352014-05-28 22:37:13 -07006247 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006248 * set, although the list of available actions comes from the main notification and not
6249 * from the child page's notification.
6250 *
6251 * <p>If wearable specific actions were added to the main notification, this index will
6252 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006253 *
6254 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006255 */
6256 public int getContentAction() {
6257 return mContentActionIndex;
6258 }
6259
6260 /**
6261 * Set the gravity that this notification should have within the available viewport space.
6262 * Supported values include {@link android.view.Gravity#TOP},
6263 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6264 * The default value is {@link android.view.Gravity#BOTTOM}.
6265 */
6266 public WearableExtender setGravity(int gravity) {
6267 mGravity = gravity;
6268 return this;
6269 }
6270
6271 /**
6272 * Get the gravity that this notification should have within the available viewport space.
6273 * Supported values include {@link android.view.Gravity#TOP},
6274 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6275 * The default value is {@link android.view.Gravity#BOTTOM}.
6276 */
6277 public int getGravity() {
6278 return mGravity;
6279 }
6280
6281 /**
6282 * Set the custom size preset for the display of this notification out of the available
6283 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6284 * {@link #SIZE_LARGE}.
6285 * <p>Some custom size presets are only applicable for custom display notifications created
6286 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6287 * documentation for the preset in question. See also
6288 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6289 */
6290 public WearableExtender setCustomSizePreset(int sizePreset) {
6291 mCustomSizePreset = sizePreset;
6292 return this;
6293 }
6294
6295 /**
6296 * Get the custom size preset for the display of this notification out of the available
6297 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6298 * {@link #SIZE_LARGE}.
6299 * <p>Some custom size presets are only applicable for custom display notifications created
6300 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6301 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6302 */
6303 public int getCustomSizePreset() {
6304 return mCustomSizePreset;
6305 }
6306
6307 /**
6308 * Set the custom height in pixels for the display of this notification's content.
6309 * <p>This option is only available for custom display notifications created
6310 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6311 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6312 * {@link #getCustomContentHeight}.
6313 */
6314 public WearableExtender setCustomContentHeight(int height) {
6315 mCustomContentHeight = height;
6316 return this;
6317 }
6318
6319 /**
6320 * Get the custom height in pixels for the display of this notification's content.
6321 * <p>This option is only available for custom display notifications created
6322 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6323 * {@link #setCustomContentHeight}.
6324 */
6325 public int getCustomContentHeight() {
6326 return mCustomContentHeight;
6327 }
6328
6329 /**
6330 * Set whether the scrolling position for the contents of this notification should start
6331 * at the bottom of the contents instead of the top when the contents are too long to
6332 * display within the screen. Default is false (start scroll at the top).
6333 */
6334 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6335 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6336 return this;
6337 }
6338
6339 /**
6340 * Get whether the scrolling position for the contents of this notification should start
6341 * at the bottom of the contents instead of the top when the contents are too long to
6342 * display within the screen. Default is false (start scroll at the top).
6343 */
6344 public boolean getStartScrollBottom() {
6345 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6346 }
6347
6348 /**
6349 * Set whether the content intent is available when the wearable device is not connected
6350 * to a companion device. The user can still trigger this intent when the wearable device
6351 * is offline, but a visual hint will indicate that the content intent may not be available.
6352 * Defaults to true.
6353 */
6354 public WearableExtender setContentIntentAvailableOffline(
6355 boolean contentIntentAvailableOffline) {
6356 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6357 return this;
6358 }
6359
6360 /**
6361 * Get whether the content intent is available when the wearable device is not connected
6362 * to a companion device. The user can still trigger this intent when the wearable device
6363 * is offline, but a visual hint will indicate that the content intent may not be available.
6364 * Defaults to true.
6365 */
6366 public boolean getContentIntentAvailableOffline() {
6367 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6368 }
6369
6370 /**
6371 * Set a hint that this notification's icon should not be displayed.
6372 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6373 * @return this object for method chaining
6374 */
6375 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6376 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6377 return this;
6378 }
6379
6380 /**
6381 * Get a hint that this notification's icon should not be displayed.
6382 * @return {@code true} if this icon should not be displayed, false otherwise.
6383 * The default value is {@code false} if this was never set.
6384 */
6385 public boolean getHintHideIcon() {
6386 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6387 }
6388
6389 /**
6390 * Set a visual hint that only the background image of this notification should be
6391 * displayed, and other semantic content should be hidden. This hint is only applicable
6392 * to sub-pages added using {@link #addPage}.
6393 */
6394 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6395 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6396 return this;
6397 }
6398
6399 /**
6400 * Get a visual hint that only the background image of this notification should be
6401 * displayed, and other semantic content should be hidden. This hint is only applicable
6402 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6403 */
6404 public boolean getHintShowBackgroundOnly() {
6405 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6406 }
6407
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006408 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006409 * Set a hint that this notification's background should not be clipped if possible,
6410 * and should instead be resized to fully display on the screen, retaining the aspect
6411 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006412 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6413 * @return this object for method chaining
6414 */
6415 public WearableExtender setHintAvoidBackgroundClipping(
6416 boolean hintAvoidBackgroundClipping) {
6417 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6418 return this;
6419 }
6420
6421 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006422 * Get a hint that this notification's background should not be clipped if possible,
6423 * and should instead be resized to fully display on the screen, retaining the aspect
6424 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006425 * @return {@code true} if it's ok if the background is clipped on the screen, false
6426 * otherwise. The default value is {@code false} if this was never set.
6427 */
6428 public boolean getHintAvoidBackgroundClipping() {
6429 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6430 }
6431
6432 /**
6433 * Set a hint that the screen should remain on for at least this duration when
6434 * this notification is displayed on the screen.
6435 * @param timeout The requested screen timeout in milliseconds. Can also be either
6436 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6437 * @return this object for method chaining
6438 */
6439 public WearableExtender setHintScreenTimeout(int timeout) {
6440 mHintScreenTimeout = timeout;
6441 return this;
6442 }
6443
6444 /**
6445 * Get the duration, in milliseconds, that the screen should remain on for
6446 * when this notification is displayed.
6447 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6448 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6449 */
6450 public int getHintScreenTimeout() {
6451 return mHintScreenTimeout;
6452 }
6453
Alex Hills9ab3a232016-04-05 14:54:56 -04006454 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006455 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6456 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6457 * qr codes, as well as other simple black-and-white tickets.
6458 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6459 * @return this object for method chaining
6460 */
6461 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6462 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6463 return this;
6464 }
6465
6466 /**
6467 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6468 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6469 * qr codes, as well as other simple black-and-white tickets.
6470 * @return {@code true} if it should be displayed in ambient, false otherwise
6471 * otherwise. The default value is {@code false} if this was never set.
6472 */
6473 public boolean getHintAmbientBigPicture() {
6474 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6475 }
6476
6477 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006478 * Set a hint that this notification's content intent will launch an {@link Activity}
6479 * directly, telling the platform that it can generate the appropriate transitions.
6480 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6481 * an activity and transitions should be generated, false otherwise.
6482 * @return this object for method chaining
6483 */
6484 public WearableExtender setHintContentIntentLaunchesActivity(
6485 boolean hintContentIntentLaunchesActivity) {
6486 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6487 return this;
6488 }
6489
6490 /**
6491 * Get a hint that this notification's content intent will launch an {@link Activity}
6492 * directly, telling the platform that it can generate the appropriate transitions
6493 * @return {@code true} if the content intent will launch an activity and transitions should
6494 * be generated, false otherwise. The default value is {@code false} if this was never set.
6495 */
6496 public boolean getHintContentIntentLaunchesActivity() {
6497 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6498 }
6499
Nadia Benbernou948627e2016-04-14 14:41:08 -04006500 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006501 * Sets the dismissal id for this notification. If a notification is posted with a
6502 * dismissal id, then when that notification is canceled, notifications on other wearables
6503 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04006504 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006505 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04006506 * @param dismissalId the dismissal id of the notification.
6507 * @return this object for method chaining
6508 */
6509 public WearableExtender setDismissalId(String dismissalId) {
6510 mDismissalId = dismissalId;
6511 return this;
6512 }
6513
6514 /**
6515 * Returns the dismissal id of the notification.
6516 * @return the dismissal id of the notification or null if it has not been set.
6517 */
6518 public String getDismissalId() {
6519 return mDismissalId;
6520 }
6521
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006522 /**
6523 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
6524 * posted from a phone to provide finer-grained control on what notifications are bridged
6525 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
6526 * Features to Notifications</a> for more information.
6527 * @param bridgeTag the bridge tag of the notification.
6528 * @return this object for method chaining
6529 */
6530 public WearableExtender setBridgeTag(String bridgeTag) {
6531 mBridgeTag = bridgeTag;
6532 return this;
6533 }
6534
6535 /**
6536 * Returns the bridge tag of the notification.
6537 * @return the bridge tag or null if not present.
6538 */
6539 public String getBridgeTag() {
6540 return mBridgeTag;
6541 }
6542
Griff Hazen61a9e862014-05-22 16:05:19 -07006543 private void setFlag(int mask, boolean value) {
6544 if (value) {
6545 mFlags |= mask;
6546 } else {
6547 mFlags &= ~mask;
6548 }
6549 }
6550 }
6551
6552 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006553 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6554 * with car extensions:
6555 *
6556 * <ol>
6557 * <li>Create an {@link Notification.Builder}, setting any desired
6558 * properties.
6559 * <li>Create a {@link CarExtender}.
6560 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6561 * {@link CarExtender}.
6562 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6563 * to apply the extensions to a notification.
6564 * </ol>
6565 *
6566 * <pre class="prettyprint">
6567 * Notification notification = new Notification.Builder(context)
6568 * ...
6569 * .extend(new CarExtender()
6570 * .set*(...))
6571 * .build();
6572 * </pre>
6573 *
6574 * <p>Car extensions can be accessed on an existing notification by using the
6575 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6576 * to access values.
6577 */
6578 public static final class CarExtender implements Extender {
6579 private static final String TAG = "CarExtender";
6580
6581 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6582 private static final String EXTRA_LARGE_ICON = "large_icon";
6583 private static final String EXTRA_CONVERSATION = "car_conversation";
6584 private static final String EXTRA_COLOR = "app_color";
6585
6586 private Bitmap mLargeIcon;
6587 private UnreadConversation mUnreadConversation;
6588 private int mColor = Notification.COLOR_DEFAULT;
6589
6590 /**
6591 * Create a {@link CarExtender} with default options.
6592 */
6593 public CarExtender() {
6594 }
6595
6596 /**
6597 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6598 *
6599 * @param notif The notification from which to copy options.
6600 */
6601 public CarExtender(Notification notif) {
6602 Bundle carBundle = notif.extras == null ?
6603 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6604 if (carBundle != null) {
6605 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6606 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6607
6608 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6609 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6610 }
6611 }
6612
6613 /**
6614 * Apply car extensions to a notification that is being built. This is typically called by
6615 * the {@link Notification.Builder#extend(Notification.Extender)}
6616 * method of {@link Notification.Builder}.
6617 */
6618 @Override
6619 public Notification.Builder extend(Notification.Builder builder) {
6620 Bundle carExtensions = new Bundle();
6621
6622 if (mLargeIcon != null) {
6623 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6624 }
6625 if (mColor != Notification.COLOR_DEFAULT) {
6626 carExtensions.putInt(EXTRA_COLOR, mColor);
6627 }
6628
6629 if (mUnreadConversation != null) {
6630 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6631 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6632 }
6633
6634 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6635 return builder;
6636 }
6637
6638 /**
6639 * Sets the accent color to use when Android Auto presents the notification.
6640 *
6641 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6642 * to accent the displayed notification. However, not all colors are acceptable in an
6643 * automotive setting. This method can be used to override the color provided in the
6644 * notification in such a situation.
6645 */
Tor Norbye80756e32015-03-02 09:39:27 -08006646 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006647 mColor = color;
6648 return this;
6649 }
6650
6651 /**
6652 * Gets the accent color.
6653 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006654 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006655 */
Tor Norbye80756e32015-03-02 09:39:27 -08006656 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006657 public int getColor() {
6658 return mColor;
6659 }
6660
6661 /**
6662 * Sets the large icon of the car notification.
6663 *
6664 * If no large icon is set in the extender, Android Auto will display the icon
6665 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6666 *
6667 * @param largeIcon The large icon to use in the car notification.
6668 * @return This object for method chaining.
6669 */
6670 public CarExtender setLargeIcon(Bitmap largeIcon) {
6671 mLargeIcon = largeIcon;
6672 return this;
6673 }
6674
6675 /**
6676 * Gets the large icon used in this car notification, or null if no icon has been set.
6677 *
6678 * @return The large icon for the car notification.
6679 * @see CarExtender#setLargeIcon
6680 */
6681 public Bitmap getLargeIcon() {
6682 return mLargeIcon;
6683 }
6684
6685 /**
6686 * Sets the unread conversation in a message notification.
6687 *
6688 * @param unreadConversation The unread part of the conversation this notification conveys.
6689 * @return This object for method chaining.
6690 */
6691 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6692 mUnreadConversation = unreadConversation;
6693 return this;
6694 }
6695
6696 /**
6697 * Returns the unread conversation conveyed by this notification.
6698 * @see #setUnreadConversation(UnreadConversation)
6699 */
6700 public UnreadConversation getUnreadConversation() {
6701 return mUnreadConversation;
6702 }
6703
6704 /**
6705 * A class which holds the unread messages from a conversation.
6706 */
6707 public static class UnreadConversation {
6708 private static final String KEY_AUTHOR = "author";
6709 private static final String KEY_TEXT = "text";
6710 private static final String KEY_MESSAGES = "messages";
6711 private static final String KEY_REMOTE_INPUT = "remote_input";
6712 private static final String KEY_ON_REPLY = "on_reply";
6713 private static final String KEY_ON_READ = "on_read";
6714 private static final String KEY_PARTICIPANTS = "participants";
6715 private static final String KEY_TIMESTAMP = "timestamp";
6716
6717 private final String[] mMessages;
6718 private final RemoteInput mRemoteInput;
6719 private final PendingIntent mReplyPendingIntent;
6720 private final PendingIntent mReadPendingIntent;
6721 private final String[] mParticipants;
6722 private final long mLatestTimestamp;
6723
6724 UnreadConversation(String[] messages, RemoteInput remoteInput,
6725 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6726 String[] participants, long latestTimestamp) {
6727 mMessages = messages;
6728 mRemoteInput = remoteInput;
6729 mReadPendingIntent = readPendingIntent;
6730 mReplyPendingIntent = replyPendingIntent;
6731 mParticipants = participants;
6732 mLatestTimestamp = latestTimestamp;
6733 }
6734
6735 /**
6736 * Gets the list of messages conveyed by this notification.
6737 */
6738 public String[] getMessages() {
6739 return mMessages;
6740 }
6741
6742 /**
6743 * Gets the remote input that will be used to convey the response to a message list, or
6744 * null if no such remote input exists.
6745 */
6746 public RemoteInput getRemoteInput() {
6747 return mRemoteInput;
6748 }
6749
6750 /**
6751 * Gets the pending intent that will be triggered when the user replies to this
6752 * notification.
6753 */
6754 public PendingIntent getReplyPendingIntent() {
6755 return mReplyPendingIntent;
6756 }
6757
6758 /**
6759 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6760 * in this object's message list.
6761 */
6762 public PendingIntent getReadPendingIntent() {
6763 return mReadPendingIntent;
6764 }
6765
6766 /**
6767 * Gets the participants in the conversation.
6768 */
6769 public String[] getParticipants() {
6770 return mParticipants;
6771 }
6772
6773 /**
6774 * Gets the firs participant in the conversation.
6775 */
6776 public String getParticipant() {
6777 return mParticipants.length > 0 ? mParticipants[0] : null;
6778 }
6779
6780 /**
6781 * Gets the timestamp of the conversation.
6782 */
6783 public long getLatestTimestamp() {
6784 return mLatestTimestamp;
6785 }
6786
6787 Bundle getBundleForUnreadConversation() {
6788 Bundle b = new Bundle();
6789 String author = null;
6790 if (mParticipants != null && mParticipants.length > 1) {
6791 author = mParticipants[0];
6792 }
6793 Parcelable[] messages = new Parcelable[mMessages.length];
6794 for (int i = 0; i < messages.length; i++) {
6795 Bundle m = new Bundle();
6796 m.putString(KEY_TEXT, mMessages[i]);
6797 m.putString(KEY_AUTHOR, author);
6798 messages[i] = m;
6799 }
6800 b.putParcelableArray(KEY_MESSAGES, messages);
6801 if (mRemoteInput != null) {
6802 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6803 }
6804 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6805 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6806 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6807 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6808 return b;
6809 }
6810
6811 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6812 if (b == null) {
6813 return null;
6814 }
6815 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6816 String[] messages = null;
6817 if (parcelableMessages != null) {
6818 String[] tmp = new String[parcelableMessages.length];
6819 boolean success = true;
6820 for (int i = 0; i < tmp.length; i++) {
6821 if (!(parcelableMessages[i] instanceof Bundle)) {
6822 success = false;
6823 break;
6824 }
6825 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6826 if (tmp[i] == null) {
6827 success = false;
6828 break;
6829 }
6830 }
6831 if (success) {
6832 messages = tmp;
6833 } else {
6834 return null;
6835 }
6836 }
6837
6838 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6839 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6840
6841 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6842
6843 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6844 if (participants == null || participants.length != 1) {
6845 return null;
6846 }
6847
6848 return new UnreadConversation(messages,
6849 remoteInput,
6850 onReply,
6851 onRead,
6852 participants, b.getLong(KEY_TIMESTAMP));
6853 }
6854 };
6855
6856 /**
6857 * Builder class for {@link CarExtender.UnreadConversation} objects.
6858 */
6859 public static class Builder {
6860 private final List<String> mMessages = new ArrayList<String>();
6861 private final String mParticipant;
6862 private RemoteInput mRemoteInput;
6863 private PendingIntent mReadPendingIntent;
6864 private PendingIntent mReplyPendingIntent;
6865 private long mLatestTimestamp;
6866
6867 /**
6868 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6869 *
6870 * @param name The name of the other participant in the conversation.
6871 */
6872 public Builder(String name) {
6873 mParticipant = name;
6874 }
6875
6876 /**
6877 * Appends a new unread message to the list of messages for this conversation.
6878 *
6879 * The messages should be added from oldest to newest.
6880 *
6881 * @param message The text of the new unread message.
6882 * @return This object for method chaining.
6883 */
6884 public Builder addMessage(String message) {
6885 mMessages.add(message);
6886 return this;
6887 }
6888
6889 /**
6890 * Sets the pending intent and remote input which will convey the reply to this
6891 * notification.
6892 *
6893 * @param pendingIntent The pending intent which will be triggered on a reply.
6894 * @param remoteInput The remote input parcelable which will carry the reply.
6895 * @return This object for method chaining.
6896 *
6897 * @see CarExtender.UnreadConversation#getRemoteInput
6898 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6899 */
6900 public Builder setReplyAction(
6901 PendingIntent pendingIntent, RemoteInput remoteInput) {
6902 mRemoteInput = remoteInput;
6903 mReplyPendingIntent = pendingIntent;
6904
6905 return this;
6906 }
6907
6908 /**
6909 * Sets the pending intent that will be sent once the messages in this notification
6910 * are read.
6911 *
6912 * @param pendingIntent The pending intent to use.
6913 * @return This object for method chaining.
6914 */
6915 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6916 mReadPendingIntent = pendingIntent;
6917 return this;
6918 }
6919
6920 /**
6921 * Sets the timestamp of the most recent message in an unread conversation.
6922 *
6923 * If a messaging notification has been posted by your application and has not
6924 * yet been cancelled, posting a later notification with the same id and tag
6925 * but without a newer timestamp may result in Android Auto not displaying a
6926 * heads up notification for the later notification.
6927 *
6928 * @param timestamp The timestamp of the most recent message in the conversation.
6929 * @return This object for method chaining.
6930 */
6931 public Builder setLatestTimestamp(long timestamp) {
6932 mLatestTimestamp = timestamp;
6933 return this;
6934 }
6935
6936 /**
6937 * Builds a new unread conversation object.
6938 *
6939 * @return The new unread conversation object.
6940 */
6941 public UnreadConversation build() {
6942 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6943 String[] participants = { mParticipant };
6944 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6945 mReadPendingIntent, participants, mLatestTimestamp);
6946 }
6947 }
6948 }
6949
6950 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006951 * Get an array of Notification objects from a parcelable array bundle field.
6952 * Update the bundle to have a typed array so fetches in the future don't need
6953 * to do an array copy.
6954 */
6955 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6956 Parcelable[] array = bundle.getParcelableArray(key);
6957 if (array instanceof Notification[] || array == null) {
6958 return (Notification[]) array;
6959 }
6960 Notification[] typedArray = Arrays.copyOf(array, array.length,
6961 Notification[].class);
6962 bundle.putParcelableArray(key, typedArray);
6963 return typedArray;
6964 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006965
6966 private static class BuilderRemoteViews extends RemoteViews {
6967 public BuilderRemoteViews(Parcel parcel) {
6968 super(parcel);
6969 }
6970
Kenny Guy77320062014-08-27 21:37:15 +01006971 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6972 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006973 }
6974
6975 @Override
6976 public BuilderRemoteViews clone() {
6977 Parcel p = Parcel.obtain();
6978 writeToParcel(p, 0);
6979 p.setDataPosition(0);
6980 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6981 p.recycle();
6982 return brv;
6983 }
6984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985}