blob: a4c98e88cbc7832b5574259dcc7a7f04e86ed3ee [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 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -0400621 * Bit to be bitswised-ored into the {@link #flags} field that should be
622 * set by the system if this notification is showing as a bubble.
Mady Mellor65dcaa92019-04-03 12:21:44 -0700623 *
Julia Tuttle28fb1a92019-05-03 18:07:45 -0400624 * Applications cannot set this flag directly; they should instead call
625 * {@link Notification.Builder#setBubbleMetadata(BubbleMetadata)} to
626 * request that a notification be displayed as a bubble, and then check
627 * this flag to see whether that request was honored by the system.
Mady Mellor7eb18ef2019-03-27 14:03:46 -0700628 */
629 public static final int FLAG_BUBBLE = 0x00001000;
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public int flags;
632
Tor Norbyed9273d62013-05-30 15:59:53 -0700633 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700634 @IntDef(prefix = { "PRIORITY_" }, value = {
635 PRIORITY_DEFAULT,
636 PRIORITY_LOW,
637 PRIORITY_MIN,
638 PRIORITY_HIGH,
639 PRIORITY_MAX
640 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700641 @Retention(RetentionPolicy.SOURCE)
642 public @interface Priority {}
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 * Default notification {@link #priority}. If your application does not prioritize its own
646 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500647 *
648 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500650 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 public static final int PRIORITY_DEFAULT = 0;
652
653 /**
654 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
655 * items smaller, or at a different position in the list, compared with your app's
656 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500657 *
658 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500660 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661 public static final int PRIORITY_LOW = -1;
662
663 /**
664 * Lowest {@link #priority}; these items might not be shown to the user except under special
665 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500666 *
667 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500668 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500669 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500670 public static final int PRIORITY_MIN = -2;
671
672 /**
673 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
674 * show these items larger, or at a different position in notification lists, compared with
675 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500676 *
677 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500678 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500679 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500680 public static final int PRIORITY_HIGH = 1;
681
682 /**
683 * Highest {@link #priority}, for your application's most important items that require the
684 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500685 *
686 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500687 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500688 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500689 public static final int PRIORITY_MAX = 2;
690
691 /**
692 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800693 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500694 * Priority is an indication of how much of the user's valuable attention should be consumed by
695 * this notification. Low-priority notifications may be hidden from the user in certain
696 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500697 * system will make a determination about how to interpret this priority when presenting
698 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400699 *
700 * <p>
701 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
702 * as a heads-up notification.
703 * </p>
704 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500705 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500706 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700707 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500708 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500709 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800710
Dan Sandler26e81cf2014-05-06 10:01:27 -0400711 /**
712 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
713 * to be applied by the standard Style templates when presenting this notification.
714 *
715 * The current template design constructs a colorful header image by overlaying the
716 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
717 * ignored.
718 */
Tor Norbye80756e32015-03-02 09:39:27 -0800719 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400720 public int color = COLOR_DEFAULT;
721
722 /**
723 * Special value of {@link #color} telling the system not to decorate this notification with
724 * any special color but instead use default colors when presenting this notification.
725 */
Tor Norbye80756e32015-03-02 09:39:27 -0800726 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400727 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600728
729 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800730 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800731 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800732 */
733 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800734 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800735
736 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700737 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
738 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600739 * lockscreen).
740 *
741 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
742 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
743 * shown in all situations, but the contents are only available if the device is unlocked for
744 * the appropriate user.
745 *
746 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
747 * can be read even in an "insecure" context (that is, above a secure lockscreen).
748 * To modify the public version of this notification—for example, to redact some portions—see
749 * {@link Builder#setPublicVersion(Notification)}.
750 *
751 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
752 * and ticker until the user has bypassed the lockscreen.
753 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600754 public @Visibility int visibility;
755
756 /** @hide */
757 @IntDef(prefix = { "VISIBILITY_" }, value = {
758 VISIBILITY_PUBLIC,
759 VISIBILITY_PRIVATE,
760 VISIBILITY_SECRET,
761 })
762 @Retention(RetentionPolicy.SOURCE)
763 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600764
Griff Hazenfc3922d2014-08-20 11:56:44 -0700765 /**
766 * Notification visibility: Show this notification in its entirety on all lockscreens.
767 *
768 * {@see #visibility}
769 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600770 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700771
772 /**
773 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
774 * private information on secure lockscreens.
775 *
776 * {@see #visibility}
777 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600778 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700779
780 /**
781 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
782 *
783 * {@see #visibility}
784 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600785 public static final int VISIBILITY_SECRET = -1;
786
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700793 * Notification category: map turn-by-turn navigation.
794 */
795 public static final String CATEGORY_NAVIGATION = "navigation";
796
797 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400800 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801
802 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500804 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400805 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500806
807 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400808 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500809 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400810 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500811
812 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400813 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500814 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400815 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500816
817 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400818 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500819 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400820 public static final String CATEGORY_ALARM = "alarm";
821
822 /**
823 * Notification category: progress of a long-running background operation.
824 */
825 public static final String CATEGORY_PROGRESS = "progress";
826
827 /**
828 * Notification category: social network or sharing update.
829 */
830 public static final String CATEGORY_SOCIAL = "social";
831
832 /**
833 * Notification category: error in background operation or authentication status.
834 */
835 public static final String CATEGORY_ERROR = "err";
836
837 /**
838 * Notification category: media transport control for playback.
839 */
840 public static final String CATEGORY_TRANSPORT = "transport";
841
842 /**
843 * Notification category: system or device status update. Reserved for system use.
844 */
845 public static final String CATEGORY_SYSTEM = "sys";
846
847 /**
848 * Notification category: indication of running background service.
849 */
850 public static final String CATEGORY_SERVICE = "service";
851
852 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400853 * Notification category: a specific, timely recommendation for a single thing.
854 * For example, a news app might want to recommend a news story it believes the user will
855 * want to read next.
856 */
857 public static final String CATEGORY_RECOMMENDATION = "recommendation";
858
859 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400860 * Notification category: ongoing information about device or contextual status.
861 */
862 public static final String CATEGORY_STATUS = "status";
863
864 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400865 * Notification category: user-scheduled reminder.
866 */
867 public static final String CATEGORY_REMINDER = "reminder";
868
869 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700870 * Notification category: extreme car emergencies.
871 * @hide
872 */
873 @SystemApi
874 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
875
876 /**
877 * Notification category: car warnings.
878 * @hide
879 */
880 @SystemApi
881 public static final String CATEGORY_CAR_WARNING = "car_warning";
882
883 /**
884 * Notification category: general car system information.
885 * @hide
886 */
887 @SystemApi
888 public static final String CATEGORY_CAR_INFORMATION = "car_information";
889
890 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400891 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
892 * that best describes this Notification. May be used by the system for ranking and filtering.
893 */
894 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500895
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100896 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700897 private String mGroupKey;
898
899 /**
900 * Get the key used to group this notification into a cluster or stack
901 * with other notifications on devices which support such rendering.
902 */
903 public String getGroup() {
904 return mGroupKey;
905 }
906
907 private String mSortKey;
908
909 /**
910 * Get a sort key that orders this notification among other notifications from the
911 * same package. This can be useful if an external sort was already applied and an app
912 * would like to preserve this. Notifications will be sorted lexicographically using this
913 * value, although providing different priorities in addition to providing sort key may
914 * cause this value to be ignored.
915 *
916 * <p>This sort key can also be used to order members of a notification group. See
917 * {@link Builder#setGroup}.
918 *
919 * @see String#compareTo(String)
920 */
921 public String getSortKey() {
922 return mSortKey;
923 }
924
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500925 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400926 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400927 * <p>
928 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
929 * APIs, and are intended to be used by
930 * {@link android.service.notification.NotificationListenerService} implementations to extract
931 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500932 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400933 public Bundle extras = new Bundle();
934
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400935 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700936 * All pending intents in the notification as the system needs to be able to access them but
937 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700938 * custom parcelable objects.
939 *
940 * @hide
941 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100942 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700943 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700944
945 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700946 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
947 * pending intents inside of it, so only those will get the behavior.
948 *
949 * @hide
950 */
Adrian Roosfb921842017-10-26 14:49:56 +0200951 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700952
953 /**
954 * Must be set by a process to start associating tokens with Notification objects
955 * coming in to it. This is set by NotificationManagerService.
956 *
957 * @hide
958 */
959 static public IBinder processWhitelistToken;
960
961 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962 * {@link #extras} key: this is the title of the notification,
963 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
964 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500965 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400966
967 /**
968 * {@link #extras} key: this is the title of the notification when shown in expanded form,
969 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
970 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400971 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400972
973 /**
974 * {@link #extras} key: this is the main text payload, as supplied to
975 * {@link Builder#setContentText(CharSequence)}.
976 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500977 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400978
979 /**
980 * {@link #extras} key: this is a third line of text, as supplied to
981 * {@link Builder#setSubText(CharSequence)}.
982 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400983 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400984
985 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800986 * {@link #extras} key: this is the remote input history, as supplied to
987 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700988 *
989 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
990 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
991 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
992 * notifications once the other party has responded).
993 *
994 * The extra with this key is of type CharSequence[] and contains the most recent entry at
995 * the 0 index, the second most recent at the 1 index, etc.
996 *
997 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800998 */
999 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
1000
1001 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01001002 * {@link #extras} key: boolean as supplied to
1003 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
1004 *
1005 * If set to true, then the view displaying the remote input history from
1006 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
1007 *
1008 * @see Builder#setShowRemoteInputSpinner(boolean)
1009 * @hide
1010 */
1011 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
1012
1013 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01001014 * {@link #extras} key: boolean as supplied to
1015 * {@link Builder#setHideSmartReplies(boolean)}.
1016 *
1017 * If set to true, then any smart reply buttons will be hidden.
1018 *
1019 * @see Builder#setHideSmartReplies(boolean)
1020 * @hide
1021 */
1022 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1023
1024 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001025 * {@link #extras} key: this is a small piece of additional text as supplied to
1026 * {@link Builder#setContentInfo(CharSequence)}.
1027 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001028 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001029
1030 /**
1031 * {@link #extras} key: this is a line of summary information intended to be shown
1032 * alongside expanded notifications, as supplied to (e.g.)
1033 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1034 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001035 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001038 * {@link #extras} key: this is the longer text shown in the big form of a
1039 * {@link BigTextStyle} notification, as supplied to
1040 * {@link BigTextStyle#bigText(CharSequence)}.
1041 */
1042 public static final String EXTRA_BIG_TEXT = "android.bigText";
1043
1044 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001045 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1046 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001047 *
1048 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001049 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001050 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001051 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001052
1053 /**
1054 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1055 * notification payload, as
1056 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001057 *
1058 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001059 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001060 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001061 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001062
1063 /**
1064 * {@link #extras} key: this is a bitmap to be used instead of the one from
1065 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1066 * shown in its expanded form, as supplied to
1067 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1068 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001069 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001070
1071 /**
1072 * {@link #extras} key: this is the progress value supplied to
1073 * {@link Builder#setProgress(int, int, boolean)}.
1074 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001075 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001076
1077 /**
1078 * {@link #extras} key: this is the maximum value supplied to
1079 * {@link Builder#setProgress(int, int, boolean)}.
1080 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001081 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001082
1083 /**
1084 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1085 * {@link Builder#setProgress(int, int, boolean)}.
1086 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001087 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001088
1089 /**
1090 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1091 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1092 * {@link Builder#setUsesChronometer(boolean)}.
1093 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001094 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001095
1096 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001097 * {@link #extras} key: whether the chronometer set on the notification should count down
1098 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001099 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001100 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001101 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001102
1103 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001104 * {@link #extras} key: whether {@link #when} should be shown,
1105 * as supplied to {@link Builder#setShowWhen(boolean)}.
1106 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001107 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001108
1109 /**
1110 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1111 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1112 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001113 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001114
1115 /**
1116 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1117 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1118 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001119 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001120
1121 /**
1122 * {@link #extras} key: A string representing the name of the specific
1123 * {@link android.app.Notification.Style} used to create this notification.
1124 */
Chris Wren91ad5632013-06-05 15:05:57 -04001125 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001126
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001127 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001128 * {@link #extras} key: A String array containing the people that this notification relates to,
1129 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001130 *
1131 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001132 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001133 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001134
1135 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001136 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1137 * this notification relates to.
1138 */
1139 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1140
1141 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001142 * Allow certain system-generated notifications to appear before the device is provisioned.
1143 * Only available to notifications coming from the android package.
1144 * @hide
1145 */
Maurice Lam96c10032017-03-29 15:42:38 -07001146 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001147 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001148 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1149
1150 /**
Robin Leed107af62018-04-27 13:55:56 +02001151 * {@link #extras} key:
1152 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1153 * pointing to an image that can be displayed in the background when the notification is
1154 * selected. Used on television platforms. The URI must point to an image stream suitable for
1155 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001156 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001157 */
1158 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1159
1160 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001161 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001162 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001163 * {@link android.app.Notification.MediaStyle} notification.
1164 */
1165 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1166
1167 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001168 * {@link #extras} key: the indices of actions to be shown in the compact view,
1169 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1170 */
1171 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1172
Christoph Studer943aa672014-08-03 20:31:16 +02001173 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001174 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1175 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001176 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1177 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001178 *
1179 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001180 */
1181 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1182
1183 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001184 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1185 * direct replies
1186 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1187 * {@link Person}
1188 */
1189 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1190
1191 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001192 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001193 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001194 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001195 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001196
1197 /**
1198 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1199 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001200 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1201 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001202 */
1203 public static final String EXTRA_MESSAGES = "android.messages";
1204
1205 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001206 * {@link #extras} key: an array of
1207 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1208 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1209 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1210 * array of bundles.
1211 */
1212 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1213
1214 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001215 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1216 * represents a group conversation.
1217 */
1218 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1219
1220 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001221 * {@link #extras} key: whether the notification should be colorized as
Gustav Senntondf4c2442019-03-18 11:41:11 +00001222 * supplied to {@link Builder#setColorized(boolean)}.
Selim Cinek7b9605b2017-01-19 17:36:00 -08001223 */
1224 public static final String EXTRA_COLORIZED = "android.colorized";
1225
1226 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001227 * @hide
1228 */
1229 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1230
Selim Cinek247fa012016-02-18 09:50:48 -08001231 /**
1232 * @hide
1233 */
1234 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1235
Shane Brennan472a3b32016-12-12 15:28:10 -08001236 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001237 * @hide
1238 */
1239 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1240
1241 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001242 * {@link #extras} key: the audio contents of this notification.
1243 *
1244 * This is for use when rendering the notification on an audio-focused interface;
1245 * the audio contents are a complete sound sample that contains the contents/body of the
1246 * notification. This may be used in substitute of a Text-to-Speech reading of the
1247 * notification. For example if the notification represents a voice message this should point
1248 * to the audio of that message.
1249 *
1250 * The data stored under this key should be a String representation of a Uri that contains the
1251 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1252 *
1253 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1254 * has a field for holding data URI. That field can be used for audio.
1255 * See {@code Message#setData}.
1256 *
1257 * Example usage:
1258 * <pre>
1259 * {@code
1260 * Notification.Builder myBuilder = (build your Notification as normal);
1261 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1262 * }
1263 * </pre>
1264 */
1265 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1266
Dan Sandler80eaa592016-04-14 23:34:54 -04001267 /** @hide */
1268 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001269 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001270 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1271
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001272 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001273 * This is set on the notifications shown by system_server about apps running foreground
1274 * services. It indicates that the notification should be shown
1275 * only if any of the given apps do not already have a properly tagged
1276 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1277 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001278 * @hide
1279 */
1280 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1281
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001282 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001283 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001284 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001285 private Icon mLargeIcon;
1286
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001287 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001288 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001289 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001290
Julia Reynolds13d898c2017-02-02 12:22:05 -05001291 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001292 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001293 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001294
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001295 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001296
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001297 /** @hide */
1298 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1299 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1300 })
1301 @Retention(RetentionPolicy.SOURCE)
1302 public @interface GroupAlertBehavior {}
1303
1304 /**
1305 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1306 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1307 * notification will not be muted when it is in a group.
1308 */
1309 public static final int GROUP_ALERT_ALL = 0;
1310
1311 /**
1312 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1313 * notification in a group should be silenced (no sound or vibration) even if they are posted
1314 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001315 * mute this notification if this notification is a group child. This must be applied to all
1316 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001317 *
1318 * <p> For example, you might want to use this constant if you post a number of children
1319 * notifications at once (say, after a periodic sync), and only need to notify the user
1320 * audibly once.
1321 */
1322 public static final int GROUP_ALERT_SUMMARY = 1;
1323
1324 /**
1325 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1326 * notification in a group should be silenced (no sound or vibration) even if they are
1327 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1328 * to mute this notification if this notification is a group summary.
1329 *
1330 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001331 * in your group have content and the summary is only used to visually group notifications
1332 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001333 */
1334 public static final int GROUP_ALERT_CHILDREN = 2;
1335
Julia Reynolds2f431e22017-06-07 14:12:09 +00001336 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001337
Julia Reynolds13d898c2017-02-02 12:22:05 -05001338 /**
1339 * If this notification is being shown as a badge, always show as a number.
1340 */
1341 public static final int BADGE_ICON_NONE = 0;
1342
1343 /**
1344 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1345 * represent this notification.
1346 */
1347 public static final int BADGE_ICON_SMALL = 1;
1348
1349 /**
1350 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1351 * represent this notification.
1352 */
1353 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001354 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001355
Chris Wren51c75102013-07-16 20:49:17 -04001356 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001357 * Determines whether the platform can generate contextual actions for a notification.
1358 */
1359 private boolean mAllowSystemGeneratedContextualActions = true;
1360
1361 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001362 * Structure to encapsulate a named action that can be shown as part of this notification.
1363 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1364 * selected by the user.
1365 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001366 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1367 * or {@link Notification.Builder#addAction(Notification.Action)}
1368 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001369 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001370 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001371 /**
1372 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1373 * {@link RemoteInput}s.
1374 *
1375 * This is intended for {@link RemoteInput}s that only accept data, meaning
1376 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
Gustav Senntondf4c2442019-03-18 11:41:11 +00001377 * is null or empty, and {@link RemoteInput#getAllowedDataTypes} is non-null and not
Shane Brennan472a3b32016-12-12 15:28:10 -08001378 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1379 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1380 *
1381 * You can test if a RemoteInput matches these constraints using
1382 * {@link RemoteInput#isDataOnly}.
1383 */
1384 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1385
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001386 /**
1387 * {@link }: No semantic action defined.
1388 */
1389 public static final int SEMANTIC_ACTION_NONE = 0;
1390
1391 /**
1392 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1393 * may be appropriate.
1394 */
1395 public static final int SEMANTIC_ACTION_REPLY = 1;
1396
1397 /**
1398 * {@code SemanticAction}: Mark content as read.
1399 */
1400 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1401
1402 /**
1403 * {@code SemanticAction}: Mark content as unread.
1404 */
1405 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1406
1407 /**
1408 * {@code SemanticAction}: Delete the content associated with the notification. This
1409 * could mean deleting an email, message, etc.
1410 */
1411 public static final int SEMANTIC_ACTION_DELETE = 4;
1412
1413 /**
1414 * {@code SemanticAction}: Archive the content associated with the notification. This
1415 * could mean archiving an email, message, etc.
1416 */
1417 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1418
1419 /**
1420 * {@code SemanticAction}: Mute the content associated with the notification. This could
1421 * mean silencing a conversation or currently playing media.
1422 */
1423 public static final int SEMANTIC_ACTION_MUTE = 6;
1424
1425 /**
1426 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1427 * mean un-silencing a conversation or currently playing media.
1428 */
1429 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1430
1431 /**
1432 * {@code SemanticAction}: Mark content with a thumbs up.
1433 */
1434 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1435
1436 /**
1437 * {@code SemanticAction}: Mark content with a thumbs down.
1438 */
1439 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1440
Kodlee Yincda5b092018-02-15 15:34:53 -08001441 /**
1442 * {@code SemanticAction}: Call a contact, group, etc.
1443 */
1444 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001445
Griff Hazen959591e2014-05-15 22:26:18 -07001446 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001447 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001448 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001449 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001450 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001451 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001452 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001453
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001454 /**
1455 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001456 *
1457 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001458 */
Dan Sandler86647982015-05-13 23:41:13 -04001459 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001460 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001461
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001462 /**
1463 * Title of the action.
1464 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001465 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001466
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001467 /**
1468 * Intent to send when the user invokes this action. May be null, in which case the action
1469 * may be rendered in a disabled presentation by the system UI.
1470 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001471 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001472
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001473 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001474 if (in.readInt() != 0) {
1475 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001476 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1477 icon = mIcon.getResId();
1478 }
Dan Sandler86647982015-05-13 23:41:13 -04001479 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001480 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1481 if (in.readInt() == 1) {
1482 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1483 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001484 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001485 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001486 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001487 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001488 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001489 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001490
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001491 /**
Dan Sandler86647982015-05-13 23:41:13 -04001492 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001493 */
Dan Sandler86647982015-05-13 23:41:13 -04001494 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001495 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001496 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001497 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001498 }
1499
Adrian Roos7af53622016-10-12 13:44:05 -07001500 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001501 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001502 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001503 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001504 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001505 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1506 this.icon = icon.getResId();
1507 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001508 this.title = title;
1509 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001510 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001511 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001512 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001513 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001514 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001515 }
1516
1517 /**
Dan Sandler86647982015-05-13 23:41:13 -04001518 * Return an icon representing the action.
1519 */
1520 public Icon getIcon() {
1521 if (mIcon == null && icon != 0) {
1522 // you snuck an icon in here without using the builder; let's try to keep it
1523 mIcon = Icon.createWithResource("", icon);
1524 }
1525 return mIcon;
1526 }
1527
1528 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001529 * Get additional metadata carried around with this Action.
1530 */
1531 public Bundle getExtras() {
1532 return mExtras;
1533 }
1534
1535 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001536 * Return whether the platform should automatically generate possible replies for this
1537 * {@link Action}
1538 */
1539 public boolean getAllowGeneratedReplies() {
1540 return mAllowGeneratedReplies;
1541 }
1542
1543 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001544 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001545 * May return null if no remote inputs were added. Only returns inputs which accept
1546 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001547 */
1548 public RemoteInput[] getRemoteInputs() {
1549 return mRemoteInputs;
1550 }
1551
1552 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001553 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1554 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1555 * (eg. reply, mark as read, delete, etc).
1556 */
1557 public @SemanticAction int getSemanticAction() {
1558 return mSemanticAction;
1559 }
1560
1561 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001562 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1563 * notification message body. An example of a contextual action could be an action opening a
1564 * map application with an address shown in the notification.
1565 */
1566 public boolean isContextual() {
1567 return mIsContextual;
1568 }
1569
1570 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001571 * Get the list of inputs to be collected from the user that ONLY accept data when this
1572 * action is sent. These remote inputs are guaranteed to return true on a call to
1573 * {@link RemoteInput#isDataOnly}.
1574 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001575 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001576 *
1577 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1578 * of non-textual RemoteInputs do not access these remote inputs.
1579 */
1580 public RemoteInput[] getDataOnlyRemoteInputs() {
1581 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1582 }
1583
1584 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001585 * Builder class for {@link Action} objects.
1586 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001587 public static final class Builder {
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001588 @Nullable private final Icon mIcon;
1589 @Nullable private final CharSequence mTitle;
1590 @Nullable private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001591 private boolean mAllowGeneratedReplies = true;
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001592 @NonNull private final Bundle mExtras;
1593 @Nullable private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001594 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001595 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001596
1597 /**
1598 * Construct a new builder for {@link Action} object.
1599 * @param icon icon to show for this action
1600 * @param title the title of the action
1601 * @param intent the {@link PendingIntent} to fire when users trigger this action
1602 */
Dan Sandler86647982015-05-13 23:41:13 -04001603 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001604 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001605 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001606 }
1607
1608 /**
1609 * Construct a new builder for {@link Action} object.
1610 * @param icon icon to show for this action
1611 * @param title the title of the action
1612 * @param intent the {@link PendingIntent} to fire when users trigger this action
1613 */
1614 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001615 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001616 }
1617
1618 /**
1619 * Construct a new builder for {@link Action} object using the fields from an
1620 * {@link Action}.
1621 * @param action the action to read fields from.
1622 */
1623 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001624 this(action.getIcon(), action.title, action.actionIntent,
1625 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001626 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001627 }
1628
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001629 private Builder(@Nullable Icon icon, @Nullable CharSequence title,
1630 @Nullable PendingIntent intent, @NonNull Bundle extras,
1631 @Nullable RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1632 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001633 mIcon = icon;
1634 mTitle = title;
1635 mIntent = intent;
1636 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001637 if (remoteInputs != null) {
1638 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1639 Collections.addAll(mRemoteInputs, remoteInputs);
1640 }
Adrian Roos7af53622016-10-12 13:44:05 -07001641 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001642 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001643 }
1644
1645 /**
1646 * Merge additional metadata into this builder.
1647 *
1648 * <p>Values within the Bundle will replace existing extras values in this Builder.
1649 *
1650 * @see Notification.Action#extras
1651 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001652 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001653 public Builder addExtras(Bundle extras) {
1654 if (extras != null) {
1655 mExtras.putAll(extras);
1656 }
1657 return this;
1658 }
1659
1660 /**
1661 * Get the metadata Bundle used by this Builder.
1662 *
1663 * <p>The returned Bundle is shared with this Builder.
1664 */
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001665 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001666 public Bundle getExtras() {
1667 return mExtras;
1668 }
1669
1670 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001671 * Add an input to be collected from the user when this action is sent.
1672 * Response values can be retrieved from the fired intent by using the
1673 * {@link RemoteInput#getResultsFromIntent} function.
1674 * @param remoteInput a {@link RemoteInput} to add to the action
1675 * @return this object for method chaining
1676 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001677 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001678 public Builder addRemoteInput(RemoteInput remoteInput) {
1679 if (mRemoteInputs == null) {
1680 mRemoteInputs = new ArrayList<RemoteInput>();
1681 }
1682 mRemoteInputs.add(remoteInput);
1683 return this;
1684 }
1685
1686 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001687 * Set whether the platform should automatically generate possible replies to add to
1688 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1689 * {@link RemoteInput}, this has no effect.
1690 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1691 * otherwise
1692 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001693 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001694 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001695 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001696 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1697 mAllowGeneratedReplies = allowGeneratedReplies;
1698 return this;
1699 }
1700
1701 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001702 * Sets the {@code SemanticAction} for this {@link Action}. A
1703 * {@code SemanticAction} denotes what an {@link Action}'s
1704 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1705 * @param semanticAction a SemanticAction defined within {@link Action} with
1706 * {@code SEMANTIC_ACTION_} prefixes
1707 * @return this object for method chaining
1708 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001709 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001710 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1711 mSemanticAction = semanticAction;
1712 return this;
1713 }
1714
1715 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001716 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1717 * dependent on the notification message body. An example of a contextual action could
1718 * be an action opening a map application with an address shown in the notification.
1719 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001720 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001721 public Builder setContextual(boolean isContextual) {
1722 mIsContextual = isContextual;
1723 return this;
1724 }
1725
1726 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001727 * Apply an extender to this action builder. Extenders may be used to add
1728 * metadata or change options on this builder.
1729 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001730 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001731 public Builder extend(Extender extender) {
1732 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001733 return this;
1734 }
1735
1736 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001737 * Throws an NPE if we are building a contextual action missing one of the fields
1738 * necessary to display the action.
1739 */
1740 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001741 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001742
1743 if (mIcon == null) {
1744 throw new NullPointerException("Contextual Actions must contain a valid icon");
1745 }
1746
1747 if (mIntent == null) {
1748 throw new NullPointerException(
1749 "Contextual Actions must contain a valid PendingIntent");
1750 }
1751 }
1752
1753 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001754 * Combine all of the options that have been set and return a new {@link Action}
1755 * object.
1756 * @return the built action
1757 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001758 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001759 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001760 checkContextualActionNullFields();
1761
Shane Brennan472a3b32016-12-12 15:28:10 -08001762 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1763 RemoteInput[] previousDataInputs =
1764 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001765 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001766 for (RemoteInput input : previousDataInputs) {
1767 dataOnlyInputs.add(input);
1768 }
1769 }
1770 List<RemoteInput> textInputs = new ArrayList<>();
1771 if (mRemoteInputs != null) {
1772 for (RemoteInput input : mRemoteInputs) {
1773 if (input.isDataOnly()) {
1774 dataOnlyInputs.add(input);
1775 } else {
1776 textInputs.add(input);
1777 }
1778 }
1779 }
1780 if (!dataOnlyInputs.isEmpty()) {
1781 RemoteInput[] dataInputsArr =
1782 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1783 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1784 }
1785 RemoteInput[] textInputsArr = textInputs.isEmpty()
1786 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1787 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001788 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001789 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001790 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001791
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001792 @Override
1793 public Action clone() {
1794 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001795 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001796 title,
1797 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001798 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001799 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001800 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001801 getSemanticAction(),
1802 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001803 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001804
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001805 @Override
1806 public int describeContents() {
1807 return 0;
1808 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001809
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001810 @Override
1811 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001812 final Icon ic = getIcon();
1813 if (ic != null) {
1814 out.writeInt(1);
1815 ic.writeToParcel(out, 0);
1816 } else {
1817 out.writeInt(0);
1818 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001819 TextUtils.writeToParcel(title, out, flags);
1820 if (actionIntent != null) {
1821 out.writeInt(1);
1822 actionIntent.writeToParcel(out, flags);
1823 } else {
1824 out.writeInt(0);
1825 }
Griff Hazen959591e2014-05-15 22:26:18 -07001826 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001827 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001828 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001829 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001830 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001831 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001832
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001833 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001834 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001835 public Action createFromParcel(Parcel in) {
1836 return new Action(in);
1837 }
1838 public Action[] newArray(int size) {
1839 return new Action[size];
1840 }
1841 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001842
1843 /**
1844 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1845 * metadata or change options on an action builder.
1846 */
1847 public interface Extender {
1848 /**
1849 * Apply this extender to a notification action builder.
1850 * @param builder the builder to be modified.
1851 * @return the build object for chaining.
1852 */
1853 public Builder extend(Builder builder);
1854 }
1855
1856 /**
1857 * Wearable extender for notification actions. To add extensions to an action,
1858 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1859 * the {@code WearableExtender()} constructor and apply it to a
1860 * {@link android.app.Notification.Action.Builder} using
1861 * {@link android.app.Notification.Action.Builder#extend}.
1862 *
1863 * <pre class="prettyprint">
1864 * Notification.Action action = new Notification.Action.Builder(
1865 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001866 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001867 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001868 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001869 */
1870 public static final class WearableExtender implements Extender {
1871 /** Notification action extra which contains wearable extensions */
1872 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1873
Pete Gastaf6781d2014-10-07 15:17:05 -04001874 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001875 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001876 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1877 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1878 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001879
1880 // Flags bitwise-ored to mFlags
1881 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001882 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001883 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001884
1885 // Default value for flags integer
1886 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1887
1888 private int mFlags = DEFAULT_FLAGS;
1889
Pete Gastaf6781d2014-10-07 15:17:05 -04001890 private CharSequence mInProgressLabel;
1891 private CharSequence mConfirmLabel;
1892 private CharSequence mCancelLabel;
1893
Griff Hazen61a9e862014-05-22 16:05:19 -07001894 /**
1895 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1896 * options.
1897 */
1898 public WearableExtender() {
1899 }
1900
1901 /**
1902 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1903 * wearable options present in an existing notification action.
1904 * @param action the notification action to inspect.
1905 */
1906 public WearableExtender(Action action) {
1907 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1908 if (wearableBundle != null) {
1909 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001910 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1911 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1912 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001913 }
1914 }
1915
1916 /**
1917 * Apply wearable extensions to a notification action that is being built. This is
1918 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1919 * method of {@link android.app.Notification.Action.Builder}.
1920 */
1921 @Override
1922 public Action.Builder extend(Action.Builder builder) {
1923 Bundle wearableBundle = new Bundle();
1924
1925 if (mFlags != DEFAULT_FLAGS) {
1926 wearableBundle.putInt(KEY_FLAGS, mFlags);
1927 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001928 if (mInProgressLabel != null) {
1929 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1930 }
1931 if (mConfirmLabel != null) {
1932 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1933 }
1934 if (mCancelLabel != null) {
1935 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1936 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001937
1938 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1939 return builder;
1940 }
1941
1942 @Override
1943 public WearableExtender clone() {
1944 WearableExtender that = new WearableExtender();
1945 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001946 that.mInProgressLabel = this.mInProgressLabel;
1947 that.mConfirmLabel = this.mConfirmLabel;
1948 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001949 return that;
1950 }
1951
1952 /**
1953 * Set whether this action is available when the wearable device is not connected to
1954 * a companion device. The user can still trigger this action when the wearable device is
1955 * offline, but a visual hint will indicate that the action may not be available.
1956 * Defaults to true.
1957 */
1958 public WearableExtender setAvailableOffline(boolean availableOffline) {
1959 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1960 return this;
1961 }
1962
1963 /**
1964 * Get whether this action is available when the wearable device is not connected to
1965 * a companion device. The user can still trigger this action when the wearable device is
1966 * offline, but a visual hint will indicate that the action may not be available.
1967 * Defaults to true.
1968 */
1969 public boolean isAvailableOffline() {
1970 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1971 }
1972
1973 private void setFlag(int mask, boolean value) {
1974 if (value) {
1975 mFlags |= mask;
1976 } else {
1977 mFlags &= ~mask;
1978 }
1979 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001980
1981 /**
1982 * Set a label to display while the wearable is preparing to automatically execute the
1983 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1984 *
1985 * @param label the label to display while the action is being prepared to execute
1986 * @return this object for method chaining
1987 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001988 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001989 public WearableExtender setInProgressLabel(CharSequence label) {
1990 mInProgressLabel = label;
1991 return this;
1992 }
1993
1994 /**
1995 * Get the label to display while the wearable is preparing to automatically execute
1996 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1997 *
1998 * @return the label to display while the action is being prepared to execute
1999 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002000 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002001 public CharSequence getInProgressLabel() {
2002 return mInProgressLabel;
2003 }
2004
2005 /**
2006 * Set a label to display to confirm that the action should be executed.
2007 * This is usually an imperative verb like "Send".
2008 *
2009 * @param label the label to confirm the action should be executed
2010 * @return this object for method chaining
2011 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002012 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002013 public WearableExtender setConfirmLabel(CharSequence label) {
2014 mConfirmLabel = label;
2015 return this;
2016 }
2017
2018 /**
2019 * Get the label to display to confirm that the action should be executed.
2020 * This is usually an imperative verb like "Send".
2021 *
2022 * @return the label to confirm the action should be executed
2023 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002024 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002025 public CharSequence getConfirmLabel() {
2026 return mConfirmLabel;
2027 }
2028
2029 /**
2030 * Set a label to display to cancel the action.
2031 * This is usually an imperative verb, like "Cancel".
2032 *
2033 * @param label the label to display to cancel the action
2034 * @return this object for method chaining
2035 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002036 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002037 public WearableExtender setCancelLabel(CharSequence label) {
2038 mCancelLabel = label;
2039 return this;
2040 }
2041
2042 /**
2043 * Get the label to display to cancel the action.
2044 * This is usually an imperative verb like "Cancel".
2045 *
2046 * @return the label to display to cancel the action
2047 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002048 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002049 public CharSequence getCancelLabel() {
2050 return mCancelLabel;
2051 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002052
2053 /**
2054 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2055 * platform that it can generate the appropriate transitions.
2056 * @param hintLaunchesActivity {@code true} if the content intent will launch
2057 * an activity and transitions should be generated, false otherwise.
2058 * @return this object for method chaining
2059 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002060 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002061 boolean hintLaunchesActivity) {
2062 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2063 return this;
2064 }
2065
2066 /**
2067 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2068 * platform that it can generate the appropriate transitions
2069 * @return {@code true} if the content intent will launch an activity and transitions
2070 * should be generated, false otherwise. The default value is {@code false} if this was
2071 * never set.
2072 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002073 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002074 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2075 }
Alex Hills9f087612016-06-07 09:08:59 -04002076
2077 /**
2078 * Set a hint that this Action should be displayed inline.
2079 *
2080 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2081 * otherwise
2082 * @return this object for method chaining
2083 */
2084 public WearableExtender setHintDisplayActionInline(
2085 boolean hintDisplayInline) {
2086 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2087 return this;
2088 }
2089
2090 /**
2091 * Get a hint that this Action should be displayed inline.
2092 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002093 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002094 * otherwise. The default value is {@code false} if this was never set.
2095 */
2096 public boolean getHintDisplayActionInline() {
2097 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2098 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002099 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002100
2101 /**
2102 * Provides meaning to an {@link Action} that hints at what the associated
2103 * {@link PendingIntent} will do. For example, an {@link Action} with a
2104 * {@link PendingIntent} that replies to a text message notification may have the
2105 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2106 *
2107 * @hide
2108 */
2109 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2110 SEMANTIC_ACTION_NONE,
2111 SEMANTIC_ACTION_REPLY,
2112 SEMANTIC_ACTION_MARK_AS_READ,
2113 SEMANTIC_ACTION_MARK_AS_UNREAD,
2114 SEMANTIC_ACTION_DELETE,
2115 SEMANTIC_ACTION_ARCHIVE,
2116 SEMANTIC_ACTION_MUTE,
2117 SEMANTIC_ACTION_UNMUTE,
2118 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002119 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002120 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002121 })
2122 @Retention(RetentionPolicy.SOURCE)
2123 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002124 }
2125
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002126 /**
2127 * Array of all {@link Action} structures attached to this notification by
2128 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2129 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2130 * interface for invoking actions.
2131 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002132 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002133
2134 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002135 * Replacement version of this notification whose content will be shown
2136 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2137 * and {@link #VISIBILITY_PUBLIC}.
2138 */
2139 public Notification publicVersion;
2140
2141 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002143 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 */
2145 public Notification()
2146 {
2147 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002148 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002149 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151
2152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 * @hide
2154 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002155 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 public Notification(Context context, int icon, CharSequence tickerText, long when,
2157 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2158 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002159 new Builder(context)
2160 .setWhen(when)
2161 .setSmallIcon(icon)
2162 .setTicker(tickerText)
2163 .setContentTitle(contentTitle)
2164 .setContentText(contentText)
2165 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2166 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168
2169 /**
2170 * Constructs a Notification object with the information needed to
2171 * have a status bar icon without the standard expanded view.
2172 *
2173 * @param icon The resource id of the icon to put in the status bar.
2174 * @param tickerText The text that flows by in the status bar when the notification first
2175 * activates.
2176 * @param when The time to show in the time field. In the System.currentTimeMillis
2177 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002178 *
2179 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002181 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 public Notification(int icon, CharSequence tickerText, long when)
2183 {
2184 this.icon = icon;
2185 this.tickerText = tickerText;
2186 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002187 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
2189
2190 /**
2191 * Unflatten the notification from a parcel.
2192 */
Svet Ganovddb94882016-06-23 19:55:24 -07002193 @SuppressWarnings("unchecked")
2194 public Notification(Parcel parcel) {
2195 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2196 // intents in extras are always written as the last entry.
2197 readFromParcelImpl(parcel);
2198 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002199 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002200 }
2201
2202 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 {
2204 int version = parcel.readInt();
2205
Adrian Roosfb921842017-10-26 14:49:56 +02002206 mWhitelistToken = parcel.readStrongBinder();
2207 if (mWhitelistToken == null) {
2208 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002209 }
2210 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002211 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002214 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002215 if (parcel.readInt() != 0) {
2216 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002217 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2218 icon = mSmallIcon.getResId();
2219 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 number = parcel.readInt();
2222 if (parcel.readInt() != 0) {
2223 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2224 }
2225 if (parcel.readInt() != 0) {
2226 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2227 }
2228 if (parcel.readInt() != 0) {
2229 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2230 }
2231 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002232 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002233 }
2234 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2236 }
Joe Onorato561d3852010-11-20 18:09:34 -08002237 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002238 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 defaults = parcel.readInt();
2241 flags = parcel.readInt();
2242 if (parcel.readInt() != 0) {
2243 sound = Uri.CREATOR.createFromParcel(parcel);
2244 }
2245
2246 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002247 if (parcel.readInt() != 0) {
2248 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 vibrate = parcel.createLongArray();
2251 ledARGB = parcel.readInt();
2252 ledOnMS = parcel.readInt();
2253 ledOffMS = parcel.readInt();
2254 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002255
2256 if (parcel.readInt() != 0) {
2257 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2258 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002259
2260 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002261
John Spurlockfd7f1e02014-03-18 16:41:57 -04002262 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002264 mGroupKey = parcel.readString();
2265
2266 mSortKey = parcel.readString();
2267
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002268 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002269 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002270
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002271 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2272
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002273 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002274 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2275 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002276
Chris Wren8fd39ec2014-02-27 17:43:26 -05002277 if (parcel.readInt() != 0) {
2278 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2279 }
2280
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002281 visibility = parcel.readInt();
2282
2283 if (parcel.readInt() != 0) {
2284 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2285 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002286
2287 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002288
2289 if (parcel.readInt() != 0) {
2290 mChannelId = parcel.readString();
2291 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002292 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002293
2294 if (parcel.readInt() != 0) {
2295 mShortcutId = parcel.readString();
2296 }
2297
Felipe Leme90205ef2019-03-05 09:59:52 -08002298 if (parcel.readInt() != 0) {
2299 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2300 }
2301
Julia Reynolds13d898c2017-02-02 12:22:05 -05002302 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002303
2304 if (parcel.readInt() != 0) {
2305 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2306 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002307
2308 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002309 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002310 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002311 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002312
2313 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
2315
Andy Stadler110988c2010-12-03 14:29:16 -08002316 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002317 public Notification clone() {
2318 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002319 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002320 return that;
2321 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002322
Daniel Sandler1a497d32013-04-18 14:52:45 -04002323 /**
2324 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2325 * of this into that.
2326 * @hide
2327 */
2328 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002329 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002330 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002331 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002332 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002333 that.number = this.number;
2334
2335 // PendingIntents are global, so there's no reason (or way) to clone them.
2336 that.contentIntent = this.contentIntent;
2337 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002338 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002339
2340 if (this.tickerText != null) {
2341 that.tickerText = this.tickerText.toString();
2342 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002343 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002344 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002345 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002346 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002347 that.contentView = this.contentView.clone();
2348 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002349 if (heavy && this.mLargeIcon != null) {
2350 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002351 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002352 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002353 that.sound = this.sound; // android.net.Uri is immutable
2354 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002355 if (this.audioAttributes != null) {
2356 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2357 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002358
2359 final long[] vibrate = this.vibrate;
2360 if (vibrate != null) {
2361 final int N = vibrate.length;
2362 final long[] vib = that.vibrate = new long[N];
2363 System.arraycopy(vibrate, 0, vib, 0, N);
2364 }
2365
2366 that.ledARGB = this.ledARGB;
2367 that.ledOnMS = this.ledOnMS;
2368 that.ledOffMS = this.ledOffMS;
2369 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002370
Joe Onorato18e69df2010-05-17 22:26:12 -07002371 that.flags = this.flags;
2372
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002373 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002374
John Spurlockfd7f1e02014-03-18 16:41:57 -04002375 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002376
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002377 that.mGroupKey = this.mGroupKey;
2378
2379 that.mSortKey = this.mSortKey;
2380
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002381 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002382 try {
2383 that.extras = new Bundle(this.extras);
2384 // will unparcel
2385 that.extras.size();
2386 } catch (BadParcelableException e) {
2387 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2388 that.extras = null;
2389 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002390 }
2391
Felipe Lemedd85da62016-06-28 11:29:54 -07002392 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2393 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002394 }
2395
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002396 if (this.actions != null) {
2397 that.actions = new Action[this.actions.length];
2398 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002399 if ( this.actions[i] != null) {
2400 that.actions[i] = this.actions[i].clone();
2401 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002402 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002403 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002404
Daniel Sandler1a497d32013-04-18 14:52:45 -04002405 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002406 that.bigContentView = this.bigContentView.clone();
2407 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002408
Chris Wren8fd39ec2014-02-27 17:43:26 -05002409 if (heavy && this.headsUpContentView != null) {
2410 that.headsUpContentView = this.headsUpContentView.clone();
2411 }
2412
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002413 that.visibility = this.visibility;
2414
2415 if (this.publicVersion != null) {
2416 that.publicVersion = new Notification();
2417 this.publicVersion.cloneInto(that.publicVersion, heavy);
2418 }
2419
Dan Sandler26e81cf2014-05-06 10:01:27 -04002420 that.color = this.color;
2421
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002422 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002423 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002424 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002425 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002426 that.mBadgeIcon = this.mBadgeIcon;
2427 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002428 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002429 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002430 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002431
Daniel Sandler1a497d32013-04-18 14:52:45 -04002432 if (!heavy) {
2433 that.lightenPayload(); // will clean out extras
2434 }
2435 }
2436
2437 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002438 * Note all {@link Uri} that are referenced internally, with the expectation
2439 * that Uri permission grants will need to be issued to ensure the recipient
2440 * of this object is able to render its contents.
2441 *
2442 * @hide
2443 */
2444 public void visitUris(@NonNull Consumer<Uri> visitor) {
2445 visitor.accept(sound);
2446
2447 if (tickerView != null) tickerView.visitUris(visitor);
2448 if (contentView != null) contentView.visitUris(visitor);
2449 if (bigContentView != null) bigContentView.visitUris(visitor);
2450 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2451
2452 if (extras != null) {
2453 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002454 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2455 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2456 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002457 }
2458
2459 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2460 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2461 if (!ArrayUtils.isEmpty(messages)) {
2462 for (MessagingStyle.Message message : MessagingStyle.Message
2463 .getMessagesFromBundleArray(messages)) {
2464 visitor.accept(message.getDataUri());
2465 }
2466 }
2467
2468 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2469 if (!ArrayUtils.isEmpty(historic)) {
2470 for (MessagingStyle.Message message : MessagingStyle.Message
2471 .getMessagesFromBundleArray(historic)) {
2472 visitor.accept(message.getDataUri());
2473 }
2474 }
2475 }
2476 }
2477
2478 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002479 * Removes heavyweight parts of the Notification object for archival or for sending to
2480 * listeners when the full contents are not necessary.
2481 * @hide
2482 */
2483 public final void lightenPayload() {
2484 tickerView = null;
2485 contentView = null;
2486 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002487 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002488 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002489 if (extras != null && !extras.isEmpty()) {
2490 final Set<String> keyset = extras.keySet();
2491 final int N = keyset.size();
2492 final String[] keys = keyset.toArray(new String[N]);
2493 for (int i=0; i<N; i++) {
2494 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002495 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2496 continue;
2497 }
Dan Sandler50128532015-12-08 15:42:41 -05002498 final Object obj = extras.get(key);
2499 if (obj != null &&
2500 ( obj instanceof Parcelable
2501 || obj instanceof Parcelable[]
2502 || obj instanceof SparseArray
2503 || obj instanceof ArrayList)) {
2504 extras.remove(key);
2505 }
2506 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002507 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002508 }
2509
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002510 /**
2511 * Make sure this CharSequence is safe to put into a bundle, which basically
2512 * means it had better not be some custom Parcelable implementation.
2513 * @hide
2514 */
2515 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002516 if (cs == null) return cs;
2517 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2518 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2519 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002520 if (cs instanceof Parcelable) {
2521 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2522 + " instance is a custom Parcelable and not allowed in Notification");
2523 return cs.toString();
2524 }
Selim Cinek60a54252016-02-26 17:03:25 -08002525 return removeTextSizeSpans(cs);
2526 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002527
Selim Cinek60a54252016-02-26 17:03:25 -08002528 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2529 if (charSequence instanceof Spanned) {
2530 Spanned ss = (Spanned) charSequence;
2531 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2532 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2533 for (Object span : spans) {
2534 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002535 if (resultSpan instanceof CharacterStyle) {
2536 resultSpan = ((CharacterStyle) span).getUnderlying();
2537 }
2538 if (resultSpan instanceof TextAppearanceSpan) {
2539 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002540 resultSpan = new TextAppearanceSpan(
2541 originalSpan.getFamily(),
2542 originalSpan.getTextStyle(),
2543 -1,
2544 originalSpan.getTextColor(),
2545 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002546 } else if (resultSpan instanceof RelativeSizeSpan
2547 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002548 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002549 } else {
2550 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002551 }
2552 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2553 ss.getSpanFlags(span));
2554 }
2555 return builder;
2556 }
2557 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002558 }
2559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 public int describeContents() {
2561 return 0;
2562 }
2563
2564 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002565 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 */
Svet Ganovddb94882016-06-23 19:55:24 -07002567 public void writeToParcel(Parcel parcel, int flags) {
2568 // We need to mark all pending intents getting into the notification
2569 // system as being put there to later allow the notification ranker
2570 // to launch them and by doing so add the app to the battery saver white
2571 // list for a short period of time. The problem is that the system
2572 // cannot look into the extras as there may be parcelables there that
2573 // the platform does not know how to handle. To go around that we have
2574 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002575 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002576 if (collectPendingIntents) {
2577 PendingIntent.setOnMarshaledListener(
2578 (PendingIntent intent, Parcel out, int outFlags) -> {
2579 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002580 if (allPendingIntents == null) {
2581 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002582 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002583 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002584 }
2585 });
2586 }
2587 try {
2588 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002589 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002590 writeToParcelImpl(parcel, flags);
2591 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002592 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002593 } finally {
2594 if (collectPendingIntents) {
2595 PendingIntent.setOnMarshaledListener(null);
2596 }
2597 }
2598 }
2599
2600 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 parcel.writeInt(1);
2602
Adrian Roosfb921842017-10-26 14:49:56 +02002603 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002605 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002606 if (mSmallIcon == null && icon != 0) {
2607 // you snuck an icon in here without using the builder; let's try to keep it
2608 mSmallIcon = Icon.createWithResource("", icon);
2609 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002610 if (mSmallIcon != null) {
2611 parcel.writeInt(1);
2612 mSmallIcon.writeToParcel(parcel, 0);
2613 } else {
2614 parcel.writeInt(0);
2615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 parcel.writeInt(number);
2617 if (contentIntent != null) {
2618 parcel.writeInt(1);
2619 contentIntent.writeToParcel(parcel, 0);
2620 } else {
2621 parcel.writeInt(0);
2622 }
2623 if (deleteIntent != null) {
2624 parcel.writeInt(1);
2625 deleteIntent.writeToParcel(parcel, 0);
2626 } else {
2627 parcel.writeInt(0);
2628 }
2629 if (tickerText != null) {
2630 parcel.writeInt(1);
2631 TextUtils.writeToParcel(tickerText, parcel, flags);
2632 } else {
2633 parcel.writeInt(0);
2634 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002635 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002636 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002637 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002638 } else {
2639 parcel.writeInt(0);
2640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 if (contentView != null) {
2642 parcel.writeInt(1);
2643 contentView.writeToParcel(parcel, 0);
2644 } else {
2645 parcel.writeInt(0);
2646 }
Selim Cinek279fa862016-06-14 10:57:25 -07002647 if (mLargeIcon == null && largeIcon != null) {
2648 // you snuck an icon in here without using the builder; let's try to keep it
2649 mLargeIcon = Icon.createWithBitmap(largeIcon);
2650 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002651 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002652 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002653 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002654 } else {
2655 parcel.writeInt(0);
2656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657
2658 parcel.writeInt(defaults);
2659 parcel.writeInt(this.flags);
2660
2661 if (sound != null) {
2662 parcel.writeInt(1);
2663 sound.writeToParcel(parcel, 0);
2664 } else {
2665 parcel.writeInt(0);
2666 }
2667 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002668
2669 if (audioAttributes != null) {
2670 parcel.writeInt(1);
2671 audioAttributes.writeToParcel(parcel, 0);
2672 } else {
2673 parcel.writeInt(0);
2674 }
2675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 parcel.writeLongArray(vibrate);
2677 parcel.writeInt(ledARGB);
2678 parcel.writeInt(ledOnMS);
2679 parcel.writeInt(ledOffMS);
2680 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002681
2682 if (fullScreenIntent != null) {
2683 parcel.writeInt(1);
2684 fullScreenIntent.writeToParcel(parcel, 0);
2685 } else {
2686 parcel.writeInt(0);
2687 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002688
2689 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002690
John Spurlockfd7f1e02014-03-18 16:41:57 -04002691 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002692
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002693 parcel.writeString(mGroupKey);
2694
2695 parcel.writeString(mSortKey);
2696
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002697 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002698
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002699 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002700
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002701 if (bigContentView != null) {
2702 parcel.writeInt(1);
2703 bigContentView.writeToParcel(parcel, 0);
2704 } else {
2705 parcel.writeInt(0);
2706 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002707
Chris Wren8fd39ec2014-02-27 17:43:26 -05002708 if (headsUpContentView != null) {
2709 parcel.writeInt(1);
2710 headsUpContentView.writeToParcel(parcel, 0);
2711 } else {
2712 parcel.writeInt(0);
2713 }
2714
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002715 parcel.writeInt(visibility);
2716
2717 if (publicVersion != null) {
2718 parcel.writeInt(1);
2719 publicVersion.writeToParcel(parcel, 0);
2720 } else {
2721 parcel.writeInt(0);
2722 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002723
2724 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002725
2726 if (mChannelId != null) {
2727 parcel.writeInt(1);
2728 parcel.writeString(mChannelId);
2729 } else {
2730 parcel.writeInt(0);
2731 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002732 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002733
2734 if (mShortcutId != null) {
2735 parcel.writeInt(1);
2736 parcel.writeString(mShortcutId);
2737 } else {
2738 parcel.writeInt(0);
2739 }
2740
Felipe Leme90205ef2019-03-05 09:59:52 -08002741 if (mLocusId != null) {
2742 parcel.writeInt(1);
2743 mLocusId.writeToParcel(parcel, 0);
2744 } else {
2745 parcel.writeInt(0);
2746 }
2747
Julia Reynolds13d898c2017-02-02 12:22:05 -05002748 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002749
2750 if (mSettingsText != null) {
2751 parcel.writeInt(1);
2752 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2753 } else {
2754 parcel.writeInt(0);
2755 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002756
2757 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002758
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002759 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002760 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002761 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002762 } else {
2763 parcel.writeInt(0);
2764 }
2765
Gustav Sennton761884c2018-11-19 17:40:19 +00002766 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2767
Julia Reynoldsfc640012018-02-21 12:25:27 -05002768 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
2770
2771 /**
2772 * Parcelable.Creator that instantiates Notification objects
2773 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002774 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 = new Parcelable.Creator<Notification>()
2776 {
2777 public Notification createFromParcel(Parcel parcel)
2778 {
2779 return new Notification(parcel);
2780 }
2781
2782 public Notification[] newArray(int size)
2783 {
2784 return new Notification[size];
2785 }
2786 };
2787
2788 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002789 * @hide
2790 */
2791 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2792 Notification.Action[] firstAs = first.actions;
2793 Notification.Action[] secondAs = second.actions;
2794 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2795 return true;
2796 }
2797 if (firstAs != null && secondAs != null) {
2798 if (firstAs.length != secondAs.length) {
2799 return true;
2800 }
2801 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002802 if (!Objects.equals(String.valueOf(firstAs[i].title),
2803 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002804 return true;
2805 }
2806 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2807 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2808 if (firstRs == null) {
2809 firstRs = new RemoteInput[0];
2810 }
2811 if (secondRs == null) {
2812 secondRs = new RemoteInput[0];
2813 }
2814 if (firstRs.length != secondRs.length) {
2815 return true;
2816 }
2817 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002818 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2819 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002820 return true;
2821 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002822 }
2823 }
2824 }
2825 return false;
2826 }
2827
2828 /**
2829 * @hide
2830 */
2831 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2832 if (first.getStyle() == null) {
2833 return second.getStyle() != null;
2834 }
2835 if (second.getStyle() == null) {
2836 return true;
2837 }
2838 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2839 }
2840
2841 /**
2842 * @hide
2843 */
2844 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002845 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2846 return true;
2847 }
2848
2849 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2850 return true;
2851 }
2852 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2853 return true;
2854 }
2855 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2856 return true;
2857 }
2858
2859 return false;
2860 }
2861
2862 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2863 if (first == null && second == null) {
2864 return false;
2865 }
2866 if (first == null && second != null || first != null && second == null) {
2867 return true;
2868 }
2869
2870 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2871 return true;
2872 }
2873
2874 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2875 return true;
2876 }
2877
2878 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002879 }
2880
2881 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002882 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2883 * <p>
2884 * For backwards compatibility {@code extras} holds some references to "real" member data such
2885 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2886 * fine as long as the object stays in one process.
2887 * <p>
2888 * However, once the notification goes into a parcel each reference gets marshalled separately,
2889 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2890 */
2891 private void fixDuplicateExtras() {
2892 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002893 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2894 }
2895 }
2896
2897 /**
2898 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2899 * separate object, replace it with the field's version to avoid holding duplicate copies.
2900 */
2901 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2902 if (original != null && extras.getParcelable(extraName) != null) {
2903 extras.putParcelable(extraName, original);
2904 }
2905 }
2906
2907 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2909 * layout.
2910 *
2911 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2912 * in the view.</p>
2913 * @param context The context for your application / activity.
2914 * @param contentTitle The title that goes in the expanded entry.
2915 * @param contentText The text that goes in the expanded entry.
2916 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2917 * If this is an activity, it must include the
2918 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002919 * that you take care of task management as described in the
2920 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2921 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002922 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002923 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002924 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002926 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 public void setLatestEventInfo(Context context,
2928 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002929 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2930 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2931 new Throwable());
2932 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002933
Selim Cinek4ac6f602016-06-13 15:47:03 -07002934 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2935 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2936 }
2937
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002938 // ensure that any information already set directly is preserved
2939 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002940
2941 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002943 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
2945 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002946 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002948 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002949
2950 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952
Julia Reynoldsda303542015-11-23 14:00:20 -05002953 /**
2954 * @hide
2955 */
2956 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002957 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002958 }
2959
2960 /**
2961 * @hide
2962 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002963 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002964 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002965 }
2966
Yi Jin6b514142017-10-30 14:54:12 -07002967 /**
2968 * @hide
2969 */
2970 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2971 long token = proto.start(fieldId);
2972 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2973 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2974 proto.write(NotificationProto.FLAGS, this.flags);
2975 proto.write(NotificationProto.COLOR, this.color);
2976 proto.write(NotificationProto.CATEGORY, this.category);
2977 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2978 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2979 if (this.actions != null) {
2980 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2981 }
2982 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2983 proto.write(NotificationProto.VISIBILITY, this.visibility);
2984 }
2985 if (publicVersion != null) {
2986 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2987 }
2988 proto.end(token);
2989 }
2990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 @Override
2992 public String toString() {
2993 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002994 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002995 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002996 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002997 sb.append(priority);
2998 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002999 if (contentView != null) {
3000 sb.append(contentView.getPackage());
3001 sb.append("/0x");
3002 sb.append(Integer.toHexString(contentView.getLayoutId()));
3003 } else {
3004 sb.append("null");
3005 }
3006 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003007 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
3008 sb.append("default");
3009 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 int N = this.vibrate.length-1;
3011 sb.append("[");
3012 for (int i=0; i<N; i++) {
3013 sb.append(this.vibrate[i]);
3014 sb.append(',');
3015 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02003016 if (N != -1) {
3017 sb.append(this.vibrate[N]);
3018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 } else {
3021 sb.append("null");
3022 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003023 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003024 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003026 } else if (this.sound != null) {
3027 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 } else {
3029 sb.append("null");
3030 }
Chris Wren365b6d32015-07-16 10:39:26 -04003031 if (this.tickerText != null) {
3032 sb.append(" tick");
3033 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003036 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003037 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003038 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003039 if (this.category != null) {
3040 sb.append(" category=");
3041 sb.append(this.category);
3042 }
3043 if (this.mGroupKey != null) {
3044 sb.append(" groupKey=");
3045 sb.append(this.mGroupKey);
3046 }
3047 if (this.mSortKey != null) {
3048 sb.append(" sortKey=");
3049 sb.append(this.mSortKey);
3050 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003051 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003052 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003053 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003054 }
3055 sb.append(" vis=");
3056 sb.append(visibilityToString(this.visibility));
3057 if (this.publicVersion != null) {
3058 sb.append(" publicVersion=");
3059 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003060 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003061 if (this.mLocusId != null) {
3062 sb.append(" locusId=");
3063 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3064 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003065 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 return sb.toString();
3067 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003068
Dan Sandler1b718782014-07-18 12:43:45 -04003069 /**
3070 * {@hide}
3071 */
3072 public static String visibilityToString(int vis) {
3073 switch (vis) {
3074 case VISIBILITY_PRIVATE:
3075 return "PRIVATE";
3076 case VISIBILITY_PUBLIC:
3077 return "PUBLIC";
3078 case VISIBILITY_SECRET:
3079 return "SECRET";
3080 default:
3081 return "UNKNOWN(" + String.valueOf(vis) + ")";
3082 }
3083 }
3084
Joe Onoratocb109a02011-01-18 17:57:41 -08003085 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003086 * {@hide}
3087 */
3088 public static String priorityToString(@Priority int pri) {
3089 switch (pri) {
3090 case PRIORITY_MIN:
3091 return "MIN";
3092 case PRIORITY_LOW:
3093 return "LOW";
3094 case PRIORITY_DEFAULT:
3095 return "DEFAULT";
3096 case PRIORITY_HIGH:
3097 return "HIGH";
3098 case PRIORITY_MAX:
3099 return "MAX";
3100 default:
3101 return "UNKNOWN(" + String.valueOf(pri) + ")";
3102 }
3103 }
3104
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003105 /**
3106 * @hide
3107 */
3108 public boolean hasCompletedProgress() {
3109 // not a progress notification; can't be complete
3110 if (!extras.containsKey(EXTRA_PROGRESS)
3111 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3112 return false;
3113 }
3114 // many apps use max 0 for 'indeterminate'; not complete
3115 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3116 return false;
3117 }
3118 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3119 }
3120
Jeff Sharkey000ce802017-04-29 13:13:27 -06003121 /** @removed */
3122 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003123 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003124 return mChannelId;
3125 }
3126
3127 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003128 * Returns the id of the channel this notification posts to.
3129 */
3130 public String getChannelId() {
3131 return mChannelId;
3132 }
3133
Jeff Sharkey000ce802017-04-29 13:13:27 -06003134 /** @removed */
3135 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003136 public long getTimeout() {
3137 return mTimeout;
3138 }
3139
3140 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003141 * Returns the duration from posting after which this notification should be canceled by the
3142 * system, if it's not canceled already.
3143 */
3144 public long getTimeoutAfter() {
3145 return mTimeout;
3146 }
3147
3148 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003149 * Returns what icon should be shown for this notification if it is being displayed in a
3150 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3151 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3152 */
3153 public int getBadgeIconType() {
3154 return mBadgeIcon;
3155 }
3156
3157 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003158 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003159 *
3160 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3161 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003162 */
3163 public String getShortcutId() {
3164 return mShortcutId;
3165 }
3166
Felipe Leme90205ef2019-03-05 09:59:52 -08003167 /**
3168 * Gets the {@link LocusId} associated with this notification.
3169 *
Felipe Leme8c2360b2019-04-17 11:16:17 -07003170 * <p>Used by the Android system to correlate objects (such as
3171 * {@link ShortcutInfo} and {@link ContentCaptureContext}).
Felipe Leme90205ef2019-03-05 09:59:52 -08003172 */
3173 @Nullable
3174 public LocusId getLocusId() {
3175 return mLocusId;
3176 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003177
3178 /**
3179 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3180 */
3181 public CharSequence getSettingsText() {
3182 return mSettingsText;
3183 }
3184
Julia Reynolds13d898c2017-02-02 12:22:05 -05003185 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003186 * Returns which type of notifications in a group are responsible for audibly alerting the
3187 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3188 * {@link #GROUP_ALERT_SUMMARY}.
3189 */
3190 public @GroupAlertBehavior int getGroupAlertBehavior() {
3191 return mGroupAlertBehavior;
3192 }
3193
3194 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003195 * Returns the bubble metadata that will be used to display app content in a floating window
3196 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003197 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003198 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003199 public BubbleMetadata getBubbleMetadata() {
3200 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003201 }
3202
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003203 /**
3204 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3205 * for this notification.
3206 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003207 public boolean getAllowSystemGeneratedContextualActions() {
3208 return mAllowSystemGeneratedContextualActions;
3209 }
3210
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003211 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003212 * The small icon representing this notification in the status bar and content view.
3213 *
3214 * @return the small icon representing this notification.
3215 *
3216 * @see Builder#getSmallIcon()
3217 * @see Builder#setSmallIcon(Icon)
3218 */
3219 public Icon getSmallIcon() {
3220 return mSmallIcon;
3221 }
3222
3223 /**
3224 * Used when notifying to clean up legacy small icons.
3225 * @hide
3226 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003227 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003228 public void setSmallIcon(Icon icon) {
3229 mSmallIcon = icon;
3230 }
3231
3232 /**
3233 * The large icon shown in this notification's content view.
3234 * @see Builder#getLargeIcon()
3235 * @see Builder#setLargeIcon(Icon)
3236 */
3237 public Icon getLargeIcon() {
3238 return mLargeIcon;
3239 }
3240
3241 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003242 * @hide
3243 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003244 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003245 public boolean isGroupSummary() {
3246 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3247 }
3248
3249 /**
3250 * @hide
3251 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003252 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003253 public boolean isGroupChild() {
3254 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3255 }
3256
3257 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003258 * @hide
3259 */
3260 public boolean suppressAlertingDueToGrouping() {
3261 if (isGroupSummary()
3262 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3263 return true;
3264 } else if (isGroupChild()
3265 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3266 return true;
3267 }
3268 return false;
3269 }
3270
Tony Mak638430e2018-10-08 19:19:10 +01003271
3272 /**
3273 * Finds and returns a remote input and its corresponding action.
3274 *
3275 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3276 * @return the result pair, {@code null} if no result is found.
3277 *
3278 * @hide
3279 */
3280 @Nullable
3281 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3282 if (actions == null) {
3283 return null;
3284 }
3285 for (Notification.Action action : actions) {
3286 if (action.getRemoteInputs() == null) {
3287 continue;
3288 }
3289 RemoteInput resultRemoteInput = null;
3290 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3291 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3292 resultRemoteInput = remoteInput;
3293 }
3294 }
3295 if (resultRemoteInput != null) {
3296 return Pair.create(resultRemoteInput, action);
3297 }
3298 }
3299 return null;
3300 }
3301
Julia Reynolds30203152017-05-26 13:36:31 -04003302 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003303 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003304 *
3305 * @hide
3306 */
3307 public List<Notification.Action> getContextualActions() {
3308 if (actions == null) return Collections.emptyList();
3309
3310 List<Notification.Action> contextualActions = new ArrayList<>();
3311 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003312 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003313 contextualActions.add(action);
3314 }
3315 }
3316 return contextualActions;
3317 }
3318
3319 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003320 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003321 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003322 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003323 * content views using the platform's notification layout template. If your app supports
3324 * versions of Android as old as API level 4, you can instead use
3325 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3326 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3327 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003328 *
Scott Main183bf112012-08-13 19:12:13 -07003329 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003330 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003331 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003332 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003333 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3334 * .setContentText(subject)
3335 * .setSmallIcon(R.drawable.new_mail)
3336 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003337 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003338 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003339 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003340 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003341 /**
3342 * @hide
3343 */
3344 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3345 "android.rebuild.contentViewActionCount";
3346 /**
3347 * @hide
3348 */
3349 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3350 = "android.rebuild.bigViewActionCount";
3351 /**
3352 * @hide
3353 */
3354 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3355 = "android.rebuild.hudViewActionCount";
3356
Selim Cinek6743c0b2017-01-18 18:24:01 -08003357 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3358 SystemProperties.getBoolean("notifications.only_title", true);
3359
Selim Cinek389edcd2017-05-11 19:16:44 -07003360 /**
3361 * The lightness difference that has to be added to the primary text color to obtain the
3362 * secondary text color when the background is light.
3363 */
3364 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3365
3366 /**
3367 * The lightness difference that has to be added to the primary text color to obtain the
3368 * secondary text color when the background is dark.
3369 * A bit less then the above value, since it looks better on dark backgrounds.
3370 */
3371 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3372
Joe Onorato46439ce2010-11-19 13:56:21 -08003373 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003374 private Notification mN;
3375 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003376 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003377 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003378 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003379 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003380 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003381 private boolean mIsLegacy;
3382 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003383
3384 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003385 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3386 */
3387 private int mCachedContrastColor = COLOR_INVALID;
3388 private int mCachedContrastColorIsFor = COLOR_INVALID;
Selim Cinekc3fec682019-06-06 18:11:07 -07003389
Selim Cinek4717d862018-04-19 09:19:15 +08003390 /**
3391 * A neutral color color that can be used for icons.
3392 */
3393 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003394
3395 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003396 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3397 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3398 */
3399 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003400 private int mTextColorsAreForBackground = COLOR_INVALID;
3401 private int mPrimaryTextColor = COLOR_INVALID;
3402 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003403 private int mBackgroundColor = COLOR_INVALID;
3404 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003405 /**
3406 * A temporary location where actions are stored. If != null the view originally has action
3407 * but doesn't have any for this inflation.
3408 */
3409 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003410 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003411
Anthony Chenad4d1582017-04-10 16:07:58 -07003412 private boolean mTintActionButtons;
3413 private boolean mInNightMode;
3414
Adrian Roos70d7aa32017-01-11 15:39:06 -08003415 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003416 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003417 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003418 * @param context
3419 * A {@link Context} that will be used by the Builder to construct the
3420 * RemoteViews. The Context will not be held past the lifetime of this Builder
3421 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003422 * @param channelId
3423 * The constructed Notification will be posted on this
3424 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3425 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003426 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003427 public Builder(Context context, String channelId) {
3428 this(context, (Notification) null);
3429 mN.mChannelId = channelId;
3430 }
3431
3432 /**
3433 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3434 * instead. All posted Notifications must specify a NotificationChannel Id.
3435 */
3436 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003437 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003438 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003439 }
3440
Joe Onoratocb109a02011-01-18 17:57:41 -08003441 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003442 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003443 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003444 public Builder(Context context, Notification toAdopt) {
3445 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003446 Resources res = mContext.getResources();
3447 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3448
3449 if (res.getBoolean(R.bool.config_enableNightMode)) {
3450 Configuration currentConfig = res.getConfiguration();
3451 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3452 == Configuration.UI_MODE_NIGHT_YES;
3453 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003454
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003455 if (toAdopt == null) {
3456 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003457 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3458 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3459 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003460 mN.priority = PRIORITY_DEFAULT;
3461 mN.visibility = VISIBILITY_PRIVATE;
3462 } else {
3463 mN = toAdopt;
3464 if (mN.actions != null) {
3465 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003466 }
3467
Selim Cineke7238dd2017-12-14 17:48:32 -08003468 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3469 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3470 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003471 }
3472
Selim Cinek4ac6f602016-06-13 15:47:03 -07003473 if (mN.getSmallIcon() == null && mN.icon != 0) {
3474 setSmallIcon(mN.icon);
3475 }
3476
3477 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3478 setLargeIcon(mN.largeIcon);
3479 }
3480
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003481 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3482 if (!TextUtils.isEmpty(templateClass)) {
3483 final Class<? extends Style> styleClass
3484 = getNotificationStyleClass(templateClass);
3485 if (styleClass == null) {
3486 Log.d(TAG, "Unknown style class: " + templateClass);
3487 } else {
3488 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003489 final Constructor<? extends Style> ctor =
3490 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003491 ctor.setAccessible(true);
3492 final Style style = ctor.newInstance();
3493 style.restoreFromExtras(mN.extras);
3494
3495 if (style != null) {
3496 setStyle(style);
3497 }
3498 } catch (Throwable t) {
3499 Log.e(TAG, "Could not create Style", t);
3500 }
3501 }
3502 }
3503
3504 }
3505 }
3506
Lucas Dupina291d192018-06-07 13:59:42 -07003507 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003508 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003509 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003510 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003511 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003512 }
3513
3514 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003515 * If this notification is duplicative of a Launcher shortcut, sets the
3516 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3517 * the shortcut.
3518 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003519 * This field will be ignored by Launchers that don't support badging, don't show
3520 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003521 *
3522 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3523 * supersedes
3524 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003525 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003526 public Builder setShortcutId(String shortcutId) {
3527 mN.mShortcutId = shortcutId;
3528 return this;
3529 }
3530
3531 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003532 * Sets the {@link LocusId} associated with this notification.
3533 *
3534 * <p>This method should be called when the {@link LocusId} is used in other places (such
Felipe Leme8c2360b2019-04-17 11:16:17 -07003535 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the Android system can
3536 * correlate them.
Felipe Leme90205ef2019-03-05 09:59:52 -08003537 */
3538 @NonNull
3539 public Builder setLocusId(@Nullable LocusId locusId) {
3540 mN.mLocusId = locusId;
3541 return this;
3542 }
3543
3544 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003545 * Sets which icon to display as a badge for this notification.
3546 *
3547 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3548 * {@link #BADGE_ICON_LARGE}.
3549 *
3550 * Note: This value might be ignored, for launchers that don't support badge icons.
3551 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003552 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003553 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003554 mN.mBadgeIcon = icon;
3555 return this;
3556 }
3557
3558 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003559 * Sets the group alert behavior for this notification. Use this method to mute this
3560 * notification if alerts for this notification's group should be handled by a different
3561 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003562 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3563 * mute. For example, if you want only the summary of your group to make noise, all
3564 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003565 *
3566 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3567 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003568 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003569 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3570 mN.mGroupAlertBehavior = groupAlertBehavior;
3571 return this;
3572 }
3573
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003574 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003575 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3576 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003577 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003578 * <p>This data will be ignored unless the notification is posted to a channel that
3579 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003580 *
Mady Mellor65dcaa92019-04-03 12:21:44 -07003581 * <p>Notifications allowed to bubble that have valid bubble metadata will display in
3582 * collapsed state outside of the notification shade on unlocked devices. When a user
3583 * interacts with the collapsed state, the bubble intent will be invoked and displayed.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003584 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003585 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003586 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003587 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003588 return this;
3589 }
3590
Jeff Sharkey000ce802017-04-29 13:13:27 -06003591 /** @removed */
3592 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003593 public Builder setChannel(String channelId) {
3594 mN.mChannelId = channelId;
3595 return this;
3596 }
3597
3598 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003599 * Specifies the channel the notification should be delivered on.
3600 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003601 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003602 public Builder setChannelId(String channelId) {
3603 mN.mChannelId = channelId;
3604 return this;
3605 }
3606
Jeff Sharkey000ce802017-04-29 13:13:27 -06003607 /** @removed */
3608 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003609 public Builder setTimeout(long durationMs) {
3610 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003611 return this;
3612 }
3613
3614 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003615 * Specifies a duration in milliseconds after which this notification should be canceled,
3616 * if it is not already canceled.
3617 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003618 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003619 public Builder setTimeoutAfter(long durationMs) {
3620 mN.mTimeout = durationMs;
3621 return this;
3622 }
3623
3624 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003625 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003626 *
3627 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3628 * shown anymore by default and must be opted into by using
3629 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003630 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003631 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003632 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003633 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003634 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003636 return this;
3637 }
3638
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003640 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003641 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003642 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3643 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003644 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003645 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003646 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003647 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003648 return this;
3649 }
3650
3651 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003652 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003653 *
3654 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003655 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003656 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003657 * Useful when showing an elapsed time (like an ongoing phone call).
3658 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003659 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003660 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003661 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003662 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003663 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003664 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003665 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003666 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003667 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003668 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003669 return this;
3670 }
3671
3672 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003673 * Sets the Chronometer to count down instead of counting up.
3674 *
3675 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3676 * If it isn't set the chronometer will count up.
3677 *
3678 * @see #setUsesChronometer(boolean)
3679 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003680 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003681 public Builder setChronometerCountDown(boolean countDown) {
3682 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003683 return this;
3684 }
3685
3686 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003687 * Set the small icon resource, which will be used to represent the notification in the
3688 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003689 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003690
3691 * The platform template for the expanded view will draw this icon in the left, unless a
3692 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3693 * icon will be moved to the right-hand side.
3694 *
3695
3696 * @param icon
3697 * A resource ID in the application's package of the drawable to use.
3698 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003699 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003700 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003701 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003702 return setSmallIcon(icon != 0
3703 ? Icon.createWithResource(mContext, icon)
3704 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003705 }
3706
Joe Onoratocb109a02011-01-18 17:57:41 -08003707 /**
3708 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3709 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3710 * LevelListDrawable}.
3711 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003712 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003713 * @param level The level to use for the icon.
3714 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003715 * @see Notification#icon
3716 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003717 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003718 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003719 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003720 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003721 return setSmallIcon(icon);
3722 }
3723
3724 /**
3725 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003726 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003727 * {@link #setLargeIcon(Bitmap) large icon}).
3728 *
3729 * @param icon An Icon object to use.
3730 * @see Notification#icon
3731 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003732 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003733 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003734 mN.setSmallIcon(icon);
3735 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3736 mN.icon = icon.getResId();
3737 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003738 return this;
3739 }
3740
Joe Onoratocb109a02011-01-18 17:57:41 -08003741 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003743 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003744 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003745 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003746 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003747 return this;
3748 }
3749
Joe Onoratocb109a02011-01-18 17:57:41 -08003750 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003751 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003752 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003753 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003754 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003756 return this;
3757 }
3758
Joe Onoratocb109a02011-01-18 17:57:41 -08003759 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003760 * This provides some additional information that is displayed in the notification. No
3761 * guarantees are given where exactly it is displayed.
3762 *
3763 * <p>This information should only be provided if it provides an essential
3764 * benefit to the understanding of the notification. The more text you provide the
3765 * less readable it becomes. For example, an email client should only provide the account
3766 * name here if more than one email account has been added.</p>
3767 *
3768 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3769 * notification header area.
3770 *
3771 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3772 * this will be shown in the third line of text in the platform notification template.
3773 * You should not be using {@link #setProgress(int, int, boolean)} at the
3774 * same time on those versions; they occupy the same place.
3775 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003776 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003777 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003778 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003779 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003780 return this;
3781 }
3782
3783 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003784 * Provides text that will appear as a link to your application's settings.
3785 *
3786 * <p>This text does not appear within notification {@link Style templates} but may
3787 * appear when the user uses an affordance to learn more about the notification.
3788 * Additionally, this text will not appear unless you provide a valid link target by
3789 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3790 *
3791 * <p>This text is meant to be concise description about what the user can customize
3792 * when they click on this link. The recommended maximum length is 40 characters.
3793 * @param text
3794 * @return
3795 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003796 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003797 public Builder setSettingsText(CharSequence text) {
3798 mN.mSettingsText = safeCharSequence(text);
3799 return this;
3800 }
3801
3802 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003803 * Set the remote input history.
3804 *
3805 * This should be set to the most recent inputs that have been sent
3806 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3807 * longer relevant (e.g. for chat notifications once the other party has responded).
3808 *
3809 * The most recent input must be stored at the 0 index, the second most recent at the
3810 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3811 * and how much of each individual input is shown.
3812 *
3813 * <p>Note: The reply text will only be shown on notifications that have least one action
3814 * with a {@code RemoteInput}.</p>
3815 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003816 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003817 public Builder setRemoteInputHistory(CharSequence[] text) {
3818 if (text == null) {
3819 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3820 } else {
3821 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3822 CharSequence[] safe = new CharSequence[N];
3823 for (int i = 0; i < N; i++) {
3824 safe[i] = safeCharSequence(text[i]);
3825 }
3826 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3827 }
3828 return this;
3829 }
3830
3831 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003832 * Sets whether remote history entries view should have a spinner.
3833 * @hide
3834 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003835 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003836 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3837 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3838 return this;
3839 }
3840
3841 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003842 * Sets whether smart reply buttons should be hidden.
3843 * @hide
3844 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003845 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003846 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3847 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3848 return this;
3849 }
3850
3851 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003852 * Sets the number of items this notification represents. May be displayed as a badge count
3853 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003854 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003855 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003856 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003857 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003858 return this;
3859 }
3860
Joe Onoratocb109a02011-01-18 17:57:41 -08003861 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003862 * A small piece of additional information pertaining to this notification.
3863 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003864 * The platform template will draw this on the last line of the notification, at the far
3865 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003866 *
3867 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3868 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3869 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003870 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003871 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003872 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003873 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003874 return this;
3875 }
3876
Joe Onoratocb109a02011-01-18 17:57:41 -08003877 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003878 * Set the progress this notification represents.
3879 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003881 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003882 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003883 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003884 mN.extras.putInt(EXTRA_PROGRESS, progress);
3885 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3886 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003887 return this;
3888 }
3889
3890 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003891 * Supply a custom RemoteViews to use instead of the platform template.
3892 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003893 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003894 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003895 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003896 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003897 return setCustomContentView(views);
3898 }
3899
3900 /**
3901 * Supply custom RemoteViews to use instead of the platform template.
3902 *
3903 * This will override the layout that would otherwise be constructed by this Builder
3904 * object.
3905 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003906 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 public Builder setCustomContentView(RemoteViews contentView) {
3908 mN.contentView = contentView;
3909 return this;
3910 }
3911
3912 /**
3913 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3914 *
3915 * This will override the expanded layout that would otherwise be constructed by this
3916 * Builder object.
3917 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003918 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003919 public Builder setCustomBigContentView(RemoteViews contentView) {
3920 mN.bigContentView = contentView;
3921 return this;
3922 }
3923
3924 /**
3925 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3926 *
3927 * This will override the heads-up layout that would otherwise be constructed by this
3928 * Builder object.
3929 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003930 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003931 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3932 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003933 return this;
3934 }
3935
Joe Onoratocb109a02011-01-18 17:57:41 -08003936 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003937 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3938 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003939 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3940 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3941 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003942 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003943 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003944 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003945 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003946 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003947 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003948 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003949 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003950 return this;
3951 }
3952
Joe Onoratocb109a02011-01-18 17:57:41 -08003953 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003954 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3955 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003956 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003957 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003958 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003959 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003960 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003961 return this;
3962 }
3963
Joe Onoratocb109a02011-01-18 17:57:41 -08003964 /**
3965 * An intent to launch instead of posting the notification to the status bar.
3966 * Only for use with extremely high-priority notifications demanding the user's
3967 * <strong>immediate</strong> attention, such as an incoming phone call or
3968 * alarm clock that the user has explicitly set to a particular time.
3969 * If this facility is used for something else, please give the user an option
3970 * to turn it off and use a normal notification, as this can be extremely
3971 * disruptive.
3972 *
Chris Wren47c20a12014-06-18 17:27:29 -04003973 * <p>
3974 * The system UI may choose to display a heads-up notification, instead of
3975 * launching this intent, while the user is using the device.
3976 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003977 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3978 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3979 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003980 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003981 * @param intent The pending intent to launch.
3982 * @param highPriority Passing true will cause this notification to be sent
3983 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003984 *
3985 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003986 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003987 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003988 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003990 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3991 return this;
3992 }
3993
Joe Onoratocb109a02011-01-18 17:57:41 -08003994 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003995 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003996 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003997 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003998 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003999 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004000 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004001 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08004002 return this;
4003 }
4004
Joe Onoratocb109a02011-01-18 17:57:41 -08004005 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004006 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004007 *
Joe Onoratocb109a02011-01-18 17:57:41 -08004008 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004009 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004010 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004011 setTicker(tickerText);
4012 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004013 return this;
4014 }
4015
Joe Onoratocb109a02011-01-18 17:57:41 -08004016 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004017 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004018 *
4019 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004020 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4021 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004022 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004023 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004024 public Builder setLargeIcon(Bitmap b) {
4025 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4026 }
4027
4028 /**
4029 * Add a large icon to the notification content view.
4030 *
4031 * In the platform template, this image will be shown on the left of the notification view
4032 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4033 * badge atop the large icon).
4034 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004035 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004036 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004037 mN.mLargeIcon = icon;
4038 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004039 return this;
4040 }
4041
Joe Onoratocb109a02011-01-18 17:57:41 -08004042 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004043 * Set the sound to play.
4044 *
John Spurlockc0650f022014-07-19 13:22:39 -04004045 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4046 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004047 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004048 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004049 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004050 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004051 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004052 mN.sound = sound;
4053 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004054 return this;
4055 }
4056
Joe Onoratocb109a02011-01-18 17:57:41 -08004057 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004058 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004059 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004060 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4061 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004062 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004063 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004064 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004065 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004066 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004067 mN.sound = sound;
4068 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004069 return this;
4070 }
4071
Joe Onoratocb109a02011-01-18 17:57:41 -08004072 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004073 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4074 * use during playback.
4075 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004076 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004077 * @see Notification#sound
4078 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004079 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004080 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004081 mN.sound = sound;
4082 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004083 return this;
4084 }
4085
4086 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004087 * Set the vibration pattern to use.
4088 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004089 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4090 * <code>pattern</code> parameter.
4091 *
Chris Wren47c20a12014-06-18 17:27:29 -04004092 * <p>
4093 * A notification that vibrates is more likely to be presented as a heads-up notification.
4094 * </p>
4095 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004096 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004097 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004098 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004099 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004100 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004101 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004102 return this;
4103 }
4104
Joe Onoratocb109a02011-01-18 17:57:41 -08004105 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004106 * Set the desired color for the indicator LED on the device, as well as the
4107 * blink duty cycle (specified in milliseconds).
4108 *
4109
4110 * Not all devices will honor all (or even any) of these values.
4111 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004112 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004113 * @see Notification#ledARGB
4114 * @see Notification#ledOnMS
4115 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004116 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004117 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004118 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004119 mN.ledARGB = argb;
4120 mN.ledOnMS = onMs;
4121 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004122 if (onMs != 0 || offMs != 0) {
4123 mN.flags |= FLAG_SHOW_LIGHTS;
4124 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004125 return this;
4126 }
4127
Joe Onoratocb109a02011-01-18 17:57:41 -08004128 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004129 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004130 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004131
4132 * Ongoing notifications cannot be dismissed by the user, so your application or service
4133 * must take care of canceling them.
4134 *
4135
4136 * They are typically used to indicate a background task that the user is actively engaged
4137 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4138 * (e.g., a file download, sync operation, active network connection).
4139 *
4140
4141 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004142 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004143 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004144 public Builder setOngoing(boolean ongoing) {
4145 setFlag(FLAG_ONGOING_EVENT, ongoing);
4146 return this;
4147 }
4148
Joe Onoratocb109a02011-01-18 17:57:41 -08004149 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004150 * Set whether this notification should be colorized. When set, the color set with
4151 * {@link #setColor(int)} will be used as the background color of this notification.
4152 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004153 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4154 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004155 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004156 * For most styles, the coloring will only be applied if the notification is for a
4157 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004158 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004159 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004160 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004161 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004162 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004163 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004164 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004165 public Builder setColorized(boolean colorize) {
4166 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4167 return this;
4168 }
4169
4170 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004171 * Set this flag if you would only like the sound, vibrate
4172 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004173 *
4174 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004175 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004176 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004177 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4178 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4179 return this;
4180 }
4181
Joe Onoratocb109a02011-01-18 17:57:41 -08004182 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004183 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004184 *
4185 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004186 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004187 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004188 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004189 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004190 return this;
4191 }
4192
Joe Onoratocb109a02011-01-18 17:57:41 -08004193 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004194 * Set whether or not this notification should not bridge to other devices.
4195 *
4196 * <p>Some notifications can be bridged to other devices for remote display.
4197 * This hint can be set to recommend this notification not be bridged.
4198 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004199 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004200 public Builder setLocalOnly(boolean localOnly) {
4201 setFlag(FLAG_LOCAL_ONLY, localOnly);
4202 return this;
4203 }
4204
4205 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004206 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004207 * <p>
4208 * The value should be one or more of the following fields combined with
4209 * bitwise-or:
4210 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4211 * <p>
4212 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004213 *
4214 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004215 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004216 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004217 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004218 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004219 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004220 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004221 return this;
4222 }
4223
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004224 /**
4225 * Set the priority of this notification.
4226 *
4227 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004228 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004229 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004230 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004231 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004232 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004233 return this;
4234 }
Joe Malin8d40d042012-11-05 11:36:40 -08004235
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004236 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004237 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004238 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004239 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004240 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004241 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004242 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004243 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004244 return this;
4245 }
4246
4247 /**
Chris Wrendde75302014-03-26 17:24:15 -04004248 * Add a person that is relevant to this notification.
4249 *
Chris Wrene6c48932014-09-29 17:19:27 -04004250 * <P>
4251 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004252 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4253 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4254 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004255 * </P>
4256 *
4257 * <P>
4258 * The person should be specified by the {@code String} representation of a
4259 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4260 * </P>
4261 *
4262 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4263 * URIs. The path part of these URIs must exist in the contacts database, in the
4264 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4265 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004266 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4267 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004268 * </P>
4269 *
4270 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004271 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004272 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004273 */
Chris Wrene6c48932014-09-29 17:19:27 -04004274 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004275 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004276 return this;
4277 }
4278
4279 /**
4280 * Add a person that is relevant to this notification.
4281 *
4282 * <P>
4283 * Depending on user preferences, this annotation may allow the notification to pass
4284 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4285 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4286 * appear more prominently in the user interface.
4287 * </P>
4288 *
4289 * <P>
4290 * A person should usually contain a uri in order to benefit from the ranking boost.
4291 * However, even if no uri is provided, it's beneficial to provide other people in the
4292 * notification, such that listeners and voice only devices can announce and handle them
4293 * properly.
4294 * </P>
4295 *
4296 * @param person the person to add.
4297 * @see Notification#EXTRA_PEOPLE_LIST
4298 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004299 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004300 public Builder addPerson(Person person) {
4301 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004302 return this;
4303 }
4304
4305 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004306 * Set this notification to be part of a group of notifications sharing the same key.
4307 * Grouped notifications may display in a cluster or stack on devices which
4308 * support such rendering.
4309 *
4310 * <p>To make this notification the summary for its group, also call
4311 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4312 * {@link #setSortKey}.
4313 * @param groupKey The group key of the group.
4314 * @return this object for method chaining
4315 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004316 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004317 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004318 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004319 return this;
4320 }
4321
4322 /**
4323 * Set this notification to be the group summary for a group of notifications.
4324 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004325 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4326 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004327 * @param isGroupSummary Whether this notification should be a group summary.
4328 * @return this object for method chaining
4329 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004330 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004331 public Builder setGroupSummary(boolean isGroupSummary) {
4332 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4333 return this;
4334 }
4335
4336 /**
4337 * Set a sort key that orders this notification among other notifications from the
4338 * same package. This can be useful if an external sort was already applied and an app
4339 * would like to preserve this. Notifications will be sorted lexicographically using this
4340 * value, although providing different priorities in addition to providing sort key may
4341 * cause this value to be ignored.
4342 *
4343 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004344 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004345 *
4346 * @see String#compareTo(String)
4347 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004348 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004349 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004350 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004351 return this;
4352 }
4353
4354 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004355 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004356 *
Griff Hazen720042b2014-02-24 15:46:56 -08004357 * <p>Values within the Bundle will replace existing extras values in this Builder.
4358 *
4359 * @see Notification#extras
4360 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004361 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004362 public Builder addExtras(Bundle extras) {
4363 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004364 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004365 }
4366 return this;
4367 }
4368
4369 /**
4370 * Set metadata for this notification.
4371 *
4372 * <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 -04004373 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004374 * called.
4375 *
Griff Hazen720042b2014-02-24 15:46:56 -08004376 * <p>Replaces any existing extras values with those from the provided Bundle.
4377 * Use {@link #addExtras} to merge in metadata instead.
4378 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004379 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004380 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004381 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004382 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004383 if (extras != null) {
4384 mUserExtras = extras;
4385 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004386 return this;
4387 }
4388
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004389 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004390 * Get the current metadata Bundle used by this notification Builder.
4391 *
4392 * <p>The returned Bundle is shared with this Builder.
4393 *
4394 * <p>The current contents of this Bundle are copied into the Notification each time
4395 * {@link #build()} is called.
4396 *
4397 * @see Notification#extras
4398 */
4399 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004400 return mUserExtras;
4401 }
4402
4403 private Bundle getAllExtras() {
4404 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4405 saveExtras.putAll(mN.extras);
4406 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004407 }
4408
4409 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004410 * Add an action to this notification. Actions are typically displayed by
4411 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004412 * <p>
4413 * Every action must have an icon (32dp square and matching the
4414 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4415 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4416 * <p>
4417 * A notification in its expanded form can display up to 3 actions, from left to right in
4418 * the order they were added. Actions will not be displayed when the notification is
4419 * collapsed, however, so be sure that any essential functions may be accessed by the user
4420 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004421 *
4422 * @param icon Resource ID of a drawable that represents the action.
4423 * @param title Text describing the action.
4424 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004425 *
4426 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004427 */
Dan Sandler86647982015-05-13 23:41:13 -04004428 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004429 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004430 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004431 return this;
4432 }
4433
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004434 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004435 * Add an action to this notification. Actions are typically displayed by
4436 * the system as a button adjacent to the notification content.
4437 * <p>
4438 * Every action must have an icon (32dp square and matching the
4439 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4440 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4441 * <p>
4442 * A notification in its expanded form can display up to 3 actions, from left to right in
4443 * the order they were added. Actions will not be displayed when the notification is
4444 * collapsed, however, so be sure that any essential functions may be accessed by the user
4445 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4446 *
4447 * @param action The action to add.
4448 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004449 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004450 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004451 if (action != null) {
4452 mActions.add(action);
4453 }
Griff Hazen959591e2014-05-15 22:26:18 -07004454 return this;
4455 }
4456
4457 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004458 * Alter the complete list of actions attached to this notification.
4459 * @see #addAction(Action).
4460 *
4461 * @param actions
4462 * @return
4463 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004464 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004465 public Builder setActions(Action... actions) {
4466 mActions.clear();
4467 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004468 if (actions[i] != null) {
4469 mActions.add(actions[i]);
4470 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004471 }
4472 return this;
4473 }
4474
4475 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004476 * Add a rich notification style to be applied at build time.
4477 *
4478 * @param style Object responsible for modifying the notification style.
4479 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004480 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004481 public Builder setStyle(Style style) {
4482 if (mStyle != style) {
4483 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004484 if (mStyle != null) {
4485 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004486 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4487 } else {
4488 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004489 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004490 }
4491 return this;
4492 }
4493
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004494 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004495 * Returns the style set by {@link #setStyle(Style)}.
4496 */
4497 public Style getStyle() {
4498 return mStyle;
4499 }
4500
4501 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004502 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004503 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004504 * @return The same Builder.
4505 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004506 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004507 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004508 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004509 return this;
4510 }
4511
4512 /**
4513 * Supply a replacement Notification whose contents should be shown in insecure contexts
4514 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4515 * @param n A replacement notification, presumably with some or all info redacted.
4516 * @return The same Builder.
4517 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004518 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004519 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004520 if (n != null) {
4521 mN.publicVersion = new Notification();
4522 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4523 } else {
4524 mN.publicVersion = null;
4525 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004526 return this;
4527 }
4528
Griff Hazenb720abe2014-05-20 13:15:30 -07004529 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004530 * Apply an extender to this notification builder. Extenders may be used to add
4531 * metadata or change options on this builder.
4532 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004533 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004534 public Builder extend(Extender extender) {
4535 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004536 return this;
4537 }
4538
Dan Sandler4e787062015-06-17 15:09:48 -04004539 /**
4540 * @hide
4541 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004542 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004543 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004544 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004545 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004546 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004547 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004548 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004549 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004550 }
4551
Dan Sandler26e81cf2014-05-06 10:01:27 -04004552 /**
4553 * Sets {@link Notification#color}.
4554 *
4555 * @param argb The accent color to use
4556 *
4557 * @return The same Builder.
4558 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004559 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004560 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004561 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004562 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004563 return this;
4564 }
4565
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004566 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004567 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4568 // This user can never be a badged profile,
4569 // and also includes USER_ALL system notifications.
4570 return null;
4571 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004572 // Note: This assumes that the current user can read the profile badge of the
4573 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004574 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004575 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004576 }
4577
4578 private Bitmap getProfileBadge() {
4579 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004580 if (badge == null) {
4581 return null;
4582 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004583 final int size = mContext.getResources().getDimensionPixelSize(
4584 R.dimen.notification_badge_size);
4585 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004586 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004587 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004588 badge.draw(canvas);
4589 return bitmap;
4590 }
4591
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004592 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004593 Bitmap profileBadge = getProfileBadge();
4594
Kenny Guy98193ea2014-07-24 19:54:37 +01004595 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004596 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4597 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004598 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004599 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004600 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004601 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004602 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004603 }
4604
Gus Prevasbae5ba32018-11-28 15:45:13 -05004605 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4606 contentView.setDrawableTint(
4607 R.id.alerted_icon,
4608 false /* targetBackground */,
4609 getNeutralColor(p),
4610 PorterDuff.Mode.SRC_ATOP);
4611 }
4612
Julia Reynoldsfc640012018-02-21 12:25:27 -05004613 /**
4614 * @hide
4615 */
4616 public boolean usesStandardHeader() {
4617 if (mN.mUsesStandardHeader) {
4618 return true;
4619 }
4620 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4621 if (mN.contentView == null && mN.bigContentView == null) {
4622 return true;
4623 }
4624 }
4625 boolean contentViewUsesHeader = mN.contentView == null
4626 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4627 boolean bigContentViewUsesHeader = mN.bigContentView == null
4628 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4629 return contentViewUsesHeader && bigContentViewUsesHeader;
4630 }
4631
Christoph Studerfe718432014-09-01 18:21:18 +02004632 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004633 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004634 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004635 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004636 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004637 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004638 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004639 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004640 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004641 }
4642
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004643 /**
4644 * Resets the notification header to its original state
4645 */
4646 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004647 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4648 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004649 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004650 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004651 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004652 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004653 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004654 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4655 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004656 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004657 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004658 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004659 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004660 contentView.setImageViewIcon(R.id.profile_badge, null);
4661 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004662 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004663 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004664 }
4665
Selim Cinek384804b2018-04-18 14:31:07 +08004666 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4667 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4668 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004669 }
4670
Selim Cinek384804b2018-04-18 14:31:07 +08004671 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4672 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004673 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004674
Christoph Studerfe718432014-09-01 18:21:18 +02004675 resetStandardTemplate(contentView);
4676
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004677 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004678 updateBackgroundColor(contentView, p);
4679 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004680 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004681 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004682 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004683 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004684 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004685 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004686 contentView.setViewLayoutWidth(R.id.title, showProgress
4687 ? ViewGroup.LayoutParams.WRAP_CONTENT
4688 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004689 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004690 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004691 int textId = showProgress ? com.android.internal.R.id.text_line_1
4692 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004693 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004694 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004695 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004696 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004697
Selim Cinek279fa862016-06-14 10:57:25 -07004698 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004699
Selim Cinek29603462015-11-17 19:04:39 -08004700 return contentView;
4701 }
4702
Selim Cinek48f66b72017-08-18 16:17:51 -07004703 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004704 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004705 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004706 }
4707 return text;
4708 }
4709
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004710 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4711 StandardTemplateParams p) {
4712 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004713 contentView.setTextColor(id, mPrimaryTextColor);
4714 }
4715
Selim Cinek48f66b72017-08-18 16:17:51 -07004716 private boolean hasForegroundColor() {
4717 return mForegroundColor != COLOR_INVALID;
4718 }
4719
Selim Cinek389edcd2017-05-11 19:16:44 -07004720 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004721 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004722 * @hide
4723 */
4724 @VisibleForTesting
4725 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004726 return getPrimaryTextColor(mParams);
4727 }
4728
4729 /**
4730 * @param p the template params to inflate this with
4731 * @return the primary text color
4732 * @hide
4733 */
4734 @VisibleForTesting
4735 public int getPrimaryTextColor(StandardTemplateParams p) {
4736 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004737 return mPrimaryTextColor;
4738 }
4739
Selim Cinek389edcd2017-05-11 19:16:44 -07004740 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004741 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004742 * @hide
4743 */
4744 @VisibleForTesting
4745 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004746 return getSecondaryTextColor(mParams);
4747 }
4748
4749 /**
4750 * @param p the template params to inflate this with
4751 * @return the secondary text color
4752 * @hide
4753 */
4754 @VisibleForTesting
4755 public int getSecondaryTextColor(StandardTemplateParams p) {
4756 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004757 return mSecondaryTextColor;
4758 }
4759
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004760 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4761 StandardTemplateParams p) {
4762 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004763 contentView.setTextColor(id, mSecondaryTextColor);
4764 }
4765
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004766 private void ensureColors(StandardTemplateParams p) {
4767 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004768 if (mPrimaryTextColor == COLOR_INVALID
4769 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004770 || mTextColorsAreForBackground != backgroundColor) {
4771 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004772 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004773 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004774 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004775 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004776 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004777 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004778 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004779 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004780 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004781 mSecondaryTextColor, backgroundColor, 4.5);
4782 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004783 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004784 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4785 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4786 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004787 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004788 // We only respect the given colors if worst case Black or White still has
4789 // contrast
4790 boolean backgroundLight = backLum > textLum
4791 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4792 || backLum <= textLum
4793 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004794 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004795 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004796 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004797 mForegroundColor,
4798 backgroundColor,
4799 true /* findFG */,
4800 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004801 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004802 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004803 } else {
4804 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004805 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004806 mForegroundColor,
4807 backgroundColor,
4808 true /* findFG */,
4809 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004810 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004811 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004812 }
4813 } else {
4814 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004815 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004816 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4817 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004818 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004819 backgroundColor) < 4.5f) {
4820 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004821 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004822 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004823 mSecondaryTextColor,
4824 backgroundColor,
4825 true /* findFG */,
4826 4.5f);
4827 } else {
4828 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004829 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004830 mSecondaryTextColor,
4831 backgroundColor,
4832 true /* findFG */,
4833 4.5f);
4834 }
Lucas Dupina291d192018-06-07 13:59:42 -07004835 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004836 mSecondaryTextColor, backgroundLight
4837 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4838 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004839 }
4840 }
4841 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004842 }
4843 }
4844
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004845 private void updateBackgroundColor(RemoteViews contentView,
4846 StandardTemplateParams p) {
4847 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004848 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004849 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004850 } else {
4851 // Clear it!
4852 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4853 0);
4854 }
4855 }
4856
Selim Cinek860b6da2015-12-16 19:02:19 -08004857 /**
4858 * @param remoteView the remote view to update the minheight in
4859 * @param hasMinHeight does it have a mimHeight
4860 * @hide
4861 */
4862 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4863 int minHeight = 0;
4864 if (hasMinHeight) {
4865 // we need to set the minHeight of the notification
4866 minHeight = mContext.getResources().getDimensionPixelSize(
4867 com.android.internal.R.dimen.notification_min_content_height);
4868 }
4869 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4870 }
4871
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004872 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4873 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004874 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4875 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4876 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004877 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004878 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004879 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004880 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004881 contentView.setProgressBackgroundTintList(
4882 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4883 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004884 if (getRawColor(p) != COLOR_DEFAULT) {
Beth Thibodeau38275c32019-05-08 16:36:59 -04004885 int color = isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p);
4886 ColorStateList colorStateList = ColorStateList.valueOf(color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004887 contentView.setProgressTintList(R.id.progress, colorStateList);
4888 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004889 }
Selim Cinek29603462015-11-17 19:04:39 -08004890 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004891 } else {
4892 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004893 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004894 }
Joe Onorato561d3852010-11-20 18:09:34 -08004895 }
4896
Selim Cinek384804b2018-04-18 14:31:07 +08004897 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4898 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004899 boolean largeIconShown = bindLargeIcon(contentView, p);
4900 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004901 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004902 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004903 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004904 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4905 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4906 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4907 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004908 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004909 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004910 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004911 }
4912 }
4913
Selim Cinek1c72fa02018-04-23 18:00:54 +08004914 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4915 int marginEnd = 0;
4916 int contentMargin = mContext.getResources().getDimensionPixelSize(
4917 R.dimen.notification_content_margin_end);
4918 int iconSize = mContext.getResources().getDimensionPixelSize(
4919 R.dimen.notification_right_icon_size);
4920 if (replyIconShown) {
4921 // The size of the reply icon
4922 marginEnd += iconSize;
4923
4924 int replyInset = mContext.getResources().getDimensionPixelSize(
4925 R.dimen.notification_reply_inset);
4926 // We're subtracting the inset of the reply icon to make sure it's
4927 // aligned nicely on the right, and remove it from the following padding
4928 marginEnd -= replyInset * 2;
4929 }
4930 if (largeIconShown) {
4931 // adding size of the right icon
4932 marginEnd += iconSize;
4933
4934 if (replyIconShown) {
4935 // We also add some padding to the reply icon if it's around
4936 marginEnd += contentMargin;
4937 }
4938 }
4939 if (replyIconShown || largeIconShown) {
4940 // The padding to the content
4941 marginEnd += contentMargin;
4942 }
4943 return marginEnd;
4944 }
4945
Selim Cinek384804b2018-04-18 14:31:07 +08004946 /**
4947 * Bind the large icon.
4948 * @return if the largeIcon is visible
4949 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004950 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004951 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4952 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4953 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004954 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004955 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004956 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4957 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004958 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004959 }
Selim Cinek384804b2018-04-18 14:31:07 +08004960 return showLargeIcon;
4961 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004962
Selim Cinek384804b2018-04-18 14:31:07 +08004963 /**
4964 * Bind the reply icon.
4965 * @return if the reply icon is visible
4966 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004967 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004968 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004969 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004970 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004971 action = findReplyAction();
4972 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004973 }
Selim Cinek384804b2018-04-18 14:31:07 +08004974 if (actionVisible) {
4975 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4976 contentView.setDrawableTint(R.id.reply_icon_action,
4977 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004978 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004979 PorterDuff.Mode.SRC_ATOP);
4980 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4981 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4982 } else {
4983 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4984 }
4985 contentView.setViewVisibility(R.id.reply_icon_action,
4986 actionVisible ? View.VISIBLE : View.GONE);
4987 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004988 }
4989
4990 private Action findReplyAction() {
4991 ArrayList<Action> actions = mActions;
4992 if (mOriginalActions != null) {
4993 actions = mOriginalActions;
4994 }
4995 int numActions = actions.size();
4996 for (int i = 0; i < numActions; i++) {
4997 Action action = actions.get(i);
4998 if (hasValidRemoteInput(action)) {
4999 return action;
5000 }
5001 }
5002 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005003 }
5004
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005005 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
5006 bindSmallIcon(contentView, p);
5007 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005008 bindHeaderText(contentView, p);
5009 bindHeaderTextSecondary(contentView, p);
5010 bindHeaderChronometerAndTime(contentView, p);
5011 bindProfileBadge(contentView, p);
5012 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005013 bindActivePermissions(contentView, p);
5014 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005015 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005016 }
5017
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005018 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005019 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005020 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5021 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5022 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5023 }
5024
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005025 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5026 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005027 contentView.setDrawableTint(R.id.expand_button, false, color,
5028 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005029 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005030 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005031 }
5032
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005033 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5034 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005035 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005036 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005037 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005038 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5039 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5040 contentView.setLong(R.id.chronometer, "setBase",
5041 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5042 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005043 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005044 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005045 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005046 } else {
5047 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5048 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005049 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005050 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005051 } else {
5052 // We still want a time to be set but gone, such that we can show and hide it
5053 // on demand in case it's a child notification without anything in the header
5054 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005055 }
5056 }
5057
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005058 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5059 CharSequence summaryText = p.summaryText;
5060 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005061 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005062 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005063 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005064 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005065 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5066 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005067 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005068 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005069 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005070 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005071 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005072 processLegacyText(summaryText)));
5073 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005074 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5075 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005076 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005077 }
5078 }
5079
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005080 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5081 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005082 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005083 processLegacyText(p.headerTextSecondary)));
5084 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005085 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5086 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005087 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005088 }
5089 }
5090
Adrian Rooseba05822016-04-22 17:09:27 -07005091 /**
5092 * @hide
5093 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005094 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005095 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005096 CharSequence name = null;
5097 final PackageManager pm = mContext.getPackageManager();
5098 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5099 // only system packages which lump together a bunch of unrelated stuff
5100 // may substitute a different name to make the purpose of the
5101 // notification more clear. the correct package label should always
5102 // be accessible via SystemUI.
5103 final String pkg = mContext.getPackageName();
5104 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5105 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5106 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5107 name = subName;
5108 } else {
5109 Log.w(TAG, "warning: pkg "
5110 + pkg + " attempting to substitute app name '" + subName
5111 + "' without holding perm "
5112 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5113 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005114 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005115 if (TextUtils.isEmpty(name)) {
5116 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5117 }
5118 if (TextUtils.isEmpty(name)) {
5119 // still nothing?
5120 return null;
5121 }
5122
5123 return String.valueOf(name);
5124 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005125 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005126 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005127 if (isColorized(p)) {
5128 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005129 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005130 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005131 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005132 }
5133
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005134 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005135 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005136 }
5137
5138 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005139 if (mN.mSmallIcon == null && mN.icon != 0) {
5140 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5141 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005142 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005143 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005144 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005145 }
5146
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005147 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005148 * @return true if the built notification will show the time or the chronometer; false
5149 * otherwise
5150 */
5151 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005152 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005153 }
5154
Christoph Studerfe718432014-09-01 18:21:18 +02005155 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005156 // actions_container is only reset when there are no actions to avoid focus issues with
5157 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005158 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005159 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005160
5161 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5162 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005163 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5164 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005165
5166 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5167 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5168 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5169 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005170
Selim Cineked64a142018-02-06 18:06:01 -08005171 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5172 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005173 }
5174
Selim Cinek384804b2018-04-18 14:31:07 +08005175 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5176 TemplateBindResult result) {
5177 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5178 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005179 }
5180
Gustav Sennton1463d832018-11-06 16:12:48 +00005181 private static List<Notification.Action> filterOutContextualActions(
5182 List<Notification.Action> actions) {
5183 List<Notification.Action> nonContextualActions = new ArrayList<>();
5184 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005185 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005186 nonContextualActions.add(action);
5187 }
5188 }
5189 return nonContextualActions;
5190 }
5191
Adrian Roos70d7aa32017-01-11 15:39:06 -08005192 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005193 StandardTemplateParams p, TemplateBindResult result) {
5194 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005195
Christoph Studerfe718432014-09-01 18:21:18 +02005196 resetStandardTemplateWithActions(big);
5197
Adrian Roose458aa82015-12-08 16:17:19 -08005198 boolean validRemoteInput = false;
5199
Gustav Sennton1463d832018-11-06 16:12:48 +00005200 // In the UI contextual actions appear separately from the standard actions, so we
5201 // filter them out here.
5202 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5203
5204 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005205 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005206 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005207 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005208 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005209 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005210 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005211 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005212 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005213 Action action = nonContextualActions.get(i);
5214
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005215 boolean actionHasValidInput = hasValidRemoteInput(action);
5216 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005217
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005218 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005219 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005220 // Clear the drawable
5221 button.setInt(R.id.action0, "setBackgroundResource", 0);
5222 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005223 big.addView(R.id.actions, button);
5224 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005225 } else {
5226 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005227 }
Adrian Roose458aa82015-12-08 16:17:19 -08005228
5229 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005230 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005231 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5232 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005233 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005234 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005235 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5236 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005237 big.setTextViewText(R.id.notification_material_reply_text_1,
5238 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005239 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005240 big.setViewVisibility(R.id.notification_material_reply_progress,
5241 showSpinner ? View.VISIBLE : View.GONE);
5242 big.setProgressIndeterminateTintList(
5243 R.id.notification_material_reply_progress,
5244 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005245 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005246
Selim Cinekbee4e072018-05-21 22:06:43 -07005247 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5248 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005249 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005250 big.setTextViewText(R.id.notification_material_reply_text_2,
5251 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005252 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005253
Selim Cinekbee4e072018-05-21 22:06:43 -07005254 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5255 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005256 big.setViewVisibility(
5257 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005258 big.setTextViewText(R.id.notification_material_reply_text_3,
5259 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005260 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005261 }
5262 }
5263 }
5264
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005265 return big;
5266 }
5267
Adrian Roose458aa82015-12-08 16:17:19 -08005268 private boolean hasValidRemoteInput(Action action) {
5269 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5270 // Weird actions
5271 return false;
5272 }
5273
5274 RemoteInput[] remoteInputs = action.getRemoteInputs();
5275 if (remoteInputs == null) {
5276 return false;
5277 }
5278
5279 for (RemoteInput r : remoteInputs) {
5280 CharSequence[] choices = r.getChoices();
5281 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5282 return true;
5283 }
5284 }
5285 return false;
5286 }
5287
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005288 /**
5289 * Construct a RemoteViews for the final 1U notification layout. In order:
5290 * 1. Custom contentView from the caller
5291 * 2. Style's proposed content view
5292 * 3. Standard template view
5293 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005294 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005295 return createContentView(false /* increasedheight */ );
5296 }
5297
5298 /**
5299 * Construct a RemoteViews for the smaller content view.
5300 *
5301 * @param increasedHeight true if this layout be created with an increased height. Some
5302 * styles may support showing more then just that basic 1U size
5303 * and the system may decide to render important notifications
5304 * slightly bigger even when collapsed.
5305 *
5306 * @hide
5307 */
5308 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005309 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005310 return mN.contentView;
5311 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005312 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005313 if (styleView != null) {
5314 return styleView;
5315 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005316 }
Selim Cinek384804b2018-04-18 14:31:07 +08005317 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005318 }
5319
Selim Cineka7679b62017-05-10 16:33:25 -07005320 private boolean useExistingRemoteView() {
5321 return mStyle == null || (!mStyle.displayCustomViewInline()
5322 && !mRebuildStyledRemoteViews);
5323 }
5324
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005325 /**
5326 * Construct a RemoteViews for the final big notification layout.
5327 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005328 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005329 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005330 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005331 return mN.bigContentView;
5332 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005333 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005334 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005335 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005336 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5337 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005338 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005339 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005340 return result;
5341 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005342
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005343 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005344 * Construct a RemoteViews for the final notification header only. This will not be
5345 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005346 *
5347 * @hide
5348 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005349 public RemoteViews makeNotificationHeader() {
5350 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005351 }
5352
5353 /**
5354 * Construct a RemoteViews for the final notification header only. This will not be
5355 * colorized.
5356 *
5357 * @param p the template params to inflate this with
5358 */
5359 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5360 // Headers on their own are never colorized
5361 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005362 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005363 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005364 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005365 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005366 return header;
5367 }
5368
Adrian Roos487374f2017-01-11 15:48:14 -08005369 /**
5370 * Construct a RemoteViews for the ambient version of the notification.
5371 *
5372 * @hide
5373 */
5374 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005375 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5376 if (headsUpContentView != null) {
5377 return headsUpContentView;
5378 }
5379 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005380 }
5381
Selim Cinek29603462015-11-17 19:04:39 -08005382 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005383 if (result != null) {
5384 result.setViewVisibility(R.id.text_line_1, View.GONE);
5385 }
Selim Cinek29603462015-11-17 19:04:39 -08005386 }
5387
Selim Cinek6743c0b2017-01-18 18:24:01 -08005388 /**
5389 * Adapt the Notification header if this view is used as an expanded view.
5390 *
5391 * @hide
5392 */
5393 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005394 if (result != null) {
5395 result.setBoolean(R.id.notification_header, "setExpanded", true);
5396 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005397 }
5398
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005399 /**
5400 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005401 *
5402 * @param increasedHeight true if this layout be created with an increased height. Some
5403 * styles may support showing more then just that basic 1U size
5404 * and the system may decide to render important notifications
5405 * slightly bigger even when collapsed.
5406 *
5407 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005408 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005409 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005410 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005411 return mN.headsUpContentView;
5412 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005413 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5414 if (styleView != null) {
5415 return styleView;
5416 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005417 } else if (mActions.size() == 0) {
5418 return null;
5419 }
5420
Selim Cinekbee4e072018-05-21 22:06:43 -07005421 // We only want at most a single remote input history to be shown here, otherwise
5422 // the content would become squished.
5423 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5424 .setMaxRemoteInputHistory(1);
5425 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5426 p,
5427 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005428 }
5429
Selim Cinek624c02db2015-12-14 21:00:02 -08005430 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005431 * Construct a RemoteViews for the final heads-up notification layout.
5432 */
5433 public RemoteViews createHeadsUpContentView() {
5434 return createHeadsUpContentView(false /* useIncreasedHeight */);
5435 }
5436
5437 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005438 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5439 *
Selim Cinekabcc2012019-07-23 18:44:07 -07005440 * @param isLowPriority is this notification low priority
Selim Cinek624c02db2015-12-14 21:00:02 -08005441 * @hide
5442 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005443 @UnsupportedAppUsage
Selim Cinekabcc2012019-07-23 18:44:07 -07005444 public RemoteViews makePublicContentView(boolean isLowPriority) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005445 if (mN.publicVersion != null) {
5446 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Selim Cinekc3fec682019-06-06 18:11:07 -07005447 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005448 }
5449 Bundle savedBundle = mN.extras;
5450 Style style = mStyle;
5451 mStyle = null;
5452 Icon largeIcon = mN.mLargeIcon;
5453 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005454 Bitmap largeIconLegacy = mN.largeIcon;
5455 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005456 ArrayList<Action> actions = mActions;
5457 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005458 Bundle publicExtras = new Bundle();
5459 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5460 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5461 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5462 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005463 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5464 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005465 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5466 if (appName != null) {
5467 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5468 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005469 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005470 RemoteViews view;
Selim Cinekabcc2012019-07-23 18:44:07 -07005471 StandardTemplateParams params = mParams.reset().fillTextsFrom(this);
5472 if (isLowPriority) {
5473 params.forceDefaultColor();
5474 }
5475 view = makeNotificationHeader(params);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005476 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005477 mN.extras = savedBundle;
5478 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005479 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005480 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005481 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005482 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005483 }
5484
Selim Cinek6743c0b2017-01-18 18:24:01 -08005485 /**
5486 * Construct a content view for the display when low - priority
5487 *
5488 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5489 * a new subtext is created consisting of the content of the
5490 * notification.
5491 * @hide
5492 */
5493 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005494 StandardTemplateParams p = mParams.reset()
5495 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005496 .fillTextsFrom(this);
5497 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5498 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005499 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005500 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005501 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005502 return header;
5503 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005504
Selim Cinek6743c0b2017-01-18 18:24:01 -08005505 private CharSequence createSummaryText() {
5506 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5507 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5508 return titleText;
5509 }
5510 SpannableStringBuilder summary = new SpannableStringBuilder();
5511 if (titleText == null) {
5512 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5513 }
5514 BidiFormatter bidi = BidiFormatter.getInstance();
5515 if (titleText != null) {
5516 summary.append(bidi.unicodeWrap(titleText));
5517 }
5518 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5519 if (titleText != null && contentText != null) {
5520 summary.append(bidi.unicodeWrap(mContext.getText(
5521 R.string.notification_header_divider_symbol_with_spaces)));
5522 }
5523 if (contentText != null) {
5524 summary.append(bidi.unicodeWrap(contentText));
5525 }
5526 return summary;
5527 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005528
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005529 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005530 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005531 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005532 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005533 emphazisedMode ? getEmphasizedActionLayoutResource()
5534 : tombstone ? getActionTombstoneLayoutResource()
5535 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005536 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005537 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005538 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005539 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005540 if (action.mRemoteInputs != null) {
5541 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5542 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005543 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005544 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005545 CharSequence title = action.title;
5546 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005547 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005548 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005549 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005550 } else {
5551 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005552 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005553 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005554 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005555 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005556 int rippleColor;
5557 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5558 if (hasColorOverride) {
5559 // There's a span spanning the full text, let's take it and use it as the
5560 // background color
5561 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005562 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005563 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005564 button.setTextColor(R.id.action0, textColor);
5565 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005566 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
Lucas Dupinf15c5e52019-04-01 19:00:12 -07005567 && mTintActionButtons && !mInNightMode) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005568 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005569 button.setTextColor(R.id.action0, rippleColor);
5570 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005571 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005572 }
Selim Cinek396caca2018-04-10 17:46:46 -07005573 // We only want about 20% alpha for the ripple
5574 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5575 button.setColorStateList(R.id.action0, "setRippleColor",
5576 ColorStateList.valueOf(rippleColor));
5577 button.setColorStateList(R.id.action0, "setButtonBackground",
5578 ColorStateList.valueOf(background));
5579 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005580 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005581 button.setTextViewText(R.id.action0, processTextSpans(
5582 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005583 if (isColorized(p)) {
5584 setTextViewColorPrimary(button, R.id.action0, p);
5585 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005586 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005587 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005588 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005589 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5590 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005591 return button;
5592 }
5593
Joe Onoratocb109a02011-01-18 17:57:41 -08005594 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005595 * Ensures contrast on color spans against a background color. also returns the color of the
5596 * text if a span was found that spans over the whole text.
5597 *
5598 * @param charSequence the charSequence on which the spans are
5599 * @param background the background color to ensure the contrast against
5600 * @param outResultColor an array in which a color will be returned as the first element if
5601 * there exists a full length color span.
5602 * @return the contrasted charSequence
5603 */
5604 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5605 ColorStateList[] outResultColor) {
5606 if (charSequence instanceof Spanned) {
5607 Spanned ss = (Spanned) charSequence;
5608 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5609 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5610 for (Object span : spans) {
5611 Object resultSpan = span;
5612 int spanStart = ss.getSpanStart(span);
5613 int spanEnd = ss.getSpanEnd(span);
5614 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5615 if (resultSpan instanceof CharacterStyle) {
5616 resultSpan = ((CharacterStyle) span).getUnderlying();
5617 }
5618 if (resultSpan instanceof TextAppearanceSpan) {
5619 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5620 ColorStateList textColor = originalSpan.getTextColor();
5621 if (textColor != null) {
5622 int[] colors = textColor.getColors();
5623 int[] newColors = new int[colors.length];
5624 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005625 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005626 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005627 }
5628 textColor = new ColorStateList(textColor.getStates().clone(),
5629 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005630 if (fullLength) {
5631 outResultColor[0] = textColor;
5632 // Let's drop the color from the span
5633 textColor = null;
5634 }
Selim Cinek981962e2016-07-20 20:41:58 -07005635 resultSpan = new TextAppearanceSpan(
5636 originalSpan.getFamily(),
5637 originalSpan.getTextStyle(),
5638 originalSpan.getTextSize(),
5639 textColor,
5640 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005641 }
5642 } else if (resultSpan instanceof ForegroundColorSpan) {
5643 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5644 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005645 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005646 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005647 if (fullLength) {
5648 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005649 resultSpan = null;
5650 } else {
5651 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005652 }
5653 } else {
5654 resultSpan = span;
5655 }
Selim Cinek396caca2018-04-10 17:46:46 -07005656 if (resultSpan != null) {
5657 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5658 }
Selim Cinek981962e2016-07-20 20:41:58 -07005659 }
5660 return builder;
5661 }
5662 return charSequence;
5663 }
5664
5665 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005666 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005667 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005668 */
5669 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005670 if (!mIsLegacyInitialized) {
5671 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5672 < Build.VERSION_CODES.LOLLIPOP;
5673 mIsLegacyInitialized = true;
5674 }
5675 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005676 }
5677
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005678 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005679 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005680 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005681 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005682 } else {
5683 return charSequence;
5684 }
5685 }
5686
Dan Sandler26e81cf2014-05-06 10:01:27 -04005687 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005688 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005689 */
Adrian Roos487374f2017-01-11 15:48:14 -08005690 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005691 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005692 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005693 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005694 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005695 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005696 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005697 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005698 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005699 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005700 contentView.setDrawableTint(R.id.icon, false, color,
5701 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005702
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005703 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005704 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005705 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005706 }
5707
Dan Sandler26e81cf2014-05-06 10:01:27 -04005708 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005709 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005710 * if it's grayscale).
5711 */
5712 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005713 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5714 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005715 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005716 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005717 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005718 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005719 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005720 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005721 }
5722
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005723 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005724 if (mN.color != COLOR_DEFAULT) {
5725 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005726 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005727 }
5728
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005729 int resolveContrastColor(StandardTemplateParams p) {
5730 int rawColor = getRawColor(p);
5731 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005732 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005733 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005734
Selim Cinekac5f0272017-05-02 16:05:41 -07005735 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005736 int background = mContext.getColor(
5737 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005738 if (rawColor == COLOR_DEFAULT) {
5739 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005740 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005741 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005742 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005743 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005744 }
5745 if (Color.alpha(color) < 255) {
5746 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005747 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005748 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005749 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005750 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005751 }
5752
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005753 /**
5754 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5755 *
5756 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5757 * @param p the template params to inflate this with
5758 */
5759 private int getRawColor(StandardTemplateParams p) {
5760 if (p.forceDefaultColor) {
5761 return COLOR_DEFAULT;
5762 }
5763 return mN.color;
5764 }
5765
Selim Cinek4717d862018-04-19 09:19:15 +08005766 int resolveNeutralColor() {
5767 if (mNeutralColor != COLOR_INVALID) {
5768 return mNeutralColor;
5769 }
5770 int background = mContext.getColor(
5771 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005772 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5773 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005774 if (Color.alpha(mNeutralColor) < 255) {
5775 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005776 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005777 }
5778 return mNeutralColor;
5779 }
5780
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005781 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005782 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005783 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005784 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005785 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005786 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005787 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005788 mN.actions = new Action[mActions.size()];
5789 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005790 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005791 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005792 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005793 }
Selim Cinek247fa012016-02-18 09:50:48 -08005794 if (mN.bigContentView != null || mN.contentView != null
5795 || mN.headsUpContentView != null) {
5796 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5797 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005798 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005799 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005800
Julia Reynolds3b848122016-02-26 10:45:32 -05005801 /**
5802 * Creates a Builder from an existing notification so further changes can be made.
5803 * @param context The context for your application / activity.
5804 * @param n The notification to create a Builder from.
5805 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005806 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005807 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005808 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005809 ApplicationInfo applicationInfo = n.extras.getParcelable(
5810 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005811 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005812 if (applicationInfo != null) {
5813 try {
5814 builderContext = context.createApplicationContext(applicationInfo,
5815 Context.CONTEXT_RESTRICTED);
5816 } catch (NameNotFoundException e) {
5817 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5818 builderContext = context; // try with our context
5819 }
5820 } else {
5821 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005822 }
5823
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005824 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005825 }
5826
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005827 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005828 * Determines whether the platform can generate contextual actions for a notification.
5829 * By default this is true.
5830 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005831 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005832 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5833 mN.mAllowSystemGeneratedContextualActions = allowed;
5834 return this;
5835 }
5836
5837 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005838 * @deprecated Use {@link #build()} instead.
5839 */
5840 @Deprecated
5841 public Notification getNotification() {
5842 return build();
5843 }
5844
5845 /**
5846 * Combine all of the options that have been set and return a new {@link Notification}
5847 * object.
5848 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005849 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005850 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005851 // first, add any extras from the calling code
5852 if (mUserExtras != null) {
5853 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005854 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005855
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005856 mN.creationTime = System.currentTimeMillis();
5857
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005858 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005859 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005860
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005861 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005862
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005863 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005864 mStyle.reduceImageSizes(mContext);
5865 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005866 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005867 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005868 }
Selim Cinekd0426622017-07-11 13:19:59 +02005869 mN.reduceImageSizes(mContext);
5870
Adrian Roos5081c0d2016-02-26 16:04:19 -08005871 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005872 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005873 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005874 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005875 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5876 mN.contentView.getSequenceNumber());
5877 }
5878 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005879 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005880 if (mN.bigContentView != null) {
5881 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5882 mN.bigContentView.getSequenceNumber());
5883 }
5884 }
5885 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005886 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005887 if (mN.headsUpContentView != null) {
5888 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5889 mN.headsUpContentView.getSequenceNumber());
5890 }
5891 }
5892 }
5893
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005894 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5895 mN.flags |= FLAG_SHOW_LIGHTS;
5896 }
5897
Adrian Roosfb921842017-10-26 14:49:56 +02005898 mN.allPendingIntents = null;
5899
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005900 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005901 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005902
5903 /**
5904 * Apply this Builder to an existing {@link Notification} object.
5905 *
5906 * @hide
5907 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005908 @NonNull
5909 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005910 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005911 return n;
5912 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005913
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005914 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005915 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005916 * change. Also removes extenders on low ram devices, as
5917 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005918 *
5919 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5920 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005921 * @hide
5922 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005923 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5924 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005925 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005926
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005927 // Only strip views for known Styles because we won't know how to
5928 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005929 if (!isLowRam
5930 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005931 && getNotificationStyleClass(templateClass) == null) {
5932 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005933 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005934
5935 // Only strip unmodified BuilderRemoteViews.
5936 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005937 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005938 n.contentView.getSequenceNumber();
5939 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005940 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005941 n.bigContentView.getSequenceNumber();
5942 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005943 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005944 n.headsUpContentView.getSequenceNumber();
5945
5946 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005947 if (!isLowRam
5948 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005949 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005950 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005951
5952 Notification clone = n.clone();
5953 if (stripContentView) {
5954 clone.contentView = null;
5955 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5956 }
5957 if (stripBigContentView) {
5958 clone.bigContentView = null;
5959 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5960 }
5961 if (stripHeadsUpContentView) {
5962 clone.headsUpContentView = null;
5963 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5964 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005965 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005966 String[] allowedServices = context.getResources().getStringArray(
5967 R.array.config_allowedManagedServicesOnLowRamDevices);
5968 if (allowedServices.length == 0) {
5969 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5970 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5971 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5972 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005973 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005974 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005975 }
5976
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005977 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005978 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005979 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005980 }
5981
5982 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005983 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005984 }
5985
5986 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005987 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005988 }
5989
5990 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005991 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005992 }
5993
5994 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005995 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005996 }
5997
Adrian Roosc1a80b02016-04-05 14:54:55 -07005998 private int getMessagingLayoutResource() {
5999 return R.layout.notification_template_material_messaging;
6000 }
6001
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006002 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006003 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006004 }
6005
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006006 private int getEmphasizedActionLayoutResource() {
6007 return R.layout.notification_material_action_emphasized;
6008 }
6009
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006010 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006011 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006012 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006013
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006014 private int getBackgroundColor(StandardTemplateParams p) {
6015 if (isColorized(p)) {
6016 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006017 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006018 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006019 }
6020 }
6021
Selim Cinek396caca2018-04-10 17:46:46 -07006022 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006023 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006024 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006025 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006026 private int getNeutralColor(StandardTemplateParams p) {
6027 if (isColorized(p)) {
6028 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006029 } else {
6030 return resolveNeutralColor();
6031 }
6032 }
6033
6034 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006035 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006036 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006037 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006038 private int resolveBackgroundColor(StandardTemplateParams p) {
6039 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006040 if (backgroundColor == COLOR_DEFAULT) {
6041 backgroundColor = mContext.getColor(
6042 com.android.internal.R.color.notification_material_background_color);
6043 }
6044 return backgroundColor;
6045 }
6046
Anthony Chenad4d1582017-04-10 16:07:58 -07006047 private boolean shouldTintActionButtons() {
6048 return mTintActionButtons;
6049 }
6050
Selim Cinek99104832017-01-25 14:47:33 -08006051 private boolean textColorsNeedInversion() {
6052 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6053 return false;
6054 }
6055 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6056 return targetSdkVersion > Build.VERSION_CODES.M
6057 && targetSdkVersion < Build.VERSION_CODES.O;
6058 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006059
6060 /**
6061 * Set a color palette to be used as the background and textColors
6062 *
6063 * @param backgroundColor the color to be used as the background
6064 * @param foregroundColor the color to be used as the foreground
6065 *
6066 * @hide
6067 */
6068 public void setColorPalette(int backgroundColor, int foregroundColor) {
6069 mBackgroundColor = backgroundColor;
6070 mForegroundColor = foregroundColor;
6071 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006072 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006073 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006074
6075 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006076 * Forces all styled remoteViews to be built from scratch and not use any cached
6077 * RemoteViews.
6078 * This is needed for legacy apps that are baking in their remoteviews into the
6079 * notification.
6080 *
6081 * @hide
6082 */
6083 public void setRebuildStyledRemoteViews(boolean rebuild) {
6084 mRebuildStyledRemoteViews = rebuild;
6085 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006086
6087 /**
6088 * Get the text that should be displayed in the statusBar when heads upped. This is
6089 * usually just the app name, but may be different depending on the style.
6090 *
6091 * @param publicMode If true, return a text that is safe to display in public.
6092 *
6093 * @hide
6094 */
6095 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6096 if (mStyle != null && !publicMode) {
6097 CharSequence text = mStyle.getHeadsUpStatusBarText();
6098 if (!TextUtils.isEmpty(text)) {
6099 return text;
6100 }
6101 }
6102 return loadHeaderAppName();
6103 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006104 }
6105
6106 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006107 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6108 * remote views.
6109 *
6110 * @hide
6111 */
6112 void reduceImageSizes(Context context) {
6113 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6114 return;
6115 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006116 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006117 if (mLargeIcon != null || largeIcon != null) {
6118 Resources resources = context.getResources();
6119 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006120 int maxWidth = resources.getDimensionPixelSize(isLowRam
6121 ? R.dimen.notification_right_icon_size_low_ram
6122 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006123 int maxHeight = maxWidth;
6124 if (MediaStyle.class.equals(style)
6125 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006126 maxHeight = resources.getDimensionPixelSize(isLowRam
6127 ? R.dimen.notification_media_image_max_height_low_ram
6128 : R.dimen.notification_media_image_max_height);
6129 maxWidth = resources.getDimensionPixelSize(isLowRam
6130 ? R.dimen.notification_media_image_max_width_low_ram
6131 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006132 }
6133 if (mLargeIcon != null) {
6134 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6135 }
6136 if (largeIcon != null) {
6137 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6138 }
6139 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006140 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6141 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6142 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006143 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6144 }
6145
Selim Cineka8cb1262017-08-15 16:53:44 -07006146 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6147 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006148 if (remoteView != null) {
6149 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006150 int maxWidth = resources.getDimensionPixelSize(isLowRam
6151 ? R.dimen.notification_custom_view_max_image_width_low_ram
6152 : R.dimen.notification_custom_view_max_image_width);
6153 int maxHeight = resources.getDimensionPixelSize(isLowRam
6154 ? R.dimen.notification_custom_view_max_image_height_low_ram
6155 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006156 remoteView.reduceImageSizes(maxWidth, maxHeight);
6157 }
6158 }
6159
6160 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006161 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006162 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006163 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006164 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006165 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006166 }
6167
6168 /**
Selim Cinek99104832017-01-25 14:47:33 -08006169 * @return whether this notification has a media session attached
6170 * @hide
6171 */
6172 public boolean hasMediaSession() {
6173 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6174 }
6175
6176 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006177 * @return the style class of this notification
6178 * @hide
6179 */
6180 public Class<? extends Notification.Style> getNotificationStyle() {
6181 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6182
6183 if (!TextUtils.isEmpty(templateClass)) {
6184 return Notification.getNotificationStyleClass(templateClass);
6185 }
6186 return null;
6187 }
6188
6189 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006190 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006191 *
6192 * @hide
6193 */
6194 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006195 if (isColorizedMedia()) {
6196 return true;
6197 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006198 return extras.getBoolean(EXTRA_COLORIZED)
6199 && (hasColorizedPermission() || isForegroundService());
6200 }
6201
6202 /**
6203 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6204 * permission. The permission is checked when a notification is enqueued.
6205 */
6206 private boolean hasColorizedPermission() {
6207 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006208 }
6209
6210 /**
6211 * @return true if this notification is colorized and it is a media notification
6212 *
6213 * @hide
6214 */
6215 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006216 Class<? extends Style> style = getNotificationStyle();
6217 if (MediaStyle.class.equals(style)) {
6218 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6219 if ((colorized == null || colorized) && hasMediaSession()) {
6220 return true;
6221 }
6222 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6223 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6224 return true;
6225 }
6226 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006227 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006228 }
6229
Selim Cinek0847acd2017-04-24 19:48:29 -07006230
6231 /**
6232 * @return true if this is a media notification
6233 *
6234 * @hide
6235 */
6236 public boolean isMediaNotification() {
6237 Class<? extends Style> style = getNotificationStyle();
6238 if (MediaStyle.class.equals(style)) {
6239 return true;
6240 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6241 return true;
6242 }
6243 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006244 }
6245
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006246 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -04006247 * @return true if this notification is showing as a bubble
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006248 *
6249 * @hide
6250 */
6251 public boolean isBubbleNotification() {
6252 return (flags & Notification.FLAG_BUBBLE) != 0;
6253 }
6254
Selim Cinek279fa862016-06-14 10:57:25 -07006255 private boolean hasLargeIcon() {
6256 return mLargeIcon != null || largeIcon != null;
6257 }
6258
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006259 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006260 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006261 * @hide
6262 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006263 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006264 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6265 }
6266
6267 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006268 * @return true if the notification will show a chronometer; false otherwise
6269 * @hide
6270 */
6271 public boolean showsChronometer() {
6272 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6273 }
6274
6275 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006276 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006277 */
6278 @SystemApi
6279 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6280 Class<? extends Style>[] classes = new Class[] {
6281 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6282 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6283 MessagingStyle.class };
6284 for (Class<? extends Style> innerClass : classes) {
6285 if (templateClass.equals(innerClass.getName())) {
6286 return innerClass;
6287 }
6288 }
6289 return null;
6290 }
6291
6292 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006293 * An object that can apply a rich notification style to a {@link Notification.Builder}
6294 * object.
6295 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006296 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006297
6298 /**
6299 * The number of items allowed simulatanously in the remote input history.
6300 * @hide
6301 */
6302 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006303 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006304
6305 /**
6306 * @hide
6307 */
6308 protected CharSequence mSummaryText = null;
6309
6310 /**
6311 * @hide
6312 */
6313 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006314
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006315 protected Builder mBuilder;
6316
Chris Wrend6297db2012-05-03 16:20:13 -04006317 /**
6318 * Overrides ContentTitle in the big form of the template.
6319 * This defaults to the value passed to setContentTitle().
6320 */
6321 protected void internalSetBigContentTitle(CharSequence title) {
6322 mBigContentTitle = title;
6323 }
6324
6325 /**
6326 * Set the first line of text after the detail section in the big form of the template.
6327 */
6328 protected void internalSetSummaryText(CharSequence cs) {
6329 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006330 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006331 }
6332
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006333 public void setBuilder(Builder builder) {
6334 if (mBuilder != builder) {
6335 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006336 if (mBuilder != null) {
6337 mBuilder.setStyle(this);
6338 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006339 }
6340 }
6341
Chris Wrend6297db2012-05-03 16:20:13 -04006342 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006343 if (mBuilder == null) {
6344 throw new IllegalArgumentException("Style requires a valid Builder object");
6345 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006346 }
Chris Wrend6297db2012-05-03 16:20:13 -04006347
6348 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006349 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6350 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006351 }
6352
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006353
Selim Cinek384804b2018-04-18 14:31:07 +08006354 /**
6355 * Get the standard view for this style.
6356 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006357 * @param layoutId The layout id to use.
6358 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006359 * @param result The result where template bind information is saved.
6360 * @return A remoteView for this style.
6361 * @hide
6362 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006363 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6364 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006365 checkBuilder();
6366
6367 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006368 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006369 }
6370
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006371 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6372 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006373
Chris Wrend6297db2012-05-03 16:20:13 -04006374 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6375 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006376 } else {
6377 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006378 }
6379
Chris Wrend6297db2012-05-03 16:20:13 -04006380 return contentView;
6381 }
6382
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006383 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006384 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006385 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006386 *
6387 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006388 * @hide
6389 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006390 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006391 return null;
6392 }
6393
6394 /**
6395 * Construct a Style-specific RemoteViews for the final big notification layout.
6396 * @hide
6397 */
6398 public RemoteViews makeBigContentView() {
6399 return null;
6400 }
6401
6402 /**
6403 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006404 *
6405 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006406 * @hide
6407 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006408 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006409 return null;
6410 }
6411
6412 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006413 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006414 * @hide
6415 */
6416 public void addExtras(Bundle extras) {
6417 if (mSummaryTextSet) {
6418 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6419 }
6420 if (mBigContentTitle != null) {
6421 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6422 }
Chris Wren91ad5632013-06-05 15:05:57 -04006423 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006424 }
6425
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006426 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006427 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006428 * @hide
6429 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006430 protected void restoreFromExtras(Bundle extras) {
6431 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6432 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6433 mSummaryTextSet = true;
6434 }
6435 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6436 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6437 }
6438 }
6439
6440
6441 /**
6442 * @hide
6443 */
6444 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006445 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006446 return wip;
6447 }
6448
Daniel Sandler0ec46202015-06-24 01:27:05 -04006449 /**
6450 * @hide
6451 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006452 public void purgeResources() {}
6453
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006454 /**
6455 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6456 * attached to.
6457 *
6458 * @return the fully constructed Notification.
6459 */
6460 public Notification build() {
6461 checkBuilder();
6462 return mBuilder.build();
6463 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006464
6465 /**
6466 * @hide
6467 * @return true if the style positions the progress bar on the second line; false if the
6468 * style hides the progress bar
6469 */
6470 protected boolean hasProgress() {
6471 return true;
6472 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006473
6474 /**
6475 * @hide
6476 * @return Whether we should put the summary be put into the notification header
6477 */
6478 public boolean hasSummaryInHeader() {
6479 return true;
6480 }
Selim Cinek593610c2016-02-16 18:42:57 -08006481
6482 /**
6483 * @hide
6484 * @return Whether custom content views are displayed inline in the style
6485 */
6486 public boolean displayCustomViewInline() {
6487 return false;
6488 }
Selim Cinekd0426622017-07-11 13:19:59 +02006489
6490 /**
6491 * Reduces the image sizes contained in this style.
6492 *
6493 * @hide
6494 */
6495 public void reduceImageSizes(Context context) {
6496 }
Selim Cinek90343862018-02-01 11:07:11 -08006497
6498 /**
6499 * Validate that this style was properly composed. This is called at build time.
6500 * @hide
6501 */
6502 public void validate(Context context) {
6503 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006504
6505 /**
6506 * @hide
6507 */
6508 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006509
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006510 /**
koprivaa1a78482018-10-09 10:09:23 -07006511 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006512 * If {@code null} is returned, the default implementation will be used.
6513 *
6514 * @hide
6515 */
6516 public CharSequence getHeadsUpStatusBarText() {
6517 return null;
6518 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006519 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006520
6521 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006522 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006523 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006524 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006525 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006526 * Notification notif = new Notification.Builder(mContext)
6527 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6528 * .setContentText(subject)
6529 * .setSmallIcon(R.drawable.new_post)
6530 * .setLargeIcon(aBitmap)
6531 * .setStyle(new Notification.BigPictureStyle()
6532 * .bigPicture(aBigBitmap))
6533 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006534 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006535 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006536 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006537 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006538 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006539 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006540 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006541 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006542
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006543 public BigPictureStyle() {
6544 }
6545
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006546 /**
6547 * @deprecated use {@code BigPictureStyle()}.
6548 */
6549 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006550 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006551 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006552 }
6553
Chris Wrend6297db2012-05-03 16:20:13 -04006554 /**
6555 * Overrides ContentTitle in the big form of the template.
6556 * This defaults to the value passed to setContentTitle().
6557 */
6558 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006559 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006560 return this;
6561 }
6562
6563 /**
6564 * Set the first line of text after the detail section in the big form of the template.
6565 */
6566 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006567 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006568 return this;
6569 }
6570
Chris Wren0bd664d2012-08-01 13:56:56 -04006571 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006572 * @hide
6573 */
6574 public Bitmap getBigPicture() {
6575 return mPicture;
6576 }
6577
6578 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006579 * Provide the bitmap to be used as the payload for the BigPicture notification.
6580 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006581 public BigPictureStyle bigPicture(Bitmap b) {
6582 mPicture = b;
6583 return this;
6584 }
6585
Chris Wren3745a3d2012-05-22 15:11:52 -04006586 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006587 * Override the large icon when the big notification is shown.
6588 */
6589 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006590 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6591 }
6592
6593 /**
6594 * Override the large icon when the big notification is shown.
6595 */
6596 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006597 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006598 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006599 return this;
6600 }
6601
Riley Andrews0394a0c2015-11-03 23:36:52 -08006602 /** @hide */
6603 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6604
Daniel Sandler0ec46202015-06-24 01:27:05 -04006605 /**
6606 * @hide
6607 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006608 @Override
6609 public void purgeResources() {
6610 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006611 if (mPicture != null &&
6612 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006613 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006614 mPicture = mPicture.createAshmemBitmap();
6615 }
6616 if (mBigLargeIcon != null) {
6617 mBigLargeIcon.convertToAshmem();
6618 }
6619 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006620
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006621 /**
6622 * @hide
6623 */
Selim Cinekd0426622017-07-11 13:19:59 +02006624 @Override
6625 public void reduceImageSizes(Context context) {
6626 super.reduceImageSizes(context);
6627 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006628 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006629 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006630 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6631 ? R.dimen.notification_big_picture_max_height_low_ram
6632 : R.dimen.notification_big_picture_max_height);
6633 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6634 ? R.dimen.notification_big_picture_max_width_low_ram
6635 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006636 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6637 }
6638 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006639 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6640 ? R.dimen.notification_right_icon_size_low_ram
6641 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006642 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6643 }
6644 }
6645
6646 /**
6647 * @hide
6648 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006649 public RemoteViews makeBigContentView() {
6650 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006651 // This covers the following cases:
6652 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006653 // mN.mLargeIcon
6654 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006655 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006656 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006657 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006658 oldLargeIcon = mBuilder.mN.mLargeIcon;
6659 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006660 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6661 // replacement if the other one is null. Because we're restoring these legacy icons
6662 // for old listeners, this is in general non-null.
6663 largeIconLegacy = mBuilder.mN.largeIcon;
6664 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006665 }
6666
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006667 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006668 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006669 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006670 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006671 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6672 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006673 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006674 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006675 }
Selim Cinek279fa862016-06-14 10:57:25 -07006676 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006677
Christoph Studer5c510ee2014-12-15 16:32:27 +01006678 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006679 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006680 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006681 }
6682
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006683 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006684 return contentView;
6685 }
6686
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006687 /**
6688 * @hide
6689 */
6690 public void addExtras(Bundle extras) {
6691 super.addExtras(extras);
6692
6693 if (mBigLargeIconSet) {
6694 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6695 }
6696 extras.putParcelable(EXTRA_PICTURE, mPicture);
6697 }
6698
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006699 /**
6700 * @hide
6701 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006702 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006703 protected void restoreFromExtras(Bundle extras) {
6704 super.restoreFromExtras(extras);
6705
6706 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006707 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006708 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006709 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006710 mPicture = extras.getParcelable(EXTRA_PICTURE);
6711 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006712
6713 /**
6714 * @hide
6715 */
6716 @Override
6717 public boolean hasSummaryInHeader() {
6718 return false;
6719 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006720
6721 /**
6722 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006723 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6724 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006725 */
6726 @Override
6727 public boolean areNotificationsVisiblyDifferent(Style other) {
6728 if (other == null || getClass() != other.getClass()) {
6729 return true;
6730 }
6731 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006732 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6733 }
6734
6735 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6736 if (a == b) {
6737 return false;
6738 }
6739 if (a == null || b == null) {
6740 return true;
6741 }
6742 return a.getWidth() != b.getWidth()
6743 || a.getHeight() != b.getHeight()
6744 || a.getConfig() != b.getConfig()
6745 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006746 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006747 }
6748
6749 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006750 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006751 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006752 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006753 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006754 * Notification notif = new Notification.Builder(mContext)
6755 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6756 * .setContentText(subject)
6757 * .setSmallIcon(R.drawable.new_mail)
6758 * .setLargeIcon(aBitmap)
6759 * .setStyle(new Notification.BigTextStyle()
6760 * .bigText(aVeryLongString))
6761 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006762 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006763 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006764 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006765 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006766 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006767
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006768 private CharSequence mBigText;
6769
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006770 public BigTextStyle() {
6771 }
6772
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006773 /**
6774 * @deprecated use {@code BigTextStyle()}.
6775 */
6776 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006777 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006778 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006779 }
6780
Chris Wrend6297db2012-05-03 16:20:13 -04006781 /**
6782 * Overrides ContentTitle in the big form of the template.
6783 * This defaults to the value passed to setContentTitle().
6784 */
6785 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006786 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006787 return this;
6788 }
6789
6790 /**
6791 * Set the first line of text after the detail section in the big form of the template.
6792 */
6793 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006794 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006795 return this;
6796 }
6797
Chris Wren0bd664d2012-08-01 13:56:56 -04006798 /**
6799 * Provide the longer text to be displayed in the big form of the
6800 * template in place of the content text.
6801 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006802 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006803 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006804 return this;
6805 }
6806
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006807 /**
6808 * @hide
6809 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006810 public CharSequence getBigText() {
6811 return mBigText;
6812 }
6813
6814 /**
6815 * @hide
6816 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006817 public void addExtras(Bundle extras) {
6818 super.addExtras(extras);
6819
Christoph Studer4600f9b2014-07-22 22:44:43 +02006820 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6821 }
6822
6823 /**
6824 * @hide
6825 */
6826 @Override
6827 protected void restoreFromExtras(Bundle extras) {
6828 super.restoreFromExtras(extras);
6829
6830 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006831 }
6832
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006833 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006834 * @param increasedHeight true if this layout be created with an increased height.
6835 *
6836 * @hide
6837 */
6838 @Override
6839 public RemoteViews makeContentView(boolean increasedHeight) {
6840 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006841 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006842 mBuilder.mActions = new ArrayList<>();
6843 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006844 mBuilder.mActions = mBuilder.mOriginalActions;
6845 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006846 return remoteViews;
6847 }
6848 return super.makeContentView(increasedHeight);
6849 }
6850
6851 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006852 * @hide
6853 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006854 @Override
6855 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6856 if (increasedHeight && mBuilder.mActions.size() > 0) {
6857 return makeBigContentView();
6858 }
6859 return super.makeHeadsUpContentView(increasedHeight);
6860 }
6861
6862 /**
6863 * @hide
6864 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006865 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006866 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006867 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006868 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6869 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006870 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006871
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006872 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006873 if (TextUtils.isEmpty(bigTextText)) {
6874 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6875 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006876 bigTextText = mBuilder.processLegacyText(
6877 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006878 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006879 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6880 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6881 contentView.setViewVisibility(R.id.big_text,
6882 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006883 contentView.setBoolean(R.id.big_text, "setHasImage",
6884 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006885
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006886 return contentView;
6887 }
6888
Julia Reynolds7217dc92018-03-07 12:12:09 -05006889 /**
6890 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006891 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006892 */
6893 @Override
6894 public boolean areNotificationsVisiblyDifferent(Style other) {
6895 if (other == null || getClass() != other.getClass()) {
6896 return true;
6897 }
6898 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006899 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006900 }
6901
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006902 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006903
6904 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006905 * Helper class for generating large-format notifications that include multiple back-and-forth
6906 * messages of varying types between any number of people.
6907 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006908 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006909 * If the platform does not provide large-format notifications, this method has no effect. The
6910 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006911 *
6912 * <p>
6913 * If the app is targeting Android P and above, it is required to use the {@link Person}
6914 * class in order to get an optimal rendering of the notification and its avatars. For
6915 * conversations involving multiple people, the app should also make sure that it marks the
6916 * conversation as a group with {@link #setGroupConversation(boolean)}.
6917 *
6918 * <p>
6919 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6920 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006921 * <pre class="prettyprint">
6922 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006923 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6924 * MessagingStyle style = new MessagingStyle(user)
6925 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6926 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6927 * .setGroupConversation(hasMultiplePeople());
6928 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006929 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006930 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006931 * .setContentText(subject)
6932 * .setSmallIcon(R.drawable.new_message)
6933 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006934 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006935 * .build();
6936 * </pre>
6937 */
6938 public static class MessagingStyle extends Style {
6939
6940 /**
6941 * The maximum number of messages that will be retained in the Notification itself (the
6942 * number displayed is up to the platform).
6943 */
6944 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6945
Selim Cinekcb8b9852017-12-15 18:01:52 -08006946 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006947 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006948 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006949 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006950 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006951
6952 MessagingStyle() {
6953 }
6954
6955 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006956 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6957 * user before the posting app reposts the notification with those messages after they've
6958 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006959 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006960 *
6961 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006962 */
Alex Hillsfd590442016-10-07 09:52:44 -04006963 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006964 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006965 }
6966
6967 /**
6968 * @param user Required - The person displayed for any messages that are sent by the
6969 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6970 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006971 * by this user. The user also needs to have a valid name associated with it, which will
6972 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006973 */
6974 public MessagingStyle(@NonNull Person user) {
6975 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006976 }
6977
6978 /**
6979 * Validate that this style was properly composed. This is called at build time.
6980 * @hide
6981 */
6982 @Override
6983 public void validate(Context context) {
6984 super.validate(context);
6985 if (context.getApplicationInfo().targetSdkVersion
6986 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6987 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006988 }
6989 }
6990
6991 /**
koprivaa1a78482018-10-09 10:09:23 -07006992 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006993 * If {@code null} is returned, the default implementation will be used.
6994 *
6995 * @hide
6996 */
6997 @Override
6998 public CharSequence getHeadsUpStatusBarText() {
6999 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
7000 ? super.mBigContentTitle
7001 : mConversationTitle;
7002 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
7003 return conversationTitle;
7004 }
7005 return null;
7006 }
7007
7008 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08007009 * @return the user to be displayed for any replies sent by the user
7010 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007011 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08007012 public Person getUser() {
7013 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007014 }
7015
7016 /**
7017 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007018 *
7019 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007020 */
7021 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007022 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007023 }
7024
7025 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007026 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7027 *
Kodlee Yin14656422017-12-22 17:00:46 -08007028 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7029 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7030 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7031 * {@code true} and passing {@code null} will make it return {@code false}. In
7032 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7033 * to set group conversation status.
7034 *
7035 * @param conversationTitle Title displayed for this conversation
7036 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007037 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007038 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007039 mConversationTitle = conversationTitle;
7040 return this;
7041 }
7042
7043 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007044 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007045 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007046 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007047 public CharSequence getConversationTitle() {
7048 return mConversationTitle;
7049 }
7050
7051 /**
7052 * Adds a message for display by this notification. Convenience call for a simple
7053 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7054 * @param text A {@link CharSequence} to be displayed as the message content
7055 * @param timestamp Time at which the message arrived
7056 * @param sender A {@link CharSequence} to be used for displaying the name of the
7057 * sender. Should be <code>null</code> for messages by the current user, in which case
7058 * the platform will insert {@link #getUserDisplayName()}.
7059 * Should be unique amongst all individuals in the conversation, and should be
7060 * consistent during re-posts of the notification.
7061 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007062 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007063 *
7064 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007065 *
7066 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007067 */
7068 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007069 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007070 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007071 }
7072
7073 /**
7074 * Adds a message for display by this notification. Convenience call for a simple
7075 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7076 * @param text A {@link CharSequence} to be displayed as the message content
7077 * @param timestamp Time at which the message arrived
7078 * @param sender The {@link Person} who sent the message.
7079 * Should be <code>null</code> for messages by the current user, in which case
7080 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7081 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007082 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007083 *
7084 * @return this object for method chaining
7085 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007086 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7087 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007088 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007089 }
7090
7091 /**
7092 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007093 *
7094 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7095 * the newest last.
7096 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007097 * @param message The {@link Message} to be displayed
7098 * @return this object for method chaining
7099 */
7100 public MessagingStyle addMessage(Message message) {
7101 mMessages.add(message);
7102 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7103 mMessages.remove(0);
7104 }
7105 return this;
7106 }
7107
7108 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007109 * Adds a {@link Message} for historic context in this notification.
7110 *
7111 * <p>Messages should be added as historic if they are not the main subject of the
7112 * notification but may give context to a conversation. The system may choose to present
7113 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7114 *
7115 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7116 * the newest last.
7117 *
7118 * @param message The historic {@link Message} to be added
7119 * @return this object for method chaining
7120 */
7121 public MessagingStyle addHistoricMessage(Message message) {
7122 mHistoricMessages.add(message);
7123 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7124 mHistoricMessages.remove(0);
7125 }
7126 return this;
7127 }
7128
7129 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007130 * Gets the list of {@code Message} objects that represent the notification
7131 */
7132 public List<Message> getMessages() {
7133 return mMessages;
7134 }
7135
7136 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007137 * Gets the list of historic {@code Message}s in the notification.
7138 */
7139 public List<Message> getHistoricMessages() {
7140 return mHistoricMessages;
7141 }
7142
7143 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007144 * Sets whether this conversation notification represents a group. If the app is targeting
7145 * Android P, this is required if the app wants to display the largeIcon set with
7146 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007147 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007148 * @param isGroupConversation {@code true} if the conversation represents a group,
7149 * {@code false} otherwise.
7150 * @return this object for method chaining
7151 */
7152 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7153 mIsGroupConversation = isGroupConversation;
7154 return this;
7155 }
7156
7157 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007158 * Returns {@code true} if this notification represents a group conversation, otherwise
7159 * {@code false}.
7160 *
7161 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7162 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7163 * not the conversation title is set; returning {@code true} if the conversation title is
7164 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7165 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7166 * named, non-group conversations.
7167 *
7168 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007169 */
7170 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007171 // When target SDK version is < P, a non-null conversation title dictates if this is
7172 // as group conversation.
7173 if (mBuilder != null
7174 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7175 < Build.VERSION_CODES.P) {
7176 return mConversationTitle != null;
7177 }
7178
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007179 return mIsGroupConversation;
7180 }
7181
7182 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007183 * @hide
7184 */
7185 @Override
7186 public void addExtras(Bundle extras) {
7187 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007188 if (mUser != null) {
7189 // For legacy usages
7190 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7191 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007192 }
7193 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007194 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007195 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007196 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7197 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007198 }
Adrian Roos437cd562017-01-18 15:47:03 -08007199 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7200 Message.getBundleArrayForMessages(mHistoricMessages));
7201 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007202
7203 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007204 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007205 }
7206
7207 private void fixTitleAndTextExtras(Bundle extras) {
7208 Message m = findLatestIncomingMessage();
7209 CharSequence text = (m == null) ? null : m.mText;
7210 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007211 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7212 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007213 CharSequence title;
7214 if (!TextUtils.isEmpty(mConversationTitle)) {
7215 if (!TextUtils.isEmpty(sender)) {
7216 BidiFormatter bidi = BidiFormatter.getInstance();
7217 title = mBuilder.mContext.getString(
7218 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007219 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007220 } else {
7221 title = mConversationTitle;
7222 }
7223 } else {
7224 title = sender;
7225 }
7226
7227 if (title != null) {
7228 extras.putCharSequence(EXTRA_TITLE, title);
7229 }
7230 if (text != null) {
7231 extras.putCharSequence(EXTRA_TEXT, text);
7232 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007233 }
7234
7235 /**
7236 * @hide
7237 */
7238 @Override
7239 protected void restoreFromExtras(Bundle extras) {
7240 super.restoreFromExtras(extras);
7241
Selim Cinekcb8b9852017-12-15 18:01:52 -08007242 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7243 if (mUser == null) {
7244 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007245 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007246 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007247 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007248 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007249 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007250 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007251 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007252 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007253 }
7254
7255 /**
7256 * @hide
7257 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007258 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007259 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007260 mBuilder.mOriginalActions = mBuilder.mActions;
7261 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007262 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007263 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007264 mBuilder.mActions = mBuilder.mOriginalActions;
7265 mBuilder.mOriginalActions = null;
7266 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007267 }
7268
Julia Reynolds7217dc92018-03-07 12:12:09 -05007269 /**
7270 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007271 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007272 */
7273 @Override
7274 public boolean areNotificationsVisiblyDifferent(Style other) {
7275 if (other == null || getClass() != other.getClass()) {
7276 return true;
7277 }
7278 MessagingStyle newS = (MessagingStyle) other;
7279 List<MessagingStyle.Message> oldMs = getMessages();
7280 List<MessagingStyle.Message> newMs = newS.getMessages();
7281
Dan Sandler7d67bd42018-05-15 14:06:38 -04007282 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007283 newMs = new ArrayList<>();
7284 }
7285
7286 int n = oldMs.size();
7287 if (n != newMs.size()) {
7288 return true;
7289 }
7290 for (int i = 0; i < n; i++) {
7291 MessagingStyle.Message oldM = oldMs.get(i);
7292 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007293 if (!Objects.equals(
7294 String.valueOf(oldM.getText()),
7295 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007296 return true;
7297 }
7298 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7299 return true;
7300 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007301 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7302 ? oldM.getSender()
7303 : oldM.getSenderPerson().getName());
7304 String newSender = String.valueOf(newM.getSenderPerson() == null
7305 ? newM.getSender()
7306 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007307 if (!Objects.equals(oldSender, newSender)) {
7308 return true;
7309 }
7310
7311 String oldKey = oldM.getSenderPerson() == null
7312 ? null : oldM.getSenderPerson().getKey();
7313 String newKey = newM.getSenderPerson() == null
7314 ? null : newM.getSenderPerson().getKey();
7315 if (!Objects.equals(oldKey, newKey)) {
7316 return true;
7317 }
7318 // Other fields (like timestamp) intentionally excluded
7319 }
7320 return false;
7321 }
7322
Adrian Roosc1a80b02016-04-05 14:54:55 -07007323 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007324 return findLatestIncomingMessage(mMessages);
7325 }
7326
7327 /**
7328 * @hide
7329 */
7330 @Nullable
7331 public static Message findLatestIncomingMessage(
7332 List<Message> messages) {
7333 for (int i = messages.size() - 1; i >= 0; i--) {
7334 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007335 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007336 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007337 return m;
7338 }
7339 }
Selim Cinek88188f22017-09-19 16:46:56 -07007340 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007341 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007342 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007343 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007344 return null;
7345 }
7346
7347 /**
7348 * @hide
7349 */
7350 @Override
7351 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007352 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007353 }
7354
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007355 /**
7356 * Create a messaging layout.
7357 *
7358 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7359 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007360 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007361 * @return the created remoteView.
7362 */
Selim Cinekafeed292017-12-12 17:32:44 -08007363 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007364 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007365 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007366 ? super.mBigContentTitle
7367 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007368 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7369 >= Build.VERSION_CODES.P;
7370 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007371 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007372 Icon avatarReplacement = null;
7373 if (!atLeastP) {
7374 isOneToOne = TextUtils.isEmpty(conversationTitle);
7375 avatarReplacement = mBuilder.mN.mLargeIcon;
7376 if (hasOnlyWhiteSpaceSenders()) {
7377 isOneToOne = true;
7378 nameReplacement = conversationTitle;
7379 conversationTitle = null;
7380 }
7381 } else {
7382 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007383 }
Selim Cinek384804b2018-04-18 14:31:07 +08007384 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007385 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7386 conversationTitle).text(null)
7387 .hideLargeIcon(hideRightIcons || isOneToOne)
7388 .hideReplyIcon(hideRightIcons)
7389 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007390 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007391 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007392 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007393 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007394 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007395 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007396 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7397 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007398 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007399 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7400 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007401 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007402 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007403 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007404 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007405 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7406 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007407 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7408 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007409 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7410 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007411 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7412 isOneToOne);
7413 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7414 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007415 return contentView;
7416 }
7417
Selim Cinekf7409db2017-10-24 16:17:14 -07007418 private boolean hasOnlyWhiteSpaceSenders() {
7419 for (int i = 0; i < mMessages.size(); i++) {
7420 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007421 Person sender = m.getSenderPerson();
7422 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007423 return false;
7424 }
7425 }
7426 return true;
7427 }
7428
7429 private boolean isWhiteSpace(CharSequence sender) {
7430 if (TextUtils.isEmpty(sender)) {
7431 return true;
7432 }
7433 if (sender.toString().matches("^\\s*$")) {
7434 return true;
7435 }
7436 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7437 // For the presentation that we had.
7438 for (int i = 0; i < sender.length(); i++) {
7439 char c = sender.charAt(i);
7440 if (c != '\u200B') {
7441 return false;
7442 }
7443 }
7444 return true;
7445 }
7446
Selim Cinek88188f22017-09-19 16:46:56 -07007447 private CharSequence createConversationTitleFromMessages() {
7448 ArraySet<CharSequence> names = new ArraySet<>();
7449 for (int i = 0; i < mMessages.size(); i++) {
7450 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007451 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007452 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007453 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007454 }
7455 }
7456 SpannableStringBuilder title = new SpannableStringBuilder();
7457 int size = names.size();
7458 for (int i = 0; i < size; i++) {
7459 CharSequence name = names.valueAt(i);
7460 if (!TextUtils.isEmpty(title)) {
7461 title.append(", ");
7462 }
7463 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7464 }
7465 return title;
7466 }
7467
Adrian Roosdedd1df2016-04-26 16:38:47 -07007468 /**
7469 * @hide
7470 */
7471 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007472 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007473 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007474 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007475 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7476 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007477 }
7478
Adrian Roosc1a80b02016-04-05 14:54:55 -07007479 private static TextAppearanceSpan makeFontColorSpan(int color) {
7480 return new TextAppearanceSpan(null, 0, 0,
7481 ColorStateList.valueOf(color), null);
7482 }
7483
Alex Hillsd9b04d92016-04-11 16:38:16 -04007484 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007485 /** @hide */
7486 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007487 static final String KEY_TIMESTAMP = "time";
7488 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007489 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007490 static final String KEY_DATA_MIME_TYPE = "type";
7491 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007492 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007493 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007494
7495 private final CharSequence mText;
7496 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007497 @Nullable
7498 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007499 /** True if this message was generated from the extra
7500 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7501 */
7502 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007503
Shane Brennan5a871862017-03-11 13:14:17 -08007504 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007505 private String mDataMimeType;
7506 private Uri mDataUri;
7507
7508 /**
7509 * Constructor
7510 * @param text A {@link CharSequence} to be displayed as the message content
7511 * @param timestamp Time at which the message arrived
7512 * @param sender A {@link CharSequence} to be used for displaying the name of the
7513 * sender. Should be <code>null</code> for messages by the current user, in which case
7514 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7515 * Should be unique amongst all individuals in the conversation, and should be
7516 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007517 *
7518 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007519 */
7520 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007521 this(text, timestamp, sender == null ? null
7522 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007523 }
7524
7525 /**
7526 * Constructor
7527 * @param text A {@link CharSequence} to be displayed as the message content
7528 * @param timestamp Time at which the message arrived
7529 * @param sender The {@link Person} who sent the message.
7530 * Should be <code>null</code> for messages by the current user, in which case
7531 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7532 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007533 * The person provided should contain an Icon, set with
7534 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7535 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7536 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7537 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007538 * </p>
7539 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007540 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007541 this(text, timestamp, sender, false /* remoteHistory */);
7542 }
7543
7544 /**
7545 * Constructor
7546 * @param text A {@link CharSequence} to be displayed as the message content
7547 * @param timestamp Time at which the message arrived
7548 * @param sender The {@link Person} who sent the message.
7549 * Should be <code>null</code> for messages by the current user, in which case
7550 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7551 * @param remoteInputHistory True if the messages was generated from the extra
7552 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7553 * <p>
7554 * The person provided should contain an Icon, set with
7555 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7556 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7557 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7558 * to differentiate between the different users.
7559 * </p>
7560 * @hide
7561 */
7562 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7563 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007564 mText = text;
7565 mTimestamp = timestamp;
7566 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007567 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007568 }
7569
7570 /**
7571 * Sets a binary blob of data and an associated MIME type for a message. In the case
7572 * where the platform doesn't support the MIME type, the original text provided in the
7573 * constructor will be used.
7574 * @param dataMimeType The MIME type of the content. See
7575 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7576 * types on Android and Android Wear.
7577 * @param dataUri The uri containing the content whose type is given by the MIME type.
7578 * <p class="note">
7579 * <ol>
7580 * <li>Notification Listeners including the System UI need permission to access the
7581 * data the Uri points to. The recommended ways to do this are:</li>
7582 * <li>Store the data in your own ContentProvider, making sure that other apps have
7583 * the correct permission to access your provider. The preferred mechanism for
7584 * providing access is to use per-URI permissions which are temporary and only
7585 * grant access to the receiving application. An easy way to create a
7586 * ContentProvider like this is to use the FileProvider helper class.</li>
7587 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7588 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7589 * also store non-media types (see MediaStore.Files for more info). Files can be
7590 * inserted into the MediaStore using scanFile() after which a content:// style
7591 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7592 * Note that once added to the system MediaStore the content is accessible to any
7593 * app on the device.</li>
7594 * </ol>
7595 * @return this object for method chaining
7596 */
7597 public Message setData(String dataMimeType, Uri dataUri) {
7598 mDataMimeType = dataMimeType;
7599 mDataUri = dataUri;
7600 return this;
7601 }
7602
Alex Hillsfc737de2016-03-23 17:33:02 -04007603 /**
7604 * Get the text to be used for this message, or the fallback text if a type and content
7605 * Uri have been set
7606 */
7607 public CharSequence getText() {
7608 return mText;
7609 }
7610
7611 /**
7612 * Get the time at which this message arrived
7613 */
7614 public long getTimestamp() {
7615 return mTimestamp;
7616 }
7617
7618 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007619 * Get the extras Bundle for this message.
7620 */
7621 public Bundle getExtras() {
7622 return mExtras;
7623 }
7624
7625 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007626 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007627 *
7628 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007629 */
7630 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007631 return mSender == null ? null : mSender.getName();
7632 }
7633
7634 /**
7635 * Get the sender associated with this message.
7636 */
7637 @Nullable
7638 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007639 return mSender;
7640 }
7641
7642 /**
7643 * Get the MIME type of the data pointed to by the Uri
7644 */
7645 public String getDataMimeType() {
7646 return mDataMimeType;
7647 }
7648
7649 /**
koprivaa1a78482018-10-09 10:09:23 -07007650 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007651 * {@see #getText()} is used.
7652 */
7653 public Uri getDataUri() {
7654 return mDataUri;
7655 }
7656
Kenny Guya0f6de82018-04-06 16:20:16 +01007657 /**
7658 * @return True if the message was generated from
7659 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7660 * @hide
7661 */
7662 public boolean isRemoteInputHistory() {
7663 return mRemoteInputHistory;
7664 }
7665
Beverlye98937a2018-11-15 10:18:57 -05007666 /**
7667 * @hide
7668 */
7669 @VisibleForTesting
7670 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007671 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007672 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007673 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007674 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007675 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007676 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007677 // Legacy listeners need this
7678 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7679 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007680 }
7681 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007682 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007683 }
7684 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007685 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007686 }
Shane Brennan5a871862017-03-11 13:14:17 -08007687 if (mExtras != null) {
7688 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7689 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007690 if (mRemoteInputHistory) {
7691 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7692 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007693 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007694 }
7695
Alex Hillsd9b04d92016-04-11 16:38:16 -04007696 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7697 Bundle[] bundles = new Bundle[messages.size()];
7698 final int N = messages.size();
7699 for (int i = 0; i < N; i++) {
7700 bundles[i] = messages.get(i).toBundle();
7701 }
7702 return bundles;
7703 }
7704
Selim Cinek88188f22017-09-19 16:46:56 -07007705 /**
7706 * @return A list of messages read from the bundles.
7707 *
7708 * @hide
7709 */
7710 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7711 if (bundles == null) {
7712 return new ArrayList<>();
7713 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007714 List<Message> messages = new ArrayList<>(bundles.length);
7715 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007716 if (bundles[i] instanceof Bundle) {
7717 Message message = getMessageFromBundle((Bundle)bundles[i]);
7718 if (message != null) {
7719 messages.add(message);
7720 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007721 }
7722 }
7723 return messages;
7724 }
7725
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007726 /**
7727 * @return The message that is stored in the bundle or null if the message couldn't be
7728 * resolved.
7729 *
7730 * @hide
7731 */
7732 @Nullable
7733 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007734 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007735 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007736 return null;
7737 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007738
7739 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7740 if (senderPerson == null) {
7741 // Legacy apps that use compat don't actually provide the sender objects
7742 // We need to fix the compat version to provide people / use
7743 // the native api instead
7744 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7745 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007746 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007747 }
7748 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007749 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007750 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007751 senderPerson,
7752 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007753 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7754 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007755 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7756 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007757 }
Shane Brennan5a871862017-03-11 13:14:17 -08007758 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7759 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7760 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007761 return message;
7762 }
7763 } catch (ClassCastException e) {
7764 return null;
7765 }
7766 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007767 }
7768 }
7769
7770 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007771 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007772 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007773 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007774 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007775 * Notification notif = new Notification.Builder(mContext)
7776 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7777 * .setContentText(subject)
7778 * .setSmallIcon(R.drawable.new_mail)
7779 * .setLargeIcon(aBitmap)
7780 * .setStyle(new Notification.InboxStyle()
7781 * .addLine(str1)
7782 * .addLine(str2)
7783 * .setContentTitle(&quot;&quot;)
7784 * .setSummaryText(&quot;+3 more&quot;))
7785 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007786 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007787 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007788 * @see Notification#bigContentView
7789 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007790 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007791
7792 /**
7793 * The number of lines of remote input history allowed until we start reducing lines.
7794 */
7795 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007796 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7797
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007798 public InboxStyle() {
7799 }
7800
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007801 /**
7802 * @deprecated use {@code InboxStyle()}.
7803 */
7804 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007805 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007806 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007807 }
7808
Chris Wrend6297db2012-05-03 16:20:13 -04007809 /**
7810 * Overrides ContentTitle in the big form of the template.
7811 * This defaults to the value passed to setContentTitle().
7812 */
7813 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007814 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007815 return this;
7816 }
7817
7818 /**
7819 * Set the first line of text after the detail section in the big form of the template.
7820 */
7821 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007822 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007823 return this;
7824 }
7825
Chris Wren0bd664d2012-08-01 13:56:56 -04007826 /**
7827 * Append a line to the digest section of the Inbox notification.
7828 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007829 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007830 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007831 return this;
7832 }
7833
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007834 /**
7835 * @hide
7836 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007837 public ArrayList<CharSequence> getLines() {
7838 return mTexts;
7839 }
7840
7841 /**
7842 * @hide
7843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007844 public void addExtras(Bundle extras) {
7845 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007846
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007847 CharSequence[] a = new CharSequence[mTexts.size()];
7848 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7849 }
7850
Christoph Studer4600f9b2014-07-22 22:44:43 +02007851 /**
7852 * @hide
7853 */
7854 @Override
7855 protected void restoreFromExtras(Bundle extras) {
7856 super.restoreFromExtras(extras);
7857
7858 mTexts.clear();
7859 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7860 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7861 }
7862 }
7863
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007864 /**
7865 * @hide
7866 */
7867 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007868 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007869 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007870 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007871
Chris Wrend6297db2012-05-03 16:20:13 -04007872 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 -04007873 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007874
Chris Wren4ed80d52012-05-17 09:30:03 -04007875 // Make sure all rows are gone in case we reuse a view.
7876 for (int rowId : rowIds) {
7877 contentView.setViewVisibility(rowId, View.GONE);
7878 }
7879
Daniel Sandler879c5e02012-04-17 16:46:51 -04007880 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007881 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7882 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007883 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007884 int onlyViewId = 0;
7885 int maxRows = rowIds.length;
7886 if (mBuilder.mActions.size() > 0) {
7887 maxRows--;
7888 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007889 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7890 EXTRA_REMOTE_INPUT_HISTORY);
7891 if (remoteInputHistory != null
7892 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7893 // Let's remove some messages to make room for the remote input history.
7894 // 1 is always able to fit, but let's remove them if they are 2 or 3
7895 int numRemoteInputs = Math.min(remoteInputHistory.length,
7896 MAX_REMOTE_INPUT_HISTORY_LINES);
7897 int totalNumRows = mTexts.size() + numRemoteInputs
7898 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7899 if (totalNumRows > maxRows) {
7900 int overflow = totalNumRows - maxRows;
7901 if (mTexts.size() > maxRows) {
7902 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7903 // few messages, even with the remote input
7904 maxRows -= overflow;
7905 } else {
7906 // otherwise we drop the first messages
7907 i = overflow;
7908 }
7909 }
7910 }
Selim Cinek07c80172016-04-21 16:40:47 -07007911 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007912 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007913 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007914 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007915 contentView.setTextViewText(rowIds[i],
7916 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007917 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007918 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007919 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007920 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007921 if (first) {
7922 onlyViewId = rowIds[i];
7923 } else {
7924 onlyViewId = 0;
7925 }
Selim Cinek247fa012016-02-18 09:50:48 -08007926 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007927 }
7928 i++;
7929 }
Selim Cinek07c80172016-04-21 16:40:47 -07007930 if (onlyViewId != 0) {
7931 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7932 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7933 R.dimen.notification_text_margin_top);
7934 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7935 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007936
Daniel Sandler879c5e02012-04-17 16:46:51 -04007937 return contentView;
7938 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007939
Julia Reynolds7217dc92018-03-07 12:12:09 -05007940 /**
7941 * @hide
7942 */
7943 @Override
7944 public boolean areNotificationsVisiblyDifferent(Style other) {
7945 if (other == null || getClass() != other.getClass()) {
7946 return true;
7947 }
7948 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007949
7950 final ArrayList<CharSequence> myLines = getLines();
7951 final ArrayList<CharSequence> newLines = newS.getLines();
7952 final int n = myLines.size();
7953 if (n != newLines.size()) {
7954 return true;
7955 }
7956
7957 for (int i = 0; i < n; i++) {
7958 if (!Objects.equals(
7959 String.valueOf(myLines.get(i)),
7960 String.valueOf(newLines.get(i)))) {
7961 return true;
7962 }
7963 }
7964 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007965 }
7966
Selim Cinek384804b2018-04-18 14:31:07 +08007967 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007968 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007969 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007970 if (first) {
7971 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7972 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7973 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007974 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007975 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007976 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007977 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007978 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007979 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007980 }
Dan Sandler842dd772014-05-15 09:36:47 -04007981
7982 /**
7983 * Notification style for media playback notifications.
7984 *
7985 * In the expanded form, {@link Notification#bigContentView}, up to 5
7986 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007987 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007988 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7989 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7990 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007991 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007992 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7993 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007994 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007995 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007996 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007997 * Notifications created with MediaStyle will have their category set to
7998 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7999 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08008000 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07008001 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
8002 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04008003 * the System UI can identify this as a notification representing an active media session
8004 * and respond accordingly (by showing album artwork in the lockscreen, for example).
8005 *
Selim Cinek99104832017-01-25 14:47:33 -08008006 * <p>
8007 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
8008 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
8009 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
8010 * <p>
8011 *
Dan Sandler842dd772014-05-15 09:36:47 -04008012 * To use this style with your Notification, feed it to
8013 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8014 * <pre class="prettyprint">
8015 * Notification noti = new Notification.Builder()
8016 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008017 * .setContentTitle(&quot;Track title&quot;)
8018 * .setContentText(&quot;Artist - Album&quot;)
8019 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008020 * .setStyle(<b>new Notification.MediaStyle()</b>
8021 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008022 * .build();
8023 * </pre>
8024 *
8025 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008026 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008027 */
8028 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008029 // Changing max media buttons requires also changing templates
8030 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008031 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008032 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008033 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8034 R.id.action0,
8035 R.id.action1,
8036 R.id.action2,
8037 R.id.action3,
8038 R.id.action4,
8039 };
Dan Sandler842dd772014-05-15 09:36:47 -04008040
8041 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008042 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008043
8044 public MediaStyle() {
8045 }
8046
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008047 /**
8048 * @deprecated use {@code MediaStyle()}.
8049 */
8050 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008051 public MediaStyle(Builder builder) {
8052 setBuilder(builder);
8053 }
8054
8055 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008056 * 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 -04008057 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008058 *
8059 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008060 */
8061 public MediaStyle setShowActionsInCompactView(int...actions) {
8062 mActionsToShowInCompact = actions;
8063 return this;
8064 }
8065
8066 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008067 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8068 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008069 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008070 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008071 mToken = token;
8072 return this;
8073 }
8074
Christoph Studer4600f9b2014-07-22 22:44:43 +02008075 /**
8076 * @hide
8077 */
Dan Sandler842dd772014-05-15 09:36:47 -04008078 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008079 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008080 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008081 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008082 if (wip.category == null) {
8083 wip.category = Notification.CATEGORY_TRANSPORT;
8084 }
Dan Sandler842dd772014-05-15 09:36:47 -04008085 return wip;
8086 }
8087
Christoph Studer4600f9b2014-07-22 22:44:43 +02008088 /**
8089 * @hide
8090 */
8091 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008092 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008093 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008094 }
8095
8096 /**
8097 * @hide
8098 */
8099 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008100 public RemoteViews makeBigContentView() {
8101 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008102 }
8103
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008104 /**
8105 * @hide
8106 */
8107 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008108 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek16f334e2019-07-18 16:00:55 -07008109 return makeMediaContentView();
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008110 }
8111
Dan Sandler842dd772014-05-15 09:36:47 -04008112 /** @hide */
8113 @Override
8114 public void addExtras(Bundle extras) {
8115 super.addExtras(extras);
8116
8117 if (mToken != null) {
8118 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8119 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008120 if (mActionsToShowInCompact != null) {
8121 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8122 }
Dan Sandler842dd772014-05-15 09:36:47 -04008123 }
8124
Christoph Studer4600f9b2014-07-22 22:44:43 +02008125 /**
8126 * @hide
8127 */
8128 @Override
8129 protected void restoreFromExtras(Bundle extras) {
8130 super.restoreFromExtras(extras);
8131
8132 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8133 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8134 }
8135 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8136 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8137 }
8138 }
8139
Julia Reynolds7217dc92018-03-07 12:12:09 -05008140 /**
8141 * @hide
8142 */
8143 @Override
8144 public boolean areNotificationsVisiblyDifferent(Style other) {
8145 if (other == null || getClass() != other.getClass()) {
8146 return true;
8147 }
8148 // All fields to compare are on the Notification object
8149 return false;
8150 }
8151
Gus Prevas9cc96602018-10-11 11:24:23 -04008152 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008153 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008154 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008155 container.setViewVisibility(buttonId, View.VISIBLE);
8156 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008157
8158 // If the action buttons should not be tinted, then just use the default
8159 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008160 Resources resources = mBuilder.mContext.getResources();
8161 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008162 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8163 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008164 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8165 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008166 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008167 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008168
Gus Prevas9cc96602018-10-11 11:24:23 -04008169 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008170 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008171
8172 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8173 new int[]{ android.R.attr.colorControlHighlight });
8174 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8175 typedArray.recycle();
8176 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8177 Color.blue(tintColor));
8178 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8179
Dan Sandler842dd772014-05-15 09:36:47 -04008180 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008181 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008182 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008183 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008184 }
8185
8186 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008187 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8188 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008189 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008190 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008191 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008192
8193 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008194 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008195 ? 0
8196 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008197 if (numActionsToShow > numActions) {
8198 throw new IllegalArgumentException(String.format(
8199 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8200 numActions, numActions - 1));
8201 }
8202 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8203 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008204 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008205 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008206 } else {
8207 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008208 }
8209 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008210 handleImage(view);
8211 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008212 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008213 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008214 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008215 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008216 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008217 return view;
8218 }
8219
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008220 private int getActionColor(StandardTemplateParams p) {
8221 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8222 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008223 }
8224
Dan Sandler842dd772014-05-15 09:36:47 -04008225 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008226 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008227 // Dont add an expanded view if there is no more content to be revealed
8228 int actionsInCompact = mActionsToShowInCompact == null
8229 ? 0
8230 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008231 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008232 return null;
8233 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008234 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8235 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008236 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008237 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008238
Gus Prevas9cc96602018-10-11 11:24:23 -04008239 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8240 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008241 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008242 } else {
8243 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008244 }
8245 }
linanson2bcd4032019-01-14 15:22:04 +08008246 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8247 mBuilder.mContext.getString(
8248 com.android.internal.R.string.ext_media_seamless_action), null), p);
8249 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008250 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008251 return big;
8252 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008253
Selim Cinek5bf069a2015-11-10 19:14:27 -05008254 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008255 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008256 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8257 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008258 }
8259 }
8260
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008261 /**
8262 * @hide
8263 */
8264 @Override
8265 protected boolean hasProgress() {
8266 return false;
8267 }
Dan Sandler842dd772014-05-15 09:36:47 -04008268 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008269
Selim Cinek593610c2016-02-16 18:42:57 -08008270 /**
8271 * Notification style for custom views that are decorated by the system
8272 *
8273 * <p>Instead of providing a notification that is completely custom, a developer can set this
8274 * style and still obtain system decorations like the notification header with the expand
8275 * affordance and actions.
8276 *
8277 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8278 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8279 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8280 * corresponding custom views to display.
8281 *
8282 * To use this style with your Notification, feed it to
8283 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8284 * <pre class="prettyprint">
8285 * Notification noti = new Notification.Builder()
8286 * .setSmallIcon(R.drawable.ic_stat_player)
8287 * .setLargeIcon(albumArtBitmap))
8288 * .setCustomContentView(contentView);
8289 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8290 * .build();
8291 * </pre>
8292 */
8293 public static class DecoratedCustomViewStyle extends Style {
8294
8295 public DecoratedCustomViewStyle() {
8296 }
8297
Selim Cinek593610c2016-02-16 18:42:57 -08008298 /**
8299 * @hide
8300 */
8301 public boolean displayCustomViewInline() {
8302 return true;
8303 }
8304
8305 /**
8306 * @hide
8307 */
8308 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008309 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008310 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8311 }
8312
8313 /**
8314 * @hide
8315 */
8316 @Override
8317 public RemoteViews makeBigContentView() {
8318 return makeDecoratedBigContentView();
8319 }
8320
8321 /**
8322 * @hide
8323 */
8324 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008325 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008326 return makeDecoratedHeadsUpContentView();
8327 }
8328
Selim Cinek593610c2016-02-16 18:42:57 -08008329 private RemoteViews makeDecoratedHeadsUpContentView() {
8330 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8331 ? mBuilder.mN.contentView
8332 : mBuilder.mN.headsUpContentView;
8333 if (mBuilder.mActions.size() == 0) {
8334 return makeStandardTemplateWithCustomContent(headsUpContentView);
8335 }
Selim Cinek384804b2018-04-18 14:31:07 +08008336 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008337 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008338 mBuilder.getBigBaseLayoutResource(), result);
8339 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008340 return remoteViews;
8341 }
8342
Selim Cinek593610c2016-02-16 18:42:57 -08008343 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008344 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008345 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008346 mBuilder.getBaseLayoutResource(), result);
8347 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008348 return remoteViews;
8349 }
8350
Selim Cinek593610c2016-02-16 18:42:57 -08008351 private RemoteViews makeDecoratedBigContentView() {
8352 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8353 ? mBuilder.mN.contentView
8354 : mBuilder.mN.bigContentView;
8355 if (mBuilder.mActions.size() == 0) {
8356 return makeStandardTemplateWithCustomContent(bigContentView);
8357 }
Selim Cinek384804b2018-04-18 14:31:07 +08008358 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008359 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008360 mBuilder.getBigBaseLayoutResource(), result);
8361 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008362 return remoteViews;
8363 }
Selim Cinek247fa012016-02-18 09:50:48 -08008364
8365 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008366 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008367 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008368 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008369 // Need to clone customContent before adding, because otherwise it can no longer be
8370 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008371 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008372 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8373 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008374 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008375 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008376 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008377 remoteViews.setIntTag(R.id.notification_main_column,
8378 com.android.internal.R.id.notification_custom_view_index_tag,
8379 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008380 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008381 Resources resources = mBuilder.mContext.getResources();
8382 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008383 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008384 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008385 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008386
8387 /**
8388 * @hide
8389 */
8390 @Override
8391 public boolean areNotificationsVisiblyDifferent(Style other) {
8392 if (other == null || getClass() != other.getClass()) {
8393 return true;
8394 }
8395 // Comparison done for all custom RemoteViews, independent of style
8396 return false;
8397 }
Selim Cinek593610c2016-02-16 18:42:57 -08008398 }
8399
Selim Cinek03eb3b72016-02-18 10:39:45 -08008400 /**
8401 * Notification style for media custom views that are decorated by the system
8402 *
8403 * <p>Instead of providing a media notification that is completely custom, a developer can set
8404 * this style and still obtain system decorations like the notification header with the expand
8405 * affordance and actions.
8406 *
8407 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8408 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8409 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8410 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008411 * <p>
8412 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8413 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8414 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008415 * To use this style with your Notification, feed it to
8416 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8417 * <pre class="prettyprint">
8418 * Notification noti = new Notification.Builder()
8419 * .setSmallIcon(R.drawable.ic_stat_player)
8420 * .setLargeIcon(albumArtBitmap))
8421 * .setCustomContentView(contentView);
8422 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8423 * .setMediaSession(mySession))
8424 * .build();
8425 * </pre>
8426 *
8427 * @see android.app.Notification.DecoratedCustomViewStyle
8428 * @see android.app.Notification.MediaStyle
8429 */
8430 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8431
8432 public DecoratedMediaCustomViewStyle() {
8433 }
8434
Selim Cinek03eb3b72016-02-18 10:39:45 -08008435 /**
8436 * @hide
8437 */
8438 public boolean displayCustomViewInline() {
8439 return true;
8440 }
8441
8442 /**
8443 * @hide
8444 */
8445 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008446 public RemoteViews makeContentView(boolean increasedHeight) {
8447 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008448 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8449 mBuilder.mN.contentView);
8450 }
8451
8452 /**
8453 * @hide
8454 */
8455 @Override
8456 public RemoteViews makeBigContentView() {
8457 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8458 ? mBuilder.mN.bigContentView
8459 : mBuilder.mN.contentView;
8460 return makeBigContentViewWithCustomContent(customRemoteView);
8461 }
8462
8463 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8464 RemoteViews remoteViews = super.makeBigContentView();
8465 if (remoteViews != null) {
8466 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8467 customRemoteView);
8468 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008469 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008470 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8471 customRemoteView);
8472 } else {
8473 return null;
8474 }
8475 }
8476
8477 /**
8478 * @hide
8479 */
8480 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008481 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008482 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8483 ? mBuilder.mN.headsUpContentView
8484 : mBuilder.mN.contentView;
8485 return makeBigContentViewWithCustomContent(customRemoteView);
8486 }
8487
Julia Reynolds7217dc92018-03-07 12:12:09 -05008488 /**
8489 * @hide
8490 */
8491 @Override
8492 public boolean areNotificationsVisiblyDifferent(Style other) {
8493 if (other == null || getClass() != other.getClass()) {
8494 return true;
8495 }
8496 // Comparison done for all custom RemoteViews, independent of style
8497 return false;
8498 }
8499
Selim Cinek03eb3b72016-02-18 10:39:45 -08008500 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8501 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008502 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008503 // Need to clone customContent before adding, because otherwise it can no longer be
8504 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008505 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008506 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008507 remoteViews.removeAllViews(id);
8508 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008509 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008510 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008511 return remoteViews;
8512 }
8513 }
8514
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008515 /**
8516 * Encapsulates the information needed to display a notification as a bubble.
8517 *
8518 * <p>A bubble is used to display app content in a floating window over the existing
8519 * foreground activity. A bubble has a collapsed state represented by an icon,
8520 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8521 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8522 *
8523 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8524 * outside of the notification shade on unlocked devices. When a user interacts with the
8525 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8526 *
8527 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8528 */
8529 public static final class BubbleMetadata implements Parcelable {
8530
8531 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008532 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008533 private Icon mIcon;
8534 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008535 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008536 private int mFlags;
8537
8538 /**
8539 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8540 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8541 *
Mady Mellor1f4703a2019-07-12 13:23:51 -07008542 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
8543 * The app is considered foreground if it is visible and on the screen, note that
8544 * a foreground service does not qualify.
8545 * </p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008546 *
8547 * <p>Generally this flag should only be set if the user has performed an action to request
8548 * or create a bubble.</p>
Mady Mellora7731962019-06-17 17:57:02 -07008549 *
8550 * @hide
Mady Mellor0157ff22019-01-24 15:31:48 -08008551 */
Mady Mellora7731962019-06-17 17:57:02 -07008552 public static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
Mady Mellor0157ff22019-01-24 15:31:48 -08008553
8554 /**
Mady Mellor8a1f0252019-04-01 11:31:34 -07008555 * If set and the app posting the bubble is in the foreground, the bubble will
8556 * be posted <b>without</b> the associated notification in the notification shade.
Mady Mellor0157ff22019-01-24 15:31:48 -08008557 *
Mady Mellor1f4703a2019-07-12 13:23:51 -07008558 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
8559 * The app is considered foreground if it is visible and on the screen, note that
8560 * a foreground service does not qualify.
8561 * </p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008562 *
8563 * <p>Generally this flag should only be set if the user has performed an action to request
Mady Mellor8a1f0252019-04-01 11:31:34 -07008564 * or create a bubble, or if the user has seen the content in the notification and the
8565 * notification is no longer relevant.</p>
Mady Mellora7731962019-06-17 17:57:02 -07008566 *
8567 * @hide
Mady Mellor0157ff22019-01-24 15:31:48 -08008568 */
Mady Mellora7731962019-06-17 17:57:02 -07008569 public static final int FLAG_SUPPRESS_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 /**
Mady Mellorc529d6d2019-04-16 14:22:52 -07008645 * @return whether this bubble should suppress the notification when it is posted.
8646 *
8647 * @see BubbleMetadata.Builder#setSuppressNotification(boolean)
8648 */
8649 public boolean isNotificationSuppressed() {
Mady Mellor8a1f0252019-04-01 11:31:34 -07008650 return (mFlags & FLAG_SUPPRESS_NOTIFICATION) != 0;
Mady Mellor0157ff22019-01-24 15:31:48 -08008651 }
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
Mady Mellora7731962019-06-17 17:57:02 -07008685 /**
8686 * @hide
8687 */
8688 public void setFlags(int flags) {
Mady Mellor0157ff22019-01-24 15:31:48 -08008689 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008690 }
8691
8692 /**
Mady Mellora7731962019-06-17 17:57:02 -07008693 * @hide
8694 */
8695 public int getFlags() {
8696 return mFlags;
8697 }
8698
8699 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008700 * Builder to construct a {@link BubbleMetadata} object.
8701 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008702 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008703
8704 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008705 private Icon mIcon;
8706 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008707 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008708 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008709 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008710
8711 /**
8712 * Constructs a new builder object.
8713 */
8714 public Builder() {
8715 }
8716
8717 /**
8718 * Sets the intent that will be used when the bubble is expanded. This will display the
8719 * app content in a floating window over the existing foreground activity.
Julia Tuttled9f48352019-04-15 14:06:54 -04008720 *
8721 * <p>An intent is required.</p>
8722 *
8723 * @throws IllegalArgumentException if intent is null
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008724 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008725 @NonNull
8726 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008727 if (intent == null) {
8728 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8729 }
8730 mPendingIntent = intent;
8731 return this;
8732 }
8733
8734 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008735 * Sets the icon that will represent the bubble when it is collapsed.
8736 *
8737 * <p>An icon is required and should be representative of the content within the bubble.
8738 * If your app produces multiple bubbles, the image should be unique for each of them.
8739 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008740 *
8741 * <p>The shape of a bubble icon is adaptive and can match the device theme.
8742 *
8743 * If your icon is bitmap-based, you should create it using
8744 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise this method will throw.
8745 *
8746 * If your icon is not bitmap-based, you should expect that the icon will be tinted.
8747 * </p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008748 *
8749 * @throws IllegalArgumentException if icon is null or a non-adaptive bitmap
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008750 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008751 @NonNull
8752 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008753 if (icon == null) {
8754 throw new IllegalArgumentException("Bubbles require non-null icon");
8755 }
Mady Mellor9848a6c2019-03-19 15:29:05 -07008756 if (icon.getType() == TYPE_BITMAP) {
8757 throw new IllegalArgumentException("When using bitmap based icons, Bubbles "
8758 + "require TYPE_ADAPTIVE_BITMAP, please use"
8759 + " Icon#createWithAdaptiveBitmap instead");
8760 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008761 mIcon = icon;
8762 return this;
8763 }
8764
8765 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008766 * Sets the desired height in DPs for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008767 * {@link #setIntent(PendingIntent)}.
8768 *
8769 * <p>This height may not be respected if there is not enough space on the screen or if
8770 * the provided height is too small to be useful.</p>
8771 *
8772 * <p>If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008773 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008774 * be used instead.</p>
8775 *
8776 * <p>A desired height (in DPs or via resID) is optional.</p>
8777 *
8778 * @see #setDesiredHeightResId(int)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008779 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008780 @NonNull
Mady Mellor8a529e22019-03-25 17:37:55 -07008781 public BubbleMetadata.Builder setDesiredHeight(@Dimension(unit = DP) int height) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008782 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008783 mDesiredHeightResId = 0;
8784 return this;
8785 }
8786
8787
8788 /**
8789 * Sets the desired height via resId for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008790 * {@link #setIntent(PendingIntent)}.
8791 *
8792 * <p>This height may not be respected if there is not enough space on the screen or if
8793 * the provided height is too small to be useful.</p>
8794 *
8795 * <p>If {@link #setDesiredHeight(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008796 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008797 * be used instead.</p>
8798 *
8799 * <p>A desired height (in DPs or via resID) is optional.</p>
8800 *
8801 * @see #setDesiredHeight(int)
Mady Mellor7af771a2019-03-07 15:04:54 -08008802 */
8803 @NonNull
8804 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8805 mDesiredHeightResId = heightResId;
8806 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008807 return this;
8808 }
8809
8810 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008811 * Sets whether the bubble will be posted in its expanded state (with the contents of
8812 * {@link #getIntent()} in a floating window).
Mady Mellor0157ff22019-01-24 15:31:48 -08008813 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008814 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor1f4703a2019-07-12 13:23:51 -07008815 * The app is considered foreground if it is visible and on the screen, note that
8816 * a foreground service does not qualify.
Mady Mellor0157ff22019-01-24 15:31:48 -08008817 * </p>
8818 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008819 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor0157ff22019-01-24 15:31:48 -08008820 * request or create a bubble.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008821 *
8822 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008823 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008824 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008825 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8826 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8827 return this;
8828 }
8829
8830 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008831 * Sets whether the bubble will be posted <b>without</b> the associated notification in
8832 * the notification shade.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008833 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008834 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor1f4703a2019-07-12 13:23:51 -07008835 * The app is considered foreground if it is visible and on the screen, note that
8836 * a foreground service does not qualify.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008837 * </p>
8838 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008839 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor8a1f0252019-04-01 11:31:34 -07008840 * request or create a bubble, or if the user has seen the content in the notification
8841 * and the notification is no longer relevant.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008842 *
8843 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor8a1f0252019-04-01 11:31:34 -07008844 */
8845 @NonNull
Julia Tuttle4f1bf202019-05-06 13:40:05 -04008846 public BubbleMetadata.Builder setSuppressNotification(boolean shouldSuppressNotif) {
8847 setFlag(FLAG_SUPPRESS_NOTIFICATION, shouldSuppressNotif);
Mady Mellor0157ff22019-01-24 15:31:48 -08008848 return this;
8849 }
8850
8851 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008852 * Sets an intent to send when this bubble is explicitly removed by the user.
8853 *
8854 * <p>Setting a delete intent is optional.</p>
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008855 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008856 @NonNull
8857 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008858 mDeleteIntent = deleteIntent;
8859 return this;
8860 }
8861
8862 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008863 * Creates the {@link BubbleMetadata} defined by this builder.
Julia Tuttled9f48352019-04-15 14:06:54 -04008864 *
8865 * @throws IllegalStateException if {@link #setIntent(PendingIntent)} and/or
8866 * {@link #setIcon(Icon)} have not been called on this
8867 * builder.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008868 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008869 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008870 public BubbleMetadata build() {
8871 if (mPendingIntent == null) {
8872 throw new IllegalStateException("Must supply pending intent to bubble");
8873 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008874 if (mIcon == null) {
8875 throw new IllegalStateException("Must supply an icon for the bubble");
8876 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008877 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008878 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008879 data.setFlags(mFlags);
8880 return data;
8881 }
8882
8883 /**
8884 * @hide
8885 */
8886 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8887 if (value) {
8888 mFlags |= mask;
8889 } else {
8890 mFlags &= ~mask;
8891 }
8892 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008893 }
8894 }
8895 }
8896
8897
Christoph Studer4600f9b2014-07-22 22:44:43 +02008898 // When adding a new Style subclass here, don't forget to update
8899 // Builder.getNotificationStyleClass.
8900
Griff Hazen61a9e862014-05-22 16:05:19 -07008901 /**
8902 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8903 * metadata or change options on a notification builder.
8904 */
8905 public interface Extender {
8906 /**
8907 * Apply this extender to a notification builder.
8908 * @param builder the builder to be modified.
8909 * @return the build object for chaining.
8910 */
8911 public Builder extend(Builder builder);
8912 }
8913
8914 /**
8915 * Helper class to add wearable extensions to notifications.
8916 * <p class="note"> See
8917 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8918 * for Android Wear</a> for more information on how to use this class.
8919 * <p>
8920 * To create a notification with wearable extensions:
8921 * <ol>
8922 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8923 * properties.
8924 * <li>Create a {@link android.app.Notification.WearableExtender}.
8925 * <li>Set wearable-specific properties using the
8926 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8927 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8928 * notification.
8929 * <li>Post the notification to the notification system with the
8930 * {@code NotificationManager.notify(...)} methods.
8931 * </ol>
8932 *
8933 * <pre class="prettyprint">
8934 * Notification notif = new Notification.Builder(mContext)
8935 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8936 * .setContentText(subject)
8937 * .setSmallIcon(R.drawable.new_mail)
8938 * .extend(new Notification.WearableExtender()
8939 * .setContentIcon(R.drawable.new_mail))
8940 * .build();
8941 * NotificationManager notificationManger =
8942 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8943 * notificationManger.notify(0, notif);</pre>
8944 *
8945 * <p>Wearable extensions can be accessed on an existing notification by using the
8946 * {@code WearableExtender(Notification)} constructor,
8947 * and then using the {@code get} methods to access values.
8948 *
8949 * <pre class="prettyprint">
8950 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8951 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008952 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008953 */
8954 public static final class WearableExtender implements Extender {
8955 /**
8956 * Sentinel value for an action index that is unset.
8957 */
8958 public static final int UNSET_ACTION_INDEX = -1;
8959
8960 /**
8961 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8962 * default sizing.
8963 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008964 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008965 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008966 *
8967 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008968 */
Gus Prevasd7363752018-09-18 14:35:15 -04008969 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008970 public static final int SIZE_DEFAULT = 0;
8971
8972 /**
8973 * Size value for use with {@link #setCustomSizePreset} to show this notification
8974 * with an extra small size.
8975 * <p>This value is only applicable for custom display notifications created using
8976 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008977 *
8978 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008979 */
Gus Prevasd7363752018-09-18 14:35:15 -04008980 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008981 public static final int SIZE_XSMALL = 1;
8982
8983 /**
8984 * Size value for use with {@link #setCustomSizePreset} to show this notification
8985 * with a small size.
8986 * <p>This value is only applicable for custom display notifications created using
8987 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008988 *
8989 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008990 */
Gus Prevasd7363752018-09-18 14:35:15 -04008991 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008992 public static final int SIZE_SMALL = 2;
8993
8994 /**
8995 * Size value for use with {@link #setCustomSizePreset} to show this notification
8996 * with a medium size.
8997 * <p>This value is only applicable for custom display notifications created using
8998 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008999 *
9000 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009001 */
Gus Prevasd7363752018-09-18 14:35:15 -04009002 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009003 public static final int SIZE_MEDIUM = 3;
9004
9005 /**
9006 * Size value for use with {@link #setCustomSizePreset} to show this notification
9007 * with a large size.
9008 * <p>This value is only applicable for custom display notifications created using
9009 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009010 *
9011 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009012 */
Gus Prevasd7363752018-09-18 14:35:15 -04009013 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009014 public static final int SIZE_LARGE = 4;
9015
Griff Hazend5f11f92014-05-27 15:40:09 -07009016 /**
9017 * Size value for use with {@link #setCustomSizePreset} to show this notification
9018 * full screen.
9019 * <p>This value is only applicable for custom display notifications created using
9020 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009021 *
9022 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07009023 */
Gus Prevasd7363752018-09-18 14:35:15 -04009024 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07009025 public static final int SIZE_FULL_SCREEN = 5;
9026
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009027 /**
9028 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
9029 * short amount of time when this notification is displayed on the screen. This
9030 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04009031 *
9032 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009033 */
Gus Prevasd7363752018-09-18 14:35:15 -04009034 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009035 public static final int SCREEN_TIMEOUT_SHORT = 0;
9036
9037 /**
9038 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
9039 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04009040 *
9041 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009042 */
Gus Prevasd7363752018-09-18 14:35:15 -04009043 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009044 public static final int SCREEN_TIMEOUT_LONG = -1;
9045
Griff Hazen61a9e862014-05-22 16:05:19 -07009046 /** Notification extra which contains wearable extensions */
9047 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
9048
Pete Gastaf6781d2014-10-07 15:17:05 -04009049 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07009050 private static final String KEY_ACTIONS = "actions";
9051 private static final String KEY_FLAGS = "flags";
9052 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9053 private static final String KEY_PAGES = "pages";
9054 private static final String KEY_BACKGROUND = "background";
9055 private static final String KEY_CONTENT_ICON = "contentIcon";
9056 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9057 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9058 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9059 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9060 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009061 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009062 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009063 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009064
9065 // Flags bitwise-ored to mFlags
9066 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9067 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9068 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9069 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009070 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009071 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009072 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009073
9074 // Default value for flags integer
9075 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9076
9077 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9078 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9079
9080 private ArrayList<Action> mActions = new ArrayList<Action>();
9081 private int mFlags = DEFAULT_FLAGS;
9082 private PendingIntent mDisplayIntent;
9083 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9084 private Bitmap mBackground;
9085 private int mContentIcon;
9086 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9087 private int mContentActionIndex = UNSET_ACTION_INDEX;
9088 private int mCustomSizePreset = SIZE_DEFAULT;
9089 private int mCustomContentHeight;
9090 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009091 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009092 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009093 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009094
9095 /**
9096 * Create a {@link android.app.Notification.WearableExtender} with default
9097 * options.
9098 */
9099 public WearableExtender() {
9100 }
9101
9102 public WearableExtender(Notification notif) {
9103 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9104 if (wearableBundle != null) {
9105 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9106 if (actions != null) {
9107 mActions.addAll(actions);
9108 }
9109
9110 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9111 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9112
9113 Notification[] pages = getNotificationArrayFromBundle(
9114 wearableBundle, KEY_PAGES);
9115 if (pages != null) {
9116 Collections.addAll(mPages, pages);
9117 }
9118
9119 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9120 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9121 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9122 DEFAULT_CONTENT_ICON_GRAVITY);
9123 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9124 UNSET_ACTION_INDEX);
9125 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9126 SIZE_DEFAULT);
9127 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9128 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009129 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009130 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009131 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009132 }
9133 }
9134
9135 /**
9136 * Apply wearable extensions to a notification that is being built. This is typically
9137 * called by the {@link android.app.Notification.Builder#extend} method of
9138 * {@link android.app.Notification.Builder}.
9139 */
9140 @Override
9141 public Notification.Builder extend(Notification.Builder builder) {
9142 Bundle wearableBundle = new Bundle();
9143
9144 if (!mActions.isEmpty()) {
9145 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9146 }
9147 if (mFlags != DEFAULT_FLAGS) {
9148 wearableBundle.putInt(KEY_FLAGS, mFlags);
9149 }
9150 if (mDisplayIntent != null) {
9151 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9152 }
9153 if (!mPages.isEmpty()) {
9154 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9155 new Notification[mPages.size()]));
9156 }
9157 if (mBackground != null) {
9158 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9159 }
9160 if (mContentIcon != 0) {
9161 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9162 }
9163 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9164 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9165 }
9166 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9167 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9168 mContentActionIndex);
9169 }
9170 if (mCustomSizePreset != SIZE_DEFAULT) {
9171 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9172 }
9173 if (mCustomContentHeight != 0) {
9174 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9175 }
9176 if (mGravity != DEFAULT_GRAVITY) {
9177 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9178 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009179 if (mHintScreenTimeout != 0) {
9180 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9181 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009182 if (mDismissalId != null) {
9183 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9184 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009185 if (mBridgeTag != null) {
9186 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9187 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009188
9189 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9190 return builder;
9191 }
9192
9193 @Override
9194 public WearableExtender clone() {
9195 WearableExtender that = new WearableExtender();
9196 that.mActions = new ArrayList<Action>(this.mActions);
9197 that.mFlags = this.mFlags;
9198 that.mDisplayIntent = this.mDisplayIntent;
9199 that.mPages = new ArrayList<Notification>(this.mPages);
9200 that.mBackground = this.mBackground;
9201 that.mContentIcon = this.mContentIcon;
9202 that.mContentIconGravity = this.mContentIconGravity;
9203 that.mContentActionIndex = this.mContentActionIndex;
9204 that.mCustomSizePreset = this.mCustomSizePreset;
9205 that.mCustomContentHeight = this.mCustomContentHeight;
9206 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009207 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009208 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009209 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009210 return that;
9211 }
9212
9213 /**
9214 * Add a wearable action to this notification.
9215 *
9216 * <p>When wearable actions are added using this method, the set of actions that
9217 * show on a wearable device splits from devices that only show actions added
9218 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9219 * of which actions display on different devices.
9220 *
9221 * @param action the action to add to this notification
9222 * @return this object for method chaining
9223 * @see android.app.Notification.Action
9224 */
9225 public WearableExtender addAction(Action action) {
9226 mActions.add(action);
9227 return this;
9228 }
9229
9230 /**
9231 * Adds wearable actions to this notification.
9232 *
9233 * <p>When wearable actions are added using this method, the set of actions that
9234 * show on a wearable device splits from devices that only show actions added
9235 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9236 * of which actions display on different devices.
9237 *
9238 * @param actions the actions to add to this notification
9239 * @return this object for method chaining
9240 * @see android.app.Notification.Action
9241 */
9242 public WearableExtender addActions(List<Action> actions) {
9243 mActions.addAll(actions);
9244 return this;
9245 }
9246
9247 /**
9248 * Clear all wearable actions present on this builder.
9249 * @return this object for method chaining.
9250 * @see #addAction
9251 */
9252 public WearableExtender clearActions() {
9253 mActions.clear();
9254 return this;
9255 }
9256
9257 /**
9258 * Get the wearable actions present on this notification.
9259 */
9260 public List<Action> getActions() {
9261 return mActions;
9262 }
9263
9264 /**
9265 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009266 * this notification. The {@link PendingIntent} provided should be for an activity.
9267 *
9268 * <pre class="prettyprint">
9269 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9270 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9271 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9272 * Notification notif = new Notification.Builder(context)
9273 * .extend(new Notification.WearableExtender()
9274 * .setDisplayIntent(displayPendingIntent)
9275 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9276 * .build();</pre>
9277 *
9278 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009279 * should have an empty task affinity. It is also recommended to use the device
9280 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009281 *
9282 * <p>Example AndroidManifest.xml entry:
9283 * <pre class="prettyprint">
9284 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9285 * android:exported=&quot;true&quot;
9286 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009287 * android:taskAffinity=&quot;&quot;
9288 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009289 *
9290 * @param intent the {@link PendingIntent} for an activity
9291 * @return this object for method chaining
9292 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009293 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009294 */
Gus Prevasd7363752018-09-18 14:35:15 -04009295 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009296 public WearableExtender setDisplayIntent(PendingIntent intent) {
9297 mDisplayIntent = intent;
9298 return this;
9299 }
9300
9301 /**
9302 * Get the intent to launch inside of an activity view when displaying this
9303 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009304 *
9305 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009306 */
Gus Prevasd7363752018-09-18 14:35:15 -04009307 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009308 public PendingIntent getDisplayIntent() {
9309 return mDisplayIntent;
9310 }
9311
9312 /**
9313 * Add an additional page of content to display with this notification. The current
9314 * notification forms the first page, and pages added using this function form
9315 * subsequent pages. This field can be used to separate a notification into multiple
9316 * sections.
9317 *
9318 * @param page the notification to add as another page
9319 * @return this object for method chaining
9320 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009321 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009322 */
Gus Prevasd7363752018-09-18 14:35:15 -04009323 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009324 public WearableExtender addPage(Notification page) {
9325 mPages.add(page);
9326 return this;
9327 }
9328
9329 /**
9330 * Add additional pages of content to display with this notification. The current
9331 * notification forms the first page, and pages added using this function form
9332 * subsequent pages. This field can be used to separate a notification into multiple
9333 * sections.
9334 *
9335 * @param pages a list of notifications
9336 * @return this object for method chaining
9337 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009338 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009339 */
Gus Prevasd7363752018-09-18 14:35:15 -04009340 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009341 public WearableExtender addPages(List<Notification> pages) {
9342 mPages.addAll(pages);
9343 return this;
9344 }
9345
9346 /**
9347 * Clear all additional pages present on this builder.
9348 * @return this object for method chaining.
9349 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009350 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009351 */
Gus Prevasd7363752018-09-18 14:35:15 -04009352 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009353 public WearableExtender clearPages() {
9354 mPages.clear();
9355 return this;
9356 }
9357
9358 /**
9359 * Get the array of additional pages of content for displaying this notification. The
9360 * current notification forms the first page, and elements within this array form
9361 * subsequent pages. This field can be used to separate a notification into multiple
9362 * sections.
9363 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009364 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009365 */
Gus Prevasd7363752018-09-18 14:35:15 -04009366 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009367 public List<Notification> getPages() {
9368 return mPages;
9369 }
9370
9371 /**
9372 * Set a background image to be displayed behind the notification content.
9373 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9374 * will work with any notification style.
9375 *
9376 * @param background the background bitmap
9377 * @return this object for method chaining
9378 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009379 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009380 */
Gus Prevasd7363752018-09-18 14:35:15 -04009381 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009382 public WearableExtender setBackground(Bitmap background) {
9383 mBackground = background;
9384 return this;
9385 }
9386
9387 /**
9388 * Get a background image to be displayed behind the notification content.
9389 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9390 * will work with any notification style.
9391 *
9392 * @return the background image
9393 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009394 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009395 */
Gus Prevasd7363752018-09-18 14:35:15 -04009396 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009397 public Bitmap getBackground() {
9398 return mBackground;
9399 }
9400
9401 /**
9402 * Set an icon that goes with the content of this notification.
9403 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009404 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009405 public WearableExtender setContentIcon(int icon) {
9406 mContentIcon = icon;
9407 return this;
9408 }
9409
9410 /**
9411 * Get an icon that goes with the content of this notification.
9412 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009413 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009414 public int getContentIcon() {
9415 return mContentIcon;
9416 }
9417
9418 /**
9419 * Set the gravity that the content icon should have within the notification display.
9420 * Supported values include {@link android.view.Gravity#START} and
9421 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9422 * @see #setContentIcon
9423 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009424 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009425 public WearableExtender setContentIconGravity(int contentIconGravity) {
9426 mContentIconGravity = contentIconGravity;
9427 return this;
9428 }
9429
9430 /**
9431 * Get the gravity that the content icon should have within the notification display.
9432 * Supported values include {@link android.view.Gravity#START} and
9433 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9434 * @see #getContentIcon
9435 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009436 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009437 public int getContentIconGravity() {
9438 return mContentIconGravity;
9439 }
9440
9441 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009442 * Set an action from this notification's actions as the primary action. If the action has a
9443 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9444 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009445 *
Gus Prevasd7363752018-09-18 14:35:15 -04009446 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009447 * If wearable actions were added to the main notification, this index
9448 * will apply to that list, otherwise it will apply to the regular
9449 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009450 */
9451 public WearableExtender setContentAction(int actionIndex) {
9452 mContentActionIndex = actionIndex;
9453 return this;
9454 }
9455
9456 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009457 * Get the index of the notification action, if any, that was specified as the primary
9458 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009459 *
9460 * <p>If wearable specific actions were added to the main notification, this index will
9461 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009462 *
9463 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009464 */
9465 public int getContentAction() {
9466 return mContentActionIndex;
9467 }
9468
9469 /**
9470 * Set the gravity that this notification should have within the available viewport space.
9471 * Supported values include {@link android.view.Gravity#TOP},
9472 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9473 * The default value is {@link android.view.Gravity#BOTTOM}.
9474 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009475 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009476 public WearableExtender setGravity(int gravity) {
9477 mGravity = gravity;
9478 return this;
9479 }
9480
9481 /**
9482 * Get the gravity that this notification should have within the available viewport space.
9483 * Supported values include {@link android.view.Gravity#TOP},
9484 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9485 * The default value is {@link android.view.Gravity#BOTTOM}.
9486 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009487 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009488 public int getGravity() {
9489 return mGravity;
9490 }
9491
9492 /**
9493 * Set the custom size preset for the display of this notification out of the available
9494 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9495 * {@link #SIZE_LARGE}.
9496 * <p>Some custom size presets are only applicable for custom display notifications created
9497 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9498 * documentation for the preset in question. See also
9499 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9500 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009501 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009502 public WearableExtender setCustomSizePreset(int sizePreset) {
9503 mCustomSizePreset = sizePreset;
9504 return this;
9505 }
9506
9507 /**
9508 * Get the custom size preset for the display of this notification out of the available
9509 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9510 * {@link #SIZE_LARGE}.
9511 * <p>Some custom size presets are only applicable for custom display notifications created
9512 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9513 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9514 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009515 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009516 public int getCustomSizePreset() {
9517 return mCustomSizePreset;
9518 }
9519
9520 /**
9521 * Set the custom height in pixels for the display of this notification's content.
9522 * <p>This option is only available for custom display notifications created
9523 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9524 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9525 * {@link #getCustomContentHeight}.
9526 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009527 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009528 public WearableExtender setCustomContentHeight(int height) {
9529 mCustomContentHeight = height;
9530 return this;
9531 }
9532
9533 /**
9534 * Get the custom height in pixels for the display of this notification's content.
9535 * <p>This option is only available for custom display notifications created
9536 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9537 * {@link #setCustomContentHeight}.
9538 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009539 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009540 public int getCustomContentHeight() {
9541 return mCustomContentHeight;
9542 }
9543
9544 /**
9545 * Set whether the scrolling position for the contents of this notification should start
9546 * at the bottom of the contents instead of the top when the contents are too long to
9547 * display within the screen. Default is false (start scroll at the top).
9548 */
9549 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9550 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9551 return this;
9552 }
9553
9554 /**
9555 * Get whether the scrolling position for the contents of this notification should start
9556 * at the bottom of the contents instead of the top when the contents are too long to
9557 * display within the screen. Default is false (start scroll at the top).
9558 */
9559 public boolean getStartScrollBottom() {
9560 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9561 }
9562
9563 /**
9564 * Set whether the content intent is available when the wearable device is not connected
9565 * to a companion device. The user can still trigger this intent when the wearable device
9566 * is offline, but a visual hint will indicate that the content intent may not be available.
9567 * Defaults to true.
9568 */
9569 public WearableExtender setContentIntentAvailableOffline(
9570 boolean contentIntentAvailableOffline) {
9571 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9572 return this;
9573 }
9574
9575 /**
9576 * Get whether the content intent is available when the wearable device is not connected
9577 * to a companion device. The user can still trigger this intent when the wearable device
9578 * is offline, but a visual hint will indicate that the content intent may not be available.
9579 * Defaults to true.
9580 */
9581 public boolean getContentIntentAvailableOffline() {
9582 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9583 }
9584
9585 /**
9586 * Set a hint that this notification's icon should not be displayed.
9587 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9588 * @return this object for method chaining
9589 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009590 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009591 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9592 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9593 return this;
9594 }
9595
9596 /**
9597 * Get a hint that this notification's icon should not be displayed.
9598 * @return {@code true} if this icon should not be displayed, false otherwise.
9599 * The default value is {@code false} if this was never set.
9600 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009601 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009602 public boolean getHintHideIcon() {
9603 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9604 }
9605
9606 /**
9607 * Set a visual hint that only the background image of this notification should be
9608 * displayed, and other semantic content should be hidden. This hint is only applicable
9609 * to sub-pages added using {@link #addPage}.
9610 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009611 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009612 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9613 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9614 return this;
9615 }
9616
9617 /**
9618 * Get a visual hint that only the background image of this notification should be
9619 * displayed, and other semantic content should be hidden. This hint is only applicable
9620 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9621 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009622 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009623 public boolean getHintShowBackgroundOnly() {
9624 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9625 }
9626
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009627 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009628 * Set a hint that this notification's background should not be clipped if possible,
9629 * and should instead be resized to fully display on the screen, retaining the aspect
9630 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009631 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9632 * @return this object for method chaining
9633 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009634 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009635 public WearableExtender setHintAvoidBackgroundClipping(
9636 boolean hintAvoidBackgroundClipping) {
9637 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9638 return this;
9639 }
9640
9641 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009642 * Get a hint that this notification's background should not be clipped if possible,
9643 * and should instead be resized to fully display on the screen, retaining the aspect
9644 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009645 * @return {@code true} if it's ok if the background is clipped on the screen, false
9646 * otherwise. The default value is {@code false} if this was never set.
9647 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009648 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009649 public boolean getHintAvoidBackgroundClipping() {
9650 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9651 }
9652
9653 /**
9654 * Set a hint that the screen should remain on for at least this duration when
9655 * this notification is displayed on the screen.
9656 * @param timeout The requested screen timeout in milliseconds. Can also be either
9657 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9658 * @return this object for method chaining
9659 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009660 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009661 public WearableExtender setHintScreenTimeout(int timeout) {
9662 mHintScreenTimeout = timeout;
9663 return this;
9664 }
9665
9666 /**
9667 * Get the duration, in milliseconds, that the screen should remain on for
9668 * when this notification is displayed.
9669 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9670 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9671 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009672 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009673 public int getHintScreenTimeout() {
9674 return mHintScreenTimeout;
9675 }
9676
Alex Hills9ab3a232016-04-05 14:54:56 -04009677 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009678 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9679 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9680 * qr codes, as well as other simple black-and-white tickets.
9681 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9682 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009683 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009684 */
Gus Prevasd7363752018-09-18 14:35:15 -04009685 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009686 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9687 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9688 return this;
9689 }
9690
9691 /**
9692 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9693 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9694 * qr codes, as well as other simple black-and-white tickets.
9695 * @return {@code true} if it should be displayed in ambient, false otherwise
9696 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009697 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009698 */
Gus Prevasd7363752018-09-18 14:35:15 -04009699 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009700 public boolean getHintAmbientBigPicture() {
9701 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9702 }
9703
9704 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009705 * Set a hint that this notification's content intent will launch an {@link Activity}
9706 * directly, telling the platform that it can generate the appropriate transitions.
9707 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9708 * an activity and transitions should be generated, false otherwise.
9709 * @return this object for method chaining
9710 */
9711 public WearableExtender setHintContentIntentLaunchesActivity(
9712 boolean hintContentIntentLaunchesActivity) {
9713 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9714 return this;
9715 }
9716
9717 /**
9718 * Get a hint that this notification's content intent will launch an {@link Activity}
9719 * directly, telling the platform that it can generate the appropriate transitions
9720 * @return {@code true} if the content intent will launch an activity and transitions should
9721 * be generated, false otherwise. The default value is {@code false} if this was never set.
9722 */
9723 public boolean getHintContentIntentLaunchesActivity() {
9724 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9725 }
9726
Nadia Benbernou948627e2016-04-14 14:41:08 -04009727 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009728 * Sets the dismissal id for this notification. If a notification is posted with a
9729 * dismissal id, then when that notification is canceled, notifications on other wearables
9730 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009731 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009732 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009733 * @param dismissalId the dismissal id of the notification.
9734 * @return this object for method chaining
9735 */
9736 public WearableExtender setDismissalId(String dismissalId) {
9737 mDismissalId = dismissalId;
9738 return this;
9739 }
9740
9741 /**
9742 * Returns the dismissal id of the notification.
9743 * @return the dismissal id of the notification or null if it has not been set.
9744 */
9745 public String getDismissalId() {
9746 return mDismissalId;
9747 }
9748
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009749 /**
9750 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9751 * posted from a phone to provide finer-grained control on what notifications are bridged
9752 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9753 * Features to Notifications</a> for more information.
9754 * @param bridgeTag the bridge tag of the notification.
9755 * @return this object for method chaining
9756 */
9757 public WearableExtender setBridgeTag(String bridgeTag) {
9758 mBridgeTag = bridgeTag;
9759 return this;
9760 }
9761
9762 /**
9763 * Returns the bridge tag of the notification.
9764 * @return the bridge tag or null if not present.
9765 */
9766 public String getBridgeTag() {
9767 return mBridgeTag;
9768 }
9769
Griff Hazen61a9e862014-05-22 16:05:19 -07009770 private void setFlag(int mask, boolean value) {
9771 if (value) {
9772 mFlags |= mask;
9773 } else {
9774 mFlags &= ~mask;
9775 }
9776 }
9777 }
9778
9779 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009780 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9781 * with car extensions:
9782 *
9783 * <ol>
9784 * <li>Create an {@link Notification.Builder}, setting any desired
9785 * properties.
9786 * <li>Create a {@link CarExtender}.
9787 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9788 * {@link CarExtender}.
9789 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9790 * to apply the extensions to a notification.
9791 * </ol>
9792 *
9793 * <pre class="prettyprint">
9794 * Notification notification = new Notification.Builder(context)
9795 * ...
9796 * .extend(new CarExtender()
9797 * .set*(...))
9798 * .build();
9799 * </pre>
9800 *
9801 * <p>Car extensions can be accessed on an existing notification by using the
9802 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9803 * to access values.
9804 */
9805 public static final class CarExtender implements Extender {
9806 private static final String TAG = "CarExtender";
9807
9808 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9809 private static final String EXTRA_LARGE_ICON = "large_icon";
9810 private static final String EXTRA_CONVERSATION = "car_conversation";
9811 private static final String EXTRA_COLOR = "app_color";
9812
9813 private Bitmap mLargeIcon;
9814 private UnreadConversation mUnreadConversation;
9815 private int mColor = Notification.COLOR_DEFAULT;
9816
9817 /**
9818 * Create a {@link CarExtender} with default options.
9819 */
9820 public CarExtender() {
9821 }
9822
9823 /**
9824 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9825 *
9826 * @param notif The notification from which to copy options.
9827 */
9828 public CarExtender(Notification notif) {
9829 Bundle carBundle = notif.extras == null ?
9830 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9831 if (carBundle != null) {
9832 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9833 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9834
9835 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9836 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9837 }
9838 }
9839
9840 /**
9841 * Apply car extensions to a notification that is being built. This is typically called by
9842 * the {@link Notification.Builder#extend(Notification.Extender)}
9843 * method of {@link Notification.Builder}.
9844 */
9845 @Override
9846 public Notification.Builder extend(Notification.Builder builder) {
9847 Bundle carExtensions = new Bundle();
9848
9849 if (mLargeIcon != null) {
9850 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9851 }
9852 if (mColor != Notification.COLOR_DEFAULT) {
9853 carExtensions.putInt(EXTRA_COLOR, mColor);
9854 }
9855
9856 if (mUnreadConversation != null) {
9857 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9858 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9859 }
9860
9861 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9862 return builder;
9863 }
9864
9865 /**
9866 * Sets the accent color to use when Android Auto presents the notification.
9867 *
9868 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9869 * to accent the displayed notification. However, not all colors are acceptable in an
9870 * automotive setting. This method can be used to override the color provided in the
9871 * notification in such a situation.
9872 */
Tor Norbye80756e32015-03-02 09:39:27 -08009873 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009874 mColor = color;
9875 return this;
9876 }
9877
9878 /**
9879 * Gets the accent color.
9880 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009881 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009882 */
Tor Norbye80756e32015-03-02 09:39:27 -08009883 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009884 public int getColor() {
9885 return mColor;
9886 }
9887
9888 /**
9889 * Sets the large icon of the car notification.
9890 *
9891 * If no large icon is set in the extender, Android Auto will display the icon
9892 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9893 *
9894 * @param largeIcon The large icon to use in the car notification.
9895 * @return This object for method chaining.
9896 */
9897 public CarExtender setLargeIcon(Bitmap largeIcon) {
9898 mLargeIcon = largeIcon;
9899 return this;
9900 }
9901
9902 /**
9903 * Gets the large icon used in this car notification, or null if no icon has been set.
9904 *
9905 * @return The large icon for the car notification.
9906 * @see CarExtender#setLargeIcon
9907 */
9908 public Bitmap getLargeIcon() {
9909 return mLargeIcon;
9910 }
9911
9912 /**
9913 * Sets the unread conversation in a message notification.
9914 *
9915 * @param unreadConversation The unread part of the conversation this notification conveys.
9916 * @return This object for method chaining.
9917 */
9918 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9919 mUnreadConversation = unreadConversation;
9920 return this;
9921 }
9922
9923 /**
9924 * Returns the unread conversation conveyed by this notification.
9925 * @see #setUnreadConversation(UnreadConversation)
9926 */
9927 public UnreadConversation getUnreadConversation() {
9928 return mUnreadConversation;
9929 }
9930
9931 /**
9932 * A class which holds the unread messages from a conversation.
9933 */
9934 public static class UnreadConversation {
9935 private static final String KEY_AUTHOR = "author";
9936 private static final String KEY_TEXT = "text";
9937 private static final String KEY_MESSAGES = "messages";
9938 private static final String KEY_REMOTE_INPUT = "remote_input";
9939 private static final String KEY_ON_REPLY = "on_reply";
9940 private static final String KEY_ON_READ = "on_read";
9941 private static final String KEY_PARTICIPANTS = "participants";
9942 private static final String KEY_TIMESTAMP = "timestamp";
9943
9944 private final String[] mMessages;
9945 private final RemoteInput mRemoteInput;
9946 private final PendingIntent mReplyPendingIntent;
9947 private final PendingIntent mReadPendingIntent;
9948 private final String[] mParticipants;
9949 private final long mLatestTimestamp;
9950
9951 UnreadConversation(String[] messages, RemoteInput remoteInput,
9952 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9953 String[] participants, long latestTimestamp) {
9954 mMessages = messages;
9955 mRemoteInput = remoteInput;
9956 mReadPendingIntent = readPendingIntent;
9957 mReplyPendingIntent = replyPendingIntent;
9958 mParticipants = participants;
9959 mLatestTimestamp = latestTimestamp;
9960 }
9961
9962 /**
9963 * Gets the list of messages conveyed by this notification.
9964 */
9965 public String[] getMessages() {
9966 return mMessages;
9967 }
9968
9969 /**
9970 * Gets the remote input that will be used to convey the response to a message list, or
9971 * null if no such remote input exists.
9972 */
9973 public RemoteInput getRemoteInput() {
9974 return mRemoteInput;
9975 }
9976
9977 /**
9978 * Gets the pending intent that will be triggered when the user replies to this
9979 * notification.
9980 */
9981 public PendingIntent getReplyPendingIntent() {
9982 return mReplyPendingIntent;
9983 }
9984
9985 /**
9986 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9987 * in this object's message list.
9988 */
9989 public PendingIntent getReadPendingIntent() {
9990 return mReadPendingIntent;
9991 }
9992
9993 /**
9994 * Gets the participants in the conversation.
9995 */
9996 public String[] getParticipants() {
9997 return mParticipants;
9998 }
9999
10000 /**
10001 * Gets the firs participant in the conversation.
10002 */
10003 public String getParticipant() {
10004 return mParticipants.length > 0 ? mParticipants[0] : null;
10005 }
10006
10007 /**
10008 * Gets the timestamp of the conversation.
10009 */
10010 public long getLatestTimestamp() {
10011 return mLatestTimestamp;
10012 }
10013
10014 Bundle getBundleForUnreadConversation() {
10015 Bundle b = new Bundle();
10016 String author = null;
10017 if (mParticipants != null && mParticipants.length > 1) {
10018 author = mParticipants[0];
10019 }
10020 Parcelable[] messages = new Parcelable[mMessages.length];
10021 for (int i = 0; i < messages.length; i++) {
10022 Bundle m = new Bundle();
10023 m.putString(KEY_TEXT, mMessages[i]);
10024 m.putString(KEY_AUTHOR, author);
10025 messages[i] = m;
10026 }
10027 b.putParcelableArray(KEY_MESSAGES, messages);
10028 if (mRemoteInput != null) {
10029 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
10030 }
10031 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
10032 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
10033 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
10034 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
10035 return b;
10036 }
10037
10038 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
10039 if (b == null) {
10040 return null;
10041 }
10042 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
10043 String[] messages = null;
10044 if (parcelableMessages != null) {
10045 String[] tmp = new String[parcelableMessages.length];
10046 boolean success = true;
10047 for (int i = 0; i < tmp.length; i++) {
10048 if (!(parcelableMessages[i] instanceof Bundle)) {
10049 success = false;
10050 break;
10051 }
10052 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10053 if (tmp[i] == null) {
10054 success = false;
10055 break;
10056 }
10057 }
10058 if (success) {
10059 messages = tmp;
10060 } else {
10061 return null;
10062 }
10063 }
10064
10065 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10066 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10067
10068 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10069
10070 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10071 if (participants == null || participants.length != 1) {
10072 return null;
10073 }
10074
10075 return new UnreadConversation(messages,
10076 remoteInput,
10077 onReply,
10078 onRead,
10079 participants, b.getLong(KEY_TIMESTAMP));
10080 }
10081 };
10082
10083 /**
10084 * Builder class for {@link CarExtender.UnreadConversation} objects.
10085 */
10086 public static class Builder {
10087 private final List<String> mMessages = new ArrayList<String>();
10088 private final String mParticipant;
10089 private RemoteInput mRemoteInput;
10090 private PendingIntent mReadPendingIntent;
10091 private PendingIntent mReplyPendingIntent;
10092 private long mLatestTimestamp;
10093
10094 /**
10095 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10096 *
10097 * @param name The name of the other participant in the conversation.
10098 */
10099 public Builder(String name) {
10100 mParticipant = name;
10101 }
10102
10103 /**
10104 * Appends a new unread message to the list of messages for this conversation.
10105 *
10106 * The messages should be added from oldest to newest.
10107 *
10108 * @param message The text of the new unread message.
10109 * @return This object for method chaining.
10110 */
10111 public Builder addMessage(String message) {
10112 mMessages.add(message);
10113 return this;
10114 }
10115
10116 /**
10117 * Sets the pending intent and remote input which will convey the reply to this
10118 * notification.
10119 *
10120 * @param pendingIntent The pending intent which will be triggered on a reply.
10121 * @param remoteInput The remote input parcelable which will carry the reply.
10122 * @return This object for method chaining.
10123 *
10124 * @see CarExtender.UnreadConversation#getRemoteInput
10125 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10126 */
10127 public Builder setReplyAction(
10128 PendingIntent pendingIntent, RemoteInput remoteInput) {
10129 mRemoteInput = remoteInput;
10130 mReplyPendingIntent = pendingIntent;
10131
10132 return this;
10133 }
10134
10135 /**
10136 * Sets the pending intent that will be sent once the messages in this notification
10137 * are read.
10138 *
10139 * @param pendingIntent The pending intent to use.
10140 * @return This object for method chaining.
10141 */
10142 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10143 mReadPendingIntent = pendingIntent;
10144 return this;
10145 }
10146
10147 /**
10148 * Sets the timestamp of the most recent message in an unread conversation.
10149 *
10150 * If a messaging notification has been posted by your application and has not
10151 * yet been cancelled, posting a later notification with the same id and tag
10152 * but without a newer timestamp may result in Android Auto not displaying a
10153 * heads up notification for the later notification.
10154 *
10155 * @param timestamp The timestamp of the most recent message in the conversation.
10156 * @return This object for method chaining.
10157 */
10158 public Builder setLatestTimestamp(long timestamp) {
10159 mLatestTimestamp = timestamp;
10160 return this;
10161 }
10162
10163 /**
10164 * Builds a new unread conversation object.
10165 *
10166 * @return The new unread conversation object.
10167 */
10168 public UnreadConversation build() {
10169 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10170 String[] participants = { mParticipant };
10171 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10172 mReadPendingIntent, participants, mLatestTimestamp);
10173 }
10174 }
10175 }
10176
10177 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010178 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10179 * with a TV extension:
10180 *
10181 * <ol>
10182 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10183 * <li>Create a {@link TvExtender}.
10184 * <li>Set TV-specific properties using the {@code set} methods of
10185 * {@link TvExtender}.
10186 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10187 * to apply the extension to a notification.
10188 * </ol>
10189 *
10190 * <pre class="prettyprint">
10191 * Notification notification = new Notification.Builder(context)
10192 * ...
10193 * .extend(new TvExtender()
10194 * .set*(...))
10195 * .build();
10196 * </pre>
10197 *
10198 * <p>TV extensions can be accessed on an existing notification by using the
10199 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10200 * to access values.
10201 *
10202 * @hide
10203 */
10204 @SystemApi
10205 public static final class TvExtender implements Extender {
10206 private static final String TAG = "TvExtender";
10207
10208 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10209 private static final String EXTRA_FLAGS = "flags";
10210 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10211 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010212 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010213 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010214
10215 // Flags bitwise-ored to mFlags
10216 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10217
10218 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010219 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010220 private PendingIntent mContentIntent;
10221 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010222 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010223
10224 /**
10225 * Create a {@link TvExtender} with default options.
10226 */
10227 public TvExtender() {
10228 mFlags = FLAG_AVAILABLE_ON_TV;
10229 }
10230
10231 /**
10232 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10233 *
10234 * @param notif The notification from which to copy options.
10235 */
10236 public TvExtender(Notification notif) {
10237 Bundle bundle = notif.extras == null ?
10238 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10239 if (bundle != null) {
10240 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010241 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010242 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010243 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10244 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10245 }
10246 }
10247
10248 /**
10249 * Apply a TV extension to a notification that is being built. This is typically called by
10250 * the {@link Notification.Builder#extend(Notification.Extender)}
10251 * method of {@link Notification.Builder}.
10252 */
10253 @Override
10254 public Notification.Builder extend(Notification.Builder builder) {
10255 Bundle bundle = new Bundle();
10256
10257 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010258 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010259 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010260 if (mContentIntent != null) {
10261 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10262 }
10263
10264 if (mDeleteIntent != null) {
10265 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10266 }
10267
10268 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10269 return builder;
10270 }
10271
10272 /**
10273 * Returns true if this notification should be shown on TV. This method return true
10274 * if the notification was extended with a TvExtender.
10275 */
10276 public boolean isAvailableOnTv() {
10277 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10278 }
10279
10280 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010281 * Specifies the channel the notification should be delivered on when shown on TV.
10282 * It can be different from the channel that the notification is delivered to when
10283 * posting on a non-TV device.
10284 */
10285 public TvExtender setChannel(String channelId) {
10286 mChannelId = channelId;
10287 return this;
10288 }
10289
10290 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010291 * Specifies the channel the notification should be delivered on when shown on TV.
10292 * It can be different from the channel that the notification is delivered to when
10293 * posting on a non-TV device.
10294 */
10295 public TvExtender setChannelId(String channelId) {
10296 mChannelId = channelId;
10297 return this;
10298 }
10299
Jeff Sharkey000ce802017-04-29 13:13:27 -060010300 /** @removed */
10301 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010302 public String getChannel() {
10303 return mChannelId;
10304 }
10305
10306 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010307 * Returns the id of the channel this notification posts to on TV.
10308 */
10309 public String getChannelId() {
10310 return mChannelId;
10311 }
10312
10313 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010314 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10315 * If provided, it is used instead of the content intent specified
10316 * at the level of Notification.
10317 */
10318 public TvExtender setContentIntent(PendingIntent intent) {
10319 mContentIntent = intent;
10320 return this;
10321 }
10322
10323 /**
10324 * Returns the TV-specific content intent. If this method returns null, the
10325 * main content intent on the notification should be used.
10326 *
10327 * @see {@link Notification#contentIntent}
10328 */
10329 public PendingIntent getContentIntent() {
10330 return mContentIntent;
10331 }
10332
10333 /**
10334 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10335 * by the user on TV. If provided, it is used instead of the delete intent specified
10336 * at the level of Notification.
10337 */
10338 public TvExtender setDeleteIntent(PendingIntent intent) {
10339 mDeleteIntent = intent;
10340 return this;
10341 }
10342
10343 /**
10344 * Returns the TV-specific delete intent. If this method returns null, the
10345 * main delete intent on the notification should be used.
10346 *
10347 * @see {@link Notification#deleteIntent}
10348 */
10349 public PendingIntent getDeleteIntent() {
10350 return mDeleteIntent;
10351 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010352
10353 /**
10354 * Specifies whether this notification should suppress showing a message over top of apps
10355 * outside of the launcher.
10356 */
10357 public TvExtender setSuppressShowOverApps(boolean suppress) {
10358 mSuppressShowOverApps = suppress;
10359 return this;
10360 }
10361
10362 /**
10363 * Returns true if this notification should not show messages over top of apps
10364 * outside of the launcher.
10365 */
10366 public boolean getSuppressShowOverApps() {
10367 return mSuppressShowOverApps;
10368 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010369 }
10370
10371 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010372 * Get an array of Notification objects from a parcelable array bundle field.
10373 * Update the bundle to have a typed array so fetches in the future don't need
10374 * to do an array copy.
10375 */
10376 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10377 Parcelable[] array = bundle.getParcelableArray(key);
10378 if (array instanceof Notification[] || array == null) {
10379 return (Notification[]) array;
10380 }
10381 Notification[] typedArray = Arrays.copyOf(array, array.length,
10382 Notification[].class);
10383 bundle.putParcelableArray(key, typedArray);
10384 return typedArray;
10385 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010386
10387 private static class BuilderRemoteViews extends RemoteViews {
10388 public BuilderRemoteViews(Parcel parcel) {
10389 super(parcel);
10390 }
10391
Kenny Guy77320062014-08-27 21:37:15 +010010392 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10393 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010394 }
10395
10396 @Override
10397 public BuilderRemoteViews clone() {
10398 Parcel p = Parcel.obtain();
10399 writeToParcel(p, 0);
10400 p.setDataPosition(0);
10401 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10402 p.recycle();
10403 return brv;
10404 }
10405 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010406
Selim Cinek384804b2018-04-18 14:31:07 +080010407 /**
10408 * A result object where information about the template that was created is saved.
10409 */
10410 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010411 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010412 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010413
10414 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010415 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010416 * with the right actions.
10417 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010418 public int getIconMarginEnd() {
10419 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010420 }
10421
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010422 /**
10423 * Is the icon container visible on the right size because of the reply button or the
10424 * right icon.
10425 */
10426 public boolean isRightIconContainerVisible() {
10427 return mRightIconContainerVisible;
10428 }
10429
Selim Cinek1c72fa02018-04-23 18:00:54 +080010430 public void setIconMarginEnd(int iconMarginEnd) {
10431 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010432 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010433
10434 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10435 mRightIconContainerVisible = iconContainerVisible;
10436 }
Selim Cinek384804b2018-04-18 14:31:07 +080010437 }
10438
Adrian Roos70d7aa32017-01-11 15:39:06 -080010439 private static class StandardTemplateParams {
10440 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010441 CharSequence title;
10442 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010443 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010444 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010445 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010446 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010447 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010448 boolean allowColorization = true;
10449 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010450
10451 final StandardTemplateParams reset() {
10452 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010453 title = null;
10454 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010455 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010456 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010457 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010458 allowColorization = true;
10459 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010460 return this;
10461 }
10462
10463 final StandardTemplateParams hasProgress(boolean hasProgress) {
10464 this.hasProgress = hasProgress;
10465 return this;
10466 }
10467
10468 final StandardTemplateParams title(CharSequence title) {
10469 this.title = title;
10470 return this;
10471 }
10472
10473 final StandardTemplateParams text(CharSequence text) {
10474 this.text = text;
10475 return this;
10476 }
10477
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010478 final StandardTemplateParams summaryText(CharSequence text) {
10479 this.summaryText = text;
10480 return this;
10481 }
10482
Selim Cinekafeed292017-12-12 17:32:44 -080010483 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10484 this.headerTextSecondary = text;
10485 return this;
10486 }
10487
Selim Cinek384804b2018-04-18 14:31:07 +080010488 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10489 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010490 return this;
10491 }
10492
Selim Cinek384804b2018-04-18 14:31:07 +080010493 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10494 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010495 return this;
10496 }
10497
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010498 final StandardTemplateParams disallowColorization() {
10499 this.allowColorization = false;
10500 return this;
10501 }
10502
10503 final StandardTemplateParams forceDefaultColor() {
10504 this.forceDefaultColor = true;
10505 return this;
10506 }
10507
Adrian Roos70d7aa32017-01-11 15:39:06 -080010508 final StandardTemplateParams fillTextsFrom(Builder b) {
10509 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010510 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin7b4d8852019-09-23 17:07:35 -070010511 this.text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT));
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010512 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010513 return this;
10514 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010515
10516 /**
10517 * Set the maximum lines of remote input history lines allowed.
10518 * @param maxRemoteInputHistory The number of lines.
10519 * @return The builder for method chaining.
10520 */
10521 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10522 this.maxRemoteInputHistory = maxRemoteInputHistory;
10523 return this;
10524 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526}