blob: 46e597aada56af5762385bb4ee2815613e841963 [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
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060025import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040026import android.annotation.SdkConstant;
27import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040028import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
30import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010031import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040032import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020033import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050034import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020035import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040036import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010037import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070038import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010039import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040041import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040042import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070043import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080044import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070045import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040047import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020048import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050049import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070050import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Parcel;
52import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040053import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080054import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070055import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070056import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080057import android.text.SpannableStringBuilder;
58import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070061import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080062import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070063import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080064import android.text.style.RelativeSizeSpan;
65import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070066import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040067import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050068import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070069import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080070import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080071import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070072import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070073import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.RemoteViews;
75
Griff Hazen959591e2014-05-15 22:26:18 -070076import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070077import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070078import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070079import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080080import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070081
Tor Norbyed9273d62013-05-30 15:59:53 -070082import java.lang.annotation.Retention;
83import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020084import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050085import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070086import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070087import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070088import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050089import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091/**
92 * A class that represents how a persistent notification is to be presented to
93 * the user using the {@link android.app.NotificationManager}.
94 *
Joe Onoratocb109a02011-01-18 17:57:41 -080095 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
96 * easier to construct Notifications.</p>
97 *
Joe Fernandez558459f2011-10-13 16:47:36 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For a guide to creating notifications, read the
101 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
102 * developer guide.</p>
103 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 */
105public class Notification implements Parcelable
106{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400107 private static final String TAG = "Notification";
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400110 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400111 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400112 */
113 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
114 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
115 = "android.intent.category.NOTIFICATION_PREFERENCES";
116
117 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500118 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
119 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500121 */
122 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
123
124 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400125 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
126 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
127 * that can be used to narrow down what settings should be shown in the target app.
128 */
129 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
130
131 /**
132 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
133 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
134 * that can be used to narrow down what settings should be shown in the target app.
135 */
136 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
137
138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * Use all default values (where applicable).
140 */
141 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
144 * Use the default notification sound. This will ignore any given
145 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146 *
Chris Wren47c20a12014-06-18 17:27:29 -0400147 * <p>
148 * A notification that is noisy is more likely to be presented as a heads-up notification.
149 * </p>
150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152 */
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final int DEFAULT_SOUND = 1;
155
156 /**
157 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700159 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 *
Chris Wren47c20a12014-06-18 17:27:29 -0400161 * <p>
162 * A notification that vibrates is more likely to be presented as a heads-up notification.
163 * </p>
164 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 */
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
171 * Use the default notification lights. This will ignore the
172 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
173 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 */
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200181 * Maximum length of CharSequences accepted by Builder and friends.
182 *
183 * <p>
184 * Avoids spamming the system with overly large strings such as full e-mails.
185 */
186 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
187
188 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800189 * Maximum entries of reply text that are accepted by Builder and friends.
190 */
191 private static final int MAX_REPLY_HISTORY = 5;
192
193 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500194 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800195 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196 * Default value: {@link System#currentTimeMillis() Now}.
197 *
198 * Choose a timestamp that will be most relevant to the user. For most finite events, this
199 * corresponds to the time the event happened (or will happen, in the case of events that have
200 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800201 * timestamped according to when the activity began.
202 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500203 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500205 * <ul>
206 * <li>Notification of a new chat message should be stamped when the message was received.</li>
207 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
208 * <li>Notification of a completed file download should be stamped when the download finished.</li>
209 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
210 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
211 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800212 * </ul>
213 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700214 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
215 * anymore by default and must be opted into by using
216 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 */
218 public long when;
219
220 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700221 * The creation time of the notification
222 */
223 private long creationTime;
224
225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400227 *
228 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Dan Sandler86647982015-05-13 23:41:13 -0400230 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700231 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public int icon;
233
234 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800235 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
236 * leave it at its default value of 0.
237 *
238 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700239 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800240 */
241 public int iconLevel;
242
243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500244 * The number of events that this notification represents. For example, in a new mail
245 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800246 *
Julia Reynolds30331982017-04-27 10:12:50 -0400247 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500248 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
249 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
Julia Reynolds30331982017-04-27 10:12:50 -0400251 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 /**
254 * The intent to execute when the expanded status entry is clicked. If
255 * this is an activity, it must include the
256 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800257 * that you take care of task management as described in the
258 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800259 * Stack</a> document. In particular, make sure to read the notification section
260 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
261 * Notifications</a> for the correct ways to launch an application from a
262 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 */
264 public PendingIntent contentIntent;
265
266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500267 * The intent to execute when the notification is explicitly dismissed by the user, either with
268 * the "Clear All" button or by swiping it away individually.
269 *
270 * This probably shouldn't be launching an activity since several of those will be sent
271 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public PendingIntent deleteIntent;
274
275 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700276 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800277 *
Chris Wren47c20a12014-06-18 17:27:29 -0400278 * <p>
279 * The system UI may choose to display a heads-up notification, instead of
280 * launching this intent, while the user is using the device.
281 * </p>
282 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800283 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400284 */
285 public PendingIntent fullScreenIntent;
286
287 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400288 * Text that summarizes this notification for accessibility services.
289 *
290 * As of the L release, this text is no longer shown on screen, but it is still useful to
291 * accessibility services (where it serves as an audible announcement of the notification's
292 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400293 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800294 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
296 public CharSequence tickerText;
297
298 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400299 * Formerly, a view showing the {@link #tickerText}.
300 *
301 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400302 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400303 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400305
306 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400307 * The view that will represent this notification in the notification list (which is pulled
308 * down from the status bar).
309 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500310 * As of N, this field may be null. The notification view is determined by the inputs
311 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 public RemoteViews contentView;
316
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400317 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400318 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400319 * opportunity to show more detail. The system UI may choose to show this
320 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400321 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500322 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400323 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
324 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400325 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400326 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400327 public RemoteViews bigContentView;
328
Chris Wren8fd39ec2014-02-27 17:43:26 -0500329
330 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400331 * A medium-format version of {@link #contentView}, providing the Notification an
332 * opportunity to add action buttons to contentView. At its discretion, the system UI may
333 * choose to show this as a heads-up notification, which will pop up so the user can see
334 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500336 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400337 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
338 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500339 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400340 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500341 public RemoteViews headsUpContentView;
342
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400343 /**
Dan Sandler86647982015-05-13 23:41:13 -0400344 * A large bitmap to be shown in the notification content area.
345 *
346 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Dan Sandler86647982015-05-13 23:41:13 -0400348 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800349 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
351 /**
352 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400355 * A notification that is noisy is more likely to be presented as a heads-up notification.
356 * </p>
357 *
358 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500361 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500363 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 public Uri sound;
365
366 /**
367 * Use this constant as the value for audioStreamType to request that
368 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700369 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400370 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700373 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public static final int STREAM_DEFAULT = -1;
375
376 /**
377 * The audio stream type to use when playing the sound.
378 * Should be one of the STREAM_ constants from
379 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400380 *
381 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public int audioStreamType = STREAM_DEFAULT;
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400387 * The default value of {@link #audioAttributes}.
388 */
389 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
390 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
391 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
392 .build();
393
394 /**
395 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500396 *
397 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400398 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500399 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400400 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
401
402 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 * The pattern with which to vibrate.
404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * <p>
406 * To vibrate the default pattern, see {@link #defaults}.
407 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public long[] vibrate;
414
415 /**
416 * The color of the led. The hardware will do its best approximation.
417 *
418 * @see #FLAG_SHOW_LIGHTS
419 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Tor Norbye80756e32015-03-02 09:39:27 -0800422 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500423 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 public int ledARGB;
425
426 /**
427 * The number of milliseconds for the LED to be on while it's flashing.
428 * The hardware will do its best approximation.
429 *
430 * @see #FLAG_SHOW_LIGHTS
431 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500434 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 public int ledOnMS;
436
437 /**
438 * The number of milliseconds for the LED to be off while it's flashing.
439 * The hardware will do its best approximation.
440 *
441 * @see #FLAG_SHOW_LIGHTS
442 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 *
444 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500446 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 public int ledOffMS;
448
449 /**
450 * Specifies which values should be taken from the defaults.
451 * <p>
452 * To set, OR the desired from {@link #DEFAULT_SOUND},
453 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
454 * values, use {@link #DEFAULT_ALL}.
455 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 *
457 * @deprecated use {@link NotificationChannel#getSound()} and
458 * {@link NotificationChannel#shouldShowLights()} and
459 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public int defaults;
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
466 * set if you want the LED on for this notification.
467 * <ul>
468 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
469 * or 0 for both ledOnMS and ledOffMS.</li>
470 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
471 * <li>To flash the LED, pass the number of milliseconds that it should
472 * be on and off to ledOnMS and ledOffMS.</li>
473 * </ul>
474 * <p>
475 * Since hardware varies, you are not guaranteed that any of the values
476 * you pass are honored exactly. Use the system defaults (TODO) if possible
477 * because they will be set to values that work on any given hardware.
478 * <p>
479 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500480 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
485
486 /**
487 * Bit to be bitwise-ored into the {@link #flags} field that should be
488 * set if this notification is in reference to something that is ongoing,
489 * like a phone call. It should not be set if this notification is in
490 * reference to something that happened at a particular point in time,
491 * like a missed phone call.
492 */
493 public static final int FLAG_ONGOING_EVENT = 0x00000002;
494
495 /**
496 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700497 * the audio will be repeated until the notification is
498 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 */
500 public static final int FLAG_INSISTENT = 0x00000004;
501
502 /**
503 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700504 * set if you would only like the sound, vibrate and ticker to be played
505 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
507 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
508
509 /**
510 * Bit to be bitwise-ored into the {@link #flags} field that should be
511 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500512 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 */
514 public static final int FLAG_AUTO_CANCEL = 0x00000010;
515
516 /**
517 * Bit to be bitwise-ored into the {@link #flags} field that should be
518 * set if the notification should not be canceled when the user clicks
519 * the Clear all button.
520 */
521 public static final int FLAG_NO_CLEAR = 0x00000020;
522
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700523 /**
524 * Bit to be bitwise-ored into the {@link #flags} field that should be
525 * set if this notification represents a currently running service. This
526 * will normally be set for you by {@link Service#startForeground}.
527 */
528 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
529
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400530 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500531 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400534 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700535 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400537
Griff Hazendfcb0802014-02-11 12:00:00 -0800538 /**
539 * Bit to be bitswise-ored into the {@link #flags} field that should be
540 * set if this notification is relevant to the current device only
541 * and it is not recommended that it bridge to other devices.
542 */
543 public static final int FLAG_LOCAL_ONLY = 0x00000100;
544
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700545 /**
546 * Bit to be bitswise-ored into the {@link #flags} field that should be
547 * set if this notification is the group summary for a group of notifications.
548 * Grouped notifications may display in a cluster or stack on devices which
549 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
550 */
551 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
552
Julia Reynoldse46bb372016-03-17 11:05:58 -0400553 /**
554 * Bit to be bitswise-ored into the {@link #flags} field that should be
555 * set if this notification is the group summary for an auto-group of notifications.
556 *
557 * @hide
558 */
559 @SystemApi
560 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
561
Julia Reynoldsd71c5a92017-06-30 13:34:01 -0400562 /**
563 * @hide
564 */
565 public static final int FLAG_CAN_COLORIZE = 0x00000800;
566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public int flags;
568
Tor Norbyed9273d62013-05-30 15:59:53 -0700569 /** @hide */
570 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
571 @Retention(RetentionPolicy.SOURCE)
572 public @interface Priority {}
573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 * Default notification {@link #priority}. If your application does not prioritize its own
576 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500577 *
578 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500580 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 public static final int PRIORITY_DEFAULT = 0;
582
583 /**
584 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
585 * items smaller, or at a different position in the list, compared with your app's
586 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500587 *
588 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500590 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 public static final int PRIORITY_LOW = -1;
592
593 /**
594 * Lowest {@link #priority}; these items might not be shown to the user except under special
595 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 *
597 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500598 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500599 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500600 public static final int PRIORITY_MIN = -2;
601
602 /**
603 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
604 * show these items larger, or at a different position in notification lists, compared with
605 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 *
607 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500608 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500609 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500610 public static final int PRIORITY_HIGH = 1;
611
612 /**
613 * Highest {@link #priority}, for your application's most important items that require the
614 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500615 *
616 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500618 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 public static final int PRIORITY_MAX = 2;
620
621 /**
622 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800623 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 * Priority is an indication of how much of the user's valuable attention should be consumed by
625 * this notification. Low-priority notifications may be hidden from the user in certain
626 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500627 * system will make a determination about how to interpret this priority when presenting
628 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400629 *
630 * <p>
631 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
632 * as a heads-up notification.
633 * </p>
634 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700637 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500638 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800640
Dan Sandler26e81cf2014-05-06 10:01:27 -0400641 /**
642 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
643 * to be applied by the standard Style templates when presenting this notification.
644 *
645 * The current template design constructs a colorful header image by overlaying the
646 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
647 * ignored.
648 */
Tor Norbye80756e32015-03-02 09:39:27 -0800649 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400650 public int color = COLOR_DEFAULT;
651
652 /**
653 * Special value of {@link #color} telling the system not to decorate this notification with
654 * any special color but instead use default colors when presenting this notification.
655 */
Tor Norbye80756e32015-03-02 09:39:27 -0800656 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400657 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600658
659 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800660 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800661 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800662 */
663 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800664 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800665
666 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700667 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
668 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600669 * lockscreen).
670 *
671 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
672 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
673 * shown in all situations, but the contents are only available if the device is unlocked for
674 * the appropriate user.
675 *
676 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
677 * can be read even in an "insecure" context (that is, above a secure lockscreen).
678 * To modify the public version of this notification—for example, to redact some portions—see
679 * {@link Builder#setPublicVersion(Notification)}.
680 *
681 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
682 * and ticker until the user has bypassed the lockscreen.
683 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600684 public @Visibility int visibility;
685
686 /** @hide */
687 @IntDef(prefix = { "VISIBILITY_" }, value = {
688 VISIBILITY_PUBLIC,
689 VISIBILITY_PRIVATE,
690 VISIBILITY_SECRET,
691 })
692 @Retention(RetentionPolicy.SOURCE)
693 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600694
Griff Hazenfc3922d2014-08-20 11:56:44 -0700695 /**
696 * Notification visibility: Show this notification in its entirety on all lockscreens.
697 *
698 * {@see #visibility}
699 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600700 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700701
702 /**
703 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
704 * private information on secure lockscreens.
705 *
706 * {@see #visibility}
707 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600708 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700709
710 /**
711 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
712 *
713 * {@see #visibility}
714 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600715 public static final int VISIBILITY_SECRET = -1;
716
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500717 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400718 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400720 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500721
722 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400723 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500724 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400725 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726
727 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400728 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400730 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500731
732 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400733 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500734 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500736
737 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400738 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500739 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400740 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500741
742 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400743 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500744 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400745 public static final String CATEGORY_ALARM = "alarm";
746
747 /**
748 * Notification category: progress of a long-running background operation.
749 */
750 public static final String CATEGORY_PROGRESS = "progress";
751
752 /**
753 * Notification category: social network or sharing update.
754 */
755 public static final String CATEGORY_SOCIAL = "social";
756
757 /**
758 * Notification category: error in background operation or authentication status.
759 */
760 public static final String CATEGORY_ERROR = "err";
761
762 /**
763 * Notification category: media transport control for playback.
764 */
765 public static final String CATEGORY_TRANSPORT = "transport";
766
767 /**
768 * Notification category: system or device status update. Reserved for system use.
769 */
770 public static final String CATEGORY_SYSTEM = "sys";
771
772 /**
773 * Notification category: indication of running background service.
774 */
775 public static final String CATEGORY_SERVICE = "service";
776
777 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400778 * Notification category: a specific, timely recommendation for a single thing.
779 * For example, a news app might want to recommend a news story it believes the user will
780 * want to read next.
781 */
782 public static final String CATEGORY_RECOMMENDATION = "recommendation";
783
784 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 * Notification category: ongoing information about device or contextual status.
786 */
787 public static final String CATEGORY_STATUS = "status";
788
789 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400790 * Notification category: user-scheduled reminder.
791 */
792 public static final String CATEGORY_REMINDER = "reminder";
793
794 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
796 * that best describes this Notification. May be used by the system for ranking and filtering.
797 */
798 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700800 private String mGroupKey;
801
802 /**
803 * Get the key used to group this notification into a cluster or stack
804 * with other notifications on devices which support such rendering.
805 */
806 public String getGroup() {
807 return mGroupKey;
808 }
809
810 private String mSortKey;
811
812 /**
813 * Get a sort key that orders this notification among other notifications from the
814 * same package. This can be useful if an external sort was already applied and an app
815 * would like to preserve this. Notifications will be sorted lexicographically using this
816 * value, although providing different priorities in addition to providing sort key may
817 * cause this value to be ignored.
818 *
819 * <p>This sort key can also be used to order members of a notification group. See
820 * {@link Builder#setGroup}.
821 *
822 * @see String#compareTo(String)
823 */
824 public String getSortKey() {
825 return mSortKey;
826 }
827
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500828 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400829 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400830 * <p>
831 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
832 * APIs, and are intended to be used by
833 * {@link android.service.notification.NotificationListenerService} implementations to extract
834 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500835 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400836 public Bundle extras = new Bundle();
837
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700839 * All pending intents in the notification as the system needs to be able to access them but
840 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700841 * custom parcelable objects.
842 *
843 * @hide
844 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700845 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700846
847 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700848 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
849 * pending intents inside of it, so only those will get the behavior.
850 *
851 * @hide
852 */
853 static public IBinder whitelistToken;
854
855 /**
856 * Must be set by a process to start associating tokens with Notification objects
857 * coming in to it. This is set by NotificationManagerService.
858 *
859 * @hide
860 */
861 static public IBinder processWhitelistToken;
862
863 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864 * {@link #extras} key: this is the title of the notification,
865 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
866 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500867 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400868
869 /**
870 * {@link #extras} key: this is the title of the notification when shown in expanded form,
871 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
872 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400873 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874
875 /**
876 * {@link #extras} key: this is the main text payload, as supplied to
877 * {@link Builder#setContentText(CharSequence)}.
878 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500879 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880
881 /**
882 * {@link #extras} key: this is a third line of text, as supplied to
883 * {@link Builder#setSubText(CharSequence)}.
884 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400885 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400886
887 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800888 * {@link #extras} key: this is the remote input history, as supplied to
889 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700890 *
891 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
892 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
893 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
894 * notifications once the other party has responded).
895 *
896 * The extra with this key is of type CharSequence[] and contains the most recent entry at
897 * the 0 index, the second most recent at the 1 index, etc.
898 *
899 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800900 */
901 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
902
903 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400904 * {@link #extras} key: this is a small piece of additional text as supplied to
905 * {@link Builder#setContentInfo(CharSequence)}.
906 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400907 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400908
909 /**
910 * {@link #extras} key: this is a line of summary information intended to be shown
911 * alongside expanded notifications, as supplied to (e.g.)
912 * {@link BigTextStyle#setSummaryText(CharSequence)}.
913 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400914 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915
916 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200917 * {@link #extras} key: this is the longer text shown in the big form of a
918 * {@link BigTextStyle} notification, as supplied to
919 * {@link BigTextStyle#bigText(CharSequence)}.
920 */
921 public static final String EXTRA_BIG_TEXT = "android.bigText";
922
923 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
925 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400926 *
927 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400929 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500930 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931
932 /**
933 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
934 * notification payload, as
935 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400936 *
937 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400938 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400939 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400940 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400941
942 /**
943 * {@link #extras} key: this is a bitmap to be used instead of the one from
944 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
945 * shown in its expanded form, as supplied to
946 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
947 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400948 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949
950 /**
951 * {@link #extras} key: this is the progress value supplied to
952 * {@link Builder#setProgress(int, int, boolean)}.
953 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400954 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400955
956 /**
957 * {@link #extras} key: this is the maximum value supplied to
958 * {@link Builder#setProgress(int, int, boolean)}.
959 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400960 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400961
962 /**
963 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
964 * {@link Builder#setProgress(int, int, boolean)}.
965 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400966 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967
968 /**
969 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
970 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
971 * {@link Builder#setUsesChronometer(boolean)}.
972 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400973 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400974
975 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800976 * {@link #extras} key: whether the chronometer set on the notification should count down
977 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700978 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800979 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700980 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800981
982 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983 * {@link #extras} key: whether {@link #when} should be shown,
984 * as supplied to {@link Builder#setShowWhen(boolean)}.
985 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400986 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400987
988 /**
989 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
990 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
991 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400992 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400993
994 /**
995 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
996 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
997 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400998 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400999
1000 /**
1001 * {@link #extras} key: A string representing the name of the specific
1002 * {@link android.app.Notification.Style} used to create this notification.
1003 */
Chris Wren91ad5632013-06-05 15:05:57 -04001004 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001005
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001006 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001007 * {@link #extras} key: A String array containing the people that this notification relates to,
1008 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001010 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001011
1012 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001013 * Allow certain system-generated notifications to appear before the device is provisioned.
1014 * Only available to notifications coming from the android package.
1015 * @hide
1016 */
Maurice Lam96c10032017-03-29 15:42:38 -07001017 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001018 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001019 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1020
1021 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001022 * {@link #extras} key: A
1023 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1024 * in the background when the notification is selected. The URI must point to an image stream
1025 * suitable for passing into
1026 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1027 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1028 * URI used for this purpose must require no permissions to read the image data.
1029 */
1030 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1031
1032 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001033 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001034 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001035 * {@link android.app.Notification.MediaStyle} notification.
1036 */
1037 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1038
1039 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001040 * {@link #extras} key: the indices of actions to be shown in the compact view,
1041 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1042 */
1043 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1044
Christoph Studer943aa672014-08-03 20:31:16 +02001045 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001046 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1047 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001048 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1049 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001050 */
1051 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1052
1053 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001054 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001055 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001056 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001057 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001058
1059 /**
1060 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1061 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001062 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1063 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001064 */
1065 public static final String EXTRA_MESSAGES = "android.messages";
1066
1067 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001068 * {@link #extras} key: an array of
1069 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1070 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1071 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1072 * array of bundles.
1073 */
1074 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1075
1076 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001077 * {@link #extras} key: whether the notification should be colorized as
1078 * supplied to {@link Builder#setColorized(boolean)}}.
1079 */
1080 public static final String EXTRA_COLORIZED = "android.colorized";
1081
1082 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001083 * @hide
1084 */
1085 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1086
Selim Cinek247fa012016-02-18 09:50:48 -08001087 /**
1088 * @hide
1089 */
1090 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1091
Shane Brennan472a3b32016-12-12 15:28:10 -08001092 /**
1093 * {@link #extras} key: the audio contents of this notification.
1094 *
1095 * This is for use when rendering the notification on an audio-focused interface;
1096 * the audio contents are a complete sound sample that contains the contents/body of the
1097 * notification. This may be used in substitute of a Text-to-Speech reading of the
1098 * notification. For example if the notification represents a voice message this should point
1099 * to the audio of that message.
1100 *
1101 * The data stored under this key should be a String representation of a Uri that contains the
1102 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1103 *
1104 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1105 * has a field for holding data URI. That field can be used for audio.
1106 * See {@code Message#setData}.
1107 *
1108 * Example usage:
1109 * <pre>
1110 * {@code
1111 * Notification.Builder myBuilder = (build your Notification as normal);
1112 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1113 * }
1114 * </pre>
1115 */
1116 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1117
Dan Sandler80eaa592016-04-14 23:34:54 -04001118 /** @hide */
1119 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001120 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001121 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1122
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001123 /**
1124 * This is set on the notification shown by the activity manager about all apps
1125 * running in the background. It indicates that the notification should be shown
1126 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1127 * notification currently visible to the user. This is a string array of all
1128 * package names of the apps.
1129 * @hide
1130 */
1131 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1132
Dan Sandlerd63f9322015-05-06 15:18:49 -04001133 private Icon mSmallIcon;
1134 private Icon mLargeIcon;
1135
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001136 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001137 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001138
Julia Reynolds13d898c2017-02-02 12:22:05 -05001139 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001140 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001141
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001142 /** @hide */
1143 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1144 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1145 })
1146 @Retention(RetentionPolicy.SOURCE)
1147 public @interface GroupAlertBehavior {}
1148
1149 /**
1150 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1151 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1152 * notification will not be muted when it is in a group.
1153 */
1154 public static final int GROUP_ALERT_ALL = 0;
1155
1156 /**
1157 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1158 * notification in a group should be silenced (no sound or vibration) even if they are posted
1159 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
1160 * mute this notification if this notification is a group child.
1161 *
1162 * <p> For example, you might want to use this constant if you post a number of children
1163 * notifications at once (say, after a periodic sync), and only need to notify the user
1164 * audibly once.
1165 */
1166 public static final int GROUP_ALERT_SUMMARY = 1;
1167
1168 /**
1169 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1170 * notification in a group should be silenced (no sound or vibration) even if they are
1171 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1172 * to mute this notification if this notification is a group summary.
1173 *
1174 * <p>For example, you might want to use this constant if only the children notifications
1175 * in your group have content and the summary is only used to visually group notifications.
1176 */
1177 public static final int GROUP_ALERT_CHILDREN = 2;
1178
Julia Reynolds2f431e22017-06-07 14:12:09 +00001179 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001180
Julia Reynolds13d898c2017-02-02 12:22:05 -05001181 /**
1182 * If this notification is being shown as a badge, always show as a number.
1183 */
1184 public static final int BADGE_ICON_NONE = 0;
1185
1186 /**
1187 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1188 * represent this notification.
1189 */
1190 public static final int BADGE_ICON_SMALL = 1;
1191
1192 /**
1193 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1194 * represent this notification.
1195 */
1196 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001197 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001198
Chris Wren51c75102013-07-16 20:49:17 -04001199 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001200 * Structure to encapsulate a named action that can be shown as part of this notification.
1201 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1202 * selected by the user.
1203 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001204 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1205 * or {@link Notification.Builder#addAction(Notification.Action)}
1206 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001207 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001208 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001209 /**
1210 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1211 * {@link RemoteInput}s.
1212 *
1213 * This is intended for {@link RemoteInput}s that only accept data, meaning
1214 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1215 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1216 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1217 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1218 *
1219 * You can test if a RemoteInput matches these constraints using
1220 * {@link RemoteInput#isDataOnly}.
1221 */
1222 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1223
Griff Hazen959591e2014-05-15 22:26:18 -07001224 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001225 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001226 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001227 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001228
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001229 /**
1230 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001231 *
1232 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001233 */
Dan Sandler86647982015-05-13 23:41:13 -04001234 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001235 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001236
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001237 /**
1238 * Title of the action.
1239 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001240 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001241
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001242 /**
1243 * Intent to send when the user invokes this action. May be null, in which case the action
1244 * may be rendered in a disabled presentation by the system UI.
1245 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001246 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001247
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001248 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001249 if (in.readInt() != 0) {
1250 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001251 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1252 icon = mIcon.getResId();
1253 }
Dan Sandler86647982015-05-13 23:41:13 -04001254 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001255 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1256 if (in.readInt() == 1) {
1257 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1258 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001259 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001260 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001261 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001262 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001263
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001264 /**
Dan Sandler86647982015-05-13 23:41:13 -04001265 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001266 */
Dan Sandler86647982015-05-13 23:41:13 -04001267 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001268 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001269 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001270 }
1271
Adrian Roos7af53622016-10-12 13:44:05 -07001272 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001273 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001274 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001275 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001276 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1277 this.icon = icon.getResId();
1278 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001279 this.title = title;
1280 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001281 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001282 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001283 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001284 }
1285
1286 /**
Dan Sandler86647982015-05-13 23:41:13 -04001287 * Return an icon representing the action.
1288 */
1289 public Icon getIcon() {
1290 if (mIcon == null && icon != 0) {
1291 // you snuck an icon in here without using the builder; let's try to keep it
1292 mIcon = Icon.createWithResource("", icon);
1293 }
1294 return mIcon;
1295 }
1296
1297 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001298 * Get additional metadata carried around with this Action.
1299 */
1300 public Bundle getExtras() {
1301 return mExtras;
1302 }
1303
1304 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001305 * Return whether the platform should automatically generate possible replies for this
1306 * {@link Action}
1307 */
1308 public boolean getAllowGeneratedReplies() {
1309 return mAllowGeneratedReplies;
1310 }
1311
1312 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001313 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001314 * May return null if no remote inputs were added. Only returns inputs which accept
1315 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001316 */
1317 public RemoteInput[] getRemoteInputs() {
1318 return mRemoteInputs;
1319 }
1320
1321 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001322 * Get the list of inputs to be collected from the user that ONLY accept data when this
1323 * action is sent. These remote inputs are guaranteed to return true on a call to
1324 * {@link RemoteInput#isDataOnly}.
1325 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001326 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001327 *
1328 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1329 * of non-textual RemoteInputs do not access these remote inputs.
1330 */
1331 public RemoteInput[] getDataOnlyRemoteInputs() {
1332 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1333 }
1334
1335 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001336 * Builder class for {@link Action} objects.
1337 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001338 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001339 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001340 private final CharSequence mTitle;
1341 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001342 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001343 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001344 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001345
1346 /**
1347 * Construct a new builder for {@link Action} object.
1348 * @param icon icon to show for this action
1349 * @param title the title of the action
1350 * @param intent the {@link PendingIntent} to fire when users trigger this action
1351 */
Dan Sandler86647982015-05-13 23:41:13 -04001352 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001353 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001354 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001355 }
1356
1357 /**
1358 * Construct a new builder for {@link Action} object.
1359 * @param icon icon to show for this action
1360 * @param title the title of the action
1361 * @param intent the {@link PendingIntent} to fire when users trigger this action
1362 */
1363 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001364 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001365 }
1366
1367 /**
1368 * Construct a new builder for {@link Action} object using the fields from an
1369 * {@link Action}.
1370 * @param action the action to read fields from.
1371 */
1372 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001373 this(action.getIcon(), action.title, action.actionIntent,
1374 new Bundle(action.mExtras), action.getRemoteInputs(),
1375 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001376 }
1377
Dan Sandler86647982015-05-13 23:41:13 -04001378 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001379 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001380 mIcon = icon;
1381 mTitle = title;
1382 mIntent = intent;
1383 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001384 if (remoteInputs != null) {
1385 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1386 Collections.addAll(mRemoteInputs, remoteInputs);
1387 }
Adrian Roos7af53622016-10-12 13:44:05 -07001388 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001389 }
1390
1391 /**
1392 * Merge additional metadata into this builder.
1393 *
1394 * <p>Values within the Bundle will replace existing extras values in this Builder.
1395 *
1396 * @see Notification.Action#extras
1397 */
1398 public Builder addExtras(Bundle extras) {
1399 if (extras != null) {
1400 mExtras.putAll(extras);
1401 }
1402 return this;
1403 }
1404
1405 /**
1406 * Get the metadata Bundle used by this Builder.
1407 *
1408 * <p>The returned Bundle is shared with this Builder.
1409 */
1410 public Bundle getExtras() {
1411 return mExtras;
1412 }
1413
1414 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001415 * Add an input to be collected from the user when this action is sent.
1416 * Response values can be retrieved from the fired intent by using the
1417 * {@link RemoteInput#getResultsFromIntent} function.
1418 * @param remoteInput a {@link RemoteInput} to add to the action
1419 * @return this object for method chaining
1420 */
1421 public Builder addRemoteInput(RemoteInput remoteInput) {
1422 if (mRemoteInputs == null) {
1423 mRemoteInputs = new ArrayList<RemoteInput>();
1424 }
1425 mRemoteInputs.add(remoteInput);
1426 return this;
1427 }
1428
1429 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001430 * Set whether the platform should automatically generate possible replies to add to
1431 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1432 * {@link RemoteInput}, this has no effect.
1433 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1434 * otherwise
1435 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001436 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001437 */
1438 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1439 mAllowGeneratedReplies = allowGeneratedReplies;
1440 return this;
1441 }
1442
1443 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001444 * Apply an extender to this action builder. Extenders may be used to add
1445 * metadata or change options on this builder.
1446 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001447 public Builder extend(Extender extender) {
1448 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001449 return this;
1450 }
1451
1452 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001453 * Combine all of the options that have been set and return a new {@link Action}
1454 * object.
1455 * @return the built action
1456 */
1457 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001458 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1459 RemoteInput[] previousDataInputs =
1460 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001461 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001462 for (RemoteInput input : previousDataInputs) {
1463 dataOnlyInputs.add(input);
1464 }
1465 }
1466 List<RemoteInput> textInputs = new ArrayList<>();
1467 if (mRemoteInputs != null) {
1468 for (RemoteInput input : mRemoteInputs) {
1469 if (input.isDataOnly()) {
1470 dataOnlyInputs.add(input);
1471 } else {
1472 textInputs.add(input);
1473 }
1474 }
1475 }
1476 if (!dataOnlyInputs.isEmpty()) {
1477 RemoteInput[] dataInputsArr =
1478 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1479 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1480 }
1481 RemoteInput[] textInputsArr = textInputs.isEmpty()
1482 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1483 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001484 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001485 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001486 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001487
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001488 @Override
1489 public Action clone() {
1490 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001491 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001492 title,
1493 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001494 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001495 getRemoteInputs(),
1496 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001497 }
1498 @Override
1499 public int describeContents() {
1500 return 0;
1501 }
1502 @Override
1503 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001504 final Icon ic = getIcon();
1505 if (ic != null) {
1506 out.writeInt(1);
1507 ic.writeToParcel(out, 0);
1508 } else {
1509 out.writeInt(0);
1510 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001511 TextUtils.writeToParcel(title, out, flags);
1512 if (actionIntent != null) {
1513 out.writeInt(1);
1514 actionIntent.writeToParcel(out, flags);
1515 } else {
1516 out.writeInt(0);
1517 }
Griff Hazen959591e2014-05-15 22:26:18 -07001518 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001519 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001520 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001521 }
Griff Hazen959591e2014-05-15 22:26:18 -07001522 public static final Parcelable.Creator<Action> CREATOR =
1523 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001524 public Action createFromParcel(Parcel in) {
1525 return new Action(in);
1526 }
1527 public Action[] newArray(int size) {
1528 return new Action[size];
1529 }
1530 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001531
1532 /**
1533 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1534 * metadata or change options on an action builder.
1535 */
1536 public interface Extender {
1537 /**
1538 * Apply this extender to a notification action builder.
1539 * @param builder the builder to be modified.
1540 * @return the build object for chaining.
1541 */
1542 public Builder extend(Builder builder);
1543 }
1544
1545 /**
1546 * Wearable extender for notification actions. To add extensions to an action,
1547 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1548 * the {@code WearableExtender()} constructor and apply it to a
1549 * {@link android.app.Notification.Action.Builder} using
1550 * {@link android.app.Notification.Action.Builder#extend}.
1551 *
1552 * <pre class="prettyprint">
1553 * Notification.Action action = new Notification.Action.Builder(
1554 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001555 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001556 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001557 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001558 */
1559 public static final class WearableExtender implements Extender {
1560 /** Notification action extra which contains wearable extensions */
1561 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1562
Pete Gastaf6781d2014-10-07 15:17:05 -04001563 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001564 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001565 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1566 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1567 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001568
1569 // Flags bitwise-ored to mFlags
1570 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001571 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001572 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001573
1574 // Default value for flags integer
1575 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1576
1577 private int mFlags = DEFAULT_FLAGS;
1578
Pete Gastaf6781d2014-10-07 15:17:05 -04001579 private CharSequence mInProgressLabel;
1580 private CharSequence mConfirmLabel;
1581 private CharSequence mCancelLabel;
1582
Griff Hazen61a9e862014-05-22 16:05:19 -07001583 /**
1584 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1585 * options.
1586 */
1587 public WearableExtender() {
1588 }
1589
1590 /**
1591 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1592 * wearable options present in an existing notification action.
1593 * @param action the notification action to inspect.
1594 */
1595 public WearableExtender(Action action) {
1596 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1597 if (wearableBundle != null) {
1598 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001599 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1600 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1601 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001602 }
1603 }
1604
1605 /**
1606 * Apply wearable extensions to a notification action that is being built. This is
1607 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1608 * method of {@link android.app.Notification.Action.Builder}.
1609 */
1610 @Override
1611 public Action.Builder extend(Action.Builder builder) {
1612 Bundle wearableBundle = new Bundle();
1613
1614 if (mFlags != DEFAULT_FLAGS) {
1615 wearableBundle.putInt(KEY_FLAGS, mFlags);
1616 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001617 if (mInProgressLabel != null) {
1618 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1619 }
1620 if (mConfirmLabel != null) {
1621 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1622 }
1623 if (mCancelLabel != null) {
1624 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1625 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001626
1627 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1628 return builder;
1629 }
1630
1631 @Override
1632 public WearableExtender clone() {
1633 WearableExtender that = new WearableExtender();
1634 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001635 that.mInProgressLabel = this.mInProgressLabel;
1636 that.mConfirmLabel = this.mConfirmLabel;
1637 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001638 return that;
1639 }
1640
1641 /**
1642 * Set whether this action is available when the wearable device is not connected to
1643 * a companion device. The user can still trigger this action when the wearable device is
1644 * offline, but a visual hint will indicate that the action may not be available.
1645 * Defaults to true.
1646 */
1647 public WearableExtender setAvailableOffline(boolean availableOffline) {
1648 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1649 return this;
1650 }
1651
1652 /**
1653 * Get whether this action is available when the wearable device is not connected to
1654 * a companion device. The user can still trigger this action when the wearable device is
1655 * offline, but a visual hint will indicate that the action may not be available.
1656 * Defaults to true.
1657 */
1658 public boolean isAvailableOffline() {
1659 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1660 }
1661
1662 private void setFlag(int mask, boolean value) {
1663 if (value) {
1664 mFlags |= mask;
1665 } else {
1666 mFlags &= ~mask;
1667 }
1668 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001669
1670 /**
1671 * Set a label to display while the wearable is preparing to automatically execute the
1672 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1673 *
1674 * @param label the label to display while the action is being prepared to execute
1675 * @return this object for method chaining
1676 */
1677 public WearableExtender setInProgressLabel(CharSequence label) {
1678 mInProgressLabel = label;
1679 return this;
1680 }
1681
1682 /**
1683 * Get the label to display while the wearable is preparing to automatically execute
1684 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1685 *
1686 * @return the label to display while the action is being prepared to execute
1687 */
1688 public CharSequence getInProgressLabel() {
1689 return mInProgressLabel;
1690 }
1691
1692 /**
1693 * Set a label to display to confirm that the action should be executed.
1694 * This is usually an imperative verb like "Send".
1695 *
1696 * @param label the label to confirm the action should be executed
1697 * @return this object for method chaining
1698 */
1699 public WearableExtender setConfirmLabel(CharSequence label) {
1700 mConfirmLabel = label;
1701 return this;
1702 }
1703
1704 /**
1705 * Get the label to display to confirm that the action should be executed.
1706 * This is usually an imperative verb like "Send".
1707 *
1708 * @return the label to confirm the action should be executed
1709 */
1710 public CharSequence getConfirmLabel() {
1711 return mConfirmLabel;
1712 }
1713
1714 /**
1715 * Set a label to display to cancel the action.
1716 * This is usually an imperative verb, like "Cancel".
1717 *
1718 * @param label the label to display to cancel the action
1719 * @return this object for method chaining
1720 */
1721 public WearableExtender setCancelLabel(CharSequence label) {
1722 mCancelLabel = label;
1723 return this;
1724 }
1725
1726 /**
1727 * Get the label to display to cancel the action.
1728 * This is usually an imperative verb like "Cancel".
1729 *
1730 * @return the label to display to cancel the action
1731 */
1732 public CharSequence getCancelLabel() {
1733 return mCancelLabel;
1734 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001735
1736 /**
1737 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1738 * platform that it can generate the appropriate transitions.
1739 * @param hintLaunchesActivity {@code true} if the content intent will launch
1740 * an activity and transitions should be generated, false otherwise.
1741 * @return this object for method chaining
1742 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001743 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001744 boolean hintLaunchesActivity) {
1745 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1746 return this;
1747 }
1748
1749 /**
1750 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1751 * platform that it can generate the appropriate transitions
1752 * @return {@code true} if the content intent will launch an activity and transitions
1753 * should be generated, false otherwise. The default value is {@code false} if this was
1754 * never set.
1755 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001756 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001757 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1758 }
Alex Hills9f087612016-06-07 09:08:59 -04001759
1760 /**
1761 * Set a hint that this Action should be displayed inline.
1762 *
1763 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1764 * otherwise
1765 * @return this object for method chaining
1766 */
1767 public WearableExtender setHintDisplayActionInline(
1768 boolean hintDisplayInline) {
1769 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1770 return this;
1771 }
1772
1773 /**
1774 * Get a hint that this Action should be displayed inline.
1775 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001776 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001777 * otherwise. The default value is {@code false} if this was never set.
1778 */
1779 public boolean getHintDisplayActionInline() {
1780 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1781 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001782 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001783 }
1784
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001785 /**
1786 * Array of all {@link Action} structures attached to this notification by
1787 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1788 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1789 * interface for invoking actions.
1790 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001791 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001792
1793 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001794 * Replacement version of this notification whose content will be shown
1795 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1796 * and {@link #VISIBILITY_PUBLIC}.
1797 */
1798 public Notification publicVersion;
1799
1800 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001801 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001802 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 */
1804 public Notification()
1805 {
1806 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001807 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001808 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810
1811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 * @hide
1813 */
1814 public Notification(Context context, int icon, CharSequence tickerText, long when,
1815 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1816 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001817 new Builder(context)
1818 .setWhen(when)
1819 .setSmallIcon(icon)
1820 .setTicker(tickerText)
1821 .setContentTitle(contentTitle)
1822 .setContentText(contentText)
1823 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1824 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
1826
1827 /**
1828 * Constructs a Notification object with the information needed to
1829 * have a status bar icon without the standard expanded view.
1830 *
1831 * @param icon The resource id of the icon to put in the status bar.
1832 * @param tickerText The text that flows by in the status bar when the notification first
1833 * activates.
1834 * @param when The time to show in the time field. In the System.currentTimeMillis
1835 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001836 *
1837 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001839 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 public Notification(int icon, CharSequence tickerText, long when)
1841 {
1842 this.icon = icon;
1843 this.tickerText = tickerText;
1844 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001845 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
1847
1848 /**
1849 * Unflatten the notification from a parcel.
1850 */
Svet Ganovddb94882016-06-23 19:55:24 -07001851 @SuppressWarnings("unchecked")
1852 public Notification(Parcel parcel) {
1853 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1854 // intents in extras are always written as the last entry.
1855 readFromParcelImpl(parcel);
1856 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001857 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001858 }
1859
1860 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 {
1862 int version = parcel.readInt();
1863
Dianne Hackborn98305522017-05-05 17:53:53 -07001864 whitelistToken = parcel.readStrongBinder();
1865 if (whitelistToken == null) {
1866 whitelistToken = processWhitelistToken;
1867 }
1868 // Propagate this token to all pending intents that are unmarshalled from the parcel.
1869 parcel.setClassCookie(PendingIntent.class, whitelistToken);
1870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001872 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001873 if (parcel.readInt() != 0) {
1874 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001875 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1876 icon = mSmallIcon.getResId();
1877 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 number = parcel.readInt();
1880 if (parcel.readInt() != 0) {
1881 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1882 }
1883 if (parcel.readInt() != 0) {
1884 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1885 }
1886 if (parcel.readInt() != 0) {
1887 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1888 }
1889 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001890 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001891 }
1892 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1894 }
Joe Onorato561d3852010-11-20 18:09:34 -08001895 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001896 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 defaults = parcel.readInt();
1899 flags = parcel.readInt();
1900 if (parcel.readInt() != 0) {
1901 sound = Uri.CREATOR.createFromParcel(parcel);
1902 }
1903
1904 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001905 if (parcel.readInt() != 0) {
1906 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 vibrate = parcel.createLongArray();
1909 ledARGB = parcel.readInt();
1910 ledOnMS = parcel.readInt();
1911 ledOffMS = parcel.readInt();
1912 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001913
1914 if (parcel.readInt() != 0) {
1915 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1916 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001917
1918 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001919
John Spurlockfd7f1e02014-03-18 16:41:57 -04001920 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001921
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001922 mGroupKey = parcel.readString();
1923
1924 mSortKey = parcel.readString();
1925
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001926 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001927
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001928 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1929
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001930 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001931 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1932 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001933
Chris Wren8fd39ec2014-02-27 17:43:26 -05001934 if (parcel.readInt() != 0) {
1935 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1936 }
1937
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001938 visibility = parcel.readInt();
1939
1940 if (parcel.readInt() != 0) {
1941 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1942 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001943
1944 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001945
1946 if (parcel.readInt() != 0) {
1947 mChannelId = parcel.readString();
1948 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001949 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001950
1951 if (parcel.readInt() != 0) {
1952 mShortcutId = parcel.readString();
1953 }
1954
1955 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001956
1957 if (parcel.readInt() != 0) {
1958 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1959 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001960
1961 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
1963
Andy Stadler110988c2010-12-03 14:29:16 -08001964 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001965 public Notification clone() {
1966 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001967 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001968 return that;
1969 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001970
Daniel Sandler1a497d32013-04-18 14:52:45 -04001971 /**
1972 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1973 * of this into that.
1974 * @hide
1975 */
1976 public void cloneInto(Notification that, boolean heavy) {
Dianne Hackborn98305522017-05-05 17:53:53 -07001977 that.whitelistToken = this.whitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07001978 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001979 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001980 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001981 that.number = this.number;
1982
1983 // PendingIntents are global, so there's no reason (or way) to clone them.
1984 that.contentIntent = this.contentIntent;
1985 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001986 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001987
1988 if (this.tickerText != null) {
1989 that.tickerText = this.tickerText.toString();
1990 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001991 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001992 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001993 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001994 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001995 that.contentView = this.contentView.clone();
1996 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001997 if (heavy && this.mLargeIcon != null) {
1998 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001999 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002000 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002001 that.sound = this.sound; // android.net.Uri is immutable
2002 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002003 if (this.audioAttributes != null) {
2004 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2005 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002006
2007 final long[] vibrate = this.vibrate;
2008 if (vibrate != null) {
2009 final int N = vibrate.length;
2010 final long[] vib = that.vibrate = new long[N];
2011 System.arraycopy(vibrate, 0, vib, 0, N);
2012 }
2013
2014 that.ledARGB = this.ledARGB;
2015 that.ledOnMS = this.ledOnMS;
2016 that.ledOffMS = this.ledOffMS;
2017 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002018
Joe Onorato18e69df2010-05-17 22:26:12 -07002019 that.flags = this.flags;
2020
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002021 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002022
John Spurlockfd7f1e02014-03-18 16:41:57 -04002023 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002024
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002025 that.mGroupKey = this.mGroupKey;
2026
2027 that.mSortKey = this.mSortKey;
2028
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002029 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002030 try {
2031 that.extras = new Bundle(this.extras);
2032 // will unparcel
2033 that.extras.size();
2034 } catch (BadParcelableException e) {
2035 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2036 that.extras = null;
2037 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002038 }
2039
Felipe Lemedd85da62016-06-28 11:29:54 -07002040 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2041 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002042 }
2043
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002044 if (this.actions != null) {
2045 that.actions = new Action[this.actions.length];
2046 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002047 if ( this.actions[i] != null) {
2048 that.actions[i] = this.actions[i].clone();
2049 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002050 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002051 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002052
Daniel Sandler1a497d32013-04-18 14:52:45 -04002053 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002054 that.bigContentView = this.bigContentView.clone();
2055 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002056
Chris Wren8fd39ec2014-02-27 17:43:26 -05002057 if (heavy && this.headsUpContentView != null) {
2058 that.headsUpContentView = this.headsUpContentView.clone();
2059 }
2060
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002061 that.visibility = this.visibility;
2062
2063 if (this.publicVersion != null) {
2064 that.publicVersion = new Notification();
2065 this.publicVersion.cloneInto(that.publicVersion, heavy);
2066 }
2067
Dan Sandler26e81cf2014-05-06 10:01:27 -04002068 that.color = this.color;
2069
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002070 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002071 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002072 that.mShortcutId = this.mShortcutId;
2073 that.mBadgeIcon = this.mBadgeIcon;
2074 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002075 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002076
Daniel Sandler1a497d32013-04-18 14:52:45 -04002077 if (!heavy) {
2078 that.lightenPayload(); // will clean out extras
2079 }
2080 }
2081
2082 /**
2083 * Removes heavyweight parts of the Notification object for archival or for sending to
2084 * listeners when the full contents are not necessary.
2085 * @hide
2086 */
2087 public final void lightenPayload() {
2088 tickerView = null;
2089 contentView = null;
2090 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002091 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002092 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002093 if (extras != null && !extras.isEmpty()) {
2094 final Set<String> keyset = extras.keySet();
2095 final int N = keyset.size();
2096 final String[] keys = keyset.toArray(new String[N]);
2097 for (int i=0; i<N; i++) {
2098 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002099 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2100 continue;
2101 }
Dan Sandler50128532015-12-08 15:42:41 -05002102 final Object obj = extras.get(key);
2103 if (obj != null &&
2104 ( obj instanceof Parcelable
2105 || obj instanceof Parcelable[]
2106 || obj instanceof SparseArray
2107 || obj instanceof ArrayList)) {
2108 extras.remove(key);
2109 }
2110 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002111 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002112 }
2113
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002114 /**
2115 * Make sure this CharSequence is safe to put into a bundle, which basically
2116 * means it had better not be some custom Parcelable implementation.
2117 * @hide
2118 */
2119 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002120 if (cs == null) return cs;
2121 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2122 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2123 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002124 if (cs instanceof Parcelable) {
2125 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2126 + " instance is a custom Parcelable and not allowed in Notification");
2127 return cs.toString();
2128 }
Selim Cinek60a54252016-02-26 17:03:25 -08002129 return removeTextSizeSpans(cs);
2130 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002131
Selim Cinek60a54252016-02-26 17:03:25 -08002132 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2133 if (charSequence instanceof Spanned) {
2134 Spanned ss = (Spanned) charSequence;
2135 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2136 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2137 for (Object span : spans) {
2138 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002139 if (resultSpan instanceof CharacterStyle) {
2140 resultSpan = ((CharacterStyle) span).getUnderlying();
2141 }
2142 if (resultSpan instanceof TextAppearanceSpan) {
2143 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002144 resultSpan = new TextAppearanceSpan(
2145 originalSpan.getFamily(),
2146 originalSpan.getTextStyle(),
2147 -1,
2148 originalSpan.getTextColor(),
2149 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002150 } else if (resultSpan instanceof RelativeSizeSpan
2151 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002152 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002153 } else {
2154 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002155 }
2156 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2157 ss.getSpanFlags(span));
2158 }
2159 return builder;
2160 }
2161 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002162 }
2163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 public int describeContents() {
2165 return 0;
2166 }
2167
2168 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002169 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 */
Svet Ganovddb94882016-06-23 19:55:24 -07002171 public void writeToParcel(Parcel parcel, int flags) {
2172 // We need to mark all pending intents getting into the notification
2173 // system as being put there to later allow the notification ranker
2174 // to launch them and by doing so add the app to the battery saver white
2175 // list for a short period of time. The problem is that the system
2176 // cannot look into the extras as there may be parcelables there that
2177 // the platform does not know how to handle. To go around that we have
2178 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002179 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002180 if (collectPendingIntents) {
2181 PendingIntent.setOnMarshaledListener(
2182 (PendingIntent intent, Parcel out, int outFlags) -> {
2183 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002184 if (allPendingIntents == null) {
2185 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002186 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002187 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002188 }
2189 });
2190 }
2191 try {
2192 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002193 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002194 writeToParcelImpl(parcel, flags);
2195 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002196 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002197 } finally {
2198 if (collectPendingIntents) {
2199 PendingIntent.setOnMarshaledListener(null);
2200 }
2201 }
2202 }
2203
2204 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 parcel.writeInt(1);
2206
Dianne Hackborn98305522017-05-05 17:53:53 -07002207 parcel.writeStrongBinder(whitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002209 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002210 if (mSmallIcon == null && icon != 0) {
2211 // you snuck an icon in here without using the builder; let's try to keep it
2212 mSmallIcon = Icon.createWithResource("", icon);
2213 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002214 if (mSmallIcon != null) {
2215 parcel.writeInt(1);
2216 mSmallIcon.writeToParcel(parcel, 0);
2217 } else {
2218 parcel.writeInt(0);
2219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 parcel.writeInt(number);
2221 if (contentIntent != null) {
2222 parcel.writeInt(1);
2223 contentIntent.writeToParcel(parcel, 0);
2224 } else {
2225 parcel.writeInt(0);
2226 }
2227 if (deleteIntent != null) {
2228 parcel.writeInt(1);
2229 deleteIntent.writeToParcel(parcel, 0);
2230 } else {
2231 parcel.writeInt(0);
2232 }
2233 if (tickerText != null) {
2234 parcel.writeInt(1);
2235 TextUtils.writeToParcel(tickerText, parcel, flags);
2236 } else {
2237 parcel.writeInt(0);
2238 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002239 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002240 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002241 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002242 } else {
2243 parcel.writeInt(0);
2244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 if (contentView != null) {
2246 parcel.writeInt(1);
2247 contentView.writeToParcel(parcel, 0);
2248 } else {
2249 parcel.writeInt(0);
2250 }
Selim Cinek279fa862016-06-14 10:57:25 -07002251 if (mLargeIcon == null && largeIcon != null) {
2252 // you snuck an icon in here without using the builder; let's try to keep it
2253 mLargeIcon = Icon.createWithBitmap(largeIcon);
2254 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002255 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002256 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002257 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002258 } else {
2259 parcel.writeInt(0);
2260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261
2262 parcel.writeInt(defaults);
2263 parcel.writeInt(this.flags);
2264
2265 if (sound != null) {
2266 parcel.writeInt(1);
2267 sound.writeToParcel(parcel, 0);
2268 } else {
2269 parcel.writeInt(0);
2270 }
2271 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002272
2273 if (audioAttributes != null) {
2274 parcel.writeInt(1);
2275 audioAttributes.writeToParcel(parcel, 0);
2276 } else {
2277 parcel.writeInt(0);
2278 }
2279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 parcel.writeLongArray(vibrate);
2281 parcel.writeInt(ledARGB);
2282 parcel.writeInt(ledOnMS);
2283 parcel.writeInt(ledOffMS);
2284 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002285
2286 if (fullScreenIntent != null) {
2287 parcel.writeInt(1);
2288 fullScreenIntent.writeToParcel(parcel, 0);
2289 } else {
2290 parcel.writeInt(0);
2291 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002292
2293 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002294
John Spurlockfd7f1e02014-03-18 16:41:57 -04002295 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002296
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002297 parcel.writeString(mGroupKey);
2298
2299 parcel.writeString(mSortKey);
2300
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002301 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002302
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002303 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002304
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002305 if (bigContentView != null) {
2306 parcel.writeInt(1);
2307 bigContentView.writeToParcel(parcel, 0);
2308 } else {
2309 parcel.writeInt(0);
2310 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002311
Chris Wren8fd39ec2014-02-27 17:43:26 -05002312 if (headsUpContentView != null) {
2313 parcel.writeInt(1);
2314 headsUpContentView.writeToParcel(parcel, 0);
2315 } else {
2316 parcel.writeInt(0);
2317 }
2318
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002319 parcel.writeInt(visibility);
2320
2321 if (publicVersion != null) {
2322 parcel.writeInt(1);
2323 publicVersion.writeToParcel(parcel, 0);
2324 } else {
2325 parcel.writeInt(0);
2326 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002327
2328 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002329
2330 if (mChannelId != null) {
2331 parcel.writeInt(1);
2332 parcel.writeString(mChannelId);
2333 } else {
2334 parcel.writeInt(0);
2335 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002336 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002337
2338 if (mShortcutId != null) {
2339 parcel.writeInt(1);
2340 parcel.writeString(mShortcutId);
2341 } else {
2342 parcel.writeInt(0);
2343 }
2344
2345 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002346
2347 if (mSettingsText != null) {
2348 parcel.writeInt(1);
2349 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2350 } else {
2351 parcel.writeInt(0);
2352 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002353
2354 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
2356
2357 /**
2358 * Parcelable.Creator that instantiates Notification objects
2359 */
2360 public static final Parcelable.Creator<Notification> CREATOR
2361 = new Parcelable.Creator<Notification>()
2362 {
2363 public Notification createFromParcel(Parcel parcel)
2364 {
2365 return new Notification(parcel);
2366 }
2367
2368 public Notification[] newArray(int size)
2369 {
2370 return new Notification[size];
2371 }
2372 };
2373
2374 /**
2375 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2376 * layout.
2377 *
2378 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2379 * in the view.</p>
2380 * @param context The context for your application / activity.
2381 * @param contentTitle The title that goes in the expanded entry.
2382 * @param contentText The text that goes in the expanded entry.
2383 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2384 * If this is an activity, it must include the
2385 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002386 * that you take care of task management as described in the
2387 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2388 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002390 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002391 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002393 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 public void setLatestEventInfo(Context context,
2395 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002396 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2397 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2398 new Throwable());
2399 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002400
Selim Cinek4ac6f602016-06-13 15:47:03 -07002401 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2402 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2403 }
2404
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002405 // ensure that any information already set directly is preserved
2406 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002407
2408 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002410 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
2412 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002413 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002415 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002416
2417 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
2419
Julia Reynoldsda303542015-11-23 14:00:20 -05002420 /**
2421 * @hide
2422 */
2423 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002424 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002425 }
2426
2427 /**
2428 * @hide
2429 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002430 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002431 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002432 }
2433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 @Override
2435 public String toString() {
2436 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002437 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002438 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002439 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002440 sb.append(priority);
2441 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002442 if (contentView != null) {
2443 sb.append(contentView.getPackage());
2444 sb.append("/0x");
2445 sb.append(Integer.toHexString(contentView.getLayoutId()));
2446 } else {
2447 sb.append("null");
2448 }
2449 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002450 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2451 sb.append("default");
2452 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 int N = this.vibrate.length-1;
2454 sb.append("[");
2455 for (int i=0; i<N; i++) {
2456 sb.append(this.vibrate[i]);
2457 sb.append(',');
2458 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002459 if (N != -1) {
2460 sb.append(this.vibrate[N]);
2461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 } else {
2464 sb.append("null");
2465 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002467 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002469 } else if (this.sound != null) {
2470 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 } else {
2472 sb.append("null");
2473 }
Chris Wren365b6d32015-07-16 10:39:26 -04002474 if (this.tickerText != null) {
2475 sb.append(" tick");
2476 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002477 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002479 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002480 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002481 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002482 if (this.category != null) {
2483 sb.append(" category=");
2484 sb.append(this.category);
2485 }
2486 if (this.mGroupKey != null) {
2487 sb.append(" groupKey=");
2488 sb.append(this.mGroupKey);
2489 }
2490 if (this.mSortKey != null) {
2491 sb.append(" sortKey=");
2492 sb.append(this.mSortKey);
2493 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002494 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002495 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002496 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002497 }
2498 sb.append(" vis=");
2499 sb.append(visibilityToString(this.visibility));
2500 if (this.publicVersion != null) {
2501 sb.append(" publicVersion=");
2502 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002503 }
2504 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 return sb.toString();
2506 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002507
Dan Sandler1b718782014-07-18 12:43:45 -04002508 /**
2509 * {@hide}
2510 */
2511 public static String visibilityToString(int vis) {
2512 switch (vis) {
2513 case VISIBILITY_PRIVATE:
2514 return "PRIVATE";
2515 case VISIBILITY_PUBLIC:
2516 return "PUBLIC";
2517 case VISIBILITY_SECRET:
2518 return "SECRET";
2519 default:
2520 return "UNKNOWN(" + String.valueOf(vis) + ")";
2521 }
2522 }
2523
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002525 * {@hide}
2526 */
2527 public static String priorityToString(@Priority int pri) {
2528 switch (pri) {
2529 case PRIORITY_MIN:
2530 return "MIN";
2531 case PRIORITY_LOW:
2532 return "LOW";
2533 case PRIORITY_DEFAULT:
2534 return "DEFAULT";
2535 case PRIORITY_HIGH:
2536 return "HIGH";
2537 case PRIORITY_MAX:
2538 return "MAX";
2539 default:
2540 return "UNKNOWN(" + String.valueOf(pri) + ")";
2541 }
2542 }
2543
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002544 /**
2545 * @hide
2546 */
2547 public boolean hasCompletedProgress() {
2548 // not a progress notification; can't be complete
2549 if (!extras.containsKey(EXTRA_PROGRESS)
2550 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2551 return false;
2552 }
2553 // many apps use max 0 for 'indeterminate'; not complete
2554 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2555 return false;
2556 }
2557 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2558 }
2559
Jeff Sharkey000ce802017-04-29 13:13:27 -06002560 /** @removed */
2561 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002562 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002563 return mChannelId;
2564 }
2565
2566 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002567 * Returns the id of the channel this notification posts to.
2568 */
2569 public String getChannelId() {
2570 return mChannelId;
2571 }
2572
Jeff Sharkey000ce802017-04-29 13:13:27 -06002573 /** @removed */
2574 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002575 public long getTimeout() {
2576 return mTimeout;
2577 }
2578
2579 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002580 * Returns the duration from posting after which this notification should be canceled by the
2581 * system, if it's not canceled already.
2582 */
2583 public long getTimeoutAfter() {
2584 return mTimeout;
2585 }
2586
2587 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002588 * Returns what icon should be shown for this notification if it is being displayed in a
2589 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2590 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2591 */
2592 public int getBadgeIconType() {
2593 return mBadgeIcon;
2594 }
2595
2596 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002597 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002598 *
2599 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2600 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002601 */
2602 public String getShortcutId() {
2603 return mShortcutId;
2604 }
2605
Julia Reynolds3aedded2017-03-31 14:42:09 -04002606
2607 /**
2608 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2609 */
2610 public CharSequence getSettingsText() {
2611 return mSettingsText;
2612 }
2613
Julia Reynolds13d898c2017-02-02 12:22:05 -05002614 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002615 * Returns which type of notifications in a group are responsible for audibly alerting the
2616 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2617 * {@link #GROUP_ALERT_SUMMARY}.
2618 */
2619 public @GroupAlertBehavior int getGroupAlertBehavior() {
2620 return mGroupAlertBehavior;
2621 }
2622
2623 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002624 * The small icon representing this notification in the status bar and content view.
2625 *
2626 * @return the small icon representing this notification.
2627 *
2628 * @see Builder#getSmallIcon()
2629 * @see Builder#setSmallIcon(Icon)
2630 */
2631 public Icon getSmallIcon() {
2632 return mSmallIcon;
2633 }
2634
2635 /**
2636 * Used when notifying to clean up legacy small icons.
2637 * @hide
2638 */
2639 public void setSmallIcon(Icon icon) {
2640 mSmallIcon = icon;
2641 }
2642
2643 /**
2644 * The large icon shown in this notification's content view.
2645 * @see Builder#getLargeIcon()
2646 * @see Builder#setLargeIcon(Icon)
2647 */
2648 public Icon getLargeIcon() {
2649 return mLargeIcon;
2650 }
2651
2652 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002653 * @hide
2654 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002655 public boolean isGroupSummary() {
2656 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2657 }
2658
2659 /**
2660 * @hide
2661 */
2662 public boolean isGroupChild() {
2663 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2664 }
2665
2666 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002667 * @hide
2668 */
2669 public boolean suppressAlertingDueToGrouping() {
2670 if (isGroupSummary()
2671 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2672 return true;
2673 } else if (isGroupChild()
2674 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2675 return true;
2676 }
2677 return false;
2678 }
2679
2680 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002681 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002682 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002683 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002684 * content views using the platform's notification layout template. If your app supports
2685 * versions of Android as old as API level 4, you can instead use
2686 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2687 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2688 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002689 *
Scott Main183bf112012-08-13 19:12:13 -07002690 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002691 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002692 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002693 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002694 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2695 * .setContentText(subject)
2696 * .setSmallIcon(R.drawable.new_mail)
2697 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002698 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002699 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002700 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002701 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002702 /**
2703 * @hide
2704 */
2705 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2706 "android.rebuild.contentViewActionCount";
2707 /**
2708 * @hide
2709 */
2710 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2711 = "android.rebuild.bigViewActionCount";
2712 /**
2713 * @hide
2714 */
2715 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2716 = "android.rebuild.hudViewActionCount";
2717
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002718 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002719
Selim Cinek6743c0b2017-01-18 18:24:01 -08002720 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2721 SystemProperties.getBoolean("notifications.only_title", true);
2722
Selim Cinek389edcd2017-05-11 19:16:44 -07002723 /**
2724 * The lightness difference that has to be added to the primary text color to obtain the
2725 * secondary text color when the background is light.
2726 */
2727 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2728
2729 /**
2730 * The lightness difference that has to be added to the primary text color to obtain the
2731 * secondary text color when the background is dark.
2732 * A bit less then the above value, since it looks better on dark backgrounds.
2733 */
2734 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2735
Joe Onorato46439ce2010-11-19 13:56:21 -08002736 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002737 private Notification mN;
2738 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002739 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002740 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2741 private ArrayList<String> mPersonList = new ArrayList<String>();
2742 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002743 private boolean mIsLegacy;
2744 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002745
2746 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002747 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2748 */
2749 private int mCachedContrastColor = COLOR_INVALID;
2750 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002751 /**
2752 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2753 */
2754 private int mCachedAmbientColor = COLOR_INVALID;
2755 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002756
2757 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002758 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2759 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2760 */
2761 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002762 private int mTextColorsAreForBackground = COLOR_INVALID;
2763 private int mPrimaryTextColor = COLOR_INVALID;
2764 private int mSecondaryTextColor = COLOR_INVALID;
2765 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002766 private int mBackgroundColor = COLOR_INVALID;
2767 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002768 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002769 /**
2770 * A temporary location where actions are stored. If != null the view originally has action
2771 * but doesn't have any for this inflation.
2772 */
2773 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002774 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002775
2776 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002777 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002778 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002779 * @param context
2780 * A {@link Context} that will be used by the Builder to construct the
2781 * RemoteViews. The Context will not be held past the lifetime of this Builder
2782 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002783 * @param channelId
2784 * The constructed Notification will be posted on this
2785 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2786 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002787 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002788 public Builder(Context context, String channelId) {
2789 this(context, (Notification) null);
2790 mN.mChannelId = channelId;
2791 }
2792
2793 /**
2794 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2795 * instead. All posted Notifications must specify a NotificationChannel Id.
2796 */
2797 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002798 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002799 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002800 }
2801
Joe Onoratocb109a02011-01-18 17:57:41 -08002802 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002803 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002804 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002805 public Builder(Context context, Notification toAdopt) {
2806 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002807
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002808 if (toAdopt == null) {
2809 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002810 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2811 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2812 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002813 mN.priority = PRIORITY_DEFAULT;
2814 mN.visibility = VISIBILITY_PRIVATE;
2815 } else {
2816 mN = toAdopt;
2817 if (mN.actions != null) {
2818 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002819 }
2820
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002821 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2822 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2823 }
2824
Selim Cinek4ac6f602016-06-13 15:47:03 -07002825 if (mN.getSmallIcon() == null && mN.icon != 0) {
2826 setSmallIcon(mN.icon);
2827 }
2828
2829 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2830 setLargeIcon(mN.largeIcon);
2831 }
2832
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002833 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2834 if (!TextUtils.isEmpty(templateClass)) {
2835 final Class<? extends Style> styleClass
2836 = getNotificationStyleClass(templateClass);
2837 if (styleClass == null) {
2838 Log.d(TAG, "Unknown style class: " + templateClass);
2839 } else {
2840 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002841 final Constructor<? extends Style> ctor =
2842 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002843 ctor.setAccessible(true);
2844 final Style style = ctor.newInstance();
2845 style.restoreFromExtras(mN.extras);
2846
2847 if (style != null) {
2848 setStyle(style);
2849 }
2850 } catch (Throwable t) {
2851 Log.e(TAG, "Could not create Style", t);
2852 }
2853 }
2854 }
2855
2856 }
2857 }
2858
2859 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002860 if (mColorUtil == null) {
2861 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002862 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002863 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002864 }
2865
2866 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002867 * If this notification is duplicative of a Launcher shortcut, sets the
2868 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2869 * the shortcut.
2870 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002871 * This field will be ignored by Launchers that don't support badging, don't show
2872 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002873 *
2874 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2875 * supersedes
2876 */
2877 public Builder setShortcutId(String shortcutId) {
2878 mN.mShortcutId = shortcutId;
2879 return this;
2880 }
2881
2882 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002883 * Sets which icon to display as a badge for this notification.
2884 *
2885 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2886 * {@link #BADGE_ICON_LARGE}.
2887 *
2888 * Note: This value might be ignored, for launchers that don't support badge icons.
2889 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002890 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002891 mN.mBadgeIcon = icon;
2892 return this;
2893 }
2894
2895 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002896 * Sets the group alert behavior for this notification. Use this method to mute this
2897 * notification if alerts for this notification's group should be handled by a different
2898 * notification. This is only applicable for notifications that belong to a
2899 * {@link #setGroup(String) group}.
2900 *
2901 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2902 */
2903 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2904 mN.mGroupAlertBehavior = groupAlertBehavior;
2905 return this;
2906 }
2907
Jeff Sharkey000ce802017-04-29 13:13:27 -06002908 /** @removed */
2909 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002910 public Builder setChannel(String channelId) {
2911 mN.mChannelId = channelId;
2912 return this;
2913 }
2914
2915 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002916 * Specifies the channel the notification should be delivered on.
2917 */
2918 public Builder setChannelId(String channelId) {
2919 mN.mChannelId = channelId;
2920 return this;
2921 }
2922
Jeff Sharkey000ce802017-04-29 13:13:27 -06002923 /** @removed */
2924 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002925 public Builder setTimeout(long durationMs) {
2926 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002927 return this;
2928 }
2929
2930 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002931 * Specifies a duration in milliseconds after which this notification should be canceled,
2932 * if it is not already canceled.
2933 */
2934 public Builder setTimeoutAfter(long durationMs) {
2935 mN.mTimeout = durationMs;
2936 return this;
2937 }
2938
2939 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002940 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002941 *
2942 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2943 * shown anymore by default and must be opted into by using
2944 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002945 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002946 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002947 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002948 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002949 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002950 return this;
2951 }
2952
Joe Onoratocb109a02011-01-18 17:57:41 -08002953 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002954 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002955 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002956 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2957 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002958 */
2959 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002960 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002961 return this;
2962 }
2963
2964 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002965 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002966 *
2967 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002968 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002969 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002970 * Useful when showing an elapsed time (like an ongoing phone call).
2971 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002972 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002973 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002974 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002975 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002976 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002977 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002978 */
2979 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002980 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002981 return this;
2982 }
2983
2984 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002985 * Sets the Chronometer to count down instead of counting up.
2986 *
2987 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2988 * If it isn't set the chronometer will count up.
2989 *
2990 * @see #setUsesChronometer(boolean)
2991 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002992 public Builder setChronometerCountDown(boolean countDown) {
2993 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002994 return this;
2995 }
2996
2997 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002998 * Set the small icon resource, which will be used to represent the notification in the
2999 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003000 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003001
3002 * The platform template for the expanded view will draw this icon in the left, unless a
3003 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3004 * icon will be moved to the right-hand side.
3005 *
3006
3007 * @param icon
3008 * A resource ID in the application's package of the drawable to use.
3009 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003010 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003011 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003012 return setSmallIcon(icon != 0
3013 ? Icon.createWithResource(mContext, icon)
3014 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003015 }
3016
Joe Onoratocb109a02011-01-18 17:57:41 -08003017 /**
3018 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3019 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3020 * LevelListDrawable}.
3021 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003022 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003023 * @param level The level to use for the icon.
3024 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003025 * @see Notification#icon
3026 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003027 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003028 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003029 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003030 return setSmallIcon(icon);
3031 }
3032
3033 /**
3034 * Set the small icon, which will be used to represent the notification in the
3035 * status bar and content view (unless overriden there by a
3036 * {@link #setLargeIcon(Bitmap) large icon}).
3037 *
3038 * @param icon An Icon object to use.
3039 * @see Notification#icon
3040 */
3041 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003042 mN.setSmallIcon(icon);
3043 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3044 mN.icon = icon.getResId();
3045 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003046 return this;
3047 }
3048
Joe Onoratocb109a02011-01-18 17:57:41 -08003049 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003050 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003051 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003052 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003053 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003054 return this;
3055 }
3056
Joe Onoratocb109a02011-01-18 17:57:41 -08003057 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003058 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003059 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003060 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003061 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003062 return this;
3063 }
3064
Joe Onoratocb109a02011-01-18 17:57:41 -08003065 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003066 * This provides some additional information that is displayed in the notification. No
3067 * guarantees are given where exactly it is displayed.
3068 *
3069 * <p>This information should only be provided if it provides an essential
3070 * benefit to the understanding of the notification. The more text you provide the
3071 * less readable it becomes. For example, an email client should only provide the account
3072 * name here if more than one email account has been added.</p>
3073 *
3074 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3075 * notification header area.
3076 *
3077 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3078 * this will be shown in the third line of text in the platform notification template.
3079 * You should not be using {@link #setProgress(int, int, boolean)} at the
3080 * same time on those versions; they occupy the same place.
3081 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003082 */
3083 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003084 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003085 return this;
3086 }
3087
3088 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003089 * Provides text that will appear as a link to your application's settings.
3090 *
3091 * <p>This text does not appear within notification {@link Style templates} but may
3092 * appear when the user uses an affordance to learn more about the notification.
3093 * Additionally, this text will not appear unless you provide a valid link target by
3094 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3095 *
3096 * <p>This text is meant to be concise description about what the user can customize
3097 * when they click on this link. The recommended maximum length is 40 characters.
3098 * @param text
3099 * @return
3100 */
3101 public Builder setSettingsText(CharSequence text) {
3102 mN.mSettingsText = safeCharSequence(text);
3103 return this;
3104 }
3105
3106 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003107 * Set the remote input history.
3108 *
3109 * This should be set to the most recent inputs that have been sent
3110 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3111 * longer relevant (e.g. for chat notifications once the other party has responded).
3112 *
3113 * The most recent input must be stored at the 0 index, the second most recent at the
3114 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3115 * and how much of each individual input is shown.
3116 *
3117 * <p>Note: The reply text will only be shown on notifications that have least one action
3118 * with a {@code RemoteInput}.</p>
3119 */
3120 public Builder setRemoteInputHistory(CharSequence[] text) {
3121 if (text == null) {
3122 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3123 } else {
3124 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3125 CharSequence[] safe = new CharSequence[N];
3126 for (int i = 0; i < N; i++) {
3127 safe[i] = safeCharSequence(text[i]);
3128 }
3129 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3130 }
3131 return this;
3132 }
3133
3134 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003135 * Sets the number of items this notification represents. May be displayed as a badge count
3136 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003137 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003138 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003139 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003140 return this;
3141 }
3142
Joe Onoratocb109a02011-01-18 17:57:41 -08003143 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003144 * A small piece of additional information pertaining to this notification.
3145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003146 * The platform template will draw this on the last line of the notification, at the far
3147 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003148 *
3149 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3150 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3151 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003152 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003153 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003154 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003155 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003156 return this;
3157 }
3158
Joe Onoratocb109a02011-01-18 17:57:41 -08003159 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003160 * Set the progress this notification represents.
3161 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003162 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003163 */
3164 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003165 mN.extras.putInt(EXTRA_PROGRESS, progress);
3166 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3167 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003168 return this;
3169 }
3170
3171 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003172 * Supply a custom RemoteViews to use instead of the platform template.
3173 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003174 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003175 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003176 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003177 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003178 return setCustomContentView(views);
3179 }
3180
3181 /**
3182 * Supply custom RemoteViews to use instead of the platform template.
3183 *
3184 * This will override the layout that would otherwise be constructed by this Builder
3185 * object.
3186 */
3187 public Builder setCustomContentView(RemoteViews contentView) {
3188 mN.contentView = contentView;
3189 return this;
3190 }
3191
3192 /**
3193 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3194 *
3195 * This will override the expanded layout that would otherwise be constructed by this
3196 * Builder object.
3197 */
3198 public Builder setCustomBigContentView(RemoteViews contentView) {
3199 mN.bigContentView = contentView;
3200 return this;
3201 }
3202
3203 /**
3204 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3205 *
3206 * This will override the heads-up layout that would otherwise be constructed by this
3207 * Builder object.
3208 */
3209 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3210 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003211 return this;
3212 }
3213
Joe Onoratocb109a02011-01-18 17:57:41 -08003214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003215 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003217 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3218 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3219 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003220 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003221 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003223 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003224 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003225 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003226 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003227 return this;
3228 }
3229
Joe Onoratocb109a02011-01-18 17:57:41 -08003230 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003231 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3232 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003233 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003234 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003235 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003236 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003237 return this;
3238 }
3239
Joe Onoratocb109a02011-01-18 17:57:41 -08003240 /**
3241 * An intent to launch instead of posting the notification to the status bar.
3242 * Only for use with extremely high-priority notifications demanding the user's
3243 * <strong>immediate</strong> attention, such as an incoming phone call or
3244 * alarm clock that the user has explicitly set to a particular time.
3245 * If this facility is used for something else, please give the user an option
3246 * to turn it off and use a normal notification, as this can be extremely
3247 * disruptive.
3248 *
Chris Wren47c20a12014-06-18 17:27:29 -04003249 * <p>
3250 * The system UI may choose to display a heads-up notification, instead of
3251 * launching this intent, while the user is using the device.
3252 * </p>
3253 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003254 * @param intent The pending intent to launch.
3255 * @param highPriority Passing true will cause this notification to be sent
3256 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003257 *
3258 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003259 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003260 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003261 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003262 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3263 return this;
3264 }
3265
Joe Onoratocb109a02011-01-18 17:57:41 -08003266 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003267 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003268 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003269 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003270 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003271 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003272 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003273 return this;
3274 }
3275
Joe Onoratocb109a02011-01-18 17:57:41 -08003276 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003277 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003278 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003279 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003280 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003281 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003282 setTicker(tickerText);
3283 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003284 return this;
3285 }
3286
Joe Onoratocb109a02011-01-18 17:57:41 -08003287 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003288 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003289 *
3290 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003291 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3292 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003293 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003294 public Builder setLargeIcon(Bitmap b) {
3295 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3296 }
3297
3298 /**
3299 * Add a large icon to the notification content view.
3300 *
3301 * In the platform template, this image will be shown on the left of the notification view
3302 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3303 * badge atop the large icon).
3304 */
3305 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003306 mN.mLargeIcon = icon;
3307 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003308 return this;
3309 }
3310
Joe Onoratocb109a02011-01-18 17:57:41 -08003311 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003312 * Set the sound to play.
3313 *
John Spurlockc0650f022014-07-19 13:22:39 -04003314 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3315 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003316 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003317 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003318 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003319 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003320 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003321 mN.sound = sound;
3322 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003323 return this;
3324 }
3325
Joe Onoratocb109a02011-01-18 17:57:41 -08003326 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003327 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003328 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003329 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3330 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003331 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003332 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003333 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003334 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003335 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 mN.sound = sound;
3337 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003338 return this;
3339 }
3340
Joe Onoratocb109a02011-01-18 17:57:41 -08003341 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003342 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3343 * use during playback.
3344 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003345 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003346 * @see Notification#sound
3347 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003348 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003349 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003350 mN.sound = sound;
3351 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003352 return this;
3353 }
3354
3355 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003356 * Set the vibration pattern to use.
3357 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003358 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3359 * <code>pattern</code> parameter.
3360 *
Chris Wren47c20a12014-06-18 17:27:29 -04003361 * <p>
3362 * A notification that vibrates is more likely to be presented as a heads-up notification.
3363 * </p>
3364 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003365 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003366 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003367 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003368 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003369 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003370 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003371 return this;
3372 }
3373
Joe Onoratocb109a02011-01-18 17:57:41 -08003374 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003375 * Set the desired color for the indicator LED on the device, as well as the
3376 * blink duty cycle (specified in milliseconds).
3377 *
3378
3379 * Not all devices will honor all (or even any) of these values.
3380 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003381 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003382 * @see Notification#ledARGB
3383 * @see Notification#ledOnMS
3384 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003385 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003386 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003387 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003388 mN.ledARGB = argb;
3389 mN.ledOnMS = onMs;
3390 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003391 if (onMs != 0 || offMs != 0) {
3392 mN.flags |= FLAG_SHOW_LIGHTS;
3393 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003394 return this;
3395 }
3396
Joe Onoratocb109a02011-01-18 17:57:41 -08003397 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003398 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003399 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003400
3401 * Ongoing notifications cannot be dismissed by the user, so your application or service
3402 * must take care of canceling them.
3403 *
3404
3405 * They are typically used to indicate a background task that the user is actively engaged
3406 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3407 * (e.g., a file download, sync operation, active network connection).
3408 *
3409
3410 * @see Notification#FLAG_ONGOING_EVENT
3411 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003413 public Builder setOngoing(boolean ongoing) {
3414 setFlag(FLAG_ONGOING_EVENT, ongoing);
3415 return this;
3416 }
3417
Joe Onoratocb109a02011-01-18 17:57:41 -08003418 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003419 * Set whether this notification should be colorized. When set, the color set with
3420 * {@link #setColor(int)} will be used as the background color of this notification.
3421 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003422 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3423 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003424 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003425 * For most styles, the coloring will only be applied if the notification is for a
3426 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003427 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003428 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003429 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003430 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003431 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003432 */
3433 public Builder setColorized(boolean colorize) {
3434 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3435 return this;
3436 }
3437
3438 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003439 * Set this flag if you would only like the sound, vibrate
3440 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003441 *
3442 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003443 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003444 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3445 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3446 return this;
3447 }
3448
Joe Onoratocb109a02011-01-18 17:57:41 -08003449 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003450 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003451 *
3452 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003453 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003454 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003455 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003456 return this;
3457 }
3458
Joe Onoratocb109a02011-01-18 17:57:41 -08003459 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003460 * Set whether or not this notification should not bridge to other devices.
3461 *
3462 * <p>Some notifications can be bridged to other devices for remote display.
3463 * This hint can be set to recommend this notification not be bridged.
3464 */
3465 public Builder setLocalOnly(boolean localOnly) {
3466 setFlag(FLAG_LOCAL_ONLY, localOnly);
3467 return this;
3468 }
3469
3470 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003471 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 * <p>
3473 * The value should be one or more of the following fields combined with
3474 * bitwise-or:
3475 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3476 * <p>
3477 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003478 *
3479 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003480 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003481 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003483 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003484 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003485 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003486 return this;
3487 }
3488
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003489 /**
3490 * Set the priority of this notification.
3491 *
3492 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003493 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003494 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003495 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003496 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003498 return this;
3499 }
Joe Malin8d40d042012-11-05 11:36:40 -08003500
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003501 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003502 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003503 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003504 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003506 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003507 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003508 return this;
3509 }
3510
3511 /**
Chris Wrendde75302014-03-26 17:24:15 -04003512 * Add a person that is relevant to this notification.
3513 *
Chris Wrene6c48932014-09-29 17:19:27 -04003514 * <P>
3515 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003516 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3517 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3518 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003519 * </P>
3520 *
3521 * <P>
3522 * The person should be specified by the {@code String} representation of a
3523 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3524 * </P>
3525 *
3526 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3527 * URIs. The path part of these URIs must exist in the contacts database, in the
3528 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3529 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3530 * </P>
3531 *
3532 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003533 * @see Notification#EXTRA_PEOPLE
3534 */
Chris Wrene6c48932014-09-29 17:19:27 -04003535 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003536 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003537 return this;
3538 }
3539
3540 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003541 * Set this notification to be part of a group of notifications sharing the same key.
3542 * Grouped notifications may display in a cluster or stack on devices which
3543 * support such rendering.
3544 *
3545 * <p>To make this notification the summary for its group, also call
3546 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3547 * {@link #setSortKey}.
3548 * @param groupKey The group key of the group.
3549 * @return this object for method chaining
3550 */
3551 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003552 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003553 return this;
3554 }
3555
3556 /**
3557 * Set this notification to be the group summary for a group of notifications.
3558 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003559 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3560 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003561 * @param isGroupSummary Whether this notification should be a group summary.
3562 * @return this object for method chaining
3563 */
3564 public Builder setGroupSummary(boolean isGroupSummary) {
3565 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3566 return this;
3567 }
3568
3569 /**
3570 * Set a sort key that orders this notification among other notifications from the
3571 * same package. This can be useful if an external sort was already applied and an app
3572 * would like to preserve this. Notifications will be sorted lexicographically using this
3573 * value, although providing different priorities in addition to providing sort key may
3574 * cause this value to be ignored.
3575 *
3576 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003577 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003578 *
3579 * @see String#compareTo(String)
3580 */
3581 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003582 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003583 return this;
3584 }
3585
3586 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003587 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003588 *
Griff Hazen720042b2014-02-24 15:46:56 -08003589 * <p>Values within the Bundle will replace existing extras values in this Builder.
3590 *
3591 * @see Notification#extras
3592 */
Griff Hazen959591e2014-05-15 22:26:18 -07003593 public Builder addExtras(Bundle extras) {
3594 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003595 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003596 }
3597 return this;
3598 }
3599
3600 /**
3601 * Set metadata for this notification.
3602 *
3603 * <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 -04003604 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003605 * called.
3606 *
Griff Hazen720042b2014-02-24 15:46:56 -08003607 * <p>Replaces any existing extras values with those from the provided Bundle.
3608 * Use {@link #addExtras} to merge in metadata instead.
3609 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003610 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003611 */
Griff Hazen959591e2014-05-15 22:26:18 -07003612 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003613 if (extras != null) {
3614 mUserExtras = extras;
3615 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003616 return this;
3617 }
3618
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003619 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003620 * Get the current metadata Bundle used by this notification Builder.
3621 *
3622 * <p>The returned Bundle is shared with this Builder.
3623 *
3624 * <p>The current contents of this Bundle are copied into the Notification each time
3625 * {@link #build()} is called.
3626 *
3627 * @see Notification#extras
3628 */
3629 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003630 return mUserExtras;
3631 }
3632
3633 private Bundle getAllExtras() {
3634 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3635 saveExtras.putAll(mN.extras);
3636 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003637 }
3638
3639 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003640 * Add an action to this notification. Actions are typically displayed by
3641 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003642 * <p>
3643 * Every action must have an icon (32dp square and matching the
3644 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3645 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3646 * <p>
3647 * A notification in its expanded form can display up to 3 actions, from left to right in
3648 * the order they were added. Actions will not be displayed when the notification is
3649 * collapsed, however, so be sure that any essential functions may be accessed by the user
3650 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003651 *
3652 * @param icon Resource ID of a drawable that represents the action.
3653 * @param title Text describing the action.
3654 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003655 *
3656 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003657 */
Dan Sandler86647982015-05-13 23:41:13 -04003658 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003659 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003660 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003661 return this;
3662 }
3663
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003664 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003665 * Add an action to this notification. Actions are typically displayed by
3666 * the system as a button adjacent to the notification content.
3667 * <p>
3668 * Every action must have an icon (32dp square and matching the
3669 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3670 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3671 * <p>
3672 * A notification in its expanded form can display up to 3 actions, from left to right in
3673 * the order they were added. Actions will not be displayed when the notification is
3674 * collapsed, however, so be sure that any essential functions may be accessed by the user
3675 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3676 *
3677 * @param action The action to add.
3678 */
3679 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003680 if (action != null) {
3681 mActions.add(action);
3682 }
Griff Hazen959591e2014-05-15 22:26:18 -07003683 return this;
3684 }
3685
3686 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003687 * Alter the complete list of actions attached to this notification.
3688 * @see #addAction(Action).
3689 *
3690 * @param actions
3691 * @return
3692 */
3693 public Builder setActions(Action... actions) {
3694 mActions.clear();
3695 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003696 if (actions[i] != null) {
3697 mActions.add(actions[i]);
3698 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003699 }
3700 return this;
3701 }
3702
3703 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003704 * Add a rich notification style to be applied at build time.
3705 *
3706 * @param style Object responsible for modifying the notification style.
3707 */
3708 public Builder setStyle(Style style) {
3709 if (mStyle != style) {
3710 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003711 if (mStyle != null) {
3712 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003713 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3714 } else {
3715 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003716 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003717 }
3718 return this;
3719 }
3720
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003721 /**
3722 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003723 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003724 * @return The same Builder.
3725 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003726 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003727 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003728 return this;
3729 }
3730
3731 /**
3732 * Supply a replacement Notification whose contents should be shown in insecure contexts
3733 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3734 * @param n A replacement notification, presumably with some or all info redacted.
3735 * @return The same Builder.
3736 */
3737 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003738 if (n != null) {
3739 mN.publicVersion = new Notification();
3740 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3741 } else {
3742 mN.publicVersion = null;
3743 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003744 return this;
3745 }
3746
Griff Hazenb720abe2014-05-20 13:15:30 -07003747 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003748 * Apply an extender to this notification builder. Extenders may be used to add
3749 * metadata or change options on this builder.
3750 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003751 public Builder extend(Extender extender) {
3752 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003753 return this;
3754 }
3755
Dan Sandler4e787062015-06-17 15:09:48 -04003756 /**
3757 * @hide
3758 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003759 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003760 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003761 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003762 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003763 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003764 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003765 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003766 }
3767
Dan Sandler26e81cf2014-05-06 10:01:27 -04003768 /**
3769 * Sets {@link Notification#color}.
3770 *
3771 * @param argb The accent color to use
3772 *
3773 * @return The same Builder.
3774 */
Tor Norbye80756e32015-03-02 09:39:27 -08003775 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003776 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003777 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003778 return this;
3779 }
3780
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003781 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003782 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3783 // This user can never be a badged profile,
3784 // and also includes USER_ALL system notifications.
3785 return null;
3786 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003787 // Note: This assumes that the current user can read the profile badge of the
3788 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003789 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003790 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003791 }
3792
3793 private Bitmap getProfileBadge() {
3794 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003795 if (badge == null) {
3796 return null;
3797 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003798 final int size = mContext.getResources().getDimensionPixelSize(
3799 R.dimen.notification_badge_size);
3800 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003801 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003802 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003803 badge.draw(canvas);
3804 return bitmap;
3805 }
3806
Selim Cinekc848c3a2016-01-13 15:27:30 -08003807 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003808 Bitmap profileBadge = getProfileBadge();
3809
Kenny Guy98193ea2014-07-24 19:54:37 +01003810 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003811 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3812 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003813 if (isColorized()) {
3814 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3815 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3816 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003817 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003818 }
3819
Christoph Studerfe718432014-09-01 18:21:18 +02003820 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003821 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003822 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003823 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003824 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003825 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003826 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003827 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003828 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003829 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003830 }
3831
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003832 /**
3833 * Resets the notification header to its original state
3834 */
3835 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003836 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3837 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003838 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003839 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003840 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003841 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003842 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003843 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003844 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003845 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003846 contentView.setImageViewIcon(R.id.profile_badge, null);
3847 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003848 }
3849
3850 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003851 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3852 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003853 }
3854
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003855 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003856 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003857 }
3858
3859 /**
3860 * @param hasProgress whether the progress bar should be shown and set
3861 */
3862 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003863 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3864 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003865 }
3866
Adrian Roos70d7aa32017-01-11 15:39:06 -08003867 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003868 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003869
Christoph Studerfe718432014-09-01 18:21:18 +02003870 resetStandardTemplate(contentView);
3871
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003872 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003873 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003874 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003875 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003876 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3877 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003878 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003879 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003880 if (!p.ambient) {
3881 setTextViewColorPrimary(contentView, R.id.title);
3882 }
Selim Cinek954cc232016-05-20 13:29:23 -07003883 contentView.setViewLayoutWidth(R.id.title, showProgress
3884 ? ViewGroup.LayoutParams.WRAP_CONTENT
3885 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003886 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003887 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003888 int textId = showProgress ? com.android.internal.R.id.text_line_1
3889 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003890 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003891 if (!p.ambient) {
3892 setTextViewColorSecondary(contentView, textId);
3893 }
Selim Cinek41598732016-01-11 16:58:37 -08003894 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003895 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003896
Selim Cinek279fa862016-06-14 10:57:25 -07003897 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003898
Selim Cinek29603462015-11-17 19:04:39 -08003899 return contentView;
3900 }
3901
Selim Cinek7b9605b2017-01-19 17:36:00 -08003902 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3903 ensureColors();
3904 contentView.setTextColor(id, mPrimaryTextColor);
3905 }
3906
Selim Cinek389edcd2017-05-11 19:16:44 -07003907 /**
3908 * @return the primary text color
3909 * @hide
3910 */
3911 @VisibleForTesting
3912 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003913 ensureColors();
3914 return mPrimaryTextColor;
3915 }
3916
Selim Cinek389edcd2017-05-11 19:16:44 -07003917 /**
3918 * @return the secondary text color
3919 * @hide
3920 */
3921 @VisibleForTesting
3922 public int getSecondaryTextColor() {
3923 ensureColors();
3924 return mSecondaryTextColor;
3925 }
3926
Selim Cinek7b9605b2017-01-19 17:36:00 -08003927 private int getActionBarColor() {
3928 ensureColors();
3929 return mActionBarColor;
3930 }
3931
Selim Cinek622c64a2017-04-17 17:10:05 -07003932 private int getActionBarColorDeEmphasized() {
3933 int backgroundColor = getBackgroundColor();
3934 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3935 }
3936
Selim Cinek7b9605b2017-01-19 17:36:00 -08003937 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3938 ensureColors();
3939 contentView.setTextColor(id, mSecondaryTextColor);
3940 }
3941
3942 private void ensureColors() {
3943 int backgroundColor = getBackgroundColor();
3944 if (mPrimaryTextColor == COLOR_INVALID
3945 || mSecondaryTextColor == COLOR_INVALID
3946 || mActionBarColor == COLOR_INVALID
3947 || mTextColorsAreForBackground != backgroundColor) {
3948 mTextColorsAreForBackground = backgroundColor;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003949 if (mForegroundColor == COLOR_INVALID || !isColorized()) {
3950 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
3951 backgroundColor);
3952 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
3953 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07003954 if (backgroundColor != COLOR_DEFAULT
3955 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
3956 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3957 mPrimaryTextColor, backgroundColor, 4.5);
3958 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3959 mSecondaryTextColor, backgroundColor, 4.5);
3960 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07003961 } else {
3962 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
3963 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
3964 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
3965 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07003966 // We only respect the given colors if worst case Black or White still has
3967 // contrast
3968 boolean backgroundLight = backLum > textLum
3969 && satisfiesTextContrast(backgroundColor, Color.BLACK)
3970 || backLum <= textLum
3971 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003972 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07003973 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07003974 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
3975 mForegroundColor,
3976 backgroundColor,
3977 true /* findFG */,
3978 4.5f);
3979 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003980 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003981 } else {
3982 mSecondaryTextColor =
3983 NotificationColorUtil.findContrastColorAgainstDark(
3984 mForegroundColor,
3985 backgroundColor,
3986 true /* findFG */,
3987 4.5f);
3988 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003989 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003990 }
3991 } else {
3992 mPrimaryTextColor = mForegroundColor;
3993 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003994 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
3995 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003996 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
3997 backgroundColor) < 4.5f) {
3998 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07003999 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004000 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4001 mSecondaryTextColor,
4002 backgroundColor,
4003 true /* findFG */,
4004 4.5f);
4005 } else {
4006 mSecondaryTextColor
4007 = NotificationColorUtil.findContrastColorAgainstDark(
4008 mSecondaryTextColor,
4009 backgroundColor,
4010 true /* findFG */,
4011 4.5f);
4012 }
4013 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004014 mSecondaryTextColor, backgroundLight
4015 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4016 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004017 }
4018 }
4019 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004020 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4021 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004022 }
4023 }
4024
4025 private void updateBackgroundColor(RemoteViews contentView) {
4026 if (isColorized()) {
4027 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4028 getBackgroundColor());
4029 } else {
4030 // Clear it!
4031 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4032 0);
4033 }
4034 }
4035
Selim Cinek860b6da2015-12-16 19:02:19 -08004036 /**
4037 * @param remoteView the remote view to update the minheight in
4038 * @param hasMinHeight does it have a mimHeight
4039 * @hide
4040 */
4041 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4042 int minHeight = 0;
4043 if (hasMinHeight) {
4044 // we need to set the minHeight of the notification
4045 minHeight = mContext.getResources().getDimensionPixelSize(
4046 com.android.internal.R.dimen.notification_min_content_height);
4047 }
4048 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4049 }
4050
Selim Cinek29603462015-11-17 19:04:39 -08004051 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004052 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4053 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4054 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4055 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004056 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004057 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004058 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004059 contentView.setProgressBackgroundTintList(
4060 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4061 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004062 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004063 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004064 contentView.setProgressTintList(R.id.progress, colorStateList);
4065 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004066 }
Selim Cinek29603462015-11-17 19:04:39 -08004067 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004068 } else {
4069 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004070 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004071 }
Joe Onorato561d3852010-11-20 18:09:34 -08004072 }
4073
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004074 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07004075 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4076 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4077 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004078 if (mN.mLargeIcon != null) {
4079 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4080 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4081 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004082 int endMargin = R.dimen.notification_content_picture_margin;
4083 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4084 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4085 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004086 // Bind the reply action
4087 Action action = findReplyAction();
4088 contentView.setViewVisibility(R.id.reply_icon_action, action != null
4089 ? View.VISIBLE
4090 : View.GONE);
4091
4092 if (action != null) {
4093 int contrastColor = resolveContrastColor();
4094 contentView.setDrawableParameters(R.id.reply_icon_action,
4095 true /* targetBackground */,
4096 -1,
4097 contrastColor,
4098 PorterDuff.Mode.SRC_ATOP, -1);
4099 int iconColor = NotificationColorUtil.isColorLight(contrastColor)
4100 ? Color.BLACK : Color.WHITE;
4101 contentView.setDrawableParameters(R.id.reply_icon_action,
4102 false /* targetBackground */,
4103 -1,
4104 iconColor,
4105 PorterDuff.Mode.SRC_ATOP, -1);
4106 contentView.setOnClickPendingIntent(R.id.right_icon,
4107 action.actionIntent);
4108 contentView.setOnClickPendingIntent(R.id.reply_icon_action,
4109 action.actionIntent);
4110 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
4111 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4112
4113 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004114 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004115 contentView.setViewVisibility(R.id.right_icon_container, mN.mLargeIcon != null
4116 ? View.VISIBLE
4117 : View.GONE);
4118 }
4119
4120 private Action findReplyAction() {
4121 ArrayList<Action> actions = mActions;
4122 if (mOriginalActions != null) {
4123 actions = mOriginalActions;
4124 }
4125 int numActions = actions.size();
4126 for (int i = 0; i < numActions; i++) {
4127 Action action = actions.get(i);
4128 if (hasValidRemoteInput(action)) {
4129 return action;
4130 }
4131 }
4132 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004133 }
4134
Adrian Roos487374f2017-01-11 15:48:14 -08004135 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4136 bindSmallIcon(contentView, ambient);
4137 bindHeaderAppName(contentView, ambient);
4138 if (!ambient) {
4139 // Ambient view does not have these
4140 bindHeaderText(contentView);
4141 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004142 bindProfileBadge(contentView);
4143 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004144 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004145 }
4146
4147 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004148 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08004149 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004150 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004151 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004152 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004153 }
4154
Selim Cinek99104832017-01-25 14:47:33 -08004155 /**
4156 * @return the color that is used as the first primary highlight color. This is applied
4157 * in several places like the action buttons or the app name in the header.
4158 */
4159 private int getPrimaryHighlightColor() {
4160 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4161 }
4162
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004163 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4164 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004165 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004166 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004167 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4168 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4169 contentView.setLong(R.id.chronometer, "setBase",
4170 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4171 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004172 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004173 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004174 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004175 } else {
4176 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4177 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004178 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004179 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004180 } else {
4181 // We still want a time to be set but gone, such that we can show and hide it
4182 // on demand in case it's a child notification without anything in the header
4183 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004184 }
4185 }
4186
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004187 private void bindHeaderText(RemoteViews contentView) {
4188 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4189 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004190 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004191 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004192 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004193 if (headerText == null
4194 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4195 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4196 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4197 }
4198 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004199 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004200 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004201 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004202 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4203 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004204 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004205 }
4206 }
4207
Adrian Rooseba05822016-04-22 17:09:27 -07004208 /**
4209 * @hide
4210 */
4211 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004212 CharSequence name = null;
4213 final PackageManager pm = mContext.getPackageManager();
4214 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4215 // only system packages which lump together a bunch of unrelated stuff
4216 // may substitute a different name to make the purpose of the
4217 // notification more clear. the correct package label should always
4218 // be accessible via SystemUI.
4219 final String pkg = mContext.getPackageName();
4220 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4221 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4222 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4223 name = subName;
4224 } else {
4225 Log.w(TAG, "warning: pkg "
4226 + pkg + " attempting to substitute app name '" + subName
4227 + "' without holding perm "
4228 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4229 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004230 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004231 if (TextUtils.isEmpty(name)) {
4232 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4233 }
4234 if (TextUtils.isEmpty(name)) {
4235 // still nothing?
4236 return null;
4237 }
4238
4239 return String.valueOf(name);
4240 }
Adrian Roos487374f2017-01-11 15:48:14 -08004241 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004242 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004243 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004244 setTextViewColorPrimary(contentView, R.id.app_name_text);
4245 } else {
4246 contentView.setTextColor(R.id.app_name_text,
4247 ambient ? resolveAmbientColor() : resolveContrastColor());
4248 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004249 }
4250
Adrian Roos487374f2017-01-11 15:48:14 -08004251 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004252 if (mN.mSmallIcon == null && mN.icon != 0) {
4253 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4254 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004255 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07004256 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
4257 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004258 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004259 }
4260
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004261 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004262 * @return true if the built notification will show the time or the chronometer; false
4263 * otherwise
4264 */
4265 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004266 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004267 }
4268
Christoph Studerfe718432014-09-01 18:21:18 +02004269 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004270 // actions_container is only reset when there are no actions to avoid focus issues with
4271 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004272 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004273 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004274
4275 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4276 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4277
4278 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4279 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4280 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4281 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004282
4283 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004284 }
4285
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004286 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004287 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004288 }
4289
Adrian Roos70d7aa32017-01-11 15:39:06 -08004290 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4291 StandardTemplateParams p) {
4292 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004293
Christoph Studerfe718432014-09-01 18:21:18 +02004294 resetStandardTemplateWithActions(big);
4295
Adrian Roose458aa82015-12-08 16:17:19 -08004296 boolean validRemoteInput = false;
4297
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004298 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004299 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004300 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004301 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004302 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004303 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004304 if (p.ambient) {
4305 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004306 } else if (isColorized()) {
4307 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4308 } else {
4309 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4310 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004311 }
Adrian Roosf852a422016-06-03 13:33:43 -07004312 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4313 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004314 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004315 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004316 Action action = mActions.get(i);
4317 validRemoteInput |= hasValidRemoteInput(action);
4318
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004319 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004320 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004321 big.addView(R.id.actions, button);
4322 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004323 } else {
4324 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004325 }
Adrian Roose458aa82015-12-08 16:17:19 -08004326
4327 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004328 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004329 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4330 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4331 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004332 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004333
4334 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4335 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4336 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004337 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004338
4339 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4340 big.setViewVisibility(
4341 R.id.notification_material_reply_text_3, View.VISIBLE);
4342 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004343 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004344 }
4345 }
4346 }
4347
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004348 return big;
4349 }
4350
Adrian Roose458aa82015-12-08 16:17:19 -08004351 private boolean hasValidRemoteInput(Action action) {
4352 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4353 // Weird actions
4354 return false;
4355 }
4356
4357 RemoteInput[] remoteInputs = action.getRemoteInputs();
4358 if (remoteInputs == null) {
4359 return false;
4360 }
4361
4362 for (RemoteInput r : remoteInputs) {
4363 CharSequence[] choices = r.getChoices();
4364 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4365 return true;
4366 }
4367 }
4368 return false;
4369 }
4370
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004371 /**
4372 * Construct a RemoteViews for the final 1U notification layout. In order:
4373 * 1. Custom contentView from the caller
4374 * 2. Style's proposed content view
4375 * 3. Standard template view
4376 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004377 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004378 return createContentView(false /* increasedheight */ );
4379 }
4380
4381 /**
4382 * Construct a RemoteViews for the smaller content view.
4383 *
4384 * @param increasedHeight true if this layout be created with an increased height. Some
4385 * styles may support showing more then just that basic 1U size
4386 * and the system may decide to render important notifications
4387 * slightly bigger even when collapsed.
4388 *
4389 * @hide
4390 */
4391 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004392 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004393 return mN.contentView;
4394 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004395 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004396 if (styleView != null) {
4397 return styleView;
4398 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004399 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004400 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004401 }
4402
Selim Cineka7679b62017-05-10 16:33:25 -07004403 private boolean useExistingRemoteView() {
4404 return mStyle == null || (!mStyle.displayCustomViewInline()
4405 && !mRebuildStyledRemoteViews);
4406 }
4407
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004408 /**
4409 * Construct a RemoteViews for the final big notification layout.
4410 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004411 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004412 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004413 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004414 return mN.bigContentView;
4415 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004416 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004417 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004418 } else if (mActions.size() != 0) {
4419 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004420 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004421 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004422 return result;
4423 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004424
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004425 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004426 * Construct a RemoteViews for the final notification header only. This will not be
4427 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004428 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004429 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004430 * @hide
4431 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004432 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004433 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4434 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004435 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004436 ambient ? R.layout.notification_template_ambient_header
4437 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004438 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004439 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004440 if (colorized != null) {
4441 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4442 } else {
4443 mN.extras.remove(EXTRA_COLORIZED);
4444 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004445 return header;
4446 }
4447
Adrian Roos487374f2017-01-11 15:48:14 -08004448 /**
4449 * Construct a RemoteViews for the ambient version of the notification.
4450 *
4451 * @hide
4452 */
4453 public RemoteViews makeAmbientNotification() {
4454 RemoteViews ambient = applyStandardTemplateWithActions(
4455 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004456 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004457 return ambient;
4458 }
4459
Selim Cinek29603462015-11-17 19:04:39 -08004460 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004461 if (result != null) {
4462 result.setViewVisibility(R.id.text_line_1, View.GONE);
4463 }
Selim Cinek29603462015-11-17 19:04:39 -08004464 }
4465
Selim Cinek6743c0b2017-01-18 18:24:01 -08004466 /**
4467 * Adapt the Notification header if this view is used as an expanded view.
4468 *
4469 * @hide
4470 */
4471 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004472 if (result != null) {
4473 result.setBoolean(R.id.notification_header, "setExpanded", true);
4474 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004475 }
4476
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004477 /**
4478 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004479 *
4480 * @param increasedHeight true if this layout be created with an increased height. Some
4481 * styles may support showing more then just that basic 1U size
4482 * and the system may decide to render important notifications
4483 * slightly bigger even when collapsed.
4484 *
4485 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004486 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004487 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004488 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004489 return mN.headsUpContentView;
4490 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004491 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4492 if (styleView != null) {
4493 return styleView;
4494 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004495 } else if (mActions.size() == 0) {
4496 return null;
4497 }
4498
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004499 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004500 }
4501
Selim Cinek624c02db2015-12-14 21:00:02 -08004502 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004503 * Construct a RemoteViews for the final heads-up notification layout.
4504 */
4505 public RemoteViews createHeadsUpContentView() {
4506 return createHeadsUpContentView(false /* useIncreasedHeight */);
4507 }
4508
4509 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004510 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4511 *
4512 * @hide
4513 */
4514 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004515 return makePublicView(false /* ambient */);
4516 }
4517
4518 /**
4519 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4520 *
4521 * @hide
4522 */
4523 public RemoteViews makePublicAmbientNotification() {
4524 return makePublicView(true /* ambient */);
4525 }
4526
4527 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004528 if (mN.publicVersion != null) {
4529 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004530 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004531 }
4532 Bundle savedBundle = mN.extras;
4533 Style style = mStyle;
4534 mStyle = null;
4535 Icon largeIcon = mN.mLargeIcon;
4536 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004537 Bitmap largeIconLegacy = mN.largeIcon;
4538 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004539 ArrayList<Action> actions = mActions;
4540 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004541 Bundle publicExtras = new Bundle();
4542 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4543 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4544 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4545 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004546 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4547 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004548 publicExtras.putCharSequence(EXTRA_TITLE,
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004549 mContext.getString(com.android.internal.R.string.notification_hidden_text));
Selim Cinek624c02db2015-12-14 21:00:02 -08004550 mN.extras = publicExtras;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004551 final RemoteViews view = ambient ? makeAmbientNotification()
4552 : applyStandardTemplate(getBaseLayoutResource());
Selim Cinek624c02db2015-12-14 21:00:02 -08004553 mN.extras = savedBundle;
4554 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004555 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004556 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004557 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004558 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004559 }
4560
Selim Cinek6743c0b2017-01-18 18:24:01 -08004561 /**
4562 * Construct a content view for the display when low - priority
4563 *
4564 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4565 * a new subtext is created consisting of the content of the
4566 * notification.
4567 * @hide
4568 */
4569 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4570 int color = mN.color;
4571 mN.color = COLOR_DEFAULT;
4572 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4573 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4574 CharSequence newSummary = createSummaryText();
4575 if (!TextUtils.isEmpty(newSummary)) {
4576 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4577 }
4578 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004579
Adrian Roos6f6e1592017-05-02 16:22:53 -07004580 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004581 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004582 if (summary != null) {
4583 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4584 } else {
4585 mN.extras.remove(EXTRA_SUB_TEXT);
4586 }
4587 mN.color = color;
4588 return header;
4589 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004590
Selim Cinek6743c0b2017-01-18 18:24:01 -08004591 private CharSequence createSummaryText() {
4592 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4593 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4594 return titleText;
4595 }
4596 SpannableStringBuilder summary = new SpannableStringBuilder();
4597 if (titleText == null) {
4598 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4599 }
4600 BidiFormatter bidi = BidiFormatter.getInstance();
4601 if (titleText != null) {
4602 summary.append(bidi.unicodeWrap(titleText));
4603 }
4604 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4605 if (titleText != null && contentText != null) {
4606 summary.append(bidi.unicodeWrap(mContext.getText(
4607 R.string.notification_header_divider_symbol_with_spaces)));
4608 }
4609 if (contentText != null) {
4610 summary.append(bidi.unicodeWrap(contentText));
4611 }
4612 return summary;
4613 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004614
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004615 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004616 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004617 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004618 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004619 emphazisedMode ? getEmphasizedActionLayoutResource()
4620 : tombstone ? getActionTombstoneLayoutResource()
4621 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004622 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004623 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004624 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004625 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004626 if (action.mRemoteInputs != null) {
4627 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4628 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004629 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004630 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004631 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004632 int bgColor;
4633 if (isColorized()) {
4634 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4635 } else {
4636 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4637 : R.color.notification_action_list_dark);
4638 }
Selim Cinek981962e2016-07-20 20:41:58 -07004639 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004640 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004641 CharSequence title = action.title;
4642 ColorStateList[] outResultColor = null;
4643 if (isLegacy()) {
4644 title = clearColorSpans(title);
4645 } else {
4646 outResultColor = new ColorStateList[1];
4647 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4648 }
4649 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004650 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004651 if (outResultColor != null && outResultColor[0] != null) {
4652 // We need to set the text color as well since changing a text to uppercase
4653 // clears its spans.
4654 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004655 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004656 button.setTextColor(R.id.action0,resolveContrastColor());
4657 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004658 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004659 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004660 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004661 setTextViewColorPrimary(button, R.id.action0);
4662 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004663 button.setTextColor(R.id.action0,
4664 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004665 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004666 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004667 return button;
4668 }
4669
Joe Onoratocb109a02011-01-18 17:57:41 -08004670 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004671 * Clears all color spans of a text
4672 * @param charSequence the input text
4673 * @return the same text but without color spans
4674 */
4675 private CharSequence clearColorSpans(CharSequence charSequence) {
4676 if (charSequence instanceof Spanned) {
4677 Spanned ss = (Spanned) charSequence;
4678 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4679 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4680 for (Object span : spans) {
4681 Object resultSpan = span;
4682 if (resultSpan instanceof CharacterStyle) {
4683 resultSpan = ((CharacterStyle) span).getUnderlying();
4684 }
4685 if (resultSpan instanceof TextAppearanceSpan) {
4686 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4687 if (originalSpan.getTextColor() != null) {
4688 resultSpan = new TextAppearanceSpan(
4689 originalSpan.getFamily(),
4690 originalSpan.getTextStyle(),
4691 originalSpan.getTextSize(),
4692 null,
4693 originalSpan.getLinkTextColor());
4694 }
4695 } else if (resultSpan instanceof ForegroundColorSpan
4696 || (resultSpan instanceof BackgroundColorSpan)) {
4697 continue;
4698 } else {
4699 resultSpan = span;
4700 }
4701 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4702 ss.getSpanFlags(span));
4703 }
4704 return builder;
4705 }
4706 return charSequence;
4707 }
4708
4709 /**
4710 * Ensures contrast on color spans against a background color. also returns the color of the
4711 * text if a span was found that spans over the whole text.
4712 *
4713 * @param charSequence the charSequence on which the spans are
4714 * @param background the background color to ensure the contrast against
4715 * @param outResultColor an array in which a color will be returned as the first element if
4716 * there exists a full length color span.
4717 * @return the contrasted charSequence
4718 */
4719 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4720 ColorStateList[] outResultColor) {
4721 if (charSequence instanceof Spanned) {
4722 Spanned ss = (Spanned) charSequence;
4723 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4724 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4725 for (Object span : spans) {
4726 Object resultSpan = span;
4727 int spanStart = ss.getSpanStart(span);
4728 int spanEnd = ss.getSpanEnd(span);
4729 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4730 if (resultSpan instanceof CharacterStyle) {
4731 resultSpan = ((CharacterStyle) span).getUnderlying();
4732 }
4733 if (resultSpan instanceof TextAppearanceSpan) {
4734 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4735 ColorStateList textColor = originalSpan.getTextColor();
4736 if (textColor != null) {
4737 int[] colors = textColor.getColors();
4738 int[] newColors = new int[colors.length];
4739 for (int i = 0; i < newColors.length; i++) {
4740 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4741 colors[i], background);
4742 }
4743 textColor = new ColorStateList(textColor.getStates().clone(),
4744 newColors);
4745 resultSpan = new TextAppearanceSpan(
4746 originalSpan.getFamily(),
4747 originalSpan.getTextStyle(),
4748 originalSpan.getTextSize(),
4749 textColor,
4750 originalSpan.getLinkTextColor());
4751 if (fullLength) {
4752 outResultColor[0] = new ColorStateList(
4753 textColor.getStates().clone(), newColors);
4754 }
4755 }
4756 } else if (resultSpan instanceof ForegroundColorSpan) {
4757 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4758 int foregroundColor = originalSpan.getForegroundColor();
4759 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4760 foregroundColor, background);
4761 resultSpan = new ForegroundColorSpan(foregroundColor);
4762 if (fullLength) {
4763 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4764 }
4765 } else {
4766 resultSpan = span;
4767 }
4768
4769 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4770 }
4771 return builder;
4772 }
4773 return charSequence;
4774 }
4775
4776 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004777 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004778 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004779 */
4780 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004781 if (!mIsLegacyInitialized) {
4782 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4783 < Build.VERSION_CODES.LOLLIPOP;
4784 mIsLegacyInitialized = true;
4785 }
4786 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004787 }
4788
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004789 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004790 return processLegacyText(charSequence, false /* ambient */);
4791 }
4792
4793 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4794 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4795 boolean wantLightText = ambient;
4796 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004797 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004798 } else {
4799 return charSequence;
4800 }
4801 }
4802
Dan Sandler26e81cf2014-05-06 10:01:27 -04004803 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004804 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004805 */
Adrian Roos487374f2017-01-11 15:48:14 -08004806 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4807 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004808 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004809 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004810 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004811 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004812 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004813
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004814 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004815 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004816 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004817 }
4818
Dan Sandler26e81cf2014-05-06 10:01:27 -04004819 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004820 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004821 * if it's grayscale).
4822 */
4823 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004824 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4825 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004826 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004827 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004828 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004829 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004830 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004831 }
4832
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004833 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004834 if (mN.color != COLOR_DEFAULT) {
4835 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004836 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004837 }
4838
Adrian Roos4ff3b122016-02-01 12:26:13 -08004839 int resolveContrastColor() {
4840 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4841 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004842 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004843
Selim Cinekac5f0272017-05-02 16:05:41 -07004844 int color;
4845 int background = mBackgroundColorHint;
4846 if (mBackgroundColorHint == COLOR_INVALID) {
4847 background = mContext.getColor(
4848 com.android.internal.R.color.notification_material_background_color);
4849 }
4850 if (mN.color == COLOR_DEFAULT) {
4851 ensureColors();
4852 color = mSecondaryTextColor;
4853 } else {
4854 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
4855 background);
4856 }
4857 if (Color.alpha(color) < 255) {
4858 // alpha doesn't go well for color filters, so let's blend it manually
4859 color = NotificationColorUtil.compositeColors(color, background);
4860 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004861 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004862 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004863 }
4864
Adrian Roos487374f2017-01-11 15:48:14 -08004865 int resolveAmbientColor() {
4866 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4867 return mCachedAmbientColor;
4868 }
4869 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4870
4871 mCachedAmbientColorIsFor = mN.color;
4872 return mCachedAmbientColor = contrasted;
4873 }
4874
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004875 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004876 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004877 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004878 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004879 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004880 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004881 mN.actions = new Action[mActions.size()];
4882 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004883 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004884 if (!mPersonList.isEmpty()) {
4885 mN.extras.putStringArray(EXTRA_PEOPLE,
4886 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004887 }
Selim Cinek247fa012016-02-18 09:50:48 -08004888 if (mN.bigContentView != null || mN.contentView != null
4889 || mN.headsUpContentView != null) {
4890 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4891 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004892 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004893 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004894
Julia Reynolds3b848122016-02-26 10:45:32 -05004895 /**
4896 * Creates a Builder from an existing notification so further changes can be made.
4897 * @param context The context for your application / activity.
4898 * @param n The notification to create a Builder from.
4899 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004900 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004901 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004902 ApplicationInfo applicationInfo = n.extras.getParcelable(
4903 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004904 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004905 if (applicationInfo != null) {
4906 try {
4907 builderContext = context.createApplicationContext(applicationInfo,
4908 Context.CONTEXT_RESTRICTED);
4909 } catch (NameNotFoundException e) {
4910 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4911 builderContext = context; // try with our context
4912 }
4913 } else {
4914 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004915 }
4916
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004917 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004918 }
4919
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004920 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004921 * @deprecated Use {@link #build()} instead.
4922 */
4923 @Deprecated
4924 public Notification getNotification() {
4925 return build();
4926 }
4927
4928 /**
4929 * Combine all of the options that have been set and return a new {@link Notification}
4930 * object.
4931 */
4932 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004933 // first, add any extras from the calling code
4934 if (mUserExtras != null) {
4935 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004936 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004937
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004938 mN.creationTime = System.currentTimeMillis();
4939
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004940 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004941 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004942
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004943 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004944
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004945 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004946 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004947 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004948
Adrian Roos5081c0d2016-02-26 16:04:19 -08004949 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07004950 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004951 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004952 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004953 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4954 mN.contentView.getSequenceNumber());
4955 }
4956 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004957 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004958 if (mN.bigContentView != null) {
4959 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4960 mN.bigContentView.getSequenceNumber());
4961 }
4962 }
4963 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004964 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004965 if (mN.headsUpContentView != null) {
4966 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4967 mN.headsUpContentView.getSequenceNumber());
4968 }
4969 }
4970 }
4971
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004972 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4973 mN.flags |= FLAG_SHOW_LIGHTS;
4974 }
4975
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004976 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004977 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004978
4979 /**
4980 * Apply this Builder to an existing {@link Notification} object.
4981 *
4982 * @hide
4983 */
4984 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004985 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004986 return n;
4987 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004988
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004989 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004990 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4991 * change.
4992 *
4993 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4994 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004995 * @hide
4996 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004997 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004998 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004999
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005000 // Only strip views for known Styles because we won't know how to
5001 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08005002 if (!TextUtils.isEmpty(templateClass)
5003 && getNotificationStyleClass(templateClass) == null) {
5004 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005005 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005006
5007 // Only strip unmodified BuilderRemoteViews.
5008 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005009 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005010 n.contentView.getSequenceNumber();
5011 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005012 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005013 n.bigContentView.getSequenceNumber();
5014 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005015 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005016 n.headsUpContentView.getSequenceNumber();
5017
5018 // Nothing to do here, no need to clone.
5019 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
5020 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005021 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005022
5023 Notification clone = n.clone();
5024 if (stripContentView) {
5025 clone.contentView = null;
5026 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5027 }
5028 if (stripBigContentView) {
5029 clone.bigContentView = null;
5030 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5031 }
5032 if (stripHeadsUpContentView) {
5033 clone.headsUpContentView = null;
5034 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5035 }
5036 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005037 }
5038
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005039 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005040 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005041 }
5042
5043 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005044 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005045 }
5046
5047 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005048 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005049 }
5050
5051 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005052 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005053 }
5054
5055 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005056 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005057 }
5058
Adrian Roosc1a80b02016-04-05 14:54:55 -07005059 private int getMessagingLayoutResource() {
5060 return R.layout.notification_template_material_messaging;
5061 }
5062
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005063 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005064 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005065 }
5066
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005067 private int getEmphasizedActionLayoutResource() {
5068 return R.layout.notification_material_action_emphasized;
5069 }
5070
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005071 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005072 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005073 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005074
5075 private int getBackgroundColor() {
5076 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005077 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005078 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005079 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5080 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005081 }
5082 }
5083
5084 private boolean isColorized() {
5085 return mN.isColorized();
5086 }
Selim Cinek99104832017-01-25 14:47:33 -08005087
5088 private boolean textColorsNeedInversion() {
5089 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5090 return false;
5091 }
5092 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5093 return targetSdkVersion > Build.VERSION_CODES.M
5094 && targetSdkVersion < Build.VERSION_CODES.O;
5095 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005096
5097 /**
5098 * Set a color palette to be used as the background and textColors
5099 *
5100 * @param backgroundColor the color to be used as the background
5101 * @param foregroundColor the color to be used as the foreground
5102 *
5103 * @hide
5104 */
5105 public void setColorPalette(int backgroundColor, int foregroundColor) {
5106 mBackgroundColor = backgroundColor;
5107 mForegroundColor = foregroundColor;
5108 mTextColorsAreForBackground = COLOR_INVALID;
5109 ensureColors();
5110 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005111
5112 /**
5113 * Sets the background color for this notification to be a different one then the default.
5114 * This is mainly used to calculate contrast and won't necessarily be applied to the
5115 * background.
5116 *
5117 * @hide
5118 */
5119 public void setBackgroundColorHint(int backgroundColor) {
5120 mBackgroundColorHint = backgroundColor;
5121 }
Selim Cineka7679b62017-05-10 16:33:25 -07005122
5123
5124 /**
5125 * Forces all styled remoteViews to be built from scratch and not use any cached
5126 * RemoteViews.
5127 * This is needed for legacy apps that are baking in their remoteviews into the
5128 * notification.
5129 *
5130 * @hide
5131 */
5132 public void setRebuildStyledRemoteViews(boolean rebuild) {
5133 mRebuildStyledRemoteViews = rebuild;
5134 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005135 }
5136
5137 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005138 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005139 */
Selim Cinek22714f12017-04-13 16:23:53 -07005140 private boolean isForegroundService() {
5141 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005142 }
5143
5144 /**
Selim Cinek99104832017-01-25 14:47:33 -08005145 * @return whether this notification has a media session attached
5146 * @hide
5147 */
5148 public boolean hasMediaSession() {
5149 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5150 }
5151
5152 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005153 * @return the style class of this notification
5154 * @hide
5155 */
5156 public Class<? extends Notification.Style> getNotificationStyle() {
5157 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5158
5159 if (!TextUtils.isEmpty(templateClass)) {
5160 return Notification.getNotificationStyleClass(templateClass);
5161 }
5162 return null;
5163 }
5164
5165 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005166 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005167 *
5168 * @hide
5169 */
5170 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005171 if (isColorizedMedia()) {
5172 return true;
5173 }
Julia Reynoldsd71c5a92017-06-30 13:34:01 -04005174 return extras.getBoolean(EXTRA_COLORIZED)
5175 && (hasColorizedPermission() || isForegroundService());
5176 }
5177
5178 /**
5179 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5180 * permission. The permission is checked when a notification is enqueued.
5181 */
5182 private boolean hasColorizedPermission() {
5183 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005184 }
5185
5186 /**
5187 * @return true if this notification is colorized and it is a media notification
5188 *
5189 * @hide
5190 */
5191 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005192 Class<? extends Style> style = getNotificationStyle();
5193 if (MediaStyle.class.equals(style)) {
5194 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5195 if ((colorized == null || colorized) && hasMediaSession()) {
5196 return true;
5197 }
5198 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5199 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5200 return true;
5201 }
5202 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005203 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005204 }
5205
Selim Cinek0847acd2017-04-24 19:48:29 -07005206
5207 /**
5208 * @return true if this is a media notification
5209 *
5210 * @hide
5211 */
5212 public boolean isMediaNotification() {
5213 Class<? extends Style> style = getNotificationStyle();
5214 if (MediaStyle.class.equals(style)) {
5215 return true;
5216 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5217 return true;
5218 }
5219 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005220 }
5221
Selim Cinek279fa862016-06-14 10:57:25 -07005222 private boolean hasLargeIcon() {
5223 return mLargeIcon != null || largeIcon != null;
5224 }
5225
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005226 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005227 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005228 * @hide
5229 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005230 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005231 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5232 }
5233
5234 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005235 * @return true if the notification will show a chronometer; false otherwise
5236 * @hide
5237 */
5238 public boolean showsChronometer() {
5239 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5240 }
5241
5242 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005243 * @hide
5244 */
5245 @SystemApi
5246 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5247 Class<? extends Style>[] classes = new Class[] {
5248 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5249 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5250 MessagingStyle.class };
5251 for (Class<? extends Style> innerClass : classes) {
5252 if (templateClass.equals(innerClass.getName())) {
5253 return innerClass;
5254 }
5255 }
5256 return null;
5257 }
5258
5259 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005260 * An object that can apply a rich notification style to a {@link Notification.Builder}
5261 * object.
5262 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005263 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005264 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005265
5266 /**
5267 * @hide
5268 */
5269 protected CharSequence mSummaryText = null;
5270
5271 /**
5272 * @hide
5273 */
5274 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005275
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005276 protected Builder mBuilder;
5277
Chris Wrend6297db2012-05-03 16:20:13 -04005278 /**
5279 * Overrides ContentTitle in the big form of the template.
5280 * This defaults to the value passed to setContentTitle().
5281 */
5282 protected void internalSetBigContentTitle(CharSequence title) {
5283 mBigContentTitle = title;
5284 }
5285
5286 /**
5287 * Set the first line of text after the detail section in the big form of the template.
5288 */
5289 protected void internalSetSummaryText(CharSequence cs) {
5290 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005291 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005292 }
5293
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005294 public void setBuilder(Builder builder) {
5295 if (mBuilder != builder) {
5296 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005297 if (mBuilder != null) {
5298 mBuilder.setStyle(this);
5299 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005300 }
5301 }
5302
Chris Wrend6297db2012-05-03 16:20:13 -04005303 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005304 if (mBuilder == null) {
5305 throw new IllegalArgumentException("Style requires a valid Builder object");
5306 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005307 }
Chris Wrend6297db2012-05-03 16:20:13 -04005308
5309 protected RemoteViews getStandardView(int layoutId) {
5310 checkBuilder();
5311
Christoph Studer4600f9b2014-07-22 22:44:43 +02005312 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005313 CharSequence oldBuilderContentTitle =
5314 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005315 if (mBigContentTitle != null) {
5316 mBuilder.setContentTitle(mBigContentTitle);
5317 }
5318
Chris Wrend6297db2012-05-03 16:20:13 -04005319 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5320
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005321 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005322
Chris Wrend6297db2012-05-03 16:20:13 -04005323 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5324 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005325 } else {
5326 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005327 }
5328
Chris Wrend6297db2012-05-03 16:20:13 -04005329 return contentView;
5330 }
5331
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005332 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005333 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005334 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005335 *
5336 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005337 * @hide
5338 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005339 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005340 return null;
5341 }
5342
5343 /**
5344 * Construct a Style-specific RemoteViews for the final big notification layout.
5345 * @hide
5346 */
5347 public RemoteViews makeBigContentView() {
5348 return null;
5349 }
5350
5351 /**
5352 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005353 *
5354 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005355 * @hide
5356 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005357 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005358 return null;
5359 }
5360
5361 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005362 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005363 * @hide
5364 */
5365 public void addExtras(Bundle extras) {
5366 if (mSummaryTextSet) {
5367 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5368 }
5369 if (mBigContentTitle != null) {
5370 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5371 }
Chris Wren91ad5632013-06-05 15:05:57 -04005372 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005373 }
5374
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005375 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005376 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005377 * @hide
5378 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005379 protected void restoreFromExtras(Bundle extras) {
5380 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5381 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5382 mSummaryTextSet = true;
5383 }
5384 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5385 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5386 }
5387 }
5388
5389
5390 /**
5391 * @hide
5392 */
5393 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005394 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005395 return wip;
5396 }
5397
Daniel Sandler0ec46202015-06-24 01:27:05 -04005398 /**
5399 * @hide
5400 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005401 public void purgeResources() {}
5402
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005403 /**
5404 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5405 * attached to.
5406 *
5407 * @return the fully constructed Notification.
5408 */
5409 public Notification build() {
5410 checkBuilder();
5411 return mBuilder.build();
5412 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005413
5414 /**
5415 * @hide
5416 * @return true if the style positions the progress bar on the second line; false if the
5417 * style hides the progress bar
5418 */
5419 protected boolean hasProgress() {
5420 return true;
5421 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005422
5423 /**
5424 * @hide
5425 * @return Whether we should put the summary be put into the notification header
5426 */
5427 public boolean hasSummaryInHeader() {
5428 return true;
5429 }
Selim Cinek593610c2016-02-16 18:42:57 -08005430
5431 /**
5432 * @hide
5433 * @return Whether custom content views are displayed inline in the style
5434 */
5435 public boolean displayCustomViewInline() {
5436 return false;
5437 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005438 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005439
5440 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005441 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005442 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005443 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005444 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005445 * Notification notif = new Notification.Builder(mContext)
5446 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5447 * .setContentText(subject)
5448 * .setSmallIcon(R.drawable.new_post)
5449 * .setLargeIcon(aBitmap)
5450 * .setStyle(new Notification.BigPictureStyle()
5451 * .bigPicture(aBigBitmap))
5452 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005453 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005454 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005455 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005456 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005457 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005458 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005459 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005460 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005461
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005462 public BigPictureStyle() {
5463 }
5464
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005465 /**
5466 * @deprecated use {@code BigPictureStyle()}.
5467 */
5468 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005469 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005470 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005471 }
5472
Chris Wrend6297db2012-05-03 16:20:13 -04005473 /**
5474 * Overrides ContentTitle in the big form of the template.
5475 * This defaults to the value passed to setContentTitle().
5476 */
5477 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005478 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005479 return this;
5480 }
5481
5482 /**
5483 * Set the first line of text after the detail section in the big form of the template.
5484 */
5485 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005486 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005487 return this;
5488 }
5489
Chris Wren0bd664d2012-08-01 13:56:56 -04005490 /**
5491 * Provide the bitmap to be used as the payload for the BigPicture notification.
5492 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005493 public BigPictureStyle bigPicture(Bitmap b) {
5494 mPicture = b;
5495 return this;
5496 }
5497
Chris Wren3745a3d2012-05-22 15:11:52 -04005498 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005499 * Override the large icon when the big notification is shown.
5500 */
5501 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005502 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5503 }
5504
5505 /**
5506 * Override the large icon when the big notification is shown.
5507 */
5508 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005509 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005510 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005511 return this;
5512 }
5513
Riley Andrews0394a0c2015-11-03 23:36:52 -08005514 /** @hide */
5515 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5516
Daniel Sandler0ec46202015-06-24 01:27:05 -04005517 /**
5518 * @hide
5519 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005520 @Override
5521 public void purgeResources() {
5522 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005523 if (mPicture != null &&
5524 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005525 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005526 mPicture = mPicture.createAshmemBitmap();
5527 }
5528 if (mBigLargeIcon != null) {
5529 mBigLargeIcon.convertToAshmem();
5530 }
5531 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005532
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005533 /**
5534 * @hide
5535 */
5536 public RemoteViews makeBigContentView() {
5537 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005538 // This covers the following cases:
5539 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005540 // mN.mLargeIcon
5541 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005542 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005543 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005544 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005545 oldLargeIcon = mBuilder.mN.mLargeIcon;
5546 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005547 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5548 // replacement if the other one is null. Because we're restoring these legacy icons
5549 // for old listeners, this is in general non-null.
5550 largeIconLegacy = mBuilder.mN.largeIcon;
5551 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005552 }
5553
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005554 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005555 if (mSummaryTextSet) {
5556 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005557 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005558 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005559 }
Selim Cinek279fa862016-06-14 10:57:25 -07005560 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005561
Christoph Studer5c510ee2014-12-15 16:32:27 +01005562 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005563 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005564 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005565 }
5566
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005567 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005568 return contentView;
5569 }
5570
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005571 /**
5572 * @hide
5573 */
5574 public void addExtras(Bundle extras) {
5575 super.addExtras(extras);
5576
5577 if (mBigLargeIconSet) {
5578 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5579 }
5580 extras.putParcelable(EXTRA_PICTURE, mPicture);
5581 }
5582
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005583 /**
5584 * @hide
5585 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005586 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005587 protected void restoreFromExtras(Bundle extras) {
5588 super.restoreFromExtras(extras);
5589
5590 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005591 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005592 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005593 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005594 mPicture = extras.getParcelable(EXTRA_PICTURE);
5595 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005596
5597 /**
5598 * @hide
5599 */
5600 @Override
5601 public boolean hasSummaryInHeader() {
5602 return false;
5603 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005604 }
5605
5606 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005607 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005608 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005609 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005610 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005611 * Notification notif = new Notification.Builder(mContext)
5612 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5613 * .setContentText(subject)
5614 * .setSmallIcon(R.drawable.new_mail)
5615 * .setLargeIcon(aBitmap)
5616 * .setStyle(new Notification.BigTextStyle()
5617 * .bigText(aVeryLongString))
5618 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005619 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005620 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005621 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005622 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005623 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005624
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005625 private CharSequence mBigText;
5626
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005627 public BigTextStyle() {
5628 }
5629
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005630 /**
5631 * @deprecated use {@code BigTextStyle()}.
5632 */
5633 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005634 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005635 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005636 }
5637
Chris Wrend6297db2012-05-03 16:20:13 -04005638 /**
5639 * Overrides ContentTitle in the big form of the template.
5640 * This defaults to the value passed to setContentTitle().
5641 */
5642 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005643 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005644 return this;
5645 }
5646
5647 /**
5648 * Set the first line of text after the detail section in the big form of the template.
5649 */
5650 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005651 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005652 return this;
5653 }
5654
Chris Wren0bd664d2012-08-01 13:56:56 -04005655 /**
5656 * Provide the longer text to be displayed in the big form of the
5657 * template in place of the content text.
5658 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005659 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005660 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005661 return this;
5662 }
5663
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005664 /**
5665 * @hide
5666 */
5667 public void addExtras(Bundle extras) {
5668 super.addExtras(extras);
5669
Christoph Studer4600f9b2014-07-22 22:44:43 +02005670 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5671 }
5672
5673 /**
5674 * @hide
5675 */
5676 @Override
5677 protected void restoreFromExtras(Bundle extras) {
5678 super.restoreFromExtras(extras);
5679
5680 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005681 }
5682
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005683 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005684 * @param increasedHeight true if this layout be created with an increased height.
5685 *
5686 * @hide
5687 */
5688 @Override
5689 public RemoteViews makeContentView(boolean increasedHeight) {
5690 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005691 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005692 mBuilder.mActions = new ArrayList<>();
5693 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005694 mBuilder.mActions = mBuilder.mOriginalActions;
5695 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005696 return remoteViews;
5697 }
5698 return super.makeContentView(increasedHeight);
5699 }
5700
5701 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005702 * @hide
5703 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005704 @Override
5705 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5706 if (increasedHeight && mBuilder.mActions.size() > 0) {
5707 return makeBigContentView();
5708 }
5709 return super.makeHeadsUpContentView(increasedHeight);
5710 }
5711
5712 /**
5713 * @hide
5714 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005715 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005716
5717 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005718 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005719 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005720
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005721 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005722
Selim Cinek75998782016-04-26 10:39:17 -07005723 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005724
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005725 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005726 if (TextUtils.isEmpty(bigTextText)) {
5727 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5728 // experience
5729 bigTextText = mBuilder.processLegacyText(text);
5730 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005731 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005732
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005733 return contentView;
5734 }
5735
Adrian Roosb1f427c2016-05-26 12:27:15 -07005736 static void applyBigTextContentView(Builder builder,
5737 RemoteViews contentView, CharSequence bigTextText) {
5738 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005739 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005740 contentView.setViewVisibility(R.id.big_text,
5741 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005742 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005743 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005744 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005745
5746 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005747 * Helper class for generating large-format notifications that include multiple back-and-forth
5748 * messages of varying types between any number of people.
5749 *
5750 * <br>
5751 * If the platform does not provide large-format notifications, this method has no effect. The
5752 * user will always see the normal notification view.
5753 * <br>
5754 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5755 * so:
5756 * <pre class="prettyprint">
5757 *
5758 * Notification noti = new Notification.Builder()
5759 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5760 * .setContentText(subject)
5761 * .setSmallIcon(R.drawable.new_message)
5762 * .setLargeIcon(aBitmap)
5763 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5764 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5765 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5766 * .build();
5767 * </pre>
5768 */
5769 public static class MessagingStyle extends Style {
5770
5771 /**
5772 * The maximum number of messages that will be retained in the Notification itself (the
5773 * number displayed is up to the platform).
5774 */
5775 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5776
5777 CharSequence mUserDisplayName;
5778 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005779 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005780 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005781
5782 MessagingStyle() {
5783 }
5784
5785 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005786 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5787 * user before the posting app reposts the notification with those messages after they've
5788 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005789 * {@link #addMessage(Notification.MessagingStyle.Message)}
5790 */
Alex Hillsfd590442016-10-07 09:52:44 -04005791 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005792 mUserDisplayName = userDisplayName;
5793 }
5794
5795 /**
5796 * Returns the name to be displayed for any replies sent by the user
5797 */
5798 public CharSequence getUserDisplayName() {
5799 return mUserDisplayName;
5800 }
5801
5802 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005803 * Sets the title to be displayed on this conversation. This should only be used for
5804 * group messaging and left unset for one-on-one conversations.
5805 * @param conversationTitle
5806 * @return this object for method chaining.
5807 */
5808 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5809 mConversationTitle = conversationTitle;
5810 return this;
5811 }
5812
5813 /**
5814 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5815 * should be for one-on-one conversations
5816 */
5817 public CharSequence getConversationTitle() {
5818 return mConversationTitle;
5819 }
5820
5821 /**
5822 * Adds a message for display by this notification. Convenience call for a simple
5823 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5824 * @param text A {@link CharSequence} to be displayed as the message content
5825 * @param timestamp Time at which the message arrived
5826 * @param sender A {@link CharSequence} to be used for displaying the name of the
5827 * sender. Should be <code>null</code> for messages by the current user, in which case
5828 * the platform will insert {@link #getUserDisplayName()}.
5829 * Should be unique amongst all individuals in the conversation, and should be
5830 * consistent during re-posts of the notification.
5831 *
5832 * @see Message#Message(CharSequence, long, CharSequence)
5833 *
5834 * @return this object for method chaining
5835 */
5836 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005837 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005838 }
5839
5840 /**
5841 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005842 *
5843 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5844 * the newest last.
5845 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005846 * @param message The {@link Message} to be displayed
5847 * @return this object for method chaining
5848 */
5849 public MessagingStyle addMessage(Message message) {
5850 mMessages.add(message);
5851 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5852 mMessages.remove(0);
5853 }
5854 return this;
5855 }
5856
5857 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005858 * Adds a {@link Message} for historic context in this notification.
5859 *
5860 * <p>Messages should be added as historic if they are not the main subject of the
5861 * notification but may give context to a conversation. The system may choose to present
5862 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5863 *
5864 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5865 * the newest last.
5866 *
5867 * @param message The historic {@link Message} to be added
5868 * @return this object for method chaining
5869 */
5870 public MessagingStyle addHistoricMessage(Message message) {
5871 mHistoricMessages.add(message);
5872 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5873 mHistoricMessages.remove(0);
5874 }
5875 return this;
5876 }
5877
5878 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005879 * Gets the list of {@code Message} objects that represent the notification
5880 */
5881 public List<Message> getMessages() {
5882 return mMessages;
5883 }
5884
5885 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005886 * Gets the list of historic {@code Message}s in the notification.
5887 */
5888 public List<Message> getHistoricMessages() {
5889 return mHistoricMessages;
5890 }
5891
5892 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005893 * @hide
5894 */
5895 @Override
5896 public void addExtras(Bundle extras) {
5897 super.addExtras(extras);
5898 if (mUserDisplayName != null) {
5899 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5900 }
5901 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005902 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005903 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005904 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5905 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005906 }
Adrian Roos437cd562017-01-18 15:47:03 -08005907 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5908 Message.getBundleArrayForMessages(mHistoricMessages));
5909 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005910
5911 fixTitleAndTextExtras(extras);
5912 }
5913
5914 private void fixTitleAndTextExtras(Bundle extras) {
5915 Message m = findLatestIncomingMessage();
5916 CharSequence text = (m == null) ? null : m.mText;
5917 CharSequence sender = m == null ? null
5918 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5919 CharSequence title;
5920 if (!TextUtils.isEmpty(mConversationTitle)) {
5921 if (!TextUtils.isEmpty(sender)) {
5922 BidiFormatter bidi = BidiFormatter.getInstance();
5923 title = mBuilder.mContext.getString(
5924 com.android.internal.R.string.notification_messaging_title_template,
5925 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5926 } else {
5927 title = mConversationTitle;
5928 }
5929 } else {
5930 title = sender;
5931 }
5932
5933 if (title != null) {
5934 extras.putCharSequence(EXTRA_TITLE, title);
5935 }
5936 if (text != null) {
5937 extras.putCharSequence(EXTRA_TEXT, text);
5938 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005939 }
5940
5941 /**
5942 * @hide
5943 */
5944 @Override
5945 protected void restoreFromExtras(Bundle extras) {
5946 super.restoreFromExtras(extras);
5947
5948 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005949 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005950 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5951 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005952 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5953 if (messages != null && messages instanceof Parcelable[]) {
5954 mMessages = Message.getMessagesFromBundleArray(messages);
5955 }
5956 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5957 if (histMessages != null && histMessages instanceof Parcelable[]) {
5958 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005959 }
5960 }
5961
5962 /**
5963 * @hide
5964 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005965 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005966 public RemoteViews makeContentView(boolean increasedHeight) {
5967 if (!increasedHeight) {
5968 Message m = findLatestIncomingMessage();
5969 CharSequence title = mConversationTitle != null
5970 ? mConversationTitle
5971 : (m == null) ? null : m.mSender;
5972 CharSequence text = (m == null)
5973 ? null
5974 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005975
Selim Cinek7d1009b2017-01-25 15:28:28 -08005976 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5977 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5978 } else {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005979 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005980 mBuilder.mActions = new ArrayList<>();
5981 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005982 mBuilder.mActions = mBuilder.mOriginalActions;
5983 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005984 return remoteViews;
5985 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005986 }
5987
5988 private Message findLatestIncomingMessage() {
5989 for (int i = mMessages.size() - 1; i >= 0; i--) {
5990 Message m = mMessages.get(i);
5991 // Incoming messages have a non-empty sender.
5992 if (!TextUtils.isEmpty(m.mSender)) {
5993 return m;
5994 }
5995 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005996 if (!mMessages.isEmpty()) {
5997 // No incoming messages, fall back to outgoing message
5998 return mMessages.get(mMessages.size() - 1);
5999 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006000 return null;
6001 }
6002
6003 /**
6004 * @hide
6005 */
6006 @Override
6007 public RemoteViews makeBigContentView() {
6008 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
6009 ? super.mBigContentTitle
6010 : mConversationTitle;
6011 boolean hasTitle = !TextUtils.isEmpty(title);
6012
Adrian Roosfeafa052016-06-01 17:09:45 -07006013 if (mMessages.size() == 1) {
6014 // Special case for a single message: Use the big text style
6015 // so the collapsed and expanded versions match nicely.
6016 CharSequence bigTitle;
6017 CharSequence text;
6018 if (hasTitle) {
6019 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006020 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07006021 } else {
6022 bigTitle = mMessages.get(0).mSender;
6023 text = mMessages.get(0).mText;
6024 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006025 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
6026 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006027 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07006028 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
6029 return contentView;
6030 }
6031
Adrian Roos48d746a2016-04-12 14:57:28 -07006032 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006033 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006034 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006035
6036 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
6037 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
6038
6039 // Make sure all rows are gone in case we reuse a view.
6040 for (int rowId : rowIds) {
6041 contentView.setViewVisibility(rowId, View.GONE);
6042 }
6043
6044 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006045 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
6046 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006047 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07006048 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006049
Adrian Roosfeafa052016-06-01 17:09:45 -07006050 int contractedChildId = View.NO_ID;
6051 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08006052 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
6053 - (rowIds.length - mMessages.size()));
6054 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
6055 Message m = mHistoricMessages.get(firstHistoricMessage + i);
6056 int rowId = rowIds[i];
6057
Selim Cinek7b9605b2017-01-19 17:36:00 -08006058 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08006059
6060 if (contractedMessage == m) {
6061 contractedChildId = rowId;
6062 }
6063
6064 i++;
6065 }
6066
Adrian Roosc1a80b02016-04-05 14:54:55 -07006067 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
6068 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
6069 Message m = mMessages.get(firstMessage + i);
6070 int rowId = rowIds[i];
6071
6072 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006073 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
6074 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006075
Adrian Roosfeafa052016-06-01 17:09:45 -07006076 if (contractedMessage == m) {
6077 contractedChildId = rowId;
6078 }
6079
Adrian Roosc1a80b02016-04-05 14:54:55 -07006080 i++;
6081 }
Adrian Roos437cd562017-01-18 15:47:03 -08006082 // Clear the remaining views for reapply. Ensures that historic message views can
6083 // reliably be identified as being GONE and having non-null text.
6084 while (i < rowIds.length) {
6085 int rowId = rowIds[i];
6086 contentView.setTextViewText(rowId, null);
6087 i++;
6088 }
6089
Adrian Roosfeafa052016-06-01 17:09:45 -07006090 // Record this here to allow transformation between the contracted and expanded views.
6091 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
6092 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04006093 return contentView;
6094 }
6095
Selim Cinek7b9605b2017-01-19 17:36:00 -08006096 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006097 BidiFormatter bidi = BidiFormatter.getInstance();
6098 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08006099 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07006100 if (TextUtils.isEmpty(m.mSender)) {
6101 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
6102 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006103 makeFontColorSpan(colorize
6104 ? builder.getPrimaryTextColor()
6105 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006106 0 /* flags */);
6107 } else {
6108 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006109 makeFontColorSpan(colorize
6110 ? builder.getPrimaryTextColor()
6111 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006112 0 /* flags */);
6113 }
6114 CharSequence text = m.mText == null ? "" : m.mText;
6115 sb.append(" ").append(bidi.unicodeWrap(text));
6116 return sb;
6117 }
6118
Adrian Roosdedd1df2016-04-26 16:38:47 -07006119 /**
6120 * @hide
6121 */
6122 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006123 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6124 if (increasedHeight) {
6125 return makeBigContentView();
6126 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07006127 Message m = findLatestIncomingMessage();
6128 CharSequence title = mConversationTitle != null
6129 ? mConversationTitle
6130 : (m == null) ? null : m.mSender;
6131 CharSequence text = (m == null)
6132 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08006133 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006134
6135 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006136 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07006137 }
6138
Adrian Roosc1a80b02016-04-05 14:54:55 -07006139 private static TextAppearanceSpan makeFontColorSpan(int color) {
6140 return new TextAppearanceSpan(null, 0, 0,
6141 ColorStateList.valueOf(color), null);
6142 }
6143
Alex Hillsd9b04d92016-04-11 16:38:16 -04006144 public static final class Message {
6145
6146 static final String KEY_TEXT = "text";
6147 static final String KEY_TIMESTAMP = "time";
6148 static final String KEY_SENDER = "sender";
6149 static final String KEY_DATA_MIME_TYPE = "type";
6150 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006151 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006152
6153 private final CharSequence mText;
6154 private final long mTimestamp;
6155 private final CharSequence mSender;
6156
Shane Brennan5a871862017-03-11 13:14:17 -08006157 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006158 private String mDataMimeType;
6159 private Uri mDataUri;
6160
6161 /**
6162 * Constructor
6163 * @param text A {@link CharSequence} to be displayed as the message content
6164 * @param timestamp Time at which the message arrived
6165 * @param sender A {@link CharSequence} to be used for displaying the name of the
6166 * sender. Should be <code>null</code> for messages by the current user, in which case
6167 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6168 * Should be unique amongst all individuals in the conversation, and should be
6169 * consistent during re-posts of the notification.
6170 */
6171 public Message(CharSequence text, long timestamp, CharSequence sender){
6172 mText = text;
6173 mTimestamp = timestamp;
6174 mSender = sender;
6175 }
6176
6177 /**
6178 * Sets a binary blob of data and an associated MIME type for a message. In the case
6179 * where the platform doesn't support the MIME type, the original text provided in the
6180 * constructor will be used.
6181 * @param dataMimeType The MIME type of the content. See
6182 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6183 * types on Android and Android Wear.
6184 * @param dataUri The uri containing the content whose type is given by the MIME type.
6185 * <p class="note">
6186 * <ol>
6187 * <li>Notification Listeners including the System UI need permission to access the
6188 * data the Uri points to. The recommended ways to do this are:</li>
6189 * <li>Store the data in your own ContentProvider, making sure that other apps have
6190 * the correct permission to access your provider. The preferred mechanism for
6191 * providing access is to use per-URI permissions which are temporary and only
6192 * grant access to the receiving application. An easy way to create a
6193 * ContentProvider like this is to use the FileProvider helper class.</li>
6194 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6195 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6196 * also store non-media types (see MediaStore.Files for more info). Files can be
6197 * inserted into the MediaStore using scanFile() after which a content:// style
6198 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6199 * Note that once added to the system MediaStore the content is accessible to any
6200 * app on the device.</li>
6201 * </ol>
6202 * @return this object for method chaining
6203 */
6204 public Message setData(String dataMimeType, Uri dataUri) {
6205 mDataMimeType = dataMimeType;
6206 mDataUri = dataUri;
6207 return this;
6208 }
6209
Alex Hillsfc737de2016-03-23 17:33:02 -04006210 /**
6211 * Get the text to be used for this message, or the fallback text if a type and content
6212 * Uri have been set
6213 */
6214 public CharSequence getText() {
6215 return mText;
6216 }
6217
6218 /**
6219 * Get the time at which this message arrived
6220 */
6221 public long getTimestamp() {
6222 return mTimestamp;
6223 }
6224
6225 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006226 * Get the extras Bundle for this message.
6227 */
6228 public Bundle getExtras() {
6229 return mExtras;
6230 }
6231
6232 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006233 * Get the text used to display the contact's name in the messaging experience
6234 */
6235 public CharSequence getSender() {
6236 return mSender;
6237 }
6238
6239 /**
6240 * Get the MIME type of the data pointed to by the Uri
6241 */
6242 public String getDataMimeType() {
6243 return mDataMimeType;
6244 }
6245
6246 /**
6247 * Get the the Uri pointing to the content of the message. Can be null, in which case
6248 * {@see #getText()} is used.
6249 */
6250 public Uri getDataUri() {
6251 return mDataUri;
6252 }
6253
Alex Hillsd9b04d92016-04-11 16:38:16 -04006254 private Bundle toBundle() {
6255 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006256 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006257 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006258 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006259 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006260 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006261 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006262 }
6263 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006264 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006265 }
6266 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006267 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006268 }
Shane Brennan5a871862017-03-11 13:14:17 -08006269 if (mExtras != null) {
6270 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6271 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006272 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006273 }
6274
Alex Hillsd9b04d92016-04-11 16:38:16 -04006275 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6276 Bundle[] bundles = new Bundle[messages.size()];
6277 final int N = messages.size();
6278 for (int i = 0; i < N; i++) {
6279 bundles[i] = messages.get(i).toBundle();
6280 }
6281 return bundles;
6282 }
6283
Adrian Roosdedd1df2016-04-26 16:38:47 -07006284 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006285 List<Message> messages = new ArrayList<>(bundles.length);
6286 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006287 if (bundles[i] instanceof Bundle) {
6288 Message message = getMessageFromBundle((Bundle)bundles[i]);
6289 if (message != null) {
6290 messages.add(message);
6291 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006292 }
6293 }
6294 return messages;
6295 }
6296
6297 static Message getMessageFromBundle(Bundle bundle) {
6298 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006299 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006300 return null;
6301 } else {
6302 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6303 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6304 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6305 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006306 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6307 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006308 }
Shane Brennan5a871862017-03-11 13:14:17 -08006309 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6310 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6311 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006312 return message;
6313 }
6314 } catch (ClassCastException e) {
6315 return null;
6316 }
6317 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006318 }
6319 }
6320
6321 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006322 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006323 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006324 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006325 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006326 * Notification notif = new Notification.Builder(mContext)
6327 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6328 * .setContentText(subject)
6329 * .setSmallIcon(R.drawable.new_mail)
6330 * .setLargeIcon(aBitmap)
6331 * .setStyle(new Notification.InboxStyle()
6332 * .addLine(str1)
6333 * .addLine(str2)
6334 * .setContentTitle(&quot;&quot;)
6335 * .setSummaryText(&quot;+3 more&quot;))
6336 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006337 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006338 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006339 * @see Notification#bigContentView
6340 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006341 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006342 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6343
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006344 public InboxStyle() {
6345 }
6346
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006347 /**
6348 * @deprecated use {@code InboxStyle()}.
6349 */
6350 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006351 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006352 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006353 }
6354
Chris Wrend6297db2012-05-03 16:20:13 -04006355 /**
6356 * Overrides ContentTitle in the big form of the template.
6357 * This defaults to the value passed to setContentTitle().
6358 */
6359 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006360 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006361 return this;
6362 }
6363
6364 /**
6365 * Set the first line of text after the detail section in the big form of the template.
6366 */
6367 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006368 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006369 return this;
6370 }
6371
Chris Wren0bd664d2012-08-01 13:56:56 -04006372 /**
6373 * Append a line to the digest section of the Inbox notification.
6374 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006375 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006376 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006377 return this;
6378 }
6379
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006380 /**
6381 * @hide
6382 */
6383 public void addExtras(Bundle extras) {
6384 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006385
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006386 CharSequence[] a = new CharSequence[mTexts.size()];
6387 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6388 }
6389
Christoph Studer4600f9b2014-07-22 22:44:43 +02006390 /**
6391 * @hide
6392 */
6393 @Override
6394 protected void restoreFromExtras(Bundle extras) {
6395 super.restoreFromExtras(extras);
6396
6397 mTexts.clear();
6398 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6399 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6400 }
6401 }
6402
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006403 /**
6404 * @hide
6405 */
6406 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006407 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006408 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006409 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6410 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006411
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006412 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006413
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006414 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006415
Chris Wrend6297db2012-05-03 16:20:13 -04006416 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 -04006417 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006418
Chris Wren4ed80d52012-05-17 09:30:03 -04006419 // Make sure all rows are gone in case we reuse a view.
6420 for (int rowId : rowIds) {
6421 contentView.setViewVisibility(rowId, View.GONE);
6422 }
6423
Daniel Sandler879c5e02012-04-17 16:46:51 -04006424 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006425 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6426 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006427 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006428 int onlyViewId = 0;
6429 int maxRows = rowIds.length;
6430 if (mBuilder.mActions.size() > 0) {
6431 maxRows--;
6432 }
6433 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006434 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006435 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006436 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006437 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006438 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006439 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006440 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006441 if (first) {
6442 onlyViewId = rowIds[i];
6443 } else {
6444 onlyViewId = 0;
6445 }
Selim Cinek247fa012016-02-18 09:50:48 -08006446 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006447 }
6448 i++;
6449 }
Selim Cinek07c80172016-04-21 16:40:47 -07006450 if (onlyViewId != 0) {
6451 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6452 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6453 R.dimen.notification_text_margin_top);
6454 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6455 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006456
Daniel Sandler879c5e02012-04-17 16:46:51 -04006457 return contentView;
6458 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006459
Selim Cinek247fa012016-02-18 09:50:48 -08006460 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006461 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006462 if (first) {
6463 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6464 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6465 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006466 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006467 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006468 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006469 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006470 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006471 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006472 }
Dan Sandler842dd772014-05-15 09:36:47 -04006473
6474 /**
6475 * Notification style for media playback notifications.
6476 *
6477 * In the expanded form, {@link Notification#bigContentView}, up to 5
6478 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006479 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006480 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6481 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6482 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006483 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006484 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6485 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006486 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006487 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006488 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006489 * Notifications created with MediaStyle will have their category set to
6490 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6491 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006492 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006493 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6494 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006495 * the System UI can identify this as a notification representing an active media session
6496 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6497 *
Selim Cinek99104832017-01-25 14:47:33 -08006498 * <p>
6499 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6500 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6501 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6502 * <p>
6503 *
Dan Sandler842dd772014-05-15 09:36:47 -04006504 * To use this style with your Notification, feed it to
6505 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6506 * <pre class="prettyprint">
6507 * Notification noti = new Notification.Builder()
6508 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006509 * .setContentTitle(&quot;Track title&quot;)
6510 * .setContentText(&quot;Artist - Album&quot;)
6511 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006512 * .setStyle(<b>new Notification.MediaStyle()</b>
6513 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006514 * .build();
6515 * </pre>
6516 *
6517 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006518 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006519 */
6520 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006521 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006522 static final int MAX_MEDIA_BUTTONS = 5;
6523
6524 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006525 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006526
6527 public MediaStyle() {
6528 }
6529
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006530 /**
6531 * @deprecated use {@code MediaStyle()}.
6532 */
6533 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006534 public MediaStyle(Builder builder) {
6535 setBuilder(builder);
6536 }
6537
6538 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006539 * 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 -04006540 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006541 *
6542 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006543 */
6544 public MediaStyle setShowActionsInCompactView(int...actions) {
6545 mActionsToShowInCompact = actions;
6546 return this;
6547 }
6548
6549 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006550 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6551 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006552 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006553 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006554 mToken = token;
6555 return this;
6556 }
6557
Christoph Studer4600f9b2014-07-22 22:44:43 +02006558 /**
6559 * @hide
6560 */
Dan Sandler842dd772014-05-15 09:36:47 -04006561 @Override
6562 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006563 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006564 if (wip.category == null) {
6565 wip.category = Notification.CATEGORY_TRANSPORT;
6566 }
Dan Sandler842dd772014-05-15 09:36:47 -04006567 return wip;
6568 }
6569
Christoph Studer4600f9b2014-07-22 22:44:43 +02006570 /**
6571 * @hide
6572 */
6573 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006574 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006575 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006576 }
6577
6578 /**
6579 * @hide
6580 */
6581 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006582 public RemoteViews makeBigContentView() {
6583 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006584 }
6585
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006586 /**
6587 * @hide
6588 */
6589 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006590 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006591 RemoteViews expanded = makeMediaBigContentView();
6592 return expanded != null ? expanded : makeMediaContentView();
6593 }
6594
Dan Sandler842dd772014-05-15 09:36:47 -04006595 /** @hide */
6596 @Override
6597 public void addExtras(Bundle extras) {
6598 super.addExtras(extras);
6599
6600 if (mToken != null) {
6601 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6602 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006603 if (mActionsToShowInCompact != null) {
6604 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6605 }
Dan Sandler842dd772014-05-15 09:36:47 -04006606 }
6607
Christoph Studer4600f9b2014-07-22 22:44:43 +02006608 /**
6609 * @hide
6610 */
6611 @Override
6612 protected void restoreFromExtras(Bundle extras) {
6613 super.restoreFromExtras(extras);
6614
6615 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6616 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6617 }
6618 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6619 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6620 }
6621 }
6622
Selim Cinek5bf069a2015-11-10 19:14:27 -05006623 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006624 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006625 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006626 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006627 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006628 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6629 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006630 if (!tombstone) {
6631 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6632 }
6633 button.setContentDescription(R.id.action0, action.title);
6634 return button;
6635 }
6636
6637 private RemoteViews makeMediaContentView() {
6638 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006639 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006640
6641 final int numActions = mBuilder.mActions.size();
6642 final int N = mActionsToShowInCompact == null
6643 ? 0
6644 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6645 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006646 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006647 for (int i = 0; i < N; i++) {
6648 if (i >= numActions) {
6649 throw new IllegalArgumentException(String.format(
6650 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6651 i, numActions - 1));
6652 }
6653
6654 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006655 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006656 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006657 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006658 }
6659 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006660 handleImage(view);
6661 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006662 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006663 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006664 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006665 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006666 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006667 return view;
6668 }
6669
Selim Cinek99104832017-01-25 14:47:33 -08006670 private int getPrimaryHighlightColor() {
6671 return mBuilder.getPrimaryHighlightColor();
6672 }
6673
Dan Sandler842dd772014-05-15 09:36:47 -04006674 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006675 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006676 // Dont add an expanded view if there is no more content to be revealed
6677 int actionsInCompact = mActionsToShowInCompact == null
6678 ? 0
6679 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006680 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006681 return null;
6682 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006683 RemoteViews big = mBuilder.applyStandardTemplate(
6684 R.layout.notification_template_material_big_media,
6685 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006686
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006687 if (actionCount > 0) {
6688 big.removeAllViews(com.android.internal.R.id.media_actions);
6689 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006690 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006691 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006692 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006693 }
6694 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006695 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006696 return big;
6697 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006698
Selim Cinek5bf069a2015-11-10 19:14:27 -05006699 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006700 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006701 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6702 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006703 }
6704 }
6705
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006706 /**
6707 * @hide
6708 */
6709 @Override
6710 protected boolean hasProgress() {
6711 return false;
6712 }
Dan Sandler842dd772014-05-15 09:36:47 -04006713 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006714
Selim Cinek593610c2016-02-16 18:42:57 -08006715 /**
6716 * Notification style for custom views that are decorated by the system
6717 *
6718 * <p>Instead of providing a notification that is completely custom, a developer can set this
6719 * style and still obtain system decorations like the notification header with the expand
6720 * affordance and actions.
6721 *
6722 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6723 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6724 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6725 * corresponding custom views to display.
6726 *
6727 * To use this style with your Notification, feed it to
6728 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6729 * <pre class="prettyprint">
6730 * Notification noti = new Notification.Builder()
6731 * .setSmallIcon(R.drawable.ic_stat_player)
6732 * .setLargeIcon(albumArtBitmap))
6733 * .setCustomContentView(contentView);
6734 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6735 * .build();
6736 * </pre>
6737 */
6738 public static class DecoratedCustomViewStyle extends Style {
6739
6740 public DecoratedCustomViewStyle() {
6741 }
6742
Selim Cinek593610c2016-02-16 18:42:57 -08006743 /**
6744 * @hide
6745 */
6746 public boolean displayCustomViewInline() {
6747 return true;
6748 }
6749
6750 /**
6751 * @hide
6752 */
6753 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006754 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006755 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6756 }
6757
6758 /**
6759 * @hide
6760 */
6761 @Override
6762 public RemoteViews makeBigContentView() {
6763 return makeDecoratedBigContentView();
6764 }
6765
6766 /**
6767 * @hide
6768 */
6769 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006770 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006771 return makeDecoratedHeadsUpContentView();
6772 }
6773
Selim Cinek593610c2016-02-16 18:42:57 -08006774 private RemoteViews makeDecoratedHeadsUpContentView() {
6775 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6776 ? mBuilder.mN.contentView
6777 : mBuilder.mN.headsUpContentView;
6778 if (mBuilder.mActions.size() == 0) {
6779 return makeStandardTemplateWithCustomContent(headsUpContentView);
6780 }
6781 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6782 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006783 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006784 return remoteViews;
6785 }
6786
Selim Cinek593610c2016-02-16 18:42:57 -08006787 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6788 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6789 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006790 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006791 return remoteViews;
6792 }
6793
Selim Cinek593610c2016-02-16 18:42:57 -08006794 private RemoteViews makeDecoratedBigContentView() {
6795 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6796 ? mBuilder.mN.contentView
6797 : mBuilder.mN.bigContentView;
6798 if (mBuilder.mActions.size() == 0) {
6799 return makeStandardTemplateWithCustomContent(bigContentView);
6800 }
6801 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6802 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006803 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006804 return remoteViews;
6805 }
Selim Cinek247fa012016-02-18 09:50:48 -08006806
6807 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6808 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006809 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006810 // Need to clone customContent before adding, because otherwise it can no longer be
6811 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006812 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07006813 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
6814 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006815 }
Selim Cinek247fa012016-02-18 09:50:48 -08006816 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006817 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006818 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006819 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006820 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006821 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006822 }
Selim Cinek593610c2016-02-16 18:42:57 -08006823 }
6824
Selim Cinek03eb3b72016-02-18 10:39:45 -08006825 /**
6826 * Notification style for media custom views that are decorated by the system
6827 *
6828 * <p>Instead of providing a media notification that is completely custom, a developer can set
6829 * this style and still obtain system decorations like the notification header with the expand
6830 * affordance and actions.
6831 *
6832 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6833 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6834 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6835 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006836 * <p>
6837 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6838 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6839 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006840 * To use this style with your Notification, feed it to
6841 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6842 * <pre class="prettyprint">
6843 * Notification noti = new Notification.Builder()
6844 * .setSmallIcon(R.drawable.ic_stat_player)
6845 * .setLargeIcon(albumArtBitmap))
6846 * .setCustomContentView(contentView);
6847 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6848 * .setMediaSession(mySession))
6849 * .build();
6850 * </pre>
6851 *
6852 * @see android.app.Notification.DecoratedCustomViewStyle
6853 * @see android.app.Notification.MediaStyle
6854 */
6855 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6856
6857 public DecoratedMediaCustomViewStyle() {
6858 }
6859
Selim Cinek03eb3b72016-02-18 10:39:45 -08006860 /**
6861 * @hide
6862 */
6863 public boolean displayCustomViewInline() {
6864 return true;
6865 }
6866
6867 /**
6868 * @hide
6869 */
6870 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006871 public RemoteViews makeContentView(boolean increasedHeight) {
6872 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006873 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6874 mBuilder.mN.contentView);
6875 }
6876
6877 /**
6878 * @hide
6879 */
6880 @Override
6881 public RemoteViews makeBigContentView() {
6882 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6883 ? mBuilder.mN.bigContentView
6884 : mBuilder.mN.contentView;
6885 return makeBigContentViewWithCustomContent(customRemoteView);
6886 }
6887
6888 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6889 RemoteViews remoteViews = super.makeBigContentView();
6890 if (remoteViews != null) {
6891 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6892 customRemoteView);
6893 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006894 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006895 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6896 customRemoteView);
6897 } else {
6898 return null;
6899 }
6900 }
6901
6902 /**
6903 * @hide
6904 */
6905 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006906 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006907 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6908 ? mBuilder.mN.headsUpContentView
6909 : mBuilder.mN.contentView;
6910 return makeBigContentViewWithCustomContent(customRemoteView);
6911 }
6912
6913 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6914 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006915 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006916 // Need to clone customContent before adding, because otherwise it can no longer be
6917 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006918 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006919 remoteViews.removeAllViews(id);
6920 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006921 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006922 return remoteViews;
6923 }
6924 }
6925
Christoph Studer4600f9b2014-07-22 22:44:43 +02006926 // When adding a new Style subclass here, don't forget to update
6927 // Builder.getNotificationStyleClass.
6928
Griff Hazen61a9e862014-05-22 16:05:19 -07006929 /**
6930 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6931 * metadata or change options on a notification builder.
6932 */
6933 public interface Extender {
6934 /**
6935 * Apply this extender to a notification builder.
6936 * @param builder the builder to be modified.
6937 * @return the build object for chaining.
6938 */
6939 public Builder extend(Builder builder);
6940 }
6941
6942 /**
6943 * Helper class to add wearable extensions to notifications.
6944 * <p class="note"> See
6945 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6946 * for Android Wear</a> for more information on how to use this class.
6947 * <p>
6948 * To create a notification with wearable extensions:
6949 * <ol>
6950 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6951 * properties.
6952 * <li>Create a {@link android.app.Notification.WearableExtender}.
6953 * <li>Set wearable-specific properties using the
6954 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6955 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6956 * notification.
6957 * <li>Post the notification to the notification system with the
6958 * {@code NotificationManager.notify(...)} methods.
6959 * </ol>
6960 *
6961 * <pre class="prettyprint">
6962 * Notification notif = new Notification.Builder(mContext)
6963 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6964 * .setContentText(subject)
6965 * .setSmallIcon(R.drawable.new_mail)
6966 * .extend(new Notification.WearableExtender()
6967 * .setContentIcon(R.drawable.new_mail))
6968 * .build();
6969 * NotificationManager notificationManger =
6970 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6971 * notificationManger.notify(0, notif);</pre>
6972 *
6973 * <p>Wearable extensions can be accessed on an existing notification by using the
6974 * {@code WearableExtender(Notification)} constructor,
6975 * and then using the {@code get} methods to access values.
6976 *
6977 * <pre class="prettyprint">
6978 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6979 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006980 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006981 */
6982 public static final class WearableExtender implements Extender {
6983 /**
6984 * Sentinel value for an action index that is unset.
6985 */
6986 public static final int UNSET_ACTION_INDEX = -1;
6987
6988 /**
6989 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6990 * default sizing.
6991 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006992 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006993 * on their content.
6994 */
6995 public static final int SIZE_DEFAULT = 0;
6996
6997 /**
6998 * Size value for use with {@link #setCustomSizePreset} to show this notification
6999 * with an extra small size.
7000 * <p>This value is only applicable for custom display notifications created using
7001 * {@link #setDisplayIntent}.
7002 */
7003 public static final int SIZE_XSMALL = 1;
7004
7005 /**
7006 * Size value for use with {@link #setCustomSizePreset} to show this notification
7007 * with a small size.
7008 * <p>This value is only applicable for custom display notifications created using
7009 * {@link #setDisplayIntent}.
7010 */
7011 public static final int SIZE_SMALL = 2;
7012
7013 /**
7014 * Size value for use with {@link #setCustomSizePreset} to show this notification
7015 * with a medium size.
7016 * <p>This value is only applicable for custom display notifications created using
7017 * {@link #setDisplayIntent}.
7018 */
7019 public static final int SIZE_MEDIUM = 3;
7020
7021 /**
7022 * Size value for use with {@link #setCustomSizePreset} to show this notification
7023 * with a large size.
7024 * <p>This value is only applicable for custom display notifications created using
7025 * {@link #setDisplayIntent}.
7026 */
7027 public static final int SIZE_LARGE = 4;
7028
Griff Hazend5f11f92014-05-27 15:40:09 -07007029 /**
7030 * Size value for use with {@link #setCustomSizePreset} to show this notification
7031 * full screen.
7032 * <p>This value is only applicable for custom display notifications created using
7033 * {@link #setDisplayIntent}.
7034 */
7035 public static final int SIZE_FULL_SCREEN = 5;
7036
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007037 /**
7038 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7039 * short amount of time when this notification is displayed on the screen. This
7040 * is the default value.
7041 */
7042 public static final int SCREEN_TIMEOUT_SHORT = 0;
7043
7044 /**
7045 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7046 * for a longer amount of time when this notification is displayed on the screen.
7047 */
7048 public static final int SCREEN_TIMEOUT_LONG = -1;
7049
Griff Hazen61a9e862014-05-22 16:05:19 -07007050 /** Notification extra which contains wearable extensions */
7051 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7052
Pete Gastaf6781d2014-10-07 15:17:05 -04007053 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007054 private static final String KEY_ACTIONS = "actions";
7055 private static final String KEY_FLAGS = "flags";
7056 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7057 private static final String KEY_PAGES = "pages";
7058 private static final String KEY_BACKGROUND = "background";
7059 private static final String KEY_CONTENT_ICON = "contentIcon";
7060 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7061 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7062 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7063 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7064 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007065 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007066 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007067 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007068
7069 // Flags bitwise-ored to mFlags
7070 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7071 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7072 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7073 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007074 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007075 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007076 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007077
7078 // Default value for flags integer
7079 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7080
7081 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7082 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7083
7084 private ArrayList<Action> mActions = new ArrayList<Action>();
7085 private int mFlags = DEFAULT_FLAGS;
7086 private PendingIntent mDisplayIntent;
7087 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7088 private Bitmap mBackground;
7089 private int mContentIcon;
7090 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7091 private int mContentActionIndex = UNSET_ACTION_INDEX;
7092 private int mCustomSizePreset = SIZE_DEFAULT;
7093 private int mCustomContentHeight;
7094 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007095 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007096 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007097 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007098
7099 /**
7100 * Create a {@link android.app.Notification.WearableExtender} with default
7101 * options.
7102 */
7103 public WearableExtender() {
7104 }
7105
7106 public WearableExtender(Notification notif) {
7107 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7108 if (wearableBundle != null) {
7109 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7110 if (actions != null) {
7111 mActions.addAll(actions);
7112 }
7113
7114 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7115 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7116
7117 Notification[] pages = getNotificationArrayFromBundle(
7118 wearableBundle, KEY_PAGES);
7119 if (pages != null) {
7120 Collections.addAll(mPages, pages);
7121 }
7122
7123 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7124 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7125 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7126 DEFAULT_CONTENT_ICON_GRAVITY);
7127 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7128 UNSET_ACTION_INDEX);
7129 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7130 SIZE_DEFAULT);
7131 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7132 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007133 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007134 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007135 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007136 }
7137 }
7138
7139 /**
7140 * Apply wearable extensions to a notification that is being built. This is typically
7141 * called by the {@link android.app.Notification.Builder#extend} method of
7142 * {@link android.app.Notification.Builder}.
7143 */
7144 @Override
7145 public Notification.Builder extend(Notification.Builder builder) {
7146 Bundle wearableBundle = new Bundle();
7147
7148 if (!mActions.isEmpty()) {
7149 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7150 }
7151 if (mFlags != DEFAULT_FLAGS) {
7152 wearableBundle.putInt(KEY_FLAGS, mFlags);
7153 }
7154 if (mDisplayIntent != null) {
7155 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7156 }
7157 if (!mPages.isEmpty()) {
7158 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7159 new Notification[mPages.size()]));
7160 }
7161 if (mBackground != null) {
7162 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7163 }
7164 if (mContentIcon != 0) {
7165 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7166 }
7167 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7168 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7169 }
7170 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7171 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7172 mContentActionIndex);
7173 }
7174 if (mCustomSizePreset != SIZE_DEFAULT) {
7175 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7176 }
7177 if (mCustomContentHeight != 0) {
7178 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7179 }
7180 if (mGravity != DEFAULT_GRAVITY) {
7181 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7182 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007183 if (mHintScreenTimeout != 0) {
7184 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7185 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007186 if (mDismissalId != null) {
7187 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7188 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007189 if (mBridgeTag != null) {
7190 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7191 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007192
7193 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7194 return builder;
7195 }
7196
7197 @Override
7198 public WearableExtender clone() {
7199 WearableExtender that = new WearableExtender();
7200 that.mActions = new ArrayList<Action>(this.mActions);
7201 that.mFlags = this.mFlags;
7202 that.mDisplayIntent = this.mDisplayIntent;
7203 that.mPages = new ArrayList<Notification>(this.mPages);
7204 that.mBackground = this.mBackground;
7205 that.mContentIcon = this.mContentIcon;
7206 that.mContentIconGravity = this.mContentIconGravity;
7207 that.mContentActionIndex = this.mContentActionIndex;
7208 that.mCustomSizePreset = this.mCustomSizePreset;
7209 that.mCustomContentHeight = this.mCustomContentHeight;
7210 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007211 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007212 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007213 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007214 return that;
7215 }
7216
7217 /**
7218 * Add a wearable action to this notification.
7219 *
7220 * <p>When wearable actions are added using this method, the set of actions that
7221 * show on a wearable device splits from devices that only show actions added
7222 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7223 * of which actions display on different devices.
7224 *
7225 * @param action the action to add to this notification
7226 * @return this object for method chaining
7227 * @see android.app.Notification.Action
7228 */
7229 public WearableExtender addAction(Action action) {
7230 mActions.add(action);
7231 return this;
7232 }
7233
7234 /**
7235 * Adds wearable actions to this notification.
7236 *
7237 * <p>When wearable actions are added using this method, the set of actions that
7238 * show on a wearable device splits from devices that only show actions added
7239 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7240 * of which actions display on different devices.
7241 *
7242 * @param actions the actions to add to this notification
7243 * @return this object for method chaining
7244 * @see android.app.Notification.Action
7245 */
7246 public WearableExtender addActions(List<Action> actions) {
7247 mActions.addAll(actions);
7248 return this;
7249 }
7250
7251 /**
7252 * Clear all wearable actions present on this builder.
7253 * @return this object for method chaining.
7254 * @see #addAction
7255 */
7256 public WearableExtender clearActions() {
7257 mActions.clear();
7258 return this;
7259 }
7260
7261 /**
7262 * Get the wearable actions present on this notification.
7263 */
7264 public List<Action> getActions() {
7265 return mActions;
7266 }
7267
7268 /**
7269 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007270 * this notification. The {@link PendingIntent} provided should be for an activity.
7271 *
7272 * <pre class="prettyprint">
7273 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7274 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7275 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7276 * Notification notif = new Notification.Builder(context)
7277 * .extend(new Notification.WearableExtender()
7278 * .setDisplayIntent(displayPendingIntent)
7279 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7280 * .build();</pre>
7281 *
7282 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007283 * should have an empty task affinity. It is also recommended to use the device
7284 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007285 *
7286 * <p>Example AndroidManifest.xml entry:
7287 * <pre class="prettyprint">
7288 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7289 * android:exported=&quot;true&quot;
7290 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007291 * android:taskAffinity=&quot;&quot;
7292 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007293 *
7294 * @param intent the {@link PendingIntent} for an activity
7295 * @return this object for method chaining
7296 * @see android.app.Notification.WearableExtender#getDisplayIntent
7297 */
7298 public WearableExtender setDisplayIntent(PendingIntent intent) {
7299 mDisplayIntent = intent;
7300 return this;
7301 }
7302
7303 /**
7304 * Get the intent to launch inside of an activity view when displaying this
7305 * notification. This {@code PendingIntent} should be for an activity.
7306 */
7307 public PendingIntent getDisplayIntent() {
7308 return mDisplayIntent;
7309 }
7310
7311 /**
7312 * Add an additional page of content to display with this notification. The current
7313 * notification forms the first page, and pages added using this function form
7314 * subsequent pages. This field can be used to separate a notification into multiple
7315 * sections.
7316 *
7317 * @param page the notification to add as another page
7318 * @return this object for method chaining
7319 * @see android.app.Notification.WearableExtender#getPages
7320 */
7321 public WearableExtender addPage(Notification page) {
7322 mPages.add(page);
7323 return this;
7324 }
7325
7326 /**
7327 * Add additional pages of content to display with this notification. The current
7328 * notification forms the first page, and pages added using this function form
7329 * subsequent pages. This field can be used to separate a notification into multiple
7330 * sections.
7331 *
7332 * @param pages a list of notifications
7333 * @return this object for method chaining
7334 * @see android.app.Notification.WearableExtender#getPages
7335 */
7336 public WearableExtender addPages(List<Notification> pages) {
7337 mPages.addAll(pages);
7338 return this;
7339 }
7340
7341 /**
7342 * Clear all additional pages present on this builder.
7343 * @return this object for method chaining.
7344 * @see #addPage
7345 */
7346 public WearableExtender clearPages() {
7347 mPages.clear();
7348 return this;
7349 }
7350
7351 /**
7352 * Get the array of additional pages of content for displaying this notification. The
7353 * current notification forms the first page, and elements within this array form
7354 * subsequent pages. This field can be used to separate a notification into multiple
7355 * sections.
7356 * @return the pages for this notification
7357 */
7358 public List<Notification> getPages() {
7359 return mPages;
7360 }
7361
7362 /**
7363 * Set a background image to be displayed behind the notification content.
7364 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7365 * will work with any notification style.
7366 *
7367 * @param background the background bitmap
7368 * @return this object for method chaining
7369 * @see android.app.Notification.WearableExtender#getBackground
7370 */
7371 public WearableExtender setBackground(Bitmap background) {
7372 mBackground = background;
7373 return this;
7374 }
7375
7376 /**
7377 * Get a background image to be displayed behind the notification content.
7378 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7379 * will work with any notification style.
7380 *
7381 * @return the background image
7382 * @see android.app.Notification.WearableExtender#setBackground
7383 */
7384 public Bitmap getBackground() {
7385 return mBackground;
7386 }
7387
7388 /**
7389 * Set an icon that goes with the content of this notification.
7390 */
7391 public WearableExtender setContentIcon(int icon) {
7392 mContentIcon = icon;
7393 return this;
7394 }
7395
7396 /**
7397 * Get an icon that goes with the content of this notification.
7398 */
7399 public int getContentIcon() {
7400 return mContentIcon;
7401 }
7402
7403 /**
7404 * Set the gravity that the content icon should have within the notification display.
7405 * Supported values include {@link android.view.Gravity#START} and
7406 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7407 * @see #setContentIcon
7408 */
7409 public WearableExtender setContentIconGravity(int contentIconGravity) {
7410 mContentIconGravity = contentIconGravity;
7411 return this;
7412 }
7413
7414 /**
7415 * Get the gravity that the content icon should have within the notification display.
7416 * Supported values include {@link android.view.Gravity#START} and
7417 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7418 * @see #getContentIcon
7419 */
7420 public int getContentIconGravity() {
7421 return mContentIconGravity;
7422 }
7423
7424 /**
7425 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007426 * this notification. This action will no longer display separately from the
7427 * notification's content.
7428 *
Griff Hazenca48d352014-05-28 22:37:13 -07007429 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007430 * set, although the list of available actions comes from the main notification and not
7431 * from the child page's notification.
7432 *
7433 * @param actionIndex The index of the action to hoist onto the current notification page.
7434 * If wearable actions were added to the main notification, this index
7435 * will apply to that list, otherwise it will apply to the regular
7436 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007437 */
7438 public WearableExtender setContentAction(int actionIndex) {
7439 mContentActionIndex = actionIndex;
7440 return this;
7441 }
7442
7443 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007444 * Get the index of the notification action, if any, that was specified as being clickable
7445 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007446 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007447 *
Griff Hazenca48d352014-05-28 22:37:13 -07007448 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007449 * set, although the list of available actions comes from the main notification and not
7450 * from the child page's notification.
7451 *
7452 * <p>If wearable specific actions were added to the main notification, this index will
7453 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007454 *
7455 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007456 */
7457 public int getContentAction() {
7458 return mContentActionIndex;
7459 }
7460
7461 /**
7462 * Set the gravity that this notification should have within the available viewport space.
7463 * Supported values include {@link android.view.Gravity#TOP},
7464 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7465 * The default value is {@link android.view.Gravity#BOTTOM}.
7466 */
7467 public WearableExtender setGravity(int gravity) {
7468 mGravity = gravity;
7469 return this;
7470 }
7471
7472 /**
7473 * Get the gravity that this notification should have within the available viewport space.
7474 * Supported values include {@link android.view.Gravity#TOP},
7475 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7476 * The default value is {@link android.view.Gravity#BOTTOM}.
7477 */
7478 public int getGravity() {
7479 return mGravity;
7480 }
7481
7482 /**
7483 * Set the custom size preset for the display of this notification out of the available
7484 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7485 * {@link #SIZE_LARGE}.
7486 * <p>Some custom size presets are only applicable for custom display notifications created
7487 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7488 * documentation for the preset in question. See also
7489 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7490 */
7491 public WearableExtender setCustomSizePreset(int sizePreset) {
7492 mCustomSizePreset = sizePreset;
7493 return this;
7494 }
7495
7496 /**
7497 * Get the custom size preset for the display of this notification out of the available
7498 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7499 * {@link #SIZE_LARGE}.
7500 * <p>Some custom size presets are only applicable for custom display notifications created
7501 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7502 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7503 */
7504 public int getCustomSizePreset() {
7505 return mCustomSizePreset;
7506 }
7507
7508 /**
7509 * Set the custom height in pixels for the display of this notification's content.
7510 * <p>This option is only available for custom display notifications created
7511 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7512 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7513 * {@link #getCustomContentHeight}.
7514 */
7515 public WearableExtender setCustomContentHeight(int height) {
7516 mCustomContentHeight = height;
7517 return this;
7518 }
7519
7520 /**
7521 * Get the custom height in pixels for the display of this notification's content.
7522 * <p>This option is only available for custom display notifications created
7523 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7524 * {@link #setCustomContentHeight}.
7525 */
7526 public int getCustomContentHeight() {
7527 return mCustomContentHeight;
7528 }
7529
7530 /**
7531 * Set whether the scrolling position for the contents of this notification should start
7532 * at the bottom of the contents instead of the top when the contents are too long to
7533 * display within the screen. Default is false (start scroll at the top).
7534 */
7535 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7536 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7537 return this;
7538 }
7539
7540 /**
7541 * Get whether the scrolling position for the contents of this notification should start
7542 * at the bottom of the contents instead of the top when the contents are too long to
7543 * display within the screen. Default is false (start scroll at the top).
7544 */
7545 public boolean getStartScrollBottom() {
7546 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7547 }
7548
7549 /**
7550 * Set whether the content intent is available when the wearable device is not connected
7551 * to a companion device. The user can still trigger this intent when the wearable device
7552 * is offline, but a visual hint will indicate that the content intent may not be available.
7553 * Defaults to true.
7554 */
7555 public WearableExtender setContentIntentAvailableOffline(
7556 boolean contentIntentAvailableOffline) {
7557 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7558 return this;
7559 }
7560
7561 /**
7562 * Get whether the content intent is available when the wearable device is not connected
7563 * to a companion device. The user can still trigger this intent when the wearable device
7564 * is offline, but a visual hint will indicate that the content intent may not be available.
7565 * Defaults to true.
7566 */
7567 public boolean getContentIntentAvailableOffline() {
7568 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7569 }
7570
7571 /**
7572 * Set a hint that this notification's icon should not be displayed.
7573 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7574 * @return this object for method chaining
7575 */
7576 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7577 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7578 return this;
7579 }
7580
7581 /**
7582 * Get a hint that this notification's icon should not be displayed.
7583 * @return {@code true} if this icon should not be displayed, false otherwise.
7584 * The default value is {@code false} if this was never set.
7585 */
7586 public boolean getHintHideIcon() {
7587 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7588 }
7589
7590 /**
7591 * Set a visual hint that only the background image of this notification should be
7592 * displayed, and other semantic content should be hidden. This hint is only applicable
7593 * to sub-pages added using {@link #addPage}.
7594 */
7595 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7596 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7597 return this;
7598 }
7599
7600 /**
7601 * Get a visual hint that only the background image of this notification should be
7602 * displayed, and other semantic content should be hidden. This hint is only applicable
7603 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7604 */
7605 public boolean getHintShowBackgroundOnly() {
7606 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7607 }
7608
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007609 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007610 * Set a hint that this notification's background should not be clipped if possible,
7611 * and should instead be resized to fully display on the screen, retaining the aspect
7612 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007613 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7614 * @return this object for method chaining
7615 */
7616 public WearableExtender setHintAvoidBackgroundClipping(
7617 boolean hintAvoidBackgroundClipping) {
7618 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7619 return this;
7620 }
7621
7622 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007623 * Get a hint that this notification's background should not be clipped if possible,
7624 * and should instead be resized to fully display on the screen, retaining the aspect
7625 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007626 * @return {@code true} if it's ok if the background is clipped on the screen, false
7627 * otherwise. The default value is {@code false} if this was never set.
7628 */
7629 public boolean getHintAvoidBackgroundClipping() {
7630 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7631 }
7632
7633 /**
7634 * Set a hint that the screen should remain on for at least this duration when
7635 * this notification is displayed on the screen.
7636 * @param timeout The requested screen timeout in milliseconds. Can also be either
7637 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7638 * @return this object for method chaining
7639 */
7640 public WearableExtender setHintScreenTimeout(int timeout) {
7641 mHintScreenTimeout = timeout;
7642 return this;
7643 }
7644
7645 /**
7646 * Get the duration, in milliseconds, that the screen should remain on for
7647 * when this notification is displayed.
7648 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7649 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7650 */
7651 public int getHintScreenTimeout() {
7652 return mHintScreenTimeout;
7653 }
7654
Alex Hills9ab3a232016-04-05 14:54:56 -04007655 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007656 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7657 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7658 * qr codes, as well as other simple black-and-white tickets.
7659 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7660 * @return this object for method chaining
7661 */
7662 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7663 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7664 return this;
7665 }
7666
7667 /**
7668 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7669 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7670 * qr codes, as well as other simple black-and-white tickets.
7671 * @return {@code true} if it should be displayed in ambient, false otherwise
7672 * otherwise. The default value is {@code false} if this was never set.
7673 */
7674 public boolean getHintAmbientBigPicture() {
7675 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7676 }
7677
7678 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007679 * Set a hint that this notification's content intent will launch an {@link Activity}
7680 * directly, telling the platform that it can generate the appropriate transitions.
7681 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7682 * an activity and transitions should be generated, false otherwise.
7683 * @return this object for method chaining
7684 */
7685 public WearableExtender setHintContentIntentLaunchesActivity(
7686 boolean hintContentIntentLaunchesActivity) {
7687 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7688 return this;
7689 }
7690
7691 /**
7692 * Get a hint that this notification's content intent will launch an {@link Activity}
7693 * directly, telling the platform that it can generate the appropriate transitions
7694 * @return {@code true} if the content intent will launch an activity and transitions should
7695 * be generated, false otherwise. The default value is {@code false} if this was never set.
7696 */
7697 public boolean getHintContentIntentLaunchesActivity() {
7698 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7699 }
7700
Nadia Benbernou948627e2016-04-14 14:41:08 -04007701 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007702 * Sets the dismissal id for this notification. If a notification is posted with a
7703 * dismissal id, then when that notification is canceled, notifications on other wearables
7704 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007705 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007706 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007707 * @param dismissalId the dismissal id of the notification.
7708 * @return this object for method chaining
7709 */
7710 public WearableExtender setDismissalId(String dismissalId) {
7711 mDismissalId = dismissalId;
7712 return this;
7713 }
7714
7715 /**
7716 * Returns the dismissal id of the notification.
7717 * @return the dismissal id of the notification or null if it has not been set.
7718 */
7719 public String getDismissalId() {
7720 return mDismissalId;
7721 }
7722
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007723 /**
7724 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7725 * posted from a phone to provide finer-grained control on what notifications are bridged
7726 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7727 * Features to Notifications</a> for more information.
7728 * @param bridgeTag the bridge tag of the notification.
7729 * @return this object for method chaining
7730 */
7731 public WearableExtender setBridgeTag(String bridgeTag) {
7732 mBridgeTag = bridgeTag;
7733 return this;
7734 }
7735
7736 /**
7737 * Returns the bridge tag of the notification.
7738 * @return the bridge tag or null if not present.
7739 */
7740 public String getBridgeTag() {
7741 return mBridgeTag;
7742 }
7743
Griff Hazen61a9e862014-05-22 16:05:19 -07007744 private void setFlag(int mask, boolean value) {
7745 if (value) {
7746 mFlags |= mask;
7747 } else {
7748 mFlags &= ~mask;
7749 }
7750 }
7751 }
7752
7753 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007754 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7755 * with car extensions:
7756 *
7757 * <ol>
7758 * <li>Create an {@link Notification.Builder}, setting any desired
7759 * properties.
7760 * <li>Create a {@link CarExtender}.
7761 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7762 * {@link CarExtender}.
7763 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7764 * to apply the extensions to a notification.
7765 * </ol>
7766 *
7767 * <pre class="prettyprint">
7768 * Notification notification = new Notification.Builder(context)
7769 * ...
7770 * .extend(new CarExtender()
7771 * .set*(...))
7772 * .build();
7773 * </pre>
7774 *
7775 * <p>Car extensions can be accessed on an existing notification by using the
7776 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7777 * to access values.
7778 */
7779 public static final class CarExtender implements Extender {
7780 private static final String TAG = "CarExtender";
7781
7782 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7783 private static final String EXTRA_LARGE_ICON = "large_icon";
7784 private static final String EXTRA_CONVERSATION = "car_conversation";
7785 private static final String EXTRA_COLOR = "app_color";
7786
7787 private Bitmap mLargeIcon;
7788 private UnreadConversation mUnreadConversation;
7789 private int mColor = Notification.COLOR_DEFAULT;
7790
7791 /**
7792 * Create a {@link CarExtender} with default options.
7793 */
7794 public CarExtender() {
7795 }
7796
7797 /**
7798 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7799 *
7800 * @param notif The notification from which to copy options.
7801 */
7802 public CarExtender(Notification notif) {
7803 Bundle carBundle = notif.extras == null ?
7804 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7805 if (carBundle != null) {
7806 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7807 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7808
7809 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7810 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7811 }
7812 }
7813
7814 /**
7815 * Apply car extensions to a notification that is being built. This is typically called by
7816 * the {@link Notification.Builder#extend(Notification.Extender)}
7817 * method of {@link Notification.Builder}.
7818 */
7819 @Override
7820 public Notification.Builder extend(Notification.Builder builder) {
7821 Bundle carExtensions = new Bundle();
7822
7823 if (mLargeIcon != null) {
7824 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7825 }
7826 if (mColor != Notification.COLOR_DEFAULT) {
7827 carExtensions.putInt(EXTRA_COLOR, mColor);
7828 }
7829
7830 if (mUnreadConversation != null) {
7831 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7832 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7833 }
7834
7835 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7836 return builder;
7837 }
7838
7839 /**
7840 * Sets the accent color to use when Android Auto presents the notification.
7841 *
7842 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7843 * to accent the displayed notification. However, not all colors are acceptable in an
7844 * automotive setting. This method can be used to override the color provided in the
7845 * notification in such a situation.
7846 */
Tor Norbye80756e32015-03-02 09:39:27 -08007847 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007848 mColor = color;
7849 return this;
7850 }
7851
7852 /**
7853 * Gets the accent color.
7854 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007855 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007856 */
Tor Norbye80756e32015-03-02 09:39:27 -08007857 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007858 public int getColor() {
7859 return mColor;
7860 }
7861
7862 /**
7863 * Sets the large icon of the car notification.
7864 *
7865 * If no large icon is set in the extender, Android Auto will display the icon
7866 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7867 *
7868 * @param largeIcon The large icon to use in the car notification.
7869 * @return This object for method chaining.
7870 */
7871 public CarExtender setLargeIcon(Bitmap largeIcon) {
7872 mLargeIcon = largeIcon;
7873 return this;
7874 }
7875
7876 /**
7877 * Gets the large icon used in this car notification, or null if no icon has been set.
7878 *
7879 * @return The large icon for the car notification.
7880 * @see CarExtender#setLargeIcon
7881 */
7882 public Bitmap getLargeIcon() {
7883 return mLargeIcon;
7884 }
7885
7886 /**
7887 * Sets the unread conversation in a message notification.
7888 *
7889 * @param unreadConversation The unread part of the conversation this notification conveys.
7890 * @return This object for method chaining.
7891 */
7892 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7893 mUnreadConversation = unreadConversation;
7894 return this;
7895 }
7896
7897 /**
7898 * Returns the unread conversation conveyed by this notification.
7899 * @see #setUnreadConversation(UnreadConversation)
7900 */
7901 public UnreadConversation getUnreadConversation() {
7902 return mUnreadConversation;
7903 }
7904
7905 /**
7906 * A class which holds the unread messages from a conversation.
7907 */
7908 public static class UnreadConversation {
7909 private static final String KEY_AUTHOR = "author";
7910 private static final String KEY_TEXT = "text";
7911 private static final String KEY_MESSAGES = "messages";
7912 private static final String KEY_REMOTE_INPUT = "remote_input";
7913 private static final String KEY_ON_REPLY = "on_reply";
7914 private static final String KEY_ON_READ = "on_read";
7915 private static final String KEY_PARTICIPANTS = "participants";
7916 private static final String KEY_TIMESTAMP = "timestamp";
7917
7918 private final String[] mMessages;
7919 private final RemoteInput mRemoteInput;
7920 private final PendingIntent mReplyPendingIntent;
7921 private final PendingIntent mReadPendingIntent;
7922 private final String[] mParticipants;
7923 private final long mLatestTimestamp;
7924
7925 UnreadConversation(String[] messages, RemoteInput remoteInput,
7926 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7927 String[] participants, long latestTimestamp) {
7928 mMessages = messages;
7929 mRemoteInput = remoteInput;
7930 mReadPendingIntent = readPendingIntent;
7931 mReplyPendingIntent = replyPendingIntent;
7932 mParticipants = participants;
7933 mLatestTimestamp = latestTimestamp;
7934 }
7935
7936 /**
7937 * Gets the list of messages conveyed by this notification.
7938 */
7939 public String[] getMessages() {
7940 return mMessages;
7941 }
7942
7943 /**
7944 * Gets the remote input that will be used to convey the response to a message list, or
7945 * null if no such remote input exists.
7946 */
7947 public RemoteInput getRemoteInput() {
7948 return mRemoteInput;
7949 }
7950
7951 /**
7952 * Gets the pending intent that will be triggered when the user replies to this
7953 * notification.
7954 */
7955 public PendingIntent getReplyPendingIntent() {
7956 return mReplyPendingIntent;
7957 }
7958
7959 /**
7960 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7961 * in this object's message list.
7962 */
7963 public PendingIntent getReadPendingIntent() {
7964 return mReadPendingIntent;
7965 }
7966
7967 /**
7968 * Gets the participants in the conversation.
7969 */
7970 public String[] getParticipants() {
7971 return mParticipants;
7972 }
7973
7974 /**
7975 * Gets the firs participant in the conversation.
7976 */
7977 public String getParticipant() {
7978 return mParticipants.length > 0 ? mParticipants[0] : null;
7979 }
7980
7981 /**
7982 * Gets the timestamp of the conversation.
7983 */
7984 public long getLatestTimestamp() {
7985 return mLatestTimestamp;
7986 }
7987
7988 Bundle getBundleForUnreadConversation() {
7989 Bundle b = new Bundle();
7990 String author = null;
7991 if (mParticipants != null && mParticipants.length > 1) {
7992 author = mParticipants[0];
7993 }
7994 Parcelable[] messages = new Parcelable[mMessages.length];
7995 for (int i = 0; i < messages.length; i++) {
7996 Bundle m = new Bundle();
7997 m.putString(KEY_TEXT, mMessages[i]);
7998 m.putString(KEY_AUTHOR, author);
7999 messages[i] = m;
8000 }
8001 b.putParcelableArray(KEY_MESSAGES, messages);
8002 if (mRemoteInput != null) {
8003 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8004 }
8005 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8006 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8007 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8008 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8009 return b;
8010 }
8011
8012 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8013 if (b == null) {
8014 return null;
8015 }
8016 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8017 String[] messages = null;
8018 if (parcelableMessages != null) {
8019 String[] tmp = new String[parcelableMessages.length];
8020 boolean success = true;
8021 for (int i = 0; i < tmp.length; i++) {
8022 if (!(parcelableMessages[i] instanceof Bundle)) {
8023 success = false;
8024 break;
8025 }
8026 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8027 if (tmp[i] == null) {
8028 success = false;
8029 break;
8030 }
8031 }
8032 if (success) {
8033 messages = tmp;
8034 } else {
8035 return null;
8036 }
8037 }
8038
8039 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8040 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8041
8042 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8043
8044 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8045 if (participants == null || participants.length != 1) {
8046 return null;
8047 }
8048
8049 return new UnreadConversation(messages,
8050 remoteInput,
8051 onReply,
8052 onRead,
8053 participants, b.getLong(KEY_TIMESTAMP));
8054 }
8055 };
8056
8057 /**
8058 * Builder class for {@link CarExtender.UnreadConversation} objects.
8059 */
8060 public static class Builder {
8061 private final List<String> mMessages = new ArrayList<String>();
8062 private final String mParticipant;
8063 private RemoteInput mRemoteInput;
8064 private PendingIntent mReadPendingIntent;
8065 private PendingIntent mReplyPendingIntent;
8066 private long mLatestTimestamp;
8067
8068 /**
8069 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8070 *
8071 * @param name The name of the other participant in the conversation.
8072 */
8073 public Builder(String name) {
8074 mParticipant = name;
8075 }
8076
8077 /**
8078 * Appends a new unread message to the list of messages for this conversation.
8079 *
8080 * The messages should be added from oldest to newest.
8081 *
8082 * @param message The text of the new unread message.
8083 * @return This object for method chaining.
8084 */
8085 public Builder addMessage(String message) {
8086 mMessages.add(message);
8087 return this;
8088 }
8089
8090 /**
8091 * Sets the pending intent and remote input which will convey the reply to this
8092 * notification.
8093 *
8094 * @param pendingIntent The pending intent which will be triggered on a reply.
8095 * @param remoteInput The remote input parcelable which will carry the reply.
8096 * @return This object for method chaining.
8097 *
8098 * @see CarExtender.UnreadConversation#getRemoteInput
8099 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8100 */
8101 public Builder setReplyAction(
8102 PendingIntent pendingIntent, RemoteInput remoteInput) {
8103 mRemoteInput = remoteInput;
8104 mReplyPendingIntent = pendingIntent;
8105
8106 return this;
8107 }
8108
8109 /**
8110 * Sets the pending intent that will be sent once the messages in this notification
8111 * are read.
8112 *
8113 * @param pendingIntent The pending intent to use.
8114 * @return This object for method chaining.
8115 */
8116 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8117 mReadPendingIntent = pendingIntent;
8118 return this;
8119 }
8120
8121 /**
8122 * Sets the timestamp of the most recent message in an unread conversation.
8123 *
8124 * If a messaging notification has been posted by your application and has not
8125 * yet been cancelled, posting a later notification with the same id and tag
8126 * but without a newer timestamp may result in Android Auto not displaying a
8127 * heads up notification for the later notification.
8128 *
8129 * @param timestamp The timestamp of the most recent message in the conversation.
8130 * @return This object for method chaining.
8131 */
8132 public Builder setLatestTimestamp(long timestamp) {
8133 mLatestTimestamp = timestamp;
8134 return this;
8135 }
8136
8137 /**
8138 * Builds a new unread conversation object.
8139 *
8140 * @return The new unread conversation object.
8141 */
8142 public UnreadConversation build() {
8143 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8144 String[] participants = { mParticipant };
8145 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8146 mReadPendingIntent, participants, mLatestTimestamp);
8147 }
8148 }
8149 }
8150
8151 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008152 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8153 * with a TV extension:
8154 *
8155 * <ol>
8156 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8157 * <li>Create a {@link TvExtender}.
8158 * <li>Set TV-specific properties using the {@code set} methods of
8159 * {@link TvExtender}.
8160 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8161 * to apply the extension to a notification.
8162 * </ol>
8163 *
8164 * <pre class="prettyprint">
8165 * Notification notification = new Notification.Builder(context)
8166 * ...
8167 * .extend(new TvExtender()
8168 * .set*(...))
8169 * .build();
8170 * </pre>
8171 *
8172 * <p>TV extensions can be accessed on an existing notification by using the
8173 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8174 * to access values.
8175 *
8176 * @hide
8177 */
8178 @SystemApi
8179 public static final class TvExtender implements Extender {
8180 private static final String TAG = "TvExtender";
8181
8182 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8183 private static final String EXTRA_FLAGS = "flags";
8184 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8185 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008186 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008187
8188 // Flags bitwise-ored to mFlags
8189 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8190
8191 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008192 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008193 private PendingIntent mContentIntent;
8194 private PendingIntent mDeleteIntent;
8195
8196 /**
8197 * Create a {@link TvExtender} with default options.
8198 */
8199 public TvExtender() {
8200 mFlags = FLAG_AVAILABLE_ON_TV;
8201 }
8202
8203 /**
8204 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8205 *
8206 * @param notif The notification from which to copy options.
8207 */
8208 public TvExtender(Notification notif) {
8209 Bundle bundle = notif.extras == null ?
8210 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8211 if (bundle != null) {
8212 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008213 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008214 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8215 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8216 }
8217 }
8218
8219 /**
8220 * Apply a TV extension to a notification that is being built. This is typically called by
8221 * the {@link Notification.Builder#extend(Notification.Extender)}
8222 * method of {@link Notification.Builder}.
8223 */
8224 @Override
8225 public Notification.Builder extend(Notification.Builder builder) {
8226 Bundle bundle = new Bundle();
8227
8228 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008229 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008230 if (mContentIntent != null) {
8231 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8232 }
8233
8234 if (mDeleteIntent != null) {
8235 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8236 }
8237
8238 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8239 return builder;
8240 }
8241
8242 /**
8243 * Returns true if this notification should be shown on TV. This method return true
8244 * if the notification was extended with a TvExtender.
8245 */
8246 public boolean isAvailableOnTv() {
8247 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8248 }
8249
8250 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008251 * Specifies the channel the notification should be delivered on when shown on TV.
8252 * It can be different from the channel that the notification is delivered to when
8253 * posting on a non-TV device.
8254 */
8255 public TvExtender setChannel(String channelId) {
8256 mChannelId = channelId;
8257 return this;
8258 }
8259
8260 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008261 * Specifies the channel the notification should be delivered on when shown on TV.
8262 * It can be different from the channel that the notification is delivered to when
8263 * posting on a non-TV device.
8264 */
8265 public TvExtender setChannelId(String channelId) {
8266 mChannelId = channelId;
8267 return this;
8268 }
8269
Jeff Sharkey000ce802017-04-29 13:13:27 -06008270 /** @removed */
8271 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008272 public String getChannel() {
8273 return mChannelId;
8274 }
8275
8276 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008277 * Returns the id of the channel this notification posts to on TV.
8278 */
8279 public String getChannelId() {
8280 return mChannelId;
8281 }
8282
8283 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008284 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8285 * If provided, it is used instead of the content intent specified
8286 * at the level of Notification.
8287 */
8288 public TvExtender setContentIntent(PendingIntent intent) {
8289 mContentIntent = intent;
8290 return this;
8291 }
8292
8293 /**
8294 * Returns the TV-specific content intent. If this method returns null, the
8295 * main content intent on the notification should be used.
8296 *
8297 * @see {@link Notification#contentIntent}
8298 */
8299 public PendingIntent getContentIntent() {
8300 return mContentIntent;
8301 }
8302
8303 /**
8304 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8305 * by the user on TV. If provided, it is used instead of the delete intent specified
8306 * at the level of Notification.
8307 */
8308 public TvExtender setDeleteIntent(PendingIntent intent) {
8309 mDeleteIntent = intent;
8310 return this;
8311 }
8312
8313 /**
8314 * Returns the TV-specific delete intent. If this method returns null, the
8315 * main delete intent on the notification should be used.
8316 *
8317 * @see {@link Notification#deleteIntent}
8318 */
8319 public PendingIntent getDeleteIntent() {
8320 return mDeleteIntent;
8321 }
8322 }
8323
8324 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008325 * Get an array of Notification objects from a parcelable array bundle field.
8326 * Update the bundle to have a typed array so fetches in the future don't need
8327 * to do an array copy.
8328 */
8329 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8330 Parcelable[] array = bundle.getParcelableArray(key);
8331 if (array instanceof Notification[] || array == null) {
8332 return (Notification[]) array;
8333 }
8334 Notification[] typedArray = Arrays.copyOf(array, array.length,
8335 Notification[].class);
8336 bundle.putParcelableArray(key, typedArray);
8337 return typedArray;
8338 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008339
8340 private static class BuilderRemoteViews extends RemoteViews {
8341 public BuilderRemoteViews(Parcel parcel) {
8342 super(parcel);
8343 }
8344
Kenny Guy77320062014-08-27 21:37:15 +01008345 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8346 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008347 }
8348
8349 @Override
8350 public BuilderRemoteViews clone() {
8351 Parcel p = Parcel.obtain();
8352 writeToParcel(p, 0);
8353 p.setDataPosition(0);
8354 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8355 p.recycle();
8356 return brv;
8357 }
8358 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008359
8360 private static class StandardTemplateParams {
8361 boolean hasProgress = true;
8362 boolean ambient = false;
8363 CharSequence title;
8364 CharSequence text;
8365
8366 final StandardTemplateParams reset() {
8367 hasProgress = true;
8368 ambient = false;
8369 title = null;
8370 text = null;
8371 return this;
8372 }
8373
8374 final StandardTemplateParams hasProgress(boolean hasProgress) {
8375 this.hasProgress = hasProgress;
8376 return this;
8377 }
8378
8379 final StandardTemplateParams title(CharSequence title) {
8380 this.title = title;
8381 return this;
8382 }
8383
8384 final StandardTemplateParams text(CharSequence text) {
8385 this.text = text;
8386 return this;
8387 }
8388
8389 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008390 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008391 this.ambient = ambient;
8392 return this;
8393 }
8394
8395 final StandardTemplateParams fillTextsFrom(Builder b) {
8396 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008397 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8398 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008399 return this;
8400 }
8401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402}