blob: a90185cf1468d1d94d32315933377750ed15f845 [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 Mellor8a529e22019-03-25 17:37:55 -070019import static android.annotation.Dimension.DP;
Mady Mellor9848a6c2019-03-19 15:29:05 -070020import static android.graphics.drawable.Icon.TYPE_BITMAP;
21
Lucas Dupina291d192018-06-07 13:59:42 -070022import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070023
Tor Norbye80756e32015-03-02 09:39:27 -080024import android.annotation.ColorInt;
Mady Mellor7af771a2019-03-07 15:04:54 -080025import android.annotation.DimenRes;
Mady Mellor8a529e22019-03-25 17:37:55 -070026import android.annotation.Dimension;
Tor Norbye7b9c9122013-05-30 16:48:33 -070027import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040028import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070029import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040030import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070031import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060032import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040033import android.annotation.SdkConstant;
34import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040035import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010036import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.Context;
38import android.content.Intent;
Felipe Leme90205ef2019-03-05 09:59:52 -080039import android.content.LocusId;
Kenny Guy77320062014-08-27 21:37:15 +010040import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040041import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020042import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050043import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020044import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070045import android.content.res.Configuration;
46import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040047import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040048import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010049import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070050import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010051import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010052import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040053import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040054import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070055import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080056import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070057import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040059import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020060import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050061import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070062import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Parcel;
64import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040065import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080066import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070067import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070068import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.SpannableStringBuilder;
70import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080072import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080073import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070074import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080075import android.text.style.RelativeSizeSpan;
76import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070077import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040078import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010079import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050080import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070081import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070082import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080083import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080084import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070085import android.view.ViewGroup;
Felipe Leme90205ef2019-03-05 09:59:52 -080086import android.view.contentcapture.ContentCaptureContext;
Jeff Sharkey1c400132011-08-05 14:50:13 -070087import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.widget.RemoteViews;
89
Griff Hazen959591e2014-05-15 22:26:18 -070090import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070091import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070092import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070093import com.android.internal.util.ContrastColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070094
Tor Norbyed9273d62013-05-30 15:59:53 -070095import java.lang.annotation.Retention;
96import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020097import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050098import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070099import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700100import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -0700101import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -0500102import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -0500103import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -0600104import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -0800105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106/**
107 * A class that represents how a persistent notification is to be presented to
108 * the user using the {@link android.app.NotificationManager}.
109 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800110 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
111 * easier to construct Notifications.</p>
112 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700113 * <div class="special reference">
114 * <h3>Developer Guides</h3>
115 * <p>For a guide to creating notifications, read the
116 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
117 * developer guide.</p>
118 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 */
120public class Notification implements Parcelable
121{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400122 private static final String TAG = "Notification";
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400125 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400126 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400127 */
128 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
129 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
130 = "android.intent.category.NOTIFICATION_PREFERENCES";
131
132 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500133 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
134 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400135 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500136 */
137 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
138
139 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400140 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400141 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
142 * what settings should be shown in the target app.
143 */
144 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
145
146 /**
147 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400148 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
149 * that can be used to narrow down what settings should be shown in the target app.
150 */
151 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
152
153 /**
154 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
155 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
156 * that can be used to narrow down what settings should be shown in the target app.
157 */
158 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
159
160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * Use all default values (where applicable).
162 */
163 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /**
166 * Use the default notification sound. This will ignore any given
167 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 *
Chris Wren47c20a12014-06-18 17:27:29 -0400169 * <p>
170 * A notification that is noisy is more likely to be presented as a heads-up notification.
171 * </p>
172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 */
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final int DEFAULT_SOUND = 1;
177
178 /**
179 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500180 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700181 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 *
Chris Wren47c20a12014-06-18 17:27:29 -0400183 * <p>
184 * A notification that vibrates is more likely to be presented as a heads-up notification.
185 * </p>
186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500188 */
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
193 * Use the default notification lights. This will ignore the
194 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
195 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 */
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200203 * Maximum length of CharSequences accepted by Builder and friends.
204 *
205 * <p>
206 * Avoids spamming the system with overly large strings such as full e-mails.
207 */
208 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
209
210 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800211 * Maximum entries of reply text that are accepted by Builder and friends.
212 */
213 private static final int MAX_REPLY_HISTORY = 5;
214
Tony Mak09db2ea2018-06-27 18:12:48 +0100215 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000216 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
217 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100218 * @hide
219 */
220 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800221
222 /**
223 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
224 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
225 *
226 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
227 * sends messages.</p>
228 */
229 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
230
Adrian Roose458aa82015-12-08 16:17:19 -0800231 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500232 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500234 * Default value: {@link System#currentTimeMillis() Now}.
235 *
236 * Choose a timestamp that will be most relevant to the user. For most finite events, this
237 * corresponds to the time the event happened (or will happen, in the case of events that have
238 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800239 * timestamped according to when the activity began.
240 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500241 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800242 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500243 * <ul>
244 * <li>Notification of a new chat message should be stamped when the message was received.</li>
245 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
246 * <li>Notification of a completed file download should be stamped when the download finished.</li>
247 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
248 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
249 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800250 * </ul>
251 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700252 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
253 * anymore by default and must be opted into by using
254 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
256 public long when;
257
258 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700259 * The creation time of the notification
260 */
261 private long creationTime;
262
263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400265 *
266 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 */
Dan Sandler86647982015-05-13 23:41:13 -0400268 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700269 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public int icon;
271
272 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800273 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
274 * leave it at its default value of 0.
275 *
276 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700277 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800278 */
279 public int iconLevel;
280
281 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500282 * The number of events that this notification represents. For example, in a new mail
283 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800284 *
Julia Reynolds30331982017-04-27 10:12:50 -0400285 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500286 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
287 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 */
Julia Reynolds30331982017-04-27 10:12:50 -0400289 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
291 /**
292 * The intent to execute when the expanded status entry is clicked. If
293 * this is an activity, it must include the
294 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800295 * that you take care of task management as described in the
296 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800297 * Stack</a> document. In particular, make sure to read the notification section
298 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
299 * Notifications</a> for the correct ways to launch an application from a
300 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
302 public PendingIntent contentIntent;
303
304 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500305 * The intent to execute when the notification is explicitly dismissed by the user, either with
306 * the "Clear All" button or by swiping it away individually.
307 *
308 * This probably shouldn't be launching an activity since several of those will be sent
309 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 */
311 public PendingIntent deleteIntent;
312
313 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700314 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800315 *
Chris Wren47c20a12014-06-18 17:27:29 -0400316 * <p>
317 * The system UI may choose to display a heads-up notification, instead of
318 * launching this intent, while the user is using the device.
319 * </p>
320 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800321 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400322 */
323 public PendingIntent fullScreenIntent;
324
325 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400326 * Text that summarizes this notification for accessibility services.
327 *
328 * As of the L release, this text is no longer shown on screen, but it is still useful to
329 * accessibility services (where it serves as an audible announcement of the notification's
330 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400331 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800332 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
334 public CharSequence tickerText;
335
336 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400337 * Formerly, a view showing the {@link #tickerText}.
338 *
339 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400340 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400341 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800342 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400343
344 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 * The view that will represent this notification in the notification list (which is pulled
346 * down from the status bar).
347 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500348 * As of N, this field may be null. The notification view is determined by the inputs
349 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400350 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400352 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 public RemoteViews contentView;
354
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400355 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400356 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400357 * opportunity to show more detail. The system UI may choose to show this
358 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400359 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500360 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400361 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
362 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400363 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400364 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400365 public RemoteViews bigContentView;
366
Chris Wren8fd39ec2014-02-27 17:43:26 -0500367
368 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400369 * A medium-format version of {@link #contentView}, providing the Notification an
370 * opportunity to add action buttons to contentView. At its discretion, the system UI may
371 * choose to show this as a heads-up notification, which will pop up so the user can see
372 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400373 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500374 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400375 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
376 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500377 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400378 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500379 public RemoteViews headsUpContentView;
380
Julia Reynoldsfc640012018-02-21 12:25:27 -0500381 private boolean mUsesStandardHeader;
382
383 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
384 static {
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
389 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
390 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
391 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
392 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500393 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500394 }
395
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400396 /**
Dan Sandler86647982015-05-13 23:41:13 -0400397 * A large bitmap to be shown in the notification content area.
398 *
399 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 */
Dan Sandler86647982015-05-13 23:41:13 -0400401 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800402 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
404 /**
405 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400408 * A notification that is noisy is more likely to be presented as a heads-up notification.
409 * </p>
410 *
411 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500412 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500414 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500416 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 public Uri sound;
418
419 /**
420 * Use this constant as the value for audioStreamType to request that
421 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700422 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400423 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500424 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700426 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 public static final int STREAM_DEFAULT = -1;
428
429 /**
430 * The audio stream type to use when playing the sound.
431 * Should be one of the STREAM_ constants from
432 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400433 *
434 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700436 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 public int audioStreamType = STREAM_DEFAULT;
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400440 * The default value of {@link #audioAttributes}.
441 */
442 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
443 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
444 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
445 .build();
446
447 /**
448 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500449 *
450 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400451 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500452 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400453 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
454
455 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 * The pattern with which to vibrate.
457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * <p>
459 * To vibrate the default pattern, see {@link #defaults}.
460 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500463 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500465 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public long[] vibrate;
467
468 /**
469 * The color of the led. The hardware will do its best approximation.
470 *
471 * @see #FLAG_SHOW_LIGHTS
472 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500473 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 */
Tor Norbye80756e32015-03-02 09:39:27 -0800475 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500476 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 public int ledARGB;
478
479 /**
480 * The number of milliseconds for the LED to be on while it's flashing.
481 * The hardware will do its best approximation.
482 *
483 * @see #FLAG_SHOW_LIGHTS
484 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500485 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500487 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 public int ledOnMS;
489
490 /**
491 * The number of milliseconds for the LED to be off while it's flashing.
492 * The hardware will do its best approximation.
493 *
494 * @see #FLAG_SHOW_LIGHTS
495 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500496 *
497 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500499 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public int ledOffMS;
501
502 /**
503 * Specifies which values should be taken from the defaults.
504 * <p>
505 * To set, OR the desired from {@link #DEFAULT_SOUND},
506 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
507 * values, use {@link #DEFAULT_ALL}.
508 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500509 *
510 * @deprecated use {@link NotificationChannel#getSound()} and
511 * {@link NotificationChannel#shouldShowLights()} and
512 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500514 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 public int defaults;
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 /**
518 * Bit to be bitwise-ored into the {@link #flags} field that should be
519 * set if you want the LED on for this notification.
520 * <ul>
521 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
522 * or 0 for both ledOnMS and ledOffMS.</li>
523 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
524 * <li>To flash the LED, pass the number of milliseconds that it should
525 * be on and off to ledOnMS and ledOffMS.</li>
526 * </ul>
527 * <p>
528 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800529 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 * because they will be set to values that work on any given hardware.
531 * <p>
532 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500534 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500536 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
538
539 /**
540 * Bit to be bitwise-ored into the {@link #flags} field that should be
541 * set if this notification is in reference to something that is ongoing,
542 * like a phone call. It should not be set if this notification is in
543 * reference to something that happened at a particular point in time,
544 * like a missed phone call.
545 */
546 public static final int FLAG_ONGOING_EVENT = 0x00000002;
547
548 /**
549 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700550 * the audio will be repeated until the notification is
551 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 */
553 public static final int FLAG_INSISTENT = 0x00000004;
554
555 /**
556 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700557 * set if you would only like the sound, vibrate and ticker to be played
558 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 */
560 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
561
562 /**
563 * Bit to be bitwise-ored into the {@link #flags} field that should be
564 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500565 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 */
567 public static final int FLAG_AUTO_CANCEL = 0x00000010;
568
569 /**
570 * Bit to be bitwise-ored into the {@link #flags} field that should be
571 * set if the notification should not be canceled when the user clicks
572 * the Clear all button.
573 */
574 public static final int FLAG_NO_CLEAR = 0x00000020;
575
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700576 /**
577 * Bit to be bitwise-ored into the {@link #flags} field that should be
578 * set if this notification represents a currently running service. This
579 * will normally be set for you by {@link Service#startForeground}.
580 */
581 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
582
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400583 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800585 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400587 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700588 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400590
Griff Hazendfcb0802014-02-11 12:00:00 -0800591 /**
592 * Bit to be bitswise-ored into the {@link #flags} field that should be
593 * set if this notification is relevant to the current device only
594 * and it is not recommended that it bridge to other devices.
595 */
596 public static final int FLAG_LOCAL_ONLY = 0x00000100;
597
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700598 /**
599 * Bit to be bitswise-ored into the {@link #flags} field that should be
600 * set if this notification is the group summary for a group of notifications.
601 * Grouped notifications may display in a cluster or stack on devices which
602 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
603 */
604 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
605
Julia Reynoldse46bb372016-03-17 11:05:58 -0400606 /**
607 * Bit to be bitswise-ored into the {@link #flags} field that should be
608 * set if this notification is the group summary for an auto-group of notifications.
609 *
610 * @hide
611 */
612 @SystemApi
613 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
614
Julia Reynolds4db59552017-06-30 13:34:01 -0400615 /**
616 * @hide
617 */
618 public static final int FLAG_CAN_COLORIZE = 0x00000800;
619
Mady Mellor7eb18ef2019-03-27 14:03:46 -0700620 /**
Mady Mellor65dcaa92019-04-03 12:21:44 -0700621 * Bit to be bitswised-ored into the {@link #flags} field that should be set if this
622 * notification is showing as a bubble. This will be set by the system if it is determined
623 * that your notification is allowed to be a bubble.
624 *
625 * @see {@link Notification.Builder#setBubbleMetadata(BubbleMetadata)}
Mady Mellor7eb18ef2019-03-27 14:03:46 -0700626 */
627 public static final int FLAG_BUBBLE = 0x00001000;
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public int flags;
630
Tor Norbyed9273d62013-05-30 15:59:53 -0700631 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700632 @IntDef(prefix = { "PRIORITY_" }, value = {
633 PRIORITY_DEFAULT,
634 PRIORITY_LOW,
635 PRIORITY_MIN,
636 PRIORITY_HIGH,
637 PRIORITY_MAX
638 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700639 @Retention(RetentionPolicy.SOURCE)
640 public @interface Priority {}
641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 * Default notification {@link #priority}. If your application does not prioritize its own
644 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500645 *
646 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500648 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 public static final int PRIORITY_DEFAULT = 0;
650
651 /**
652 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
653 * items smaller, or at a different position in the list, compared with your app's
654 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500655 *
656 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500658 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659 public static final int PRIORITY_LOW = -1;
660
661 /**
662 * Lowest {@link #priority}; these items might not be shown to the user except under special
663 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 *
665 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500667 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500668 public static final int PRIORITY_MIN = -2;
669
670 /**
671 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
672 * show these items larger, or at a different position in notification lists, compared with
673 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500674 *
675 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500676 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500677 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500678 public static final int PRIORITY_HIGH = 1;
679
680 /**
681 * Highest {@link #priority}, for your application's most important items that require the
682 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500683 *
684 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500685 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500686 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500687 public static final int PRIORITY_MAX = 2;
688
689 /**
690 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800691 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500692 * Priority is an indication of how much of the user's valuable attention should be consumed by
693 * this notification. Low-priority notifications may be hidden from the user in certain
694 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500695 * system will make a determination about how to interpret this priority when presenting
696 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400697 *
698 * <p>
699 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
700 * as a heads-up notification.
701 * </p>
702 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500703 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500704 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700705 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500706 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500707 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800708
Dan Sandler26e81cf2014-05-06 10:01:27 -0400709 /**
710 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
711 * to be applied by the standard Style templates when presenting this notification.
712 *
713 * The current template design constructs a colorful header image by overlaying the
714 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
715 * ignored.
716 */
Tor Norbye80756e32015-03-02 09:39:27 -0800717 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400718 public int color = COLOR_DEFAULT;
719
720 /**
721 * Special value of {@link #color} telling the system not to decorate this notification with
722 * any special color but instead use default colors when presenting this notification.
723 */
Tor Norbye80756e32015-03-02 09:39:27 -0800724 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400725 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600726
727 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800728 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800729 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800730 */
731 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800732 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800733
734 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700735 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
736 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600737 * lockscreen).
738 *
739 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
740 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
741 * shown in all situations, but the contents are only available if the device is unlocked for
742 * the appropriate user.
743 *
744 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
745 * can be read even in an "insecure" context (that is, above a secure lockscreen).
746 * To modify the public version of this notification—for example, to redact some portions—see
747 * {@link Builder#setPublicVersion(Notification)}.
748 *
749 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
750 * and ticker until the user has bypassed the lockscreen.
751 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600752 public @Visibility int visibility;
753
754 /** @hide */
755 @IntDef(prefix = { "VISIBILITY_" }, value = {
756 VISIBILITY_PUBLIC,
757 VISIBILITY_PRIVATE,
758 VISIBILITY_SECRET,
759 })
760 @Retention(RetentionPolicy.SOURCE)
761 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600762
Griff Hazenfc3922d2014-08-20 11:56:44 -0700763 /**
764 * Notification visibility: Show this notification in its entirety on all lockscreens.
765 *
766 * {@see #visibility}
767 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600768 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700769
770 /**
771 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
772 * private information on secure lockscreens.
773 *
774 * {@see #visibility}
775 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600776 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700777
778 /**
779 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
780 *
781 * {@see #visibility}
782 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600783 public static final int VISIBILITY_SECRET = -1;
784
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400786 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789
790 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700791 * Notification category: map turn-by-turn navigation.
792 */
793 public static final String CATEGORY_NAVIGATION = "navigation";
794
795 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400796 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500797 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799
800 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400801 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500802 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500804
805 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400806 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500807 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400808 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500809
810 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400811 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500812 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400813 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500814
815 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400816 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500817 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400818 public static final String CATEGORY_ALARM = "alarm";
819
820 /**
821 * Notification category: progress of a long-running background operation.
822 */
823 public static final String CATEGORY_PROGRESS = "progress";
824
825 /**
826 * Notification category: social network or sharing update.
827 */
828 public static final String CATEGORY_SOCIAL = "social";
829
830 /**
831 * Notification category: error in background operation or authentication status.
832 */
833 public static final String CATEGORY_ERROR = "err";
834
835 /**
836 * Notification category: media transport control for playback.
837 */
838 public static final String CATEGORY_TRANSPORT = "transport";
839
840 /**
841 * Notification category: system or device status update. Reserved for system use.
842 */
843 public static final String CATEGORY_SYSTEM = "sys";
844
845 /**
846 * Notification category: indication of running background service.
847 */
848 public static final String CATEGORY_SERVICE = "service";
849
850 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400851 * Notification category: a specific, timely recommendation for a single thing.
852 * For example, a news app might want to recommend a news story it believes the user will
853 * want to read next.
854 */
855 public static final String CATEGORY_RECOMMENDATION = "recommendation";
856
857 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400858 * Notification category: ongoing information about device or contextual status.
859 */
860 public static final String CATEGORY_STATUS = "status";
861
862 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400863 * Notification category: user-scheduled reminder.
864 */
865 public static final String CATEGORY_REMINDER = "reminder";
866
867 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700868 * Notification category: extreme car emergencies.
869 * @hide
870 */
871 @SystemApi
872 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
873
874 /**
875 * Notification category: car warnings.
876 * @hide
877 */
878 @SystemApi
879 public static final String CATEGORY_CAR_WARNING = "car_warning";
880
881 /**
882 * Notification category: general car system information.
883 * @hide
884 */
885 @SystemApi
886 public static final String CATEGORY_CAR_INFORMATION = "car_information";
887
888 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400889 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
890 * that best describes this Notification. May be used by the system for ranking and filtering.
891 */
892 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500893
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100894 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700895 private String mGroupKey;
896
897 /**
898 * Get the key used to group this notification into a cluster or stack
899 * with other notifications on devices which support such rendering.
900 */
901 public String getGroup() {
902 return mGroupKey;
903 }
904
905 private String mSortKey;
906
907 /**
908 * Get a sort key that orders this notification among other notifications from the
909 * same package. This can be useful if an external sort was already applied and an app
910 * would like to preserve this. Notifications will be sorted lexicographically using this
911 * value, although providing different priorities in addition to providing sort key may
912 * cause this value to be ignored.
913 *
914 * <p>This sort key can also be used to order members of a notification group. See
915 * {@link Builder#setGroup}.
916 *
917 * @see String#compareTo(String)
918 */
919 public String getSortKey() {
920 return mSortKey;
921 }
922
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500923 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400924 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400925 * <p>
926 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
927 * APIs, and are intended to be used by
928 * {@link android.service.notification.NotificationListenerService} implementations to extract
929 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500930 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400931 public Bundle extras = new Bundle();
932
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700934 * All pending intents in the notification as the system needs to be able to access them but
935 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700936 * custom parcelable objects.
937 *
938 * @hide
939 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100940 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700941 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700942
943 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700944 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
945 * pending intents inside of it, so only those will get the behavior.
946 *
947 * @hide
948 */
Adrian Roosfb921842017-10-26 14:49:56 +0200949 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700950
951 /**
952 * Must be set by a process to start associating tokens with Notification objects
953 * coming in to it. This is set by NotificationManagerService.
954 *
955 * @hide
956 */
957 static public IBinder processWhitelistToken;
958
959 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400960 * {@link #extras} key: this is the title of the notification,
961 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
962 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500963 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400964
965 /**
966 * {@link #extras} key: this is the title of the notification when shown in expanded form,
967 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
968 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400969 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400970
971 /**
972 * {@link #extras} key: this is the main text payload, as supplied to
973 * {@link Builder#setContentText(CharSequence)}.
974 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500975 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400976
977 /**
978 * {@link #extras} key: this is a third line of text, as supplied to
979 * {@link Builder#setSubText(CharSequence)}.
980 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400981 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400982
983 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800984 * {@link #extras} key: this is the remote input history, as supplied to
985 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700986 *
987 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
988 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
989 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
990 * notifications once the other party has responded).
991 *
992 * The extra with this key is of type CharSequence[] and contains the most recent entry at
993 * the 0 index, the second most recent at the 1 index, etc.
994 *
995 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800996 */
997 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
998
999 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01001000 * {@link #extras} key: boolean as supplied to
1001 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
1002 *
1003 * If set to true, then the view displaying the remote input history from
1004 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
1005 *
1006 * @see Builder#setShowRemoteInputSpinner(boolean)
1007 * @hide
1008 */
1009 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
1010
1011 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01001012 * {@link #extras} key: boolean as supplied to
1013 * {@link Builder#setHideSmartReplies(boolean)}.
1014 *
1015 * If set to true, then any smart reply buttons will be hidden.
1016 *
1017 * @see Builder#setHideSmartReplies(boolean)
1018 * @hide
1019 */
1020 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1021
1022 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001023 * {@link #extras} key: this is a small piece of additional text as supplied to
1024 * {@link Builder#setContentInfo(CharSequence)}.
1025 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001026 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001027
1028 /**
1029 * {@link #extras} key: this is a line of summary information intended to be shown
1030 * alongside expanded notifications, as supplied to (e.g.)
1031 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1032 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001033 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001034
1035 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001036 * {@link #extras} key: this is the longer text shown in the big form of a
1037 * {@link BigTextStyle} notification, as supplied to
1038 * {@link BigTextStyle#bigText(CharSequence)}.
1039 */
1040 public static final String EXTRA_BIG_TEXT = "android.bigText";
1041
1042 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1044 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001045 *
1046 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001047 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001048 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001049 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001050
1051 /**
1052 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1053 * notification payload, as
1054 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001055 *
1056 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001057 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001058 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001059 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001060
1061 /**
1062 * {@link #extras} key: this is a bitmap to be used instead of the one from
1063 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1064 * shown in its expanded form, as supplied to
1065 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1066 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001067 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001068
1069 /**
1070 * {@link #extras} key: this is the progress value supplied to
1071 * {@link Builder#setProgress(int, int, boolean)}.
1072 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001073 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001074
1075 /**
1076 * {@link #extras} key: this is the maximum value supplied to
1077 * {@link Builder#setProgress(int, int, boolean)}.
1078 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001079 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001080
1081 /**
1082 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1083 * {@link Builder#setProgress(int, int, boolean)}.
1084 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001085 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001086
1087 /**
1088 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1089 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1090 * {@link Builder#setUsesChronometer(boolean)}.
1091 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001092 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001093
1094 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001095 * {@link #extras} key: whether the chronometer set on the notification should count down
1096 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001097 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001098 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001099 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001100
1101 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001102 * {@link #extras} key: whether {@link #when} should be shown,
1103 * as supplied to {@link Builder#setShowWhen(boolean)}.
1104 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001105 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001106
1107 /**
1108 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1109 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1110 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001111 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001112
1113 /**
1114 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1115 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1116 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001117 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001118
1119 /**
1120 * {@link #extras} key: A string representing the name of the specific
1121 * {@link android.app.Notification.Style} used to create this notification.
1122 */
Chris Wren91ad5632013-06-05 15:05:57 -04001123 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001124
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001125 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001126 * {@link #extras} key: A String array containing the people that this notification relates to,
1127 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001128 *
1129 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001130 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001131 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001132
1133 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001134 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1135 * this notification relates to.
1136 */
1137 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1138
1139 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001140 * Allow certain system-generated notifications to appear before the device is provisioned.
1141 * Only available to notifications coming from the android package.
1142 * @hide
1143 */
Maurice Lam96c10032017-03-29 15:42:38 -07001144 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001145 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001146 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1147
1148 /**
Robin Leed107af62018-04-27 13:55:56 +02001149 * {@link #extras} key:
1150 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1151 * pointing to an image that can be displayed in the background when the notification is
1152 * selected. Used on television platforms. The URI must point to an image stream suitable for
1153 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001154 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001155 */
1156 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1157
1158 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001159 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001160 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001161 * {@link android.app.Notification.MediaStyle} notification.
1162 */
1163 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1164
1165 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001166 * {@link #extras} key: the indices of actions to be shown in the compact view,
1167 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1168 */
1169 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1170
Christoph Studer943aa672014-08-03 20:31:16 +02001171 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001172 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1173 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001174 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1175 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001176 *
1177 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001178 */
1179 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1180
1181 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001182 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1183 * direct replies
1184 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1185 * {@link Person}
1186 */
1187 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1188
1189 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001190 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001191 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001192 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001193 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001194
1195 /**
1196 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1197 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001198 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1199 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001200 */
1201 public static final String EXTRA_MESSAGES = "android.messages";
1202
1203 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001204 * {@link #extras} key: an array of
1205 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1206 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1207 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1208 * array of bundles.
1209 */
1210 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1211
1212 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001213 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1214 * represents a group conversation.
1215 */
1216 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1217
1218 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001219 * {@link #extras} key: whether the notification should be colorized as
Gustav Senntondf4c2442019-03-18 11:41:11 +00001220 * supplied to {@link Builder#setColorized(boolean)}.
Selim Cinek7b9605b2017-01-19 17:36:00 -08001221 */
1222 public static final String EXTRA_COLORIZED = "android.colorized";
1223
1224 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001225 * @hide
1226 */
1227 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1228
Selim Cinek247fa012016-02-18 09:50:48 -08001229 /**
1230 * @hide
1231 */
1232 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1233
Shane Brennan472a3b32016-12-12 15:28:10 -08001234 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001235 * @hide
1236 */
1237 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1238
1239 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001240 * {@link #extras} key: the audio contents of this notification.
1241 *
1242 * This is for use when rendering the notification on an audio-focused interface;
1243 * the audio contents are a complete sound sample that contains the contents/body of the
1244 * notification. This may be used in substitute of a Text-to-Speech reading of the
1245 * notification. For example if the notification represents a voice message this should point
1246 * to the audio of that message.
1247 *
1248 * The data stored under this key should be a String representation of a Uri that contains the
1249 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1250 *
1251 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1252 * has a field for holding data URI. That field can be used for audio.
1253 * See {@code Message#setData}.
1254 *
1255 * Example usage:
1256 * <pre>
1257 * {@code
1258 * Notification.Builder myBuilder = (build your Notification as normal);
1259 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1260 * }
1261 * </pre>
1262 */
1263 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1264
Dan Sandler80eaa592016-04-14 23:34:54 -04001265 /** @hide */
1266 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001267 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001268 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1269
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001270 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001271 * This is set on the notifications shown by system_server about apps running foreground
1272 * services. It indicates that the notification should be shown
1273 * only if any of the given apps do not already have a properly tagged
1274 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1275 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001276 * @hide
1277 */
1278 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1279
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001280 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001281 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001282 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001283 private Icon mLargeIcon;
1284
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001285 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001286 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001287 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001288
Julia Reynolds13d898c2017-02-02 12:22:05 -05001289 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001290 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001291 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001292
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001293 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001294
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001295 /** @hide */
1296 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1297 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1298 })
1299 @Retention(RetentionPolicy.SOURCE)
1300 public @interface GroupAlertBehavior {}
1301
1302 /**
1303 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1304 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1305 * notification will not be muted when it is in a group.
1306 */
1307 public static final int GROUP_ALERT_ALL = 0;
1308
1309 /**
1310 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1311 * notification in a group should be silenced (no sound or vibration) even if they are posted
1312 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001313 * mute this notification if this notification is a group child. This must be applied to all
1314 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001315 *
1316 * <p> For example, you might want to use this constant if you post a number of children
1317 * notifications at once (say, after a periodic sync), and only need to notify the user
1318 * audibly once.
1319 */
1320 public static final int GROUP_ALERT_SUMMARY = 1;
1321
1322 /**
1323 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1324 * notification in a group should be silenced (no sound or vibration) even if they are
1325 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1326 * to mute this notification if this notification is a group summary.
1327 *
1328 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001329 * in your group have content and the summary is only used to visually group notifications
1330 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001331 */
1332 public static final int GROUP_ALERT_CHILDREN = 2;
1333
Julia Reynolds2f431e22017-06-07 14:12:09 +00001334 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001335
Julia Reynolds13d898c2017-02-02 12:22:05 -05001336 /**
1337 * If this notification is being shown as a badge, always show as a number.
1338 */
1339 public static final int BADGE_ICON_NONE = 0;
1340
1341 /**
1342 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1343 * represent this notification.
1344 */
1345 public static final int BADGE_ICON_SMALL = 1;
1346
1347 /**
1348 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1349 * represent this notification.
1350 */
1351 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001352 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001353
Chris Wren51c75102013-07-16 20:49:17 -04001354 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001355 * Determines whether the platform can generate contextual actions for a notification.
1356 */
1357 private boolean mAllowSystemGeneratedContextualActions = true;
1358
1359 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001360 * Structure to encapsulate a named action that can be shown as part of this notification.
1361 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1362 * selected by the user.
1363 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001364 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1365 * or {@link Notification.Builder#addAction(Notification.Action)}
1366 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001367 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001368 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001369 /**
1370 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1371 * {@link RemoteInput}s.
1372 *
1373 * This is intended for {@link RemoteInput}s that only accept data, meaning
1374 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
Gustav Senntondf4c2442019-03-18 11:41:11 +00001375 * is null or empty, and {@link RemoteInput#getAllowedDataTypes} is non-null and not
Shane Brennan472a3b32016-12-12 15:28:10 -08001376 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1377 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1378 *
1379 * You can test if a RemoteInput matches these constraints using
1380 * {@link RemoteInput#isDataOnly}.
1381 */
1382 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1383
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001384 /**
1385 * {@link }: No semantic action defined.
1386 */
1387 public static final int SEMANTIC_ACTION_NONE = 0;
1388
1389 /**
1390 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1391 * may be appropriate.
1392 */
1393 public static final int SEMANTIC_ACTION_REPLY = 1;
1394
1395 /**
1396 * {@code SemanticAction}: Mark content as read.
1397 */
1398 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1399
1400 /**
1401 * {@code SemanticAction}: Mark content as unread.
1402 */
1403 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1404
1405 /**
1406 * {@code SemanticAction}: Delete the content associated with the notification. This
1407 * could mean deleting an email, message, etc.
1408 */
1409 public static final int SEMANTIC_ACTION_DELETE = 4;
1410
1411 /**
1412 * {@code SemanticAction}: Archive the content associated with the notification. This
1413 * could mean archiving an email, message, etc.
1414 */
1415 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1416
1417 /**
1418 * {@code SemanticAction}: Mute the content associated with the notification. This could
1419 * mean silencing a conversation or currently playing media.
1420 */
1421 public static final int SEMANTIC_ACTION_MUTE = 6;
1422
1423 /**
1424 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1425 * mean un-silencing a conversation or currently playing media.
1426 */
1427 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1428
1429 /**
1430 * {@code SemanticAction}: Mark content with a thumbs up.
1431 */
1432 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1433
1434 /**
1435 * {@code SemanticAction}: Mark content with a thumbs down.
1436 */
1437 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1438
Kodlee Yincda5b092018-02-15 15:34:53 -08001439 /**
1440 * {@code SemanticAction}: Call a contact, group, etc.
1441 */
1442 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001443
Griff Hazen959591e2014-05-15 22:26:18 -07001444 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001445 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001446 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001447 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001448 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001449 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001450 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001451
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001452 /**
1453 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001454 *
1455 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001456 */
Dan Sandler86647982015-05-13 23:41:13 -04001457 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001458 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001459
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001460 /**
1461 * Title of the action.
1462 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001463 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001464
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001465 /**
1466 * Intent to send when the user invokes this action. May be null, in which case the action
1467 * may be rendered in a disabled presentation by the system UI.
1468 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001469 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001470
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001471 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001472 if (in.readInt() != 0) {
1473 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001474 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1475 icon = mIcon.getResId();
1476 }
Dan Sandler86647982015-05-13 23:41:13 -04001477 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001478 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1479 if (in.readInt() == 1) {
1480 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1481 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001482 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001483 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001484 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001485 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001486 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001487 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001488
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001489 /**
Dan Sandler86647982015-05-13 23:41:13 -04001490 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001491 */
Dan Sandler86647982015-05-13 23:41:13 -04001492 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001493 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001494 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001495 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001496 }
1497
Adrian Roos7af53622016-10-12 13:44:05 -07001498 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001499 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001500 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001501 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001502 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001503 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1504 this.icon = icon.getResId();
1505 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001506 this.title = title;
1507 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001508 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001509 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001510 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001511 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001512 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001513 }
1514
1515 /**
Dan Sandler86647982015-05-13 23:41:13 -04001516 * Return an icon representing the action.
1517 */
1518 public Icon getIcon() {
1519 if (mIcon == null && icon != 0) {
1520 // you snuck an icon in here without using the builder; let's try to keep it
1521 mIcon = Icon.createWithResource("", icon);
1522 }
1523 return mIcon;
1524 }
1525
1526 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001527 * Get additional metadata carried around with this Action.
1528 */
1529 public Bundle getExtras() {
1530 return mExtras;
1531 }
1532
1533 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001534 * Return whether the platform should automatically generate possible replies for this
1535 * {@link Action}
1536 */
1537 public boolean getAllowGeneratedReplies() {
1538 return mAllowGeneratedReplies;
1539 }
1540
1541 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001542 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001543 * May return null if no remote inputs were added. Only returns inputs which accept
1544 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001545 */
1546 public RemoteInput[] getRemoteInputs() {
1547 return mRemoteInputs;
1548 }
1549
1550 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001551 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1552 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1553 * (eg. reply, mark as read, delete, etc).
1554 */
1555 public @SemanticAction int getSemanticAction() {
1556 return mSemanticAction;
1557 }
1558
1559 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001560 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1561 * notification message body. An example of a contextual action could be an action opening a
1562 * map application with an address shown in the notification.
1563 */
1564 public boolean isContextual() {
1565 return mIsContextual;
1566 }
1567
1568 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001569 * Get the list of inputs to be collected from the user that ONLY accept data when this
1570 * action is sent. These remote inputs are guaranteed to return true on a call to
1571 * {@link RemoteInput#isDataOnly}.
1572 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001573 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001574 *
1575 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1576 * of non-textual RemoteInputs do not access these remote inputs.
1577 */
1578 public RemoteInput[] getDataOnlyRemoteInputs() {
1579 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1580 }
1581
1582 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001583 * Builder class for {@link Action} objects.
1584 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001585 public static final class Builder {
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001586 @Nullable private final Icon mIcon;
1587 @Nullable private final CharSequence mTitle;
1588 @Nullable private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001589 private boolean mAllowGeneratedReplies = true;
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001590 @NonNull private final Bundle mExtras;
1591 @Nullable private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001592 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001593 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001594
1595 /**
1596 * Construct a new builder for {@link Action} object.
1597 * @param icon icon to show for this action
1598 * @param title the title of the action
1599 * @param intent the {@link PendingIntent} to fire when users trigger this action
1600 */
Dan Sandler86647982015-05-13 23:41:13 -04001601 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001602 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001603 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001604 }
1605
1606 /**
1607 * Construct a new builder for {@link Action} object.
1608 * @param icon icon to show for this action
1609 * @param title the title of the action
1610 * @param intent the {@link PendingIntent} to fire when users trigger this action
1611 */
1612 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001613 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001614 }
1615
1616 /**
1617 * Construct a new builder for {@link Action} object using the fields from an
1618 * {@link Action}.
1619 * @param action the action to read fields from.
1620 */
1621 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001622 this(action.getIcon(), action.title, action.actionIntent,
1623 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001624 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001625 }
1626
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001627 private Builder(@Nullable Icon icon, @Nullable CharSequence title,
1628 @Nullable PendingIntent intent, @NonNull Bundle extras,
1629 @Nullable RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1630 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001631 mIcon = icon;
1632 mTitle = title;
1633 mIntent = intent;
1634 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001635 if (remoteInputs != null) {
1636 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1637 Collections.addAll(mRemoteInputs, remoteInputs);
1638 }
Adrian Roos7af53622016-10-12 13:44:05 -07001639 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001640 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001641 }
1642
1643 /**
1644 * Merge additional metadata into this builder.
1645 *
1646 * <p>Values within the Bundle will replace existing extras values in this Builder.
1647 *
1648 * @see Notification.Action#extras
1649 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001650 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001651 public Builder addExtras(Bundle extras) {
1652 if (extras != null) {
1653 mExtras.putAll(extras);
1654 }
1655 return this;
1656 }
1657
1658 /**
1659 * Get the metadata Bundle used by this Builder.
1660 *
1661 * <p>The returned Bundle is shared with this Builder.
1662 */
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001663 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001664 public Bundle getExtras() {
1665 return mExtras;
1666 }
1667
1668 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001669 * Add an input to be collected from the user when this action is sent.
1670 * Response values can be retrieved from the fired intent by using the
1671 * {@link RemoteInput#getResultsFromIntent} function.
1672 * @param remoteInput a {@link RemoteInput} to add to the action
1673 * @return this object for method chaining
1674 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001675 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001676 public Builder addRemoteInput(RemoteInput remoteInput) {
1677 if (mRemoteInputs == null) {
1678 mRemoteInputs = new ArrayList<RemoteInput>();
1679 }
1680 mRemoteInputs.add(remoteInput);
1681 return this;
1682 }
1683
1684 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001685 * Set whether the platform should automatically generate possible replies to add to
1686 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1687 * {@link RemoteInput}, this has no effect.
1688 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1689 * otherwise
1690 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001691 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001692 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001693 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001694 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1695 mAllowGeneratedReplies = allowGeneratedReplies;
1696 return this;
1697 }
1698
1699 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001700 * Sets the {@code SemanticAction} for this {@link Action}. A
1701 * {@code SemanticAction} denotes what an {@link Action}'s
1702 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1703 * @param semanticAction a SemanticAction defined within {@link Action} with
1704 * {@code SEMANTIC_ACTION_} prefixes
1705 * @return this object for method chaining
1706 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001707 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001708 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1709 mSemanticAction = semanticAction;
1710 return this;
1711 }
1712
1713 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001714 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1715 * dependent on the notification message body. An example of a contextual action could
1716 * be an action opening a map application with an address shown in the notification.
1717 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001718 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001719 public Builder setContextual(boolean isContextual) {
1720 mIsContextual = isContextual;
1721 return this;
1722 }
1723
1724 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001725 * Apply an extender to this action builder. Extenders may be used to add
1726 * metadata or change options on this builder.
1727 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001728 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001729 public Builder extend(Extender extender) {
1730 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001731 return this;
1732 }
1733
1734 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001735 * Throws an NPE if we are building a contextual action missing one of the fields
1736 * necessary to display the action.
1737 */
1738 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001739 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001740
1741 if (mIcon == null) {
1742 throw new NullPointerException("Contextual Actions must contain a valid icon");
1743 }
1744
1745 if (mIntent == null) {
1746 throw new NullPointerException(
1747 "Contextual Actions must contain a valid PendingIntent");
1748 }
1749 }
1750
1751 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001752 * Combine all of the options that have been set and return a new {@link Action}
1753 * object.
1754 * @return the built action
1755 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001756 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001757 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001758 checkContextualActionNullFields();
1759
Shane Brennan472a3b32016-12-12 15:28:10 -08001760 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1761 RemoteInput[] previousDataInputs =
1762 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001763 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001764 for (RemoteInput input : previousDataInputs) {
1765 dataOnlyInputs.add(input);
1766 }
1767 }
1768 List<RemoteInput> textInputs = new ArrayList<>();
1769 if (mRemoteInputs != null) {
1770 for (RemoteInput input : mRemoteInputs) {
1771 if (input.isDataOnly()) {
1772 dataOnlyInputs.add(input);
1773 } else {
1774 textInputs.add(input);
1775 }
1776 }
1777 }
1778 if (!dataOnlyInputs.isEmpty()) {
1779 RemoteInput[] dataInputsArr =
1780 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1781 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1782 }
1783 RemoteInput[] textInputsArr = textInputs.isEmpty()
1784 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1785 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001786 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001787 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001788 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001789
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001790 @Override
1791 public Action clone() {
1792 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001793 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001794 title,
1795 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001796 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001797 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001798 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001799 getSemanticAction(),
1800 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001801 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001802
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001803 @Override
1804 public int describeContents() {
1805 return 0;
1806 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001807
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001808 @Override
1809 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001810 final Icon ic = getIcon();
1811 if (ic != null) {
1812 out.writeInt(1);
1813 ic.writeToParcel(out, 0);
1814 } else {
1815 out.writeInt(0);
1816 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001817 TextUtils.writeToParcel(title, out, flags);
1818 if (actionIntent != null) {
1819 out.writeInt(1);
1820 actionIntent.writeToParcel(out, flags);
1821 } else {
1822 out.writeInt(0);
1823 }
Griff Hazen959591e2014-05-15 22:26:18 -07001824 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001825 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001826 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001827 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001828 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001829 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001830
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001831 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001832 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001833 public Action createFromParcel(Parcel in) {
1834 return new Action(in);
1835 }
1836 public Action[] newArray(int size) {
1837 return new Action[size];
1838 }
1839 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001840
1841 /**
1842 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1843 * metadata or change options on an action builder.
1844 */
1845 public interface Extender {
1846 /**
1847 * Apply this extender to a notification action builder.
1848 * @param builder the builder to be modified.
1849 * @return the build object for chaining.
1850 */
1851 public Builder extend(Builder builder);
1852 }
1853
1854 /**
1855 * Wearable extender for notification actions. To add extensions to an action,
1856 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1857 * the {@code WearableExtender()} constructor and apply it to a
1858 * {@link android.app.Notification.Action.Builder} using
1859 * {@link android.app.Notification.Action.Builder#extend}.
1860 *
1861 * <pre class="prettyprint">
1862 * Notification.Action action = new Notification.Action.Builder(
1863 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001864 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001865 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001866 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001867 */
1868 public static final class WearableExtender implements Extender {
1869 /** Notification action extra which contains wearable extensions */
1870 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1871
Pete Gastaf6781d2014-10-07 15:17:05 -04001872 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001873 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001874 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1875 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1876 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001877
1878 // Flags bitwise-ored to mFlags
1879 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001880 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001881 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001882
1883 // Default value for flags integer
1884 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1885
1886 private int mFlags = DEFAULT_FLAGS;
1887
Pete Gastaf6781d2014-10-07 15:17:05 -04001888 private CharSequence mInProgressLabel;
1889 private CharSequence mConfirmLabel;
1890 private CharSequence mCancelLabel;
1891
Griff Hazen61a9e862014-05-22 16:05:19 -07001892 /**
1893 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1894 * options.
1895 */
1896 public WearableExtender() {
1897 }
1898
1899 /**
1900 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1901 * wearable options present in an existing notification action.
1902 * @param action the notification action to inspect.
1903 */
1904 public WearableExtender(Action action) {
1905 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1906 if (wearableBundle != null) {
1907 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001908 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1909 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1910 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001911 }
1912 }
1913
1914 /**
1915 * Apply wearable extensions to a notification action that is being built. This is
1916 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1917 * method of {@link android.app.Notification.Action.Builder}.
1918 */
1919 @Override
1920 public Action.Builder extend(Action.Builder builder) {
1921 Bundle wearableBundle = new Bundle();
1922
1923 if (mFlags != DEFAULT_FLAGS) {
1924 wearableBundle.putInt(KEY_FLAGS, mFlags);
1925 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001926 if (mInProgressLabel != null) {
1927 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1928 }
1929 if (mConfirmLabel != null) {
1930 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1931 }
1932 if (mCancelLabel != null) {
1933 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1934 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001935
1936 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1937 return builder;
1938 }
1939
1940 @Override
1941 public WearableExtender clone() {
1942 WearableExtender that = new WearableExtender();
1943 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001944 that.mInProgressLabel = this.mInProgressLabel;
1945 that.mConfirmLabel = this.mConfirmLabel;
1946 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001947 return that;
1948 }
1949
1950 /**
1951 * Set whether this action is available when the wearable device is not connected to
1952 * a companion device. The user can still trigger this action when the wearable device is
1953 * offline, but a visual hint will indicate that the action may not be available.
1954 * Defaults to true.
1955 */
1956 public WearableExtender setAvailableOffline(boolean availableOffline) {
1957 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1958 return this;
1959 }
1960
1961 /**
1962 * Get whether this action is available when the wearable device is not connected to
1963 * a companion device. The user can still trigger this action when the wearable device is
1964 * offline, but a visual hint will indicate that the action may not be available.
1965 * Defaults to true.
1966 */
1967 public boolean isAvailableOffline() {
1968 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1969 }
1970
1971 private void setFlag(int mask, boolean value) {
1972 if (value) {
1973 mFlags |= mask;
1974 } else {
1975 mFlags &= ~mask;
1976 }
1977 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001978
1979 /**
1980 * Set a label to display while the wearable is preparing to automatically execute the
1981 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1982 *
1983 * @param label the label to display while the action is being prepared to execute
1984 * @return this object for method chaining
1985 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001986 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001987 public WearableExtender setInProgressLabel(CharSequence label) {
1988 mInProgressLabel = label;
1989 return this;
1990 }
1991
1992 /**
1993 * Get the label to display while the wearable is preparing to automatically execute
1994 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1995 *
1996 * @return the label to display while the action is being prepared to execute
1997 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001998 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001999 public CharSequence getInProgressLabel() {
2000 return mInProgressLabel;
2001 }
2002
2003 /**
2004 * Set a label to display to confirm that the action should be executed.
2005 * This is usually an imperative verb like "Send".
2006 *
2007 * @param label the label to confirm the action should be executed
2008 * @return this object for method chaining
2009 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002010 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002011 public WearableExtender setConfirmLabel(CharSequence label) {
2012 mConfirmLabel = label;
2013 return this;
2014 }
2015
2016 /**
2017 * Get the label to display to confirm that the action should be executed.
2018 * This is usually an imperative verb like "Send".
2019 *
2020 * @return the label to confirm the action should be executed
2021 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002022 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002023 public CharSequence getConfirmLabel() {
2024 return mConfirmLabel;
2025 }
2026
2027 /**
2028 * Set a label to display to cancel the action.
2029 * This is usually an imperative verb, like "Cancel".
2030 *
2031 * @param label the label to display to cancel the action
2032 * @return this object for method chaining
2033 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002034 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002035 public WearableExtender setCancelLabel(CharSequence label) {
2036 mCancelLabel = label;
2037 return this;
2038 }
2039
2040 /**
2041 * Get the label to display to cancel the action.
2042 * This is usually an imperative verb like "Cancel".
2043 *
2044 * @return the label to display to cancel the action
2045 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002046 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002047 public CharSequence getCancelLabel() {
2048 return mCancelLabel;
2049 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002050
2051 /**
2052 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2053 * platform that it can generate the appropriate transitions.
2054 * @param hintLaunchesActivity {@code true} if the content intent will launch
2055 * an activity and transitions should be generated, false otherwise.
2056 * @return this object for method chaining
2057 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002058 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002059 boolean hintLaunchesActivity) {
2060 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2061 return this;
2062 }
2063
2064 /**
2065 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2066 * platform that it can generate the appropriate transitions
2067 * @return {@code true} if the content intent will launch an activity and transitions
2068 * should be generated, false otherwise. The default value is {@code false} if this was
2069 * never set.
2070 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002071 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002072 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2073 }
Alex Hills9f087612016-06-07 09:08:59 -04002074
2075 /**
2076 * Set a hint that this Action should be displayed inline.
2077 *
2078 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2079 * otherwise
2080 * @return this object for method chaining
2081 */
2082 public WearableExtender setHintDisplayActionInline(
2083 boolean hintDisplayInline) {
2084 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2085 return this;
2086 }
2087
2088 /**
2089 * Get a hint that this Action should be displayed inline.
2090 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002091 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002092 * otherwise. The default value is {@code false} if this was never set.
2093 */
2094 public boolean getHintDisplayActionInline() {
2095 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2096 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002097 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002098
2099 /**
2100 * Provides meaning to an {@link Action} that hints at what the associated
2101 * {@link PendingIntent} will do. For example, an {@link Action} with a
2102 * {@link PendingIntent} that replies to a text message notification may have the
2103 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2104 *
2105 * @hide
2106 */
2107 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2108 SEMANTIC_ACTION_NONE,
2109 SEMANTIC_ACTION_REPLY,
2110 SEMANTIC_ACTION_MARK_AS_READ,
2111 SEMANTIC_ACTION_MARK_AS_UNREAD,
2112 SEMANTIC_ACTION_DELETE,
2113 SEMANTIC_ACTION_ARCHIVE,
2114 SEMANTIC_ACTION_MUTE,
2115 SEMANTIC_ACTION_UNMUTE,
2116 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002117 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002118 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002119 })
2120 @Retention(RetentionPolicy.SOURCE)
2121 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002122 }
2123
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002124 /**
2125 * Array of all {@link Action} structures attached to this notification by
2126 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2127 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2128 * interface for invoking actions.
2129 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002130 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002131
2132 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002133 * Replacement version of this notification whose content will be shown
2134 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2135 * and {@link #VISIBILITY_PUBLIC}.
2136 */
2137 public Notification publicVersion;
2138
2139 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002140 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002141 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 */
2143 public Notification()
2144 {
2145 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002146 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002147 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
2149
2150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 * @hide
2152 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002153 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 public Notification(Context context, int icon, CharSequence tickerText, long when,
2155 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2156 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002157 new Builder(context)
2158 .setWhen(when)
2159 .setSmallIcon(icon)
2160 .setTicker(tickerText)
2161 .setContentTitle(contentTitle)
2162 .setContentText(contentText)
2163 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2164 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
2166
2167 /**
2168 * Constructs a Notification object with the information needed to
2169 * have a status bar icon without the standard expanded view.
2170 *
2171 * @param icon The resource id of the icon to put in the status bar.
2172 * @param tickerText The text that flows by in the status bar when the notification first
2173 * activates.
2174 * @param when The time to show in the time field. In the System.currentTimeMillis
2175 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002176 *
2177 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002179 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 public Notification(int icon, CharSequence tickerText, long when)
2181 {
2182 this.icon = icon;
2183 this.tickerText = tickerText;
2184 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002185 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
2187
2188 /**
2189 * Unflatten the notification from a parcel.
2190 */
Svet Ganovddb94882016-06-23 19:55:24 -07002191 @SuppressWarnings("unchecked")
2192 public Notification(Parcel parcel) {
2193 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2194 // intents in extras are always written as the last entry.
2195 readFromParcelImpl(parcel);
2196 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002197 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002198 }
2199
2200 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 {
2202 int version = parcel.readInt();
2203
Adrian Roosfb921842017-10-26 14:49:56 +02002204 mWhitelistToken = parcel.readStrongBinder();
2205 if (mWhitelistToken == null) {
2206 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002207 }
2208 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002209 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002212 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002213 if (parcel.readInt() != 0) {
2214 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002215 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2216 icon = mSmallIcon.getResId();
2217 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 number = parcel.readInt();
2220 if (parcel.readInt() != 0) {
2221 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2222 }
2223 if (parcel.readInt() != 0) {
2224 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2225 }
2226 if (parcel.readInt() != 0) {
2227 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2228 }
2229 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002230 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002231 }
2232 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2234 }
Joe Onorato561d3852010-11-20 18:09:34 -08002235 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002236 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 defaults = parcel.readInt();
2239 flags = parcel.readInt();
2240 if (parcel.readInt() != 0) {
2241 sound = Uri.CREATOR.createFromParcel(parcel);
2242 }
2243
2244 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002245 if (parcel.readInt() != 0) {
2246 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 vibrate = parcel.createLongArray();
2249 ledARGB = parcel.readInt();
2250 ledOnMS = parcel.readInt();
2251 ledOffMS = parcel.readInt();
2252 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002253
2254 if (parcel.readInt() != 0) {
2255 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2256 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002257
2258 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002259
John Spurlockfd7f1e02014-03-18 16:41:57 -04002260 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002261
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002262 mGroupKey = parcel.readString();
2263
2264 mSortKey = parcel.readString();
2265
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002266 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002267 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002268
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002269 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2270
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002271 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002272 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2273 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002274
Chris Wren8fd39ec2014-02-27 17:43:26 -05002275 if (parcel.readInt() != 0) {
2276 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2277 }
2278
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002279 visibility = parcel.readInt();
2280
2281 if (parcel.readInt() != 0) {
2282 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2283 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002284
2285 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002286
2287 if (parcel.readInt() != 0) {
2288 mChannelId = parcel.readString();
2289 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002290 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002291
2292 if (parcel.readInt() != 0) {
2293 mShortcutId = parcel.readString();
2294 }
2295
Felipe Leme90205ef2019-03-05 09:59:52 -08002296 if (parcel.readInt() != 0) {
2297 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2298 }
2299
Julia Reynolds13d898c2017-02-02 12:22:05 -05002300 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002301
2302 if (parcel.readInt() != 0) {
2303 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2304 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002305
2306 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002307 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002308 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002309 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002310
2311 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
2313
Andy Stadler110988c2010-12-03 14:29:16 -08002314 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002315 public Notification clone() {
2316 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002317 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002318 return that;
2319 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002320
Daniel Sandler1a497d32013-04-18 14:52:45 -04002321 /**
2322 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2323 * of this into that.
2324 * @hide
2325 */
2326 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002327 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002328 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002329 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002330 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002331 that.number = this.number;
2332
2333 // PendingIntents are global, so there's no reason (or way) to clone them.
2334 that.contentIntent = this.contentIntent;
2335 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002336 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002337
2338 if (this.tickerText != null) {
2339 that.tickerText = this.tickerText.toString();
2340 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002341 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002342 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002343 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002344 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002345 that.contentView = this.contentView.clone();
2346 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002347 if (heavy && this.mLargeIcon != null) {
2348 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002349 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002350 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002351 that.sound = this.sound; // android.net.Uri is immutable
2352 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002353 if (this.audioAttributes != null) {
2354 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2355 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002356
2357 final long[] vibrate = this.vibrate;
2358 if (vibrate != null) {
2359 final int N = vibrate.length;
2360 final long[] vib = that.vibrate = new long[N];
2361 System.arraycopy(vibrate, 0, vib, 0, N);
2362 }
2363
2364 that.ledARGB = this.ledARGB;
2365 that.ledOnMS = this.ledOnMS;
2366 that.ledOffMS = this.ledOffMS;
2367 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002368
Joe Onorato18e69df2010-05-17 22:26:12 -07002369 that.flags = this.flags;
2370
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002371 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002372
John Spurlockfd7f1e02014-03-18 16:41:57 -04002373 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002374
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002375 that.mGroupKey = this.mGroupKey;
2376
2377 that.mSortKey = this.mSortKey;
2378
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002379 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002380 try {
2381 that.extras = new Bundle(this.extras);
2382 // will unparcel
2383 that.extras.size();
2384 } catch (BadParcelableException e) {
2385 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2386 that.extras = null;
2387 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002388 }
2389
Felipe Lemedd85da62016-06-28 11:29:54 -07002390 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2391 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002392 }
2393
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002394 if (this.actions != null) {
2395 that.actions = new Action[this.actions.length];
2396 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002397 if ( this.actions[i] != null) {
2398 that.actions[i] = this.actions[i].clone();
2399 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002400 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002401 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002402
Daniel Sandler1a497d32013-04-18 14:52:45 -04002403 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002404 that.bigContentView = this.bigContentView.clone();
2405 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002406
Chris Wren8fd39ec2014-02-27 17:43:26 -05002407 if (heavy && this.headsUpContentView != null) {
2408 that.headsUpContentView = this.headsUpContentView.clone();
2409 }
2410
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002411 that.visibility = this.visibility;
2412
2413 if (this.publicVersion != null) {
2414 that.publicVersion = new Notification();
2415 this.publicVersion.cloneInto(that.publicVersion, heavy);
2416 }
2417
Dan Sandler26e81cf2014-05-06 10:01:27 -04002418 that.color = this.color;
2419
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002420 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002421 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002422 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002423 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002424 that.mBadgeIcon = this.mBadgeIcon;
2425 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002426 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002427 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002428 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002429
Daniel Sandler1a497d32013-04-18 14:52:45 -04002430 if (!heavy) {
2431 that.lightenPayload(); // will clean out extras
2432 }
2433 }
2434
2435 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002436 * Note all {@link Uri} that are referenced internally, with the expectation
2437 * that Uri permission grants will need to be issued to ensure the recipient
2438 * of this object is able to render its contents.
2439 *
2440 * @hide
2441 */
2442 public void visitUris(@NonNull Consumer<Uri> visitor) {
2443 visitor.accept(sound);
2444
2445 if (tickerView != null) tickerView.visitUris(visitor);
2446 if (contentView != null) contentView.visitUris(visitor);
2447 if (bigContentView != null) bigContentView.visitUris(visitor);
2448 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2449
2450 if (extras != null) {
2451 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002452 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2453 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2454 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002455 }
2456
2457 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2458 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2459 if (!ArrayUtils.isEmpty(messages)) {
2460 for (MessagingStyle.Message message : MessagingStyle.Message
2461 .getMessagesFromBundleArray(messages)) {
2462 visitor.accept(message.getDataUri());
2463 }
2464 }
2465
2466 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2467 if (!ArrayUtils.isEmpty(historic)) {
2468 for (MessagingStyle.Message message : MessagingStyle.Message
2469 .getMessagesFromBundleArray(historic)) {
2470 visitor.accept(message.getDataUri());
2471 }
2472 }
2473 }
2474 }
2475
2476 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002477 * Removes heavyweight parts of the Notification object for archival or for sending to
2478 * listeners when the full contents are not necessary.
2479 * @hide
2480 */
2481 public final void lightenPayload() {
2482 tickerView = null;
2483 contentView = null;
2484 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002485 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002486 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002487 if (extras != null && !extras.isEmpty()) {
2488 final Set<String> keyset = extras.keySet();
2489 final int N = keyset.size();
2490 final String[] keys = keyset.toArray(new String[N]);
2491 for (int i=0; i<N; i++) {
2492 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002493 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2494 continue;
2495 }
Dan Sandler50128532015-12-08 15:42:41 -05002496 final Object obj = extras.get(key);
2497 if (obj != null &&
2498 ( obj instanceof Parcelable
2499 || obj instanceof Parcelable[]
2500 || obj instanceof SparseArray
2501 || obj instanceof ArrayList)) {
2502 extras.remove(key);
2503 }
2504 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002505 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002506 }
2507
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002508 /**
2509 * Make sure this CharSequence is safe to put into a bundle, which basically
2510 * means it had better not be some custom Parcelable implementation.
2511 * @hide
2512 */
2513 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002514 if (cs == null) return cs;
2515 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2516 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2517 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002518 if (cs instanceof Parcelable) {
2519 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2520 + " instance is a custom Parcelable and not allowed in Notification");
2521 return cs.toString();
2522 }
Selim Cinek60a54252016-02-26 17:03:25 -08002523 return removeTextSizeSpans(cs);
2524 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002525
Selim Cinek60a54252016-02-26 17:03:25 -08002526 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2527 if (charSequence instanceof Spanned) {
2528 Spanned ss = (Spanned) charSequence;
2529 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2530 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2531 for (Object span : spans) {
2532 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002533 if (resultSpan instanceof CharacterStyle) {
2534 resultSpan = ((CharacterStyle) span).getUnderlying();
2535 }
2536 if (resultSpan instanceof TextAppearanceSpan) {
2537 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002538 resultSpan = new TextAppearanceSpan(
2539 originalSpan.getFamily(),
2540 originalSpan.getTextStyle(),
2541 -1,
2542 originalSpan.getTextColor(),
2543 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002544 } else if (resultSpan instanceof RelativeSizeSpan
2545 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002546 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002547 } else {
2548 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002549 }
2550 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2551 ss.getSpanFlags(span));
2552 }
2553 return builder;
2554 }
2555 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002556 }
2557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 public int describeContents() {
2559 return 0;
2560 }
2561
2562 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002563 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 */
Svet Ganovddb94882016-06-23 19:55:24 -07002565 public void writeToParcel(Parcel parcel, int flags) {
2566 // We need to mark all pending intents getting into the notification
2567 // system as being put there to later allow the notification ranker
2568 // to launch them and by doing so add the app to the battery saver white
2569 // list for a short period of time. The problem is that the system
2570 // cannot look into the extras as there may be parcelables there that
2571 // the platform does not know how to handle. To go around that we have
2572 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002573 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002574 if (collectPendingIntents) {
2575 PendingIntent.setOnMarshaledListener(
2576 (PendingIntent intent, Parcel out, int outFlags) -> {
2577 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002578 if (allPendingIntents == null) {
2579 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002580 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002581 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002582 }
2583 });
2584 }
2585 try {
2586 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002587 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002588 writeToParcelImpl(parcel, flags);
2589 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002590 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002591 } finally {
2592 if (collectPendingIntents) {
2593 PendingIntent.setOnMarshaledListener(null);
2594 }
2595 }
2596 }
2597
2598 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 parcel.writeInt(1);
2600
Adrian Roosfb921842017-10-26 14:49:56 +02002601 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002603 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002604 if (mSmallIcon == null && icon != 0) {
2605 // you snuck an icon in here without using the builder; let's try to keep it
2606 mSmallIcon = Icon.createWithResource("", icon);
2607 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002608 if (mSmallIcon != null) {
2609 parcel.writeInt(1);
2610 mSmallIcon.writeToParcel(parcel, 0);
2611 } else {
2612 parcel.writeInt(0);
2613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 parcel.writeInt(number);
2615 if (contentIntent != null) {
2616 parcel.writeInt(1);
2617 contentIntent.writeToParcel(parcel, 0);
2618 } else {
2619 parcel.writeInt(0);
2620 }
2621 if (deleteIntent != null) {
2622 parcel.writeInt(1);
2623 deleteIntent.writeToParcel(parcel, 0);
2624 } else {
2625 parcel.writeInt(0);
2626 }
2627 if (tickerText != null) {
2628 parcel.writeInt(1);
2629 TextUtils.writeToParcel(tickerText, parcel, flags);
2630 } else {
2631 parcel.writeInt(0);
2632 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002633 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002634 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002635 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002636 } else {
2637 parcel.writeInt(0);
2638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 if (contentView != null) {
2640 parcel.writeInt(1);
2641 contentView.writeToParcel(parcel, 0);
2642 } else {
2643 parcel.writeInt(0);
2644 }
Selim Cinek279fa862016-06-14 10:57:25 -07002645 if (mLargeIcon == null && largeIcon != null) {
2646 // you snuck an icon in here without using the builder; let's try to keep it
2647 mLargeIcon = Icon.createWithBitmap(largeIcon);
2648 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002649 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002650 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002651 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002652 } else {
2653 parcel.writeInt(0);
2654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655
2656 parcel.writeInt(defaults);
2657 parcel.writeInt(this.flags);
2658
2659 if (sound != null) {
2660 parcel.writeInt(1);
2661 sound.writeToParcel(parcel, 0);
2662 } else {
2663 parcel.writeInt(0);
2664 }
2665 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002666
2667 if (audioAttributes != null) {
2668 parcel.writeInt(1);
2669 audioAttributes.writeToParcel(parcel, 0);
2670 } else {
2671 parcel.writeInt(0);
2672 }
2673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 parcel.writeLongArray(vibrate);
2675 parcel.writeInt(ledARGB);
2676 parcel.writeInt(ledOnMS);
2677 parcel.writeInt(ledOffMS);
2678 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002679
2680 if (fullScreenIntent != null) {
2681 parcel.writeInt(1);
2682 fullScreenIntent.writeToParcel(parcel, 0);
2683 } else {
2684 parcel.writeInt(0);
2685 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002686
2687 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002688
John Spurlockfd7f1e02014-03-18 16:41:57 -04002689 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002690
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002691 parcel.writeString(mGroupKey);
2692
2693 parcel.writeString(mSortKey);
2694
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002695 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002696
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002697 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002698
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002699 if (bigContentView != null) {
2700 parcel.writeInt(1);
2701 bigContentView.writeToParcel(parcel, 0);
2702 } else {
2703 parcel.writeInt(0);
2704 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002705
Chris Wren8fd39ec2014-02-27 17:43:26 -05002706 if (headsUpContentView != null) {
2707 parcel.writeInt(1);
2708 headsUpContentView.writeToParcel(parcel, 0);
2709 } else {
2710 parcel.writeInt(0);
2711 }
2712
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002713 parcel.writeInt(visibility);
2714
2715 if (publicVersion != null) {
2716 parcel.writeInt(1);
2717 publicVersion.writeToParcel(parcel, 0);
2718 } else {
2719 parcel.writeInt(0);
2720 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002721
2722 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002723
2724 if (mChannelId != null) {
2725 parcel.writeInt(1);
2726 parcel.writeString(mChannelId);
2727 } else {
2728 parcel.writeInt(0);
2729 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002730 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002731
2732 if (mShortcutId != null) {
2733 parcel.writeInt(1);
2734 parcel.writeString(mShortcutId);
2735 } else {
2736 parcel.writeInt(0);
2737 }
2738
Felipe Leme90205ef2019-03-05 09:59:52 -08002739 if (mLocusId != null) {
2740 parcel.writeInt(1);
2741 mLocusId.writeToParcel(parcel, 0);
2742 } else {
2743 parcel.writeInt(0);
2744 }
2745
Julia Reynolds13d898c2017-02-02 12:22:05 -05002746 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002747
2748 if (mSettingsText != null) {
2749 parcel.writeInt(1);
2750 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2751 } else {
2752 parcel.writeInt(0);
2753 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002754
2755 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002756
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002757 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002758 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002759 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002760 } else {
2761 parcel.writeInt(0);
2762 }
2763
Gustav Sennton761884c2018-11-19 17:40:19 +00002764 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2765
Julia Reynoldsfc640012018-02-21 12:25:27 -05002766 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 }
2768
2769 /**
2770 * Parcelable.Creator that instantiates Notification objects
2771 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002772 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 = new Parcelable.Creator<Notification>()
2774 {
2775 public Notification createFromParcel(Parcel parcel)
2776 {
2777 return new Notification(parcel);
2778 }
2779
2780 public Notification[] newArray(int size)
2781 {
2782 return new Notification[size];
2783 }
2784 };
2785
2786 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002787 * @hide
2788 */
2789 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2790 Notification.Action[] firstAs = first.actions;
2791 Notification.Action[] secondAs = second.actions;
2792 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2793 return true;
2794 }
2795 if (firstAs != null && secondAs != null) {
2796 if (firstAs.length != secondAs.length) {
2797 return true;
2798 }
2799 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002800 if (!Objects.equals(String.valueOf(firstAs[i].title),
2801 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002802 return true;
2803 }
2804 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2805 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2806 if (firstRs == null) {
2807 firstRs = new RemoteInput[0];
2808 }
2809 if (secondRs == null) {
2810 secondRs = new RemoteInput[0];
2811 }
2812 if (firstRs.length != secondRs.length) {
2813 return true;
2814 }
2815 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002816 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2817 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002818 return true;
2819 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002820 }
2821 }
2822 }
2823 return false;
2824 }
2825
2826 /**
2827 * @hide
2828 */
2829 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2830 if (first.getStyle() == null) {
2831 return second.getStyle() != null;
2832 }
2833 if (second.getStyle() == null) {
2834 return true;
2835 }
2836 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2837 }
2838
2839 /**
2840 * @hide
2841 */
2842 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002843 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2844 return true;
2845 }
2846
2847 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2848 return true;
2849 }
2850 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2851 return true;
2852 }
2853 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2854 return true;
2855 }
2856
2857 return false;
2858 }
2859
2860 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2861 if (first == null && second == null) {
2862 return false;
2863 }
2864 if (first == null && second != null || first != null && second == null) {
2865 return true;
2866 }
2867
2868 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2869 return true;
2870 }
2871
2872 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2873 return true;
2874 }
2875
2876 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002877 }
2878
2879 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002880 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2881 * <p>
2882 * For backwards compatibility {@code extras} holds some references to "real" member data such
2883 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2884 * fine as long as the object stays in one process.
2885 * <p>
2886 * However, once the notification goes into a parcel each reference gets marshalled separately,
2887 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2888 */
2889 private void fixDuplicateExtras() {
2890 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002891 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2892 }
2893 }
2894
2895 /**
2896 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2897 * separate object, replace it with the field's version to avoid holding duplicate copies.
2898 */
2899 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2900 if (original != null && extras.getParcelable(extraName) != null) {
2901 extras.putParcelable(extraName, original);
2902 }
2903 }
2904
2905 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2907 * layout.
2908 *
2909 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2910 * in the view.</p>
2911 * @param context The context for your application / activity.
2912 * @param contentTitle The title that goes in the expanded entry.
2913 * @param contentText The text that goes in the expanded entry.
2914 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2915 * If this is an activity, it must include the
2916 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002917 * that you take care of task management as described in the
2918 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2919 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002920 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002921 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002922 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002924 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 public void setLatestEventInfo(Context context,
2926 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002927 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2928 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2929 new Throwable());
2930 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002931
Selim Cinek4ac6f602016-06-13 15:47:03 -07002932 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2933 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2934 }
2935
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002936 // ensure that any information already set directly is preserved
2937 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002938
2939 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002941 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 }
2943 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002944 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002946 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002947
2948 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 }
2950
Julia Reynoldsda303542015-11-23 14:00:20 -05002951 /**
2952 * @hide
2953 */
2954 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002955 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002956 }
2957
2958 /**
2959 * @hide
2960 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002961 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002962 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002963 }
2964
Yi Jin6b514142017-10-30 14:54:12 -07002965 /**
2966 * @hide
2967 */
2968 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2969 long token = proto.start(fieldId);
2970 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2971 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2972 proto.write(NotificationProto.FLAGS, this.flags);
2973 proto.write(NotificationProto.COLOR, this.color);
2974 proto.write(NotificationProto.CATEGORY, this.category);
2975 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2976 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2977 if (this.actions != null) {
2978 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2979 }
2980 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2981 proto.write(NotificationProto.VISIBILITY, this.visibility);
2982 }
2983 if (publicVersion != null) {
2984 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2985 }
2986 proto.end(token);
2987 }
2988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 @Override
2990 public String toString() {
2991 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002992 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002993 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002994 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002995 sb.append(priority);
2996 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002997 if (contentView != null) {
2998 sb.append(contentView.getPackage());
2999 sb.append("/0x");
3000 sb.append(Integer.toHexString(contentView.getLayoutId()));
3001 } else {
3002 sb.append("null");
3003 }
3004 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003005 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
3006 sb.append("default");
3007 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 int N = this.vibrate.length-1;
3009 sb.append("[");
3010 for (int i=0; i<N; i++) {
3011 sb.append(this.vibrate[i]);
3012 sb.append(',');
3013 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02003014 if (N != -1) {
3015 sb.append(this.vibrate[N]);
3016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 } else {
3019 sb.append("null");
3020 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003021 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003022 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003024 } else if (this.sound != null) {
3025 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 } else {
3027 sb.append("null");
3028 }
Chris Wren365b6d32015-07-16 10:39:26 -04003029 if (this.tickerText != null) {
3030 sb.append(" tick");
3031 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003032 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003035 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003036 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003037 if (this.category != null) {
3038 sb.append(" category=");
3039 sb.append(this.category);
3040 }
3041 if (this.mGroupKey != null) {
3042 sb.append(" groupKey=");
3043 sb.append(this.mGroupKey);
3044 }
3045 if (this.mSortKey != null) {
3046 sb.append(" sortKey=");
3047 sb.append(this.mSortKey);
3048 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003049 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003050 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003051 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003052 }
3053 sb.append(" vis=");
3054 sb.append(visibilityToString(this.visibility));
3055 if (this.publicVersion != null) {
3056 sb.append(" publicVersion=");
3057 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003058 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003059 if (this.mLocusId != null) {
3060 sb.append(" locusId=");
3061 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3062 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003063 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 return sb.toString();
3065 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003066
Dan Sandler1b718782014-07-18 12:43:45 -04003067 /**
3068 * {@hide}
3069 */
3070 public static String visibilityToString(int vis) {
3071 switch (vis) {
3072 case VISIBILITY_PRIVATE:
3073 return "PRIVATE";
3074 case VISIBILITY_PUBLIC:
3075 return "PUBLIC";
3076 case VISIBILITY_SECRET:
3077 return "SECRET";
3078 default:
3079 return "UNKNOWN(" + String.valueOf(vis) + ")";
3080 }
3081 }
3082
Joe Onoratocb109a02011-01-18 17:57:41 -08003083 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003084 * {@hide}
3085 */
3086 public static String priorityToString(@Priority int pri) {
3087 switch (pri) {
3088 case PRIORITY_MIN:
3089 return "MIN";
3090 case PRIORITY_LOW:
3091 return "LOW";
3092 case PRIORITY_DEFAULT:
3093 return "DEFAULT";
3094 case PRIORITY_HIGH:
3095 return "HIGH";
3096 case PRIORITY_MAX:
3097 return "MAX";
3098 default:
3099 return "UNKNOWN(" + String.valueOf(pri) + ")";
3100 }
3101 }
3102
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003103 /**
3104 * @hide
3105 */
3106 public boolean hasCompletedProgress() {
3107 // not a progress notification; can't be complete
3108 if (!extras.containsKey(EXTRA_PROGRESS)
3109 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3110 return false;
3111 }
3112 // many apps use max 0 for 'indeterminate'; not complete
3113 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3114 return false;
3115 }
3116 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3117 }
3118
Jeff Sharkey000ce802017-04-29 13:13:27 -06003119 /** @removed */
3120 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003121 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003122 return mChannelId;
3123 }
3124
3125 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003126 * Returns the id of the channel this notification posts to.
3127 */
3128 public String getChannelId() {
3129 return mChannelId;
3130 }
3131
Jeff Sharkey000ce802017-04-29 13:13:27 -06003132 /** @removed */
3133 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003134 public long getTimeout() {
3135 return mTimeout;
3136 }
3137
3138 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003139 * Returns the duration from posting after which this notification should be canceled by the
3140 * system, if it's not canceled already.
3141 */
3142 public long getTimeoutAfter() {
3143 return mTimeout;
3144 }
3145
3146 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003147 * Returns what icon should be shown for this notification if it is being displayed in a
3148 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3149 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3150 */
3151 public int getBadgeIconType() {
3152 return mBadgeIcon;
3153 }
3154
3155 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003156 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003157 *
3158 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3159 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003160 */
3161 public String getShortcutId() {
3162 return mShortcutId;
3163 }
3164
Felipe Leme90205ef2019-03-05 09:59:52 -08003165 /**
3166 * Gets the {@link LocusId} associated with this notification.
3167 *
3168 * <p>Used by the device's intelligence services to correlate objects (such as
3169 * {@link ShortcutInfo} and {@link ContentCaptureContext}) that are correlated.
3170 */
3171 @Nullable
3172 public LocusId getLocusId() {
3173 return mLocusId;
3174 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003175
3176 /**
3177 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3178 */
3179 public CharSequence getSettingsText() {
3180 return mSettingsText;
3181 }
3182
Julia Reynolds13d898c2017-02-02 12:22:05 -05003183 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003184 * Returns which type of notifications in a group are responsible for audibly alerting the
3185 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3186 * {@link #GROUP_ALERT_SUMMARY}.
3187 */
3188 public @GroupAlertBehavior int getGroupAlertBehavior() {
3189 return mGroupAlertBehavior;
3190 }
3191
3192 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003193 * Returns the bubble metadata that will be used to display app content in a floating window
3194 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003195 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003196 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003197 public BubbleMetadata getBubbleMetadata() {
3198 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003199 }
3200
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003201 /**
3202 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3203 * for this notification.
3204 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003205 public boolean getAllowSystemGeneratedContextualActions() {
3206 return mAllowSystemGeneratedContextualActions;
3207 }
3208
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003209 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003210 * The small icon representing this notification in the status bar and content view.
3211 *
3212 * @return the small icon representing this notification.
3213 *
3214 * @see Builder#getSmallIcon()
3215 * @see Builder#setSmallIcon(Icon)
3216 */
3217 public Icon getSmallIcon() {
3218 return mSmallIcon;
3219 }
3220
3221 /**
3222 * Used when notifying to clean up legacy small icons.
3223 * @hide
3224 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003225 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003226 public void setSmallIcon(Icon icon) {
3227 mSmallIcon = icon;
3228 }
3229
3230 /**
3231 * The large icon shown in this notification's content view.
3232 * @see Builder#getLargeIcon()
3233 * @see Builder#setLargeIcon(Icon)
3234 */
3235 public Icon getLargeIcon() {
3236 return mLargeIcon;
3237 }
3238
3239 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003240 * @hide
3241 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003242 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003243 public boolean isGroupSummary() {
3244 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3245 }
3246
3247 /**
3248 * @hide
3249 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003250 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003251 public boolean isGroupChild() {
3252 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3253 }
3254
3255 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003256 * @hide
3257 */
3258 public boolean suppressAlertingDueToGrouping() {
3259 if (isGroupSummary()
3260 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3261 return true;
3262 } else if (isGroupChild()
3263 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3264 return true;
3265 }
3266 return false;
3267 }
3268
Tony Mak638430e2018-10-08 19:19:10 +01003269
3270 /**
3271 * Finds and returns a remote input and its corresponding action.
3272 *
3273 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3274 * @return the result pair, {@code null} if no result is found.
3275 *
3276 * @hide
3277 */
3278 @Nullable
3279 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3280 if (actions == null) {
3281 return null;
3282 }
3283 for (Notification.Action action : actions) {
3284 if (action.getRemoteInputs() == null) {
3285 continue;
3286 }
3287 RemoteInput resultRemoteInput = null;
3288 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3289 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3290 resultRemoteInput = remoteInput;
3291 }
3292 }
3293 if (resultRemoteInput != null) {
3294 return Pair.create(resultRemoteInput, action);
3295 }
3296 }
3297 return null;
3298 }
3299
Julia Reynolds30203152017-05-26 13:36:31 -04003300 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003301 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003302 *
3303 * @hide
3304 */
3305 public List<Notification.Action> getContextualActions() {
3306 if (actions == null) return Collections.emptyList();
3307
3308 List<Notification.Action> contextualActions = new ArrayList<>();
3309 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003310 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003311 contextualActions.add(action);
3312 }
3313 }
3314 return contextualActions;
3315 }
3316
3317 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003318 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003319 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003320 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003321 * content views using the platform's notification layout template. If your app supports
3322 * versions of Android as old as API level 4, you can instead use
3323 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3324 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3325 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003326 *
Scott Main183bf112012-08-13 19:12:13 -07003327 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003328 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003329 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003330 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003331 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3332 * .setContentText(subject)
3333 * .setSmallIcon(R.drawable.new_mail)
3334 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003335 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003336 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003337 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003338 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003339 /**
3340 * @hide
3341 */
3342 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3343 "android.rebuild.contentViewActionCount";
3344 /**
3345 * @hide
3346 */
3347 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3348 = "android.rebuild.bigViewActionCount";
3349 /**
3350 * @hide
3351 */
3352 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3353 = "android.rebuild.hudViewActionCount";
3354
Selim Cinek6743c0b2017-01-18 18:24:01 -08003355 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3356 SystemProperties.getBoolean("notifications.only_title", true);
3357
Selim Cinek389edcd2017-05-11 19:16:44 -07003358 /**
3359 * The lightness difference that has to be added to the primary text color to obtain the
3360 * secondary text color when the background is light.
3361 */
3362 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3363
3364 /**
3365 * The lightness difference that has to be added to the primary text color to obtain the
3366 * secondary text color when the background is dark.
3367 * A bit less then the above value, since it looks better on dark backgrounds.
3368 */
3369 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3370
Joe Onorato46439ce2010-11-19 13:56:21 -08003371 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003372 private Notification mN;
3373 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003374 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003375 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003376 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003377 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003378 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003379 private boolean mIsLegacy;
3380 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003381
3382 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003383 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3384 */
3385 private int mCachedContrastColor = COLOR_INVALID;
3386 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003387 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003388 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003389 */
3390 private int mCachedAmbientColor = COLOR_INVALID;
3391 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003392 /**
3393 * A neutral color color that can be used for icons.
3394 */
3395 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003396
3397 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003398 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3399 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3400 */
3401 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003402 private int mTextColorsAreForBackground = COLOR_INVALID;
3403 private int mPrimaryTextColor = COLOR_INVALID;
3404 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003405 private int mBackgroundColor = COLOR_INVALID;
3406 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003407 /**
3408 * A temporary location where actions are stored. If != null the view originally has action
3409 * but doesn't have any for this inflation.
3410 */
3411 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003412 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003413
Anthony Chenad4d1582017-04-10 16:07:58 -07003414 private boolean mTintActionButtons;
3415 private boolean mInNightMode;
3416
Adrian Roos70d7aa32017-01-11 15:39:06 -08003417 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003418 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003419 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003420 * @param context
3421 * A {@link Context} that will be used by the Builder to construct the
3422 * RemoteViews. The Context will not be held past the lifetime of this Builder
3423 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003424 * @param channelId
3425 * The constructed Notification will be posted on this
3426 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3427 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003428 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003429 public Builder(Context context, String channelId) {
3430 this(context, (Notification) null);
3431 mN.mChannelId = channelId;
3432 }
3433
3434 /**
3435 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3436 * instead. All posted Notifications must specify a NotificationChannel Id.
3437 */
3438 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003439 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003440 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003441 }
3442
Joe Onoratocb109a02011-01-18 17:57:41 -08003443 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003444 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003445 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003446 public Builder(Context context, Notification toAdopt) {
3447 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003448 Resources res = mContext.getResources();
3449 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3450
3451 if (res.getBoolean(R.bool.config_enableNightMode)) {
3452 Configuration currentConfig = res.getConfiguration();
3453 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3454 == Configuration.UI_MODE_NIGHT_YES;
3455 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003456
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003457 if (toAdopt == null) {
3458 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003459 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3460 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3461 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003462 mN.priority = PRIORITY_DEFAULT;
3463 mN.visibility = VISIBILITY_PRIVATE;
3464 } else {
3465 mN = toAdopt;
3466 if (mN.actions != null) {
3467 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003468 }
3469
Selim Cineke7238dd2017-12-14 17:48:32 -08003470 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3471 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3472 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003473 }
3474
Selim Cinek4ac6f602016-06-13 15:47:03 -07003475 if (mN.getSmallIcon() == null && mN.icon != 0) {
3476 setSmallIcon(mN.icon);
3477 }
3478
3479 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3480 setLargeIcon(mN.largeIcon);
3481 }
3482
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003483 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3484 if (!TextUtils.isEmpty(templateClass)) {
3485 final Class<? extends Style> styleClass
3486 = getNotificationStyleClass(templateClass);
3487 if (styleClass == null) {
3488 Log.d(TAG, "Unknown style class: " + templateClass);
3489 } else {
3490 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003491 final Constructor<? extends Style> ctor =
3492 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003493 ctor.setAccessible(true);
3494 final Style style = ctor.newInstance();
3495 style.restoreFromExtras(mN.extras);
3496
3497 if (style != null) {
3498 setStyle(style);
3499 }
3500 } catch (Throwable t) {
3501 Log.e(TAG, "Could not create Style", t);
3502 }
3503 }
3504 }
3505
3506 }
3507 }
3508
Lucas Dupina291d192018-06-07 13:59:42 -07003509 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003510 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003511 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003512 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003513 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003514 }
3515
3516 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003517 * If this notification is duplicative of a Launcher shortcut, sets the
3518 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3519 * the shortcut.
3520 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003521 * This field will be ignored by Launchers that don't support badging, don't show
3522 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003523 *
3524 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3525 * supersedes
3526 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003527 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003528 public Builder setShortcutId(String shortcutId) {
3529 mN.mShortcutId = shortcutId;
3530 return this;
3531 }
3532
3533 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003534 * Sets the {@link LocusId} associated with this notification.
3535 *
3536 * <p>This method should be called when the {@link LocusId} is used in other places (such
3537 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the device's intelligence
3538 * services can correlate them.
3539 */
3540 @NonNull
3541 public Builder setLocusId(@Nullable LocusId locusId) {
3542 mN.mLocusId = locusId;
3543 return this;
3544 }
3545
3546 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003547 * Sets which icon to display as a badge for this notification.
3548 *
3549 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3550 * {@link #BADGE_ICON_LARGE}.
3551 *
3552 * Note: This value might be ignored, for launchers that don't support badge icons.
3553 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003554 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003555 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003556 mN.mBadgeIcon = icon;
3557 return this;
3558 }
3559
3560 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003561 * Sets the group alert behavior for this notification. Use this method to mute this
3562 * notification if alerts for this notification's group should be handled by a different
3563 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003564 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3565 * mute. For example, if you want only the summary of your group to make noise, all
3566 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003567 *
3568 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3569 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003570 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003571 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3572 mN.mGroupAlertBehavior = groupAlertBehavior;
3573 return this;
3574 }
3575
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003576 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003577 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3578 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003579 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003580 * <p>This data will be ignored unless the notification is posted to a channel that
3581 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003582 *
Mady Mellor65dcaa92019-04-03 12:21:44 -07003583 * <p>Notifications allowed to bubble that have valid bubble metadata will display in
3584 * collapsed state outside of the notification shade on unlocked devices. When a user
3585 * interacts with the collapsed state, the bubble intent will be invoked and displayed.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003586 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003587 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003588 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003589 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003590 return this;
3591 }
3592
Jeff Sharkey000ce802017-04-29 13:13:27 -06003593 /** @removed */
3594 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003595 public Builder setChannel(String channelId) {
3596 mN.mChannelId = channelId;
3597 return this;
3598 }
3599
3600 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003601 * Specifies the channel the notification should be delivered on.
3602 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003603 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003604 public Builder setChannelId(String channelId) {
3605 mN.mChannelId = channelId;
3606 return this;
3607 }
3608
Jeff Sharkey000ce802017-04-29 13:13:27 -06003609 /** @removed */
3610 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003611 public Builder setTimeout(long durationMs) {
3612 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003613 return this;
3614 }
3615
3616 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003617 * Specifies a duration in milliseconds after which this notification should be canceled,
3618 * if it is not already canceled.
3619 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003620 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003621 public Builder setTimeoutAfter(long durationMs) {
3622 mN.mTimeout = durationMs;
3623 return this;
3624 }
3625
3626 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003627 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003628 *
3629 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3630 * shown anymore by default and must be opted into by using
3631 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003632 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003633 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003634 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003635 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003636 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003637 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003638 return this;
3639 }
3640
Joe Onoratocb109a02011-01-18 17:57:41 -08003641 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003642 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003643 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003644 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3645 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003646 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003647 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003648 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003649 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003650 return this;
3651 }
3652
3653 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003654 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003655 *
3656 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003657 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003658 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003659 * Useful when showing an elapsed time (like an ongoing phone call).
3660 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003661 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003662 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003663 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003664 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003665 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003666 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003667 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003668 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003669 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003670 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003671 return this;
3672 }
3673
3674 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003675 * Sets the Chronometer to count down instead of counting up.
3676 *
3677 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3678 * If it isn't set the chronometer will count up.
3679 *
3680 * @see #setUsesChronometer(boolean)
3681 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003682 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003683 public Builder setChronometerCountDown(boolean countDown) {
3684 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003685 return this;
3686 }
3687
3688 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003689 * Set the small icon resource, which will be used to represent the notification in the
3690 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003691 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003692
3693 * The platform template for the expanded view will draw this icon in the left, unless a
3694 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3695 * icon will be moved to the right-hand side.
3696 *
3697
3698 * @param icon
3699 * A resource ID in the application's package of the drawable to use.
3700 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003701 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003702 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003703 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003704 return setSmallIcon(icon != 0
3705 ? Icon.createWithResource(mContext, icon)
3706 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003707 }
3708
Joe Onoratocb109a02011-01-18 17:57:41 -08003709 /**
3710 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3711 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3712 * LevelListDrawable}.
3713 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003714 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003715 * @param level The level to use for the icon.
3716 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003717 * @see Notification#icon
3718 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003719 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003720 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003721 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003722 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003723 return setSmallIcon(icon);
3724 }
3725
3726 /**
3727 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003728 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003729 * {@link #setLargeIcon(Bitmap) large icon}).
3730 *
3731 * @param icon An Icon object to use.
3732 * @see Notification#icon
3733 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003734 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003735 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003736 mN.setSmallIcon(icon);
3737 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3738 mN.icon = icon.getResId();
3739 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003740 return this;
3741 }
3742
Joe Onoratocb109a02011-01-18 17:57:41 -08003743 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003744 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003745 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003746 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003747 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003748 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003749 return this;
3750 }
3751
Joe Onoratocb109a02011-01-18 17:57:41 -08003752 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003753 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003754 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003755 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003756 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003757 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003758 return this;
3759 }
3760
Joe Onoratocb109a02011-01-18 17:57:41 -08003761 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003762 * This provides some additional information that is displayed in the notification. No
3763 * guarantees are given where exactly it is displayed.
3764 *
3765 * <p>This information should only be provided if it provides an essential
3766 * benefit to the understanding of the notification. The more text you provide the
3767 * less readable it becomes. For example, an email client should only provide the account
3768 * name here if more than one email account has been added.</p>
3769 *
3770 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3771 * notification header area.
3772 *
3773 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3774 * this will be shown in the third line of text in the platform notification template.
3775 * You should not be using {@link #setProgress(int, int, boolean)} at the
3776 * same time on those versions; they occupy the same place.
3777 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003778 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003779 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003780 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003781 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003782 return this;
3783 }
3784
3785 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003786 * Provides text that will appear as a link to your application's settings.
3787 *
3788 * <p>This text does not appear within notification {@link Style templates} but may
3789 * appear when the user uses an affordance to learn more about the notification.
3790 * Additionally, this text will not appear unless you provide a valid link target by
3791 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3792 *
3793 * <p>This text is meant to be concise description about what the user can customize
3794 * when they click on this link. The recommended maximum length is 40 characters.
3795 * @param text
3796 * @return
3797 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003798 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003799 public Builder setSettingsText(CharSequence text) {
3800 mN.mSettingsText = safeCharSequence(text);
3801 return this;
3802 }
3803
3804 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003805 * Set the remote input history.
3806 *
3807 * This should be set to the most recent inputs that have been sent
3808 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3809 * longer relevant (e.g. for chat notifications once the other party has responded).
3810 *
3811 * The most recent input must be stored at the 0 index, the second most recent at the
3812 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3813 * and how much of each individual input is shown.
3814 *
3815 * <p>Note: The reply text will only be shown on notifications that have least one action
3816 * with a {@code RemoteInput}.</p>
3817 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003818 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003819 public Builder setRemoteInputHistory(CharSequence[] text) {
3820 if (text == null) {
3821 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3822 } else {
3823 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3824 CharSequence[] safe = new CharSequence[N];
3825 for (int i = 0; i < N; i++) {
3826 safe[i] = safeCharSequence(text[i]);
3827 }
3828 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3829 }
3830 return this;
3831 }
3832
3833 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003834 * Sets whether remote history entries view should have a spinner.
3835 * @hide
3836 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003837 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003838 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3839 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3840 return this;
3841 }
3842
3843 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003844 * Sets whether smart reply buttons should be hidden.
3845 * @hide
3846 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003847 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003848 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3849 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3850 return this;
3851 }
3852
3853 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003854 * Sets the number of items this notification represents. May be displayed as a badge count
3855 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003856 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003857 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003858 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003859 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003860 return this;
3861 }
3862
Joe Onoratocb109a02011-01-18 17:57:41 -08003863 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003864 * A small piece of additional information pertaining to this notification.
3865 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003866 * The platform template will draw this on the last line of the notification, at the far
3867 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003868 *
3869 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3870 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3871 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003872 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003873 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003874 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003875 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003876 return this;
3877 }
3878
Joe Onoratocb109a02011-01-18 17:57:41 -08003879 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 * Set the progress this notification represents.
3881 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003882 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003883 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003884 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003885 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003886 mN.extras.putInt(EXTRA_PROGRESS, progress);
3887 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3888 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003889 return this;
3890 }
3891
3892 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003893 * Supply a custom RemoteViews to use instead of the platform template.
3894 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003895 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003896 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003897 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003898 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003899 return setCustomContentView(views);
3900 }
3901
3902 /**
3903 * Supply custom RemoteViews to use instead of the platform template.
3904 *
3905 * This will override the layout that would otherwise be constructed by this Builder
3906 * object.
3907 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003908 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003909 public Builder setCustomContentView(RemoteViews contentView) {
3910 mN.contentView = contentView;
3911 return this;
3912 }
3913
3914 /**
3915 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3916 *
3917 * This will override the expanded layout that would otherwise be constructed by this
3918 * Builder object.
3919 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003920 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003921 public Builder setCustomBigContentView(RemoteViews contentView) {
3922 mN.bigContentView = contentView;
3923 return this;
3924 }
3925
3926 /**
3927 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3928 *
3929 * This will override the heads-up layout that would otherwise be constructed by this
3930 * Builder object.
3931 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003932 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003933 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3934 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003935 return this;
3936 }
3937
Joe Onoratocb109a02011-01-18 17:57:41 -08003938 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003939 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3940 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003941 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3942 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3943 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003944 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003945 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003946 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003947 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003948 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003949 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003950 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003951 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003952 return this;
3953 }
3954
Joe Onoratocb109a02011-01-18 17:57:41 -08003955 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003956 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3957 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003958 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003959 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003960 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003961 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003962 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003963 return this;
3964 }
3965
Joe Onoratocb109a02011-01-18 17:57:41 -08003966 /**
3967 * An intent to launch instead of posting the notification to the status bar.
3968 * Only for use with extremely high-priority notifications demanding the user's
3969 * <strong>immediate</strong> attention, such as an incoming phone call or
3970 * alarm clock that the user has explicitly set to a particular time.
3971 * If this facility is used for something else, please give the user an option
3972 * to turn it off and use a normal notification, as this can be extremely
3973 * disruptive.
3974 *
Chris Wren47c20a12014-06-18 17:27:29 -04003975 * <p>
3976 * The system UI may choose to display a heads-up notification, instead of
3977 * launching this intent, while the user is using the device.
3978 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003979 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3980 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3981 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003982 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003983 * @param intent The pending intent to launch.
3984 * @param highPriority Passing true will cause this notification to be sent
3985 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003986 *
3987 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003988 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003989 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003990 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003991 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003992 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3993 return this;
3994 }
3995
Joe Onoratocb109a02011-01-18 17:57:41 -08003996 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003997 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003998 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003999 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08004000 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004001 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004002 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004003 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08004004 return this;
4005 }
4006
Joe Onoratocb109a02011-01-18 17:57:41 -08004007 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004008 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004009 *
Joe Onoratocb109a02011-01-18 17:57:41 -08004010 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004011 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004012 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004013 setTicker(tickerText);
4014 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004015 return this;
4016 }
4017
Joe Onoratocb109a02011-01-18 17:57:41 -08004018 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004019 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004020 *
4021 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004022 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4023 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004024 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004025 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004026 public Builder setLargeIcon(Bitmap b) {
4027 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4028 }
4029
4030 /**
4031 * Add a large icon to the notification content view.
4032 *
4033 * In the platform template, this image will be shown on the left of the notification view
4034 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4035 * badge atop the large icon).
4036 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004037 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004038 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004039 mN.mLargeIcon = icon;
4040 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004041 return this;
4042 }
4043
Joe Onoratocb109a02011-01-18 17:57:41 -08004044 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004045 * Set the sound to play.
4046 *
John Spurlockc0650f022014-07-19 13:22:39 -04004047 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4048 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004049 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004050 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004051 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004052 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004053 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004054 mN.sound = sound;
4055 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004056 return this;
4057 }
4058
Joe Onoratocb109a02011-01-18 17:57:41 -08004059 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004060 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004061 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004062 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4063 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004064 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004065 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004066 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004067 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004068 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004069 mN.sound = sound;
4070 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004071 return this;
4072 }
4073
Joe Onoratocb109a02011-01-18 17:57:41 -08004074 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004075 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4076 * use during playback.
4077 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004078 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004079 * @see Notification#sound
4080 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004081 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004082 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004083 mN.sound = sound;
4084 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004085 return this;
4086 }
4087
4088 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004089 * Set the vibration pattern to use.
4090 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004091 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4092 * <code>pattern</code> parameter.
4093 *
Chris Wren47c20a12014-06-18 17:27:29 -04004094 * <p>
4095 * A notification that vibrates is more likely to be presented as a heads-up notification.
4096 * </p>
4097 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004098 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004099 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004100 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004101 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004102 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004103 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004104 return this;
4105 }
4106
Joe Onoratocb109a02011-01-18 17:57:41 -08004107 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004108 * Set the desired color for the indicator LED on the device, as well as the
4109 * blink duty cycle (specified in milliseconds).
4110 *
4111
4112 * Not all devices will honor all (or even any) of these values.
4113 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004114 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004115 * @see Notification#ledARGB
4116 * @see Notification#ledOnMS
4117 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004118 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004119 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004120 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004121 mN.ledARGB = argb;
4122 mN.ledOnMS = onMs;
4123 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004124 if (onMs != 0 || offMs != 0) {
4125 mN.flags |= FLAG_SHOW_LIGHTS;
4126 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004127 return this;
4128 }
4129
Joe Onoratocb109a02011-01-18 17:57:41 -08004130 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004131 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004132 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004133
4134 * Ongoing notifications cannot be dismissed by the user, so your application or service
4135 * must take care of canceling them.
4136 *
4137
4138 * They are typically used to indicate a background task that the user is actively engaged
4139 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4140 * (e.g., a file download, sync operation, active network connection).
4141 *
4142
4143 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004144 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004145 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004146 public Builder setOngoing(boolean ongoing) {
4147 setFlag(FLAG_ONGOING_EVENT, ongoing);
4148 return this;
4149 }
4150
Joe Onoratocb109a02011-01-18 17:57:41 -08004151 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004152 * Set whether this notification should be colorized. When set, the color set with
4153 * {@link #setColor(int)} will be used as the background color of this notification.
4154 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004155 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4156 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004157 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004158 * For most styles, the coloring will only be applied if the notification is for a
4159 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004160 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004161 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004162 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004163 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004164 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004165 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004166 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004167 public Builder setColorized(boolean colorize) {
4168 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4169 return this;
4170 }
4171
4172 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004173 * Set this flag if you would only like the sound, vibrate
4174 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004175 *
4176 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004177 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004178 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004179 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4180 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4181 return this;
4182 }
4183
Joe Onoratocb109a02011-01-18 17:57:41 -08004184 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004185 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004186 *
4187 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004188 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004189 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004190 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004191 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004192 return this;
4193 }
4194
Joe Onoratocb109a02011-01-18 17:57:41 -08004195 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004196 * Set whether or not this notification should not bridge to other devices.
4197 *
4198 * <p>Some notifications can be bridged to other devices for remote display.
4199 * This hint can be set to recommend this notification not be bridged.
4200 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004201 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004202 public Builder setLocalOnly(boolean localOnly) {
4203 setFlag(FLAG_LOCAL_ONLY, localOnly);
4204 return this;
4205 }
4206
4207 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004208 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004209 * <p>
4210 * The value should be one or more of the following fields combined with
4211 * bitwise-or:
4212 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4213 * <p>
4214 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004215 *
4216 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004217 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004218 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004219 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004220 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004221 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004222 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004223 return this;
4224 }
4225
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004226 /**
4227 * Set the priority of this notification.
4228 *
4229 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004230 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004231 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004232 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004233 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004234 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004235 return this;
4236 }
Joe Malin8d40d042012-11-05 11:36:40 -08004237
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004238 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004239 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004240 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004241 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004242 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004243 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004244 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004245 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004246 return this;
4247 }
4248
4249 /**
Chris Wrendde75302014-03-26 17:24:15 -04004250 * Add a person that is relevant to this notification.
4251 *
Chris Wrene6c48932014-09-29 17:19:27 -04004252 * <P>
4253 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004254 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4255 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4256 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004257 * </P>
4258 *
4259 * <P>
4260 * The person should be specified by the {@code String} representation of a
4261 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4262 * </P>
4263 *
4264 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4265 * URIs. The path part of these URIs must exist in the contacts database, in the
4266 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4267 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004268 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4269 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004270 * </P>
4271 *
4272 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004273 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004274 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004275 */
Chris Wrene6c48932014-09-29 17:19:27 -04004276 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004277 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004278 return this;
4279 }
4280
4281 /**
4282 * Add a person that is relevant to this notification.
4283 *
4284 * <P>
4285 * Depending on user preferences, this annotation may allow the notification to pass
4286 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4287 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4288 * appear more prominently in the user interface.
4289 * </P>
4290 *
4291 * <P>
4292 * A person should usually contain a uri in order to benefit from the ranking boost.
4293 * However, even if no uri is provided, it's beneficial to provide other people in the
4294 * notification, such that listeners and voice only devices can announce and handle them
4295 * properly.
4296 * </P>
4297 *
4298 * @param person the person to add.
4299 * @see Notification#EXTRA_PEOPLE_LIST
4300 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004301 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004302 public Builder addPerson(Person person) {
4303 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004304 return this;
4305 }
4306
4307 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004308 * Set this notification to be part of a group of notifications sharing the same key.
4309 * Grouped notifications may display in a cluster or stack on devices which
4310 * support such rendering.
4311 *
4312 * <p>To make this notification the summary for its group, also call
4313 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4314 * {@link #setSortKey}.
4315 * @param groupKey The group key of the group.
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 setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004320 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004321 return this;
4322 }
4323
4324 /**
4325 * Set this notification to be the group summary for a group of notifications.
4326 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004327 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4328 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004329 * @param isGroupSummary Whether this notification should be a group summary.
4330 * @return this object for method chaining
4331 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004332 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004333 public Builder setGroupSummary(boolean isGroupSummary) {
4334 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4335 return this;
4336 }
4337
4338 /**
4339 * Set a sort key that orders this notification among other notifications from the
4340 * same package. This can be useful if an external sort was already applied and an app
4341 * would like to preserve this. Notifications will be sorted lexicographically using this
4342 * value, although providing different priorities in addition to providing sort key may
4343 * cause this value to be ignored.
4344 *
4345 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004346 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004347 *
4348 * @see String#compareTo(String)
4349 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004350 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004351 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004352 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004353 return this;
4354 }
4355
4356 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004357 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004358 *
Griff Hazen720042b2014-02-24 15:46:56 -08004359 * <p>Values within the Bundle will replace existing extras values in this Builder.
4360 *
4361 * @see Notification#extras
4362 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004363 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004364 public Builder addExtras(Bundle extras) {
4365 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004366 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004367 }
4368 return this;
4369 }
4370
4371 /**
4372 * Set metadata for this notification.
4373 *
4374 * <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 -04004375 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004376 * called.
4377 *
Griff Hazen720042b2014-02-24 15:46:56 -08004378 * <p>Replaces any existing extras values with those from the provided Bundle.
4379 * Use {@link #addExtras} to merge in metadata instead.
4380 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004381 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004382 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004383 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004384 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004385 if (extras != null) {
4386 mUserExtras = extras;
4387 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004388 return this;
4389 }
4390
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004391 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004392 * Get the current metadata Bundle used by this notification Builder.
4393 *
4394 * <p>The returned Bundle is shared with this Builder.
4395 *
4396 * <p>The current contents of this Bundle are copied into the Notification each time
4397 * {@link #build()} is called.
4398 *
4399 * @see Notification#extras
4400 */
4401 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004402 return mUserExtras;
4403 }
4404
4405 private Bundle getAllExtras() {
4406 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4407 saveExtras.putAll(mN.extras);
4408 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004409 }
4410
4411 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004412 * Add an action to this notification. Actions are typically displayed by
4413 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004414 * <p>
4415 * Every action must have an icon (32dp square and matching the
4416 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4417 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4418 * <p>
4419 * A notification in its expanded form can display up to 3 actions, from left to right in
4420 * the order they were added. Actions will not be displayed when the notification is
4421 * collapsed, however, so be sure that any essential functions may be accessed by the user
4422 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004423 *
4424 * @param icon Resource ID of a drawable that represents the action.
4425 * @param title Text describing the action.
4426 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004427 *
4428 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004429 */
Dan Sandler86647982015-05-13 23:41:13 -04004430 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004431 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004432 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004433 return this;
4434 }
4435
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004436 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004437 * Add an action to this notification. Actions are typically displayed by
4438 * the system as a button adjacent to the notification content.
4439 * <p>
4440 * Every action must have an icon (32dp square and matching the
4441 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4442 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4443 * <p>
4444 * A notification in its expanded form can display up to 3 actions, from left to right in
4445 * the order they were added. Actions will not be displayed when the notification is
4446 * collapsed, however, so be sure that any essential functions may be accessed by the user
4447 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4448 *
4449 * @param action The action to add.
4450 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004451 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004452 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004453 if (action != null) {
4454 mActions.add(action);
4455 }
Griff Hazen959591e2014-05-15 22:26:18 -07004456 return this;
4457 }
4458
4459 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004460 * Alter the complete list of actions attached to this notification.
4461 * @see #addAction(Action).
4462 *
4463 * @param actions
4464 * @return
4465 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004466 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004467 public Builder setActions(Action... actions) {
4468 mActions.clear();
4469 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004470 if (actions[i] != null) {
4471 mActions.add(actions[i]);
4472 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004473 }
4474 return this;
4475 }
4476
4477 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004478 * Add a rich notification style to be applied at build time.
4479 *
4480 * @param style Object responsible for modifying the notification style.
4481 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004482 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004483 public Builder setStyle(Style style) {
4484 if (mStyle != style) {
4485 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004486 if (mStyle != null) {
4487 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004488 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4489 } else {
4490 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004491 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004492 }
4493 return this;
4494 }
4495
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004496 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004497 * Returns the style set by {@link #setStyle(Style)}.
4498 */
4499 public Style getStyle() {
4500 return mStyle;
4501 }
4502
4503 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004504 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004505 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004506 * @return The same Builder.
4507 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004508 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004509 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004510 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004511 return this;
4512 }
4513
4514 /**
4515 * Supply a replacement Notification whose contents should be shown in insecure contexts
4516 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4517 * @param n A replacement notification, presumably with some or all info redacted.
4518 * @return The same Builder.
4519 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004520 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004521 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004522 if (n != null) {
4523 mN.publicVersion = new Notification();
4524 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4525 } else {
4526 mN.publicVersion = null;
4527 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004528 return this;
4529 }
4530
Griff Hazenb720abe2014-05-20 13:15:30 -07004531 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004532 * Apply an extender to this notification builder. Extenders may be used to add
4533 * metadata or change options on this builder.
4534 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004535 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004536 public Builder extend(Extender extender) {
4537 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004538 return this;
4539 }
4540
Dan Sandler4e787062015-06-17 15:09:48 -04004541 /**
4542 * @hide
4543 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004544 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004545 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004546 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004547 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004548 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004549 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004550 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004551 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004552 }
4553
Dan Sandler26e81cf2014-05-06 10:01:27 -04004554 /**
4555 * Sets {@link Notification#color}.
4556 *
4557 * @param argb The accent color to use
4558 *
4559 * @return The same Builder.
4560 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004561 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004562 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004563 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004564 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004565 return this;
4566 }
4567
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004568 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004569 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4570 // This user can never be a badged profile,
4571 // and also includes USER_ALL system notifications.
4572 return null;
4573 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004574 // Note: This assumes that the current user can read the profile badge of the
4575 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004576 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004577 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004578 }
4579
4580 private Bitmap getProfileBadge() {
4581 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004582 if (badge == null) {
4583 return null;
4584 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004585 final int size = mContext.getResources().getDimensionPixelSize(
4586 R.dimen.notification_badge_size);
4587 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004588 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004589 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004590 badge.draw(canvas);
4591 return bitmap;
4592 }
4593
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004594 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004595 Bitmap profileBadge = getProfileBadge();
4596
Kenny Guy98193ea2014-07-24 19:54:37 +01004597 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004598 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4599 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004600 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004601 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004602 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004603 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004604 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004605 }
4606
Gus Prevasbae5ba32018-11-28 15:45:13 -05004607 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4608 contentView.setDrawableTint(
4609 R.id.alerted_icon,
4610 false /* targetBackground */,
4611 getNeutralColor(p),
4612 PorterDuff.Mode.SRC_ATOP);
4613 }
4614
Julia Reynoldsfc640012018-02-21 12:25:27 -05004615 /**
4616 * @hide
4617 */
4618 public boolean usesStandardHeader() {
4619 if (mN.mUsesStandardHeader) {
4620 return true;
4621 }
4622 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4623 if (mN.contentView == null && mN.bigContentView == null) {
4624 return true;
4625 }
4626 }
4627 boolean contentViewUsesHeader = mN.contentView == null
4628 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4629 boolean bigContentViewUsesHeader = mN.bigContentView == null
4630 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4631 return contentViewUsesHeader && bigContentViewUsesHeader;
4632 }
4633
Christoph Studerfe718432014-09-01 18:21:18 +02004634 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004635 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004636 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004637 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004638 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004639 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004640 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004641 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004642 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004643 }
4644
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004645 /**
4646 * Resets the notification header to its original state
4647 */
4648 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004649 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4650 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004651 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004652 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004653 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004654 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004655 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004656 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4657 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004658 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004659 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004660 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004661 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004662 contentView.setImageViewIcon(R.id.profile_badge, null);
4663 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004664 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004665 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004666 }
4667
Selim Cinek384804b2018-04-18 14:31:07 +08004668 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4669 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4670 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004671 }
4672
Selim Cinek384804b2018-04-18 14:31:07 +08004673 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4674 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004675 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004676
Christoph Studerfe718432014-09-01 18:21:18 +02004677 resetStandardTemplate(contentView);
4678
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004679 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004680 updateBackgroundColor(contentView, p);
4681 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004682 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004683 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004684 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004685 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004686 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004687 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004688 contentView.setViewLayoutWidth(R.id.title, showProgress
4689 ? ViewGroup.LayoutParams.WRAP_CONTENT
4690 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004691 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004692 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004693 int textId = showProgress ? com.android.internal.R.id.text_line_1
4694 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004695 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004696 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004697 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004698 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004699
Selim Cinek279fa862016-06-14 10:57:25 -07004700 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004701
Selim Cinek29603462015-11-17 19:04:39 -08004702 return contentView;
4703 }
4704
Selim Cinek48f66b72017-08-18 16:17:51 -07004705 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004706 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004707 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004708 }
4709 return text;
4710 }
4711
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004712 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4713 StandardTemplateParams p) {
4714 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004715 contentView.setTextColor(id, mPrimaryTextColor);
4716 }
4717
Selim Cinek48f66b72017-08-18 16:17:51 -07004718 private boolean hasForegroundColor() {
4719 return mForegroundColor != COLOR_INVALID;
4720 }
4721
Selim Cinek389edcd2017-05-11 19:16:44 -07004722 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004723 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004724 * @hide
4725 */
4726 @VisibleForTesting
4727 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004728 return getPrimaryTextColor(mParams);
4729 }
4730
4731 /**
4732 * @param p the template params to inflate this with
4733 * @return the primary text color
4734 * @hide
4735 */
4736 @VisibleForTesting
4737 public int getPrimaryTextColor(StandardTemplateParams p) {
4738 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004739 return mPrimaryTextColor;
4740 }
4741
Selim Cinek389edcd2017-05-11 19:16:44 -07004742 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004743 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004744 * @hide
4745 */
4746 @VisibleForTesting
4747 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004748 return getSecondaryTextColor(mParams);
4749 }
4750
4751 /**
4752 * @param p the template params to inflate this with
4753 * @return the secondary text color
4754 * @hide
4755 */
4756 @VisibleForTesting
4757 public int getSecondaryTextColor(StandardTemplateParams p) {
4758 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004759 return mSecondaryTextColor;
4760 }
4761
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004762 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4763 StandardTemplateParams p) {
4764 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004765 contentView.setTextColor(id, mSecondaryTextColor);
4766 }
4767
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004768 private void ensureColors(StandardTemplateParams p) {
4769 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004770 if (mPrimaryTextColor == COLOR_INVALID
4771 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004772 || mTextColorsAreForBackground != backgroundColor) {
4773 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004774 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004775 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004776 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004777 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004778 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004779 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004780 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004781 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004782 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004783 mSecondaryTextColor, backgroundColor, 4.5);
4784 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004785 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004786 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4787 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4788 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004789 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004790 // We only respect the given colors if worst case Black or White still has
4791 // contrast
4792 boolean backgroundLight = backLum > textLum
4793 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4794 || backLum <= textLum
4795 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004796 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004797 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004798 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004799 mForegroundColor,
4800 backgroundColor,
4801 true /* findFG */,
4802 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004803 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004804 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004805 } else {
4806 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004807 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004808 mForegroundColor,
4809 backgroundColor,
4810 true /* findFG */,
4811 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004812 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004813 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004814 }
4815 } else {
4816 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004817 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004818 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4819 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004820 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004821 backgroundColor) < 4.5f) {
4822 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004823 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004824 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004825 mSecondaryTextColor,
4826 backgroundColor,
4827 true /* findFG */,
4828 4.5f);
4829 } else {
4830 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004831 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004832 mSecondaryTextColor,
4833 backgroundColor,
4834 true /* findFG */,
4835 4.5f);
4836 }
Lucas Dupina291d192018-06-07 13:59:42 -07004837 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004838 mSecondaryTextColor, backgroundLight
4839 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4840 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004841 }
4842 }
4843 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004844 }
4845 }
4846
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004847 private void updateBackgroundColor(RemoteViews contentView,
4848 StandardTemplateParams p) {
4849 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004850 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004851 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004852 } else {
4853 // Clear it!
4854 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4855 0);
4856 }
4857 }
4858
Selim Cinek860b6da2015-12-16 19:02:19 -08004859 /**
4860 * @param remoteView the remote view to update the minheight in
4861 * @param hasMinHeight does it have a mimHeight
4862 * @hide
4863 */
4864 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4865 int minHeight = 0;
4866 if (hasMinHeight) {
4867 // we need to set the minHeight of the notification
4868 minHeight = mContext.getResources().getDimensionPixelSize(
4869 com.android.internal.R.dimen.notification_min_content_height);
4870 }
4871 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4872 }
4873
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004874 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4875 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004876 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4877 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4878 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004879 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004880 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004881 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004882 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004883 contentView.setProgressBackgroundTintList(
4884 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4885 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004886 if (getRawColor(p) != COLOR_DEFAULT) {
4887 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004888 contentView.setProgressTintList(R.id.progress, colorStateList);
4889 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004890 }
Selim Cinek29603462015-11-17 19:04:39 -08004891 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004892 } else {
4893 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004894 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004895 }
Joe Onorato561d3852010-11-20 18:09:34 -08004896 }
4897
Selim Cinek384804b2018-04-18 14:31:07 +08004898 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4899 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004900 boolean largeIconShown = bindLargeIcon(contentView, p);
4901 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004902 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004903 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004904 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004905 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4906 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4907 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4908 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004909 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004910 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004911 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004912 }
4913 }
4914
Selim Cinek1c72fa02018-04-23 18:00:54 +08004915 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4916 int marginEnd = 0;
4917 int contentMargin = mContext.getResources().getDimensionPixelSize(
4918 R.dimen.notification_content_margin_end);
4919 int iconSize = mContext.getResources().getDimensionPixelSize(
4920 R.dimen.notification_right_icon_size);
4921 if (replyIconShown) {
4922 // The size of the reply icon
4923 marginEnd += iconSize;
4924
4925 int replyInset = mContext.getResources().getDimensionPixelSize(
4926 R.dimen.notification_reply_inset);
4927 // We're subtracting the inset of the reply icon to make sure it's
4928 // aligned nicely on the right, and remove it from the following padding
4929 marginEnd -= replyInset * 2;
4930 }
4931 if (largeIconShown) {
4932 // adding size of the right icon
4933 marginEnd += iconSize;
4934
4935 if (replyIconShown) {
4936 // We also add some padding to the reply icon if it's around
4937 marginEnd += contentMargin;
4938 }
4939 }
4940 if (replyIconShown || largeIconShown) {
4941 // The padding to the content
4942 marginEnd += contentMargin;
4943 }
4944 return marginEnd;
4945 }
4946
Selim Cinek384804b2018-04-18 14:31:07 +08004947 /**
4948 * Bind the large icon.
4949 * @return if the largeIcon is visible
4950 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004951 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004952 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4953 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4954 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004955 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004956 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004957 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4958 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004959 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004960 }
Selim Cinek384804b2018-04-18 14:31:07 +08004961 return showLargeIcon;
4962 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004963
Selim Cinek384804b2018-04-18 14:31:07 +08004964 /**
4965 * Bind the reply icon.
4966 * @return if the reply icon is visible
4967 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004968 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004969 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004970 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004971 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004972 action = findReplyAction();
4973 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004974 }
Selim Cinek384804b2018-04-18 14:31:07 +08004975 if (actionVisible) {
4976 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4977 contentView.setDrawableTint(R.id.reply_icon_action,
4978 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004979 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004980 PorterDuff.Mode.SRC_ATOP);
4981 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4982 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4983 } else {
4984 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4985 }
4986 contentView.setViewVisibility(R.id.reply_icon_action,
4987 actionVisible ? View.VISIBLE : View.GONE);
4988 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004989 }
4990
4991 private Action findReplyAction() {
4992 ArrayList<Action> actions = mActions;
4993 if (mOriginalActions != null) {
4994 actions = mOriginalActions;
4995 }
4996 int numActions = actions.size();
4997 for (int i = 0; i < numActions; i++) {
4998 Action action = actions.get(i);
4999 if (hasValidRemoteInput(action)) {
5000 return action;
5001 }
5002 }
5003 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005004 }
5005
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005006 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
5007 bindSmallIcon(contentView, p);
5008 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005009 bindHeaderText(contentView, p);
5010 bindHeaderTextSecondary(contentView, p);
5011 bindHeaderChronometerAndTime(contentView, p);
5012 bindProfileBadge(contentView, p);
5013 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005014 bindActivePermissions(contentView, p);
5015 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005016 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005017 }
5018
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005019 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005020 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005021 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5022 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5023 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5024 }
5025
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005026 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5027 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005028 contentView.setDrawableTint(R.id.expand_button, false, color,
5029 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005030 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005031 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005032 }
5033
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005034 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5035 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005036 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005037 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005038 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005039 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5040 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5041 contentView.setLong(R.id.chronometer, "setBase",
5042 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5043 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005044 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005045 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005046 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005047 } else {
5048 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5049 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005050 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005051 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005052 } else {
5053 // We still want a time to be set but gone, such that we can show and hide it
5054 // on demand in case it's a child notification without anything in the header
5055 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005056 }
5057 }
5058
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005059 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5060 CharSequence summaryText = p.summaryText;
5061 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005062 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005063 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005064 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005065 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005066 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5067 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005068 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005069 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005070 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005071 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005072 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005073 processLegacyText(summaryText)));
5074 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005075 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5076 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005077 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005078 }
5079 }
5080
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005081 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5082 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005083 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005084 processLegacyText(p.headerTextSecondary)));
5085 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005086 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5087 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005088 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005089 }
5090 }
5091
Adrian Rooseba05822016-04-22 17:09:27 -07005092 /**
5093 * @hide
5094 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005095 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005096 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005097 CharSequence name = null;
5098 final PackageManager pm = mContext.getPackageManager();
5099 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5100 // only system packages which lump together a bunch of unrelated stuff
5101 // may substitute a different name to make the purpose of the
5102 // notification more clear. the correct package label should always
5103 // be accessible via SystemUI.
5104 final String pkg = mContext.getPackageName();
5105 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5106 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5107 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5108 name = subName;
5109 } else {
5110 Log.w(TAG, "warning: pkg "
5111 + pkg + " attempting to substitute app name '" + subName
5112 + "' without holding perm "
5113 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5114 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005115 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005116 if (TextUtils.isEmpty(name)) {
5117 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5118 }
5119 if (TextUtils.isEmpty(name)) {
5120 // still nothing?
5121 return null;
5122 }
5123
5124 return String.valueOf(name);
5125 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005126 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005127 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005128 if (isColorized(p)) {
5129 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005130 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005131 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005132 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005133 }
5134
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005135 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005136 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005137 }
5138
5139 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005140 if (mN.mSmallIcon == null && mN.icon != 0) {
5141 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5142 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005143 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005144 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005145 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005146 }
5147
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005148 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005149 * @return true if the built notification will show the time or the chronometer; false
5150 * otherwise
5151 */
5152 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005153 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005154 }
5155
Christoph Studerfe718432014-09-01 18:21:18 +02005156 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005157 // actions_container is only reset when there are no actions to avoid focus issues with
5158 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005159 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005160 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005161
5162 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5163 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005164 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5165 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005166
5167 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5168 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5169 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5170 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005171
Selim Cineked64a142018-02-06 18:06:01 -08005172 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5173 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005174 }
5175
Selim Cinek384804b2018-04-18 14:31:07 +08005176 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5177 TemplateBindResult result) {
5178 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5179 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005180 }
5181
Gustav Sennton1463d832018-11-06 16:12:48 +00005182 private static List<Notification.Action> filterOutContextualActions(
5183 List<Notification.Action> actions) {
5184 List<Notification.Action> nonContextualActions = new ArrayList<>();
5185 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005186 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005187 nonContextualActions.add(action);
5188 }
5189 }
5190 return nonContextualActions;
5191 }
5192
Adrian Roos70d7aa32017-01-11 15:39:06 -08005193 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005194 StandardTemplateParams p, TemplateBindResult result) {
5195 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005196
Christoph Studerfe718432014-09-01 18:21:18 +02005197 resetStandardTemplateWithActions(big);
5198
Adrian Roose458aa82015-12-08 16:17:19 -08005199 boolean validRemoteInput = false;
5200
Gustav Sennton1463d832018-11-06 16:12:48 +00005201 // In the UI contextual actions appear separately from the standard actions, so we
5202 // filter them out here.
5203 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5204
5205 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005206 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005207 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005208 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005209 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005210 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005211 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005212 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005213 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005214 Action action = nonContextualActions.get(i);
5215
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005216 boolean actionHasValidInput = hasValidRemoteInput(action);
5217 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005218
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005219 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005220 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005221 // Clear the drawable
5222 button.setInt(R.id.action0, "setBackgroundResource", 0);
5223 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005224 big.addView(R.id.actions, button);
5225 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005226 } else {
5227 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005228 }
Adrian Roose458aa82015-12-08 16:17:19 -08005229
5230 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005231 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005232 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5233 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005234 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005235 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005236 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5237 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005238 big.setTextViewText(R.id.notification_material_reply_text_1,
5239 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005240 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005241 big.setViewVisibility(R.id.notification_material_reply_progress,
5242 showSpinner ? View.VISIBLE : View.GONE);
5243 big.setProgressIndeterminateTintList(
5244 R.id.notification_material_reply_progress,
5245 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005246 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005247
Selim Cinekbee4e072018-05-21 22:06:43 -07005248 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5249 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005250 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005251 big.setTextViewText(R.id.notification_material_reply_text_2,
5252 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005253 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005254
Selim Cinekbee4e072018-05-21 22:06:43 -07005255 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5256 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005257 big.setViewVisibility(
5258 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005259 big.setTextViewText(R.id.notification_material_reply_text_3,
5260 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005261 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005262 }
5263 }
5264 }
5265
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005266 return big;
5267 }
5268
Adrian Roose458aa82015-12-08 16:17:19 -08005269 private boolean hasValidRemoteInput(Action action) {
5270 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5271 // Weird actions
5272 return false;
5273 }
5274
5275 RemoteInput[] remoteInputs = action.getRemoteInputs();
5276 if (remoteInputs == null) {
5277 return false;
5278 }
5279
5280 for (RemoteInput r : remoteInputs) {
5281 CharSequence[] choices = r.getChoices();
5282 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5283 return true;
5284 }
5285 }
5286 return false;
5287 }
5288
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005289 /**
5290 * Construct a RemoteViews for the final 1U notification layout. In order:
5291 * 1. Custom contentView from the caller
5292 * 2. Style's proposed content view
5293 * 3. Standard template view
5294 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005295 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005296 return createContentView(false /* increasedheight */ );
5297 }
5298
5299 /**
5300 * Construct a RemoteViews for the smaller content view.
5301 *
5302 * @param increasedHeight true if this layout be created with an increased height. Some
5303 * styles may support showing more then just that basic 1U size
5304 * and the system may decide to render important notifications
5305 * slightly bigger even when collapsed.
5306 *
5307 * @hide
5308 */
5309 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005310 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005311 return mN.contentView;
5312 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005313 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005314 if (styleView != null) {
5315 return styleView;
5316 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005317 }
Selim Cinek384804b2018-04-18 14:31:07 +08005318 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005319 }
5320
Selim Cineka7679b62017-05-10 16:33:25 -07005321 private boolean useExistingRemoteView() {
5322 return mStyle == null || (!mStyle.displayCustomViewInline()
5323 && !mRebuildStyledRemoteViews);
5324 }
5325
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005326 /**
5327 * Construct a RemoteViews for the final big notification layout.
5328 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005329 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005330 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005331 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005332 return mN.bigContentView;
5333 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005334 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005335 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005336 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005337 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5338 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005339 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005340 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005341 return result;
5342 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005343
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005344 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005345 * Construct a RemoteViews for the final notification header only. This will not be
5346 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005347 *
5348 * @hide
5349 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005350 public RemoteViews makeNotificationHeader() {
5351 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005352 }
5353
5354 /**
5355 * Construct a RemoteViews for the final notification header only. This will not be
5356 * colorized.
5357 *
5358 * @param p the template params to inflate this with
5359 */
5360 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5361 // Headers on their own are never colorized
5362 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005363 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005364 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005365 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005366 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005367 return header;
5368 }
5369
Adrian Roos487374f2017-01-11 15:48:14 -08005370 /**
5371 * Construct a RemoteViews for the ambient version of the notification.
5372 *
5373 * @hide
5374 */
5375 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005376 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5377 if (headsUpContentView != null) {
5378 return headsUpContentView;
5379 }
5380 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005381 }
5382
Selim Cinek29603462015-11-17 19:04:39 -08005383 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005384 if (result != null) {
5385 result.setViewVisibility(R.id.text_line_1, View.GONE);
5386 }
Selim Cinek29603462015-11-17 19:04:39 -08005387 }
5388
Selim Cinek6743c0b2017-01-18 18:24:01 -08005389 /**
5390 * Adapt the Notification header if this view is used as an expanded view.
5391 *
5392 * @hide
5393 */
5394 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005395 if (result != null) {
5396 result.setBoolean(R.id.notification_header, "setExpanded", true);
5397 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005398 }
5399
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005400 /**
5401 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005402 *
5403 * @param increasedHeight true if this layout be created with an increased height. Some
5404 * styles may support showing more then just that basic 1U size
5405 * and the system may decide to render important notifications
5406 * slightly bigger even when collapsed.
5407 *
5408 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005409 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005410 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005411 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005412 return mN.headsUpContentView;
5413 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005414 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5415 if (styleView != null) {
5416 return styleView;
5417 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005418 } else if (mActions.size() == 0) {
5419 return null;
5420 }
5421
Selim Cinekbee4e072018-05-21 22:06:43 -07005422 // We only want at most a single remote input history to be shown here, otherwise
5423 // the content would become squished.
5424 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5425 .setMaxRemoteInputHistory(1);
5426 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5427 p,
5428 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005429 }
5430
Selim Cinek624c02db2015-12-14 21:00:02 -08005431 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005432 * Construct a RemoteViews for the final heads-up notification layout.
5433 */
5434 public RemoteViews createHeadsUpContentView() {
5435 return createHeadsUpContentView(false /* useIncreasedHeight */);
5436 }
5437
5438 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005439 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5440 *
5441 * @hide
5442 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005443 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005444 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005445 return makePublicView(false /* ambient */);
5446 }
5447
5448 /**
5449 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5450 *
5451 * @hide
5452 */
5453 public RemoteViews makePublicAmbientNotification() {
5454 return makePublicView(true /* ambient */);
5455 }
5456
5457 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005458 if (mN.publicVersion != null) {
5459 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005460 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005461 }
5462 Bundle savedBundle = mN.extras;
5463 Style style = mStyle;
5464 mStyle = null;
5465 Icon largeIcon = mN.mLargeIcon;
5466 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005467 Bitmap largeIconLegacy = mN.largeIcon;
5468 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005469 ArrayList<Action> actions = mActions;
5470 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005471 Bundle publicExtras = new Bundle();
5472 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5473 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5474 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5475 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005476 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5477 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005478 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5479 if (appName != null) {
5480 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5481 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005482 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005483 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005484 view = makeNotificationHeader();
5485 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005486 mN.extras = savedBundle;
5487 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005488 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005489 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005490 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005491 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005492 }
5493
Selim Cinek6743c0b2017-01-18 18:24:01 -08005494 /**
5495 * Construct a content view for the display when low - priority
5496 *
5497 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5498 * a new subtext is created consisting of the content of the
5499 * notification.
5500 * @hide
5501 */
5502 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005503 StandardTemplateParams p = mParams.reset()
5504 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005505 .fillTextsFrom(this);
5506 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5507 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005508 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005509 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005510 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005511 return header;
5512 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005513
Selim Cinek6743c0b2017-01-18 18:24:01 -08005514 private CharSequence createSummaryText() {
5515 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5516 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5517 return titleText;
5518 }
5519 SpannableStringBuilder summary = new SpannableStringBuilder();
5520 if (titleText == null) {
5521 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5522 }
5523 BidiFormatter bidi = BidiFormatter.getInstance();
5524 if (titleText != null) {
5525 summary.append(bidi.unicodeWrap(titleText));
5526 }
5527 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5528 if (titleText != null && contentText != null) {
5529 summary.append(bidi.unicodeWrap(mContext.getText(
5530 R.string.notification_header_divider_symbol_with_spaces)));
5531 }
5532 if (contentText != null) {
5533 summary.append(bidi.unicodeWrap(contentText));
5534 }
5535 return summary;
5536 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005537
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005538 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005539 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005540 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005541 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005542 emphazisedMode ? getEmphasizedActionLayoutResource()
5543 : tombstone ? getActionTombstoneLayoutResource()
5544 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005545 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005546 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005547 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005548 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005549 if (action.mRemoteInputs != null) {
5550 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5551 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005552 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005553 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005554 CharSequence title = action.title;
5555 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005556 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005557 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005558 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005559 } else {
5560 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005561 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005562 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005563 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005564 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005565 int rippleColor;
5566 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5567 if (hasColorOverride) {
5568 // There's a span spanning the full text, let's take it and use it as the
5569 // background color
5570 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005571 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005572 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005573 button.setTextColor(R.id.action0, textColor);
5574 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005575 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
Lucas Dupinf15c5e52019-04-01 19:00:12 -07005576 && mTintActionButtons && !mInNightMode) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005577 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005578 button.setTextColor(R.id.action0, rippleColor);
5579 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005580 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005581 }
Selim Cinek396caca2018-04-10 17:46:46 -07005582 // We only want about 20% alpha for the ripple
5583 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5584 button.setColorStateList(R.id.action0, "setRippleColor",
5585 ColorStateList.valueOf(rippleColor));
5586 button.setColorStateList(R.id.action0, "setButtonBackground",
5587 ColorStateList.valueOf(background));
5588 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005589 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005590 button.setTextViewText(R.id.action0, processTextSpans(
5591 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005592 if (isColorized(p)) {
5593 setTextViewColorPrimary(button, R.id.action0, p);
5594 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005595 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005596 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005597 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005598 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5599 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005600 return button;
5601 }
5602
Joe Onoratocb109a02011-01-18 17:57:41 -08005603 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005604 * Ensures contrast on color spans against a background color. also returns the color of the
5605 * text if a span was found that spans over the whole text.
5606 *
5607 * @param charSequence the charSequence on which the spans are
5608 * @param background the background color to ensure the contrast against
5609 * @param outResultColor an array in which a color will be returned as the first element if
5610 * there exists a full length color span.
5611 * @return the contrasted charSequence
5612 */
5613 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5614 ColorStateList[] outResultColor) {
5615 if (charSequence instanceof Spanned) {
5616 Spanned ss = (Spanned) charSequence;
5617 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5618 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5619 for (Object span : spans) {
5620 Object resultSpan = span;
5621 int spanStart = ss.getSpanStart(span);
5622 int spanEnd = ss.getSpanEnd(span);
5623 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5624 if (resultSpan instanceof CharacterStyle) {
5625 resultSpan = ((CharacterStyle) span).getUnderlying();
5626 }
5627 if (resultSpan instanceof TextAppearanceSpan) {
5628 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5629 ColorStateList textColor = originalSpan.getTextColor();
5630 if (textColor != null) {
5631 int[] colors = textColor.getColors();
5632 int[] newColors = new int[colors.length];
5633 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005634 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005635 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005636 }
5637 textColor = new ColorStateList(textColor.getStates().clone(),
5638 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005639 if (fullLength) {
5640 outResultColor[0] = textColor;
5641 // Let's drop the color from the span
5642 textColor = null;
5643 }
Selim Cinek981962e2016-07-20 20:41:58 -07005644 resultSpan = new TextAppearanceSpan(
5645 originalSpan.getFamily(),
5646 originalSpan.getTextStyle(),
5647 originalSpan.getTextSize(),
5648 textColor,
5649 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005650 }
5651 } else if (resultSpan instanceof ForegroundColorSpan) {
5652 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5653 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005654 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005655 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005656 if (fullLength) {
5657 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005658 resultSpan = null;
5659 } else {
5660 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005661 }
5662 } else {
5663 resultSpan = span;
5664 }
Selim Cinek396caca2018-04-10 17:46:46 -07005665 if (resultSpan != null) {
5666 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5667 }
Selim Cinek981962e2016-07-20 20:41:58 -07005668 }
5669 return builder;
5670 }
5671 return charSequence;
5672 }
5673
5674 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005675 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005676 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005677 */
5678 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005679 if (!mIsLegacyInitialized) {
5680 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5681 < Build.VERSION_CODES.LOLLIPOP;
5682 mIsLegacyInitialized = true;
5683 }
5684 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005685 }
5686
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005687 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005688 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005689 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005690 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005691 } else {
5692 return charSequence;
5693 }
5694 }
5695
Dan Sandler26e81cf2014-05-06 10:01:27 -04005696 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005697 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005698 */
Adrian Roos487374f2017-01-11 15:48:14 -08005699 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005700 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005701 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005702 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005703 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005704 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005705 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005706 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005707 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005708 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005709 contentView.setDrawableTint(R.id.icon, false, color,
5710 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005711
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005712 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005713 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005714 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005715 }
5716
Dan Sandler26e81cf2014-05-06 10:01:27 -04005717 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005718 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005719 * if it's grayscale).
5720 */
5721 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005722 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5723 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005724 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005725 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005726 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005727 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005728 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005729 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005730 }
5731
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005732 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005733 if (mN.color != COLOR_DEFAULT) {
5734 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005735 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005736 }
5737
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005738 int resolveContrastColor(StandardTemplateParams p) {
5739 int rawColor = getRawColor(p);
5740 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005741 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005742 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005743
Selim Cinekac5f0272017-05-02 16:05:41 -07005744 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005745 int background = mContext.getColor(
5746 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005747 if (rawColor == COLOR_DEFAULT) {
5748 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005749 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005750 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005751 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005752 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005753 }
5754 if (Color.alpha(color) < 255) {
5755 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005756 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005757 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005758 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005759 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005760 }
5761
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005762 /**
5763 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5764 *
5765 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5766 * @param p the template params to inflate this with
5767 */
5768 private int getRawColor(StandardTemplateParams p) {
5769 if (p.forceDefaultColor) {
5770 return COLOR_DEFAULT;
5771 }
5772 return mN.color;
5773 }
5774
Selim Cinek4717d862018-04-19 09:19:15 +08005775 int resolveNeutralColor() {
5776 if (mNeutralColor != COLOR_INVALID) {
5777 return mNeutralColor;
5778 }
5779 int background = mContext.getColor(
5780 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005781 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5782 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005783 if (Color.alpha(mNeutralColor) < 255) {
5784 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005785 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005786 }
5787 return mNeutralColor;
5788 }
5789
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005790 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005791 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005792 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005793 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005794 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005795 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005796 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005797 mN.actions = new Action[mActions.size()];
5798 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005799 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005800 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005801 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005802 }
Selim Cinek247fa012016-02-18 09:50:48 -08005803 if (mN.bigContentView != null || mN.contentView != null
5804 || mN.headsUpContentView != null) {
5805 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5806 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005807 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005808 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005809
Julia Reynolds3b848122016-02-26 10:45:32 -05005810 /**
5811 * Creates a Builder from an existing notification so further changes can be made.
5812 * @param context The context for your application / activity.
5813 * @param n The notification to create a Builder from.
5814 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005815 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005816 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005817 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005818 ApplicationInfo applicationInfo = n.extras.getParcelable(
5819 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005820 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005821 if (applicationInfo != null) {
5822 try {
5823 builderContext = context.createApplicationContext(applicationInfo,
5824 Context.CONTEXT_RESTRICTED);
5825 } catch (NameNotFoundException e) {
5826 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5827 builderContext = context; // try with our context
5828 }
5829 } else {
5830 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005831 }
5832
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005833 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005834 }
5835
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005836 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005837 * Determines whether the platform can generate contextual actions for a notification.
5838 * By default this is true.
5839 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005840 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005841 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5842 mN.mAllowSystemGeneratedContextualActions = allowed;
5843 return this;
5844 }
5845
5846 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005847 * @deprecated Use {@link #build()} instead.
5848 */
5849 @Deprecated
5850 public Notification getNotification() {
5851 return build();
5852 }
5853
5854 /**
5855 * Combine all of the options that have been set and return a new {@link Notification}
5856 * object.
5857 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005858 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005859 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005860 // first, add any extras from the calling code
5861 if (mUserExtras != null) {
5862 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005863 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005864
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005865 mN.creationTime = System.currentTimeMillis();
5866
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005867 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005868 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005869
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005870 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005871
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005872 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005873 mStyle.reduceImageSizes(mContext);
5874 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005875 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005876 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005877 }
Selim Cinekd0426622017-07-11 13:19:59 +02005878 mN.reduceImageSizes(mContext);
5879
Adrian Roos5081c0d2016-02-26 16:04:19 -08005880 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005881 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005882 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005883 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005884 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5885 mN.contentView.getSequenceNumber());
5886 }
5887 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005888 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005889 if (mN.bigContentView != null) {
5890 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5891 mN.bigContentView.getSequenceNumber());
5892 }
5893 }
5894 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005895 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005896 if (mN.headsUpContentView != null) {
5897 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5898 mN.headsUpContentView.getSequenceNumber());
5899 }
5900 }
5901 }
5902
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005903 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5904 mN.flags |= FLAG_SHOW_LIGHTS;
5905 }
5906
Adrian Roosfb921842017-10-26 14:49:56 +02005907 mN.allPendingIntents = null;
5908
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005909 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005910 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005911
5912 /**
5913 * Apply this Builder to an existing {@link Notification} object.
5914 *
5915 * @hide
5916 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005917 @NonNull
5918 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005919 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005920 return n;
5921 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005922
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005923 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005924 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005925 * change. Also removes extenders on low ram devices, as
5926 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005927 *
5928 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5929 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005930 * @hide
5931 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005932 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5933 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005934 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005935
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005936 // Only strip views for known Styles because we won't know how to
5937 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005938 if (!isLowRam
5939 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005940 && getNotificationStyleClass(templateClass) == null) {
5941 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005942 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005943
5944 // Only strip unmodified BuilderRemoteViews.
5945 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005946 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005947 n.contentView.getSequenceNumber();
5948 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005949 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005950 n.bigContentView.getSequenceNumber();
5951 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005952 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005953 n.headsUpContentView.getSequenceNumber();
5954
5955 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005956 if (!isLowRam
5957 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005958 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005959 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005960
5961 Notification clone = n.clone();
5962 if (stripContentView) {
5963 clone.contentView = null;
5964 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5965 }
5966 if (stripBigContentView) {
5967 clone.bigContentView = null;
5968 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5969 }
5970 if (stripHeadsUpContentView) {
5971 clone.headsUpContentView = null;
5972 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5973 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005974 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005975 String[] allowedServices = context.getResources().getStringArray(
5976 R.array.config_allowedManagedServicesOnLowRamDevices);
5977 if (allowedServices.length == 0) {
5978 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5979 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5980 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5981 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005982 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005983 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005984 }
5985
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005986 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005987 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005988 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005989 }
5990
5991 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005992 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005993 }
5994
5995 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005996 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005997 }
5998
5999 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02006000 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006001 }
6002
6003 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006004 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006005 }
6006
Adrian Roosc1a80b02016-04-05 14:54:55 -07006007 private int getMessagingLayoutResource() {
6008 return R.layout.notification_template_material_messaging;
6009 }
6010
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006011 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006012 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006013 }
6014
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006015 private int getEmphasizedActionLayoutResource() {
6016 return R.layout.notification_material_action_emphasized;
6017 }
6018
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006019 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006020 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006021 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006022
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006023 private int getBackgroundColor(StandardTemplateParams p) {
6024 if (isColorized(p)) {
6025 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006026 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006027 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006028 }
6029 }
6030
Selim Cinek396caca2018-04-10 17:46:46 -07006031 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006032 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006033 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006034 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006035 private int getNeutralColor(StandardTemplateParams p) {
6036 if (isColorized(p)) {
6037 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006038 } else {
6039 return resolveNeutralColor();
6040 }
6041 }
6042
6043 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006044 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006045 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006046 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006047 private int resolveBackgroundColor(StandardTemplateParams p) {
6048 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006049 if (backgroundColor == COLOR_DEFAULT) {
6050 backgroundColor = mContext.getColor(
6051 com.android.internal.R.color.notification_material_background_color);
6052 }
6053 return backgroundColor;
6054 }
6055
Anthony Chenad4d1582017-04-10 16:07:58 -07006056 private boolean shouldTintActionButtons() {
6057 return mTintActionButtons;
6058 }
6059
Selim Cinek99104832017-01-25 14:47:33 -08006060 private boolean textColorsNeedInversion() {
6061 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6062 return false;
6063 }
6064 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6065 return targetSdkVersion > Build.VERSION_CODES.M
6066 && targetSdkVersion < Build.VERSION_CODES.O;
6067 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006068
6069 /**
6070 * Set a color palette to be used as the background and textColors
6071 *
6072 * @param backgroundColor the color to be used as the background
6073 * @param foregroundColor the color to be used as the foreground
6074 *
6075 * @hide
6076 */
6077 public void setColorPalette(int backgroundColor, int foregroundColor) {
6078 mBackgroundColor = backgroundColor;
6079 mForegroundColor = foregroundColor;
6080 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006081 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006082 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006083
6084 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006085 * Forces all styled remoteViews to be built from scratch and not use any cached
6086 * RemoteViews.
6087 * This is needed for legacy apps that are baking in their remoteviews into the
6088 * notification.
6089 *
6090 * @hide
6091 */
6092 public void setRebuildStyledRemoteViews(boolean rebuild) {
6093 mRebuildStyledRemoteViews = rebuild;
6094 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006095
6096 /**
6097 * Get the text that should be displayed in the statusBar when heads upped. This is
6098 * usually just the app name, but may be different depending on the style.
6099 *
6100 * @param publicMode If true, return a text that is safe to display in public.
6101 *
6102 * @hide
6103 */
6104 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6105 if (mStyle != null && !publicMode) {
6106 CharSequence text = mStyle.getHeadsUpStatusBarText();
6107 if (!TextUtils.isEmpty(text)) {
6108 return text;
6109 }
6110 }
6111 return loadHeaderAppName();
6112 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006113 }
6114
6115 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006116 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6117 * remote views.
6118 *
6119 * @hide
6120 */
6121 void reduceImageSizes(Context context) {
6122 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6123 return;
6124 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006125 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006126 if (mLargeIcon != null || largeIcon != null) {
6127 Resources resources = context.getResources();
6128 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006129 int maxWidth = resources.getDimensionPixelSize(isLowRam
6130 ? R.dimen.notification_right_icon_size_low_ram
6131 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006132 int maxHeight = maxWidth;
6133 if (MediaStyle.class.equals(style)
6134 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006135 maxHeight = resources.getDimensionPixelSize(isLowRam
6136 ? R.dimen.notification_media_image_max_height_low_ram
6137 : R.dimen.notification_media_image_max_height);
6138 maxWidth = resources.getDimensionPixelSize(isLowRam
6139 ? R.dimen.notification_media_image_max_width_low_ram
6140 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006141 }
6142 if (mLargeIcon != null) {
6143 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6144 }
6145 if (largeIcon != null) {
6146 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6147 }
6148 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006149 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6150 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6151 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006152 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6153 }
6154
Selim Cineka8cb1262017-08-15 16:53:44 -07006155 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6156 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006157 if (remoteView != null) {
6158 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006159 int maxWidth = resources.getDimensionPixelSize(isLowRam
6160 ? R.dimen.notification_custom_view_max_image_width_low_ram
6161 : R.dimen.notification_custom_view_max_image_width);
6162 int maxHeight = resources.getDimensionPixelSize(isLowRam
6163 ? R.dimen.notification_custom_view_max_image_height_low_ram
6164 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006165 remoteView.reduceImageSizes(maxWidth, maxHeight);
6166 }
6167 }
6168
6169 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006170 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006171 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006172 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006173 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006174 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006175 }
6176
6177 /**
Selim Cinek99104832017-01-25 14:47:33 -08006178 * @return whether this notification has a media session attached
6179 * @hide
6180 */
6181 public boolean hasMediaSession() {
6182 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6183 }
6184
6185 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006186 * @return the style class of this notification
6187 * @hide
6188 */
6189 public Class<? extends Notification.Style> getNotificationStyle() {
6190 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6191
6192 if (!TextUtils.isEmpty(templateClass)) {
6193 return Notification.getNotificationStyleClass(templateClass);
6194 }
6195 return null;
6196 }
6197
6198 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006199 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006200 *
6201 * @hide
6202 */
6203 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006204 if (isColorizedMedia()) {
6205 return true;
6206 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006207 return extras.getBoolean(EXTRA_COLORIZED)
6208 && (hasColorizedPermission() || isForegroundService());
6209 }
6210
6211 /**
6212 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6213 * permission. The permission is checked when a notification is enqueued.
6214 */
6215 private boolean hasColorizedPermission() {
6216 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006217 }
6218
6219 /**
6220 * @return true if this notification is colorized and it is a media notification
6221 *
6222 * @hide
6223 */
6224 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006225 Class<? extends Style> style = getNotificationStyle();
6226 if (MediaStyle.class.equals(style)) {
6227 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6228 if ((colorized == null || colorized) && hasMediaSession()) {
6229 return true;
6230 }
6231 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6232 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6233 return true;
6234 }
6235 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006236 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006237 }
6238
Selim Cinek0847acd2017-04-24 19:48:29 -07006239
6240 /**
6241 * @return true if this is a media notification
6242 *
6243 * @hide
6244 */
6245 public boolean isMediaNotification() {
6246 Class<? extends Style> style = getNotificationStyle();
6247 if (MediaStyle.class.equals(style)) {
6248 return true;
6249 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6250 return true;
6251 }
6252 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006253 }
6254
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006255 /**
6256 * @return true if this is a notification that can show as a bubble.
6257 *
6258 * @hide
6259 */
6260 public boolean isBubbleNotification() {
6261 return (flags & Notification.FLAG_BUBBLE) != 0;
6262 }
6263
Selim Cinek279fa862016-06-14 10:57:25 -07006264 private boolean hasLargeIcon() {
6265 return mLargeIcon != null || largeIcon != null;
6266 }
6267
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006268 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006269 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006270 * @hide
6271 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006272 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006273 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6274 }
6275
6276 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006277 * @return true if the notification will show a chronometer; false otherwise
6278 * @hide
6279 */
6280 public boolean showsChronometer() {
6281 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6282 }
6283
6284 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006285 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006286 */
6287 @SystemApi
6288 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6289 Class<? extends Style>[] classes = new Class[] {
6290 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6291 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6292 MessagingStyle.class };
6293 for (Class<? extends Style> innerClass : classes) {
6294 if (templateClass.equals(innerClass.getName())) {
6295 return innerClass;
6296 }
6297 }
6298 return null;
6299 }
6300
6301 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006302 * An object that can apply a rich notification style to a {@link Notification.Builder}
6303 * object.
6304 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006305 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006306
6307 /**
6308 * The number of items allowed simulatanously in the remote input history.
6309 * @hide
6310 */
6311 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006312 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006313
6314 /**
6315 * @hide
6316 */
6317 protected CharSequence mSummaryText = null;
6318
6319 /**
6320 * @hide
6321 */
6322 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006323
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006324 protected Builder mBuilder;
6325
Chris Wrend6297db2012-05-03 16:20:13 -04006326 /**
6327 * Overrides ContentTitle in the big form of the template.
6328 * This defaults to the value passed to setContentTitle().
6329 */
6330 protected void internalSetBigContentTitle(CharSequence title) {
6331 mBigContentTitle = title;
6332 }
6333
6334 /**
6335 * Set the first line of text after the detail section in the big form of the template.
6336 */
6337 protected void internalSetSummaryText(CharSequence cs) {
6338 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006339 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006340 }
6341
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006342 public void setBuilder(Builder builder) {
6343 if (mBuilder != builder) {
6344 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006345 if (mBuilder != null) {
6346 mBuilder.setStyle(this);
6347 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006348 }
6349 }
6350
Chris Wrend6297db2012-05-03 16:20:13 -04006351 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006352 if (mBuilder == null) {
6353 throw new IllegalArgumentException("Style requires a valid Builder object");
6354 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006355 }
Chris Wrend6297db2012-05-03 16:20:13 -04006356
6357 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006358 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6359 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006360 }
6361
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006362
Selim Cinek384804b2018-04-18 14:31:07 +08006363 /**
6364 * Get the standard view for this style.
6365 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006366 * @param layoutId The layout id to use.
6367 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006368 * @param result The result where template bind information is saved.
6369 * @return A remoteView for this style.
6370 * @hide
6371 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006372 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6373 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006374 checkBuilder();
6375
6376 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006377 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006378 }
6379
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006380 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6381 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006382
Chris Wrend6297db2012-05-03 16:20:13 -04006383 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6384 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006385 } else {
6386 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006387 }
6388
Chris Wrend6297db2012-05-03 16:20:13 -04006389 return contentView;
6390 }
6391
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006392 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006393 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006394 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006395 *
6396 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006397 * @hide
6398 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006399 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006400 return null;
6401 }
6402
6403 /**
6404 * Construct a Style-specific RemoteViews for the final big notification layout.
6405 * @hide
6406 */
6407 public RemoteViews makeBigContentView() {
6408 return null;
6409 }
6410
6411 /**
6412 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006413 *
6414 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006415 * @hide
6416 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006417 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006418 return null;
6419 }
6420
6421 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006422 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006423 * @hide
6424 */
6425 public void addExtras(Bundle extras) {
6426 if (mSummaryTextSet) {
6427 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6428 }
6429 if (mBigContentTitle != null) {
6430 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6431 }
Chris Wren91ad5632013-06-05 15:05:57 -04006432 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006433 }
6434
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006435 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006436 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006437 * @hide
6438 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006439 protected void restoreFromExtras(Bundle extras) {
6440 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6441 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6442 mSummaryTextSet = true;
6443 }
6444 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6445 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6446 }
6447 }
6448
6449
6450 /**
6451 * @hide
6452 */
6453 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006454 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006455 return wip;
6456 }
6457
Daniel Sandler0ec46202015-06-24 01:27:05 -04006458 /**
6459 * @hide
6460 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006461 public void purgeResources() {}
6462
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006463 /**
6464 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6465 * attached to.
6466 *
6467 * @return the fully constructed Notification.
6468 */
6469 public Notification build() {
6470 checkBuilder();
6471 return mBuilder.build();
6472 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006473
6474 /**
6475 * @hide
6476 * @return true if the style positions the progress bar on the second line; false if the
6477 * style hides the progress bar
6478 */
6479 protected boolean hasProgress() {
6480 return true;
6481 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006482
6483 /**
6484 * @hide
6485 * @return Whether we should put the summary be put into the notification header
6486 */
6487 public boolean hasSummaryInHeader() {
6488 return true;
6489 }
Selim Cinek593610c2016-02-16 18:42:57 -08006490
6491 /**
6492 * @hide
6493 * @return Whether custom content views are displayed inline in the style
6494 */
6495 public boolean displayCustomViewInline() {
6496 return false;
6497 }
Selim Cinekd0426622017-07-11 13:19:59 +02006498
6499 /**
6500 * Reduces the image sizes contained in this style.
6501 *
6502 * @hide
6503 */
6504 public void reduceImageSizes(Context context) {
6505 }
Selim Cinek90343862018-02-01 11:07:11 -08006506
6507 /**
6508 * Validate that this style was properly composed. This is called at build time.
6509 * @hide
6510 */
6511 public void validate(Context context) {
6512 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006513
6514 /**
6515 * @hide
6516 */
6517 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006518
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006519 /**
koprivaa1a78482018-10-09 10:09:23 -07006520 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006521 * If {@code null} is returned, the default implementation will be used.
6522 *
6523 * @hide
6524 */
6525 public CharSequence getHeadsUpStatusBarText() {
6526 return null;
6527 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006528 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006529
6530 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006531 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006532 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006533 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006534 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006535 * Notification notif = new Notification.Builder(mContext)
6536 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6537 * .setContentText(subject)
6538 * .setSmallIcon(R.drawable.new_post)
6539 * .setLargeIcon(aBitmap)
6540 * .setStyle(new Notification.BigPictureStyle()
6541 * .bigPicture(aBigBitmap))
6542 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006543 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006544 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006545 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006546 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006547 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006548 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006549 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006550 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006551
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006552 public BigPictureStyle() {
6553 }
6554
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006555 /**
6556 * @deprecated use {@code BigPictureStyle()}.
6557 */
6558 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006559 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006560 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006561 }
6562
Chris Wrend6297db2012-05-03 16:20:13 -04006563 /**
6564 * Overrides ContentTitle in the big form of the template.
6565 * This defaults to the value passed to setContentTitle().
6566 */
6567 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006568 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006569 return this;
6570 }
6571
6572 /**
6573 * Set the first line of text after the detail section in the big form of the template.
6574 */
6575 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006576 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006577 return this;
6578 }
6579
Chris Wren0bd664d2012-08-01 13:56:56 -04006580 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006581 * @hide
6582 */
6583 public Bitmap getBigPicture() {
6584 return mPicture;
6585 }
6586
6587 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006588 * Provide the bitmap to be used as the payload for the BigPicture notification.
6589 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006590 public BigPictureStyle bigPicture(Bitmap b) {
6591 mPicture = b;
6592 return this;
6593 }
6594
Chris Wren3745a3d2012-05-22 15:11:52 -04006595 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006596 * Override the large icon when the big notification is shown.
6597 */
6598 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006599 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6600 }
6601
6602 /**
6603 * Override the large icon when the big notification is shown.
6604 */
6605 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006606 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006607 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006608 return this;
6609 }
6610
Riley Andrews0394a0c2015-11-03 23:36:52 -08006611 /** @hide */
6612 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6613
Daniel Sandler0ec46202015-06-24 01:27:05 -04006614 /**
6615 * @hide
6616 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006617 @Override
6618 public void purgeResources() {
6619 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006620 if (mPicture != null &&
6621 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006622 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006623 mPicture = mPicture.createAshmemBitmap();
6624 }
6625 if (mBigLargeIcon != null) {
6626 mBigLargeIcon.convertToAshmem();
6627 }
6628 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006629
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006630 /**
6631 * @hide
6632 */
Selim Cinekd0426622017-07-11 13:19:59 +02006633 @Override
6634 public void reduceImageSizes(Context context) {
6635 super.reduceImageSizes(context);
6636 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006637 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006638 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006639 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6640 ? R.dimen.notification_big_picture_max_height_low_ram
6641 : R.dimen.notification_big_picture_max_height);
6642 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6643 ? R.dimen.notification_big_picture_max_width_low_ram
6644 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006645 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6646 }
6647 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006648 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6649 ? R.dimen.notification_right_icon_size_low_ram
6650 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006651 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6652 }
6653 }
6654
6655 /**
6656 * @hide
6657 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006658 public RemoteViews makeBigContentView() {
6659 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006660 // This covers the following cases:
6661 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006662 // mN.mLargeIcon
6663 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006664 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006665 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006666 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006667 oldLargeIcon = mBuilder.mN.mLargeIcon;
6668 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006669 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6670 // replacement if the other one is null. Because we're restoring these legacy icons
6671 // for old listeners, this is in general non-null.
6672 largeIconLegacy = mBuilder.mN.largeIcon;
6673 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006674 }
6675
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006676 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006677 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006678 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006679 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006680 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6681 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006682 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006683 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006684 }
Selim Cinek279fa862016-06-14 10:57:25 -07006685 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006686
Christoph Studer5c510ee2014-12-15 16:32:27 +01006687 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006688 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006689 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006690 }
6691
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006692 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006693 return contentView;
6694 }
6695
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006696 /**
6697 * @hide
6698 */
6699 public void addExtras(Bundle extras) {
6700 super.addExtras(extras);
6701
6702 if (mBigLargeIconSet) {
6703 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6704 }
6705 extras.putParcelable(EXTRA_PICTURE, mPicture);
6706 }
6707
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006708 /**
6709 * @hide
6710 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006711 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006712 protected void restoreFromExtras(Bundle extras) {
6713 super.restoreFromExtras(extras);
6714
6715 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006716 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006717 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006718 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006719 mPicture = extras.getParcelable(EXTRA_PICTURE);
6720 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006721
6722 /**
6723 * @hide
6724 */
6725 @Override
6726 public boolean hasSummaryInHeader() {
6727 return false;
6728 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006729
6730 /**
6731 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006732 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6733 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006734 */
6735 @Override
6736 public boolean areNotificationsVisiblyDifferent(Style other) {
6737 if (other == null || getClass() != other.getClass()) {
6738 return true;
6739 }
6740 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006741 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6742 }
6743
6744 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6745 if (a == b) {
6746 return false;
6747 }
6748 if (a == null || b == null) {
6749 return true;
6750 }
6751 return a.getWidth() != b.getWidth()
6752 || a.getHeight() != b.getHeight()
6753 || a.getConfig() != b.getConfig()
6754 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006755 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006756 }
6757
6758 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006759 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006760 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006761 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006762 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006763 * Notification notif = new Notification.Builder(mContext)
6764 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6765 * .setContentText(subject)
6766 * .setSmallIcon(R.drawable.new_mail)
6767 * .setLargeIcon(aBitmap)
6768 * .setStyle(new Notification.BigTextStyle()
6769 * .bigText(aVeryLongString))
6770 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006771 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006772 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006773 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006774 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006775 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006776
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006777 private CharSequence mBigText;
6778
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006779 public BigTextStyle() {
6780 }
6781
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006782 /**
6783 * @deprecated use {@code BigTextStyle()}.
6784 */
6785 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006786 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006787 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006788 }
6789
Chris Wrend6297db2012-05-03 16:20:13 -04006790 /**
6791 * Overrides ContentTitle in the big form of the template.
6792 * This defaults to the value passed to setContentTitle().
6793 */
6794 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006795 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006796 return this;
6797 }
6798
6799 /**
6800 * Set the first line of text after the detail section in the big form of the template.
6801 */
6802 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006803 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006804 return this;
6805 }
6806
Chris Wren0bd664d2012-08-01 13:56:56 -04006807 /**
6808 * Provide the longer text to be displayed in the big form of the
6809 * template in place of the content text.
6810 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006811 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006812 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006813 return this;
6814 }
6815
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006816 /**
6817 * @hide
6818 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006819 public CharSequence getBigText() {
6820 return mBigText;
6821 }
6822
6823 /**
6824 * @hide
6825 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006826 public void addExtras(Bundle extras) {
6827 super.addExtras(extras);
6828
Christoph Studer4600f9b2014-07-22 22:44:43 +02006829 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6830 }
6831
6832 /**
6833 * @hide
6834 */
6835 @Override
6836 protected void restoreFromExtras(Bundle extras) {
6837 super.restoreFromExtras(extras);
6838
6839 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006840 }
6841
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006842 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006843 * @param increasedHeight true if this layout be created with an increased height.
6844 *
6845 * @hide
6846 */
6847 @Override
6848 public RemoteViews makeContentView(boolean increasedHeight) {
6849 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006850 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006851 mBuilder.mActions = new ArrayList<>();
6852 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006853 mBuilder.mActions = mBuilder.mOriginalActions;
6854 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006855 return remoteViews;
6856 }
6857 return super.makeContentView(increasedHeight);
6858 }
6859
6860 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006861 * @hide
6862 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006863 @Override
6864 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6865 if (increasedHeight && mBuilder.mActions.size() > 0) {
6866 return makeBigContentView();
6867 }
6868 return super.makeHeadsUpContentView(increasedHeight);
6869 }
6870
6871 /**
6872 * @hide
6873 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006874 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006875 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006876 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006877 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6878 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006879 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006880
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006881 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006882 if (TextUtils.isEmpty(bigTextText)) {
6883 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6884 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006885 bigTextText = mBuilder.processLegacyText(
6886 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006887 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006888 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6889 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6890 contentView.setViewVisibility(R.id.big_text,
6891 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006892 contentView.setBoolean(R.id.big_text, "setHasImage",
6893 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006894
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006895 return contentView;
6896 }
6897
Julia Reynolds7217dc92018-03-07 12:12:09 -05006898 /**
6899 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006900 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006901 */
6902 @Override
6903 public boolean areNotificationsVisiblyDifferent(Style other) {
6904 if (other == null || getClass() != other.getClass()) {
6905 return true;
6906 }
6907 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006908 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006909 }
6910
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006911 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006912
6913 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006914 * Helper class for generating large-format notifications that include multiple back-and-forth
6915 * messages of varying types between any number of people.
6916 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006917 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006918 * If the platform does not provide large-format notifications, this method has no effect. The
6919 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006920 *
6921 * <p>
6922 * If the app is targeting Android P and above, it is required to use the {@link Person}
6923 * class in order to get an optimal rendering of the notification and its avatars. For
6924 * conversations involving multiple people, the app should also make sure that it marks the
6925 * conversation as a group with {@link #setGroupConversation(boolean)}.
6926 *
6927 * <p>
6928 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6929 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006930 * <pre class="prettyprint">
6931 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006932 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6933 * MessagingStyle style = new MessagingStyle(user)
6934 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6935 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6936 * .setGroupConversation(hasMultiplePeople());
6937 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006938 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006939 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006940 * .setContentText(subject)
6941 * .setSmallIcon(R.drawable.new_message)
6942 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006943 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006944 * .build();
6945 * </pre>
6946 */
6947 public static class MessagingStyle extends Style {
6948
6949 /**
6950 * The maximum number of messages that will be retained in the Notification itself (the
6951 * number displayed is up to the platform).
6952 */
6953 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6954
Selim Cinekcb8b9852017-12-15 18:01:52 -08006955 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006956 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006957 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006958 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006959 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006960
6961 MessagingStyle() {
6962 }
6963
6964 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006965 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6966 * user before the posting app reposts the notification with those messages after they've
6967 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006968 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006969 *
6970 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006971 */
Alex Hillsfd590442016-10-07 09:52:44 -04006972 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006973 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006974 }
6975
6976 /**
6977 * @param user Required - The person displayed for any messages that are sent by the
6978 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6979 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006980 * by this user. The user also needs to have a valid name associated with it, which will
6981 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006982 */
6983 public MessagingStyle(@NonNull Person user) {
6984 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006985 }
6986
6987 /**
6988 * Validate that this style was properly composed. This is called at build time.
6989 * @hide
6990 */
6991 @Override
6992 public void validate(Context context) {
6993 super.validate(context);
6994 if (context.getApplicationInfo().targetSdkVersion
6995 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6996 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006997 }
6998 }
6999
7000 /**
koprivaa1a78482018-10-09 10:09:23 -07007001 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08007002 * If {@code null} is returned, the default implementation will be used.
7003 *
7004 * @hide
7005 */
7006 @Override
7007 public CharSequence getHeadsUpStatusBarText() {
7008 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
7009 ? super.mBigContentTitle
7010 : mConversationTitle;
7011 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
7012 return conversationTitle;
7013 }
7014 return null;
7015 }
7016
7017 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08007018 * @return the user to be displayed for any replies sent by the user
7019 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007020 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08007021 public Person getUser() {
7022 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007023 }
7024
7025 /**
7026 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007027 *
7028 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007029 */
7030 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007031 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007032 }
7033
7034 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007035 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7036 *
Kodlee Yin14656422017-12-22 17:00:46 -08007037 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7038 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7039 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7040 * {@code true} and passing {@code null} will make it return {@code false}. In
7041 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7042 * to set group conversation status.
7043 *
7044 * @param conversationTitle Title displayed for this conversation
7045 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007046 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007047 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007048 mConversationTitle = conversationTitle;
7049 return this;
7050 }
7051
7052 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007053 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007054 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007055 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007056 public CharSequence getConversationTitle() {
7057 return mConversationTitle;
7058 }
7059
7060 /**
7061 * Adds a message for display by this notification. Convenience call for a simple
7062 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7063 * @param text A {@link CharSequence} to be displayed as the message content
7064 * @param timestamp Time at which the message arrived
7065 * @param sender A {@link CharSequence} to be used for displaying the name of the
7066 * sender. Should be <code>null</code> for messages by the current user, in which case
7067 * the platform will insert {@link #getUserDisplayName()}.
7068 * Should be unique amongst all individuals in the conversation, and should be
7069 * consistent during re-posts of the notification.
7070 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007071 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007072 *
7073 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007074 *
7075 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007076 */
7077 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007078 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007079 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007080 }
7081
7082 /**
7083 * Adds a message for display by this notification. Convenience call for a simple
7084 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7085 * @param text A {@link CharSequence} to be displayed as the message content
7086 * @param timestamp Time at which the message arrived
7087 * @param sender The {@link Person} who sent the message.
7088 * Should be <code>null</code> for messages by the current user, in which case
7089 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7090 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007091 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007092 *
7093 * @return this object for method chaining
7094 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007095 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7096 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007097 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007098 }
7099
7100 /**
7101 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007102 *
7103 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7104 * the newest last.
7105 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007106 * @param message The {@link Message} to be displayed
7107 * @return this object for method chaining
7108 */
7109 public MessagingStyle addMessage(Message message) {
7110 mMessages.add(message);
7111 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7112 mMessages.remove(0);
7113 }
7114 return this;
7115 }
7116
7117 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007118 * Adds a {@link Message} for historic context in this notification.
7119 *
7120 * <p>Messages should be added as historic if they are not the main subject of the
7121 * notification but may give context to a conversation. The system may choose to present
7122 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7123 *
7124 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7125 * the newest last.
7126 *
7127 * @param message The historic {@link Message} to be added
7128 * @return this object for method chaining
7129 */
7130 public MessagingStyle addHistoricMessage(Message message) {
7131 mHistoricMessages.add(message);
7132 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7133 mHistoricMessages.remove(0);
7134 }
7135 return this;
7136 }
7137
7138 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007139 * Gets the list of {@code Message} objects that represent the notification
7140 */
7141 public List<Message> getMessages() {
7142 return mMessages;
7143 }
7144
7145 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007146 * Gets the list of historic {@code Message}s in the notification.
7147 */
7148 public List<Message> getHistoricMessages() {
7149 return mHistoricMessages;
7150 }
7151
7152 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007153 * Sets whether this conversation notification represents a group. If the app is targeting
7154 * Android P, this is required if the app wants to display the largeIcon set with
7155 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007156 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007157 * @param isGroupConversation {@code true} if the conversation represents a group,
7158 * {@code false} otherwise.
7159 * @return this object for method chaining
7160 */
7161 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7162 mIsGroupConversation = isGroupConversation;
7163 return this;
7164 }
7165
7166 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007167 * Returns {@code true} if this notification represents a group conversation, otherwise
7168 * {@code false}.
7169 *
7170 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7171 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7172 * not the conversation title is set; returning {@code true} if the conversation title is
7173 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7174 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7175 * named, non-group conversations.
7176 *
7177 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007178 */
7179 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007180 // When target SDK version is < P, a non-null conversation title dictates if this is
7181 // as group conversation.
7182 if (mBuilder != null
7183 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7184 < Build.VERSION_CODES.P) {
7185 return mConversationTitle != null;
7186 }
7187
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007188 return mIsGroupConversation;
7189 }
7190
7191 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007192 * @hide
7193 */
7194 @Override
7195 public void addExtras(Bundle extras) {
7196 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007197 if (mUser != null) {
7198 // For legacy usages
7199 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7200 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007201 }
7202 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007203 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007204 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007205 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7206 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007207 }
Adrian Roos437cd562017-01-18 15:47:03 -08007208 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7209 Message.getBundleArrayForMessages(mHistoricMessages));
7210 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007211
7212 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007213 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007214 }
7215
7216 private void fixTitleAndTextExtras(Bundle extras) {
7217 Message m = findLatestIncomingMessage();
7218 CharSequence text = (m == null) ? null : m.mText;
7219 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007220 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7221 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007222 CharSequence title;
7223 if (!TextUtils.isEmpty(mConversationTitle)) {
7224 if (!TextUtils.isEmpty(sender)) {
7225 BidiFormatter bidi = BidiFormatter.getInstance();
7226 title = mBuilder.mContext.getString(
7227 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007228 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007229 } else {
7230 title = mConversationTitle;
7231 }
7232 } else {
7233 title = sender;
7234 }
7235
7236 if (title != null) {
7237 extras.putCharSequence(EXTRA_TITLE, title);
7238 }
7239 if (text != null) {
7240 extras.putCharSequence(EXTRA_TEXT, text);
7241 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007242 }
7243
7244 /**
7245 * @hide
7246 */
7247 @Override
7248 protected void restoreFromExtras(Bundle extras) {
7249 super.restoreFromExtras(extras);
7250
Selim Cinekcb8b9852017-12-15 18:01:52 -08007251 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7252 if (mUser == null) {
7253 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007254 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007255 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007256 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007257 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007258 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007259 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007260 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007261 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007262 }
7263
7264 /**
7265 * @hide
7266 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007267 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007268 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007269 mBuilder.mOriginalActions = mBuilder.mActions;
7270 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007271 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007272 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007273 mBuilder.mActions = mBuilder.mOriginalActions;
7274 mBuilder.mOriginalActions = null;
7275 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007276 }
7277
Julia Reynolds7217dc92018-03-07 12:12:09 -05007278 /**
7279 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007280 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007281 */
7282 @Override
7283 public boolean areNotificationsVisiblyDifferent(Style other) {
7284 if (other == null || getClass() != other.getClass()) {
7285 return true;
7286 }
7287 MessagingStyle newS = (MessagingStyle) other;
7288 List<MessagingStyle.Message> oldMs = getMessages();
7289 List<MessagingStyle.Message> newMs = newS.getMessages();
7290
Dan Sandler7d67bd42018-05-15 14:06:38 -04007291 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007292 newMs = new ArrayList<>();
7293 }
7294
7295 int n = oldMs.size();
7296 if (n != newMs.size()) {
7297 return true;
7298 }
7299 for (int i = 0; i < n; i++) {
7300 MessagingStyle.Message oldM = oldMs.get(i);
7301 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007302 if (!Objects.equals(
7303 String.valueOf(oldM.getText()),
7304 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007305 return true;
7306 }
7307 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7308 return true;
7309 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007310 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7311 ? oldM.getSender()
7312 : oldM.getSenderPerson().getName());
7313 String newSender = String.valueOf(newM.getSenderPerson() == null
7314 ? newM.getSender()
7315 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007316 if (!Objects.equals(oldSender, newSender)) {
7317 return true;
7318 }
7319
7320 String oldKey = oldM.getSenderPerson() == null
7321 ? null : oldM.getSenderPerson().getKey();
7322 String newKey = newM.getSenderPerson() == null
7323 ? null : newM.getSenderPerson().getKey();
7324 if (!Objects.equals(oldKey, newKey)) {
7325 return true;
7326 }
7327 // Other fields (like timestamp) intentionally excluded
7328 }
7329 return false;
7330 }
7331
Adrian Roosc1a80b02016-04-05 14:54:55 -07007332 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007333 return findLatestIncomingMessage(mMessages);
7334 }
7335
7336 /**
7337 * @hide
7338 */
7339 @Nullable
7340 public static Message findLatestIncomingMessage(
7341 List<Message> messages) {
7342 for (int i = messages.size() - 1; i >= 0; i--) {
7343 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007344 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007345 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007346 return m;
7347 }
7348 }
Selim Cinek88188f22017-09-19 16:46:56 -07007349 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007350 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007351 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007352 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007353 return null;
7354 }
7355
7356 /**
7357 * @hide
7358 */
7359 @Override
7360 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007361 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007362 }
7363
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007364 /**
7365 * Create a messaging layout.
7366 *
7367 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7368 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007369 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007370 * @return the created remoteView.
7371 */
Selim Cinekafeed292017-12-12 17:32:44 -08007372 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007373 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007374 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007375 ? super.mBigContentTitle
7376 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007377 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7378 >= Build.VERSION_CODES.P;
7379 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007380 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007381 Icon avatarReplacement = null;
7382 if (!atLeastP) {
7383 isOneToOne = TextUtils.isEmpty(conversationTitle);
7384 avatarReplacement = mBuilder.mN.mLargeIcon;
7385 if (hasOnlyWhiteSpaceSenders()) {
7386 isOneToOne = true;
7387 nameReplacement = conversationTitle;
7388 conversationTitle = null;
7389 }
7390 } else {
7391 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007392 }
Selim Cinek384804b2018-04-18 14:31:07 +08007393 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007394 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7395 conversationTitle).text(null)
7396 .hideLargeIcon(hideRightIcons || isOneToOne)
7397 .hideReplyIcon(hideRightIcons)
7398 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007399 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007400 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007401 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007402 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007403 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007404 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007405 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7406 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007407 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007408 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7409 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007410 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007411 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007412 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007413 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007414 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7415 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007416 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7417 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007418 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7419 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007420 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7421 isOneToOne);
7422 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7423 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007424 return contentView;
7425 }
7426
Selim Cinekf7409db2017-10-24 16:17:14 -07007427 private boolean hasOnlyWhiteSpaceSenders() {
7428 for (int i = 0; i < mMessages.size(); i++) {
7429 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007430 Person sender = m.getSenderPerson();
7431 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007432 return false;
7433 }
7434 }
7435 return true;
7436 }
7437
7438 private boolean isWhiteSpace(CharSequence sender) {
7439 if (TextUtils.isEmpty(sender)) {
7440 return true;
7441 }
7442 if (sender.toString().matches("^\\s*$")) {
7443 return true;
7444 }
7445 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7446 // For the presentation that we had.
7447 for (int i = 0; i < sender.length(); i++) {
7448 char c = sender.charAt(i);
7449 if (c != '\u200B') {
7450 return false;
7451 }
7452 }
7453 return true;
7454 }
7455
Selim Cinek88188f22017-09-19 16:46:56 -07007456 private CharSequence createConversationTitleFromMessages() {
7457 ArraySet<CharSequence> names = new ArraySet<>();
7458 for (int i = 0; i < mMessages.size(); i++) {
7459 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007460 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007461 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007462 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007463 }
7464 }
7465 SpannableStringBuilder title = new SpannableStringBuilder();
7466 int size = names.size();
7467 for (int i = 0; i < size; i++) {
7468 CharSequence name = names.valueAt(i);
7469 if (!TextUtils.isEmpty(title)) {
7470 title.append(", ");
7471 }
7472 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7473 }
7474 return title;
7475 }
7476
Adrian Roosdedd1df2016-04-26 16:38:47 -07007477 /**
7478 * @hide
7479 */
7480 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007481 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007482 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007483 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007484 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7485 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007486 }
7487
Adrian Roosc1a80b02016-04-05 14:54:55 -07007488 private static TextAppearanceSpan makeFontColorSpan(int color) {
7489 return new TextAppearanceSpan(null, 0, 0,
7490 ColorStateList.valueOf(color), null);
7491 }
7492
Alex Hillsd9b04d92016-04-11 16:38:16 -04007493 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007494 /** @hide */
7495 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007496 static final String KEY_TIMESTAMP = "time";
7497 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007498 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007499 static final String KEY_DATA_MIME_TYPE = "type";
7500 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007501 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007502 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007503
7504 private final CharSequence mText;
7505 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007506 @Nullable
7507 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007508 /** True if this message was generated from the extra
7509 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7510 */
7511 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007512
Shane Brennan5a871862017-03-11 13:14:17 -08007513 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007514 private String mDataMimeType;
7515 private Uri mDataUri;
7516
7517 /**
7518 * Constructor
7519 * @param text A {@link CharSequence} to be displayed as the message content
7520 * @param timestamp Time at which the message arrived
7521 * @param sender A {@link CharSequence} to be used for displaying the name of the
7522 * sender. Should be <code>null</code> for messages by the current user, in which case
7523 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7524 * Should be unique amongst all individuals in the conversation, and should be
7525 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007526 *
7527 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007528 */
7529 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007530 this(text, timestamp, sender == null ? null
7531 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007532 }
7533
7534 /**
7535 * Constructor
7536 * @param text A {@link CharSequence} to be displayed as the message content
7537 * @param timestamp Time at which the message arrived
7538 * @param sender The {@link Person} who sent the message.
7539 * Should be <code>null</code> for messages by the current user, in which case
7540 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7541 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007542 * The person provided should contain an Icon, set with
7543 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7544 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7545 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7546 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007547 * </p>
7548 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007549 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007550 this(text, timestamp, sender, false /* remoteHistory */);
7551 }
7552
7553 /**
7554 * Constructor
7555 * @param text A {@link CharSequence} to be displayed as the message content
7556 * @param timestamp Time at which the message arrived
7557 * @param sender The {@link Person} who sent the message.
7558 * Should be <code>null</code> for messages by the current user, in which case
7559 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7560 * @param remoteInputHistory True if the messages was generated from the extra
7561 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7562 * <p>
7563 * The person provided should contain an Icon, set with
7564 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7565 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7566 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7567 * to differentiate between the different users.
7568 * </p>
7569 * @hide
7570 */
7571 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7572 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007573 mText = text;
7574 mTimestamp = timestamp;
7575 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007576 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007577 }
7578
7579 /**
7580 * Sets a binary blob of data and an associated MIME type for a message. In the case
7581 * where the platform doesn't support the MIME type, the original text provided in the
7582 * constructor will be used.
7583 * @param dataMimeType The MIME type of the content. See
7584 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7585 * types on Android and Android Wear.
7586 * @param dataUri The uri containing the content whose type is given by the MIME type.
7587 * <p class="note">
7588 * <ol>
7589 * <li>Notification Listeners including the System UI need permission to access the
7590 * data the Uri points to. The recommended ways to do this are:</li>
7591 * <li>Store the data in your own ContentProvider, making sure that other apps have
7592 * the correct permission to access your provider. The preferred mechanism for
7593 * providing access is to use per-URI permissions which are temporary and only
7594 * grant access to the receiving application. An easy way to create a
7595 * ContentProvider like this is to use the FileProvider helper class.</li>
7596 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7597 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7598 * also store non-media types (see MediaStore.Files for more info). Files can be
7599 * inserted into the MediaStore using scanFile() after which a content:// style
7600 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7601 * Note that once added to the system MediaStore the content is accessible to any
7602 * app on the device.</li>
7603 * </ol>
7604 * @return this object for method chaining
7605 */
7606 public Message setData(String dataMimeType, Uri dataUri) {
7607 mDataMimeType = dataMimeType;
7608 mDataUri = dataUri;
7609 return this;
7610 }
7611
Alex Hillsfc737de2016-03-23 17:33:02 -04007612 /**
7613 * Get the text to be used for this message, or the fallback text if a type and content
7614 * Uri have been set
7615 */
7616 public CharSequence getText() {
7617 return mText;
7618 }
7619
7620 /**
7621 * Get the time at which this message arrived
7622 */
7623 public long getTimestamp() {
7624 return mTimestamp;
7625 }
7626
7627 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007628 * Get the extras Bundle for this message.
7629 */
7630 public Bundle getExtras() {
7631 return mExtras;
7632 }
7633
7634 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007635 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007636 *
7637 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007638 */
7639 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007640 return mSender == null ? null : mSender.getName();
7641 }
7642
7643 /**
7644 * Get the sender associated with this message.
7645 */
7646 @Nullable
7647 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007648 return mSender;
7649 }
7650
7651 /**
7652 * Get the MIME type of the data pointed to by the Uri
7653 */
7654 public String getDataMimeType() {
7655 return mDataMimeType;
7656 }
7657
7658 /**
koprivaa1a78482018-10-09 10:09:23 -07007659 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007660 * {@see #getText()} is used.
7661 */
7662 public Uri getDataUri() {
7663 return mDataUri;
7664 }
7665
Kenny Guya0f6de82018-04-06 16:20:16 +01007666 /**
7667 * @return True if the message was generated from
7668 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7669 * @hide
7670 */
7671 public boolean isRemoteInputHistory() {
7672 return mRemoteInputHistory;
7673 }
7674
Beverlye98937a2018-11-15 10:18:57 -05007675 /**
7676 * @hide
7677 */
7678 @VisibleForTesting
7679 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007680 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007681 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007682 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007683 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007684 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007685 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007686 // Legacy listeners need this
7687 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7688 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007689 }
7690 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007691 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007692 }
7693 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007694 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007695 }
Shane Brennan5a871862017-03-11 13:14:17 -08007696 if (mExtras != null) {
7697 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7698 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007699 if (mRemoteInputHistory) {
7700 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7701 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007702 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007703 }
7704
Alex Hillsd9b04d92016-04-11 16:38:16 -04007705 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7706 Bundle[] bundles = new Bundle[messages.size()];
7707 final int N = messages.size();
7708 for (int i = 0; i < N; i++) {
7709 bundles[i] = messages.get(i).toBundle();
7710 }
7711 return bundles;
7712 }
7713
Selim Cinek88188f22017-09-19 16:46:56 -07007714 /**
7715 * @return A list of messages read from the bundles.
7716 *
7717 * @hide
7718 */
7719 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7720 if (bundles == null) {
7721 return new ArrayList<>();
7722 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007723 List<Message> messages = new ArrayList<>(bundles.length);
7724 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007725 if (bundles[i] instanceof Bundle) {
7726 Message message = getMessageFromBundle((Bundle)bundles[i]);
7727 if (message != null) {
7728 messages.add(message);
7729 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007730 }
7731 }
7732 return messages;
7733 }
7734
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007735 /**
7736 * @return The message that is stored in the bundle or null if the message couldn't be
7737 * resolved.
7738 *
7739 * @hide
7740 */
7741 @Nullable
7742 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007743 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007744 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007745 return null;
7746 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007747
7748 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7749 if (senderPerson == null) {
7750 // Legacy apps that use compat don't actually provide the sender objects
7751 // We need to fix the compat version to provide people / use
7752 // the native api instead
7753 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7754 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007755 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007756 }
7757 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007758 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007759 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007760 senderPerson,
7761 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007762 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7763 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007764 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7765 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007766 }
Shane Brennan5a871862017-03-11 13:14:17 -08007767 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7768 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7769 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007770 return message;
7771 }
7772 } catch (ClassCastException e) {
7773 return null;
7774 }
7775 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007776 }
7777 }
7778
7779 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007780 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007781 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007782 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007783 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007784 * Notification notif = new Notification.Builder(mContext)
7785 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7786 * .setContentText(subject)
7787 * .setSmallIcon(R.drawable.new_mail)
7788 * .setLargeIcon(aBitmap)
7789 * .setStyle(new Notification.InboxStyle()
7790 * .addLine(str1)
7791 * .addLine(str2)
7792 * .setContentTitle(&quot;&quot;)
7793 * .setSummaryText(&quot;+3 more&quot;))
7794 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007795 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007796 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007797 * @see Notification#bigContentView
7798 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007799 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007800
7801 /**
7802 * The number of lines of remote input history allowed until we start reducing lines.
7803 */
7804 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007805 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7806
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007807 public InboxStyle() {
7808 }
7809
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007810 /**
7811 * @deprecated use {@code InboxStyle()}.
7812 */
7813 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007814 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007815 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007816 }
7817
Chris Wrend6297db2012-05-03 16:20:13 -04007818 /**
7819 * Overrides ContentTitle in the big form of the template.
7820 * This defaults to the value passed to setContentTitle().
7821 */
7822 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007823 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007824 return this;
7825 }
7826
7827 /**
7828 * Set the first line of text after the detail section in the big form of the template.
7829 */
7830 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007831 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007832 return this;
7833 }
7834
Chris Wren0bd664d2012-08-01 13:56:56 -04007835 /**
7836 * Append a line to the digest section of the Inbox notification.
7837 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007838 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007839 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007840 return this;
7841 }
7842
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007843 /**
7844 * @hide
7845 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007846 public ArrayList<CharSequence> getLines() {
7847 return mTexts;
7848 }
7849
7850 /**
7851 * @hide
7852 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007853 public void addExtras(Bundle extras) {
7854 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007855
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007856 CharSequence[] a = new CharSequence[mTexts.size()];
7857 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7858 }
7859
Christoph Studer4600f9b2014-07-22 22:44:43 +02007860 /**
7861 * @hide
7862 */
7863 @Override
7864 protected void restoreFromExtras(Bundle extras) {
7865 super.restoreFromExtras(extras);
7866
7867 mTexts.clear();
7868 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7869 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7870 }
7871 }
7872
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007873 /**
7874 * @hide
7875 */
7876 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007877 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007878 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007879 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007880
Chris Wrend6297db2012-05-03 16:20:13 -04007881 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 -04007882 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007883
Chris Wren4ed80d52012-05-17 09:30:03 -04007884 // Make sure all rows are gone in case we reuse a view.
7885 for (int rowId : rowIds) {
7886 contentView.setViewVisibility(rowId, View.GONE);
7887 }
7888
Daniel Sandler879c5e02012-04-17 16:46:51 -04007889 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007890 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7891 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007892 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007893 int onlyViewId = 0;
7894 int maxRows = rowIds.length;
7895 if (mBuilder.mActions.size() > 0) {
7896 maxRows--;
7897 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007898 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7899 EXTRA_REMOTE_INPUT_HISTORY);
7900 if (remoteInputHistory != null
7901 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7902 // Let's remove some messages to make room for the remote input history.
7903 // 1 is always able to fit, but let's remove them if they are 2 or 3
7904 int numRemoteInputs = Math.min(remoteInputHistory.length,
7905 MAX_REMOTE_INPUT_HISTORY_LINES);
7906 int totalNumRows = mTexts.size() + numRemoteInputs
7907 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7908 if (totalNumRows > maxRows) {
7909 int overflow = totalNumRows - maxRows;
7910 if (mTexts.size() > maxRows) {
7911 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7912 // few messages, even with the remote input
7913 maxRows -= overflow;
7914 } else {
7915 // otherwise we drop the first messages
7916 i = overflow;
7917 }
7918 }
7919 }
Selim Cinek07c80172016-04-21 16:40:47 -07007920 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007921 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007922 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007923 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007924 contentView.setTextViewText(rowIds[i],
7925 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007926 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007927 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007928 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007929 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007930 if (first) {
7931 onlyViewId = rowIds[i];
7932 } else {
7933 onlyViewId = 0;
7934 }
Selim Cinek247fa012016-02-18 09:50:48 -08007935 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007936 }
7937 i++;
7938 }
Selim Cinek07c80172016-04-21 16:40:47 -07007939 if (onlyViewId != 0) {
7940 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7941 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7942 R.dimen.notification_text_margin_top);
7943 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7944 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007945
Daniel Sandler879c5e02012-04-17 16:46:51 -04007946 return contentView;
7947 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007948
Julia Reynolds7217dc92018-03-07 12:12:09 -05007949 /**
7950 * @hide
7951 */
7952 @Override
7953 public boolean areNotificationsVisiblyDifferent(Style other) {
7954 if (other == null || getClass() != other.getClass()) {
7955 return true;
7956 }
7957 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007958
7959 final ArrayList<CharSequence> myLines = getLines();
7960 final ArrayList<CharSequence> newLines = newS.getLines();
7961 final int n = myLines.size();
7962 if (n != newLines.size()) {
7963 return true;
7964 }
7965
7966 for (int i = 0; i < n; i++) {
7967 if (!Objects.equals(
7968 String.valueOf(myLines.get(i)),
7969 String.valueOf(newLines.get(i)))) {
7970 return true;
7971 }
7972 }
7973 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007974 }
7975
Selim Cinek384804b2018-04-18 14:31:07 +08007976 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007977 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007978 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007979 if (first) {
7980 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7981 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7982 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007983 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007984 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007985 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007986 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007987 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007988 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007989 }
Dan Sandler842dd772014-05-15 09:36:47 -04007990
7991 /**
7992 * Notification style for media playback notifications.
7993 *
7994 * In the expanded form, {@link Notification#bigContentView}, up to 5
7995 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007996 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007997 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7998 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7999 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08008000 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04008001 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
8002 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01008003 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04008004 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08008005 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008006 * Notifications created with MediaStyle will have their category set to
8007 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
8008 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08008009 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07008010 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
8011 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04008012 * the System UI can identify this as a notification representing an active media session
8013 * and respond accordingly (by showing album artwork in the lockscreen, for example).
8014 *
Selim Cinek99104832017-01-25 14:47:33 -08008015 * <p>
8016 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
8017 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
8018 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
8019 * <p>
8020 *
Dan Sandler842dd772014-05-15 09:36:47 -04008021 * To use this style with your Notification, feed it to
8022 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8023 * <pre class="prettyprint">
8024 * Notification noti = new Notification.Builder()
8025 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008026 * .setContentTitle(&quot;Track title&quot;)
8027 * .setContentText(&quot;Artist - Album&quot;)
8028 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008029 * .setStyle(<b>new Notification.MediaStyle()</b>
8030 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008031 * .build();
8032 * </pre>
8033 *
8034 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008035 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008036 */
8037 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008038 // Changing max media buttons requires also changing templates
8039 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008040 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008041 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008042 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8043 R.id.action0,
8044 R.id.action1,
8045 R.id.action2,
8046 R.id.action3,
8047 R.id.action4,
8048 };
Dan Sandler842dd772014-05-15 09:36:47 -04008049
8050 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008051 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008052
8053 public MediaStyle() {
8054 }
8055
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008056 /**
8057 * @deprecated use {@code MediaStyle()}.
8058 */
8059 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008060 public MediaStyle(Builder builder) {
8061 setBuilder(builder);
8062 }
8063
8064 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008065 * 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 -04008066 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008067 *
8068 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008069 */
8070 public MediaStyle setShowActionsInCompactView(int...actions) {
8071 mActionsToShowInCompact = actions;
8072 return this;
8073 }
8074
8075 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008076 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8077 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008078 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008079 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008080 mToken = token;
8081 return this;
8082 }
8083
Christoph Studer4600f9b2014-07-22 22:44:43 +02008084 /**
8085 * @hide
8086 */
Dan Sandler842dd772014-05-15 09:36:47 -04008087 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008088 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008089 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008090 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008091 if (wip.category == null) {
8092 wip.category = Notification.CATEGORY_TRANSPORT;
8093 }
Dan Sandler842dd772014-05-15 09:36:47 -04008094 return wip;
8095 }
8096
Christoph Studer4600f9b2014-07-22 22:44:43 +02008097 /**
8098 * @hide
8099 */
8100 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008101 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008102 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008103 }
8104
8105 /**
8106 * @hide
8107 */
8108 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008109 public RemoteViews makeBigContentView() {
8110 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008111 }
8112
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008113 /**
8114 * @hide
8115 */
8116 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008117 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008118 RemoteViews expanded = makeMediaBigContentView();
8119 return expanded != null ? expanded : makeMediaContentView();
8120 }
8121
Dan Sandler842dd772014-05-15 09:36:47 -04008122 /** @hide */
8123 @Override
8124 public void addExtras(Bundle extras) {
8125 super.addExtras(extras);
8126
8127 if (mToken != null) {
8128 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8129 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008130 if (mActionsToShowInCompact != null) {
8131 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8132 }
Dan Sandler842dd772014-05-15 09:36:47 -04008133 }
8134
Christoph Studer4600f9b2014-07-22 22:44:43 +02008135 /**
8136 * @hide
8137 */
8138 @Override
8139 protected void restoreFromExtras(Bundle extras) {
8140 super.restoreFromExtras(extras);
8141
8142 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8143 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8144 }
8145 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8146 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8147 }
8148 }
8149
Julia Reynolds7217dc92018-03-07 12:12:09 -05008150 /**
8151 * @hide
8152 */
8153 @Override
8154 public boolean areNotificationsVisiblyDifferent(Style other) {
8155 if (other == null || getClass() != other.getClass()) {
8156 return true;
8157 }
8158 // All fields to compare are on the Notification object
8159 return false;
8160 }
8161
Gus Prevas9cc96602018-10-11 11:24:23 -04008162 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008163 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008164 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008165 container.setViewVisibility(buttonId, View.VISIBLE);
8166 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008167
8168 // If the action buttons should not be tinted, then just use the default
8169 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008170 Resources resources = mBuilder.mContext.getResources();
8171 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008172 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8173 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008174 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8175 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008176 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008177 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008178
Gus Prevas9cc96602018-10-11 11:24:23 -04008179 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008180 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008181
8182 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8183 new int[]{ android.R.attr.colorControlHighlight });
8184 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8185 typedArray.recycle();
8186 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8187 Color.blue(tintColor));
8188 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8189
Dan Sandler842dd772014-05-15 09:36:47 -04008190 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008191 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008192 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008193 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008194 }
8195
8196 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008197 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8198 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008199 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008200 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008201 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008202
8203 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008204 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008205 ? 0
8206 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008207 if (numActionsToShow > numActions) {
8208 throw new IllegalArgumentException(String.format(
8209 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8210 numActions, numActions - 1));
8211 }
8212 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8213 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008214 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008215 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008216 } else {
8217 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008218 }
8219 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008220 handleImage(view);
8221 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008222 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008223 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008224 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008225 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008226 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008227 return view;
8228 }
8229
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008230 private int getActionColor(StandardTemplateParams p) {
8231 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8232 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008233 }
8234
Dan Sandler842dd772014-05-15 09:36:47 -04008235 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008236 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008237 // Dont add an expanded view if there is no more content to be revealed
8238 int actionsInCompact = mActionsToShowInCompact == null
8239 ? 0
8240 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008241 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008242 return null;
8243 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008244 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8245 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008246 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008247 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008248
Gus Prevas9cc96602018-10-11 11:24:23 -04008249 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8250 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008251 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008252 } else {
8253 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008254 }
8255 }
linanson2bcd4032019-01-14 15:22:04 +08008256 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8257 mBuilder.mContext.getString(
8258 com.android.internal.R.string.ext_media_seamless_action), null), p);
8259 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008260 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008261 return big;
8262 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008263
Selim Cinek5bf069a2015-11-10 19:14:27 -05008264 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008265 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008266 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8267 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008268 }
8269 }
8270
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008271 /**
8272 * @hide
8273 */
8274 @Override
8275 protected boolean hasProgress() {
8276 return false;
8277 }
Dan Sandler842dd772014-05-15 09:36:47 -04008278 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008279
Selim Cinek593610c2016-02-16 18:42:57 -08008280 /**
8281 * Notification style for custom views that are decorated by the system
8282 *
8283 * <p>Instead of providing a notification that is completely custom, a developer can set this
8284 * style and still obtain system decorations like the notification header with the expand
8285 * affordance and actions.
8286 *
8287 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8288 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8289 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8290 * corresponding custom views to display.
8291 *
8292 * To use this style with your Notification, feed it to
8293 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8294 * <pre class="prettyprint">
8295 * Notification noti = new Notification.Builder()
8296 * .setSmallIcon(R.drawable.ic_stat_player)
8297 * .setLargeIcon(albumArtBitmap))
8298 * .setCustomContentView(contentView);
8299 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8300 * .build();
8301 * </pre>
8302 */
8303 public static class DecoratedCustomViewStyle extends Style {
8304
8305 public DecoratedCustomViewStyle() {
8306 }
8307
Selim Cinek593610c2016-02-16 18:42:57 -08008308 /**
8309 * @hide
8310 */
8311 public boolean displayCustomViewInline() {
8312 return true;
8313 }
8314
8315 /**
8316 * @hide
8317 */
8318 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008319 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008320 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8321 }
8322
8323 /**
8324 * @hide
8325 */
8326 @Override
8327 public RemoteViews makeBigContentView() {
8328 return makeDecoratedBigContentView();
8329 }
8330
8331 /**
8332 * @hide
8333 */
8334 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008335 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008336 return makeDecoratedHeadsUpContentView();
8337 }
8338
Selim Cinek593610c2016-02-16 18:42:57 -08008339 private RemoteViews makeDecoratedHeadsUpContentView() {
8340 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8341 ? mBuilder.mN.contentView
8342 : mBuilder.mN.headsUpContentView;
8343 if (mBuilder.mActions.size() == 0) {
8344 return makeStandardTemplateWithCustomContent(headsUpContentView);
8345 }
Selim Cinek384804b2018-04-18 14:31:07 +08008346 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008347 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008348 mBuilder.getBigBaseLayoutResource(), result);
8349 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008350 return remoteViews;
8351 }
8352
Selim Cinek593610c2016-02-16 18:42:57 -08008353 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008354 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008355 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008356 mBuilder.getBaseLayoutResource(), result);
8357 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008358 return remoteViews;
8359 }
8360
Selim Cinek593610c2016-02-16 18:42:57 -08008361 private RemoteViews makeDecoratedBigContentView() {
8362 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8363 ? mBuilder.mN.contentView
8364 : mBuilder.mN.bigContentView;
8365 if (mBuilder.mActions.size() == 0) {
8366 return makeStandardTemplateWithCustomContent(bigContentView);
8367 }
Selim Cinek384804b2018-04-18 14:31:07 +08008368 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008369 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008370 mBuilder.getBigBaseLayoutResource(), result);
8371 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008372 return remoteViews;
8373 }
Selim Cinek247fa012016-02-18 09:50:48 -08008374
8375 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008376 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008377 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008378 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008379 // Need to clone customContent before adding, because otherwise it can no longer be
8380 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008381 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008382 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8383 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008384 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008385 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008386 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008387 remoteViews.setIntTag(R.id.notification_main_column,
8388 com.android.internal.R.id.notification_custom_view_index_tag,
8389 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008390 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008391 Resources resources = mBuilder.mContext.getResources();
8392 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008393 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008394 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008395 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008396
8397 /**
8398 * @hide
8399 */
8400 @Override
8401 public boolean areNotificationsVisiblyDifferent(Style other) {
8402 if (other == null || getClass() != other.getClass()) {
8403 return true;
8404 }
8405 // Comparison done for all custom RemoteViews, independent of style
8406 return false;
8407 }
Selim Cinek593610c2016-02-16 18:42:57 -08008408 }
8409
Selim Cinek03eb3b72016-02-18 10:39:45 -08008410 /**
8411 * Notification style for media custom views that are decorated by the system
8412 *
8413 * <p>Instead of providing a media notification that is completely custom, a developer can set
8414 * this style and still obtain system decorations like the notification header with the expand
8415 * affordance and actions.
8416 *
8417 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8418 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8419 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8420 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008421 * <p>
8422 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8423 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8424 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008425 * To use this style with your Notification, feed it to
8426 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8427 * <pre class="prettyprint">
8428 * Notification noti = new Notification.Builder()
8429 * .setSmallIcon(R.drawable.ic_stat_player)
8430 * .setLargeIcon(albumArtBitmap))
8431 * .setCustomContentView(contentView);
8432 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8433 * .setMediaSession(mySession))
8434 * .build();
8435 * </pre>
8436 *
8437 * @see android.app.Notification.DecoratedCustomViewStyle
8438 * @see android.app.Notification.MediaStyle
8439 */
8440 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8441
8442 public DecoratedMediaCustomViewStyle() {
8443 }
8444
Selim Cinek03eb3b72016-02-18 10:39:45 -08008445 /**
8446 * @hide
8447 */
8448 public boolean displayCustomViewInline() {
8449 return true;
8450 }
8451
8452 /**
8453 * @hide
8454 */
8455 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008456 public RemoteViews makeContentView(boolean increasedHeight) {
8457 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008458 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8459 mBuilder.mN.contentView);
8460 }
8461
8462 /**
8463 * @hide
8464 */
8465 @Override
8466 public RemoteViews makeBigContentView() {
8467 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8468 ? mBuilder.mN.bigContentView
8469 : mBuilder.mN.contentView;
8470 return makeBigContentViewWithCustomContent(customRemoteView);
8471 }
8472
8473 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8474 RemoteViews remoteViews = super.makeBigContentView();
8475 if (remoteViews != null) {
8476 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8477 customRemoteView);
8478 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008479 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008480 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8481 customRemoteView);
8482 } else {
8483 return null;
8484 }
8485 }
8486
8487 /**
8488 * @hide
8489 */
8490 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008491 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008492 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8493 ? mBuilder.mN.headsUpContentView
8494 : mBuilder.mN.contentView;
8495 return makeBigContentViewWithCustomContent(customRemoteView);
8496 }
8497
Julia Reynolds7217dc92018-03-07 12:12:09 -05008498 /**
8499 * @hide
8500 */
8501 @Override
8502 public boolean areNotificationsVisiblyDifferent(Style other) {
8503 if (other == null || getClass() != other.getClass()) {
8504 return true;
8505 }
8506 // Comparison done for all custom RemoteViews, independent of style
8507 return false;
8508 }
8509
Selim Cinek03eb3b72016-02-18 10:39:45 -08008510 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8511 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008512 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008513 // Need to clone customContent before adding, because otherwise it can no longer be
8514 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008515 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008516 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008517 remoteViews.removeAllViews(id);
8518 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008519 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008520 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008521 return remoteViews;
8522 }
8523 }
8524
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008525 /**
8526 * Encapsulates the information needed to display a notification as a bubble.
8527 *
8528 * <p>A bubble is used to display app content in a floating window over the existing
8529 * foreground activity. A bubble has a collapsed state represented by an icon,
8530 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8531 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8532 *
8533 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8534 * outside of the notification shade on unlocked devices. When a user interacts with the
8535 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8536 *
8537 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8538 */
8539 public static final class BubbleMetadata implements Parcelable {
8540
8541 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008542 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008543 private Icon mIcon;
8544 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008545 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008546 private int mFlags;
8547
8548 /**
8549 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8550 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8551 *
8552 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8553 *
8554 * <p>Generally this flag should only be set if the user has performed an action to request
8555 * or create a bubble.</p>
8556 */
8557 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8558
8559 /**
8560 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8561 * <b>without</b> the associated notification in the notification shade. Subsequent update
8562 * notifications to this bubble will post a notification in the shade.
8563 *
8564 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8565 *
8566 * <p>Generally this flag should only be set if the user has performed an action to request
8567 * or create a bubble.</p>
8568 */
8569 private static final int FLAG_SUPPRESS_INITIAL_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008570
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008571 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008572 Icon icon, int height, @DimenRes int heightResId) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008573 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008574 mIcon = icon;
8575 mDesiredHeight = height;
Mady Mellor7af771a2019-03-07 15:04:54 -08008576 mDesiredHeightResId = heightResId;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008577 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008578 }
8579
8580 private BubbleMetadata(Parcel in) {
8581 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008582 mIcon = Icon.CREATOR.createFromParcel(in);
8583 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008584 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008585 if (in.readInt() != 0) {
8586 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8587 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008588 mDesiredHeightResId = in.readInt();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008589 }
8590
8591 /**
8592 * @return the pending intent used to populate the floating window for this bubble.
8593 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008594 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008595 public PendingIntent getIntent() {
8596 return mPendingIntent;
8597 }
8598
8599 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008600 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8601 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008602 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008603 public PendingIntent getDeleteIntent() {
8604 return mDeleteIntent;
8605 }
8606
8607 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008608 * @return the icon that will be displayed for this bubble when it is collapsed.
8609 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008610 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008611 public Icon getIcon() {
8612 return mIcon;
8613 }
8614
8615 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008616 * @return the ideal height, in DPs, for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008617 * {@link #getIntent()} for this bubble. A value of 0 indicates a desired height has not
8618 * been set.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008619 */
Mady Mellor8a529e22019-03-25 17:37:55 -07008620 @Dimension(unit = DP)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008621 public int getDesiredHeight() {
8622 return mDesiredHeight;
8623 }
8624
Mady Mellor0157ff22019-01-24 15:31:48 -08008625 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008626 * @return the resId of ideal height for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008627 * {@link #getIntent()} for this bubble. A value of 0 indicates a res value has not
8628 * been provided for the desired height.
Mady Mellor7af771a2019-03-07 15:04:54 -08008629 */
8630 @DimenRes
8631 public int getDesiredHeightResId() {
8632 return mDesiredHeightResId;
8633 }
8634
8635 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008636 * @return whether this bubble should auto expand when it is posted.
8637 *
8638 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8639 */
8640 public boolean getAutoExpandBubble() {
8641 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8642 }
8643
8644 /**
8645 * @return whether this bubble should suppress the initial notification when it is posted.
8646 *
8647 * @see BubbleMetadata.Builder#setSuppressInitialNotification(boolean)
8648 */
8649 public boolean getSuppressInitialNotification() {
8650 return (mFlags & FLAG_SUPPRESS_INITIAL_NOTIFICATION) != 0;
8651 }
8652
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008653 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008654 new Parcelable.Creator<BubbleMetadata>() {
8655
8656 @Override
8657 public BubbleMetadata createFromParcel(Parcel source) {
8658 return new BubbleMetadata(source);
8659 }
8660
8661 @Override
8662 public BubbleMetadata[] newArray(int size) {
8663 return new BubbleMetadata[size];
8664 }
8665 };
8666
8667 @Override
8668 public int describeContents() {
8669 return 0;
8670 }
8671
8672 @Override
8673 public void writeToParcel(Parcel out, int flags) {
8674 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008675 mIcon.writeToParcel(out, 0);
8676 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008677 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008678 out.writeInt(mDeleteIntent != null ? 1 : 0);
8679 if (mDeleteIntent != null) {
8680 mDeleteIntent.writeToParcel(out, 0);
8681 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008682 out.writeInt(mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008683 }
8684
8685 private void setFlags(int flags) {
8686 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008687 }
8688
8689 /**
8690 * Builder to construct a {@link BubbleMetadata} object.
8691 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008692 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008693
8694 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008695 private Icon mIcon;
8696 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008697 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008698 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008699 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008700
8701 /**
8702 * Constructs a new builder object.
8703 */
8704 public Builder() {
8705 }
8706
8707 /**
8708 * Sets the intent that will be used when the bubble is expanded. This will display the
8709 * app content in a floating window over the existing foreground activity.
8710 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008711 @NonNull
8712 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008713 if (intent == null) {
8714 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8715 }
8716 mPendingIntent = intent;
8717 return this;
8718 }
8719
8720 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008721 * Sets the icon that will represent the bubble when it is collapsed.
8722 *
8723 * <p>An icon is required and should be representative of the content within the bubble.
8724 * If your app produces multiple bubbles, the image should be unique for each of them.
8725 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008726 *
8727 * <p>The shape of a bubble icon is adaptive and can match the device theme.
8728 *
8729 * If your icon is bitmap-based, you should create it using
8730 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise this method will throw.
8731 *
8732 * If your icon is not bitmap-based, you should expect that the icon will be tinted.
8733 * </p>
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008734 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008735 @NonNull
8736 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008737 if (icon == null) {
8738 throw new IllegalArgumentException("Bubbles require non-null icon");
8739 }
Mady Mellor9848a6c2019-03-19 15:29:05 -07008740 if (icon.getType() == TYPE_BITMAP) {
8741 throw new IllegalArgumentException("When using bitmap based icons, Bubbles "
8742 + "require TYPE_ADAPTIVE_BITMAP, please use"
8743 + " Icon#createWithAdaptiveBitmap instead");
8744 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008745 mIcon = icon;
8746 return this;
8747 }
8748
8749 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008750 * Sets the desired height in DPs for the app content defined by
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008751 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8752 * enough space on the screen or if the provided height is too small to be useful.
Mady Mellor7af771a2019-03-07 15:04:54 -08008753 * <p>
8754 * If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
8755 * previous value set will be cleared after calling this method, and this value will
8756 * be used instead.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008757 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008758 @NonNull
Mady Mellor8a529e22019-03-25 17:37:55 -07008759 public BubbleMetadata.Builder setDesiredHeight(@Dimension(unit = DP) int height) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008760 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008761 mDesiredHeightResId = 0;
8762 return this;
8763 }
8764
8765
8766 /**
8767 * Sets the desired height via resId for the app content defined by
8768 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8769 * enough space on the screen or if the provided height is too small to be useful.
8770 * <p>
8771 * If {@link #setDesiredHeight(int)} was previously called on this builder, the
8772 * previous value set will be cleared after calling this method, and this value will
8773 * be used instead.
8774 */
8775 @NonNull
8776 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8777 mDesiredHeightResId = heightResId;
8778 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008779 return this;
8780 }
8781
8782 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008783 * If set and the app creating the bubble is in the foreground, the bubble will be
8784 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8785 * floating window.
8786 *
8787 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8788 * </p>
8789 *
8790 * <p>Generally this flag should only be set if the user has performed an action to
8791 * request or create a bubble.</p>
8792 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008793 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008794 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8795 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8796 return this;
8797 }
8798
8799 /**
8800 * If set and the app creating the bubble is in the foreground, the bubble will be
8801 * posted <b>without</b> the associated notification in the notification shade.
8802 * Subsequent update notifications to this bubble will post a notification in the shade.
8803 *
8804 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8805 * </p>
8806 *
8807 * <p>Generally this flag should only be set if the user has performed an action to
8808 * request or create a bubble.</p>
8809 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008810 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008811 public BubbleMetadata.Builder setSuppressInitialNotification(
8812 boolean shouldSupressNotif) {
8813 setFlag(FLAG_SUPPRESS_INITIAL_NOTIFICATION, shouldSupressNotif);
8814 return this;
8815 }
8816
8817 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008818 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8819 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008820 @NonNull
8821 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008822 mDeleteIntent = deleteIntent;
8823 return this;
8824 }
8825
8826 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008827 * Creates the {@link BubbleMetadata} defined by this builder.
8828 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8829 * on this builder.</p>
8830 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008831 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008832 public BubbleMetadata build() {
8833 if (mPendingIntent == null) {
8834 throw new IllegalStateException("Must supply pending intent to bubble");
8835 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008836 if (mIcon == null) {
8837 throw new IllegalStateException("Must supply an icon for the bubble");
8838 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008839 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008840 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008841 data.setFlags(mFlags);
8842 return data;
8843 }
8844
8845 /**
8846 * @hide
8847 */
8848 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8849 if (value) {
8850 mFlags |= mask;
8851 } else {
8852 mFlags &= ~mask;
8853 }
8854 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008855 }
8856 }
8857 }
8858
8859
Christoph Studer4600f9b2014-07-22 22:44:43 +02008860 // When adding a new Style subclass here, don't forget to update
8861 // Builder.getNotificationStyleClass.
8862
Griff Hazen61a9e862014-05-22 16:05:19 -07008863 /**
8864 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8865 * metadata or change options on a notification builder.
8866 */
8867 public interface Extender {
8868 /**
8869 * Apply this extender to a notification builder.
8870 * @param builder the builder to be modified.
8871 * @return the build object for chaining.
8872 */
8873 public Builder extend(Builder builder);
8874 }
8875
8876 /**
8877 * Helper class to add wearable extensions to notifications.
8878 * <p class="note"> See
8879 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8880 * for Android Wear</a> for more information on how to use this class.
8881 * <p>
8882 * To create a notification with wearable extensions:
8883 * <ol>
8884 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8885 * properties.
8886 * <li>Create a {@link android.app.Notification.WearableExtender}.
8887 * <li>Set wearable-specific properties using the
8888 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8889 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8890 * notification.
8891 * <li>Post the notification to the notification system with the
8892 * {@code NotificationManager.notify(...)} methods.
8893 * </ol>
8894 *
8895 * <pre class="prettyprint">
8896 * Notification notif = new Notification.Builder(mContext)
8897 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8898 * .setContentText(subject)
8899 * .setSmallIcon(R.drawable.new_mail)
8900 * .extend(new Notification.WearableExtender()
8901 * .setContentIcon(R.drawable.new_mail))
8902 * .build();
8903 * NotificationManager notificationManger =
8904 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8905 * notificationManger.notify(0, notif);</pre>
8906 *
8907 * <p>Wearable extensions can be accessed on an existing notification by using the
8908 * {@code WearableExtender(Notification)} constructor,
8909 * and then using the {@code get} methods to access values.
8910 *
8911 * <pre class="prettyprint">
8912 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8913 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008914 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008915 */
8916 public static final class WearableExtender implements Extender {
8917 /**
8918 * Sentinel value for an action index that is unset.
8919 */
8920 public static final int UNSET_ACTION_INDEX = -1;
8921
8922 /**
8923 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8924 * default sizing.
8925 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008926 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008927 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008928 *
8929 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008930 */
Gus Prevasd7363752018-09-18 14:35:15 -04008931 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008932 public static final int SIZE_DEFAULT = 0;
8933
8934 /**
8935 * Size value for use with {@link #setCustomSizePreset} to show this notification
8936 * with an extra small size.
8937 * <p>This value is only applicable for custom display notifications created using
8938 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008939 *
8940 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008941 */
Gus Prevasd7363752018-09-18 14:35:15 -04008942 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008943 public static final int SIZE_XSMALL = 1;
8944
8945 /**
8946 * Size value for use with {@link #setCustomSizePreset} to show this notification
8947 * with a small size.
8948 * <p>This value is only applicable for custom display notifications created using
8949 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008950 *
8951 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008952 */
Gus Prevasd7363752018-09-18 14:35:15 -04008953 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008954 public static final int SIZE_SMALL = 2;
8955
8956 /**
8957 * Size value for use with {@link #setCustomSizePreset} to show this notification
8958 * with a medium size.
8959 * <p>This value is only applicable for custom display notifications created using
8960 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008961 *
8962 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008963 */
Gus Prevasd7363752018-09-18 14:35:15 -04008964 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008965 public static final int SIZE_MEDIUM = 3;
8966
8967 /**
8968 * Size value for use with {@link #setCustomSizePreset} to show this notification
8969 * with a large size.
8970 * <p>This value is only applicable for custom display notifications created using
8971 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008972 *
8973 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008974 */
Gus Prevasd7363752018-09-18 14:35:15 -04008975 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008976 public static final int SIZE_LARGE = 4;
8977
Griff Hazend5f11f92014-05-27 15:40:09 -07008978 /**
8979 * Size value for use with {@link #setCustomSizePreset} to show this notification
8980 * full screen.
8981 * <p>This value is only applicable for custom display notifications created using
8982 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008983 *
8984 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008985 */
Gus Prevasd7363752018-09-18 14:35:15 -04008986 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008987 public static final int SIZE_FULL_SCREEN = 5;
8988
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008989 /**
8990 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8991 * short amount of time when this notification is displayed on the screen. This
8992 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008993 *
8994 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008995 */
Gus Prevasd7363752018-09-18 14:35:15 -04008996 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008997 public static final int SCREEN_TIMEOUT_SHORT = 0;
8998
8999 /**
9000 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
9001 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04009002 *
9003 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009004 */
Gus Prevasd7363752018-09-18 14:35:15 -04009005 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009006 public static final int SCREEN_TIMEOUT_LONG = -1;
9007
Griff Hazen61a9e862014-05-22 16:05:19 -07009008 /** Notification extra which contains wearable extensions */
9009 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
9010
Pete Gastaf6781d2014-10-07 15:17:05 -04009011 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07009012 private static final String KEY_ACTIONS = "actions";
9013 private static final String KEY_FLAGS = "flags";
9014 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9015 private static final String KEY_PAGES = "pages";
9016 private static final String KEY_BACKGROUND = "background";
9017 private static final String KEY_CONTENT_ICON = "contentIcon";
9018 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9019 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9020 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9021 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9022 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009023 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009024 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009025 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009026
9027 // Flags bitwise-ored to mFlags
9028 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9029 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9030 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9031 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009032 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009033 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009034 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009035
9036 // Default value for flags integer
9037 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9038
9039 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9040 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9041
9042 private ArrayList<Action> mActions = new ArrayList<Action>();
9043 private int mFlags = DEFAULT_FLAGS;
9044 private PendingIntent mDisplayIntent;
9045 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9046 private Bitmap mBackground;
9047 private int mContentIcon;
9048 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9049 private int mContentActionIndex = UNSET_ACTION_INDEX;
9050 private int mCustomSizePreset = SIZE_DEFAULT;
9051 private int mCustomContentHeight;
9052 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009053 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009054 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009055 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009056
9057 /**
9058 * Create a {@link android.app.Notification.WearableExtender} with default
9059 * options.
9060 */
9061 public WearableExtender() {
9062 }
9063
9064 public WearableExtender(Notification notif) {
9065 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9066 if (wearableBundle != null) {
9067 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9068 if (actions != null) {
9069 mActions.addAll(actions);
9070 }
9071
9072 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9073 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9074
9075 Notification[] pages = getNotificationArrayFromBundle(
9076 wearableBundle, KEY_PAGES);
9077 if (pages != null) {
9078 Collections.addAll(mPages, pages);
9079 }
9080
9081 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9082 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9083 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9084 DEFAULT_CONTENT_ICON_GRAVITY);
9085 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9086 UNSET_ACTION_INDEX);
9087 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9088 SIZE_DEFAULT);
9089 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9090 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009091 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009092 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009093 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009094 }
9095 }
9096
9097 /**
9098 * Apply wearable extensions to a notification that is being built. This is typically
9099 * called by the {@link android.app.Notification.Builder#extend} method of
9100 * {@link android.app.Notification.Builder}.
9101 */
9102 @Override
9103 public Notification.Builder extend(Notification.Builder builder) {
9104 Bundle wearableBundle = new Bundle();
9105
9106 if (!mActions.isEmpty()) {
9107 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9108 }
9109 if (mFlags != DEFAULT_FLAGS) {
9110 wearableBundle.putInt(KEY_FLAGS, mFlags);
9111 }
9112 if (mDisplayIntent != null) {
9113 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9114 }
9115 if (!mPages.isEmpty()) {
9116 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9117 new Notification[mPages.size()]));
9118 }
9119 if (mBackground != null) {
9120 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9121 }
9122 if (mContentIcon != 0) {
9123 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9124 }
9125 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9126 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9127 }
9128 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9129 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9130 mContentActionIndex);
9131 }
9132 if (mCustomSizePreset != SIZE_DEFAULT) {
9133 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9134 }
9135 if (mCustomContentHeight != 0) {
9136 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9137 }
9138 if (mGravity != DEFAULT_GRAVITY) {
9139 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9140 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009141 if (mHintScreenTimeout != 0) {
9142 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9143 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009144 if (mDismissalId != null) {
9145 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9146 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009147 if (mBridgeTag != null) {
9148 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9149 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009150
9151 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9152 return builder;
9153 }
9154
9155 @Override
9156 public WearableExtender clone() {
9157 WearableExtender that = new WearableExtender();
9158 that.mActions = new ArrayList<Action>(this.mActions);
9159 that.mFlags = this.mFlags;
9160 that.mDisplayIntent = this.mDisplayIntent;
9161 that.mPages = new ArrayList<Notification>(this.mPages);
9162 that.mBackground = this.mBackground;
9163 that.mContentIcon = this.mContentIcon;
9164 that.mContentIconGravity = this.mContentIconGravity;
9165 that.mContentActionIndex = this.mContentActionIndex;
9166 that.mCustomSizePreset = this.mCustomSizePreset;
9167 that.mCustomContentHeight = this.mCustomContentHeight;
9168 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009169 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009170 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009171 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009172 return that;
9173 }
9174
9175 /**
9176 * Add a wearable action to this notification.
9177 *
9178 * <p>When wearable actions are added using this method, the set of actions that
9179 * show on a wearable device splits from devices that only show actions added
9180 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9181 * of which actions display on different devices.
9182 *
9183 * @param action the action to add to this notification
9184 * @return this object for method chaining
9185 * @see android.app.Notification.Action
9186 */
9187 public WearableExtender addAction(Action action) {
9188 mActions.add(action);
9189 return this;
9190 }
9191
9192 /**
9193 * Adds wearable actions to this notification.
9194 *
9195 * <p>When wearable actions are added using this method, the set of actions that
9196 * show on a wearable device splits from devices that only show actions added
9197 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9198 * of which actions display on different devices.
9199 *
9200 * @param actions the actions to add to this notification
9201 * @return this object for method chaining
9202 * @see android.app.Notification.Action
9203 */
9204 public WearableExtender addActions(List<Action> actions) {
9205 mActions.addAll(actions);
9206 return this;
9207 }
9208
9209 /**
9210 * Clear all wearable actions present on this builder.
9211 * @return this object for method chaining.
9212 * @see #addAction
9213 */
9214 public WearableExtender clearActions() {
9215 mActions.clear();
9216 return this;
9217 }
9218
9219 /**
9220 * Get the wearable actions present on this notification.
9221 */
9222 public List<Action> getActions() {
9223 return mActions;
9224 }
9225
9226 /**
9227 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009228 * this notification. The {@link PendingIntent} provided should be for an activity.
9229 *
9230 * <pre class="prettyprint">
9231 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9232 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9233 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9234 * Notification notif = new Notification.Builder(context)
9235 * .extend(new Notification.WearableExtender()
9236 * .setDisplayIntent(displayPendingIntent)
9237 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9238 * .build();</pre>
9239 *
9240 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009241 * should have an empty task affinity. It is also recommended to use the device
9242 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009243 *
9244 * <p>Example AndroidManifest.xml entry:
9245 * <pre class="prettyprint">
9246 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9247 * android:exported=&quot;true&quot;
9248 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009249 * android:taskAffinity=&quot;&quot;
9250 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009251 *
9252 * @param intent the {@link PendingIntent} for an activity
9253 * @return this object for method chaining
9254 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009255 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009256 */
Gus Prevasd7363752018-09-18 14:35:15 -04009257 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009258 public WearableExtender setDisplayIntent(PendingIntent intent) {
9259 mDisplayIntent = intent;
9260 return this;
9261 }
9262
9263 /**
9264 * Get the intent to launch inside of an activity view when displaying this
9265 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009266 *
9267 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009268 */
Gus Prevasd7363752018-09-18 14:35:15 -04009269 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009270 public PendingIntent getDisplayIntent() {
9271 return mDisplayIntent;
9272 }
9273
9274 /**
9275 * Add an additional page of content to display with this notification. The current
9276 * notification forms the first page, and pages added using this function form
9277 * subsequent pages. This field can be used to separate a notification into multiple
9278 * sections.
9279 *
9280 * @param page the notification to add as another page
9281 * @return this object for method chaining
9282 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009283 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009284 */
Gus Prevasd7363752018-09-18 14:35:15 -04009285 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009286 public WearableExtender addPage(Notification page) {
9287 mPages.add(page);
9288 return this;
9289 }
9290
9291 /**
9292 * Add additional pages of content to display with this notification. The current
9293 * notification forms the first page, and pages added using this function form
9294 * subsequent pages. This field can be used to separate a notification into multiple
9295 * sections.
9296 *
9297 * @param pages a list of notifications
9298 * @return this object for method chaining
9299 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009300 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009301 */
Gus Prevasd7363752018-09-18 14:35:15 -04009302 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009303 public WearableExtender addPages(List<Notification> pages) {
9304 mPages.addAll(pages);
9305 return this;
9306 }
9307
9308 /**
9309 * Clear all additional pages present on this builder.
9310 * @return this object for method chaining.
9311 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009312 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009313 */
Gus Prevasd7363752018-09-18 14:35:15 -04009314 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009315 public WearableExtender clearPages() {
9316 mPages.clear();
9317 return this;
9318 }
9319
9320 /**
9321 * Get the array of additional pages of content for displaying this notification. The
9322 * current notification forms the first page, and elements within this array form
9323 * subsequent pages. This field can be used to separate a notification into multiple
9324 * sections.
9325 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009326 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009327 */
Gus Prevasd7363752018-09-18 14:35:15 -04009328 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009329 public List<Notification> getPages() {
9330 return mPages;
9331 }
9332
9333 /**
9334 * Set a background image to be displayed behind the notification content.
9335 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9336 * will work with any notification style.
9337 *
9338 * @param background the background bitmap
9339 * @return this object for method chaining
9340 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009341 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009342 */
Gus Prevasd7363752018-09-18 14:35:15 -04009343 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009344 public WearableExtender setBackground(Bitmap background) {
9345 mBackground = background;
9346 return this;
9347 }
9348
9349 /**
9350 * Get a background image to be displayed behind the notification content.
9351 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9352 * will work with any notification style.
9353 *
9354 * @return the background image
9355 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009356 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009357 */
Gus Prevasd7363752018-09-18 14:35:15 -04009358 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009359 public Bitmap getBackground() {
9360 return mBackground;
9361 }
9362
9363 /**
9364 * Set an icon that goes with the content of this notification.
9365 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009366 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009367 public WearableExtender setContentIcon(int icon) {
9368 mContentIcon = icon;
9369 return this;
9370 }
9371
9372 /**
9373 * Get an icon that goes with the content of this notification.
9374 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009375 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009376 public int getContentIcon() {
9377 return mContentIcon;
9378 }
9379
9380 /**
9381 * Set the gravity that the content icon should have within the notification display.
9382 * Supported values include {@link android.view.Gravity#START} and
9383 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9384 * @see #setContentIcon
9385 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009386 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009387 public WearableExtender setContentIconGravity(int contentIconGravity) {
9388 mContentIconGravity = contentIconGravity;
9389 return this;
9390 }
9391
9392 /**
9393 * Get the gravity that the content icon should have within the notification display.
9394 * Supported values include {@link android.view.Gravity#START} and
9395 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9396 * @see #getContentIcon
9397 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009398 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009399 public int getContentIconGravity() {
9400 return mContentIconGravity;
9401 }
9402
9403 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009404 * Set an action from this notification's actions as the primary action. If the action has a
9405 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9406 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009407 *
Gus Prevasd7363752018-09-18 14:35:15 -04009408 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009409 * If wearable actions were added to the main notification, this index
9410 * will apply to that list, otherwise it will apply to the regular
9411 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009412 */
9413 public WearableExtender setContentAction(int actionIndex) {
9414 mContentActionIndex = actionIndex;
9415 return this;
9416 }
9417
9418 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009419 * Get the index of the notification action, if any, that was specified as the primary
9420 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009421 *
9422 * <p>If wearable specific actions were added to the main notification, this index will
9423 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009424 *
9425 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009426 */
9427 public int getContentAction() {
9428 return mContentActionIndex;
9429 }
9430
9431 /**
9432 * Set the gravity that this notification should have within the available viewport space.
9433 * Supported values include {@link android.view.Gravity#TOP},
9434 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9435 * The default value is {@link android.view.Gravity#BOTTOM}.
9436 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009437 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009438 public WearableExtender setGravity(int gravity) {
9439 mGravity = gravity;
9440 return this;
9441 }
9442
9443 /**
9444 * Get the gravity that this notification should have within the available viewport space.
9445 * Supported values include {@link android.view.Gravity#TOP},
9446 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9447 * The default value is {@link android.view.Gravity#BOTTOM}.
9448 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009449 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009450 public int getGravity() {
9451 return mGravity;
9452 }
9453
9454 /**
9455 * Set the custom size preset for the display of this notification out of the available
9456 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9457 * {@link #SIZE_LARGE}.
9458 * <p>Some custom size presets are only applicable for custom display notifications created
9459 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9460 * documentation for the preset in question. See also
9461 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9462 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009463 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009464 public WearableExtender setCustomSizePreset(int sizePreset) {
9465 mCustomSizePreset = sizePreset;
9466 return this;
9467 }
9468
9469 /**
9470 * Get the custom size preset for the display of this notification out of the available
9471 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9472 * {@link #SIZE_LARGE}.
9473 * <p>Some custom size presets are only applicable for custom display notifications created
9474 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9475 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9476 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009477 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009478 public int getCustomSizePreset() {
9479 return mCustomSizePreset;
9480 }
9481
9482 /**
9483 * Set the custom height in pixels for the display of this notification's content.
9484 * <p>This option is only available for custom display notifications created
9485 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9486 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9487 * {@link #getCustomContentHeight}.
9488 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009489 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009490 public WearableExtender setCustomContentHeight(int height) {
9491 mCustomContentHeight = height;
9492 return this;
9493 }
9494
9495 /**
9496 * Get the custom height in pixels for the display of this notification's content.
9497 * <p>This option is only available for custom display notifications created
9498 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9499 * {@link #setCustomContentHeight}.
9500 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009501 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009502 public int getCustomContentHeight() {
9503 return mCustomContentHeight;
9504 }
9505
9506 /**
9507 * Set whether the scrolling position for the contents of this notification should start
9508 * at the bottom of the contents instead of the top when the contents are too long to
9509 * display within the screen. Default is false (start scroll at the top).
9510 */
9511 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9512 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9513 return this;
9514 }
9515
9516 /**
9517 * Get whether the scrolling position for the contents of this notification should start
9518 * at the bottom of the contents instead of the top when the contents are too long to
9519 * display within the screen. Default is false (start scroll at the top).
9520 */
9521 public boolean getStartScrollBottom() {
9522 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9523 }
9524
9525 /**
9526 * Set whether the content intent is available when the wearable device is not connected
9527 * to a companion device. The user can still trigger this intent when the wearable device
9528 * is offline, but a visual hint will indicate that the content intent may not be available.
9529 * Defaults to true.
9530 */
9531 public WearableExtender setContentIntentAvailableOffline(
9532 boolean contentIntentAvailableOffline) {
9533 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9534 return this;
9535 }
9536
9537 /**
9538 * Get whether the content intent is available when the wearable device is not connected
9539 * to a companion device. The user can still trigger this intent when the wearable device
9540 * is offline, but a visual hint will indicate that the content intent may not be available.
9541 * Defaults to true.
9542 */
9543 public boolean getContentIntentAvailableOffline() {
9544 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9545 }
9546
9547 /**
9548 * Set a hint that this notification's icon should not be displayed.
9549 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9550 * @return this object for method chaining
9551 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009552 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009553 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9554 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9555 return this;
9556 }
9557
9558 /**
9559 * Get a hint that this notification's icon should not be displayed.
9560 * @return {@code true} if this icon should not be displayed, false otherwise.
9561 * The default value is {@code false} if this was never set.
9562 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009563 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009564 public boolean getHintHideIcon() {
9565 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9566 }
9567
9568 /**
9569 * Set a visual hint that only the background image of this notification should be
9570 * displayed, and other semantic content should be hidden. This hint is only applicable
9571 * to sub-pages added using {@link #addPage}.
9572 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009573 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009574 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9575 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9576 return this;
9577 }
9578
9579 /**
9580 * Get a visual hint that only the background image of this notification should be
9581 * displayed, and other semantic content should be hidden. This hint is only applicable
9582 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9583 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009584 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009585 public boolean getHintShowBackgroundOnly() {
9586 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9587 }
9588
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009589 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009590 * Set a hint that this notification's background should not be clipped if possible,
9591 * and should instead be resized to fully display on the screen, retaining the aspect
9592 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009593 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9594 * @return this object for method chaining
9595 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009596 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009597 public WearableExtender setHintAvoidBackgroundClipping(
9598 boolean hintAvoidBackgroundClipping) {
9599 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9600 return this;
9601 }
9602
9603 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009604 * Get a hint that this notification's background should not be clipped if possible,
9605 * and should instead be resized to fully display on the screen, retaining the aspect
9606 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009607 * @return {@code true} if it's ok if the background is clipped on the screen, false
9608 * otherwise. The default value is {@code false} if this was never set.
9609 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009610 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009611 public boolean getHintAvoidBackgroundClipping() {
9612 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9613 }
9614
9615 /**
9616 * Set a hint that the screen should remain on for at least this duration when
9617 * this notification is displayed on the screen.
9618 * @param timeout The requested screen timeout in milliseconds. Can also be either
9619 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9620 * @return this object for method chaining
9621 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009622 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009623 public WearableExtender setHintScreenTimeout(int timeout) {
9624 mHintScreenTimeout = timeout;
9625 return this;
9626 }
9627
9628 /**
9629 * Get the duration, in milliseconds, that the screen should remain on for
9630 * when this notification is displayed.
9631 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9632 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9633 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009634 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009635 public int getHintScreenTimeout() {
9636 return mHintScreenTimeout;
9637 }
9638
Alex Hills9ab3a232016-04-05 14:54:56 -04009639 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009640 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9641 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9642 * qr codes, as well as other simple black-and-white tickets.
9643 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9644 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009645 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009646 */
Gus Prevasd7363752018-09-18 14:35:15 -04009647 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009648 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9649 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9650 return this;
9651 }
9652
9653 /**
9654 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9655 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9656 * qr codes, as well as other simple black-and-white tickets.
9657 * @return {@code true} if it should be displayed in ambient, false otherwise
9658 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009659 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009660 */
Gus Prevasd7363752018-09-18 14:35:15 -04009661 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009662 public boolean getHintAmbientBigPicture() {
9663 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9664 }
9665
9666 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009667 * Set a hint that this notification's content intent will launch an {@link Activity}
9668 * directly, telling the platform that it can generate the appropriate transitions.
9669 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9670 * an activity and transitions should be generated, false otherwise.
9671 * @return this object for method chaining
9672 */
9673 public WearableExtender setHintContentIntentLaunchesActivity(
9674 boolean hintContentIntentLaunchesActivity) {
9675 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9676 return this;
9677 }
9678
9679 /**
9680 * Get a hint that this notification's content intent will launch an {@link Activity}
9681 * directly, telling the platform that it can generate the appropriate transitions
9682 * @return {@code true} if the content intent will launch an activity and transitions should
9683 * be generated, false otherwise. The default value is {@code false} if this was never set.
9684 */
9685 public boolean getHintContentIntentLaunchesActivity() {
9686 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9687 }
9688
Nadia Benbernou948627e2016-04-14 14:41:08 -04009689 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009690 * Sets the dismissal id for this notification. If a notification is posted with a
9691 * dismissal id, then when that notification is canceled, notifications on other wearables
9692 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009693 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009694 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009695 * @param dismissalId the dismissal id of the notification.
9696 * @return this object for method chaining
9697 */
9698 public WearableExtender setDismissalId(String dismissalId) {
9699 mDismissalId = dismissalId;
9700 return this;
9701 }
9702
9703 /**
9704 * Returns the dismissal id of the notification.
9705 * @return the dismissal id of the notification or null if it has not been set.
9706 */
9707 public String getDismissalId() {
9708 return mDismissalId;
9709 }
9710
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009711 /**
9712 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9713 * posted from a phone to provide finer-grained control on what notifications are bridged
9714 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9715 * Features to Notifications</a> for more information.
9716 * @param bridgeTag the bridge tag of the notification.
9717 * @return this object for method chaining
9718 */
9719 public WearableExtender setBridgeTag(String bridgeTag) {
9720 mBridgeTag = bridgeTag;
9721 return this;
9722 }
9723
9724 /**
9725 * Returns the bridge tag of the notification.
9726 * @return the bridge tag or null if not present.
9727 */
9728 public String getBridgeTag() {
9729 return mBridgeTag;
9730 }
9731
Griff Hazen61a9e862014-05-22 16:05:19 -07009732 private void setFlag(int mask, boolean value) {
9733 if (value) {
9734 mFlags |= mask;
9735 } else {
9736 mFlags &= ~mask;
9737 }
9738 }
9739 }
9740
9741 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009742 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9743 * with car extensions:
9744 *
9745 * <ol>
9746 * <li>Create an {@link Notification.Builder}, setting any desired
9747 * properties.
9748 * <li>Create a {@link CarExtender}.
9749 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9750 * {@link CarExtender}.
9751 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9752 * to apply the extensions to a notification.
9753 * </ol>
9754 *
9755 * <pre class="prettyprint">
9756 * Notification notification = new Notification.Builder(context)
9757 * ...
9758 * .extend(new CarExtender()
9759 * .set*(...))
9760 * .build();
9761 * </pre>
9762 *
9763 * <p>Car extensions can be accessed on an existing notification by using the
9764 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9765 * to access values.
9766 */
9767 public static final class CarExtender implements Extender {
9768 private static final String TAG = "CarExtender";
9769
9770 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9771 private static final String EXTRA_LARGE_ICON = "large_icon";
9772 private static final String EXTRA_CONVERSATION = "car_conversation";
9773 private static final String EXTRA_COLOR = "app_color";
9774
9775 private Bitmap mLargeIcon;
9776 private UnreadConversation mUnreadConversation;
9777 private int mColor = Notification.COLOR_DEFAULT;
9778
9779 /**
9780 * Create a {@link CarExtender} with default options.
9781 */
9782 public CarExtender() {
9783 }
9784
9785 /**
9786 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9787 *
9788 * @param notif The notification from which to copy options.
9789 */
9790 public CarExtender(Notification notif) {
9791 Bundle carBundle = notif.extras == null ?
9792 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9793 if (carBundle != null) {
9794 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9795 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9796
9797 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9798 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9799 }
9800 }
9801
9802 /**
9803 * Apply car extensions to a notification that is being built. This is typically called by
9804 * the {@link Notification.Builder#extend(Notification.Extender)}
9805 * method of {@link Notification.Builder}.
9806 */
9807 @Override
9808 public Notification.Builder extend(Notification.Builder builder) {
9809 Bundle carExtensions = new Bundle();
9810
9811 if (mLargeIcon != null) {
9812 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9813 }
9814 if (mColor != Notification.COLOR_DEFAULT) {
9815 carExtensions.putInt(EXTRA_COLOR, mColor);
9816 }
9817
9818 if (mUnreadConversation != null) {
9819 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9820 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9821 }
9822
9823 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9824 return builder;
9825 }
9826
9827 /**
9828 * Sets the accent color to use when Android Auto presents the notification.
9829 *
9830 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9831 * to accent the displayed notification. However, not all colors are acceptable in an
9832 * automotive setting. This method can be used to override the color provided in the
9833 * notification in such a situation.
9834 */
Tor Norbye80756e32015-03-02 09:39:27 -08009835 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009836 mColor = color;
9837 return this;
9838 }
9839
9840 /**
9841 * Gets the accent color.
9842 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009843 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009844 */
Tor Norbye80756e32015-03-02 09:39:27 -08009845 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009846 public int getColor() {
9847 return mColor;
9848 }
9849
9850 /**
9851 * Sets the large icon of the car notification.
9852 *
9853 * If no large icon is set in the extender, Android Auto will display the icon
9854 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9855 *
9856 * @param largeIcon The large icon to use in the car notification.
9857 * @return This object for method chaining.
9858 */
9859 public CarExtender setLargeIcon(Bitmap largeIcon) {
9860 mLargeIcon = largeIcon;
9861 return this;
9862 }
9863
9864 /**
9865 * Gets the large icon used in this car notification, or null if no icon has been set.
9866 *
9867 * @return The large icon for the car notification.
9868 * @see CarExtender#setLargeIcon
9869 */
9870 public Bitmap getLargeIcon() {
9871 return mLargeIcon;
9872 }
9873
9874 /**
9875 * Sets the unread conversation in a message notification.
9876 *
9877 * @param unreadConversation The unread part of the conversation this notification conveys.
9878 * @return This object for method chaining.
9879 */
9880 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9881 mUnreadConversation = unreadConversation;
9882 return this;
9883 }
9884
9885 /**
9886 * Returns the unread conversation conveyed by this notification.
9887 * @see #setUnreadConversation(UnreadConversation)
9888 */
9889 public UnreadConversation getUnreadConversation() {
9890 return mUnreadConversation;
9891 }
9892
9893 /**
9894 * A class which holds the unread messages from a conversation.
9895 */
9896 public static class UnreadConversation {
9897 private static final String KEY_AUTHOR = "author";
9898 private static final String KEY_TEXT = "text";
9899 private static final String KEY_MESSAGES = "messages";
9900 private static final String KEY_REMOTE_INPUT = "remote_input";
9901 private static final String KEY_ON_REPLY = "on_reply";
9902 private static final String KEY_ON_READ = "on_read";
9903 private static final String KEY_PARTICIPANTS = "participants";
9904 private static final String KEY_TIMESTAMP = "timestamp";
9905
9906 private final String[] mMessages;
9907 private final RemoteInput mRemoteInput;
9908 private final PendingIntent mReplyPendingIntent;
9909 private final PendingIntent mReadPendingIntent;
9910 private final String[] mParticipants;
9911 private final long mLatestTimestamp;
9912
9913 UnreadConversation(String[] messages, RemoteInput remoteInput,
9914 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9915 String[] participants, long latestTimestamp) {
9916 mMessages = messages;
9917 mRemoteInput = remoteInput;
9918 mReadPendingIntent = readPendingIntent;
9919 mReplyPendingIntent = replyPendingIntent;
9920 mParticipants = participants;
9921 mLatestTimestamp = latestTimestamp;
9922 }
9923
9924 /**
9925 * Gets the list of messages conveyed by this notification.
9926 */
9927 public String[] getMessages() {
9928 return mMessages;
9929 }
9930
9931 /**
9932 * Gets the remote input that will be used to convey the response to a message list, or
9933 * null if no such remote input exists.
9934 */
9935 public RemoteInput getRemoteInput() {
9936 return mRemoteInput;
9937 }
9938
9939 /**
9940 * Gets the pending intent that will be triggered when the user replies to this
9941 * notification.
9942 */
9943 public PendingIntent getReplyPendingIntent() {
9944 return mReplyPendingIntent;
9945 }
9946
9947 /**
9948 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9949 * in this object's message list.
9950 */
9951 public PendingIntent getReadPendingIntent() {
9952 return mReadPendingIntent;
9953 }
9954
9955 /**
9956 * Gets the participants in the conversation.
9957 */
9958 public String[] getParticipants() {
9959 return mParticipants;
9960 }
9961
9962 /**
9963 * Gets the firs participant in the conversation.
9964 */
9965 public String getParticipant() {
9966 return mParticipants.length > 0 ? mParticipants[0] : null;
9967 }
9968
9969 /**
9970 * Gets the timestamp of the conversation.
9971 */
9972 public long getLatestTimestamp() {
9973 return mLatestTimestamp;
9974 }
9975
9976 Bundle getBundleForUnreadConversation() {
9977 Bundle b = new Bundle();
9978 String author = null;
9979 if (mParticipants != null && mParticipants.length > 1) {
9980 author = mParticipants[0];
9981 }
9982 Parcelable[] messages = new Parcelable[mMessages.length];
9983 for (int i = 0; i < messages.length; i++) {
9984 Bundle m = new Bundle();
9985 m.putString(KEY_TEXT, mMessages[i]);
9986 m.putString(KEY_AUTHOR, author);
9987 messages[i] = m;
9988 }
9989 b.putParcelableArray(KEY_MESSAGES, messages);
9990 if (mRemoteInput != null) {
9991 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9992 }
9993 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9994 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9995 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9996 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9997 return b;
9998 }
9999
10000 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
10001 if (b == null) {
10002 return null;
10003 }
10004 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
10005 String[] messages = null;
10006 if (parcelableMessages != null) {
10007 String[] tmp = new String[parcelableMessages.length];
10008 boolean success = true;
10009 for (int i = 0; i < tmp.length; i++) {
10010 if (!(parcelableMessages[i] instanceof Bundle)) {
10011 success = false;
10012 break;
10013 }
10014 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10015 if (tmp[i] == null) {
10016 success = false;
10017 break;
10018 }
10019 }
10020 if (success) {
10021 messages = tmp;
10022 } else {
10023 return null;
10024 }
10025 }
10026
10027 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10028 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10029
10030 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10031
10032 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10033 if (participants == null || participants.length != 1) {
10034 return null;
10035 }
10036
10037 return new UnreadConversation(messages,
10038 remoteInput,
10039 onReply,
10040 onRead,
10041 participants, b.getLong(KEY_TIMESTAMP));
10042 }
10043 };
10044
10045 /**
10046 * Builder class for {@link CarExtender.UnreadConversation} objects.
10047 */
10048 public static class Builder {
10049 private final List<String> mMessages = new ArrayList<String>();
10050 private final String mParticipant;
10051 private RemoteInput mRemoteInput;
10052 private PendingIntent mReadPendingIntent;
10053 private PendingIntent mReplyPendingIntent;
10054 private long mLatestTimestamp;
10055
10056 /**
10057 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10058 *
10059 * @param name The name of the other participant in the conversation.
10060 */
10061 public Builder(String name) {
10062 mParticipant = name;
10063 }
10064
10065 /**
10066 * Appends a new unread message to the list of messages for this conversation.
10067 *
10068 * The messages should be added from oldest to newest.
10069 *
10070 * @param message The text of the new unread message.
10071 * @return This object for method chaining.
10072 */
10073 public Builder addMessage(String message) {
10074 mMessages.add(message);
10075 return this;
10076 }
10077
10078 /**
10079 * Sets the pending intent and remote input which will convey the reply to this
10080 * notification.
10081 *
10082 * @param pendingIntent The pending intent which will be triggered on a reply.
10083 * @param remoteInput The remote input parcelable which will carry the reply.
10084 * @return This object for method chaining.
10085 *
10086 * @see CarExtender.UnreadConversation#getRemoteInput
10087 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10088 */
10089 public Builder setReplyAction(
10090 PendingIntent pendingIntent, RemoteInput remoteInput) {
10091 mRemoteInput = remoteInput;
10092 mReplyPendingIntent = pendingIntent;
10093
10094 return this;
10095 }
10096
10097 /**
10098 * Sets the pending intent that will be sent once the messages in this notification
10099 * are read.
10100 *
10101 * @param pendingIntent The pending intent to use.
10102 * @return This object for method chaining.
10103 */
10104 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10105 mReadPendingIntent = pendingIntent;
10106 return this;
10107 }
10108
10109 /**
10110 * Sets the timestamp of the most recent message in an unread conversation.
10111 *
10112 * If a messaging notification has been posted by your application and has not
10113 * yet been cancelled, posting a later notification with the same id and tag
10114 * but without a newer timestamp may result in Android Auto not displaying a
10115 * heads up notification for the later notification.
10116 *
10117 * @param timestamp The timestamp of the most recent message in the conversation.
10118 * @return This object for method chaining.
10119 */
10120 public Builder setLatestTimestamp(long timestamp) {
10121 mLatestTimestamp = timestamp;
10122 return this;
10123 }
10124
10125 /**
10126 * Builds a new unread conversation object.
10127 *
10128 * @return The new unread conversation object.
10129 */
10130 public UnreadConversation build() {
10131 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10132 String[] participants = { mParticipant };
10133 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10134 mReadPendingIntent, participants, mLatestTimestamp);
10135 }
10136 }
10137 }
10138
10139 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010140 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10141 * with a TV extension:
10142 *
10143 * <ol>
10144 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10145 * <li>Create a {@link TvExtender}.
10146 * <li>Set TV-specific properties using the {@code set} methods of
10147 * {@link TvExtender}.
10148 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10149 * to apply the extension to a notification.
10150 * </ol>
10151 *
10152 * <pre class="prettyprint">
10153 * Notification notification = new Notification.Builder(context)
10154 * ...
10155 * .extend(new TvExtender()
10156 * .set*(...))
10157 * .build();
10158 * </pre>
10159 *
10160 * <p>TV extensions can be accessed on an existing notification by using the
10161 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10162 * to access values.
10163 *
10164 * @hide
10165 */
10166 @SystemApi
10167 public static final class TvExtender implements Extender {
10168 private static final String TAG = "TvExtender";
10169
10170 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10171 private static final String EXTRA_FLAGS = "flags";
10172 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10173 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010174 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010175 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010176
10177 // Flags bitwise-ored to mFlags
10178 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10179
10180 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010181 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010182 private PendingIntent mContentIntent;
10183 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010184 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010185
10186 /**
10187 * Create a {@link TvExtender} with default options.
10188 */
10189 public TvExtender() {
10190 mFlags = FLAG_AVAILABLE_ON_TV;
10191 }
10192
10193 /**
10194 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10195 *
10196 * @param notif The notification from which to copy options.
10197 */
10198 public TvExtender(Notification notif) {
10199 Bundle bundle = notif.extras == null ?
10200 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10201 if (bundle != null) {
10202 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010203 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010204 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010205 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10206 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10207 }
10208 }
10209
10210 /**
10211 * Apply a TV extension to a notification that is being built. This is typically called by
10212 * the {@link Notification.Builder#extend(Notification.Extender)}
10213 * method of {@link Notification.Builder}.
10214 */
10215 @Override
10216 public Notification.Builder extend(Notification.Builder builder) {
10217 Bundle bundle = new Bundle();
10218
10219 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010220 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010221 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010222 if (mContentIntent != null) {
10223 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10224 }
10225
10226 if (mDeleteIntent != null) {
10227 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10228 }
10229
10230 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10231 return builder;
10232 }
10233
10234 /**
10235 * Returns true if this notification should be shown on TV. This method return true
10236 * if the notification was extended with a TvExtender.
10237 */
10238 public boolean isAvailableOnTv() {
10239 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10240 }
10241
10242 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010243 * Specifies the channel the notification should be delivered on when shown on TV.
10244 * It can be different from the channel that the notification is delivered to when
10245 * posting on a non-TV device.
10246 */
10247 public TvExtender setChannel(String channelId) {
10248 mChannelId = channelId;
10249 return this;
10250 }
10251
10252 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010253 * Specifies the channel the notification should be delivered on when shown on TV.
10254 * It can be different from the channel that the notification is delivered to when
10255 * posting on a non-TV device.
10256 */
10257 public TvExtender setChannelId(String channelId) {
10258 mChannelId = channelId;
10259 return this;
10260 }
10261
Jeff Sharkey000ce802017-04-29 13:13:27 -060010262 /** @removed */
10263 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010264 public String getChannel() {
10265 return mChannelId;
10266 }
10267
10268 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010269 * Returns the id of the channel this notification posts to on TV.
10270 */
10271 public String getChannelId() {
10272 return mChannelId;
10273 }
10274
10275 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010276 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10277 * If provided, it is used instead of the content intent specified
10278 * at the level of Notification.
10279 */
10280 public TvExtender setContentIntent(PendingIntent intent) {
10281 mContentIntent = intent;
10282 return this;
10283 }
10284
10285 /**
10286 * Returns the TV-specific content intent. If this method returns null, the
10287 * main content intent on the notification should be used.
10288 *
10289 * @see {@link Notification#contentIntent}
10290 */
10291 public PendingIntent getContentIntent() {
10292 return mContentIntent;
10293 }
10294
10295 /**
10296 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10297 * by the user on TV. If provided, it is used instead of the delete intent specified
10298 * at the level of Notification.
10299 */
10300 public TvExtender setDeleteIntent(PendingIntent intent) {
10301 mDeleteIntent = intent;
10302 return this;
10303 }
10304
10305 /**
10306 * Returns the TV-specific delete intent. If this method returns null, the
10307 * main delete intent on the notification should be used.
10308 *
10309 * @see {@link Notification#deleteIntent}
10310 */
10311 public PendingIntent getDeleteIntent() {
10312 return mDeleteIntent;
10313 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010314
10315 /**
10316 * Specifies whether this notification should suppress showing a message over top of apps
10317 * outside of the launcher.
10318 */
10319 public TvExtender setSuppressShowOverApps(boolean suppress) {
10320 mSuppressShowOverApps = suppress;
10321 return this;
10322 }
10323
10324 /**
10325 * Returns true if this notification should not show messages over top of apps
10326 * outside of the launcher.
10327 */
10328 public boolean getSuppressShowOverApps() {
10329 return mSuppressShowOverApps;
10330 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010331 }
10332
10333 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010334 * Get an array of Notification objects from a parcelable array bundle field.
10335 * Update the bundle to have a typed array so fetches in the future don't need
10336 * to do an array copy.
10337 */
10338 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10339 Parcelable[] array = bundle.getParcelableArray(key);
10340 if (array instanceof Notification[] || array == null) {
10341 return (Notification[]) array;
10342 }
10343 Notification[] typedArray = Arrays.copyOf(array, array.length,
10344 Notification[].class);
10345 bundle.putParcelableArray(key, typedArray);
10346 return typedArray;
10347 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010348
10349 private static class BuilderRemoteViews extends RemoteViews {
10350 public BuilderRemoteViews(Parcel parcel) {
10351 super(parcel);
10352 }
10353
Kenny Guy77320062014-08-27 21:37:15 +010010354 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10355 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010356 }
10357
10358 @Override
10359 public BuilderRemoteViews clone() {
10360 Parcel p = Parcel.obtain();
10361 writeToParcel(p, 0);
10362 p.setDataPosition(0);
10363 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10364 p.recycle();
10365 return brv;
10366 }
10367 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010368
Selim Cinek384804b2018-04-18 14:31:07 +080010369 /**
10370 * A result object where information about the template that was created is saved.
10371 */
10372 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010373 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010374 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010375
10376 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010377 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010378 * with the right actions.
10379 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010380 public int getIconMarginEnd() {
10381 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010382 }
10383
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010384 /**
10385 * Is the icon container visible on the right size because of the reply button or the
10386 * right icon.
10387 */
10388 public boolean isRightIconContainerVisible() {
10389 return mRightIconContainerVisible;
10390 }
10391
Selim Cinek1c72fa02018-04-23 18:00:54 +080010392 public void setIconMarginEnd(int iconMarginEnd) {
10393 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010394 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010395
10396 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10397 mRightIconContainerVisible = iconContainerVisible;
10398 }
Selim Cinek384804b2018-04-18 14:31:07 +080010399 }
10400
Adrian Roos70d7aa32017-01-11 15:39:06 -080010401 private static class StandardTemplateParams {
10402 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010403 CharSequence title;
10404 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010405 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010406 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010407 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010408 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010409 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010410 boolean allowColorization = true;
10411 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010412
10413 final StandardTemplateParams reset() {
10414 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010415 title = null;
10416 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010417 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010418 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010419 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010420 allowColorization = true;
10421 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010422 return this;
10423 }
10424
10425 final StandardTemplateParams hasProgress(boolean hasProgress) {
10426 this.hasProgress = hasProgress;
10427 return this;
10428 }
10429
10430 final StandardTemplateParams title(CharSequence title) {
10431 this.title = title;
10432 return this;
10433 }
10434
10435 final StandardTemplateParams text(CharSequence text) {
10436 this.text = text;
10437 return this;
10438 }
10439
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010440 final StandardTemplateParams summaryText(CharSequence text) {
10441 this.summaryText = text;
10442 return this;
10443 }
10444
Selim Cinekafeed292017-12-12 17:32:44 -080010445 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10446 this.headerTextSecondary = text;
10447 return this;
10448 }
10449
Selim Cinek384804b2018-04-18 14:31:07 +080010450 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10451 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010452 return this;
10453 }
10454
Selim Cinek384804b2018-04-18 14:31:07 +080010455 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10456 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010457 return this;
10458 }
10459
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010460 final StandardTemplateParams disallowColorization() {
10461 this.allowColorization = false;
10462 return this;
10463 }
10464
10465 final StandardTemplateParams forceDefaultColor() {
10466 this.forceDefaultColor = true;
10467 return this;
10468 }
10469
Adrian Roos70d7aa32017-01-11 15:39:06 -080010470 final StandardTemplateParams fillTextsFrom(Builder b) {
10471 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010472 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010473
Lucas Dupin06c5e642017-09-13 16:34:58 -070010474 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010475 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010476 text = extras.getCharSequence(EXTRA_TEXT);
10477 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010478 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010479 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010480 return this;
10481 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010482
10483 /**
10484 * Set the maximum lines of remote input history lines allowed.
10485 * @param maxRemoteInputHistory The number of lines.
10486 * @return The builder for method chaining.
10487 */
10488 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10489 this.maxRemoteInputHistory = maxRemoteInputHistory;
10490 return this;
10491 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010493}