blob: 057602bdecdbd24547e02be931a5fe8d61c6ce2a [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
Mady Mellor9848a6c2019-03-19 15:29:05 -070019import static android.graphics.drawable.Icon.TYPE_BITMAP;
20
Lucas Dupina291d192018-06-07 13:59:42 -070021import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070022
Tor Norbye80756e32015-03-02 09:39:27 -080023import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070024import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040025import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070026import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040027import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070028import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060029import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040030import android.annotation.SdkConstant;
31import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040032import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010033import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.Context;
35import android.content.Intent;
Felipe Leme90205ef2019-03-05 09:59:52 -080036import android.content.LocusId;
Kenny Guy77320062014-08-27 21:37:15 +010037import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040038import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020039import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050040import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020041import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070042import android.content.res.Configuration;
43import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040044import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040045import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010046import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070047import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010048import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010049import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040050import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040051import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070052import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080053import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070054import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040056import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020057import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050058import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070059import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Parcel;
61import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040062import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080063import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070064import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070065import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.SpannableStringBuilder;
67import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080070import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070071import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080072import android.text.style.RelativeSizeSpan;
73import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070074import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040075import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010076import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050077import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070078import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070079import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080080import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080081import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070082import android.view.ViewGroup;
Felipe Leme90205ef2019-03-05 09:59:52 -080083import android.view.contentcapture.ContentCaptureContext;
Jeff Sharkey1c400132011-08-05 14:50:13 -070084import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.widget.RemoteViews;
86
Griff Hazen959591e2014-05-15 22:26:18 -070087import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070088import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070089import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070090import com.android.internal.util.ContrastColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070091
Tor Norbyed9273d62013-05-30 15:59:53 -070092import java.lang.annotation.Retention;
93import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020094import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070096import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070097import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070098import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050099import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -0500100import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -0600101import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -0800102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103/**
104 * A class that represents how a persistent notification is to be presented to
105 * the user using the {@link android.app.NotificationManager}.
106 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800107 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
108 * easier to construct Notifications.</p>
109 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700110 * <div class="special reference">
111 * <h3>Developer Guides</h3>
112 * <p>For a guide to creating notifications, read the
113 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
114 * developer guide.</p>
115 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 */
117public class Notification implements Parcelable
118{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400119 private static final String TAG = "Notification";
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400122 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400123 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400124 */
125 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
126 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
127 = "android.intent.category.NOTIFICATION_PREFERENCES";
128
129 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500130 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
131 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400132 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500133 */
134 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
135
136 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400137 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400138 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
139 * what settings should be shown in the target app.
140 */
141 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
142
143 /**
144 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400145 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
146 * that can be used to narrow down what settings should be shown in the target app.
147 */
148 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
149
150 /**
151 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
152 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
153 * that can be used to narrow down what settings should be shown in the target app.
154 */
155 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
156
157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 * Use all default values (where applicable).
159 */
160 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 /**
163 * Use the default notification sound. This will ignore any given
164 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500165 *
Chris Wren47c20a12014-06-18 17:27:29 -0400166 * <p>
167 * A notification that is noisy is more likely to be presented as a heads-up notification.
168 * </p>
169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 */
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int DEFAULT_SOUND = 1;
174
175 /**
176 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500177 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700178 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179 *
Chris Wren47c20a12014-06-18 17:27:29 -0400180 * <p>
181 * A notification that vibrates is more likely to be presented as a heads-up notification.
182 * </p>
183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185 */
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /**
190 * Use the default notification lights. This will ignore the
191 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
192 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195 */
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200200 * Maximum length of CharSequences accepted by Builder and friends.
201 *
202 * <p>
203 * Avoids spamming the system with overly large strings such as full e-mails.
204 */
205 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
206
207 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800208 * Maximum entries of reply text that are accepted by Builder and friends.
209 */
210 private static final int MAX_REPLY_HISTORY = 5;
211
Tony Mak09db2ea2018-06-27 18:12:48 +0100212 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000213 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
214 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100215 * @hide
216 */
217 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800218
219 /**
220 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
221 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
222 *
223 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
224 * sends messages.</p>
225 */
226 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
227
Adrian Roose458aa82015-12-08 16:17:19 -0800228 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500229 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800230 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500231 * Default value: {@link System#currentTimeMillis() Now}.
232 *
233 * Choose a timestamp that will be most relevant to the user. For most finite events, this
234 * corresponds to the time the event happened (or will happen, in the case of events that have
235 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800236 * timestamped according to when the activity began.
237 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500238 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800239 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500240 * <ul>
241 * <li>Notification of a new chat message should be stamped when the message was received.</li>
242 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
243 * <li>Notification of a completed file download should be stamped when the download finished.</li>
244 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
245 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
246 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800247 * </ul>
248 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700249 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
250 * anymore by default and must be opted into by using
251 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
253 public long when;
254
255 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700256 * The creation time of the notification
257 */
258 private long creationTime;
259
260 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400262 *
263 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 */
Dan Sandler86647982015-05-13 23:41:13 -0400265 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700266 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 public int icon;
268
269 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800270 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
271 * leave it at its default value of 0.
272 *
273 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700274 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800275 */
276 public int iconLevel;
277
278 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500279 * The number of events that this notification represents. For example, in a new mail
280 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800281 *
Julia Reynolds30331982017-04-27 10:12:50 -0400282 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500283 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
284 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 */
Julia Reynolds30331982017-04-27 10:12:50 -0400286 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
288 /**
289 * The intent to execute when the expanded status entry is clicked. If
290 * this is an activity, it must include the
291 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800292 * that you take care of task management as described in the
293 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800294 * Stack</a> document. In particular, make sure to read the notification section
295 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
296 * Notifications</a> for the correct ways to launch an application from a
297 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 */
299 public PendingIntent contentIntent;
300
301 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500302 * The intent to execute when the notification is explicitly dismissed by the user, either with
303 * the "Clear All" button or by swiping it away individually.
304 *
305 * This probably shouldn't be launching an activity since several of those will be sent
306 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 */
308 public PendingIntent deleteIntent;
309
310 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700311 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800312 *
Chris Wren47c20a12014-06-18 17:27:29 -0400313 * <p>
314 * The system UI may choose to display a heads-up notification, instead of
315 * launching this intent, while the user is using the device.
316 * </p>
317 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800318 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400319 */
320 public PendingIntent fullScreenIntent;
321
322 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400323 * Text that summarizes this notification for accessibility services.
324 *
325 * As of the L release, this text is no longer shown on screen, but it is still useful to
326 * accessibility services (where it serves as an audible announcement of the notification's
327 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400328 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800329 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 */
331 public CharSequence tickerText;
332
333 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400334 * Formerly, a view showing the {@link #tickerText}.
335 *
336 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400337 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400338 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800339 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400340
341 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400342 * The view that will represent this notification in the notification list (which is pulled
343 * down from the status bar).
344 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500345 * As of N, this field may be null. The notification view is determined by the inputs
346 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400347 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400349 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 public RemoteViews contentView;
351
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400352 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400353 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400354 * opportunity to show more detail. The system UI may choose to show this
355 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400356 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500357 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400358 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
359 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400360 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400361 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400362 public RemoteViews bigContentView;
363
Chris Wren8fd39ec2014-02-27 17:43:26 -0500364
365 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400366 * A medium-format version of {@link #contentView}, providing the Notification an
367 * opportunity to add action buttons to contentView. At its discretion, the system UI may
368 * choose to show this as a heads-up notification, which will pop up so the user can see
369 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400370 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500371 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400372 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
373 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500374 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400375 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500376 public RemoteViews headsUpContentView;
377
Julia Reynoldsfc640012018-02-21 12:25:27 -0500378 private boolean mUsesStandardHeader;
379
380 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
381 static {
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
389 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500390 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500391 }
392
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400393 /**
Dan Sandler86647982015-05-13 23:41:13 -0400394 * A large bitmap to be shown in the notification content area.
395 *
396 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 */
Dan Sandler86647982015-05-13 23:41:13 -0400398 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800399 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400
401 /**
402 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400405 * A notification that is noisy is more likely to be presented as a heads-up notification.
406 * </p>
407 *
408 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500409 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500411 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500413 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 public Uri sound;
415
416 /**
417 * Use this constant as the value for audioStreamType to request that
418 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700419 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400420 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500421 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700423 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 public static final int STREAM_DEFAULT = -1;
425
426 /**
427 * The audio stream type to use when playing the sound.
428 * Should be one of the STREAM_ constants from
429 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400430 *
431 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700433 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public int audioStreamType = STREAM_DEFAULT;
435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400437 * The default value of {@link #audioAttributes}.
438 */
439 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
440 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
441 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
442 .build();
443
444 /**
445 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500446 *
447 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400448 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500449 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400450 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
451
452 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500453 * The pattern with which to vibrate.
454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * <p>
456 * To vibrate the default pattern, see {@link #defaults}.
457 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500460 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500462 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public long[] vibrate;
464
465 /**
466 * The color of the led. The hardware will do its best approximation.
467 *
468 * @see #FLAG_SHOW_LIGHTS
469 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500470 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 */
Tor Norbye80756e32015-03-02 09:39:27 -0800472 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500473 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 public int ledARGB;
475
476 /**
477 * The number of milliseconds for the LED to be on while it's flashing.
478 * The hardware will do its best approximation.
479 *
480 * @see #FLAG_SHOW_LIGHTS
481 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500482 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500484 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 public int ledOnMS;
486
487 /**
488 * The number of milliseconds for the LED to be off while it's flashing.
489 * The hardware will do its best approximation.
490 *
491 * @see #FLAG_SHOW_LIGHTS
492 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500493 *
494 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500496 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 public int ledOffMS;
498
499 /**
500 * Specifies which values should be taken from the defaults.
501 * <p>
502 * To set, OR the desired from {@link #DEFAULT_SOUND},
503 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
504 * values, use {@link #DEFAULT_ALL}.
505 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500506 *
507 * @deprecated use {@link NotificationChannel#getSound()} and
508 * {@link NotificationChannel#shouldShowLights()} and
509 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500511 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 public int defaults;
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 /**
515 * Bit to be bitwise-ored into the {@link #flags} field that should be
516 * set if you want the LED on for this notification.
517 * <ul>
518 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
519 * or 0 for both ledOnMS and ledOffMS.</li>
520 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
521 * <li>To flash the LED, pass the number of milliseconds that it should
522 * be on and off to ledOnMS and ledOffMS.</li>
523 * </ul>
524 * <p>
525 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800526 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * because they will be set to values that work on any given hardware.
528 * <p>
529 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500530 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500531 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500533 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
535
536 /**
537 * Bit to be bitwise-ored into the {@link #flags} field that should be
538 * set if this notification is in reference to something that is ongoing,
539 * like a phone call. It should not be set if this notification is in
540 * reference to something that happened at a particular point in time,
541 * like a missed phone call.
542 */
543 public static final int FLAG_ONGOING_EVENT = 0x00000002;
544
545 /**
546 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700547 * the audio will be repeated until the notification is
548 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 */
550 public static final int FLAG_INSISTENT = 0x00000004;
551
552 /**
553 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700554 * set if you would only like the sound, vibrate and ticker to be played
555 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 */
557 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
558
559 /**
560 * Bit to be bitwise-ored into the {@link #flags} field that should be
561 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500562 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 */
564 public static final int FLAG_AUTO_CANCEL = 0x00000010;
565
566 /**
567 * Bit to be bitwise-ored into the {@link #flags} field that should be
568 * set if the notification should not be canceled when the user clicks
569 * the Clear all button.
570 */
571 public static final int FLAG_NO_CLEAR = 0x00000020;
572
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700573 /**
574 * Bit to be bitwise-ored into the {@link #flags} field that should be
575 * set if this notification represents a currently running service. This
576 * will normally be set for you by {@link Service#startForeground}.
577 */
578 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
579
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400580 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800582 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400584 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700585 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400587
Griff Hazendfcb0802014-02-11 12:00:00 -0800588 /**
589 * Bit to be bitswise-ored into the {@link #flags} field that should be
590 * set if this notification is relevant to the current device only
591 * and it is not recommended that it bridge to other devices.
592 */
593 public static final int FLAG_LOCAL_ONLY = 0x00000100;
594
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700595 /**
596 * Bit to be bitswise-ored into the {@link #flags} field that should be
597 * set if this notification is the group summary for a group of notifications.
598 * Grouped notifications may display in a cluster or stack on devices which
599 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
600 */
601 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
602
Julia Reynoldse46bb372016-03-17 11:05:58 -0400603 /**
604 * Bit to be bitswise-ored into the {@link #flags} field that should be
605 * set if this notification is the group summary for an auto-group of notifications.
606 *
607 * @hide
608 */
609 @SystemApi
610 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
611
Julia Reynolds4db59552017-06-30 13:34:01 -0400612 /**
613 * @hide
614 */
615 public static final int FLAG_CAN_COLORIZE = 0x00000800;
616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 public int flags;
618
Tor Norbyed9273d62013-05-30 15:59:53 -0700619 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700620 @IntDef(prefix = { "PRIORITY_" }, value = {
621 PRIORITY_DEFAULT,
622 PRIORITY_LOW,
623 PRIORITY_MIN,
624 PRIORITY_HIGH,
625 PRIORITY_MAX
626 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700627 @Retention(RetentionPolicy.SOURCE)
628 public @interface Priority {}
629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631 * Default notification {@link #priority}. If your application does not prioritize its own
632 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500633 *
634 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500636 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500637 public static final int PRIORITY_DEFAULT = 0;
638
639 /**
640 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
641 * items smaller, or at a different position in the list, compared with your app's
642 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500643 *
644 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500646 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647 public static final int PRIORITY_LOW = -1;
648
649 /**
650 * Lowest {@link #priority}; these items might not be shown to the user except under special
651 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500652 *
653 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500655 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656 public static final int PRIORITY_MIN = -2;
657
658 /**
659 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
660 * show these items larger, or at a different position in notification lists, compared with
661 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500662 *
663 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500665 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666 public static final int PRIORITY_HIGH = 1;
667
668 /**
669 * Highest {@link #priority}, for your application's most important items that require the
670 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500671 *
672 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500673 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500674 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500675 public static final int PRIORITY_MAX = 2;
676
677 /**
678 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800679 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500680 * Priority is an indication of how much of the user's valuable attention should be consumed by
681 * this notification. Low-priority notifications may be hidden from the user in certain
682 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500683 * system will make a determination about how to interpret this priority when presenting
684 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400685 *
686 * <p>
687 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
688 * as a heads-up notification.
689 * </p>
690 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500691 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500692 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700693 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500694 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500695 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800696
Dan Sandler26e81cf2014-05-06 10:01:27 -0400697 /**
698 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
699 * to be applied by the standard Style templates when presenting this notification.
700 *
701 * The current template design constructs a colorful header image by overlaying the
702 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
703 * ignored.
704 */
Tor Norbye80756e32015-03-02 09:39:27 -0800705 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400706 public int color = COLOR_DEFAULT;
707
708 /**
709 * Special value of {@link #color} telling the system not to decorate this notification with
710 * any special color but instead use default colors when presenting this notification.
711 */
Tor Norbye80756e32015-03-02 09:39:27 -0800712 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400713 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600714
715 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800716 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800717 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800718 */
719 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800720 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800721
722 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700723 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
724 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600725 * lockscreen).
726 *
727 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
728 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
729 * shown in all situations, but the contents are only available if the device is unlocked for
730 * the appropriate user.
731 *
732 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
733 * can be read even in an "insecure" context (that is, above a secure lockscreen).
734 * To modify the public version of this notification—for example, to redact some portions—see
735 * {@link Builder#setPublicVersion(Notification)}.
736 *
737 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
738 * and ticker until the user has bypassed the lockscreen.
739 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600740 public @Visibility int visibility;
741
742 /** @hide */
743 @IntDef(prefix = { "VISIBILITY_" }, value = {
744 VISIBILITY_PUBLIC,
745 VISIBILITY_PRIVATE,
746 VISIBILITY_SECRET,
747 })
748 @Retention(RetentionPolicy.SOURCE)
749 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600750
Griff Hazenfc3922d2014-08-20 11:56:44 -0700751 /**
752 * Notification visibility: Show this notification in its entirety on all lockscreens.
753 *
754 * {@see #visibility}
755 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600756 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700757
758 /**
759 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
760 * private information on secure lockscreens.
761 *
762 * {@see #visibility}
763 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600764 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700765
766 /**
767 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
768 *
769 * {@see #visibility}
770 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600771 public static final int VISIBILITY_SECRET = -1;
772
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400774 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500775 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400776 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500777
778 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700779 * Notification category: map turn-by-turn navigation.
780 */
781 public static final String CATEGORY_NAVIGATION = "navigation";
782
783 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400784 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400786 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787
788 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400791 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500792
793 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500795 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400796 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500797
798 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400799 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500800 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400801 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500802
803 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400804 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500805 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400806 public static final String CATEGORY_ALARM = "alarm";
807
808 /**
809 * Notification category: progress of a long-running background operation.
810 */
811 public static final String CATEGORY_PROGRESS = "progress";
812
813 /**
814 * Notification category: social network or sharing update.
815 */
816 public static final String CATEGORY_SOCIAL = "social";
817
818 /**
819 * Notification category: error in background operation or authentication status.
820 */
821 public static final String CATEGORY_ERROR = "err";
822
823 /**
824 * Notification category: media transport control for playback.
825 */
826 public static final String CATEGORY_TRANSPORT = "transport";
827
828 /**
829 * Notification category: system or device status update. Reserved for system use.
830 */
831 public static final String CATEGORY_SYSTEM = "sys";
832
833 /**
834 * Notification category: indication of running background service.
835 */
836 public static final String CATEGORY_SERVICE = "service";
837
838 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400839 * Notification category: a specific, timely recommendation for a single thing.
840 * For example, a news app might want to recommend a news story it believes the user will
841 * want to read next.
842 */
843 public static final String CATEGORY_RECOMMENDATION = "recommendation";
844
845 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400846 * Notification category: ongoing information about device or contextual status.
847 */
848 public static final String CATEGORY_STATUS = "status";
849
850 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400851 * Notification category: user-scheduled reminder.
852 */
853 public static final String CATEGORY_REMINDER = "reminder";
854
855 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700856 * Notification category: extreme car emergencies.
857 * @hide
858 */
859 @SystemApi
860 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
861
862 /**
863 * Notification category: car warnings.
864 * @hide
865 */
866 @SystemApi
867 public static final String CATEGORY_CAR_WARNING = "car_warning";
868
869 /**
870 * Notification category: general car system information.
871 * @hide
872 */
873 @SystemApi
874 public static final String CATEGORY_CAR_INFORMATION = "car_information";
875
876 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400877 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
878 * that best describes this Notification. May be used by the system for ranking and filtering.
879 */
880 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500881
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100882 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700883 private String mGroupKey;
884
885 /**
886 * Get the key used to group this notification into a cluster or stack
887 * with other notifications on devices which support such rendering.
888 */
889 public String getGroup() {
890 return mGroupKey;
891 }
892
893 private String mSortKey;
894
895 /**
896 * Get a sort key that orders this notification among other notifications from the
897 * same package. This can be useful if an external sort was already applied and an app
898 * would like to preserve this. Notifications will be sorted lexicographically using this
899 * value, although providing different priorities in addition to providing sort key may
900 * cause this value to be ignored.
901 *
902 * <p>This sort key can also be used to order members of a notification group. See
903 * {@link Builder#setGroup}.
904 *
905 * @see String#compareTo(String)
906 */
907 public String getSortKey() {
908 return mSortKey;
909 }
910
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500911 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400912 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400913 * <p>
914 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
915 * APIs, and are intended to be used by
916 * {@link android.service.notification.NotificationListenerService} implementations to extract
917 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500918 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400919 public Bundle extras = new Bundle();
920
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400921 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700922 * All pending intents in the notification as the system needs to be able to access them but
923 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700924 * custom parcelable objects.
925 *
926 * @hide
927 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100928 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700929 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700930
931 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700932 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
933 * pending intents inside of it, so only those will get the behavior.
934 *
935 * @hide
936 */
Adrian Roosfb921842017-10-26 14:49:56 +0200937 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700938
939 /**
940 * Must be set by a process to start associating tokens with Notification objects
941 * coming in to it. This is set by NotificationManagerService.
942 *
943 * @hide
944 */
945 static public IBinder processWhitelistToken;
946
947 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948 * {@link #extras} key: this is the title of the notification,
949 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
950 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500951 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400952
953 /**
954 * {@link #extras} key: this is the title of the notification when shown in expanded form,
955 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
956 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400957 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400958
959 /**
960 * {@link #extras} key: this is the main text payload, as supplied to
961 * {@link Builder#setContentText(CharSequence)}.
962 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500963 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400964
965 /**
966 * {@link #extras} key: this is a third line of text, as supplied to
967 * {@link Builder#setSubText(CharSequence)}.
968 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400969 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400970
971 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800972 * {@link #extras} key: this is the remote input history, as supplied to
973 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700974 *
975 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
976 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
977 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
978 * notifications once the other party has responded).
979 *
980 * The extra with this key is of type CharSequence[] and contains the most recent entry at
981 * the 0 index, the second most recent at the 1 index, etc.
982 *
983 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800984 */
985 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
986
987 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100988 * {@link #extras} key: boolean as supplied to
989 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
990 *
991 * If set to true, then the view displaying the remote input history from
992 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
993 *
994 * @see Builder#setShowRemoteInputSpinner(boolean)
995 * @hide
996 */
997 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
998
999 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01001000 * {@link #extras} key: boolean as supplied to
1001 * {@link Builder#setHideSmartReplies(boolean)}.
1002 *
1003 * If set to true, then any smart reply buttons will be hidden.
1004 *
1005 * @see Builder#setHideSmartReplies(boolean)
1006 * @hide
1007 */
1008 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1009
1010 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001011 * {@link #extras} key: this is a small piece of additional text as supplied to
1012 * {@link Builder#setContentInfo(CharSequence)}.
1013 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001014 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001015
1016 /**
1017 * {@link #extras} key: this is a line of summary information intended to be shown
1018 * alongside expanded notifications, as supplied to (e.g.)
1019 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1020 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001021 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022
1023 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001024 * {@link #extras} key: this is the longer text shown in the big form of a
1025 * {@link BigTextStyle} notification, as supplied to
1026 * {@link BigTextStyle#bigText(CharSequence)}.
1027 */
1028 public static final String EXTRA_BIG_TEXT = "android.bigText";
1029
1030 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001031 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1032 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001033 *
1034 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001035 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001036 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001037 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001038
1039 /**
1040 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1041 * notification payload, as
1042 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001043 *
1044 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001045 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001046 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001047 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001048
1049 /**
1050 * {@link #extras} key: this is a bitmap to be used instead of the one from
1051 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1052 * shown in its expanded form, as supplied to
1053 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1054 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001055 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001056
1057 /**
1058 * {@link #extras} key: this is the progress value supplied to
1059 * {@link Builder#setProgress(int, int, boolean)}.
1060 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001061 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001062
1063 /**
1064 * {@link #extras} key: this is the maximum value supplied to
1065 * {@link Builder#setProgress(int, int, boolean)}.
1066 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001067 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001068
1069 /**
1070 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1071 * {@link Builder#setProgress(int, int, boolean)}.
1072 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001073 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001074
1075 /**
1076 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1077 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1078 * {@link Builder#setUsesChronometer(boolean)}.
1079 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001080 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001081
1082 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001083 * {@link #extras} key: whether the chronometer set on the notification should count down
1084 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001085 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001086 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001087 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001088
1089 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001090 * {@link #extras} key: whether {@link #when} should be shown,
1091 * as supplied to {@link Builder#setShowWhen(boolean)}.
1092 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001093 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001094
1095 /**
1096 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1097 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1098 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001099 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001100
1101 /**
1102 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1103 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1104 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001105 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001106
1107 /**
1108 * {@link #extras} key: A string representing the name of the specific
1109 * {@link android.app.Notification.Style} used to create this notification.
1110 */
Chris Wren91ad5632013-06-05 15:05:57 -04001111 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001112
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001113 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001114 * {@link #extras} key: A String array containing the people that this notification relates to,
1115 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001116 *
1117 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001118 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001119 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001120
1121 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001122 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1123 * this notification relates to.
1124 */
1125 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1126
1127 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001128 * Allow certain system-generated notifications to appear before the device is provisioned.
1129 * Only available to notifications coming from the android package.
1130 * @hide
1131 */
Maurice Lam96c10032017-03-29 15:42:38 -07001132 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001133 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001134 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1135
1136 /**
Robin Leed107af62018-04-27 13:55:56 +02001137 * {@link #extras} key:
1138 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1139 * pointing to an image that can be displayed in the background when the notification is
1140 * selected. Used on television platforms. The URI must point to an image stream suitable for
1141 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001142 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001143 */
1144 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1145
1146 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001147 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001148 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001149 * {@link android.app.Notification.MediaStyle} notification.
1150 */
1151 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1152
1153 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001154 * {@link #extras} key: the indices of actions to be shown in the compact view,
1155 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1156 */
1157 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1158
Christoph Studer943aa672014-08-03 20:31:16 +02001159 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001160 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1161 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001162 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1163 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001164 *
1165 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001166 */
1167 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1168
1169 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001170 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1171 * direct replies
1172 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1173 * {@link Person}
1174 */
1175 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1176
1177 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001178 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001179 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001180 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001181 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001182
1183 /**
1184 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1185 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001186 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1187 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001188 */
1189 public static final String EXTRA_MESSAGES = "android.messages";
1190
1191 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001192 * {@link #extras} key: an array of
1193 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1194 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1195 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1196 * array of bundles.
1197 */
1198 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1199
1200 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001201 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1202 * represents a group conversation.
1203 */
1204 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1205
1206 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001207 * {@link #extras} key: whether the notification should be colorized as
Gustav Senntondf4c2442019-03-18 11:41:11 +00001208 * supplied to {@link Builder#setColorized(boolean)}.
Selim Cinek7b9605b2017-01-19 17:36:00 -08001209 */
1210 public static final String EXTRA_COLORIZED = "android.colorized";
1211
1212 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001213 * @hide
1214 */
1215 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1216
Selim Cinek247fa012016-02-18 09:50:48 -08001217 /**
1218 * @hide
1219 */
1220 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1221
Shane Brennan472a3b32016-12-12 15:28:10 -08001222 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001223 * @hide
1224 */
1225 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1226
1227 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001228 * {@link #extras} key: the audio contents of this notification.
1229 *
1230 * This is for use when rendering the notification on an audio-focused interface;
1231 * the audio contents are a complete sound sample that contains the contents/body of the
1232 * notification. This may be used in substitute of a Text-to-Speech reading of the
1233 * notification. For example if the notification represents a voice message this should point
1234 * to the audio of that message.
1235 *
1236 * The data stored under this key should be a String representation of a Uri that contains the
1237 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1238 *
1239 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1240 * has a field for holding data URI. That field can be used for audio.
1241 * See {@code Message#setData}.
1242 *
1243 * Example usage:
1244 * <pre>
1245 * {@code
1246 * Notification.Builder myBuilder = (build your Notification as normal);
1247 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1248 * }
1249 * </pre>
1250 */
1251 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1252
Dan Sandler80eaa592016-04-14 23:34:54 -04001253 /** @hide */
1254 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001255 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001256 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1257
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001258 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001259 * This is set on the notifications shown by system_server about apps running foreground
1260 * services. It indicates that the notification should be shown
1261 * only if any of the given apps do not already have a properly tagged
1262 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1263 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001264 * @hide
1265 */
1266 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1267
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001268 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001269 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001270 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001271 private Icon mLargeIcon;
1272
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001273 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001274 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001275 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001276
Julia Reynolds13d898c2017-02-02 12:22:05 -05001277 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001278 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001279 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001280
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001281 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001282
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001283 /** @hide */
1284 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1285 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1286 })
1287 @Retention(RetentionPolicy.SOURCE)
1288 public @interface GroupAlertBehavior {}
1289
1290 /**
1291 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1292 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1293 * notification will not be muted when it is in a group.
1294 */
1295 public static final int GROUP_ALERT_ALL = 0;
1296
1297 /**
1298 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1299 * notification in a group should be silenced (no sound or vibration) even if they are posted
1300 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001301 * mute this notification if this notification is a group child. This must be applied to all
1302 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001303 *
1304 * <p> For example, you might want to use this constant if you post a number of children
1305 * notifications at once (say, after a periodic sync), and only need to notify the user
1306 * audibly once.
1307 */
1308 public static final int GROUP_ALERT_SUMMARY = 1;
1309
1310 /**
1311 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1312 * notification in a group should be silenced (no sound or vibration) even if they are
1313 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1314 * to mute this notification if this notification is a group summary.
1315 *
1316 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001317 * in your group have content and the summary is only used to visually group notifications
1318 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001319 */
1320 public static final int GROUP_ALERT_CHILDREN = 2;
1321
Julia Reynolds2f431e22017-06-07 14:12:09 +00001322 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001323
Julia Reynolds13d898c2017-02-02 12:22:05 -05001324 /**
1325 * If this notification is being shown as a badge, always show as a number.
1326 */
1327 public static final int BADGE_ICON_NONE = 0;
1328
1329 /**
1330 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1331 * represent this notification.
1332 */
1333 public static final int BADGE_ICON_SMALL = 1;
1334
1335 /**
1336 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1337 * represent this notification.
1338 */
1339 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001340 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001341
Chris Wren51c75102013-07-16 20:49:17 -04001342 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001343 * Determines whether the platform can generate contextual actions for a notification.
1344 */
1345 private boolean mAllowSystemGeneratedContextualActions = true;
1346
1347 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001348 * Structure to encapsulate a named action that can be shown as part of this notification.
1349 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1350 * selected by the user.
1351 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001352 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1353 * or {@link Notification.Builder#addAction(Notification.Action)}
1354 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001355 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001356 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001357 /**
1358 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1359 * {@link RemoteInput}s.
1360 *
1361 * This is intended for {@link RemoteInput}s that only accept data, meaning
1362 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
Gustav Senntondf4c2442019-03-18 11:41:11 +00001363 * is null or empty, and {@link RemoteInput#getAllowedDataTypes} is non-null and not
Shane Brennan472a3b32016-12-12 15:28:10 -08001364 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1365 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1366 *
1367 * You can test if a RemoteInput matches these constraints using
1368 * {@link RemoteInput#isDataOnly}.
1369 */
1370 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1371
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001372 /**
1373 * {@link }: No semantic action defined.
1374 */
1375 public static final int SEMANTIC_ACTION_NONE = 0;
1376
1377 /**
1378 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1379 * may be appropriate.
1380 */
1381 public static final int SEMANTIC_ACTION_REPLY = 1;
1382
1383 /**
1384 * {@code SemanticAction}: Mark content as read.
1385 */
1386 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1387
1388 /**
1389 * {@code SemanticAction}: Mark content as unread.
1390 */
1391 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1392
1393 /**
1394 * {@code SemanticAction}: Delete the content associated with the notification. This
1395 * could mean deleting an email, message, etc.
1396 */
1397 public static final int SEMANTIC_ACTION_DELETE = 4;
1398
1399 /**
1400 * {@code SemanticAction}: Archive the content associated with the notification. This
1401 * could mean archiving an email, message, etc.
1402 */
1403 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1404
1405 /**
1406 * {@code SemanticAction}: Mute the content associated with the notification. This could
1407 * mean silencing a conversation or currently playing media.
1408 */
1409 public static final int SEMANTIC_ACTION_MUTE = 6;
1410
1411 /**
1412 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1413 * mean un-silencing a conversation or currently playing media.
1414 */
1415 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1416
1417 /**
1418 * {@code SemanticAction}: Mark content with a thumbs up.
1419 */
1420 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1421
1422 /**
1423 * {@code SemanticAction}: Mark content with a thumbs down.
1424 */
1425 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1426
Kodlee Yincda5b092018-02-15 15:34:53 -08001427 /**
1428 * {@code SemanticAction}: Call a contact, group, etc.
1429 */
1430 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001431
Griff Hazen959591e2014-05-15 22:26:18 -07001432 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001433 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001434 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001435 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001436 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001437 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001438 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001439
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001440 /**
1441 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001442 *
1443 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001444 */
Dan Sandler86647982015-05-13 23:41:13 -04001445 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001446 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001447
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001448 /**
1449 * Title of the action.
1450 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001451 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001452
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001453 /**
1454 * Intent to send when the user invokes this action. May be null, in which case the action
1455 * may be rendered in a disabled presentation by the system UI.
1456 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001457 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001458
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001459 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001460 if (in.readInt() != 0) {
1461 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001462 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1463 icon = mIcon.getResId();
1464 }
Dan Sandler86647982015-05-13 23:41:13 -04001465 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001466 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1467 if (in.readInt() == 1) {
1468 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1469 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001470 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001471 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001472 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001473 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001474 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001475 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001476
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001477 /**
Dan Sandler86647982015-05-13 23:41:13 -04001478 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001479 */
Dan Sandler86647982015-05-13 23:41:13 -04001480 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001481 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001482 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001483 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001484 }
1485
Adrian Roos7af53622016-10-12 13:44:05 -07001486 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001487 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001488 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001489 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001490 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001491 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1492 this.icon = icon.getResId();
1493 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001494 this.title = title;
1495 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001496 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001497 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001498 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001499 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001500 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001501 }
1502
1503 /**
Dan Sandler86647982015-05-13 23:41:13 -04001504 * Return an icon representing the action.
1505 */
1506 public Icon getIcon() {
1507 if (mIcon == null && icon != 0) {
1508 // you snuck an icon in here without using the builder; let's try to keep it
1509 mIcon = Icon.createWithResource("", icon);
1510 }
1511 return mIcon;
1512 }
1513
1514 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001515 * Get additional metadata carried around with this Action.
1516 */
1517 public Bundle getExtras() {
1518 return mExtras;
1519 }
1520
1521 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001522 * Return whether the platform should automatically generate possible replies for this
1523 * {@link Action}
1524 */
1525 public boolean getAllowGeneratedReplies() {
1526 return mAllowGeneratedReplies;
1527 }
1528
1529 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001530 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001531 * May return null if no remote inputs were added. Only returns inputs which accept
1532 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001533 */
1534 public RemoteInput[] getRemoteInputs() {
1535 return mRemoteInputs;
1536 }
1537
1538 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001539 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1540 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1541 * (eg. reply, mark as read, delete, etc).
1542 */
1543 public @SemanticAction int getSemanticAction() {
1544 return mSemanticAction;
1545 }
1546
1547 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001548 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1549 * notification message body. An example of a contextual action could be an action opening a
1550 * map application with an address shown in the notification.
1551 */
1552 public boolean isContextual() {
1553 return mIsContextual;
1554 }
1555
1556 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001557 * Get the list of inputs to be collected from the user that ONLY accept data when this
1558 * action is sent. These remote inputs are guaranteed to return true on a call to
1559 * {@link RemoteInput#isDataOnly}.
1560 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001561 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001562 *
1563 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1564 * of non-textual RemoteInputs do not access these remote inputs.
1565 */
1566 public RemoteInput[] getDataOnlyRemoteInputs() {
1567 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1568 }
1569
1570 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001571 * Builder class for {@link Action} objects.
1572 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001573 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001574 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001575 private final CharSequence mTitle;
1576 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001577 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001578 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001579 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001580 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001581 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001582
1583 /**
1584 * Construct a new builder for {@link Action} object.
1585 * @param icon icon to show for this action
1586 * @param title the title of the action
1587 * @param intent the {@link PendingIntent} to fire when users trigger this action
1588 */
Dan Sandler86647982015-05-13 23:41:13 -04001589 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001590 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001591 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001592 }
1593
1594 /**
1595 * Construct a new builder for {@link Action} object.
1596 * @param icon icon to show for this action
1597 * @param title the title of the action
1598 * @param intent the {@link PendingIntent} to fire when users trigger this action
1599 */
1600 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001601 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001602 }
1603
1604 /**
1605 * Construct a new builder for {@link Action} object using the fields from an
1606 * {@link Action}.
1607 * @param action the action to read fields from.
1608 */
1609 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001610 this(action.getIcon(), action.title, action.actionIntent,
1611 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001612 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001613 }
1614
Dan Sandler86647982015-05-13 23:41:13 -04001615 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001616 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1617 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001618 mIcon = icon;
1619 mTitle = title;
1620 mIntent = intent;
1621 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001622 if (remoteInputs != null) {
1623 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1624 Collections.addAll(mRemoteInputs, remoteInputs);
1625 }
Adrian Roos7af53622016-10-12 13:44:05 -07001626 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001627 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001628 }
1629
1630 /**
1631 * Merge additional metadata into this builder.
1632 *
1633 * <p>Values within the Bundle will replace existing extras values in this Builder.
1634 *
1635 * @see Notification.Action#extras
1636 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001637 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001638 public Builder addExtras(Bundle extras) {
1639 if (extras != null) {
1640 mExtras.putAll(extras);
1641 }
1642 return this;
1643 }
1644
1645 /**
1646 * Get the metadata Bundle used by this Builder.
1647 *
1648 * <p>The returned Bundle is shared with this Builder.
1649 */
1650 public Bundle getExtras() {
1651 return mExtras;
1652 }
1653
1654 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001655 * Add an input to be collected from the user when this action is sent.
1656 * Response values can be retrieved from the fired intent by using the
1657 * {@link RemoteInput#getResultsFromIntent} function.
1658 * @param remoteInput a {@link RemoteInput} to add to the action
1659 * @return this object for method chaining
1660 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001661 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001662 public Builder addRemoteInput(RemoteInput remoteInput) {
1663 if (mRemoteInputs == null) {
1664 mRemoteInputs = new ArrayList<RemoteInput>();
1665 }
1666 mRemoteInputs.add(remoteInput);
1667 return this;
1668 }
1669
1670 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001671 * Set whether the platform should automatically generate possible replies to add to
1672 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1673 * {@link RemoteInput}, this has no effect.
1674 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1675 * otherwise
1676 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001677 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001678 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001679 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001680 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1681 mAllowGeneratedReplies = allowGeneratedReplies;
1682 return this;
1683 }
1684
1685 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001686 * Sets the {@code SemanticAction} for this {@link Action}. A
1687 * {@code SemanticAction} denotes what an {@link Action}'s
1688 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1689 * @param semanticAction a SemanticAction defined within {@link Action} with
1690 * {@code SEMANTIC_ACTION_} prefixes
1691 * @return this object for method chaining
1692 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001693 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001694 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1695 mSemanticAction = semanticAction;
1696 return this;
1697 }
1698
1699 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001700 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1701 * dependent on the notification message body. An example of a contextual action could
1702 * be an action opening a map application with an address shown in the notification.
1703 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001704 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001705 public Builder setContextual(boolean isContextual) {
1706 mIsContextual = isContextual;
1707 return this;
1708 }
1709
1710 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001711 * Apply an extender to this action builder. Extenders may be used to add
1712 * metadata or change options on this builder.
1713 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001714 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001715 public Builder extend(Extender extender) {
1716 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001717 return this;
1718 }
1719
1720 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001721 * Throws an NPE if we are building a contextual action missing one of the fields
1722 * necessary to display the action.
1723 */
1724 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001725 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001726
1727 if (mIcon == null) {
1728 throw new NullPointerException("Contextual Actions must contain a valid icon");
1729 }
1730
1731 if (mIntent == null) {
1732 throw new NullPointerException(
1733 "Contextual Actions must contain a valid PendingIntent");
1734 }
1735 }
1736
1737 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001738 * Combine all of the options that have been set and return a new {@link Action}
1739 * object.
1740 * @return the built action
1741 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001742 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001743 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001744 checkContextualActionNullFields();
1745
Shane Brennan472a3b32016-12-12 15:28:10 -08001746 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1747 RemoteInput[] previousDataInputs =
1748 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001749 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001750 for (RemoteInput input : previousDataInputs) {
1751 dataOnlyInputs.add(input);
1752 }
1753 }
1754 List<RemoteInput> textInputs = new ArrayList<>();
1755 if (mRemoteInputs != null) {
1756 for (RemoteInput input : mRemoteInputs) {
1757 if (input.isDataOnly()) {
1758 dataOnlyInputs.add(input);
1759 } else {
1760 textInputs.add(input);
1761 }
1762 }
1763 }
1764 if (!dataOnlyInputs.isEmpty()) {
1765 RemoteInput[] dataInputsArr =
1766 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1767 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1768 }
1769 RemoteInput[] textInputsArr = textInputs.isEmpty()
1770 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1771 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001772 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001773 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001774 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001775
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001776 @Override
1777 public Action clone() {
1778 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001779 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001780 title,
1781 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001782 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001783 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001784 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001785 getSemanticAction(),
1786 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001787 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001788
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001789 @Override
1790 public int describeContents() {
1791 return 0;
1792 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001793
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001794 @Override
1795 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001796 final Icon ic = getIcon();
1797 if (ic != null) {
1798 out.writeInt(1);
1799 ic.writeToParcel(out, 0);
1800 } else {
1801 out.writeInt(0);
1802 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001803 TextUtils.writeToParcel(title, out, flags);
1804 if (actionIntent != null) {
1805 out.writeInt(1);
1806 actionIntent.writeToParcel(out, flags);
1807 } else {
1808 out.writeInt(0);
1809 }
Griff Hazen959591e2014-05-15 22:26:18 -07001810 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001811 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001812 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001813 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001814 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001815 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001816
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001817 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001818 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001819 public Action createFromParcel(Parcel in) {
1820 return new Action(in);
1821 }
1822 public Action[] newArray(int size) {
1823 return new Action[size];
1824 }
1825 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001826
1827 /**
1828 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1829 * metadata or change options on an action builder.
1830 */
1831 public interface Extender {
1832 /**
1833 * Apply this extender to a notification action builder.
1834 * @param builder the builder to be modified.
1835 * @return the build object for chaining.
1836 */
1837 public Builder extend(Builder builder);
1838 }
1839
1840 /**
1841 * Wearable extender for notification actions. To add extensions to an action,
1842 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1843 * the {@code WearableExtender()} constructor and apply it to a
1844 * {@link android.app.Notification.Action.Builder} using
1845 * {@link android.app.Notification.Action.Builder#extend}.
1846 *
1847 * <pre class="prettyprint">
1848 * Notification.Action action = new Notification.Action.Builder(
1849 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001850 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001851 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001852 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001853 */
1854 public static final class WearableExtender implements Extender {
1855 /** Notification action extra which contains wearable extensions */
1856 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1857
Pete Gastaf6781d2014-10-07 15:17:05 -04001858 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001859 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001860 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1861 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1862 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001863
1864 // Flags bitwise-ored to mFlags
1865 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001866 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001867 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001868
1869 // Default value for flags integer
1870 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1871
1872 private int mFlags = DEFAULT_FLAGS;
1873
Pete Gastaf6781d2014-10-07 15:17:05 -04001874 private CharSequence mInProgressLabel;
1875 private CharSequence mConfirmLabel;
1876 private CharSequence mCancelLabel;
1877
Griff Hazen61a9e862014-05-22 16:05:19 -07001878 /**
1879 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1880 * options.
1881 */
1882 public WearableExtender() {
1883 }
1884
1885 /**
1886 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1887 * wearable options present in an existing notification action.
1888 * @param action the notification action to inspect.
1889 */
1890 public WearableExtender(Action action) {
1891 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1892 if (wearableBundle != null) {
1893 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001894 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1895 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1896 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001897 }
1898 }
1899
1900 /**
1901 * Apply wearable extensions to a notification action that is being built. This is
1902 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1903 * method of {@link android.app.Notification.Action.Builder}.
1904 */
1905 @Override
1906 public Action.Builder extend(Action.Builder builder) {
1907 Bundle wearableBundle = new Bundle();
1908
1909 if (mFlags != DEFAULT_FLAGS) {
1910 wearableBundle.putInt(KEY_FLAGS, mFlags);
1911 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001912 if (mInProgressLabel != null) {
1913 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1914 }
1915 if (mConfirmLabel != null) {
1916 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1917 }
1918 if (mCancelLabel != null) {
1919 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1920 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001921
1922 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1923 return builder;
1924 }
1925
1926 @Override
1927 public WearableExtender clone() {
1928 WearableExtender that = new WearableExtender();
1929 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001930 that.mInProgressLabel = this.mInProgressLabel;
1931 that.mConfirmLabel = this.mConfirmLabel;
1932 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001933 return that;
1934 }
1935
1936 /**
1937 * Set whether this action is available when the wearable device is not connected to
1938 * a companion device. The user can still trigger this action when the wearable device is
1939 * offline, but a visual hint will indicate that the action may not be available.
1940 * Defaults to true.
1941 */
1942 public WearableExtender setAvailableOffline(boolean availableOffline) {
1943 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1944 return this;
1945 }
1946
1947 /**
1948 * Get whether this action is available when the wearable device is not connected to
1949 * a companion device. The user can still trigger this action when the wearable device is
1950 * offline, but a visual hint will indicate that the action may not be available.
1951 * Defaults to true.
1952 */
1953 public boolean isAvailableOffline() {
1954 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1955 }
1956
1957 private void setFlag(int mask, boolean value) {
1958 if (value) {
1959 mFlags |= mask;
1960 } else {
1961 mFlags &= ~mask;
1962 }
1963 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001964
1965 /**
1966 * Set a label to display while the wearable is preparing to automatically execute the
1967 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1968 *
1969 * @param label the label to display while the action is being prepared to execute
1970 * @return this object for method chaining
1971 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001972 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001973 public WearableExtender setInProgressLabel(CharSequence label) {
1974 mInProgressLabel = label;
1975 return this;
1976 }
1977
1978 /**
1979 * Get the label to display while the wearable is preparing to automatically execute
1980 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1981 *
1982 * @return the label to display while the action is being prepared to execute
1983 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001984 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001985 public CharSequence getInProgressLabel() {
1986 return mInProgressLabel;
1987 }
1988
1989 /**
1990 * Set a label to display to confirm that the action should be executed.
1991 * This is usually an imperative verb like "Send".
1992 *
1993 * @param label the label to confirm the action should be executed
1994 * @return this object for method chaining
1995 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001996 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001997 public WearableExtender setConfirmLabel(CharSequence label) {
1998 mConfirmLabel = label;
1999 return this;
2000 }
2001
2002 /**
2003 * Get the label to display to confirm that the action should be executed.
2004 * This is usually an imperative verb like "Send".
2005 *
2006 * @return the label to confirm the action should be executed
2007 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002008 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002009 public CharSequence getConfirmLabel() {
2010 return mConfirmLabel;
2011 }
2012
2013 /**
2014 * Set a label to display to cancel the action.
2015 * This is usually an imperative verb, like "Cancel".
2016 *
2017 * @param label the label to display to cancel the action
2018 * @return this object for method chaining
2019 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002020 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002021 public WearableExtender setCancelLabel(CharSequence label) {
2022 mCancelLabel = label;
2023 return this;
2024 }
2025
2026 /**
2027 * Get the label to display to cancel the action.
2028 * This is usually an imperative verb like "Cancel".
2029 *
2030 * @return the label to display to cancel the action
2031 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002032 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002033 public CharSequence getCancelLabel() {
2034 return mCancelLabel;
2035 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002036
2037 /**
2038 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2039 * platform that it can generate the appropriate transitions.
2040 * @param hintLaunchesActivity {@code true} if the content intent will launch
2041 * an activity and transitions should be generated, false otherwise.
2042 * @return this object for method chaining
2043 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002044 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002045 boolean hintLaunchesActivity) {
2046 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2047 return this;
2048 }
2049
2050 /**
2051 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2052 * platform that it can generate the appropriate transitions
2053 * @return {@code true} if the content intent will launch an activity and transitions
2054 * should be generated, false otherwise. The default value is {@code false} if this was
2055 * never set.
2056 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002057 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002058 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2059 }
Alex Hills9f087612016-06-07 09:08:59 -04002060
2061 /**
2062 * Set a hint that this Action should be displayed inline.
2063 *
2064 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2065 * otherwise
2066 * @return this object for method chaining
2067 */
2068 public WearableExtender setHintDisplayActionInline(
2069 boolean hintDisplayInline) {
2070 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2071 return this;
2072 }
2073
2074 /**
2075 * Get a hint that this Action should be displayed inline.
2076 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002077 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002078 * otherwise. The default value is {@code false} if this was never set.
2079 */
2080 public boolean getHintDisplayActionInline() {
2081 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2082 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002083 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002084
2085 /**
2086 * Provides meaning to an {@link Action} that hints at what the associated
2087 * {@link PendingIntent} will do. For example, an {@link Action} with a
2088 * {@link PendingIntent} that replies to a text message notification may have the
2089 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2090 *
2091 * @hide
2092 */
2093 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2094 SEMANTIC_ACTION_NONE,
2095 SEMANTIC_ACTION_REPLY,
2096 SEMANTIC_ACTION_MARK_AS_READ,
2097 SEMANTIC_ACTION_MARK_AS_UNREAD,
2098 SEMANTIC_ACTION_DELETE,
2099 SEMANTIC_ACTION_ARCHIVE,
2100 SEMANTIC_ACTION_MUTE,
2101 SEMANTIC_ACTION_UNMUTE,
2102 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002103 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002104 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002105 })
2106 @Retention(RetentionPolicy.SOURCE)
2107 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002108 }
2109
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002110 /**
2111 * Array of all {@link Action} structures attached to this notification by
2112 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2113 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2114 * interface for invoking actions.
2115 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002116 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002117
2118 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002119 * Replacement version of this notification whose content will be shown
2120 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2121 * and {@link #VISIBILITY_PUBLIC}.
2122 */
2123 public Notification publicVersion;
2124
2125 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002126 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002127 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 */
2129 public Notification()
2130 {
2131 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002132 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002133 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
2135
2136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 * @hide
2138 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002139 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 public Notification(Context context, int icon, CharSequence tickerText, long when,
2141 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2142 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002143 new Builder(context)
2144 .setWhen(when)
2145 .setSmallIcon(icon)
2146 .setTicker(tickerText)
2147 .setContentTitle(contentTitle)
2148 .setContentText(contentText)
2149 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2150 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
2152
2153 /**
2154 * Constructs a Notification object with the information needed to
2155 * have a status bar icon without the standard expanded view.
2156 *
2157 * @param icon The resource id of the icon to put in the status bar.
2158 * @param tickerText The text that flows by in the status bar when the notification first
2159 * activates.
2160 * @param when The time to show in the time field. In the System.currentTimeMillis
2161 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002162 *
2163 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002165 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 public Notification(int icon, CharSequence tickerText, long when)
2167 {
2168 this.icon = icon;
2169 this.tickerText = tickerText;
2170 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002171 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
2173
2174 /**
2175 * Unflatten the notification from a parcel.
2176 */
Svet Ganovddb94882016-06-23 19:55:24 -07002177 @SuppressWarnings("unchecked")
2178 public Notification(Parcel parcel) {
2179 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2180 // intents in extras are always written as the last entry.
2181 readFromParcelImpl(parcel);
2182 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002183 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002184 }
2185
2186 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 {
2188 int version = parcel.readInt();
2189
Adrian Roosfb921842017-10-26 14:49:56 +02002190 mWhitelistToken = parcel.readStrongBinder();
2191 if (mWhitelistToken == null) {
2192 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002193 }
2194 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002195 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002198 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002199 if (parcel.readInt() != 0) {
2200 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002201 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2202 icon = mSmallIcon.getResId();
2203 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 number = parcel.readInt();
2206 if (parcel.readInt() != 0) {
2207 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2208 }
2209 if (parcel.readInt() != 0) {
2210 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2211 }
2212 if (parcel.readInt() != 0) {
2213 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2214 }
2215 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002216 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002217 }
2218 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2220 }
Joe Onorato561d3852010-11-20 18:09:34 -08002221 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002222 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 defaults = parcel.readInt();
2225 flags = parcel.readInt();
2226 if (parcel.readInt() != 0) {
2227 sound = Uri.CREATOR.createFromParcel(parcel);
2228 }
2229
2230 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002231 if (parcel.readInt() != 0) {
2232 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 vibrate = parcel.createLongArray();
2235 ledARGB = parcel.readInt();
2236 ledOnMS = parcel.readInt();
2237 ledOffMS = parcel.readInt();
2238 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002239
2240 if (parcel.readInt() != 0) {
2241 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2242 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002243
2244 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002245
John Spurlockfd7f1e02014-03-18 16:41:57 -04002246 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002247
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002248 mGroupKey = parcel.readString();
2249
2250 mSortKey = parcel.readString();
2251
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002252 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002253 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002254
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002255 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2256
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002257 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002258 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2259 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002260
Chris Wren8fd39ec2014-02-27 17:43:26 -05002261 if (parcel.readInt() != 0) {
2262 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2263 }
2264
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002265 visibility = parcel.readInt();
2266
2267 if (parcel.readInt() != 0) {
2268 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2269 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002270
2271 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002272
2273 if (parcel.readInt() != 0) {
2274 mChannelId = parcel.readString();
2275 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002276 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002277
2278 if (parcel.readInt() != 0) {
2279 mShortcutId = parcel.readString();
2280 }
2281
Felipe Leme90205ef2019-03-05 09:59:52 -08002282 if (parcel.readInt() != 0) {
2283 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2284 }
2285
Julia Reynolds13d898c2017-02-02 12:22:05 -05002286 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002287
2288 if (parcel.readInt() != 0) {
2289 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2290 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002291
2292 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002293 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002294 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002295 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002296
2297 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 }
2299
Andy Stadler110988c2010-12-03 14:29:16 -08002300 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002301 public Notification clone() {
2302 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002303 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002304 return that;
2305 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002306
Daniel Sandler1a497d32013-04-18 14:52:45 -04002307 /**
2308 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2309 * of this into that.
2310 * @hide
2311 */
2312 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002313 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002314 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002315 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002316 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002317 that.number = this.number;
2318
2319 // PendingIntents are global, so there's no reason (or way) to clone them.
2320 that.contentIntent = this.contentIntent;
2321 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002322 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002323
2324 if (this.tickerText != null) {
2325 that.tickerText = this.tickerText.toString();
2326 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002327 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002328 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002329 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002330 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002331 that.contentView = this.contentView.clone();
2332 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002333 if (heavy && this.mLargeIcon != null) {
2334 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002335 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002336 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002337 that.sound = this.sound; // android.net.Uri is immutable
2338 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002339 if (this.audioAttributes != null) {
2340 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2341 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002342
2343 final long[] vibrate = this.vibrate;
2344 if (vibrate != null) {
2345 final int N = vibrate.length;
2346 final long[] vib = that.vibrate = new long[N];
2347 System.arraycopy(vibrate, 0, vib, 0, N);
2348 }
2349
2350 that.ledARGB = this.ledARGB;
2351 that.ledOnMS = this.ledOnMS;
2352 that.ledOffMS = this.ledOffMS;
2353 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002354
Joe Onorato18e69df2010-05-17 22:26:12 -07002355 that.flags = this.flags;
2356
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002357 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002358
John Spurlockfd7f1e02014-03-18 16:41:57 -04002359 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002360
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002361 that.mGroupKey = this.mGroupKey;
2362
2363 that.mSortKey = this.mSortKey;
2364
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002365 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002366 try {
2367 that.extras = new Bundle(this.extras);
2368 // will unparcel
2369 that.extras.size();
2370 } catch (BadParcelableException e) {
2371 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2372 that.extras = null;
2373 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002374 }
2375
Felipe Lemedd85da62016-06-28 11:29:54 -07002376 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2377 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002378 }
2379
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002380 if (this.actions != null) {
2381 that.actions = new Action[this.actions.length];
2382 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002383 if ( this.actions[i] != null) {
2384 that.actions[i] = this.actions[i].clone();
2385 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002386 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002387 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002388
Daniel Sandler1a497d32013-04-18 14:52:45 -04002389 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002390 that.bigContentView = this.bigContentView.clone();
2391 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002392
Chris Wren8fd39ec2014-02-27 17:43:26 -05002393 if (heavy && this.headsUpContentView != null) {
2394 that.headsUpContentView = this.headsUpContentView.clone();
2395 }
2396
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002397 that.visibility = this.visibility;
2398
2399 if (this.publicVersion != null) {
2400 that.publicVersion = new Notification();
2401 this.publicVersion.cloneInto(that.publicVersion, heavy);
2402 }
2403
Dan Sandler26e81cf2014-05-06 10:01:27 -04002404 that.color = this.color;
2405
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002406 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002407 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002408 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002409 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002410 that.mBadgeIcon = this.mBadgeIcon;
2411 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002412 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002413 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002414 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002415
Daniel Sandler1a497d32013-04-18 14:52:45 -04002416 if (!heavy) {
2417 that.lightenPayload(); // will clean out extras
2418 }
2419 }
2420
2421 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002422 * Note all {@link Uri} that are referenced internally, with the expectation
2423 * that Uri permission grants will need to be issued to ensure the recipient
2424 * of this object is able to render its contents.
2425 *
2426 * @hide
2427 */
2428 public void visitUris(@NonNull Consumer<Uri> visitor) {
2429 visitor.accept(sound);
2430
2431 if (tickerView != null) tickerView.visitUris(visitor);
2432 if (contentView != null) contentView.visitUris(visitor);
2433 if (bigContentView != null) bigContentView.visitUris(visitor);
2434 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2435
2436 if (extras != null) {
2437 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002438 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2439 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2440 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002441 }
2442
2443 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2444 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2445 if (!ArrayUtils.isEmpty(messages)) {
2446 for (MessagingStyle.Message message : MessagingStyle.Message
2447 .getMessagesFromBundleArray(messages)) {
2448 visitor.accept(message.getDataUri());
2449 }
2450 }
2451
2452 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2453 if (!ArrayUtils.isEmpty(historic)) {
2454 for (MessagingStyle.Message message : MessagingStyle.Message
2455 .getMessagesFromBundleArray(historic)) {
2456 visitor.accept(message.getDataUri());
2457 }
2458 }
2459 }
2460 }
2461
2462 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002463 * Removes heavyweight parts of the Notification object for archival or for sending to
2464 * listeners when the full contents are not necessary.
2465 * @hide
2466 */
2467 public final void lightenPayload() {
2468 tickerView = null;
2469 contentView = null;
2470 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002471 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002472 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002473 if (extras != null && !extras.isEmpty()) {
2474 final Set<String> keyset = extras.keySet();
2475 final int N = keyset.size();
2476 final String[] keys = keyset.toArray(new String[N]);
2477 for (int i=0; i<N; i++) {
2478 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002479 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2480 continue;
2481 }
Dan Sandler50128532015-12-08 15:42:41 -05002482 final Object obj = extras.get(key);
2483 if (obj != null &&
2484 ( obj instanceof Parcelable
2485 || obj instanceof Parcelable[]
2486 || obj instanceof SparseArray
2487 || obj instanceof ArrayList)) {
2488 extras.remove(key);
2489 }
2490 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002491 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002492 }
2493
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002494 /**
2495 * Make sure this CharSequence is safe to put into a bundle, which basically
2496 * means it had better not be some custom Parcelable implementation.
2497 * @hide
2498 */
2499 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002500 if (cs == null) return cs;
2501 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2502 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2503 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002504 if (cs instanceof Parcelable) {
2505 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2506 + " instance is a custom Parcelable and not allowed in Notification");
2507 return cs.toString();
2508 }
Selim Cinek60a54252016-02-26 17:03:25 -08002509 return removeTextSizeSpans(cs);
2510 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002511
Selim Cinek60a54252016-02-26 17:03:25 -08002512 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2513 if (charSequence instanceof Spanned) {
2514 Spanned ss = (Spanned) charSequence;
2515 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2516 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2517 for (Object span : spans) {
2518 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002519 if (resultSpan instanceof CharacterStyle) {
2520 resultSpan = ((CharacterStyle) span).getUnderlying();
2521 }
2522 if (resultSpan instanceof TextAppearanceSpan) {
2523 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002524 resultSpan = new TextAppearanceSpan(
2525 originalSpan.getFamily(),
2526 originalSpan.getTextStyle(),
2527 -1,
2528 originalSpan.getTextColor(),
2529 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002530 } else if (resultSpan instanceof RelativeSizeSpan
2531 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002532 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002533 } else {
2534 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002535 }
2536 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2537 ss.getSpanFlags(span));
2538 }
2539 return builder;
2540 }
2541 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002542 }
2543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 public int describeContents() {
2545 return 0;
2546 }
2547
2548 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002549 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 */
Svet Ganovddb94882016-06-23 19:55:24 -07002551 public void writeToParcel(Parcel parcel, int flags) {
2552 // We need to mark all pending intents getting into the notification
2553 // system as being put there to later allow the notification ranker
2554 // to launch them and by doing so add the app to the battery saver white
2555 // list for a short period of time. The problem is that the system
2556 // cannot look into the extras as there may be parcelables there that
2557 // the platform does not know how to handle. To go around that we have
2558 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002559 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002560 if (collectPendingIntents) {
2561 PendingIntent.setOnMarshaledListener(
2562 (PendingIntent intent, Parcel out, int outFlags) -> {
2563 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002564 if (allPendingIntents == null) {
2565 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002566 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002567 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002568 }
2569 });
2570 }
2571 try {
2572 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002573 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002574 writeToParcelImpl(parcel, flags);
2575 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002576 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002577 } finally {
2578 if (collectPendingIntents) {
2579 PendingIntent.setOnMarshaledListener(null);
2580 }
2581 }
2582 }
2583
2584 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 parcel.writeInt(1);
2586
Adrian Roosfb921842017-10-26 14:49:56 +02002587 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002589 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002590 if (mSmallIcon == null && icon != 0) {
2591 // you snuck an icon in here without using the builder; let's try to keep it
2592 mSmallIcon = Icon.createWithResource("", icon);
2593 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002594 if (mSmallIcon != null) {
2595 parcel.writeInt(1);
2596 mSmallIcon.writeToParcel(parcel, 0);
2597 } else {
2598 parcel.writeInt(0);
2599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 parcel.writeInt(number);
2601 if (contentIntent != null) {
2602 parcel.writeInt(1);
2603 contentIntent.writeToParcel(parcel, 0);
2604 } else {
2605 parcel.writeInt(0);
2606 }
2607 if (deleteIntent != null) {
2608 parcel.writeInt(1);
2609 deleteIntent.writeToParcel(parcel, 0);
2610 } else {
2611 parcel.writeInt(0);
2612 }
2613 if (tickerText != null) {
2614 parcel.writeInt(1);
2615 TextUtils.writeToParcel(tickerText, parcel, flags);
2616 } else {
2617 parcel.writeInt(0);
2618 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002619 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002620 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002621 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002622 } else {
2623 parcel.writeInt(0);
2624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 if (contentView != null) {
2626 parcel.writeInt(1);
2627 contentView.writeToParcel(parcel, 0);
2628 } else {
2629 parcel.writeInt(0);
2630 }
Selim Cinek279fa862016-06-14 10:57:25 -07002631 if (mLargeIcon == null && largeIcon != null) {
2632 // you snuck an icon in here without using the builder; let's try to keep it
2633 mLargeIcon = Icon.createWithBitmap(largeIcon);
2634 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002635 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002636 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002637 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002638 } else {
2639 parcel.writeInt(0);
2640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641
2642 parcel.writeInt(defaults);
2643 parcel.writeInt(this.flags);
2644
2645 if (sound != null) {
2646 parcel.writeInt(1);
2647 sound.writeToParcel(parcel, 0);
2648 } else {
2649 parcel.writeInt(0);
2650 }
2651 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002652
2653 if (audioAttributes != null) {
2654 parcel.writeInt(1);
2655 audioAttributes.writeToParcel(parcel, 0);
2656 } else {
2657 parcel.writeInt(0);
2658 }
2659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 parcel.writeLongArray(vibrate);
2661 parcel.writeInt(ledARGB);
2662 parcel.writeInt(ledOnMS);
2663 parcel.writeInt(ledOffMS);
2664 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002665
2666 if (fullScreenIntent != null) {
2667 parcel.writeInt(1);
2668 fullScreenIntent.writeToParcel(parcel, 0);
2669 } else {
2670 parcel.writeInt(0);
2671 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002672
2673 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002674
John Spurlockfd7f1e02014-03-18 16:41:57 -04002675 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002676
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002677 parcel.writeString(mGroupKey);
2678
2679 parcel.writeString(mSortKey);
2680
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002681 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002682
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002683 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002684
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002685 if (bigContentView != null) {
2686 parcel.writeInt(1);
2687 bigContentView.writeToParcel(parcel, 0);
2688 } else {
2689 parcel.writeInt(0);
2690 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002691
Chris Wren8fd39ec2014-02-27 17:43:26 -05002692 if (headsUpContentView != null) {
2693 parcel.writeInt(1);
2694 headsUpContentView.writeToParcel(parcel, 0);
2695 } else {
2696 parcel.writeInt(0);
2697 }
2698
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002699 parcel.writeInt(visibility);
2700
2701 if (publicVersion != null) {
2702 parcel.writeInt(1);
2703 publicVersion.writeToParcel(parcel, 0);
2704 } else {
2705 parcel.writeInt(0);
2706 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002707
2708 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002709
2710 if (mChannelId != null) {
2711 parcel.writeInt(1);
2712 parcel.writeString(mChannelId);
2713 } else {
2714 parcel.writeInt(0);
2715 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002716 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002717
2718 if (mShortcutId != null) {
2719 parcel.writeInt(1);
2720 parcel.writeString(mShortcutId);
2721 } else {
2722 parcel.writeInt(0);
2723 }
2724
Felipe Leme90205ef2019-03-05 09:59:52 -08002725 if (mLocusId != null) {
2726 parcel.writeInt(1);
2727 mLocusId.writeToParcel(parcel, 0);
2728 } else {
2729 parcel.writeInt(0);
2730 }
2731
Julia Reynolds13d898c2017-02-02 12:22:05 -05002732 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002733
2734 if (mSettingsText != null) {
2735 parcel.writeInt(1);
2736 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2737 } else {
2738 parcel.writeInt(0);
2739 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002740
2741 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002742
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002743 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002744 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002745 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002746 } else {
2747 parcel.writeInt(0);
2748 }
2749
Gustav Sennton761884c2018-11-19 17:40:19 +00002750 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2751
Julia Reynoldsfc640012018-02-21 12:25:27 -05002752 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
2754
2755 /**
2756 * Parcelable.Creator that instantiates Notification objects
2757 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002758 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 = new Parcelable.Creator<Notification>()
2760 {
2761 public Notification createFromParcel(Parcel parcel)
2762 {
2763 return new Notification(parcel);
2764 }
2765
2766 public Notification[] newArray(int size)
2767 {
2768 return new Notification[size];
2769 }
2770 };
2771
2772 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002773 * @hide
2774 */
2775 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2776 Notification.Action[] firstAs = first.actions;
2777 Notification.Action[] secondAs = second.actions;
2778 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2779 return true;
2780 }
2781 if (firstAs != null && secondAs != null) {
2782 if (firstAs.length != secondAs.length) {
2783 return true;
2784 }
2785 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002786 if (!Objects.equals(String.valueOf(firstAs[i].title),
2787 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002788 return true;
2789 }
2790 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2791 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2792 if (firstRs == null) {
2793 firstRs = new RemoteInput[0];
2794 }
2795 if (secondRs == null) {
2796 secondRs = new RemoteInput[0];
2797 }
2798 if (firstRs.length != secondRs.length) {
2799 return true;
2800 }
2801 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002802 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2803 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002804 return true;
2805 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002806 }
2807 }
2808 }
2809 return false;
2810 }
2811
2812 /**
2813 * @hide
2814 */
2815 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2816 if (first.getStyle() == null) {
2817 return second.getStyle() != null;
2818 }
2819 if (second.getStyle() == null) {
2820 return true;
2821 }
2822 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2823 }
2824
2825 /**
2826 * @hide
2827 */
2828 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002829 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2830 return true;
2831 }
2832
2833 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2834 return true;
2835 }
2836 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2837 return true;
2838 }
2839 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2840 return true;
2841 }
2842
2843 return false;
2844 }
2845
2846 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2847 if (first == null && second == null) {
2848 return false;
2849 }
2850 if (first == null && second != null || first != null && second == null) {
2851 return true;
2852 }
2853
2854 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2855 return true;
2856 }
2857
2858 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2859 return true;
2860 }
2861
2862 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002863 }
2864
2865 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002866 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2867 * <p>
2868 * For backwards compatibility {@code extras} holds some references to "real" member data such
2869 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2870 * fine as long as the object stays in one process.
2871 * <p>
2872 * However, once the notification goes into a parcel each reference gets marshalled separately,
2873 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2874 */
2875 private void fixDuplicateExtras() {
2876 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002877 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2878 }
2879 }
2880
2881 /**
2882 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2883 * separate object, replace it with the field's version to avoid holding duplicate copies.
2884 */
2885 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2886 if (original != null && extras.getParcelable(extraName) != null) {
2887 extras.putParcelable(extraName, original);
2888 }
2889 }
2890
2891 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2893 * layout.
2894 *
2895 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2896 * in the view.</p>
2897 * @param context The context for your application / activity.
2898 * @param contentTitle The title that goes in the expanded entry.
2899 * @param contentText The text that goes in the expanded entry.
2900 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2901 * If this is an activity, it must include the
2902 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002903 * that you take care of task management as described in the
2904 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2905 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002906 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002907 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002908 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002910 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 public void setLatestEventInfo(Context context,
2912 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002913 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2914 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2915 new Throwable());
2916 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002917
Selim Cinek4ac6f602016-06-13 15:47:03 -07002918 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2919 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2920 }
2921
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002922 // ensure that any information already set directly is preserved
2923 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002924
2925 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002927 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
2929 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002930 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002932 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002933
2934 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
2936
Julia Reynoldsda303542015-11-23 14:00:20 -05002937 /**
2938 * @hide
2939 */
2940 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002941 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002942 }
2943
2944 /**
2945 * @hide
2946 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002947 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002948 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002949 }
2950
Yi Jin6b514142017-10-30 14:54:12 -07002951 /**
2952 * @hide
2953 */
2954 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2955 long token = proto.start(fieldId);
2956 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2957 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2958 proto.write(NotificationProto.FLAGS, this.flags);
2959 proto.write(NotificationProto.COLOR, this.color);
2960 proto.write(NotificationProto.CATEGORY, this.category);
2961 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2962 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2963 if (this.actions != null) {
2964 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2965 }
2966 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2967 proto.write(NotificationProto.VISIBILITY, this.visibility);
2968 }
2969 if (publicVersion != null) {
2970 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2971 }
2972 proto.end(token);
2973 }
2974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 @Override
2976 public String toString() {
2977 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002978 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002979 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002980 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002981 sb.append(priority);
2982 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002983 if (contentView != null) {
2984 sb.append(contentView.getPackage());
2985 sb.append("/0x");
2986 sb.append(Integer.toHexString(contentView.getLayoutId()));
2987 } else {
2988 sb.append("null");
2989 }
2990 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002991 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2992 sb.append("default");
2993 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 int N = this.vibrate.length-1;
2995 sb.append("[");
2996 for (int i=0; i<N; i++) {
2997 sb.append(this.vibrate[i]);
2998 sb.append(',');
2999 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02003000 if (N != -1) {
3001 sb.append(this.vibrate[N]);
3002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 } else {
3005 sb.append("null");
3006 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003007 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003008 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003010 } else if (this.sound != null) {
3011 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 } else {
3013 sb.append("null");
3014 }
Chris Wren365b6d32015-07-16 10:39:26 -04003015 if (this.tickerText != null) {
3016 sb.append(" tick");
3017 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003018 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003020 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003021 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003022 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003023 if (this.category != null) {
3024 sb.append(" category=");
3025 sb.append(this.category);
3026 }
3027 if (this.mGroupKey != null) {
3028 sb.append(" groupKey=");
3029 sb.append(this.mGroupKey);
3030 }
3031 if (this.mSortKey != null) {
3032 sb.append(" sortKey=");
3033 sb.append(this.mSortKey);
3034 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003035 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003036 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003037 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003038 }
3039 sb.append(" vis=");
3040 sb.append(visibilityToString(this.visibility));
3041 if (this.publicVersion != null) {
3042 sb.append(" publicVersion=");
3043 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003044 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003045 if (this.mLocusId != null) {
3046 sb.append(" locusId=");
3047 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3048 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003049 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 return sb.toString();
3051 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003052
Dan Sandler1b718782014-07-18 12:43:45 -04003053 /**
3054 * {@hide}
3055 */
3056 public static String visibilityToString(int vis) {
3057 switch (vis) {
3058 case VISIBILITY_PRIVATE:
3059 return "PRIVATE";
3060 case VISIBILITY_PUBLIC:
3061 return "PUBLIC";
3062 case VISIBILITY_SECRET:
3063 return "SECRET";
3064 default:
3065 return "UNKNOWN(" + String.valueOf(vis) + ")";
3066 }
3067 }
3068
Joe Onoratocb109a02011-01-18 17:57:41 -08003069 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003070 * {@hide}
3071 */
3072 public static String priorityToString(@Priority int pri) {
3073 switch (pri) {
3074 case PRIORITY_MIN:
3075 return "MIN";
3076 case PRIORITY_LOW:
3077 return "LOW";
3078 case PRIORITY_DEFAULT:
3079 return "DEFAULT";
3080 case PRIORITY_HIGH:
3081 return "HIGH";
3082 case PRIORITY_MAX:
3083 return "MAX";
3084 default:
3085 return "UNKNOWN(" + String.valueOf(pri) + ")";
3086 }
3087 }
3088
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003089 /**
3090 * @hide
3091 */
3092 public boolean hasCompletedProgress() {
3093 // not a progress notification; can't be complete
3094 if (!extras.containsKey(EXTRA_PROGRESS)
3095 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3096 return false;
3097 }
3098 // many apps use max 0 for 'indeterminate'; not complete
3099 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3100 return false;
3101 }
3102 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3103 }
3104
Jeff Sharkey000ce802017-04-29 13:13:27 -06003105 /** @removed */
3106 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003107 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003108 return mChannelId;
3109 }
3110
3111 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003112 * Returns the id of the channel this notification posts to.
3113 */
3114 public String getChannelId() {
3115 return mChannelId;
3116 }
3117
Jeff Sharkey000ce802017-04-29 13:13:27 -06003118 /** @removed */
3119 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003120 public long getTimeout() {
3121 return mTimeout;
3122 }
3123
3124 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003125 * Returns the duration from posting after which this notification should be canceled by the
3126 * system, if it's not canceled already.
3127 */
3128 public long getTimeoutAfter() {
3129 return mTimeout;
3130 }
3131
3132 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003133 * Returns what icon should be shown for this notification if it is being displayed in a
3134 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3135 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3136 */
3137 public int getBadgeIconType() {
3138 return mBadgeIcon;
3139 }
3140
3141 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003142 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003143 *
3144 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3145 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003146 */
3147 public String getShortcutId() {
3148 return mShortcutId;
3149 }
3150
Felipe Leme90205ef2019-03-05 09:59:52 -08003151 /**
3152 * Gets the {@link LocusId} associated with this notification.
3153 *
3154 * <p>Used by the device's intelligence services to correlate objects (such as
3155 * {@link ShortcutInfo} and {@link ContentCaptureContext}) that are correlated.
3156 */
3157 @Nullable
3158 public LocusId getLocusId() {
3159 return mLocusId;
3160 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003161
3162 /**
3163 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3164 */
3165 public CharSequence getSettingsText() {
3166 return mSettingsText;
3167 }
3168
Julia Reynolds13d898c2017-02-02 12:22:05 -05003169 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003170 * Returns which type of notifications in a group are responsible for audibly alerting the
3171 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3172 * {@link #GROUP_ALERT_SUMMARY}.
3173 */
3174 public @GroupAlertBehavior int getGroupAlertBehavior() {
3175 return mGroupAlertBehavior;
3176 }
3177
3178 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003179 * Returns the bubble metadata that will be used to display app content in a floating window
3180 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003181 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003182 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003183 public BubbleMetadata getBubbleMetadata() {
3184 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003185 }
3186
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003187 /**
3188 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3189 * for this notification.
3190 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003191 public boolean getAllowSystemGeneratedContextualActions() {
3192 return mAllowSystemGeneratedContextualActions;
3193 }
3194
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003195 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003196 * The small icon representing this notification in the status bar and content view.
3197 *
3198 * @return the small icon representing this notification.
3199 *
3200 * @see Builder#getSmallIcon()
3201 * @see Builder#setSmallIcon(Icon)
3202 */
3203 public Icon getSmallIcon() {
3204 return mSmallIcon;
3205 }
3206
3207 /**
3208 * Used when notifying to clean up legacy small icons.
3209 * @hide
3210 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003211 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003212 public void setSmallIcon(Icon icon) {
3213 mSmallIcon = icon;
3214 }
3215
3216 /**
3217 * The large icon shown in this notification's content view.
3218 * @see Builder#getLargeIcon()
3219 * @see Builder#setLargeIcon(Icon)
3220 */
3221 public Icon getLargeIcon() {
3222 return mLargeIcon;
3223 }
3224
3225 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003226 * @hide
3227 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003228 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003229 public boolean isGroupSummary() {
3230 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3231 }
3232
3233 /**
3234 * @hide
3235 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003236 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003237 public boolean isGroupChild() {
3238 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3239 }
3240
3241 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003242 * @hide
3243 */
3244 public boolean suppressAlertingDueToGrouping() {
3245 if (isGroupSummary()
3246 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3247 return true;
3248 } else if (isGroupChild()
3249 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3250 return true;
3251 }
3252 return false;
3253 }
3254
Tony Mak638430e2018-10-08 19:19:10 +01003255
3256 /**
3257 * Finds and returns a remote input and its corresponding action.
3258 *
3259 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3260 * @return the result pair, {@code null} if no result is found.
3261 *
3262 * @hide
3263 */
3264 @Nullable
3265 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3266 if (actions == null) {
3267 return null;
3268 }
3269 for (Notification.Action action : actions) {
3270 if (action.getRemoteInputs() == null) {
3271 continue;
3272 }
3273 RemoteInput resultRemoteInput = null;
3274 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3275 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3276 resultRemoteInput = remoteInput;
3277 }
3278 }
3279 if (resultRemoteInput != null) {
3280 return Pair.create(resultRemoteInput, action);
3281 }
3282 }
3283 return null;
3284 }
3285
Julia Reynolds30203152017-05-26 13:36:31 -04003286 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003287 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003288 *
3289 * @hide
3290 */
3291 public List<Notification.Action> getContextualActions() {
3292 if (actions == null) return Collections.emptyList();
3293
3294 List<Notification.Action> contextualActions = new ArrayList<>();
3295 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003296 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003297 contextualActions.add(action);
3298 }
3299 }
3300 return contextualActions;
3301 }
3302
3303 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003304 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003305 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003306 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003307 * content views using the platform's notification layout template. If your app supports
3308 * versions of Android as old as API level 4, you can instead use
3309 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3310 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3311 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003312 *
Scott Main183bf112012-08-13 19:12:13 -07003313 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003314 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003315 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003316 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003317 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3318 * .setContentText(subject)
3319 * .setSmallIcon(R.drawable.new_mail)
3320 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003321 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003322 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003323 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003324 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003325 /**
3326 * @hide
3327 */
3328 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3329 "android.rebuild.contentViewActionCount";
3330 /**
3331 * @hide
3332 */
3333 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3334 = "android.rebuild.bigViewActionCount";
3335 /**
3336 * @hide
3337 */
3338 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3339 = "android.rebuild.hudViewActionCount";
3340
Selim Cinek6743c0b2017-01-18 18:24:01 -08003341 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3342 SystemProperties.getBoolean("notifications.only_title", true);
3343
Selim Cinek389edcd2017-05-11 19:16:44 -07003344 /**
3345 * The lightness difference that has to be added to the primary text color to obtain the
3346 * secondary text color when the background is light.
3347 */
3348 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3349
3350 /**
3351 * The lightness difference that has to be added to the primary text color to obtain the
3352 * secondary text color when the background is dark.
3353 * A bit less then the above value, since it looks better on dark backgrounds.
3354 */
3355 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3356
Joe Onorato46439ce2010-11-19 13:56:21 -08003357 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003358 private Notification mN;
3359 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003360 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003361 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003362 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003363 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003364 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003365 private boolean mIsLegacy;
3366 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003367
3368 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003369 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3370 */
3371 private int mCachedContrastColor = COLOR_INVALID;
3372 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003373 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003374 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003375 */
3376 private int mCachedAmbientColor = COLOR_INVALID;
3377 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003378 /**
3379 * A neutral color color that can be used for icons.
3380 */
3381 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003382
3383 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003384 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3385 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3386 */
3387 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003388 private int mTextColorsAreForBackground = COLOR_INVALID;
3389 private int mPrimaryTextColor = COLOR_INVALID;
3390 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003391 private int mBackgroundColor = COLOR_INVALID;
3392 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003393 /**
3394 * A temporary location where actions are stored. If != null the view originally has action
3395 * but doesn't have any for this inflation.
3396 */
3397 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003398 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003399
Anthony Chenad4d1582017-04-10 16:07:58 -07003400 private boolean mTintActionButtons;
3401 private boolean mInNightMode;
3402
Adrian Roos70d7aa32017-01-11 15:39:06 -08003403 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003404 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003405 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003406 * @param context
3407 * A {@link Context} that will be used by the Builder to construct the
3408 * RemoteViews. The Context will not be held past the lifetime of this Builder
3409 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003410 * @param channelId
3411 * The constructed Notification will be posted on this
3412 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3413 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003414 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003415 public Builder(Context context, String channelId) {
3416 this(context, (Notification) null);
3417 mN.mChannelId = channelId;
3418 }
3419
3420 /**
3421 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3422 * instead. All posted Notifications must specify a NotificationChannel Id.
3423 */
3424 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003425 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003426 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003427 }
3428
Joe Onoratocb109a02011-01-18 17:57:41 -08003429 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003430 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003431 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003432 public Builder(Context context, Notification toAdopt) {
3433 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003434 Resources res = mContext.getResources();
3435 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3436
3437 if (res.getBoolean(R.bool.config_enableNightMode)) {
3438 Configuration currentConfig = res.getConfiguration();
3439 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3440 == Configuration.UI_MODE_NIGHT_YES;
3441 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003442
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003443 if (toAdopt == null) {
3444 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003445 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3446 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3447 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003448 mN.priority = PRIORITY_DEFAULT;
3449 mN.visibility = VISIBILITY_PRIVATE;
3450 } else {
3451 mN = toAdopt;
3452 if (mN.actions != null) {
3453 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003454 }
3455
Selim Cineke7238dd2017-12-14 17:48:32 -08003456 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3457 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3458 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003459 }
3460
Selim Cinek4ac6f602016-06-13 15:47:03 -07003461 if (mN.getSmallIcon() == null && mN.icon != 0) {
3462 setSmallIcon(mN.icon);
3463 }
3464
3465 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3466 setLargeIcon(mN.largeIcon);
3467 }
3468
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003469 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3470 if (!TextUtils.isEmpty(templateClass)) {
3471 final Class<? extends Style> styleClass
3472 = getNotificationStyleClass(templateClass);
3473 if (styleClass == null) {
3474 Log.d(TAG, "Unknown style class: " + templateClass);
3475 } else {
3476 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003477 final Constructor<? extends Style> ctor =
3478 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003479 ctor.setAccessible(true);
3480 final Style style = ctor.newInstance();
3481 style.restoreFromExtras(mN.extras);
3482
3483 if (style != null) {
3484 setStyle(style);
3485 }
3486 } catch (Throwable t) {
3487 Log.e(TAG, "Could not create Style", t);
3488 }
3489 }
3490 }
3491
3492 }
3493 }
3494
Lucas Dupina291d192018-06-07 13:59:42 -07003495 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003496 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003497 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003498 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003499 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003500 }
3501
3502 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003503 * If this notification is duplicative of a Launcher shortcut, sets the
3504 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3505 * the shortcut.
3506 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003507 * This field will be ignored by Launchers that don't support badging, don't show
3508 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003509 *
3510 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3511 * supersedes
3512 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003513 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003514 public Builder setShortcutId(String shortcutId) {
3515 mN.mShortcutId = shortcutId;
3516 return this;
3517 }
3518
3519 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003520 * Sets the {@link LocusId} associated with this notification.
3521 *
3522 * <p>This method should be called when the {@link LocusId} is used in other places (such
3523 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the device's intelligence
3524 * services can correlate them.
3525 */
3526 @NonNull
3527 public Builder setLocusId(@Nullable LocusId locusId) {
3528 mN.mLocusId = locusId;
3529 return this;
3530 }
3531
3532 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003533 * Sets which icon to display as a badge for this notification.
3534 *
3535 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3536 * {@link #BADGE_ICON_LARGE}.
3537 *
3538 * Note: This value might be ignored, for launchers that don't support badge icons.
3539 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003540 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003541 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003542 mN.mBadgeIcon = icon;
3543 return this;
3544 }
3545
3546 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003547 * Sets the group alert behavior for this notification. Use this method to mute this
3548 * notification if alerts for this notification's group should be handled by a different
3549 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003550 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3551 * mute. For example, if you want only the summary of your group to make noise, all
3552 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003553 *
3554 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3555 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003556 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003557 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3558 mN.mGroupAlertBehavior = groupAlertBehavior;
3559 return this;
3560 }
3561
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003562 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003563 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3564 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003565 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003566 * <p>This data will be ignored unless the notification is posted to a channel that
3567 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003568 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003569 * <b>Notifications with a valid and allowed bubble metadata will display in collapsed state
3570 * outside of the notification shade on unlocked devices. When a user interacts with the
3571 * collapsed state, the bubble intent will be invoked and displayed.</b>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003572 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003573 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003574 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003575 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003576 return this;
3577 }
3578
Jeff Sharkey000ce802017-04-29 13:13:27 -06003579 /** @removed */
3580 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003581 public Builder setChannel(String channelId) {
3582 mN.mChannelId = channelId;
3583 return this;
3584 }
3585
3586 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003587 * Specifies the channel the notification should be delivered on.
3588 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003589 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003590 public Builder setChannelId(String channelId) {
3591 mN.mChannelId = channelId;
3592 return this;
3593 }
3594
Jeff Sharkey000ce802017-04-29 13:13:27 -06003595 /** @removed */
3596 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003597 public Builder setTimeout(long durationMs) {
3598 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003599 return this;
3600 }
3601
3602 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003603 * Specifies a duration in milliseconds after which this notification should be canceled,
3604 * if it is not already canceled.
3605 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003606 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003607 public Builder setTimeoutAfter(long durationMs) {
3608 mN.mTimeout = durationMs;
3609 return this;
3610 }
3611
3612 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003613 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003614 *
3615 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3616 * shown anymore by default and must be opted into by using
3617 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003618 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003619 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003620 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003621 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003622 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003623 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003624 return this;
3625 }
3626
Joe Onoratocb109a02011-01-18 17:57:41 -08003627 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003628 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003629 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003630 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3631 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003632 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003633 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003634 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003636 return this;
3637 }
3638
3639 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003640 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003641 *
3642 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003643 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003644 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003645 * Useful when showing an elapsed time (like an ongoing phone call).
3646 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003647 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003648 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003649 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003650 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003651 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003652 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003653 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003654 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003655 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003656 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003657 return this;
3658 }
3659
3660 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003661 * Sets the Chronometer to count down instead of counting up.
3662 *
3663 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3664 * If it isn't set the chronometer will count up.
3665 *
3666 * @see #setUsesChronometer(boolean)
3667 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003668 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003669 public Builder setChronometerCountDown(boolean countDown) {
3670 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003671 return this;
3672 }
3673
3674 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003675 * Set the small icon resource, which will be used to represent the notification in the
3676 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003678
3679 * The platform template for the expanded view will draw this icon in the left, unless a
3680 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3681 * icon will be moved to the right-hand side.
3682 *
3683
3684 * @param icon
3685 * A resource ID in the application's package of the drawable to use.
3686 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003687 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003688 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003689 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003690 return setSmallIcon(icon != 0
3691 ? Icon.createWithResource(mContext, icon)
3692 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003693 }
3694
Joe Onoratocb109a02011-01-18 17:57:41 -08003695 /**
3696 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3697 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3698 * LevelListDrawable}.
3699 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003700 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003701 * @param level The level to use for the icon.
3702 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003703 * @see Notification#icon
3704 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003705 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003706 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003707 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003708 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003709 return setSmallIcon(icon);
3710 }
3711
3712 /**
3713 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003714 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003715 * {@link #setLargeIcon(Bitmap) large icon}).
3716 *
3717 * @param icon An Icon object to use.
3718 * @see Notification#icon
3719 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003720 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003721 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003722 mN.setSmallIcon(icon);
3723 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3724 mN.icon = icon.getResId();
3725 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003726 return this;
3727 }
3728
Joe Onoratocb109a02011-01-18 17:57:41 -08003729 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003730 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003731 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003732 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003733 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003734 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003735 return this;
3736 }
3737
Joe Onoratocb109a02011-01-18 17:57:41 -08003738 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003739 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003740 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003741 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003742 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003743 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003744 return this;
3745 }
3746
Joe Onoratocb109a02011-01-18 17:57:41 -08003747 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003748 * This provides some additional information that is displayed in the notification. No
3749 * guarantees are given where exactly it is displayed.
3750 *
3751 * <p>This information should only be provided if it provides an essential
3752 * benefit to the understanding of the notification. The more text you provide the
3753 * less readable it becomes. For example, an email client should only provide the account
3754 * name here if more than one email account has been added.</p>
3755 *
3756 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3757 * notification header area.
3758 *
3759 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3760 * this will be shown in the third line of text in the platform notification template.
3761 * You should not be using {@link #setProgress(int, int, boolean)} at the
3762 * same time on those versions; they occupy the same place.
3763 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003764 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003765 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003766 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003767 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003768 return this;
3769 }
3770
3771 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003772 * Provides text that will appear as a link to your application's settings.
3773 *
3774 * <p>This text does not appear within notification {@link Style templates} but may
3775 * appear when the user uses an affordance to learn more about the notification.
3776 * Additionally, this text will not appear unless you provide a valid link target by
3777 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3778 *
3779 * <p>This text is meant to be concise description about what the user can customize
3780 * when they click on this link. The recommended maximum length is 40 characters.
3781 * @param text
3782 * @return
3783 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003784 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003785 public Builder setSettingsText(CharSequence text) {
3786 mN.mSettingsText = safeCharSequence(text);
3787 return this;
3788 }
3789
3790 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003791 * Set the remote input history.
3792 *
3793 * This should be set to the most recent inputs that have been sent
3794 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3795 * longer relevant (e.g. for chat notifications once the other party has responded).
3796 *
3797 * The most recent input must be stored at the 0 index, the second most recent at the
3798 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3799 * and how much of each individual input is shown.
3800 *
3801 * <p>Note: The reply text will only be shown on notifications that have least one action
3802 * with a {@code RemoteInput}.</p>
3803 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003804 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003805 public Builder setRemoteInputHistory(CharSequence[] text) {
3806 if (text == null) {
3807 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3808 } else {
3809 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3810 CharSequence[] safe = new CharSequence[N];
3811 for (int i = 0; i < N; i++) {
3812 safe[i] = safeCharSequence(text[i]);
3813 }
3814 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3815 }
3816 return this;
3817 }
3818
3819 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003820 * Sets whether remote history entries view should have a spinner.
3821 * @hide
3822 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003823 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003824 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3825 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3826 return this;
3827 }
3828
3829 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003830 * Sets whether smart reply buttons should be hidden.
3831 * @hide
3832 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003833 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003834 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3835 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3836 return this;
3837 }
3838
3839 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003840 * Sets the number of items this notification represents. May be displayed as a badge count
3841 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003842 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003843 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003844 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003845 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003846 return this;
3847 }
3848
Joe Onoratocb109a02011-01-18 17:57:41 -08003849 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003850 * A small piece of additional information pertaining to this notification.
3851 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003852 * The platform template will draw this on the last line of the notification, at the far
3853 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003854 *
3855 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3856 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3857 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003858 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003859 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003860 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003861 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003862 return this;
3863 }
3864
Joe Onoratocb109a02011-01-18 17:57:41 -08003865 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003866 * Set the progress this notification represents.
3867 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003868 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003869 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003870 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003871 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003872 mN.extras.putInt(EXTRA_PROGRESS, progress);
3873 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3874 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003875 return this;
3876 }
3877
3878 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003879 * Supply a custom RemoteViews to use instead of the platform template.
3880 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003881 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003882 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003883 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003884 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003885 return setCustomContentView(views);
3886 }
3887
3888 /**
3889 * Supply custom RemoteViews to use instead of the platform template.
3890 *
3891 * This will override the layout that would otherwise be constructed by this Builder
3892 * object.
3893 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003894 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003895 public Builder setCustomContentView(RemoteViews contentView) {
3896 mN.contentView = contentView;
3897 return this;
3898 }
3899
3900 /**
3901 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3902 *
3903 * This will override the expanded layout that would otherwise be constructed by this
3904 * Builder object.
3905 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003906 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 public Builder setCustomBigContentView(RemoteViews contentView) {
3908 mN.bigContentView = contentView;
3909 return this;
3910 }
3911
3912 /**
3913 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3914 *
3915 * This will override the heads-up layout that would otherwise be constructed by this
3916 * Builder object.
3917 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003918 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003919 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3920 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003921 return this;
3922 }
3923
Joe Onoratocb109a02011-01-18 17:57:41 -08003924 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003925 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3926 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003927 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3928 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3929 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003930 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003931 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003932 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003933 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003934 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003935 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003936 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003937 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003938 return this;
3939 }
3940
Joe Onoratocb109a02011-01-18 17:57:41 -08003941 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003942 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3943 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003944 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003945 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003946 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003947 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003948 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003949 return this;
3950 }
3951
Joe Onoratocb109a02011-01-18 17:57:41 -08003952 /**
3953 * An intent to launch instead of posting the notification to the status bar.
3954 * Only for use with extremely high-priority notifications demanding the user's
3955 * <strong>immediate</strong> attention, such as an incoming phone call or
3956 * alarm clock that the user has explicitly set to a particular time.
3957 * If this facility is used for something else, please give the user an option
3958 * to turn it off and use a normal notification, as this can be extremely
3959 * disruptive.
3960 *
Chris Wren47c20a12014-06-18 17:27:29 -04003961 * <p>
3962 * The system UI may choose to display a heads-up notification, instead of
3963 * launching this intent, while the user is using the device.
3964 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003965 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3966 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3967 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003968 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003969 * @param intent The pending intent to launch.
3970 * @param highPriority Passing true will cause this notification to be sent
3971 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003972 *
3973 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003974 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003975 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003976 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003977 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003978 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3979 return this;
3980 }
3981
Joe Onoratocb109a02011-01-18 17:57:41 -08003982 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003983 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003984 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003985 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003986 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003987 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003988 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003990 return this;
3991 }
3992
Joe Onoratocb109a02011-01-18 17:57:41 -08003993 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003994 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003995 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003996 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003997 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003998 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003999 setTicker(tickerText);
4000 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004001 return this;
4002 }
4003
Joe Onoratocb109a02011-01-18 17:57:41 -08004004 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004005 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004006 *
4007 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004008 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4009 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004010 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004011 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004012 public Builder setLargeIcon(Bitmap b) {
4013 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4014 }
4015
4016 /**
4017 * Add a large icon to the notification content view.
4018 *
4019 * In the platform template, this image will be shown on the left of the notification view
4020 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4021 * badge atop the large icon).
4022 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004023 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004024 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004025 mN.mLargeIcon = icon;
4026 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004027 return this;
4028 }
4029
Joe Onoratocb109a02011-01-18 17:57:41 -08004030 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004031 * Set the sound to play.
4032 *
John Spurlockc0650f022014-07-19 13:22:39 -04004033 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4034 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004035 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004036 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004037 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004038 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004039 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004040 mN.sound = sound;
4041 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004042 return this;
4043 }
4044
Joe Onoratocb109a02011-01-18 17:57:41 -08004045 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004046 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004047 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004048 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4049 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004050 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004051 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004052 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004053 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004054 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004055 mN.sound = sound;
4056 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004057 return this;
4058 }
4059
Joe Onoratocb109a02011-01-18 17:57:41 -08004060 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004061 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4062 * use during playback.
4063 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004064 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004065 * @see Notification#sound
4066 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004067 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004068 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004069 mN.sound = sound;
4070 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004071 return this;
4072 }
4073
4074 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004075 * Set the vibration pattern to use.
4076 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004077 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4078 * <code>pattern</code> parameter.
4079 *
Chris Wren47c20a12014-06-18 17:27:29 -04004080 * <p>
4081 * A notification that vibrates is more likely to be presented as a heads-up notification.
4082 * </p>
4083 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004084 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004085 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004086 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004087 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004088 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004089 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004090 return this;
4091 }
4092
Joe Onoratocb109a02011-01-18 17:57:41 -08004093 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004094 * Set the desired color for the indicator LED on the device, as well as the
4095 * blink duty cycle (specified in milliseconds).
4096 *
4097
4098 * Not all devices will honor all (or even any) of these values.
4099 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004100 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004101 * @see Notification#ledARGB
4102 * @see Notification#ledOnMS
4103 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004104 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004105 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004106 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004107 mN.ledARGB = argb;
4108 mN.ledOnMS = onMs;
4109 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004110 if (onMs != 0 || offMs != 0) {
4111 mN.flags |= FLAG_SHOW_LIGHTS;
4112 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004113 return this;
4114 }
4115
Joe Onoratocb109a02011-01-18 17:57:41 -08004116 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004117 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004118 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004119
4120 * Ongoing notifications cannot be dismissed by the user, so your application or service
4121 * must take care of canceling them.
4122 *
4123
4124 * They are typically used to indicate a background task that the user is actively engaged
4125 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4126 * (e.g., a file download, sync operation, active network connection).
4127 *
4128
4129 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004130 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004131 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004132 public Builder setOngoing(boolean ongoing) {
4133 setFlag(FLAG_ONGOING_EVENT, ongoing);
4134 return this;
4135 }
4136
Joe Onoratocb109a02011-01-18 17:57:41 -08004137 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004138 * Set whether this notification should be colorized. When set, the color set with
4139 * {@link #setColor(int)} will be used as the background color of this notification.
4140 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004141 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4142 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004143 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004144 * For most styles, the coloring will only be applied if the notification is for a
4145 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004146 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004147 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004148 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004149 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004150 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004151 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004152 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004153 public Builder setColorized(boolean colorize) {
4154 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4155 return this;
4156 }
4157
4158 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004159 * Set this flag if you would only like the sound, vibrate
4160 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004161 *
4162 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004163 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004164 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004165 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4166 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4167 return this;
4168 }
4169
Joe Onoratocb109a02011-01-18 17:57:41 -08004170 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004171 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004172 *
4173 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004174 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004175 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004176 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004177 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004178 return this;
4179 }
4180
Joe Onoratocb109a02011-01-18 17:57:41 -08004181 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004182 * Set whether or not this notification should not bridge to other devices.
4183 *
4184 * <p>Some notifications can be bridged to other devices for remote display.
4185 * This hint can be set to recommend this notification not be bridged.
4186 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004187 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004188 public Builder setLocalOnly(boolean localOnly) {
4189 setFlag(FLAG_LOCAL_ONLY, localOnly);
4190 return this;
4191 }
4192
4193 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004194 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004195 * <p>
4196 * The value should be one or more of the following fields combined with
4197 * bitwise-or:
4198 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4199 * <p>
4200 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004201 *
4202 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004203 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004204 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004205 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004206 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004207 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004208 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004209 return this;
4210 }
4211
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004212 /**
4213 * Set the priority of this notification.
4214 *
4215 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004216 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004217 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004218 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004219 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004220 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004221 return this;
4222 }
Joe Malin8d40d042012-11-05 11:36:40 -08004223
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004224 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004225 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004226 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004227 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004228 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004229 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004230 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004231 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004232 return this;
4233 }
4234
4235 /**
Chris Wrendde75302014-03-26 17:24:15 -04004236 * Add a person that is relevant to this notification.
4237 *
Chris Wrene6c48932014-09-29 17:19:27 -04004238 * <P>
4239 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004240 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4241 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4242 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004243 * </P>
4244 *
4245 * <P>
4246 * The person should be specified by the {@code String} representation of a
4247 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4248 * </P>
4249 *
4250 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4251 * URIs. The path part of these URIs must exist in the contacts database, in the
4252 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4253 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004254 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4255 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004256 * </P>
4257 *
4258 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004259 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004260 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004261 */
Chris Wrene6c48932014-09-29 17:19:27 -04004262 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004263 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004264 return this;
4265 }
4266
4267 /**
4268 * Add a person that is relevant to this notification.
4269 *
4270 * <P>
4271 * Depending on user preferences, this annotation may allow the notification to pass
4272 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4273 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4274 * appear more prominently in the user interface.
4275 * </P>
4276 *
4277 * <P>
4278 * A person should usually contain a uri in order to benefit from the ranking boost.
4279 * However, even if no uri is provided, it's beneficial to provide other people in the
4280 * notification, such that listeners and voice only devices can announce and handle them
4281 * properly.
4282 * </P>
4283 *
4284 * @param person the person to add.
4285 * @see Notification#EXTRA_PEOPLE_LIST
4286 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004287 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004288 public Builder addPerson(Person person) {
4289 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004290 return this;
4291 }
4292
4293 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004294 * Set this notification to be part of a group of notifications sharing the same key.
4295 * Grouped notifications may display in a cluster or stack on devices which
4296 * support such rendering.
4297 *
4298 * <p>To make this notification the summary for its group, also call
4299 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4300 * {@link #setSortKey}.
4301 * @param groupKey The group key of the group.
4302 * @return this object for method chaining
4303 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004304 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004305 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004306 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004307 return this;
4308 }
4309
4310 /**
4311 * Set this notification to be the group summary for a group of notifications.
4312 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004313 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4314 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004315 * @param isGroupSummary Whether this notification should be a group summary.
4316 * @return this object for method chaining
4317 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004318 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004319 public Builder setGroupSummary(boolean isGroupSummary) {
4320 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4321 return this;
4322 }
4323
4324 /**
4325 * Set a sort key that orders this notification among other notifications from the
4326 * same package. This can be useful if an external sort was already applied and an app
4327 * would like to preserve this. Notifications will be sorted lexicographically using this
4328 * value, although providing different priorities in addition to providing sort key may
4329 * cause this value to be ignored.
4330 *
4331 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004332 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004333 *
4334 * @see String#compareTo(String)
4335 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004336 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004337 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004338 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004339 return this;
4340 }
4341
4342 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004343 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004344 *
Griff Hazen720042b2014-02-24 15:46:56 -08004345 * <p>Values within the Bundle will replace existing extras values in this Builder.
4346 *
4347 * @see Notification#extras
4348 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004349 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004350 public Builder addExtras(Bundle extras) {
4351 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004352 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004353 }
4354 return this;
4355 }
4356
4357 /**
4358 * Set metadata for this notification.
4359 *
4360 * <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 -04004361 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004362 * called.
4363 *
Griff Hazen720042b2014-02-24 15:46:56 -08004364 * <p>Replaces any existing extras values with those from the provided Bundle.
4365 * Use {@link #addExtras} to merge in metadata instead.
4366 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004367 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004368 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004369 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004370 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004371 if (extras != null) {
4372 mUserExtras = extras;
4373 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004374 return this;
4375 }
4376
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004377 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004378 * Get the current metadata Bundle used by this notification Builder.
4379 *
4380 * <p>The returned Bundle is shared with this Builder.
4381 *
4382 * <p>The current contents of this Bundle are copied into the Notification each time
4383 * {@link #build()} is called.
4384 *
4385 * @see Notification#extras
4386 */
4387 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004388 return mUserExtras;
4389 }
4390
4391 private Bundle getAllExtras() {
4392 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4393 saveExtras.putAll(mN.extras);
4394 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004395 }
4396
4397 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004398 * Add an action to this notification. Actions are typically displayed by
4399 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004400 * <p>
4401 * Every action must have an icon (32dp square and matching the
4402 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4403 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4404 * <p>
4405 * A notification in its expanded form can display up to 3 actions, from left to right in
4406 * the order they were added. Actions will not be displayed when the notification is
4407 * collapsed, however, so be sure that any essential functions may be accessed by the user
4408 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004409 *
4410 * @param icon Resource ID of a drawable that represents the action.
4411 * @param title Text describing the action.
4412 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004413 *
4414 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004415 */
Dan Sandler86647982015-05-13 23:41:13 -04004416 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004417 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004418 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004419 return this;
4420 }
4421
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004422 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004423 * Add an action to this notification. Actions are typically displayed by
4424 * the system as a button adjacent to the notification content.
4425 * <p>
4426 * Every action must have an icon (32dp square and matching the
4427 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4428 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4429 * <p>
4430 * A notification in its expanded form can display up to 3 actions, from left to right in
4431 * the order they were added. Actions will not be displayed when the notification is
4432 * collapsed, however, so be sure that any essential functions may be accessed by the user
4433 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4434 *
4435 * @param action The action to add.
4436 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004437 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004438 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004439 if (action != null) {
4440 mActions.add(action);
4441 }
Griff Hazen959591e2014-05-15 22:26:18 -07004442 return this;
4443 }
4444
4445 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004446 * Alter the complete list of actions attached to this notification.
4447 * @see #addAction(Action).
4448 *
4449 * @param actions
4450 * @return
4451 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004452 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004453 public Builder setActions(Action... actions) {
4454 mActions.clear();
4455 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004456 if (actions[i] != null) {
4457 mActions.add(actions[i]);
4458 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004459 }
4460 return this;
4461 }
4462
4463 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004464 * Add a rich notification style to be applied at build time.
4465 *
4466 * @param style Object responsible for modifying the notification style.
4467 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004468 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004469 public Builder setStyle(Style style) {
4470 if (mStyle != style) {
4471 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004472 if (mStyle != null) {
4473 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004474 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4475 } else {
4476 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004477 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004478 }
4479 return this;
4480 }
4481
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004482 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004483 * Returns the style set by {@link #setStyle(Style)}.
4484 */
4485 public Style getStyle() {
4486 return mStyle;
4487 }
4488
4489 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004490 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004491 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004492 * @return The same Builder.
4493 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004494 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004495 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004496 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004497 return this;
4498 }
4499
4500 /**
4501 * Supply a replacement Notification whose contents should be shown in insecure contexts
4502 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4503 * @param n A replacement notification, presumably with some or all info redacted.
4504 * @return The same Builder.
4505 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004506 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004507 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004508 if (n != null) {
4509 mN.publicVersion = new Notification();
4510 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4511 } else {
4512 mN.publicVersion = null;
4513 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004514 return this;
4515 }
4516
Griff Hazenb720abe2014-05-20 13:15:30 -07004517 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004518 * Apply an extender to this notification builder. Extenders may be used to add
4519 * metadata or change options on this builder.
4520 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004521 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004522 public Builder extend(Extender extender) {
4523 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004524 return this;
4525 }
4526
Dan Sandler4e787062015-06-17 15:09:48 -04004527 /**
4528 * @hide
4529 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004530 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004531 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004532 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004533 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004534 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004535 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004536 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004537 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004538 }
4539
Dan Sandler26e81cf2014-05-06 10:01:27 -04004540 /**
4541 * Sets {@link Notification#color}.
4542 *
4543 * @param argb The accent color to use
4544 *
4545 * @return The same Builder.
4546 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004547 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004548 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004549 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004550 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004551 return this;
4552 }
4553
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004554 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004555 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4556 // This user can never be a badged profile,
4557 // and also includes USER_ALL system notifications.
4558 return null;
4559 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004560 // Note: This assumes that the current user can read the profile badge of the
4561 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004562 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004563 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004564 }
4565
4566 private Bitmap getProfileBadge() {
4567 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004568 if (badge == null) {
4569 return null;
4570 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004571 final int size = mContext.getResources().getDimensionPixelSize(
4572 R.dimen.notification_badge_size);
4573 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004574 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004575 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004576 badge.draw(canvas);
4577 return bitmap;
4578 }
4579
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004580 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004581 Bitmap profileBadge = getProfileBadge();
4582
Kenny Guy98193ea2014-07-24 19:54:37 +01004583 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004584 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4585 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004586 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004587 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004588 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004589 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004590 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004591 }
4592
Gus Prevasbae5ba32018-11-28 15:45:13 -05004593 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4594 contentView.setDrawableTint(
4595 R.id.alerted_icon,
4596 false /* targetBackground */,
4597 getNeutralColor(p),
4598 PorterDuff.Mode.SRC_ATOP);
4599 }
4600
Julia Reynoldsfc640012018-02-21 12:25:27 -05004601 /**
4602 * @hide
4603 */
4604 public boolean usesStandardHeader() {
4605 if (mN.mUsesStandardHeader) {
4606 return true;
4607 }
4608 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4609 if (mN.contentView == null && mN.bigContentView == null) {
4610 return true;
4611 }
4612 }
4613 boolean contentViewUsesHeader = mN.contentView == null
4614 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4615 boolean bigContentViewUsesHeader = mN.bigContentView == null
4616 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4617 return contentViewUsesHeader && bigContentViewUsesHeader;
4618 }
4619
Christoph Studerfe718432014-09-01 18:21:18 +02004620 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004621 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004622 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004623 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004624 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004625 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004626 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004627 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004628 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004629 }
4630
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004631 /**
4632 * Resets the notification header to its original state
4633 */
4634 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004635 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4636 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004637 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004638 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004639 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004640 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004641 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004642 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4643 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004644 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004645 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004646 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004647 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004648 contentView.setImageViewIcon(R.id.profile_badge, null);
4649 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004650 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004651 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004652 }
4653
Selim Cinek384804b2018-04-18 14:31:07 +08004654 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4655 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4656 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004657 }
4658
Selim Cinek384804b2018-04-18 14:31:07 +08004659 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4660 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004661 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004662
Christoph Studerfe718432014-09-01 18:21:18 +02004663 resetStandardTemplate(contentView);
4664
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004665 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004666 updateBackgroundColor(contentView, p);
4667 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004668 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004669 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004670 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004671 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004672 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004673 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004674 contentView.setViewLayoutWidth(R.id.title, showProgress
4675 ? ViewGroup.LayoutParams.WRAP_CONTENT
4676 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004677 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004678 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004679 int textId = showProgress ? com.android.internal.R.id.text_line_1
4680 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004681 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004682 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004683 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004684 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004685
Selim Cinek279fa862016-06-14 10:57:25 -07004686 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004687
Selim Cinek29603462015-11-17 19:04:39 -08004688 return contentView;
4689 }
4690
Selim Cinek48f66b72017-08-18 16:17:51 -07004691 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004692 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004693 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004694 }
4695 return text;
4696 }
4697
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004698 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4699 StandardTemplateParams p) {
4700 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004701 contentView.setTextColor(id, mPrimaryTextColor);
4702 }
4703
Selim Cinek48f66b72017-08-18 16:17:51 -07004704 private boolean hasForegroundColor() {
4705 return mForegroundColor != COLOR_INVALID;
4706 }
4707
Selim Cinek389edcd2017-05-11 19:16:44 -07004708 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004709 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004710 * @hide
4711 */
4712 @VisibleForTesting
4713 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004714 return getPrimaryTextColor(mParams);
4715 }
4716
4717 /**
4718 * @param p the template params to inflate this with
4719 * @return the primary text color
4720 * @hide
4721 */
4722 @VisibleForTesting
4723 public int getPrimaryTextColor(StandardTemplateParams p) {
4724 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004725 return mPrimaryTextColor;
4726 }
4727
Selim Cinek389edcd2017-05-11 19:16:44 -07004728 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004729 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004730 * @hide
4731 */
4732 @VisibleForTesting
4733 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004734 return getSecondaryTextColor(mParams);
4735 }
4736
4737 /**
4738 * @param p the template params to inflate this with
4739 * @return the secondary text color
4740 * @hide
4741 */
4742 @VisibleForTesting
4743 public int getSecondaryTextColor(StandardTemplateParams p) {
4744 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004745 return mSecondaryTextColor;
4746 }
4747
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004748 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4749 StandardTemplateParams p) {
4750 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004751 contentView.setTextColor(id, mSecondaryTextColor);
4752 }
4753
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004754 private void ensureColors(StandardTemplateParams p) {
4755 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004756 if (mPrimaryTextColor == COLOR_INVALID
4757 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004758 || mTextColorsAreForBackground != backgroundColor) {
4759 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004760 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004761 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004762 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004763 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004764 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004765 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004766 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004767 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004768 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004769 mSecondaryTextColor, backgroundColor, 4.5);
4770 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004771 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004772 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4773 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4774 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004775 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004776 // We only respect the given colors if worst case Black or White still has
4777 // contrast
4778 boolean backgroundLight = backLum > textLum
4779 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4780 || backLum <= textLum
4781 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004782 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004783 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004784 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004785 mForegroundColor,
4786 backgroundColor,
4787 true /* findFG */,
4788 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004789 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004790 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004791 } else {
4792 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004793 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004794 mForegroundColor,
4795 backgroundColor,
4796 true /* findFG */,
4797 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004798 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004799 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004800 }
4801 } else {
4802 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004803 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004804 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4805 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004806 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004807 backgroundColor) < 4.5f) {
4808 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004809 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004810 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004811 mSecondaryTextColor,
4812 backgroundColor,
4813 true /* findFG */,
4814 4.5f);
4815 } else {
4816 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004817 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004818 mSecondaryTextColor,
4819 backgroundColor,
4820 true /* findFG */,
4821 4.5f);
4822 }
Lucas Dupina291d192018-06-07 13:59:42 -07004823 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004824 mSecondaryTextColor, backgroundLight
4825 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4826 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004827 }
4828 }
4829 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004830 }
4831 }
4832
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004833 private void updateBackgroundColor(RemoteViews contentView,
4834 StandardTemplateParams p) {
4835 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004836 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004837 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004838 } else {
4839 // Clear it!
4840 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4841 0);
4842 }
4843 }
4844
Selim Cinek860b6da2015-12-16 19:02:19 -08004845 /**
4846 * @param remoteView the remote view to update the minheight in
4847 * @param hasMinHeight does it have a mimHeight
4848 * @hide
4849 */
4850 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4851 int minHeight = 0;
4852 if (hasMinHeight) {
4853 // we need to set the minHeight of the notification
4854 minHeight = mContext.getResources().getDimensionPixelSize(
4855 com.android.internal.R.dimen.notification_min_content_height);
4856 }
4857 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4858 }
4859
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004860 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4861 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004862 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4863 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4864 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004865 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004866 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004867 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004868 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004869 contentView.setProgressBackgroundTintList(
4870 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4871 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004872 if (getRawColor(p) != COLOR_DEFAULT) {
4873 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004874 contentView.setProgressTintList(R.id.progress, colorStateList);
4875 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004876 }
Selim Cinek29603462015-11-17 19:04:39 -08004877 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004878 } else {
4879 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004880 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004881 }
Joe Onorato561d3852010-11-20 18:09:34 -08004882 }
4883
Selim Cinek384804b2018-04-18 14:31:07 +08004884 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4885 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004886 boolean largeIconShown = bindLargeIcon(contentView, p);
4887 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004888 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004889 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004890 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004891 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4892 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4893 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4894 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004895 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004896 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004897 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004898 }
4899 }
4900
Selim Cinek1c72fa02018-04-23 18:00:54 +08004901 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4902 int marginEnd = 0;
4903 int contentMargin = mContext.getResources().getDimensionPixelSize(
4904 R.dimen.notification_content_margin_end);
4905 int iconSize = mContext.getResources().getDimensionPixelSize(
4906 R.dimen.notification_right_icon_size);
4907 if (replyIconShown) {
4908 // The size of the reply icon
4909 marginEnd += iconSize;
4910
4911 int replyInset = mContext.getResources().getDimensionPixelSize(
4912 R.dimen.notification_reply_inset);
4913 // We're subtracting the inset of the reply icon to make sure it's
4914 // aligned nicely on the right, and remove it from the following padding
4915 marginEnd -= replyInset * 2;
4916 }
4917 if (largeIconShown) {
4918 // adding size of the right icon
4919 marginEnd += iconSize;
4920
4921 if (replyIconShown) {
4922 // We also add some padding to the reply icon if it's around
4923 marginEnd += contentMargin;
4924 }
4925 }
4926 if (replyIconShown || largeIconShown) {
4927 // The padding to the content
4928 marginEnd += contentMargin;
4929 }
4930 return marginEnd;
4931 }
4932
Selim Cinek384804b2018-04-18 14:31:07 +08004933 /**
4934 * Bind the large icon.
4935 * @return if the largeIcon is visible
4936 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004937 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004938 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4939 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4940 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004941 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004942 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004943 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4944 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004945 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004946 }
Selim Cinek384804b2018-04-18 14:31:07 +08004947 return showLargeIcon;
4948 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004949
Selim Cinek384804b2018-04-18 14:31:07 +08004950 /**
4951 * Bind the reply icon.
4952 * @return if the reply icon is visible
4953 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004954 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004955 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004956 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004957 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004958 action = findReplyAction();
4959 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004960 }
Selim Cinek384804b2018-04-18 14:31:07 +08004961 if (actionVisible) {
4962 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4963 contentView.setDrawableTint(R.id.reply_icon_action,
4964 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004965 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004966 PorterDuff.Mode.SRC_ATOP);
4967 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4968 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4969 } else {
4970 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4971 }
4972 contentView.setViewVisibility(R.id.reply_icon_action,
4973 actionVisible ? View.VISIBLE : View.GONE);
4974 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004975 }
4976
4977 private Action findReplyAction() {
4978 ArrayList<Action> actions = mActions;
4979 if (mOriginalActions != null) {
4980 actions = mOriginalActions;
4981 }
4982 int numActions = actions.size();
4983 for (int i = 0; i < numActions; i++) {
4984 Action action = actions.get(i);
4985 if (hasValidRemoteInput(action)) {
4986 return action;
4987 }
4988 }
4989 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004990 }
4991
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004992 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4993 bindSmallIcon(contentView, p);
4994 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08004995 bindHeaderText(contentView, p);
4996 bindHeaderTextSecondary(contentView, p);
4997 bindHeaderChronometerAndTime(contentView, p);
4998 bindProfileBadge(contentView, p);
4999 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005000 bindActivePermissions(contentView, p);
5001 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005002 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005003 }
5004
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005005 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005006 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005007 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5008 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5009 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5010 }
5011
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005012 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5013 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005014 contentView.setDrawableTint(R.id.expand_button, false, color,
5015 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005016 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005017 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005018 }
5019
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005020 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5021 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005022 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005023 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005024 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005025 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5026 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5027 contentView.setLong(R.id.chronometer, "setBase",
5028 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5029 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005030 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005031 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005032 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005033 } else {
5034 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5035 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005036 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005037 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005038 } else {
5039 // We still want a time to be set but gone, such that we can show and hide it
5040 // on demand in case it's a child notification without anything in the header
5041 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005042 }
5043 }
5044
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005045 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5046 CharSequence summaryText = p.summaryText;
5047 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005048 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005049 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005050 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005051 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005052 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5053 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005054 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005055 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005056 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005057 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005058 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005059 processLegacyText(summaryText)));
5060 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005061 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5062 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005063 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005064 }
5065 }
5066
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005067 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5068 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005069 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005070 processLegacyText(p.headerTextSecondary)));
5071 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005072 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5073 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005074 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005075 }
5076 }
5077
Adrian Rooseba05822016-04-22 17:09:27 -07005078 /**
5079 * @hide
5080 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005081 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005082 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005083 CharSequence name = null;
5084 final PackageManager pm = mContext.getPackageManager();
5085 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5086 // only system packages which lump together a bunch of unrelated stuff
5087 // may substitute a different name to make the purpose of the
5088 // notification more clear. the correct package label should always
5089 // be accessible via SystemUI.
5090 final String pkg = mContext.getPackageName();
5091 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5092 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5093 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5094 name = subName;
5095 } else {
5096 Log.w(TAG, "warning: pkg "
5097 + pkg + " attempting to substitute app name '" + subName
5098 + "' without holding perm "
5099 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5100 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005101 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005102 if (TextUtils.isEmpty(name)) {
5103 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5104 }
5105 if (TextUtils.isEmpty(name)) {
5106 // still nothing?
5107 return null;
5108 }
5109
5110 return String.valueOf(name);
5111 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005112 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005113 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005114 if (isColorized(p)) {
5115 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005116 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005117 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005118 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005119 }
5120
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005121 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005122 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005123 }
5124
5125 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005126 if (mN.mSmallIcon == null && mN.icon != 0) {
5127 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5128 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005129 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005130 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005131 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005132 }
5133
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005134 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005135 * @return true if the built notification will show the time or the chronometer; false
5136 * otherwise
5137 */
5138 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005139 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005140 }
5141
Christoph Studerfe718432014-09-01 18:21:18 +02005142 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005143 // actions_container is only reset when there are no actions to avoid focus issues with
5144 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005145 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005146 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005147
5148 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5149 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005150 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5151 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005152
5153 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5154 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5155 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5156 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005157
Selim Cineked64a142018-02-06 18:06:01 -08005158 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5159 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005160 }
5161
Selim Cinek384804b2018-04-18 14:31:07 +08005162 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5163 TemplateBindResult result) {
5164 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5165 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005166 }
5167
Gustav Sennton1463d832018-11-06 16:12:48 +00005168 private static List<Notification.Action> filterOutContextualActions(
5169 List<Notification.Action> actions) {
5170 List<Notification.Action> nonContextualActions = new ArrayList<>();
5171 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005172 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005173 nonContextualActions.add(action);
5174 }
5175 }
5176 return nonContextualActions;
5177 }
5178
Adrian Roos70d7aa32017-01-11 15:39:06 -08005179 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005180 StandardTemplateParams p, TemplateBindResult result) {
5181 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005182
Christoph Studerfe718432014-09-01 18:21:18 +02005183 resetStandardTemplateWithActions(big);
5184
Adrian Roose458aa82015-12-08 16:17:19 -08005185 boolean validRemoteInput = false;
5186
Gustav Sennton1463d832018-11-06 16:12:48 +00005187 // In the UI contextual actions appear separately from the standard actions, so we
5188 // filter them out here.
5189 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5190
5191 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005192 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005193 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005194 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005195 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005196 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005197 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005198 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005199 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005200 Action action = nonContextualActions.get(i);
5201
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005202 boolean actionHasValidInput = hasValidRemoteInput(action);
5203 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005204
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005205 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005206 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005207 // Clear the drawable
5208 button.setInt(R.id.action0, "setBackgroundResource", 0);
5209 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005210 big.addView(R.id.actions, button);
5211 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005212 } else {
5213 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005214 }
Adrian Roose458aa82015-12-08 16:17:19 -08005215
5216 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005217 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005218 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5219 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005220 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005221 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005222 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5223 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005224 big.setTextViewText(R.id.notification_material_reply_text_1,
5225 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005226 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005227 big.setViewVisibility(R.id.notification_material_reply_progress,
5228 showSpinner ? View.VISIBLE : View.GONE);
5229 big.setProgressIndeterminateTintList(
5230 R.id.notification_material_reply_progress,
5231 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005232 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005233
Selim Cinekbee4e072018-05-21 22:06:43 -07005234 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5235 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005236 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005237 big.setTextViewText(R.id.notification_material_reply_text_2,
5238 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005239 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005240
Selim Cinekbee4e072018-05-21 22:06:43 -07005241 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5242 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005243 big.setViewVisibility(
5244 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005245 big.setTextViewText(R.id.notification_material_reply_text_3,
5246 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005247 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005248 }
5249 }
5250 }
5251
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005252 return big;
5253 }
5254
Adrian Roose458aa82015-12-08 16:17:19 -08005255 private boolean hasValidRemoteInput(Action action) {
5256 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5257 // Weird actions
5258 return false;
5259 }
5260
5261 RemoteInput[] remoteInputs = action.getRemoteInputs();
5262 if (remoteInputs == null) {
5263 return false;
5264 }
5265
5266 for (RemoteInput r : remoteInputs) {
5267 CharSequence[] choices = r.getChoices();
5268 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5269 return true;
5270 }
5271 }
5272 return false;
5273 }
5274
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005275 /**
5276 * Construct a RemoteViews for the final 1U notification layout. In order:
5277 * 1. Custom contentView from the caller
5278 * 2. Style's proposed content view
5279 * 3. Standard template view
5280 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005281 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005282 return createContentView(false /* increasedheight */ );
5283 }
5284
5285 /**
5286 * Construct a RemoteViews for the smaller content view.
5287 *
5288 * @param increasedHeight true if this layout be created with an increased height. Some
5289 * styles may support showing more then just that basic 1U size
5290 * and the system may decide to render important notifications
5291 * slightly bigger even when collapsed.
5292 *
5293 * @hide
5294 */
5295 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005296 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005297 return mN.contentView;
5298 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005299 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005300 if (styleView != null) {
5301 return styleView;
5302 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005303 }
Selim Cinek384804b2018-04-18 14:31:07 +08005304 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005305 }
5306
Selim Cineka7679b62017-05-10 16:33:25 -07005307 private boolean useExistingRemoteView() {
5308 return mStyle == null || (!mStyle.displayCustomViewInline()
5309 && !mRebuildStyledRemoteViews);
5310 }
5311
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005312 /**
5313 * Construct a RemoteViews for the final big notification layout.
5314 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005315 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005316 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005317 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005318 return mN.bigContentView;
5319 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005320 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005321 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005322 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005323 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5324 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005325 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005326 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005327 return result;
5328 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005329
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005330 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005331 * Construct a RemoteViews for the final notification header only. This will not be
5332 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005333 *
5334 * @hide
5335 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005336 public RemoteViews makeNotificationHeader() {
5337 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005338 }
5339
5340 /**
5341 * Construct a RemoteViews for the final notification header only. This will not be
5342 * colorized.
5343 *
5344 * @param p the template params to inflate this with
5345 */
5346 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5347 // Headers on their own are never colorized
5348 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005349 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005350 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005351 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005352 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005353 return header;
5354 }
5355
Adrian Roos487374f2017-01-11 15:48:14 -08005356 /**
5357 * Construct a RemoteViews for the ambient version of the notification.
5358 *
5359 * @hide
5360 */
5361 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005362 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5363 if (headsUpContentView != null) {
5364 return headsUpContentView;
5365 }
5366 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005367 }
5368
Selim Cinek29603462015-11-17 19:04:39 -08005369 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005370 if (result != null) {
5371 result.setViewVisibility(R.id.text_line_1, View.GONE);
5372 }
Selim Cinek29603462015-11-17 19:04:39 -08005373 }
5374
Selim Cinek6743c0b2017-01-18 18:24:01 -08005375 /**
5376 * Adapt the Notification header if this view is used as an expanded view.
5377 *
5378 * @hide
5379 */
5380 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005381 if (result != null) {
5382 result.setBoolean(R.id.notification_header, "setExpanded", true);
5383 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005384 }
5385
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005386 /**
5387 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005388 *
5389 * @param increasedHeight true if this layout be created with an increased height. Some
5390 * styles may support showing more then just that basic 1U size
5391 * and the system may decide to render important notifications
5392 * slightly bigger even when collapsed.
5393 *
5394 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005395 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005396 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005397 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005398 return mN.headsUpContentView;
5399 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005400 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5401 if (styleView != null) {
5402 return styleView;
5403 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005404 } else if (mActions.size() == 0) {
5405 return null;
5406 }
5407
Selim Cinekbee4e072018-05-21 22:06:43 -07005408 // We only want at most a single remote input history to be shown here, otherwise
5409 // the content would become squished.
5410 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5411 .setMaxRemoteInputHistory(1);
5412 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5413 p,
5414 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005415 }
5416
Selim Cinek624c02db2015-12-14 21:00:02 -08005417 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005418 * Construct a RemoteViews for the final heads-up notification layout.
5419 */
5420 public RemoteViews createHeadsUpContentView() {
5421 return createHeadsUpContentView(false /* useIncreasedHeight */);
5422 }
5423
5424 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005425 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5426 *
5427 * @hide
5428 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005429 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005430 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005431 return makePublicView(false /* ambient */);
5432 }
5433
5434 /**
5435 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5436 *
5437 * @hide
5438 */
5439 public RemoteViews makePublicAmbientNotification() {
5440 return makePublicView(true /* ambient */);
5441 }
5442
5443 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005444 if (mN.publicVersion != null) {
5445 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005446 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005447 }
5448 Bundle savedBundle = mN.extras;
5449 Style style = mStyle;
5450 mStyle = null;
5451 Icon largeIcon = mN.mLargeIcon;
5452 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005453 Bitmap largeIconLegacy = mN.largeIcon;
5454 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005455 ArrayList<Action> actions = mActions;
5456 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005457 Bundle publicExtras = new Bundle();
5458 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5459 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5460 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5461 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005462 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5463 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005464 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5465 if (appName != null) {
5466 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5467 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005468 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005469 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005470 view = makeNotificationHeader();
5471 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005472 mN.extras = savedBundle;
5473 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005474 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005475 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005476 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005477 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005478 }
5479
Selim Cinek6743c0b2017-01-18 18:24:01 -08005480 /**
5481 * Construct a content view for the display when low - priority
5482 *
5483 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5484 * a new subtext is created consisting of the content of the
5485 * notification.
5486 * @hide
5487 */
5488 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005489 StandardTemplateParams p = mParams.reset()
5490 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005491 .fillTextsFrom(this);
5492 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5493 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005494 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005495 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005496 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005497 return header;
5498 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005499
Selim Cinek6743c0b2017-01-18 18:24:01 -08005500 private CharSequence createSummaryText() {
5501 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5502 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5503 return titleText;
5504 }
5505 SpannableStringBuilder summary = new SpannableStringBuilder();
5506 if (titleText == null) {
5507 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5508 }
5509 BidiFormatter bidi = BidiFormatter.getInstance();
5510 if (titleText != null) {
5511 summary.append(bidi.unicodeWrap(titleText));
5512 }
5513 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5514 if (titleText != null && contentText != null) {
5515 summary.append(bidi.unicodeWrap(mContext.getText(
5516 R.string.notification_header_divider_symbol_with_spaces)));
5517 }
5518 if (contentText != null) {
5519 summary.append(bidi.unicodeWrap(contentText));
5520 }
5521 return summary;
5522 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005523
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005524 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005525 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005526 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005527 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005528 emphazisedMode ? getEmphasizedActionLayoutResource()
5529 : tombstone ? getActionTombstoneLayoutResource()
5530 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005531 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005532 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005533 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005534 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005535 if (action.mRemoteInputs != null) {
5536 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5537 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005538 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005539 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005540 CharSequence title = action.title;
5541 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005542 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005543 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005544 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005545 } else {
5546 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005547 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005548 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005549 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005550 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005551 int rippleColor;
5552 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5553 if (hasColorOverride) {
5554 // There's a span spanning the full text, let's take it and use it as the
5555 // background color
5556 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005557 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005558 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005559 button.setTextColor(R.id.action0, textColor);
5560 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005561 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5562 && mTintActionButtons) {
5563 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005564 button.setTextColor(R.id.action0, rippleColor);
5565 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005566 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005567 }
Selim Cinek396caca2018-04-10 17:46:46 -07005568 // We only want about 20% alpha for the ripple
5569 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5570 button.setColorStateList(R.id.action0, "setRippleColor",
5571 ColorStateList.valueOf(rippleColor));
5572 button.setColorStateList(R.id.action0, "setButtonBackground",
5573 ColorStateList.valueOf(background));
5574 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005575 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005576 button.setTextViewText(R.id.action0, processTextSpans(
5577 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005578 if (isColorized(p)) {
5579 setTextViewColorPrimary(button, R.id.action0, p);
5580 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005581 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005582 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005583 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005584 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5585 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005586 return button;
5587 }
5588
Joe Onoratocb109a02011-01-18 17:57:41 -08005589 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005590 * Ensures contrast on color spans against a background color. also returns the color of the
5591 * text if a span was found that spans over the whole text.
5592 *
5593 * @param charSequence the charSequence on which the spans are
5594 * @param background the background color to ensure the contrast against
5595 * @param outResultColor an array in which a color will be returned as the first element if
5596 * there exists a full length color span.
5597 * @return the contrasted charSequence
5598 */
5599 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5600 ColorStateList[] outResultColor) {
5601 if (charSequence instanceof Spanned) {
5602 Spanned ss = (Spanned) charSequence;
5603 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5604 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5605 for (Object span : spans) {
5606 Object resultSpan = span;
5607 int spanStart = ss.getSpanStart(span);
5608 int spanEnd = ss.getSpanEnd(span);
5609 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5610 if (resultSpan instanceof CharacterStyle) {
5611 resultSpan = ((CharacterStyle) span).getUnderlying();
5612 }
5613 if (resultSpan instanceof TextAppearanceSpan) {
5614 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5615 ColorStateList textColor = originalSpan.getTextColor();
5616 if (textColor != null) {
5617 int[] colors = textColor.getColors();
5618 int[] newColors = new int[colors.length];
5619 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005620 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005621 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005622 }
5623 textColor = new ColorStateList(textColor.getStates().clone(),
5624 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005625 if (fullLength) {
5626 outResultColor[0] = textColor;
5627 // Let's drop the color from the span
5628 textColor = null;
5629 }
Selim Cinek981962e2016-07-20 20:41:58 -07005630 resultSpan = new TextAppearanceSpan(
5631 originalSpan.getFamily(),
5632 originalSpan.getTextStyle(),
5633 originalSpan.getTextSize(),
5634 textColor,
5635 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005636 }
5637 } else if (resultSpan instanceof ForegroundColorSpan) {
5638 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5639 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005640 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005641 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005642 if (fullLength) {
5643 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005644 resultSpan = null;
5645 } else {
5646 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005647 }
5648 } else {
5649 resultSpan = span;
5650 }
Selim Cinek396caca2018-04-10 17:46:46 -07005651 if (resultSpan != null) {
5652 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5653 }
Selim Cinek981962e2016-07-20 20:41:58 -07005654 }
5655 return builder;
5656 }
5657 return charSequence;
5658 }
5659
5660 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005661 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005662 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005663 */
5664 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005665 if (!mIsLegacyInitialized) {
5666 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5667 < Build.VERSION_CODES.LOLLIPOP;
5668 mIsLegacyInitialized = true;
5669 }
5670 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005671 }
5672
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005673 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005674 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005675 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005676 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005677 } else {
5678 return charSequence;
5679 }
5680 }
5681
Dan Sandler26e81cf2014-05-06 10:01:27 -04005682 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005683 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005684 */
Adrian Roos487374f2017-01-11 15:48:14 -08005685 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005686 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005687 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005688 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005689 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005690 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005691 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005692 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005693 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005694 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005695 contentView.setDrawableTint(R.id.icon, false, color,
5696 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005697
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005698 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005699 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005700 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005701 }
5702
Dan Sandler26e81cf2014-05-06 10:01:27 -04005703 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005704 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005705 * if it's grayscale).
5706 */
5707 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005708 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5709 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005710 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005711 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005712 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005713 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005714 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005715 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005716 }
5717
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005718 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005719 if (mN.color != COLOR_DEFAULT) {
5720 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005721 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005722 }
5723
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005724 int resolveContrastColor(StandardTemplateParams p) {
5725 int rawColor = getRawColor(p);
5726 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005727 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005728 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005729
Selim Cinekac5f0272017-05-02 16:05:41 -07005730 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005731 int background = mContext.getColor(
5732 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005733 if (rawColor == COLOR_DEFAULT) {
5734 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005735 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005736 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005737 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005738 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005739 }
5740 if (Color.alpha(color) < 255) {
5741 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005742 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005743 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005744 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005745 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005746 }
5747
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005748 /**
5749 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5750 *
5751 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5752 * @param p the template params to inflate this with
5753 */
5754 private int getRawColor(StandardTemplateParams p) {
5755 if (p.forceDefaultColor) {
5756 return COLOR_DEFAULT;
5757 }
5758 return mN.color;
5759 }
5760
Selim Cinek4717d862018-04-19 09:19:15 +08005761 int resolveNeutralColor() {
5762 if (mNeutralColor != COLOR_INVALID) {
5763 return mNeutralColor;
5764 }
5765 int background = mContext.getColor(
5766 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005767 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5768 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005769 if (Color.alpha(mNeutralColor) < 255) {
5770 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005771 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005772 }
5773 return mNeutralColor;
5774 }
5775
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005776 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005777 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005778 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005779 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005780 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005781 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005782 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005783 mN.actions = new Action[mActions.size()];
5784 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005785 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005786 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005787 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005788 }
Selim Cinek247fa012016-02-18 09:50:48 -08005789 if (mN.bigContentView != null || mN.contentView != null
5790 || mN.headsUpContentView != null) {
5791 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5792 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005793 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005794 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005795
Julia Reynolds3b848122016-02-26 10:45:32 -05005796 /**
5797 * Creates a Builder from an existing notification so further changes can be made.
5798 * @param context The context for your application / activity.
5799 * @param n The notification to create a Builder from.
5800 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005801 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005802 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005803 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005804 ApplicationInfo applicationInfo = n.extras.getParcelable(
5805 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005806 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005807 if (applicationInfo != null) {
5808 try {
5809 builderContext = context.createApplicationContext(applicationInfo,
5810 Context.CONTEXT_RESTRICTED);
5811 } catch (NameNotFoundException e) {
5812 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5813 builderContext = context; // try with our context
5814 }
5815 } else {
5816 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005817 }
5818
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005819 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005820 }
5821
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005822 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005823 * Determines whether the platform can generate contextual actions for a notification.
5824 * By default this is true.
5825 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005826 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005827 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5828 mN.mAllowSystemGeneratedContextualActions = allowed;
5829 return this;
5830 }
5831
5832 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005833 * @deprecated Use {@link #build()} instead.
5834 */
5835 @Deprecated
5836 public Notification getNotification() {
5837 return build();
5838 }
5839
5840 /**
5841 * Combine all of the options that have been set and return a new {@link Notification}
5842 * object.
5843 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005844 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005845 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005846 // first, add any extras from the calling code
5847 if (mUserExtras != null) {
5848 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005849 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005850
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005851 mN.creationTime = System.currentTimeMillis();
5852
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005853 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005854 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005855
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005856 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005857
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005858 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005859 mStyle.reduceImageSizes(mContext);
5860 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005861 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005862 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005863 }
Selim Cinekd0426622017-07-11 13:19:59 +02005864 mN.reduceImageSizes(mContext);
5865
Adrian Roos5081c0d2016-02-26 16:04:19 -08005866 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005867 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005868 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005869 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005870 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5871 mN.contentView.getSequenceNumber());
5872 }
5873 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005874 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005875 if (mN.bigContentView != null) {
5876 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5877 mN.bigContentView.getSequenceNumber());
5878 }
5879 }
5880 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005881 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005882 if (mN.headsUpContentView != null) {
5883 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5884 mN.headsUpContentView.getSequenceNumber());
5885 }
5886 }
5887 }
5888
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005889 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5890 mN.flags |= FLAG_SHOW_LIGHTS;
5891 }
5892
Adrian Roosfb921842017-10-26 14:49:56 +02005893 mN.allPendingIntents = null;
5894
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005895 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005896 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005897
5898 /**
5899 * Apply this Builder to an existing {@link Notification} object.
5900 *
5901 * @hide
5902 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005903 @NonNull
5904 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005905 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005906 return n;
5907 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005908
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005909 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005910 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005911 * change. Also removes extenders on low ram devices, as
5912 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005913 *
5914 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5915 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005916 * @hide
5917 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005918 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5919 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005920 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005921
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005922 // Only strip views for known Styles because we won't know how to
5923 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005924 if (!isLowRam
5925 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005926 && getNotificationStyleClass(templateClass) == null) {
5927 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005928 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005929
5930 // Only strip unmodified BuilderRemoteViews.
5931 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005932 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005933 n.contentView.getSequenceNumber();
5934 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005935 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005936 n.bigContentView.getSequenceNumber();
5937 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005938 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005939 n.headsUpContentView.getSequenceNumber();
5940
5941 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005942 if (!isLowRam
5943 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005944 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005945 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005946
5947 Notification clone = n.clone();
5948 if (stripContentView) {
5949 clone.contentView = null;
5950 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5951 }
5952 if (stripBigContentView) {
5953 clone.bigContentView = null;
5954 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5955 }
5956 if (stripHeadsUpContentView) {
5957 clone.headsUpContentView = null;
5958 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5959 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005960 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005961 String[] allowedServices = context.getResources().getStringArray(
5962 R.array.config_allowedManagedServicesOnLowRamDevices);
5963 if (allowedServices.length == 0) {
5964 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5965 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5966 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5967 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005968 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005969 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005970 }
5971
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005972 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005973 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005974 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005975 }
5976
5977 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005978 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005979 }
5980
5981 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005982 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005983 }
5984
5985 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005986 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005987 }
5988
5989 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005990 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005991 }
5992
Adrian Roosc1a80b02016-04-05 14:54:55 -07005993 private int getMessagingLayoutResource() {
5994 return R.layout.notification_template_material_messaging;
5995 }
5996
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005997 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005998 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005999 }
6000
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006001 private int getEmphasizedActionLayoutResource() {
6002 return R.layout.notification_material_action_emphasized;
6003 }
6004
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006005 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006006 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006007 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006008
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006009 private int getBackgroundColor(StandardTemplateParams p) {
6010 if (isColorized(p)) {
6011 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006012 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006013 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006014 }
6015 }
6016
Selim Cinek396caca2018-04-10 17:46:46 -07006017 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006018 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006019 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006020 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006021 private int getNeutralColor(StandardTemplateParams p) {
6022 if (isColorized(p)) {
6023 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006024 } else {
6025 return resolveNeutralColor();
6026 }
6027 }
6028
6029 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006030 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006031 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006032 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006033 private int resolveBackgroundColor(StandardTemplateParams p) {
6034 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006035 if (backgroundColor == COLOR_DEFAULT) {
6036 backgroundColor = mContext.getColor(
6037 com.android.internal.R.color.notification_material_background_color);
6038 }
6039 return backgroundColor;
6040 }
6041
Anthony Chenad4d1582017-04-10 16:07:58 -07006042 private boolean shouldTintActionButtons() {
6043 return mTintActionButtons;
6044 }
6045
Selim Cinek99104832017-01-25 14:47:33 -08006046 private boolean textColorsNeedInversion() {
6047 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6048 return false;
6049 }
6050 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6051 return targetSdkVersion > Build.VERSION_CODES.M
6052 && targetSdkVersion < Build.VERSION_CODES.O;
6053 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006054
6055 /**
6056 * Set a color palette to be used as the background and textColors
6057 *
6058 * @param backgroundColor the color to be used as the background
6059 * @param foregroundColor the color to be used as the foreground
6060 *
6061 * @hide
6062 */
6063 public void setColorPalette(int backgroundColor, int foregroundColor) {
6064 mBackgroundColor = backgroundColor;
6065 mForegroundColor = foregroundColor;
6066 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006067 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006068 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006069
6070 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006071 * Forces all styled remoteViews to be built from scratch and not use any cached
6072 * RemoteViews.
6073 * This is needed for legacy apps that are baking in their remoteviews into the
6074 * notification.
6075 *
6076 * @hide
6077 */
6078 public void setRebuildStyledRemoteViews(boolean rebuild) {
6079 mRebuildStyledRemoteViews = rebuild;
6080 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006081
6082 /**
6083 * Get the text that should be displayed in the statusBar when heads upped. This is
6084 * usually just the app name, but may be different depending on the style.
6085 *
6086 * @param publicMode If true, return a text that is safe to display in public.
6087 *
6088 * @hide
6089 */
6090 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6091 if (mStyle != null && !publicMode) {
6092 CharSequence text = mStyle.getHeadsUpStatusBarText();
6093 if (!TextUtils.isEmpty(text)) {
6094 return text;
6095 }
6096 }
6097 return loadHeaderAppName();
6098 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006099 }
6100
6101 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006102 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6103 * remote views.
6104 *
6105 * @hide
6106 */
6107 void reduceImageSizes(Context context) {
6108 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6109 return;
6110 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006111 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006112 if (mLargeIcon != null || largeIcon != null) {
6113 Resources resources = context.getResources();
6114 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006115 int maxWidth = resources.getDimensionPixelSize(isLowRam
6116 ? R.dimen.notification_right_icon_size_low_ram
6117 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006118 int maxHeight = maxWidth;
6119 if (MediaStyle.class.equals(style)
6120 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006121 maxHeight = resources.getDimensionPixelSize(isLowRam
6122 ? R.dimen.notification_media_image_max_height_low_ram
6123 : R.dimen.notification_media_image_max_height);
6124 maxWidth = resources.getDimensionPixelSize(isLowRam
6125 ? R.dimen.notification_media_image_max_width_low_ram
6126 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006127 }
6128 if (mLargeIcon != null) {
6129 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6130 }
6131 if (largeIcon != null) {
6132 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6133 }
6134 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006135 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6136 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6137 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006138 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6139 }
6140
Selim Cineka8cb1262017-08-15 16:53:44 -07006141 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6142 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006143 if (remoteView != null) {
6144 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006145 int maxWidth = resources.getDimensionPixelSize(isLowRam
6146 ? R.dimen.notification_custom_view_max_image_width_low_ram
6147 : R.dimen.notification_custom_view_max_image_width);
6148 int maxHeight = resources.getDimensionPixelSize(isLowRam
6149 ? R.dimen.notification_custom_view_max_image_height_low_ram
6150 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006151 remoteView.reduceImageSizes(maxWidth, maxHeight);
6152 }
6153 }
6154
6155 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006156 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006157 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006158 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006159 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006160 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006161 }
6162
6163 /**
Selim Cinek99104832017-01-25 14:47:33 -08006164 * @return whether this notification has a media session attached
6165 * @hide
6166 */
6167 public boolean hasMediaSession() {
6168 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6169 }
6170
6171 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006172 * @return the style class of this notification
6173 * @hide
6174 */
6175 public Class<? extends Notification.Style> getNotificationStyle() {
6176 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6177
6178 if (!TextUtils.isEmpty(templateClass)) {
6179 return Notification.getNotificationStyleClass(templateClass);
6180 }
6181 return null;
6182 }
6183
6184 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006185 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006186 *
6187 * @hide
6188 */
6189 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006190 if (isColorizedMedia()) {
6191 return true;
6192 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006193 return extras.getBoolean(EXTRA_COLORIZED)
6194 && (hasColorizedPermission() || isForegroundService());
6195 }
6196
6197 /**
6198 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6199 * permission. The permission is checked when a notification is enqueued.
6200 */
6201 private boolean hasColorizedPermission() {
6202 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006203 }
6204
6205 /**
6206 * @return true if this notification is colorized and it is a media notification
6207 *
6208 * @hide
6209 */
6210 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006211 Class<? extends Style> style = getNotificationStyle();
6212 if (MediaStyle.class.equals(style)) {
6213 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6214 if ((colorized == null || colorized) && hasMediaSession()) {
6215 return true;
6216 }
6217 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6218 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6219 return true;
6220 }
6221 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006222 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006223 }
6224
Selim Cinek0847acd2017-04-24 19:48:29 -07006225
6226 /**
6227 * @return true if this is a media notification
6228 *
6229 * @hide
6230 */
6231 public boolean isMediaNotification() {
6232 Class<? extends Style> style = getNotificationStyle();
6233 if (MediaStyle.class.equals(style)) {
6234 return true;
6235 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6236 return true;
6237 }
6238 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006239 }
6240
Selim Cinek279fa862016-06-14 10:57:25 -07006241 private boolean hasLargeIcon() {
6242 return mLargeIcon != null || largeIcon != null;
6243 }
6244
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006245 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006246 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006247 * @hide
6248 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006249 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006250 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6251 }
6252
6253 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006254 * @return true if the notification will show a chronometer; false otherwise
6255 * @hide
6256 */
6257 public boolean showsChronometer() {
6258 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6259 }
6260
6261 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006262 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006263 */
6264 @SystemApi
6265 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6266 Class<? extends Style>[] classes = new Class[] {
6267 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6268 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6269 MessagingStyle.class };
6270 for (Class<? extends Style> innerClass : classes) {
6271 if (templateClass.equals(innerClass.getName())) {
6272 return innerClass;
6273 }
6274 }
6275 return null;
6276 }
6277
6278 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006279 * An object that can apply a rich notification style to a {@link Notification.Builder}
6280 * object.
6281 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006282 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006283
6284 /**
6285 * The number of items allowed simulatanously in the remote input history.
6286 * @hide
6287 */
6288 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006289 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006290
6291 /**
6292 * @hide
6293 */
6294 protected CharSequence mSummaryText = null;
6295
6296 /**
6297 * @hide
6298 */
6299 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006300
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006301 protected Builder mBuilder;
6302
Chris Wrend6297db2012-05-03 16:20:13 -04006303 /**
6304 * Overrides ContentTitle in the big form of the template.
6305 * This defaults to the value passed to setContentTitle().
6306 */
6307 protected void internalSetBigContentTitle(CharSequence title) {
6308 mBigContentTitle = title;
6309 }
6310
6311 /**
6312 * Set the first line of text after the detail section in the big form of the template.
6313 */
6314 protected void internalSetSummaryText(CharSequence cs) {
6315 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006316 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006317 }
6318
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006319 public void setBuilder(Builder builder) {
6320 if (mBuilder != builder) {
6321 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006322 if (mBuilder != null) {
6323 mBuilder.setStyle(this);
6324 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006325 }
6326 }
6327
Chris Wrend6297db2012-05-03 16:20:13 -04006328 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006329 if (mBuilder == null) {
6330 throw new IllegalArgumentException("Style requires a valid Builder object");
6331 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006332 }
Chris Wrend6297db2012-05-03 16:20:13 -04006333
6334 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006335 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6336 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006337 }
6338
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006339
Selim Cinek384804b2018-04-18 14:31:07 +08006340 /**
6341 * Get the standard view for this style.
6342 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006343 * @param layoutId The layout id to use.
6344 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006345 * @param result The result where template bind information is saved.
6346 * @return A remoteView for this style.
6347 * @hide
6348 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006349 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6350 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006351 checkBuilder();
6352
6353 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006354 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006355 }
6356
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006357 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6358 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006359
Chris Wrend6297db2012-05-03 16:20:13 -04006360 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6361 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006362 } else {
6363 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006364 }
6365
Chris Wrend6297db2012-05-03 16:20:13 -04006366 return contentView;
6367 }
6368
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006369 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006370 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006371 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006372 *
6373 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006374 * @hide
6375 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006376 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006377 return null;
6378 }
6379
6380 /**
6381 * Construct a Style-specific RemoteViews for the final big notification layout.
6382 * @hide
6383 */
6384 public RemoteViews makeBigContentView() {
6385 return null;
6386 }
6387
6388 /**
6389 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006390 *
6391 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006392 * @hide
6393 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006394 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006395 return null;
6396 }
6397
6398 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006399 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006400 * @hide
6401 */
6402 public void addExtras(Bundle extras) {
6403 if (mSummaryTextSet) {
6404 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6405 }
6406 if (mBigContentTitle != null) {
6407 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6408 }
Chris Wren91ad5632013-06-05 15:05:57 -04006409 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006410 }
6411
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006412 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006413 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006414 * @hide
6415 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006416 protected void restoreFromExtras(Bundle extras) {
6417 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6418 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6419 mSummaryTextSet = true;
6420 }
6421 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6422 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6423 }
6424 }
6425
6426
6427 /**
6428 * @hide
6429 */
6430 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006431 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006432 return wip;
6433 }
6434
Daniel Sandler0ec46202015-06-24 01:27:05 -04006435 /**
6436 * @hide
6437 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006438 public void purgeResources() {}
6439
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006440 /**
6441 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6442 * attached to.
6443 *
6444 * @return the fully constructed Notification.
6445 */
6446 public Notification build() {
6447 checkBuilder();
6448 return mBuilder.build();
6449 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006450
6451 /**
6452 * @hide
6453 * @return true if the style positions the progress bar on the second line; false if the
6454 * style hides the progress bar
6455 */
6456 protected boolean hasProgress() {
6457 return true;
6458 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006459
6460 /**
6461 * @hide
6462 * @return Whether we should put the summary be put into the notification header
6463 */
6464 public boolean hasSummaryInHeader() {
6465 return true;
6466 }
Selim Cinek593610c2016-02-16 18:42:57 -08006467
6468 /**
6469 * @hide
6470 * @return Whether custom content views are displayed inline in the style
6471 */
6472 public boolean displayCustomViewInline() {
6473 return false;
6474 }
Selim Cinekd0426622017-07-11 13:19:59 +02006475
6476 /**
6477 * Reduces the image sizes contained in this style.
6478 *
6479 * @hide
6480 */
6481 public void reduceImageSizes(Context context) {
6482 }
Selim Cinek90343862018-02-01 11:07:11 -08006483
6484 /**
6485 * Validate that this style was properly composed. This is called at build time.
6486 * @hide
6487 */
6488 public void validate(Context context) {
6489 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006490
6491 /**
6492 * @hide
6493 */
6494 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006495
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006496 /**
koprivaa1a78482018-10-09 10:09:23 -07006497 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006498 * If {@code null} is returned, the default implementation will be used.
6499 *
6500 * @hide
6501 */
6502 public CharSequence getHeadsUpStatusBarText() {
6503 return null;
6504 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006505 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006506
6507 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006508 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006509 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006510 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006511 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006512 * Notification notif = new Notification.Builder(mContext)
6513 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6514 * .setContentText(subject)
6515 * .setSmallIcon(R.drawable.new_post)
6516 * .setLargeIcon(aBitmap)
6517 * .setStyle(new Notification.BigPictureStyle()
6518 * .bigPicture(aBigBitmap))
6519 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006520 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006521 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006522 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006523 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006524 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006525 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006526 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006527 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006528
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006529 public BigPictureStyle() {
6530 }
6531
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006532 /**
6533 * @deprecated use {@code BigPictureStyle()}.
6534 */
6535 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006536 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006537 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006538 }
6539
Chris Wrend6297db2012-05-03 16:20:13 -04006540 /**
6541 * Overrides ContentTitle in the big form of the template.
6542 * This defaults to the value passed to setContentTitle().
6543 */
6544 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006545 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006546 return this;
6547 }
6548
6549 /**
6550 * Set the first line of text after the detail section in the big form of the template.
6551 */
6552 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006553 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006554 return this;
6555 }
6556
Chris Wren0bd664d2012-08-01 13:56:56 -04006557 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006558 * @hide
6559 */
6560 public Bitmap getBigPicture() {
6561 return mPicture;
6562 }
6563
6564 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006565 * Provide the bitmap to be used as the payload for the BigPicture notification.
6566 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006567 public BigPictureStyle bigPicture(Bitmap b) {
6568 mPicture = b;
6569 return this;
6570 }
6571
Chris Wren3745a3d2012-05-22 15:11:52 -04006572 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006573 * Override the large icon when the big notification is shown.
6574 */
6575 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006576 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6577 }
6578
6579 /**
6580 * Override the large icon when the big notification is shown.
6581 */
6582 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006583 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006584 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006585 return this;
6586 }
6587
Riley Andrews0394a0c2015-11-03 23:36:52 -08006588 /** @hide */
6589 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6590
Daniel Sandler0ec46202015-06-24 01:27:05 -04006591 /**
6592 * @hide
6593 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006594 @Override
6595 public void purgeResources() {
6596 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006597 if (mPicture != null &&
6598 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006599 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006600 mPicture = mPicture.createAshmemBitmap();
6601 }
6602 if (mBigLargeIcon != null) {
6603 mBigLargeIcon.convertToAshmem();
6604 }
6605 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006606
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006607 /**
6608 * @hide
6609 */
Selim Cinekd0426622017-07-11 13:19:59 +02006610 @Override
6611 public void reduceImageSizes(Context context) {
6612 super.reduceImageSizes(context);
6613 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006614 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006615 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006616 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6617 ? R.dimen.notification_big_picture_max_height_low_ram
6618 : R.dimen.notification_big_picture_max_height);
6619 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6620 ? R.dimen.notification_big_picture_max_width_low_ram
6621 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006622 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6623 }
6624 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006625 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6626 ? R.dimen.notification_right_icon_size_low_ram
6627 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006628 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6629 }
6630 }
6631
6632 /**
6633 * @hide
6634 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006635 public RemoteViews makeBigContentView() {
6636 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006637 // This covers the following cases:
6638 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006639 // mN.mLargeIcon
6640 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006641 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006642 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006643 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006644 oldLargeIcon = mBuilder.mN.mLargeIcon;
6645 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006646 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6647 // replacement if the other one is null. Because we're restoring these legacy icons
6648 // for old listeners, this is in general non-null.
6649 largeIconLegacy = mBuilder.mN.largeIcon;
6650 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006651 }
6652
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006653 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006654 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006655 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006656 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006657 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6658 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006659 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006660 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006661 }
Selim Cinek279fa862016-06-14 10:57:25 -07006662 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006663
Christoph Studer5c510ee2014-12-15 16:32:27 +01006664 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006665 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006666 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006667 }
6668
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006669 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006670 return contentView;
6671 }
6672
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006673 /**
6674 * @hide
6675 */
6676 public void addExtras(Bundle extras) {
6677 super.addExtras(extras);
6678
6679 if (mBigLargeIconSet) {
6680 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6681 }
6682 extras.putParcelable(EXTRA_PICTURE, mPicture);
6683 }
6684
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006685 /**
6686 * @hide
6687 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006688 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006689 protected void restoreFromExtras(Bundle extras) {
6690 super.restoreFromExtras(extras);
6691
6692 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006693 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006694 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006695 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006696 mPicture = extras.getParcelable(EXTRA_PICTURE);
6697 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006698
6699 /**
6700 * @hide
6701 */
6702 @Override
6703 public boolean hasSummaryInHeader() {
6704 return false;
6705 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006706
6707 /**
6708 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006709 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6710 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006711 */
6712 @Override
6713 public boolean areNotificationsVisiblyDifferent(Style other) {
6714 if (other == null || getClass() != other.getClass()) {
6715 return true;
6716 }
6717 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006718 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6719 }
6720
6721 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6722 if (a == b) {
6723 return false;
6724 }
6725 if (a == null || b == null) {
6726 return true;
6727 }
6728 return a.getWidth() != b.getWidth()
6729 || a.getHeight() != b.getHeight()
6730 || a.getConfig() != b.getConfig()
6731 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006732 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006733 }
6734
6735 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006736 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006737 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006738 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006739 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006740 * Notification notif = new Notification.Builder(mContext)
6741 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6742 * .setContentText(subject)
6743 * .setSmallIcon(R.drawable.new_mail)
6744 * .setLargeIcon(aBitmap)
6745 * .setStyle(new Notification.BigTextStyle()
6746 * .bigText(aVeryLongString))
6747 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006748 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006749 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006750 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006751 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006752 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006753
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006754 private CharSequence mBigText;
6755
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006756 public BigTextStyle() {
6757 }
6758
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006759 /**
6760 * @deprecated use {@code BigTextStyle()}.
6761 */
6762 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006763 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006764 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006765 }
6766
Chris Wrend6297db2012-05-03 16:20:13 -04006767 /**
6768 * Overrides ContentTitle in the big form of the template.
6769 * This defaults to the value passed to setContentTitle().
6770 */
6771 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006772 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006773 return this;
6774 }
6775
6776 /**
6777 * Set the first line of text after the detail section in the big form of the template.
6778 */
6779 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006780 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006781 return this;
6782 }
6783
Chris Wren0bd664d2012-08-01 13:56:56 -04006784 /**
6785 * Provide the longer text to be displayed in the big form of the
6786 * template in place of the content text.
6787 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006788 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006789 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006790 return this;
6791 }
6792
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006793 /**
6794 * @hide
6795 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006796 public CharSequence getBigText() {
6797 return mBigText;
6798 }
6799
6800 /**
6801 * @hide
6802 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006803 public void addExtras(Bundle extras) {
6804 super.addExtras(extras);
6805
Christoph Studer4600f9b2014-07-22 22:44:43 +02006806 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6807 }
6808
6809 /**
6810 * @hide
6811 */
6812 @Override
6813 protected void restoreFromExtras(Bundle extras) {
6814 super.restoreFromExtras(extras);
6815
6816 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006817 }
6818
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006819 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006820 * @param increasedHeight true if this layout be created with an increased height.
6821 *
6822 * @hide
6823 */
6824 @Override
6825 public RemoteViews makeContentView(boolean increasedHeight) {
6826 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006827 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006828 mBuilder.mActions = new ArrayList<>();
6829 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006830 mBuilder.mActions = mBuilder.mOriginalActions;
6831 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006832 return remoteViews;
6833 }
6834 return super.makeContentView(increasedHeight);
6835 }
6836
6837 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006838 * @hide
6839 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006840 @Override
6841 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6842 if (increasedHeight && mBuilder.mActions.size() > 0) {
6843 return makeBigContentView();
6844 }
6845 return super.makeHeadsUpContentView(increasedHeight);
6846 }
6847
6848 /**
6849 * @hide
6850 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006851 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006852 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006853 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006854 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6855 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006856 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006857
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006858 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006859 if (TextUtils.isEmpty(bigTextText)) {
6860 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6861 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006862 bigTextText = mBuilder.processLegacyText(
6863 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006864 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006865 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6866 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6867 contentView.setViewVisibility(R.id.big_text,
6868 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006869 contentView.setBoolean(R.id.big_text, "setHasImage",
6870 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006871
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006872 return contentView;
6873 }
6874
Julia Reynolds7217dc92018-03-07 12:12:09 -05006875 /**
6876 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006877 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006878 */
6879 @Override
6880 public boolean areNotificationsVisiblyDifferent(Style other) {
6881 if (other == null || getClass() != other.getClass()) {
6882 return true;
6883 }
6884 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006885 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006886 }
6887
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006888 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006889
6890 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006891 * Helper class for generating large-format notifications that include multiple back-and-forth
6892 * messages of varying types between any number of people.
6893 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006894 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006895 * If the platform does not provide large-format notifications, this method has no effect. The
6896 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006897 *
6898 * <p>
6899 * If the app is targeting Android P and above, it is required to use the {@link Person}
6900 * class in order to get an optimal rendering of the notification and its avatars. For
6901 * conversations involving multiple people, the app should also make sure that it marks the
6902 * conversation as a group with {@link #setGroupConversation(boolean)}.
6903 *
6904 * <p>
6905 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6906 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006907 * <pre class="prettyprint">
6908 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006909 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6910 * MessagingStyle style = new MessagingStyle(user)
6911 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6912 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6913 * .setGroupConversation(hasMultiplePeople());
6914 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006915 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006916 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006917 * .setContentText(subject)
6918 * .setSmallIcon(R.drawable.new_message)
6919 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006920 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006921 * .build();
6922 * </pre>
6923 */
6924 public static class MessagingStyle extends Style {
6925
6926 /**
6927 * The maximum number of messages that will be retained in the Notification itself (the
6928 * number displayed is up to the platform).
6929 */
6930 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6931
Selim Cinekcb8b9852017-12-15 18:01:52 -08006932 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006933 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006934 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006935 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006936 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006937
6938 MessagingStyle() {
6939 }
6940
6941 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006942 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6943 * user before the posting app reposts the notification with those messages after they've
6944 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006945 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006946 *
6947 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006948 */
Alex Hillsfd590442016-10-07 09:52:44 -04006949 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006950 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006951 }
6952
6953 /**
6954 * @param user Required - The person displayed for any messages that are sent by the
6955 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6956 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006957 * by this user. The user also needs to have a valid name associated with it, which will
6958 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006959 */
6960 public MessagingStyle(@NonNull Person user) {
6961 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006962 }
6963
6964 /**
6965 * Validate that this style was properly composed. This is called at build time.
6966 * @hide
6967 */
6968 @Override
6969 public void validate(Context context) {
6970 super.validate(context);
6971 if (context.getApplicationInfo().targetSdkVersion
6972 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6973 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006974 }
6975 }
6976
6977 /**
koprivaa1a78482018-10-09 10:09:23 -07006978 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006979 * If {@code null} is returned, the default implementation will be used.
6980 *
6981 * @hide
6982 */
6983 @Override
6984 public CharSequence getHeadsUpStatusBarText() {
6985 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6986 ? super.mBigContentTitle
6987 : mConversationTitle;
6988 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6989 return conversationTitle;
6990 }
6991 return null;
6992 }
6993
6994 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006995 * @return the user to be displayed for any replies sent by the user
6996 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006997 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006998 public Person getUser() {
6999 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007000 }
7001
7002 /**
7003 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007004 *
7005 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007006 */
7007 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007008 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007009 }
7010
7011 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007012 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7013 *
Kodlee Yin14656422017-12-22 17:00:46 -08007014 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7015 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7016 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7017 * {@code true} and passing {@code null} will make it return {@code false}. In
7018 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7019 * to set group conversation status.
7020 *
7021 * @param conversationTitle Title displayed for this conversation
7022 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007023 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007024 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007025 mConversationTitle = conversationTitle;
7026 return this;
7027 }
7028
7029 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007030 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007031 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007032 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007033 public CharSequence getConversationTitle() {
7034 return mConversationTitle;
7035 }
7036
7037 /**
7038 * Adds a message for display by this notification. Convenience call for a simple
7039 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7040 * @param text A {@link CharSequence} to be displayed as the message content
7041 * @param timestamp Time at which the message arrived
7042 * @param sender A {@link CharSequence} to be used for displaying the name of the
7043 * sender. Should be <code>null</code> for messages by the current user, in which case
7044 * the platform will insert {@link #getUserDisplayName()}.
7045 * Should be unique amongst all individuals in the conversation, and should be
7046 * consistent during re-posts of the notification.
7047 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007048 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007049 *
7050 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007051 *
7052 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007053 */
7054 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007055 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007056 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007057 }
7058
7059 /**
7060 * Adds a message for display by this notification. Convenience call for a simple
7061 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7062 * @param text A {@link CharSequence} to be displayed as the message content
7063 * @param timestamp Time at which the message arrived
7064 * @param sender The {@link Person} who sent the message.
7065 * Should be <code>null</code> for messages by the current user, in which case
7066 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7067 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007068 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007069 *
7070 * @return this object for method chaining
7071 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007072 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7073 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007074 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007075 }
7076
7077 /**
7078 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007079 *
7080 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7081 * the newest last.
7082 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007083 * @param message The {@link Message} to be displayed
7084 * @return this object for method chaining
7085 */
7086 public MessagingStyle addMessage(Message message) {
7087 mMessages.add(message);
7088 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7089 mMessages.remove(0);
7090 }
7091 return this;
7092 }
7093
7094 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007095 * Adds a {@link Message} for historic context in this notification.
7096 *
7097 * <p>Messages should be added as historic if they are not the main subject of the
7098 * notification but may give context to a conversation. The system may choose to present
7099 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7100 *
7101 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7102 * the newest last.
7103 *
7104 * @param message The historic {@link Message} to be added
7105 * @return this object for method chaining
7106 */
7107 public MessagingStyle addHistoricMessage(Message message) {
7108 mHistoricMessages.add(message);
7109 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7110 mHistoricMessages.remove(0);
7111 }
7112 return this;
7113 }
7114
7115 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007116 * Gets the list of {@code Message} objects that represent the notification
7117 */
7118 public List<Message> getMessages() {
7119 return mMessages;
7120 }
7121
7122 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007123 * Gets the list of historic {@code Message}s in the notification.
7124 */
7125 public List<Message> getHistoricMessages() {
7126 return mHistoricMessages;
7127 }
7128
7129 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007130 * Sets whether this conversation notification represents a group. If the app is targeting
7131 * Android P, this is required if the app wants to display the largeIcon set with
7132 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007133 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007134 * @param isGroupConversation {@code true} if the conversation represents a group,
7135 * {@code false} otherwise.
7136 * @return this object for method chaining
7137 */
7138 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7139 mIsGroupConversation = isGroupConversation;
7140 return this;
7141 }
7142
7143 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007144 * Returns {@code true} if this notification represents a group conversation, otherwise
7145 * {@code false}.
7146 *
7147 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7148 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7149 * not the conversation title is set; returning {@code true} if the conversation title is
7150 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7151 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7152 * named, non-group conversations.
7153 *
7154 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007155 */
7156 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007157 // When target SDK version is < P, a non-null conversation title dictates if this is
7158 // as group conversation.
7159 if (mBuilder != null
7160 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7161 < Build.VERSION_CODES.P) {
7162 return mConversationTitle != null;
7163 }
7164
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007165 return mIsGroupConversation;
7166 }
7167
7168 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007169 * @hide
7170 */
7171 @Override
7172 public void addExtras(Bundle extras) {
7173 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007174 if (mUser != null) {
7175 // For legacy usages
7176 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7177 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007178 }
7179 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007180 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007181 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007182 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7183 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007184 }
Adrian Roos437cd562017-01-18 15:47:03 -08007185 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7186 Message.getBundleArrayForMessages(mHistoricMessages));
7187 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007188
7189 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007190 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007191 }
7192
7193 private void fixTitleAndTextExtras(Bundle extras) {
7194 Message m = findLatestIncomingMessage();
7195 CharSequence text = (m == null) ? null : m.mText;
7196 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007197 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7198 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007199 CharSequence title;
7200 if (!TextUtils.isEmpty(mConversationTitle)) {
7201 if (!TextUtils.isEmpty(sender)) {
7202 BidiFormatter bidi = BidiFormatter.getInstance();
7203 title = mBuilder.mContext.getString(
7204 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007205 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007206 } else {
7207 title = mConversationTitle;
7208 }
7209 } else {
7210 title = sender;
7211 }
7212
7213 if (title != null) {
7214 extras.putCharSequence(EXTRA_TITLE, title);
7215 }
7216 if (text != null) {
7217 extras.putCharSequence(EXTRA_TEXT, text);
7218 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007219 }
7220
7221 /**
7222 * @hide
7223 */
7224 @Override
7225 protected void restoreFromExtras(Bundle extras) {
7226 super.restoreFromExtras(extras);
7227
Selim Cinekcb8b9852017-12-15 18:01:52 -08007228 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7229 if (mUser == null) {
7230 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007231 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007232 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007233 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007234 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007235 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007236 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007237 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007238 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007239 }
7240
7241 /**
7242 * @hide
7243 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007244 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007245 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007246 mBuilder.mOriginalActions = mBuilder.mActions;
7247 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007248 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007249 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007250 mBuilder.mActions = mBuilder.mOriginalActions;
7251 mBuilder.mOriginalActions = null;
7252 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007253 }
7254
Julia Reynolds7217dc92018-03-07 12:12:09 -05007255 /**
7256 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007257 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007258 */
7259 @Override
7260 public boolean areNotificationsVisiblyDifferent(Style other) {
7261 if (other == null || getClass() != other.getClass()) {
7262 return true;
7263 }
7264 MessagingStyle newS = (MessagingStyle) other;
7265 List<MessagingStyle.Message> oldMs = getMessages();
7266 List<MessagingStyle.Message> newMs = newS.getMessages();
7267
Dan Sandler7d67bd42018-05-15 14:06:38 -04007268 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007269 newMs = new ArrayList<>();
7270 }
7271
7272 int n = oldMs.size();
7273 if (n != newMs.size()) {
7274 return true;
7275 }
7276 for (int i = 0; i < n; i++) {
7277 MessagingStyle.Message oldM = oldMs.get(i);
7278 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007279 if (!Objects.equals(
7280 String.valueOf(oldM.getText()),
7281 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007282 return true;
7283 }
7284 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7285 return true;
7286 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007287 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7288 ? oldM.getSender()
7289 : oldM.getSenderPerson().getName());
7290 String newSender = String.valueOf(newM.getSenderPerson() == null
7291 ? newM.getSender()
7292 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007293 if (!Objects.equals(oldSender, newSender)) {
7294 return true;
7295 }
7296
7297 String oldKey = oldM.getSenderPerson() == null
7298 ? null : oldM.getSenderPerson().getKey();
7299 String newKey = newM.getSenderPerson() == null
7300 ? null : newM.getSenderPerson().getKey();
7301 if (!Objects.equals(oldKey, newKey)) {
7302 return true;
7303 }
7304 // Other fields (like timestamp) intentionally excluded
7305 }
7306 return false;
7307 }
7308
Adrian Roosc1a80b02016-04-05 14:54:55 -07007309 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007310 return findLatestIncomingMessage(mMessages);
7311 }
7312
7313 /**
7314 * @hide
7315 */
7316 @Nullable
7317 public static Message findLatestIncomingMessage(
7318 List<Message> messages) {
7319 for (int i = messages.size() - 1; i >= 0; i--) {
7320 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007321 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007322 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007323 return m;
7324 }
7325 }
Selim Cinek88188f22017-09-19 16:46:56 -07007326 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007327 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007328 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007329 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007330 return null;
7331 }
7332
7333 /**
7334 * @hide
7335 */
7336 @Override
7337 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007338 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007339 }
7340
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007341 /**
7342 * Create a messaging layout.
7343 *
7344 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7345 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007346 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007347 * @return the created remoteView.
7348 */
Selim Cinekafeed292017-12-12 17:32:44 -08007349 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007350 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007351 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007352 ? super.mBigContentTitle
7353 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007354 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7355 >= Build.VERSION_CODES.P;
7356 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007357 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007358 Icon avatarReplacement = null;
7359 if (!atLeastP) {
7360 isOneToOne = TextUtils.isEmpty(conversationTitle);
7361 avatarReplacement = mBuilder.mN.mLargeIcon;
7362 if (hasOnlyWhiteSpaceSenders()) {
7363 isOneToOne = true;
7364 nameReplacement = conversationTitle;
7365 conversationTitle = null;
7366 }
7367 } else {
7368 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007369 }
Selim Cinek384804b2018-04-18 14:31:07 +08007370 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007371 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7372 conversationTitle).text(null)
7373 .hideLargeIcon(hideRightIcons || isOneToOne)
7374 .hideReplyIcon(hideRightIcons)
7375 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007376 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007377 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007378 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007379 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007380 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007381 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007382 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7383 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007384 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007385 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7386 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007387 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007388 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007389 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007390 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007391 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7392 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007393 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7394 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007395 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7396 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007397 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7398 isOneToOne);
7399 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7400 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007401 return contentView;
7402 }
7403
Selim Cinekf7409db2017-10-24 16:17:14 -07007404 private boolean hasOnlyWhiteSpaceSenders() {
7405 for (int i = 0; i < mMessages.size(); i++) {
7406 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007407 Person sender = m.getSenderPerson();
7408 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007409 return false;
7410 }
7411 }
7412 return true;
7413 }
7414
7415 private boolean isWhiteSpace(CharSequence sender) {
7416 if (TextUtils.isEmpty(sender)) {
7417 return true;
7418 }
7419 if (sender.toString().matches("^\\s*$")) {
7420 return true;
7421 }
7422 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7423 // For the presentation that we had.
7424 for (int i = 0; i < sender.length(); i++) {
7425 char c = sender.charAt(i);
7426 if (c != '\u200B') {
7427 return false;
7428 }
7429 }
7430 return true;
7431 }
7432
Selim Cinek88188f22017-09-19 16:46:56 -07007433 private CharSequence createConversationTitleFromMessages() {
7434 ArraySet<CharSequence> names = new ArraySet<>();
7435 for (int i = 0; i < mMessages.size(); i++) {
7436 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007437 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007438 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007439 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007440 }
7441 }
7442 SpannableStringBuilder title = new SpannableStringBuilder();
7443 int size = names.size();
7444 for (int i = 0; i < size; i++) {
7445 CharSequence name = names.valueAt(i);
7446 if (!TextUtils.isEmpty(title)) {
7447 title.append(", ");
7448 }
7449 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7450 }
7451 return title;
7452 }
7453
Adrian Roosdedd1df2016-04-26 16:38:47 -07007454 /**
7455 * @hide
7456 */
7457 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007458 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007459 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007460 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007461 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7462 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007463 }
7464
Adrian Roosc1a80b02016-04-05 14:54:55 -07007465 private static TextAppearanceSpan makeFontColorSpan(int color) {
7466 return new TextAppearanceSpan(null, 0, 0,
7467 ColorStateList.valueOf(color), null);
7468 }
7469
Alex Hillsd9b04d92016-04-11 16:38:16 -04007470 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007471 /** @hide */
7472 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007473 static final String KEY_TIMESTAMP = "time";
7474 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007475 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007476 static final String KEY_DATA_MIME_TYPE = "type";
7477 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007478 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007479 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007480
7481 private final CharSequence mText;
7482 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007483 @Nullable
7484 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007485 /** True if this message was generated from the extra
7486 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7487 */
7488 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007489
Shane Brennan5a871862017-03-11 13:14:17 -08007490 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007491 private String mDataMimeType;
7492 private Uri mDataUri;
7493
7494 /**
7495 * Constructor
7496 * @param text A {@link CharSequence} to be displayed as the message content
7497 * @param timestamp Time at which the message arrived
7498 * @param sender A {@link CharSequence} to be used for displaying the name of the
7499 * sender. Should be <code>null</code> for messages by the current user, in which case
7500 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7501 * Should be unique amongst all individuals in the conversation, and should be
7502 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007503 *
7504 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007505 */
7506 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007507 this(text, timestamp, sender == null ? null
7508 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007509 }
7510
7511 /**
7512 * Constructor
7513 * @param text A {@link CharSequence} to be displayed as the message content
7514 * @param timestamp Time at which the message arrived
7515 * @param sender The {@link Person} who sent the message.
7516 * Should be <code>null</code> for messages by the current user, in which case
7517 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7518 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007519 * The person provided should contain an Icon, set with
7520 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7521 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7522 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7523 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007524 * </p>
7525 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007526 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007527 this(text, timestamp, sender, false /* remoteHistory */);
7528 }
7529
7530 /**
7531 * Constructor
7532 * @param text A {@link CharSequence} to be displayed as the message content
7533 * @param timestamp Time at which the message arrived
7534 * @param sender The {@link Person} who sent the message.
7535 * Should be <code>null</code> for messages by the current user, in which case
7536 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7537 * @param remoteInputHistory True if the messages was generated from the extra
7538 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7539 * <p>
7540 * The person provided should contain an Icon, set with
7541 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7542 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7543 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7544 * to differentiate between the different users.
7545 * </p>
7546 * @hide
7547 */
7548 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7549 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007550 mText = text;
7551 mTimestamp = timestamp;
7552 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007553 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007554 }
7555
7556 /**
7557 * Sets a binary blob of data and an associated MIME type for a message. In the case
7558 * where the platform doesn't support the MIME type, the original text provided in the
7559 * constructor will be used.
7560 * @param dataMimeType The MIME type of the content. See
7561 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7562 * types on Android and Android Wear.
7563 * @param dataUri The uri containing the content whose type is given by the MIME type.
7564 * <p class="note">
7565 * <ol>
7566 * <li>Notification Listeners including the System UI need permission to access the
7567 * data the Uri points to. The recommended ways to do this are:</li>
7568 * <li>Store the data in your own ContentProvider, making sure that other apps have
7569 * the correct permission to access your provider. The preferred mechanism for
7570 * providing access is to use per-URI permissions which are temporary and only
7571 * grant access to the receiving application. An easy way to create a
7572 * ContentProvider like this is to use the FileProvider helper class.</li>
7573 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7574 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7575 * also store non-media types (see MediaStore.Files for more info). Files can be
7576 * inserted into the MediaStore using scanFile() after which a content:// style
7577 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7578 * Note that once added to the system MediaStore the content is accessible to any
7579 * app on the device.</li>
7580 * </ol>
7581 * @return this object for method chaining
7582 */
7583 public Message setData(String dataMimeType, Uri dataUri) {
7584 mDataMimeType = dataMimeType;
7585 mDataUri = dataUri;
7586 return this;
7587 }
7588
Alex Hillsfc737de2016-03-23 17:33:02 -04007589 /**
7590 * Get the text to be used for this message, or the fallback text if a type and content
7591 * Uri have been set
7592 */
7593 public CharSequence getText() {
7594 return mText;
7595 }
7596
7597 /**
7598 * Get the time at which this message arrived
7599 */
7600 public long getTimestamp() {
7601 return mTimestamp;
7602 }
7603
7604 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007605 * Get the extras Bundle for this message.
7606 */
7607 public Bundle getExtras() {
7608 return mExtras;
7609 }
7610
7611 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007612 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007613 *
7614 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007615 */
7616 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007617 return mSender == null ? null : mSender.getName();
7618 }
7619
7620 /**
7621 * Get the sender associated with this message.
7622 */
7623 @Nullable
7624 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007625 return mSender;
7626 }
7627
7628 /**
7629 * Get the MIME type of the data pointed to by the Uri
7630 */
7631 public String getDataMimeType() {
7632 return mDataMimeType;
7633 }
7634
7635 /**
koprivaa1a78482018-10-09 10:09:23 -07007636 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007637 * {@see #getText()} is used.
7638 */
7639 public Uri getDataUri() {
7640 return mDataUri;
7641 }
7642
Kenny Guya0f6de82018-04-06 16:20:16 +01007643 /**
7644 * @return True if the message was generated from
7645 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7646 * @hide
7647 */
7648 public boolean isRemoteInputHistory() {
7649 return mRemoteInputHistory;
7650 }
7651
Beverlye98937a2018-11-15 10:18:57 -05007652 /**
7653 * @hide
7654 */
7655 @VisibleForTesting
7656 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007657 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007658 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007659 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007660 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007661 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007662 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007663 // Legacy listeners need this
7664 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7665 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007666 }
7667 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007668 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007669 }
7670 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007671 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007672 }
Shane Brennan5a871862017-03-11 13:14:17 -08007673 if (mExtras != null) {
7674 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7675 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007676 if (mRemoteInputHistory) {
7677 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7678 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007679 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007680 }
7681
Alex Hillsd9b04d92016-04-11 16:38:16 -04007682 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7683 Bundle[] bundles = new Bundle[messages.size()];
7684 final int N = messages.size();
7685 for (int i = 0; i < N; i++) {
7686 bundles[i] = messages.get(i).toBundle();
7687 }
7688 return bundles;
7689 }
7690
Selim Cinek88188f22017-09-19 16:46:56 -07007691 /**
7692 * @return A list of messages read from the bundles.
7693 *
7694 * @hide
7695 */
7696 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7697 if (bundles == null) {
7698 return new ArrayList<>();
7699 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007700 List<Message> messages = new ArrayList<>(bundles.length);
7701 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007702 if (bundles[i] instanceof Bundle) {
7703 Message message = getMessageFromBundle((Bundle)bundles[i]);
7704 if (message != null) {
7705 messages.add(message);
7706 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007707 }
7708 }
7709 return messages;
7710 }
7711
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007712 /**
7713 * @return The message that is stored in the bundle or null if the message couldn't be
7714 * resolved.
7715 *
7716 * @hide
7717 */
7718 @Nullable
7719 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007720 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007721 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007722 return null;
7723 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007724
7725 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7726 if (senderPerson == null) {
7727 // Legacy apps that use compat don't actually provide the sender objects
7728 // We need to fix the compat version to provide people / use
7729 // the native api instead
7730 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7731 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007732 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007733 }
7734 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007735 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007736 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007737 senderPerson,
7738 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007739 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7740 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007741 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7742 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007743 }
Shane Brennan5a871862017-03-11 13:14:17 -08007744 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7745 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7746 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007747 return message;
7748 }
7749 } catch (ClassCastException e) {
7750 return null;
7751 }
7752 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007753 }
7754 }
7755
7756 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007757 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007758 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007759 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007760 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007761 * Notification notif = new Notification.Builder(mContext)
7762 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7763 * .setContentText(subject)
7764 * .setSmallIcon(R.drawable.new_mail)
7765 * .setLargeIcon(aBitmap)
7766 * .setStyle(new Notification.InboxStyle()
7767 * .addLine(str1)
7768 * .addLine(str2)
7769 * .setContentTitle(&quot;&quot;)
7770 * .setSummaryText(&quot;+3 more&quot;))
7771 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007772 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007773 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007774 * @see Notification#bigContentView
7775 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007776 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007777
7778 /**
7779 * The number of lines of remote input history allowed until we start reducing lines.
7780 */
7781 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007782 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7783
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007784 public InboxStyle() {
7785 }
7786
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007787 /**
7788 * @deprecated use {@code InboxStyle()}.
7789 */
7790 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007791 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007792 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007793 }
7794
Chris Wrend6297db2012-05-03 16:20:13 -04007795 /**
7796 * Overrides ContentTitle in the big form of the template.
7797 * This defaults to the value passed to setContentTitle().
7798 */
7799 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007800 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007801 return this;
7802 }
7803
7804 /**
7805 * Set the first line of text after the detail section in the big form of the template.
7806 */
7807 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007808 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007809 return this;
7810 }
7811
Chris Wren0bd664d2012-08-01 13:56:56 -04007812 /**
7813 * Append a line to the digest section of the Inbox notification.
7814 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007815 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007816 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007817 return this;
7818 }
7819
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007820 /**
7821 * @hide
7822 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007823 public ArrayList<CharSequence> getLines() {
7824 return mTexts;
7825 }
7826
7827 /**
7828 * @hide
7829 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007830 public void addExtras(Bundle extras) {
7831 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007832
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007833 CharSequence[] a = new CharSequence[mTexts.size()];
7834 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7835 }
7836
Christoph Studer4600f9b2014-07-22 22:44:43 +02007837 /**
7838 * @hide
7839 */
7840 @Override
7841 protected void restoreFromExtras(Bundle extras) {
7842 super.restoreFromExtras(extras);
7843
7844 mTexts.clear();
7845 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7846 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7847 }
7848 }
7849
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007850 /**
7851 * @hide
7852 */
7853 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007854 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007855 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007856 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007857
Chris Wrend6297db2012-05-03 16:20:13 -04007858 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 -04007859 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007860
Chris Wren4ed80d52012-05-17 09:30:03 -04007861 // Make sure all rows are gone in case we reuse a view.
7862 for (int rowId : rowIds) {
7863 contentView.setViewVisibility(rowId, View.GONE);
7864 }
7865
Daniel Sandler879c5e02012-04-17 16:46:51 -04007866 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007867 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7868 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007869 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007870 int onlyViewId = 0;
7871 int maxRows = rowIds.length;
7872 if (mBuilder.mActions.size() > 0) {
7873 maxRows--;
7874 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007875 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7876 EXTRA_REMOTE_INPUT_HISTORY);
7877 if (remoteInputHistory != null
7878 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7879 // Let's remove some messages to make room for the remote input history.
7880 // 1 is always able to fit, but let's remove them if they are 2 or 3
7881 int numRemoteInputs = Math.min(remoteInputHistory.length,
7882 MAX_REMOTE_INPUT_HISTORY_LINES);
7883 int totalNumRows = mTexts.size() + numRemoteInputs
7884 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7885 if (totalNumRows > maxRows) {
7886 int overflow = totalNumRows - maxRows;
7887 if (mTexts.size() > maxRows) {
7888 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7889 // few messages, even with the remote input
7890 maxRows -= overflow;
7891 } else {
7892 // otherwise we drop the first messages
7893 i = overflow;
7894 }
7895 }
7896 }
Selim Cinek07c80172016-04-21 16:40:47 -07007897 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007898 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007899 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007900 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007901 contentView.setTextViewText(rowIds[i],
7902 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007903 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007904 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007905 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007906 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007907 if (first) {
7908 onlyViewId = rowIds[i];
7909 } else {
7910 onlyViewId = 0;
7911 }
Selim Cinek247fa012016-02-18 09:50:48 -08007912 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007913 }
7914 i++;
7915 }
Selim Cinek07c80172016-04-21 16:40:47 -07007916 if (onlyViewId != 0) {
7917 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7918 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7919 R.dimen.notification_text_margin_top);
7920 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7921 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007922
Daniel Sandler879c5e02012-04-17 16:46:51 -04007923 return contentView;
7924 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007925
Julia Reynolds7217dc92018-03-07 12:12:09 -05007926 /**
7927 * @hide
7928 */
7929 @Override
7930 public boolean areNotificationsVisiblyDifferent(Style other) {
7931 if (other == null || getClass() != other.getClass()) {
7932 return true;
7933 }
7934 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007935
7936 final ArrayList<CharSequence> myLines = getLines();
7937 final ArrayList<CharSequence> newLines = newS.getLines();
7938 final int n = myLines.size();
7939 if (n != newLines.size()) {
7940 return true;
7941 }
7942
7943 for (int i = 0; i < n; i++) {
7944 if (!Objects.equals(
7945 String.valueOf(myLines.get(i)),
7946 String.valueOf(newLines.get(i)))) {
7947 return true;
7948 }
7949 }
7950 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007951 }
7952
Selim Cinek384804b2018-04-18 14:31:07 +08007953 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007954 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007955 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007956 if (first) {
7957 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7958 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7959 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007960 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007961 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007962 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007963 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007964 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007965 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007966 }
Dan Sandler842dd772014-05-15 09:36:47 -04007967
7968 /**
7969 * Notification style for media playback notifications.
7970 *
7971 * In the expanded form, {@link Notification#bigContentView}, up to 5
7972 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007973 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007974 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7975 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7976 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007977 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007978 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7979 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007980 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007981 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007982 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007983 * Notifications created with MediaStyle will have their category set to
7984 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7985 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007986 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007987 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7988 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007989 * the System UI can identify this as a notification representing an active media session
7990 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7991 *
Selim Cinek99104832017-01-25 14:47:33 -08007992 * <p>
7993 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7994 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7995 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7996 * <p>
7997 *
Dan Sandler842dd772014-05-15 09:36:47 -04007998 * To use this style with your Notification, feed it to
7999 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8000 * <pre class="prettyprint">
8001 * Notification noti = new Notification.Builder()
8002 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008003 * .setContentTitle(&quot;Track title&quot;)
8004 * .setContentText(&quot;Artist - Album&quot;)
8005 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008006 * .setStyle(<b>new Notification.MediaStyle()</b>
8007 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008008 * .build();
8009 * </pre>
8010 *
8011 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008012 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008013 */
8014 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008015 // Changing max media buttons requires also changing templates
8016 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008017 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008018 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008019 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8020 R.id.action0,
8021 R.id.action1,
8022 R.id.action2,
8023 R.id.action3,
8024 R.id.action4,
8025 };
Dan Sandler842dd772014-05-15 09:36:47 -04008026
8027 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008028 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008029
8030 public MediaStyle() {
8031 }
8032
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008033 /**
8034 * @deprecated use {@code MediaStyle()}.
8035 */
8036 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008037 public MediaStyle(Builder builder) {
8038 setBuilder(builder);
8039 }
8040
8041 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008042 * 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 -04008043 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008044 *
8045 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008046 */
8047 public MediaStyle setShowActionsInCompactView(int...actions) {
8048 mActionsToShowInCompact = actions;
8049 return this;
8050 }
8051
8052 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008053 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8054 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008055 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008056 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008057 mToken = token;
8058 return this;
8059 }
8060
Christoph Studer4600f9b2014-07-22 22:44:43 +02008061 /**
8062 * @hide
8063 */
Dan Sandler842dd772014-05-15 09:36:47 -04008064 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008065 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008066 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008067 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008068 if (wip.category == null) {
8069 wip.category = Notification.CATEGORY_TRANSPORT;
8070 }
Dan Sandler842dd772014-05-15 09:36:47 -04008071 return wip;
8072 }
8073
Christoph Studer4600f9b2014-07-22 22:44:43 +02008074 /**
8075 * @hide
8076 */
8077 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008078 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008079 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008080 }
8081
8082 /**
8083 * @hide
8084 */
8085 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008086 public RemoteViews makeBigContentView() {
8087 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008088 }
8089
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008090 /**
8091 * @hide
8092 */
8093 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008094 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008095 RemoteViews expanded = makeMediaBigContentView();
8096 return expanded != null ? expanded : makeMediaContentView();
8097 }
8098
Dan Sandler842dd772014-05-15 09:36:47 -04008099 /** @hide */
8100 @Override
8101 public void addExtras(Bundle extras) {
8102 super.addExtras(extras);
8103
8104 if (mToken != null) {
8105 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8106 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008107 if (mActionsToShowInCompact != null) {
8108 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8109 }
Dan Sandler842dd772014-05-15 09:36:47 -04008110 }
8111
Christoph Studer4600f9b2014-07-22 22:44:43 +02008112 /**
8113 * @hide
8114 */
8115 @Override
8116 protected void restoreFromExtras(Bundle extras) {
8117 super.restoreFromExtras(extras);
8118
8119 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8120 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8121 }
8122 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8123 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8124 }
8125 }
8126
Julia Reynolds7217dc92018-03-07 12:12:09 -05008127 /**
8128 * @hide
8129 */
8130 @Override
8131 public boolean areNotificationsVisiblyDifferent(Style other) {
8132 if (other == null || getClass() != other.getClass()) {
8133 return true;
8134 }
8135 // All fields to compare are on the Notification object
8136 return false;
8137 }
8138
Gus Prevas9cc96602018-10-11 11:24:23 -04008139 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008140 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008141 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008142 container.setViewVisibility(buttonId, View.VISIBLE);
8143 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008144
8145 // If the action buttons should not be tinted, then just use the default
8146 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008147 Resources resources = mBuilder.mContext.getResources();
8148 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008149 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8150 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008151 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8152 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008153 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008154 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008155
Gus Prevas9cc96602018-10-11 11:24:23 -04008156 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008157 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008158
8159 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8160 new int[]{ android.R.attr.colorControlHighlight });
8161 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8162 typedArray.recycle();
8163 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8164 Color.blue(tintColor));
8165 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8166
Dan Sandler842dd772014-05-15 09:36:47 -04008167 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008168 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008169 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008170 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008171 }
8172
8173 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008174 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8175 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008176 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008177 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008178 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008179
8180 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008181 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008182 ? 0
8183 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008184 if (numActionsToShow > numActions) {
8185 throw new IllegalArgumentException(String.format(
8186 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8187 numActions, numActions - 1));
8188 }
8189 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8190 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008191 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008192 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008193 } else {
8194 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008195 }
8196 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008197 handleImage(view);
8198 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008199 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008200 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008201 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008202 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008203 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008204 return view;
8205 }
8206
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008207 private int getActionColor(StandardTemplateParams p) {
8208 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8209 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008210 }
8211
Dan Sandler842dd772014-05-15 09:36:47 -04008212 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008213 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008214 // Dont add an expanded view if there is no more content to be revealed
8215 int actionsInCompact = mActionsToShowInCompact == null
8216 ? 0
8217 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008218 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008219 return null;
8220 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008221 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8222 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008223 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008224 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008225
Gus Prevas9cc96602018-10-11 11:24:23 -04008226 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8227 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008228 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008229 } else {
8230 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008231 }
8232 }
linanson2bcd4032019-01-14 15:22:04 +08008233 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8234 mBuilder.mContext.getString(
8235 com.android.internal.R.string.ext_media_seamless_action), null), p);
8236 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008237 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008238 return big;
8239 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008240
Selim Cinek5bf069a2015-11-10 19:14:27 -05008241 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008242 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008243 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8244 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008245 }
8246 }
8247
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008248 /**
8249 * @hide
8250 */
8251 @Override
8252 protected boolean hasProgress() {
8253 return false;
8254 }
Dan Sandler842dd772014-05-15 09:36:47 -04008255 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008256
Selim Cinek593610c2016-02-16 18:42:57 -08008257 /**
8258 * Notification style for custom views that are decorated by the system
8259 *
8260 * <p>Instead of providing a notification that is completely custom, a developer can set this
8261 * style and still obtain system decorations like the notification header with the expand
8262 * affordance and actions.
8263 *
8264 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8265 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8266 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8267 * corresponding custom views to display.
8268 *
8269 * To use this style with your Notification, feed it to
8270 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8271 * <pre class="prettyprint">
8272 * Notification noti = new Notification.Builder()
8273 * .setSmallIcon(R.drawable.ic_stat_player)
8274 * .setLargeIcon(albumArtBitmap))
8275 * .setCustomContentView(contentView);
8276 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8277 * .build();
8278 * </pre>
8279 */
8280 public static class DecoratedCustomViewStyle extends Style {
8281
8282 public DecoratedCustomViewStyle() {
8283 }
8284
Selim Cinek593610c2016-02-16 18:42:57 -08008285 /**
8286 * @hide
8287 */
8288 public boolean displayCustomViewInline() {
8289 return true;
8290 }
8291
8292 /**
8293 * @hide
8294 */
8295 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008296 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008297 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8298 }
8299
8300 /**
8301 * @hide
8302 */
8303 @Override
8304 public RemoteViews makeBigContentView() {
8305 return makeDecoratedBigContentView();
8306 }
8307
8308 /**
8309 * @hide
8310 */
8311 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008312 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008313 return makeDecoratedHeadsUpContentView();
8314 }
8315
Selim Cinek593610c2016-02-16 18:42:57 -08008316 private RemoteViews makeDecoratedHeadsUpContentView() {
8317 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8318 ? mBuilder.mN.contentView
8319 : mBuilder.mN.headsUpContentView;
8320 if (mBuilder.mActions.size() == 0) {
8321 return makeStandardTemplateWithCustomContent(headsUpContentView);
8322 }
Selim Cinek384804b2018-04-18 14:31:07 +08008323 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008324 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008325 mBuilder.getBigBaseLayoutResource(), result);
8326 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008327 return remoteViews;
8328 }
8329
Selim Cinek593610c2016-02-16 18:42:57 -08008330 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008331 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008332 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008333 mBuilder.getBaseLayoutResource(), result);
8334 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008335 return remoteViews;
8336 }
8337
Selim Cinek593610c2016-02-16 18:42:57 -08008338 private RemoteViews makeDecoratedBigContentView() {
8339 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8340 ? mBuilder.mN.contentView
8341 : mBuilder.mN.bigContentView;
8342 if (mBuilder.mActions.size() == 0) {
8343 return makeStandardTemplateWithCustomContent(bigContentView);
8344 }
Selim Cinek384804b2018-04-18 14:31:07 +08008345 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008346 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008347 mBuilder.getBigBaseLayoutResource(), result);
8348 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008349 return remoteViews;
8350 }
Selim Cinek247fa012016-02-18 09:50:48 -08008351
8352 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008353 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008354 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008355 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008356 // Need to clone customContent before adding, because otherwise it can no longer be
8357 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008358 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008359 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8360 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008361 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008362 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008363 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008364 remoteViews.setIntTag(R.id.notification_main_column,
8365 com.android.internal.R.id.notification_custom_view_index_tag,
8366 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008367 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008368 Resources resources = mBuilder.mContext.getResources();
8369 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008370 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008371 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008372 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008373
8374 /**
8375 * @hide
8376 */
8377 @Override
8378 public boolean areNotificationsVisiblyDifferent(Style other) {
8379 if (other == null || getClass() != other.getClass()) {
8380 return true;
8381 }
8382 // Comparison done for all custom RemoteViews, independent of style
8383 return false;
8384 }
Selim Cinek593610c2016-02-16 18:42:57 -08008385 }
8386
Selim Cinek03eb3b72016-02-18 10:39:45 -08008387 /**
8388 * Notification style for media custom views that are decorated by the system
8389 *
8390 * <p>Instead of providing a media notification that is completely custom, a developer can set
8391 * this style and still obtain system decorations like the notification header with the expand
8392 * affordance and actions.
8393 *
8394 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8395 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8396 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8397 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008398 * <p>
8399 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8400 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8401 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008402 * To use this style with your Notification, feed it to
8403 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8404 * <pre class="prettyprint">
8405 * Notification noti = new Notification.Builder()
8406 * .setSmallIcon(R.drawable.ic_stat_player)
8407 * .setLargeIcon(albumArtBitmap))
8408 * .setCustomContentView(contentView);
8409 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8410 * .setMediaSession(mySession))
8411 * .build();
8412 * </pre>
8413 *
8414 * @see android.app.Notification.DecoratedCustomViewStyle
8415 * @see android.app.Notification.MediaStyle
8416 */
8417 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8418
8419 public DecoratedMediaCustomViewStyle() {
8420 }
8421
Selim Cinek03eb3b72016-02-18 10:39:45 -08008422 /**
8423 * @hide
8424 */
8425 public boolean displayCustomViewInline() {
8426 return true;
8427 }
8428
8429 /**
8430 * @hide
8431 */
8432 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008433 public RemoteViews makeContentView(boolean increasedHeight) {
8434 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008435 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8436 mBuilder.mN.contentView);
8437 }
8438
8439 /**
8440 * @hide
8441 */
8442 @Override
8443 public RemoteViews makeBigContentView() {
8444 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8445 ? mBuilder.mN.bigContentView
8446 : mBuilder.mN.contentView;
8447 return makeBigContentViewWithCustomContent(customRemoteView);
8448 }
8449
8450 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8451 RemoteViews remoteViews = super.makeBigContentView();
8452 if (remoteViews != null) {
8453 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8454 customRemoteView);
8455 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008456 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008457 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8458 customRemoteView);
8459 } else {
8460 return null;
8461 }
8462 }
8463
8464 /**
8465 * @hide
8466 */
8467 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008468 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008469 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8470 ? mBuilder.mN.headsUpContentView
8471 : mBuilder.mN.contentView;
8472 return makeBigContentViewWithCustomContent(customRemoteView);
8473 }
8474
Julia Reynolds7217dc92018-03-07 12:12:09 -05008475 /**
8476 * @hide
8477 */
8478 @Override
8479 public boolean areNotificationsVisiblyDifferent(Style other) {
8480 if (other == null || getClass() != other.getClass()) {
8481 return true;
8482 }
8483 // Comparison done for all custom RemoteViews, independent of style
8484 return false;
8485 }
8486
Selim Cinek03eb3b72016-02-18 10:39:45 -08008487 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8488 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008489 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008490 // Need to clone customContent before adding, because otherwise it can no longer be
8491 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008492 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008493 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008494 remoteViews.removeAllViews(id);
8495 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008496 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008497 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008498 return remoteViews;
8499 }
8500 }
8501
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008502 /**
8503 * Encapsulates the information needed to display a notification as a bubble.
8504 *
8505 * <p>A bubble is used to display app content in a floating window over the existing
8506 * foreground activity. A bubble has a collapsed state represented by an icon,
8507 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8508 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8509 *
8510 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8511 * outside of the notification shade on unlocked devices. When a user interacts with the
8512 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8513 *
8514 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8515 */
8516 public static final class BubbleMetadata implements Parcelable {
8517
8518 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008519 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008520 private Icon mIcon;
8521 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008522 private int mFlags;
8523
8524 /**
8525 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8526 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8527 *
8528 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8529 *
8530 * <p>Generally this flag should only be set if the user has performed an action to request
8531 * or create a bubble.</p>
8532 */
8533 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8534
8535 /**
8536 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8537 * <b>without</b> the associated notification in the notification shade. Subsequent update
8538 * notifications to this bubble will post a notification in the shade.
8539 *
8540 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8541 *
8542 * <p>Generally this flag should only be set if the user has performed an action to request
8543 * or create a bubble.</p>
8544 */
8545 private static final int FLAG_SUPPRESS_INITIAL_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008546
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008547 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor138f4c92019-02-20 16:12:17 -08008548 Icon icon, int height) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008549 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008550 mIcon = icon;
8551 mDesiredHeight = height;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008552 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008553 }
8554
8555 private BubbleMetadata(Parcel in) {
8556 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008557 mIcon = Icon.CREATOR.createFromParcel(in);
8558 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008559 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008560 if (in.readInt() != 0) {
8561 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8562 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008563 }
8564
8565 /**
8566 * @return the pending intent used to populate the floating window for this bubble.
8567 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008568 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008569 public PendingIntent getIntent() {
8570 return mPendingIntent;
8571 }
8572
8573 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008574 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8575 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008576 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008577 public PendingIntent getDeleteIntent() {
8578 return mDeleteIntent;
8579 }
8580
8581 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008582 * @return the title that will appear along with the app content defined by
8583 * {@link #getIntent()} for this bubble.
Mady Mellor138f4c92019-02-20 16:12:17 -08008584 *
8585 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008586 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008587 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008588 public CharSequence getTitle() {
Mady Mellor138f4c92019-02-20 16:12:17 -08008589 return "";
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008590 }
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008591
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008592 /**
8593 * @return the icon that will be displayed for this bubble when it is collapsed.
8594 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008595 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008596 public Icon getIcon() {
8597 return mIcon;
8598 }
8599
8600 /**
8601 * @return the ideal height for the floating window that app content defined by
8602 * {@link #getIntent()} for this bubble.
8603 */
8604 public int getDesiredHeight() {
8605 return mDesiredHeight;
8606 }
8607
Mady Mellor0157ff22019-01-24 15:31:48 -08008608 /**
8609 * @return whether this bubble should auto expand when it is posted.
8610 *
8611 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8612 */
8613 public boolean getAutoExpandBubble() {
8614 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8615 }
8616
8617 /**
8618 * @return whether this bubble should suppress the initial notification when it is posted.
8619 *
8620 * @see BubbleMetadata.Builder#setSuppressInitialNotification(boolean)
8621 */
8622 public boolean getSuppressInitialNotification() {
8623 return (mFlags & FLAG_SUPPRESS_INITIAL_NOTIFICATION) != 0;
8624 }
8625
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008626 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008627 new Parcelable.Creator<BubbleMetadata>() {
8628
8629 @Override
8630 public BubbleMetadata createFromParcel(Parcel source) {
8631 return new BubbleMetadata(source);
8632 }
8633
8634 @Override
8635 public BubbleMetadata[] newArray(int size) {
8636 return new BubbleMetadata[size];
8637 }
8638 };
8639
8640 @Override
8641 public int describeContents() {
8642 return 0;
8643 }
8644
8645 @Override
8646 public void writeToParcel(Parcel out, int flags) {
8647 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008648 mIcon.writeToParcel(out, 0);
8649 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008650 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008651 out.writeInt(mDeleteIntent != null ? 1 : 0);
8652 if (mDeleteIntent != null) {
8653 mDeleteIntent.writeToParcel(out, 0);
8654 }
Mady Mellor0157ff22019-01-24 15:31:48 -08008655 }
8656
8657 private void setFlags(int flags) {
8658 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008659 }
8660
8661 /**
8662 * Builder to construct a {@link BubbleMetadata} object.
8663 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008664 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008665
8666 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008667 private Icon mIcon;
8668 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008669 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008670 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008671
8672 /**
8673 * Constructs a new builder object.
8674 */
8675 public Builder() {
8676 }
8677
8678 /**
8679 * Sets the intent that will be used when the bubble is expanded. This will display the
8680 * app content in a floating window over the existing foreground activity.
8681 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008682 @NonNull
8683 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008684 if (intent == null) {
8685 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8686 }
8687 mPendingIntent = intent;
8688 return this;
8689 }
8690
8691 /**
8692 * Sets the title that will appear along with the app content for this bubble.
8693 *
8694 * <p>A title is required and should expect to fit on a single line and make sense when
8695 * shown with the content defined by {@link #setIntent(PendingIntent)}.</p>
Mady Mellor138f4c92019-02-20 16:12:17 -08008696 *
8697 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008698 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008699 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008700 public BubbleMetadata.Builder setTitle(CharSequence title) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008701 return this;
8702 }
8703
8704 /**
8705 * Sets the icon that will represent the bubble when it is collapsed.
8706 *
8707 * <p>An icon is required and should be representative of the content within the bubble.
8708 * If your app produces multiple bubbles, the image should be unique for each of them.
8709 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008710 *
8711 * <p>The shape of a bubble icon is adaptive and can match the device theme.
8712 *
8713 * If your icon is bitmap-based, you should create it using
8714 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise this method will throw.
8715 *
8716 * If your icon is not bitmap-based, you should expect that the icon will be tinted.
8717 * </p>
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008718 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008719 @NonNull
8720 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008721 if (icon == null) {
8722 throw new IllegalArgumentException("Bubbles require non-null icon");
8723 }
Mady Mellor9848a6c2019-03-19 15:29:05 -07008724 if (icon.getType() == TYPE_BITMAP) {
8725 throw new IllegalArgumentException("When using bitmap based icons, Bubbles "
8726 + "require TYPE_ADAPTIVE_BITMAP, please use"
8727 + " Icon#createWithAdaptiveBitmap instead");
8728 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008729 mIcon = icon;
8730 return this;
8731 }
8732
8733 /**
8734 * Sets the desired height for the app content defined by
8735 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8736 * enough space on the screen or if the provided height is too small to be useful.
8737 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008738 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008739 public BubbleMetadata.Builder setDesiredHeight(int height) {
8740 mDesiredHeight = Math.max(height, 0);
8741 return this;
8742 }
8743
8744 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008745 * If set and the app creating the bubble is in the foreground, the bubble will be
8746 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8747 * floating window.
8748 *
8749 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8750 * </p>
8751 *
8752 * <p>Generally this flag should only be set if the user has performed an action to
8753 * request or create a bubble.</p>
8754 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008755 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008756 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8757 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8758 return this;
8759 }
8760
8761 /**
8762 * If set and the app creating the bubble is in the foreground, the bubble will be
8763 * posted <b>without</b> the associated notification in the notification shade.
8764 * Subsequent update notifications to this bubble will post a notification in the shade.
8765 *
8766 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8767 * </p>
8768 *
8769 * <p>Generally this flag should only be set if the user has performed an action to
8770 * request or create a bubble.</p>
8771 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008772 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008773 public BubbleMetadata.Builder setSuppressInitialNotification(
8774 boolean shouldSupressNotif) {
8775 setFlag(FLAG_SUPPRESS_INITIAL_NOTIFICATION, shouldSupressNotif);
8776 return this;
8777 }
8778
8779 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008780 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8781 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008782 @NonNull
8783 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008784 mDeleteIntent = deleteIntent;
8785 return this;
8786 }
8787
8788 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008789 * Creates the {@link BubbleMetadata} defined by this builder.
8790 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8791 * on this builder.</p>
8792 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008793 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008794 public BubbleMetadata build() {
8795 if (mPendingIntent == null) {
8796 throw new IllegalStateException("Must supply pending intent to bubble");
8797 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008798 if (mIcon == null) {
8799 throw new IllegalStateException("Must supply an icon for the bubble");
8800 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008801 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008802 mIcon, mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008803 data.setFlags(mFlags);
8804 return data;
8805 }
8806
8807 /**
8808 * @hide
8809 */
8810 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8811 if (value) {
8812 mFlags |= mask;
8813 } else {
8814 mFlags &= ~mask;
8815 }
8816 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008817 }
8818 }
8819 }
8820
8821
Christoph Studer4600f9b2014-07-22 22:44:43 +02008822 // When adding a new Style subclass here, don't forget to update
8823 // Builder.getNotificationStyleClass.
8824
Griff Hazen61a9e862014-05-22 16:05:19 -07008825 /**
8826 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8827 * metadata or change options on a notification builder.
8828 */
8829 public interface Extender {
8830 /**
8831 * Apply this extender to a notification builder.
8832 * @param builder the builder to be modified.
8833 * @return the build object for chaining.
8834 */
8835 public Builder extend(Builder builder);
8836 }
8837
8838 /**
8839 * Helper class to add wearable extensions to notifications.
8840 * <p class="note"> See
8841 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8842 * for Android Wear</a> for more information on how to use this class.
8843 * <p>
8844 * To create a notification with wearable extensions:
8845 * <ol>
8846 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8847 * properties.
8848 * <li>Create a {@link android.app.Notification.WearableExtender}.
8849 * <li>Set wearable-specific properties using the
8850 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8851 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8852 * notification.
8853 * <li>Post the notification to the notification system with the
8854 * {@code NotificationManager.notify(...)} methods.
8855 * </ol>
8856 *
8857 * <pre class="prettyprint">
8858 * Notification notif = new Notification.Builder(mContext)
8859 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8860 * .setContentText(subject)
8861 * .setSmallIcon(R.drawable.new_mail)
8862 * .extend(new Notification.WearableExtender()
8863 * .setContentIcon(R.drawable.new_mail))
8864 * .build();
8865 * NotificationManager notificationManger =
8866 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8867 * notificationManger.notify(0, notif);</pre>
8868 *
8869 * <p>Wearable extensions can be accessed on an existing notification by using the
8870 * {@code WearableExtender(Notification)} constructor,
8871 * and then using the {@code get} methods to access values.
8872 *
8873 * <pre class="prettyprint">
8874 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8875 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008876 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008877 */
8878 public static final class WearableExtender implements Extender {
8879 /**
8880 * Sentinel value for an action index that is unset.
8881 */
8882 public static final int UNSET_ACTION_INDEX = -1;
8883
8884 /**
8885 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8886 * default sizing.
8887 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008888 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008889 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008890 *
8891 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008892 */
Gus Prevasd7363752018-09-18 14:35:15 -04008893 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008894 public static final int SIZE_DEFAULT = 0;
8895
8896 /**
8897 * Size value for use with {@link #setCustomSizePreset} to show this notification
8898 * with an extra small size.
8899 * <p>This value is only applicable for custom display notifications created using
8900 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008901 *
8902 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008903 */
Gus Prevasd7363752018-09-18 14:35:15 -04008904 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008905 public static final int SIZE_XSMALL = 1;
8906
8907 /**
8908 * Size value for use with {@link #setCustomSizePreset} to show this notification
8909 * with a small size.
8910 * <p>This value is only applicable for custom display notifications created using
8911 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008912 *
8913 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008914 */
Gus Prevasd7363752018-09-18 14:35:15 -04008915 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008916 public static final int SIZE_SMALL = 2;
8917
8918 /**
8919 * Size value for use with {@link #setCustomSizePreset} to show this notification
8920 * with a medium size.
8921 * <p>This value is only applicable for custom display notifications created using
8922 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008923 *
8924 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008925 */
Gus Prevasd7363752018-09-18 14:35:15 -04008926 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008927 public static final int SIZE_MEDIUM = 3;
8928
8929 /**
8930 * Size value for use with {@link #setCustomSizePreset} to show this notification
8931 * with a large size.
8932 * <p>This value is only applicable for custom display notifications created using
8933 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008934 *
8935 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008936 */
Gus Prevasd7363752018-09-18 14:35:15 -04008937 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008938 public static final int SIZE_LARGE = 4;
8939
Griff Hazend5f11f92014-05-27 15:40:09 -07008940 /**
8941 * Size value for use with {@link #setCustomSizePreset} to show this notification
8942 * full screen.
8943 * <p>This value is only applicable for custom display notifications created using
8944 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008945 *
8946 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008947 */
Gus Prevasd7363752018-09-18 14:35:15 -04008948 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008949 public static final int SIZE_FULL_SCREEN = 5;
8950
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008951 /**
8952 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8953 * short amount of time when this notification is displayed on the screen. This
8954 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008955 *
8956 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008957 */
Gus Prevasd7363752018-09-18 14:35:15 -04008958 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008959 public static final int SCREEN_TIMEOUT_SHORT = 0;
8960
8961 /**
8962 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8963 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008964 *
8965 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008966 */
Gus Prevasd7363752018-09-18 14:35:15 -04008967 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008968 public static final int SCREEN_TIMEOUT_LONG = -1;
8969
Griff Hazen61a9e862014-05-22 16:05:19 -07008970 /** Notification extra which contains wearable extensions */
8971 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8972
Pete Gastaf6781d2014-10-07 15:17:05 -04008973 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008974 private static final String KEY_ACTIONS = "actions";
8975 private static final String KEY_FLAGS = "flags";
8976 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8977 private static final String KEY_PAGES = "pages";
8978 private static final String KEY_BACKGROUND = "background";
8979 private static final String KEY_CONTENT_ICON = "contentIcon";
8980 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8981 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8982 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8983 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8984 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008985 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008986 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008987 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008988
8989 // Flags bitwise-ored to mFlags
8990 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8991 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8992 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8993 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008994 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008995 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008996 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008997
8998 // Default value for flags integer
8999 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9000
9001 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9002 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9003
9004 private ArrayList<Action> mActions = new ArrayList<Action>();
9005 private int mFlags = DEFAULT_FLAGS;
9006 private PendingIntent mDisplayIntent;
9007 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9008 private Bitmap mBackground;
9009 private int mContentIcon;
9010 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9011 private int mContentActionIndex = UNSET_ACTION_INDEX;
9012 private int mCustomSizePreset = SIZE_DEFAULT;
9013 private int mCustomContentHeight;
9014 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009015 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009016 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009017 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009018
9019 /**
9020 * Create a {@link android.app.Notification.WearableExtender} with default
9021 * options.
9022 */
9023 public WearableExtender() {
9024 }
9025
9026 public WearableExtender(Notification notif) {
9027 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9028 if (wearableBundle != null) {
9029 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9030 if (actions != null) {
9031 mActions.addAll(actions);
9032 }
9033
9034 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9035 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9036
9037 Notification[] pages = getNotificationArrayFromBundle(
9038 wearableBundle, KEY_PAGES);
9039 if (pages != null) {
9040 Collections.addAll(mPages, pages);
9041 }
9042
9043 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9044 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9045 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9046 DEFAULT_CONTENT_ICON_GRAVITY);
9047 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9048 UNSET_ACTION_INDEX);
9049 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9050 SIZE_DEFAULT);
9051 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9052 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009053 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009054 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009055 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009056 }
9057 }
9058
9059 /**
9060 * Apply wearable extensions to a notification that is being built. This is typically
9061 * called by the {@link android.app.Notification.Builder#extend} method of
9062 * {@link android.app.Notification.Builder}.
9063 */
9064 @Override
9065 public Notification.Builder extend(Notification.Builder builder) {
9066 Bundle wearableBundle = new Bundle();
9067
9068 if (!mActions.isEmpty()) {
9069 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9070 }
9071 if (mFlags != DEFAULT_FLAGS) {
9072 wearableBundle.putInt(KEY_FLAGS, mFlags);
9073 }
9074 if (mDisplayIntent != null) {
9075 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9076 }
9077 if (!mPages.isEmpty()) {
9078 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9079 new Notification[mPages.size()]));
9080 }
9081 if (mBackground != null) {
9082 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9083 }
9084 if (mContentIcon != 0) {
9085 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9086 }
9087 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9088 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9089 }
9090 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9091 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9092 mContentActionIndex);
9093 }
9094 if (mCustomSizePreset != SIZE_DEFAULT) {
9095 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9096 }
9097 if (mCustomContentHeight != 0) {
9098 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9099 }
9100 if (mGravity != DEFAULT_GRAVITY) {
9101 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9102 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009103 if (mHintScreenTimeout != 0) {
9104 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9105 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009106 if (mDismissalId != null) {
9107 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9108 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009109 if (mBridgeTag != null) {
9110 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9111 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009112
9113 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9114 return builder;
9115 }
9116
9117 @Override
9118 public WearableExtender clone() {
9119 WearableExtender that = new WearableExtender();
9120 that.mActions = new ArrayList<Action>(this.mActions);
9121 that.mFlags = this.mFlags;
9122 that.mDisplayIntent = this.mDisplayIntent;
9123 that.mPages = new ArrayList<Notification>(this.mPages);
9124 that.mBackground = this.mBackground;
9125 that.mContentIcon = this.mContentIcon;
9126 that.mContentIconGravity = this.mContentIconGravity;
9127 that.mContentActionIndex = this.mContentActionIndex;
9128 that.mCustomSizePreset = this.mCustomSizePreset;
9129 that.mCustomContentHeight = this.mCustomContentHeight;
9130 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009131 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009132 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009133 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009134 return that;
9135 }
9136
9137 /**
9138 * Add a wearable action to this notification.
9139 *
9140 * <p>When wearable actions are added using this method, the set of actions that
9141 * show on a wearable device splits from devices that only show actions added
9142 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9143 * of which actions display on different devices.
9144 *
9145 * @param action the action to add to this notification
9146 * @return this object for method chaining
9147 * @see android.app.Notification.Action
9148 */
9149 public WearableExtender addAction(Action action) {
9150 mActions.add(action);
9151 return this;
9152 }
9153
9154 /**
9155 * Adds wearable actions to this notification.
9156 *
9157 * <p>When wearable actions are added using this method, the set of actions that
9158 * show on a wearable device splits from devices that only show actions added
9159 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9160 * of which actions display on different devices.
9161 *
9162 * @param actions the actions to add to this notification
9163 * @return this object for method chaining
9164 * @see android.app.Notification.Action
9165 */
9166 public WearableExtender addActions(List<Action> actions) {
9167 mActions.addAll(actions);
9168 return this;
9169 }
9170
9171 /**
9172 * Clear all wearable actions present on this builder.
9173 * @return this object for method chaining.
9174 * @see #addAction
9175 */
9176 public WearableExtender clearActions() {
9177 mActions.clear();
9178 return this;
9179 }
9180
9181 /**
9182 * Get the wearable actions present on this notification.
9183 */
9184 public List<Action> getActions() {
9185 return mActions;
9186 }
9187
9188 /**
9189 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009190 * this notification. The {@link PendingIntent} provided should be for an activity.
9191 *
9192 * <pre class="prettyprint">
9193 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9194 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9195 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9196 * Notification notif = new Notification.Builder(context)
9197 * .extend(new Notification.WearableExtender()
9198 * .setDisplayIntent(displayPendingIntent)
9199 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9200 * .build();</pre>
9201 *
9202 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009203 * should have an empty task affinity. It is also recommended to use the device
9204 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009205 *
9206 * <p>Example AndroidManifest.xml entry:
9207 * <pre class="prettyprint">
9208 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9209 * android:exported=&quot;true&quot;
9210 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009211 * android:taskAffinity=&quot;&quot;
9212 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009213 *
9214 * @param intent the {@link PendingIntent} for an activity
9215 * @return this object for method chaining
9216 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009217 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009218 */
Gus Prevasd7363752018-09-18 14:35:15 -04009219 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009220 public WearableExtender setDisplayIntent(PendingIntent intent) {
9221 mDisplayIntent = intent;
9222 return this;
9223 }
9224
9225 /**
9226 * Get the intent to launch inside of an activity view when displaying this
9227 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009228 *
9229 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009230 */
Gus Prevasd7363752018-09-18 14:35:15 -04009231 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009232 public PendingIntent getDisplayIntent() {
9233 return mDisplayIntent;
9234 }
9235
9236 /**
9237 * Add an additional page of content to display with this notification. The current
9238 * notification forms the first page, and pages added using this function form
9239 * subsequent pages. This field can be used to separate a notification into multiple
9240 * sections.
9241 *
9242 * @param page the notification to add as another page
9243 * @return this object for method chaining
9244 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009245 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009246 */
Gus Prevasd7363752018-09-18 14:35:15 -04009247 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009248 public WearableExtender addPage(Notification page) {
9249 mPages.add(page);
9250 return this;
9251 }
9252
9253 /**
9254 * Add additional pages of content to display with this notification. The current
9255 * notification forms the first page, and pages added using this function form
9256 * subsequent pages. This field can be used to separate a notification into multiple
9257 * sections.
9258 *
9259 * @param pages a list of notifications
9260 * @return this object for method chaining
9261 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009262 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009263 */
Gus Prevasd7363752018-09-18 14:35:15 -04009264 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009265 public WearableExtender addPages(List<Notification> pages) {
9266 mPages.addAll(pages);
9267 return this;
9268 }
9269
9270 /**
9271 * Clear all additional pages present on this builder.
9272 * @return this object for method chaining.
9273 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009274 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009275 */
Gus Prevasd7363752018-09-18 14:35:15 -04009276 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009277 public WearableExtender clearPages() {
9278 mPages.clear();
9279 return this;
9280 }
9281
9282 /**
9283 * Get the array of additional pages of content for displaying this notification. The
9284 * current notification forms the first page, and elements within this array form
9285 * subsequent pages. This field can be used to separate a notification into multiple
9286 * sections.
9287 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009288 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009289 */
Gus Prevasd7363752018-09-18 14:35:15 -04009290 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009291 public List<Notification> getPages() {
9292 return mPages;
9293 }
9294
9295 /**
9296 * Set a background image to be displayed behind the notification content.
9297 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9298 * will work with any notification style.
9299 *
9300 * @param background the background bitmap
9301 * @return this object for method chaining
9302 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009303 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009304 */
Gus Prevasd7363752018-09-18 14:35:15 -04009305 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009306 public WearableExtender setBackground(Bitmap background) {
9307 mBackground = background;
9308 return this;
9309 }
9310
9311 /**
9312 * Get a background image to be displayed behind the notification content.
9313 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9314 * will work with any notification style.
9315 *
9316 * @return the background image
9317 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009318 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009319 */
Gus Prevasd7363752018-09-18 14:35:15 -04009320 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009321 public Bitmap getBackground() {
9322 return mBackground;
9323 }
9324
9325 /**
9326 * Set an icon that goes with the content of this notification.
9327 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009328 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009329 public WearableExtender setContentIcon(int icon) {
9330 mContentIcon = icon;
9331 return this;
9332 }
9333
9334 /**
9335 * Get an icon that goes with the content of this notification.
9336 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009337 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009338 public int getContentIcon() {
9339 return mContentIcon;
9340 }
9341
9342 /**
9343 * Set the gravity that the content icon should have within the notification display.
9344 * Supported values include {@link android.view.Gravity#START} and
9345 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9346 * @see #setContentIcon
9347 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009348 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009349 public WearableExtender setContentIconGravity(int contentIconGravity) {
9350 mContentIconGravity = contentIconGravity;
9351 return this;
9352 }
9353
9354 /**
9355 * Get the gravity that the content icon should have within the notification display.
9356 * Supported values include {@link android.view.Gravity#START} and
9357 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9358 * @see #getContentIcon
9359 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009360 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009361 public int getContentIconGravity() {
9362 return mContentIconGravity;
9363 }
9364
9365 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009366 * Set an action from this notification's actions as the primary action. If the action has a
9367 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9368 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009369 *
Gus Prevasd7363752018-09-18 14:35:15 -04009370 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009371 * If wearable actions were added to the main notification, this index
9372 * will apply to that list, otherwise it will apply to the regular
9373 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009374 */
9375 public WearableExtender setContentAction(int actionIndex) {
9376 mContentActionIndex = actionIndex;
9377 return this;
9378 }
9379
9380 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009381 * Get the index of the notification action, if any, that was specified as the primary
9382 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009383 *
9384 * <p>If wearable specific actions were added to the main notification, this index will
9385 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009386 *
9387 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009388 */
9389 public int getContentAction() {
9390 return mContentActionIndex;
9391 }
9392
9393 /**
9394 * Set the gravity that this notification should have within the available viewport space.
9395 * Supported values include {@link android.view.Gravity#TOP},
9396 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9397 * The default value is {@link android.view.Gravity#BOTTOM}.
9398 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009399 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009400 public WearableExtender setGravity(int gravity) {
9401 mGravity = gravity;
9402 return this;
9403 }
9404
9405 /**
9406 * Get the gravity that this notification should have within the available viewport space.
9407 * Supported values include {@link android.view.Gravity#TOP},
9408 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9409 * The default value is {@link android.view.Gravity#BOTTOM}.
9410 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009411 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009412 public int getGravity() {
9413 return mGravity;
9414 }
9415
9416 /**
9417 * Set the custom size preset for the display of this notification out of the available
9418 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9419 * {@link #SIZE_LARGE}.
9420 * <p>Some custom size presets are only applicable for custom display notifications created
9421 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9422 * documentation for the preset in question. See also
9423 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9424 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009425 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009426 public WearableExtender setCustomSizePreset(int sizePreset) {
9427 mCustomSizePreset = sizePreset;
9428 return this;
9429 }
9430
9431 /**
9432 * Get the custom size preset for the display of this notification out of the available
9433 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9434 * {@link #SIZE_LARGE}.
9435 * <p>Some custom size presets are only applicable for custom display notifications created
9436 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9437 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9438 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009439 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009440 public int getCustomSizePreset() {
9441 return mCustomSizePreset;
9442 }
9443
9444 /**
9445 * Set the custom height in pixels for the display of this notification's content.
9446 * <p>This option is only available for custom display notifications created
9447 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9448 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9449 * {@link #getCustomContentHeight}.
9450 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009451 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009452 public WearableExtender setCustomContentHeight(int height) {
9453 mCustomContentHeight = height;
9454 return this;
9455 }
9456
9457 /**
9458 * Get the custom height in pixels for the display of this notification's content.
9459 * <p>This option is only available for custom display notifications created
9460 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9461 * {@link #setCustomContentHeight}.
9462 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009463 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009464 public int getCustomContentHeight() {
9465 return mCustomContentHeight;
9466 }
9467
9468 /**
9469 * Set whether the scrolling position for the contents of this notification should start
9470 * at the bottom of the contents instead of the top when the contents are too long to
9471 * display within the screen. Default is false (start scroll at the top).
9472 */
9473 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9474 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9475 return this;
9476 }
9477
9478 /**
9479 * Get whether the scrolling position for the contents of this notification should start
9480 * at the bottom of the contents instead of the top when the contents are too long to
9481 * display within the screen. Default is false (start scroll at the top).
9482 */
9483 public boolean getStartScrollBottom() {
9484 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9485 }
9486
9487 /**
9488 * Set whether the content intent is available when the wearable device is not connected
9489 * to a companion device. The user can still trigger this intent when the wearable device
9490 * is offline, but a visual hint will indicate that the content intent may not be available.
9491 * Defaults to true.
9492 */
9493 public WearableExtender setContentIntentAvailableOffline(
9494 boolean contentIntentAvailableOffline) {
9495 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9496 return this;
9497 }
9498
9499 /**
9500 * Get whether the content intent is available when the wearable device is not connected
9501 * to a companion device. The user can still trigger this intent when the wearable device
9502 * is offline, but a visual hint will indicate that the content intent may not be available.
9503 * Defaults to true.
9504 */
9505 public boolean getContentIntentAvailableOffline() {
9506 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9507 }
9508
9509 /**
9510 * Set a hint that this notification's icon should not be displayed.
9511 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9512 * @return this object for method chaining
9513 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009514 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009515 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9516 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9517 return this;
9518 }
9519
9520 /**
9521 * Get a hint that this notification's icon should not be displayed.
9522 * @return {@code true} if this icon should not be displayed, false otherwise.
9523 * The default value is {@code false} if this was never set.
9524 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009525 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009526 public boolean getHintHideIcon() {
9527 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9528 }
9529
9530 /**
9531 * Set a visual hint that only the background image of this notification should be
9532 * displayed, and other semantic content should be hidden. This hint is only applicable
9533 * to sub-pages added using {@link #addPage}.
9534 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009535 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009536 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9537 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9538 return this;
9539 }
9540
9541 /**
9542 * Get a visual hint that only the background image of this notification should be
9543 * displayed, and other semantic content should be hidden. This hint is only applicable
9544 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9545 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009546 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009547 public boolean getHintShowBackgroundOnly() {
9548 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9549 }
9550
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009551 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009552 * Set a hint that this notification's background should not be clipped if possible,
9553 * and should instead be resized to fully display on the screen, retaining the aspect
9554 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009555 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9556 * @return this object for method chaining
9557 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009558 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009559 public WearableExtender setHintAvoidBackgroundClipping(
9560 boolean hintAvoidBackgroundClipping) {
9561 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9562 return this;
9563 }
9564
9565 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009566 * Get a hint that this notification's background should not be clipped if possible,
9567 * and should instead be resized to fully display on the screen, retaining the aspect
9568 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009569 * @return {@code true} if it's ok if the background is clipped on the screen, false
9570 * otherwise. The default value is {@code false} if this was never set.
9571 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009572 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009573 public boolean getHintAvoidBackgroundClipping() {
9574 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9575 }
9576
9577 /**
9578 * Set a hint that the screen should remain on for at least this duration when
9579 * this notification is displayed on the screen.
9580 * @param timeout The requested screen timeout in milliseconds. Can also be either
9581 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9582 * @return this object for method chaining
9583 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009584 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009585 public WearableExtender setHintScreenTimeout(int timeout) {
9586 mHintScreenTimeout = timeout;
9587 return this;
9588 }
9589
9590 /**
9591 * Get the duration, in milliseconds, that the screen should remain on for
9592 * when this notification is displayed.
9593 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9594 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9595 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009596 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009597 public int getHintScreenTimeout() {
9598 return mHintScreenTimeout;
9599 }
9600
Alex Hills9ab3a232016-04-05 14:54:56 -04009601 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009602 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9603 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9604 * qr codes, as well as other simple black-and-white tickets.
9605 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9606 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009607 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009608 */
Gus Prevasd7363752018-09-18 14:35:15 -04009609 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009610 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9611 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9612 return this;
9613 }
9614
9615 /**
9616 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9617 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9618 * qr codes, as well as other simple black-and-white tickets.
9619 * @return {@code true} if it should be displayed in ambient, false otherwise
9620 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009621 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009622 */
Gus Prevasd7363752018-09-18 14:35:15 -04009623 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009624 public boolean getHintAmbientBigPicture() {
9625 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9626 }
9627
9628 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009629 * Set a hint that this notification's content intent will launch an {@link Activity}
9630 * directly, telling the platform that it can generate the appropriate transitions.
9631 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9632 * an activity and transitions should be generated, false otherwise.
9633 * @return this object for method chaining
9634 */
9635 public WearableExtender setHintContentIntentLaunchesActivity(
9636 boolean hintContentIntentLaunchesActivity) {
9637 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9638 return this;
9639 }
9640
9641 /**
9642 * Get a hint that this notification's content intent will launch an {@link Activity}
9643 * directly, telling the platform that it can generate the appropriate transitions
9644 * @return {@code true} if the content intent will launch an activity and transitions should
9645 * be generated, false otherwise. The default value is {@code false} if this was never set.
9646 */
9647 public boolean getHintContentIntentLaunchesActivity() {
9648 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9649 }
9650
Nadia Benbernou948627e2016-04-14 14:41:08 -04009651 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009652 * Sets the dismissal id for this notification. If a notification is posted with a
9653 * dismissal id, then when that notification is canceled, notifications on other wearables
9654 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009655 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009656 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009657 * @param dismissalId the dismissal id of the notification.
9658 * @return this object for method chaining
9659 */
9660 public WearableExtender setDismissalId(String dismissalId) {
9661 mDismissalId = dismissalId;
9662 return this;
9663 }
9664
9665 /**
9666 * Returns the dismissal id of the notification.
9667 * @return the dismissal id of the notification or null if it has not been set.
9668 */
9669 public String getDismissalId() {
9670 return mDismissalId;
9671 }
9672
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009673 /**
9674 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9675 * posted from a phone to provide finer-grained control on what notifications are bridged
9676 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9677 * Features to Notifications</a> for more information.
9678 * @param bridgeTag the bridge tag of the notification.
9679 * @return this object for method chaining
9680 */
9681 public WearableExtender setBridgeTag(String bridgeTag) {
9682 mBridgeTag = bridgeTag;
9683 return this;
9684 }
9685
9686 /**
9687 * Returns the bridge tag of the notification.
9688 * @return the bridge tag or null if not present.
9689 */
9690 public String getBridgeTag() {
9691 return mBridgeTag;
9692 }
9693
Griff Hazen61a9e862014-05-22 16:05:19 -07009694 private void setFlag(int mask, boolean value) {
9695 if (value) {
9696 mFlags |= mask;
9697 } else {
9698 mFlags &= ~mask;
9699 }
9700 }
9701 }
9702
9703 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009704 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9705 * with car extensions:
9706 *
9707 * <ol>
9708 * <li>Create an {@link Notification.Builder}, setting any desired
9709 * properties.
9710 * <li>Create a {@link CarExtender}.
9711 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9712 * {@link CarExtender}.
9713 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9714 * to apply the extensions to a notification.
9715 * </ol>
9716 *
9717 * <pre class="prettyprint">
9718 * Notification notification = new Notification.Builder(context)
9719 * ...
9720 * .extend(new CarExtender()
9721 * .set*(...))
9722 * .build();
9723 * </pre>
9724 *
9725 * <p>Car extensions can be accessed on an existing notification by using the
9726 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9727 * to access values.
9728 */
9729 public static final class CarExtender implements Extender {
9730 private static final String TAG = "CarExtender";
9731
9732 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9733 private static final String EXTRA_LARGE_ICON = "large_icon";
9734 private static final String EXTRA_CONVERSATION = "car_conversation";
9735 private static final String EXTRA_COLOR = "app_color";
9736
9737 private Bitmap mLargeIcon;
9738 private UnreadConversation mUnreadConversation;
9739 private int mColor = Notification.COLOR_DEFAULT;
9740
9741 /**
9742 * Create a {@link CarExtender} with default options.
9743 */
9744 public CarExtender() {
9745 }
9746
9747 /**
9748 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9749 *
9750 * @param notif The notification from which to copy options.
9751 */
9752 public CarExtender(Notification notif) {
9753 Bundle carBundle = notif.extras == null ?
9754 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9755 if (carBundle != null) {
9756 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9757 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9758
9759 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9760 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9761 }
9762 }
9763
9764 /**
9765 * Apply car extensions to a notification that is being built. This is typically called by
9766 * the {@link Notification.Builder#extend(Notification.Extender)}
9767 * method of {@link Notification.Builder}.
9768 */
9769 @Override
9770 public Notification.Builder extend(Notification.Builder builder) {
9771 Bundle carExtensions = new Bundle();
9772
9773 if (mLargeIcon != null) {
9774 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9775 }
9776 if (mColor != Notification.COLOR_DEFAULT) {
9777 carExtensions.putInt(EXTRA_COLOR, mColor);
9778 }
9779
9780 if (mUnreadConversation != null) {
9781 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9782 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9783 }
9784
9785 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9786 return builder;
9787 }
9788
9789 /**
9790 * Sets the accent color to use when Android Auto presents the notification.
9791 *
9792 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9793 * to accent the displayed notification. However, not all colors are acceptable in an
9794 * automotive setting. This method can be used to override the color provided in the
9795 * notification in such a situation.
9796 */
Tor Norbye80756e32015-03-02 09:39:27 -08009797 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009798 mColor = color;
9799 return this;
9800 }
9801
9802 /**
9803 * Gets the accent color.
9804 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009805 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009806 */
Tor Norbye80756e32015-03-02 09:39:27 -08009807 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009808 public int getColor() {
9809 return mColor;
9810 }
9811
9812 /**
9813 * Sets the large icon of the car notification.
9814 *
9815 * If no large icon is set in the extender, Android Auto will display the icon
9816 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9817 *
9818 * @param largeIcon The large icon to use in the car notification.
9819 * @return This object for method chaining.
9820 */
9821 public CarExtender setLargeIcon(Bitmap largeIcon) {
9822 mLargeIcon = largeIcon;
9823 return this;
9824 }
9825
9826 /**
9827 * Gets the large icon used in this car notification, or null if no icon has been set.
9828 *
9829 * @return The large icon for the car notification.
9830 * @see CarExtender#setLargeIcon
9831 */
9832 public Bitmap getLargeIcon() {
9833 return mLargeIcon;
9834 }
9835
9836 /**
9837 * Sets the unread conversation in a message notification.
9838 *
9839 * @param unreadConversation The unread part of the conversation this notification conveys.
9840 * @return This object for method chaining.
9841 */
9842 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9843 mUnreadConversation = unreadConversation;
9844 return this;
9845 }
9846
9847 /**
9848 * Returns the unread conversation conveyed by this notification.
9849 * @see #setUnreadConversation(UnreadConversation)
9850 */
9851 public UnreadConversation getUnreadConversation() {
9852 return mUnreadConversation;
9853 }
9854
9855 /**
9856 * A class which holds the unread messages from a conversation.
9857 */
9858 public static class UnreadConversation {
9859 private static final String KEY_AUTHOR = "author";
9860 private static final String KEY_TEXT = "text";
9861 private static final String KEY_MESSAGES = "messages";
9862 private static final String KEY_REMOTE_INPUT = "remote_input";
9863 private static final String KEY_ON_REPLY = "on_reply";
9864 private static final String KEY_ON_READ = "on_read";
9865 private static final String KEY_PARTICIPANTS = "participants";
9866 private static final String KEY_TIMESTAMP = "timestamp";
9867
9868 private final String[] mMessages;
9869 private final RemoteInput mRemoteInput;
9870 private final PendingIntent mReplyPendingIntent;
9871 private final PendingIntent mReadPendingIntent;
9872 private final String[] mParticipants;
9873 private final long mLatestTimestamp;
9874
9875 UnreadConversation(String[] messages, RemoteInput remoteInput,
9876 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9877 String[] participants, long latestTimestamp) {
9878 mMessages = messages;
9879 mRemoteInput = remoteInput;
9880 mReadPendingIntent = readPendingIntent;
9881 mReplyPendingIntent = replyPendingIntent;
9882 mParticipants = participants;
9883 mLatestTimestamp = latestTimestamp;
9884 }
9885
9886 /**
9887 * Gets the list of messages conveyed by this notification.
9888 */
9889 public String[] getMessages() {
9890 return mMessages;
9891 }
9892
9893 /**
9894 * Gets the remote input that will be used to convey the response to a message list, or
9895 * null if no such remote input exists.
9896 */
9897 public RemoteInput getRemoteInput() {
9898 return mRemoteInput;
9899 }
9900
9901 /**
9902 * Gets the pending intent that will be triggered when the user replies to this
9903 * notification.
9904 */
9905 public PendingIntent getReplyPendingIntent() {
9906 return mReplyPendingIntent;
9907 }
9908
9909 /**
9910 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9911 * in this object's message list.
9912 */
9913 public PendingIntent getReadPendingIntent() {
9914 return mReadPendingIntent;
9915 }
9916
9917 /**
9918 * Gets the participants in the conversation.
9919 */
9920 public String[] getParticipants() {
9921 return mParticipants;
9922 }
9923
9924 /**
9925 * Gets the firs participant in the conversation.
9926 */
9927 public String getParticipant() {
9928 return mParticipants.length > 0 ? mParticipants[0] : null;
9929 }
9930
9931 /**
9932 * Gets the timestamp of the conversation.
9933 */
9934 public long getLatestTimestamp() {
9935 return mLatestTimestamp;
9936 }
9937
9938 Bundle getBundleForUnreadConversation() {
9939 Bundle b = new Bundle();
9940 String author = null;
9941 if (mParticipants != null && mParticipants.length > 1) {
9942 author = mParticipants[0];
9943 }
9944 Parcelable[] messages = new Parcelable[mMessages.length];
9945 for (int i = 0; i < messages.length; i++) {
9946 Bundle m = new Bundle();
9947 m.putString(KEY_TEXT, mMessages[i]);
9948 m.putString(KEY_AUTHOR, author);
9949 messages[i] = m;
9950 }
9951 b.putParcelableArray(KEY_MESSAGES, messages);
9952 if (mRemoteInput != null) {
9953 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9954 }
9955 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9956 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9957 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9958 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9959 return b;
9960 }
9961
9962 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9963 if (b == null) {
9964 return null;
9965 }
9966 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9967 String[] messages = null;
9968 if (parcelableMessages != null) {
9969 String[] tmp = new String[parcelableMessages.length];
9970 boolean success = true;
9971 for (int i = 0; i < tmp.length; i++) {
9972 if (!(parcelableMessages[i] instanceof Bundle)) {
9973 success = false;
9974 break;
9975 }
9976 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9977 if (tmp[i] == null) {
9978 success = false;
9979 break;
9980 }
9981 }
9982 if (success) {
9983 messages = tmp;
9984 } else {
9985 return null;
9986 }
9987 }
9988
9989 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9990 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9991
9992 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9993
9994 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9995 if (participants == null || participants.length != 1) {
9996 return null;
9997 }
9998
9999 return new UnreadConversation(messages,
10000 remoteInput,
10001 onReply,
10002 onRead,
10003 participants, b.getLong(KEY_TIMESTAMP));
10004 }
10005 };
10006
10007 /**
10008 * Builder class for {@link CarExtender.UnreadConversation} objects.
10009 */
10010 public static class Builder {
10011 private final List<String> mMessages = new ArrayList<String>();
10012 private final String mParticipant;
10013 private RemoteInput mRemoteInput;
10014 private PendingIntent mReadPendingIntent;
10015 private PendingIntent mReplyPendingIntent;
10016 private long mLatestTimestamp;
10017
10018 /**
10019 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10020 *
10021 * @param name The name of the other participant in the conversation.
10022 */
10023 public Builder(String name) {
10024 mParticipant = name;
10025 }
10026
10027 /**
10028 * Appends a new unread message to the list of messages for this conversation.
10029 *
10030 * The messages should be added from oldest to newest.
10031 *
10032 * @param message The text of the new unread message.
10033 * @return This object for method chaining.
10034 */
10035 public Builder addMessage(String message) {
10036 mMessages.add(message);
10037 return this;
10038 }
10039
10040 /**
10041 * Sets the pending intent and remote input which will convey the reply to this
10042 * notification.
10043 *
10044 * @param pendingIntent The pending intent which will be triggered on a reply.
10045 * @param remoteInput The remote input parcelable which will carry the reply.
10046 * @return This object for method chaining.
10047 *
10048 * @see CarExtender.UnreadConversation#getRemoteInput
10049 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10050 */
10051 public Builder setReplyAction(
10052 PendingIntent pendingIntent, RemoteInput remoteInput) {
10053 mRemoteInput = remoteInput;
10054 mReplyPendingIntent = pendingIntent;
10055
10056 return this;
10057 }
10058
10059 /**
10060 * Sets the pending intent that will be sent once the messages in this notification
10061 * are read.
10062 *
10063 * @param pendingIntent The pending intent to use.
10064 * @return This object for method chaining.
10065 */
10066 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10067 mReadPendingIntent = pendingIntent;
10068 return this;
10069 }
10070
10071 /**
10072 * Sets the timestamp of the most recent message in an unread conversation.
10073 *
10074 * If a messaging notification has been posted by your application and has not
10075 * yet been cancelled, posting a later notification with the same id and tag
10076 * but without a newer timestamp may result in Android Auto not displaying a
10077 * heads up notification for the later notification.
10078 *
10079 * @param timestamp The timestamp of the most recent message in the conversation.
10080 * @return This object for method chaining.
10081 */
10082 public Builder setLatestTimestamp(long timestamp) {
10083 mLatestTimestamp = timestamp;
10084 return this;
10085 }
10086
10087 /**
10088 * Builds a new unread conversation object.
10089 *
10090 * @return The new unread conversation object.
10091 */
10092 public UnreadConversation build() {
10093 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10094 String[] participants = { mParticipant };
10095 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10096 mReadPendingIntent, participants, mLatestTimestamp);
10097 }
10098 }
10099 }
10100
10101 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010102 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10103 * with a TV extension:
10104 *
10105 * <ol>
10106 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10107 * <li>Create a {@link TvExtender}.
10108 * <li>Set TV-specific properties using the {@code set} methods of
10109 * {@link TvExtender}.
10110 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10111 * to apply the extension to a notification.
10112 * </ol>
10113 *
10114 * <pre class="prettyprint">
10115 * Notification notification = new Notification.Builder(context)
10116 * ...
10117 * .extend(new TvExtender()
10118 * .set*(...))
10119 * .build();
10120 * </pre>
10121 *
10122 * <p>TV extensions can be accessed on an existing notification by using the
10123 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10124 * to access values.
10125 *
10126 * @hide
10127 */
10128 @SystemApi
10129 public static final class TvExtender implements Extender {
10130 private static final String TAG = "TvExtender";
10131
10132 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10133 private static final String EXTRA_FLAGS = "flags";
10134 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10135 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010136 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010137 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010138
10139 // Flags bitwise-ored to mFlags
10140 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10141
10142 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010143 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010144 private PendingIntent mContentIntent;
10145 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010146 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010147
10148 /**
10149 * Create a {@link TvExtender} with default options.
10150 */
10151 public TvExtender() {
10152 mFlags = FLAG_AVAILABLE_ON_TV;
10153 }
10154
10155 /**
10156 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10157 *
10158 * @param notif The notification from which to copy options.
10159 */
10160 public TvExtender(Notification notif) {
10161 Bundle bundle = notif.extras == null ?
10162 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10163 if (bundle != null) {
10164 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010165 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010166 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010167 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10168 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10169 }
10170 }
10171
10172 /**
10173 * Apply a TV extension to a notification that is being built. This is typically called by
10174 * the {@link Notification.Builder#extend(Notification.Extender)}
10175 * method of {@link Notification.Builder}.
10176 */
10177 @Override
10178 public Notification.Builder extend(Notification.Builder builder) {
10179 Bundle bundle = new Bundle();
10180
10181 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010182 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010183 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010184 if (mContentIntent != null) {
10185 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10186 }
10187
10188 if (mDeleteIntent != null) {
10189 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10190 }
10191
10192 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10193 return builder;
10194 }
10195
10196 /**
10197 * Returns true if this notification should be shown on TV. This method return true
10198 * if the notification was extended with a TvExtender.
10199 */
10200 public boolean isAvailableOnTv() {
10201 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10202 }
10203
10204 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010205 * Specifies the channel the notification should be delivered on when shown on TV.
10206 * It can be different from the channel that the notification is delivered to when
10207 * posting on a non-TV device.
10208 */
10209 public TvExtender setChannel(String channelId) {
10210 mChannelId = channelId;
10211 return this;
10212 }
10213
10214 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010215 * Specifies the channel the notification should be delivered on when shown on TV.
10216 * It can be different from the channel that the notification is delivered to when
10217 * posting on a non-TV device.
10218 */
10219 public TvExtender setChannelId(String channelId) {
10220 mChannelId = channelId;
10221 return this;
10222 }
10223
Jeff Sharkey000ce802017-04-29 13:13:27 -060010224 /** @removed */
10225 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010226 public String getChannel() {
10227 return mChannelId;
10228 }
10229
10230 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010231 * Returns the id of the channel this notification posts to on TV.
10232 */
10233 public String getChannelId() {
10234 return mChannelId;
10235 }
10236
10237 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010238 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10239 * If provided, it is used instead of the content intent specified
10240 * at the level of Notification.
10241 */
10242 public TvExtender setContentIntent(PendingIntent intent) {
10243 mContentIntent = intent;
10244 return this;
10245 }
10246
10247 /**
10248 * Returns the TV-specific content intent. If this method returns null, the
10249 * main content intent on the notification should be used.
10250 *
10251 * @see {@link Notification#contentIntent}
10252 */
10253 public PendingIntent getContentIntent() {
10254 return mContentIntent;
10255 }
10256
10257 /**
10258 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10259 * by the user on TV. If provided, it is used instead of the delete intent specified
10260 * at the level of Notification.
10261 */
10262 public TvExtender setDeleteIntent(PendingIntent intent) {
10263 mDeleteIntent = intent;
10264 return this;
10265 }
10266
10267 /**
10268 * Returns the TV-specific delete intent. If this method returns null, the
10269 * main delete intent on the notification should be used.
10270 *
10271 * @see {@link Notification#deleteIntent}
10272 */
10273 public PendingIntent getDeleteIntent() {
10274 return mDeleteIntent;
10275 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010276
10277 /**
10278 * Specifies whether this notification should suppress showing a message over top of apps
10279 * outside of the launcher.
10280 */
10281 public TvExtender setSuppressShowOverApps(boolean suppress) {
10282 mSuppressShowOverApps = suppress;
10283 return this;
10284 }
10285
10286 /**
10287 * Returns true if this notification should not show messages over top of apps
10288 * outside of the launcher.
10289 */
10290 public boolean getSuppressShowOverApps() {
10291 return mSuppressShowOverApps;
10292 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010293 }
10294
10295 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010296 * Get an array of Notification objects from a parcelable array bundle field.
10297 * Update the bundle to have a typed array so fetches in the future don't need
10298 * to do an array copy.
10299 */
10300 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10301 Parcelable[] array = bundle.getParcelableArray(key);
10302 if (array instanceof Notification[] || array == null) {
10303 return (Notification[]) array;
10304 }
10305 Notification[] typedArray = Arrays.copyOf(array, array.length,
10306 Notification[].class);
10307 bundle.putParcelableArray(key, typedArray);
10308 return typedArray;
10309 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010310
10311 private static class BuilderRemoteViews extends RemoteViews {
10312 public BuilderRemoteViews(Parcel parcel) {
10313 super(parcel);
10314 }
10315
Kenny Guy77320062014-08-27 21:37:15 +010010316 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10317 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010318 }
10319
10320 @Override
10321 public BuilderRemoteViews clone() {
10322 Parcel p = Parcel.obtain();
10323 writeToParcel(p, 0);
10324 p.setDataPosition(0);
10325 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10326 p.recycle();
10327 return brv;
10328 }
10329 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010330
Selim Cinek384804b2018-04-18 14:31:07 +080010331 /**
10332 * A result object where information about the template that was created is saved.
10333 */
10334 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010335 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010336 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010337
10338 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010339 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010340 * with the right actions.
10341 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010342 public int getIconMarginEnd() {
10343 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010344 }
10345
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010346 /**
10347 * Is the icon container visible on the right size because of the reply button or the
10348 * right icon.
10349 */
10350 public boolean isRightIconContainerVisible() {
10351 return mRightIconContainerVisible;
10352 }
10353
Selim Cinek1c72fa02018-04-23 18:00:54 +080010354 public void setIconMarginEnd(int iconMarginEnd) {
10355 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010356 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010357
10358 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10359 mRightIconContainerVisible = iconContainerVisible;
10360 }
Selim Cinek384804b2018-04-18 14:31:07 +080010361 }
10362
Adrian Roos70d7aa32017-01-11 15:39:06 -080010363 private static class StandardTemplateParams {
10364 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010365 CharSequence title;
10366 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010367 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010368 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010369 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010370 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010371 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010372 boolean allowColorization = true;
10373 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010374
10375 final StandardTemplateParams reset() {
10376 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010377 title = null;
10378 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010379 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010380 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010381 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010382 allowColorization = true;
10383 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010384 return this;
10385 }
10386
10387 final StandardTemplateParams hasProgress(boolean hasProgress) {
10388 this.hasProgress = hasProgress;
10389 return this;
10390 }
10391
10392 final StandardTemplateParams title(CharSequence title) {
10393 this.title = title;
10394 return this;
10395 }
10396
10397 final StandardTemplateParams text(CharSequence text) {
10398 this.text = text;
10399 return this;
10400 }
10401
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010402 final StandardTemplateParams summaryText(CharSequence text) {
10403 this.summaryText = text;
10404 return this;
10405 }
10406
Selim Cinekafeed292017-12-12 17:32:44 -080010407 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10408 this.headerTextSecondary = text;
10409 return this;
10410 }
10411
Selim Cinek384804b2018-04-18 14:31:07 +080010412 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10413 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010414 return this;
10415 }
10416
Selim Cinek384804b2018-04-18 14:31:07 +080010417 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10418 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010419 return this;
10420 }
10421
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010422 final StandardTemplateParams disallowColorization() {
10423 this.allowColorization = false;
10424 return this;
10425 }
10426
10427 final StandardTemplateParams forceDefaultColor() {
10428 this.forceDefaultColor = true;
10429 return this;
10430 }
10431
Adrian Roos70d7aa32017-01-11 15:39:06 -080010432 final StandardTemplateParams fillTextsFrom(Builder b) {
10433 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010434 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010435
Lucas Dupin06c5e642017-09-13 16:34:58 -070010436 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010437 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010438 text = extras.getCharSequence(EXTRA_TEXT);
10439 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010440 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010441 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010442 return this;
10443 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010444
10445 /**
10446 * Set the maximum lines of remote input history lines allowed.
10447 * @param maxRemoteInputHistory The number of lines.
10448 * @return The builder for method chaining.
10449 */
10450 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10451 this.maxRemoteInputHistory = maxRemoteInputHistory;
10452 return this;
10453 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455}